aboutsummaryrefslogtreecommitdiff
path: root/src/routines/set_local.c
blob: 99764d3de6381f3245d80f2f35007143e3e10757 (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
/*
 * 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/interp.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/ops.h>
#include <nw/routines.h>
#include <nw/stack.h>
#include <nw/types.h>

static enum nw_state set_local(struct nw_interp *const i)
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    const enum nw_state n = nwp_stack_write(i, &sl->io, sl->f.l.addr);

    if (n)
        return n;

    i->next = sl->next;
    return NW_AGAIN;
}

static int init_io(struct nw_interp *const i, const enum nw_type t)
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    size_t sz;

    if (nwp_type_sz(t, &sz))
    {
        static const char *const exc = "invalid type";

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

        io.buf = &sl->out.value;
        io.n = sz;
        sl->io = io;
    }

    return 0;
}

static enum nw_state pop(struct nw_interp *const i, const enum nw_type t,
    enum nw_state (*const next)(struct nw_interp *))
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    const enum nw_state n = nwp_stack_pop(i, &sl->io);

    if (n)
        return n;
    else if (init_io(i, t))
        return NW_FATAL;

    sl->out.type = t;
    i->next = next;
    return NW_AGAIN;
}

static enum nw_state pop_local(struct nw_interp *const i)
{
    const struct nw_local_meta *const m = &i->sm.set_local.f.l.meta;

    return pop(i, m->type, set_local);
}

static enum nw_state prepare_local(struct nw_interp *const i)
{
    const struct nw_local_meta *const m = &i->sm.set_local.f.l.meta;

    if (init_io(i, m->type))
        return NW_FATAL;

    i->next = pop_local;
    return NW_AGAIN;
}

static enum nw_state set_param(struct nw_interp *const i)
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    const enum nw_state n = nwp_stack_write(i, &sl->io, sl->f.p.out.addr);

    if (n)
        return n;

    i->next = sl->next;
    return NW_AGAIN;
}

static enum nw_state pop_param(struct nw_interp *const i)
{
    const struct nw_find_param_out *const o = &i->sm.set_local.f.p.out;

    return pop(i, o->type, set_param);
}

static enum nw_state prepare_param(struct nw_interp *const i)
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    const struct nw_find_param_out *const o = &sl->f.p.out;

    if (init_io(i, o->type))
        return NW_FATAL;

    i->next = pop_param;
    return NW_AGAIN;
}

static enum nw_state get_index(struct nw_interp *const i)
{
    struct nw_i_sm_set_local *const sl = &i->sm.set_local;
    struct nw_sm_leb128 *const l = &sl->leb128;
    const struct nw_frame *const fr = &i->fr;
    const struct nw_fn *const fn = &fr->fn;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, &sl->index, cfg->user);
    nw_varuint32 index;

    if (n)
        return n;
    else if (sl->index < fn->param_count)
        nwp_find_param(i, &sl->f.p, sl->index, prepare_param, NULL);
    else if ((index = sl->index - fn->param_count) >= fr->local_count)
    {
        static const char *const exc = "invalid local index";

        i->exception = exc;
#ifdef NW_LOG
        nwp_log("%s: %lu\n", exc, (unsigned long)sl->index);
#endif
        return NW_FATAL;
    }
    else
        nwp_find_local(i, &sl->f.l, index, prepare_local, NULL);

    return NW_AGAIN;
}

void nwp_set_local(struct nw_interp *const i,
    enum nw_state (*const next)(struct nw_interp *))
{
    const struct nw_i_sm_set_local sl = {0};
    struct nw_i_sm_set_local *const p = &i->sm.set_local;

    *p = sl;
    p->next = next;
    i->next = get_index;
}