aboutsummaryrefslogtreecommitdiff
path: root/tools/wasm_compile.h
blob: c5ff49c060df2ba67abc30e6f674c9cdb648d99b (plain)
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
#ifndef WASM_COMPILE_H
#define WASM_COMPILE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>

#define STACK_FRAME_BACKUP_ADDR 0x0FFFFC
#define STACK_TOP_ADDR          0x0FFFFC
#define CODE_BOTTOM_ADDR        0x000000
#define CODE_TOP_ADDR           0x000400
#define MEMORY_BOTTOM_ADDR      0x000400

/* Error messages */
#define MSG_SIZE_OVERFLOW "Number overflows size_t\n"
#define MSG_EOF           "Unexpected end of bytes\n"
#define MSG_BAD_NUM_ENC   "Improper number encoding\n"
#define MSG_BAD_NUM       "Couldn't parse number\n"
#define MSG_BAD_SIZE      "Couldn't compute size to allocate\n"
#define MSG_ALLOC_FAIL(n) "Failed to allocate %lu bytes\n", (unsigned long) (n)
#define MSG_BAD(nam, val) "Found 0x%02hhx instead of %s\n", (char) (val), nam
#define MSG_BYTES_REMAIN  "Didn't use up all bytes when parsing\n"
#define MSG_SEEK_FAIL     "Couldn't navigate the file\n"
#define MSG_BAD_IDX(nam)  "Got %s out of range\n", nam

#define PRERR(...) fprintf(stderr, __VA_ARGS__)

#define POW(n) (((int64_t) 1) << (n))

struct resulttype {
	uint32_t count;
	char *types;
};

struct functype {
	struct resulttype args, results;
};

struct function {
	struct functype *type;

	uint32_t locals_count;
	char *locals;

	struct instruction *translated_body;
	struct target *targets;
	uint32_t start_addr;
};

struct export {
	char *name;
	char desc;
	uint32_t idx;
};

struct module {
	uint32_t functypes_count;
	struct functype *functypes;
	uint32_t functions_count;
	struct function *functions;
	enum {
		MEM_NONE = 0,
		MEM_MIN,
		MEM_MIN_MAX
	} memory_type;
	uint32_t mem_min, mem_max;
	uint32_t exports_count;
	struct export *exports;
	struct target *targets;
	struct instruction *startup;
};

struct translated_word {
	uint16_t contents;
	struct instruction *instr;
};

int leb_32(FILE *handle, uint32_t *result, bool with_sign);

inline static int leb_u32(FILE *handle, uint32_t *result)
{
	return leb_32(handle, result, false);
}

inline static int leb_s32(FILE *handle, int32_t *result)
{
	return leb_32(handle, (uint32_t *) result, true);
}

void free_expr(struct instruction *expr);

void free_targets(struct target *top);

void free_module(struct module *module);

struct module *parse_module(FILE *handle);

int translate(FILE *handle, struct function *function, struct module *module);

int assemble(uint32_t memory_size, struct translated_word memory[memory_size],
	     struct module *module);

#endif  /* WASM_COMPILE_H */