1b8021494Sopenharmony_ci// Copyright 2017, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#ifndef VIXL_AARCH32_TEST_UTILS_AARCH32_H_
28b8021494Sopenharmony_ci#define VIXL_AARCH32_TEST_UTILS_AARCH32_H_
29b8021494Sopenharmony_ci
30b8021494Sopenharmony_ci#include "../test-pool-manager.h"
31b8021494Sopenharmony_ci#include "../test-runner.h"
32b8021494Sopenharmony_ci#include "aarch32/constants-aarch32.h"
33b8021494Sopenharmony_ci#include "aarch32/instructions-aarch32.h"
34b8021494Sopenharmony_ci#include "aarch32/macro-assembler-aarch32.h"
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_cinamespace vixl {
37b8021494Sopenharmony_ci
38b8021494Sopenharmony_cinamespace aarch32 {
39b8021494Sopenharmony_ci
40b8021494Sopenharmony_ciclass TestMacroAssembler {
41b8021494Sopenharmony_ci public:
42b8021494Sopenharmony_ci  explicit TestMacroAssembler(MacroAssembler* masm)
43b8021494Sopenharmony_ci      : test(&masm->pool_manager_) {}
44b8021494Sopenharmony_ci  int32_t GetPoolCheckpoint() const { return test.GetPoolCheckpoint(); }
45b8021494Sopenharmony_ci  int GetPoolSize() const { return test.GetPoolSize(); }
46b8021494Sopenharmony_ci  bool PoolIsEmpty() const { return test.PoolIsEmpty(); }
47b8021494Sopenharmony_ci
48b8021494Sopenharmony_ci private:
49b8021494Sopenharmony_ci  TestPoolManager test;
50b8021494Sopenharmony_ci};
51b8021494Sopenharmony_ci
52b8021494Sopenharmony_ci// Only check the simulator tests when we can actually run them.
53b8021494Sopenharmony_ci// TODO: Improve this.
54b8021494Sopenharmony_ci#if defined(__arm__)
55b8021494Sopenharmony_cistatic const bool kCheckSimulatorTestResults = true;
56b8021494Sopenharmony_ci#else
57b8021494Sopenharmony_cistatic const bool kCheckSimulatorTestResults = false;
58b8021494Sopenharmony_ci#endif
59b8021494Sopenharmony_ci
60b8021494Sopenharmony_ci// Helper constants used to check for condition code combinations.  These are
61b8021494Sopenharmony_ci// not part of instruction definitions as no instruction uses them directly.
62b8021494Sopenharmony_ciconst uint32_t NoFlag = 0x0;
63b8021494Sopenharmony_ciconst uint32_t NFlag = 0x80000000;
64b8021494Sopenharmony_ciconst uint32_t ZFlag = 0x40000000;
65b8021494Sopenharmony_ciconst uint32_t CFlag = 0x20000000;
66b8021494Sopenharmony_ciconst uint32_t VFlag = 0x10000000;
67b8021494Sopenharmony_ciconst uint32_t NZFlag = NFlag | ZFlag;
68b8021494Sopenharmony_ciconst uint32_t NCFlag = NFlag | CFlag;
69b8021494Sopenharmony_ciconst uint32_t NVFlag = NFlag | VFlag;
70b8021494Sopenharmony_ciconst uint32_t ZCFlag = ZFlag | CFlag;
71b8021494Sopenharmony_ciconst uint32_t ZVFlag = ZFlag | VFlag;
72b8021494Sopenharmony_ciconst uint32_t CVFlag = CFlag | VFlag;
73b8021494Sopenharmony_ciconst uint32_t NZCFlag = NFlag | ZFlag | CFlag;
74b8021494Sopenharmony_ciconst uint32_t NZVFlag = NFlag | ZFlag | VFlag;
75b8021494Sopenharmony_ciconst uint32_t NCVFlag = NFlag | CFlag | VFlag;
76b8021494Sopenharmony_ciconst uint32_t ZCVFlag = ZFlag | CFlag | VFlag;
77b8021494Sopenharmony_ciconst uint32_t NZCVFlag = NFlag | ZFlag | CFlag | VFlag;
78b8021494Sopenharmony_ciconst uint32_t QFlag = 0x08000000;
79b8021494Sopenharmony_ci
80b8021494Sopenharmony_ciconst uint32_t GE0Flag = 0x00010000;
81b8021494Sopenharmony_ciconst uint32_t GE1Flag = 0x00020000;
82b8021494Sopenharmony_ciconst uint32_t GE2Flag = 0x00040000;
83b8021494Sopenharmony_ciconst uint32_t GE3Flag = 0x00080000;
84b8021494Sopenharmony_ciconst uint32_t GE01Flag = GE0Flag | GE1Flag;
85b8021494Sopenharmony_ciconst uint32_t GE02Flag = GE0Flag | GE2Flag;
86b8021494Sopenharmony_ciconst uint32_t GE03Flag = GE0Flag | GE3Flag;
87b8021494Sopenharmony_ciconst uint32_t GE12Flag = GE1Flag | GE2Flag;
88b8021494Sopenharmony_ciconst uint32_t GE13Flag = GE1Flag | GE3Flag;
89b8021494Sopenharmony_ciconst uint32_t GE23Flag = GE2Flag | GE3Flag;
90b8021494Sopenharmony_ciconst uint32_t GE012Flag = GE0Flag | GE1Flag | GE2Flag;
91b8021494Sopenharmony_ciconst uint32_t GE013Flag = GE0Flag | GE1Flag | GE3Flag;
92b8021494Sopenharmony_ciconst uint32_t GE023Flag = GE0Flag | GE2Flag | GE3Flag;
93b8021494Sopenharmony_ciconst uint32_t GE123Flag = GE1Flag | GE2Flag | GE3Flag;
94b8021494Sopenharmony_ciconst uint32_t GE0123Flag = GE0Flag | GE1Flag | GE2Flag | GE3Flag;
95b8021494Sopenharmony_ciconst uint32_t GEFlags = GE0123Flag;
96b8021494Sopenharmony_ci
97b8021494Sopenharmony_cistruct vec128_t {
98b8021494Sopenharmony_ci  uint64_t l;
99b8021494Sopenharmony_ci  uint64_t h;
100b8021494Sopenharmony_ci};
101b8021494Sopenharmony_ci
102b8021494Sopenharmony_ciclass RegisterDump {
103b8021494Sopenharmony_ci public:
104b8021494Sopenharmony_ci  RegisterDump() : completed_(false) {
105b8021494Sopenharmony_ci    VIXL_ASSERT(sizeof(dump_.r_[0]) == kRegSizeInBytes);
106b8021494Sopenharmony_ci  }
107b8021494Sopenharmony_ci
108b8021494Sopenharmony_ci  // The Dump method generates code to store a snapshot of the register values.
109b8021494Sopenharmony_ci  // It needs to be able to use the stack temporarily.
110b8021494Sopenharmony_ci  //
111b8021494Sopenharmony_ci  // The dumping code is generated though the given MacroAssembler. No registers
112b8021494Sopenharmony_ci  // are corrupted in the process apart for the program counter, but the stack
113b8021494Sopenharmony_ci  // is used briefly.  Note the program counter cannot be retrieved from the
114b8021494Sopenharmony_ci  // register dump anyway.
115b8021494Sopenharmony_ci  void Dump(MacroAssembler* masm);
116b8021494Sopenharmony_ci
117b8021494Sopenharmony_ci  // Register accessors.
118b8021494Sopenharmony_ci  int32_t reg(unsigned code) const {
119b8021494Sopenharmony_ci    VIXL_ASSERT(IsComplete());
120b8021494Sopenharmony_ci    // The collected program counter should not be accessed.
121b8021494Sopenharmony_ci    VIXL_ASSERT(code != kPcCode);
122b8021494Sopenharmony_ci    return dump_.r_[code];
123b8021494Sopenharmony_ci  }
124b8021494Sopenharmony_ci
125b8021494Sopenharmony_ci  // QRegister accessors
126b8021494Sopenharmony_ci  vec128_t GetQRegisterBits(unsigned code) const {
127b8021494Sopenharmony_ci    VIXL_ASSERT(IsComplete());
128b8021494Sopenharmony_ci    VIXL_ASSERT(code < kNumberOfQRegisters);
129b8021494Sopenharmony_ci    vec128_t content = {dump_.d_[code * 2], dump_.d_[(code * 2) + 1]};
130b8021494Sopenharmony_ci    return content;
131b8021494Sopenharmony_ci  }
132b8021494Sopenharmony_ci
133b8021494Sopenharmony_ci  // DRegister accessors
134b8021494Sopenharmony_ci  uint64_t GetDRegisterBits(unsigned code) const {
135b8021494Sopenharmony_ci    VIXL_ASSERT(IsComplete());
136b8021494Sopenharmony_ci    VIXL_ASSERT(code < kMaxNumberOfDRegisters);
137b8021494Sopenharmony_ci    return dump_.d_[code];
138b8021494Sopenharmony_ci  }
139b8021494Sopenharmony_ci
140b8021494Sopenharmony_ci  // SRegister accessors
141b8021494Sopenharmony_ci  uint32_t GetSRegisterBits(unsigned code) const {
142b8021494Sopenharmony_ci    VIXL_ASSERT(IsComplete());
143b8021494Sopenharmony_ci    VIXL_ASSERT(code < kNumberOfSRegisters);
144b8021494Sopenharmony_ci    if ((code % 2) == 0) {
145b8021494Sopenharmony_ci      return GetDRegisterBits(code / 2) & 0xffffffff;
146b8021494Sopenharmony_ci    } else {
147b8021494Sopenharmony_ci      return GetDRegisterBits(code / 2) >> 32;
148b8021494Sopenharmony_ci    }
149b8021494Sopenharmony_ci    VIXL_UNREACHABLE();
150b8021494Sopenharmony_ci    return 0;
151b8021494Sopenharmony_ci  }
152b8021494Sopenharmony_ci
153b8021494Sopenharmony_ci  // Stack pointer accessors.
154b8021494Sopenharmony_ci  int32_t spreg() const { return reg(kSPRegNum); }
155b8021494Sopenharmony_ci
156b8021494Sopenharmony_ci  // Flags accessors.
157b8021494Sopenharmony_ci  uint32_t flags_nzcv() const {
158b8021494Sopenharmony_ci    VIXL_ASSERT(IsComplete());
159b8021494Sopenharmony_ci    return dump_.flags_ & NZCVFlag;
160b8021494Sopenharmony_ci  }
161b8021494Sopenharmony_ci
162b8021494Sopenharmony_ci  bool IsComplete() const { return completed_; }
163b8021494Sopenharmony_ci
164b8021494Sopenharmony_ci private:
165b8021494Sopenharmony_ci  // Indicate whether the dump operation has been completed.
166b8021494Sopenharmony_ci  bool completed_;
167b8021494Sopenharmony_ci
168b8021494Sopenharmony_ci  // Store all the dumped elements in a simple struct so the implementation can
169b8021494Sopenharmony_ci  // use offsetof to quickly find the correct field.
170b8021494Sopenharmony_ci  struct dump_t {
171b8021494Sopenharmony_ci    // Core registers, except for PC.
172b8021494Sopenharmony_ci    uint32_t r_[kNumberOfRegisters - 1];
173b8021494Sopenharmony_ci    uint64_t d_[kMaxNumberOfDRegisters];
174b8021494Sopenharmony_ci
175b8021494Sopenharmony_ci    // NZCV flags, stored in bits 28 to 31.
176b8021494Sopenharmony_ci    // bit[31] : Negative
177b8021494Sopenharmony_ci    // bit[30] : Zero
178b8021494Sopenharmony_ci    // bit[29] : Carry
179b8021494Sopenharmony_ci    // bit[28] : oVerflow
180b8021494Sopenharmony_ci    uint32_t flags_;
181b8021494Sopenharmony_ci  } dump_;
182b8021494Sopenharmony_ci};
183b8021494Sopenharmony_ci
184b8021494Sopenharmony_cibool Equal32(uint32_t expected, const RegisterDump* core, const Register& reg);
185b8021494Sopenharmony_cibool Equal32(uint32_t expected, const RegisterDump* core, uint32_t result);
186b8021494Sopenharmony_cibool Equal32(uint32_t expected,
187b8021494Sopenharmony_ci             const RegisterDump* core,
188b8021494Sopenharmony_ci             const SRegister& sreg);
189b8021494Sopenharmony_cibool Equal64(uint64_t expected,
190b8021494Sopenharmony_ci             const RegisterDump* core,
191b8021494Sopenharmony_ci             const DRegister& dreg);
192b8021494Sopenharmony_cibool Equal128(uint64_t expected_h,
193b8021494Sopenharmony_ci              uint64_t expected_l,
194b8021494Sopenharmony_ci              const RegisterDump* core,
195b8021494Sopenharmony_ci              const QRegister& qreg);
196b8021494Sopenharmony_cibool EqualFP32(float expected, const RegisterDump* core, const SRegister& dreg);
197b8021494Sopenharmony_cibool EqualFP64(double expected,
198b8021494Sopenharmony_ci               const RegisterDump* core,
199b8021494Sopenharmony_ci               const DRegister& dreg);
200b8021494Sopenharmony_cibool EqualNzcv(uint32_t expected, uint32_t result);
201b8021494Sopenharmony_ci
202b8021494Sopenharmony_ci}  // namespace aarch32
203b8021494Sopenharmony_ci}  // namespace vixl
204b8021494Sopenharmony_ci
205b8021494Sopenharmony_ci#endif  // VIXL_AARCH32_TEST_UTILS_AARCH32_H_
206