aboutsummaryrefslogtreecommitdiff
path: root/types.h
blob: 1702f440065b6a2f6215d838f3cb271330c454ff (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
/*
 * nwc, a NanoWasm compiler
 * Copyright (C) 2025 Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TYPES_H
#define TYPES_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

enum
{
    SECTION_CUSTOM,
    SECTION_TYPE,
    SECTION_IMPORT,
    SECTION_FUNCTION,
    SECTION_TABLE,
    SECTION_MEMORY,
    SECTION_GLOBAL,
    SECTION_EXPORT,
    SECTION_START,
    SECTION_ELEMENT,
    SECTION_CODE,
    SECTION_DATA
};

typedef bool varint1;
typedef bool varuint1;
typedef signed char varint7;
typedef short varint16;
typedef long varint32;
typedef long long varint64;
typedef unsigned char varuint7;
typedef unsigned short varuint16;
typedef unsigned long varuint32;
typedef unsigned long long varuint64;
typedef varint7 value_type;

struct parse
{
    struct offsets
    {
        uint32_t *offsets;
        size_t n;
    } to, fti, fbo, iti;

    struct lo
    {
        struct lo_entry
        {
            uint8_t op;
            const struct block *b;
            uint32_t pc, dst;
        } *entries;

        size_t n;
    } *lo;

    size_t n_lo;
};

int read_varint1(FILE *f, varint1 *out);
int read_varint7(FILE *f, varint7 *out);
int read_varint32(FILE *f, varint32 *out);
int read_varint64(FILE *f, varint64 *out);
int read_varuint1(FILE *f, varuint1 *out);
int read_varuint7(FILE *f, varuint7 *out);
int read_varuint32(FILE *f, varuint32 *out);
int read_varuint64(FILE *f, varuint64 *out);
int read_instr(FILE *f, struct parse *p);

int write_varuint7(FILE *f, varuint7 v);
int write_varuint32(FILE *f, varuint32 v);
size_t len_varuint32(varuint32 v);

#endif