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