Chromium Code Reviews| Index: test/mjsunit/wasm/asm-wasm-expr.js |
| diff --git a/test/mjsunit/wasm/asm-wasm-expr.js b/test/mjsunit/wasm/asm-wasm-expr.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a9a3e781c6cad2aa6d9906395149d592e104a4a2 |
| --- /dev/null |
| +++ b/test/mjsunit/wasm/asm-wasm-expr.js |
| @@ -0,0 +1,152 @@ |
| +// Copyright 2016 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Flags: --validate-asm --allow-natives-syntax |
| + |
| +var selectedTest = undefined; |
| +//selectedTest = 16; |
|
bradnelson
2016/09/23 11:36:01
Leftover?
titzer
2016/09/23 12:07:30
Left it there intentionally so that you can select
|
| + |
| +function skip(a) { |
| + return selectedTest != undefined ? a != selectedTest : false; |
| +} |
| + |
| +const assign_in_stmt = [ |
| +"if (E) =", |
|
bradnelson
2016/09/23 11:36:01
Indented oddly.
titzer
2016/09/23 12:07:30
Done.
|
| +"if (=) E", |
| +"if (E) E; else =", |
| +"for (=; E; S) S", |
| +"for (E; =; S) S", |
| +"for (E; E; =) E", |
| +"for (E; E; E) =", |
| +"do { = } while(E)", |
| +"do { S } while (=)", |
| +]; |
| +const assign_in_expr = [ |
| +"i32_func(=)", |
| +"(=) ? E : E", |
| +"E ? (=) : E", |
| +"E ? E : (=)", |
| +"(=) + E", |
| +"E + (=)", |
| +"imul(=, E)", |
| +"imul(E, =)", |
| +"~(=)", |
| +"(=) | 0", |
| +"(=), E", |
| +"E, (=)", |
| +"E, E, (=)", |
| +"E, (=), E", |
| +"(=), E, E", |
| +]; |
| + |
| +const stdlib = { |
| + Math: Math, |
| + Int8Array: Int8Array, |
| + Int16Array: Int16Array, |
| + Int32Array: Int32Array, |
| + Uint8Array: Uint8Array, |
| + Uint16Array: Uint16Array, |
| + Uint32Array: Uint32Array, |
| + Float32Array: Float32Array, |
| + Float64Array: Float64Array, |
| +}; |
| + |
| +const buffer = new ArrayBuffer(65536); |
| + |
| +// Template for a module. |
| +function MODULE_TEMPLATE(stdlib, foreign, buffer) { |
| + "use asm"; |
| + var imul = stdlib.Math.imul; |
| + var fround = stdlib.Math.fround; |
| + var M = new stdlib.Int32Array(buffer); |
| + var G = 0; |
| + |
| + function void_func() {} |
| + function i32_func(a) { |
| + a = a | 0; |
| + return a | 0; |
| + } |
| + |
| + FUNC_DECL |
| + return {main: main}; |
| +} |
| + |
| +// Template for main function. |
| +{ |
| + function main(i32, f32, f64) { |
| + i32 = i32 | 0; |
| + f32 = fround(f32); |
| + f64 = +f64; |
| + FUNC_BODY |
| + } |
| +} |
| + |
| +function RunAsmJsTest(asmfunc, expect) { |
| + var asm_source = asmfunc.toString(); |
| + var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| + |
| + print("Testing " + asmfunc.name + " (js)..."); |
| + var js_module = eval("(" + nonasm_source + ")")(stdlib, {}, buffer); |
| + expect(js_module); |
| + |
| + print("Testing " + asmfunc.name + " (asm.js)..."); |
| + var asm_module = asmfunc(stdlib, {}, buffer); |
| + assertTrue(%IsAsmWasmCode(asmfunc)); |
| + expect(asm_module); |
| +} |
| + |
| +var test = 0; |
| + |
| +function DoTheTests(expr, assign, stmt) { |
| + // ==== Expression assignment tests ======================================== |
| + for (let e of assign_in_expr) { |
| + if (skip(++test)) continue; |
| + var orig = e; |
| + e = e.replace(/=/g, assign); |
| + e = e.replace(/E/g, expr); |
| + e = e.replace(/S/g, stmt); |
| + var str = main.toString().replace("FUNC_BODY", "return (" + e + ") | 0;"); |
| + var asm_source = MODULE_TEMPLATE.toString().replace("FUNC_DECL", str); |
| + // TODO(titzer): a verbosity API for these kinds of tests? |
| + // print(asm_source); |
| + |
| + doTest(asm_source, "(" + test + ") " + e); |
| + } |
| + |
| + // ==== Statement assignment tests ========================================= |
| + for (let e of assign_in_stmt) { |
| + if (skip(++test)) continue; |
| + var orig = e; |
| + e = e.replace(/=/g, assign); |
| + e = e.replace(/E/g, expr); |
| + e = e.replace(/S/g, stmt); |
| + var str = main.toString().replace("FUNC_BODY", e + "; return 0;"); |
| + var asm_source = MODULE_TEMPLATE.toString().replace("FUNC_DECL", str); |
| +// print(asm_source); |
|
bradnelson
2016/09/23 11:36:01
leftover
titzer
2016/09/23 12:07:30
Mircea asked about the same thing. I left it for e
|
| + |
| + doTest(asm_source, "(" + test + ") " + e); |
| + } |
| + |
| + function doTest(asm_source, orig) { |
| + |
|
bradnelson
2016/09/23 11:36:01
Drop line
titzer
2016/09/23 12:07:30
Done.
|
| + var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| + print("Testing JS: " + orig); |
| + var js_module = eval("(" + nonasm_source + ")")(stdlib, {}, buffer); |
| + expect(js_module); |
| + |
| + var asmfunc = eval("(" + asm_source + ")"); |
| + |
| + print("Testing ASMJS: " + orig); |
| + var asm_module = asmfunc(stdlib, {}, buffer); |
| + assertTrue(%IsAsmWasmCode(asmfunc)); |
| + expect(asm_module); |
| + } |
| + |
| + function expect(module) { module.main(0, 0, 0); print(" ok"); return true; } |
| +} |
| + |
| +DoTheTests("(i32 | 0)", "i32 = 0", "void_func()"); |
|
bradnelson
2016/09/23 11:36:01
Good to have more coverage.
Interested what you hi
titzer
2016/09/23 12:07:31
I hit a few cases of missing or extra kDrop instru
|
| +DoTheTests("G", "G = 0", "void_func()"); |
| +DoTheTests("G", "G = 0", "G"); |
| +DoTheTests("(M[0] | 0)", "M[0] = 0", "void_func()"); |