aboutsummaryrefslogtreecommitdiff
path: root/src/routines/init_data.c
blob: c9482a03d490f6e0dadca7fc4e53bae487cf75cf (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
/*
 * 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/linear.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_index(struct nw_inst *);
static enum nw_state get_byte(struct nw_inst *);

static enum nw_state store_byte(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    struct nw_interp *const i = &inst->interp;
    const unsigned long offset = d->offset + d->bytes_i;
    const enum nw_state n = nwp_linear_store(i, &d->io, offset);

    if (n)
        return n;
    else if (++d->bytes_i >= d->size)
        inst->next = ++d->entry_i >= d->count ? d->next : get_index;
    else
        inst->next = get_byte;

    return NW_AGAIN;
}

static enum nw_state get_byte(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    struct nw_interp *const i = &inst->interp;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    struct nw_sm_io io = {0};
    enum nw_state n;

    io.buf = &d->b;
    io.n = sizeof d->b;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;
    else
    {
        struct nw_sm_io io = {0};

        io.buf = &d->b;
        io.n = sizeof d->b;
        d->io = io;
    }

    inst->next = store_byte;
    return NW_AGAIN;
}

static enum nw_state get_size(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    struct nw_sm_leb128 *const l = &d->leb128;
    struct nw_interp *const i = &inst->interp;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, &d->size, cfg->user);

    if (n)
        return n;

    d->bytes_i = 0;
    inst->next = get_byte;
    return NW_AGAIN;
}

static enum nw_state pop(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    const enum nw_state n = nwp_stack_pop(&inst->interp, &d->io);

    if (n)
        return n;

    inst->next = get_size;
    return NW_AGAIN;
}

static enum nw_state loop(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    const enum nw_state n = nwp_interp_run(&inst->interp);
    struct nw_sm_io io = {0};

    if (n)
        return n;

    io.buf = &d->offset;
    io.n = sizeof d->offset;
    d->io = io;
    inst->next = pop;
    return NW_AGAIN;
}

static enum nw_state setup_initexpr(struct nw_inst *const inst)
{
    struct nw_interp *const i = &inst->interp;
    const struct nw_interp_cfg icfg = i->cfg;

    if (nwp_interp_start(i, &icfg, &nwp_interp_data_set))
    {
#ifdef NW_LOG
        nwp_log("nw_interp_start failed\n");
#endif
        return NW_FATAL;
    }

    inst->next = loop;
    return NW_AGAIN;
}

static enum nw_state get_index(struct nw_inst *const inst)
{
    struct nw_inst_sm_d *const d = &inst->sm.data;
    struct nw_sm_leb128 *const l = &d->leb128;
    struct nw_interp *const i = &inst->interp;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varuint32(cfg, l, &d->index, cfg->user);

    if (n)
        return n;

    return setup_initexpr(inst);
}

static enum nw_state get_count(struct nw_inst *const i)
{
    struct nw_inst_sm_d *const d = &i->sm.data;
    struct nw_sm_leb128 *const l = &d->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->data_count;
    const enum nw_state n = nwp_varuint32(cfg, l, &d->count, cfg->user);

    if (n)
        return n;
    else if (d->count != expected)
    {
#ifdef NW_LOG
        static const char *const exc = "data count mismatch";

        nwp_log("%s, expected %lu, got %lu\n", exc,
            (unsigned long)expected, (unsigned long)d->count);
#endif
        return NW_FATAL;
    }

    i->next = d->count ? get_index : d->next;
    return NW_AGAIN;
}

static enum nw_state seek_data(struct nw_inst *const i)
{
    struct nw_inst_sm_gl *const d = &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_DATA];
    enum nw_state n;

    if (!offset)
    {
        i->next = d->next;
        return NW_AGAIN;
    }
    else if ((n = cfg->seek(offset, cfg->user)))
        return n;

    i->next = get_count;
    return NW_AGAIN;
}

void nwp_init_data(struct nw_inst *const i,
    enum nw_state (*const next)(struct nw_inst *))
{
    const struct nw_inst_sm_d d = {0};
    struct nw_inst_sm_d *const p = &i->sm.data;

    *p = d;
    p->next = next;
    i->next = seek_data;
}