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 42b8021494Sopenharmony_ci#define BUF_SIZE (4096) 43b8021494Sopenharmony_ci 44b8021494Sopenharmony_cinamespace vixl { 45b8021494Sopenharmony_cinamespace aarch32 { 46b8021494Sopenharmony_ci 47b8021494Sopenharmony_ci// List of instruction mnemonics. 48b8021494Sopenharmony_ci#define FOREACH_INSTRUCTION(M) \ 49b8021494Sopenharmony_ci ${instruction_list_declaration} 50b8021494Sopenharmony_ci 51b8021494Sopenharmony_ci// The following definitions are defined again in each generated test, therefore 52b8021494Sopenharmony_ci// we need to place them in an anonymous namespace. It expresses that they are 53b8021494Sopenharmony_ci// local to this file only, and the compiler is not allowed to share these types 54b8021494Sopenharmony_ci// across test files during template instantiation. Specifically, `Operands` has 55b8021494Sopenharmony_ci// various layouts across generated tests so it absolutely cannot be shared. 56b8021494Sopenharmony_ci 57b8021494Sopenharmony_ci#ifdef ${isa_guard} 58b8021494Sopenharmony_cinamespace { 59b8021494Sopenharmony_ci 60b8021494Sopenharmony_ci// Values to be passed to the assembler to produce the instruction under test. 61b8021494Sopenharmony_cistruct Operands { 62b8021494Sopenharmony_ci ${operand_list_declaration} 63b8021494Sopenharmony_ci}; 64b8021494Sopenharmony_ci 65b8021494Sopenharmony_ci// This structure contains all data needed to test one specific 66b8021494Sopenharmony_ci// instruction. 67b8021494Sopenharmony_cistruct TestData { 68b8021494Sopenharmony_ci // The `operands` field represents what to pass to the assembler to 69b8021494Sopenharmony_ci // produce the instruction. 70b8021494Sopenharmony_ci Operands operands; 71b8021494Sopenharmony_ci // True if we need to generate an IT instruction for this test to be valid. 72b8021494Sopenharmony_ci bool in_it_block; 73b8021494Sopenharmony_ci // The condition to give the IT instruction, this will be set to "al" by 74b8021494Sopenharmony_ci // default. 75b8021494Sopenharmony_ci Condition it_condition; 76b8021494Sopenharmony_ci // Description of the operands, used for error reporting. 77b8021494Sopenharmony_ci const char* operands_description; 78b8021494Sopenharmony_ci // Unique identifier, used for generating traces. 79b8021494Sopenharmony_ci const char* identifier; 80b8021494Sopenharmony_ci}; 81b8021494Sopenharmony_ci 82b8021494Sopenharmony_cistruct TestResult { 83b8021494Sopenharmony_ci size_t size; 84b8021494Sopenharmony_ci const byte* encoding; 85b8021494Sopenharmony_ci}; 86b8021494Sopenharmony_ci 87b8021494Sopenharmony_ci// Each element of this array produce one instruction encoding. 88b8021494Sopenharmony_ciconst TestData kTests[] = {${test_case_definitions}}; 89b8021494Sopenharmony_ci 90b8021494Sopenharmony_ci// These headers each contain an array of `TestResult` with the reference output 91b8021494Sopenharmony_ci// values. The reference arrays are names `kReference{mnemonic}`. 92b8021494Sopenharmony_ci${include_trace_files} 93b8021494Sopenharmony_ci 94b8021494Sopenharmony_ci// The maximum number of errors to report in detail for each test. 95b8021494Sopenharmony_ciconst unsigned kErrorReportLimit = 8; 96b8021494Sopenharmony_ci 97b8021494Sopenharmony_citypedef void (MacroAssembler::*Fn)(${macroassembler_method_args}); 98b8021494Sopenharmony_ci 99b8021494Sopenharmony_civoid TestHelper(Fn instruction, const char* mnemonic, 100b8021494Sopenharmony_ci const TestResult reference[]) { 101b8021494Sopenharmony_ci unsigned total_error_count = 0; 102b8021494Sopenharmony_ci MacroAssembler masm(BUF_SIZE); 103b8021494Sopenharmony_ci 104b8021494Sopenharmony_ci ${macroassembler_set_isa} 105b8021494Sopenharmony_ci 106b8021494Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) { 107b8021494Sopenharmony_ci // Values to pass to the macro-assembler. 108b8021494Sopenharmony_ci ${code_instantiate_operands} 109b8021494Sopenharmony_ci 110b8021494Sopenharmony_ci int32_t start = masm.GetCursorOffset(); 111b8021494Sopenharmony_ci { 112b8021494Sopenharmony_ci // We never generate more that 4 bytes, as IT instructions are only 113b8021494Sopenharmony_ci // allowed for narrow encodings. 114b8021494Sopenharmony_ci ExactAssemblyScope scope(&masm, 4, ExactAssemblyScope::kMaximumSize); 115b8021494Sopenharmony_ci if (kTests[i].in_it_block) { 116b8021494Sopenharmony_ci masm.it(kTests[i].it_condition); 117b8021494Sopenharmony_ci } 118b8021494Sopenharmony_ci (masm.*instruction)(${code_parameter_list}); 119b8021494Sopenharmony_ci } 120b8021494Sopenharmony_ci int32_t end = masm.GetCursorOffset(); 121b8021494Sopenharmony_ci 122b8021494Sopenharmony_ci const byte* result_ptr = 123b8021494Sopenharmony_ci masm.GetBuffer()->GetOffsetAddress<const byte*>(start); 124b8021494Sopenharmony_ci VIXL_ASSERT(start < end); 125b8021494Sopenharmony_ci uint32_t result_size = end - start; 126b8021494Sopenharmony_ci 127b8021494Sopenharmony_ci if (Test::generate_test_trace()) { 128b8021494Sopenharmony_ci // Print the result bytes. 129b8021494Sopenharmony_ci printf("const byte kInstruction_%s_%s[] = {\n", mnemonic, 130b8021494Sopenharmony_ci kTests[i].identifier); 131b8021494Sopenharmony_ci for (uint32_t j = 0; j < result_size; j++) { 132b8021494Sopenharmony_ci if (j == 0) { 133b8021494Sopenharmony_ci printf(" 0x%02" PRIx8, result_ptr[j]); 134b8021494Sopenharmony_ci } else { 135b8021494Sopenharmony_ci printf(", 0x%02" PRIx8, result_ptr[j]); 136b8021494Sopenharmony_ci } 137b8021494Sopenharmony_ci } 138b8021494Sopenharmony_ci // This comment is meant to be used by external tools to validate 139b8021494Sopenharmony_ci // the encoding. We can parse the comment to figure out what 140b8021494Sopenharmony_ci // instruction this corresponds to. 141b8021494Sopenharmony_ci if (kTests[i].in_it_block) { 142b8021494Sopenharmony_ci printf(" // It %s; %s %s\n};\n", kTests[i].it_condition.GetName(), 143b8021494Sopenharmony_ci mnemonic, kTests[i].operands_description); 144b8021494Sopenharmony_ci } else { 145b8021494Sopenharmony_ci printf(" // %s %s\n};\n", mnemonic, kTests[i].operands_description); 146b8021494Sopenharmony_ci } 147b8021494Sopenharmony_ci } else { 148b8021494Sopenharmony_ci // Check we've emitted the exact same encoding as present in the 149b8021494Sopenharmony_ci // trace file. Only print up to `kErrorReportLimit` errors. 150b8021494Sopenharmony_ci if (((result_size != reference[i].size) || 151b8021494Sopenharmony_ci (memcmp(result_ptr, 152b8021494Sopenharmony_ci reference[i].encoding, 153b8021494Sopenharmony_ci reference[i].size) != 0)) && 154b8021494Sopenharmony_ci (++total_error_count <= kErrorReportLimit)) { 155b8021494Sopenharmony_ci printf("Error when testing \"%s\" with operands \"%s\":\n", mnemonic, 156b8021494Sopenharmony_ci kTests[i].operands_description); 157b8021494Sopenharmony_ci printf(" Expected: "); 158b8021494Sopenharmony_ci for (uint32_t j = 0; j < reference[i].size; j++) { 159b8021494Sopenharmony_ci if (j == 0) { 160b8021494Sopenharmony_ci printf("0x%02" PRIx8, reference[i].encoding[j]); 161b8021494Sopenharmony_ci } else { 162b8021494Sopenharmony_ci printf(", 0x%02" PRIx8, reference[i].encoding[j]); 163b8021494Sopenharmony_ci } 164b8021494Sopenharmony_ci } 165b8021494Sopenharmony_ci printf("\n"); 166b8021494Sopenharmony_ci printf(" Found: "); 167b8021494Sopenharmony_ci for (uint32_t j = 0; j < result_size; j++) { 168b8021494Sopenharmony_ci if (j == 0) { 169b8021494Sopenharmony_ci printf("0x%02" PRIx8, result_ptr[j]); 170b8021494Sopenharmony_ci } else { 171b8021494Sopenharmony_ci printf(", 0x%02" PRIx8, result_ptr[j]); 172b8021494Sopenharmony_ci } 173b8021494Sopenharmony_ci } 174b8021494Sopenharmony_ci printf("\n"); 175b8021494Sopenharmony_ci } 176b8021494Sopenharmony_ci } 177b8021494Sopenharmony_ci } 178b8021494Sopenharmony_ci 179b8021494Sopenharmony_ci masm.FinalizeCode(); 180b8021494Sopenharmony_ci 181b8021494Sopenharmony_ci if (Test::generate_test_trace()) { 182b8021494Sopenharmony_ci // Finalize the trace file by writing the final `TestResult` array 183b8021494Sopenharmony_ci // which links all generated instruction encodings. 184b8021494Sopenharmony_ci printf("const TestResult kReference%s[] = {\n", mnemonic); 185b8021494Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) { 186b8021494Sopenharmony_ci printf(" {\n"); 187b8021494Sopenharmony_ci printf(" ARRAY_SIZE(kInstruction_%s_%s),\n", mnemonic, 188b8021494Sopenharmony_ci kTests[i].identifier); 189b8021494Sopenharmony_ci printf(" kInstruction_%s_%s,\n", mnemonic, kTests[i].identifier); 190b8021494Sopenharmony_ci printf(" },\n"); 191b8021494Sopenharmony_ci } 192b8021494Sopenharmony_ci printf("};\n"); 193b8021494Sopenharmony_ci } else { 194b8021494Sopenharmony_ci if (total_error_count > kErrorReportLimit) { 195b8021494Sopenharmony_ci printf("%u other errors follow.\n", 196b8021494Sopenharmony_ci total_error_count - kErrorReportLimit); 197b8021494Sopenharmony_ci } 198b8021494Sopenharmony_ci // Crash if the test failed. 199b8021494Sopenharmony_ci VIXL_CHECK(total_error_count == 0); 200b8021494Sopenharmony_ci } 201b8021494Sopenharmony_ci} 202b8021494Sopenharmony_ci 203b8021494Sopenharmony_ci// Instantiate tests for each instruction in the list. 204b8021494Sopenharmony_ci#define TEST(mnemonic) \ 205b8021494Sopenharmony_ci void Test_##mnemonic() { \ 206b8021494Sopenharmony_ci TestHelper(&MacroAssembler::mnemonic, #mnemonic, kReference##mnemonic); \ 207b8021494Sopenharmony_ci } \ 208b8021494Sopenharmony_ci Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic "_${test_isa}", \ 209b8021494Sopenharmony_ci &Test_##mnemonic); 210b8021494Sopenharmony_ciFOREACH_INSTRUCTION(TEST) 211b8021494Sopenharmony_ci#undef TEST 212b8021494Sopenharmony_ci 213b8021494Sopenharmony_ci} // namespace 214b8021494Sopenharmony_ci#endif 215b8021494Sopenharmony_ci 216b8021494Sopenharmony_ci} // namespace aarch32 217b8021494Sopenharmony_ci} // namespace vixl 218