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