1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the ARMMCCodeEmitter class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/ARMAddressingModes.h" 15 #include "MCTargetDesc/ARMBaseInfo.h" 16 #include "MCTargetDesc/ARMFixupKinds.h" 17 #include "MCTargetDesc/ARMMCExpr.h" 18 #include "llvm/ADT/APFloat.h" 19 #include "llvm/ADT/APInt.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/MC/MCCodeEmitter.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCExpr.h" 26 #include "llvm/MC/MCFixup.h" 27 #include "llvm/MC/MCInst.h" 28 #include "llvm/MC/MCInstrDesc.h" 29 #include "llvm/MC/MCInstrInfo.h" 30 #include "llvm/MC/MCRegisterInfo.h" 31 #include "llvm/MC/MCSubtargetInfo.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/Compiler.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/MathExtras.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include <algorithm> 38 #include <cassert> 39 #include <cstdint> 40 #include <cstdlib> 41 42 using namespace llvm; 43 44 #define DEBUG_TYPE "mccodeemitter" 45 46 STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); 47 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created."); 48 49 namespace { 50 51 class ARMMCCodeEmitter : public MCCodeEmitter { 52 const MCInstrInfo &MCII; 53 const MCContext &CTX; 54 bool IsLittleEndian; 55 56 public: 57 ARMMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx, bool IsLittle) 58 : MCII(mcii), CTX(ctx), IsLittleEndian(IsLittle) { 59 } 60 ARMMCCodeEmitter(const ARMMCCodeEmitter &) = delete; 61 ARMMCCodeEmitter &operator=(const ARMMCCodeEmitter &) = delete; 62 ~ARMMCCodeEmitter() override = default; 63 64 bool isThumb(const MCSubtargetInfo &STI) const { 65 return STI.getFeatureBits()[ARM::ModeThumb]; 66 } 67 68 bool isThumb2(const MCSubtargetInfo &STI) const { 69 return isThumb(STI) && STI.getFeatureBits()[ARM::FeatureThumb2]; 70 } 71 72 bool isTargetMachO(const MCSubtargetInfo &STI) const { 73 const Triple &TT = STI.getTargetTriple(); 74 return TT.isOSBinFormatMachO(); 75 } 76 77 unsigned getMachineSoImmOpValue(unsigned SoImm) const; 78 79 // getBinaryCodeForInstr - TableGen'erated function for getting the 80 // binary encoding for an instruction. 81 uint64_t getBinaryCodeForInstr(const MCInst &MI, 82 SmallVectorImpl<MCFixup> &Fixups, 83 const MCSubtargetInfo &STI) const; 84 85 /// getMachineOpValue - Return binary encoding of operand. If the machine 86 /// operand requires relocation, record the relocation and return zero. 87 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, 88 SmallVectorImpl<MCFixup> &Fixups, 89 const MCSubtargetInfo &STI) const; 90 91 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of 92 /// the specified operand. This is used for operands with :lower16: and 93 /// :upper16: prefixes. 94 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx, 95 SmallVectorImpl<MCFixup> &Fixups, 96 const MCSubtargetInfo &STI) const; 97 98 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, 99 unsigned &Reg, unsigned &Imm, 100 SmallVectorImpl<MCFixup> &Fixups, 101 const MCSubtargetInfo &STI) const; 102 103 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate 104 /// BL branch target. 105 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx, 106 SmallVectorImpl<MCFixup> &Fixups, 107 const MCSubtargetInfo &STI) const; 108 109 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate 110 /// BLX branch target. 111 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx, 112 SmallVectorImpl<MCFixup> &Fixups, 113 const MCSubtargetInfo &STI) const; 114 115 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target. 116 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx, 117 SmallVectorImpl<MCFixup> &Fixups, 118 const MCSubtargetInfo &STI) const; 119 120 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target. 121 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx, 122 SmallVectorImpl<MCFixup> &Fixups, 123 const MCSubtargetInfo &STI) const; 124 125 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target. 126 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx, 127 SmallVectorImpl<MCFixup> &Fixups, 128 const MCSubtargetInfo &STI) const; 129 130 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate 131 /// branch target. 132 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 133 SmallVectorImpl<MCFixup> &Fixups, 134 const MCSubtargetInfo &STI) const; 135 136 /// getThumbBranchTargetOpValue - Return encoding info for 24-bit 137 /// immediate Thumb2 direct branch target. 138 uint32_t getThumbBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 139 SmallVectorImpl<MCFixup> &Fixups, 140 const MCSubtargetInfo &STI) const; 141 142 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate 143 /// branch target. 144 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 145 SmallVectorImpl<MCFixup> &Fixups, 146 const MCSubtargetInfo &STI) const; 147 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx, 148 SmallVectorImpl<MCFixup> &Fixups, 149 const MCSubtargetInfo &STI) const; 150 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx, 151 SmallVectorImpl<MCFixup> &Fixups, 152 const MCSubtargetInfo &STI) const; 153 154 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate 155 /// ADR label target. 156 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 157 SmallVectorImpl<MCFixup> &Fixups, 158 const MCSubtargetInfo &STI) const; 159 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 160 SmallVectorImpl<MCFixup> &Fixups, 161 const MCSubtargetInfo &STI) const; 162 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 163 SmallVectorImpl<MCFixup> &Fixups, 164 const MCSubtargetInfo &STI) const; 165 166 167 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' 168 /// operand. 169 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx, 170 SmallVectorImpl<MCFixup> &Fixups, 171 const MCSubtargetInfo &STI) const; 172 173 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand. 174 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx, 175 SmallVectorImpl<MCFixup> &Fixups, 176 const MCSubtargetInfo &STI) const; 177 178 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2' 179 /// operand. 180 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx, 181 SmallVectorImpl<MCFixup> &Fixups, 182 const MCSubtargetInfo &STI) const; 183 184 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2' 185 /// operand. 186 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx, 187 SmallVectorImpl<MCFixup> &Fixups, 188 const MCSubtargetInfo &STI) const; 189 190 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2' 191 /// operand. 192 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx, 193 SmallVectorImpl<MCFixup> &Fixups, 194 const MCSubtargetInfo &STI) const; 195 196 197 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm' 198 /// operand as needed by load/store instructions. 199 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx, 200 SmallVectorImpl<MCFixup> &Fixups, 201 const MCSubtargetInfo &STI) const; 202 203 /// getLdStmModeOpValue - Return encoding for load/store multiple mode. 204 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx, 205 SmallVectorImpl<MCFixup> &Fixups, 206 const MCSubtargetInfo &STI) const { 207 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm(); 208 switch (Mode) { 209 default: llvm_unreachable("Unknown addressing sub-mode!"); 210 case ARM_AM::da: return 0; 211 case ARM_AM::ia: return 1; 212 case ARM_AM::db: return 2; 213 case ARM_AM::ib: return 3; 214 } 215 } 216 217 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. 218 /// 219 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const { 220 switch (ShOpc) { 221 case ARM_AM::no_shift: 222 case ARM_AM::lsl: return 0; 223 case ARM_AM::lsr: return 1; 224 case ARM_AM::asr: return 2; 225 case ARM_AM::ror: 226 case ARM_AM::rrx: return 3; 227 } 228 llvm_unreachable("Invalid ShiftOpc!"); 229 } 230 231 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands. 232 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx, 233 SmallVectorImpl<MCFixup> &Fixups, 234 const MCSubtargetInfo &STI) const; 235 236 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands. 237 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx, 238 SmallVectorImpl<MCFixup> &Fixups, 239 const MCSubtargetInfo &STI) const; 240 241 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands. 242 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx, 243 SmallVectorImpl<MCFixup> &Fixups, 244 const MCSubtargetInfo &STI) const; 245 246 /// getAddrMode3OpValue - Return encoding for addrmode3 operands. 247 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx, 248 SmallVectorImpl<MCFixup> &Fixups, 249 const MCSubtargetInfo &STI) const; 250 251 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12' 252 /// operand. 253 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx, 254 SmallVectorImpl<MCFixup> &Fixups, 255 const MCSubtargetInfo &STI) const; 256 257 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands. 258 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx, 259 SmallVectorImpl<MCFixup> &Fixups, 260 const MCSubtargetInfo &STI) const; 261 262 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands. 263 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx, 264 SmallVectorImpl<MCFixup> &Fixups, 265 const MCSubtargetInfo &STI) const; 266 267 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand. 268 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx, 269 SmallVectorImpl<MCFixup> &Fixups, 270 const MCSubtargetInfo &STI) const; 271 272 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand. 273 uint32_t getAddrMode5FP16OpValue(const MCInst &MI, unsigned OpIdx, 274 SmallVectorImpl<MCFixup> &Fixups, 275 const MCSubtargetInfo &STI) const; 276 277 /// getCCOutOpValue - Return encoding of the 's' bit. 278 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op, 279 SmallVectorImpl<MCFixup> &Fixups, 280 const MCSubtargetInfo &STI) const { 281 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or 282 // '1' respectively. 283 return MI.getOperand(Op).getReg() == ARM::CPSR; 284 } 285 286 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value. 287 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op, 288 SmallVectorImpl<MCFixup> &Fixups, 289 const MCSubtargetInfo &STI) const { 290 const MCOperand &MO = MI.getOperand(Op); 291 292 // We expect MO to be an immediate or an expression, 293 // if it is an immediate - that's fine, just encode the value. 294 // Otherwise - create a Fixup. 295 if (MO.isExpr()) { 296 const MCExpr *Expr = MO.getExpr(); 297 // In instruction code this value always encoded as lowest 12 bits, 298 // so we don't have to perform any specific adjustments. 299 // Due to requirements of relocatable records we have to use FK_Data_4. 300 // See ARMELFObjectWriter::ExplicitRelSym and 301 // ARMELFObjectWriter::GetRelocTypeInner for more details. 302 MCFixupKind Kind = MCFixupKind(FK_Data_4); 303 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 304 return 0; 305 } 306 307 unsigned SoImm = MO.getImm(); 308 int SoImmVal = ARM_AM::getSOImmVal(SoImm); 309 assert(SoImmVal != -1 && "Not a valid so_imm value!"); 310 311 // Encode rotate_imm. 312 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) 313 << ARMII::SoRotImmShift; 314 315 // Encode immed_8. 316 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); 317 return Binary; 318 } 319 320 unsigned getModImmOpValue(const MCInst &MI, unsigned Op, 321 SmallVectorImpl<MCFixup> &Fixups, 322 const MCSubtargetInfo &ST) const { 323 const MCOperand &MO = MI.getOperand(Op); 324 325 // Support for fixups (MCFixup) 326 if (MO.isExpr()) { 327 const MCExpr *Expr = MO.getExpr(); 328 // Fixups resolve to plain values that need to be encoded. 329 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_mod_imm); 330 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 331 return 0; 332 } 333 334 // Immediate is already in its encoded format 335 return MO.getImm(); 336 } 337 338 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value. 339 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op, 340 SmallVectorImpl<MCFixup> &Fixups, 341 const MCSubtargetInfo &STI) const { 342 unsigned SoImm = MI.getOperand(Op).getImm(); 343 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm); 344 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?"); 345 return Encoded; 346 } 347 348 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum, 349 SmallVectorImpl<MCFixup> &Fixups, 350 const MCSubtargetInfo &STI) const; 351 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum, 352 SmallVectorImpl<MCFixup> &Fixups, 353 const MCSubtargetInfo &STI) const; 354 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum, 355 SmallVectorImpl<MCFixup> &Fixups, 356 const MCSubtargetInfo &STI) const; 357 358 /// getSORegOpValue - Return an encoded so_reg shifted register value. 359 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op, 360 SmallVectorImpl<MCFixup> &Fixups, 361 const MCSubtargetInfo &STI) const; 362 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op, 363 SmallVectorImpl<MCFixup> &Fixups, 364 const MCSubtargetInfo &STI) const; 365 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op, 366 SmallVectorImpl<MCFixup> &Fixups, 367 const MCSubtargetInfo &STI) const; 368 369 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op, 370 SmallVectorImpl<MCFixup> &Fixups, 371 const MCSubtargetInfo &STI) const { 372 return 64 - MI.getOperand(Op).getImm(); 373 } 374 375 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op, 376 SmallVectorImpl<MCFixup> &Fixups, 377 const MCSubtargetInfo &STI) const; 378 379 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op, 380 SmallVectorImpl<MCFixup> &Fixups, 381 const MCSubtargetInfo &STI) const; 382 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op, 383 SmallVectorImpl<MCFixup> &Fixups, 384 const MCSubtargetInfo &STI) const; 385 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op, 386 SmallVectorImpl<MCFixup> &Fixups, 387 const MCSubtargetInfo &STI) const; 388 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op, 389 SmallVectorImpl<MCFixup> &Fixups, 390 const MCSubtargetInfo &STI) const; 391 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op, 392 SmallVectorImpl<MCFixup> &Fixups, 393 const MCSubtargetInfo &STI) const; 394 395 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op, 396 SmallVectorImpl<MCFixup> &Fixups, 397 const MCSubtargetInfo &STI) const; 398 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op, 399 SmallVectorImpl<MCFixup> &Fixups, 400 const MCSubtargetInfo &STI) const; 401 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op, 402 SmallVectorImpl<MCFixup> &Fixups, 403 const MCSubtargetInfo &STI) const; 404 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op, 405 SmallVectorImpl<MCFixup> &Fixups, 406 const MCSubtargetInfo &STI) const; 407 408 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op, 409 SmallVectorImpl<MCFixup> &Fixups, 410 const MCSubtargetInfo &STI) const; 411 412 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI, 413 unsigned EncodedValue, 414 const MCSubtargetInfo &STI) const; 415 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI, 416 unsigned EncodedValue, 417 const MCSubtargetInfo &STI) const; 418 unsigned NEONThumb2DupPostEncoder(const MCInst &MI, 419 unsigned EncodedValue, 420 const MCSubtargetInfo &STI) const; 421 unsigned NEONThumb2V8PostEncoder(const MCInst &MI, 422 unsigned EncodedValue, 423 const MCSubtargetInfo &STI) const; 424 425 unsigned VFPThumb2PostEncoder(const MCInst &MI, 426 unsigned EncodedValue, 427 const MCSubtargetInfo &STI) const; 428 429 void EmitByte(unsigned char C, raw_ostream &OS) const { 430 OS << (char)C; 431 } 432 433 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const { 434 // Output the constant in little endian byte order. 435 for (unsigned i = 0; i != Size; ++i) { 436 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8; 437 EmitByte((Val >> Shift) & 0xff, OS); 438 } 439 } 440 441 void encodeInstruction(const MCInst &MI, raw_ostream &OS, 442 SmallVectorImpl<MCFixup> &Fixups, 443 const MCSubtargetInfo &STI) const override; 444 }; 445 446 } // end anonymous namespace 447 448 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing 449 /// instructions, and rewrite them to their Thumb2 form if we are currently in 450 /// Thumb2 mode. 451 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI, 452 unsigned EncodedValue, 453 const MCSubtargetInfo &STI) const { 454 if (isThumb2(STI)) { 455 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved 456 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are 457 // set to 1111. 458 unsigned Bit24 = EncodedValue & 0x01000000; 459 unsigned Bit28 = Bit24 << 4; 460 EncodedValue &= 0xEFFFFFFF; 461 EncodedValue |= Bit28; 462 EncodedValue |= 0x0F000000; 463 } 464 465 return EncodedValue; 466 } 467 468 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store 469 /// instructions, and rewrite them to their Thumb2 form if we are currently in 470 /// Thumb2 mode. 471 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI, 472 unsigned EncodedValue, 473 const MCSubtargetInfo &STI) const { 474 if (isThumb2(STI)) { 475 EncodedValue &= 0xF0FFFFFF; 476 EncodedValue |= 0x09000000; 477 } 478 479 return EncodedValue; 480 } 481 482 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup 483 /// instructions, and rewrite them to their Thumb2 form if we are currently in 484 /// Thumb2 mode. 485 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI, 486 unsigned EncodedValue, 487 const MCSubtargetInfo &STI) const { 488 if (isThumb2(STI)) { 489 EncodedValue &= 0x00FFFFFF; 490 EncodedValue |= 0xEE000000; 491 } 492 493 return EncodedValue; 494 } 495 496 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form 497 /// if we are in Thumb2. 498 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI, 499 unsigned EncodedValue, 500 const MCSubtargetInfo &STI) const { 501 if (isThumb2(STI)) { 502 EncodedValue |= 0xC000000; // Set bits 27-26 503 } 504 505 return EncodedValue; 506 } 507 508 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite 509 /// them to their Thumb2 form if we are currently in Thumb2 mode. 510 unsigned ARMMCCodeEmitter:: 511 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue, 512 const MCSubtargetInfo &STI) const { 513 if (isThumb2(STI)) { 514 EncodedValue &= 0x0FFFFFFF; 515 EncodedValue |= 0xE0000000; 516 } 517 return EncodedValue; 518 } 519 520 /// getMachineOpValue - Return binary encoding of operand. If the machine 521 /// operand requires relocation, record the relocation and return zero. 522 unsigned ARMMCCodeEmitter:: 523 getMachineOpValue(const MCInst &MI, const MCOperand &MO, 524 SmallVectorImpl<MCFixup> &Fixups, 525 const MCSubtargetInfo &STI) const { 526 if (MO.isReg()) { 527 unsigned Reg = MO.getReg(); 528 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg); 529 530 // Q registers are encoded as 2x their register number. 531 switch (Reg) { 532 default: 533 return RegNo; 534 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3: 535 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7: 536 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11: 537 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15: 538 return 2 * RegNo; 539 } 540 } else if (MO.isImm()) { 541 return static_cast<unsigned>(MO.getImm()); 542 } else if (MO.isFPImm()) { 543 return static_cast<unsigned>(APFloat(MO.getFPImm()) 544 .bitcastToAPInt().getHiBits(32).getLimitedValue()); 545 } 546 547 llvm_unreachable("Unable to encode MCOperand!"); 548 } 549 550 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand. 551 bool ARMMCCodeEmitter:: 552 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg, 553 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups, 554 const MCSubtargetInfo &STI) const { 555 const MCOperand &MO = MI.getOperand(OpIdx); 556 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 557 558 Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 559 560 int32_t SImm = MO1.getImm(); 561 bool isAdd = true; 562 563 // Special value for #-0 564 if (SImm == INT32_MIN) { 565 SImm = 0; 566 isAdd = false; 567 } 568 569 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 570 if (SImm < 0) { 571 SImm = -SImm; 572 isAdd = false; 573 } 574 575 Imm = SImm; 576 return isAdd; 577 } 578 579 /// getBranchTargetOpValue - Helper function to get the branch target operand, 580 /// which is either an immediate or requires a fixup. 581 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 582 unsigned FixupKind, 583 SmallVectorImpl<MCFixup> &Fixups, 584 const MCSubtargetInfo &STI) { 585 const MCOperand &MO = MI.getOperand(OpIdx); 586 587 // If the destination is an immediate, we have nothing to do. 588 if (MO.isImm()) return MO.getImm(); 589 assert(MO.isExpr() && "Unexpected branch target type!"); 590 const MCExpr *Expr = MO.getExpr(); 591 MCFixupKind Kind = MCFixupKind(FixupKind); 592 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 593 594 // All of the information is in the fixup. 595 return 0; 596 } 597 598 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are 599 // determined by negating them and XOR'ing them with bit 23. 600 static int32_t encodeThumbBLOffset(int32_t offset) { 601 offset >>= 1; 602 uint32_t S = (offset & 0x800000) >> 23; 603 uint32_t J1 = (offset & 0x400000) >> 22; 604 uint32_t J2 = (offset & 0x200000) >> 21; 605 J1 = (~J1 & 0x1); 606 J2 = (~J2 & 0x1); 607 J1 ^= S; 608 J2 ^= S; 609 610 offset &= ~0x600000; 611 offset |= J1 << 22; 612 offset |= J2 << 21; 613 614 return offset; 615 } 616 617 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target. 618 uint32_t ARMMCCodeEmitter:: 619 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx, 620 SmallVectorImpl<MCFixup> &Fixups, 621 const MCSubtargetInfo &STI) const { 622 const MCOperand MO = MI.getOperand(OpIdx); 623 if (MO.isExpr()) 624 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, 625 Fixups, STI); 626 return encodeThumbBLOffset(MO.getImm()); 627 } 628 629 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate 630 /// BLX branch target. 631 uint32_t ARMMCCodeEmitter:: 632 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx, 633 SmallVectorImpl<MCFixup> &Fixups, 634 const MCSubtargetInfo &STI) const { 635 const MCOperand MO = MI.getOperand(OpIdx); 636 if (MO.isExpr()) 637 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, 638 Fixups, STI); 639 return encodeThumbBLOffset(MO.getImm()); 640 } 641 642 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target. 643 uint32_t ARMMCCodeEmitter:: 644 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx, 645 SmallVectorImpl<MCFixup> &Fixups, 646 const MCSubtargetInfo &STI) const { 647 const MCOperand MO = MI.getOperand(OpIdx); 648 if (MO.isExpr()) 649 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, 650 Fixups, STI); 651 return (MO.getImm() >> 1); 652 } 653 654 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target. 655 uint32_t ARMMCCodeEmitter:: 656 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx, 657 SmallVectorImpl<MCFixup> &Fixups, 658 const MCSubtargetInfo &STI) const { 659 const MCOperand MO = MI.getOperand(OpIdx); 660 if (MO.isExpr()) 661 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, 662 Fixups, STI); 663 return (MO.getImm() >> 1); 664 } 665 666 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target. 667 uint32_t ARMMCCodeEmitter:: 668 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx, 669 SmallVectorImpl<MCFixup> &Fixups, 670 const MCSubtargetInfo &STI) const { 671 const MCOperand MO = MI.getOperand(OpIdx); 672 if (MO.isExpr()) 673 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups, STI); 674 return (MO.getImm() >> 1); 675 } 676 677 /// Return true if this branch has a non-always predication 678 static bool HasConditionalBranch(const MCInst &MI) { 679 int NumOp = MI.getNumOperands(); 680 if (NumOp >= 2) { 681 for (int i = 0; i < NumOp-1; ++i) { 682 const MCOperand &MCOp1 = MI.getOperand(i); 683 const MCOperand &MCOp2 = MI.getOperand(i + 1); 684 if (MCOp1.isImm() && MCOp2.isReg() && 685 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) { 686 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL) 687 return true; 688 } 689 } 690 } 691 return false; 692 } 693 694 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch 695 /// target. 696 uint32_t ARMMCCodeEmitter:: 697 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 698 SmallVectorImpl<MCFixup> &Fixups, 699 const MCSubtargetInfo &STI) const { 700 // FIXME: This really, really shouldn't use TargetMachine. We don't want 701 // coupling between MC and TM anywhere we can help it. 702 if (isThumb2(STI)) 703 return 704 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups, STI); 705 return getARMBranchTargetOpValue(MI, OpIdx, Fixups, STI); 706 } 707 708 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch 709 /// target. 710 uint32_t ARMMCCodeEmitter:: 711 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 712 SmallVectorImpl<MCFixup> &Fixups, 713 const MCSubtargetInfo &STI) const { 714 const MCOperand MO = MI.getOperand(OpIdx); 715 if (MO.isExpr()) { 716 if (HasConditionalBranch(MI)) 717 return ::getBranchTargetOpValue(MI, OpIdx, 718 ARM::fixup_arm_condbranch, Fixups, STI); 719 return ::getBranchTargetOpValue(MI, OpIdx, 720 ARM::fixup_arm_uncondbranch, Fixups, STI); 721 } 722 723 return MO.getImm() >> 2; 724 } 725 726 uint32_t ARMMCCodeEmitter:: 727 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx, 728 SmallVectorImpl<MCFixup> &Fixups, 729 const MCSubtargetInfo &STI) const { 730 const MCOperand MO = MI.getOperand(OpIdx); 731 if (MO.isExpr()) { 732 if (HasConditionalBranch(MI)) 733 return ::getBranchTargetOpValue(MI, OpIdx, 734 ARM::fixup_arm_condbl, Fixups, STI); 735 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups, STI); 736 } 737 738 return MO.getImm() >> 2; 739 } 740 741 uint32_t ARMMCCodeEmitter:: 742 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx, 743 SmallVectorImpl<MCFixup> &Fixups, 744 const MCSubtargetInfo &STI) const { 745 const MCOperand MO = MI.getOperand(OpIdx); 746 if (MO.isExpr()) 747 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups, STI); 748 749 return MO.getImm() >> 1; 750 } 751 752 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit 753 /// immediate branch target. 754 uint32_t ARMMCCodeEmitter::getThumbBranchTargetOpValue( 755 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups, 756 const MCSubtargetInfo &STI) const { 757 unsigned Val = 0; 758 const MCOperand MO = MI.getOperand(OpIdx); 759 760 if(MO.isExpr()) 761 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups, STI); 762 else 763 Val = MO.getImm() >> 1; 764 765 bool I = (Val & 0x800000); 766 bool J1 = (Val & 0x400000); 767 bool J2 = (Val & 0x200000); 768 if (I ^ J1) 769 Val &= ~0x400000; 770 else 771 Val |= 0x400000; 772 773 if (I ^ J2) 774 Val &= ~0x200000; 775 else 776 Val |= 0x200000; 777 778 return Val; 779 } 780 781 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate 782 /// ADR label target. 783 uint32_t ARMMCCodeEmitter:: 784 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 785 SmallVectorImpl<MCFixup> &Fixups, 786 const MCSubtargetInfo &STI) const { 787 const MCOperand MO = MI.getOperand(OpIdx); 788 if (MO.isExpr()) 789 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12, 790 Fixups, STI); 791 int64_t offset = MO.getImm(); 792 uint32_t Val = 0x2000; 793 794 int SoImmVal; 795 if (offset == INT32_MIN) { 796 Val = 0x1000; 797 SoImmVal = 0; 798 } else if (offset < 0) { 799 Val = 0x1000; 800 offset *= -1; 801 SoImmVal = ARM_AM::getSOImmVal(offset); 802 if(SoImmVal == -1) { 803 Val = 0x2000; 804 offset *= -1; 805 SoImmVal = ARM_AM::getSOImmVal(offset); 806 } 807 } else { 808 SoImmVal = ARM_AM::getSOImmVal(offset); 809 if(SoImmVal == -1) { 810 Val = 0x1000; 811 offset *= -1; 812 SoImmVal = ARM_AM::getSOImmVal(offset); 813 } 814 } 815 816 assert(SoImmVal != -1 && "Not a valid so_imm value!"); 817 818 Val |= SoImmVal; 819 return Val; 820 } 821 822 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label 823 /// target. 824 uint32_t ARMMCCodeEmitter:: 825 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 826 SmallVectorImpl<MCFixup> &Fixups, 827 const MCSubtargetInfo &STI) const { 828 const MCOperand MO = MI.getOperand(OpIdx); 829 if (MO.isExpr()) 830 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12, 831 Fixups, STI); 832 int32_t Val = MO.getImm(); 833 if (Val == INT32_MIN) 834 Val = 0x1000; 835 else if (Val < 0) { 836 Val *= -1; 837 Val |= 0x1000; 838 } 839 return Val; 840 } 841 842 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label 843 /// target. 844 uint32_t ARMMCCodeEmitter:: 845 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 846 SmallVectorImpl<MCFixup> &Fixups, 847 const MCSubtargetInfo &STI) const { 848 const MCOperand MO = MI.getOperand(OpIdx); 849 if (MO.isExpr()) 850 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10, 851 Fixups, STI); 852 return MO.getImm(); 853 } 854 855 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg' 856 /// operand. 857 uint32_t ARMMCCodeEmitter:: 858 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx, 859 SmallVectorImpl<MCFixup> &, 860 const MCSubtargetInfo &STI) const { 861 // [Rn, Rm] 862 // {5-3} = Rm 863 // {2-0} = Rn 864 const MCOperand &MO1 = MI.getOperand(OpIdx); 865 const MCOperand &MO2 = MI.getOperand(OpIdx + 1); 866 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg()); 867 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg()); 868 return (Rm << 3) | Rn; 869 } 870 871 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand. 872 uint32_t ARMMCCodeEmitter:: 873 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx, 874 SmallVectorImpl<MCFixup> &Fixups, 875 const MCSubtargetInfo &STI) const { 876 // {17-13} = reg 877 // {12} = (U)nsigned (add == '1', sub == '0') 878 // {11-0} = imm12 879 unsigned Reg, Imm12; 880 bool isAdd = true; 881 // If The first operand isn't a register, we have a label reference. 882 const MCOperand &MO = MI.getOperand(OpIdx); 883 if (!MO.isReg()) { 884 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC. 885 Imm12 = 0; 886 887 if (MO.isExpr()) { 888 const MCExpr *Expr = MO.getExpr(); 889 isAdd = false ; // 'U' bit is set as part of the fixup. 890 891 MCFixupKind Kind; 892 if (isThumb2(STI)) 893 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12); 894 else 895 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12); 896 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 897 898 ++MCNumCPRelocations; 899 } else { 900 Reg = ARM::PC; 901 int32_t Offset = MO.getImm(); 902 if (Offset == INT32_MIN) { 903 Offset = 0; 904 isAdd = false; 905 } else if (Offset < 0) { 906 Offset *= -1; 907 isAdd = false; 908 } 909 Imm12 = Offset; 910 } 911 } else 912 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups, STI); 913 914 uint32_t Binary = Imm12 & 0xfff; 915 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 916 if (isAdd) 917 Binary |= (1 << 12); 918 Binary |= (Reg << 13); 919 return Binary; 920 } 921 922 /// getT2Imm8s4OpValue - Return encoding info for 923 /// '+/- imm8<<2' operand. 924 uint32_t ARMMCCodeEmitter:: 925 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx, 926 SmallVectorImpl<MCFixup> &Fixups, 927 const MCSubtargetInfo &STI) const { 928 // FIXME: The immediate operand should have already been encoded like this 929 // before ever getting here. The encoder method should just need to combine 930 // the MI operands for the register and the offset into a single 931 // representation for the complex operand in the .td file. This isn't just 932 // style, unfortunately. As-is, we can't represent the distinct encoding 933 // for #-0. 934 935 // {8} = (U)nsigned (add == '1', sub == '0') 936 // {7-0} = imm8 937 int32_t Imm8 = MI.getOperand(OpIdx).getImm(); 938 bool isAdd = Imm8 >= 0; 939 940 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 941 if (Imm8 < 0) 942 Imm8 = -(uint32_t)Imm8; 943 944 // Scaled by 4. 945 Imm8 /= 4; 946 947 uint32_t Binary = Imm8 & 0xff; 948 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 949 if (isAdd) 950 Binary |= (1 << 8); 951 return Binary; 952 } 953 954 /// getT2AddrModeImm8s4OpValue - Return encoding info for 955 /// 'reg +/- imm8<<2' operand. 956 uint32_t ARMMCCodeEmitter:: 957 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx, 958 SmallVectorImpl<MCFixup> &Fixups, 959 const MCSubtargetInfo &STI) const { 960 // {12-9} = reg 961 // {8} = (U)nsigned (add == '1', sub == '0') 962 // {7-0} = imm8 963 unsigned Reg, Imm8; 964 bool isAdd = true; 965 // If The first operand isn't a register, we have a label reference. 966 const MCOperand &MO = MI.getOperand(OpIdx); 967 if (!MO.isReg()) { 968 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC. 969 Imm8 = 0; 970 isAdd = false ; // 'U' bit is set as part of the fixup. 971 972 assert(MO.isExpr() && "Unexpected machine operand type!"); 973 const MCExpr *Expr = MO.getExpr(); 974 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10); 975 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 976 977 ++MCNumCPRelocations; 978 } else 979 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI); 980 981 // FIXME: The immediate operand should have already been encoded like this 982 // before ever getting here. The encoder method should just need to combine 983 // the MI operands for the register and the offset into a single 984 // representation for the complex operand in the .td file. This isn't just 985 // style, unfortunately. As-is, we can't represent the distinct encoding 986 // for #-0. 987 uint32_t Binary = (Imm8 >> 2) & 0xff; 988 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 989 if (isAdd) 990 Binary |= (1 << 8); 991 Binary |= (Reg << 9); 992 return Binary; 993 } 994 995 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 996 /// 'reg + imm8<<2' operand. 997 uint32_t ARMMCCodeEmitter:: 998 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx, 999 SmallVectorImpl<MCFixup> &Fixups, 1000 const MCSubtargetInfo &STI) const { 1001 // {11-8} = reg 1002 // {7-0} = imm8 1003 const MCOperand &MO = MI.getOperand(OpIdx); 1004 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1005 unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1006 unsigned Imm8 = MO1.getImm(); 1007 return (Reg << 8) | Imm8; 1008 } 1009 1010 uint32_t 1011 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx, 1012 SmallVectorImpl<MCFixup> &Fixups, 1013 const MCSubtargetInfo &STI) const { 1014 // {20-16} = imm{15-12} 1015 // {11-0} = imm{11-0} 1016 const MCOperand &MO = MI.getOperand(OpIdx); 1017 if (MO.isImm()) 1018 // Hi / lo 16 bits already extracted during earlier passes. 1019 return static_cast<unsigned>(MO.getImm()); 1020 1021 // Handle :upper16: and :lower16: assembly prefixes. 1022 const MCExpr *E = MO.getExpr(); 1023 MCFixupKind Kind; 1024 if (E->getKind() == MCExpr::Target) { 1025 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E); 1026 E = ARM16Expr->getSubExpr(); 1027 1028 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(E)) { 1029 const int64_t Value = MCE->getValue(); 1030 if (Value > UINT32_MAX) 1031 report_fatal_error("constant value truncated (limited to 32-bit)"); 1032 1033 switch (ARM16Expr->getKind()) { 1034 case ARMMCExpr::VK_ARM_HI16: 1035 return (int32_t(Value) & 0xffff0000) >> 16; 1036 case ARMMCExpr::VK_ARM_LO16: 1037 return (int32_t(Value) & 0x0000ffff); 1038 default: llvm_unreachable("Unsupported ARMFixup"); 1039 } 1040 } 1041 1042 switch (ARM16Expr->getKind()) { 1043 default: llvm_unreachable("Unsupported ARMFixup"); 1044 case ARMMCExpr::VK_ARM_HI16: 1045 Kind = MCFixupKind(isThumb(STI) ? ARM::fixup_t2_movt_hi16 1046 : ARM::fixup_arm_movt_hi16); 1047 break; 1048 case ARMMCExpr::VK_ARM_LO16: 1049 Kind = MCFixupKind(isThumb(STI) ? ARM::fixup_t2_movw_lo16 1050 : ARM::fixup_arm_movw_lo16); 1051 break; 1052 } 1053 1054 Fixups.push_back(MCFixup::create(0, E, Kind, MI.getLoc())); 1055 return 0; 1056 } 1057 // If the expression doesn't have :upper16: or :lower16: on it, 1058 // it's just a plain immediate expression, previously those evaluated to 1059 // the lower 16 bits of the expression regardless of whether 1060 // we have a movt or a movw, but that led to misleadingly results. 1061 // This is disallowed in the AsmParser in validateInstruction() 1062 // so this should never happen. 1063 llvm_unreachable("expression without :upper16: or :lower16:"); 1064 } 1065 1066 uint32_t ARMMCCodeEmitter:: 1067 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx, 1068 SmallVectorImpl<MCFixup> &Fixups, 1069 const MCSubtargetInfo &STI) const { 1070 const MCOperand &MO = MI.getOperand(OpIdx); 1071 const MCOperand &MO1 = MI.getOperand(OpIdx+1); 1072 const MCOperand &MO2 = MI.getOperand(OpIdx+2); 1073 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1074 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg()); 1075 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()); 1076 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add; 1077 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm()); 1078 unsigned SBits = getShiftOp(ShOp); 1079 1080 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift 1081 // amount. However, it would be an easy mistake to make so check here. 1082 assert((ShImm & ~0x1f) == 0 && "Out of range shift amount"); 1083 1084 // {16-13} = Rn 1085 // {12} = isAdd 1086 // {11-0} = shifter 1087 // {3-0} = Rm 1088 // {4} = 0 1089 // {6-5} = type 1090 // {11-7} = imm 1091 uint32_t Binary = Rm; 1092 Binary |= Rn << 13; 1093 Binary |= SBits << 5; 1094 Binary |= ShImm << 7; 1095 if (isAdd) 1096 Binary |= 1 << 12; 1097 return Binary; 1098 } 1099 1100 uint32_t ARMMCCodeEmitter:: 1101 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx, 1102 SmallVectorImpl<MCFixup> &Fixups, 1103 const MCSubtargetInfo &STI) const { 1104 // {13} 1 == imm12, 0 == Rm 1105 // {12} isAdd 1106 // {11-0} imm12/Rm 1107 const MCOperand &MO = MI.getOperand(OpIdx); 1108 const MCOperand &MO1 = MI.getOperand(OpIdx+1); 1109 unsigned Imm = MO1.getImm(); 1110 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add; 1111 bool isReg = MO.getReg() != 0; 1112 uint32_t Binary = ARM_AM::getAM2Offset(Imm); 1113 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12 1114 if (isReg) { 1115 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm); 1116 Binary <<= 7; // Shift amount is bits [11:7] 1117 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5] 1118 Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0] 1119 } 1120 return Binary | (isAdd << 12) | (isReg << 13); 1121 } 1122 1123 uint32_t ARMMCCodeEmitter:: 1124 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx, 1125 SmallVectorImpl<MCFixup> &Fixups, 1126 const MCSubtargetInfo &STI) const { 1127 // {4} isAdd 1128 // {3-0} Rm 1129 const MCOperand &MO = MI.getOperand(OpIdx); 1130 const MCOperand &MO1 = MI.getOperand(OpIdx+1); 1131 bool isAdd = MO1.getImm() != 0; 1132 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4); 1133 } 1134 1135 uint32_t ARMMCCodeEmitter:: 1136 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx, 1137 SmallVectorImpl<MCFixup> &Fixups, 1138 const MCSubtargetInfo &STI) const { 1139 // {9} 1 == imm8, 0 == Rm 1140 // {8} isAdd 1141 // {7-4} imm7_4/zero 1142 // {3-0} imm3_0/Rm 1143 const MCOperand &MO = MI.getOperand(OpIdx); 1144 const MCOperand &MO1 = MI.getOperand(OpIdx+1); 1145 unsigned Imm = MO1.getImm(); 1146 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add; 1147 bool isImm = MO.getReg() == 0; 1148 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm); 1149 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8 1150 if (!isImm) 1151 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1152 return Imm8 | (isAdd << 8) | (isImm << 9); 1153 } 1154 1155 uint32_t ARMMCCodeEmitter:: 1156 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx, 1157 SmallVectorImpl<MCFixup> &Fixups, 1158 const MCSubtargetInfo &STI) const { 1159 // {13} 1 == imm8, 0 == Rm 1160 // {12-9} Rn 1161 // {8} isAdd 1162 // {7-4} imm7_4/zero 1163 // {3-0} imm3_0/Rm 1164 const MCOperand &MO = MI.getOperand(OpIdx); 1165 const MCOperand &MO1 = MI.getOperand(OpIdx+1); 1166 const MCOperand &MO2 = MI.getOperand(OpIdx+2); 1167 1168 // If The first operand isn't a register, we have a label reference. 1169 if (!MO.isReg()) { 1170 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC. 1171 1172 assert(MO.isExpr() && "Unexpected machine operand type!"); 1173 const MCExpr *Expr = MO.getExpr(); 1174 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled); 1175 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 1176 1177 ++MCNumCPRelocations; 1178 return (Rn << 9) | (1 << 13); 1179 } 1180 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1181 unsigned Imm = MO2.getImm(); 1182 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add; 1183 bool isImm = MO1.getReg() == 0; 1184 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm); 1185 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8 1186 if (!isImm) 1187 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg()); 1188 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13); 1189 } 1190 1191 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands. 1192 uint32_t ARMMCCodeEmitter:: 1193 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx, 1194 SmallVectorImpl<MCFixup> &Fixups, 1195 const MCSubtargetInfo &STI) const { 1196 // [SP, #imm] 1197 // {7-0} = imm8 1198 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1199 assert(MI.getOperand(OpIdx).getReg() == ARM::SP && 1200 "Unexpected base register!"); 1201 1202 // The immediate is already shifted for the implicit zeroes, so no change 1203 // here. 1204 return MO1.getImm() & 0xff; 1205 } 1206 1207 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands. 1208 uint32_t ARMMCCodeEmitter:: 1209 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx, 1210 SmallVectorImpl<MCFixup> &Fixups, 1211 const MCSubtargetInfo &STI) const { 1212 // [Rn, #imm] 1213 // {7-3} = imm5 1214 // {2-0} = Rn 1215 const MCOperand &MO = MI.getOperand(OpIdx); 1216 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1217 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1218 unsigned Imm5 = MO1.getImm(); 1219 return ((Imm5 & 0x1f) << 3) | Rn; 1220 } 1221 1222 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands. 1223 uint32_t ARMMCCodeEmitter:: 1224 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx, 1225 SmallVectorImpl<MCFixup> &Fixups, 1226 const MCSubtargetInfo &STI) const { 1227 const MCOperand MO = MI.getOperand(OpIdx); 1228 if (MO.isExpr()) 1229 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups, STI); 1230 return (MO.getImm() >> 2); 1231 } 1232 1233 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand. 1234 uint32_t ARMMCCodeEmitter:: 1235 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx, 1236 SmallVectorImpl<MCFixup> &Fixups, 1237 const MCSubtargetInfo &STI) const { 1238 // {12-9} = reg 1239 // {8} = (U)nsigned (add == '1', sub == '0') 1240 // {7-0} = imm8 1241 unsigned Reg, Imm8; 1242 bool isAdd; 1243 // If The first operand isn't a register, we have a label reference. 1244 const MCOperand &MO = MI.getOperand(OpIdx); 1245 if (!MO.isReg()) { 1246 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC. 1247 Imm8 = 0; 1248 isAdd = false; // 'U' bit is handled as part of the fixup. 1249 1250 assert(MO.isExpr() && "Unexpected machine operand type!"); 1251 const MCExpr *Expr = MO.getExpr(); 1252 MCFixupKind Kind; 1253 if (isThumb2(STI)) 1254 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10); 1255 else 1256 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10); 1257 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 1258 1259 ++MCNumCPRelocations; 1260 } else { 1261 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI); 1262 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add; 1263 } 1264 1265 uint32_t Binary = ARM_AM::getAM5Offset(Imm8); 1266 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 1267 if (isAdd) 1268 Binary |= (1 << 8); 1269 Binary |= (Reg << 9); 1270 return Binary; 1271 } 1272 1273 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand. 1274 uint32_t ARMMCCodeEmitter:: 1275 getAddrMode5FP16OpValue(const MCInst &MI, unsigned OpIdx, 1276 SmallVectorImpl<MCFixup> &Fixups, 1277 const MCSubtargetInfo &STI) const { 1278 // {12-9} = reg 1279 // {8} = (U)nsigned (add == '1', sub == '0') 1280 // {7-0} = imm8 1281 unsigned Reg, Imm8; 1282 bool isAdd; 1283 // If The first operand isn't a register, we have a label reference. 1284 const MCOperand &MO = MI.getOperand(OpIdx); 1285 if (!MO.isReg()) { 1286 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC. 1287 Imm8 = 0; 1288 isAdd = false; // 'U' bit is handled as part of the fixup. 1289 1290 assert(MO.isExpr() && "Unexpected machine operand type!"); 1291 const MCExpr *Expr = MO.getExpr(); 1292 MCFixupKind Kind; 1293 if (isThumb2(STI)) 1294 Kind = MCFixupKind(ARM::fixup_t2_pcrel_9); 1295 else 1296 Kind = MCFixupKind(ARM::fixup_arm_pcrel_9); 1297 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 1298 1299 ++MCNumCPRelocations; 1300 } else { 1301 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI); 1302 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add; 1303 } 1304 1305 uint32_t Binary = ARM_AM::getAM5Offset(Imm8); 1306 // Immediate is always encoded as positive. The 'U' bit controls add vs sub. 1307 if (isAdd) 1308 Binary |= (1 << 8); 1309 Binary |= (Reg << 9); 1310 return Binary; 1311 } 1312 1313 unsigned ARMMCCodeEmitter:: 1314 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx, 1315 SmallVectorImpl<MCFixup> &Fixups, 1316 const MCSubtargetInfo &STI) const { 1317 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be 1318 // shifted. The second is Rs, the amount to shift by, and the third specifies 1319 // the type of the shift. 1320 // 1321 // {3-0} = Rm. 1322 // {4} = 1 1323 // {6-5} = type 1324 // {11-8} = Rs 1325 // {7} = 0 1326 1327 const MCOperand &MO = MI.getOperand(OpIdx); 1328 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1329 const MCOperand &MO2 = MI.getOperand(OpIdx + 2); 1330 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); 1331 1332 // Encode Rm. 1333 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1334 1335 // Encode the shift opcode. 1336 unsigned SBits = 0; 1337 unsigned Rs = MO1.getReg(); 1338 if (Rs) { 1339 // Set shift operand (bit[7:4]). 1340 // LSL - 0001 1341 // LSR - 0011 1342 // ASR - 0101 1343 // ROR - 0111 1344 switch (SOpc) { 1345 default: llvm_unreachable("Unknown shift opc!"); 1346 case ARM_AM::lsl: SBits = 0x1; break; 1347 case ARM_AM::lsr: SBits = 0x3; break; 1348 case ARM_AM::asr: SBits = 0x5; break; 1349 case ARM_AM::ror: SBits = 0x7; break; 1350 } 1351 } 1352 1353 Binary |= SBits << 4; 1354 1355 // Encode the shift operation Rs. 1356 // Encode Rs bit[11:8]. 1357 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); 1358 return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift); 1359 } 1360 1361 unsigned ARMMCCodeEmitter:: 1362 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx, 1363 SmallVectorImpl<MCFixup> &Fixups, 1364 const MCSubtargetInfo &STI) const { 1365 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be 1366 // shifted. The second is the amount to shift by. 1367 // 1368 // {3-0} = Rm. 1369 // {4} = 0 1370 // {6-5} = type 1371 // {11-7} = imm 1372 1373 const MCOperand &MO = MI.getOperand(OpIdx); 1374 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1375 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm()); 1376 1377 // Encode Rm. 1378 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1379 1380 // Encode the shift opcode. 1381 unsigned SBits = 0; 1382 1383 // Set shift operand (bit[6:4]). 1384 // LSL - 000 1385 // LSR - 010 1386 // ASR - 100 1387 // ROR - 110 1388 // RRX - 110 and bit[11:8] clear. 1389 switch (SOpc) { 1390 default: llvm_unreachable("Unknown shift opc!"); 1391 case ARM_AM::lsl: SBits = 0x0; break; 1392 case ARM_AM::lsr: SBits = 0x2; break; 1393 case ARM_AM::asr: SBits = 0x4; break; 1394 case ARM_AM::ror: SBits = 0x6; break; 1395 case ARM_AM::rrx: 1396 Binary |= 0x60; 1397 return Binary; 1398 } 1399 1400 // Encode shift_imm bit[11:7]. 1401 Binary |= SBits << 4; 1402 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm()); 1403 assert(Offset < 32 && "Offset must be in range 0-31!"); 1404 return Binary | (Offset << 7); 1405 } 1406 1407 1408 unsigned ARMMCCodeEmitter:: 1409 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum, 1410 SmallVectorImpl<MCFixup> &Fixups, 1411 const MCSubtargetInfo &STI) const { 1412 const MCOperand &MO1 = MI.getOperand(OpNum); 1413 const MCOperand &MO2 = MI.getOperand(OpNum+1); 1414 const MCOperand &MO3 = MI.getOperand(OpNum+2); 1415 1416 // Encoded as [Rn, Rm, imm]. 1417 // FIXME: Needs fixup support. 1418 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg()); 1419 Value <<= 4; 1420 Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg()); 1421 Value <<= 2; 1422 Value |= MO3.getImm(); 1423 1424 return Value; 1425 } 1426 1427 unsigned ARMMCCodeEmitter:: 1428 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum, 1429 SmallVectorImpl<MCFixup> &Fixups, 1430 const MCSubtargetInfo &STI) const { 1431 const MCOperand &MO1 = MI.getOperand(OpNum); 1432 const MCOperand &MO2 = MI.getOperand(OpNum+1); 1433 1434 // FIXME: Needs fixup support. 1435 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg()); 1436 1437 // Even though the immediate is 8 bits long, we need 9 bits in order 1438 // to represent the (inverse of the) sign bit. 1439 Value <<= 9; 1440 int32_t tmp = (int32_t)MO2.getImm(); 1441 if (tmp < 0) 1442 tmp = abs(tmp); 1443 else 1444 Value |= 256; // Set the ADD bit 1445 Value |= tmp & 255; 1446 return Value; 1447 } 1448 1449 unsigned ARMMCCodeEmitter:: 1450 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum, 1451 SmallVectorImpl<MCFixup> &Fixups, 1452 const MCSubtargetInfo &STI) const { 1453 const MCOperand &MO1 = MI.getOperand(OpNum); 1454 1455 // FIXME: Needs fixup support. 1456 unsigned Value = 0; 1457 int32_t tmp = (int32_t)MO1.getImm(); 1458 if (tmp < 0) 1459 tmp = abs(tmp); 1460 else 1461 Value |= 256; // Set the ADD bit 1462 Value |= tmp & 255; 1463 return Value; 1464 } 1465 1466 unsigned ARMMCCodeEmitter:: 1467 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx, 1468 SmallVectorImpl<MCFixup> &Fixups, 1469 const MCSubtargetInfo &STI) const { 1470 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be 1471 // shifted. The second is the amount to shift by. 1472 // 1473 // {3-0} = Rm. 1474 // {4} = 0 1475 // {6-5} = type 1476 // {11-7} = imm 1477 1478 const MCOperand &MO = MI.getOperand(OpIdx); 1479 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 1480 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm()); 1481 1482 // Encode Rm. 1483 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1484 1485 // Encode the shift opcode. 1486 unsigned SBits = 0; 1487 // Set shift operand (bit[6:4]). 1488 // LSL - 000 1489 // LSR - 010 1490 // ASR - 100 1491 // ROR - 110 1492 switch (SOpc) { 1493 default: llvm_unreachable("Unknown shift opc!"); 1494 case ARM_AM::lsl: SBits = 0x0; break; 1495 case ARM_AM::lsr: SBits = 0x2; break; 1496 case ARM_AM::asr: SBits = 0x4; break; 1497 case ARM_AM::rrx: LLVM_FALLTHROUGH; 1498 case ARM_AM::ror: SBits = 0x6; break; 1499 } 1500 1501 Binary |= SBits << 4; 1502 if (SOpc == ARM_AM::rrx) 1503 return Binary; 1504 1505 // Encode shift_imm bit[11:7]. 1506 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7; 1507 } 1508 1509 unsigned ARMMCCodeEmitter:: 1510 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op, 1511 SmallVectorImpl<MCFixup> &Fixups, 1512 const MCSubtargetInfo &STI) const { 1513 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the 1514 // msb of the mask. 1515 const MCOperand &MO = MI.getOperand(Op); 1516 uint32_t v = ~MO.getImm(); 1517 uint32_t lsb = countTrailingZeros(v); 1518 uint32_t msb = (32 - countLeadingZeros (v)) - 1; 1519 assert(v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!"); 1520 return lsb | (msb << 5); 1521 } 1522 1523 unsigned ARMMCCodeEmitter:: 1524 getRegisterListOpValue(const MCInst &MI, unsigned Op, 1525 SmallVectorImpl<MCFixup> &Fixups, 1526 const MCSubtargetInfo &STI) const { 1527 // VLDM/VSTM: 1528 // {12-8} = Vd 1529 // {7-0} = Number of registers 1530 // 1531 // LDM/STM: 1532 // {15-0} = Bitfield of GPRs. 1533 unsigned Reg = MI.getOperand(Op).getReg(); 1534 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg); 1535 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg); 1536 1537 unsigned Binary = 0; 1538 1539 if (SPRRegs || DPRRegs) { 1540 // VLDM/VSTM 1541 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg); 1542 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff; 1543 Binary |= (RegNo & 0x1f) << 8; 1544 if (SPRRegs) 1545 Binary |= NumRegs; 1546 else 1547 Binary |= NumRegs * 2; 1548 } else { 1549 const MCRegisterInfo &MRI = *CTX.getRegisterInfo(); 1550 assert(std::is_sorted(MI.begin() + Op, MI.end(), 1551 [&](const MCOperand &LHS, const MCOperand &RHS) { 1552 return MRI.getEncodingValue(LHS.getReg()) < 1553 MRI.getEncodingValue(RHS.getReg()); 1554 })); 1555 1556 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) { 1557 unsigned RegNo = MRI.getEncodingValue(MI.getOperand(I).getReg()); 1558 Binary |= 1 << RegNo; 1559 } 1560 } 1561 1562 return Binary; 1563 } 1564 1565 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along 1566 /// with the alignment operand. 1567 unsigned ARMMCCodeEmitter:: 1568 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op, 1569 SmallVectorImpl<MCFixup> &Fixups, 1570 const MCSubtargetInfo &STI) const { 1571 const MCOperand &Reg = MI.getOperand(Op); 1572 const MCOperand &Imm = MI.getOperand(Op + 1); 1573 1574 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg()); 1575 unsigned Align = 0; 1576 1577 switch (Imm.getImm()) { 1578 default: break; 1579 case 2: 1580 case 4: 1581 case 8: Align = 0x01; break; 1582 case 16: Align = 0x02; break; 1583 case 32: Align = 0x03; break; 1584 } 1585 1586 return RegNo | (Align << 4); 1587 } 1588 1589 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number 1590 /// along with the alignment operand for use in VST1 and VLD1 with size 32. 1591 unsigned ARMMCCodeEmitter:: 1592 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op, 1593 SmallVectorImpl<MCFixup> &Fixups, 1594 const MCSubtargetInfo &STI) const { 1595 const MCOperand &Reg = MI.getOperand(Op); 1596 const MCOperand &Imm = MI.getOperand(Op + 1); 1597 1598 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg()); 1599 unsigned Align = 0; 1600 1601 switch (Imm.getImm()) { 1602 default: break; 1603 case 8: 1604 case 16: 1605 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes. 1606 case 2: Align = 0x00; break; 1607 case 4: Align = 0x03; break; 1608 } 1609 1610 return RegNo | (Align << 4); 1611 } 1612 1613 1614 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and 1615 /// alignment operand for use in VLD-dup instructions. This is the same as 1616 /// getAddrMode6AddressOpValue except for the alignment encoding, which is 1617 /// different for VLD4-dup. 1618 unsigned ARMMCCodeEmitter:: 1619 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op, 1620 SmallVectorImpl<MCFixup> &Fixups, 1621 const MCSubtargetInfo &STI) const { 1622 const MCOperand &Reg = MI.getOperand(Op); 1623 const MCOperand &Imm = MI.getOperand(Op + 1); 1624 1625 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg()); 1626 unsigned Align = 0; 1627 1628 switch (Imm.getImm()) { 1629 default: break; 1630 case 2: 1631 case 4: 1632 case 8: Align = 0x01; break; 1633 case 16: Align = 0x03; break; 1634 } 1635 1636 return RegNo | (Align << 4); 1637 } 1638 1639 unsigned ARMMCCodeEmitter:: 1640 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op, 1641 SmallVectorImpl<MCFixup> &Fixups, 1642 const MCSubtargetInfo &STI) const { 1643 const MCOperand &MO = MI.getOperand(Op); 1644 if (MO.getReg() == 0) return 0x0D; 1645 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 1646 } 1647 1648 unsigned ARMMCCodeEmitter:: 1649 getShiftRight8Imm(const MCInst &MI, unsigned Op, 1650 SmallVectorImpl<MCFixup> &Fixups, 1651 const MCSubtargetInfo &STI) const { 1652 return 8 - MI.getOperand(Op).getImm(); 1653 } 1654 1655 unsigned ARMMCCodeEmitter:: 1656 getShiftRight16Imm(const MCInst &MI, unsigned Op, 1657 SmallVectorImpl<MCFixup> &Fixups, 1658 const MCSubtargetInfo &STI) const { 1659 return 16 - MI.getOperand(Op).getImm(); 1660 } 1661 1662 unsigned ARMMCCodeEmitter:: 1663 getShiftRight32Imm(const MCInst &MI, unsigned Op, 1664 SmallVectorImpl<MCFixup> &Fixups, 1665 const MCSubtargetInfo &STI) const { 1666 return 32 - MI.getOperand(Op).getImm(); 1667 } 1668 1669 unsigned ARMMCCodeEmitter:: 1670 getShiftRight64Imm(const MCInst &MI, unsigned Op, 1671 SmallVectorImpl<MCFixup> &Fixups, 1672 const MCSubtargetInfo &STI) const { 1673 return 64 - MI.getOperand(Op).getImm(); 1674 } 1675 1676 void ARMMCCodeEmitter:: 1677 encodeInstruction(const MCInst &MI, raw_ostream &OS, 1678 SmallVectorImpl<MCFixup> &Fixups, 1679 const MCSubtargetInfo &STI) const { 1680 // Pseudo instructions don't get encoded. 1681 const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); 1682 uint64_t TSFlags = Desc.TSFlags; 1683 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo) 1684 return; 1685 1686 int Size; 1687 if (Desc.getSize() == 2 || Desc.getSize() == 4) 1688 Size = Desc.getSize(); 1689 else 1690 llvm_unreachable("Unexpected instruction size!"); 1691 1692 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups, STI); 1693 // Thumb 32-bit wide instructions need to emit the high order halfword 1694 // first. 1695 if (isThumb(STI) && Size == 4) { 1696 EmitConstant(Binary >> 16, 2, OS); 1697 EmitConstant(Binary & 0xffff, 2, OS); 1698 } else 1699 EmitConstant(Binary, Size, OS); 1700 ++MCNumEmitted; // Keep track of the # of mi's emitted. 1701 } 1702 1703 #include "ARMGenMCCodeEmitter.inc" 1704 1705 MCCodeEmitter *llvm::createARMLEMCCodeEmitter(const MCInstrInfo &MCII, 1706 const MCRegisterInfo &MRI, 1707 MCContext &Ctx) { 1708 return new ARMMCCodeEmitter(MCII, Ctx, true); 1709 } 1710 1711 MCCodeEmitter *llvm::createARMBEMCCodeEmitter(const MCInstrInfo &MCII, 1712 const MCRegisterInfo &MRI, 1713 MCContext &Ctx) { 1714 return new ARMMCCodeEmitter(MCII, Ctx, false); 1715 } 1716