-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBytecodeIteratorTest.v3
219 lines (196 loc) · 7.26 KB
/
BytecodeIteratorTest.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
// Copyright 2022 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
def T = UnitTests.registerT("iterator:", _, BytecodeIteratorTester.new, _);
def X_ = void(
T("empty", test_empty),
T("nop0", test_nop0),
T("nop1", test_nop1),
T("locals0", test_locals0),
T("imm0", test_imm0),
T("imm1", test_imm1),
T("locals1", test_locals1),
()
);
class OpcodeAndImmPosGatherer(it: BytecodeIterator) extends BytecodeVisitor {
var pc: int;
var op: Opcode;
var immpos: int;
def visitOp(opcode: Opcode) {
this.pc = it.pc;
this.op = opcode;
this.immpos = it.immptr().pos;
}
}
class BytecodeIteratorTester(t: Tester) extends ModuleBuilder {
def iterator() -> BytecodeIterator {
return BytecodeIterator.new().reset(func);
}
def assert_bytecodes(expected: Array<Opcode>) {
var it = iterator();
var gather = OpcodeAndImmPosGatherer.new(it);
for (i < expected.length) {
var e = expected[i];
if (Trace.test) Trace.OUT.put2("@+%d: expecting %s", it.pc, e.name).ln();
if (!it.more()) return t.fail1("iteration %d: expected more bytecodes", i);
// test current()
var op = it.current();
if (Trace.test) Trace.OUT.put2(" @+%d: got %s", it.pc, op.name).ln();
if (op != e) return t.fail(Strings.format3("index=%d, expected %s, got %s", i, e.mnemonic, op.mnemonic));
// test dispatch()
it.dispatch(gather);
if (Trace.test) Trace.OUT.put2(" @+%d: got %s", it.pc, op.name).ln();
if (gather.op != e) return t.fail(Strings.format3("index=%d, expected %s, got %s", i, e.mnemonic, gather.op.mnemonic));
// advance
it.next();
if (Trace.test) Trace.OUT.put1(" next=+%d", it.pc).ln();
}
if (it.more()) t.fail("expected !more()");
}
def assert_pcs(expected: Array<(int, Opcode, int)>) {
var it = iterator();
var gather = OpcodeAndImmPosGatherer.new(it);
for (i < expected.length) {
var p = expected[i], pc = p.0, e = p.1;
if (Trace.test) Trace.OUT.put3("@+%d: expecting %s @+%d", it.pc, e.name, pc).ln();
if (!it.more()) return t.fail1("iteration %d: expected more bytecodes", i);
// test current()
var op = it.current();
if (Trace.test) Trace.OUT.put2(" got %s @+%d", op.name, it.pc).ln();
if (it.pc != pc) return t.fail(Strings.format3("index=%d, expected @+%d, got @+%d", i, pc, it.pc));
if (op != e) return t.fail(Strings.format3("index=%d, expected %s, got %s", i, e.mnemonic, op.mnemonic));
// test current() and immptr together
var r = (it.current(), it.immptr());
op = r.0;
if (Trace.test) Trace.OUT.put2(" got %s @+%d", op.name, it.pc).ln();
if (it.pc != pc) return t.fail(Strings.format3("index=%d, expected @+%d, got @+%d", i, pc, it.pc));
if (op != e) return t.fail(Strings.format3("index=%d, expected %s, got %s", i, e.mnemonic, op.mnemonic));
var immptr = r.1;
if (immptr.pos != p.2) return t.fail(Strings.format3("index=%d, expected immptr=+%d, got +%d", i, p.2, immptr.pos));
// test dispatch()
it.dispatch(gather);
if (it.pc != pc) return t.fail(Strings.format3("index=%d, expected @+%d, got @+%d", i, pc, it.pc));
if (gather.pc != pc) return t.fail(Strings.format3("index=%d, expected @+%d, got @+%d", i, pc, it.pc));
if (gather.op != e) return t.fail(Strings.format3("index=%d, expected %s, got %s", i, e.mnemonic, op.mnemonic));
if (gather.immpos != p.2) return t.fail(Strings.format3("index=%d, expected immptr=+%d, got +%d", i, p.2, immptr.pos));
// advance
it.next();
if (Trace.test) Trace.OUT.put1(" next=+%d", it.pc).ln();
}
if (it.more()) t.fail("expected !more()");
}
}
def test_empty(t: BytecodeIteratorTester) {
t.code([]);
t.assert_bytecodes([Opcode.END]);
}
def test_nop0(t: BytecodeIteratorTester) {
t.code([Opcode.NOP.code]);
t.assert_bytecodes([Opcode.NOP, Opcode.END]);
t.code([Opcode.NOP.code, Opcode.NOP.code]);
t.assert_bytecodes([Opcode.NOP, Opcode.NOP, Opcode.END]);
t.code([Opcode.NOP.code, Opcode.NOP.code, Opcode.NOP.code]);
t.assert_bytecodes([Opcode.NOP, Opcode.NOP, Opcode.NOP, Opcode.END]);
}
def test_nop1(t: BytecodeIteratorTester) {
t.code([Opcode.NOP.code]);
t.assert_pcs([
(1, Opcode.NOP, 2),
(2, Opcode.END, 3)]);
t.code([Opcode.NOP.code, Opcode.NOP.code]);
t.assert_pcs([
(1, Opcode.NOP, 2),
(2, Opcode.NOP, 3),
(3, Opcode.END, 4)]);
t.code([Opcode.NOP.code, Opcode.NOP.code, Opcode.NOP.code]);
t.assert_pcs([
(1, Opcode.NOP, 2),
(2, Opcode.NOP, 3),
(3, Opcode.NOP, 4),
(4, Opcode.END, 5)]);
}
def test_locals0(t: BytecodeIteratorTester) {
t.addLocal(ValueType.I32);
t.code([]);
t.assert_pcs([
(3, Opcode.END, 4)]);
t.addLocal(ValueType.F32);
t.code([]);
t.assert_pcs([
(5, Opcode.END, 6)]);
}
def test_imm0(t: BytecodeIteratorTester) {
t.code([Opcode.I32_CONST.code, 44]);
t.assert_pcs([
(1, Opcode.I32_CONST, 2),
(3, Opcode.END, 4)
]);
}
def test_imm1(t: BytecodeIteratorTester) {
def EMPTY_BLOCK: byte = 0x40;
def LABEL: byte = 0;
t.code([
Opcode.BLOCK.code, EMPTY_BLOCK, //ImmSigs.BLOCKT, null),
Opcode.LOOP.code, EMPTY_BLOCK, //ImmSigs.BLOCKT, null),
Opcode.IF.code, EMPTY_BLOCK, //ImmSigs.BLOCKT, null),
Opcode.BR.code, LABEL, //ImmSigs.LABEL, null),
Opcode.BR_IF.code, LABEL, //ImmSigs.LABEL, null),
Opcode.BR_TABLE.code, 0, LABEL, //ImmSigs.LABELS, null),
Opcode.CALL.code, 0, //ImmSigs.FUNC, null),
Opcode.CALL_INDIRECT.code, 0, 0, //ImmSigs.SIG_TABLE, null),
Opcode.RETURN_CALL.code, 0, //ImmSigs.FUNC, null),
Opcode.RETURN_CALL_INDIRECT.code, 0, 0, //ImmSigs.SIG_TABLE, null),
Opcode.CALL_REF.code, 0, //ImmSigs.SIG, null),
Opcode.RETURN_CALL_REF.code, 0, //ImmSigs.SIG, null),
Opcode.SELECT_T.code, 1, 0, //ImmSigs.VALTS, null)
Opcode.NOP.code
]);
t.assert_pcs([
(1, Opcode.BLOCK, 2),
(3, Opcode.LOOP, 4),
(5, Opcode.IF, 6),
(7, Opcode.BR, 8),
(9, Opcode.BR_IF, 10),
(11, Opcode.BR_TABLE, 12),
(14, Opcode.CALL, 15),
(16, Opcode.CALL_INDIRECT, 17),
(19, Opcode.RETURN_CALL, 20),
(21, Opcode.RETURN_CALL_INDIRECT, 22),
(24, Opcode.CALL_REF, 25),
(26, Opcode.RETURN_CALL_REF, 27),
(28, Opcode.SELECT_T, 29),
(31, Opcode.NOP, 32),
(32, Opcode.END, 33)
]);
}
class LocalCollectorVisitor extends BytecodeVisitor {
def got = Vector<(u32, ValueTypeCode)>.new();
def visitLocalDecl(count: u32, vtc: ValueTypeCode) {
got.put(count, vtc);
}
}
def test_locals1(t: BytecodeIteratorTester) {
var expected: Array<(u32, ValueType, ValueTypeCode)> = [
(3, ValueType.I32, ValueTypeCode(BpTypeCode.I32.val, 0)),
(4, ValueType.I64, ValueTypeCode(BpTypeCode.I64.val, 0)),
(5, ValueType.F32, ValueTypeCode(BpTypeCode.F32.val, 0)),
(6, ValueType.F64, ValueTypeCode(BpTypeCode.F64.val, 0)),
(7, ValueType.V128, ValueTypeCode(BpTypeCode.V128.val, 0))
];
for (e in expected) t.addLocals(int.!(e.0), e.1);
t.code([]);
var extensions: Extension.set;
var it = t.iterator();
var collector = LocalCollectorVisitor.new();
var v = collector.got;
it.dispatch(collector);
if (v.length != expected.length) return t.t.fail2("expected %d pairs of locals, got %d", expected.length, v.length);
for (i < expected.length) {
var e = expected[i], g = v[i];
if (e.0 != g.0) return t.t.fail3("locals[%d], expected %d, got %d", i, e.0, g.0);
if (e.2 != g.1) return t.t.fail2("locals[%d], expected %q", i, e.1.render); // TODO: error message
}
if (it.pc != 11) return t.t.fail2("expected pc=+%d, got +%d", 10, it.pc);
t.assert_pcs([
(11, Opcode.END, 12)
]);
}