blob: 23576fca7408f58c3d06ea576af426c23e9aa9c9 [file] [log] [blame]
Pierre Langlois88c46b82016-06-02 18:15:32 +01001#!/usr/bin/env python3
2
Alexandre Ramesb78f1392016-07-01 14:22:22 +01003# Copyright 2016, VIXL authors
Pierre Langlois88c46b82016-06-02 18:15:32 +01004# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# * Redistributions of source code must retain the above copyright notice,
10# this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright notice,
12# this list of conditions and the following disclaimer in the documentation
13# and/or other materials provided with the distribution.
14# * Neither the name of ARM Limited nor the names of its contributors may be
15# used to endorse or promote products derived from this software without
16# specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29"""
30Generating tests
31================
32
33From the VIXL toplevel directory run:
34
35 $ ./tools/generate_tests.py
36
37The script assumes that `clang-format-3.6` is in the current path. If it isn't,
38you can provide your own:
39
40 $ ./tools/generate_tests.py --clang-format /patch/to/clang-format
41
42Once the script has finished, it will have generated test files, as many as
43present in the `default_config_files` list. For example:
44
Alexandre Ramesd3832962016-07-04 15:03:43 +010045- test/aarch32/test-assembler-cond-rd-rn-immediate-a32.cc
46- test/aarch32/test-assembler-cond-rd-rn-rm-a32.cc
47- test/aarch32/test-assembler-cond-rd-rn-rm-q-a32.cc
48- test/aarch32/test-assembler-cond-rd-rn-rm-ge-a32.cc
Pierre Langlois88c46b82016-06-02 18:15:32 +010049
50Because these test cases need traces in order to build, the script will have
Alexandre Ramesd3832962016-07-04 15:03:43 +010051generated dummy trace files in `test/aarch32/traces/`. If you look at them
52you'll see they are basically empty:
Pierre Langlois88c46b82016-06-02 18:15:32 +010053
Alexandre Ramesd3832962016-07-04 15:03:43 +010054 $ cat test/aarch32/traces/sim-cond-rd-rn-immediate-adc-a32.h
Pierre Langlois88c46b82016-06-02 18:15:32 +010055 static const TestResult *kReferenceAdc = NULL;
56
57So of course, we can now build the test cases but running them will crash. We
58need to re-generate traces with real hardware; the test cases do not support
59running in the simulator just yet.
60
61Generating traces
62=================
63
64You need to have either compiled natively for ARM, or cross-compiled
65`test-runner`. The traces can then be generated in the same way as with VIXL64.
66Note that it takes a few minutes to generate everything.
67
68 ./tools/generate_simulator_traces.py --runner /path/to/test-runner \
69 --aarch32-only
70
71You can now rebuild everything. If it all goes well, running the new tests
72should pass.
73
74Test configuration format
75=========================
76
77TODO: Write a simple and well documented complete example configuration file and
78 mention it here.
79
80The underlying `test_generator` framework reads JSON description files and
Alexandre Ramesd3832962016-07-04 15:03:43 +010081generates tests according to them. These files live in `test/aarch32/config` by
Pierre Langlois88c46b82016-06-02 18:15:32 +010082default, but you may provide your own files with the `--config-files FILE ...`
83flag. The JSON format was extended to support C++ like one-line comments.
84
85Each configuration file will serve to generate one or more test files,
86we even use its file name to choose the name of the test:
87
Alexandre Ramesd3832962016-07-04 15:03:43 +010088 test/aarch32/config/cond-rd-rn-immediate-a32.json
89 `-> test/aarch32/test-simulator-cond-rd-rn-immediate-a32.cc
90 `-> test/aarch32/test-assembler-cond-rd-rn-immediate-a32.cc
Pierre Langlois88c46b82016-06-02 18:15:32 +010091
92In addition to these test configuration files, we also provide a JSON
93description with shared information. This information represents data types that
Alexandre Ramesd3832962016-07-04 15:03:43 +010094instructions use and lives in `test/aarch32/config/data-types.json`.
Pierre Langlois88c46b82016-06-02 18:15:32 +010095
96Data types description
97----------------------
98
99We refer to two kinds of data types: `operand` and `input`.
100
101An `operand` represents an argument passed to the macro-assembler to generate an
102instruction. For example, a register or an immediate are operands. We can think
103of it as "assemble-time" data.
104
105As opposed to `operands`, an `input` represents data passed to an instruction at
106runtime. For example, it will be the value you write to a register before
107executing the instruction under test.
108
109The `data-types.json` file has the following structure:
110
111~~~
112{
113 "operands": [
114 // List of operand types.
115 ],
116 "inputs": [
117 // List of input types.
118 ]
119}
120~~~
121
122Each operand is described with the following structure:
123
124~~~
125{
126 // Unique name for this operand type.
127 "name": "AllRegistersButPC",
128 // C++ type used by VIXL to represent this operand.
129 "type": "Register",
130 // List of possible variants.
131 "variants": [
132 "r0",
133 "r1",
134 "r2",
135 "r3",
136 "r4",
137 "r5",
138 "r6",
139 "r7",
140 "r8",
141 "r9",
142 "r10",
143 "r11",
144 "r12",
145 "r13",
146 "r14"
147 ],
148 // Default variant to use.
149 "default": "r0"
150}
151~~~
152
153The "name" field of the operand will be used by test configuration files in
154order to specify what kind of operands an instruction takes. The "type" field
155simply tells the generator what C++ type should be generated, e.g. "Condition",
156"Register", "uint32_t", "ShiftType", ...etc.
157
158Inputs are described in a very similar way:
159
160~~~
161{
162 // Unique name for this input type.
163 "name": "Register",
164 // Python type from `test_generator.data_types` to use to generate C++ code
165 // for this input.
166 "type": "Register",
167 // List of possible values.
168 "values": [
169 "0x00000000",
170 "0xffffffff",
171 "0xabababab",
172 "0x5a5a5a5a"
173 ],
174 // Default value.
175 "default": "0xabababab"
176}
177~~~
178
179The "name" field has the same purpose as for operands. The "type" field however,
180is the name of a Python class in `test_generator.data_types`. The type will
181specify what C++ code to generate in order to load and record the input value,
182e.g. how to load a value into a register, how to read and record it.
183
184When adding more tests, one may have to create new data types in this file. For
185example, when we want to test an instruction with a different set of registers.
186If adding new input types which need different C++ code to load and record them,
187one will have to add it to `test_generator.data_types` and override the
188`Epilogue` and `Prologue` methods.
189
190Test configuration
191------------------
192
193Once we have all the data types we need described, we need test configuration
194files to describe what instructions to test and with what `inputs` and
195`operands` they take.
196
197These files have the following structure:
198
199~~~
200{
201 "mnemonics": [
202 // List of instruction mnemonics to use. These must correspond to
203 // `MacroAssembler` methods.
204 ],
205 "description": {
206 "operands": [
207 // List of operands the instruction takes.
208 ],
209 "inputs: [
210 // List of inputs the instruction can be affected by.
211 ]
212 },
213 // List of files to generate.
214 "test-files": [
215 {
216 "type": "assembler",
217 "mnemonics": [
218 // Optional list of instruction mnemonics to use, overriding the
219 // top-level list.
220 ],
221 "test-cases": [
222 // List of test cases for "assembler" tests, see below for
223 // details.
224 ]
225 },
226 {
227 "type": "simulator",
228 "test-cases": [
229 // List of test cases for "simulator" tests, see below for
230 // details.
231 ]
232 }
233 ]
234}
235~~~
236
237- List of operands:
238
239The operand list describes the actual argument to the `MacroAssembler` method.
240For example, if we take instruction in the form
241"XXX.cond rd rn rm shift #amount":
242
243We want to generate C++ code as such:
244
245~~~
246Condition cond = ...;
247Register rd = ...;
248Register rn = ...;
249Register rm = ...;
250ShiftType type = ...;
251uint32_t amount = ...;
252Operand op(rm, type, amount);
253
254__ Xxx(cond, rd, rn, op);
255~~~
256
257We will have the following operand list:
258
259~~~
260"operands": [
261 {
262 "name": "cond",
263 "type": "Condition"
264 },
265 {
266 "name": "rd",
267 "type": "AllRegistersButPC"
268 },
269 {
270 "name": "rn",
271 "type": "AllRegistersButPC"
272 },
273 {
274 "name": "op",
275 "wrapper": "Operand",
276 "operands": [
277 {
278 "name": "rm",
279 "operand": "AllRegistersButPC"
280 },
281 {
282 "name": "type",
283 "operand": "Shift"
284 },
285 {
286 "name": "amount",
287 "operand": "ImmediateShiftAmount"
288 }
289 ]
290 }
291]
292~~~
293
294The "name" field represents the identifier of the operand and will be used as a
295variable name in the generated code. The "type" field corresponds to an operand
296type described in the `data-types.json` file as described above.
297
298We can see that we've wrapped the last three operands into an "op"
299wrapper object. This allows us to tell the generator to wrap these
300operands into a `Operand` C++ object.
301
302- List of inputs:
303
304This structure is similar to the operand list, but this time it describes what
305input data the instructions may be affected by at runtime. If we take the same
306example as above, we will have the following list:
307
308~~~
309"inputs": [
310 {
311 "name": "apsr",
312 "type": "NZCV"
313 },
314 {
315 "name": "rd",
316 "type": "Register"
317 },
318 {
319 "name": "rn",
320 "type": "Register"
321 },
322 {
323 "name": "rm",
324 "type": "Register"
325 }
326]
327~~~
328
329This will specify what C++ code to generate before and after emitting the
330instruction under test. The C++ code will set and record register values for
331example. See `test_generator.data_types` for more details.
332
333- Test files and test cases:
334
335Up until now, we've only just described the environment in which instructions
336can operate. We need to express what files we want generating, what instructions
337we want to test and what we want them to do.
338
339As previously mentioned, a configuration file can control the generation of
340several test files. We will generate one file per element in the "test-files"
341array:
342
343~~~
344"test-files": [
345 {
346 "type": "assembler",
347 "test-cases": [
348 // List of test cases for "assembler" tests, see below for
349 // details.
350 ]
351 },
352 {
353 "type": "assembler",
354 "name": "special-case",
355 "mnemonics": [
356 // Override the top-level list with a subset of instructions concerned
357 // with this special case.
358 ],
359 "test-cases": [
360 // List of test cases for "assembler" tests, see below for
361 // details.
362 ]
363 },
364 {
365 "type": "simulator",
366 "test-cases": [
367 // List of test cases for "simulator" tests, see below for
368 // details.
369 ]
370 }
371]
372~~~
373
374Above, we've decided to generate three tests: a "simulator" test and two
375"assembler" tests. The resulting files will have names with the following
376pattern.
377
Alexandre Ramesd3832962016-07-04 15:03:43 +0100378 - "test/aarch32/test-assembler-{configuration name}-a32.cc"
379 - "test/aarch32/test-assembler-{configuration name}-special-case-a32.cc"
380 - "test/aarch32/test-simulator-{configuration name}-a32.cc"
Pierre Langlois88c46b82016-06-02 18:15:32 +0100381
382The "type" field describes the kind of testing we want to do, these types are
383recognized by the generator and, at the moment, can be one of "simulator" or
384"assembler". Simulator tests will run each instruction and record the
385changes while assembler tests will only record the code buffer and never execute
386anything. Because you may want to generate more than one test of the same type,
387as we are doing in the example, we need a way to differentiate them. You may use
388the optional "name" field for this.
389
390Finally, we describe how to test the instruction by declaring a list of test
391cases with the "test-cases" field.
392
393Here is an example of what we can express:
394~~~
395[
396 // Generate all combinations of instructions where "rd" an "rn" are the same
397 // register and "cond" and "rm" are just the default.
398 // For example:
399 // __ Xxx(al, r0, r0, r0);
400 // __ Xxx(al, r1, r1, r0);
401 // __ Xxx(al, r2, r2, r0);
402 // ...
403 // __ Xxx(al, r12, r12, r0);
404 // __ Xxx(al, r13, r13, r0);
405 // __ Xxx(al, r14, r14, r0);
406 //
407 // For each of the instructions above, run them with a different value in "rd"
408 // and "rn".
409 {
410 "name": "RdIsRn",
411 "operands": [
412 "rd", "rn"
413 ],
414 "operand-filter": "rd == rn",
415 "inputs": [
416 "rd", "rn"
417 ],
418 "input-filter": "rd == rn"
419 },
420 // Generate all combinations of instructions with different condition codes.
421 // For example:
422 // __ Xxx(eq, r0, r0, r0);
423 // __ Xxx(ne, r0, r0, r0);
424 // __ Xxx(cs, r0, r0, r0);
425 // ...
426 // __ Xxx(gt, r0, r0, r0);
427 // __ Xxx(le, r0, r0, r0);
428 // __ Xxx(al, r0, r0, r0);
429 //
430 // For each of the instructions above, run them against all combinations of
431 // NZCV bits.
432 {
433 "name": "ConditionVersusNZCV",
434 "operands": [
435 "cond"
436 ],
437 "inputs": [
438 "apsr"
439 ]
440 },
441 // We are interested in testing that the Q bit gets set and cleared, so we've
442 // limited the instruction generation to a single instruction and instead have
443 // stressed the values put in "rn" and "rm".
444 //
445 // So for this instruction, we choose to run it will all combinations of
446 // values in "rn" and "rm". Additionally, we include "qbit" in the inputs,
447 // which will make the test set or clear it before executing the instruction.
448 // Note that "qbit" needs to be declared as an input in the instruction
449 // description (see "List of inputs" section).
450 {
451 "name": "Qbit",
452 "operands": [
453 "rn", "rm"
454 ],
455 "inputs": [
456 "qbit", "rn", "rm"
457 ],
458 "operand-filter": "rn != rm'",
459 "operand-limit": 1
460 },
461 // Generate 10 random instructions with all different registers but use the
462 // default condition.
463 // For example:
464 // __ Xxx(al, r5, r1, r0);
465 // __ Xxx(al, r8, r9, r7);
466 // __ Xxx(al, r9, r1, r2);
467 // __ Xxx(al, r0, r6, r2);
468 // __ Xxx(al, r11, r9, r11);
469 // __ Xxx(al, r14, r2, r11);
470 // __ Xxx(al, r8, r2, r5);
471 // __ Xxx(al, r10, r0, r1);
472 // __ Xxx(al, r11, r2, r7);
473 // __ Xxx(al, r2, r6, r1);
474 //
475 // For each instruction, feed it 200 different combination of values in the
476 // three registers.
477 {
478 "name": "RegisterSimulatorTest",
479 "operands": [
480 "rd", "rn", "rm"
481 ],
482 "inputs": [
483 "rd", "rn", "rm"
484 ],
485 "operand-limit": 10,
486 "input-limit": 200
487 }
488]
489~~~
490
491Assembler test cases are much simpler, here are some examples:
492~~~
493// Generate 2000 random instructions out of all possible operand combinations.
494{
495 "name": "LotsOfRandomInstructions",
496 "operands": [
497 "cond", "rd", "rn", "rm"
498 ],
499 "operand-limit": 2000
500},
501// Same as above but limit the test to 200 instructions where rd == rn.
502{
503 "name": "RdIsRn",
504 "operands": [
505 "cond", "rd", "rn", "rm"
506 ],
507 "operand-filter": "rd == rn",
508 "operand-limit": 200
509}
510~~~
511
512As can be expected, assembler test do not have the notion of "inputs".
513
514Here are details about each field. Note that all of them except for "name" are
515optional.
516
517 * "name":
518
519 A unique name should be given to the test case, it will be used to give the
520 generated C++ `const Input[]` array a name.
521
522 * "operands":
523
524 List of operand names that we are interested in testing. The generator will
525 lookup the list of variants for each operand and build the product of all of
526 them. It will then choose the default variant for the operands not specified
527 here.
528
529 * "operand-filter":
530
531 As you would expect, the product of all operand variants may be huge. To
532 prevent this, you may specify a Python expression to filter the list.
533
534 * "operand-limit":
535
536 We can potentially obtain a *massive* set of variants of instructions, as we
537 are computing a product of operand variants in "operands". This field allows
538 us to limit this by choosing a random sample from the computed variants.
539 Note that this is a seeded pseudo-random sample, and the seed corresponds to
540 the test case description. The same test case description will always
541 generate the same code.
542
543 * "inputs":
544
545 This is exactly the same as "operands" but for inputs.
546
547 * "input-filter":
548
549 Ditto.
550
551 * "input-limit":
552
553 Ditto.
554
555Here is an example of the C++ code that will be generated for a given test case.
556For simplicity, let's generate tests for an instruction with only `NZCV` and two
557registers as inputs.
558
559For the following test case, which will target encodings where `rd` and `rn` are
560the same registers:
561
562~~~
563{
564 "name": "RdIsRn",
565 "operands": [
566 "rd", "rn"
567 ],
568 "operand-filter": "rd == rn",
569 "inputs": [
570 "rd", "rn"
571 ],
572 "input-filter": "rd == rn"
573},
574~~~
575
576It will generate the following input array.
577
578~~~
579// apsr, rd, rn
580static const Inputs kRdIsRn[] = {{NoFlag, 0x00000000, 0x00000000},
581 {NoFlag, 0xffffffff, 0xffffffff},
582 {NoFlag, 0xabababab, 0xabababab},
583 {NoFlag, 0x5a5a5a5a, 0x5a5a5a5a}};
584~~~
585
586We can see that the default apsr value was chosen (NoFlag), as apsr is not in
587the list of "inputs".
588
589It will also generate a list of instructions to test:
590
591~~~
592static const TestLoopData kTests[] = {
593 {{al, r1, r1, 0x000000ab}, ARRAY_SIZE(kRdIsRn), kRdIsRn, "RdIsRn"},
594 {{al, r2, r2, 0x000000ab}, ARRAY_SIZE(kRdIsRn), kRdIsRn, "RdIsRn"},
595 {{al, r8, r8, 0x000000ab}, ARRAY_SIZE(kRdIsRn), kRdIsRn, "RdIsRn"},
596 {{al, r9, r9, 0x000000ab}, ARRAY_SIZE(kRdIsRn), kRdIsRn, "RdIsRn"},
597};
598~~~
599
600As a result, the new test we will assemble each instructions in "mnemonics" with
601all of the operands described in `kTests` above. And each instruction will be
602executed and passed all inputs in `kRdIsRn`.
603"""
604
605import subprocess
606import argparse
607import string
608import re
609import multiprocessing
610import functools
611
612import test_generator.parser
613
614
615default_config_files = [
Alexandre Ramesd3832962016-07-04 15:03:43 +0100616 'test/aarch32/config/rd-rn-rm-a32.json',
617 'test/aarch32/config/cond-rd-rn-operand-const-a32.json',
618 'test/aarch32/config/cond-rd-rn-operand-rm-a32.json',
619 'test/aarch32/config/cond-rd-rn-operand-rm-shift-amount-1to31-a32.json',
620 'test/aarch32/config/cond-rd-rn-operand-rm-shift-amount-1to32-a32.json',
621 'test/aarch32/config/cond-rd-rn-operand-rm-shift-rs-a32.json',
622 'test/aarch32/config/cond-rd-rn-operand-rm-ror-amount-a32.json',
623 'test/aarch32/config/cond-rd-rn-a32.json',
624 'test/aarch32/config/cond-rd-rn-rm-a32.json',
625 'test/aarch32/config/cond-rd-operand-const-a32.json',
626 'test/aarch32/config/cond-rd-operand-rn-a32.json',
627 'test/aarch32/config/cond-rd-operand-rn-shift-amount-1to31-a32.json',
628 'test/aarch32/config/cond-rd-operand-rn-shift-amount-1to32-a32.json',
629 'test/aarch32/config/cond-rd-operand-rn-shift-rs-a32.json',
630 'test/aarch32/config/cond-rd-operand-rn-ror-amount-a32.json',
631 'test/aarch32/config/cond-rd-memop-immediate-512-a32.json',
632 'test/aarch32/config/cond-rd-memop-immediate-8192-a32.json',
633 'test/aarch32/config/cond-rd-memop-rs-a32.json',
634 'test/aarch32/config/cond-rd-memop-rs-shift-amount-1to31-a32.json',
635 'test/aarch32/config/cond-rd-memop-rs-shift-amount-1to32-a32.json',
Pierre Langlois88c46b82016-06-02 18:15:32 +0100636
Alexandre Ramesd3832962016-07-04 15:03:43 +0100637 'test/aarch32/config/cond-rd-rn-t32.json',
638 'test/aarch32/config/cond-rd-rn-rm-t32.json',
639 'test/aarch32/config/cond-rdlow-rnlow-rmlow-t32.json',
640 'test/aarch32/config/cond-rd-rn-operand-const-t32.json',
641 'test/aarch32/config/cond-rd-pc-operand-imm12-t32.json',
642 'test/aarch32/config/cond-rd-rn-operand-imm12-t32.json',
643 'test/aarch32/config/cond-rd-pc-operand-imm8-t32.json',
644 'test/aarch32/config/cond-rd-sp-operand-imm8-t32.json',
645 'test/aarch32/config/cond-rdlow-rnlow-operand-immediate-t32.json',
646 'test/aarch32/config/cond-sp-sp-operand-imm7-t32.json',
647 'test/aarch32/config/cond-rd-rn-operand-rm-t32.json',
648 'test/aarch32/config/cond-rd-rn-operand-rm-shift-amount-1to31-t32.json',
649 'test/aarch32/config/cond-rd-rn-operand-rm-shift-amount-1to32-t32.json',
650 'test/aarch32/config/cond-rd-rn-operand-rm-ror-amount-t32.json',
651 'test/aarch32/config/cond-rd-operand-const-t32.json',
652 'test/aarch32/config/cond-rd-operand-imm16-t32.json',
653 'test/aarch32/config/cond-rdlow-operand-imm8-t32.json',
654 'test/aarch32/config/cond-rd-operand-rn-shift-amount-1to31-t32.json',
655 'test/aarch32/config/cond-rd-operand-rn-shift-amount-1to32-t32.json',
656 'test/aarch32/config/cond-rd-operand-rn-shift-rs-t32.json',
657 'test/aarch32/config/cond-rd-operand-rn-ror-amount-t32.json',
658 'test/aarch32/config/cond-rd-operand-rn-t32.json',
659 'test/aarch32/config/rd-rn-rm-t32.json',
Pierre Langlois88c46b82016-06-02 18:15:32 +0100660]
661
662
663# Link a test type with a template file.
664template_files = {
Alexandre Ramesd3832962016-07-04 15:03:43 +0100665 'simulator': "test/aarch32/config/template-simulator-aarch32.cc.in",
666 'assembler': "test/aarch32/config/template-assembler-aarch32.cc.in",
Pierre Langlois88c46b82016-06-02 18:15:32 +0100667}
668
669
670def BuildOptions():
671 result = argparse.ArgumentParser(
672 description = 'Test generator for AArch32.',
673 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
674 result.add_argument('--config-files', nargs='+',
675 default=default_config_files,
676 metavar='FILE',
677 help='Configuration files, each will generate a test file.')
678 result.add_argument('--clang-format',
679 default='clang-format-3.6', help='Path to clang-format.')
680 result.add_argument('--jobs', '-j', type=int, metavar='N',
681 default=multiprocessing.cpu_count(),
682 help='Allow N jobs at once')
683 return result.parse_args()
684
685
686def DoNotEditComment(template_file):
687 # We rely on `clang-format` to wrap this comment to 80 characters.
688 return """
689// -----------------------------------------------------------------------------
690// This file is auto generated from the {} template file using tools/generate_tests.py.
691//
692// PLEASE DO NOT EDIT.
693// -----------------------------------------------------------------------------
694 """.format(template_file)
695
696def GenerateTest(generator, clang_format):
697 template_file = template_files[generator.test_type]
698 generated_file = ""
699 with open(template_file, "r") as f:
700 # Strip out comments starting with three forward slashes before creating the
701 # string.Template object.
702 template = string.Template(re.sub("\/\/\/.*", "", f.read()))
703
704 # The `generator` object has methods generating strings to fill the template.
705 generated_file = template.substitute({
706 # Add a top comment stating this file is auto-generated.
707 'do_not_edit_comment': DoNotEditComment(template_file),
708
709 # List of mnemonics.
710 'instruction_list_declaration': generator.InstructionListDeclaration(),
711
712 # Declarations.
713 'operand_list_declaration': generator.OperandDeclarations(),
714 'input_declarations': generator.InputDeclarations(),
715
716 # Definitions.
717 'input_definitions': generator.InputDefinitions(),
718 'test_case_definitions': generator.TestCaseDefinitions(),
719
720 # Include traces.
721 'include_trace_files': generator.IncludeTraceFiles(),
722
723 # Define a typedef for the MacroAssembler method.
724 'macroassembler_method_args': generator.MacroAssemblerMethodArgs(),
725
726 # Generate code to switch instruction set.
727 'macroassembler_set_isa': generator.MacroAssemblerSetISA(),
728
729 # Generate code to emit instructions.
730 'code_instantiate_operands': generator.CodeInstantiateOperands(),
731 'code_prologue': generator.CodePrologue(),
732 'code_epilogue': generator.CodeEpilogue(),
733 'code_parameter_list': generator.CodeParameterList(),
734
735 # Generate code to trace the execution and print C++.
736 'trace_print_outputs': generator.TracePrintOutputs(),
737
738 # Generate code to compare the results against a trace.
739 'check_instantiate_results': generator.CheckInstantiateResults(),
740 'check_instantiate_inputs': generator.CheckInstantiateInputs(),
741 'check_instantiate_references': generator.CheckInstantiateReferences(),
742 'check_results_against_references':
743 generator.CheckResultsAgainstReferences(),
744 'check_print_input': generator.CheckPrintInput(),
745 'check_print_expected': generator.CheckPrintExpected(),
746 'check_print_found': generator.CheckPrintFound(),
747
748 'test_name': generator.TestName()
749 })
750 # Create the test case and pipe it through `clang-format` before writing it.
751 with open(
Alexandre Ramesd3832962016-07-04 15:03:43 +0100752 "test/aarch32/test-{}-{}.cc".format(generator.test_type, generator.test_name),
Pierre Langlois88c46b82016-06-02 18:15:32 +0100753 "w") as f:
754 proc = subprocess.Popen([clang_format, "-style=google"],
755 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
756 out, _ = proc.communicate(generated_file.encode())
757 f.write(out.decode())
Alexandre Ramesd3832962016-07-04 15:03:43 +0100758 # Write dummy trace files into 'test/aarch32/traces/'.
759 generator.WriteEmptyTraces("test/aarch32/traces/")
Pierre Langlois88c46b82016-06-02 18:15:32 +0100760 print("Generated {} test for \"{}\".".format(generator.test_type, generator.test_name))
761
762
763if __name__ == '__main__':
764 args = BuildOptions()
765
766 # Each file in `args.config_files` populates a `Generator` object.
Alexandre Ramesd3832962016-07-04 15:03:43 +0100767 generators = test_generator.parser.Parse('test/aarch32/config/data-types.json',
Pierre Langlois88c46b82016-06-02 18:15:32 +0100768 args.config_files)
769
770 # Call the `GenerateTest` function for each generator object in parallel. This
771 # will use as many processes as defined by `-jN`, which defaults to 1.
772 with multiprocessing.Pool(processes=args.jobs) as pool:
773 pool.map(functools.partial(GenerateTest, clang_format=args.clang_format),
774 generators)