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(Crc32b)                    \
119   M(Crc32cb)                   \
120   M(Crc32ch)                   \
121   M(Crc32cw)                   \
122   M(Crc32h)                    \
123   M(Crc32w)
124 
125 
126 // The following definitions are defined again in each generated test, therefore
127 // we need to place them in an anomymous namespace. It expresses that they are
128 // local to this file only, and the compiler is not allowed to share these types
129 // across test files during template instantiation. Specifically, `Operands` and
130 // `Inputs` have various layouts across generated tests so they absolutely
131 // cannot be shared.
132 
133 #ifdef VIXL_INCLUDE_TARGET_T32
134 namespace {
135 
136 // Values to be passed to the assembler to produce the instruction under test.
137 struct Operands {
138   Register rd;
139   Register rn;
140   Register rm;
141 };
142 
143 // Input data to feed to the instruction.
144 struct Inputs {
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.
152 struct 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 
165 static const Inputs kRnIsRm[] = {{0xffffffe0, 0x00007fff, 0x00007fff},
166                                  {0xaaaaaaaa, 0x00007fff, 0x00007fff},
167                                  {0xffffff82, 0x7ffffffe, 0x7ffffffe},
168                                  {0x7ffffffd, 0x33333333, 0x33333333},
169                                  {0x00007ffd, 0x00000002, 0x00000002},
170                                  {0xffffff80, 0xfffffffd, 0xfffffffd},
171                                  {0xffffff82, 0x33333333, 0x33333333},
172                                  {0xcccccccc, 0xffff8002, 0xffff8002},
173                                  {0x55555555, 0xffffffe0, 0xffffffe0},
174                                  {0x00007fff, 0xffffffff, 0xffffffff},
175                                  {0x55555555, 0x33333333, 0x33333333},
176                                  {0x7ffffffd, 0xffff8002, 0xffff8002},
177                                  {0x00000020, 0xffffff83, 0xffffff83},
178                                  {0x0000007e, 0x00007fff, 0x00007fff},
179                                  {0xfffffffe, 0xffffff82, 0xffffff82},
180                                  {0xffffff82, 0x00007fff, 0x00007fff},
181                                  {0xffffff82, 0xfffffffd, 0xfffffffd},
182                                  {0x80000000, 0x7fffffff, 0x7fffffff},
183                                  {0x55555555, 0x00000002, 0x00000002},
184                                  {0xfffffffe, 0xffffff80, 0xffffff80},
185                                  {0xfffffffe, 0x00000002, 0x00000002},
186                                  {0xfffffffe, 0x00000020, 0x00000020},
187                                  {0xfffffffe, 0x00007ffd, 0x00007ffd},
188                                  {0x0000007f, 0x33333333, 0x33333333},
189                                  {0xfffffffd, 0xfffffffe, 0xfffffffe},
190                                  {0x80000001, 0x7ffffffd, 0x7ffffffd},
191                                  {0x7ffffffe, 0xffffff83, 0xffffff83},
192                                  {0xffffffe0, 0x00000002, 0x00000002},
193                                  {0x00007ffe, 0xffffff83, 0xffffff83},
194                                  {0xffffff82, 0xffffff80, 0xffffff80},
195                                  {0x0000007e, 0x33333333, 0x33333333},
196                                  {0xffff8001, 0xfffffffe, 0xfffffffe},
197                                  {0xfffffffe, 0xffffff81, 0xffffff81},
198                                  {0x00000001, 0xffffff83, 0xffffff83},
199                                  {0xfffffffe, 0xaaaaaaaa, 0xaaaaaaaa},
200                                  {0xcccccccc, 0xffffff80, 0xffffff80},
201                                  {0xffffff83, 0x0000007f, 0x0000007f},
202                                  {0x00007ffd, 0x00000020, 0x00000020},
203                                  {0x0000007d, 0xfffffffd, 0xfffffffd},
204                                  {0x7ffffffe, 0xaaaaaaaa, 0xaaaaaaaa},
205                                  {0xffffff83, 0x00000001, 0x00000001},
206                                  {0x0000007d, 0xffff8000, 0xffff8000},
207                                  {0xffffffe0, 0x00000020, 0x00000020},
208                                  {0xffff8003, 0xffffff80, 0xffffff80},
209                                  {0x00000000, 0xaaaaaaaa, 0xaaaaaaaa},
210                                  {0x00007fff, 0x00000020, 0x00000020},
211                                  {0x80000000, 0x0000007d, 0x0000007d},
212                                  {0xffffff81, 0x0000007e, 0x0000007e},
213                                  {0xcccccccc, 0x00000020, 0x00000020},
214                                  {0xffffff81, 0x33333333, 0x33333333},
215                                  {0xffff8002, 0xffffffe0, 0xffffffe0},
216                                  {0xffffffe0, 0x00007ffe, 0x00007ffe},
217                                  {0xffff8002, 0xaaaaaaaa, 0xaaaaaaaa},
218                                  {0xffff8001, 0x00007ffe, 0x00007ffe},
219                                  {0x80000000, 0xcccccccc, 0xcccccccc},
220                                  {0x7ffffffd, 0x00000002, 0x00000002},
221                                  {0xcccccccc, 0x0000007f, 0x0000007f},
222                                  {0x7ffffffe, 0xffffff82, 0xffffff82},
223                                  {0x00000001, 0x0000007e, 0x0000007e},
224                                  {0x00007ffd, 0x00007ffe, 0x00007ffe},
225                                  {0x7ffffffd, 0xfffffffe, 0xfffffffe},
226                                  {0x00000001, 0x80000001, 0x80000001},
227                                  {0x00007ffe, 0x7fffffff, 0x7fffffff},
228                                  {0xffff8001, 0xffff8002, 0xffff8002},
229                                  {0x00000001, 0xffff8003, 0xffff8003},
230                                  {0x00000002, 0xfffffffe, 0xfffffffe},
231                                  {0x00000002, 0x00007ffd, 0x00007ffd},
232                                  {0x7ffffffe, 0xfffffffd, 0xfffffffd},
233                                  {0xffff8001, 0x7ffffffd, 0x7ffffffd},
234                                  {0x7ffffffd, 0x55555555, 0x55555555},
235                                  {0x80000000, 0xffffff80, 0xffffff80},
236                                  {0xffff8002, 0xffff8003, 0xffff8003},
237                                  {0xffffffff, 0x00000020, 0x00000020},
238                                  {0xfffffffe, 0x00000001, 0x00000001},
239                                  {0x00007fff, 0x00007fff, 0x00007fff},
240                                  {0x00000000, 0xffffff83, 0xffffff83},
241                                  {0x33333333, 0xaaaaaaaa, 0xaaaaaaaa},
242                                  {0x0000007d, 0x7ffffffe, 0x7ffffffe},
243                                  {0xffff8003, 0xffff8001, 0xffff8001},
244                                  {0xfffffffd, 0xcccccccc, 0xcccccccc},
245                                  {0x80000000, 0x80000001, 0x80000001},
246                                  {0xffff8002, 0x00000002, 0x00000002},
247                                  {0xfffffffd, 0xffffffe0, 0xffffffe0},
248                                  {0xffffffe0, 0xffffff82, 0xffffff82},
249                                  {0x33333333, 0x00007ffe, 0x00007ffe},
250                                  {0xffff8002, 0xffffff80, 0xffffff80},
251                                  {0xffffff81, 0x80000000, 0x80000000},
252                                  {0x00000000, 0x80000000, 0x80000000},
253                                  {0x00000000, 0xcccccccc, 0xcccccccc},
254                                  {0x00000020, 0xffffffe0, 0xffffffe0},
255                                  {0x00007ffe, 0xffffffe0, 0xffffffe0},
256                                  {0xffffff81, 0xfffffffe, 0xfffffffe},
257                                  {0x00000002, 0x00007ffe, 0x00007ffe},
258                                  {0xffff8000, 0x7fffffff, 0x7fffffff},
259                                  {0x00000002, 0x55555555, 0x55555555},
260                                  {0xffff8000, 0x55555555, 0x55555555},
261                                  {0xffff8001, 0x00000020, 0x00000020},
262                                  {0xffffffff, 0x55555555, 0x55555555},
263                                  {0x33333333, 0x00000020, 0x00000020},
264                                  {0xfffffffd, 0x7fffffff, 0x7fffffff},
265                                  {0x7ffffffd, 0x7fffffff, 0x7fffffff},
266                                  {0xaaaaaaaa, 0xffffff81, 0xffffff81},
267                                  {0x00007ffd, 0x80000001, 0x80000001},
268                                  {0x7ffffffe, 0xffffffff, 0xffffffff},
269                                  {0x00007ffe, 0x7ffffffe, 0x7ffffffe},
270                                  {0xffff8001, 0x55555555, 0x55555555},
271                                  {0xffffff80, 0xfffffffe, 0xfffffffe},
272                                  {0x0000007e, 0x7ffffffd, 0x7ffffffd},
273                                  {0x00000000, 0x00007ffd, 0x00007ffd},
274                                  {0x00000020, 0x80000001, 0x80000001},
275                                  {0xffffff83, 0xffffffe0, 0xffffffe0},
276                                  {0x55555555, 0xffff8000, 0xffff8000},
277                                  {0x33333333, 0xffff8002, 0xffff8002},
278                                  {0x7ffffffd, 0x0000007f, 0x0000007f},
279                                  {0x00000002, 0xffffff82, 0xffffff82},
280                                  {0xfffffffe, 0xcccccccc, 0xcccccccc},
281                                  {0x33333333, 0xffffff82, 0xffffff82},
282                                  {0x7ffffffd, 0x00007ffd, 0x00007ffd},
283                                  {0x00000002, 0xffffff80, 0xffffff80},
284                                  {0xffff8001, 0x00000000, 0x00000000},
285                                  {0x7ffffffe, 0x0000007d, 0x0000007d},
286                                  {0x00000001, 0x00000020, 0x00000020},
287                                  {0xaaaaaaaa, 0xffffff82, 0xffffff82},
288                                  {0x00000001, 0xfffffffd, 0xfffffffd},
289                                  {0x80000001, 0xfffffffe, 0xfffffffe},
290                                  {0xffffff80, 0x80000000, 0x80000000},
291                                  {0x80000000, 0xffffffff, 0xffffffff},
292                                  {0x00000001, 0x00000001, 0x00000001},
293                                  {0x80000001, 0xffff8003, 0xffff8003},
294                                  {0xffffffff, 0xffffff83, 0xffffff83},
295                                  {0x00007ffe, 0x00007ffe, 0x00007ffe},
296                                  {0x7ffffffd, 0xcccccccc, 0xcccccccc},
297                                  {0xffff8003, 0xcccccccc, 0xcccccccc},
298                                  {0xcccccccc, 0x00007fff, 0x00007fff},
299                                  {0x00000002, 0x7ffffffd, 0x7ffffffd},
300                                  {0x00000002, 0xffff8002, 0xffff8002},
301                                  {0xffffff80, 0x33333333, 0x33333333},
302                                  {0x55555555, 0xffffff82, 0xffffff82},
303                                  {0x7fffffff, 0x00007fff, 0x00007fff},
304                                  {0x33333333, 0x00000001, 0x00000001},
305                                  {0x33333333, 0xffffffe0, 0xffffffe0},
306                                  {0xffffff83, 0x0000007e, 0x0000007e},
307                                  {0x00000002, 0xffff8001, 0xffff8001},
308                                  {0xcccccccc, 0xffffffe0, 0xffffffe0},
309                                  {0xffff8000, 0x7ffffffe, 0x7ffffffe},
310                                  {0x00007ffe, 0xffffff81, 0xffffff81},
311                                  {0x7ffffffd, 0xffffffe0, 0xffffffe0},
312                                  {0x00007fff, 0xffffff81, 0xffffff81},
313                                  {0xffffffff, 0x00007ffd, 0x00007ffd},
314                                  {0x00007fff, 0x80000001, 0x80000001},
315                                  {0xffffffff, 0xcccccccc, 0xcccccccc},
316                                  {0x00007ffd, 0x0000007d, 0x0000007d},
317                                  {0x0000007e, 0x00000000, 0x00000000},
318                                  {0x0000007f, 0x7ffffffe, 0x7ffffffe},
319                                  {0x00000020, 0xffffff82, 0xffffff82},
320                                  {0xcccccccc, 0x80000001, 0x80000001},
321                                  {0xffff8002, 0x00007ffd, 0x00007ffd},
322                                  {0xffff8000, 0x00000001, 0x00000001},
323                                  {0xffffff83, 0xffffff83, 0xffffff83},
324                                  {0x00000002, 0xffffffe0, 0xffffffe0},
325                                  {0xfffffffe, 0xffff8000, 0xffff8000},
326                                  {0x7ffffffe, 0xffffff80, 0xffffff80},
327                                  {0x0000007f, 0xffffff81, 0xffffff81},
328                                  {0xffffffff, 0x0000007f, 0x0000007f},
329                                  {0x7ffffffe, 0x0000007e, 0x0000007e},
330                                  {0x80000000, 0xfffffffd, 0xfffffffd},
331                                  {0x33333333, 0x0000007d, 0x0000007d},
332                                  {0x80000001, 0xcccccccc, 0xcccccccc},
333                                  {0xaaaaaaaa, 0x7ffffffd, 0x7ffffffd},
334                                  {0x7fffffff, 0x0000007f, 0x0000007f},
335                                  {0x0000007e, 0xffff8001, 0xffff8001},
336                                  {0x0000007d, 0xaaaaaaaa, 0xaaaaaaaa},
337                                  {0xffffff82, 0x55555555, 0x55555555},
338                                  {0x55555555, 0xffffff81, 0xffffff81},
339                                  {0xcccccccc, 0xffffff82, 0xffffff82},
340                                  {0x0000007d, 0x0000007d, 0x0000007d},
341                                  {0xfffffffd, 0xfffffffd, 0xfffffffd},
342                                  {0xffffff82, 0xffff8002, 0xffff8002},
343                                  {0xffffffe0, 0x00000001, 0x00000001},
344                                  {0x0000007d, 0x00007ffe, 0x00007ffe},
345                                  {0xfffffffe, 0xffff8003, 0xffff8003},
346                                  {0x00000001, 0xffff8001, 0xffff8001},
347                                  {0xffff8003, 0x00007ffe, 0x00007ffe},
348                                  {0x7ffffffd, 0x00000020, 0x00000020},
349                                  {0xfffffffd, 0x00007ffe, 0x00007ffe},
350                                  {0x7fffffff, 0xfffffffd, 0xfffffffd},
351                                  {0x7fffffff, 0xffff8001, 0xffff8001},
352                                  {0xffff8003, 0xffffffff, 0xffffffff},
353                                  {0x0000007d, 0x33333333, 0x33333333},
354                                  {0xffffffff, 0x00000000, 0x00000000},
355                                  {0xffffffe0, 0xffffff81, 0xffffff81},
356                                  {0xffffffe0, 0xffffffff, 0xffffffff},
357                                  {0x00007ffd, 0x00007ffd, 0x00007ffd},
358                                  {0x00000002, 0x33333333, 0x33333333},
359                                  {0x0000007f, 0x00000002, 0x00000002},
360                                  {0xffff8000, 0x0000007e, 0x0000007e},
361                                  {0x80000000, 0x00007fff, 0x00007fff},
362                                  {0xffff8000, 0xfffffffd, 0xfffffffd},
363                                  {0xffff8002, 0xcccccccc, 0xcccccccc},
364                                  {0xffff8003, 0xffffff82, 0xffffff82}};
365 
366 static const Inputs kRnIsNotRm[] = {{0x00007ffe, 0x00007ffe, 0x80000000},
367                                     {0x80000001, 0xcccccccc, 0x00000001},
368                                     {0x0000007d, 0x00000020, 0xcccccccc},
369                                     {0x7ffffffd, 0x55555555, 0x0000007f},
370                                     {0x00000020, 0xffff8001, 0x80000001},
371                                     {0xffffffe0, 0xffffff81, 0xffffff83},
372                                     {0x00000002, 0xffffff80, 0xaaaaaaaa},
373                                     {0x00000002, 0x00000001, 0x0000007d},
374                                     {0xffffff83, 0x00007ffd, 0x00000002},
375                                     {0xffff8000, 0x00000020, 0x55555555},
376                                     {0x00007ffd, 0x0000007f, 0x7ffffffd},
377                                     {0xfffffffd, 0x80000000, 0x00000000},
378                                     {0x0000007f, 0x33333333, 0x7fffffff},
379                                     {0x00000000, 0x33333333, 0x55555555},
380                                     {0xffffff81, 0x00007fff, 0xaaaaaaaa},
381                                     {0x33333333, 0x00000000, 0x7ffffffe},
382                                     {0xfffffffe, 0x00000001, 0xffffffff},
383                                     {0xaaaaaaaa, 0xffffffe0, 0xffffff80},
384                                     {0x33333333, 0xffffffe0, 0x7fffffff},
385                                     {0x55555555, 0x7ffffffd, 0xffffff81},
386                                     {0xaaaaaaaa, 0xffffffe0, 0x0000007f},
387                                     {0x00007fff, 0xfffffffd, 0xcccccccc},
388                                     {0x7fffffff, 0xffff8002, 0x00000001},
389                                     {0x0000007d, 0x00000002, 0x7fffffff},
390                                     {0x00000002, 0x55555555, 0x00007ffe},
391                                     {0x00007ffd, 0x0000007d, 0xfffffffe},
392                                     {0x7ffffffd, 0x55555555, 0xffffff83},
393                                     {0x7ffffffe, 0x80000001, 0x00000001},
394                                     {0xfffffffd, 0x80000001, 0x80000000},
395                                     {0xffff8002, 0x00000000, 0x80000001},
396                                     {0x0000007d, 0x00000020, 0x0000007e},
397                                     {0x00000020, 0x0000007f, 0x80000001},
398                                     {0x7ffffffe, 0x80000001, 0xffff8002},
399                                     {0xffffff81, 0xffffff80, 0xffffff81},
400                                     {0x7ffffffe, 0xffffff80, 0x0000007d},
401                                     {0x00007ffd, 0x00000000, 0x55555555},
402                                     {0xcccccccc, 0xcccccccc, 0x7ffffffe},
403                                     {0xffff8002, 0x00000001, 0xffff8000},
404                                     {0x00007ffe, 0x0000007d, 0xffffffe0},
405                                     {0x00007ffd, 0x7ffffffe, 0xffffffe0},
406                                     {0x80000001, 0x7ffffffe, 0xffff8001},
407                                     {0x55555555, 0xffffff80, 0xffff8003},
408                                     {0xfffffffd, 0xffff8002, 0x55555555},
409                                     {0x33333333, 0xffff8000, 0xffff8003},
410                                     {0x7ffffffd, 0xffff8002, 0x0000007e},
411                                     {0x7fffffff, 0xffffff80, 0xfffffffd},
412                                     {0xffff8002, 0x00000001, 0xaaaaaaaa},
413                                     {0xffffff83, 0xffff8001, 0x33333333},
414                                     {0x00000000, 0xfffffffe, 0x0000007e},
415                                     {0x80000000, 0xffffffe0, 0x0000007d},
416                                     {0xffff8000, 0x33333333, 0x00000002},
417                                     {0xfffffffe, 0x00000002, 0xffffffe0},
418                                     {0x00007ffd, 0x0000007e, 0x7ffffffe},
419                                     {0xffffff81, 0xffffffff, 0x00000000},
420                                     {0x0000007e, 0x7ffffffe, 0x33333333},
421                                     {0x00000002, 0x80000001, 0xffffff80},
422                                     {0x7ffffffd, 0xffffff82, 0x7ffffffe},
423                                     {0xffffff81, 0x0000007e, 0xffffff81},
424                                     {0xffff8002, 0x00007fff, 0x0000007f},
425                                     {0xcccccccc, 0xcccccccc, 0x0000007d},
426                                     {0x7ffffffd, 0xffffff80, 0x00007ffe},
427                                     {0x55555555, 0x55555555, 0xfffffffe},
428                                     {0x00000001, 0x00000020, 0x00000001},
429                                     {0x0000007f, 0x7ffffffd, 0x80000001},
430                                     {0x55555555, 0xaaaaaaaa, 0xfffffffd},
431                                     {0x00000020, 0x00007fff, 0xffffff81},
432                                     {0xffffff80, 0x33333333, 0xffffff81},
433                                     {0x00000000, 0xffff8001, 0xffffff83},
434                                     {0x0000007e, 0x33333333, 0x7ffffffd},
435                                     {0xaaaaaaaa, 0x55555555, 0xffffffff},
436                                     {0xfffffffd, 0xffff8000, 0x00000000},
437                                     {0xffff8000, 0xfffffffe, 0x7fffffff},
438                                     {0xffff8001, 0xcccccccc, 0x33333333},
439                                     {0x00007ffd, 0xffff8001, 0x0000007f},
440                                     {0xffffff83, 0x7ffffffd, 0x7fffffff},
441                                     {0xffffff81, 0xffffffe0, 0x00007fff},
442                                     {0xffffffff, 0x00000002, 0xffffff82},
443                                     {0xffff8001, 0xffffff81, 0x55555555},
444                                     {0x00007fff, 0xffffff83, 0x00007ffe},
445                                     {0x00007fff, 0xcccccccc, 0x00000020},
446                                     {0x7fffffff, 0x7ffffffd, 0xffff8000},
447                                     {0x0000007f, 0x80000000, 0x7ffffffd},
448                                     {0x0000007e, 0x0000007e, 0x0000007d},
449                                     {0xfffffffe, 0x00000020, 0xffffff80},
450                                     {0x00007ffd, 0x00007ffe, 0xffffff82},
451                                     {0x00000020, 0x80000000, 0x0000007e},
452                                     {0x00000020, 0x00000002, 0xffffffff},
453                                     {0x00007fff, 0xffff8000, 0x00007fff},
454                                     {0xcccccccc, 0x7ffffffd, 0x80000000},
455                                     {0xffffff80, 0x00000002, 0xffffff81},
456                                     {0xfffffffe, 0xffffff83, 0x33333333},
457                                     {0xffff8000, 0x7ffffffe, 0x55555555},
458                                     {0x00007fff, 0x0000007e, 0xffffff81},
459                                     {0xaaaaaaaa, 0xcccccccc, 0xffffffe0},
460                                     {0x7ffffffd, 0xffffffff, 0xffff8003},
461                                     {0xffffff81, 0xffff8002, 0xffffff82},
462                                     {0x00000002, 0xffffffe0, 0x80000001},
463                                     {0x7ffffffe, 0x33333333, 0xaaaaaaaa},
464                                     {0x00007fff, 0x00000020, 0xfffffffe},
465                                     {0xcccccccc, 0xfffffffe, 0x00000000},
466                                     {0x7ffffffe, 0x80000001, 0xfffffffd},
467                                     {0xffffffe0, 0x00000000, 0xaaaaaaaa},
468                                     {0x80000000, 0xffffff83, 0xffff8000},
469                                     {0xffffff81, 0xfffffffe, 0xffffff83},
470                                     {0x80000000, 0x0000007e, 0xffff8002},
471                                     {0x80000001, 0x00000020, 0x00007ffe},
472                                     {0x00000000, 0x00000020, 0x0000007e},
473                                     {0x80000001, 0xffffffff, 0x7ffffffe},
474                                     {0xffffff83, 0x33333333, 0x00000001},
475                                     {0x00000020, 0xffffff80, 0xffffff81},
476                                     {0xcccccccc, 0xffff8000, 0x00007ffd},
477                                     {0xffff8003, 0x00000000, 0x0000007f},
478                                     {0xffff8003, 0x00000001, 0x0000007d},
479                                     {0x0000007e, 0x00000020, 0x55555555},
480                                     {0xffffffff, 0xfffffffe, 0xffffff83},
481                                     {0xffffff80, 0x80000000, 0xfffffffe},
482                                     {0x0000007e, 0x0000007e, 0x00000000},
483                                     {0xaaaaaaaa, 0x00000002, 0xffffffe0},
484                                     {0xffff8001, 0x00007ffe, 0x00000020},
485                                     {0x7ffffffe, 0xffff8000, 0x00007ffe},
486                                     {0xffffff80, 0x33333333, 0x00007fff},
487                                     {0x33333333, 0xffffff81, 0x00000001},
488                                     {0x00000000, 0x33333333, 0x00000020},
489                                     {0x00007fff, 0x00000020, 0x0000007e},
490                                     {0xcccccccc, 0x55555555, 0xffffffe0},
491                                     {0xffffff80, 0x0000007e, 0x33333333},
492                                     {0x0000007e, 0xffff8002, 0x00000000},
493                                     {0xffffff83, 0xffffffe0, 0xfffffffd},
494                                     {0x00000001, 0x0000007f, 0xffff8001},
495                                     {0xffffffff, 0x00000020, 0xcccccccc},
496                                     {0x7ffffffd, 0xfffffffd, 0xffff8000},
497                                     {0xffffffff, 0x80000000, 0x00007fff},
498                                     {0x00000020, 0xffffffe0, 0xfffffffd},
499                                     {0x55555555, 0xffff8002, 0x0000007f},
500                                     {0xffff8002, 0x55555555, 0x0000007f},
501                                     {0xffff8003, 0xffff8000, 0x0000007d},
502                                     {0x00007fff, 0x00007ffe, 0x00000000},
503                                     {0xcccccccc, 0xffffff81, 0x00007fff},
504                                     {0x7ffffffd, 0xcccccccc, 0xffff8000},
505                                     {0x7ffffffe, 0xffff8001, 0x80000000},
506                                     {0x0000007f, 0xffffffe0, 0x80000001},
507                                     {0xffff8000, 0x00007ffd, 0xffff8003},
508                                     {0xffff8001, 0xffffff80, 0x00000001},
509                                     {0x33333333, 0x00000000, 0x80000000},
510                                     {0xfffffffe, 0x7ffffffd, 0x80000001},
511                                     {0x00000000, 0xffffffe0, 0x80000001},
512                                     {0xffff8002, 0x7ffffffe, 0x33333333},
513                                     {0xffffffe0, 0x0000007e, 0x00000001},
514                                     {0xffffff80, 0xffffffe0, 0x00007ffe},
515                                     {0xffff8000, 0x0000007f, 0x80000001},
516                                     {0xffffff80, 0xffffff80, 0xffff8002},
517                                     {0x7fffffff, 0x0000007f, 0xfffffffd},
518                                     {0x00000001, 0xffffffe0, 0xffffffff},
519                                     {0x55555555, 0x0000007f, 0xffff8002},
520                                     {0xffff8001, 0xfffffffd, 0x00000020},
521                                     {0x00007ffd, 0xfffffffd, 0x0000007e},
522                                     {0xfffffffd, 0xffff8003, 0x55555555},
523                                     {0xfffffffe, 0xffffff80, 0x0000007f},
524                                     {0x0000007f, 0x0000007f, 0xfffffffe},
525                                     {0x33333333, 0x55555555, 0xffff8003},
526                                     {0x0000007d, 0x00000020, 0x55555555},
527                                     {0xffff8003, 0x33333333, 0xffffff83},
528                                     {0xffffffe0, 0x00007fff, 0xffffff80},
529                                     {0x00000020, 0x00000001, 0x0000007d},
530                                     {0xffffff83, 0x00007fff, 0xaaaaaaaa},
531                                     {0xfffffffd, 0xffffff83, 0x55555555},
532                                     {0xffff8002, 0x00007fff, 0xffffffe0},
533                                     {0x80000001, 0xffffff82, 0x00007ffd},
534                                     {0xffff8000, 0x00000002, 0xffffffe0},
535                                     {0xfffffffd, 0xffffff80, 0x7ffffffe},
536                                     {0xffffffff, 0xaaaaaaaa, 0xffff8000},
537                                     {0x7ffffffd, 0x00000002, 0xffff8002},
538                                     {0xffff8001, 0x00007ffe, 0xffffffff},
539                                     {0xffff8001, 0x00000001, 0x00000000},
540                                     {0x80000001, 0xffffff80, 0x7ffffffd},
541                                     {0xffffff80, 0x0000007d, 0xffffff81},
542                                     {0xffffff81, 0xcccccccc, 0xffffff81},
543                                     {0xaaaaaaaa, 0x00000000, 0xffffff81},
544                                     {0x00000002, 0x00000020, 0xffffff81},
545                                     {0x7ffffffd, 0x7ffffffe, 0xffffff83},
546                                     {0x7fffffff, 0x55555555, 0x0000007f},
547                                     {0xfffffffd, 0xffffff82, 0xfffffffd},
548                                     {0x00000020, 0x00007ffd, 0xffffff81},
549                                     {0x0000007e, 0xffffff83, 0x33333333},
550                                     {0xffffff83, 0x00007fff, 0x00000002},
551                                     {0xffffffe0, 0xffffff80, 0xffff8001},
552                                     {0x00000000, 0x00000000, 0x00000002},
553                                     {0xfffffffd, 0x7ffffffe, 0x00000001},
554                                     {0xfffffffd, 0xfffffffd, 0xffffff80},
555                                     {0xffffff83, 0x00000000, 0x00007ffe},
556                                     {0xffff8002, 0xcccccccc, 0x33333333},
557                                     {0xffffff80, 0xffffff81, 0x0000007d},
558                                     {0x0000007e, 0xffff8000, 0x00007ffe},
559                                     {0x0000007e, 0x80000000, 0x80000001},
560                                     {0xffffff83, 0x00007fff, 0xffffffe0},
561                                     {0xcccccccc, 0x00007fff, 0xffffff80},
562                                     {0x0000007f, 0x0000007e, 0xfffffffd},
563                                     {0xcccccccc, 0xffffffff, 0x00000001},
564                                     {0xffffffff, 0xffff8000, 0xffffff82},
565                                     {0xfffffffe, 0x00000002, 0xffffffff}};
566 
567 
568 // A loop will be generated for each element of this array.
569 const TestLoopData kTests[] = {
570     {{r9, r8, r8}, "r9 r8 r8", "RnIsRm_r9_r8_r8", ARRAY_SIZE(kRnIsRm), kRnIsRm},
571     {{r6, r11, r11},
572      "r6 r11 r11",
573      "RnIsRm_r6_r11_r11",
574      ARRAY_SIZE(kRnIsRm),
575      kRnIsRm},
576     {{r3, r12, r12},
577      "r3 r12 r12",
578      "RnIsRm_r3_r12_r12",
579      ARRAY_SIZE(kRnIsRm),
580      kRnIsRm},
581     {{r14, r3, r3},
582      "r14 r3 r3",
583      "RnIsRm_r14_r3_r3",
584      ARRAY_SIZE(kRnIsRm),
585      kRnIsRm},
586     {{r10, r4, r4},
587      "r10 r4 r4",
588      "RnIsRm_r10_r4_r4",
589      ARRAY_SIZE(kRnIsRm),
590      kRnIsRm},
591     {{r6, r8, r8}, "r6 r8 r8", "RnIsRm_r6_r8_r8", ARRAY_SIZE(kRnIsRm), kRnIsRm},
592     {{r5, r7, r7}, "r5 r7 r7", "RnIsRm_r5_r7_r7", ARRAY_SIZE(kRnIsRm), kRnIsRm},
593     {{r6, r3, r3}, "r6 r3 r3", "RnIsRm_r6_r3_r3", ARRAY_SIZE(kRnIsRm), kRnIsRm},
594     {{r7, r0, r0}, "r7 r0 r0", "RnIsRm_r7_r0_r0", ARRAY_SIZE(kRnIsRm), kRnIsRm},
595     {{r2, r1, r1}, "r2 r1 r1", "RnIsRm_r2_r1_r1", ARRAY_SIZE(kRnIsRm), kRnIsRm},
596     {{r5, r3, r14},
597      "r5 r3 r14",
598      "RnIsNotRm_r5_r3_r14",
599      ARRAY_SIZE(kRnIsNotRm),
600      kRnIsNotRm},
601     {{r10, r8, r3},
602      "r10 r8 r3",
603      "RnIsNotRm_r10_r8_r3",
604      ARRAY_SIZE(kRnIsNotRm),
605      kRnIsNotRm},
606     {{r2, r8, r11},
607      "r2 r8 r11",
608      "RnIsNotRm_r2_r8_r11",
609      ARRAY_SIZE(kRnIsNotRm),
610      kRnIsNotRm},
611     {{r7, r12, r7},
612      "r7 r12 r7",
613      "RnIsNotRm_r7_r12_r7",
614      ARRAY_SIZE(kRnIsNotRm),
615      kRnIsNotRm},
616     {{r2, r4, r10},
617      "r2 r4 r10",
618      "RnIsNotRm_r2_r4_r10",
619      ARRAY_SIZE(kRnIsNotRm),
620      kRnIsNotRm},
621     {{r1, r10, r8},
622      "r1 r10 r8",
623      "RnIsNotRm_r1_r10_r8",
624      ARRAY_SIZE(kRnIsNotRm),
625      kRnIsNotRm},
626     {{r1, r4, r2},
627      "r1 r4 r2",
628      "RnIsNotRm_r1_r4_r2",
629      ARRAY_SIZE(kRnIsNotRm),
630      kRnIsNotRm},
631     {{r12, r2, r12},
632      "r12 r2 r12",
633      "RnIsNotRm_r12_r2_r12",
634      ARRAY_SIZE(kRnIsNotRm),
635      kRnIsNotRm},
636     {{r4, r8, r5},
637      "r4 r8 r5",
638      "RnIsNotRm_r4_r8_r5",
639      ARRAY_SIZE(kRnIsNotRm),
640      kRnIsNotRm},
641     {{r4, r0, r9},
642      "r4 r0 r9",
643      "RnIsNotRm_r4_r0_r9",
644      ARRAY_SIZE(kRnIsNotRm),
645      kRnIsNotRm}};
646 
647 // We record all inputs to the instructions as outputs. This way, we also check
648 // that what shouldn't change didn't change.
649 struct TestResult {
650   size_t output_size;
651   const Inputs* outputs;
652 };
653 
654 // These headers each contain an array of `TestResult` with the reference output
655 // values. The reference arrays are names `kReference{mnemonic}`.
656 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32b.h"
657 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32cb.h"
658 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32ch.h"
659 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32cw.h"
660 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32h.h"
661 #include "aarch32/traces/simulator-rd-rn-rm-t32-crc32w.h"
662 
663 
664 // The maximum number of errors to report in detail for each test.
665 const unsigned kErrorReportLimit = 8;
666 
667 typedef void (MacroAssembler::*Fn)(Register rd, Register rn, Register rm);
668 
TestHelper(Fn instruction,const char * mnemonic,const TestResult reference[])669 void TestHelper(Fn instruction,
670                 const char* mnemonic,
671                 const TestResult reference[]) {
672   SETUP();
673   masm.UseT32();
674   START();
675 
676   // Data to compare to `reference`.
677   TestResult* results[ARRAY_SIZE(kTests)];
678 
679   // Test cases for memory bound instructions may allocate a buffer and save its
680   // address in this array.
681   byte* scratch_memory_buffers[ARRAY_SIZE(kTests)];
682 
683   // Generate a loop for each element in `kTests`. Each loop tests one specific
684   // instruction.
685   for (unsigned i = 0; i < ARRAY_SIZE(kTests); i++) {
686     // Allocate results on the heap for this test.
687     results[i] = new TestResult;
688     results[i]->outputs = new Inputs[kTests[i].input_size];
689     results[i]->output_size = kTests[i].input_size;
690 
691     size_t input_stride = sizeof(kTests[i].inputs[0]) * kTests[i].input_size;
692     VIXL_ASSERT(IsUint32(input_stride));
693 
694     scratch_memory_buffers[i] = NULL;
695 
696     Label loop;
697     UseScratchRegisterScope scratch_registers(&masm);
698     // Include all registers from r0 ro r12.
699     scratch_registers.Include(RegisterList(0x1fff));
700 
701     // Values to pass to the macro-assembler.
702     Register rd = kTests[i].operands.rd;
703     Register rn = kTests[i].operands.rn;
704     Register rm = kTests[i].operands.rm;
705     scratch_registers.Exclude(rd);
706     scratch_registers.Exclude(rn);
707     scratch_registers.Exclude(rm);
708 
709     // Allocate reserved registers for our own use.
710     Register input_ptr = scratch_registers.Acquire();
711     Register input_end = scratch_registers.Acquire();
712     Register result_ptr = scratch_registers.Acquire();
713 
714     // Initialize `input_ptr` to the first element and `input_end` the address
715     // after the array.
716     __ Mov(input_ptr, Operand::From(kTests[i].inputs));
717     __ Add(input_end, input_ptr, static_cast<uint32_t>(input_stride));
718     __ Mov(result_ptr, Operand::From(results[i]->outputs));
719     __ Bind(&loop);
720 
721     __ Ldr(rd, MemOperand(input_ptr, offsetof(Inputs, rd)));
722     __ Ldr(rn, MemOperand(input_ptr, offsetof(Inputs, rn)));
723     __ Ldr(rm, MemOperand(input_ptr, offsetof(Inputs, rm)));
724 
725     (masm.*instruction)(rd, rn, rm);
726 
727     __ Str(rd, MemOperand(result_ptr, offsetof(Inputs, rd)));
728     __ Str(rn, MemOperand(result_ptr, offsetof(Inputs, rn)));
729     __ Str(rm, MemOperand(result_ptr, offsetof(Inputs, rm)));
730 
731     // Advance the result pointer.
732     __ Add(result_ptr, result_ptr, Operand::From(sizeof(kTests[i].inputs[0])));
733     // Loop back until `input_ptr` is lower than `input_base`.
734     __ Add(input_ptr, input_ptr, Operand::From(sizeof(kTests[i].inputs[0])));
735     __ Cmp(input_ptr, input_end);
736     __ B(ne, &loop);
737   }
738 
739   END();
740 
741   RUN();
742 
743   if (Test::generate_test_trace()) {
744     // Print the results.
745     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
746       printf("const Inputs kOutputs_%s_%s[] = {\n",
747              mnemonic,
748              kTests[i].identifier);
749       for (size_t j = 0; j < results[i]->output_size; j++) {
750         printf("  { ");
751         printf("0x%08" PRIx32, results[i]->outputs[j].rd);
752         printf(", ");
753         printf("0x%08" PRIx32, results[i]->outputs[j].rn);
754         printf(", ");
755         printf("0x%08" PRIx32, results[i]->outputs[j].rm);
756         printf(" },\n");
757       }
758       printf("};\n");
759     }
760     printf("const TestResult kReference%s[] = {\n", mnemonic);
761     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
762       printf("  {\n");
763       printf("    ARRAY_SIZE(kOutputs_%s_%s),\n",
764              mnemonic,
765              kTests[i].identifier);
766       printf("    kOutputs_%s_%s,\n", mnemonic, kTests[i].identifier);
767       printf("  },\n");
768     }
769     printf("};\n");
770   } else if (kCheckSimulatorTestResults) {
771     // Check the results.
772     unsigned total_error_count = 0;
773     for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
774       bool instruction_has_errors = false;
775       for (size_t j = 0; j < kTests[i].input_size; j++) {
776         uint32_t rd = results[i]->outputs[j].rd;
777         uint32_t rn = results[i]->outputs[j].rn;
778         uint32_t rm = results[i]->outputs[j].rm;
779         uint32_t rd_input = kTests[i].inputs[j].rd;
780         uint32_t rn_input = kTests[i].inputs[j].rn;
781         uint32_t rm_input = kTests[i].inputs[j].rm;
782         uint32_t rd_ref = reference[i].outputs[j].rd;
783         uint32_t rn_ref = reference[i].outputs[j].rn;
784         uint32_t rm_ref = reference[i].outputs[j].rm;
785 
786         if (((rd != rd_ref) || (rn != rn_ref) || (rm != rm_ref)) &&
787             (++total_error_count <= kErrorReportLimit)) {
788           // Print the instruction once even if it triggered multiple failures.
789           if (!instruction_has_errors) {
790             printf("Error(s) when testing \"%s %s\":\n",
791                    mnemonic,
792                    kTests[i].operands_description);
793             instruction_has_errors = true;
794           }
795           // Print subsequent errors.
796           printf("  Input:    ");
797           printf("0x%08" PRIx32, rd_input);
798           printf(", ");
799           printf("0x%08" PRIx32, rn_input);
800           printf(", ");
801           printf("0x%08" PRIx32, rm_input);
802           printf("\n");
803           printf("  Expected: ");
804           printf("0x%08" PRIx32, rd_ref);
805           printf(", ");
806           printf("0x%08" PRIx32, rn_ref);
807           printf(", ");
808           printf("0x%08" PRIx32, rm_ref);
809           printf("\n");
810           printf("  Found:    ");
811           printf("0x%08" PRIx32, rd);
812           printf(", ");
813           printf("0x%08" PRIx32, rn);
814           printf(", ");
815           printf("0x%08" PRIx32, rm);
816           printf("\n\n");
817         }
818       }
819     }
820 
821     if (total_error_count > kErrorReportLimit) {
822       printf("%u other errors follow.\n",
823              total_error_count - kErrorReportLimit);
824     }
825     VIXL_CHECK(total_error_count == 0);
826   } else {
827     VIXL_WARNING("Assembled the code, but did not run anything.\n");
828   }
829 
830   for (size_t i = 0; i < ARRAY_SIZE(kTests); i++) {
831     delete[] results[i]->outputs;
832     delete results[i];
833     delete[] scratch_memory_buffers[i];
834   }
835 
836   TEARDOWN();
837 }
838 
839 // Instantiate tests for each instruction in the list.
840 // TODO: Remove this limitation by having a sandboxing mechanism.
841 #if defined(VIXL_HOST_POINTER_32)
842 #define TEST(mnemonic)                                                      \
843   void Test_##mnemonic() {                                                  \
844     TestHelper(&MacroAssembler::mnemonic, #mnemonic, kReference##mnemonic); \
845   }                                                                         \
846   Test test_##mnemonic("AARCH32_SIMULATOR_RD_RN_RM_T32_" #mnemonic,         \
847                        &Test_##mnemonic);
848 #else
849 #define TEST(mnemonic)                                              \
850   void Test_##mnemonic() {                                          \
851     VIXL_WARNING("This test can only run on a 32-bit host.\n");     \
852     USE(TestHelper);                                                \
853   }                                                                 \
854   Test test_##mnemonic("AARCH32_SIMULATOR_RD_RN_RM_T32_" #mnemonic, \
855                        &Test_##mnemonic);
856 #endif
857 
858 FOREACH_INSTRUCTION(TEST)
859 #undef TEST
860 
861 }  // namespace
862 #endif
863 
864 }  // namespace aarch32
865 }  // namespace vixl
866