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
|
/*
* 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 <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <stddef.h>
static enum nw_state entry_loop(struct nw_mod *);
static enum nw_state get_return_type(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
struct nw_sm_leb128 *const l = &st->leb128;
const struct nw_io_cfg *const cfg = &m->cfg.io;
nw_varint7 return_type;
const enum nw_state n = nwp_varint7(cfg, l, &return_type, cfg->user);
if (n)
return n;
m->next = entry_loop;
st->entry_i++;
return NW_AGAIN;
}
static enum nw_state get_return_count(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
struct nw_sm_leb128 *const l = &st->leb128;
const struct nw_io_cfg *const cfg = &m->cfg.io;
nw_varuint1 return_count;
const enum nw_state n = nwp_varuint1(cfg, l, &return_count, cfg->user);
if (n)
return n;
else if (return_count)
m->next = get_return_type;
else
{
m->next = entry_loop;
st->entry_i++;
}
return NW_AGAIN;
}
static enum nw_state param_loop(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_leb128 *const l = &st->leb128;
nw_varint7 type;
enum nw_state n;
if (st->p_i >= st->param_count)
{
m->next = get_return_count;
return NW_AGAIN;
}
else if ((n = nwp_varint7(cfg, l, &type, cfg->user)))
return n;
st->p_i++;
return NW_AGAIN;
}
static enum nw_state get_param_count(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
struct nw_sm_leb128 *const l = &st->leb128;
const struct nw_io_cfg *const cfg = &m->cfg.io;
const enum nw_state n = nwp_varuint32(cfg, l, &st->param_count,
cfg->user);
if (n)
return n;
st->p_i = 0;
m->next = param_loop;
return NW_AGAIN;
}
static int check_form(const nw_varint7 form)
{
static const nw_varint7 v[] =
{
/* TODO: move values to enums? */
0x7f, /* i32 */
0x7e, /* i64 */
0x7d, /* f32 */
0x7c, /* f64 */
0x70, /* anyfunc */
0x60, /* func */
0x40 /* empty block_type */
};
size_t i;
for (i = 0; i < sizeof v / sizeof *v; i++)
if (form == v[i])
return 0;
return -1;
}
static enum nw_state get_form(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
struct nw_sm_leb128 *const l = &st->leb128;
const struct nw_io_cfg *const cfg = &m->cfg.io;
nw_varint7 form;
const enum nw_state n = nwp_varint7(cfg, l, &form, cfg->user);
if (n)
return n;
else if (check_form(form))
{
#ifdef NW_LOG
nwp_log("invalid form %#x\n", (unsigned)form);
#endif
return NW_FATAL;
}
m->next = get_param_count;
return NW_AGAIN;
}
static enum nw_state entry_loop(struct nw_mod *const m)
{
struct nw_sm_st *const st = &m->sm.type;
m->next = st->entry_i < m->type_count ? get_form : nwp_section_exit;
return NW_AGAIN;
}
static enum nw_state get_count(struct nw_mod *const m)
{
const struct nw_io_cfg *const cfg = &m->cfg.io;
struct nw_sm_st *const st = &m->sm.type;
struct nw_sm_leb128 *const l = &st->leb128;
const enum nw_state n = nwp_varuint32(cfg, l, &m->type_count, cfg->user);
if (n)
return n;
m->next = entry_loop;
return NW_AGAIN;
}
void nwp_section_type(struct nw_mod *const m)
{
const struct nw_sm_st st = {0};
m->next = get_count;
m->sm.type = st;
}
|