| 1 |
// Scaffolding for testing x64 Ion code generation patterns). See |
| 2 |
// ../tests/wasm/README-codegen.md for general information, and the *codegen.js |
| 3 |
// tests in that directory and its subdirectories for specifics. |
| 4 |
// |
| 5 |
// The structure of the inputs can vary for the different testing predicates but |
| 6 |
// each case generally has an operator name and an expected-pattern; the latter |
| 7 |
// is a string that represents a regular expression, possibly with newlines, |
| 8 |
// representing the instruction or instructions we are looking for for the |
| 9 |
// operator. Spaces in the expected-pattern are arbitrary, we preprocess the |
| 10 |
// pattern to replace any space string with \s+. Lines are separated by |
| 11 |
// newlines and leading and trailing spaces are currently stripped. |
| 12 |
// |
| 13 |
// The testers additionally take an optional options bag with the following |
| 14 |
// optional entries: |
| 15 |
// features: if present, an object to pass as the last argument to functions |
| 16 |
// that compile wasm bytecode |
| 17 |
// instanceBox: if present, an object with a `value` property that will |
| 18 |
// receive the constructed instance |
| 19 |
// no_prefix: by default, the required pattern must be immediately preceded |
| 20 |
// by `x64_prefix`, and this is checked. Setting this to true skips |
| 21 |
// the check. |
| 22 |
// no_suffix: by default, the required pattern must be immediately followed |
| 23 |
// by `x64_suffix`, and this is checked. Setting this to true skips |
| 24 |
// the check. |
| 25 |
// baseline: by default, the output to be tested is expected to be produced |
| 26 |
// at the tier "ion". Setting this to true changes the expected |
| 27 |
// tier to "baseline". |
| 28 |
// memory: if present, add a memory of length given by this property |
| 29 |
// log: for debugging -- print the disassembly, then the preprocessed pattern |
| 30 |
|
| 31 |
load(libdir + "codegen-test-common.js"); |
| 32 |
|
| 33 |
// RIP-relative address following the instruction mnemonic |
| 34 |
var RIPR = `0x${HEXES}`; |
| 35 |
|
| 36 |
// RIP-relative address in the binary encoding |
| 37 |
var RIPRADDR = `${HEX}{2} ${HEX}{2} ${HEX}{2} ${HEX}{2}`; |
| 38 |
|
| 39 |
// End of prologue. The move from r14 to rbp is writing the callee's wasm |
| 40 |
// instance into the frame for debug checks -- see WasmFrame.h. |
| 41 |
var x64_prefix = ` |
| 42 |
48 89 e5 mov %rsp, %rbp( |
| 43 |
4c 89 75 .0 movq %r14, (0x10|0x30)\\(%rbp\\))? |
| 44 |
` |
| 45 |
|
| 46 |
// Start of epilogue |
| 47 |
var x64_suffix = `5d pop %rbp`; |
| 48 |
|
| 49 |
// v128 OP v128 -> v128 |
| 50 |
// inputs: [[complete-opname, expected-pattern], ...] |
| 51 |
function codegenTestX64_v128xv128_v128(inputs, options = {}) { |
| 52 |
for ( let [op, expected] of inputs ) { |
| 53 |
codegenTestX64BinopInternal(op, expected, options, 'v128', 'v128', 'v128', '0', '1'); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// v128 OP param1-type -> v128 |
| 58 |
// inputs: [[complete-opname, param1-type, expected-pattern], ...] |
| 59 |
function codegenTestX64_v128xPTYPE_v128(inputs, options = {}) { |
| 60 |
for ( let [op, p1type, expected] of inputs ) { |
| 61 |
codegenTestX64BinopInternal(op, expected, options, 'v128', p1type, 'v128', '0', '1'); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// v128 OP literal -> v128 |
| 66 |
// inputs: [[complete-opname, rhs-literal, expected-pattern], ...] |
| 67 |
function codegenTestX64_v128xLITERAL_v128(inputs, options = {}) { |
| 68 |
for ( let [op, literal, expected] of inputs ) { |
| 69 |
codegenTestX64_adhoc(wrap(options, ` |
| 70 |
(func (export "f") (param v128) (result v128) |
| 71 |
(${op} (local.get 0) ${literal}))`), |
| 72 |
'f', |
| 73 |
expected, |
| 74 |
options) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// literal OP v128 -> v128 |
| 79 |
// inputs: [[complete-opname, lhs-literal, expected-pattern], ...] |
| 80 |
function codegenTestX64_LITERALxv128_v128(inputs, options = {}) { |
| 81 |
for ( let [op, literal, expected] of inputs ) { |
| 82 |
codegenTestX64_adhoc(wrap(options, ` |
| 83 |
(func (export "f") (param v128) (result v128) |
| 84 |
(${op} ${literal} (local.get 0)))`), |
| 85 |
'f', |
| 86 |
expected, |
| 87 |
options) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// v128 OP v128 -> v128, but operands are swapped |
| 92 |
// inputs: [[complete-opname, expected-pattern], ...] |
| 93 |
function codegenTestX64_v128xv128_v128_reversed(inputs, options = {}) { |
| 94 |
for ( let [op, expected] of inputs ) { |
| 95 |
codegenTestX64BinopInternal(op, expected, options, 'v128', 'v128', 'v128', '1', '0'); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// OP v128 -> v128 |
| 100 |
// inputs: [[complete-opname, expected-pattern], ...] |
| 101 |
function codegenTestX64_v128_v128(inputs, options = {}) { |
| 102 |
for ( let [op, expected] of inputs ) { |
| 103 |
codegenTestX64_adhoc(wrap(options, ` |
| 104 |
(func (export "f") (param v128) (result v128) |
| 105 |
(${op} (local.get 0)))`), |
| 106 |
'f', |
| 107 |
expected, |
| 108 |
options); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// OP param-type -> v128 |
| 113 |
// inputs [[complete-opname, param-type, expected-pattern], ...] |
| 114 |
function codegenTestX64_PTYPE_v128(inputs, options = {}) { |
| 115 |
for ( let [op, ptype, expected] of inputs ) { |
| 116 |
codegenTestX64_adhoc(wrap(options, ` |
| 117 |
(func (export "f") (param ${ptype}) (result v128) |
| 118 |
(${op} (local.get 0)))`), |
| 119 |
'f', |
| 120 |
expected, |
| 121 |
options); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// OP v128 -> v128, but the function takes two arguments and the first is ignored |
| 126 |
// inputs: [[complete-opname, expected-pattern], ...] |
| 127 |
function codegenTestX64_IGNOREDxv128_v128(inputs, options = {}) { |
| 128 |
for ( let [op, expected] of inputs ) { |
| 129 |
codegenTestX64_adhoc(wrap(options, ` |
| 130 |
(func (export "f") (param v128) (param v128) (result v128) |
| 131 |
(${op} (local.get 1)))`), |
| 132 |
'f', |
| 133 |
expected, |
| 134 |
options); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// () -> v128 |
| 139 |
// inputs: [[complete-opname, expected-pattern], ...] |
| 140 |
function codegenTestX64_unit_v128(inputs, options = {}) { |
| 141 |
for ( let [op, expected] of inputs ) { |
| 142 |
codegenTestX64_adhoc(wrap(options, ` |
| 143 |
(func (export "f") (result v128) |
| 144 |
(${op}))`), |
| 145 |
'f', |
| 146 |
expected, |
| 147 |
options); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// For when nothing else applies: `module_text` is the complete source text of |
| 152 |
// the module, `export_name` is the name of the function to be tested, |
| 153 |
// `expected` is the non-preprocessed pattern, and options is an options bag, |
| 154 |
// described above. |
| 155 |
function codegenTestX64_adhoc(module_text, export_name, expected, options = {}) { |
| 156 |
assertEq(hasDisassembler(), true); |
| 157 |
|
| 158 |
let ins = wasmEvalText(module_text, {}, options.features); |
| 159 |
if (options.instanceBox) |
| 160 |
options.instanceBox.value = ins; |
| 161 |
let tierTxt = options.baseline ? "baseline" : "ion"; |
| 162 |
let output = wasmDis(ins.exports[export_name], |
| 163 |
{tier:tierTxt, asString:true}); |
| 164 |
if (!options.no_prefix) |
| 165 |
expected = x64_prefix + '\n' + expected; |
| 166 |
if (!options.no_suffix) |
| 167 |
expected = expected + '\n' + x64_suffix; |
| 168 |
const expected_pretty = striplines(expected); |
| 169 |
expected = fixlines(expected); |
| 170 |
|
| 171 |
const success = output.match(new RegExp(expected)) != null; |
| 172 |
if (options.log || !success) { |
| 173 |
print("Module text:") |
| 174 |
print(module_text); |
| 175 |
print("Actual output:") |
| 176 |
print(output); |
| 177 |
print("Expected output (as text):") |
| 178 |
print(expected_pretty); |
| 179 |
print(""); |
| 180 |
print("Expected output (as regex):") |
| 181 |
print(expected); |
| 182 |
print(""); |
| 183 |
} |
| 184 |
assertEq(success, true); |
| 185 |
} |
| 186 |
|
| 187 |
// Internal code below this line |
| 188 |
|
| 189 |
function codegenTestX64BinopInternal(op, expected, options, p0type, p1type, restype, arg0, arg1) { |
| 190 |
codegenTestX64_adhoc(wrap(options, ` |
| 191 |
(func (export "f") (param ${p0type}) (param ${p1type}) (result ${restype}) |
| 192 |
(${op} (local.get ${arg0}) (local.get ${arg1})))`), |
| 193 |
'f', |
| 194 |
expected, |
| 195 |
options); |
| 196 |
} |