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