Debian Codegraph @

js/src/jit-test/lib/adhoc-multiplatform-test.js

raw
1
2 // This file provides a version of the functions
3 //
4 // codegenTestX64_adhoc (src/jit-test/lib/codegen-x64-test.js)
5 // codegenTestX86_adhoc (src/jit-test/lib/codegen-x86-test.js)
6 // codegenTestARM64_adhoc (src/jit-test/lib/codegen-arm64-test.js)
7 // (and the equivalent arm(32) function)
8 //
9 // generalised so the output can be specified for all 4 targets in one place.
10 //
11 // Usage:
12 // codegenTestMultiplatform_adhoc(module_text, export_name,
13 // expectedAllTargets, options = {})
14 //
15 // where
16 // `expectedAllTargets` states the expected-output regexps for each target,
17 // thusly:
18 //
19 // {x64: 'text', x86: 'text', arm64: 'text', arm: 'text'}
20 //
21 // The arm(32) expected output is optional. The other 3 must be present.
22 //
23 // Each 'text' is a string that represents a regular expression, possibly
24 // with newlines, representing the instruction or instructions we are looking
25 // for for the operator. Spaces in the expected-pattern are arbitrary, we
26 // preprocess the pattern to replace any space string with \s+. Lines are
27 // separated by newlines and leading and trailing spaces are stripped.
28 // Pattern strings may be empty, denoting "no instruction(s)".
29 //
30 // options specifies options thusly:
31 //
32 // instanceBox: if present, an object with a `value` property that will
33 // receive the constructed instance
34 //
35 // log: for debugging -- print the disassembly and other info helpful to
36 // resolving test failures. This is also printed on a test failure
37 // regardless of the log setting.
38 //
39 // features: this is passed on verbatim to wasmEvalText,
40 // as its third argument.
41 //
42 // no_prefix: by default, the required pattern must be immediately preceded
43 // by `<target>_prefix`, and this is checked. Setting this to
44 // true skips the check. Try not to use this.
45 //
46 // no_suffix: by default, the required pattern must be immediately followed
47 // by `<target>_suffix`, and this is checked. Setting this to
48 // true skips the check. Try not to use this.
49 //
50 // no_prefix/no_suffix apply to all 4 targets. Per-target overrides are
51 // supported, by putting them in a suitably tagged sub-object, eg:
52 // options = {x86: {no_prefix: true}}
53
54 load(libdir + "codegen-test-common.js");
55
56 // Architectures supported by this script.
57 const knownArchs = ["x64", "x86", "arm64", "arm"];
58
59 // Architectures for which `expectedAllTargets` must supply an expected result.
60 const requiredArchs = ["x64", "x86", "arm64"];
61
62 // These define the end-of-prologue ("prefix") and start-of-epilogue
63 // ("suffix") to be matched.
64 const prefixAndSuffix =
65 {x64: {
66 // The move from r14 to rbp is writing the callee's wasm instance
67 // into the frame for debug checks -- see WasmFrame.h.
68 prefix: `48 89 e5 mov %rsp, %rbp(
69 4c 89 75 .0 movq %r14, (0x10|0x30)\\(%rbp\\))?`,
70 suffix: `5d pop %rbp`
71 },
72 x86: {
73 // The move from esi to rbp is writing the callee's wasm instance
74 // into the frame for debug checks -- see WasmFrame.h. The mov to
75 // e[ac]x is debug code, inserted by the register allocator to
76 // clobber e[ac]x before a move group. But it is only present if
77 // there is a move group there.
78 prefix: `8b ec mov %esp, %ebp(
79 89 75 08 movl %esi, 0x08\\(%rbp\\))?(
80 b. ef be ad de mov \\$0xDEADBEEF, %e.x)?`,
81 // `.bp` because zydis chooses `rbp` even on 32-bit systems.
82 suffix: `5d pop %.bp`
83 },
84 arm64: {
85 // The move from x23 to x29 is writing the callee's wasm instance
86 // into the frame for debug checks -- see WasmFrame.h.
87 prefix: `910003fd mov x29, sp
88 910003fc mov x28, sp(
89 f9000bb7 str x23, \\[x29, #16\\])?`,
90 suffix: `f94007fe ldr x30, \\[sp, #8\\]
91 f94003fd ldr x29, \\[sp\\]`
92 },
93 arm: {
94 // The move from r9 to fp is writing the callee's wasm instance into
95 // the frame for debug checks -- see WasmFrame.h.
96 prefix: `e52db004 str fp, \\[sp, #-4\\]!
97 e1a0b00d mov fp, sp(
98 e58b9008 str r9, \\[fp, #\\+8\\])?`,
99 suffix: `e49db004 ldr fp, \\[sp\\], #\\+4`
100 }
101 };
102
103 // The options object may a mix of generic (all-targets) options and contain
104 // sub-objects containing arch-specific options, for example:
105 //
106 // {a_generic_option: 1337, x86: {no_prefix:true}, arm64: {foo:4771}}
107 //
108 // promoteArchSpecificOptions lifts options for `archName` to the top level
109 // and deletes *all* arch-specific subobjects, hence producing the final
110 // to-be-used option set. For the above example, if `archName` is "x86" we
111 // get:
112 //
113 // {a_generic_option: 1337, no_prefix: true}
114 //
115 function promoteArchSpecificOptions(options, archName) {
116 assertEq(true, knownArchs.some(a => archName == a));
117 if (options.hasOwnProperty(archName)) {
118 let archOptions = options[archName];
119 for (optName in archOptions) {
120 options[optName] = archOptions[optName];
121 if (options.log) {
122 print("---- adding " + archName + "-specific option {"
123 + optName + ":" + archOptions[optName] + "}");
124 }
125 }
126 }
127 for (a of knownArchs) {
128 delete options[a];
129 }
130 if (options.log) {
131 print("---- final options");
132 for (optName in options) {
133 print("{" + optName + ":" + options[optName] + "}");
134 }
135 }
136 return options;
137 }
138
139 // Main test function. See comments at top of this file.
140 function codegenTestMultiplatform_adhoc(module_text, export_name,
141 expectedAllTargets, options = {}) {
142 assertEq(hasDisassembler(), true);
143
144 // Check that we've been provided with an expected result for at least
145 // x64, x86 and arm64.
146 assertEq(true,
147 requiredArchs.every(a => expectedAllTargets.hasOwnProperty(a)));
148
149 // Poke the build-configuration object to find out what target we're
150 // generating code for.
151 let genX64 = getBuildConfiguration("x64");
152 let genX86 = getBuildConfiguration("x86");
153 let genArm64 = getBuildConfiguration("arm64");
154 let genArm = getBuildConfiguration("arm");
155 // So far so good, except .. X64 or X86 might be emulating something else.
156 if (genX64 && genArm64 && getBuildConfiguration("arm64-simulator")) {
157 genX64 = false;
158 }
159 if (genX86 && genArm && getBuildConfiguration("arm-simulator")) {
160 genX86 = false;
161 }
162
163 // Check we've definitively identified exactly one architecture to test.
164 assertEq(1, [genX64, genX86, genArm64, genArm].map(x => x ? 1 : 0)
165 .reduce((a,b) => a+b, 0));
166
167 // Decide on the arch name for which we're testing. Everything is keyed
168 // off this.
169 let archName = "";
170 if (genX64) {
171 archName = "x64";
172 } else if (genX86) {
173 archName = "x86";
174 } else if (genArm64) {
175 archName = "arm64";
176 } else if (genArm) {
177 archName = "arm";
178 }
179 if (options.log) {
180 print("---- testing for architecture \"" + archName + "\"");
181 }
182 // If this fails, it means we're running on an "unknown" architecture.
183 assertEq(true, archName.length > 0);
184
185 // Finalise options, by promoting arch-specific ones to the top level of
186 // the options object.
187 options = promoteArchSpecificOptions(options, archName);
188
189 // Get the prefix and suffix strings for the target.
190 assertEq(true, prefixAndSuffix.hasOwnProperty(archName));
191 let prefix = prefixAndSuffix[archName].prefix;
192 let suffix = prefixAndSuffix[archName].suffix;
193 assertEq(true, prefix.length >= 10);
194 assertEq(true, suffix.length >= 10);
195
196 // Get the expected output string, or skip the test if no expected output
197 // has been provided. Note, because of the assertion near the top of this
198 // file, this will currently only allow arm(32) tests to be skipped.
199 let expected = "";
200 if (expectedAllTargets.hasOwnProperty(archName)) {
201 expected = expectedAllTargets[archName];
202 } else {
203 // Paranoia. Don't want to silently skip tests due to logic bugs above.
204 assertEq(archName, "arm");
205 if (options.log) {
206 print("---- !! no expected output for target, skipping !!");
207 }
208 return;
209 }
210
211 // Finalise the expected-result string, and stash the original for
212 // debug-printing.
213 expectedInitial = expected;
214 if (!options.no_prefix) {
215 expected = prefix + '\n' + expected;
216 }
217 if (!options.no_suffix) {
218 expected = expected + '\n' + suffix;
219 }
220 if (genArm) {
221 // For obscure reasons, the arm(32) disassembler prints the
222 // instruction word twice. Rather than forcing all expected lines to
223 // do the same, we detect any line starting with 8 hex digits followed
224 // by a space, and duplicate them so as to match the
225 // disassembler's output.
226 let newExpected = "";
227 let pattern = /^[0-9a-fA-F]{8} /;
228 for (line of expected.split(/\n+/)) {
229 // Remove whitespace at the start of the line. This could happen
230 // for continuation lines in backtick-style expected strings.
231 while (line.match(/^\s/)) {
232 line = line.slice(1);
233 }
234 if (line.match(pattern)) {
235 line = line.slice(0,9) + line;
236 }
237 newExpected = newExpected + line + "\n";
238 }
239 expected = newExpected;
240 }
241 expected = fixlines(expected);
242
243 // Compile the test case and collect disassembly output.
244 let ins = wasmEvalText(module_text, {}, options.features);
245 if (options.instanceBox)
246 options.instanceBox.value = ins;
247 let output = wasmDis(ins.exports[export_name], {tier:"ion", asString:true});
248
249 // Check for success, print diagnostics
250 let output_matches_expected = output.match(new RegExp(expected)) != null;
251 if (!output_matches_expected) {
252 print("---- adhoc-tier1-test.js: TEST FAILED ----");
253 }
254 if (options.log && output_matches_expected) {
255 print("---- adhoc-tier1-test.js: TEST PASSED ----");
256 }
257 if (options.log || !output_matches_expected) {
258 print("---- module text");
259 print(module_text);
260 print("---- actual");
261 print(output);
262 print("---- expected (initial)");
263 print(expectedInitial);
264 print("---- expected (as used)");
265 print(expected);
266 print("----");
267 }
268
269 // Finally, the whole point of this:
270 assertEq(output_matches_expected, true);
271 }