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 <iostream>
35b8021494Sopenharmony_ci#include <map>
36b8021494Sopenharmony_ci
37b8021494Sopenharmony_ci#include "test-runner.h"
38b8021494Sopenharmony_ci
39b8021494Sopenharmony_ci#include "test-utils.h"
40b8021494Sopenharmony_ci#include "test-utils-aarch32.h"
41b8021494Sopenharmony_ci
42b8021494Sopenharmony_ci#include "aarch32/assembler-aarch32.h"
43b8021494Sopenharmony_ci#include "aarch32/disasm-aarch32.h"
44b8021494Sopenharmony_ci#include "aarch32/macro-assembler-aarch32.h"
45b8021494Sopenharmony_ci
46b8021494Sopenharmony_ci#define BUF_SIZE (4096)
47b8021494Sopenharmony_ci
48b8021494Sopenharmony_cinamespace vixl {
49b8021494Sopenharmony_cinamespace aarch32 {
50b8021494Sopenharmony_ci
51b8021494Sopenharmony_ci// List of instruction mnemonics.
52b8021494Sopenharmony_ci#define FOREACH_INSTRUCTION(M) \
53b8021494Sopenharmony_ci  ${instruction_list_declaration}
54b8021494Sopenharmony_ci
55b8021494Sopenharmony_ci// The following definitions are defined again in each generated test, therefore
56b8021494Sopenharmony_ci// we need to place them in an anonymous namespace. It expresses that they are
57b8021494Sopenharmony_ci// local to this file only, and the compiler is not allowed to share these types
58b8021494Sopenharmony_ci// across test files during template instantiation. Specifically, `Operands` has
59b8021494Sopenharmony_ci// various layouts across generated tests so it absolutely cannot be shared.
60b8021494Sopenharmony_ci
61b8021494Sopenharmony_ci#ifdef ${isa_guard}
62b8021494Sopenharmony_cinamespace {
63b8021494Sopenharmony_ci
64b8021494Sopenharmony_ci// Values to be passed to the assembler to produce the instruction under test.
65b8021494Sopenharmony_cistruct Operands {
66b8021494Sopenharmony_ci  ${operand_list_declaration}
67b8021494Sopenharmony_ci};
68b8021494Sopenharmony_ci
69b8021494Sopenharmony_ci// This structure contains all data needed to test one specific
70b8021494Sopenharmony_ci// instruction.
71b8021494Sopenharmony_cistruct TestData {
72b8021494Sopenharmony_ci  // The `operands` field represents what to pass to the assembler to
73b8021494Sopenharmony_ci  // produce the instruction.
74b8021494Sopenharmony_ci  Operands operands;
75b8021494Sopenharmony_ci  // Description of the operands, used for error reporting.
76b8021494Sopenharmony_ci  const char* operands_description;
77b8021494Sopenharmony_ci  // Unique identifier, used for generating traces.
78b8021494Sopenharmony_ci  const char* identifier;
79b8021494Sopenharmony_ci};
80b8021494Sopenharmony_ci
81b8021494Sopenharmony_ci// Each element of this array produce one instruction encoding.
82b8021494Sopenharmony_ciconst TestData kTests[] = {${test_case_definitions}};
83b8021494Sopenharmony_ci
84b8021494Sopenharmony_citypedef void (MacroAssembler::*Fn)(${macroassembler_method_args});
85b8021494Sopenharmony_ci
86b8021494Sopenharmony_ci// Use a customised disassembler to label test cases.
87b8021494Sopenharmony_citypedef std::map<uint32_t, int> TestCaseSymbolMap;
88b8021494Sopenharmony_ci
89b8021494Sopenharmony_ciclass TestDisassembler : public PrintDisassembler {
90b8021494Sopenharmony_ci public:
91b8021494Sopenharmony_ci  TestDisassembler(std::ostream& os,  // NOLINT(runtime/references)
92b8021494Sopenharmony_ci                   const TestCaseSymbolMap& symbols,
93b8021494Sopenharmony_ci                   const char* mnemonic)
94b8021494Sopenharmony_ci      : PrintDisassembler(os), symbols_(symbols), mnemonic_(mnemonic) {}
95b8021494Sopenharmony_ci
96b8021494Sopenharmony_ci  virtual void PrintCodeAddress(uint32_t pc) VIXL_OVERRIDE {
97b8021494Sopenharmony_ci    // Label test cases.
98b8021494Sopenharmony_ci    TestCaseSymbolMap::const_iterator symbol = symbols_.find(pc);
99b8021494Sopenharmony_ci    if (symbol != symbols_.end()) {
100b8021494Sopenharmony_ci      int n = symbol->second;
101b8021494Sopenharmony_ci      os().os() << "// " << mnemonic_ << "(" <<
102b8021494Sopenharmony_ci          kTests[n].operands_description << ")" << std::endl;
103b8021494Sopenharmony_ci    }
104b8021494Sopenharmony_ci    // Print the code address as normal.
105b8021494Sopenharmony_ci    PrintDisassembler::PrintCodeAddress(pc);
106b8021494Sopenharmony_ci  }
107b8021494Sopenharmony_ci
108b8021494Sopenharmony_ci private:
109b8021494Sopenharmony_ci  const TestCaseSymbolMap& symbols_;
110b8021494Sopenharmony_ci  const char* mnemonic_;
111b8021494Sopenharmony_ci};
112b8021494Sopenharmony_ci
113b8021494Sopenharmony_civoid TestHelper(Fn instruction, const char* mnemonic) {
114b8021494Sopenharmony_ci  MacroAssembler masm(BUF_SIZE);
115b8021494Sopenharmony_ci
116b8021494Sopenharmony_ci  ${macroassembler_set_isa}
117b8021494Sopenharmony_ci
118b8021494Sopenharmony_ci  TestCaseSymbolMap symbols;
119b8021494Sopenharmony_ci
120b8021494Sopenharmony_ci  for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) {
121b8021494Sopenharmony_ci    if (Test::disassemble()) {
122b8021494Sopenharmony_ci      // TODO: This will fail if the MacroAssembler generates no code. We can
123b8021494Sopenharmony_ci      // fix this with multimap but then we must take care to print the labels
124b8021494Sopenharmony_ci      // in the correct order. (Insertion order is only preserved for C++11.)
125b8021494Sopenharmony_ci      symbols.insert(std::pair<uint32_t, int>(masm.GetCursorOffset(), i));
126b8021494Sopenharmony_ci    }
127b8021494Sopenharmony_ci
128b8021494Sopenharmony_ci    // Values to pass to the macro-assembler.
129b8021494Sopenharmony_ci    UseScratchRegisterScope scratch_registers(&masm);
130b8021494Sopenharmony_ci    ${code_instantiate_operands}
131b8021494Sopenharmony_ci
132b8021494Sopenharmony_ci    (masm.*instruction)(${code_parameter_list});
133b8021494Sopenharmony_ci
134b8021494Sopenharmony_ci    // For now, these test don't currently produce (or check) any trace; we just
135b8021494Sopenharmony_ci    // check that the MacroAssembler didn't crash.
136b8021494Sopenharmony_ci    // TODO: We could generate disassembly as a trace here, to check for sane
137b8021494Sopenharmony_ci    // output, though the trace would need to be manually checked.
138b8021494Sopenharmony_ci  }
139b8021494Sopenharmony_ci
140b8021494Sopenharmony_ci  masm.FinalizeCode();
141b8021494Sopenharmony_ci
142b8021494Sopenharmony_ci  if (Test::disassemble()) {
143b8021494Sopenharmony_ci    // Disassemble to stdout if given --disassemble on the command line.
144b8021494Sopenharmony_ci    TestDisassembler dis(std::cout, symbols, mnemonic);
145b8021494Sopenharmony_ci    if (masm.IsUsingT32()) {
146b8021494Sopenharmony_ci      dis.DisassembleT32Buffer(masm.GetBuffer()->GetStartAddress<uint16_t*>(),
147b8021494Sopenharmony_ci                               masm.GetCursorOffset());
148b8021494Sopenharmony_ci    } else {
149b8021494Sopenharmony_ci      dis.DisassembleA32Buffer(masm.GetBuffer()->GetStartAddress<uint32_t*>(),
150b8021494Sopenharmony_ci                               masm.GetCursorOffset());
151b8021494Sopenharmony_ci    }
152b8021494Sopenharmony_ci  }
153b8021494Sopenharmony_ci}
154b8021494Sopenharmony_ci
155b8021494Sopenharmony_ci// Instantiate tests for each instruction in the list.
156b8021494Sopenharmony_ci#define TEST(mnemonic)                                    \
157b8021494Sopenharmony_ci  void Test_##mnemonic() {                                \
158b8021494Sopenharmony_ci    TestHelper(&MacroAssembler::mnemonic, #mnemonic);     \
159b8021494Sopenharmony_ci  }                                                       \
160b8021494Sopenharmony_ci  Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic, \
161b8021494Sopenharmony_ci                              &Test_##mnemonic);
162b8021494Sopenharmony_ciFOREACH_INSTRUCTION(TEST)
163b8021494Sopenharmony_ci#undef TEST
164b8021494Sopenharmony_ci
165b8021494Sopenharmony_ci}  // namespace
166b8021494Sopenharmony_ci#endif
167b8021494Sopenharmony_ci
168b8021494Sopenharmony_ci}  // namespace aarch32
169b8021494Sopenharmony_ci}  // namespace vixl
170