blob: 90d489fb023d96e12213a19d0c38dced9fcc5273 [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
20#include <vector>
21
22#include "base/logging.h"
23#include "constants_arm.h"
24#include "utils/arm/managed_register_arm.h"
25#include "utils/arm/assembler_arm.h"
26#include "offsets.h"
27#include "utils.h"
28
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),
38 next_condition_(AL) {
Dave Allison65fcc2c2014-04-28 13:45:27 -070039 }
40
41 virtual ~Thumb2Assembler() {
42 for (auto& branch : branches_) {
43 delete branch;
44 }
45 }
46
47 bool IsThumb() const OVERRIDE {
48 return true;
49 }
50
51 bool IsForced32Bit() const {
52 return force_32bit_;
53 }
54
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +010055 bool CanRelocateBranches() const {
56 return can_relocate_branches_;
Nicolas Geoffray8d486732014-07-16 16:23:40 +010057 }
58
Dave Allison65fcc2c2014-04-28 13:45:27 -070059 void FinalizeInstructions(const MemoryRegion& region) OVERRIDE {
60 EmitBranches();
61 Assembler::FinalizeInstructions(region);
62 }
63
64 // Data-processing instructions.
65 void and_(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
66
67 void eor(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
68
69 void sub(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
70 void subs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
71
72 void rsb(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
73 void rsbs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
74
75 void add(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
76
77 void adds(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
78
79 void adc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
80
81 void sbc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
82
83 void rsc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
84
85 void tst(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
86
87 void teq(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
88
89 void cmp(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
90
91 void cmn(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
92
93 void orr(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
94 void orrs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
95
96 void mov(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
97 void movs(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
98
99 void bic(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
100
101 void mvn(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
102 void mvns(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE;
103
104 // Miscellaneous data-processing instructions.
105 void clz(Register rd, Register rm, Condition cond = AL) OVERRIDE;
106 void movw(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
107 void movt(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE;
108
109 // Multiply instructions.
110 void mul(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
111 void mla(Register rd, Register rn, Register rm, Register ra,
112 Condition cond = AL) OVERRIDE;
113 void mls(Register rd, Register rn, Register rm, Register ra,
114 Condition cond = AL) OVERRIDE;
Zheng Xuc6667102015-05-15 16:08:45 +0800115 void smull(Register rd_lo, Register rd_hi, Register rn, Register rm,
116 Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700117 void umull(Register rd_lo, Register rd_hi, Register rn, Register rm,
118 Condition cond = AL) OVERRIDE;
119
120 void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
121 void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
122
Roland Levillain981e4542014-11-14 11:47:14 +0000123 // Bit field extract instructions.
Roland Levillain51d3fc42014-11-13 14:11:42 +0000124 void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain981e4542014-11-14 11:47:14 +0000125 void ubfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
Roland Levillain51d3fc42014-11-13 14:11:42 +0000126
Dave Allison65fcc2c2014-04-28 13:45:27 -0700127 // Load/store instructions.
128 void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
129 void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
130
131 void ldrb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
132 void strb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
133
134 void ldrh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
135 void strh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
136
137 void ldrsb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
138 void ldrsh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
139
Roland Levillain4af147e2015-04-07 13:54:49 +0100140 // Load/store register dual instructions using registers `rd` and `rd` + 1.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700141 void ldrd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
142 void strd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
143
Roland Levillain4af147e2015-04-07 13:54:49 +0100144 // Load/store register dual instructions using registers `rd` and `rd2`.
145 // Note that contrary to the ARM A1 encoding, the Thumb-2 T1 encoding
146 // does not require `rd` to be even, nor `rd2' to be equal to `rd` + 1.
147 void ldrd(Register rd, Register rd2, const Address& ad, Condition cond);
148 void strd(Register rd, Register rd2, const Address& ad, Condition cond);
149
150
Dave Allison65fcc2c2014-04-28 13:45:27 -0700151 void ldm(BlockAddressMode am, Register base,
152 RegList regs, Condition cond = AL) OVERRIDE;
153 void stm(BlockAddressMode am, Register base,
154 RegList regs, Condition cond = AL) OVERRIDE;
155
156 void ldrex(Register rd, Register rn, Condition cond = AL) OVERRIDE;
157 void strex(Register rd, Register rt, Register rn, Condition cond = AL) OVERRIDE;
158
159 void ldrex(Register rd, Register rn, uint16_t imm, Condition cond = AL);
160 void strex(Register rd, Register rt, Register rn, uint16_t imm, Condition cond = AL);
161
Calin Juravle52c48962014-12-16 17:02:57 +0000162 void ldrexd(Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
163 void strexd(Register rd, Register rt, Register rt2, Register rn, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700164
165 // Miscellaneous instructions.
166 void clrex(Condition cond = AL) OVERRIDE;
167 void nop(Condition cond = AL) OVERRIDE;
168
169 void bkpt(uint16_t imm16) OVERRIDE;
170 void svc(uint32_t imm24) OVERRIDE;
171
172 // If-then
173 void it(Condition firstcond, ItState i1 = kItOmitted,
174 ItState i2 = kItOmitted, ItState i3 = kItOmitted) OVERRIDE;
175
176 void cbz(Register rn, Label* target) OVERRIDE;
177 void cbnz(Register rn, Label* target) OVERRIDE;
178
179 // Floating point instructions (VFPv3-D16 and VFPv3-D32 profiles).
180 void vmovsr(SRegister sn, Register rt, Condition cond = AL) OVERRIDE;
181 void vmovrs(Register rt, SRegister sn, Condition cond = AL) OVERRIDE;
182 void vmovsrr(SRegister sm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
183 void vmovrrs(Register rt, Register rt2, SRegister sm, Condition cond = AL) OVERRIDE;
184 void vmovdrr(DRegister dm, Register rt, Register rt2, Condition cond = AL) OVERRIDE;
185 void vmovrrd(Register rt, Register rt2, DRegister dm, Condition cond = AL) OVERRIDE;
186 void vmovs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
187 void vmovd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
188
189 // Returns false if the immediate cannot be encoded.
190 bool vmovs(SRegister sd, float s_imm, Condition cond = AL) OVERRIDE;
191 bool vmovd(DRegister dd, double d_imm, Condition cond = AL) OVERRIDE;
192
193 void vldrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
194 void vstrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE;
195 void vldrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
196 void vstrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE;
197
198 void vadds(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
199 void vaddd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
200 void vsubs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
201 void vsubd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
202 void vmuls(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
203 void vmuld(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
204 void vmlas(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
205 void vmlad(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
206 void vmlss(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
207 void vmlsd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
208 void vdivs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE;
209 void vdivd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE;
210
211 void vabss(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
212 void vabsd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
213 void vnegs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
214 void vnegd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
215 void vsqrts(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
216 void vsqrtd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
217
218 void vcvtsd(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
219 void vcvtds(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
220 void vcvtis(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
221 void vcvtid(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
222 void vcvtsi(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
223 void vcvtdi(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
224 void vcvtus(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
225 void vcvtud(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE;
226 void vcvtsu(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
227 void vcvtdu(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE;
228
229 void vcmps(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE;
230 void vcmpd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE;
231 void vcmpsz(SRegister sd, Condition cond = AL) OVERRIDE;
232 void vcmpdz(DRegister dd, Condition cond = AL) OVERRIDE;
233 void vmstat(Condition cond = AL) OVERRIDE; // VMRS APSR_nzcv, FPSCR
234
235 void vpushs(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
236 void vpushd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
237 void vpops(SRegister reg, int nregs, Condition cond = AL) OVERRIDE;
238 void vpopd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE;
239
240 // Branch instructions.
241 void b(Label* label, Condition cond = AL);
242 void bl(Label* label, Condition cond = AL);
243 void blx(Label* label);
244 void blx(Register rm, Condition cond = AL) OVERRIDE;
245 void bx(Register rm, Condition cond = AL) OVERRIDE;
246
Dave Allison45fdb932014-06-25 12:37:10 -0700247 void Lsl(Register rd, Register rm, uint32_t shift_imm, bool setcc = false,
248 Condition cond = AL) OVERRIDE;
249 void Lsr(Register rd, Register rm, uint32_t shift_imm, bool setcc = false,
250 Condition cond = AL) OVERRIDE;
251 void Asr(Register rd, Register rm, uint32_t shift_imm, bool setcc = false,
252 Condition cond = AL) OVERRIDE;
253 void Ror(Register rd, Register rm, uint32_t shift_imm, bool setcc = false,
254 Condition cond = AL) OVERRIDE;
255 void Rrx(Register rd, Register rm, bool setcc = false,
256 Condition cond = AL) OVERRIDE;
257
258 void Lsl(Register rd, Register rm, Register rn, bool setcc = false,
259 Condition cond = AL) OVERRIDE;
260 void Lsr(Register rd, Register rm, Register rn, bool setcc = false,
261 Condition cond = AL) OVERRIDE;
262 void Asr(Register rd, Register rm, Register rn, bool setcc = false,
263 Condition cond = AL) OVERRIDE;
264 void Ror(Register rd, Register rm, Register rn, bool setcc = false,
265 Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700266
267 void Push(Register rd, Condition cond = AL) OVERRIDE;
268 void Pop(Register rd, Condition cond = AL) OVERRIDE;
269
270 void PushList(RegList regs, Condition cond = AL) OVERRIDE;
271 void PopList(RegList regs, Condition cond = AL) OVERRIDE;
272
273 void Mov(Register rd, Register rm, Condition cond = AL) OVERRIDE;
274
275 void CompareAndBranchIfZero(Register r, Label* label) OVERRIDE;
276 void CompareAndBranchIfNonZero(Register r, Label* label) OVERRIDE;
277
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100278 // Memory barriers.
279 void dmb(DmbOptions flavor) OVERRIDE;
280
Dave Allison65fcc2c2014-04-28 13:45:27 -0700281 // Macros.
282 // Add signed constant value to rd. May clobber IP.
283 void AddConstant(Register rd, int32_t value, Condition cond = AL) OVERRIDE;
284 void AddConstant(Register rd, Register rn, int32_t value,
285 Condition cond = AL) OVERRIDE;
286 void AddConstantSetFlags(Register rd, Register rn, int32_t value,
287 Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700288
289 // Load and Store. May clobber IP.
290 void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700291 void MarkExceptionHandler(Label* label) OVERRIDE;
292 void LoadFromOffset(LoadOperandType type,
293 Register reg,
294 Register base,
295 int32_t offset,
296 Condition cond = AL) OVERRIDE;
297 void StoreToOffset(StoreOperandType type,
298 Register reg,
299 Register base,
300 int32_t offset,
301 Condition cond = AL) OVERRIDE;
302 void LoadSFromOffset(SRegister reg,
303 Register base,
304 int32_t offset,
305 Condition cond = AL) OVERRIDE;
306 void StoreSToOffset(SRegister reg,
307 Register base,
308 int32_t offset,
309 Condition cond = AL) OVERRIDE;
310 void LoadDFromOffset(DRegister reg,
311 Register base,
312 int32_t offset,
313 Condition cond = AL) OVERRIDE;
314 void StoreDToOffset(DRegister reg,
315 Register base,
316 int32_t offset,
317 Condition cond = AL) OVERRIDE;
318
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +0000319 bool ShifterOperandCanHold(Register rd,
320 Register rn,
321 Opcode opcode,
322 uint32_t immediate,
323 ShifterOperand* shifter_op) OVERRIDE;
324
Dave Allison65fcc2c2014-04-28 13:45:27 -0700325
Ian Rogers13735952014-10-08 12:43:28 -0700326 static bool IsInstructionForExceptionHandling(uintptr_t pc);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700327
328 // Emit data (e.g. encoded instruction or immediate) to the.
329 // instruction stream.
330 void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format.
331 void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format.
332 void Bind(Label* label) OVERRIDE;
333
334 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
335
336 // Force the assembler to generate 32 bit instructions.
337 void Force32Bit() {
338 force_32bit_ = true;
339 }
340
341 private:
342 // Emit a single 32 or 16 bit data processing instruction.
343 void EmitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700344 Opcode opcode,
345 bool set_cc,
346 Register rn,
347 Register rd,
348 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700349
350 // Must the instruction be 32 bits or can it possibly be encoded
351 // in 16 bits?
352 bool Is32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700353 Opcode opcode,
354 bool set_cc,
355 Register rn,
356 Register rd,
357 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700358
359 // Emit a 32 bit data processing instruction.
360 void Emit32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700361 Opcode opcode,
362 bool set_cc,
363 Register rn,
364 Register rd,
365 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700366
367 // Emit a 16 bit data processing instruction.
368 void Emit16BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700369 Opcode opcode,
370 bool set_cc,
371 Register rn,
372 Register rd,
373 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700374
375 void Emit16BitAddSub(Condition cond,
376 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700377 bool set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700378 Register rn,
379 Register rd,
380 const ShifterOperand& so);
381
382 uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n);
383
384 void EmitLoadStore(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700385 bool load,
386 bool byte,
387 bool half,
388 bool is_signed,
389 Register rd,
390 const Address& ad);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700391
392 void EmitMemOpAddressMode3(Condition cond,
393 int32_t mode,
394 Register rd,
395 const Address& ad);
396
397 void EmitMultiMemOp(Condition cond,
398 BlockAddressMode am,
399 bool load,
400 Register base,
401 RegList regs);
402
403 void EmitMulOp(Condition cond,
404 int32_t opcode,
405 Register rd,
406 Register rn,
407 Register rm,
408 Register rs);
409
410 void EmitVFPsss(Condition cond,
411 int32_t opcode,
412 SRegister sd,
413 SRegister sn,
414 SRegister sm);
415
416 void EmitVFPddd(Condition cond,
417 int32_t opcode,
418 DRegister dd,
419 DRegister dn,
420 DRegister dm);
421
422 void EmitVFPsd(Condition cond,
423 int32_t opcode,
424 SRegister sd,
425 DRegister dm);
426
427 void EmitVFPds(Condition cond,
428 int32_t opcode,
429 DRegister dd,
430 SRegister sm);
431
432 void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond);
433
434 void EmitBranch(Condition cond, Label* label, bool link, bool x);
435 static int32_t EncodeBranchOffset(int32_t offset, int32_t inst);
436 static int DecodeBranchOffset(int32_t inst);
437 int32_t EncodeTstOffset(int offset, int32_t inst);
438 int DecodeTstOffset(int32_t inst);
Dave Allison45fdb932014-06-25 12:37:10 -0700439 void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount, bool setcc = false);
440 void EmitShift(Register rd, Register rn, Shift shift, Register rm, bool setcc = false);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700441
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100442 // Whether the assembler can relocate branches. If false, unresolved branches will be
443 // emitted on 32bits.
444 bool can_relocate_branches_;
445
446 // Force the assembler to use 32 bit thumb2 instructions.
447 bool force_32bit_;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700448
449 // IfThen conditions. Used to check that conditional instructions match the preceding IT.
450 Condition it_conditions_[4];
451 uint8_t it_cond_index_;
452 Condition next_condition_;
453
454 void SetItCondition(ItState s, Condition cond, uint8_t index);
455
456 void CheckCondition(Condition cond) {
457 CHECK_EQ(cond, next_condition_);
458
459 // Move to the next condition if there is one.
460 if (it_cond_index_ < 3) {
461 ++it_cond_index_;
462 next_condition_ = it_conditions_[it_cond_index_];
463 } else {
464 next_condition_ = AL;
465 }
466 }
467
468 void CheckConditionLastIt(Condition cond) {
469 if (it_cond_index_ < 3) {
470 // Check that the next condition is AL. This means that the
471 // current condition is the last in the IT block.
472 CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL);
473 }
474 CheckCondition(cond);
475 }
476
477 // Branches.
478 //
479 // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This
480 // depends on both the type of branch and the offset to which it is branching. When
481 // generating code for branches we don't know the size before hand (if the branch is
482 // going forward, because we haven't seen the target address yet), so we need to assume
483 // that it is going to be one of 16 or 32 bits. When we know the target (the label is 'bound')
484 // we can determine the actual size of the branch. However, if we had guessed wrong before
485 // we knew the target there will be no room in the instruction sequence for the new
486 // instruction (assume that we never decrease the size of a branch).
487 //
488 // To handle this, we keep a record of every branch in the program. The actual instruction
489 // encoding for these is delayed until we know the final size of every branch. When we
490 // bind a label to a branch (we then know the target address) we determine if the branch
491 // has changed size. If it has we need to move all the instructions in the buffer after
492 // the branch point forward by the change in size of the branch. This will create a gap
493 // in the code big enough for the new branch encoding. However, since we have moved
494 // a chunk of code we need to relocate the branches in that code to their new address.
495 //
496 // Creating a hole in the code for the new branch encoding might cause another branch that was
497 // 16 bits to become 32 bits, so we need to find this in another pass.
498 //
499 // We also need to deal with a cbz/cbnz instruction that becomes too big for its offset
500 // range. We do this by converting it to two instructions:
501 // cmp Rn, #0
502 // b<cond> target
503 // But we also need to handle the case where the conditional branch is out of range and
504 // becomes a 32 bit conditional branch.
505 //
506 // All branches have a 'branch id' which is a 16 bit unsigned number used to identify
507 // the branch. Unresolved labels use the branch id to link to the next unresolved branch.
508
509 class Branch {
510 public:
511 // Branch type.
512 enum Type {
513 kUnconditional, // B.
514 kConditional, // B<cond>.
515 kCompareAndBranchZero, // cbz.
516 kCompareAndBranchNonZero, // cbnz.
517 kUnconditionalLink, // BL.
518 kUnconditionalLinkX, // BLX.
519 kUnconditionalX // BX.
520 };
521
522 // Calculated size of branch instruction based on type and offset.
523 enum Size {
524 k16Bit,
525 k32Bit
526 };
527
528 // Unresolved branch possibly with a condition.
529 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Condition cond = AL) :
530 assembler_(assembler), type_(type), location_(location),
531 target_(kUnresolved),
532 cond_(cond), rn_(R0) {
533 CHECK(!IsCompareAndBranch());
534 size_ = CalculateSize();
535 }
536
537 // Unresolved compare-and-branch instruction with a register.
538 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Register rn) :
539 assembler_(assembler), type_(type), location_(location),
540 target_(kUnresolved), cond_(AL), rn_(rn) {
541 CHECK(IsCompareAndBranch());
542 size_ = CalculateSize();
543 }
544
545 // Resolved branch (can't be compare-and-branch) with a target and possibly a condition.
546 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, uint32_t target,
547 Condition cond = AL) :
548 assembler_(assembler), type_(type), location_(location),
549 target_(target), cond_(cond), rn_(R0) {
550 CHECK(!IsCompareAndBranch());
551 // Resolved branch.
552 size_ = CalculateSize();
553 }
554
555 bool IsCompareAndBranch() const {
556 return type_ == kCompareAndBranchNonZero || type_ == kCompareAndBranchZero;
557 }
558
559 // Resolve a branch when the target is known. If this causes the
560 // size of the branch to change return true. Otherwise return false.
561 bool Resolve(uint32_t target) {
562 target_ = target;
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100563 if (assembler_->CanRelocateBranches()) {
564 Size new_size = CalculateSize();
565 if (size_ != new_size) {
566 size_ = new_size;
567 return true;
568 }
569 return false;
570 } else {
571 if (kIsDebugBuild) {
572 Size new_size = CalculateSize();
573 // Check that the size has not increased.
574 DCHECK(!(new_size == k32Bit && size_ == k16Bit));
575 }
576 return false;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700577 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700578 }
579
580 // Move a cbz/cbnz branch. This is always forward.
581 void Move(int32_t delta) {
582 CHECK(IsCompareAndBranch());
583 CHECK_GT(delta, 0);
584 location_ += delta;
585 target_ += delta;
586 }
587
588 // Relocate a branch by a given delta. This changed the location and
589 // target if they need to be changed. It also recalculates the
590 // size of the branch instruction. It returns true if the branch
591 // has changed size.
592 bool Relocate(uint32_t oldlocation, int32_t delta) {
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100593 DCHECK(assembler_->CanRelocateBranches());
Dave Allison65fcc2c2014-04-28 13:45:27 -0700594 if (location_ > oldlocation) {
595 location_ += delta;
596 }
597 if (target_ != kUnresolved) {
598 if (target_ > oldlocation) {
599 target_ += delta;
600 }
601 } else {
602 return false; // Don't know the size yet.
603 }
604
605 // Calculate the new size.
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100606 Size new_size = CalculateSize();
607 if (size_ != new_size) {
608 size_ = new_size;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700609 return true;
610 }
611 return false;
612 }
613
614 Size GetSize() const {
615 return size_;
616 }
617
618 Type GetType() const {
619 return type_;
620 }
621
622 uint32_t GetLocation() const {
623 return location_;
624 }
625
626 // Emit the branch instruction into the assembler buffer. This does the
627 // encoding into the thumb instruction.
628 void Emit(AssemblerBuffer* buffer) const;
629
630 // Reset the type and condition to those given. This used for
631 // cbz/cbnz instructions when they are converted to cmp/b<cond>
632 void ResetTypeAndCondition(Type type, Condition cond) {
633 CHECK(IsCompareAndBranch());
634 CHECK(cond == EQ || cond == NE);
635 type_ = type;
636 cond_ = cond;
637 }
638
639 Register GetRegister() const {
640 return rn_;
641 }
642
643 void ResetSize(Size size) {
644 size_ = size;
645 }
646
647 private:
648 // Calculate the size of the branch instruction based on its type and offset.
649 Size CalculateSize() const {
Dave Allison65fcc2c2014-04-28 13:45:27 -0700650 if (target_ == kUnresolved) {
651 if (assembler_->IsForced32Bit() && (type_ == kUnconditional || type_ == kConditional)) {
652 return k32Bit;
653 }
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100654 return assembler_->CanRelocateBranches() ? k16Bit : k32Bit;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700655 }
Nicolas Geoffrayd126ba12015-05-20 11:25:27 +0100656 // When the target is resolved, we know the best encoding for it.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700657 int32_t delta = target_ - location_ - 4;
658 if (delta < 0) {
659 delta = -delta;
660 }
661 switch (type_) {
662 case kUnconditional:
663 if (assembler_->IsForced32Bit() || delta >= (1 << 11)) {
664 return k32Bit;
665 } else {
666 return k16Bit;
667 }
668 case kConditional:
669 if (assembler_->IsForced32Bit() || delta >= (1 << 8)) {
670 return k32Bit;
671 } else {
672 return k16Bit;
673 }
674 case kCompareAndBranchZero:
675 case kCompareAndBranchNonZero:
676 if (delta >= (1 << 7)) {
677 return k32Bit; // Will cause this branch to become invalid.
678 }
679 return k16Bit;
680
681 case kUnconditionalX:
682 case kUnconditionalLinkX:
683 return k16Bit;
684 case kUnconditionalLink:
685 return k32Bit;
686 }
687 LOG(FATAL) << "Cannot reach";
688 return k16Bit;
689 }
690
691 static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved.
692 const Thumb2Assembler* assembler_;
693 Type type_;
694 uint32_t location_; // Offset into assembler buffer in bytes.
695 uint32_t target_; // Offset into assembler buffer in bytes.
696 Size size_;
697 Condition cond_;
698 const Register rn_;
699 };
700
701 std::vector<Branch*> branches_;
702
703 // Add a resolved branch and return its size.
704 Branch::Size AddBranch(Branch::Type type, uint32_t location, uint32_t target,
705 Condition cond = AL) {
706 branches_.push_back(new Branch(this, type, location, target, cond));
707 return branches_[branches_.size()-1]->GetSize();
708 }
709
710 // Add a compare and branch (with a register) and return its id.
711 uint16_t AddBranch(Branch::Type type, uint32_t location, Register rn) {
712 branches_.push_back(new Branch(this, type, location, rn));
713 return branches_.size() - 1;
714 }
715
716 // Add an unresolved branch and return its id.
717 uint16_t AddBranch(Branch::Type type, uint32_t location, Condition cond = AL) {
718 branches_.push_back(new Branch(this, type, location, cond));
719 return branches_.size() - 1;
720 }
721
722 Branch* GetBranch(uint16_t branchid) {
723 if (branchid >= branches_.size()) {
724 return nullptr;
725 }
726 return branches_[branchid];
727 }
728
729 void EmitBranches();
730 void MakeHoleForBranch(uint32_t location, uint32_t size);
731};
732
733} // namespace arm
734} // namespace art
735
736#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_