1 //=- AArch64/AArch64MCCodeEmitter.cpp - Convert AArch64 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 AArch64MCCodeEmitter class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/AArch64AddressingModes.h" 15 #include "MCTargetDesc/AArch64FixupKinds.h" 16 #include "MCTargetDesc/AArch64MCExpr.h" 17 #include "Utils/AArch64BaseInfo.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/MC/MCCodeEmitter.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCFixup.h" 23 #include "llvm/MC/MCInst.h" 24 #include "llvm/MC/MCInstrInfo.h" 25 #include "llvm/MC/MCRegisterInfo.h" 26 #include "llvm/MC/MCSubtargetInfo.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/Endian.h" 29 #include "llvm/Support/EndianStream.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <cassert> 33 #include <cstdint> 34 35 using namespace llvm; 36 37 #define DEBUG_TYPE "mccodeemitter" 38 39 STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); 40 STATISTIC(MCNumFixups, "Number of MC fixups created."); 41 42 namespace { 43 44 class AArch64MCCodeEmitter : public MCCodeEmitter { 45 MCContext &Ctx; 46 const MCInstrInfo &MCII; 47 48 public: 49 AArch64MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx) 50 : Ctx(ctx), MCII(mcii) {} 51 AArch64MCCodeEmitter(const AArch64MCCodeEmitter &) = delete; 52 void operator=(const AArch64MCCodeEmitter &) = delete; 53 ~AArch64MCCodeEmitter() override = default; 54 55 // getBinaryCodeForInstr - TableGen'erated function for getting the 56 // binary encoding for an instruction. 57 uint64_t getBinaryCodeForInstr(const MCInst &MI, 58 SmallVectorImpl<MCFixup> &Fixups, 59 const MCSubtargetInfo &STI) const; 60 61 /// getMachineOpValue - Return binary encoding of operand. If the machine 62 /// operand requires relocation, record the relocation and return zero. 63 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, 64 SmallVectorImpl<MCFixup> &Fixups, 65 const MCSubtargetInfo &STI) const; 66 67 /// getLdStUImm12OpValue - Return encoding info for 12-bit unsigned immediate 68 /// attached to a load, store or prfm instruction. If operand requires a 69 /// relocation, record it and return zero in that part of the encoding. 70 template <uint32_t FixupKind> 71 uint32_t getLdStUImm12OpValue(const MCInst &MI, unsigned OpIdx, 72 SmallVectorImpl<MCFixup> &Fixups, 73 const MCSubtargetInfo &STI) const; 74 75 /// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label 76 /// target. 77 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 78 SmallVectorImpl<MCFixup> &Fixups, 79 const MCSubtargetInfo &STI) const; 80 81 /// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and 82 /// the 2-bit shift field. 83 uint32_t getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx, 84 SmallVectorImpl<MCFixup> &Fixups, 85 const MCSubtargetInfo &STI) const; 86 87 /// getAuthSImmOpValue - Return encoding for a 10-bit immediate which is 88 /// encoded as a sign bit and an 9-bit immediate, separated by a single bit. 89 uint32_t getAuthSImmOpValue(const MCInst &MI, unsigned OpIdx, 90 SmallVectorImpl<MCFixup> &Fixups, 91 const MCSubtargetInfo &STI) const; 92 93 /// getCondBranchTargetOpValue - Return the encoded value for a conditional 94 /// branch target. 95 uint32_t getCondBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 96 SmallVectorImpl<MCFixup> &Fixups, 97 const MCSubtargetInfo &STI) const; 98 99 /// getLoadLiteralOpValue - Return the encoded value for a load-literal 100 /// pc-relative address. 101 uint32_t getLoadLiteralOpValue(const MCInst &MI, unsigned OpIdx, 102 SmallVectorImpl<MCFixup> &Fixups, 103 const MCSubtargetInfo &STI) const; 104 105 /// getMemExtendOpValue - Return the encoded value for a reg-extend load/store 106 /// instruction: bit 0 is whether a shift is present, bit 1 is whether the 107 /// operation is a sign extend (as opposed to a zero extend). 108 uint32_t getMemExtendOpValue(const MCInst &MI, unsigned OpIdx, 109 SmallVectorImpl<MCFixup> &Fixups, 110 const MCSubtargetInfo &STI) const; 111 112 /// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and- 113 /// branch target. 114 uint32_t getTestBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 115 SmallVectorImpl<MCFixup> &Fixups, 116 const MCSubtargetInfo &STI) const; 117 118 /// getBranchTargetOpValue - Return the encoded value for an unconditional 119 /// branch target. 120 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 121 SmallVectorImpl<MCFixup> &Fixups, 122 const MCSubtargetInfo &STI) const; 123 124 /// getMoveWideImmOpValue - Return the encoded value for the immediate operand 125 /// of a MOVZ or MOVK instruction. 126 uint32_t getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx, 127 SmallVectorImpl<MCFixup> &Fixups, 128 const MCSubtargetInfo &STI) const; 129 130 /// getVecShifterOpValue - Return the encoded value for the vector shifter. 131 uint32_t getVecShifterOpValue(const MCInst &MI, unsigned OpIdx, 132 SmallVectorImpl<MCFixup> &Fixups, 133 const MCSubtargetInfo &STI) const; 134 135 /// getMoveVecShifterOpValue - Return the encoded value for the vector move 136 /// shifter (MSL). 137 uint32_t getMoveVecShifterOpValue(const MCInst &MI, unsigned OpIdx, 138 SmallVectorImpl<MCFixup> &Fixups, 139 const MCSubtargetInfo &STI) const; 140 141 /// getFixedPointScaleOpValue - Return the encoded value for the 142 // FP-to-fixed-point scale factor. 143 uint32_t getFixedPointScaleOpValue(const MCInst &MI, unsigned OpIdx, 144 SmallVectorImpl<MCFixup> &Fixups, 145 const MCSubtargetInfo &STI) const; 146 147 uint32_t getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx, 148 SmallVectorImpl<MCFixup> &Fixups, 149 const MCSubtargetInfo &STI) const; 150 uint32_t getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx, 151 SmallVectorImpl<MCFixup> &Fixups, 152 const MCSubtargetInfo &STI) const; 153 uint32_t getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx, 154 SmallVectorImpl<MCFixup> &Fixups, 155 const MCSubtargetInfo &STI) const; 156 uint32_t getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx, 157 SmallVectorImpl<MCFixup> &Fixups, 158 const MCSubtargetInfo &STI) const; 159 uint32_t getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx, 160 SmallVectorImpl<MCFixup> &Fixups, 161 const MCSubtargetInfo &STI) const; 162 uint32_t getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx, 163 SmallVectorImpl<MCFixup> &Fixups, 164 const MCSubtargetInfo &STI) const; 165 uint32_t getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx, 166 SmallVectorImpl<MCFixup> &Fixups, 167 const MCSubtargetInfo &STI) const; 168 uint32_t getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx, 169 SmallVectorImpl<MCFixup> &Fixups, 170 const MCSubtargetInfo &STI) const; 171 172 unsigned fixMOVZ(const MCInst &MI, unsigned EncodedValue, 173 const MCSubtargetInfo &STI) const; 174 175 void encodeInstruction(const MCInst &MI, raw_ostream &OS, 176 SmallVectorImpl<MCFixup> &Fixups, 177 const MCSubtargetInfo &STI) const override; 178 179 unsigned fixMulHigh(const MCInst &MI, unsigned EncodedValue, 180 const MCSubtargetInfo &STI) const; 181 182 template<int hasRs, int hasRt2> unsigned 183 fixLoadStoreExclusive(const MCInst &MI, unsigned EncodedValue, 184 const MCSubtargetInfo &STI) const; 185 186 unsigned fixOneOperandFPComparison(const MCInst &MI, unsigned EncodedValue, 187 const MCSubtargetInfo &STI) const; 188 189 private: 190 uint64_t computeAvailableFeatures(const FeatureBitset &FB) const; 191 void verifyInstructionPredicates(const MCInst &MI, 192 uint64_t AvailableFeatures) const; 193 }; 194 195 } // end anonymous namespace 196 197 /// getMachineOpValue - Return binary encoding of operand. If the machine 198 /// operand requires relocation, record the relocation and return zero. 199 unsigned 200 AArch64MCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO, 201 SmallVectorImpl<MCFixup> &Fixups, 202 const MCSubtargetInfo &STI) const { 203 if (MO.isReg()) 204 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()); 205 206 assert(MO.isImm() && "did not expect relocated expression"); 207 return static_cast<unsigned>(MO.getImm()); 208 } 209 210 template<unsigned FixupKind> uint32_t 211 AArch64MCCodeEmitter::getLdStUImm12OpValue(const MCInst &MI, unsigned OpIdx, 212 SmallVectorImpl<MCFixup> &Fixups, 213 const MCSubtargetInfo &STI) const { 214 const MCOperand &MO = MI.getOperand(OpIdx); 215 uint32_t ImmVal = 0; 216 217 if (MO.isImm()) 218 ImmVal = static_cast<uint32_t>(MO.getImm()); 219 else { 220 assert(MO.isExpr() && "unable to encode load/store imm operand"); 221 MCFixupKind Kind = MCFixupKind(FixupKind); 222 Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 223 ++MCNumFixups; 224 } 225 226 return ImmVal; 227 } 228 229 /// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label 230 /// target. 231 uint32_t 232 AArch64MCCodeEmitter::getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx, 233 SmallVectorImpl<MCFixup> &Fixups, 234 const MCSubtargetInfo &STI) const { 235 const MCOperand &MO = MI.getOperand(OpIdx); 236 237 // If the destination is an immediate, we have nothing to do. 238 if (MO.isImm()) 239 return MO.getImm(); 240 assert(MO.isExpr() && "Unexpected target type!"); 241 const MCExpr *Expr = MO.getExpr(); 242 243 MCFixupKind Kind = MI.getOpcode() == AArch64::ADR 244 ? MCFixupKind(AArch64::fixup_aarch64_pcrel_adr_imm21) 245 : MCFixupKind(AArch64::fixup_aarch64_pcrel_adrp_imm21); 246 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 247 248 MCNumFixups += 1; 249 250 // All of the information is in the fixup. 251 return 0; 252 } 253 254 /// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and 255 /// the 2-bit shift field. The shift field is stored in bits 13-14 of the 256 /// return value. 257 uint32_t 258 AArch64MCCodeEmitter::getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx, 259 SmallVectorImpl<MCFixup> &Fixups, 260 const MCSubtargetInfo &STI) const { 261 // Suboperands are [imm, shifter]. 262 const MCOperand &MO = MI.getOperand(OpIdx); 263 const MCOperand &MO1 = MI.getOperand(OpIdx + 1); 264 assert(AArch64_AM::getShiftType(MO1.getImm()) == AArch64_AM::LSL && 265 "unexpected shift type for add/sub immediate"); 266 unsigned ShiftVal = AArch64_AM::getShiftValue(MO1.getImm()); 267 assert((ShiftVal == 0 || ShiftVal == 12) && 268 "unexpected shift value for add/sub immediate"); 269 if (MO.isImm()) 270 return MO.getImm() | (ShiftVal == 0 ? 0 : (1 << ShiftVal)); 271 assert(MO.isExpr() && "Unable to encode MCOperand!"); 272 const MCExpr *Expr = MO.getExpr(); 273 274 // Encode the 12 bits of the fixup. 275 MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_add_imm12); 276 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc())); 277 278 ++MCNumFixups; 279 280 // Set the shift bit of the add instruction for relocation types 281 // R_AARCH64_TLSLE_ADD_TPREL_HI12 and R_AARCH64_TLSLD_ADD_DTPREL_HI12. 282 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) { 283 AArch64MCExpr::VariantKind RefKind = A64E->getKind(); 284 if (RefKind == AArch64MCExpr::VK_TPREL_HI12 || 285 RefKind == AArch64MCExpr::VK_DTPREL_HI12) 286 ShiftVal = 12; 287 } 288 return ShiftVal == 0 ? 0 : (1 << ShiftVal); 289 } 290 291 /// getAuthSImmOpValue - encode a 10-bit scale signed immediate within an 11-bit 292 /// field in which bit 10 of that field is not used to encode any information 293 /// about the immediate. 294 uint32_t 295 AArch64MCCodeEmitter::getAuthSImmOpValue(const MCInst &MI, unsigned OpIdx, 296 SmallVectorImpl<MCFixup> &Fixups, 297 const MCSubtargetInfo &STI) const { 298 const MCOperand &MO = MI.getOperand(OpIdx); 299 int32_t offset = MO.getImm(); 300 return (uint32_t)offset; 301 } 302 303 /// getCondBranchTargetOpValue - Return the encoded value for a conditional 304 /// branch target. 305 uint32_t AArch64MCCodeEmitter::getCondBranchTargetOpValue( 306 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups, 307 const MCSubtargetInfo &STI) const { 308 const MCOperand &MO = MI.getOperand(OpIdx); 309 310 // If the destination is an immediate, we have nothing to do. 311 if (MO.isImm()) 312 return MO.getImm(); 313 assert(MO.isExpr() && "Unexpected target type!"); 314 315 MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_pcrel_branch19); 316 Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 317 318 ++MCNumFixups; 319 320 // All of the information is in the fixup. 321 return 0; 322 } 323 324 /// getLoadLiteralOpValue - Return the encoded value for a load-literal 325 /// pc-relative address. 326 uint32_t 327 AArch64MCCodeEmitter::getLoadLiteralOpValue(const MCInst &MI, unsigned OpIdx, 328 SmallVectorImpl<MCFixup> &Fixups, 329 const MCSubtargetInfo &STI) const { 330 const MCOperand &MO = MI.getOperand(OpIdx); 331 332 // If the destination is an immediate, we have nothing to do. 333 if (MO.isImm()) 334 return MO.getImm(); 335 assert(MO.isExpr() && "Unexpected target type!"); 336 337 MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_ldr_pcrel_imm19); 338 Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 339 340 ++MCNumFixups; 341 342 // All of the information is in the fixup. 343 return 0; 344 } 345 346 uint32_t 347 AArch64MCCodeEmitter::getMemExtendOpValue(const MCInst &MI, unsigned OpIdx, 348 SmallVectorImpl<MCFixup> &Fixups, 349 const MCSubtargetInfo &STI) const { 350 unsigned SignExtend = MI.getOperand(OpIdx).getImm(); 351 unsigned DoShift = MI.getOperand(OpIdx + 1).getImm(); 352 return (SignExtend << 1) | DoShift; 353 } 354 355 uint32_t 356 AArch64MCCodeEmitter::getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx, 357 SmallVectorImpl<MCFixup> &Fixups, 358 const MCSubtargetInfo &STI) const { 359 const MCOperand &MO = MI.getOperand(OpIdx); 360 361 if (MO.isImm()) 362 return MO.getImm(); 363 assert(MO.isExpr() && "Unexpected movz/movk immediate"); 364 365 Fixups.push_back(MCFixup::create( 366 0, MO.getExpr(), MCFixupKind(AArch64::fixup_aarch64_movw), MI.getLoc())); 367 368 ++MCNumFixups; 369 370 return 0; 371 } 372 373 /// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and- 374 /// branch target. 375 uint32_t AArch64MCCodeEmitter::getTestBranchTargetOpValue( 376 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups, 377 const MCSubtargetInfo &STI) const { 378 const MCOperand &MO = MI.getOperand(OpIdx); 379 380 // If the destination is an immediate, we have nothing to do. 381 if (MO.isImm()) 382 return MO.getImm(); 383 assert(MO.isExpr() && "Unexpected ADR target type!"); 384 385 MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_pcrel_branch14); 386 Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 387 388 ++MCNumFixups; 389 390 // All of the information is in the fixup. 391 return 0; 392 } 393 394 /// getBranchTargetOpValue - Return the encoded value for an unconditional 395 /// branch target. 396 uint32_t 397 AArch64MCCodeEmitter::getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, 398 SmallVectorImpl<MCFixup> &Fixups, 399 const MCSubtargetInfo &STI) const { 400 const MCOperand &MO = MI.getOperand(OpIdx); 401 402 // If the destination is an immediate, we have nothing to do. 403 if (MO.isImm()) 404 return MO.getImm(); 405 assert(MO.isExpr() && "Unexpected ADR target type!"); 406 407 MCFixupKind Kind = MI.getOpcode() == AArch64::BL 408 ? MCFixupKind(AArch64::fixup_aarch64_pcrel_call26) 409 : MCFixupKind(AArch64::fixup_aarch64_pcrel_branch26); 410 Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 411 412 ++MCNumFixups; 413 414 // All of the information is in the fixup. 415 return 0; 416 } 417 418 /// getVecShifterOpValue - Return the encoded value for the vector shifter: 419 /// 420 /// 00 -> 0 421 /// 01 -> 8 422 /// 10 -> 16 423 /// 11 -> 24 424 uint32_t 425 AArch64MCCodeEmitter::getVecShifterOpValue(const MCInst &MI, unsigned OpIdx, 426 SmallVectorImpl<MCFixup> &Fixups, 427 const MCSubtargetInfo &STI) const { 428 const MCOperand &MO = MI.getOperand(OpIdx); 429 assert(MO.isImm() && "Expected an immediate value for the shift amount!"); 430 431 switch (MO.getImm()) { 432 default: 433 break; 434 case 0: 435 return 0; 436 case 8: 437 return 1; 438 case 16: 439 return 2; 440 case 24: 441 return 3; 442 } 443 444 llvm_unreachable("Invalid value for vector shift amount!"); 445 } 446 447 /// getFixedPointScaleOpValue - Return the encoded value for the 448 // FP-to-fixed-point scale factor. 449 uint32_t AArch64MCCodeEmitter::getFixedPointScaleOpValue( 450 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups, 451 const MCSubtargetInfo &STI) const { 452 const MCOperand &MO = MI.getOperand(OpIdx); 453 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 454 return 64 - MO.getImm(); 455 } 456 457 uint32_t 458 AArch64MCCodeEmitter::getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx, 459 SmallVectorImpl<MCFixup> &Fixups, 460 const MCSubtargetInfo &STI) const { 461 const MCOperand &MO = MI.getOperand(OpIdx); 462 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 463 return 64 - MO.getImm(); 464 } 465 466 uint32_t 467 AArch64MCCodeEmitter::getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx, 468 SmallVectorImpl<MCFixup> &Fixups, 469 const MCSubtargetInfo &STI) const { 470 const MCOperand &MO = MI.getOperand(OpIdx); 471 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 472 return 32 - MO.getImm(); 473 } 474 475 uint32_t 476 AArch64MCCodeEmitter::getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx, 477 SmallVectorImpl<MCFixup> &Fixups, 478 const MCSubtargetInfo &STI) const { 479 const MCOperand &MO = MI.getOperand(OpIdx); 480 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 481 return 16 - MO.getImm(); 482 } 483 484 uint32_t 485 AArch64MCCodeEmitter::getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx, 486 SmallVectorImpl<MCFixup> &Fixups, 487 const MCSubtargetInfo &STI) const { 488 const MCOperand &MO = MI.getOperand(OpIdx); 489 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 490 return 8 - MO.getImm(); 491 } 492 493 uint32_t 494 AArch64MCCodeEmitter::getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx, 495 SmallVectorImpl<MCFixup> &Fixups, 496 const MCSubtargetInfo &STI) const { 497 const MCOperand &MO = MI.getOperand(OpIdx); 498 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 499 return MO.getImm() - 64; 500 } 501 502 uint32_t 503 AArch64MCCodeEmitter::getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx, 504 SmallVectorImpl<MCFixup> &Fixups, 505 const MCSubtargetInfo &STI) const { 506 const MCOperand &MO = MI.getOperand(OpIdx); 507 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 508 return MO.getImm() - 32; 509 } 510 511 uint32_t 512 AArch64MCCodeEmitter::getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx, 513 SmallVectorImpl<MCFixup> &Fixups, 514 const MCSubtargetInfo &STI) const { 515 const MCOperand &MO = MI.getOperand(OpIdx); 516 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 517 return MO.getImm() - 16; 518 } 519 520 uint32_t 521 AArch64MCCodeEmitter::getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx, 522 SmallVectorImpl<MCFixup> &Fixups, 523 const MCSubtargetInfo &STI) const { 524 const MCOperand &MO = MI.getOperand(OpIdx); 525 assert(MO.isImm() && "Expected an immediate value for the scale amount!"); 526 return MO.getImm() - 8; 527 } 528 529 /// getMoveVecShifterOpValue - Return the encoded value for the vector move 530 /// shifter (MSL). 531 uint32_t AArch64MCCodeEmitter::getMoveVecShifterOpValue( 532 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups, 533 const MCSubtargetInfo &STI) const { 534 const MCOperand &MO = MI.getOperand(OpIdx); 535 assert(MO.isImm() && 536 "Expected an immediate value for the move shift amount!"); 537 unsigned ShiftVal = AArch64_AM::getShiftValue(MO.getImm()); 538 assert((ShiftVal == 8 || ShiftVal == 16) && "Invalid shift amount!"); 539 return ShiftVal == 8 ? 0 : 1; 540 } 541 542 unsigned AArch64MCCodeEmitter::fixMOVZ(const MCInst &MI, unsigned EncodedValue, 543 const MCSubtargetInfo &STI) const { 544 // If one of the signed fixup kinds is applied to a MOVZ instruction, the 545 // eventual result could be either a MOVZ or a MOVN. It's the MCCodeEmitter's 546 // job to ensure that any bits possibly affected by this are 0. This means we 547 // must zero out bit 30 (essentially emitting a MOVN). 548 MCOperand UImm16MO = MI.getOperand(1); 549 550 // Nothing to do if there's no fixup. 551 if (UImm16MO.isImm()) 552 return EncodedValue; 553 554 const AArch64MCExpr *A64E = cast<AArch64MCExpr>(UImm16MO.getExpr()); 555 switch (A64E->getKind()) { 556 case AArch64MCExpr::VK_DTPREL_G2: 557 case AArch64MCExpr::VK_DTPREL_G1: 558 case AArch64MCExpr::VK_DTPREL_G0: 559 case AArch64MCExpr::VK_GOTTPREL_G1: 560 case AArch64MCExpr::VK_TPREL_G2: 561 case AArch64MCExpr::VK_TPREL_G1: 562 case AArch64MCExpr::VK_TPREL_G0: 563 return EncodedValue & ~(1u << 30); 564 default: 565 // Nothing to do for an unsigned fixup. 566 return EncodedValue; 567 } 568 569 570 return EncodedValue & ~(1u << 30); 571 } 572 573 void AArch64MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS, 574 SmallVectorImpl<MCFixup> &Fixups, 575 const MCSubtargetInfo &STI) const { 576 verifyInstructionPredicates(MI, 577 computeAvailableFeatures(STI.getFeatureBits())); 578 579 if (MI.getOpcode() == AArch64::TLSDESCCALL) { 580 // This is a directive which applies an R_AARCH64_TLSDESC_CALL to the 581 // following (BLR) instruction. It doesn't emit any code itself so it 582 // doesn't go through the normal TableGenerated channels. 583 MCFixupKind Fixup = MCFixupKind(AArch64::fixup_aarch64_tlsdesc_call); 584 Fixups.push_back(MCFixup::create(0, MI.getOperand(0).getExpr(), Fixup)); 585 return; 586 } else if (MI.getOpcode() == AArch64::CompilerBarrier) { 587 // This just prevents the compiler from reordering accesses, no actual code. 588 return; 589 } 590 591 uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI); 592 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary); 593 ++MCNumEmitted; // Keep track of the # of mi's emitted. 594 } 595 596 unsigned 597 AArch64MCCodeEmitter::fixMulHigh(const MCInst &MI, 598 unsigned EncodedValue, 599 const MCSubtargetInfo &STI) const { 600 // The Ra field of SMULH and UMULH is unused: it should be assembled as 31 601 // (i.e. all bits 1) but is ignored by the processor. 602 EncodedValue |= 0x1f << 10; 603 return EncodedValue; 604 } 605 606 template<int hasRs, int hasRt2> unsigned 607 AArch64MCCodeEmitter::fixLoadStoreExclusive(const MCInst &MI, 608 unsigned EncodedValue, 609 const MCSubtargetInfo &STI) const { 610 if (!hasRs) EncodedValue |= 0x001F0000; 611 if (!hasRt2) EncodedValue |= 0x00007C00; 612 613 return EncodedValue; 614 } 615 616 unsigned AArch64MCCodeEmitter::fixOneOperandFPComparison( 617 const MCInst &MI, unsigned EncodedValue, const MCSubtargetInfo &STI) const { 618 // The Rm field of FCMP and friends is unused - it should be assembled 619 // as 0, but is ignored by the processor. 620 EncodedValue &= ~(0x1f << 16); 621 return EncodedValue; 622 } 623 624 #define ENABLE_INSTR_PREDICATE_VERIFIER 625 #include "AArch64GenMCCodeEmitter.inc" 626 627 MCCodeEmitter *llvm::createAArch64MCCodeEmitter(const MCInstrInfo &MCII, 628 const MCRegisterInfo &MRI, 629 MCContext &Ctx) { 630 return new AArch64MCCodeEmitter(MCII, Ctx); 631 } 632