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