-
Notifications
You must be signed in to change notification settings - Fork 22
/
build_test.js
284 lines (249 loc) · 8.39 KB
/
build_test.js
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
"use strict";
require('source-map-support').install()
const assert = require('assert')
const ast = require('../build/ast.js')
const {
uint8, uint32, float32, float64, varuint1, varuint7, varuint32, varint7, varint32, varint64,
i32, i64, f32, f64, any_func, func, empty_block, void_,
external_kind,
data, str, str_ascii, str_utf8,
custom_section, type_section, import_section, function_section, table_section, memory_section,
global_section, export_section, start_section, element_section, code_section, data_section,
function_import_entry, table_import_entry, memory_import_entry, global_import_entry, local_entry,
export_entry, elem_segment, data_segment,
func_type, table_type, global_type,
resizable_limits, global_variable, init_expr, function_body,
unreachable, nop, end,
block, void_block, loop, void_loop,
if_, br, br_if, br_table, return_,
call, call_indirect,
drop, select, get_local, set_local, tee_local, get_global, set_global,
current_memory, grow_memory, align8, align16, align32, align64
} = ast.c;
// const s_crypto = str_ascii("crypto")
const mod = ast.c.module([
type_section([
func_type([]), // 0
func_type([i32], i32), // 1
func_type([f32], f32), // 2
func_type([i32]), // 3
]),
import_section([
// Note: MVP only allows one table and one memory
function_import_entry(str_ascii("spectest"), str_ascii("print"), varuint32(3)),
// global_import_entry(s_crypto, str_ascii("version"), global_type(i32)),
// memory_import_entry(s_crypto, str_ascii("data"),
// resizable_limits(varuint32(0), varuint32(8))
// ),
// table_import_entry(s_crypto, str_ascii("ciphers"),
// table_type(AnyFunc, resizable_limits(varuint32(0), varuint32(8)))
// ),
]),
// Function offsets are: imported... + local...
function_section([
varuint32(0), // 1
varuint32(1), // 2
varuint32(2), // 3
]),
// Note: MVP only allows a total of 1 table; either a table_section OR a table_import_entry
table_section([
// must have enought size for element_section's element segments
table_type(any_func, resizable_limits(varuint32(1))),
]),
// Note: MVP only allows a total of 1 memory; either a MemorySection OR a MemoryImportEntry
memory_section([
resizable_limits(varuint32(1)),
]),
global_section([
global_variable(global_type(i32), init_expr([
i32.const(0)
])),
]),
export_section([
export_entry(str_ascii("foo"), external_kind.function, varuint32(2)),
export_entry(str_ascii("bar"), external_kind.function, varuint32(3)),
]),
start_section(varuint32(1)),
element_section([
elem_segment(varuint32(0), init_expr([ i32.const(0) ]), [varuint32(0)]),
]),
code_section([
function_body([], []), // ()
// func (local0 i32) i32 {
function_body(
[local_entry(varuint32(1), i32)], // var local1 i32 = 0
[
set_local(1, i32.mul(
get_local(i32,0),
i32.const(4)
)),
// loop(void_, [
// Drop(f32.const(123.4567)),
// nop,
// nop,
// ]),
// if (local1 <= 0) { set local1 = 1 } // avoid infinite loop below
if_(void_, i32.le_s(get_local(i32,1), i32.const(0)), [
set_local(1, i32.const(1))
]),
// do {
// set local1 = (local1 * 2)
// } while (local1 < 9000)
// loop(void_, [
// set_local(1,
// i32.mul(get_local(i32,1), i32.const(2)) ), // set local1 = local1 * 2
// br_if(0, // continue if
// i32.lt_u(get_local(i32,1), i32.const(9000)) ), // local1 < 9000
// ]),
// while ( (set local1 = (local1 * 2)) < 9000 ) {
// }
loop(void_, [
br_if(0, // (continue-if (lt (set local1 (* (get local1) 2)) 9000))
i32.lt_u(
tee_local(1, i32.mul(get_local(i32,1), i32.const(2))),
i32.const(9000)
)
),
]),
void_block([
nop,
]),
// if_(void_,
// i32.const(1), // cond
// [nop, drop<void_>(i32.const(10000)) ], // then
// [nop, drop<void_>(i32.const(0)) ], // else
// ),
// Calls spectest.print (i32) void
call(void_, varuint32(0), [
i32.const(123)
]),
set_local(1,
if_(i32,
i32.lt_s(i32.const(1), i32.const(3)), // condition
[ // then
nop,
i32.wrap_i64(
i64.mul(
i64.extend_s_i32(
get_local(i32,1)
),
i64.const(-0xffffffff)
)
)
],[ // else
nop,
i32.const(0)
]
)
),
set_local(1,
select(
get_local(i32,1),
i32.mul(
get_local(i32,1),
i32.trunc_s_f32(
call(f32, varuint32(3), [
f32.const(2)
])
)
),
i32.const(333)
)
),
i32.store(align32, i32.const(0),
get_local(i32,1)
),
drop(void_, grow_memory(i32.const(2))),
set_local(1, i32.const(0)),
// Note: no need for `return` if the stack contains N values where N == return_count
i32.load(align32, i32.const(0)) // load previosuly stored result
// i32.load(align32, i32.const(4)) // load from data section
// get_local(i32,1)
// i32.const(1)
// current_memory // query number of allocated memory pages
]
),
// func (local0 f32) f32 {
// return local0 + 123.4567
// }
function_body(
[], // no locals
[
f32.add(get_local(f32,0), f32.const(123.4567))
]
),
]), // end code_section
data_section([
data_segment(
varuint32(0), // linear memory index (==0 in MVP)
init_expr([i32.const(0)]), // offset at which to place the data
data([0,0,0,0, 44,0,0,0])
),
]),
custom_section(str_ascii("names"), []),
custom_section(str_utf8("Äntligen"), [ data([1,2,3]) ]),
])
if (!process.isUnitTest) {
// repr
const {repr} = require('../build/repr.js')
repr(mod, s => { process.stdout.write(s) })
process.stdout.write('\n')
// lbtext print code of each function body
const lbtext = require('../build/lbtext.js')
const codeSection = ast.get.section(mod, ast.sect_id.code)
for (let funcBody of ast.get.function_bodies(codeSection)) {
console.log('———— function ————')
lbtext.printCode(funcBody.code, s => { process.stdout.write(s) })
}
}
// emit WASM code
const {BufferedEmitter} = require('../build/emit.js')
const emitter = new BufferedEmitter(new ArrayBuffer(mod.z))
mod.emit(emitter)
if (!process.isUnitTest) {
const {reprBuffer} = require('../build/repr.js')
reprBuffer(emitter.buffer, s => { process.stdout.write(s) })
//
// Copy the output from emitter.repr and paste it into the multiline string
// of this chunk, then paste & run in a JS env with WebAssembly:
//
// WebAssembly.compile(new Uint8Array(
// `00 61 73 6d 0d 00 00 00 01 09 02 60 00 00 60 01
// 7f 01 7f 03 03 02 00 01 05 04 01 00 80 01 07 07
// 01 03 66 6f 6f 00 01 08 01 00 0a 3a 02 02 00 0b
// 35 01 01 7f 20 00 41 04 6c 21 01 03 40 01 01 01
// 0b 03 7f 41 01 0b 20 01 41 e4 00 6c 41 cd 02 20
// 01 1b 21 01 41 00 20 01 36 02 00 41 00 21 01 41
// 00 28 02 00 0f 0b 0b 0e 01 00 41 00 0b 08 00 00
// 00 00 2c 00 00 00`.split(/[\s\r\n]+/g).map(v => parseInt(v, 16))
// )).then(mod => {
// let m = new WebAssembly.Instance(mod)
// console.log('m.exports.foo() =>', m.exports.foo(1))
// })
}
function Test() {
// Run generated WASM module binary through the spec interpreter and
// finally print the text format generated.
const { specEval } = require('../build/eval.js')
return specEval(emitter.buffer, {
eval: '(invoke "foo" (i32.const 3))',
logErrors: !process.isUnitTest,
trace: !process.isUnitTest,
}).then(stdout => {
if (!process.isUnitTest) {
console.log(stdout)
}
const expected = '1536000 : i32'
assert.equal(stdout.substr(stdout.length - expected.length), expected)
}).catch(err => {
console.error(err.stack || String(err))
if (!process.isUnitTest) {
process.exit(1)
} else {
throw err
}
})
}
if (!process.isUnitTest) {
Test()
}