aboutsummaryrefslogtreecommitdiff
path: root/src/routines/find_param.c
blob: eeae86d1770c4737b4ed11103d570c297abbec87 (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
/*
 * 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 <nw/types.h>

static enum nw_state seek_pc(struct nw_interp *const i)
{
    struct nw_find_param *const fp = i->state;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = cfg->seek(fp->pc, cfg->user);

    if (n)
        return n;

    fp->out.addr = i->fr.fr_start - fp->sz;
    i->next = fp->next;
    return NW_AGAIN;
}

static enum nw_state get_param_type(struct nw_interp *const i)
{
    nw_varint7 type;
    struct nw_find_param *const fp = i->state;
    struct nw_sm_leb128 *const l = &fp->leb128;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = nwp_varint7(cfg, l, &type, cfg->user);
    enum nw_type param_type;
    size_t sz;

    if (n)
        return n;
    else if (nwp_get_type(type, &param_type)
        || nwp_type_sz(param_type, &sz))
    {
        static const char *const exc = "invalid param type";

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

    if (fp->param_i == fp->index)
        fp->out.type = param_type;

    if (fp->param_i >= fp->index)
        fp->sz += sz;

    if (++fp->param_i >= i->fr.fn.param_count)
        i->next = seek_pc;

    return NW_AGAIN;
}

static enum nw_state seek_param_types(struct nw_interp *const i)
{
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const long offset = i->fr.fn.param_types;
    const enum nw_state n = cfg->seek(offset, cfg->user);

    if (n)
        return n;

    i->next = get_param_type;
    return NW_AGAIN;
}

static enum nw_state tell(struct nw_interp *const i)
{
    struct nw_find_param *const fp = i->state;
    const struct nw_io_cfg *const cfg = &i->cfg.io;
    const enum nw_state n = cfg->tell(&fp->pc, cfg->user);

    if (n)
        return n;

    i->next = seek_param_types;
    return NW_AGAIN;
}

void nwp_find_param(struct nw_interp *const i, struct nw_find_param *const f,
    const nw_varuint32 index, enum nw_state (*const next)(struct nw_interp *),
    void *const args)
{
    const struct nw_find_param fp = {0};

    *f = fp;
    f->index = index;
    f->next = next;
    i->state = f;
    i->next = tell;
    i->args = args;
}