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