-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBpConstants.v3
229 lines (227 loc) · 6.39 KB
/
BpConstants.v3
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
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Section codes.
enum BpSection(code: byte) {
Unknown(0),
Type(1),
Import(2),
Function(3),
Table(4),
Memory(5),
Tag(13), // ext:exception-handling
Global(6),
Export(7),
Start(8),
Element(9),
DataCount(12),
Code(10),
Data(11),
}
// Import/export kind codes.
enum BpImportExportKind(code: byte) {
Function(0),
Table(1),
Memory(2),
Global(3),
Tag(4),
HeapType(5)
}
// Type constructor codes.
enum BpTypeCode(code: byte, val: i7) {
I32 (0x7F, -1),
I64 (0x7E, -2),
F32 (0x7D, -3),
F64 (0x7C, -4),
V128 (0x7B, -5),
I8 (0x78, -8),
I16 (0x77, -9),
NOCONT (0x75, -11), //
NULLFUNCREF (0x73, -13), //
NULLEXTERNREF (0x72, -14), //
NULLREF (0x71, -15), //
FUNCREF (0x70, -16), //
EXTERNREF (0x6F, -17), //
ANYREF (0x6E, -18), //
EQREF (0x6D, -19), //
I31REF (0x6C, -20), //
STRUCTREF (0x6B, -21), //
ARRAYREF (0x6A, -22), //
EXNREF (0x69, -23), //
CONTREF (0x68, -24), //
REF (0x64, -28), //
REF_NULL (0x63, -29), //
EmptyBlock (0x40, -64) // XXX: move?
}
// Heap type codes.
enum BpHeapTypeCode(code: byte, val: i7) {
FUNC (0x70, -16), // -0x10
EXTERN (0x6F, -17), // -0x11
ANY (0x6E, -18), // -0x12
EQ (0x6D, -19), // -0x13
I31 (0x6C, -20), // -0x14
NOCONT (0x75, -11), // -0x0b
NOFUNC (0x73, -13), // -0x0d
NOEXTERN (0x72, -14), // -0x0e
STRUCT (0x6B, -21), // -0x15
ARRAY (0x6A, -22), // -0x16
EXN (0x69, -23), // -0x17
CONTREF (0x68, -24), // -0x18
NONE (0x71, -15) // -0x0f
}
// Memory flag masks.
enum BpMemoryFlag(mask: byte) {
HasMax(0x01),
Shared(0x02),
Is64(0x04),
HasPageSize(0x08),
}
// Table flag masks.
enum BpTableFlag(mask: byte) {
HasMax(0x01),
}
// Global flag masks.
enum BpGlobalFlag(mask: byte) {
Mutable(0x01)
}
// Heap type declaration codes.
enum BpDefTypeCode(code: byte, val: i7) {
Function (0x60, -32),
Struct (0x5F, -33),
Array (0x5E, -34),
Continuation (0x5D, -35),
SUB (0x50, -48),
SUB_FINAL (0x4F, -49),
REC (0x4E, -50)
}
// Catch kinds
enum BpCatchKind(code: u8) {
CATCH(0x00),
CATCH_REF(0x01),
CATCH_ALL(0x02),
CATCH_ALL_REF(0x03)
}
// Catch codes
type BpCatchCode {
case Catch(tag_index: u31, label: u31);
case CatchRef(tag_index: u31, label: u31);
case CatchAll(label: u31);
case CatchAllRef(label: u31);
}
// Utilities associated with binary sections and other quantities.
component BpConstants {
def WASM_MAGIC = 0x6d736100u;
def WASM_VERSION = 0x01u;
def WASM_PAGE_SIZE = 65536u31;
def log2_WASM_PAGE_SIZE = 16u5;
def MEMARG_INDEX_FLAG = byte.view(0x40u);
def valid_section = Array<bool>.new(256);
def enum_section = Array<BpSection>.new(256);
new() {
for (s in BpSection) {
valid_section[s.code] = true;
enum_section[s.code] = s;
}
}
def typeCodeHasIndex(code: int) -> bool {
match (code) {
BpTypeCode.REF.val,
BpTypeCode.REF_NULL.val => return true;
_ => return false;
}
}
def renderSectionName(buf: StringBuilder, b: byte) -> StringBuilder {
var s = "unknown";
match (b) {
BpSection.Type.code => s = "type";
BpSection.Import.code => s = "import";
BpSection.Function.code => s = "function";
BpSection.Table.code => s = "table";
BpSection.Memory.code => s = "memory";
BpSection.Tag.code => s = "tag";
BpSection.Global.code => s = "global";
BpSection.Export.code => s = "export";
BpSection.Start.code => s = "start";
BpSection.Element.code => s = "element";
BpSection.Code.code => s = "code";
BpSection.Data.code => s = "data";
BpSection.DataCount.code => s = "data count";
}
return buf.puts(s);
}
def renderImportKind(buf: StringBuilder, b: byte) -> StringBuilder {
var s = "unknown";
match (b) {
BpImportExportKind.Function.code => s = "function";
BpImportExportKind.Table.code => s = "table";
BpImportExportKind.Memory.code => s = "memory";
BpImportExportKind.Global.code => s = "global";
BpImportExportKind.Tag.code => s = "tag";
BpImportExportKind.HeapType.code => s = "type";
}
return buf.puts(s);
}
def renderTypeCode(buf: StringBuilder, val: int) -> StringBuilder {
var str: string;
match (val) {
BpTypeCode.I32.val => str = "i32";
BpTypeCode.I64.val => str = "i64";
BpTypeCode.F32.val => str = "f32";
BpTypeCode.F64.val => str = "f64";
BpTypeCode.V128.val => str = "v128";
BpTypeCode.I8.val => str = "i8";
BpTypeCode.I16.val => str = "i16";
BpTypeCode.NOCONT.val => str = "nocont";
BpTypeCode.CONTREF.val => str = "ref null cont";
BpTypeCode.FUNCREF.val => str = "funcref";
BpTypeCode.EXTERNREF.val => str = "externref";
BpTypeCode.ANYREF.val => str = "anyref";
BpTypeCode.EQREF.val => str = "eqref";
BpTypeCode.EmptyBlock.val => str = "empty block type";
BpTypeCode.REF_NULL.val => str = "ref null <T>";
BpTypeCode.REF.val => str = "ref <T>";
BpTypeCode.I31REF.val => str = "i31ref";
BpTypeCode.STRUCTREF.val => str = "structref";
BpTypeCode.ARRAYREF.val => str = "arrayref";
BpTypeCode.NULLREF.val => str = "ref null none";
BpTypeCode.NULLEXTERNREF.val => str = "ref null noextern";
BpTypeCode.NULLFUNCREF.val => str = "ref null nofunc";
}
if (str == null) buf.put1("<unknown type code: %d>", val);
else buf.puts(str);
return buf;
}
def renderDefTypeCode(buf: StringBuilder, b: byte) -> StringBuilder {
var s = "unknown";
match (b) {
BpDefTypeCode.Function.code => s = "func";
BpDefTypeCode.Struct.code => s = "struct";
BpDefTypeCode.Array.code => s = "array";
BpDefTypeCode.REC.code => s = "rec";
BpDefTypeCode.SUB.code => s = "sub";
BpDefTypeCode.Continuation.code => s = "cont";
}
return buf.puts(s);
}
def renderNone(buf: StringBuilder, b: byte) -> StringBuilder {
return buf;
}
def renderMutability(buf: StringBuilder, b: byte) -> StringBuilder {
if ((b & BpGlobalFlag.Mutable.mask) != 0) buf.puts("mutable");
return buf;
}
def renderTableFlags(buf: StringBuilder, b: byte) -> StringBuilder {
if ((b & BpMemoryFlag.HasMax.mask) != 0) buf.puts("has_max");
return buf;
}
def renderMemoryFlags(buf: StringBuilder, b: byte) -> StringBuilder {
if ((b & BpMemoryFlag.HasMax.mask) != 0) buf.puts("has_max");
if ((b & BpMemoryFlag.Shared.mask) != 0) buf.puts(" shared");
if ((b & BpMemoryFlag.Is64.mask) != 0) buf.puts(" is64");
if ((b & BpMemoryFlag.HasPageSize.mask) != 0) buf.puts(" has_page_size");
return buf;
}
def renderAttribute(buf: StringBuilder, b: byte) -> StringBuilder {
if ((b & 1) == 0) buf.puts("exception");
return buf;
}
}