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