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