• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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* start = masm.GetBuffer()->GetStartAddress<Instruction*>(); \
58     Instruction* end = masm.GetBuffer()->GetEndAddress<Instruction*>();     \
59     print_disasm.DisassembleBuffer(start, end);                             \
60   } while (0)
61 
62 #define COMPARE(ASM, EXP)                                                \
63   do {                                                                   \
64     masm.Reset();                                                        \
65     {                                                                    \
66       ExactAssemblyScope guard(&masm,                                    \
67                                MAX_SIZE_GENERATED,                       \
68                                ExactAssemblyScope::kMaximumSize);        \
69       masm.ASM;                                                          \
70     }                                                                    \
71     masm.FinalizeCode();                                                 \
72     decoder.Decode(masm.GetBuffer()->GetStartAddress<Instruction*>());   \
73     uint32_t encoding = *masm.GetBuffer()->GetStartAddress<uint32_t*>(); \
74     if (strcmp(disasm.GetOutput(), EXP) != 0) {                          \
75       printf("\nEncoding: %08" PRIx32 "\nExpected: %s\nFound:    %s\n",  \
76              encoding,                                                   \
77              EXP,                                                        \
78              disasm.GetOutput());                                        \
79       abort();                                                           \
80     }                                                                    \
81     if (Test::disassemble()) DISASSEMBLE();                              \
82   } while (0)
83 
84 #define COMPARE_PREFIX(ASM, EXP)                                         \
85   do {                                                                   \
86     masm.Reset();                                                        \
87     {                                                                    \
88       ExactAssemblyScope guard(&masm,                                    \
89                                MAX_SIZE_GENERATED,                       \
90                                ExactAssemblyScope::kMaximumSize);        \
91       masm.ASM;                                                          \
92     }                                                                    \
93     masm.FinalizeCode();                                                 \
94     decoder.Decode(masm.GetBuffer()->GetStartAddress<Instruction*>());   \
95     uint32_t encoding = *masm.GetBuffer()->GetStartAddress<uint32_t*>(); \
96     if (strncmp(disasm.GetOutput(), EXP, strlen(EXP)) != 0) {            \
97       printf("\nEncoding: %08" PRIx32 "\nExpected: %s\nFound:    %s\n",  \
98              encoding,                                                   \
99              EXP,                                                        \
100              disasm.GetOutput());                                        \
101       abort();                                                           \
102     }                                                                    \
103     if (Test::disassemble()) DISASSEMBLE();                              \
104   } while (0)
105 
106 #define COMPARE_MACRO_BASE(ASM, EXP)                        \
107   masm.Reset();                                             \
108   masm.ASM;                                                 \
109   masm.FinalizeCode();                                      \
110   std::string res;                                          \
111                                                             \
112   Instruction* instruction =                                \
113       masm.GetBuffer()->GetStartAddress<Instruction*>();    \
114   Instruction* end = masm.GetCursorAddress<Instruction*>(); \
115   while (instruction != end) {                              \
116     decoder.Decode(instruction);                            \
117     res.append(disasm.GetOutput());                         \
118     instruction = instruction->GetNextInstruction();        \
119     if (instruction != end) {                               \
120       res.append("\n");                                     \
121     }                                                       \
122   }
123 
124 #define COMPARE_MACRO(ASM, EXP)                                 \
125   do {                                                          \
126     COMPARE_MACRO_BASE(ASM, EXP)                                \
127     if (strcmp(res.c_str(), EXP) != 0) {                        \
128       printf("Expected: %s\nFound:    %s\n", EXP, res.c_str()); \
129       abort();                                                  \
130     }                                                           \
131     if (Test::disassemble()) DISASSEMBLE();                     \
132   } while (0)
133 
134 #define COMPARE_MACRO_PREFIX(ASM, EXP)                                   \
135   do {                                                                   \
136     COMPARE_MACRO_BASE(ASM, EXP)                                         \
137     if (strncmp(res.c_str(), EXP, strlen(EXP)) != 0) {                   \
138       printf("Expected (prefix): %s\nFound:    %s\n", EXP, res.c_str()); \
139       abort();                                                           \
140     }                                                                    \
141     if (Test::disassemble()) DISASSEMBLE();                              \
142   } while (0)
143 
144 #define CLEANUP()
145