1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_
18 #define ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_
19
20 #include <stdint.h>
21 #include <memory>
22 #include <vector>
23
24 #include <android-base/logging.h>
25
26 #include "base/bit_utils_iterator.h"
27 #include "base/macros.h"
28 #include "dwarf/register.h"
29 #include "offsets.h"
30 #include "utils/arm64/managed_register_arm64.h"
31 #include "utils/assembler.h"
32
33 // TODO(VIXL): Make VIXL compile with -Wshadow.
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wshadow"
36 #include "aarch64/disasm-aarch64.h"
37 #include "aarch64/macro-assembler-aarch64.h"
38 #pragma GCC diagnostic pop
39
40 namespace art HIDDEN {
41
42 class Arm64InstructionSetFeatures;
43
44 namespace arm64 {
45
DWARFReg(vixl::aarch64::CPURegister reg)46 static inline dwarf::Reg DWARFReg(vixl::aarch64::CPURegister reg) {
47 if (reg.IsFPRegister()) {
48 return dwarf::Reg::Arm64Fp(reg.GetCode());
49 } else {
50 DCHECK_LT(reg.GetCode(), 31u); // X0 - X30.
51 return dwarf::Reg::Arm64Core(reg.GetCode());
52 }
53 }
54
55 #define MEM_OP(...) vixl::aarch64::MemOperand(__VA_ARGS__)
56
57 enum LoadOperandType {
58 kLoadSignedByte,
59 kLoadUnsignedByte,
60 kLoadSignedHalfword,
61 kLoadUnsignedHalfword,
62 kLoadWord,
63 kLoadCoreWord,
64 kLoadSWord,
65 kLoadDWord
66 };
67
68 enum StoreOperandType {
69 kStoreByte,
70 kStoreHalfword,
71 kStoreWord,
72 kStoreCoreWord,
73 kStoreSWord,
74 kStoreDWord
75 };
76
77 class Arm64Assembler final : public Assembler {
78 public:
79 explicit Arm64Assembler(
80 ArenaAllocator* allocator, const Arm64InstructionSetFeatures* features = nullptr);
81
~Arm64Assembler()82 virtual ~Arm64Assembler() {}
83
GetVIXLAssembler()84 vixl::aarch64::MacroAssembler* GetVIXLAssembler() { return &vixl_masm_; }
85
86 // Finalize the code.
87 void FinalizeCode() override;
88
89 // Size of generated code.
90 size_t CodeSize() const override;
91 const uint8_t* CodeBufferBaseAddress() const override;
92
93 // Copy instructions out of assembly buffer into the given region of memory.
94 void CopyInstructions(const MemoryRegion& region) override;
95
96 void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs);
97
98 void SpillRegisters(vixl::aarch64::CPURegList registers, int offset);
99 void UnspillRegisters(vixl::aarch64::CPURegList registers, int offset);
100
101 // A helper to save/restore a list of ZRegisters to a specified stack offset location.
102 template <bool is_save>
SaveRestoreZRegisterList(uint32_t vreg_bit_vector,int64_t stack_offset)103 void SaveRestoreZRegisterList(uint32_t vreg_bit_vector, int64_t stack_offset) {
104 if (vreg_bit_vector == 0) {
105 return;
106 }
107 vixl::aarch64::UseScratchRegisterScope temps(GetVIXLAssembler());
108 vixl::aarch64::Register temp = temps.AcquireX();
109 vixl_masm_.Add(temp, vixl::aarch64::sp, stack_offset);
110 size_t slot_no = 0;
111 for (uint32_t i : LowToHighBits(vreg_bit_vector)) {
112 if (is_save) {
113 vixl_masm_.Str(vixl::aarch64::ZRegister(i),
114 vixl::aarch64::SVEMemOperand(temp, slot_no, vixl::aarch64::SVE_MUL_VL));
115 } else {
116 vixl_masm_.Ldr(vixl::aarch64::ZRegister(i),
117 vixl::aarch64::SVEMemOperand(temp, slot_no, vixl::aarch64::SVE_MUL_VL));
118 }
119 slot_no++;
120 }
121 }
122
123 // Jump to address (not setting link register)
124 void JumpTo(ManagedRegister m_base, Offset offs, ManagedRegister m_scratch);
125
126 //
127 // Heap poisoning.
128 //
129
130 // Poison a heap reference contained in `reg`.
131 void PoisonHeapReference(vixl::aarch64::Register reg);
132 // Unpoison a heap reference contained in `reg`.
133 void UnpoisonHeapReference(vixl::aarch64::Register reg);
134 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
135 void MaybePoisonHeapReference(vixl::aarch64::Register reg);
136 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
137 void MaybeUnpoisonHeapReference(vixl::aarch64::Register reg);
138
139 // Emit code checking the status of the Marking Register, and aborting
140 // the program if MR does not match the value stored in the art::Thread
141 // object.
142 //
143 // Argument `temp` is used as a temporary register to generate code.
144 // Argument `code` is used to identify the different occurrences of
145 // MaybeGenerateMarkingRegisterCheck and is passed to the BRK instruction.
146 void GenerateMarkingRegisterCheck(vixl::aarch64::Register temp, int code = 0);
147
Bind(Label * label)148 void Bind([[maybe_unused]] Label* label) override {
149 UNIMPLEMENTED(FATAL) << "Do not use Bind(Label*) for ARM64";
150 }
Jump(Label * label)151 void Jump([[maybe_unused]] Label* label) override {
152 UNIMPLEMENTED(FATAL) << "Do not use Jump(Label*) for ARM64";
153 }
154
Bind(vixl::aarch64::Label * label)155 void Bind(vixl::aarch64::Label* label) {
156 vixl_masm_.Bind(label);
157 }
Jump(vixl::aarch64::Label * label)158 void Jump(vixl::aarch64::Label* label) {
159 vixl_masm_.B(label);
160 }
161
reg_x(int code)162 static vixl::aarch64::Register reg_x(int code) {
163 CHECK(code < kNumberOfXRegisters) << code;
164 if (code == SP) {
165 return vixl::aarch64::sp;
166 } else if (code == XZR) {
167 return vixl::aarch64::xzr;
168 }
169 return vixl::aarch64::XRegister(code);
170 }
171
reg_w(int code)172 static vixl::aarch64::Register reg_w(int code) {
173 CHECK(code < kNumberOfWRegisters) << code;
174 if (code == WSP) {
175 return vixl::aarch64::wsp;
176 } else if (code == WZR) {
177 return vixl::aarch64::wzr;
178 }
179 return vixl::aarch64::WRegister(code);
180 }
181
reg_d(int code)182 static vixl::aarch64::VRegister reg_d(int code) {
183 return vixl::aarch64::DRegister(code);
184 }
185
reg_s(int code)186 static vixl::aarch64::VRegister reg_s(int code) {
187 return vixl::aarch64::SRegister(code);
188 }
189
190 private:
191 // VIXL assembler.
192 vixl::aarch64::MacroAssembler vixl_masm_;
193
194 // Used for testing.
195 friend class Arm64ManagedRegister_VixlRegisters_Test;
196 };
197
198 } // namespace arm64
199 } // namespace art
200
201 #endif // ART_COMPILER_UTILS_ARM64_ASSEMBLER_ARM64_H_
202