blob: 5661249695b77f2be2de710afbf0a06348af845f [file] [log] [blame]
Artem Serov12e097c2016-08-08 15:13:26 +01001/*
2 * Copyright (C) 2016 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_ARM_ASSEMBLER_ARM_VIXL_H_
18#define ART_COMPILER_UTILS_ARM_ASSEMBLER_ARM_VIXL_H_
19
20#include "base/arena_containers.h"
21#include "base/logging.h"
22#include "constants_arm.h"
23#include "offsets.h"
24#include "utils/arm/assembler_arm_shared.h"
25#include "utils/arm/managed_register_arm.h"
26#include "utils/assembler.h"
27#include "utils/jni_macro_assembler.h"
28
29// TODO(VIXL): Make VIXL compile with -Wshadow and remove pragmas.
30#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Wshadow"
32#include "aarch32/macro-assembler-aarch32.h"
33#pragma GCC diagnostic pop
34
35namespace vixl32 = vixl::aarch32;
36
37namespace art {
38namespace arm {
39
xueliang.zhongf51bc622016-11-04 09:23:32 +000040class ArmVIXLMacroAssembler FINAL : public vixl32::MacroAssembler {
41 public:
42 // The following interfaces can generate CMP+Bcc or Cbz/Cbnz.
43 // CMP+Bcc are generated by default.
44 // If a hint is given (is_far_target = false) and rn and label can all fit into Cbz/Cbnz,
45 // then Cbz/Cbnz is generated.
46 // Prefer following interfaces to using vixl32::MacroAssembler::Cbz/Cbnz.
47 // In T32, Cbz/Cbnz instructions have following limitations:
48 // - Far targets, which are over 126 bytes away, are not supported.
49 // - Only low registers can be encoded.
50 // - Backward branches are not supported.
51 void CompareAndBranchIfZero(vixl32::Register rn,
52 vixl32::Label* label,
53 bool is_far_target = true);
54 void CompareAndBranchIfNonZero(vixl32::Register rn,
55 vixl32::Label* label,
56 bool is_far_target = true);
Scott Wakelingbffdc702016-12-07 17:46:03 +000057
58 // In T32 some of the instructions (add, mov, etc) outside an IT block
59 // have only 32-bit encodings. But there are 16-bit flag setting
60 // versions of these instructions (adds, movs, etc). In most of the
61 // cases in ART we don't care if the instructions keep flags or not;
62 // thus we can benefit from smaller code size.
63 // VIXL will never generate flag setting versions (for example, adds
64 // for Add macro instruction) unless vixl32::DontCare option is
65 // explicitly specified. That's why we introduce wrappers to use
66 // DontCare option by default.
67#define WITH_FLAGS_DONT_CARE_RD_RN_OP(func_name) \
68 void (func_name)(vixl32::Register rd, vixl32::Register rn, const vixl32::Operand& operand) { \
69 MacroAssembler::func_name(vixl32::DontCare, rd, rn, operand); \
70 } \
71 using MacroAssembler::func_name
72
73 WITH_FLAGS_DONT_CARE_RD_RN_OP(Adc);
74 WITH_FLAGS_DONT_CARE_RD_RN_OP(Sub);
75 WITH_FLAGS_DONT_CARE_RD_RN_OP(Sbc);
76 WITH_FLAGS_DONT_CARE_RD_RN_OP(Rsb);
77 WITH_FLAGS_DONT_CARE_RD_RN_OP(Rsc);
78
79 WITH_FLAGS_DONT_CARE_RD_RN_OP(Eor);
80 WITH_FLAGS_DONT_CARE_RD_RN_OP(Orr);
81 WITH_FLAGS_DONT_CARE_RD_RN_OP(Orn);
82 WITH_FLAGS_DONT_CARE_RD_RN_OP(And);
83 WITH_FLAGS_DONT_CARE_RD_RN_OP(Bic);
84
85 WITH_FLAGS_DONT_CARE_RD_RN_OP(Asr);
86 WITH_FLAGS_DONT_CARE_RD_RN_OP(Lsr);
87 WITH_FLAGS_DONT_CARE_RD_RN_OP(Lsl);
88 WITH_FLAGS_DONT_CARE_RD_RN_OP(Ror);
89
90#undef WITH_FLAGS_DONT_CARE_RD_RN_OP
91
92#define WITH_FLAGS_DONT_CARE_RD_OP(func_name) \
93 void (func_name)(vixl32::Register rd, const vixl32::Operand& operand) { \
94 MacroAssembler::func_name(vixl32::DontCare, rd, operand); \
95 } \
96 using MacroAssembler::func_name
97
98 WITH_FLAGS_DONT_CARE_RD_OP(Mvn);
99 WITH_FLAGS_DONT_CARE_RD_OP(Mov);
100
101#undef WITH_FLAGS_DONT_CARE_RD_OP
102
103 // The following two functions don't fall into above categories. Overload them separately.
104 void Rrx(vixl32::Register rd, vixl32::Register rn) {
105 MacroAssembler::Rrx(vixl32::DontCare, rd, rn);
106 }
107 using MacroAssembler::Rrx;
108
109 void Mul(vixl32::Register rd, vixl32::Register rn, vixl32::Register rm) {
110 MacroAssembler::Mul(vixl32::DontCare, rd, rn, rm);
111 }
112 using MacroAssembler::Mul;
113
114 // TODO: Remove when MacroAssembler::Add(FlagsUpdate, Condition, Register, Register, Operand)
115 // makes the right decision about 16-bit encodings.
116 void Add(vixl32::Register rd, vixl32::Register rn, const vixl32::Operand& operand) {
117 if (rd.Is(rn)) {
118 MacroAssembler::Add(rd, rn, operand);
119 } else {
120 MacroAssembler::Add(vixl32::DontCare, rd, rn, operand);
121 }
122 }
123 using MacroAssembler::Add;
124
125 // These interfaces try to use 16-bit T2 encoding of B instruction.
126 void B(vixl32::Label* label);
127 void B(vixl32::Condition cond, vixl32::Label* label);
xueliang.zhongf51bc622016-11-04 09:23:32 +0000128};
129
Artem Serov12e097c2016-08-08 15:13:26 +0100130class ArmVIXLAssembler FINAL : public Assembler {
131 private:
132 class ArmException;
133 public:
134 explicit ArmVIXLAssembler(ArenaAllocator* arena)
135 : Assembler(arena) {
136 // Use Thumb2 instruction set.
137 vixl_masm_.UseT32();
138 }
139
140 virtual ~ArmVIXLAssembler() {}
xueliang.zhongf51bc622016-11-04 09:23:32 +0000141 ArmVIXLMacroAssembler* GetVIXLAssembler() { return &vixl_masm_; }
Artem Serov12e097c2016-08-08 15:13:26 +0100142 void FinalizeCode() OVERRIDE;
143
144 // Size of generated code.
145 size_t CodeSize() const OVERRIDE;
146 const uint8_t* CodeBufferBaseAddress() const OVERRIDE;
147
148 // Copy instructions out of assembly buffer into the given region of memory.
149 void FinalizeInstructions(const MemoryRegion& region) OVERRIDE;
150
151 void Bind(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
152 UNIMPLEMENTED(FATAL) << "Do not use Bind for ARM";
153 }
154 void Jump(Label* label ATTRIBUTE_UNUSED) OVERRIDE {
155 UNIMPLEMENTED(FATAL) << "Do not use Jump for ARM";
156 }
157
158 //
159 // Heap poisoning.
160 //
161 // Poison a heap reference contained in `reg`.
162 void PoisonHeapReference(vixl32::Register reg);
163 // Unpoison a heap reference contained in `reg`.
164 void UnpoisonHeapReference(vixl32::Register reg);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100165 // Poison a heap reference contained in `reg` if heap poisoning is enabled.
166 void MaybePoisonHeapReference(vixl32::Register reg);
Artem Serov12e097c2016-08-08 15:13:26 +0100167 // Unpoison a heap reference contained in `reg` if heap poisoning is enabled.
168 void MaybeUnpoisonHeapReference(vixl32::Register reg);
169
170 void StoreToOffset(StoreOperandType type,
171 vixl32::Register reg,
172 vixl32::Register base,
173 int32_t offset);
174 void StoreSToOffset(vixl32::SRegister source, vixl32::Register base, int32_t offset);
175 void StoreDToOffset(vixl32::DRegister source, vixl32::Register base, int32_t offset);
176
177 void LoadImmediate(vixl32::Register dest, int32_t value);
178 void LoadFromOffset(LoadOperandType type,
179 vixl32::Register reg,
180 vixl32::Register base,
181 int32_t offset);
182 void LoadSFromOffset(vixl32::SRegister reg, vixl32::Register base, int32_t offset);
183 void LoadDFromOffset(vixl32::DRegister reg, vixl32::Register base, int32_t offset);
184
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100185 void LoadRegisterList(RegList regs, size_t stack_offset);
186 void StoreRegisterList(RegList regs, size_t stack_offset);
187
Artem Serov12e097c2016-08-08 15:13:26 +0100188 bool ShifterOperandCanAlwaysHold(uint32_t immediate);
Artem Serov02109dd2016-09-23 17:17:54 +0100189 bool ShifterOperandCanHold(Opcode opcode, uint32_t immediate, SetCc set_cc = kCcDontCare);
Artem Serov12e097c2016-08-08 15:13:26 +0100190 bool CanSplitLoadStoreOffset(int32_t allowed_offset_bits,
191 int32_t offset,
192 /*out*/ int32_t* add_to_base,
193 /*out*/ int32_t* offset_for_load_store);
194 int32_t AdjustLoadStoreOffset(int32_t allowed_offset_bits,
195 vixl32::Register temp,
196 vixl32::Register base,
197 int32_t offset);
198 int32_t GetAllowedLoadOffsetBits(LoadOperandType type);
199 int32_t GetAllowedStoreOffsetBits(StoreOperandType type);
200
201 void AddConstant(vixl32::Register rd, int32_t value);
202 void AddConstant(vixl32::Register rd, vixl32::Register rn, int32_t value);
203 void AddConstantInIt(vixl32::Register rd,
204 vixl32::Register rn,
205 int32_t value,
206 vixl32::Condition cond = vixl32::al);
207
Artem Serovc5fcb442016-12-02 19:19:58 +0000208 template <typename T>
209 vixl::aarch32::Literal<T>* CreateLiteralDestroyedWithPool(T value) {
210 vixl::aarch32::Literal<T>* literal =
211 new vixl::aarch32::Literal<T>(value,
212 vixl32::RawLiteral::kPlacedWhenUsed,
213 vixl32::RawLiteral::kDeletedOnPoolDestruction);
214 return literal;
215 }
216
Artem Serov12e097c2016-08-08 15:13:26 +0100217 private:
218 // VIXL assembler.
xueliang.zhongf51bc622016-11-04 09:23:32 +0000219 ArmVIXLMacroAssembler vixl_masm_;
Artem Serov12e097c2016-08-08 15:13:26 +0100220};
221
222// Thread register declaration.
223extern const vixl32::Register tr;
224
225} // namespace arm
226} // namespace art
227
228#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_ARM_VIXL_H_