1b8021494Sopenharmony_ci// Copyright 2019, 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#define TEST(name) TEST_(AARCH64_DISASM_##name)
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ci#define SETUP_COMMON()                                \
30b8021494Sopenharmony_ci  MacroAssembler masm;                                \
31b8021494Sopenharmony_ci  masm.GetCPUFeatures()->Combine(CPUFeatures::All()); \
32b8021494Sopenharmony_ci  Decoder decoder;                                    \
33b8021494Sopenharmony_ci  Disassembler disasm;                                \
34b8021494Sopenharmony_ci  decoder.AppendVisitor(&disasm)
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_ci#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
37b8021494Sopenharmony_ci// Run tests with the simulator.
38b8021494Sopenharmony_ci#define SETUP()   \
39b8021494Sopenharmony_ci  SETUP_COMMON(); \
40b8021494Sopenharmony_ci  masm.SetGenerateSimulatorCode(true)
41b8021494Sopenharmony_ci
42b8021494Sopenharmony_ci#else  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH64.
43b8021494Sopenharmony_ci#define SETUP()   \
44b8021494Sopenharmony_ci  SETUP_COMMON(); \
45b8021494Sopenharmony_ci  masm.SetGenerateSimulatorCode(false)
46b8021494Sopenharmony_ci
47b8021494Sopenharmony_ci#endif  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH64.
48b8021494Sopenharmony_ci
49b8021494Sopenharmony_ci// A conservative limit for the size of the code that we generate in these
50b8021494Sopenharmony_ci// tests.
51b8021494Sopenharmony_ci#define MAX_SIZE_GENERATED 1024
52b8021494Sopenharmony_ci
53b8021494Sopenharmony_ci#define DISASSEMBLE()                                                       \
54b8021494Sopenharmony_ci  do {                                                                      \
55b8021494Sopenharmony_ci    printf("----\n");                                                       \
56b8021494Sopenharmony_ci    PrintDisassembler print_disasm(stdout);                                 \
57b8021494Sopenharmony_ci    Instruction* dis_start =                                                \
58b8021494Sopenharmony_ci        masm.GetBuffer()->GetStartAddress<Instruction*>();                  \
59b8021494Sopenharmony_ci    Instruction* dis_end = masm.GetBuffer()->GetEndAddress<Instruction*>(); \
60b8021494Sopenharmony_ci    print_disasm.DisassembleBuffer(dis_start, dis_end);                     \
61b8021494Sopenharmony_ci  } while (0)
62b8021494Sopenharmony_ci
63b8021494Sopenharmony_ci#define COMPARE(ASM, EXP)                                                \
64b8021494Sopenharmony_ci  do {                                                                   \
65b8021494Sopenharmony_ci    masm.Reset();                                                        \
66b8021494Sopenharmony_ci    {                                                                    \
67b8021494Sopenharmony_ci      ExactAssemblyScope guard(&masm,                                    \
68b8021494Sopenharmony_ci                               MAX_SIZE_GENERATED,                       \
69b8021494Sopenharmony_ci                               ExactAssemblyScope::kMaximumSize);        \
70b8021494Sopenharmony_ci      masm.ASM;                                                          \
71b8021494Sopenharmony_ci    }                                                                    \
72b8021494Sopenharmony_ci    masm.FinalizeCode();                                                 \
73b8021494Sopenharmony_ci    decoder.Decode(masm.GetBuffer()->GetStartAddress<Instruction*>());   \
74b8021494Sopenharmony_ci    uint32_t encoding = *masm.GetBuffer()->GetStartAddress<uint32_t*>(); \
75b8021494Sopenharmony_ci    if (strcmp(disasm.GetOutput(), EXP) != 0) {                          \
76b8021494Sopenharmony_ci      printf("\nEncoding: %08" PRIx32 "\nExpected: %s\nFound:    %s\n",  \
77b8021494Sopenharmony_ci             encoding,                                                   \
78b8021494Sopenharmony_ci             EXP,                                                        \
79b8021494Sopenharmony_ci             disasm.GetOutput());                                        \
80b8021494Sopenharmony_ci      abort();                                                           \
81b8021494Sopenharmony_ci    }                                                                    \
82b8021494Sopenharmony_ci    if (Test::disassemble()) DISASSEMBLE();                              \
83b8021494Sopenharmony_ci  } while (0)
84b8021494Sopenharmony_ci
85b8021494Sopenharmony_ci#define COMPARE_PREFIX(ASM, EXP)                                         \
86b8021494Sopenharmony_ci  do {                                                                   \
87b8021494Sopenharmony_ci    masm.Reset();                                                        \
88b8021494Sopenharmony_ci    {                                                                    \
89b8021494Sopenharmony_ci      ExactAssemblyScope guard(&masm,                                    \
90b8021494Sopenharmony_ci                               MAX_SIZE_GENERATED,                       \
91b8021494Sopenharmony_ci                               ExactAssemblyScope::kMaximumSize);        \
92b8021494Sopenharmony_ci      masm.ASM;                                                          \
93b8021494Sopenharmony_ci    }                                                                    \
94b8021494Sopenharmony_ci    masm.FinalizeCode();                                                 \
95b8021494Sopenharmony_ci    decoder.Decode(masm.GetBuffer()->GetStartAddress<Instruction*>());   \
96b8021494Sopenharmony_ci    uint32_t encoding = *masm.GetBuffer()->GetStartAddress<uint32_t*>(); \
97b8021494Sopenharmony_ci    if (strncmp(disasm.GetOutput(), EXP, strlen(EXP)) != 0) {            \
98b8021494Sopenharmony_ci      printf("\nEncoding: %08" PRIx32 "\nExpected: %s\nFound:    %s\n",  \
99b8021494Sopenharmony_ci             encoding,                                                   \
100b8021494Sopenharmony_ci             EXP,                                                        \
101b8021494Sopenharmony_ci             disasm.GetOutput());                                        \
102b8021494Sopenharmony_ci      abort();                                                           \
103b8021494Sopenharmony_ci    }                                                                    \
104b8021494Sopenharmony_ci    if (Test::disassemble()) DISASSEMBLE();                              \
105b8021494Sopenharmony_ci  } while (0)
106b8021494Sopenharmony_ci
107b8021494Sopenharmony_ci#define COMPARE_MACRO_BASE(ASM, EXP)                        \
108b8021494Sopenharmony_ci  masm.Reset();                                             \
109b8021494Sopenharmony_ci  masm.ASM;                                                 \
110b8021494Sopenharmony_ci  masm.FinalizeCode();                                      \
111b8021494Sopenharmony_ci  std::string res;                                          \
112b8021494Sopenharmony_ci                                                            \
113b8021494Sopenharmony_ci  Instruction* instruction =                                \
114b8021494Sopenharmony_ci      masm.GetBuffer()->GetStartAddress<Instruction*>();    \
115b8021494Sopenharmony_ci  Instruction* end = masm.GetCursorAddress<Instruction*>(); \
116b8021494Sopenharmony_ci  while (instruction != end) {                              \
117b8021494Sopenharmony_ci    decoder.Decode(instruction);                            \
118b8021494Sopenharmony_ci    res.append(disasm.GetOutput());                         \
119b8021494Sopenharmony_ci    instruction = instruction->GetNextInstruction();        \
120b8021494Sopenharmony_ci    if (instruction != end) {                               \
121b8021494Sopenharmony_ci      res.append("\n");                                     \
122b8021494Sopenharmony_ci    }                                                       \
123b8021494Sopenharmony_ci  }
124b8021494Sopenharmony_ci
125b8021494Sopenharmony_ci#define COMPARE_MACRO(ASM, EXP)                                 \
126b8021494Sopenharmony_ci  do {                                                          \
127b8021494Sopenharmony_ci    COMPARE_MACRO_BASE(ASM, EXP)                                \
128b8021494Sopenharmony_ci    if (strcmp(res.c_str(), EXP) != 0) {                        \
129b8021494Sopenharmony_ci      printf("Expected: %s\nFound:    %s\n", EXP, res.c_str()); \
130b8021494Sopenharmony_ci      abort();                                                  \
131b8021494Sopenharmony_ci    }                                                           \
132b8021494Sopenharmony_ci    if (Test::disassemble()) DISASSEMBLE();                     \
133b8021494Sopenharmony_ci  } while (0)
134b8021494Sopenharmony_ci
135b8021494Sopenharmony_ci#define COMPARE_MACRO_PREFIX(ASM, EXP)                                   \
136b8021494Sopenharmony_ci  do {                                                                   \
137b8021494Sopenharmony_ci    COMPARE_MACRO_BASE(ASM, EXP)                                         \
138b8021494Sopenharmony_ci    if (strncmp(res.c_str(), EXP, strlen(EXP)) != 0) {                   \
139b8021494Sopenharmony_ci      printf("Expected (prefix): %s\nFound:    %s\n", EXP, res.c_str()); \
140b8021494Sopenharmony_ci      abort();                                                           \
141b8021494Sopenharmony_ci    }                                                                    \
142b8021494Sopenharmony_ci    if (Test::disassemble()) DISASSEMBLE();                              \
143b8021494Sopenharmony_ci  } while (0)
144b8021494Sopenharmony_ci
145b8021494Sopenharmony_ci#define CLEANUP()
146