1b8021494Sopenharmony_ci// Copyright 2017, 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 <map>
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_ci#include "test-runner.h"
37b8021494Sopenharmony_ci
38b8021494Sopenharmony_ci#include "test-utils.h"
39b8021494Sopenharmony_ci
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  // Description of the operands, used for error reporting.
72b8021494Sopenharmony_ci  const char* operands_description;
73b8021494Sopenharmony_ci  // Unique identifier, used for generating traces.
74b8021494Sopenharmony_ci  const char* identifier;
75b8021494Sopenharmony_ci};
76b8021494Sopenharmony_ci
77b8021494Sopenharmony_ci#ifdef VIXL_NEGATIVE_TESTING
78b8021494Sopenharmony_ci// Each element of this array produce one instruction encoding.
79b8021494Sopenharmony_ciconst TestData kTests[] = {${test_case_definitions}};
80b8021494Sopenharmony_ci#endif
81b8021494Sopenharmony_ci
82b8021494Sopenharmony_citypedef void (MacroAssembler::*Fn)(${macroassembler_method_args});
83b8021494Sopenharmony_ci
84b8021494Sopenharmony_ci#ifdef VIXL_NEGATIVE_TESTING
85b8021494Sopenharmony_civoid TestHelper(Fn instruction, const char* mnemonic) {
86b8021494Sopenharmony_ci  for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) {
87b8021494Sopenharmony_ci    MacroAssembler masm(BUF_SIZE);
88b8021494Sopenharmony_ci    ${macroassembler_set_isa}
89b8021494Sopenharmony_ci
90b8021494Sopenharmony_ci    // Values to pass to the assembler.
91b8021494Sopenharmony_ci    ${code_instantiate_operands}
92b8021494Sopenharmony_ci
93b8021494Sopenharmony_ci    try {
94b8021494Sopenharmony_ci      {
95b8021494Sopenharmony_ci        ExactAssemblyScope scope(&masm, 4, ExactAssemblyScope::kMaximumSize);
96b8021494Sopenharmony_ci        (masm.*instruction)(${code_parameter_list});
97b8021494Sopenharmony_ci      }
98b8021494Sopenharmony_ci      printf("\nNegative test for: %s\n", mnemonic);
99b8021494Sopenharmony_ci      printf("%s:%d:%s\nNo exception raised.\n",
100b8021494Sopenharmony_ci             __FILE__,
101b8021494Sopenharmony_ci             __LINE__,
102b8021494Sopenharmony_ci             masm.IsUsingT32() ? "T32" : "A32");
103b8021494Sopenharmony_ci      abort();
104b8021494Sopenharmony_ci    } catch (const std::runtime_error&) {
105b8021494Sopenharmony_ci      // Nothing to do, test passed.
106b8021494Sopenharmony_ci      // TODO: Consider checking the error message here, if possible.
107b8021494Sopenharmony_ci    }
108b8021494Sopenharmony_ci  }
109b8021494Sopenharmony_ci}
110b8021494Sopenharmony_ci#else
111b8021494Sopenharmony_civoid TestHelper(Fn, const char*) {
112b8021494Sopenharmony_ci  printf("Skipping negative tests. To enable them, build with"
113b8021494Sopenharmony_ci         " 'negative_testing=on'.\n");
114b8021494Sopenharmony_ci}
115b8021494Sopenharmony_ci#endif
116b8021494Sopenharmony_ci
117b8021494Sopenharmony_ci// Instantiate tests for each instruction in the list.
118b8021494Sopenharmony_ci#define TEST(mnemonic)                                                   \
119b8021494Sopenharmony_ci  void Test_##mnemonic() {                                               \
120b8021494Sopenharmony_ci    TestHelper(&MacroAssembler::mnemonic, #mnemonic);                    \
121b8021494Sopenharmony_ci  }                                                                      \
122b8021494Sopenharmony_ci  Test test_##mnemonic("AARCH32_${test_name}_" #mnemonic "_${test_isa}", \
123b8021494Sopenharmony_ci                       &Test_##mnemonic);
124b8021494Sopenharmony_ciFOREACH_INSTRUCTION(TEST)
125b8021494Sopenharmony_ci#undef TEST
126b8021494Sopenharmony_ci
127b8021494Sopenharmony_ci}  // namespace
128b8021494Sopenharmony_ci#endif
129b8021494Sopenharmony_ci
130b8021494Sopenharmony_ci}  // namespace aarch32
131b8021494Sopenharmony_ci}  // namespace vixl
132