Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 29 | namespace art { |
| 30 | namespace arm { |
| 31 | |
| 32 | |
| 33 | class Thumb2Assembler FINAL : public ArmAssembler { |
| 34 | public: |
| 35 | Thumb2Assembler() : force_32bit_(false), it_cond_index_(kNoItCondition), next_condition_(AL) { |
| 36 | } |
| 37 | |
| 38 | virtual ~Thumb2Assembler() { |
| 39 | for (auto& branch : branches_) { |
| 40 | delete branch; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool IsThumb() const OVERRIDE { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool IsForced32Bit() const { |
| 49 | return force_32bit_; |
| 50 | } |
| 51 | |
| 52 | void FinalizeInstructions(const MemoryRegion& region) OVERRIDE { |
| 53 | EmitBranches(); |
| 54 | Assembler::FinalizeInstructions(region); |
| 55 | } |
| 56 | |
| 57 | // Data-processing instructions. |
| 58 | void and_(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 59 | |
| 60 | void eor(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 61 | |
| 62 | void sub(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 63 | void subs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 64 | |
| 65 | void rsb(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 66 | void rsbs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 67 | |
| 68 | void add(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 69 | |
| 70 | void adds(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 71 | |
| 72 | void adc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 73 | |
| 74 | void sbc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 75 | |
| 76 | void rsc(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 77 | |
| 78 | void tst(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 79 | |
| 80 | void teq(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 81 | |
| 82 | void cmp(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 83 | |
| 84 | void cmn(Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 85 | |
| 86 | void orr(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 87 | void orrs(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 88 | |
| 89 | void mov(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 90 | void movs(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 91 | |
| 92 | void bic(Register rd, Register rn, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 93 | |
| 94 | void mvn(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 95 | void mvns(Register rd, const ShifterOperand& so, Condition cond = AL) OVERRIDE; |
| 96 | |
| 97 | // Miscellaneous data-processing instructions. |
| 98 | void clz(Register rd, Register rm, Condition cond = AL) OVERRIDE; |
| 99 | void movw(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE; |
| 100 | void movt(Register rd, uint16_t imm16, Condition cond = AL) OVERRIDE; |
| 101 | |
| 102 | // Multiply instructions. |
| 103 | void mul(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 104 | void mla(Register rd, Register rn, Register rm, Register ra, |
| 105 | Condition cond = AL) OVERRIDE; |
| 106 | void mls(Register rd, Register rn, Register rm, Register ra, |
| 107 | Condition cond = AL) OVERRIDE; |
| 108 | void umull(Register rd_lo, Register rd_hi, Register rn, Register rm, |
| 109 | Condition cond = AL) OVERRIDE; |
| 110 | |
| 111 | void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 112 | void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE; |
| 113 | |
| 114 | // Load/store instructions. |
| 115 | void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 116 | void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 117 | |
| 118 | void ldrb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 119 | void strb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 120 | |
| 121 | void ldrh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 122 | void strh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 123 | |
| 124 | void ldrsb(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 125 | void ldrsh(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 126 | |
| 127 | void ldrd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 128 | void strd(Register rd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 129 | |
| 130 | void ldm(BlockAddressMode am, Register base, |
| 131 | RegList regs, Condition cond = AL) OVERRIDE; |
| 132 | void stm(BlockAddressMode am, Register base, |
| 133 | RegList regs, Condition cond = AL) OVERRIDE; |
| 134 | |
| 135 | void ldrex(Register rd, Register rn, Condition cond = AL) OVERRIDE; |
| 136 | void strex(Register rd, Register rt, Register rn, Condition cond = AL) OVERRIDE; |
| 137 | |
| 138 | void ldrex(Register rd, Register rn, uint16_t imm, Condition cond = AL); |
| 139 | void strex(Register rd, Register rt, Register rn, uint16_t imm, Condition cond = AL); |
| 140 | |
| 141 | |
| 142 | // Miscellaneous instructions. |
| 143 | void clrex(Condition cond = AL) OVERRIDE; |
| 144 | void nop(Condition cond = AL) OVERRIDE; |
| 145 | |
| 146 | void bkpt(uint16_t imm16) OVERRIDE; |
| 147 | void svc(uint32_t imm24) OVERRIDE; |
| 148 | |
| 149 | // If-then |
| 150 | void it(Condition firstcond, ItState i1 = kItOmitted, |
| 151 | ItState i2 = kItOmitted, ItState i3 = kItOmitted) OVERRIDE; |
| 152 | |
| 153 | void cbz(Register rn, Label* target) OVERRIDE; |
| 154 | void cbnz(Register rn, Label* target) OVERRIDE; |
| 155 | |
| 156 | // Floating point instructions (VFPv3-D16 and VFPv3-D32 profiles). |
| 157 | void vmovsr(SRegister sn, Register rt, Condition cond = AL) OVERRIDE; |
| 158 | void vmovrs(Register rt, SRegister sn, Condition cond = AL) OVERRIDE; |
| 159 | void vmovsrr(SRegister sm, Register rt, Register rt2, Condition cond = AL) OVERRIDE; |
| 160 | void vmovrrs(Register rt, Register rt2, SRegister sm, Condition cond = AL) OVERRIDE; |
| 161 | void vmovdrr(DRegister dm, Register rt, Register rt2, Condition cond = AL) OVERRIDE; |
| 162 | void vmovrrd(Register rt, Register rt2, DRegister dm, Condition cond = AL) OVERRIDE; |
| 163 | void vmovs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 164 | void vmovd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 165 | |
| 166 | // Returns false if the immediate cannot be encoded. |
| 167 | bool vmovs(SRegister sd, float s_imm, Condition cond = AL) OVERRIDE; |
| 168 | bool vmovd(DRegister dd, double d_imm, Condition cond = AL) OVERRIDE; |
| 169 | |
| 170 | void vldrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 171 | void vstrs(SRegister sd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 172 | void vldrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 173 | void vstrd(DRegister dd, const Address& ad, Condition cond = AL) OVERRIDE; |
| 174 | |
| 175 | void vadds(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 176 | void vaddd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 177 | void vsubs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 178 | void vsubd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 179 | void vmuls(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 180 | void vmuld(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 181 | void vmlas(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 182 | void vmlad(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 183 | void vmlss(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 184 | void vmlsd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 185 | void vdivs(SRegister sd, SRegister sn, SRegister sm, Condition cond = AL) OVERRIDE; |
| 186 | void vdivd(DRegister dd, DRegister dn, DRegister dm, Condition cond = AL) OVERRIDE; |
| 187 | |
| 188 | void vabss(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 189 | void vabsd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 190 | void vnegs(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 191 | void vnegd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 192 | void vsqrts(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 193 | void vsqrtd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 194 | |
| 195 | void vcvtsd(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 196 | void vcvtds(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 197 | void vcvtis(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 198 | void vcvtid(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 199 | void vcvtsi(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 200 | void vcvtdi(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 201 | void vcvtus(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 202 | void vcvtud(SRegister sd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 203 | void vcvtsu(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 204 | void vcvtdu(DRegister dd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 205 | |
| 206 | void vcmps(SRegister sd, SRegister sm, Condition cond = AL) OVERRIDE; |
| 207 | void vcmpd(DRegister dd, DRegister dm, Condition cond = AL) OVERRIDE; |
| 208 | void vcmpsz(SRegister sd, Condition cond = AL) OVERRIDE; |
| 209 | void vcmpdz(DRegister dd, Condition cond = AL) OVERRIDE; |
| 210 | void vmstat(Condition cond = AL) OVERRIDE; // VMRS APSR_nzcv, FPSCR |
| 211 | |
| 212 | void vpushs(SRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 213 | void vpushd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 214 | void vpops(SRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 215 | void vpopd(DRegister reg, int nregs, Condition cond = AL) OVERRIDE; |
| 216 | |
| 217 | // Branch instructions. |
| 218 | void b(Label* label, Condition cond = AL); |
| 219 | void bl(Label* label, Condition cond = AL); |
| 220 | void blx(Label* label); |
| 221 | void blx(Register rm, Condition cond = AL) OVERRIDE; |
| 222 | void bx(Register rm, Condition cond = AL) OVERRIDE; |
| 223 | |
| 224 | void Lsl(Register rd, Register rm, uint32_t shift_imm, Condition cond = AL); |
| 225 | void Lsr(Register rd, Register rm, uint32_t shift_imm, Condition cond = AL); |
| 226 | void Asr(Register rd, Register rm, uint32_t shift_imm, Condition cond = AL); |
| 227 | void Ror(Register rd, Register rm, uint32_t shift_imm, Condition cond = AL); |
| 228 | void Rrx(Register rd, Register rm, Condition cond = AL); |
| 229 | |
| 230 | void Push(Register rd, Condition cond = AL) OVERRIDE; |
| 231 | void Pop(Register rd, Condition cond = AL) OVERRIDE; |
| 232 | |
| 233 | void PushList(RegList regs, Condition cond = AL) OVERRIDE; |
| 234 | void PopList(RegList regs, Condition cond = AL) OVERRIDE; |
| 235 | |
| 236 | void Mov(Register rd, Register rm, Condition cond = AL) OVERRIDE; |
| 237 | |
| 238 | void CompareAndBranchIfZero(Register r, Label* label) OVERRIDE; |
| 239 | void CompareAndBranchIfNonZero(Register r, Label* label) OVERRIDE; |
| 240 | |
| 241 | // Macros. |
| 242 | // Add signed constant value to rd. May clobber IP. |
| 243 | void AddConstant(Register rd, int32_t value, Condition cond = AL) OVERRIDE; |
| 244 | void AddConstant(Register rd, Register rn, int32_t value, |
| 245 | Condition cond = AL) OVERRIDE; |
| 246 | void AddConstantSetFlags(Register rd, Register rn, int32_t value, |
| 247 | Condition cond = AL) OVERRIDE; |
| 248 | void AddConstantWithCarry(Register rd, Register rn, int32_t value, |
| 249 | Condition cond = AL) {} |
| 250 | |
| 251 | // Load and Store. May clobber IP. |
| 252 | void LoadImmediate(Register rd, int32_t value, Condition cond = AL) OVERRIDE; |
| 253 | void LoadSImmediate(SRegister sd, float value, Condition cond = AL) {} |
| 254 | void LoadDImmediate(DRegister dd, double value, |
| 255 | Register scratch, Condition cond = AL) {} |
| 256 | void MarkExceptionHandler(Label* label) OVERRIDE; |
| 257 | void LoadFromOffset(LoadOperandType type, |
| 258 | Register reg, |
| 259 | Register base, |
| 260 | int32_t offset, |
| 261 | Condition cond = AL) OVERRIDE; |
| 262 | void StoreToOffset(StoreOperandType type, |
| 263 | Register reg, |
| 264 | Register base, |
| 265 | int32_t offset, |
| 266 | Condition cond = AL) OVERRIDE; |
| 267 | void LoadSFromOffset(SRegister reg, |
| 268 | Register base, |
| 269 | int32_t offset, |
| 270 | Condition cond = AL) OVERRIDE; |
| 271 | void StoreSToOffset(SRegister reg, |
| 272 | Register base, |
| 273 | int32_t offset, |
| 274 | Condition cond = AL) OVERRIDE; |
| 275 | void LoadDFromOffset(DRegister reg, |
| 276 | Register base, |
| 277 | int32_t offset, |
| 278 | Condition cond = AL) OVERRIDE; |
| 279 | void StoreDToOffset(DRegister reg, |
| 280 | Register base, |
| 281 | int32_t offset, |
| 282 | Condition cond = AL) OVERRIDE; |
| 283 | |
| 284 | |
| 285 | static bool IsInstructionForExceptionHandling(uword pc); |
| 286 | |
| 287 | // Emit data (e.g. encoded instruction or immediate) to the. |
| 288 | // instruction stream. |
| 289 | void Emit32(int32_t value); // Emit a 32 bit instruction in thumb format. |
| 290 | void Emit16(int16_t value); // Emit a 16 bit instruction in little endian format. |
| 291 | void Bind(Label* label) OVERRIDE; |
| 292 | |
| 293 | void MemoryBarrier(ManagedRegister scratch) OVERRIDE; |
| 294 | |
| 295 | // Force the assembler to generate 32 bit instructions. |
| 296 | void Force32Bit() { |
| 297 | force_32bit_ = true; |
| 298 | } |
| 299 | |
| 300 | private: |
| 301 | // Emit a single 32 or 16 bit data processing instruction. |
| 302 | void EmitDataProcessing(Condition cond, |
| 303 | Opcode opcode, |
| 304 | int set_cc, |
| 305 | Register rn, |
| 306 | Register rd, |
| 307 | const ShifterOperand& so); |
| 308 | |
| 309 | // Must the instruction be 32 bits or can it possibly be encoded |
| 310 | // in 16 bits? |
| 311 | bool Is32BitDataProcessing(Condition cond, |
| 312 | Opcode opcode, |
| 313 | int set_cc, |
| 314 | Register rn, |
| 315 | Register rd, |
| 316 | const ShifterOperand& so); |
| 317 | |
| 318 | // Emit a 32 bit data processing instruction. |
| 319 | void Emit32BitDataProcessing(Condition cond, |
| 320 | Opcode opcode, |
| 321 | int set_cc, |
| 322 | Register rn, |
| 323 | Register rd, |
| 324 | const ShifterOperand& so); |
| 325 | |
| 326 | // Emit a 16 bit data processing instruction. |
| 327 | void Emit16BitDataProcessing(Condition cond, |
| 328 | Opcode opcode, |
| 329 | int set_cc, |
| 330 | Register rn, |
| 331 | Register rd, |
| 332 | const ShifterOperand& so); |
| 333 | |
| 334 | void Emit16BitAddSub(Condition cond, |
| 335 | Opcode opcode, |
| 336 | int set_cc, |
| 337 | Register rn, |
| 338 | Register rd, |
| 339 | const ShifterOperand& so); |
| 340 | |
| 341 | uint16_t EmitCompareAndBranch(Register rn, uint16_t prev, bool n); |
| 342 | |
| 343 | void EmitLoadStore(Condition cond, |
| 344 | bool load, |
| 345 | bool byte, |
| 346 | bool half, |
| 347 | bool is_signed, |
| 348 | Register rd, |
| 349 | const Address& ad); |
| 350 | |
| 351 | void EmitMemOpAddressMode3(Condition cond, |
| 352 | int32_t mode, |
| 353 | Register rd, |
| 354 | const Address& ad); |
| 355 | |
| 356 | void EmitMultiMemOp(Condition cond, |
| 357 | BlockAddressMode am, |
| 358 | bool load, |
| 359 | Register base, |
| 360 | RegList regs); |
| 361 | |
| 362 | void EmitMulOp(Condition cond, |
| 363 | int32_t opcode, |
| 364 | Register rd, |
| 365 | Register rn, |
| 366 | Register rm, |
| 367 | Register rs); |
| 368 | |
| 369 | void EmitVFPsss(Condition cond, |
| 370 | int32_t opcode, |
| 371 | SRegister sd, |
| 372 | SRegister sn, |
| 373 | SRegister sm); |
| 374 | |
| 375 | void EmitVFPddd(Condition cond, |
| 376 | int32_t opcode, |
| 377 | DRegister dd, |
| 378 | DRegister dn, |
| 379 | DRegister dm); |
| 380 | |
| 381 | void EmitVFPsd(Condition cond, |
| 382 | int32_t opcode, |
| 383 | SRegister sd, |
| 384 | DRegister dm); |
| 385 | |
| 386 | void EmitVFPds(Condition cond, |
| 387 | int32_t opcode, |
| 388 | DRegister dd, |
| 389 | SRegister sm); |
| 390 | |
| 391 | void EmitVPushPop(uint32_t reg, int nregs, bool push, bool dbl, Condition cond); |
| 392 | |
| 393 | void EmitBranch(Condition cond, Label* label, bool link, bool x); |
| 394 | static int32_t EncodeBranchOffset(int32_t offset, int32_t inst); |
| 395 | static int DecodeBranchOffset(int32_t inst); |
| 396 | int32_t EncodeTstOffset(int offset, int32_t inst); |
| 397 | int DecodeTstOffset(int32_t inst); |
| 398 | |
| 399 | bool IsLowRegister(Register r) { |
| 400 | return r < R8; |
| 401 | } |
| 402 | |
| 403 | bool IsHighRegister(Register r) { |
| 404 | return r >= R8; |
| 405 | } |
| 406 | |
| 407 | bool force_32bit_; // Force the assembler to use 32 bit thumb2 instructions. |
| 408 | |
| 409 | // IfThen conditions. Used to check that conditional instructions match the preceding IT. |
| 410 | Condition it_conditions_[4]; |
| 411 | uint8_t it_cond_index_; |
| 412 | Condition next_condition_; |
| 413 | |
| 414 | void SetItCondition(ItState s, Condition cond, uint8_t index); |
| 415 | |
| 416 | void CheckCondition(Condition cond) { |
| 417 | CHECK_EQ(cond, next_condition_); |
| 418 | |
| 419 | // Move to the next condition if there is one. |
| 420 | if (it_cond_index_ < 3) { |
| 421 | ++it_cond_index_; |
| 422 | next_condition_ = it_conditions_[it_cond_index_]; |
| 423 | } else { |
| 424 | next_condition_ = AL; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void CheckConditionLastIt(Condition cond) { |
| 429 | if (it_cond_index_ < 3) { |
| 430 | // Check that the next condition is AL. This means that the |
| 431 | // current condition is the last in the IT block. |
| 432 | CHECK_EQ(it_conditions_[it_cond_index_ + 1], AL); |
| 433 | } |
| 434 | CheckCondition(cond); |
| 435 | } |
| 436 | |
| 437 | // Branches. |
| 438 | // |
| 439 | // The thumb2 architecture allows branches to be either 16 or 32 bit instructions. This |
| 440 | // depends on both the type of branch and the offset to which it is branching. When |
| 441 | // generating code for branches we don't know the size before hand (if the branch is |
| 442 | // going forward, because we haven't seen the target address yet), so we need to assume |
| 443 | // that it is going to be one of 16 or 32 bits. When we know the target (the label is 'bound') |
| 444 | // we can determine the actual size of the branch. However, if we had guessed wrong before |
| 445 | // we knew the target there will be no room in the instruction sequence for the new |
| 446 | // instruction (assume that we never decrease the size of a branch). |
| 447 | // |
| 448 | // To handle this, we keep a record of every branch in the program. The actual instruction |
| 449 | // encoding for these is delayed until we know the final size of every branch. When we |
| 450 | // bind a label to a branch (we then know the target address) we determine if the branch |
| 451 | // has changed size. If it has we need to move all the instructions in the buffer after |
| 452 | // the branch point forward by the change in size of the branch. This will create a gap |
| 453 | // in the code big enough for the new branch encoding. However, since we have moved |
| 454 | // a chunk of code we need to relocate the branches in that code to their new address. |
| 455 | // |
| 456 | // Creating a hole in the code for the new branch encoding might cause another branch that was |
| 457 | // 16 bits to become 32 bits, so we need to find this in another pass. |
| 458 | // |
| 459 | // We also need to deal with a cbz/cbnz instruction that becomes too big for its offset |
| 460 | // range. We do this by converting it to two instructions: |
| 461 | // cmp Rn, #0 |
| 462 | // b<cond> target |
| 463 | // But we also need to handle the case where the conditional branch is out of range and |
| 464 | // becomes a 32 bit conditional branch. |
| 465 | // |
| 466 | // All branches have a 'branch id' which is a 16 bit unsigned number used to identify |
| 467 | // the branch. Unresolved labels use the branch id to link to the next unresolved branch. |
| 468 | |
| 469 | class Branch { |
| 470 | public: |
| 471 | // Branch type. |
| 472 | enum Type { |
| 473 | kUnconditional, // B. |
| 474 | kConditional, // B<cond>. |
| 475 | kCompareAndBranchZero, // cbz. |
| 476 | kCompareAndBranchNonZero, // cbnz. |
| 477 | kUnconditionalLink, // BL. |
| 478 | kUnconditionalLinkX, // BLX. |
| 479 | kUnconditionalX // BX. |
| 480 | }; |
| 481 | |
| 482 | // Calculated size of branch instruction based on type and offset. |
| 483 | enum Size { |
| 484 | k16Bit, |
| 485 | k32Bit |
| 486 | }; |
| 487 | |
| 488 | // Unresolved branch possibly with a condition. |
| 489 | Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Condition cond = AL) : |
| 490 | assembler_(assembler), type_(type), location_(location), |
| 491 | target_(kUnresolved), |
| 492 | cond_(cond), rn_(R0) { |
| 493 | CHECK(!IsCompareAndBranch()); |
| 494 | size_ = CalculateSize(); |
| 495 | } |
| 496 | |
| 497 | // Unresolved compare-and-branch instruction with a register. |
| 498 | Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, Register rn) : |
| 499 | assembler_(assembler), type_(type), location_(location), |
| 500 | target_(kUnresolved), cond_(AL), rn_(rn) { |
| 501 | CHECK(IsCompareAndBranch()); |
| 502 | size_ = CalculateSize(); |
| 503 | } |
| 504 | |
| 505 | // Resolved branch (can't be compare-and-branch) with a target and possibly a condition. |
| 506 | Branch(const Thumb2Assembler* assembler, Type type, uint32_t location, uint32_t target, |
| 507 | Condition cond = AL) : |
| 508 | assembler_(assembler), type_(type), location_(location), |
| 509 | target_(target), cond_(cond), rn_(R0) { |
| 510 | CHECK(!IsCompareAndBranch()); |
| 511 | // Resolved branch. |
| 512 | size_ = CalculateSize(); |
| 513 | } |
| 514 | |
| 515 | bool IsCompareAndBranch() const { |
| 516 | return type_ == kCompareAndBranchNonZero || type_ == kCompareAndBranchZero; |
| 517 | } |
| 518 | |
| 519 | // Resolve a branch when the target is known. If this causes the |
| 520 | // size of the branch to change return true. Otherwise return false. |
| 521 | bool Resolve(uint32_t target) { |
| 522 | target_ = target; |
| 523 | Size newsize = CalculateSize(); |
| 524 | if (size_ != newsize) { |
| 525 | size_ = newsize; |
| 526 | return true; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | // Move a cbz/cbnz branch. This is always forward. |
| 532 | void Move(int32_t delta) { |
| 533 | CHECK(IsCompareAndBranch()); |
| 534 | CHECK_GT(delta, 0); |
| 535 | location_ += delta; |
| 536 | target_ += delta; |
| 537 | } |
| 538 | |
| 539 | // Relocate a branch by a given delta. This changed the location and |
| 540 | // target if they need to be changed. It also recalculates the |
| 541 | // size of the branch instruction. It returns true if the branch |
| 542 | // has changed size. |
| 543 | bool Relocate(uint32_t oldlocation, int32_t delta) { |
| 544 | if (location_ > oldlocation) { |
| 545 | location_ += delta; |
| 546 | } |
| 547 | if (target_ != kUnresolved) { |
| 548 | if (target_ > oldlocation) { |
| 549 | target_ += delta; |
| 550 | } |
| 551 | } else { |
| 552 | return false; // Don't know the size yet. |
| 553 | } |
| 554 | |
| 555 | // Calculate the new size. |
| 556 | Size newsize = CalculateSize(); |
| 557 | if (size_ != newsize) { |
| 558 | size_ = newsize; |
| 559 | return true; |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | Size GetSize() const { |
| 565 | return size_; |
| 566 | } |
| 567 | |
| 568 | Type GetType() const { |
| 569 | return type_; |
| 570 | } |
| 571 | |
| 572 | uint32_t GetLocation() const { |
| 573 | return location_; |
| 574 | } |
| 575 | |
| 576 | // Emit the branch instruction into the assembler buffer. This does the |
| 577 | // encoding into the thumb instruction. |
| 578 | void Emit(AssemblerBuffer* buffer) const; |
| 579 | |
| 580 | // Reset the type and condition to those given. This used for |
| 581 | // cbz/cbnz instructions when they are converted to cmp/b<cond> |
| 582 | void ResetTypeAndCondition(Type type, Condition cond) { |
| 583 | CHECK(IsCompareAndBranch()); |
| 584 | CHECK(cond == EQ || cond == NE); |
| 585 | type_ = type; |
| 586 | cond_ = cond; |
| 587 | } |
| 588 | |
| 589 | Register GetRegister() const { |
| 590 | return rn_; |
| 591 | } |
| 592 | |
| 593 | void ResetSize(Size size) { |
| 594 | size_ = size; |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | // Calculate the size of the branch instruction based on its type and offset. |
| 599 | Size CalculateSize() const { |
| 600 | if (target_ == kUnresolved) { |
| 601 | if (assembler_->IsForced32Bit() && (type_ == kUnconditional || type_ == kConditional)) { |
| 602 | return k32Bit; |
| 603 | } |
| 604 | return k16Bit; |
| 605 | } |
| 606 | int32_t delta = target_ - location_ - 4; |
| 607 | if (delta < 0) { |
| 608 | delta = -delta; |
| 609 | } |
| 610 | switch (type_) { |
| 611 | case kUnconditional: |
| 612 | if (assembler_->IsForced32Bit() || delta >= (1 << 11)) { |
| 613 | return k32Bit; |
| 614 | } else { |
| 615 | return k16Bit; |
| 616 | } |
| 617 | case kConditional: |
| 618 | if (assembler_->IsForced32Bit() || delta >= (1 << 8)) { |
| 619 | return k32Bit; |
| 620 | } else { |
| 621 | return k16Bit; |
| 622 | } |
| 623 | case kCompareAndBranchZero: |
| 624 | case kCompareAndBranchNonZero: |
| 625 | if (delta >= (1 << 7)) { |
| 626 | return k32Bit; // Will cause this branch to become invalid. |
| 627 | } |
| 628 | return k16Bit; |
| 629 | |
| 630 | case kUnconditionalX: |
| 631 | case kUnconditionalLinkX: |
| 632 | return k16Bit; |
| 633 | case kUnconditionalLink: |
| 634 | return k32Bit; |
| 635 | } |
| 636 | LOG(FATAL) << "Cannot reach"; |
| 637 | return k16Bit; |
| 638 | } |
| 639 | |
| 640 | static constexpr uint32_t kUnresolved = 0xffffffff; // Value for target_ for unresolved. |
| 641 | const Thumb2Assembler* assembler_; |
| 642 | Type type_; |
| 643 | uint32_t location_; // Offset into assembler buffer in bytes. |
| 644 | uint32_t target_; // Offset into assembler buffer in bytes. |
| 645 | Size size_; |
| 646 | Condition cond_; |
| 647 | const Register rn_; |
| 648 | }; |
| 649 | |
| 650 | std::vector<Branch*> branches_; |
| 651 | |
| 652 | // Add a resolved branch and return its size. |
| 653 | Branch::Size AddBranch(Branch::Type type, uint32_t location, uint32_t target, |
| 654 | Condition cond = AL) { |
| 655 | branches_.push_back(new Branch(this, type, location, target, cond)); |
| 656 | return branches_[branches_.size()-1]->GetSize(); |
| 657 | } |
| 658 | |
| 659 | // Add a compare and branch (with a register) and return its id. |
| 660 | uint16_t AddBranch(Branch::Type type, uint32_t location, Register rn) { |
| 661 | branches_.push_back(new Branch(this, type, location, rn)); |
| 662 | return branches_.size() - 1; |
| 663 | } |
| 664 | |
| 665 | // Add an unresolved branch and return its id. |
| 666 | uint16_t AddBranch(Branch::Type type, uint32_t location, Condition cond = AL) { |
| 667 | branches_.push_back(new Branch(this, type, location, cond)); |
| 668 | return branches_.size() - 1; |
| 669 | } |
| 670 | |
| 671 | Branch* GetBranch(uint16_t branchid) { |
| 672 | if (branchid >= branches_.size()) { |
| 673 | return nullptr; |
| 674 | } |
| 675 | return branches_[branchid]; |
| 676 | } |
| 677 | |
| 678 | void EmitBranches(); |
| 679 | void MakeHoleForBranch(uint32_t location, uint32_t size); |
| 680 | }; |
| 681 | |
| 682 | } // namespace arm |
| 683 | } // namespace art |
| 684 | |
| 685 | #endif // ART_COMPILER_UTILS_ARM_ASSEMBLER_THUMB2_H_ |