-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw3Module.h
259 lines (227 loc) · 6.7 KB
/
w3Module.h
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// A WebAssembly codebase by Jay Krell
//
// https://webassembly.github.io/spec/core/binary/index.html
// https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
#include "w3.h"
#include "w3MemoryMappedFile.h"
//#include <string> TODO: namespace
//#include <vector>
#include "w3InstructionEnum.h"
#include "w3InstructionEncoding.h"
struct w3Function // section3
{
// Functions are split between two sections: types in section3, locals/body in section10
size_t function_index {}; // TODO needed?
size_t function_type_index {};
size_t local_only_count {};
size_t param_count {};
bool import {}; // TODO needed?
};
// Initial representation of X and XSection are the same.
// This might evolve, i.e. into separate TypesSection and Types,
// or just Types that is not w3Section.
struct w3FunctionType
{
// CONSIDER pointer into mmf
std::vector <w3Tag> parameters;
std::vector <w3Tag> results;
bool operator == (const w3FunctionType& other) const
{
return parameters == other.parameters && results == other.results;
}
};
#include "w3BuiltinString.h"
#include "w3WasmString.h"
#include "w3Section.h"
typedef enum w3ImportTag { // aka desc
w3ImportTag_Function = 0, // aka type
w3ImportTag_Table = 1,
w3ImportTag_Memory = 2,
w3ImportTag_Global = 3,
} w3ImportTag, w3ExportTag;
struct w3Limits
{
// TODO size_t? null?
uint32_t min {};
uint32_t max {};
bool hasMax {};
};
struct w3MemoryType
{
w3Limits limits {};
};
struct w3GlobalType
{
w3Tag value_type {};
bool is_mutable {};
};
struct w3TableType
{
w3Tag elementType {};
w3Limits limits {};
};
struct w3Import
{
WasmString module {};
WasmString name {};
w3ImportTag tag {(w3ImportTag)-1};
// TODO virtual functions to model union
//union
//{
w3TableType table {};
uint32_t function {};
w3MemoryType memory {};
w3GlobalType global {};
//};
};
struct w3DecodedInstructionZeroInit // ZeroMem-compatible part
{
w3DecodedInstructionZeroInit()
{
static w3DecodedInstructionZeroInit a;
*this = a;
}
union
{
uint32_t u32;
uint64_t u64;
int32_t i32;
int64_t i64;
float f32;
double f64;
struct // memory
{
uint32_t align;
uint32_t offset;
};
struct // block / loop
{
size_t label;
};
struct // if / else
{
size_t if_false;
size_t if_end;
};
// etc.
};
uint64_t file_offset; // to match up with disasm output, unsigned for hex
w3InstructionEnum name;
w3Tag blockType;
int id; //sourcegen
};
struct w3DecodedInstruction : w3DecodedInstructionZeroInit
{
size_t Arity() const
{
return (blockType == Tag_empty) ? 0u : 1u; // FUTURE
}
std::vector <uint32_t> vecLabel {};
};
struct Global
{
w3GlobalType global_type {};
std::vector <w3DecodedInstruction> init {};
};
struct w3Element
{
uint32_t table {};
std::vector <w3DecodedInstruction> offset_instructions {};
uint32_t offset {};
std::vector <uint32_t> functions {};
};
struct w3Export
{
WasmString name {};
w3ExportTag tag {};
bool is_start {};
bool is_main {};
union
{
uint32_t function {};
uint32_t memory;
uint32_t table;
uint32_t global;
};
};
struct w3Data // section11
{
uint32_t memory {};
std::vector <w3DecodedInstruction> expr {};
void* bytes {};
};
struct w3Code
// The code to a function.
// Functions are split between section3 and section10.
// Instructions are in section10.
// w3Function code is decoded upon first (or only) visit.
{
size_t size {};
uint8_t* cursor {};
std::vector <w3Tag> locals {}; // params in w3FunctionType
std::vector <w3DecodedInstruction> decoded_instructions {}; // section10
bool import {};
};
struct w3Module
{
std::string name {};
virtual ~w3Module();
w3MemoryMappedFile mmf {};
uint8_t* base {};
uint64_t file_size {};
uint8_t* end {};
w3Section sections [12] {};
//std::vector <std::shared_ptr<w3Section>> custom_sections; // FIXME
// The order can be take advantage of.
// For example global is read before any code,
// so the index of any global.get/set can be validated right away.
std::vector <w3FunctionType> function_types; // section1 function signatures
std::vector <w3Import> imports; // section2
std::vector <w3Function> functions; // section3 and section10 function declarations
std::vector <w3TableType> tables; // section4 indirect tables
std::vector <Global> globals; // section6
std::vector <w3Export> exports; // section7
std::vector <w3Element> elements; // section9 table initialization
std::vector <w3Code> code; // section10
std::vector <w3Data> data; // section11 memory initialization
w3Limits memory_limits;
int instructionId {}; //sourcegen
w3Export* start {};
w3Export* main {};
size_t import_function_count {};
size_t import_table_count {};
size_t import_memory_count {};
size_t import_global_count {};
WasmString read_string (uint8_t** cursor);
int32_t read_i32 (uint8_t** cursor);
int64_t read_i64 (uint8_t** cursor);
float read_f32 (uint8_t** cursor);
double read_f64 (uint8_t** cursor);
uint8_t read_byte (uint8_t** cursor);
uint8_t read_varuint7 (uint8_t** cursor);
uint32_t read_varuint32 (uint8_t** cursor);
void read_vector_varuint32 (std::vector <uint32_t>&, uint8_t** cursor);
w3Limits read_limits (uint8_t** cursor);
w3MemoryType read_memorytype (uint8_t** cursor);
w3GlobalType read_globaltype (uint8_t** cursor);
w3TableType read_tabletype (uint8_t** cursor);
w3Tag read_valuetype (uint8_t** cursor);
w3Tag read_blocktype(uint8_t** cursor);
w3Tag read_elementtype (uint8_t** cursor);
bool read_mutable (uint8_t** cursor);
void read_section (uint8_t** cursor);
void read_module (PCSTR file_name);
void read_vector_ValueType (std::vector <w3Tag>& result, uint8_t** cursor);
void read_function_type (w3FunctionType& functionType, uint8_t** cursor);
virtual void read_types (uint8_t** cursor);
virtual void read_imports (uint8_t** cursor);
virtual void read_functions (uint8_t** cursor);
virtual void read_tables (uint8_t** cursor);
virtual void read_memory (uint8_t** cursor);
virtual void read_globals (uint8_t** cursor);
virtual void read_exports (uint8_t** cursor);
virtual void read_start (uint8_t** cursor);
virtual void read_elements (uint8_t** cursor);
virtual void read_code (uint8_t** cursor);
virtual void read_data (uint8_t** cursor);
};