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