1// Copyright 2017, VIXL authors 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are met: 6// 7// * Redistributions of source code must retain the above copyright notice, 8// this list of conditions and the following disclaimer. 9// * Redistributions in binary form must reproduce the above copyright notice, 10// this list of conditions and the following disclaimer in the documentation 11// and/or other materials provided with the distribution. 12// * Neither the name of ARM Limited nor the names of its contributors may be 13// used to endorse or promote products derived from this software without 14// specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27/// This file is a template read by tools/generate_tests.py, it isn't valid C++ 28/// as it is. Variables written as ${substitute_me} are replaced by the script. 29/// Comments starting with three forward slashes such as this one are also 30/// removed. 31 32${do_not_edit_comment} 33 34#include <map> 35 36#include "test-runner.h" 37 38#include "test-utils.h" 39 40#include "aarch32/macro-assembler-aarch32.h" 41 42#define BUF_SIZE (4096) 43 44namespace vixl { 45namespace aarch32 { 46 47// List of instruction mnemonics. 48#define FOREACH_INSTRUCTION(M) \ 49 ${instruction_list_declaration} 50 51// The following definitions are defined again in each generated test, therefore 52// we need to place them in an anomymous namespace. It expresses that they are 53// local to this file only, and the compiler is not allowed to share these types 54// across test files during template instantiation. Specifically, `Operands` has 55// various layouts across generated tests so it absolutely cannot be shared. 56 57namespace { 58 59// Values to be passed to the assembler to produce the instruction under test. 60struct Operands { 61 ${operand_list_declaration} 62}; 63 64// This structure contains all data needed to test one specific 65// instruction. 66struct TestData { 67 // The `operands` field represents what to pass to the assembler to 68 // produce the instruction. 69 Operands operands; 70 // Description of the operands, used for error reporting. 71 const char* operands_description; 72 // Unique identifier, used for generating traces. 73 const char* identifier; 74}; 75 76#ifdef VIXL_NEGATIVE_TESTING 77// Each element of this array produce one instruction encoding. 78const TestData kTests[] = {${test_case_definitions}}; 79#endif 80 81typedef void (MacroAssembler::*Fn)(${macroassembler_method_args}); 82 83#ifdef VIXL_NEGATIVE_TESTING 84void TestHelper(Fn instruction, const char* mnemonic) { 85 for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) { 86 MacroAssembler masm(BUF_SIZE); 87 ${macroassembler_set_isa} 88 89 // Values to pass to the assembler. 90 ${code_instantiate_operands} 91 92 try { 93 { 94 ExactAssemblyScope scope(&masm, 4, ExactAssemblyScope::kMaximumSize); 95 (masm.*instruction)(${code_parameter_list}); 96 } 97 printf("\nNegative test for: %s\n", mnemonic); 98 printf("%s:%d:%s\nNo exception raised.\n", 99 __FILE__, 100 __LINE__, 101 masm.IsUsingT32() ? "T32" : "A32"); 102 abort(); 103 } catch (const std::runtime_error&) { 104 // Nothing to do, test passed. 105 // TODO: Consider checking the error message here, if possible. 106 } 107 } 108} 109#else 110void TestHelper(Fn, const char*) { 111 printf("Skipping negative tests. To enable them, build with" 112 " 'negative_testing=on'.\n"); 113} 114#endif 115 116// Instantiate tests for each instruction in the list. 117#define TEST(mnemonic) \ 118 void Test_##mnemonic() { \ 119 TestHelper(&MacroAssembler::mnemonic, #mnemonic); \ 120 } \ 121 Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic "_${test_isa}", \ 122 &Test_##mnemonic); 123FOREACH_INSTRUCTION(TEST) 124#undef TEST 125 126} // namespace 127 128} // namespace aarch32 129} // namespace vixl 130