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