From f25b015e5b668028c34974bbb22faa4105c26690 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sun, 26 Nov 2023 22:43:30 +0100 Subject: First commit --- src/op/numeric/CMakeLists.txt | 10 +++++++ src/op/numeric/i32_sub.c | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/op/numeric/CMakeLists.txt create mode 100644 src/op/numeric/i32_sub.c (limited to 'src/op/numeric') diff --git a/src/op/numeric/CMakeLists.txt b/src/op/numeric/CMakeLists.txt new file mode 100644 index 0000000..a259506 --- /dev/null +++ b/src/op/numeric/CMakeLists.txt @@ -0,0 +1,10 @@ +# nanowasm, a tiny WebAssembly/Wasm interpreter +# Copyright (C) 2023-2024 Xavier Del Campo Romero +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +target_sources(${PROJECT_NAME} PRIVATE + i32_sub.c +) diff --git a/src/op/numeric/i32_sub.c b/src/op/numeric/i32_sub.c new file mode 100644 index 0000000..c3d1352 --- /dev/null +++ b/src/op/numeric/i32_sub.c @@ -0,0 +1,66 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2024 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Inspired by "INT32-C. Ensure that operations on signed integers do + * not result in overflow". */ +static bool signed_overflow(const int32_t a, const int32_t b) +{ + return (b > 0 && a < INT32_MIN + b) + || (b < 0 && a > INT32_MAX + b); +} + +int op_i32_sub(struct nw_interp *const i) +{ + int32_t op1, op2; + + if (interp_stack_pop(i, &op2, sizeof op2)) + { + LOG("%s: interp_stack_pop %s failed\n", __func__, "op2"); + return -1; + } + else if (interp_stack_pop(i, &op1, sizeof op1)) + { + LOG("%s: interp_stack_pop %s failed\n", __func__, "op1"); + return -1; + } + else if (signed_overflow(op2, op1)) + { + LOG("%s: signed integer overflow (op1=%" PRIi32 ", op2=%" PRIi32 ")\n", + __func__, op1, op2); + i->exception = "signed integer overflow"; + return 1; + } + + const int32_t result = op2 - op1; + + if (interp_stack_push(i, &result, sizeof result)) + { + LOG("%s: interp_stack_push failed\n", __func__); + return -1; + } + + return 0; +} + +int check_i32_sub(FILE *const f) +{ + return 0; +} -- cgit v1.2.3