blob: 055b1379add22359aa7f0c1b7e9d4003344b2019 [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
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100343 bool ShifterOperandCanAlwaysHold(uint32_t immediate) OVERRIDE;
344
Dave Allison65fcc2c2014-04-28 13:45:27 -0700345
Ian Rogers13735952014-10-08 12:43:28 -0700346 static bool IsInstructionForExceptionHandling(uintptr_t pc);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700347
348 // Emit data (e.g. encoded instruction or immediate) to the.
349 // instruction stream.
350 void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format.
351 void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format.
352 void Bind(Label* label) OVERRIDE;
353
354 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
355
356 // Force the assembler to generate 32 bit instructions.
357 void Force32Bit() {
358 force_32bit_ = true;
359 }
360
361 private:
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000362 typedef uint16_t FixupId;
363
364 // Fixup: branches and literal pool references.
365 //
366 // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This
367 // depends on both the type of branch and the offset to which it is branching. The 16-bit
368 // cbz and cbnz instructions may also need to be replaced with a separate 16-bit compare
369 // instruction and a 16- or 32-bit branch instruction. Load from a literal pool can also be
370 // 16-bit or 32-bit instruction and, if the method is large, we may need to use a sequence
371 // of instructions to make up for the limited range of load literal instructions (up to
372 // 4KiB for the 32-bit variant). When generating code for these insns we don't know the
373 // size before hand, so we assume it is the smallest available size and determine the final
374 // code offsets and sizes and emit code in FinalizeCode().
375 //
376 // To handle this, we keep a record of every branch and literal pool load in the program.
377 // The actual instruction encoding for these is delayed until we know the final size of
378 // every instruction. When we bind a label to a branch we don't know the final location yet
379 // as some preceding instructions may need to be expanded, so we record a non-final offset.
380 // In FinalizeCode(), we expand the sizes of branches and literal loads that are out of
381 // range. With each expansion, we need to update dependent Fixups, i.e. insntructios with
382 // target on the other side of the expanded insn, as their offsets change and this may
383 // trigger further expansion.
384 //
385 // All Fixups have a 'fixup id' which is a 16 bit unsigned number used to identify the
386 // Fixup. For each unresolved label we keep a singly-linked list of all Fixups pointing
387 // to it, using the fixup ids as links. The first link is stored in the label's position
388 // (the label is linked but not bound), the following links are stored in the code buffer,
389 // in the placeholder where we will eventually emit the actual code.
390
391 class Fixup {
392 public:
393 // Branch type.
394 enum Type : uint8_t {
395 kConditional, // B<cond>.
396 kUnconditional, // B.
397 kUnconditionalLink, // BL.
398 kUnconditionalLinkX, // BLX.
399 kCompareAndBranchXZero, // cbz/cbnz.
400 kLoadLiteralNarrow, // Load narrrow integer literal.
401 kLoadLiteralWide, // Load wide integer literal.
402 kLoadFPLiteralSingle, // Load FP literal single.
403 kLoadFPLiteralDouble, // Load FP literal double.
404 };
405
406 // Calculated size of branch instruction based on type and offset.
407 enum Size : uint8_t {
408 // Branch variants.
409 kBranch16Bit,
410 kBranch32Bit,
411 // NOTE: We don't support branches which would require multiple instructions, i.e.
412 // conditinoal branches beyond +-1MiB and unconditional branches beyond +-16MiB.
413
414 // CBZ/CBNZ variants.
415 kCbxz16Bit, // CBZ/CBNZ rX, label; X < 8; 7-bit positive offset.
416 kCbxz32Bit, // CMP rX, #0 + Bcc label; X < 8; 16-bit Bcc; +-8-bit offset.
417 kCbxz48Bit, // CMP rX, #0 + Bcc label; X < 8; 32-bit Bcc; up to +-1MiB offset.
418
419 // Load integer literal variants.
420 // LDR rX, label; X < 8; 16-bit variant up to 1KiB offset; 2 bytes.
421 kLiteral1KiB,
422 // LDR rX, label; 32-bit variant up to 4KiB offset; 4 bytes.
423 kLiteral4KiB,
424 // MOV rX, imm16 + ADD rX, pc + LDR rX, [rX]; X < 8; up to 64KiB offset; 8 bytes.
425 kLiteral64KiB,
426 // MOV rX, modimm + ADD rX, pc + LDR rX, [rX, #imm12]; up to 1MiB offset; 10 bytes.
427 kLiteral1MiB,
428 // NOTE: We don't provide the 12-byte version of kLiteralFar below where the LDR is 16-bit.
429 // MOV rX, imm16 + MOVT rX, imm16 + ADD rX, pc + LDR rX, [rX]; any offset; 14 bytes.
430 kLiteralFar,
431
432 // Load long or FP literal variants.
433 // VLDR s/dX, label; 32-bit insn, up to 1KiB offset; 4 bytes.
434 kLongOrFPLiteral1KiB,
435 // MOV ip, modimm + ADD ip, pc + VLDR s/dX, [IP, #imm8*4]; up to 256KiB offset; 10 bytes.
436 kLongOrFPLiteral256KiB,
437 // MOV ip, imm16 + MOVT ip, imm16 + ADD ip, pc + VLDR s/dX, [IP]; any offset; 14 bytes.
438 kLongOrFPLiteralFar,
439 };
440
441 // Unresolved branch possibly with a condition.
442 static Fixup Branch(uint32_t location, Type type, Size size = kBranch16Bit,
443 Condition cond = AL) {
444 DCHECK(type == kConditional || type == kUnconditional ||
445 type == kUnconditionalLink || type == kUnconditionalLinkX);
446 DCHECK(size == kBranch16Bit || size == kBranch32Bit);
447 DCHECK(size == kBranch32Bit || (type == kConditional || type == kUnconditional));
448 return Fixup(kNoRegister, kNoRegister, kNoSRegister, kNoDRegister,
449 cond, type, size, location);
450 }
451
452 // Unresolved compare-and-branch instruction with a register and condition (EQ or NE).
453 static Fixup CompareAndBranch(uint32_t location, Register rn, Condition cond) {
454 DCHECK(cond == EQ || cond == NE);
455 return Fixup(rn, kNoRegister, kNoSRegister, kNoDRegister,
456 cond, kCompareAndBranchXZero, kCbxz16Bit, location);
457 }
458
459 // Load narrow literal.
460 static Fixup LoadNarrowLiteral(uint32_t location, Register rt, Size size = kLiteral1KiB) {
461 DCHECK(size == kLiteral1KiB || size == kLiteral4KiB || size == kLiteral64KiB ||
462 size == kLiteral1MiB || size == kLiteralFar);
463 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
464 return Fixup(rt, kNoRegister, kNoSRegister, kNoDRegister,
465 AL, kLoadLiteralNarrow, size, location);
466 }
467
468 // Load wide literal.
469 static Fixup LoadWideLiteral(uint32_t location, Register rt, Register rt2,
470 Size size = kLongOrFPLiteral1KiB) {
471 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
472 size == kLongOrFPLiteralFar);
473 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
474 return Fixup(rt, rt2, kNoSRegister, kNoDRegister,
475 AL, kLoadLiteralWide, size, location);
476 }
477
478 // Load FP single literal.
479 static Fixup LoadSingleLiteral(uint32_t location, SRegister sd,
480 Size size = kLongOrFPLiteral1KiB) {
481 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
482 size == kLongOrFPLiteralFar);
483 return Fixup(kNoRegister, kNoRegister, sd, kNoDRegister,
484 AL, kLoadFPLiteralSingle, size, location);
485 }
486
487 // Load FP double literal.
488 static Fixup LoadDoubleLiteral(uint32_t location, DRegister dd,
489 Size size = kLongOrFPLiteral1KiB) {
490 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
491 size == kLongOrFPLiteralFar);
492 return Fixup(kNoRegister, kNoRegister, kNoSRegister, dd,
493 AL, kLoadFPLiteralDouble, size, location);
494 }
495
496 Type GetType() const {
497 return type_;
498 }
499
Vladimir Marko663c9342015-07-22 11:28:14 +0100500 bool IsLoadLiteral() const {
501 return GetType() >= kLoadLiteralNarrow;
502 }
503
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000504 Size GetOriginalSize() const {
505 return original_size_;
506 }
507
508 Size GetSize() const {
509 return size_;
510 }
511
512 uint32_t GetOriginalSizeInBytes() const;
513
514 uint32_t GetSizeInBytes() const;
515
516 uint32_t GetLocation() const {
517 return location_;
518 }
519
520 uint32_t GetAdjustment() const {
521 return adjustment_;
522 }
523
Vladimir Marko6b756b52015-07-14 11:58:38 +0100524 // Prepare the assembler->fixup_dependents_ and each Fixup's dependents_start_/count_.
525 static void PrepareDependents(Thumb2Assembler* assembler);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000526
Vladimir Marko6b756b52015-07-14 11:58:38 +0100527 ArrayRef<FixupId> Dependents(const Thumb2Assembler& assembler) const {
528 return ArrayRef<FixupId>(assembler.fixup_dependents_.get() + dependents_start_,
529 dependents_count_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000530 }
531
532 // Resolve a branch when the target is known.
533 void Resolve(uint32_t target) {
534 DCHECK_EQ(target_, kUnresolved);
535 DCHECK_NE(target, kUnresolved);
536 target_ = target;
537 }
538
539 // Check if the current size is OK for current location_, target_ and adjustment_.
540 // If not, increase the size. Return the size increase, 0 if unchanged.
541 // If the target if after this Fixup, also add the difference to adjustment_,
542 // so that we don't need to consider forward Fixups as their own dependencies.
543 uint32_t AdjustSizeIfNeeded(uint32_t current_code_size);
544
545 // Increase adjustments. This is called for dependents of a Fixup when its size changes.
546 void IncreaseAdjustment(uint32_t increase) {
547 adjustment_ += increase;
548 }
549
550 // Finalize the branch with an adjustment to the location. Both location and target are updated.
551 void Finalize(uint32_t location_adjustment) {
552 DCHECK_NE(target_, kUnresolved);
553 location_ += location_adjustment;
554 target_ += location_adjustment;
555 }
556
557 // Emit the branch instruction into the assembler buffer. This does the
558 // encoding into the thumb instruction.
559 void Emit(AssemblerBuffer* buffer, uint32_t code_size) const;
560
561 private:
562 Fixup(Register rn, Register rt2, SRegister sd, DRegister dd,
563 Condition cond, Type type, Size size, uint32_t location)
564 : rn_(rn),
565 rt2_(rt2),
566 sd_(sd),
567 dd_(dd),
568 cond_(cond),
569 type_(type),
570 original_size_(size), size_(size),
571 location_(location),
572 target_(kUnresolved),
573 adjustment_(0u),
Vladimir Marko6b756b52015-07-14 11:58:38 +0100574 dependents_count_(0u),
575 dependents_start_(0u) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000576 }
577 static size_t SizeInBytes(Size size);
578
579 // The size of padding added before the literal pool.
580 static size_t LiteralPoolPaddingSize(uint32_t current_code_size);
581
582 // Returns the offset from the PC-using insn to the target.
583 int32_t GetOffset(uint32_t current_code_size) const;
584
585 size_t IncreaseSize(Size new_size);
586
587 int32_t LoadWideOrFpEncoding(Register rbase, int32_t offset) const;
588
589 static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved.
590
591 const Register rn_; // Rn for cbnz/cbz, Rt for literal loads.
592 Register rt2_; // For kLoadLiteralWide.
593 SRegister sd_; // For kLoadFPLiteralSingle.
594 DRegister dd_; // For kLoadFPLiteralDouble.
595 const Condition cond_;
596 const Type type_;
597 Size original_size_;
598 Size size_;
599 uint32_t location_; // Offset into assembler buffer in bytes.
600 uint32_t target_; // Offset into assembler buffer in bytes.
601 uint32_t adjustment_; // The number of extra bytes inserted between location_ and target_.
Vladimir Marko6b756b52015-07-14 11:58:38 +0100602 // Fixups that require adjustment when current size changes are stored in a single
603 // array in the assembler and we store only the start index and count here.
604 uint32_t dependents_count_;
605 uint32_t dependents_start_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000606 };
607
Dave Allison65fcc2c2014-04-28 13:45:27 -0700608 // Emit a single 32 or 16 bit data processing instruction.
609 void EmitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700610 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100611 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700612 Register rn,
613 Register rd,
614 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700615
616 // Must the instruction be 32 bits or can it possibly be encoded
617 // in 16 bits?
618 bool Is32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700619 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100620 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700621 Register rn,
622 Register rd,
623 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700624
625 // Emit a 32 bit data processing instruction.
626 void Emit32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700627 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100628 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700629 Register rn,
630 Register rd,
631 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700632
633 // Emit a 16 bit data processing instruction.
634 void Emit16BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700635 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100636 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700637 Register rn,
638 Register rd,
639 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700640
641 void Emit16BitAddSub(Condition cond,
642 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100643 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700644 Register rn,
645 Register rd,
646 const ShifterOperand& so);
647
648 uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n);
649
650 void EmitLoadStore(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700651 bool load,
652 bool byte,
653 bool half,
654 bool is_signed,
655 Register rd,
656 const Address& ad);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700657
658 void EmitMemOpAddressMode3(Condition cond,
659 int32_t mode,
660 Register rd,
661 const Address& ad);
662
663 void EmitMultiMemOp(Condition cond,
664 BlockAddressMode am,
665 bool load,
666 Register base,
667 RegList regs);
668
669 void EmitMulOp(Condition cond,
670 int32_t opcode,
671 Register rd,
672 Register rn,
673 Register rm,
674 Register rs);
675
676 void EmitVFPsss(Condition cond,
677 int32_t opcode,
678 SRegister sd,
679 SRegister sn,
680 SRegister sm);
681
682 void EmitVFPddd(Condition cond,
683 int32_t opcode,
684 DRegister dd,
685 DRegister dn,
686 DRegister dm);
687
688 void EmitVFPsd(Condition cond,
689 int32_t opcode,
690 SRegister sd,
691 DRegister dm);
692
693 void EmitVFPds(Condition cond,
694 int32_t opcode,
695 DRegister dd,
696 SRegister sm);
697
698 void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond);
699
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000700 void EmitBranch(Condition cond, Label* label, bool link, bool x);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700701 static int32_t EncodeBranchOffset(int32_t offset, int32_t inst);
702 static int DecodeBranchOffset(int32_t inst);
703 int32_t EncodeTstOffset(int offset, int32_t inst);
704 int DecodeTstOffset(int32_t inst);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100705 void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount,
706 Condition cond = AL, SetCc set_cc = kCcDontCare);
707 void EmitShift(Register rd, Register rn, Shift shift, Register rm,
708 Condition cond = AL, SetCc set_cc = kCcDontCare);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700709
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100710 // Whether the assembler can relocate branches. If false, unresolved branches will be
711 // emitted on 32bits.
712 bool can_relocate_branches_;
713
714 // Force the assembler to use 32 bit thumb2 instructions.
715 bool force_32bit_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700716
717 // IfThen conditions. Used to check that conditional instructions match the preceding IT.
718 Condition it_conditions_[4];
719 uint8_t it_cond_index_;
720 Condition next_condition_;
721
722 void SetItCondition(ItState s, Condition cond, uint8_t index);
723
724 void CheckCondition(Condition cond) {
725 CHECK_EQ(cond, next_condition_);
726
727 // Move to the next condition if there is one.
728 if (it_cond_index_ < 3) {
729 ++it_cond_index_;
730 next_condition_ = it_conditions_[it_cond_index_];
731 } else {
732 next_condition_ = AL;
733 }
734 }
735
736 void CheckConditionLastIt(Condition cond) {
737 if (it_cond_index_ < 3) {
738 // Check that the next condition is AL. This means that the
739 // current condition is the last in the IT block.
740 CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL);
741 }
742 CheckCondition(cond);
743 }
744
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000745 FixupId AddFixup(Fixup fixup) {
746 FixupId fixup_id = static_cast<FixupId>(fixups_.size());
747 fixups_.push_back(fixup);
748 // For iterating using FixupId, we need the next id to be representable.
749 DCHECK_EQ(static_cast<size_t>(static_cast<FixupId>(fixups_.size())), fixups_.size());
750 return fixup_id;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700751 }
752
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000753 Fixup* GetFixup(FixupId fixup_id) {
754 DCHECK_LT(fixup_id, fixups_.size());
755 return &fixups_[fixup_id];
Dave Allison65fcc2c2014-04-28 13:45:27 -0700756 }
757
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000758 void BindLabel(Label* label, uint32_t bound_pc);
759 void BindLiterals();
760 void AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size,
761 std::deque<FixupId>* fixups_to_recalculate);
762 uint32_t AdjustFixups();
763 void EmitFixups(uint32_t adjusted_code_size);
764 void EmitLiterals();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700765
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000766 static int16_t BEncoding16(int32_t offset, Condition cond);
767 static int32_t BEncoding32(int32_t offset, Condition cond);
768 static int16_t CbxzEncoding16(Register rn, int32_t offset, Condition cond);
769 static int16_t CmpRnImm8Encoding16(Register rn, int32_t value);
770 static int16_t AddRdnRmEncoding16(Register rdn, Register rm);
771 static int32_t MovwEncoding32(Register rd, int32_t value);
772 static int32_t MovtEncoding32(Register rd, int32_t value);
773 static int32_t MovModImmEncoding32(Register rd, int32_t value);
774 static int16_t LdrLitEncoding16(Register rt, int32_t offset);
775 static int32_t LdrLitEncoding32(Register rt, int32_t offset);
776 static int32_t LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset);
777 static int32_t VldrsEncoding32(SRegister sd, Register rn, int32_t offset);
778 static int32_t VldrdEncoding32(DRegister dd, Register rn, int32_t offset);
779 static int16_t LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset);
780 static int32_t LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700781
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000782 std::vector<Fixup> fixups_;
Vladimir Marko6b756b52015-07-14 11:58:38 +0100783 std::unique_ptr<FixupId[]> fixup_dependents_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000784
785 // Use std::deque<> for literal labels to allow insertions at the end
786 // without invalidating pointers and references to existing elements.
787 std::deque<Literal> literals_;
788
789 // Data for AdjustedPosition(), see the description there.
790 uint32_t last_position_adjustment_;
791 uint32_t last_old_position_;
792 FixupId last_fixup_id_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700793};
794
795} // namespace arm
796} // namespace art
797
798#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_