aboutsummaryrefslogtreecommitdiff
path: root/src/op/numeric
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-11-26 22:43:30 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-04-21 01:51:24 +0200
commitf25b015e5b668028c34974bbb22faa4105c26690 (patch)
tree28f2b08c17b3585d06694ad74004d0617eadb785 /src/op/numeric
downloadnanowasm-sync-f25b015e5b668028c34974bbb22faa4105c26690.tar.gz
First commit
Diffstat (limited to 'src/op/numeric')
-rw-r--r--src/op/numeric/CMakeLists.txt10
-rw-r--r--src/op/numeric/i32_sub.c66
2 files changed, 76 insertions, 0 deletions
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 <nw/log.h>
+#include <nw/ops.h>
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/types.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+/* 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;
+}