aboutsummaryrefslogtreecommitdiff
path: root/src/op/get_local.c
blob: 56b742f0982b36006c3c1b7a06d09edc53332c6b (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

/*
 * 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 int init_io(struct nw_interp *const i, const enum nw_type t)
{
    struct nw_i_sm_get_local *const gl = &i->sm.get_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 = &gl->value;
        io.n = sz;
        gl->io = io;
    }

    return 0;
}

static enum nw_state push(struct nw_interp *const i)
{
    struct nw_i_sm_get_local *const gl = &i->sm.get_local;
    const enum nw_state n = nwp_stack_push(i, &gl->io);

    if (n)
        return n;

    nwp_interp_resume(i);
    return NW_AGAIN;
}

static enum nw_state get_local(struct nw_interp *const i)
{
    struct nw_i_sm_get_local *const gl = &i->sm.get_local;
    const struct nw_local_meta *const m = &gl->f.l.meta;
    const enum nw_state n = nwp_stack_read(i, &gl->io, gl->f.l.addr);

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

    i->next = push;
    i->push_type = m->type;
    return NW_AGAIN;
}

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

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

    i->next = get_local;
    return NW_AGAIN;
}

static enum nw_state get_param(struct nw_interp *const i)
{
    struct nw_i_sm_get_local *const gl = &i->sm.get_local;
    const struct nw_find_param_out *const o = &gl->f.p.out;
    const enum nw_state n = nwp_stack_read(i, &gl->io, o->addr);

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

    i->next = push;
    i->push_type = o->type;
    return NW_AGAIN;
}

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

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

    i->next = get_param;
    return NW_AGAIN;
}

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

    if (n)
        return n;
    else if (gl->index < fn->param_count)
        nwp_find_param(i, &gl->f.p, gl->index, prepare_param, NULL);
    else if ((index = gl->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)gl->index);
#endif
        return NW_FATAL;
    }
    else
        nwp_find_local(i, &gl->f.l, index, prepare_local, NULL);

    return NW_AGAIN;
}

void nwp_op_get_local(struct nw_interp *const i)
{
    static const struct nw_i_sm_get_local gl;

    i->sm.get_local = gl;
    i->next = get_index;
}