| // Copyright 2016, ARM Limited |
| // All rights reserved. |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // * Redistributions of source code must retain the above copyright notice, |
| // this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // * Neither the name of ARM Limited nor the names of its contributors may be |
| // used to endorse or promote products derived from this software without |
| // specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| /// This file is a template read by tools/generate_tests.py, it isn't valid C++ |
| /// as it is. Variables written as ${substitute_me} are replaced by the script. |
| /// Comments starting with three forward slashes such as this one are also |
| /// removed. |
| |
| ${do_not_edit_comment} |
| |
| #include "test-runner.h" |
| |
| #include "test-utils.h" |
| #include "test-utils-a32.h" |
| |
| #include "a32/assembler-a32.h" |
| #include "a32/macro-assembler-a32.h" |
| #include "a32/disasm-a32.h" |
| |
| #define __ masm. |
| #define BUF_SIZE (4096) |
| |
| #ifdef VIXL_INCLUDE_SIMULATOR |
| // Run tests with the simulator. |
| |
| #define SETUP() MacroAssembler masm(BUF_SIZE) |
| |
| #define START() masm.GetBuffer().Reset() |
| |
| #define END() \ |
| __ Hlt(0); \ |
| __ FinalizeCode(); |
| |
| // TODO: Run the tests in the simulator. |
| #define RUN() |
| |
| #define TEARDOWN() |
| |
| #else // ifdef VIXL_INCLUDE_SIMULATOR. |
| |
| #define SETUP() \ |
| MacroAssembler masm(BUF_SIZE); |
| |
| #define START() \ |
| masm.GetBuffer().Reset(); \ |
| __ Push(r4); \ |
| __ Push(r5); \ |
| __ Push(r6); \ |
| __ Push(r7); \ |
| __ Push(r8); \ |
| __ Push(r9); \ |
| __ Push(r10); \ |
| __ Push(r11); \ |
| __ Push(r12); \ |
| __ Push(lr) |
| |
| #define END() \ |
| __ Pop(lr); \ |
| __ Pop(r12); \ |
| __ Pop(r11); \ |
| __ Pop(r10); \ |
| __ Pop(r9); \ |
| __ Pop(r8); \ |
| __ Pop(r7); \ |
| __ Pop(r6); \ |
| __ Pop(r5); \ |
| __ Pop(r4); \ |
| __ Bx(lr); \ |
| __ FinalizeCode(); |
| |
| // Copy the generated code into a memory area garanteed to be executable before |
| // executing it. |
| #define RUN() \ |
| { \ |
| ExecutableMemory code(masm.GetBuffer().GetCursorOffset()); \ |
| code.Write(masm.GetBuffer().GetOffsetAddress<byte*>(0), \ |
| masm.GetBuffer().GetCursorOffset()); \ |
| int pcs_offset = masm.IsT32() ? 1 : 0; \ |
| code.Execute(pcs_offset); \ |
| } |
| |
| #define TEARDOWN() |
| |
| #endif // ifdef VIXL_INCLUDE_SIMULATOR |
| |
| namespace vixl { |
| namespace aarch32 { |
| |
| // List of instruction encodings: |
| #define FOREACH_INSTRUCTION(M) \ |
| ${instruction_list_declaration} |
| |
| // Values to be passed to the assembler to produce the instruction under test. |
| struct Operands { |
| ${operand_list_declaration} |
| }; |
| |
| // Input data to feed to the instruction. |
| struct Inputs { |
| ${input_declarations} |
| }; |
| |
| // This structure contains all input data needed to test one specific encoding. |
| // It used to generate a loop over an instruction. |
| struct TestLoopData { |
| // The `operands` fields represents the values to pass to the assembler to |
| // produce the instruction. |
| Operands operands; |
| // Description of the operands, used for error reporting. |
| const char* operands_description; |
| // Unique identifier, used for generating traces. |
| const char* identifier; |
| // Array of values to be fed to the instruction. |
| size_t input_size; |
| const Inputs* inputs; |
| }; |
| |
| ${input_definitions} |
| |
| // A loop will be generated for each element of this array. |
| static const TestLoopData kTests[] = {${test_case_definitions}}; |
| |
| // We record all inputs to the instructions as outputs. This way, we also check |
| // that what shouldn't change didn't change. |
| struct TestResult { |
| size_t output_size; |
| const Inputs* outputs; |
| }; |
| |
| // These headers each contain an array of `TestResult` with the reference output |
| // values. The reference arrays are names `kReference{mnemonic}`. |
| ${include_trace_files} |
| |
| // The maximum number of errors to report in detail for each test. |
| static const unsigned kErrorReportLimit = 8; |
| |
| typedef void (MacroAssembler::*Fn)(${macroassembler_method_args}); |
| |
| static void TestHelper(Fn instruction, const char* mnemonic, |
| const TestResult reference[]) { |
| SETUP(); |
| ${macroassembler_set_isa} |
| START(); |
| |
| // Data to compare to `reference`. |
| TestResult* results[ARRAY_SIZE(kTests)]; |
| |
| // Test cases for memory bound instructions may allocate a buffer and save its |
| // address in this array. |
| byte* scratch_memory_buffers[ARRAY_SIZE(kTests)]; |
| |
| // Generate a loop for each element in `kTests`. Each loop tests one specific |
| // instruction. |
| for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) { |
| // Allocate results on the heap for this test. |
| results[i] = new TestResult; |
| results[i]->outputs = new Inputs[kTests[i].input_size]; |
| results[i]->output_size = kTests[i].input_size; |
| |
| uintptr_t input_address = reinterpret_cast<uintptr_t>(kTests[i].inputs); |
| uintptr_t result_address = reinterpret_cast<uintptr_t>(results[i]->outputs); |
| |
| scratch_memory_buffers[i] = NULL; |
| |
| Label loop; |
| UseScratchRegisterScope scratch_registers(&masm); |
| // Include all registers from r0 ro r12. |
| scratch_registers.Include(RegisterList(0x1fff)); |
| |
| // Values to pass to the macro-assembler. |
| ${code_instantiate_operands} |
| |
| // Allocate reserved registers for our own use. |
| Register input_ptr = scratch_registers.Acquire(); |
| Register input_end = scratch_registers.Acquire(); |
| Register result_ptr = scratch_registers.Acquire(); |
| |
| // Initialize `input_ptr` to the first element and `input_end` the address |
| // after the array. |
| __ Mov(input_ptr, input_address); |
| __ Add(input_end, input_ptr, |
| sizeof(kTests[i].inputs[0]) * kTests[i].input_size); |
| __ Mov(result_ptr, result_address); |
| __ Bind(&loop); |
| |
| ${code_prologue} |
| |
| (masm.*instruction)(${code_parameter_list}); |
| |
| ${code_epilogue} |
| |
| // Advance the result pointer. |
| __ Add(result_ptr, result_ptr, sizeof(kTests[i].inputs[0])); |
| // Loop back until `input_ptr` is lower than `input_base`. |
| __ Add(input_ptr, input_ptr, sizeof(kTests[i].inputs[0])); |
| __ Cmp(input_ptr, input_end); |
| __ B(ne, &loop); |
| } |
| |
| END(); |
| |
| RUN(); |
| |
| if (Test::generate_test_trace()) { |
| // Print the results. |
| for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { |
| printf("static const Inputs kOutputs_%s_%s[] = {\n", mnemonic, |
| kTests[i].identifier); |
| for (size_t j = 0; j < results[i]->output_size; j++) { |
| printf(" { "); |
| ${trace_print_outputs} |
| printf(" },\n"); |
| } |
| printf("};\n"); |
| } |
| printf("static const TestResult kReference%s[] = {\n", mnemonic); |
| for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { |
| printf(" {\n"); |
| printf(" ARRAY_SIZE(kOutputs_%s_%s),\n", mnemonic, |
| kTests[i].identifier); |
| printf(" kOutputs_%s_%s,\n", mnemonic, kTests[i].identifier); |
| printf(" },\n"); |
| } |
| printf("};\n"); |
| } else { |
| // Check the results. |
| unsigned total_error_count = 0; |
| for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { |
| bool instruction_has_errors = false; |
| for (size_t j = 0; j < kTests[i].input_size; j++) { |
| ${check_instantiate_results} |
| ${check_instantiate_inputs} |
| ${check_instantiate_references} |
| |
| if ((${check_results_against_references}) && |
| (++total_error_count <= kErrorReportLimit)) { |
| // Print the instruction once even if it triggered multiple failures. |
| if (!instruction_has_errors) { |
| printf("Error(s) when testing \"%s %s\":\n", mnemonic, |
| kTests[i].operands_description); |
| instruction_has_errors = true; |
| } |
| // Print subsequent errors. |
| printf(" Input: "); |
| ${check_print_input} |
| printf("\n"); |
| printf(" Expected: "); |
| ${check_print_expected} |
| printf("\n"); |
| printf(" Found: "); |
| ${check_print_found} |
| printf("\n\n"); |
| } |
| } |
| } |
| |
| if (total_error_count > kErrorReportLimit) { |
| printf("%u other errors follow.\n", |
| total_error_count - kErrorReportLimit); |
| } |
| // TODO: Do this check for the simulator too when it is ready. |
| #ifndef VIXL_INCLUDE_SIMULATOR |
| VIXL_CHECK(total_error_count == 0); |
| #endif |
| } |
| |
| for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { |
| delete[] results[i]->outputs; |
| delete results[i]; |
| delete scratch_memory_buffers[i]; |
| } |
| |
| TEARDOWN(); |
| } |
| |
| // Instantiate tests for each instruction in the list. |
| #define TEST(mnemonic) \ |
| static void Test_##mnemonic() { \ |
| TestHelper(&MacroAssembler::mnemonic, #mnemonic, kReference##mnemonic); \ |
| } \ |
| static Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic, \ |
| &Test_##mnemonic); |
| FOREACH_INSTRUCTION(TEST) |
| #undef TEST |
| |
| } // aarch32 |
| } // vixl |