aboutsummaryrefslogtreecommitdiff
path: root/src/interp/check_opcode.c
blob: b10b9270e9a7abbbdeb9f0450340fcb699e572a8 (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
/*
 * nanowasm, a tiny WebAssembly/Wasm interpreter
 * Copyright (C) 2023-2024  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/opcodes.h>

int nwp_interp_check_opcode(const uint8_t op, FILE *const f)
{
    static int (*const checks[])(FILE *) =
    {
        [OP_UNREACHABLE] = check_unreachable,
        [OP_NOP] = check_nop,
        [OP_BLOCK] = check_block,
        [OP_LOOP] = check_loop,
        [OP_IF] = check_if,
        [OP_ELSE] = check_else,
        [OP_END] = check_end,
        [OP_BR] = check_br,
        [OP_BR_IF] = check_br_if,
        [OP_BR_TABLE] = check_br_table,
        [OP_RETURN] = check_return,
        [OP_CALL] = check_call,
        [OP_CALL_INDIRECT] = check_call_indirect,
        [OP_GET_LOCAL] = check_get_local,
        [OP_SET_LOCAL] = check_set_local,
        [OP_TEE_LOCAL] = check_tee_local,
        [OP_GET_GLOBAL] = check_get_global,
        [OP_SET_GLOBAL] = check_set_global,
        [OP_I32_LOAD] = check_i32_load,
        [OP_I32_STORE] = check_i32_store,
        [OP_I32_CONST] = check_i32_const,
        [OP_I64_CONST] = check_i64_const,
        [OP_F32_CONST] = check_f32_const,
        [OP_F64_CONST] = check_f64_const,
        [OP_I32_SUB] = check_i32_sub
    };

    if (op >= sizeof checks / sizeof *checks)
    {
        LOG("%s: invalid opcode %#" PRIx8 "\n", __func__, op);
        return 1;
    }
    else if (!checks[op])
    {
        LOG("%s: unsupported opcode %#" PRIx8 "\n", __func__, op);
        return 1;
    }

    return checks[op](f);
}