aboutsummaryrefslogtreecommitdiff
path: root/src/routines/call_function.c
blob: eeee2fc8f912210961d502981b65bca720562c06 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * 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/io.h>
#include <nw/interp.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <nw/stack.h>
#include <nw/types.h>

static enum nw_state get_entry_count(struct nw_interp *);

static enum nw_state push_frame(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    const enum nw_state n = nwp_stack_push(i, &pl->io);

    if (n)
        return n;

    i->fr = pl->fr;
#ifdef NW_LOG
    nwp_log("entering function %lu\n", (unsigned long)i->fr.fn.index);
#endif
    nwp_interp_resume(i);
    return NW_AGAIN;
}

static void init(struct nw_i_sm_pl *const pl)
{
    union nw_value *const v = &pl->value;
    struct nw_sm_io io = {0};

    io.buf = v;

    switch (pl->meta.type)
    {
        case NW_TYPE_I32:
            v->i32 = 0;
            io.n = sizeof v->i32;
            break;

        case NW_TYPE_I64:
            v->i64.low = 0;
            v->i64.hi = 0;
            io.n = sizeof v->i64;
            break;

        case NW_TYPE_F32:
            v->f32 = 0;
            io.n = sizeof v->f32;
            break;

        case NW_TYPE_F64:
            v->f64 = 0;
            io.n = sizeof v->f64;
            break;
    }

    pl->io = io;
}

static enum nw_state get_code_start(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_frame *const fr = &pl->fr;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = cfg->tell(&fr->start, cfg->user);

    if (n)
        return n;
    else
    {
        struct nw_sm_io io = {0};

        io.buf = &i->fr;
        io.n = sizeof i->fr;
        pl->io = io;
        fr->local_end = nwp_stack_ptr(i);
        fr->body_size -= fr->start - pl->body_start;
        i->next = push_frame;
    }

    return NW_AGAIN;
}

static enum nw_state push_local(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    const enum nw_state n = nwp_stack_push(i, &pl->io);

    if (n)
        return n;
    else if (++pl->entry_i >= pl->meta.entry_count)
        i->next = ++pl->local_i >= pl->local_count ?
            get_code_start : get_entry_count;
    else
        init(pl);

    return NW_AGAIN;
}

static enum nw_state push_meta(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    const enum nw_state n = nwp_stack_push(i, &pl->io);

    if (n)
        return n;

    init(pl);
    i->next = push_local;
    return NW_AGAIN;
}

static enum nw_state get_type(struct nw_interp *const i)
{
    nw_varint7 type;
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_sm_leb128 *const l = &pl->leb128;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varint7(cfg, l, &type, cfg->user);

    if (n)
        return n;
    else if (nwp_get_type(type, &pl->meta.type))
    {
        static const char *const exc = "invalid type";

        i->exception = exc;
#ifdef NW_LOG
        nwp_log("%s: %x\n", exc, (unsigned)type);
#endif
        return NW_FATAL;
    }
    else
    {
        struct nw_sm_io io = {0};

        io.buf = &pl->meta;
        io.n = sizeof pl->meta;
        pl->io = io;
        i->next = push_meta;
    }

    return NW_AGAIN;
}

static enum nw_state get_entry_count(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_sm_leb128 *const l = &pl->leb128;
    nw_varuint32 *const count = &pl->meta.entry_count;
    struct nw_frame *const fr = &pl->fr;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, count, cfg->user);

    if (n)
        return n;

    pl->entry_i = 0;
    fr->local_count += *count;
    i->next = get_type;
    return NW_AGAIN;
}

static enum nw_state get_count(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_sm_leb128 *const l = &pl->leb128;
    struct nw_frame *const fr = &pl->fr;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, &pl->local_count, cfg->user);

    if (n)
        return n;

    fr->local_start = nwp_stack_ptr(i);
    i->next = pl->local_count ? get_entry_count : get_code_start;
    return NW_AGAIN;
}

static enum nw_state get_body_start(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = cfg->tell(&pl->body_start, cfg->user);

    if (n)
        return n;

    i->next = get_count;
    return NW_AGAIN;
}

static enum nw_state get_body_len(struct nw_interp *const i)
{
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_sm_leb128 *const l = &pl->leb128;
    struct nw_frame *const fr = &pl->fr;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, &fr->body_size, cfg->user);

    if (n)
        return n;

    i->next = get_body_start;
    return NW_AGAIN;
}

static void begin(struct nw_interp *const i)
{
    const struct nw_i_sm_pl l = {0};
    const struct nw_fn fn = i->sm.ffn.fn;
    struct nw_i_sm_pl *const pl = &i->sm.pl;
    struct nw_frame *const fr = &pl->fr;

    *pl = l;
    fr->child = 1;
    fr->fn = fn;
    fr->fr_start = nwp_stack_ptr(i);
    i->next = get_body_len;
}

static enum nw_state find(struct nw_interp *const i)
{
    nwp_find_function(i, &i->sm.type.out, begin);
    return NW_AGAIN;
}

void nwp_call_function(struct nw_interp *const i, const nw_varuint32 index)
{
    nwp_get_function_type(i, index, find);
}