-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathWasmAsm.v3
181 lines (180 loc) · 5.27 KB
/
WasmAsm.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
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// A utility to make generating Wasm bytecode easier.
class WasmAsm extends Vector<byte> {
def put_u32leb(v: int) -> this {
var data = u32.view(v);
while (data >= 0x80) {
put(byte.view(0x80u | (data & 0x7F)));
data = data >> 7;
}
put(byte.view(data));
}
def put_s32leb(data: int) -> this {
while (data != i7.view(data)) {
put(byte.view(0x80 | (data & 0x7F)));
data = data >> 7;
}
put(byte.view(data & 0x7F));
}
def locals(count: int, vt: ValueType) -> this {
put_u32leb(count);
put_blockType(vt);
}
def local_get(index: int) -> this {
put(Opcode.LOCAL_GET.code).put_u32leb(index);
}
def local_tee(index: int) -> this {
put(Opcode.LOCAL_TEE.code).put_u32leb(index);
}
def global_get(index: int) -> this {
put(Opcode.GLOBAL_GET.code).put_u32leb(index);
}
def global_set(index: int) -> this {
put(Opcode.GLOBAL_SET.code).put_u32leb(index);
}
def local_set(index: int) -> this {
put(Opcode.LOCAL_SET.code).put_u32leb(index);
}
def i32const(v: int) -> this {
put(Opcode.I32_CONST.code).put_u32leb(v);
}
def struct_get(decl: StructDecl, field: int) -> this {
put(Opcode.STRUCT_GET.prefix).put(Opcode.STRUCT_GET.code).put_u32leb(decl.heaptype_index).put_u32leb(field);
}
def struct_get_s(decl: StructDecl, field: int) -> this {
put(Opcode.STRUCT_GET_S.prefix).put(Opcode.STRUCT_GET_S.code).put_u32leb(decl.heaptype_index).put_u32leb(field);
}
def struct_get_u(decl: StructDecl, field: int) -> this {
put(Opcode.STRUCT_GET_U.prefix).put(Opcode.STRUCT_GET_U.code).put_u32leb(decl.heaptype_index).put_u32leb(field);
}
def struct_set(decl: StructDecl, field: int) -> this {
put(Opcode.STRUCT_SET.prefix).put(Opcode.STRUCT_SET.code).put_u32leb(decl.heaptype_index).put_u32leb(field);
}
def struct_new_canon_default(decl: StructDecl) -> this {
put(Opcode.STRUCT_NEW_DEFAULT.prefix)
.put(Opcode.STRUCT_NEW_DEFAULT.code).put_u32leb(decl.heaptype_index);
}
def struct_new_canon(decl: StructDecl) -> this {
put(Opcode.STRUCT_NEW.prefix)
.put(Opcode.STRUCT_NEW.code).put_u32leb(decl.heaptype_index);
}
def array_get(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_GET.prefix).put(Opcode.ARRAY_GET.code).put_u32leb(decl.heaptype_index);
}
def array_get_s(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_GET_S.prefix).put(Opcode.ARRAY_GET_S.code).put_u32leb(decl.heaptype_index);
}
def array_get_u(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_GET_U.prefix).put(Opcode.ARRAY_GET_U.code).put_u32leb(decl.heaptype_index);
}
def array_set(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_SET.prefix).put(Opcode.ARRAY_SET.code).put_u32leb(decl.heaptype_index);
}
def array_new(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_NEW.prefix).put(Opcode.ARRAY_NEW.code).put_u32leb(decl.heaptype_index);
}
def array_new_canon_default(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_NEW_DEFAULT.prefix)
.put(Opcode.ARRAY_NEW_DEFAULT.code).put_u32leb(decl.heaptype_index);
}
def array_len(decl: ArrayDecl) -> this {
put(Opcode.ARRAY_LEN.prefix).put(Opcode.ARRAY_LEN.code).put_u32leb(decl.heaptype_index);
}
def ref_eq() -> this {
put(Opcode.REF_EQ.code);
}
def ref_is_null() -> this {
put(Opcode.REF_IS_NULL.code);
}
def ref_cast(ft: int, tt: int) -> this {
put(Opcode.REF_CAST.prefix).put(Opcode.REF_CAST.code);
put_u32leb(tt);
}
def ref_test(ft: int, tt: int) -> this {
put(Opcode.REF_TEST.prefix).put(Opcode.REF_TEST.code);
put_u32leb(tt);
}
def ref_null(i: int) -> this {
put(Opcode.REF_NULL.code).put_u32leb(i);
}
def ref_as_non_null() -> this {
put(Opcode.REF_AS_NON_NULL.code);
}
def call_ref() -> this {
put(Opcode.CALL_REF.code);
}
def return_call_ref() -> this {
put(Opcode.RETURN_CALL_REF.code);
}
def loop0() -> this {
put(Opcode.LOOP.code);
put(BpTypeCode.EmptyBlock.code);
}
def block0() -> this {
put(Opcode.BLOCK.code);
put(BpTypeCode.EmptyBlock.code);
}
def blocks(sig: SigDecl) -> this {
put(Opcode.BLOCK.code);
put_u32leb(sig.heaptype_index);
}
def loops(sig: SigDecl) -> this {
put(Opcode.LOOP.code);
put_u32leb(sig.heaptype_index);
}
def if_(t: ValueType) -> this {
put(Opcode.IF.code);
put_blockType(t);
}
def if0() -> this {
put(Opcode.IF.code);
put(BpTypeCode.EmptyBlock.code);
}
def else_() -> this {
put(Opcode.ELSE.code);
}
def drop() -> this {
put(Opcode.DROP.code);
}
def br_if(depth: int) -> this {
put(Opcode.BR_IF.code);
put_u32leb(depth);
}
def br(depth: int) -> this {
put(Opcode.BR.code);
put_u32leb(depth);
}
def br_on_cast(depth: int, t1: int, t2: int) -> this {
put(Opcode.BR_ON_CAST.prefix).put(Opcode.BR_ON_CAST.code);
put_u32leb(depth);
put_u32leb(t2);
}
def br_on_null(depth: int) -> this {
put(Opcode.BR_ON_NULL.code);
put_u32leb(depth);
}
def end() -> this {
put(Opcode.END.code);
}
def op(opcode: Opcode) -> this {
put(opcode.code);
}
def unreachable() -> this {
put(Opcode.UNREACHABLE.code);
}
def ret() -> this {
put(Opcode.RETURN.code);
}
def put_blockType(t: ValueType) -> this {
// TODO: factor out code into src/util/WasmWriter
match (t) {
I32 => put(BpTypeCode.I32.code);
I64 => put(BpTypeCode.I64.code);
F32 => put(BpTypeCode.F32.code);
F64 => put(BpTypeCode.F64.code);
V128 => put(BpTypeCode.V128.code);
_ => ; // TODO
}
}
}