blob: 9ccdef7e1eda7990420cc39ff43c94468beadfcf [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;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700272
273 // Load and Store. May clobber IP.
274 void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE;
Dave Allison65fcc2c2014-04-28 13:45:27 -0700275 void MarkExceptionHandler(Label* label) OVERRIDE;
276 void LoadFromOffset(LoadOperandType type,
277 Register reg,
278 Register base,
279 int32_t offset,
280 Condition cond = AL) OVERRIDE;
281 void StoreToOffset(StoreOperandType type,
282 Register reg,
283 Register base,
284 int32_t offset,
285 Condition cond = AL) OVERRIDE;
286 void LoadSFromOffset(SRegister reg,
287 Register base,
288 int32_t offset,
289 Condition cond = AL) OVERRIDE;
290 void StoreSToOffset(SRegister reg,
291 Register base,
292 int32_t offset,
293 Condition cond = AL) OVERRIDE;
294 void LoadDFromOffset(DRegister reg,
295 Register base,
296 int32_t offset,
297 Condition cond = AL) OVERRIDE;
298 void StoreDToOffset(DRegister reg,
299 Register base,
300 int32_t offset,
301 Condition cond = AL) OVERRIDE;
302
303
Ian Rogers13735952014-10-08 12:43:28 -0700304 static bool IsInstructionForExceptionHandling(uintptr_t pc);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700305
306 // Emit data (e.g. encoded instruction or immediate) to the.
307 // instruction stream.
308 void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format.
309 void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format.
310 void Bind(Label* label) OVERRIDE;
311
312 void MemoryBarrier(ManagedRegister scratch) OVERRIDE;
313
314 // Force the assembler to generate 32 bit instructions.
315 void Force32Bit() {
316 force_32bit_ = true;
317 }
318
319 private:
320 // Emit a single 32 or 16 bit data processing instruction.
321 void EmitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700322 Opcode opcode,
323 bool set_cc,
324 Register rn,
325 Register rd,
326 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700327
328 // Must the instruction be 32 bits or can it possibly be encoded
329 // in 16 bits?
330 bool Is32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700331 Opcode opcode,
332 bool set_cc,
333 Register rn,
334 Register rd,
335 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700336
337 // Emit a 32 bit data processing instruction.
338 void Emit32BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700339 Opcode opcode,
340 bool set_cc,
341 Register rn,
342 Register rd,
343 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700344
345 // Emit a 16 bit data processing instruction.
346 void Emit16BitDataProcessing(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700347 Opcode opcode,
348 bool set_cc,
349 Register rn,
350 Register rd,
351 const ShifterOperand& so);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700352
353 void Emit16BitAddSub(Condition cond,
354 Opcode opcode,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700355 bool set_cc,
Dave Allison65fcc2c2014-04-28 13:45:27 -0700356 Register rn,
357 Register rd,
358 const ShifterOperand& so);
359
360 uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n);
361
362 void EmitLoadStore(Condition cond,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700363 bool load,
364 bool byte,
365 bool half,
366 bool is_signed,
367 Register rd,
368 const Address& ad);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700369
370 void EmitMemOpAddressMode3(Condition cond,
371 int32_t mode,
372 Register rd,
373 const Address& ad);
374
375 void EmitMultiMemOp(Condition cond,
376 BlockAddressMode am,
377 bool load,
378 Register base,
379 RegList regs);
380
381 void EmitMulOp(Condition cond,
382 int32_t opcode,
383 Register rd,
384 Register rn,
385 Register rm,
386 Register rs);
387
388 void EmitVFPsss(Condition cond,
389 int32_t opcode,
390 SRegister sd,
391 SRegister sn,
392 SRegister sm);
393
394 void EmitVFPddd(Condition cond,
395 int32_t opcode,
396 DRegister dd,
397 DRegister dn,
398 DRegister dm);
399
400 void EmitVFPsd(Condition cond,
401 int32_t opcode,
402 SRegister sd,
403 DRegister dm);
404
405 void EmitVFPds(Condition cond,
406 int32_t opcode,
407 DRegister dd,
408 SRegister sm);
409
410 void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond);
411
412 void EmitBranch(Condition cond, Label* label, bool link, bool x);
413 static int32_t EncodeBranchOffset(int32_t offset, int32_t inst);
414 static int DecodeBranchOffset(int32_t inst);
415 int32_t EncodeTstOffset(int offset, int32_t inst);
416 int DecodeTstOffset(int32_t inst);
Dave Allison45fdb932014-06-25 12:37:10 -0700417 void EmitShift(Register rd, Register rm, Shift shift, uint8_t amount, bool setcc = false);
418 void EmitShift(Register rd, Register rn, Shift shift, Register rm, bool setcc = false);
Dave Allison65fcc2c2014-04-28 13:45:27 -0700419
Nicolas Geoffray169277a2014-07-17 09:16:19 +0100420 bool force_32bit_branches_; // Force the assembler to use 32 bit branch instructions.
421 bool force_32bit_; // Force the assembler to use 32 bit thumb2 instructions.
Dave Allison65fcc2c2014-04-28 13:45:27 -0700422
423 // IfThen conditions. Used to check that conditional instructions match the preceding IT.
424 Condition it_conditions_[4];
425 uint8_t it_cond_index_;
426 Condition next_condition_;
427
428 void SetItCondition(ItState s, Condition cond, uint8_t index);
429
430 void CheckCondition(Condition cond) {
431 CHECK_EQ(cond, next_condition_);
432
433 // Move to the next condition if there is one.
434 if (it_cond_index_ < 3) {
435 ++it_cond_index_;
436 next_condition_ = it_conditions_[it_cond_index_];
437 } else {
438 next_condition_ = AL;
439 }
440 }
441
442 void CheckConditionLastIt(Condition cond) {
443 if (it_cond_index_ < 3) {
444 // Check that the next condition is AL. This means that the
445 // current condition is the last in the IT block.
446 CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL);
447 }
448 CheckCondition(cond);
449 }
450
451 // Branches.
452 //
453 // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This
454 // depends on both the type of branch and the offset to which it is branching. When
455 // generating code for branches we don't know the size before hand (if the branch is
456 // going forward, because we haven't seen the target address yet), so we need to assume
457 // that it is going to be one of 16 or 32 bits. When we know the target (the label is 'bound')
458 // we can determine the actual size of the branch. However, if we had guessed wrong before
459 // we knew the target there will be no room in the instruction sequence for the new
460 // instruction (assume that we never decrease the size of a branch).
461 //
462 // To handle this, we keep a record of every branch in the program. The actual instruction
463 // encoding for these is delayed until we know the final size of every branch. When we
464 // bind a label to a branch (we then know the target address) we determine if the branch
465 // has changed size. If it has we need to move all the instructions in the buffer after
466 // the branch point forward by the change in size of the branch. This will create a gap
467 // in the code big enough for the new branch encoding. However, since we have moved
468 // a chunk of code we need to relocate the branches in that code to their new address.
469 //
470 // Creating a hole in the code for the new branch encoding might cause another branch that was
471 // 16 bits to become 32 bits, so we need to find this in another pass.
472 //
473 // We also need to deal with a cbz/cbnz instruction that becomes too big for its offset
474 // range. We do this by converting it to two instructions:
475 // cmp Rn, #0
476 // b<cond> target
477 // But we also need to handle the case where the conditional branch is out of range and
478 // becomes a 32 bit conditional branch.
479 //
480 // All branches have a 'branch id' which is a 16 bit unsigned number used to identify
481 // the branch. Unresolved labels use the branch id to link to the next unresolved branch.
482
483 class Branch {
484 public:
485 // Branch type.
486 enum Type {
487 kUnconditional, // B.
488 kConditional, // B<cond>.
489 kCompareAndBranchZero, // cbz.
490 kCompareAndBranchNonZero, // cbnz.
491 kUnconditionalLink, // BL.
492 kUnconditionalLinkX, // BLX.
493 kUnconditionalX // BX.
494 };
495
496 // Calculated size of branch instruction based on type and offset.
497 enum Size {
498 k16Bit,
499 k32Bit
500 };
501
502 // Unresolved branch possibly with a condition.
503 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Condition cond = AL) :
504 assembler_(assembler), type_(type), location_(location),
505 target_(kUnresolved),
506 cond_(cond), rn_(R0) {
507 CHECK(!IsCompareAndBranch());
508 size_ = CalculateSize();
509 }
510
511 // Unresolved compare-and-branch instruction with a register.
512 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Register rn) :
513 assembler_(assembler), type_(type), location_(location),
514 target_(kUnresolved), cond_(AL), rn_(rn) {
515 CHECK(IsCompareAndBranch());
516 size_ = CalculateSize();
517 }
518
519 // Resolved branch (can't be compare-and-branch) with a target and possibly a condition.
520 Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, uint32_t target,
521 Condition cond = AL) :
522 assembler_(assembler), type_(type), location_(location),
523 target_(target), cond_(cond), rn_(R0) {
524 CHECK(!IsCompareAndBranch());
525 // Resolved branch.
526 size_ = CalculateSize();
527 }
528
529 bool IsCompareAndBranch() const {
530 return type_ == kCompareAndBranchNonZero || type_ == kCompareAndBranchZero;
531 }
532
533 // Resolve a branch when the target is known. If this causes the
534 // size of the branch to change return true. Otherwise return false.
535 bool Resolve(uint32_t target) {
536 target_ = target;
537 Size newsize = CalculateSize();
538 if (size_ != newsize) {
539 size_ = newsize;
540 return true;
541 }
542 return false;
543 }
544
545 // Move a cbz/cbnz branch. This is always forward.
546 void Move(int32_t delta) {
547 CHECK(IsCompareAndBranch());
548 CHECK_GT(delta, 0);
549 location_ += delta;
550 target_ += delta;
551 }
552
553 // Relocate a branch by a given delta. This changed the location and
554 // target if they need to be changed. It also recalculates the
555 // size of the branch instruction. It returns true if the branch
556 // has changed size.
557 bool Relocate(uint32_t oldlocation, int32_t delta) {
558 if (location_ > oldlocation) {
559 location_ += delta;
560 }
561 if (target_ != kUnresolved) {
562 if (target_ > oldlocation) {
563 target_ += delta;
564 }
565 } else {
566 return false; // Don't know the size yet.
567 }
568
569 // Calculate the new size.
570 Size newsize = CalculateSize();
571 if (size_ != newsize) {
572 size_ = newsize;
573 return true;
574 }
575 return false;
576 }
577
578 Size GetSize() const {
579 return size_;
580 }
581
582 Type GetType() const {
583 return type_;
584 }
585
586 uint32_t GetLocation() const {
587 return location_;
588 }
589
590 // Emit the branch instruction into the assembler buffer. This does the
591 // encoding into the thumb instruction.
592 void Emit(AssemblerBuffer* buffer) const;
593
594 // Reset the type and condition to those given. This used for
595 // cbz/cbnz instructions when they are converted to cmp/b<cond>
596 void ResetTypeAndCondition(Type type, Condition cond) {
597 CHECK(IsCompareAndBranch());
598 CHECK(cond == EQ || cond == NE);
599 type_ = type;
600 cond_ = cond;
601 }
602
603 Register GetRegister() const {
604 return rn_;
605 }
606
607 void ResetSize(Size size) {
608 size_ = size;
609 }
610
611 private:
612 // Calculate the size of the branch instruction based on its type and offset.
613 Size CalculateSize() const {
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100614 if (assembler_->IsForced32BitBranches()) {
615 return k32Bit;
616 }
Dave Allison65fcc2c2014-04-28 13:45:27 -0700617 if (target_ == kUnresolved) {
618 if (assembler_->IsForced32Bit() && (type_ == kUnconditional || type_ == kConditional)) {
619 return k32Bit;
620 }
621 return k16Bit;
622 }
623 int32_t delta = target_ - location_ - 4;
624 if (delta < 0) {
625 delta = -delta;
626 }
627 switch (type_) {
628 case kUnconditional:
629 if (assembler_->IsForced32Bit() || delta >= (1 << 11)) {
630 return k32Bit;
631 } else {
632 return k16Bit;
633 }
634 case kConditional:
635 if (assembler_->IsForced32Bit() || delta >= (1 << 8)) {
636 return k32Bit;
637 } else {
638 return k16Bit;
639 }
640 case kCompareAndBranchZero:
641 case kCompareAndBranchNonZero:
642 if (delta >= (1 << 7)) {
643 return k32Bit; // Will cause this branch to become invalid.
644 }
645 return k16Bit;
646
647 case kUnconditionalX:
648 case kUnconditionalLinkX:
649 return k16Bit;
650 case kUnconditionalLink:
651 return k32Bit;
652 }
653 LOG(FATAL) << "Cannot reach";
654 return k16Bit;
655 }
656
657 static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved.
658 const Thumb2Assembler* assembler_;
659 Type type_;
660 uint32_t location_; // Offset into assembler buffer in bytes.
661 uint32_t target_; // Offset into assembler buffer in bytes.
662 Size size_;
663 Condition cond_;
664 const Register rn_;
665 };
666
667 std::vector<Branch*> branches_;
668
669 // Add a resolved branch and return its size.
670 Branch::Size AddBranch(Branch::Type type, uint32_t location, uint32_t target,
671 Condition cond = AL) {
672 branches_.push_back(new Branch(this, type, location, target, cond));
673 return branches_[branches_.size()-1]->GetSize();
674 }
675
676 // Add a compare and branch (with a register) and return its id.
677 uint16_t AddBranch(Branch::Type type, uint32_t location, Register rn) {
678 branches_.push_back(new Branch(this, type, location, rn));
679 return branches_.size() - 1;
680 }
681
682 // Add an unresolved branch and return its id.
683 uint16_t AddBranch(Branch::Type type, uint32_t location, Condition cond = AL) {
684 branches_.push_back(new Branch(this, type, location, cond));
685 return branches_.size() - 1;
686 }
687
688 Branch* GetBranch(uint16_t branchid) {
689 if (branchid >= branches_.size()) {
690 return nullptr;
691 }
692 return branches_[branchid];
693 }
694
695 void EmitBranches();
696 void MakeHoleForBranch(uint32_t location, uint32_t size);
697};
698
699} // namespace arm
700} // namespace art
701
702#endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_