aboutsummaryrefslogtreecommitdiff
path: root/src/section/global.c
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/section/global.c
parentf25b015e5b668028c34974bbb22faa4105c26690 (diff)
downloadnanowasm-sync-4f9a2c7a2d8464b04cc08075a7762c6d457090df.tar.gz
WIP
Diffstat (limited to 'src/section/global.c')
-rw-r--r--src/section/global.c103
1 files changed, 39 insertions, 64 deletions
diff --git a/src/section/global.c b/src/section/global.c
index eff2e0b..4f3a2ee 100644
--- a/src/section/global.c
+++ b/src/section/global.c
@@ -30,66 +30,44 @@ struct global_type
} value;
};
-static int get_i32(const struct nw_interp *const i,
- union global_value *const value)
+static int get_global(struct nw_interp *const i, void *const dst,
+ const size_t n)
{
- if (i->stack_i != sizeof value->i32)
+ if (i->stack_i != n)
{
LOG("%s: expected %zu used stack bytes, got %zu\n",
- __func__, i->stack_i, sizeof value->i32);
+ __func__, i->stack_i, n);
+ i->exception = "unexpected stack consumption";
return -1;
}
- value->i32 = *(const int32_t *)i->cfg.stack.buf;
- return 0;
+ return interp_global_pop(i, dst, n);
}
-static int get_i64(const struct nw_interp *const i,
- union global_value *const value)
+static int get_i32(struct nw_interp *const i, union global_value *const value)
{
- if (i->stack_i != sizeof value->i64)
- {
- LOG("%s: expected %zu used stack bytes, got %zu\n",
- __func__, i->stack_i, sizeof value->i64);
- return -1;
- }
-
- value->i64 = *(const int64_t *)i->cfg.stack.buf;
- return 0;
+ return get_global(i, &value->i32, sizeof value->i32);
}
-static int get_f32(const struct nw_interp *const i,
- union global_value *const value)
+static int get_i64(struct nw_interp *const i, union global_value *const value)
{
- if (i->stack_i != sizeof value->f32)
- {
- LOG("%s: expected %zu used stack bytes, got %zu\n",
- __func__, i->stack_i, sizeof value->f32);
- return -1;
- }
-
- value->f32 = *(const float *)i->cfg.stack.buf;
- return 0;
+ return get_global(i, &value->i64, sizeof value->i64);
}
-static int get_f64(const struct nw_interp *const i,
- union global_value *const value)
+static int get_f32(struct nw_interp *const i, union global_value *const value)
{
- if (i->stack_i != sizeof value->f64)
- {
- LOG("%s: expected %zu used stack bytes, got %zu\n",
- __func__, i->stack_i, sizeof value->f64);
- return -1;
- }
+ return get_global(i, &value->f32, sizeof value->f32);
+}
- value->f64 = *(const double *)i->cfg.stack.buf;
- return 0;
+static int get_f64(struct nw_interp *const i, union global_value *const value)
+{
+ return get_global(i, &value->f64, sizeof value->f64);
}
-static int get_value(const struct nw_interp *const i,
- const enum value_type type, union global_value *const value)
+static int get_value(struct nw_interp *const i, const enum value_type type,
+ union global_value *const value)
{
- static int (*const get[])(const struct nw_interp *, union global_value *) =
+ static int (*const get[])(struct nw_interp *, union global_value *) =
{
[VALUE_TYPE_I32] = get_i32,
[VALUE_TYPE_I64] = get_i64,
@@ -163,29 +141,33 @@ static int check_global_type(FILE *const f, struct global_type *const g)
static int set_i32(struct nw_interp *const i, void *const dst,
const struct global_type *const g)
{
- *(int32_t *)dst = g->value.i32;
- return 0;
+ const int32_t value = g->value.i32;
+
+ return interp_global_push(i, &value, sizeof value);
}
static int set_i64(struct nw_interp *const i, void *const dst,
const struct global_type *const g)
{
- *(int64_t *)dst = g->value.i64;
- return 0;
+ const int64_t value = g->value.i64;
+
+ return interp_global_push(i, &value, sizeof value);
}
static int set_f32(struct nw_interp *const i, void *const dst,
const struct global_type *const g)
{
- *(float *)dst = g->value.f32;
- return 0;
+ const float value = g->value.f32;
+
+ return interp_global_push(i, &value, sizeof value);
}
static int set_f64(struct nw_interp *const i, void *const dst,
const struct global_type *const g)
{
- *(double *)dst = g->value.f64;
- return 0;
+ const double value = g->value.f64;
+
+ return interp_global_push(i, &value, sizeof value);
}
static int add_global(struct nw_interp *const i,
@@ -197,24 +179,14 @@ static int add_global(struct nw_interp *const i,
.type = g->type
};
- const size_t sz = sizeof f + get_type_size(g->type),
- max = i->cfg.global.n;
+ struct nw_gframe *const dst = interp_globalptr(i);;
- if (max < sz || i->global_i > max - sz)
+ if (interp_global_push(i, &f, sizeof f))
{
- LOG("%s: global memory overflow\n", __func__);
- i->exception = "global memory overflow";
+ LOG("%s: interp_global_push failed\n", __func__);
return -1;
}
- void *const buf = (char *)i->cfg.global.buf + i->global_i;
- struct nw_gframe *const dst = buf;
-
- if (i->gfp)
- i->gfp->next = dst;
-
- *dst = f;
-
static int (*const set[])(struct nw_interp *, void *,
const struct global_type *) =
{
@@ -226,11 +198,14 @@ static int add_global(struct nw_interp *const i,
if (set[g->type](i, dst + 1, g))
{
- LOG("%s: set failed with type %d\n", __func__, g->type);
+ LOG("%s: set failed with type %s\n", __func__,
+ value_type_tostr(g->type));
return -1;
}
- i->global_i += sz;
+ if (i->gfp)
+ i->gfp->next = dst;
+
i->gfp = dst;
return 0;
}