OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --validate-asm --allow-natives-syntax |
| 6 |
| 7 var selectedTest = undefined; |
| 8 //selectedTest = 16; |
| 9 |
| 10 function skip(a) { |
| 11 return selectedTest != undefined ? a != selectedTest : false; |
| 12 } |
| 13 |
| 14 const assign_in_stmt = [ |
| 15 "if (E) =", |
| 16 "if (=) E", |
| 17 "if (E) E; else =", |
| 18 "for (=; E; S) S", |
| 19 "for (E; =; S) S", |
| 20 "for (E; E; =) E", |
| 21 "for (E; E; E) =", |
| 22 "do { = } while(E)", |
| 23 "do { S } while (=)", |
| 24 ]; |
| 25 const assign_in_expr = [ |
| 26 "i32_func(=)", |
| 27 "(=) ? E : E", |
| 28 "E ? (=) : E", |
| 29 "E ? E : (=)", |
| 30 "(=) + E", |
| 31 "E + (=)", |
| 32 "imul(=, E)", |
| 33 "imul(E, =)", |
| 34 "~(=)", |
| 35 "(=) | 0", |
| 36 "(=), E", |
| 37 "E, (=)", |
| 38 "E, E, (=)", |
| 39 "E, (=), E", |
| 40 "(=), E, E", |
| 41 ]; |
| 42 |
| 43 const stdlib = { |
| 44 Math: Math, |
| 45 Int8Array: Int8Array, |
| 46 Int16Array: Int16Array, |
| 47 Int32Array: Int32Array, |
| 48 Uint8Array: Uint8Array, |
| 49 Uint16Array: Uint16Array, |
| 50 Uint32Array: Uint32Array, |
| 51 Float32Array: Float32Array, |
| 52 Float64Array: Float64Array, |
| 53 }; |
| 54 |
| 55 const buffer = new ArrayBuffer(65536); |
| 56 |
| 57 // Template for a module. |
| 58 function MODULE_TEMPLATE(stdlib, foreign, buffer) { |
| 59 "use asm"; |
| 60 var imul = stdlib.Math.imul; |
| 61 var fround = stdlib.Math.fround; |
| 62 var M = new stdlib.Int32Array(buffer); |
| 63 var G = 0; |
| 64 |
| 65 function void_func() {} |
| 66 function i32_func(a) { |
| 67 a = a | 0; |
| 68 return a | 0; |
| 69 } |
| 70 |
| 71 FUNC_DECL |
| 72 return {main: main}; |
| 73 } |
| 74 |
| 75 // Template for main function. |
| 76 { |
| 77 function main(i32, f32, f64) { |
| 78 i32 = i32 | 0; |
| 79 f32 = fround(f32); |
| 80 f64 = +f64; |
| 81 FUNC_BODY |
| 82 } |
| 83 } |
| 84 |
| 85 function RunAsmJsTest(asmfunc, expect) { |
| 86 var asm_source = asmfunc.toString(); |
| 87 var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| 88 |
| 89 print("Testing " + asmfunc.name + " (js)..."); |
| 90 var js_module = eval("(" + nonasm_source + ")")(stdlib, {}, buffer); |
| 91 expect(js_module); |
| 92 |
| 93 print("Testing " + asmfunc.name + " (asm.js)..."); |
| 94 var asm_module = asmfunc(stdlib, {}, buffer); |
| 95 assertTrue(%IsAsmWasmCode(asmfunc)); |
| 96 expect(asm_module); |
| 97 } |
| 98 |
| 99 var test = 0; |
| 100 |
| 101 function DoTheTests(expr, assign, stmt) { |
| 102 // ==== Expression assignment tests ======================================== |
| 103 for (let e of assign_in_expr) { |
| 104 if (skip(++test)) continue; |
| 105 var orig = e; |
| 106 e = e.replace(/=/g, assign); |
| 107 e = e.replace(/E/g, expr); |
| 108 e = e.replace(/S/g, stmt); |
| 109 var str = main.toString().replace("FUNC_BODY", "return (" + e + ") | 0;"); |
| 110 var asm_source = MODULE_TEMPLATE.toString().replace("FUNC_DECL", str); |
| 111 // print(asm_source); |
| 112 |
| 113 doTest(asm_source, "(" + test + ") " + e); |
| 114 } |
| 115 |
| 116 // ==== Statement assignment tests ========================================= |
| 117 for (let e of assign_in_stmt) { |
| 118 if (skip(++test)) continue; |
| 119 var orig = e; |
| 120 e = e.replace(/=/g, assign); |
| 121 e = e.replace(/E/g, expr); |
| 122 e = e.replace(/S/g, stmt); |
| 123 var str = main.toString().replace("FUNC_BODY", e + "; return 0;"); |
| 124 var asm_source = MODULE_TEMPLATE.toString().replace("FUNC_DECL", str); |
| 125 // print(asm_source); |
| 126 |
| 127 doTest(asm_source, "(" + test + ") " + e); |
| 128 } |
| 129 |
| 130 function doTest(asm_source, orig) { |
| 131 |
| 132 var nonasm_source = asm_source.replace(new RegExp("use asm"), ""); |
| 133 print("Testing JS: " + orig); |
| 134 var js_module = eval("(" + nonasm_source + ")")(stdlib, {}, buffer); |
| 135 expect(js_module); |
| 136 |
| 137 var asmfunc = eval("(" + asm_source + ")"); |
| 138 |
| 139 print("Testing ASMJS: " + orig); |
| 140 var asm_module = asmfunc(stdlib, {}, buffer); |
| 141 assertTrue(%IsAsmWasmCode(asmfunc)); |
| 142 expect(asm_module); |
| 143 } |
| 144 |
| 145 function expect(module) { module.main(0, 0, 0); print(" ok"); return true; } |
| 146 } |
| 147 |
| 148 DoTheTests("(i32 | 0)", "i32 = 0", "void_func()"); |
| 149 DoTheTests("G", "G = 0", "void_func()"); |
| 150 DoTheTests("G", "G = 0", "G"); |
| 151 DoTheTests("(M[0] | 0)", "M[0] = 0", "void_func()"); |
OLD | NEW |