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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WABT_IR_H_
#define WABT_IR_H_
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "binding-hash.h"
#include "common.h"
#include "opcode.h"
namespace wabt {
enum class VarType {
Index,
Name,
};
struct Var {
explicit Var(Index index = kInvalidIndex);
explicit Var(const StringSlice& name);
Var(Var&&);
Var(const Var&);
Var& operator =(const Var&);
Var& operator =(Var&&);
~Var();
Location loc;
VarType type;
union {
Index index;
StringSlice name;
};
};
typedef std::vector<Var> VarVector;
typedef StringSlice Label;
struct Const {
// Struct tags to differentiate constructors.
struct I32 {};
struct I64 {};
struct F32 {};
struct F64 {};
// Keep the default constructor trivial so it can be used as a union member.
Const() = default;
Const(I32, uint32_t);
Const(I64, uint64_t);
Const(F32, uint32_t);
Const(F64, uint64_t);
Location loc;
Type type;
union {
uint32_t u32;
uint64_t u64;
uint32_t f32_bits;
uint64_t f64_bits;
};
};
typedef std::vector<Const> ConstVector;
enum class ExprType {
Binary,
Block,
Br,
BrIf,
BrTable,
Call,
CallIndirect,
Catch,
CatchAll,
Compare,
Const,
Convert,
CurrentMemory,
Drop,
GetGlobal,
GetLocal,
GrowMemory,
If,
Load,
Loop,
Nop,
Rethrow,
Return,
Select,
SetGlobal,
SetLocal,
Store,
TeeLocal,
Throw,
TryBlock,
Unary,
Unreachable,
};
typedef TypeVector BlockSignature;
struct Expr;
struct Block {
WABT_DISALLOW_COPY_AND_ASSIGN(Block);
Block();
explicit Block(Expr* first);
~Block();
Label label;
BlockSignature sig;
Expr* first;
};
struct Expr {
WABT_DISALLOW_COPY_AND_ASSIGN(Expr);
Expr();
explicit Expr(ExprType);
~Expr();
static Expr* CreateBinary(Opcode);
static Expr* CreateBlock(Block*);
static Expr* CreateBr(Var);
static Expr* CreateBrIf(Var);
static Expr* CreateBrTable(VarVector* targets, Var default_target);
static Expr* CreateCall(Var);
static Expr* CreateCallIndirect(Var);
static Expr* CreateCatch(Var v, Expr* first);
static Expr* CreateCatchAll(Expr* first);
static Expr* CreateCompare(Opcode);
static Expr* CreateConst(const Const&);
static Expr* CreateConvert(Opcode);
static Expr* CreateCurrentMemory();
static Expr* CreateDrop();
static Expr* CreateGetGlobal(Var);
static Expr* CreateGetLocal(Var);
static Expr* CreateGrowMemory();
static Expr* CreateIf(Block* true_, Expr* false_ = nullptr);
static Expr* CreateLoad(Opcode, Address align, uint32_t offset);
static Expr* CreateLoop(Block*);
static Expr* CreateNop();
static Expr* CreateRethrow(Var);
static Expr* CreateReturn();
static Expr* CreateSelect();
static Expr* CreateSetGlobal(Var);
static Expr* CreateSetLocal(Var);
static Expr* CreateStore(Opcode, Address align, uint32_t offset);
static Expr* CreateTeeLocal(Var);
static Expr* CreateThrow(Var);
static Expr* CreateTry(Block* block, Expr* first_catch);
static Expr* CreateUnary(Opcode);
static Expr* CreateUnreachable();
Location loc;
ExprType type;
Expr* next;
union {
struct { Opcode opcode; } binary, compare, convert, unary;
struct Block *block, *loop;
struct { Block* block; Expr* first_catch; } try_block;
struct { Var var; Expr* first; } catch_;
struct { Expr* first; } catch_all;
struct { Var var; } throw_, rethrow_;
struct { Var var; } br, br_if;
struct { VarVector* targets; Var default_target; } br_table;
struct { Var var; } call, call_indirect;
struct Const const_;
struct { Var var; } get_global, set_global;
struct { Var var; } get_local, set_local, tee_local;
struct { Block* true_; Expr* false_; } if_;
struct { Opcode opcode; Address align; uint32_t offset; } load, store;
};
};
struct Exception {
StringSlice name;
TypeVector sig;
};
struct FuncSignature {
TypeVector param_types;
TypeVector result_types;
Index GetNumParams() const { return param_types.size(); }
Index GetNumResults() const { return result_types.size(); }
Type GetParamType(Index index) const { return param_types[index]; }
Type GetResultType(Index index) const { return result_types[index]; }
bool operator==(const FuncSignature&) const;
};
struct FuncType {
WABT_DISALLOW_COPY_AND_ASSIGN(FuncType);
FuncType();
~FuncType();
Index GetNumParams() const { return sig.GetNumParams(); }
Index GetNumResults() const { return sig.GetNumResults(); }
Type GetParamType(Index index) const { return sig.GetParamType(index); }
Type GetResultType(Index index) const { return sig.GetResultType(index); }
StringSlice name;
FuncSignature sig;
};
struct FuncDeclaration {
WABT_DISALLOW_COPY_AND_ASSIGN(FuncDeclaration);
FuncDeclaration();
~FuncDeclaration();
Index GetNumParams() const { return sig.GetNumParams(); }
Index GetNumResults() const { return sig.GetNumResults(); }
Type GetParamType(Index index) const { return sig.GetParamType(index); }
Type GetResultType(Index index) const { return sig.GetResultType(index); }
bool has_func_type;
Var type_var;
FuncSignature sig;
};
struct Func {
WABT_DISALLOW_COPY_AND_ASSIGN(Func);
Func();
~Func();
Type GetParamType(Index index) const { return decl.GetParamType(index); }
Type GetResultType(Index index) const { return decl.GetResultType(index); }
Index GetNumParams() const { return decl.GetNumParams(); }
Index GetNumLocals() const { return local_types.size(); }
Index GetNumParamsAndLocals() const {
return GetNumParams() + GetNumLocals();
}
Index GetNumResults() const { return decl.GetNumResults(); }
Index GetLocalIndex(const Var&) const;
StringSlice name;
FuncDeclaration decl;
TypeVector local_types;
BindingHash param_bindings;
BindingHash local_bindings;
Expr* first_expr;
};
struct Global {
WABT_DISALLOW_COPY_AND_ASSIGN(Global);
Global();
~Global();
StringSlice name;
Type type;
bool mutable_;
Expr* init_expr;
};
struct Table {
WABT_DISALLOW_COPY_AND_ASSIGN(Table);
Table();
~Table();
StringSlice name;
Limits elem_limits;
};
struct ElemSegment {
WABT_DISALLOW_COPY_AND_ASSIGN(ElemSegment);
ElemSegment();
~ElemSegment();
Var table_var;
Expr* offset;
VarVector vars;
};
struct Memory {
WABT_DISALLOW_COPY_AND_ASSIGN(Memory);
Memory();
~Memory();
StringSlice name;
Limits page_limits;
};
struct DataSegment {
WABT_DISALLOW_COPY_AND_ASSIGN(DataSegment);
DataSegment();
~DataSegment();
Var memory_var;
Expr* offset;
char* data;
size_t size;
};
struct Import {
WABT_DISALLOW_COPY_AND_ASSIGN(Import);
Import();
~Import();
StringSlice module_name;
StringSlice field_name;
ExternalKind kind;
union {
// An imported func has the type Func so it can be more easily included in
// the Module's vector of funcs, but only the FuncDeclaration will have any
// useful information.
Func* func;
Table* table;
Memory* memory;
Global* global;
Exception* except;
};
};
struct Export {
WABT_DISALLOW_COPY_AND_ASSIGN(Export);
Export();
~Export();
StringSlice name;
ExternalKind kind;
Var var;
};
enum class ModuleFieldType {
Func,
Global,
Import,
Export,
FuncType,
Table,
ElemSegment,
Memory,
DataSegment,
Start,
Except
};
struct ModuleField {
WABT_DISALLOW_COPY_AND_ASSIGN(ModuleField);
ModuleField();
explicit ModuleField(ModuleFieldType);
~ModuleField();
Location loc;
ModuleFieldType type;
ModuleField* next;
union {
Func* func;
Global* global;
Import* import;
Export* export_;
FuncType* func_type;
Table* table;
ElemSegment* elem_segment;
Memory* memory;
DataSegment* data_segment;
Exception* except;
Var start;
};
};
struct Module {
WABT_DISALLOW_COPY_AND_ASSIGN(Module);
Module();
~Module();
ModuleField* AppendField();
FuncType* AppendImplicitFuncType(const Location&, const FuncSignature&);
Index GetFuncTypeIndex(const Var&) const;
Index GetFuncTypeIndex(const FuncDeclaration&) const;
Index GetFuncTypeIndex(const FuncSignature&) const;
const FuncType* GetFuncType(const Var&) const;
FuncType* GetFuncType(const Var&);
Index GetFuncIndex(const Var&) const;
const Func* GetFunc(const Var&) const;
Func* GetFunc(const Var&);
Index GetTableIndex(const Var&) const;
Table* GetTable(const Var&);
Index GetMemoryIndex(const Var&) const;
Memory* GetMemory(const Var&);
Index GetGlobalIndex(const Var&) const;
const Global* GetGlobal(const Var&) const;
Global* GetGlobal(const Var&);
const Export* GetExport(const StringSlice&) const;
Location loc;
StringSlice name;
ModuleField* first_field;
ModuleField* last_field;
Index num_except_imports;
Index num_func_imports;
Index num_table_imports;
Index num_memory_imports;
Index num_global_imports;
// Cached for convenience; the pointers are shared with values that are
// stored in either ModuleField or Import.
std::vector<Exception*> excepts;
std::vector<Func*> funcs;
std::vector<Global*> globals;
std::vector<Import*> imports;
std::vector<Export*> exports;
std::vector<FuncType*> func_types;
std::vector<Table*> tables;
std::vector<ElemSegment*> elem_segments;
std::vector<Memory*> memories;
std::vector<DataSegment*> data_segments;
Var* start;
BindingHash except_bindings;
BindingHash func_bindings;
BindingHash global_bindings;
BindingHash export_bindings;
BindingHash func_type_bindings;
BindingHash table_bindings;
BindingHash memory_bindings;
};
// A ScriptModule is a module that may not yet be decoded. This allows for text
// and binary parsing errors to be deferred until validation time.
struct ScriptModule {
enum class Type {
Text,
Binary,
Quoted,
};
WABT_DISALLOW_COPY_AND_ASSIGN(ScriptModule);
ScriptModule();
~ScriptModule();
const Location& GetLocation() const {
switch (type) {
case Type::Binary: return binary.loc;
case Type::Quoted: return quoted.loc;
default: assert(0); // Fallthrough.
case Type::Text: return text->loc;
}
}
Type type;
union {
Module* text;
struct {
Location loc;
StringSlice name;
char* data;
size_t size;
} binary, quoted;
};
};
enum class ActionType {
Invoke,
Get,
};
struct ActionInvoke {
WABT_DISALLOW_COPY_AND_ASSIGN(ActionInvoke);
ActionInvoke();
ConstVector args;
};
struct Action {
WABT_DISALLOW_COPY_AND_ASSIGN(Action);
Action();
~Action();
Location loc;
ActionType type;
Var module_var;
StringSlice name;
union {
ActionInvoke* invoke;
struct {} get;
};
};
enum class CommandType {
Module,
Action,
Register,
AssertMalformed,
AssertInvalid,
/* This is a module that is invalid, but cannot be written as a binary module
* (e.g. it has unresolvable names.) */
AssertInvalidNonBinary,
AssertUnlinkable,
AssertUninstantiable,
AssertReturn,
AssertReturnCanonicalNan,
AssertReturnArithmeticNan,
AssertTrap,
AssertExhaustion,
First = Module,
Last = AssertExhaustion,
};
static const int kCommandTypeCount = WABT_ENUM_COUNT(CommandType);
struct Command {
WABT_DISALLOW_COPY_AND_ASSIGN(Command);
Command();
~Command();
CommandType type;
union {
Module* module;
Action* action;
struct { StringSlice module_name; Var var; } register_;
struct { Action* action; ConstVector* expected; } assert_return;
struct {
Action* action;
} assert_return_canonical_nan, assert_return_arithmetic_nan;
struct { Action* action; StringSlice text; } assert_trap;
struct {
ScriptModule* module;
StringSlice text;
} assert_malformed, assert_invalid, assert_unlinkable,
assert_uninstantiable;
};
};
typedef std::vector<std::unique_ptr<Command>> CommandPtrVector;
struct Script {
WABT_DISALLOW_COPY_AND_ASSIGN(Script);
Script();
const Module* GetFirstModule() const;
Module* GetFirstModule();
const Module* GetModule(const Var&) const;
CommandPtrVector commands;
BindingHash module_bindings;
};
void DestroyExprList(Expr*);
void MakeTypeBindingReverseMapping(
const TypeVector&,
const BindingHash&,
std::vector<std::string>* out_reverse_mapping);
} // namespace wabt
#endif /* WABT_IR_H_ */
|