aboutsummaryrefslogtreecommitdiff
path: root/test/aarch64/test-assembler-aarch64.h
blob: cc4160b4cd6482d24348f10f6fd4f2a02fce46f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2019, VIXL authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of ARM Limited nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Test infrastructure.
//
// Tests are functions which accept no parameters and have no return values.
// The testing code should not perform an explicit return once completed. For
// example to test the mov immediate instruction a very simple test would be:
//
//   TEST(mov_x0_one) {
//     SETUP();
//
//     START();
//     __ mov(x0, 1);
//     END();
//
//     if (CAN_RUN()) {
//       RUN();
//
//       ASSERT_EQUAL_64(1, x0);
//     }
//
//     TEARDOWN();
//   }
//
// Within a START ... END block all registers but sp can be modified. sp has to
// be explicitly saved/restored. The END() macro replaces the function return
// so it may appear multiple times in a test if the test has multiple exit
// points.
//
// Tests requiring specific CPU features should specify exactly what they
// require using SETUP_WITH_FEATURES(...) instead of SETUP().
//
// Once the test has been run all integer and floating point registers as well
// as flags are accessible through a RegisterDump instance, see
// utils-aarch64.cc for more info on RegisterDump.
//
// We provide some helper assert to handle common cases:
//
//   ASSERT_EQUAL_32(int32_t, int_32t)
//   ASSERT_EQUAL_FP32(float, float)
//   ASSERT_EQUAL_32(int32_t, W register)
//   ASSERT_EQUAL_FP32(float, S register)
//   ASSERT_EQUAL_64(int64_t, int_64t)
//   ASSERT_EQUAL_FP64(double, double)
//   ASSERT_EQUAL_64(int64_t, X register)
//   ASSERT_EQUAL_64(X register, X register)
//   ASSERT_EQUAL_FP64(double, D register)
//
// e.g. ASSERT_EQUAL_64(0.5, d30);
//
// If more advanced computation is required before the assert then access the
// RegisterDump named core directly:
//
//   ASSERT_EQUAL_64(0x1234, core->reg_x0() & 0xffff);

