blob: 584a3877822fbb8d5cef6169c3a1025f01834a2d [file] [log] [blame]
Dave Allison65fcc2c2014-04-28 13:45:27 -07001/*
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_ARM_ASSEMBLER_THUMB2_H_
18#define ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_
19
Vladimir Markocf93a5c2015-06-16 11:33:24 +000020#include <deque>
Dave Allison65fcc2c2014-04-28 13:45:27 -070021#include <vector>
22
23#include "base/logging.h"
24#include "constants_arm.h"
25#include "utils/arm/managed_register_arm.h"
26#include "utils/arm/assembler_arm.h"
Vladimir Marko6b756b52015-07-14 11:58:38 +010027#include "utils/array_ref.h"
Dave Allison65fcc2c2014-04-28 13:45:27 -070028#include "offsets.h"
Dave Allison65fcc2c2014-04-28 13:45:27 -070029
30namespace art {
31namespace arm {
32
Dave Allison65fcc2c2014-04-28 13:45:27 -070033class Thumb2Assembler FINAL : public ArmAssembler {
34 public:
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +010035 explicit Thumb2Assembler(bool can_relocate_branches = true)
36 : can_relocate_branches_(can_relocate_branches),
Nicolas Geoffray8d486732014-07-16 16:23:40 +010037 force_32bit_(false),
38 it_cond_index_(kNoItCondition),
Vladimir Markocf93a5c2015-06-16 11:33:24 +000039 next_condition_(AL),
40 fixups_(),
Vladimir Marko6b756b52015-07-14 11:58:38 +010041 fixup_dependents_(),
Vladimir Markocf93a5c2015-06-16 11:33:24 +000042 literals_(),
43 last_position_adjustment_(0u),
44 last_old_position_(0u),
45 last_fixup_id_(0u) {
Dave Allison65fcc2c2014-04-28 13:45:27 -070046 }
47
48 virtual ~Thumb2Assembler() {
Dave Allison65fcc2c2014-04-28 13:45:27 -070049 }
50
51 bool IsThumb() const OVERRIDE {
52 return true;
53 }
54
55 bool IsForced32Bit() const {
56 return force_32bit_;
57 }
58
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +010059 bool CanRelocateBranches() const {
60 return can_relocate_branches_;
Nicolas Geoffray8d486732014-07-16 16:23:40 +010061 }
62
Vladimir Markocf93a5c2015-06-16 11:33:24 +000063 void FinalizeCode() OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070064
65 // Data-processing instructions.
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010066 virtual void and_(Register rd, Register rn, const ShifterOperand& so,
67 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070068
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010069 virtual void eor(Register rd, Register rn, const ShifterOperand& so,
70 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070071
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010072 virtual void sub(Register rd, Register rn, const ShifterOperand& so,
73 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070074
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010075 virtual void rsb(Register rd, Register rn, const ShifterOperand& so,
76 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070077
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010078 virtual void add(Register rd, Register rn, const ShifterOperand& so,
79 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070080
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010081 virtual void adc(Register rd, Register rn, const ShifterOperand& so,
82 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070083
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010084 virtual void sbc(Register rd, Register rn, const ShifterOperand& so,
85 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070086
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010087 virtual void rsc(Register rd, Register rn, const ShifterOperand& so,
88 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -070089
90 void tst(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
91
92 void teq(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
93
94 void cmp(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
95
96 void cmn(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
97
Vladimir Marko73cf0fb2015-07-30 15:07:22 +010098 virtual void orr(Register rd, Register rn, const ShifterOperand& so,
99 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700100
Vladimir Markod2b4ca22015-09-14 15:13:26 +0100101 virtual void orn(Register rd, Register rn, const ShifterOperand& so,
102 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
103
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100104 virtual void mov(Register rd, const ShifterOperand& so,
105 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700106
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100107 virtual void bic(Register rd, Register rn, const ShifterOperand& so,
108 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700109
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100110 virtual void mvn(Register rd, const ShifterOperand& so,
111 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700112
113 // Miscellaneous data-processing instructions.
114 void clz(Register rd, Register rm, Condition cond = AL) OVERRIDE;
115 void movw(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
116 void movt(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100117 void rbit(Register rd, Register rm, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700118
119 // Multiply instructions.
120 void mul(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
121 void mla(Register rd, Register rn, Register rm, Register ra,
122 Condition cond = AL) OVERRIDE;
123 void mls(Register rd, Register rn, Register rm, Register ra,
124 Condition cond = AL) OVERRIDE;
Zheng Xuc6667102015-05-15 16:08:45 +0800125 void smull(Register rd_lo, Register rd_hi, Register rn, Register rm,
126 Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700127 void umull(Register rd_lo, Register rd_hi, Register rn, Register rm,
128 Condition cond = AL) OVERRIDE;
129
130 void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
131 void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
132
Roland Levillain981e4542014-11-14 11:47:14 +0000133 // Bit field extract instructions.
Roland Levillain51d3fc42014-11-13 14:11:42 +0000134 void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain981e4542014-11-14 11:47:14 +0000135 void ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain51d3fc42014-11-13 14:11:42 +0000136
Dave Allison65fcc2c2014-04-28 13:45:27 -0700137 // Load/store instructions.
138 void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
139 void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
140
141 void ldrb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
142 void strb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
143
144 void ldrh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
145 void strh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
146
147 void ldrsb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
148 void ldrsh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
149
Roland Levillain4af147e2015-04-07 13:54:49 +0100150 // Load/store register dual instructions using registers `rd` and `rd` + 1.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700151 void ldrd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
152 void strd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
153
Roland Levillain4af147e2015-04-07 13:54:49 +0100154 // Load/store register dual instructions using registers `rd` and `rd2`.
155 // Note that contrary to the ARM A1 encoding, the Thumb-2 T1 encoding
156 // does not require `rd` to be even, nor `rd2' to be equal to `rd` + 1.
157 void ldrd(Register rd, Register rd2, const Address& ad, Condition cond);
158 void strd(Register rd, Register rd2, const Address& ad, Condition cond);
159
160
Dave Allison65fcc2c2014-04-28 13:45:27 -0700161 void ldm(BlockAddressMode am, Register base,
162 RegList regs, Condition cond = AL) OVERRIDE;
163 void stm(BlockAddressMode am, Register base,
164 RegList regs, Condition cond = AL) OVERRIDE;
165
166 void ldrex(Register rd, Register rn, Condition cond = AL) OVERRIDE;
167 void strex(Register rd, Register rt, Register rn, Condition cond = AL) OVERRIDE;
168
169 void ldrex(Register rd, Register rn, uint16_t imm, Condition cond = AL);
170 void strex(Register rd, Register rt, Register rn, uint16_t imm, Condition cond = AL);
171
Calin Juravle52c48962014-12-16 17:02:57 +0000172 void ldrexd(Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
173 void strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700174
175 // Miscellaneous instructions.
176 void clrex(Condition cond = AL) OVERRIDE;
177 void nop(Condition cond = AL) OVERRIDE;
178
179 void bkpt(uint16_t imm16) OVERRIDE;
180 void svc(uint32_t imm24) OVERRIDE;
181
182 // If-then
183 void it(Condition firstcond, ItState i1 = kItOmitted,
184 ItState i2 = kItOmitted, ItState i3 = kItOmitted) OVERRIDE;
185
186 void cbz(Register rn, Label* target) OVERRIDE;
187 void cbnz(Register rn, Label* target) OVERRIDE;
188
189 // Floating point instructions (VFPv3-D16 and VFPv3-D32 profiles).
190 void vmovsr(SRegister sn, Register rt, Condition cond = AL) OVERRIDE;
191 void vmovrs(Register rt, SRegister sn, Condition cond = AL) OVERRIDE;
192 void vmovsrr(SRegister sm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
193 void vmovrrs(Register rt, Register rt2, SRegister sm, Condition cond = AL) OVERRIDE;
194 void vmovdrr(DRegister dm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
195 void vmovrrd(Register rt, Register rt2, DRegister dm, Condition cond = AL) OVERRIDE;
196 void vmovs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
197 void vmovd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
198
199 // Returns false if the immediate cannot be encoded.
200 bool vmovs(SRegister sd, float s_imm, Condition cond = AL) OVERRIDE;
201 bool vmovd(DRegister dd, double d_imm, Condition cond = AL) OVERRIDE;
202
203 void vldrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
204 void vstrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
205 void vldrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
206 void vstrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
207
208 void vadds(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
209 void vaddd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
210 void vsubs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
211 void vsubd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
212 void vmuls(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
213 void vmuld(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
214 void vmlas(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
215 void vmlad(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
216 void vmlss(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
217 void vmlsd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
218 void vdivs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
219 void vdivd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
220
221 void vabss(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
222 void vabsd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
223 void vnegs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
224 void vnegd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
225 void vsqrts(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
226 void vsqrtd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
227
228 void vcvtsd(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
229 void vcvtds(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
230 void vcvtis(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
231 void vcvtid(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
232 void vcvtsi(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
233 void vcvtdi(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
234 void vcvtus(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
235 void vcvtud(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
236 void vcvtsu(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
237 void vcvtdu(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
238
239 void vcmps(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
240 void vcmpd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
241 void vcmpsz(SRegister sd, Condition cond = AL) OVERRIDE;
242 void vcmpdz(DRegister dd, Condition cond = AL) OVERRIDE;
243 void vmstat(Condition cond = AL) OVERRIDE; // VMRS APSR_nzcv, FPSCR
244
245 void vpushs(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
246 void vpushd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
247 void vpops(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
248 void vpopd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
249
250 // Branch instructions.
251 void b(Label* label, Condition cond = AL);
252 void bl(Label* label, Condition cond = AL);
253 void blx(Label* label);
254 void blx(Register rm, Condition cond = AL) OVERRIDE;
255 void bx(Register rm, Condition cond = AL) OVERRIDE;
256
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100257 virtual void Lsl(Register rd, Register rm, uint32_t shift_imm,
258 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
259 virtual void Lsr(Register rd, Register rm, uint32_t shift_imm,
260 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
261 virtual void Asr(Register rd, Register rm, uint32_t shift_imm,
262 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
263 virtual void Ror(Register rd, Register rm, uint32_t shift_imm,
264 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
265 virtual void Rrx(Register rd, Register rm,
266 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison45fdb932014-06-25 12:37:10 -0700267
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100268 virtual void Lsl(Register rd, Register rm, Register rn,
269 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
270 virtual void Lsr(Register rd, Register rm, Register rn,
271 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
272 virtual void Asr(Register rd, Register rm, Register rn,
273 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
274 virtual void Ror(Register rd, Register rm, Register rn,
275 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700276
277 void Push(Register rd, Condition cond = AL) OVERRIDE;
278 void Pop(Register rd, Condition cond = AL) OVERRIDE;
279
280 void PushList(RegList regs, Condition cond = AL) OVERRIDE;
281 void PopList(RegList regs, Condition cond = AL) OVERRIDE;
282
283 void Mov(Register rd, Register rm, Condition cond = AL) OVERRIDE;
284
285 void CompareAndBranchIfZero(Register r, Label* label) OVERRIDE;
286 void CompareAndBranchIfNonZero(Register r, Label* label) OVERRIDE;
287
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288 // Memory barriers.
289 void dmb(DmbOptions flavor) OVERRIDE;
290
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000291 // Get the final position of a label after local fixup based on the old position
292 // recorded before FinalizeCode().
293 uint32_t GetAdjustedPosition(uint32_t old_position) OVERRIDE;
294
295 using ArmAssembler::NewLiteral; // Make the helper template visible.
296
297 Literal* NewLiteral(size_t size, const uint8_t* data) OVERRIDE;
298 void LoadLiteral(Register rt, Literal* literal) OVERRIDE;
299 void LoadLiteral(Register rt, Register rt2, Literal* literal) OVERRIDE;
300 void LoadLiteral(SRegister sd, Literal* literal) OVERRIDE;
301 void LoadLiteral(DRegister dd, Literal* literal) OVERRIDE;
302
Dave Allison65fcc2c2014-04-28 13:45:27 -0700303 // Add signed constant value to rd. May clobber IP.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700304 void AddConstant(Register rd, Register rn, int32_t value,
Vladimir Marko449b1092015-09-08 12:16:45 +0100305 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700306
307 // Load and Store. May clobber IP.
308 void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700309 void MarkExceptionHandler(Label* label) OVERRIDE;
310 void LoadFromOffset(LoadOperandType type,
311 Register reg,
312 Register base,
313 int32_t offset,
314 Condition cond = AL) OVERRIDE;
315 void StoreToOffset(StoreOperandType type,
316 Register reg,
317 Register base,
318 int32_t offset,
319 Condition cond = AL) OVERRIDE;
320 void LoadSFromOffset(SRegister reg,
321 Register base,
322 int32_t offset,
323 Condition cond = AL) OVERRIDE;
324 void StoreSToOffset(SRegister reg,
325 Register base,
326 int32_t offset,
327 Condition cond = AL) OVERRIDE;
328 void LoadDFromOffset(DRegister reg,
329 Register base,
330 int32_t offset,
331 Condition cond = AL) OVERRIDE;
332 void StoreDToOffset(DRegister reg,
333 Register base,
334 int32_t offset,
335 Condition cond = AL) OVERRIDE;
336
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000337 bool ShifterOperandCanHold(Register rd,
338 Register rn,
339 Opcode opcode,
340 uint32_t immediate,
341 ShifterOperand* shifter_op) OVERRIDE;
342
Dave Allison65fcc2c2014-04-28 13:45:27 -0700343
Ian Rogers13735952014-10-08 12:43:28 -0700344 static bool IsInstructionForExceptionHandling(uintptr_t pc);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700345
346 // Emit data (e.g. encoded instruction or immediate) to the.
347 // instruction stream.
348 void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format.
349 void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format.
350 void Bind(Label* label) OVERRIDE;
351
352 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
353
354 // Force the assembler to generate 32 bit instructions.
355 void Force32Bit() {
356 force_32bit_ = true;
357 }
358
359 private:
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000360 typedef uint16_t FixupId;
361
362 // Fixup: branches and literal pool references.
363 //
364 // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This
365 // depends on both the type of branch and the offset to which it is branching. The 16-bit
366 // cbz and cbnz instructions may also need to be replaced with a separate 16-bit compare
367 // instruction and a 16- or 32-bit branch instruction. Load from a literal pool can also be
368 // 16-bit or 32-bit instruction and, if the method is large, we may need to use a sequence
369 // of instructions to make up for the limited range of load literal instructions (up to
370 // 4KiB for the 32-bit variant). When generating code for these insns we don't know the
371 // size before hand, so we assume it is the smallest available size and determine the final
372 // code offsets and sizes and emit code in FinalizeCode().
373 //
374 // To handle this, we keep a record of every branch and literal pool load in the program.
375 // The actual instruction encoding for these is delayed until we know the final size of
376 // every instruction. When we bind a label to a branch we don't know the final location yet
377 // as some preceding instructions may need to be expanded, so we record a non-final offset.
378 // In FinalizeCode(), we expand the sizes of branches and literal loads that are out of
379 // range. With each expansion, we need to update dependent Fixups, i.e. insntructios with
380 // target on the other side of the expanded insn, as their offsets change and this may
381 // trigger further expansion.
382 //
383 // All Fixups have a 'fixup id' which is a 16 bit unsigned number used to identify the
384 // Fixup. For each unresolved label we keep a singly-linked list of all Fixups pointing
385 // to it, using the fixup ids as links. The first link is stored in the label's position
386 // (the label is linked but not bound), the following links are stored in the code buffer,
387 // in the placeholder where we will eventually emit the actual code.
388
389 class Fixup {
390 public:
391 // Branch type.
392 enum Type : uint8_t {
393 kConditional, // B<cond>.
394 kUnconditional, // B.
395 kUnconditionalLink, // BL.
396 kUnconditionalLinkX, // BLX.
397 kCompareAndBranchXZero, // cbz/cbnz.
398 kLoadLiteralNarrow, // Load narrrow integer literal.
399 kLoadLiteralWide, // Load wide integer literal.
400 kLoadFPLiteralSingle, // Load FP literal single.
401 kLoadFPLiteralDouble, // Load FP literal double.
402 };
403
404 // Calculated size of branch instruction based on type and offset.
405 enum Size : uint8_t {
406 // Branch variants.
407 kBranch16Bit,
408 kBranch32Bit,
409 // NOTE: We don't support branches which would require multiple instructions, i.e.
410 // conditinoal branches beyond +-1MiB and unconditional branches beyond +-16MiB.
411
412 // CBZ/CBNZ variants.
413 kCbxz16Bit, // CBZ/CBNZ rX, label; X < 8; 7-bit positive offset.
414 kCbxz32Bit, // CMP rX, #0 + Bcc label; X < 8; 16-bit Bcc; +-8-bit offset.
415 kCbxz48Bit, // CMP rX, #0 + Bcc label; X < 8; 32-bit Bcc; up to +-1MiB offset.
416
417 // Load integer literal variants.
418 // LDR rX, label; X < 8; 16-bit variant up to 1KiB offset; 2 bytes.
419 kLiteral1KiB,
420 // LDR rX, label; 32-bit variant up to 4KiB offset; 4 bytes.
421 kLiteral4KiB,
422 // MOV rX, imm16 + ADD rX, pc + LDR rX, [rX]; X < 8; up to 64KiB offset; 8 bytes.
423 kLiteral64KiB,
424 // MOV rX, modimm + ADD rX, pc + LDR rX, [rX, #imm12]; up to 1MiB offset; 10 bytes.
425 kLiteral1MiB,
426 // NOTE: We don't provide the 12-byte version of kLiteralFar below where the LDR is 16-bit.
427 // MOV rX, imm16 + MOVT rX, imm16 + ADD rX, pc + LDR rX, [rX]; any offset; 14 bytes.
428 kLiteralFar,
429
430 // Load long or FP literal variants.
431 // VLDR s/dX, label; 32-bit insn, up to 1KiB offset; 4 bytes.
432 kLongOrFPLiteral1KiB,
433 // MOV ip, modimm + ADD ip, pc + VLDR s/dX, [IP, #imm8*4]; up to 256KiB offset; 10 bytes.
434 kLongOrFPLiteral256KiB,
435 // MOV ip, imm16 + MOVT ip, imm16 + ADD ip, pc + VLDR s/dX, [IP]; any offset; 14 bytes.
436 kLongOrFPLiteralFar,
437 };
438
439 // Unresolved branch possibly with a condition.
440 static Fixup Branch(uint32_t location, Type type, Size size = kBranch16Bit,
441 Condition cond = AL) {
442 DCHECK(type == kConditional || type == kUnconditional ||
443 type == kUnconditionalLink || type == kUnconditionalLinkX);
444 DCHECK(size == kBranch16Bit || size == kBranch32Bit);
445 DCHECK(size == kBranch32Bit || (type == kConditional || type == kUnconditional));
446 return Fixup(kNoRegister, kNoRegister, kNoSRegister, kNoDRegister,
447 cond, type, size, location);
448 }
449
450 // Unresolved compare-and-branch instruction with a register and condition (EQ or NE).
451 static Fixup CompareAndBranch(uint32_t location, Register rn, Condition cond) {
452 DCHECK(cond == EQ || cond == NE);
453 return Fixup(rn, kNoRegister, kNoSRegister, kNoDRegister,
454 cond, kCompareAndBranchXZero, kCbxz16Bit, location);
455 }
456
457 // Load narrow literal.
458 static Fixup LoadNarrowLiteral(uint32_t location, Register rt, Size size = kLiteral1KiB) {
459 DCHECK(size == kLiteral1KiB || size == kLiteral4KiB || size == kLiteral64KiB ||
460 size == kLiteral1MiB || size == kLiteralFar);
461 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
462 return Fixup(rt, kNoRegister, kNoSRegister, kNoDRegister,
463 AL, kLoadLiteralNarrow, size, location);
464 }
465
466 // Load wide literal.
467 static Fixup LoadWideLiteral(uint32_t location, Register rt, Register rt2,
468 Size size = kLongOrFPLiteral1KiB) {
469 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
470 size == kLongOrFPLiteralFar);
471 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
472 return Fixup(rt, rt2, kNoSRegister, kNoDRegister,
473 AL, kLoadLiteralWide, size, location);
474 }
475
476 // Load FP single literal.
477 static Fixup LoadSingleLiteral(uint32_t location, SRegister sd,
478 Size size = kLongOrFPLiteral1KiB) {
479 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
480 size == kLongOrFPLiteralFar);
481 return Fixup(kNoRegister, kNoRegister, sd, kNoDRegister,
482 AL, kLoadFPLiteralSingle, size, location);
483 }
484
485 // Load FP double literal.
486 static Fixup LoadDoubleLiteral(uint32_t location, DRegister dd,
487 Size size = kLongOrFPLiteral1KiB) {
488 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
489 size == kLongOrFPLiteralFar);
490 return Fixup(kNoRegister, kNoRegister, kNoSRegister, dd,
491 AL, kLoadFPLiteralDouble, size, location);
492 }
493
494 Type GetType() const {
495 return type_;
496 }
497
Vladimir Marko663c9342015-07-22 11:28:14 +0100498 bool IsLoadLiteral() const {
499 return GetType() >= kLoadLiteralNarrow;
500 }
501
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000502 Size GetOriginalSize() const {
503 return original_size_;
504 }
505
506 Size GetSize() const {
507 return size_;
508 }
509
510 uint32_t GetOriginalSizeInBytes() const;
511
512 uint32_t GetSizeInBytes() const;
513
514 uint32_t GetLocation() const {
515 return location_;
516 }
517
518 uint32_t GetAdjustment() const {
519 return adjustment_;
520 }
521
Vladimir Marko6b756b52015-07-14 11:58:38 +0100522 // Prepare the assembler->fixup_dependents_ and each Fixup's dependents_start_/count_.
523 static void PrepareDependents(Thumb2Assembler* assembler);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000524
Vladimir Marko6b756b52015-07-14 11:58:38 +0100525 ArrayRef<FixupId> Dependents(const Thumb2Assembler& assembler) const {
526 return ArrayRef<FixupId>(assembler.fixup_dependents_.get() + dependents_start_,
527 dependents_count_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000528 }
529
530 // Resolve a branch when the target is known.
531 void Resolve(uint32_t target) {
532 DCHECK_EQ(target_, kUnresolved);
533 DCHECK_NE(target, kUnresolved);
534 target_ = target;
535 }
536
537 // Check if the current size is OK for current location_, target_ and adjustment_.
538 // If not, increase the size. Return the size increase, 0 if unchanged.
539 // If the target if after this Fixup, also add the difference to adjustment_,
540 // so that we don't need to consider forward Fixups as their own dependencies.
541 uint32_t AdjustSizeIfNeeded(uint32_t current_code_size);
542
543 // Increase adjustments. This is called for dependents of a Fixup when its size changes.
544 void IncreaseAdjustment(uint32_t increase) {
545 adjustment_ += increase;
546 }
547
548 // Finalize the branch with an adjustment to the location. Both location and target are updated.
549 void Finalize(uint32_t location_adjustment) {
550 DCHECK_NE(target_, kUnresolved);
551 location_ += location_adjustment;
552 target_ += location_adjustment;
553 }
554
555 // Emit the branch instruction into the assembler buffer. This does the
556 // encoding into the thumb instruction.
557 void Emit(AssemblerBuffer* buffer, uint32_t code_size) const;
558
559 private:
560 Fixup(Register rn, Register rt2, SRegister sd, DRegister dd,
561 Condition cond, Type type, Size size, uint32_t location)
562 : rn_(rn),
563 rt2_(rt2),
564 sd_(sd),
565 dd_(dd),
566 cond_(cond),
567 type_(type),
568 original_size_(size), size_(size),
569 location_(location),
570 target_(kUnresolved),
571 adjustment_(0u),
Vladimir Marko6b756b52015-07-14 11:58:38 +0100572 dependents_count_(0u),
573 dependents_start_(0u) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000574 }
575 static size_t SizeInBytes(Size size);
576
577 // The size of padding added before the literal pool.
578 static size_t LiteralPoolPaddingSize(uint32_t current_code_size);
579
580 // Returns the offset from the PC-using insn to the target.
581 int32_t GetOffset(uint32_t current_code_size) const;
582
583 size_t IncreaseSize(Size new_size);
584
585 int32_t LoadWideOrFpEncoding(Register rbase, int32_t offset) const;
586
587 static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved.
588
589 const Register rn_; // Rn for cbnz/cbz, Rt for literal loads.
590 Register rt2_; // For kLoadLiteralWide.
591 SRegister sd_; // For kLoadFPLiteralSingle.
592 DRegister dd_; // For kLoadFPLiteralDouble.
593 const Condition cond_;
594 const Type type_;
595 Size original_size_;
596 Size size_;
597 uint32_t location_; // Offset into assembler buffer in bytes.
598 uint32_t target_; // Offset into assembler buffer in bytes.
599 uint32_t adjustment_; // The number of extra bytes inserted between location_ and target_.
Vladimir Marko6b756b52015-07-14 11:58:38 +0100600 // Fixups that require adjustment when current size changes are stored in a single
601 // array in the assembler and we store only the start index and count here.
602 uint32_t dependents_count_;
603 uint32_t dependents_start_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000604 };
605
Dave Allison65fcc2c2014-04-28 13:45:27 -0700606 // Emit a single 32 or 16 bit data processing instruction.
607 void EmitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700608 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100609 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700610 Register rn,
611 Register rd,
612 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700613
614 // Must the instruction be 32 bits or can it possibly be encoded
615 // in 16 bits?
616 bool Is32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700617 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100618 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700619 Register rn,
620 Register rd,
621 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700622
623 // Emit a 32 bit data processing instruction.
624 void Emit32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700625 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100626 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700627 Register rn,
628 Register rd,
629 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700630
631 // Emit a 16 bit data processing instruction.
632 void Emit16BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700633 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100634 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700635 Register rn,
636 Register rd,
637 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700638
639 void Emit16BitAddSub(Condition cond,
640 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100641 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700642 Register rn,
643 Register rd,
644 const ShifterOperand& so);
645
646 uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n);
647
648 void EmitLoadStore(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700649 bool load,
650 bool byte,
651 bool half,
652 bool is_signed,
653 Register rd,
654 const Address& ad);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700655
656 void EmitMemOpAddressMode3(Condition cond,
657 int32_t mode,
658 Register rd,
659 const Address& ad);
660
661 void EmitMultiMemOp(Condition cond,
662 BlockAddressMode am,
663 bool load,
664 Register base,
665 RegList regs);
666
667 void EmitMulOp(Condition cond,
668 int32_t opcode,
669 Register rd,
670 Register rn,
671 Register rm,
672 Register rs);
673
674 void EmitVFPsss(Condition cond,
675 int32_t opcode,
676 SRegister sd,
677 SRegister sn,
678 SRegister sm);
679
680 void EmitVFPddd(Condition cond,
681 int32_t opcode,
682 DRegister dd,
683 DRegister dn,
684 DRegister dm);
685
686 void EmitVFPsd(Condition cond,
687 int32_t opcode,
688 SRegister sd,
689 DRegister dm);
690
691 void EmitVFPds(Condition cond,
692 int32_t opcode,
693 DRegister dd,
694 SRegister sm);
695
696 void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond);
697
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000698 void EmitBranch(Condition cond, Label* label, bool link, bool x);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700699 static int32_t EncodeBranchOffset(int32_t offset, int32_t inst);
700 static int DecodeBranchOffset(int32_t inst);
701 int32_t EncodeTstOffset(int offset, int32_t inst);
702 int DecodeTstOffset(int32_t inst);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100703 void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount,
704 Condition cond = AL, SetCc set_cc = kCcDontCare);
705 void EmitShift(Register rd, Register rn, Shift shift, Register rm,
706 Condition cond = AL, SetCc set_cc = kCcDontCare);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700707
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100708 // Whether the assembler can relocate branches. If false, unresolved branches will be
709 // emitted on 32bits.
710 bool can_relocate_branches_;
711
712 // Force the assembler to use 32 bit thumb2 instructions.
713 bool force_32bit_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700714
715 // IfThen conditions. Used to check that conditional instructions match the preceding IT.
716 Condition it_conditions_[4];
717 uint8_t it_cond_index_;
718 Condition next_condition_;
719
720 void SetItCondition(ItState s, Condition cond, uint8_t index);
721
722 void CheckCondition(Condition cond) {
723 CHECK_EQ(cond, next_condition_);
724
725 // Move to the next condition if there is one.
726 if (it_cond_index_ < 3) {
727 ++it_cond_index_;
728 next_condition_ = it_conditions_[it_cond_index_];
729 } else {
730 next_condition_ = AL;
731 }
732 }
733
734 void CheckConditionLastIt(Condition cond) {
735 if (it_cond_index_ < 3) {
736 // Check that the next condition is AL. This means that the
737 // current condition is the last in the IT block.
738 CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL);
739 }
740 CheckCondition(cond);
741 }
742
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000743 FixupId AddFixup(Fixup fixup) {
744 FixupId fixup_id = static_cast<FixupId>(fixups_.size());
745 fixups_.push_back(fixup);
746 // For iterating using FixupId, we need the next id to be representable.
747 DCHECK_EQ(static_cast<size_t>(static_cast<FixupId>(fixups_.size())), fixups_.size());
748 return fixup_id;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700749 }
750
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000751 Fixup* GetFixup(FixupId fixup_id) {
752 DCHECK_LT(fixup_id, fixups_.size());
753 return &fixups_[fixup_id];
Dave Allison65fcc2c2014-04-28 13:45:27 -0700754 }
755
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000756 void BindLabel(Label* label, uint32_t bound_pc);
757 void BindLiterals();
758 void AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size,
759 std::deque<FixupId>* fixups_to_recalculate);
760 uint32_t AdjustFixups();
761 void EmitFixups(uint32_t adjusted_code_size);
762 void EmitLiterals();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700763
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000764 static int16_t BEncoding16(int32_t offset, Condition cond);
765 static int32_t BEncoding32(int32_t offset, Condition cond);
766 static int16_t CbxzEncoding16(Register rn, int32_t offset, Condition cond);
767 static int16_t CmpRnImm8Encoding16(Register rn, int32_t value);
768 static int16_t AddRdnRmEncoding16(Register rdn, Register rm);
769 static int32_t MovwEncoding32(Register rd, int32_t value);
770 static int32_t MovtEncoding32(Register rd, int32_t value);
771 static int32_t MovModImmEncoding32(Register rd, int32_t value);
772 static int16_t LdrLitEncoding16(Register rt, int32_t offset);
773 static int32_t LdrLitEncoding32(Register rt, int32_t offset);
774 static int32_t LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset);
775 static int32_t VldrsEncoding32(SRegister sd, Register rn, int32_t offset);
776 static int32_t VldrdEncoding32(DRegister dd, Register rn, int32_t offset);
777 static int16_t LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset);
778 static int32_t LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700779
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000780 std::vector<Fixup> fixups_;
Vladimir Marko6b756b52015-07-14 11:58:38 +0100781 std::unique_ptr<FixupId[]> fixup_dependents_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000782
783 // Use std::deque<> for literal labels to allow insertions at the end
784 // without invalidating pointers and references to existing elements.
785 std::deque<Literal> literals_;
786
787 // Data for AdjustedPosition(), see the description there.
788 uint32_t last_position_adjustment_;
789 uint32_t last_old_position_;
790 FixupId last_fixup_id_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700791};
792
793} // namespace arm
794} // namespace art
795
796#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_