1 // Copyright 2016, 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 
28 // -----------------------------------------------------------------------------
29 // This file is auto generated from the
30 // test/aarch32/config/template-simulator-aarch32.cc.in template file using
31 // tools/generate_tests.py.
32 //
33 // PLEASE DO NOT EDIT.
34 // -----------------------------------------------------------------------------
35 
36 
37 #include "test-runner.h"
38 
39 #include "test-utils.h"
40 #include "test-utils-aarch32.h"
41 
42 #include "aarch32/assembler-aarch32.h"
43 #include "aarch32/macro-assembler-aarch32.h"
44 #include "aarch32/disasm-aarch32.h"
45 
46 #define __ masm.
47 #define BUF_SIZE (4096)
48 
49 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH32
50 // Run tests with the simulator.
51 
52 #define SETUP() MacroAssembler masm(BUF_SIZE)
53 
54 #define START() masm.GetBuffer()->Reset()
55 
56 #define END() \
57   __ Hlt(0);  \
58   __ FinalizeCode();
59 
60 // TODO: Run the tests in the simulator.
61 #define RUN()
62 
63 #define TEARDOWN()
64 
65 #else  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH32.
66 
67 #define SETUP()                                   \
68   MacroAssembler masm(BUF_SIZE);                  \
69   UseScratchRegisterScope harness_scratch(&masm); \
70   harness_scratch.ExcludeAll();
71 
72 #define START()              \
73   masm.GetBuffer()->Reset(); \
74   __ Push(r4);               \
75   __ Push(r5);               \
76   __ Push(r6);               \
77   __ Push(r7);               \
78   __ Push(r8);               \
79   __ Push(r9);               \
80   __ Push(r10);              \
81   __ Push(r11);              \
82   __ Push(lr);               \
83   harness_scratch.Include(ip);
84 
85 #define END()                  \
86   harness_scratch.Exclude(ip); \
87   __ Pop(lr);                  \
88   __ Pop(r11);                 \
89   __ Pop(r10);                 \
90   __ Pop(r9);                  \
91   __ Pop(r8);                  \
92   __ Pop(r7);                  \
93   __ Pop(r6);                  \
94   __ Pop(r5);                  \
95   __ Pop(r4);                  \
96   __ Bx(lr);                   \
97   __ FinalizeCode();
98 
99 #define RUN()                                                 \
100   {                                                           \
101     int pcs_offset = masm.IsUsingT32() ? 1 : 0;               \
102     masm.GetBuffer()->SetExecutable();                        \
103     ExecuteMemory(masm.GetBuffer()->GetStartAddress<byte*>(), \
104                   masm.GetSizeOfCodeGenerated(),              \
105                   pcs_offset);                                \
106     masm.GetBuffer()->SetWritable();                          \
107   }
108 
109 #define TEARDOWN() harness_scratch.Close();
110 
111 #endif  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH32
112 
113 namespace vixl {
114 namespace aarch32 {
115 
116 // List of instruction encodings:
117 #define FOREACH_INSTRUCTION(M) \
118   M(Sadd16)                    \
119   M(Sadd8)                     \
120   M(Sasx)                      \
121   M(Ssax)                      \
122   M(Ssub16)                    \
123   M(Ssub8)                     \
124   M(Uadd16)                    \
125   M(Uadd8)                     \
126   M(Uasx)                      \
127   M(Usax)                      \
128   M(Usub16)                    \
129   M(Usub8)
130 
131 
132 // The following definitions are defined again in each generated test, therefore
133 // we need to place them in an anomymous namespace. It expresses that they are
134 // local to this file only, and the compiler is not allowed to share these types
135 // across test files during template instantiation. Specifically, `Operands` and
136 // `Inputs` have various layouts across generated tests so they absolutely
137 // cannot be shared.
138 
139 #ifdef VIXL_INCLUDE_TARGET_T32
140 namespace {
141 
142 // Values to be passed to the assembler to produce the instruction under test.
143 struct Operands {
144   Condition cond;
145   Register rd;
146   Register rn;
147   Register rm;
148 };
149 
150 // Input data to feed to the instruction.
151 struct Inputs {
152   uint32_t apsr;
153   uint32_t qbit;
154   uint32_t ge;
155   uint32_t rd;
156   uint32_t rn;
157   uint32_t rm;
158 };
159 
160 // This structure contains all input data needed to test one specific encoding.
161 // It used to generate a loop over an instruction.
162 struct TestLoopData {
163   // The `operands` fields represents the values to pass to the assembler to
164   // produce the instruction.
165   Operands operands;
166   // Description of the operands, used for error reporting.
167   const char* operands_description;
168   // Unique identifier, used for generating traces.
169   const char* identifier;
170   // Array of values to be fed to the instruction.
171   size_t input_size;
172   const Inputs* inputs;
173 };
174 
175 static const Inputs kGE[] =
176     {{NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffe, 0x00007fff},
177      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000001, 0xffffff83},
178      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff80, 0x00007ffd},
179      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8002, 0xffffffe0},
180      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0xffff8003},
181      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0x0000007f},
182      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8001, 0x33333333},
183      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0x00007fff},
184      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x33333333, 0x0000007d},
185      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000001, 0xffffff80},
186      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007e, 0x00000001},
187      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007e, 0xffff8003},
188      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0xffff8003},
189      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000000, 0x00007ffd},
190      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0xcccccccc},
191      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0xffffffff},
192      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xcccccccc, 0xcccccccc},
193      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0xffffff82},
194      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffe0, 0xffff8002},
195      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff81, 0x00000002},
196      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8000, 0x7ffffffe},
197      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000000, 0xfffffffe},
198      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff82, 0xffffff82},
199      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0x00007ffd},
200      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7fffffff, 0x00000020},
201      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffe, 0x0000007e},
202      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000001, 0x00000001},
203      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000000, 0x0000007f},
204      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffe, 0xffff8002},
205      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x55555555},
206      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8003, 0x00000002},
207      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000001, 0xffffffe0},
208      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff80, 0x00000002},
209      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffd, 0x0000007e},
210      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000001, 0xffff8001},
211      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff81, 0xffffff82},
212      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff80, 0xfffffffd},
213      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xcccccccc, 0xffff8000},
214      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff82, 0x7ffffffd},
215      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xfffffffd, 0x7fffffff},
216      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x33333333, 0x80000001},
217      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffe, 0xffffff82},
218      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffe0, 0xffffff82},
219      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x00000001},
220      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0xffff8003},
221      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000000, 0xffffffff},
222      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffe, 0x00000002},
223      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8002, 0x00000020},
224      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7fffffff, 0xffff8000},
225      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8001, 0xaaaaaaaa},
226      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffe, 0x33333333},
227      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0xcccccccc},
228      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x33333333, 0xaaaaaaaa},
229      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8000, 0xffff8002},
230      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0x00007ffd},
231      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007d, 0xffff8002},
232      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffe0, 0x00007ffd},
233      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000001, 0x7ffffffd},
234      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0xffff8002},
235      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0x00000002},
236      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007fff, 0x0000007f},
237      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffe0, 0x7ffffffe},
238      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xfffffffd, 0xfffffffd},
239      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0x0000007e},
240      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0x80000001},
241      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8003, 0x0000007f},
242      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xaaaaaaaa, 0xffffff82},
243      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff83, 0xaaaaaaaa},
244      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff82, 0xffffff81},
245      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x55555555, 0x00000020},
246      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x80000000},
247      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000001, 0x80000000},
248      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0x0000007f},
249      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8000, 0xffffff82},
250      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffd, 0x7ffffffe},
251      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffff, 0x7fffffff},
252      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffd, 0x80000000},
253      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0xffffff80},
254      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffd, 0xaaaaaaaa},
255      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffe0, 0xffff8001},
256      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000000, 0xffff8000},
257      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0xffffff83},
258      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000000, 0x00000001},
259      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffe, 0x7fffffff},
260      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000002, 0x00007fff},
261      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007fff, 0x00007fff},
262      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffe, 0x00000001},
263      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x7ffffffe},
264      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000001, 0x80000001},
265      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0x55555555},
266      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff83, 0xfffffffd},
267      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000001, 0x00007ffe},
268      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x33333333, 0x0000007f},
269      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xcccccccc, 0x55555555},
270      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffe, 0x00007fff},
271      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000002, 0xffffff82},
272      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffff, 0x0000007f},
273      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff80, 0xffffff81},
274      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff82, 0xffffff80},
275      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0xffffff82},
276      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0x00007ffd},
277      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000001, 0xffff8003},
278      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0x0000007f},
279      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffe0, 0x00000020},
280      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000001, 0x00000002},
281      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffe, 0xfffffffd},
282      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7fffffff, 0x7ffffffd},
283      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7fffffff, 0x33333333},
284      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000000, 0x00007ffe},
285      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffe, 0xfffffffe},
286      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0x7fffffff},
287      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8002, 0x00000001},
288      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff80, 0xffffff80},
289      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff80, 0x0000007d},
290      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000000, 0xffffff81},
291      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffd, 0x0000007e},
292      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007e, 0x33333333},
293      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8002, 0xffffff83},
294      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff83, 0xffffffff},
295      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xcccccccc, 0xcccccccc},
296      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffd, 0x00000002},
297      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8002, 0x7ffffffd},
298      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007d, 0xcccccccc},
299      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000000, 0x00000000},
300      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00007ffd, 0x00000002},
301      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffff, 0x00000020},
302      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0xffff8002},
303      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0xffffff83},
304      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0x80000001},
305      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x55555555, 0xffffffff},
306      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffe0, 0x00000001},
307      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffe, 0x00000001},
308      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007d, 0x80000000},
309      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x80000001, 0x0000007d},
310      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0xffffff83},
311      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8003, 0xffffff81},
312      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7fffffff, 0xffff8003},
313      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0x0000007d},
314      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xfffffffe, 0xffff8001},
315      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff81, 0x7fffffff},
316      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0xffff8002},
317      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffd, 0xffffff80},
318      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffe0, 0x7ffffffd},
319      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8000, 0x7ffffffd},
320      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8003, 0xfffffffd},
321      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff82, 0xffff8002},
322      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007e, 0x33333333},
323      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x00000000},
324      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8001, 0x7ffffffd},
325      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xcccccccc, 0xffff8003},
326      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffd, 0xffff8000},
327      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007d, 0xaaaaaaaa},
328      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000020, 0xffff8003},
329      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffe, 0xffff8001},
330      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xfffffffe, 0x55555555},
331      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000001, 0xffffff83},
332      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000000, 0xfffffffe},
333      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8003, 0x0000007f},
334      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8002, 0x33333333},
335      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8003, 0x00007ffd},
336      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8000, 0x00007ffe},
337      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff81, 0xffffff81},
338      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffff8000, 0x00007ffd},
339      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007ffe, 0x33333333},
340      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0xaaaaaaaa},
341      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffffff, 0xfffffffe},
342      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000000, 0x7ffffffe},
343      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0x00000020},
344      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffff8001, 0x00007ffd},
345      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff80, 0xaaaaaaaa},
346      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffff81, 0xffffff80},
347      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000002, 0x00000000},
348      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007f, 0x33333333},
349      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00007fff, 0x0000007e},
350      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000000, 0x80000000},
351      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007d, 0x00007fff},
352      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000000, 0xffffff83},
353      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007f, 0x0000007d},
354      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x33333333, 0x0000007d},
355      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xfffffffd, 0x80000001},
356      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007e, 0xffffff81},
357      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0xffffff81},
358      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007e, 0xfffffffe},
359      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x80000000, 0x7ffffffd},
360      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x7ffffffe, 0xffffff81},
361      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007e, 0xffff8002},
362      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x00000000, 0xffffff81},
363      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0xffffffff, 0x33333333},
364      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007e, 0xffffff80},
365      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7ffffffd, 0xaaaaaaaa},
366      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7fffffff, 0x0000007d},
367      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x00000001, 0x00000002},
368      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff82, 0x33333333},
369      {NoFlag, NoFlag, GE0123Flag, 0xabababab, 0x0000007e, 0xffffffff},
370      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff83, 0x00007ffe},
371      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7ffffffd, 0xfffffffe},
372      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xffffff83, 0xffff8001},
373      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x7ffffffd, 0x33333333},
374      {NoFlag, NoFlag, NoFlag, 0xabababab, 0xcccccccc, 0xffff8003},
375      {NoFlag, NoFlag, NoFlag, 0xabababab, 0x0000007d, 0x0000007d}};
376 
377 
378 // A loop will be generated for each element of this array.
379 const TestLoopData kTests[] = {{{al, r12, r0, r10},
380                                 "al r12 r0 r10",
381                                 "GE_al_r12_r0_r10",
382                                 ARRAY_SIZE(kGE),
383                                 kGE}};
384 
385 // We record all inputs to the instructions as outputs. This way, we also check
386 // that what shouldn't change didn't change.
387 struct TestResult {
388   size_t output_size;
389   const Inputs* outputs;
390 };
391 
392 // These headers each contain an array of `TestResult` with the reference output
393 // values. The reference arrays are names `kReference{mnemonic}`.
394 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-sadd16.h"
395 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-sadd8.h"
396 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-sasx.h"
397 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-ssax.h"
398 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-ssub16.h"
399 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-ssub8.h"
400 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-uadd16.h"
401 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-uadd8.h"
402 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-uasx.h"
403 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-usax.h"
404 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-usub16.h"
405 #include "aarch32/traces/simulator-cond-rd-rn-rm-t32-ge-usub8.h"
406 
407 
408 // The maximum number of errors to report in detail for each test.
409 const unsigned kErrorReportLimit = 8;
410 
411 typedef void (MacroAssembler::*Fn)(Condition cond,
412                                    Register rd,
413                                    Register rn,
414                                    Register rm);
415 
TestHelper(Fn instruction,const char * mnemonic,const TestResult reference[])416 void TestHelper(Fn instruction,
417                 const char* mnemonic,
418                 const TestResult reference[]) {
419   SETUP();
420   masm.UseT32();
421   START();
422 
423   // Data to compare to `reference`.
424   TestResult* results[ARRAY_SIZE(kTests)];
425 
426   // Test cases for memory bound instructions may allocate a buffer and save its
427   // address in this array.
428   byte* scratch_memory_buffers[ARRAY_SIZE(kTests)];
429 
430   // Generate a loop for each element in `kTests`. Each loop tests one specific
431   // instruction.
432   for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) {
433     // Allocate results on the heap for this test.
434     results[i] = new TestResult;
435     results[i]->outputs = new Inputs[kTests[i].input_size];
436     results[i]->output_size = kTests[i].input_size;
437 
438     size_t input_stride = sizeof(kTests[i].inputs[0]) * kTests[i].input_size;
439     VIXL_ASSERT(IsUint32(input_stride));
440 
441     scratch_memory_buffers[i] = NULL;
442 
443     Label loop;
444     UseScratchRegisterScope scratch_registers(&masm);
445     // Include all registers from r0 ro r12.
446     scratch_registers.Include(RegisterList(0x1fff));
447 
448     // Values to pass to the macro-assembler.
449     Condition cond = kTests[i].operands.cond;
450     Register rd = kTests[i].operands.rd;
451     Register rn = kTests[i].operands.rn;
452     Register rm = kTests[i].operands.rm;
453     scratch_registers.Exclude(rd);
454     scratch_registers.Exclude(rn);
455     scratch_registers.Exclude(rm);
456 
457     // Allocate reserved registers for our own use.
458     Register input_ptr = scratch_registers.Acquire();
459     Register input_end = scratch_registers.Acquire();
460     Register result_ptr = scratch_registers.Acquire();
461 
462     // Initialize `input_ptr` to the first element and `input_end` the address
463     // after the array.
464     __ Mov(input_ptr, Operand::From(kTests[i].inputs));
465     __ Add(input_end, input_ptr, static_cast<uint32_t>(input_stride));
466     __ Mov(result_ptr, Operand::From(results[i]->outputs));
467     __ Bind(&loop);
468 
469     {
470       UseScratchRegisterScope temp_registers(&masm);
471       Register nzcv_bits = temp_registers.Acquire();
472       Register saved_q_bit = temp_registers.Acquire();
473       // Save the `Q` bit flag.
474       __ Mrs(saved_q_bit, APSR);
475       __ And(saved_q_bit, saved_q_bit, QFlag);
476       // Set the `NZCV` and `Q` flags together.
477       __ Ldr(nzcv_bits, MemOperand(input_ptr, offsetof(Inputs, apsr)));
478       __ Orr(nzcv_bits, nzcv_bits, saved_q_bit);
479       __ Msr(APSR_nzcvq, nzcv_bits);
480     }
481     {
482       UseScratchRegisterScope temp_registers(&masm);
483       Register q_bit = temp_registers.Acquire();
484       Register saved_nzcv_bits = temp_registers.Acquire();
485       // Save the `NZCV` flags.
486       __ Mrs(saved_nzcv_bits, APSR);
487       __ And(saved_nzcv_bits, saved_nzcv_bits, NZCVFlag);
488       // Set the `NZCV` and `Q` flags together.
489       __ Ldr(q_bit, MemOperand(input_ptr, offsetof(Inputs, qbit)));
490       __ Orr(q_bit, q_bit, saved_nzcv_bits);
491       __ Msr(APSR_nzcvq, q_bit);
492     }
493     {
494       UseScratchRegisterScope temp_registers(&masm);
495       Register ge_bits = temp_registers.Acquire();
496       __ Ldr(ge_bits, MemOperand(input_ptr, offsetof(Inputs, ge)));
497       __ Msr(APSR_g, ge_bits);
498     }
499     __ Ldr(rd, MemOperand(input_ptr, offsetof(Inputs, rd)));
500     __ Ldr(rn, MemOperand(input_ptr, offsetof(Inputs, rn)));
501     __ Ldr(rm, MemOperand(input_ptr, offsetof(Inputs, rm)));
502 
503     (masm.*instruction)(cond, rd, rn, rm);
504 
505     {
506       UseScratchRegisterScope temp_registers(&masm);
507       Register nzcv_bits = temp_registers.Acquire();
508       __ Mrs(nzcv_bits, APSR);
509       // Only record the NZCV bits.
510       __ And(nzcv_bits, nzcv_bits, NZCVFlag);
511       __ Str(nzcv_bits, MemOperand(result_ptr, offsetof(Inputs, apsr)));
512     }
513     {
514       UseScratchRegisterScope temp_registers(&masm);
515       Register q_bit = temp_registers.Acquire();
516       __ Mrs(q_bit, APSR);
517       // Only record the Q bit.
518       __ And(q_bit, q_bit, QFlag);
519       __ Str(q_bit, MemOperand(result_ptr, offsetof(Inputs, qbit)));
520     }
521     {
522       UseScratchRegisterScope temp_registers(&masm);
523       Register ge_bits = temp_registers.Acquire();
524       __ Mrs(ge_bits, APSR);
525       // Only record the GE bits.
526       __ And(ge_bits, ge_bits, GEFlags);
527       __ Str(ge_bits, MemOperand(result_ptr, offsetof(Inputs, ge)));
528     }
529     __ Str(rd, MemOperand(result_ptr, offsetof(Inputs, rd)));
530     __ Str(rn, MemOperand(result_ptr, offsetof(Inputs, rn)));
531     __ Str(rm, MemOperand(result_ptr, offsetof(Inputs, rm)));
532 
533     // Advance the result pointer.
534     __ Add(result_ptr, result_ptr, Operand::From(sizeof(kTests[i].inputs[0])));
535     // Loop back until `input_ptr` is lower than `input_base`.
536     __ Add(input_ptr, input_ptr, Operand::From(sizeof(kTests[i].inputs[0])));
537     __ Cmp(input_ptr, input_end);
538     __ B(ne, &loop);
539   }
540 
541   END();
542 
543   RUN();
544 
545   if (Test::generate_test_trace()) {
546     // Print the results.
547     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
548       printf("const Inputs kOutputs_%s_%s[] = {\n",
549              mnemonic,
550              kTests[i].identifier);
551       for (size_t j = 0; j < results[i]->output_size; j++) {
552         printf("  { ");
553         printf("0x%08" PRIx32, results[i]->outputs[j].apsr);
554         printf(", ");
555         printf("0x%08" PRIx32, results[i]->outputs[j].qbit);
556         printf(", ");
557         printf("0x%08" PRIx32, results[i]->outputs[j].ge);
558         printf(", ");
559         printf("0x%08" PRIx32, results[i]->outputs[j].rd);
560         printf(", ");
561         printf("0x%08" PRIx32, results[i]->outputs[j].rn);
562         printf(", ");
563         printf("0x%08" PRIx32, results[i]->outputs[j].rm);
564         printf(" },\n");
565       }
566       printf("};\n");
567     }
568     printf("const TestResult kReference%s[] = {\n", mnemonic);
569     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
570       printf("  {\n");
571       printf("    ARRAY_SIZE(kOutputs_%s_%s),\n",
572              mnemonic,
573              kTests[i].identifier);
574       printf("    kOutputs_%s_%s,\n", mnemonic, kTests[i].identifier);
575       printf("  },\n");
576     }
577     printf("};\n");
578   } else if (kCheckSimulatorTestResults) {
579     // Check the results.
580     unsigned total_error_count = 0;
581     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
582       bool instruction_has_errors = false;
583       for (size_t j = 0; j < kTests[i].input_size; j++) {
584         uint32_t apsr = results[i]->outputs[j].apsr;
585         uint32_t qbit = results[i]->outputs[j].qbit;
586         uint32_t ge = results[i]->outputs[j].ge;
587         uint32_t rd = results[i]->outputs[j].rd;
588         uint32_t rn = results[i]->outputs[j].rn;
589         uint32_t rm = results[i]->outputs[j].rm;
590         uint32_t apsr_input = kTests[i].inputs[j].apsr;
591         uint32_t qbit_input = kTests[i].inputs[j].qbit;
592         uint32_t ge_input = kTests[i].inputs[j].ge;
593         uint32_t rd_input = kTests[i].inputs[j].rd;
594         uint32_t rn_input = kTests[i].inputs[j].rn;
595         uint32_t rm_input = kTests[i].inputs[j].rm;
596         uint32_t apsr_ref = reference[i].outputs[j].apsr;
597         uint32_t qbit_ref = reference[i].outputs[j].qbit;
598         uint32_t ge_ref = reference[i].outputs[j].ge;
599         uint32_t rd_ref = reference[i].outputs[j].rd;
600         uint32_t rn_ref = reference[i].outputs[j].rn;
601         uint32_t rm_ref = reference[i].outputs[j].rm;
602 
603         if (((apsr != apsr_ref) || (qbit != qbit_ref) || (ge != ge_ref) ||
604              (rd != rd_ref) || (rn != rn_ref) || (rm != rm_ref)) &&
605             (++total_error_count <= kErrorReportLimit)) {
606           // Print the instruction once even if it triggered multiple failures.
607           if (!instruction_has_errors) {
608             printf("Error(s) when testing \"%s %s\":\n",
609                    mnemonic,
610                    kTests[i].operands_description);
611             instruction_has_errors = true;
612           }
613           // Print subsequent errors.
614           printf("  Input:    ");
615           printf("0x%08" PRIx32, apsr_input);
616           printf(", ");
617           printf("0x%08" PRIx32, qbit_input);
618           printf(", ");
619           printf("0x%08" PRIx32, ge_input);
620           printf(", ");
621           printf("0x%08" PRIx32, rd_input);
622           printf(", ");
623           printf("0x%08" PRIx32, rn_input);
624           printf(", ");
625           printf("0x%08" PRIx32, rm_input);
626           printf("\n");
627           printf("  Expected: ");
628           printf("0x%08" PRIx32, apsr_ref);
629           printf(", ");
630           printf("0x%08" PRIx32, qbit_ref);
631           printf(", ");
632           printf("0x%08" PRIx32, ge_ref);
633           printf(", ");
634           printf("0x%08" PRIx32, rd_ref);
635           printf(", ");
636           printf("0x%08" PRIx32, rn_ref);
637           printf(", ");
638           printf("0x%08" PRIx32, rm_ref);
639           printf("\n");
640           printf("  Found:    ");
641           printf("0x%08" PRIx32, apsr);
642           printf(", ");
643           printf("0x%08" PRIx32, qbit);
644           printf(", ");
645           printf("0x%08" PRIx32, ge);
646           printf(", ");
647           printf("0x%08" PRIx32, rd);
648           printf(", ");
649           printf("0x%08" PRIx32, rn);
650           printf(", ");
651           printf("0x%08" PRIx32, rm);
652           printf("\n\n");
653         }
654       }
655     }
656 
657     if (total_error_count > kErrorReportLimit) {
658       printf("%u other errors follow.\n",
659              total_error_count - kErrorReportLimit);
660     }
661     VIXL_CHECK(total_error_count == 0);
662   } else {
663     VIXL_WARNING("Assembled the code, but did not run anything.\n");
664   }
665 
666   for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
667     delete[] results[i]->outputs;
668     delete results[i];
669     delete[] scratch_memory_buffers[i];
670   }
671 
672   TEARDOWN();
673 }
674 
675 // Instantiate tests for each instruction in the list.
676 // TODO: Remove this limitation by having a sandboxing mechanism.
677 #if defined(VIXL_HOST_POINTER_32)
678 #define TEST(mnemonic)                                                      \
679   void Test_##mnemonic() {                                                  \
680     TestHelper(&MacroAssembler::mnemonic, #mnemonic, kReference##mnemonic); \
681   }                                                                         \
682   Test test_##mnemonic("AARCH32_SIMULATOR_COND_RD_RN_RM_T32_GE_" #mnemonic, \
683                        &Test_##mnemonic);
684 #else
685 #define TEST(mnemonic)                                                      \
686   void Test_##mnemonic() {                                                  \
687     VIXL_WARNING("This test can only run on a 32-bit host.\n");             \
688     USE(TestHelper);                                                        \
689   }                                                                         \
690   Test test_##mnemonic("AARCH32_SIMULATOR_COND_RD_RN_RM_T32_GE_" #mnemonic, \
691                        &Test_##mnemonic);
692 #endif
693 
694 FOREACH_INSTRUCTION(TEST)
695 #undef TEST
696 
697 }  // namespace
698 #endif
699 
700 }  // namespace aarch32
701 }  // namespace vixl
702