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