1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* nanowasm, a tiny WebAssembly/Wasm interpreter
* Copyright (C) 2023-2025 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 <nanowasm/nw.h>
#include <nanowasm/types.h>
#include <nw/global.h>
#include <nw/interp.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <nw/stack.h>
#include <nw/types.h>
static enum nw_state get_content_type(struct nw_inst *const i);
static enum nw_state store(struct nw_inst *const inst)
{
struct nw_inst_sm_gl *const g = &inst->sm.global;
struct nw_interp *const i = &inst->interp;
const enum nw_state n = nwp_global_store(i, &g->io, g->entry_i);
if (n)
return n;
inst->next = ++g->entry_i >= g->count ? g->next : get_content_type;
return NW_AGAIN;
}
static enum nw_state pop(struct nw_inst *const inst)
{
struct nw_inst_sm_gl *const g = &inst->sm.global;
struct nw_interp *const i = &inst->interp;
const enum nw_state n = nwp_stack_pop(i, &g->io);
if (n)
return n;
else
{
struct nw_sm_io io = {0};
io.buf = &g->out;
io.n = sizeof g->out;
g->io = io;
inst->next = store;
}
return NW_AGAIN;
}
static enum nw_state loop(struct nw_inst *const i)
{
struct nw_inst_sm_gl *const g = &i->sm.global;
const enum nw_state n = nwp_interp_run(&i->interp);
size_t sz;
if (n)
return n;
else if (nwp_type_sz(g->out.type, &sz))
{
#ifdef NW_LOG
static const char *const exc = "invalid type";
nwp_log("%s: %#x\n", exc, (unsigned)g->out.type);
#endif
return NW_FATAL;
}
else
{
struct nw_sm_io io = {0};
io.buf = &g->out.value;
io.n = sz;
g->io = io;
i->next = pop;
}
return NW_AGAIN;
}
static enum nw_state get_mutability(struct nw_inst *const inst)
{
struct nw_inst_sm_gl *const g = &inst->sm.global;
struct nw_interp *const i = &inst->interp;
const struct nw_io_cfg *const cfg = &i->cfg.io;
struct nw_sm_leb128 *const l = &g->leb128;
const enum nw_state n = nwp_varuint1(cfg, l, &g->out.mutability, cfg->user);
if (n)
return n;
nwp_interp_resume(i);
inst->next = loop;
return NW_AGAIN;
}
static enum nw_state get_content_type(struct nw_inst *const i)
{
nw_varint7 content_type;
struct nw_inst_sm_gl *const g = &i->sm.global;
const struct nw_io_cfg *const cfg = &i->interp.cfg.io;
struct nw_sm_leb128 *const l = &g->leb128;
const enum nw_state n = nwp_varint7(cfg, l, &content_type, cfg->user);
if (n)
return n;
else if (nwp_get_type(content_type, &g->out.type))
{
#ifdef NW_LOG
static const char *const exc = "invalid type";
nwp_log("%s: %#x\n", exc, (unsigned)content_type);
#endif
return NW_FATAL;
}
i->next = get_mutability;
return NW_AGAIN;
}
static enum nw_state get_count(struct nw_inst *const i)
{
struct nw_inst_sm_gl *const g = &i->sm.global;
struct nw_sm_leb128 *const l = &g->leb128;
const struct nw_interp_cfg *const icfg = &i->interp.cfg;
const struct nw_io_cfg *const cfg = &icfg->io;
const nw_varuint32 expected = icfg->m->global_count;
const enum nw_state n = nwp_varuint32(cfg, l, &g->count, cfg->user);
if (n)
return n;
else if (g->count != expected)
{
#ifdef NW_LOG
static const char *const exc = "global count mismatch";
nwp_log("%s, expected %lu, got %lu\n", exc,
(unsigned long)expected, (unsigned long)g->count);
#endif
return NW_FATAL;
}
i->next = g->count ? get_content_type : g->next;
return NW_AGAIN;
}
static enum nw_state seek_global(struct nw_inst *const i)
{
struct nw_inst_sm_gl *const g = &i->sm.global;
const struct nw_interp_cfg *const icfg = &i->interp.cfg;
const struct nw_io_cfg *const cfg = &icfg->io;
const long offset = icfg->m->sections[NW_SECTION_GLOBAL];
enum nw_state n;
if (!offset)
{
i->next = g->next;
return NW_AGAIN;
}
else if ((n = cfg->seek(offset, cfg->user)))
return n;
i->next = get_count;
return NW_AGAIN;
}
void nwp_init_globals(struct nw_inst *const i,
enum nw_state (*const next)(struct nw_inst *))
{
const struct nw_inst_sm_gl gl = {0};
struct nw_inst_sm_gl *const p = &i->sm.global;
*p = gl;
p->next = next;
i->next = seek_global;
}
|