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