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