1b8021494Sopenharmony_ci// Copyright 2016, VIXL authors 2b8021494Sopenharmony_ci// All rights reserved. 3b8021494Sopenharmony_ci// 4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met: 6b8021494Sopenharmony_ci// 7b8021494Sopenharmony_ci// * Redistributions of source code must retain the above copyright notice, 8b8021494Sopenharmony_ci// this list of conditions and the following disclaimer. 9b8021494Sopenharmony_ci// * Redistributions in binary form must reproduce the above copyright notice, 10b8021494Sopenharmony_ci// this list of conditions and the following disclaimer in the documentation 11b8021494Sopenharmony_ci// and/or other materials provided with the distribution. 12b8021494Sopenharmony_ci// * Neither the name of ARM Limited nor the names of its contributors may be 13b8021494Sopenharmony_ci// used to endorse or promote products derived from this software without 14b8021494Sopenharmony_ci// specific prior written permission. 15b8021494Sopenharmony_ci// 16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26b8021494Sopenharmony_ci 27b8021494Sopenharmony_ci/// This file is a template read by tools/generate_tests.py, it isn't valid C++ 28b8021494Sopenharmony_ci/// as it is. Variables written as ${substitute_me} are replaced by the script. 29b8021494Sopenharmony_ci/// Comments starting with three forward slashes such as this one are also 30b8021494Sopenharmony_ci/// removed. 31b8021494Sopenharmony_ci 32b8021494Sopenharmony_ci${do_not_edit_comment} 33b8021494Sopenharmony_ci 34b8021494Sopenharmony_ci#include "test-runner.h" 35b8021494Sopenharmony_ci 36b8021494Sopenharmony_ci#include "test-utils.h" 37b8021494Sopenharmony_ci#include "test-utils-aarch32.h" 38b8021494Sopenharmony_ci 39b8021494Sopenharmony_ci#include "aarch32/assembler-aarch32.h" 40b8021494Sopenharmony_ci#include "aarch32/macro-assembler-aarch32.h" 41b8021494Sopenharmony_ci#include "aarch32/disasm-aarch32.h" 42b8021494Sopenharmony_ci 43b8021494Sopenharmony_ci#define __ masm. 44b8021494Sopenharmony_ci#define BUF_SIZE (4096) 45b8021494Sopenharmony_ci 46b8021494Sopenharmony_ci#ifdef VIXL_INCLUDE_SIMULATOR_AARCH32 47b8021494Sopenharmony_ci// Run tests with the simulator. 48b8021494Sopenharmony_ci 49b8021494Sopenharmony_ci#define SETUP() MacroAssembler masm(BUF_SIZE) 50b8021494Sopenharmony_ci 51b8021494Sopenharmony_ci#define START() masm.GetBuffer()->Reset() 52b8021494Sopenharmony_ci 53b8021494Sopenharmony_ci#define END() \ 54b8021494Sopenharmony_ci __ Hlt(0); \ 55b8021494Sopenharmony_ci __ FinalizeCode(); 56b8021494Sopenharmony_ci 57b8021494Sopenharmony_ci// TODO: Run the tests in the simulator. 58b8021494Sopenharmony_ci#define RUN() 59b8021494Sopenharmony_ci 60b8021494Sopenharmony_ci#else // ifdef VIXL_INCLUDE_SIMULATOR_AARCH32. 61b8021494Sopenharmony_ci 62b8021494Sopenharmony_ci#define SETUP() \ 63b8021494Sopenharmony_ci MacroAssembler masm(BUF_SIZE); \ 64b8021494Sopenharmony_ci UseScratchRegisterScope harness_scratch; \ 65b8021494Sopenharmony_ci 66b8021494Sopenharmony_ci#define START() \ 67b8021494Sopenharmony_ci harness_scratch.Open(&masm); \ 68b8021494Sopenharmony_ci harness_scratch.ExcludeAll(); \ 69b8021494Sopenharmony_ci masm.GetBuffer()->Reset(); \ 70b8021494Sopenharmony_ci __ Push(r4); \ 71b8021494Sopenharmony_ci __ Push(r5); \ 72b8021494Sopenharmony_ci __ Push(r6); \ 73b8021494Sopenharmony_ci __ Push(r7); \ 74b8021494Sopenharmony_ci __ Push(r8); \ 75b8021494Sopenharmony_ci __ Push(r9); \ 76b8021494Sopenharmony_ci __ Push(r10); \ 77b8021494Sopenharmony_ci __ Push(r11); \ 78b8021494Sopenharmony_ci __ Push(lr); \ 79b8021494Sopenharmony_ci harness_scratch.Include(ip); 80b8021494Sopenharmony_ci 81b8021494Sopenharmony_ci#define END() \ 82b8021494Sopenharmony_ci harness_scratch.Exclude(ip); \ 83b8021494Sopenharmony_ci __ Pop(lr); \ 84b8021494Sopenharmony_ci __ Pop(r11); \ 85b8021494Sopenharmony_ci __ Pop(r10); \ 86b8021494Sopenharmony_ci __ Pop(r9); \ 87b8021494Sopenharmony_ci __ Pop(r8); \ 88b8021494Sopenharmony_ci __ Pop(r7); \ 89b8021494Sopenharmony_ci __ Pop(r6); \ 90b8021494Sopenharmony_ci __ Pop(r5); \ 91b8021494Sopenharmony_ci __ Pop(r4); \ 92b8021494Sopenharmony_ci __ Bx(lr); \ 93b8021494Sopenharmony_ci __ FinalizeCode(); \ 94b8021494Sopenharmony_ci harness_scratch.Close(); 95b8021494Sopenharmony_ci 96b8021494Sopenharmony_ci#define RUN() \ 97b8021494Sopenharmony_ci { \ 98b8021494Sopenharmony_ci int pcs_offset = masm.IsUsingT32() ? 1 : 0; \ 99b8021494Sopenharmony_ci masm.GetBuffer()->SetExecutable(); \ 100b8021494Sopenharmony_ci ExecuteMemory(masm.GetBuffer()->GetStartAddress<byte*>(), \ 101b8021494Sopenharmony_ci masm.GetSizeOfCodeGenerated(), \ 102b8021494Sopenharmony_ci pcs_offset); \ 103b8021494Sopenharmony_ci masm.GetBuffer()->SetWritable(); \ 104b8021494Sopenharmony_ci } 105b8021494Sopenharmony_ci 106b8021494Sopenharmony_ci#endif // ifdef VIXL_INCLUDE_SIMULATOR_AARCH32 107b8021494Sopenharmony_ci 108b8021494Sopenharmony_cinamespace vixl { 109b8021494Sopenharmony_cinamespace aarch32 { 110b8021494Sopenharmony_ci 111b8021494Sopenharmony_ci// List of instruction encodings: 112b8021494Sopenharmony_ci#define FOREACH_INSTRUCTION(M) \ 113b8021494Sopenharmony_ci ${instruction_list_declaration} 114b8021494Sopenharmony_ci 115b8021494Sopenharmony_ci// The following definitions are defined again in each generated test, therefore 116b8021494Sopenharmony_ci// we need to place them in an anonymous namespace. It expresses that they are 117b8021494Sopenharmony_ci// local to this file only, and the compiler is not allowed to share these types 118b8021494Sopenharmony_ci// across test files during template instantiation. Specifically, `Operands` and 119b8021494Sopenharmony_ci// `Inputs` have various layouts across generated tests so they absolutely 120b8021494Sopenharmony_ci// cannot be shared. 121b8021494Sopenharmony_ci 122b8021494Sopenharmony_ci#ifdef ${isa_guard} 123b8021494Sopenharmony_cinamespace { 124b8021494Sopenharmony_ci 125b8021494Sopenharmony_ci// Values to be passed to the assembler to produce the instruction under test. 126b8021494Sopenharmony_cistruct Operands { 127b8021494Sopenharmony_ci ${operand_list_declaration} 128b8021494Sopenharmony_ci}; 129b8021494Sopenharmony_ci 130b8021494Sopenharmony_ci// Input data to feed to the instruction. 131b8021494Sopenharmony_cistruct Inputs { 132b8021494Sopenharmony_ci ${input_declarations} 133b8021494Sopenharmony_ci}; 134b8021494Sopenharmony_ci 135b8021494Sopenharmony_ci// This structure contains all input data needed to test one specific encoding. 136b8021494Sopenharmony_ci// It used to generate a loop over an instruction. 137b8021494Sopenharmony_cistruct TestLoopData { 138b8021494Sopenharmony_ci // The `operands` fields represents the values to pass to the assembler to 139b8021494Sopenharmony_ci // produce the instruction. 140b8021494Sopenharmony_ci Operands operands; 141b8021494Sopenharmony_ci // Description of the operands, used for error reporting. 142b8021494Sopenharmony_ci const char* operands_description; 143b8021494Sopenharmony_ci // Unique identifier, used for generating traces. 144b8021494Sopenharmony_ci const char* identifier; 145b8021494Sopenharmony_ci // Array of values to be fed to the instruction. 146b8021494Sopenharmony_ci size_t input_size; 147b8021494Sopenharmony_ci const Inputs* inputs; 148b8021494Sopenharmony_ci}; 149b8021494Sopenharmony_ci 150b8021494Sopenharmony_ci${input_definitions} 151b8021494Sopenharmony_ci 152b8021494Sopenharmony_ci// A loop will be generated for each element of this array. 153b8021494Sopenharmony_ciconst TestLoopData kTests[] = {${test_case_definitions}}; 154b8021494Sopenharmony_ci 155b8021494Sopenharmony_ci// We record all inputs to the instructions as outputs. This way, we also check 156b8021494Sopenharmony_ci// that what shouldn't change didn't change. 157b8021494Sopenharmony_cistruct TestResult { 158b8021494Sopenharmony_ci size_t output_size; 159b8021494Sopenharmony_ci const Inputs* outputs; 160b8021494Sopenharmony_ci}; 161b8021494Sopenharmony_ci 162b8021494Sopenharmony_ci// These headers each contain an array of `TestResult` with the reference output 163b8021494Sopenharmony_ci// values. The reference arrays are names `kReference{mnemonic}`. 164b8021494Sopenharmony_ci${include_trace_files} 165b8021494Sopenharmony_ci 166b8021494Sopenharmony_ci// The maximum number of errors to report in detail for each test. 167b8021494Sopenharmony_ciconst unsigned kErrorReportLimit = 8; 168b8021494Sopenharmony_ci 169b8021494Sopenharmony_citypedef void (MacroAssembler::*Fn)(${macroassembler_method_args}); 170b8021494Sopenharmony_ci 171b8021494Sopenharmony_civoid TestHelper(Fn instruction, const char* mnemonic, 172b8021494Sopenharmony_ci const TestResult reference[]) { 173b8021494Sopenharmony_ci SETUP(); 174b8021494Sopenharmony_ci ${macroassembler_set_isa} 175b8021494Sopenharmony_ci START(); 176b8021494Sopenharmony_ci 177b8021494Sopenharmony_ci // Data to compare to `reference`. 178b8021494Sopenharmony_ci TestResult* results[ARRAY_SIZE(kTests)]; 179b8021494Sopenharmony_ci 180b8021494Sopenharmony_ci // Test cases for memory bound instructions may allocate a buffer and save its 181b8021494Sopenharmony_ci // address in this array. 182b8021494Sopenharmony_ci byte* scratch_memory_buffers[ARRAY_SIZE(kTests)]; 183b8021494Sopenharmony_ci 184b8021494Sopenharmony_ci // Generate a loop for each element in `kTests`. Each loop tests one specific 185b8021494Sopenharmony_ci // instruction. 186b8021494Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) { 187b8021494Sopenharmony_ci // Allocate results on the heap for this test. 188b8021494Sopenharmony_ci results[i] = new TestResult; 189b8021494Sopenharmony_ci results[i]->outputs = new Inputs[kTests[i].input_size]; 190b8021494Sopenharmony_ci results[i]->output_size = kTests[i].input_size; 191b8021494Sopenharmony_ci 192b8021494Sopenharmony_ci size_t input_stride = sizeof(kTests[i].inputs[0]) * kTests[i].input_size; 193b8021494Sopenharmony_ci VIXL_ASSERT(IsUint32(input_stride)); 194b8021494Sopenharmony_ci 195b8021494Sopenharmony_ci scratch_memory_buffers[i] = NULL; 196b8021494Sopenharmony_ci 197b8021494Sopenharmony_ci Label loop; 198b8021494Sopenharmony_ci UseScratchRegisterScope scratch_registers(&masm); 199b8021494Sopenharmony_ci // Include all registers from r0 ro r12. 200b8021494Sopenharmony_ci scratch_registers.Include(RegisterList(0x1fff)); 201b8021494Sopenharmony_ci 202b8021494Sopenharmony_ci // Values to pass to the macro-assembler. 203b8021494Sopenharmony_ci ${code_instantiate_operands} 204b8021494Sopenharmony_ci 205b8021494Sopenharmony_ci // Allocate reserved registers for our own use. 206b8021494Sopenharmony_ci Register input_ptr = scratch_registers.Acquire(); 207b8021494Sopenharmony_ci Register input_end = scratch_registers.Acquire(); 208b8021494Sopenharmony_ci Register result_ptr = scratch_registers.Acquire(); 209b8021494Sopenharmony_ci 210b8021494Sopenharmony_ci // Initialize `input_ptr` to the first element and `input_end` the address 211b8021494Sopenharmony_ci // after the array. 212b8021494Sopenharmony_ci __ Mov(input_ptr, Operand::From(kTests[i].inputs)); 213b8021494Sopenharmony_ci __ Add(input_end, input_ptr, static_cast<uint32_t>(input_stride)); 214b8021494Sopenharmony_ci __ Mov(result_ptr, Operand::From(results[i]->outputs)); 215b8021494Sopenharmony_ci __ Bind(&loop); 216b8021494Sopenharmony_ci 217b8021494Sopenharmony_ci ${code_prologue} 218b8021494Sopenharmony_ci 219b8021494Sopenharmony_ci (masm.*instruction)(${code_parameter_list}); 220b8021494Sopenharmony_ci 221b8021494Sopenharmony_ci ${code_epilogue} 222b8021494Sopenharmony_ci 223b8021494Sopenharmony_ci // Advance the result pointer. 224b8021494Sopenharmony_ci __ Add(result_ptr, result_ptr, Operand::From(sizeof(kTests[i].inputs[0]))); 225b8021494Sopenharmony_ci // Loop back until `input_ptr` is lower than `input_base`. 226b8021494Sopenharmony_ci __ Add(input_ptr, input_ptr, Operand::From(sizeof(kTests[i].inputs[0]))); 227b8021494Sopenharmony_ci __ Cmp(input_ptr, input_end); 228b8021494Sopenharmony_ci __ B(ne, &loop); 229b8021494Sopenharmony_ci } 230b8021494Sopenharmony_ci 231b8021494Sopenharmony_ci END(); 232b8021494Sopenharmony_ci 233b8021494Sopenharmony_ci RUN(); 234b8021494Sopenharmony_ci 235b8021494Sopenharmony_ci if (Test::generate_test_trace()) { 236b8021494Sopenharmony_ci // Print the results. 237b8021494Sopenharmony_ci for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { 238b8021494Sopenharmony_ci printf("const Inputs kOutputs_%s_%s[] = {\n", mnemonic, 239b8021494Sopenharmony_ci kTests[i].identifier); 240b8021494Sopenharmony_ci for (size_t j = 0; j < results[i]->output_size; j++) { 241b8021494Sopenharmony_ci printf(" { "); 242b8021494Sopenharmony_ci ${trace_print_outputs} 243b8021494Sopenharmony_ci printf(" },\n"); 244b8021494Sopenharmony_ci } 245b8021494Sopenharmony_ci printf("};\n"); 246b8021494Sopenharmony_ci } 247b8021494Sopenharmony_ci printf("const TestResult kReference%s[] = {\n", mnemonic); 248b8021494Sopenharmony_ci for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { 249b8021494Sopenharmony_ci printf(" {\n"); 250b8021494Sopenharmony_ci printf(" ARRAY_SIZE(kOutputs_%s_%s),\n", mnemonic, 251b8021494Sopenharmony_ci kTests[i].identifier); 252b8021494Sopenharmony_ci printf(" kOutputs_%s_%s,\n", mnemonic, kTests[i].identifier); 253b8021494Sopenharmony_ci printf(" },\n"); 254b8021494Sopenharmony_ci } 255b8021494Sopenharmony_ci printf("};\n"); 256b8021494Sopenharmony_ci } else if (kCheckSimulatorTestResults) { 257b8021494Sopenharmony_ci // Check the results. 258b8021494Sopenharmony_ci unsigned total_error_count = 0; 259b8021494Sopenharmony_ci for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { 260b8021494Sopenharmony_ci bool instruction_has_errors = false; 261b8021494Sopenharmony_ci for (size_t j = 0; j < kTests[i].input_size; j++) { 262b8021494Sopenharmony_ci ${check_instantiate_results} 263b8021494Sopenharmony_ci ${check_instantiate_inputs} 264b8021494Sopenharmony_ci ${check_instantiate_references} 265b8021494Sopenharmony_ci 266b8021494Sopenharmony_ci if ((${check_results_against_references}) && 267b8021494Sopenharmony_ci (++total_error_count <= kErrorReportLimit)) { 268b8021494Sopenharmony_ci // Print the instruction once even if it triggered multiple failures. 269b8021494Sopenharmony_ci if (!instruction_has_errors) { 270b8021494Sopenharmony_ci printf("Error(s) when testing \"%s %s\":\n", mnemonic, 271b8021494Sopenharmony_ci kTests[i].operands_description); 272b8021494Sopenharmony_ci instruction_has_errors = true; 273b8021494Sopenharmony_ci } 274b8021494Sopenharmony_ci // Print subsequent errors. 275b8021494Sopenharmony_ci printf(" Input: "); 276b8021494Sopenharmony_ci ${check_print_input} 277b8021494Sopenharmony_ci printf("\n"); 278b8021494Sopenharmony_ci printf(" Expected: "); 279b8021494Sopenharmony_ci ${check_print_expected} 280b8021494Sopenharmony_ci printf("\n"); 281b8021494Sopenharmony_ci printf(" Found: "); 282b8021494Sopenharmony_ci ${check_print_found} 283b8021494Sopenharmony_ci printf("\n\n"); 284b8021494Sopenharmony_ci } 285b8021494Sopenharmony_ci } 286b8021494Sopenharmony_ci } 287b8021494Sopenharmony_ci 288b8021494Sopenharmony_ci if (total_error_count > kErrorReportLimit) { 289b8021494Sopenharmony_ci printf("%u other errors follow.\n", 290b8021494Sopenharmony_ci total_error_count - kErrorReportLimit); 291b8021494Sopenharmony_ci } 292b8021494Sopenharmony_ci VIXL_CHECK(total_error_count == 0); 293b8021494Sopenharmony_ci } else { 294b8021494Sopenharmony_ci VIXL_WARNING("Assembled the code, but did not run anything.\n"); 295b8021494Sopenharmony_ci } 296b8021494Sopenharmony_ci 297b8021494Sopenharmony_ci for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) { 298b8021494Sopenharmony_ci delete[] results[i]->outputs; 299b8021494Sopenharmony_ci delete results[i]; 300b8021494Sopenharmony_ci delete[] scratch_memory_buffers[i]; 301b8021494Sopenharmony_ci } 302b8021494Sopenharmony_ci} 303b8021494Sopenharmony_ci 304b8021494Sopenharmony_ci// Instantiate tests for each instruction in the list. 305b8021494Sopenharmony_ci// TODO: Remove this limitation by having a sandboxing mechanism. 306b8021494Sopenharmony_ci#if defined(VIXL_HOST_POINTER_32) 307b8021494Sopenharmony_ci#define TEST(mnemonic) \ 308b8021494Sopenharmony_ci void Test_##mnemonic() { \ 309b8021494Sopenharmony_ci TestHelper(&MacroAssembler::mnemonic, #mnemonic, kReference##mnemonic); \ 310b8021494Sopenharmony_ci } \ 311b8021494Sopenharmony_ci Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic "_${test_isa}", \ 312b8021494Sopenharmony_ci &Test_##mnemonic); 313b8021494Sopenharmony_ci#else 314b8021494Sopenharmony_ci#define TEST(mnemonic) \ 315b8021494Sopenharmony_ci void Test_##mnemonic() { \ 316b8021494Sopenharmony_ci VIXL_WARNING("This test can only run on a 32-bit host.\n"); \ 317b8021494Sopenharmony_ci USE(TestHelper); \ 318b8021494Sopenharmony_ci } \ 319b8021494Sopenharmony_ci Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic "_${test_isa}", \ 320b8021494Sopenharmony_ci &Test_##mnemonic); 321b8021494Sopenharmony_ci#endif 322b8021494Sopenharmony_ci 323b8021494Sopenharmony_ciFOREACH_INSTRUCTION(TEST) 324b8021494Sopenharmony_ci#undef TEST 325b8021494Sopenharmony_ci 326b8021494Sopenharmony_ci} // namespace 327b8021494Sopenharmony_ci#endif 328b8021494Sopenharmony_ci 329b8021494Sopenharmony_ci} // namespace aarch32 330b8021494Sopenharmony_ci} // namespace vixl 331