aboutsummaryrefslogtreecommitdiff
path: root/src/op
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-05-22 14:04:36 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-06-12 13:38:05 +0200
commit4f9a2c7a2d8464b04cc08075a7762c6d457090df (patch)
treeae8fe229a3a5ba60d08b74299d0c1850685bda86 /src/op
parentf25b015e5b668028c34974bbb22faa4105c26690 (diff)
downloadnanowasm-sync-4f9a2c7a2d8464b04cc08075a7762c6d457090df.tar.gz
WIP
Diffstat (limited to 'src/op')
-rw-r--r--src/op/call/call.c7
-rw-r--r--src/op/memory/i32_load.c9
-rw-r--r--src/op/memory/i32_store.c5
-rw-r--r--src/op/variable_access/CMakeLists.txt1
-rw-r--r--src/op/variable_access/get_local.c19
-rw-r--r--src/op/variable_access/set_local.c65
6 files changed, 57 insertions, 49 deletions
diff --git a/src/op/call/call.c b/src/op/call/call.c
index 19355ca..4c970c5 100644
--- a/src/op/call/call.c
+++ b/src/op/call/call.c
@@ -36,16 +36,11 @@ static int call(struct nw_interp *const i, const varuint32 function_index)
LOG("%s: section_type_push failed\n", __func__);
return -1;
}
- else if (section_code_push(f, fn.start, &fr))
+ else if (section_code_push(i, fn.start))
{
LOG("%s: section_code_push failed\n", __func__);
return -1;
}
- else if (interp_push(i, &fr))
- {
- LOG("%s: interp_push failed\n", __func__);
- return -1;
- }
return 0;
}
diff --git a/src/op/memory/i32_load.c b/src/op/memory/i32_load.c
index 04dbec7..ad5abe5 100644
--- a/src/op/memory/i32_load.c
+++ b/src/op/memory/i32_load.c
@@ -19,7 +19,7 @@ static int load(struct nw_interp *const i, const varuint32 flags,
const varuint32 offset)
{
enum {ALIGN = 2};
- int32_t value;
+ int32_t value_le;
if (flags != ALIGN)
{
@@ -28,12 +28,15 @@ static int load(struct nw_interp *const i, const varuint32 flags,
i->exception = "unaligned access";
return -1;
}
- else if (interp_heap_load(i, offset, &value, sizeof value))
+ else if (interp_heap_load(i, offset, &value_le, sizeof value_le))
{
LOG("%s: interp_heap_load failed\n", __func__);
return -1;
}
- else if (interp_stack_push(i, &value, sizeof value))
+
+ const int32_t value = ntohi32(value_le);
+
+ if (interp_stack_push(i, &value, sizeof value))
{
LOG("%s: interp_stack_push failed\n", __func__);
return -1;
diff --git a/src/op/memory/i32_store.c b/src/op/memory/i32_store.c
index 212cd86..9f5351b 100644
--- a/src/op/memory/i32_store.c
+++ b/src/op/memory/i32_store.c
@@ -49,7 +49,10 @@ static int store(struct nw_interp *const i, const varuint32 flags,
i->exception = exc;
return -1;
}
- else if (interp_heap_store(i, offset + addr, &value, sizeof value))
+
+ const int32_t value_le = htoni32(value);
+
+ if (interp_heap_store(i, offset + addr, &value_le, sizeof value_le))
{
LOG("%s: interp_heap_store failed\n", __func__);
return -1;
diff --git a/src/op/variable_access/CMakeLists.txt b/src/op/variable_access/CMakeLists.txt
index 314b5bf..181f330 100644
--- a/src/op/variable_access/CMakeLists.txt
+++ b/src/op/variable_access/CMakeLists.txt
@@ -6,6 +6,7 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
target_sources(${PROJECT_NAME} PRIVATE
+ common.c
get_global.c
get_local.c
set_global.c
diff --git a/src/op/variable_access/get_local.c b/src/op/variable_access/get_local.c
index 745a2e7..7c80450 100644
--- a/src/op/variable_access/get_local.c
+++ b/src/op/variable_access/get_local.c
@@ -51,22 +51,17 @@ static int get_f64(struct nw_interp *const i, const void *const data,
static int get_local(const varuint32 local_index, struct nw_interp *const i)
{
- struct nw_frame *fr;
+ struct nw_locals *l;
varuint32 idx;
- for (idx = local_index, fr = i->fp; fr; fr = fr->next)
- if (idx < fr->n_locals)
- break;
-
- if (!fr)
+ if (locals_get(local_index, i, &l, &idx))
{
- LOG("%s: cannot access local variable %lu\n", __func__,
- (unsigned long)local_index);
- i->exception = "local variable index out of bounds";
+ LOG("%s: locals_get failed\n", __func__);
return -1;
}
- static int (*const get[])(struct nw_interp *, const void *, varuint32) =
+ static int (*const get[])(struct nw_interp *, const void *,
+ varuint32) =
{
[VALUE_TYPE_I32] = get_i32,
[VALUE_TYPE_I64] = get_i64,
@@ -74,9 +69,9 @@ static int get_local(const varuint32 local_index, struct nw_interp *const i)
[VALUE_TYPE_F64] = get_f64,
};
- if (get[fr->local_type](i, fr + 1, idx))
+ if (get[l->type](i, l + 1, idx))
{
- LOG("%s: get type %d failed\n", __func__, fr->local_type);
+ LOG("%s: get type %d failed\n", __func__, l->type);
return -1;
}
diff --git a/src/op/variable_access/set_local.c b/src/op/variable_access/set_local.c
index 8cf6440..d9ea884 100644
--- a/src/op/variable_access/set_local.c
+++ b/src/op/variable_access/set_local.c
@@ -7,6 +7,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
+#include <nw/interp.h>
#include <nw/log.h>
#include <nw/ops.h>
#include <nanowasm/nw.h>
@@ -20,61 +21,71 @@
static int set_i32(struct nw_interp *const i, void *const data,
const varuint32 idx)
{
- const void *const stackptr = (const char *)i->cfg.stack.buf + i->stack_i;
- const int32_t *const src = (const int32_t *)stackptr - 1;
- int32_t *const dst = (int32_t *)data + idx;
+ int32_t *const a = data, v;
- *dst = *src;
+ if (interp_stack_pop(i, &v, sizeof v))
+ {
+ LOG("%s: interp_stack_pop failed\n", __func__);
+ return -1;
+ }
+
+ a[idx] = v;
return 0;
}
static int set_i64(struct nw_interp *const i, void *const data,
const varuint32 idx)
{
- const void *const stackptr = (const char *)i->cfg.stack.buf + i->stack_i;
- const int64_t *const src = (const int64_t *)stackptr - 1;
- int64_t *const dst = (int64_t *)data + idx;
+ int64_t *const a = data, v;
- *dst = *src;
+ if (interp_stack_pop(i, &v, sizeof v))
+ {
+ LOG("%s: interp_stack_pop failed\n", __func__);
+ return -1;
+ }
+
+ a[idx] = v;
return 0;
}
static int set_f32(struct nw_interp *const i, void *const data,
const varuint32 idx)
{
- const void *const stackptr = (const char *)i->cfg.stack.buf + i->stack_i;
- const float *const src = (const float *)stackptr - 1;
- float *const dst = (float *)data + idx;
+ float *const a = data, v;
- *dst = *src;
+ if (interp_stack_pop(i, &v, sizeof v))
+ {
+ LOG("%s: interp_stack_pop failed\n", __func__);
+ return -1;
+ }
+
+ a[idx] = v;
return 0;
}
static int set_f64(struct nw_interp *const i, void *const data,
const varuint32 idx)
{
- const void *const stackptr = (const char *)i->cfg.stack.buf + i->stack_i;
- const double *const src = (const double *)stackptr - 1;
- double *const dst = (double *)data + idx;
+ double *const a = data, v;
+
+ if (interp_stack_pop(i, &v, sizeof v))
+ {
+ LOG("%s: interp_stack_pop failed\n", __func__);
+ return -1;
+ }
- *dst = *src;
+ a[idx] = v;
return 0;
}
static int set_local(const varuint32 local_index, struct nw_interp *const i)
{
- struct nw_frame *fr;
varuint32 idx;
+ struct nw_locals *l;
- for (idx = local_index, fr = i->fp; fr; fr = fr->next)
- if (idx < fr->n_locals)
- break;
-
- if (!fr)
+ if (!locals_get(local_index, i, &l, &idx))
{
- LOG("%s: cannot access local variable %lu\n", __func__,
- (unsigned long)local_index);
- i->exception = "local variable index out of bounds";
+ LOG("%s: get_pos failed\n", __func__);
return -1;
}
@@ -86,9 +97,9 @@ static int set_local(const varuint32 local_index, struct nw_interp *const i)
[VALUE_TYPE_F64] = set_f64,
};
- if (set[fr->local_type](i, fr + 1, idx))
+ if (set[l->type](i, l + 1, idx))
{
- LOG("%s: set type %d failed\n", __func__, fr->local_type);
+ LOG("%s: set type %d failed\n", __func__, l->type);
return -1;
}