namespace vixl {
namespace aarch64 {

#define __ masm.
#define TEST(name) TEST_(AARCH64_ASM_##name)

// PushCalleeSavedRegisters(), PopCalleeSavedRegisters() and Dump() use NEON, so
// we need to enable it in the infrastructure code for each test.
static const CPUFeatures kInfrastructureCPUFeatures(CPUFeatures::kNEON);

#ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
// Run tests with the simulator.

#define SETUP()        \
  MacroAssembler masm; \
  SETUP_COMMON()

#define SETUP_WITH_FEATURES(...)                 \
  MacroAssembler masm;                           \
  SETUP_COMMON();                                \
  masm.SetCPUFeatures(CPUFeatures(__VA_ARGS__)); \
  simulator.SetCPUFeatures(CPUFeatures(__VA_ARGS__))

#define SETUP_CUSTOM(size, pic)                                       \
  byte* buf = new byte[size + CodeBuffer::kDefaultCapacity];          \
  MacroAssembler masm(buf, size + CodeBuffer::kDefaultCapacity, pic); \
  SETUP_COMMON()

#define SETUP_COMMON()                                                   \
  bool queried_can_run = false;                                          \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */ \
  USE(queried_can_run);                                                  \
  masm.SetCPUFeatures(CPUFeatures::None());                              \
  masm.SetGenerateSimulatorCode(true);                                   \
  Decoder simulator_decoder;                                             \
  Simulator simulator(&simulator_decoder);                               \
  simulator.SetColouredTrace(Test::coloured_trace());                    \
  simulator.SetInstructionStats(Test::instruction_stats());              \
  simulator.SetCPUFeatures(CPUFeatures::None());                         \
  RegisterDump core;                                                     \
  ptrdiff_t offset_after_infrastructure_start;                           \
  ptrdiff_t offset_before_infrastructure_end

#define START()                                                               \
  masm.Reset();                                                               \
  simulator.ResetState();                                                     \
  {                                                                           \
    SimulationCPUFeaturesScope cpu(&masm, kInfrastructureCPUFeatures);        \
    __ PushCalleeSavedRegisters();                                            \
  }                                                                           \
  {                                                                           \
    int trace_parameters = 0;                                                 \
    if (Test::trace_reg()) trace_parameters |= LOG_STATE;                     \
    if (Test::trace_write()) trace_parameters |= LOG_WRITE;                   \
    if (Test::trace_sim()) trace_parameters |= LOG_DISASM;                    \
    if (Test::trace_branch()) trace_parameters |= LOG_BRANCH;                 \
    if (trace_parameters != 0) {                                              \
      __ Trace(static_cast<TraceParameters>(trace_parameters), TRACE_ENABLE); \
    }                                                                         \
  }                                                                           \
  if (Test::instruction_stats()) {                                            \
    __ EnableInstrumentation();                                               \
  }                                                                           \
  offset_after_infrastructure_start = masm.GetCursorOffset();                 \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */      \
  USE(offset_after_infrastructure_start)

#define END()                                                            \
  offset_before_infrastructure_end = masm.GetCursorOffset();             \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */ \
  USE(offset_before_infrastructure_end);                                 \
  if (Test::instruction_stats()) {                                       \
    __ DisableInstrumentation();                                         \
  }                                                                      \
  __ Trace(LOG_ALL, TRACE_DISABLE);                                      \
  {                                                                      \
    SimulationCPUFeaturesScope cpu(&masm, kInfrastructureCPUFeatures);   \
    core.Dump(&masm);                                                    \
    __ PopCalleeSavedRegisters();                                        \
  }                                                                      \
  __ Ret();                                                              \
  masm.FinalizeCode()

inline bool CanRun(const CPUFeatures& required, bool* queried_can_run) {
  // The Simulator can run any test that VIXL can assemble.
  USE(required);
  *queried_can_run = true;
  return true;
}


#define RUN()                                                                  \
  RUN_WITHOUT_SEEN_FEATURE_CHECK();                                            \
  {                                                                            \
    /* We expect the test to use all of the features it requested, plus the */ \
    /* features that the instructure code requires.                         */ \
    CPUFeatures const& expected =                                              \
        simulator.GetCPUFeatures()->With(CPUFeatures::kNEON);                  \
    CPUFeatures const& seen = simulator.GetSeenFeatures();                     \
    /* This gives three broad categories of features that we care about:    */ \
    /*  1. Things both expected and seen.                                   */ \
    /*  2. Things seen, but not expected. The simulator catches these.      */ \
    /*  3. Things expected, but not seen. We check these here.              */ \
    /* In a valid, passing test, categories 2 and 3 should be empty.        */ \
    if (seen != expected) {                                                    \
      /* The Simulator should have caught anything in category 2 already.   */ \
      VIXL_ASSERT(expected.Has(seen));                                         \
      /* Anything left is category 3: things expected, but not seen. This   */ \
      /* is not necessarily a bug in VIXL itself, but indicates that the    */ \
      /* test is less strict than it could be.                              */ \
      CPUFeatures missing = expected.Without(seen);                            \
      VIXL_ASSERT(missing.Count() > 0);                                        \
      std::cout << "Error: expected to see CPUFeatures { " << missing          \
                << " }\n";                                                     \
      VIXL_ABORT();                                                            \
    }                                                                          \
  }

#define RUN_WITHOUT_SEEN_FEATURE_CHECK() \
  DISASSEMBLE();                         \
  VIXL_ASSERT(QUERIED_CAN_RUN());        \
  VIXL_ASSERT(CAN_RUN());                \
  simulator.RunFrom(masm.GetBuffer()->GetStartAddress<Instruction*>())

#define RUN_CUSTOM() RUN()

#define TEARDOWN()

#define TEARDOWN_CUSTOM() delete[] buf;

#else  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH64.
#define SETUP()        \
  MacroAssembler masm; \
  SETUP_COMMON()

#define SETUP_WITH_FEATURES(...) \
  MacroAssembler masm;           \
  SETUP_COMMON();                \
  masm.SetCPUFeatures(CPUFeatures(__VA_ARGS__))

#define SETUP_CUSTOM(size, pic)                                         \
  byte* buffer =                                                        \
      reinterpret_cast<byte*>(mmap(NULL,                                \
                                   size + CodeBuffer::kDefaultCapacity, \
                                   PROT_READ | PROT_WRITE,              \
                                   MAP_PRIVATE | MAP_ANONYMOUS,         \
                                   -1,                                  \
                                   0));                                 \
  size_t buffer_size = size + CodeBuffer::kDefaultCapacity;             \
  MacroAssembler masm(buffer, buffer_size, pic);                        \
  SETUP_COMMON()

#define SETUP_COMMON()                                                   \
  bool queried_can_run = false;                                          \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */ \
  USE(queried_can_run);                                                  \
  masm.SetCPUFeatures(CPUFeatures::None());                              \
  masm.SetGenerateSimulatorCode(false);                                  \
  RegisterDump core;                                                     \
  CPU::SetUp();                                                          \
  ptrdiff_t offset_after_infrastructure_start;                           \
  ptrdiff_t offset_before_infrastructure_end

#define START()                                                          \
  masm.Reset();                                                          \
  {                                                                      \
    CPUFeaturesScope cpu(&masm, kInfrastructureCPUFeatures);             \
    __ PushCalleeSavedRegisters();                                       \
  }                                                                      \
  offset_after_infrastructure_start = masm.GetCursorOffset();            \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */ \
  USE(offset_after_infrastructure_start)

#define END()                                                            \
  offset_before_infrastructure_end = masm.GetCursorOffset();             \
  /* Avoid unused-variable warnings in case a test never calls RUN(). */ \
  USE(offset_before_infrastructure_end);                                 \
  {                                                                      \
    CPUFeaturesScope cpu(&masm, kInfrastructureCPUFeatures);             \
    core.Dump(&masm);                                                    \
    __ PopCalleeSavedRegisters();                                        \
  }                                                                      \
  __ Ret();                                                              \
  masm.FinalizeCode()

// Execute the generated code from the memory area.
#define RUN()                                               \
  DISASSEMBLE();                                            \
  VIXL_ASSERT(QUERIED_CAN_RUN());                           \
  VIXL_ASSERT(CAN_RUN());                                   \
  masm.GetBuffer()->SetExecutable();                        \
  ExecuteMemory(masm.GetBuffer()->GetStartAddress<byte*>(), \
                masm.GetSizeOfCodeGenerated());             \
  masm.GetBuffer()->SetWritable()

// This just provides compatibility with VIXL_INCLUDE_SIMULATOR_AARCH64 builds.
// We cannot run seen-feature checks when running natively.
#define RUN_WITHOUT_SEEN_FEATURE_CHECK() RUN()

// The generated code was written directly into `buffer`, execute it directly.
#define RUN_CUSTOM()                                    \
  DISASSEMBLE();                                        \
  mprotect(buffer, buffer_size, PROT_READ | PROT_EXEC); \
  ExecuteMemory(buffer, buffer_size);                   \
  mprotect(buffer, buffer_size, PROT_READ | PROT_WRITE)

#define TEARDOWN()

#define TEARDOWN_CUSTOM()

inline bool CanRun(const CPUFeatures& required, bool* queried_can_run) {
  CPUFeatures cpu = CPUFeatures::InferFromOS();
  // If InferFromOS fails, assume that basic features are present.
  if (cpu.HasNoFeatures()) cpu = CPUFeatures::AArch64LegacyBaseline();

  VIXL_ASSERT(cpu.Has(kInfrastructureCPUFeatures));

  if (cpu.Has(required)) {
    *queried_can_run = true;
    return true;
  }

  // Only warn if we haven't already checked CanRun(...).
  if (!*queried_can_run) {
    *queried_can_run = true;
    CPUFeatures missing = required.Without(cpu);
    // Note: This message needs to match REGEXP_MISSING_FEATURES from
    // tools/threaded_test.py.
    std::cout << "SKIPPED: Missing features: { " << missing << " }\n";
    std::cout << "This test requires the following features to run its "
                 "generated code on this CPU: "
              << required << "\n";
  }
  return false;
}

#endif  // ifdef VIXL_INCLUDE_SIMULATOR_AARCH64.

#define CAN_RUN() CanRun(*masm.GetCPUFeatures(), &queried_can_run)
#define QUERIED_CAN_RUN() (queried_can_run)

#define DISASSEMBLE()                                                     \
  if (Test::disassemble()) {                                              \
    PrintDisassembler disasm(stdout);                                     \
    CodeBuffer* buffer = masm.GetBuffer();                                \
    Instruction* start = buffer->GetOffsetAddress<Instruction*>(          \
        offset_after_infrastructure_start);                               \
    Instruction* end = buffer->GetOffsetAddress<Instruction*>(            \
        offset_before_infrastructure_end);                                \
                                                                          \
    if (Test::disassemble_infrastructure()) {                             \
      Instruction* infra_start = buffer->GetStartAddress<Instruction*>(); \
      printf("# Infrastructure code (prologue)\n");                       \
      disasm.DisassembleBuffer(infra_start, start);                       \
      printf("# Test code\n");                                            \
    } else {                                                              \
      printf(                                                             \
          "# Warning: Omitting infrastructure code. "                     \
          "Use --disassemble to see it.\n");                              \
    }                                                                     \
                                                                          \
    disasm.DisassembleBuffer(start, end);                                 \
                                                                          \
    if (Test::disassemble_infrastructure()) {                             \
      printf("# Infrastructure code (epilogue)\n");                       \
      Instruction* infra_end = buffer->GetEndAddress<Instruction*>();     \
      disasm.DisassembleBuffer(end, infra_end);                           \
    }                                                                     \
  }

#define ASSERT_EQUAL_NZCV(expected) \
  VIXL_CHECK(EqualNzcv(expected, core.flags_nzcv()))

#define ASSERT_EQUAL_REGISTERS(expected) \
  VIXL_CHECK(EqualRegisters(&expected, &core))

#define ASSERT_EQUAL_FP16(expected, result) \
  VIXL_CHECK(EqualFP16(expected, &core, result))

#define ASSERT_EQUAL_32(expected, result) \
  VIXL_CHECK(Equal32(static_cast<uint32_t>(expected), &core, result))

#define ASSERT_EQUAL_FP32(expected, result) \
  VIXL_CHECK(EqualFP32(expected, &core, result))

#define ASSERT_EQUAL_64(expected, result) \
  VIXL_CHECK(Equal64(expected, &core, result))

#define ASSERT_NOT_EQUAL_64(expected, result) \
  VIXL_CHECK(NotEqual64(expected, &core, result))

#define ASSERT_EQUAL_FP64(expected, result) \
  VIXL_CHECK(EqualFP64(expected, &core, result))

#define ASSERT_EQUAL_128(expected_h, expected_l, result) \
  VIXL_CHECK(Equal128(expected_h, expected_l, &core, result))

#define ASSERT_LITERAL_POOL_SIZE(expected) \
  VIXL_CHECK((expected + kInstructionSize) == (masm.GetLiteralPoolSize()))

#define MUST_FAIL_WITH_MESSAGE(code, message)                     \
  {                                                               \
    bool aborted = false;                                         \
    try {                                                         \
      code;                                                       \
    } catch (const std::runtime_error& e) {                       \
      const char* expected_error = message;                       \
      size_t error_length = strlen(expected_error);               \
      if (strncmp(expected_error, e.what(), error_length) == 0) { \
        aborted = true;                                           \
      } else {                                                    \
        printf("Mismatch in error message.\n");                   \
        printf("Expected: %s\n", expected_error);                 \
        printf("Found:    %s\n", e.what());                       \
      }                                                           \
    }                                                             \
    VIXL_CHECK(aborted);                                          \
  }

}  // namespace aarch64
}  // namespace vixl