blob: a1a8927f44e9ed83c01d1963cbc310e8eae9811c [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 Marko73cf0fb2015-07-30 15:07:22 +0100101 virtual void mov(Register rd, const ShifterOperand& so,
102 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700103
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100104 virtual void bic(Register rd, Register rn, 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 mvn(Register rd, const ShifterOperand& so,
108 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700109
110 // Miscellaneous data-processing instructions.
111 void clz(Register rd, Register rm, Condition cond = AL) OVERRIDE;
112 void movw(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
113 void movt(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100114 void rbit(Register rd, Register rm, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700115
116 // Multiply instructions.
117 void mul(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
118 void mla(Register rd, Register rn, Register rm, Register ra,
119 Condition cond = AL) OVERRIDE;
120 void mls(Register rd, Register rn, Register rm, Register ra,
121 Condition cond = AL) OVERRIDE;
Zheng Xuc6667102015-05-15 16:08:45 +0800122 void smull(Register rd_lo, Register rd_hi, Register rn, Register rm,
123 Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700124 void umull(Register rd_lo, Register rd_hi, Register rn, Register rm,
125 Condition cond = AL) OVERRIDE;
126
127 void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
128 void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
129
Roland Levillain981e4542014-11-14 11:47:14 +0000130 // Bit field extract instructions.
Roland Levillain51d3fc42014-11-13 14:11:42 +0000131 void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain981e4542014-11-14 11:47:14 +0000132 void ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain51d3fc42014-11-13 14:11:42 +0000133
Dave Allison65fcc2c2014-04-28 13:45:27 -0700134 // Load/store instructions.
135 void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
136 void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
137
138 void ldrb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
139 void strb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
140
141 void ldrh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
142 void strh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
143
144 void ldrsb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
145 void ldrsh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
146
Roland Levillain4af147e2015-04-07 13:54:49 +0100147 // Load/store register dual instructions using registers `rd` and `rd` + 1.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700148 void ldrd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
149 void strd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
150
Roland Levillain4af147e2015-04-07 13:54:49 +0100151 // Load/store register dual instructions using registers `rd` and `rd2`.
152 // Note that contrary to the ARM A1 encoding, the Thumb-2 T1 encoding
153 // does not require `rd` to be even, nor `rd2' to be equal to `rd` + 1.
154 void ldrd(Register rd, Register rd2, const Address& ad, Condition cond);
155 void strd(Register rd, Register rd2, const Address& ad, Condition cond);
156
157
Dave Allison65fcc2c2014-04-28 13:45:27 -0700158 void ldm(BlockAddressMode am, Register base,
159 RegList regs, Condition cond = AL) OVERRIDE;
160 void stm(BlockAddressMode am, Register base,
161 RegList regs, Condition cond = AL) OVERRIDE;
162
163 void ldrex(Register rd, Register rn, Condition cond = AL) OVERRIDE;
164 void strex(Register rd, Register rt, Register rn, Condition cond = AL) OVERRIDE;
165
166 void ldrex(Register rd, Register rn, uint16_t imm, Condition cond = AL);
167 void strex(Register rd, Register rt, Register rn, uint16_t imm, Condition cond = AL);
168
Calin Juravle52c48962014-12-16 17:02:57 +0000169 void ldrexd(Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
170 void strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700171
172 // Miscellaneous instructions.
173 void clrex(Condition cond = AL) OVERRIDE;
174 void nop(Condition cond = AL) OVERRIDE;
175
176 void bkpt(uint16_t imm16) OVERRIDE;
177 void svc(uint32_t imm24) OVERRIDE;
178
179 // If-then
180 void it(Condition firstcond, ItState i1 = kItOmitted,
181 ItState i2 = kItOmitted, ItState i3 = kItOmitted) OVERRIDE;
182
183 void cbz(Register rn, Label* target) OVERRIDE;
184 void cbnz(Register rn, Label* target) OVERRIDE;
185
186 // Floating point instructions (VFPv3-D16 and VFPv3-D32 profiles).
187 void vmovsr(SRegister sn, Register rt, Condition cond = AL) OVERRIDE;
188 void vmovrs(Register rt, SRegister sn, Condition cond = AL) OVERRIDE;
189 void vmovsrr(SRegister sm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
190 void vmovrrs(Register rt, Register rt2, SRegister sm, Condition cond = AL) OVERRIDE;
191 void vmovdrr(DRegister dm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
192 void vmovrrd(Register rt, Register rt2, DRegister dm, Condition cond = AL) OVERRIDE;
193 void vmovs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
194 void vmovd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
195
196 // Returns false if the immediate cannot be encoded.
197 bool vmovs(SRegister sd, float s_imm, Condition cond = AL) OVERRIDE;
198 bool vmovd(DRegister dd, double d_imm, Condition cond = AL) OVERRIDE;
199
200 void vldrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
201 void vstrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
202 void vldrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
203 void vstrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
204
205 void vadds(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
206 void vaddd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
207 void vsubs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
208 void vsubd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
209 void vmuls(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
210 void vmuld(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
211 void vmlas(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
212 void vmlad(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
213 void vmlss(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
214 void vmlsd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
215 void vdivs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
216 void vdivd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
217
218 void vabss(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
219 void vabsd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
220 void vnegs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
221 void vnegd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
222 void vsqrts(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
223 void vsqrtd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
224
225 void vcvtsd(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
226 void vcvtds(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
227 void vcvtis(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
228 void vcvtid(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
229 void vcvtsi(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
230 void vcvtdi(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
231 void vcvtus(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
232 void vcvtud(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
233 void vcvtsu(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
234 void vcvtdu(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
235
236 void vcmps(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
237 void vcmpd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
238 void vcmpsz(SRegister sd, Condition cond = AL) OVERRIDE;
239 void vcmpdz(DRegister dd, Condition cond = AL) OVERRIDE;
240 void vmstat(Condition cond = AL) OVERRIDE; // VMRS APSR_nzcv, FPSCR
241
242 void vpushs(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
243 void vpushd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
244 void vpops(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
245 void vpopd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
246
247 // Branch instructions.
248 void b(Label* label, Condition cond = AL);
249 void bl(Label* label, Condition cond = AL);
250 void blx(Label* label);
251 void blx(Register rm, Condition cond = AL) OVERRIDE;
252 void bx(Register rm, Condition cond = AL) OVERRIDE;
253
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100254 virtual void Lsl(Register rd, Register rm, uint32_t shift_imm,
255 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
256 virtual void Lsr(Register rd, Register rm, uint32_t shift_imm,
257 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
258 virtual void Asr(Register rd, Register rm, uint32_t shift_imm,
259 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
260 virtual void Ror(Register rd, Register rm, uint32_t shift_imm,
261 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
262 virtual void Rrx(Register rd, Register rm,
263 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison45fdb932014-06-25 12:37:10 -0700264
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100265 virtual void Lsl(Register rd, Register rm, Register rn,
266 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
267 virtual void Lsr(Register rd, Register rm, Register rn,
268 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
269 virtual void Asr(Register rd, Register rm, Register rn,
270 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
271 virtual void Ror(Register rd, Register rm, Register rn,
272 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700273
274 void Push(Register rd, Condition cond = AL) OVERRIDE;
275 void Pop(Register rd, Condition cond = AL) OVERRIDE;
276
277 void PushList(RegList regs, Condition cond = AL) OVERRIDE;
278 void PopList(RegList regs, Condition cond = AL) OVERRIDE;
279
280 void Mov(Register rd, Register rm, Condition cond = AL) OVERRIDE;
281
282 void CompareAndBranchIfZero(Register r, Label* label) OVERRIDE;
283 void CompareAndBranchIfNonZero(Register r, Label* label) OVERRIDE;
284
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100285 // Memory barriers.
286 void dmb(DmbOptions flavor) OVERRIDE;
287
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000288 // Get the final position of a label after local fixup based on the old position
289 // recorded before FinalizeCode().
290 uint32_t GetAdjustedPosition(uint32_t old_position) OVERRIDE;
291
292 using ArmAssembler::NewLiteral; // Make the helper template visible.
293
294 Literal* NewLiteral(size_t size, const uint8_t* data) OVERRIDE;
295 void LoadLiteral(Register rt, Literal* literal) OVERRIDE;
296 void LoadLiteral(Register rt, Register rt2, Literal* literal) OVERRIDE;
297 void LoadLiteral(SRegister sd, Literal* literal) OVERRIDE;
298 void LoadLiteral(DRegister dd, Literal* literal) OVERRIDE;
299
Dave Allison65fcc2c2014-04-28 13:45:27 -0700300 // Add signed constant value to rd. May clobber IP.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700301 void AddConstant(Register rd, Register rn, int32_t value,
Vladimir Marko449b1092015-09-08 12:16:45 +0100302 Condition cond = AL, SetCc set_cc = kCcDontCare) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700303
304 // Load and Store. May clobber IP.
305 void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700306 void MarkExceptionHandler(Label* label) OVERRIDE;
307 void LoadFromOffset(LoadOperandType type,
308 Register reg,
309 Register base,
310 int32_t offset,
311 Condition cond = AL) OVERRIDE;
312 void StoreToOffset(StoreOperandType type,
313 Register reg,
314 Register base,
315 int32_t offset,
316 Condition cond = AL) OVERRIDE;
317 void LoadSFromOffset(SRegister reg,
318 Register base,
319 int32_t offset,
320 Condition cond = AL) OVERRIDE;
321 void StoreSToOffset(SRegister reg,
322 Register base,
323 int32_t offset,
324 Condition cond = AL) OVERRIDE;
325 void LoadDFromOffset(DRegister reg,
326 Register base,
327 int32_t offset,
328 Condition cond = AL) OVERRIDE;
329 void StoreDToOffset(DRegister reg,
330 Register base,
331 int32_t offset,
332 Condition cond = AL) OVERRIDE;
333
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000334 bool ShifterOperandCanHold(Register rd,
335 Register rn,
336 Opcode opcode,
337 uint32_t immediate,
338 ShifterOperand* shifter_op) OVERRIDE;
339
Dave Allison65fcc2c2014-04-28 13:45:27 -0700340
Ian Rogers13735952014-10-08 12:43:28 -0700341 static bool IsInstructionForExceptionHandling(uintptr_t pc);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700342
343 // Emit data (e.g. encoded instruction or immediate) to the.
344 // instruction stream.
345 void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format.
346 void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format.
347 void Bind(Label* label) OVERRIDE;
348
349 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
350
351 // Force the assembler to generate 32 bit instructions.
352 void Force32Bit() {
353 force_32bit_ = true;
354 }
355
356 private:
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000357 typedef uint16_t FixupId;
358
359 // Fixup: branches and literal pool references.
360 //
361 // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This
362 // depends on both the type of branch and the offset to which it is branching. The 16-bit
363 // cbz and cbnz instructions may also need to be replaced with a separate 16-bit compare
364 // instruction and a 16- or 32-bit branch instruction. Load from a literal pool can also be
365 // 16-bit or 32-bit instruction and, if the method is large, we may need to use a sequence
366 // of instructions to make up for the limited range of load literal instructions (up to
367 // 4KiB for the 32-bit variant). When generating code for these insns we don't know the
368 // size before hand, so we assume it is the smallest available size and determine the final
369 // code offsets and sizes and emit code in FinalizeCode().
370 //
371 // To handle this, we keep a record of every branch and literal pool load in the program.
372 // The actual instruction encoding for these is delayed until we know the final size of
373 // every instruction. When we bind a label to a branch we don't know the final location yet
374 // as some preceding instructions may need to be expanded, so we record a non-final offset.
375 // In FinalizeCode(), we expand the sizes of branches and literal loads that are out of
376 // range. With each expansion, we need to update dependent Fixups, i.e. insntructios with
377 // target on the other side of the expanded insn, as their offsets change and this may
378 // trigger further expansion.
379 //
380 // All Fixups have a 'fixup id' which is a 16 bit unsigned number used to identify the
381 // Fixup. For each unresolved label we keep a singly-linked list of all Fixups pointing
382 // to it, using the fixup ids as links. The first link is stored in the label's position
383 // (the label is linked but not bound), the following links are stored in the code buffer,
384 // in the placeholder where we will eventually emit the actual code.
385
386 class Fixup {
387 public:
388 // Branch type.
389 enum Type : uint8_t {
390 kConditional, // B<cond>.
391 kUnconditional, // B.
392 kUnconditionalLink, // BL.
393 kUnconditionalLinkX, // BLX.
394 kCompareAndBranchXZero, // cbz/cbnz.
395 kLoadLiteralNarrow, // Load narrrow integer literal.
396 kLoadLiteralWide, // Load wide integer literal.
397 kLoadFPLiteralSingle, // Load FP literal single.
398 kLoadFPLiteralDouble, // Load FP literal double.
399 };
400
401 // Calculated size of branch instruction based on type and offset.
402 enum Size : uint8_t {
403 // Branch variants.
404 kBranch16Bit,
405 kBranch32Bit,
406 // NOTE: We don't support branches which would require multiple instructions, i.e.
407 // conditinoal branches beyond +-1MiB and unconditional branches beyond +-16MiB.
408
409 // CBZ/CBNZ variants.
410 kCbxz16Bit, // CBZ/CBNZ rX, label; X < 8; 7-bit positive offset.
411 kCbxz32Bit, // CMP rX, #0 + Bcc label; X < 8; 16-bit Bcc; +-8-bit offset.
412 kCbxz48Bit, // CMP rX, #0 + Bcc label; X < 8; 32-bit Bcc; up to +-1MiB offset.
413
414 // Load integer literal variants.
415 // LDR rX, label; X < 8; 16-bit variant up to 1KiB offset; 2 bytes.
416 kLiteral1KiB,
417 // LDR rX, label; 32-bit variant up to 4KiB offset; 4 bytes.
418 kLiteral4KiB,
419 // MOV rX, imm16 + ADD rX, pc + LDR rX, [rX]; X < 8; up to 64KiB offset; 8 bytes.
420 kLiteral64KiB,
421 // MOV rX, modimm + ADD rX, pc + LDR rX, [rX, #imm12]; up to 1MiB offset; 10 bytes.
422 kLiteral1MiB,
423 // NOTE: We don't provide the 12-byte version of kLiteralFar below where the LDR is 16-bit.
424 // MOV rX, imm16 + MOVT rX, imm16 + ADD rX, pc + LDR rX, [rX]; any offset; 14 bytes.
425 kLiteralFar,
426
427 // Load long or FP literal variants.
428 // VLDR s/dX, label; 32-bit insn, up to 1KiB offset; 4 bytes.
429 kLongOrFPLiteral1KiB,
430 // MOV ip, modimm + ADD ip, pc + VLDR s/dX, [IP, #imm8*4]; up to 256KiB offset; 10 bytes.
431 kLongOrFPLiteral256KiB,
432 // MOV ip, imm16 + MOVT ip, imm16 + ADD ip, pc + VLDR s/dX, [IP]; any offset; 14 bytes.
433 kLongOrFPLiteralFar,
434 };
435
436 // Unresolved branch possibly with a condition.
437 static Fixup Branch(uint32_t location, Type type, Size size = kBranch16Bit,
438 Condition cond = AL) {
439 DCHECK(type == kConditional || type == kUnconditional ||
440 type == kUnconditionalLink || type == kUnconditionalLinkX);
441 DCHECK(size == kBranch16Bit || size == kBranch32Bit);
442 DCHECK(size == kBranch32Bit || (type == kConditional || type == kUnconditional));
443 return Fixup(kNoRegister, kNoRegister, kNoSRegister, kNoDRegister,
444 cond, type, size, location);
445 }
446
447 // Unresolved compare-and-branch instruction with a register and condition (EQ or NE).
448 static Fixup CompareAndBranch(uint32_t location, Register rn, Condition cond) {
449 DCHECK(cond == EQ || cond == NE);
450 return Fixup(rn, kNoRegister, kNoSRegister, kNoDRegister,
451 cond, kCompareAndBranchXZero, kCbxz16Bit, location);
452 }
453
454 // Load narrow literal.
455 static Fixup LoadNarrowLiteral(uint32_t location, Register rt, Size size = kLiteral1KiB) {
456 DCHECK(size == kLiteral1KiB || size == kLiteral4KiB || size == kLiteral64KiB ||
457 size == kLiteral1MiB || size == kLiteralFar);
458 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
459 return Fixup(rt, kNoRegister, kNoSRegister, kNoDRegister,
460 AL, kLoadLiteralNarrow, size, location);
461 }
462
463 // Load wide literal.
464 static Fixup LoadWideLiteral(uint32_t location, Register rt, Register rt2,
465 Size size = kLongOrFPLiteral1KiB) {
466 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
467 size == kLongOrFPLiteralFar);
468 DCHECK(!IsHighRegister(rt) || (size != kLiteral1KiB && size != kLiteral64KiB));
469 return Fixup(rt, rt2, kNoSRegister, kNoDRegister,
470 AL, kLoadLiteralWide, size, location);
471 }
472
473 // Load FP single literal.
474 static Fixup LoadSingleLiteral(uint32_t location, SRegister sd,
475 Size size = kLongOrFPLiteral1KiB) {
476 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
477 size == kLongOrFPLiteralFar);
478 return Fixup(kNoRegister, kNoRegister, sd, kNoDRegister,
479 AL, kLoadFPLiteralSingle, size, location);
480 }
481
482 // Load FP double literal.
483 static Fixup LoadDoubleLiteral(uint32_t location, DRegister dd,
484 Size size = kLongOrFPLiteral1KiB) {
485 DCHECK(size == kLongOrFPLiteral1KiB || size == kLongOrFPLiteral256KiB ||
486 size == kLongOrFPLiteralFar);
487 return Fixup(kNoRegister, kNoRegister, kNoSRegister, dd,
488 AL, kLoadFPLiteralDouble, size, location);
489 }
490
491 Type GetType() const {
492 return type_;
493 }
494
Vladimir Marko663c9342015-07-22 11:28:14 +0100495 bool IsLoadLiteral() const {
496 return GetType() >= kLoadLiteralNarrow;
497 }
498
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000499 Size GetOriginalSize() const {
500 return original_size_;
501 }
502
503 Size GetSize() const {
504 return size_;
505 }
506
507 uint32_t GetOriginalSizeInBytes() const;
508
509 uint32_t GetSizeInBytes() const;
510
511 uint32_t GetLocation() const {
512 return location_;
513 }
514
515 uint32_t GetAdjustment() const {
516 return adjustment_;
517 }
518
Vladimir Marko6b756b52015-07-14 11:58:38 +0100519 // Prepare the assembler->fixup_dependents_ and each Fixup's dependents_start_/count_.
520 static void PrepareDependents(Thumb2Assembler* assembler);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000521
Vladimir Marko6b756b52015-07-14 11:58:38 +0100522 ArrayRef<FixupId> Dependents(const Thumb2Assembler& assembler) const {
523 return ArrayRef<FixupId>(assembler.fixup_dependents_.get() + dependents_start_,
524 dependents_count_);
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000525 }
526
527 // Resolve a branch when the target is known.
528 void Resolve(uint32_t target) {
529 DCHECK_EQ(target_, kUnresolved);
530 DCHECK_NE(target, kUnresolved);
531 target_ = target;
532 }
533
534 // Check if the current size is OK for current location_, target_ and adjustment_.
535 // If not, increase the size. Return the size increase, 0 if unchanged.
536 // If the target if after this Fixup, also add the difference to adjustment_,
537 // so that we don't need to consider forward Fixups as their own dependencies.
538 uint32_t AdjustSizeIfNeeded(uint32_t current_code_size);
539
540 // Increase adjustments. This is called for dependents of a Fixup when its size changes.
541 void IncreaseAdjustment(uint32_t increase) {
542 adjustment_ += increase;
543 }
544
545 // Finalize the branch with an adjustment to the location. Both location and target are updated.
546 void Finalize(uint32_t location_adjustment) {
547 DCHECK_NE(target_, kUnresolved);
548 location_ += location_adjustment;
549 target_ += location_adjustment;
550 }
551
552 // Emit the branch instruction into the assembler buffer. This does the
553 // encoding into the thumb instruction.
554 void Emit(AssemblerBuffer* buffer, uint32_t code_size) const;
555
556 private:
557 Fixup(Register rn, Register rt2, SRegister sd, DRegister dd,
558 Condition cond, Type type, Size size, uint32_t location)
559 : rn_(rn),
560 rt2_(rt2),
561 sd_(sd),
562 dd_(dd),
563 cond_(cond),
564 type_(type),
565 original_size_(size), size_(size),
566 location_(location),
567 target_(kUnresolved),
568 adjustment_(0u),
Vladimir Marko6b756b52015-07-14 11:58:38 +0100569 dependents_count_(0u),
570 dependents_start_(0u) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000571 }
572 static size_t SizeInBytes(Size size);
573
574 // The size of padding added before the literal pool.
575 static size_t LiteralPoolPaddingSize(uint32_t current_code_size);
576
577 // Returns the offset from the PC-using insn to the target.
578 int32_t GetOffset(uint32_t current_code_size) const;
579
580 size_t IncreaseSize(Size new_size);
581
582 int32_t LoadWideOrFpEncoding(Register rbase, int32_t offset) const;
583
584 static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved.
585
586 const Register rn_; // Rn for cbnz/cbz, Rt for literal loads.
587 Register rt2_; // For kLoadLiteralWide.
588 SRegister sd_; // For kLoadFPLiteralSingle.
589 DRegister dd_; // For kLoadFPLiteralDouble.
590 const Condition cond_;
591 const Type type_;
592 Size original_size_;
593 Size size_;
594 uint32_t location_; // Offset into assembler buffer in bytes.
595 uint32_t target_; // Offset into assembler buffer in bytes.
596 uint32_t adjustment_; // The number of extra bytes inserted between location_ and target_.
Vladimir Marko6b756b52015-07-14 11:58:38 +0100597 // Fixups that require adjustment when current size changes are stored in a single
598 // array in the assembler and we store only the start index and count here.
599 uint32_t dependents_count_;
600 uint32_t dependents_start_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000601 };
602
Dave Allison65fcc2c2014-04-28 13:45:27 -0700603 // Emit a single 32 or 16 bit data processing instruction.
604 void EmitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700605 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100606 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700607 Register rn,
608 Register rd,
609 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700610
611 // Must the instruction be 32 bits or can it possibly be encoded
612 // in 16 bits?
613 bool Is32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700614 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100615 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700616 Register rn,
617 Register rd,
618 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700619
620 // Emit a 32 bit data processing instruction.
621 void Emit32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700622 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100623 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700624 Register rn,
625 Register rd,
626 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700627
628 // Emit a 16 bit data processing instruction.
629 void Emit16BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700630 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100631 SetCc set_cc,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700632 Register rn,
633 Register rd,
634 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700635
636 void Emit16BitAddSub(Condition cond,
637 Opcode opcode,
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100638 SetCc set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700639 Register rn,
640 Register rd,
641 const ShifterOperand& so);
642
643 uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n);
644
645 void EmitLoadStore(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700646 bool load,
647 bool byte,
648 bool half,
649 bool is_signed,
650 Register rd,
651 const Address& ad);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700652
653 void EmitMemOpAddressMode3(Condition cond,
654 int32_t mode,
655 Register rd,
656 const Address& ad);
657
658 void EmitMultiMemOp(Condition cond,
659 BlockAddressMode am,
660 bool load,
661 Register base,
662 RegList regs);
663
664 void EmitMulOp(Condition cond,
665 int32_t opcode,
666 Register rd,
667 Register rn,
668 Register rm,
669 Register rs);
670
671 void EmitVFPsss(Condition cond,
672 int32_t opcode,
673 SRegister sd,
674 SRegister sn,
675 SRegister sm);
676
677 void EmitVFPddd(Condition cond,
678 int32_t opcode,
679 DRegister dd,
680 DRegister dn,
681 DRegister dm);
682
683 void EmitVFPsd(Condition cond,
684 int32_t opcode,
685 SRegister sd,
686 DRegister dm);
687
688 void EmitVFPds(Condition cond,
689 int32_t opcode,
690 DRegister dd,
691 SRegister sm);
692
693 void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond);
694
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000695 void EmitBranch(Condition cond, Label* label, bool link, bool x);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700696 static int32_t EncodeBranchOffset(int32_t offset, int32_t inst);
697 static int DecodeBranchOffset(int32_t inst);
698 int32_t EncodeTstOffset(int offset, int32_t inst);
699 int DecodeTstOffset(int32_t inst);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +0100700 void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount,
701 Condition cond = AL, SetCc set_cc = kCcDontCare);
702 void EmitShift(Register rd, Register rn, Shift shift, Register rm,
703 Condition cond = AL, SetCc set_cc = kCcDontCare);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700704
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100705 // Whether the assembler can relocate branches. If false, unresolved branches will be
706 // emitted on 32bits.
707 bool can_relocate_branches_;
708
709 // Force the assembler to use 32 bit thumb2 instructions.
710 bool force_32bit_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700711
712 // IfThen conditions. Used to check that conditional instructions match the preceding IT.
713 Condition it_conditions_[4];
714 uint8_t it_cond_index_;
715 Condition next_condition_;
716
717 void SetItCondition(ItState s, Condition cond, uint8_t index);
718
719 void CheckCondition(Condition cond) {
720 CHECK_EQ(cond, next_condition_);
721
722 // Move to the next condition if there is one.
723 if (it_cond_index_ < 3) {
724 ++it_cond_index_;
725 next_condition_ = it_conditions_[it_cond_index_];
726 } else {
727 next_condition_ = AL;
728 }
729 }
730
731 void CheckConditionLastIt(Condition cond) {
732 if (it_cond_index_ < 3) {
733 // Check that the next condition is AL. This means that the
734 // current condition is the last in the IT block.
735 CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL);
736 }
737 CheckCondition(cond);
738 }
739
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000740 FixupId AddFixup(Fixup fixup) {
741 FixupId fixup_id = static_cast<FixupId>(fixups_.size());
742 fixups_.push_back(fixup);
743 // For iterating using FixupId, we need the next id to be representable.
744 DCHECK_EQ(static_cast<size_t>(static_cast<FixupId>(fixups_.size())), fixups_.size());
745 return fixup_id;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700746 }
747
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000748 Fixup* GetFixup(FixupId fixup_id) {
749 DCHECK_LT(fixup_id, fixups_.size());
750 return &fixups_[fixup_id];
Dave Allison65fcc2c2014-04-28 13:45:27 -0700751 }
752
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000753 void BindLabel(Label* label, uint32_t bound_pc);
754 void BindLiterals();
755 void AdjustFixupIfNeeded(Fixup* fixup, uint32_t* current_code_size,
756 std::deque<FixupId>* fixups_to_recalculate);
757 uint32_t AdjustFixups();
758 void EmitFixups(uint32_t adjusted_code_size);
759 void EmitLiterals();
Dave Allison65fcc2c2014-04-28 13:45:27 -0700760
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000761 static int16_t BEncoding16(int32_t offset, Condition cond);
762 static int32_t BEncoding32(int32_t offset, Condition cond);
763 static int16_t CbxzEncoding16(Register rn, int32_t offset, Condition cond);
764 static int16_t CmpRnImm8Encoding16(Register rn, int32_t value);
765 static int16_t AddRdnRmEncoding16(Register rdn, Register rm);
766 static int32_t MovwEncoding32(Register rd, int32_t value);
767 static int32_t MovtEncoding32(Register rd, int32_t value);
768 static int32_t MovModImmEncoding32(Register rd, int32_t value);
769 static int16_t LdrLitEncoding16(Register rt, int32_t offset);
770 static int32_t LdrLitEncoding32(Register rt, int32_t offset);
771 static int32_t LdrdEncoding32(Register rt, Register rt2, Register rn, int32_t offset);
772 static int32_t VldrsEncoding32(SRegister sd, Register rn, int32_t offset);
773 static int32_t VldrdEncoding32(DRegister dd, Register rn, int32_t offset);
774 static int16_t LdrRtRnImm5Encoding16(Register rt, Register rn, int32_t offset);
775 static int32_t LdrRtRnImm12Encoding(Register rt, Register rn, int32_t offset);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700776
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000777 std::vector<Fixup> fixups_;
Vladimir Marko6b756b52015-07-14 11:58:38 +0100778 std::unique_ptr<FixupId[]> fixup_dependents_;
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000779
780 // Use std::deque<> for literal labels to allow insertions at the end
781 // without invalidating pointers and references to existing elements.
782 std::deque<Literal> literals_;
783
784 // Data for AdjustedPosition(), see the description there.
785 uint32_t last_position_adjustment_;
786 uint32_t last_old_position_;
787 FixupId last_fixup_id_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700788};
789
790} // namespace arm
791} // namespace art
792
793#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_