1 //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a printer that converts from our internal representation 10 // of machine-dependent LLVM code to GAS-format ARM assembly language. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ARMAsmPrinter.h" 15 #include "ARM.h" 16 #include "ARMConstantPoolValue.h" 17 #include "ARMMachineFunctionInfo.h" 18 #include "ARMTargetMachine.h" 19 #include "ARMTargetObjectFile.h" 20 #include "MCTargetDesc/ARMAddressingModes.h" 21 #include "MCTargetDesc/ARMInstPrinter.h" 22 #include "MCTargetDesc/ARMMCExpr.h" 23 #include "TargetInfo/ARMTargetInfo.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/BinaryFormat/COFF.h" 26 #include "llvm/CodeGen/MachineFunctionPass.h" 27 #include "llvm/CodeGen/MachineJumpTableInfo.h" 28 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 29 #include "llvm/IR/Constants.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/Mangler.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/Type.h" 34 #include "llvm/MC/MCAsmInfo.h" 35 #include "llvm/MC/MCAssembler.h" 36 #include "llvm/MC/MCContext.h" 37 #include "llvm/MC/MCELFStreamer.h" 38 #include "llvm/MC/MCInst.h" 39 #include "llvm/MC/MCInstBuilder.h" 40 #include "llvm/MC/MCObjectStreamer.h" 41 #include "llvm/MC/MCStreamer.h" 42 #include "llvm/MC/MCSymbol.h" 43 #include "llvm/MC/TargetRegistry.h" 44 #include "llvm/Support/ARMBuildAttributes.h" 45 #include "llvm/Support/Debug.h" 46 #include "llvm/Support/ErrorHandling.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include "llvm/Target/TargetMachine.h" 49 using namespace llvm; 50 51 #define DEBUG_TYPE "asm-printer" 52 53 ARMAsmPrinter::ARMAsmPrinter(TargetMachine &TM, 54 std::unique_ptr<MCStreamer> Streamer) 55 : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), AFI(nullptr), 56 MCP(nullptr), InConstantPool(false), OptimizationGoals(-1) {} 57 58 void ARMAsmPrinter::emitFunctionBodyEnd() { 59 // Make sure to terminate any constant pools that were at the end 60 // of the function. 61 if (!InConstantPool) 62 return; 63 InConstantPool = false; 64 OutStreamer->emitDataRegion(MCDR_DataRegionEnd); 65 } 66 67 void ARMAsmPrinter::emitFunctionEntryLabel() { 68 if (AFI->isThumbFunction()) { 69 OutStreamer->emitAssemblerFlag(MCAF_Code16); 70 OutStreamer->emitThumbFunc(CurrentFnSym); 71 } else { 72 OutStreamer->emitAssemblerFlag(MCAF_Code32); 73 } 74 75 // Emit symbol for CMSE non-secure entry point 76 if (AFI->isCmseNSEntryFunction()) { 77 MCSymbol *S = 78 OutContext.getOrCreateSymbol("__acle_se_" + CurrentFnSym->getName()); 79 emitLinkage(&MF->getFunction(), S); 80 OutStreamer->emitSymbolAttribute(S, MCSA_ELF_TypeFunction); 81 OutStreamer->emitLabel(S); 82 } 83 AsmPrinter::emitFunctionEntryLabel(); 84 } 85 86 void ARMAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) { 87 uint64_t Size = getDataLayout().getTypeAllocSize(CV->getType()); 88 assert(Size && "C++ constructor pointer had zero size!"); 89 90 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts()); 91 assert(GV && "C++ constructor pointer was not a GlobalValue!"); 92 93 const MCExpr *E = MCSymbolRefExpr::create(GetARMGVSymbol(GV, 94 ARMII::MO_NO_FLAG), 95 (Subtarget->isTargetELF() 96 ? MCSymbolRefExpr::VK_ARM_TARGET1 97 : MCSymbolRefExpr::VK_None), 98 OutContext); 99 100 OutStreamer->emitValue(E, Size); 101 } 102 103 void ARMAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { 104 if (PromotedGlobals.count(GV)) 105 // The global was promoted into a constant pool. It should not be emitted. 106 return; 107 AsmPrinter::emitGlobalVariable(GV); 108 } 109 110 /// runOnMachineFunction - This uses the emitInstruction() 111 /// method to print assembly for each instruction. 112 /// 113 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 114 AFI = MF.getInfo<ARMFunctionInfo>(); 115 MCP = MF.getConstantPool(); 116 Subtarget = &MF.getSubtarget<ARMSubtarget>(); 117 118 SetupMachineFunction(MF); 119 const Function &F = MF.getFunction(); 120 const TargetMachine& TM = MF.getTarget(); 121 122 // Collect all globals that had their storage promoted to a constant pool. 123 // Functions are emitted before variables, so this accumulates promoted 124 // globals from all functions in PromotedGlobals. 125 for (const auto *GV : AFI->getGlobalsPromotedToConstantPool()) 126 PromotedGlobals.insert(GV); 127 128 // Calculate this function's optimization goal. 129 unsigned OptimizationGoal; 130 if (F.hasOptNone()) 131 // For best debugging illusion, speed and small size sacrificed 132 OptimizationGoal = 6; 133 else if (F.hasMinSize()) 134 // Aggressively for small size, speed and debug illusion sacrificed 135 OptimizationGoal = 4; 136 else if (F.hasOptSize()) 137 // For small size, but speed and debugging illusion preserved 138 OptimizationGoal = 3; 139 else if (TM.getOptLevel() == CodeGenOpt::Aggressive) 140 // Aggressively for speed, small size and debug illusion sacrificed 141 OptimizationGoal = 2; 142 else if (TM.getOptLevel() > CodeGenOpt::None) 143 // For speed, but small size and good debug illusion preserved 144 OptimizationGoal = 1; 145 else // TM.getOptLevel() == CodeGenOpt::None 146 // For good debugging, but speed and small size preserved 147 OptimizationGoal = 5; 148 149 // Combine a new optimization goal with existing ones. 150 if (OptimizationGoals == -1) // uninitialized goals 151 OptimizationGoals = OptimizationGoal; 152 else if (OptimizationGoals != (int)OptimizationGoal) // conflicting goals 153 OptimizationGoals = 0; 154 155 if (Subtarget->isTargetCOFF()) { 156 bool Internal = F.hasInternalLinkage(); 157 COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC 158 : COFF::IMAGE_SYM_CLASS_EXTERNAL; 159 int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT; 160 161 OutStreamer->beginCOFFSymbolDef(CurrentFnSym); 162 OutStreamer->emitCOFFSymbolStorageClass(Scl); 163 OutStreamer->emitCOFFSymbolType(Type); 164 OutStreamer->endCOFFSymbolDef(); 165 } 166 167 // Emit the rest of the function body. 168 emitFunctionBody(); 169 170 // Emit the XRay table for this function. 171 emitXRayTable(); 172 173 // If we need V4T thumb mode Register Indirect Jump pads, emit them. 174 // These are created per function, rather than per TU, since it's 175 // relatively easy to exceed the thumb branch range within a TU. 176 if (! ThumbIndirectPads.empty()) { 177 OutStreamer->emitAssemblerFlag(MCAF_Code16); 178 emitAlignment(Align(2)); 179 for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) { 180 OutStreamer->emitLabel(TIP.second); 181 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX) 182 .addReg(TIP.first) 183 // Add predicate operands. 184 .addImm(ARMCC::AL) 185 .addReg(0)); 186 } 187 ThumbIndirectPads.clear(); 188 } 189 190 // We didn't modify anything. 191 return false; 192 } 193 194 void ARMAsmPrinter::PrintSymbolOperand(const MachineOperand &MO, 195 raw_ostream &O) { 196 assert(MO.isGlobal() && "caller should check MO.isGlobal"); 197 unsigned TF = MO.getTargetFlags(); 198 if (TF & ARMII::MO_LO16) 199 O << ":lower16:"; 200 else if (TF & ARMII::MO_HI16) 201 O << ":upper16:"; 202 else if (TF & ARMII::MO_LO_0_7) 203 O << ":lower0_7:"; 204 else if (TF & ARMII::MO_LO_8_15) 205 O << ":lower8_15:"; 206 else if (TF & ARMII::MO_HI_0_7) 207 O << ":upper0_7:"; 208 else if (TF & ARMII::MO_HI_8_15) 209 O << ":upper8_15:"; 210 211 GetARMGVSymbol(MO.getGlobal(), TF)->print(O, MAI); 212 printOffset(MO.getOffset(), O); 213 } 214 215 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum, 216 raw_ostream &O) { 217 const MachineOperand &MO = MI->getOperand(OpNum); 218 219 switch (MO.getType()) { 220 default: llvm_unreachable("<unknown operand type>"); 221 case MachineOperand::MO_Register: { 222 Register Reg = MO.getReg(); 223 assert(Reg.isPhysical()); 224 assert(!MO.getSubReg() && "Subregs should be eliminated!"); 225 if(ARM::GPRPairRegClass.contains(Reg)) { 226 const MachineFunction &MF = *MI->getParent()->getParent(); 227 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 228 Reg = TRI->getSubReg(Reg, ARM::gsub_0); 229 } 230 O << ARMInstPrinter::getRegisterName(Reg); 231 break; 232 } 233 case MachineOperand::MO_Immediate: { 234 O << '#'; 235 unsigned TF = MO.getTargetFlags(); 236 if (TF == ARMII::MO_LO16) 237 O << ":lower16:"; 238 else if (TF == ARMII::MO_HI16) 239 O << ":upper16:"; 240 else if (TF == ARMII::MO_LO_0_7) 241 O << ":lower0_7:"; 242 else if (TF == ARMII::MO_LO_8_15) 243 O << ":lower8_15:"; 244 else if (TF == ARMII::MO_HI_0_7) 245 O << ":upper0_7:"; 246 else if (TF == ARMII::MO_HI_8_15) 247 O << ":upper8_15:"; 248 O << MO.getImm(); 249 break; 250 } 251 case MachineOperand::MO_MachineBasicBlock: 252 MO.getMBB()->getSymbol()->print(O, MAI); 253 return; 254 case MachineOperand::MO_GlobalAddress: { 255 PrintSymbolOperand(MO, O); 256 break; 257 } 258 case MachineOperand::MO_ConstantPoolIndex: 259 if (Subtarget->genExecuteOnly()) 260 llvm_unreachable("execute-only should not generate constant pools"); 261 GetCPISymbol(MO.getIndex())->print(O, MAI); 262 break; 263 } 264 } 265 266 MCSymbol *ARMAsmPrinter::GetCPISymbol(unsigned CPID) const { 267 // The AsmPrinter::GetCPISymbol superclass method tries to use CPID as 268 // indexes in MachineConstantPool, which isn't in sync with indexes used here. 269 const DataLayout &DL = getDataLayout(); 270 return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 271 "CPI" + Twine(getFunctionNumber()) + "_" + 272 Twine(CPID)); 273 } 274 275 //===--------------------------------------------------------------------===// 276 277 MCSymbol *ARMAsmPrinter:: 278 GetARMJTIPICJumpTableLabel(unsigned uid) const { 279 const DataLayout &DL = getDataLayout(); 280 SmallString<60> Name; 281 raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI" 282 << getFunctionNumber() << '_' << uid; 283 return OutContext.getOrCreateSymbol(Name); 284 } 285 286 bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 287 const char *ExtraCode, raw_ostream &O) { 288 // Does this asm operand have a single letter operand modifier? 289 if (ExtraCode && ExtraCode[0]) { 290 if (ExtraCode[1] != 0) return true; // Unknown modifier. 291 292 switch (ExtraCode[0]) { 293 default: 294 // See if this is a generic print operand 295 return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O); 296 case 'P': // Print a VFP double precision register. 297 case 'q': // Print a NEON quad precision register. 298 printOperand(MI, OpNum, O); 299 return false; 300 case 'y': // Print a VFP single precision register as indexed double. 301 if (MI->getOperand(OpNum).isReg()) { 302 MCRegister Reg = MI->getOperand(OpNum).getReg().asMCReg(); 303 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 304 // Find the 'd' register that has this 's' register as a sub-register, 305 // and determine the lane number. 306 for (MCPhysReg SR : TRI->superregs(Reg)) { 307 if (!ARM::DPRRegClass.contains(SR)) 308 continue; 309 bool Lane0 = TRI->getSubReg(SR, ARM::ssub_0) == Reg; 310 O << ARMInstPrinter::getRegisterName(SR) << (Lane0 ? "[0]" : "[1]"); 311 return false; 312 } 313 } 314 return true; 315 case 'B': // Bitwise inverse of integer or symbol without a preceding #. 316 if (!MI->getOperand(OpNum).isImm()) 317 return true; 318 O << ~(MI->getOperand(OpNum).getImm()); 319 return false; 320 case 'L': // The low 16 bits of an immediate constant. 321 if (!MI->getOperand(OpNum).isImm()) 322 return true; 323 O << (MI->getOperand(OpNum).getImm() & 0xffff); 324 return false; 325 case 'M': { // A register range suitable for LDM/STM. 326 if (!MI->getOperand(OpNum).isReg()) 327 return true; 328 const MachineOperand &MO = MI->getOperand(OpNum); 329 Register RegBegin = MO.getReg(); 330 // This takes advantage of the 2 operand-ness of ldm/stm and that we've 331 // already got the operands in registers that are operands to the 332 // inline asm statement. 333 O << "{"; 334 if (ARM::GPRPairRegClass.contains(RegBegin)) { 335 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 336 Register Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0); 337 O << ARMInstPrinter::getRegisterName(Reg0) << ", "; 338 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1); 339 } 340 O << ARMInstPrinter::getRegisterName(RegBegin); 341 342 // FIXME: The register allocator not only may not have given us the 343 // registers in sequence, but may not be in ascending registers. This 344 // will require changes in the register allocator that'll need to be 345 // propagated down here if the operands change. 346 unsigned RegOps = OpNum + 1; 347 while (MI->getOperand(RegOps).isReg()) { 348 O << ", " 349 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg()); 350 RegOps++; 351 } 352 353 O << "}"; 354 355 return false; 356 } 357 case 'R': // The most significant register of a pair. 358 case 'Q': { // The least significant register of a pair. 359 if (OpNum == 0) 360 return true; 361 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1); 362 if (!FlagsOP.isImm()) 363 return true; 364 unsigned Flags = FlagsOP.getImm(); 365 366 // This operand may not be the one that actually provides the register. If 367 // it's tied to a previous one then we should refer instead to that one 368 // for registers and their classes. 369 unsigned TiedIdx; 370 if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) { 371 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) { 372 unsigned OpFlags = MI->getOperand(OpNum).getImm(); 373 OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 374 } 375 Flags = MI->getOperand(OpNum).getImm(); 376 377 // Later code expects OpNum to be pointing at the register rather than 378 // the flags. 379 OpNum += 1; 380 } 381 382 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 383 unsigned RC; 384 bool FirstHalf; 385 const ARMBaseTargetMachine &ATM = 386 static_cast<const ARMBaseTargetMachine &>(TM); 387 388 // 'Q' should correspond to the low order register and 'R' to the high 389 // order register. Whether this corresponds to the upper or lower half 390 // depends on the endianess mode. 391 if (ExtraCode[0] == 'Q') 392 FirstHalf = ATM.isLittleEndian(); 393 else 394 // ExtraCode[0] == 'R'. 395 FirstHalf = !ATM.isLittleEndian(); 396 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 397 if (InlineAsm::hasRegClassConstraint(Flags, RC) && 398 ARM::GPRPairRegClass.hasSubClassEq(TRI->getRegClass(RC))) { 399 if (NumVals != 1) 400 return true; 401 const MachineOperand &MO = MI->getOperand(OpNum); 402 if (!MO.isReg()) 403 return true; 404 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 405 Register Reg = 406 TRI->getSubReg(MO.getReg(), FirstHalf ? ARM::gsub_0 : ARM::gsub_1); 407 O << ARMInstPrinter::getRegisterName(Reg); 408 return false; 409 } 410 if (NumVals != 2) 411 return true; 412 unsigned RegOp = FirstHalf ? OpNum : OpNum + 1; 413 if (RegOp >= MI->getNumOperands()) 414 return true; 415 const MachineOperand &MO = MI->getOperand(RegOp); 416 if (!MO.isReg()) 417 return true; 418 Register Reg = MO.getReg(); 419 O << ARMInstPrinter::getRegisterName(Reg); 420 return false; 421 } 422 423 case 'e': // The low doubleword register of a NEON quad register. 424 case 'f': { // The high doubleword register of a NEON quad register. 425 if (!MI->getOperand(OpNum).isReg()) 426 return true; 427 Register Reg = MI->getOperand(OpNum).getReg(); 428 if (!ARM::QPRRegClass.contains(Reg)) 429 return true; 430 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 431 Register SubReg = 432 TRI->getSubReg(Reg, ExtraCode[0] == 'e' ? ARM::dsub_0 : ARM::dsub_1); 433 O << ARMInstPrinter::getRegisterName(SubReg); 434 return false; 435 } 436 437 // This modifier is not yet supported. 438 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1. 439 return true; 440 case 'H': { // The highest-numbered register of a pair. 441 const MachineOperand &MO = MI->getOperand(OpNum); 442 if (!MO.isReg()) 443 return true; 444 const MachineFunction &MF = *MI->getParent()->getParent(); 445 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 446 Register Reg = MO.getReg(); 447 if(!ARM::GPRPairRegClass.contains(Reg)) 448 return false; 449 Reg = TRI->getSubReg(Reg, ARM::gsub_1); 450 O << ARMInstPrinter::getRegisterName(Reg); 451 return false; 452 } 453 } 454 } 455 456 printOperand(MI, OpNum, O); 457 return false; 458 } 459 460 bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 461 unsigned OpNum, const char *ExtraCode, 462 raw_ostream &O) { 463 // Does this asm operand have a single letter operand modifier? 464 if (ExtraCode && ExtraCode[0]) { 465 if (ExtraCode[1] != 0) return true; // Unknown modifier. 466 467 switch (ExtraCode[0]) { 468 case 'A': // A memory operand for a VLD1/VST1 instruction. 469 default: return true; // Unknown modifier. 470 case 'm': // The base register of a memory operand. 471 if (!MI->getOperand(OpNum).isReg()) 472 return true; 473 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg()); 474 return false; 475 } 476 } 477 478 const MachineOperand &MO = MI->getOperand(OpNum); 479 assert(MO.isReg() && "unexpected inline asm memory operand"); 480 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]"; 481 return false; 482 } 483 484 static bool isThumb(const MCSubtargetInfo& STI) { 485 return STI.hasFeature(ARM::ModeThumb); 486 } 487 488 void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, 489 const MCSubtargetInfo *EndInfo) const { 490 // If either end mode is unknown (EndInfo == NULL) or different than 491 // the start mode, then restore the start mode. 492 const bool WasThumb = isThumb(StartInfo); 493 if (!EndInfo || WasThumb != isThumb(*EndInfo)) { 494 OutStreamer->emitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32); 495 } 496 } 497 498 void ARMAsmPrinter::emitStartOfAsmFile(Module &M) { 499 const Triple &TT = TM.getTargetTriple(); 500 // Use unified assembler syntax. 501 OutStreamer->emitAssemblerFlag(MCAF_SyntaxUnified); 502 503 // Emit ARM Build Attributes 504 if (TT.isOSBinFormatELF()) 505 emitAttributes(); 506 507 // Use the triple's architecture and subarchitecture to determine 508 // if we're thumb for the purposes of the top level code16 assembler 509 // flag. 510 if (!M.getModuleInlineAsm().empty() && TT.isThumb()) 511 OutStreamer->emitAssemblerFlag(MCAF_Code16); 512 } 513 514 static void 515 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, 516 MachineModuleInfoImpl::StubValueTy &MCSym) { 517 // L_foo$stub: 518 OutStreamer.emitLabel(StubLabel); 519 // .indirect_symbol _foo 520 OutStreamer.emitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 521 522 if (MCSym.getInt()) 523 // External to current translation unit. 524 OutStreamer.emitIntValue(0, 4/*size*/); 525 else 526 // Internal to current translation unit. 527 // 528 // When we place the LSDA into the TEXT section, the type info 529 // pointers need to be indirect and pc-rel. We accomplish this by 530 // using NLPs; however, sometimes the types are local to the file. 531 // We need to fill in the value for the NLP in those cases. 532 OutStreamer.emitValue( 533 MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()), 534 4 /*size*/); 535 } 536 537 538 void ARMAsmPrinter::emitEndOfAsmFile(Module &M) { 539 const Triple &TT = TM.getTargetTriple(); 540 if (TT.isOSBinFormatMachO()) { 541 // All darwin targets use mach-o. 542 const TargetLoweringObjectFileMachO &TLOFMacho = 543 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 544 MachineModuleInfoMachO &MMIMacho = 545 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 546 547 // Output non-lazy-pointers for external and common global variables. 548 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList(); 549 550 if (!Stubs.empty()) { 551 // Switch with ".non_lazy_symbol_pointer" directive. 552 OutStreamer->switchSection(TLOFMacho.getNonLazySymbolPointerSection()); 553 emitAlignment(Align(4)); 554 555 for (auto &Stub : Stubs) 556 emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second); 557 558 Stubs.clear(); 559 OutStreamer->addBlankLine(); 560 } 561 562 Stubs = MMIMacho.GetThreadLocalGVStubList(); 563 if (!Stubs.empty()) { 564 // Switch with ".non_lazy_symbol_pointer" directive. 565 OutStreamer->switchSection(TLOFMacho.getThreadLocalPointerSection()); 566 emitAlignment(Align(4)); 567 568 for (auto &Stub : Stubs) 569 emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second); 570 571 Stubs.clear(); 572 OutStreamer->addBlankLine(); 573 } 574 575 // Funny Darwin hack: This flag tells the linker that no global symbols 576 // contain code that falls through to other global symbols (e.g. the obvious 577 // implementation of multiple entry points). If this doesn't occur, the 578 // linker can safely perform dead code stripping. Since LLVM never 579 // generates code that does this, it is always safe to set. 580 OutStreamer->emitAssemblerFlag(MCAF_SubsectionsViaSymbols); 581 } 582 583 // The last attribute to be emitted is ABI_optimization_goals 584 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer(); 585 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 586 587 if (OptimizationGoals > 0 && 588 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() || 589 Subtarget->isTargetMuslAEABI())) 590 ATS.emitAttribute(ARMBuildAttrs::ABI_optimization_goals, OptimizationGoals); 591 OptimizationGoals = -1; 592 593 ATS.finishAttributeSection(); 594 } 595 596 //===----------------------------------------------------------------------===// 597 // Helper routines for emitStartOfAsmFile() and emitEndOfAsmFile() 598 // FIXME: 599 // The following seem like one-off assembler flags, but they actually need 600 // to appear in the .ARM.attributes section in ELF. 601 // Instead of subclassing the MCELFStreamer, we do the work here. 602 603 // Returns true if all functions have the same function attribute value. 604 // It also returns true when the module has no functions. 605 static bool checkFunctionsAttributeConsistency(const Module &M, StringRef Attr, 606 StringRef Value) { 607 return !any_of(M, [&](const Function &F) { 608 return F.getFnAttribute(Attr).getValueAsString() != Value; 609 }); 610 } 611 // Returns true if all functions have the same denormal mode. 612 // It also returns true when the module has no functions. 613 static bool checkDenormalAttributeConsistency(const Module &M, 614 StringRef Attr, 615 DenormalMode Value) { 616 return !any_of(M, [&](const Function &F) { 617 StringRef AttrVal = F.getFnAttribute(Attr).getValueAsString(); 618 return parseDenormalFPAttribute(AttrVal) != Value; 619 }); 620 } 621 622 void ARMAsmPrinter::emitAttributes() { 623 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer(); 624 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 625 626 ATS.emitTextAttribute(ARMBuildAttrs::conformance, "2.09"); 627 628 ATS.switchVendor("aeabi"); 629 630 // Compute ARM ELF Attributes based on the default subtarget that 631 // we'd have constructed. The existing ARM behavior isn't LTO clean 632 // anyhow. 633 // FIXME: For ifunc related functions we could iterate over and look 634 // for a feature string that doesn't match the default one. 635 const Triple &TT = TM.getTargetTriple(); 636 StringRef CPU = TM.getTargetCPU(); 637 StringRef FS = TM.getTargetFeatureString(); 638 std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU); 639 if (!FS.empty()) { 640 if (!ArchFS.empty()) 641 ArchFS = (Twine(ArchFS) + "," + FS).str(); 642 else 643 ArchFS = std::string(FS); 644 } 645 const ARMBaseTargetMachine &ATM = 646 static_cast<const ARMBaseTargetMachine &>(TM); 647 const ARMSubtarget STI(TT, std::string(CPU), ArchFS, ATM, 648 ATM.isLittleEndian()); 649 650 // Emit build attributes for the available hardware. 651 ATS.emitTargetAttributes(STI); 652 653 // RW data addressing. 654 if (isPositionIndependent()) { 655 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data, 656 ARMBuildAttrs::AddressRWPCRel); 657 } else if (STI.isRWPI()) { 658 // RWPI specific attributes. 659 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data, 660 ARMBuildAttrs::AddressRWSBRel); 661 } 662 663 // RO data addressing. 664 if (isPositionIndependent() || STI.isROPI()) { 665 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data, 666 ARMBuildAttrs::AddressROPCRel); 667 } 668 669 // GOT use. 670 if (isPositionIndependent()) { 671 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use, 672 ARMBuildAttrs::AddressGOT); 673 } else { 674 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use, 675 ARMBuildAttrs::AddressDirect); 676 } 677 678 // Set FP Denormals. 679 if (checkDenormalAttributeConsistency(*MMI->getModule(), "denormal-fp-math", 680 DenormalMode::getPreserveSign())) 681 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 682 ARMBuildAttrs::PreserveFPSign); 683 else if (checkDenormalAttributeConsistency(*MMI->getModule(), 684 "denormal-fp-math", 685 DenormalMode::getPositiveZero())) 686 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 687 ARMBuildAttrs::PositiveZero); 688 else if (!TM.Options.UnsafeFPMath) 689 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 690 ARMBuildAttrs::IEEEDenormals); 691 else { 692 if (!STI.hasVFP2Base()) { 693 // When the target doesn't have an FPU (by design or 694 // intention), the assumptions made on the software support 695 // mirror that of the equivalent hardware support *if it 696 // existed*. For v7 and better we indicate that denormals are 697 // flushed preserving sign, and for V6 we indicate that 698 // denormals are flushed to positive zero. 699 if (STI.hasV7Ops()) 700 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 701 ARMBuildAttrs::PreserveFPSign); 702 } else if (STI.hasVFP3Base()) { 703 // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is, 704 // the sign bit of the zero matches the sign bit of the input or 705 // result that is being flushed to zero. 706 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 707 ARMBuildAttrs::PreserveFPSign); 708 } 709 // For VFPv2 implementations it is implementation defined as 710 // to whether denormals are flushed to positive zero or to 711 // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically 712 // LLVM has chosen to flush this to positive zero (most likely for 713 // GCC compatibility), so that's the chosen value here (the 714 // absence of its emission implies zero). 715 } 716 717 // Set FP exceptions and rounding 718 if (checkFunctionsAttributeConsistency(*MMI->getModule(), 719 "no-trapping-math", "true") || 720 TM.Options.NoTrappingFPMath) 721 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 722 ARMBuildAttrs::Not_Allowed); 723 else if (!TM.Options.UnsafeFPMath) { 724 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, ARMBuildAttrs::Allowed); 725 726 // If the user has permitted this code to choose the IEEE 754 727 // rounding at run-time, emit the rounding attribute. 728 if (TM.Options.HonorSignDependentRoundingFPMathOption) 729 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_rounding, ARMBuildAttrs::Allowed); 730 } 731 732 // TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath is the 733 // equivalent of GCC's -ffinite-math-only flag. 734 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath) 735 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 736 ARMBuildAttrs::Allowed); 737 else 738 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 739 ARMBuildAttrs::AllowIEEE754); 740 741 // FIXME: add more flags to ARMBuildAttributes.h 742 // 8-bytes alignment stuff. 743 ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1); 744 ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1); 745 746 // Hard float. Use both S and D registers and conform to AAPCS-VFP. 747 if (STI.isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) 748 ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS); 749 750 // FIXME: To support emitting this build attribute as GCC does, the 751 // -mfp16-format option and associated plumbing must be 752 // supported. For now the __fp16 type is exposed by default, so this 753 // attribute should be emitted with value 1. 754 ATS.emitAttribute(ARMBuildAttrs::ABI_FP_16bit_format, 755 ARMBuildAttrs::FP16FormatIEEE); 756 757 if (const Module *SourceModule = MMI->getModule()) { 758 // ABI_PCS_wchar_t to indicate wchar_t width 759 // FIXME: There is no way to emit value 0 (wchar_t prohibited). 760 if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>( 761 SourceModule->getModuleFlag("wchar_size"))) { 762 int WCharWidth = WCharWidthValue->getZExtValue(); 763 assert((WCharWidth == 2 || WCharWidth == 4) && 764 "wchar_t width must be 2 or 4 bytes"); 765 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth); 766 } 767 768 // ABI_enum_size to indicate enum width 769 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3 770 // (all enums contain a value needing 32 bits to encode). 771 if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>( 772 SourceModule->getModuleFlag("min_enum_size"))) { 773 int EnumWidth = EnumWidthValue->getZExtValue(); 774 assert((EnumWidth == 1 || EnumWidth == 4) && 775 "Minimum enum width must be 1 or 4 bytes"); 776 int EnumBuildAttr = EnumWidth == 1 ? 1 : 2; 777 ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr); 778 } 779 780 auto *PACValue = mdconst::extract_or_null<ConstantInt>( 781 SourceModule->getModuleFlag("sign-return-address")); 782 if (PACValue && PACValue->isOne()) { 783 // If "+pacbti" is used as an architecture extension, 784 // Tag_PAC_extension is emitted in 785 // ARMTargetStreamer::emitTargetAttributes(). 786 if (!STI.hasPACBTI()) { 787 ATS.emitAttribute(ARMBuildAttrs::PAC_extension, 788 ARMBuildAttrs::AllowPACInNOPSpace); 789 } 790 ATS.emitAttribute(ARMBuildAttrs::PACRET_use, ARMBuildAttrs::PACRETUsed); 791 } 792 793 auto *BTIValue = mdconst::extract_or_null<ConstantInt>( 794 SourceModule->getModuleFlag("branch-target-enforcement")); 795 if (BTIValue && BTIValue->isOne()) { 796 // If "+pacbti" is used as an architecture extension, 797 // Tag_BTI_extension is emitted in 798 // ARMTargetStreamer::emitTargetAttributes(). 799 if (!STI.hasPACBTI()) { 800 ATS.emitAttribute(ARMBuildAttrs::BTI_extension, 801 ARMBuildAttrs::AllowBTIInNOPSpace); 802 } 803 ATS.emitAttribute(ARMBuildAttrs::BTI_use, ARMBuildAttrs::BTIUsed); 804 } 805 } 806 807 // We currently do not support using R9 as the TLS pointer. 808 if (STI.isRWPI()) 809 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use, 810 ARMBuildAttrs::R9IsSB); 811 else if (STI.isR9Reserved()) 812 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use, 813 ARMBuildAttrs::R9Reserved); 814 else 815 ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use, 816 ARMBuildAttrs::R9IsGPR); 817 } 818 819 //===----------------------------------------------------------------------===// 820 821 static MCSymbol *getBFLabel(StringRef Prefix, unsigned FunctionNumber, 822 unsigned LabelId, MCContext &Ctx) { 823 824 MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix) 825 + "BF" + Twine(FunctionNumber) + "_" + Twine(LabelId)); 826 return Label; 827 } 828 829 static MCSymbol *getPICLabel(StringRef Prefix, unsigned FunctionNumber, 830 unsigned LabelId, MCContext &Ctx) { 831 832 MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix) 833 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId)); 834 return Label; 835 } 836 837 static MCSymbolRefExpr::VariantKind 838 getModifierVariantKind(ARMCP::ARMCPModifier Modifier) { 839 switch (Modifier) { 840 case ARMCP::no_modifier: 841 return MCSymbolRefExpr::VK_None; 842 case ARMCP::TLSGD: 843 return MCSymbolRefExpr::VK_TLSGD; 844 case ARMCP::TPOFF: 845 return MCSymbolRefExpr::VK_TPOFF; 846 case ARMCP::GOTTPOFF: 847 return MCSymbolRefExpr::VK_GOTTPOFF; 848 case ARMCP::SBREL: 849 return MCSymbolRefExpr::VK_ARM_SBREL; 850 case ARMCP::GOT_PREL: 851 return MCSymbolRefExpr::VK_ARM_GOT_PREL; 852 case ARMCP::SECREL: 853 return MCSymbolRefExpr::VK_SECREL; 854 } 855 llvm_unreachable("Invalid ARMCPModifier!"); 856 } 857 858 MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV, 859 unsigned char TargetFlags) { 860 if (Subtarget->isTargetMachO()) { 861 bool IsIndirect = 862 (TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV); 863 864 if (!IsIndirect) 865 return getSymbol(GV); 866 867 // FIXME: Remove this when Darwin transition to @GOT like syntax. 868 MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 869 MachineModuleInfoMachO &MMIMachO = 870 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 871 MachineModuleInfoImpl::StubValueTy &StubSym = 872 GV->isThreadLocal() ? MMIMachO.getThreadLocalGVStubEntry(MCSym) 873 : MMIMachO.getGVStubEntry(MCSym); 874 875 if (!StubSym.getPointer()) 876 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV), 877 !GV->hasInternalLinkage()); 878 return MCSym; 879 } else if (Subtarget->isTargetCOFF()) { 880 assert(Subtarget->isTargetWindows() && 881 "Windows is the only supported COFF target"); 882 883 bool IsIndirect = 884 (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB)); 885 if (!IsIndirect) 886 return getSymbol(GV); 887 888 SmallString<128> Name; 889 if (TargetFlags & ARMII::MO_DLLIMPORT) 890 Name = "__imp_"; 891 else if (TargetFlags & ARMII::MO_COFFSTUB) 892 Name = ".refptr."; 893 getNameWithPrefix(Name, GV); 894 895 MCSymbol *MCSym = OutContext.getOrCreateSymbol(Name); 896 897 if (TargetFlags & ARMII::MO_COFFSTUB) { 898 MachineModuleInfoCOFF &MMICOFF = 899 MMI->getObjFileInfo<MachineModuleInfoCOFF>(); 900 MachineModuleInfoImpl::StubValueTy &StubSym = 901 MMICOFF.getGVStubEntry(MCSym); 902 903 if (!StubSym.getPointer()) 904 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV), true); 905 } 906 907 return MCSym; 908 } else if (Subtarget->isTargetELF()) { 909 return getSymbolPreferLocal(*GV); 910 } 911 llvm_unreachable("unexpected target"); 912 } 913 914 void ARMAsmPrinter::emitMachineConstantPoolValue( 915 MachineConstantPoolValue *MCPV) { 916 const DataLayout &DL = getDataLayout(); 917 int Size = DL.getTypeAllocSize(MCPV->getType()); 918 919 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV); 920 921 if (ACPV->isPromotedGlobal()) { 922 // This constant pool entry is actually a global whose storage has been 923 // promoted into the constant pool. This global may be referenced still 924 // by debug information, and due to the way AsmPrinter is set up, the debug 925 // info is immutable by the time we decide to promote globals to constant 926 // pools. Because of this, we need to ensure we emit a symbol for the global 927 // with private linkage (the default) so debug info can refer to it. 928 // 929 // However, if this global is promoted into several functions we must ensure 930 // we don't try and emit duplicate symbols! 931 auto *ACPC = cast<ARMConstantPoolConstant>(ACPV); 932 for (const auto *GV : ACPC->promotedGlobals()) { 933 if (!EmittedPromotedGlobalLabels.count(GV)) { 934 MCSymbol *GVSym = getSymbol(GV); 935 OutStreamer->emitLabel(GVSym); 936 EmittedPromotedGlobalLabels.insert(GV); 937 } 938 } 939 return emitGlobalConstant(DL, ACPC->getPromotedGlobalInit()); 940 } 941 942 MCSymbol *MCSym; 943 if (ACPV->isLSDA()) { 944 MCSym = getMBBExceptionSym(MF->front()); 945 } else if (ACPV->isBlockAddress()) { 946 const BlockAddress *BA = 947 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(); 948 MCSym = GetBlockAddressSymbol(BA); 949 } else if (ACPV->isGlobalValue()) { 950 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV(); 951 952 // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so 953 // flag the global as MO_NONLAZY. 954 unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0; 955 MCSym = GetARMGVSymbol(GV, TF); 956 } else if (ACPV->isMachineBasicBlock()) { 957 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB(); 958 MCSym = MBB->getSymbol(); 959 } else { 960 assert(ACPV->isExtSymbol() && "unrecognized constant pool value"); 961 auto Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(); 962 MCSym = GetExternalSymbolSymbol(Sym); 963 } 964 965 // Create an MCSymbol for the reference. 966 const MCExpr *Expr = 967 MCSymbolRefExpr::create(MCSym, getModifierVariantKind(ACPV->getModifier()), 968 OutContext); 969 970 if (ACPV->getPCAdjustment()) { 971 MCSymbol *PCLabel = 972 getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(), 973 ACPV->getLabelId(), OutContext); 974 const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext); 975 PCRelExpr = 976 MCBinaryExpr::createAdd(PCRelExpr, 977 MCConstantExpr::create(ACPV->getPCAdjustment(), 978 OutContext), 979 OutContext); 980 if (ACPV->mustAddCurrentAddress()) { 981 // We want "(<expr> - .)", but MC doesn't have a concept of the '.' 982 // label, so just emit a local label end reference that instead. 983 MCSymbol *DotSym = OutContext.createTempSymbol(); 984 OutStreamer->emitLabel(DotSym); 985 const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext); 986 PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext); 987 } 988 Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext); 989 } 990 OutStreamer->emitValue(Expr, Size); 991 } 992 993 void ARMAsmPrinter::emitJumpTableAddrs(const MachineInstr *MI) { 994 const MachineOperand &MO1 = MI->getOperand(1); 995 unsigned JTI = MO1.getIndex(); 996 997 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for 998 // ARM mode tables. 999 emitAlignment(Align(4)); 1000 1001 // Emit a label for the jump table. 1002 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI); 1003 OutStreamer->emitLabel(JTISymbol); 1004 1005 // Mark the jump table as data-in-code. 1006 OutStreamer->emitDataRegion(MCDR_DataRegionJT32); 1007 1008 // Emit each entry of the table. 1009 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1010 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 1011 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 1012 1013 for (MachineBasicBlock *MBB : JTBBs) { 1014 // Construct an MCExpr for the entry. We want a value of the form: 1015 // (BasicBlockAddr - TableBeginAddr) 1016 // 1017 // For example, a table with entries jumping to basic blocks BB0 and BB1 1018 // would look like: 1019 // LJTI_0_0: 1020 // .word (LBB0 - LJTI_0_0) 1021 // .word (LBB1 - LJTI_0_0) 1022 const MCExpr *Expr = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext); 1023 1024 if (isPositionIndependent() || Subtarget->isROPI()) 1025 Expr = MCBinaryExpr::createSub(Expr, MCSymbolRefExpr::create(JTISymbol, 1026 OutContext), 1027 OutContext); 1028 // If we're generating a table of Thumb addresses in static relocation 1029 // model, we need to add one to keep interworking correctly. 1030 else if (AFI->isThumbFunction()) 1031 Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(1,OutContext), 1032 OutContext); 1033 OutStreamer->emitValue(Expr, 4); 1034 } 1035 // Mark the end of jump table data-in-code region. 1036 OutStreamer->emitDataRegion(MCDR_DataRegionEnd); 1037 } 1038 1039 void ARMAsmPrinter::emitJumpTableInsts(const MachineInstr *MI) { 1040 const MachineOperand &MO1 = MI->getOperand(1); 1041 unsigned JTI = MO1.getIndex(); 1042 1043 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for 1044 // ARM mode tables. 1045 emitAlignment(Align(4)); 1046 1047 // Emit a label for the jump table. 1048 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI); 1049 OutStreamer->emitLabel(JTISymbol); 1050 1051 // Emit each entry of the table. 1052 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1053 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 1054 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 1055 1056 for (MachineBasicBlock *MBB : JTBBs) { 1057 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(), 1058 OutContext); 1059 // If this isn't a TBB or TBH, the entries are direct branch instructions. 1060 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2B) 1061 .addExpr(MBBSymbolExpr) 1062 .addImm(ARMCC::AL) 1063 .addReg(0)); 1064 } 1065 } 1066 1067 void ARMAsmPrinter::emitJumpTableTBInst(const MachineInstr *MI, 1068 unsigned OffsetWidth) { 1069 assert((OffsetWidth == 1 || OffsetWidth == 2) && "invalid tbb/tbh width"); 1070 const MachineOperand &MO1 = MI->getOperand(1); 1071 unsigned JTI = MO1.getIndex(); 1072 1073 if (Subtarget->isThumb1Only()) 1074 emitAlignment(Align(4)); 1075 1076 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI); 1077 OutStreamer->emitLabel(JTISymbol); 1078 1079 // Emit each entry of the table. 1080 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); 1081 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 1082 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; 1083 1084 // Mark the jump table as data-in-code. 1085 OutStreamer->emitDataRegion(OffsetWidth == 1 ? MCDR_DataRegionJT8 1086 : MCDR_DataRegionJT16); 1087 1088 for (auto *MBB : JTBBs) { 1089 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(), 1090 OutContext); 1091 // Otherwise it's an offset from the dispatch instruction. Construct an 1092 // MCExpr for the entry. We want a value of the form: 1093 // (BasicBlockAddr - TBBInstAddr + 4) / 2 1094 // 1095 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1 1096 // would look like: 1097 // LJTI_0_0: 1098 // .byte (LBB0 - (LCPI0_0 + 4)) / 2 1099 // .byte (LBB1 - (LCPI0_0 + 4)) / 2 1100 // where LCPI0_0 is a label defined just before the TBB instruction using 1101 // this table. 1102 MCSymbol *TBInstPC = GetCPISymbol(MI->getOperand(0).getImm()); 1103 const MCExpr *Expr = MCBinaryExpr::createAdd( 1104 MCSymbolRefExpr::create(TBInstPC, OutContext), 1105 MCConstantExpr::create(4, OutContext), OutContext); 1106 Expr = MCBinaryExpr::createSub(MBBSymbolExpr, Expr, OutContext); 1107 Expr = MCBinaryExpr::createDiv(Expr, MCConstantExpr::create(2, OutContext), 1108 OutContext); 1109 OutStreamer->emitValue(Expr, OffsetWidth); 1110 } 1111 // Mark the end of jump table data-in-code region. 32-bit offsets use 1112 // actual branch instructions here, so we don't mark those as a data-region 1113 // at all. 1114 OutStreamer->emitDataRegion(MCDR_DataRegionEnd); 1115 1116 // Make sure the next instruction is 2-byte aligned. 1117 emitAlignment(Align(2)); 1118 } 1119 1120 void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) { 1121 assert(MI->getFlag(MachineInstr::FrameSetup) && 1122 "Only instruction which are involved into frame setup code are allowed"); 1123 1124 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer(); 1125 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 1126 const MachineFunction &MF = *MI->getParent()->getParent(); 1127 const TargetRegisterInfo *TargetRegInfo = 1128 MF.getSubtarget().getRegisterInfo(); 1129 const MachineRegisterInfo &MachineRegInfo = MF.getRegInfo(); 1130 1131 Register FramePtr = TargetRegInfo->getFrameRegister(MF); 1132 unsigned Opc = MI->getOpcode(); 1133 unsigned SrcReg, DstReg; 1134 1135 switch (Opc) { 1136 case ARM::tPUSH: 1137 // special case: tPUSH does not have src/dst regs. 1138 SrcReg = DstReg = ARM::SP; 1139 break; 1140 case ARM::tLDRpci: 1141 case ARM::t2MOVi16: 1142 case ARM::t2MOVTi16: 1143 case ARM::tMOVi8: 1144 case ARM::tADDi8: 1145 case ARM::tLSLri: 1146 // special cases: 1147 // 1) for Thumb1 code we sometimes materialize the constant via constpool 1148 // load. 1149 // 2) for Thumb1 execute only code we materialize the constant via the 1150 // following pattern: 1151 // movs r3, #:upper8_15:<const> 1152 // lsls r3, #8 1153 // adds r3, #:upper0_7:<const> 1154 // lsls r3, #8 1155 // adds r3, #:lower8_15:<const> 1156 // lsls r3, #8 1157 // adds r3, #:lower0_7:<const> 1158 // So we need to special-case MOVS, ADDS and LSLS, and keep track of 1159 // where we are in the sequence with the simplest of state machines. 1160 // 3) for Thumb2 execute only code we materialize the constant via 1161 // immediate constants in 2 separate instructions (MOVW/MOVT). 1162 SrcReg = ~0U; 1163 DstReg = MI->getOperand(0).getReg(); 1164 break; 1165 default: 1166 SrcReg = MI->getOperand(1).getReg(); 1167 DstReg = MI->getOperand(0).getReg(); 1168 break; 1169 } 1170 1171 // Try to figure out the unwinding opcode out of src / dst regs. 1172 if (MI->mayStore()) { 1173 // Register saves. 1174 assert(DstReg == ARM::SP && 1175 "Only stack pointer as a destination reg is supported"); 1176 1177 SmallVector<unsigned, 4> RegList; 1178 // Skip src & dst reg, and pred ops. 1179 unsigned StartOp = 2 + 2; 1180 // Use all the operands. 1181 unsigned NumOffset = 0; 1182 // Amount of SP adjustment folded into a push, before the 1183 // registers are stored (pad at higher addresses). 1184 unsigned PadBefore = 0; 1185 // Amount of SP adjustment folded into a push, after the 1186 // registers are stored (pad at lower addresses). 1187 unsigned PadAfter = 0; 1188 1189 switch (Opc) { 1190 default: 1191 MI->print(errs()); 1192 llvm_unreachable("Unsupported opcode for unwinding information"); 1193 case ARM::tPUSH: 1194 // Special case here: no src & dst reg, but two extra imp ops. 1195 StartOp = 2; NumOffset = 2; 1196 [[fallthrough]]; 1197 case ARM::STMDB_UPD: 1198 case ARM::t2STMDB_UPD: 1199 case ARM::VSTMDDB_UPD: 1200 assert(SrcReg == ARM::SP && 1201 "Only stack pointer as a source reg is supported"); 1202 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset; 1203 i != NumOps; ++i) { 1204 const MachineOperand &MO = MI->getOperand(i); 1205 // Actually, there should never be any impdef stuff here. Skip it 1206 // temporary to workaround PR11902. 1207 if (MO.isImplicit()) 1208 continue; 1209 // Registers, pushed as a part of folding an SP update into the 1210 // push instruction are marked as undef and should not be 1211 // restored when unwinding, because the function can modify the 1212 // corresponding stack slots. 1213 if (MO.isUndef()) { 1214 assert(RegList.empty() && 1215 "Pad registers must come before restored ones"); 1216 unsigned Width = 1217 TargetRegInfo->getRegSizeInBits(MO.getReg(), MachineRegInfo) / 8; 1218 PadAfter += Width; 1219 continue; 1220 } 1221 // Check for registers that are remapped (for a Thumb1 prologue that 1222 // saves high registers). 1223 Register Reg = MO.getReg(); 1224 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(Reg)) 1225 Reg = RemappedReg; 1226 RegList.push_back(Reg); 1227 } 1228 break; 1229 case ARM::STR_PRE_IMM: 1230 case ARM::STR_PRE_REG: 1231 case ARM::t2STR_PRE: 1232 assert(MI->getOperand(2).getReg() == ARM::SP && 1233 "Only stack pointer as a source reg is supported"); 1234 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg)) 1235 SrcReg = RemappedReg; 1236 1237 RegList.push_back(SrcReg); 1238 break; 1239 case ARM::t2STRD_PRE: 1240 assert(MI->getOperand(3).getReg() == ARM::SP && 1241 "Only stack pointer as a source reg is supported"); 1242 SrcReg = MI->getOperand(1).getReg(); 1243 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg)) 1244 SrcReg = RemappedReg; 1245 RegList.push_back(SrcReg); 1246 SrcReg = MI->getOperand(2).getReg(); 1247 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg)) 1248 SrcReg = RemappedReg; 1249 RegList.push_back(SrcReg); 1250 PadBefore = -MI->getOperand(4).getImm() - 8; 1251 break; 1252 } 1253 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) { 1254 if (PadBefore) 1255 ATS.emitPad(PadBefore); 1256 ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD); 1257 // Account for the SP adjustment, folded into the push. 1258 if (PadAfter) 1259 ATS.emitPad(PadAfter); 1260 } 1261 } else { 1262 // Changes of stack / frame pointer. 1263 if (SrcReg == ARM::SP) { 1264 int64_t Offset = 0; 1265 switch (Opc) { 1266 default: 1267 MI->print(errs()); 1268 llvm_unreachable("Unsupported opcode for unwinding information"); 1269 case ARM::MOVr: 1270 case ARM::tMOVr: 1271 Offset = 0; 1272 break; 1273 case ARM::ADDri: 1274 case ARM::t2ADDri: 1275 case ARM::t2ADDri12: 1276 case ARM::t2ADDspImm: 1277 case ARM::t2ADDspImm12: 1278 Offset = -MI->getOperand(2).getImm(); 1279 break; 1280 case ARM::SUBri: 1281 case ARM::t2SUBri: 1282 case ARM::t2SUBri12: 1283 case ARM::t2SUBspImm: 1284 case ARM::t2SUBspImm12: 1285 Offset = MI->getOperand(2).getImm(); 1286 break; 1287 case ARM::tSUBspi: 1288 Offset = MI->getOperand(2).getImm()*4; 1289 break; 1290 case ARM::tADDspi: 1291 case ARM::tADDrSPi: 1292 Offset = -MI->getOperand(2).getImm()*4; 1293 break; 1294 case ARM::tADDhirr: 1295 Offset = 1296 -AFI->EHPrologueOffsetInRegs.lookup(MI->getOperand(2).getReg()); 1297 break; 1298 } 1299 1300 if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) { 1301 if (DstReg == FramePtr && FramePtr != ARM::SP) 1302 // Set-up of the frame pointer. Positive values correspond to "add" 1303 // instruction. 1304 ATS.emitSetFP(FramePtr, ARM::SP, -Offset); 1305 else if (DstReg == ARM::SP) { 1306 // Change of SP by an offset. Positive values correspond to "sub" 1307 // instruction. 1308 ATS.emitPad(Offset); 1309 } else { 1310 // Move of SP to a register. Positive values correspond to an "add" 1311 // instruction. 1312 ATS.emitMovSP(DstReg, -Offset); 1313 } 1314 } 1315 } else if (DstReg == ARM::SP) { 1316 MI->print(errs()); 1317 llvm_unreachable("Unsupported opcode for unwinding information"); 1318 } else { 1319 int64_t Offset = 0; 1320 switch (Opc) { 1321 case ARM::tMOVr: 1322 // If a Thumb1 function spills r8-r11, we copy the values to low 1323 // registers before pushing them. Record the copy so we can emit the 1324 // correct ".save" later. 1325 AFI->EHPrologueRemappedRegs[DstReg] = SrcReg; 1326 break; 1327 case ARM::tLDRpci: { 1328 // Grab the constpool index and check, whether it corresponds to 1329 // original or cloned constpool entry. 1330 unsigned CPI = MI->getOperand(1).getIndex(); 1331 const MachineConstantPool *MCP = MF.getConstantPool(); 1332 if (CPI >= MCP->getConstants().size()) 1333 CPI = AFI->getOriginalCPIdx(CPI); 1334 assert(CPI != -1U && "Invalid constpool index"); 1335 1336 // Derive the actual offset. 1337 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI]; 1338 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry"); 1339 Offset = cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue(); 1340 AFI->EHPrologueOffsetInRegs[DstReg] = Offset; 1341 break; 1342 } 1343 case ARM::t2MOVi16: 1344 Offset = MI->getOperand(1).getImm(); 1345 AFI->EHPrologueOffsetInRegs[DstReg] = Offset; 1346 break; 1347 case ARM::t2MOVTi16: 1348 Offset = MI->getOperand(2).getImm(); 1349 AFI->EHPrologueOffsetInRegs[DstReg] |= (Offset << 16); 1350 break; 1351 case ARM::tMOVi8: 1352 Offset = MI->getOperand(2).getImm(); 1353 AFI->EHPrologueOffsetInRegs[DstReg] = Offset; 1354 break; 1355 case ARM::tLSLri: 1356 assert(MI->getOperand(3).getImm() == 8 && 1357 "The shift amount is not equal to 8"); 1358 assert(MI->getOperand(2).getReg() == MI->getOperand(0).getReg() && 1359 "The source register is not equal to the destination register"); 1360 AFI->EHPrologueOffsetInRegs[DstReg] <<= 8; 1361 break; 1362 case ARM::tADDi8: 1363 assert(MI->getOperand(2).getReg() == MI->getOperand(0).getReg() && 1364 "The source register is not equal to the destination register"); 1365 Offset = MI->getOperand(3).getImm(); 1366 AFI->EHPrologueOffsetInRegs[DstReg] += Offset; 1367 break; 1368 case ARM::t2PAC: 1369 case ARM::t2PACBTI: 1370 AFI->EHPrologueRemappedRegs[ARM::R12] = ARM::RA_AUTH_CODE; 1371 break; 1372 default: 1373 MI->print(errs()); 1374 llvm_unreachable("Unsupported opcode for unwinding information"); 1375 } 1376 } 1377 } 1378 } 1379 1380 // Simple pseudo-instructions have their lowering (with expansion to real 1381 // instructions) auto-generated. 1382 #include "ARMGenMCPseudoLowering.inc" 1383 1384 void ARMAsmPrinter::emitInstruction(const MachineInstr *MI) { 1385 // TODOD FIXME: Enable feature predicate checks once all the test pass. 1386 // ARM_MC::verifyInstructionPredicates(MI->getOpcode(), 1387 // getSubtargetInfo().getFeatureBits()); 1388 1389 const DataLayout &DL = getDataLayout(); 1390 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer(); 1391 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS); 1392 1393 // If we just ended a constant pool, mark it as such. 1394 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) { 1395 OutStreamer->emitDataRegion(MCDR_DataRegionEnd); 1396 InConstantPool = false; 1397 } 1398 1399 // Emit unwinding stuff for frame-related instructions 1400 if (Subtarget->isTargetEHABICompatible() && 1401 MI->getFlag(MachineInstr::FrameSetup)) 1402 EmitUnwindingInstruction(MI); 1403 1404 // Do any auto-generated pseudo lowerings. 1405 if (emitPseudoExpansionLowering(*OutStreamer, MI)) 1406 return; 1407 1408 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) && 1409 "Pseudo flag setting opcode should be expanded early"); 1410 1411 // Check for manual lowerings. 1412 unsigned Opc = MI->getOpcode(); 1413 switch (Opc) { 1414 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass"); 1415 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing"); 1416 case ARM::LEApcrel: 1417 case ARM::tLEApcrel: 1418 case ARM::t2LEApcrel: { 1419 // FIXME: Need to also handle globals and externals 1420 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex()); 1421 EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() == 1422 ARM::t2LEApcrel ? ARM::t2ADR 1423 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR 1424 : ARM::ADR)) 1425 .addReg(MI->getOperand(0).getReg()) 1426 .addExpr(MCSymbolRefExpr::create(CPISymbol, OutContext)) 1427 // Add predicate operands. 1428 .addImm(MI->getOperand(2).getImm()) 1429 .addReg(MI->getOperand(3).getReg())); 1430 return; 1431 } 1432 case ARM::LEApcrelJT: 1433 case ARM::tLEApcrelJT: 1434 case ARM::t2LEApcrelJT: { 1435 MCSymbol *JTIPICSymbol = 1436 GetARMJTIPICJumpTableLabel(MI->getOperand(1).getIndex()); 1437 EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() == 1438 ARM::t2LEApcrelJT ? ARM::t2ADR 1439 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR 1440 : ARM::ADR)) 1441 .addReg(MI->getOperand(0).getReg()) 1442 .addExpr(MCSymbolRefExpr::create(JTIPICSymbol, OutContext)) 1443 // Add predicate operands. 1444 .addImm(MI->getOperand(2).getImm()) 1445 .addReg(MI->getOperand(3).getReg())); 1446 return; 1447 } 1448 // Darwin call instructions are just normal call instructions with different 1449 // clobber semantics (they clobber R9). 1450 case ARM::BX_CALL: { 1451 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr) 1452 .addReg(ARM::LR) 1453 .addReg(ARM::PC) 1454 // Add predicate operands. 1455 .addImm(ARMCC::AL) 1456 .addReg(0) 1457 // Add 's' bit operand (always reg0 for this) 1458 .addReg(0)); 1459 1460 assert(Subtarget->hasV4TOps()); 1461 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX) 1462 .addReg(MI->getOperand(0).getReg())); 1463 return; 1464 } 1465 case ARM::tBX_CALL: { 1466 if (Subtarget->hasV5TOps()) 1467 llvm_unreachable("Expected BLX to be selected for v5t+"); 1468 1469 // On ARM v4t, when doing a call from thumb mode, we need to ensure 1470 // that the saved lr has its LSB set correctly (the arch doesn't 1471 // have blx). 1472 // So here we generate a bl to a small jump pad that does bx rN. 1473 // The jump pads are emitted after the function body. 1474 1475 Register TReg = MI->getOperand(0).getReg(); 1476 MCSymbol *TRegSym = nullptr; 1477 for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) { 1478 if (TIP.first == TReg) { 1479 TRegSym = TIP.second; 1480 break; 1481 } 1482 } 1483 1484 if (!TRegSym) { 1485 TRegSym = OutContext.createTempSymbol(); 1486 ThumbIndirectPads.push_back(std::make_pair(TReg, TRegSym)); 1487 } 1488 1489 // Create a link-saving branch to the Reg Indirect Jump Pad. 1490 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBL) 1491 // Predicate comes first here. 1492 .addImm(ARMCC::AL).addReg(0) 1493 .addExpr(MCSymbolRefExpr::create(TRegSym, OutContext))); 1494 return; 1495 } 1496 case ARM::BMOVPCRX_CALL: { 1497 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr) 1498 .addReg(ARM::LR) 1499 .addReg(ARM::PC) 1500 // Add predicate operands. 1501 .addImm(ARMCC::AL) 1502 .addReg(0) 1503 // Add 's' bit operand (always reg0 for this) 1504 .addReg(0)); 1505 1506 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr) 1507 .addReg(ARM::PC) 1508 .addReg(MI->getOperand(0).getReg()) 1509 // Add predicate operands. 1510 .addImm(ARMCC::AL) 1511 .addReg(0) 1512 // Add 's' bit operand (always reg0 for this) 1513 .addReg(0)); 1514 return; 1515 } 1516 case ARM::BMOVPCB_CALL: { 1517 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr) 1518 .addReg(ARM::LR) 1519 .addReg(ARM::PC) 1520 // Add predicate operands. 1521 .addImm(ARMCC::AL) 1522 .addReg(0) 1523 // Add 's' bit operand (always reg0 for this) 1524 .addReg(0)); 1525 1526 const MachineOperand &Op = MI->getOperand(0); 1527 const GlobalValue *GV = Op.getGlobal(); 1528 const unsigned TF = Op.getTargetFlags(); 1529 MCSymbol *GVSym = GetARMGVSymbol(GV, TF); 1530 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext); 1531 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc) 1532 .addExpr(GVSymExpr) 1533 // Add predicate operands. 1534 .addImm(ARMCC::AL) 1535 .addReg(0)); 1536 return; 1537 } 1538 case ARM::MOVi16_ga_pcrel: 1539 case ARM::t2MOVi16_ga_pcrel: { 1540 MCInst TmpInst; 1541 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16); 1542 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); 1543 1544 unsigned TF = MI->getOperand(1).getTargetFlags(); 1545 const GlobalValue *GV = MI->getOperand(1).getGlobal(); 1546 MCSymbol *GVSym = GetARMGVSymbol(GV, TF); 1547 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext); 1548 1549 MCSymbol *LabelSym = 1550 getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(), 1551 MI->getOperand(2).getImm(), OutContext); 1552 const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext); 1553 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4; 1554 const MCExpr *PCRelExpr = 1555 ARMMCExpr::createLower16(MCBinaryExpr::createSub(GVSymExpr, 1556 MCBinaryExpr::createAdd(LabelSymExpr, 1557 MCConstantExpr::create(PCAdj, OutContext), 1558 OutContext), OutContext), OutContext); 1559 TmpInst.addOperand(MCOperand::createExpr(PCRelExpr)); 1560 1561 // Add predicate operands. 1562 TmpInst.addOperand(MCOperand::createImm(ARMCC::AL)); 1563 TmpInst.addOperand(MCOperand::createReg(0)); 1564 // Add 's' bit operand (always reg0 for this) 1565 TmpInst.addOperand(MCOperand::createReg(0)); 1566 EmitToStreamer(*OutStreamer, TmpInst); 1567 return; 1568 } 1569 case ARM::MOVTi16_ga_pcrel: 1570 case ARM::t2MOVTi16_ga_pcrel: { 1571 MCInst TmpInst; 1572 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel 1573 ? ARM::MOVTi16 : ARM::t2MOVTi16); 1574 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); 1575 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg())); 1576 1577 unsigned TF = MI->getOperand(2).getTargetFlags(); 1578 const GlobalValue *GV = MI->getOperand(2).getGlobal(); 1579 MCSymbol *GVSym = GetARMGVSymbol(GV, TF); 1580 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext); 1581 1582 MCSymbol *LabelSym = 1583 getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(), 1584 MI->getOperand(3).getImm(), OutContext); 1585 const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext); 1586 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4; 1587 const MCExpr *PCRelExpr = 1588 ARMMCExpr::createUpper16(MCBinaryExpr::createSub(GVSymExpr, 1589 MCBinaryExpr::createAdd(LabelSymExpr, 1590 MCConstantExpr::create(PCAdj, OutContext), 1591 OutContext), OutContext), OutContext); 1592 TmpInst.addOperand(MCOperand::createExpr(PCRelExpr)); 1593 // Add predicate operands. 1594 TmpInst.addOperand(MCOperand::createImm(ARMCC::AL)); 1595 TmpInst.addOperand(MCOperand::createReg(0)); 1596 // Add 's' bit operand (always reg0 for this) 1597 TmpInst.addOperand(MCOperand::createReg(0)); 1598 EmitToStreamer(*OutStreamer, TmpInst); 1599 return; 1600 } 1601 case ARM::t2BFi: 1602 case ARM::t2BFic: 1603 case ARM::t2BFLi: 1604 case ARM::t2BFr: 1605 case ARM::t2BFLr: { 1606 // This is a Branch Future instruction. 1607 1608 const MCExpr *BranchLabel = MCSymbolRefExpr::create( 1609 getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(), 1610 MI->getOperand(0).getIndex(), OutContext), 1611 OutContext); 1612 1613 auto MCInst = MCInstBuilder(Opc).addExpr(BranchLabel); 1614 if (MI->getOperand(1).isReg()) { 1615 // For BFr/BFLr 1616 MCInst.addReg(MI->getOperand(1).getReg()); 1617 } else { 1618 // For BFi/BFLi/BFic 1619 const MCExpr *BranchTarget; 1620 if (MI->getOperand(1).isMBB()) 1621 BranchTarget = MCSymbolRefExpr::create( 1622 MI->getOperand(1).getMBB()->getSymbol(), OutContext); 1623 else if (MI->getOperand(1).isGlobal()) { 1624 const GlobalValue *GV = MI->getOperand(1).getGlobal(); 1625 BranchTarget = MCSymbolRefExpr::create( 1626 GetARMGVSymbol(GV, MI->getOperand(1).getTargetFlags()), OutContext); 1627 } else if (MI->getOperand(1).isSymbol()) { 1628 BranchTarget = MCSymbolRefExpr::create( 1629 GetExternalSymbolSymbol(MI->getOperand(1).getSymbolName()), 1630 OutContext); 1631 } else 1632 llvm_unreachable("Unhandled operand kind in Branch Future instruction"); 1633 1634 MCInst.addExpr(BranchTarget); 1635 } 1636 1637 if (Opc == ARM::t2BFic) { 1638 const MCExpr *ElseLabel = MCSymbolRefExpr::create( 1639 getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(), 1640 MI->getOperand(2).getIndex(), OutContext), 1641 OutContext); 1642 MCInst.addExpr(ElseLabel); 1643 MCInst.addImm(MI->getOperand(3).getImm()); 1644 } else { 1645 MCInst.addImm(MI->getOperand(2).getImm()) 1646 .addReg(MI->getOperand(3).getReg()); 1647 } 1648 1649 EmitToStreamer(*OutStreamer, MCInst); 1650 return; 1651 } 1652 case ARM::t2BF_LabelPseudo: { 1653 // This is a pseudo op for a label used by a branch future instruction 1654 1655 // Emit the label. 1656 OutStreamer->emitLabel(getBFLabel(DL.getPrivateGlobalPrefix(), 1657 getFunctionNumber(), 1658 MI->getOperand(0).getIndex(), OutContext)); 1659 return; 1660 } 1661 case ARM::tPICADD: { 1662 // This is a pseudo op for a label + instruction sequence, which looks like: 1663 // LPC0: 1664 // add r0, pc 1665 // This adds the address of LPC0 to r0. 1666 1667 // Emit the label. 1668 OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(), 1669 getFunctionNumber(), 1670 MI->getOperand(2).getImm(), OutContext)); 1671 1672 // Form and emit the add. 1673 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr) 1674 .addReg(MI->getOperand(0).getReg()) 1675 .addReg(MI->getOperand(0).getReg()) 1676 .addReg(ARM::PC) 1677 // Add predicate operands. 1678 .addImm(ARMCC::AL) 1679 .addReg(0)); 1680 return; 1681 } 1682 case ARM::PICADD: { 1683 // This is a pseudo op for a label + instruction sequence, which looks like: 1684 // LPC0: 1685 // add r0, pc, r0 1686 // This adds the address of LPC0 to r0. 1687 1688 // Emit the label. 1689 OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(), 1690 getFunctionNumber(), 1691 MI->getOperand(2).getImm(), OutContext)); 1692 1693 // Form and emit the add. 1694 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr) 1695 .addReg(MI->getOperand(0).getReg()) 1696 .addReg(ARM::PC) 1697 .addReg(MI->getOperand(1).getReg()) 1698 // Add predicate operands. 1699 .addImm(MI->getOperand(3).getImm()) 1700 .addReg(MI->getOperand(4).getReg()) 1701 // Add 's' bit operand (always reg0 for this) 1702 .addReg(0)); 1703 return; 1704 } 1705 case ARM::PICSTR: 1706 case ARM::PICSTRB: 1707 case ARM::PICSTRH: 1708 case ARM::PICLDR: 1709 case ARM::PICLDRB: 1710 case ARM::PICLDRH: 1711 case ARM::PICLDRSB: 1712 case ARM::PICLDRSH: { 1713 // This is a pseudo op for a label + instruction sequence, which looks like: 1714 // LPC0: 1715 // OP r0, [pc, r0] 1716 // The LCP0 label is referenced by a constant pool entry in order to get 1717 // a PC-relative address at the ldr instruction. 1718 1719 // Emit the label. 1720 OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(), 1721 getFunctionNumber(), 1722 MI->getOperand(2).getImm(), OutContext)); 1723 1724 // Form and emit the load 1725 unsigned Opcode; 1726 switch (MI->getOpcode()) { 1727 default: 1728 llvm_unreachable("Unexpected opcode!"); 1729 case ARM::PICSTR: Opcode = ARM::STRrs; break; 1730 case ARM::PICSTRB: Opcode = ARM::STRBrs; break; 1731 case ARM::PICSTRH: Opcode = ARM::STRH; break; 1732 case ARM::PICLDR: Opcode = ARM::LDRrs; break; 1733 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break; 1734 case ARM::PICLDRH: Opcode = ARM::LDRH; break; 1735 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break; 1736 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break; 1737 } 1738 EmitToStreamer(*OutStreamer, MCInstBuilder(Opcode) 1739 .addReg(MI->getOperand(0).getReg()) 1740 .addReg(ARM::PC) 1741 .addReg(MI->getOperand(1).getReg()) 1742 .addImm(0) 1743 // Add predicate operands. 1744 .addImm(MI->getOperand(3).getImm()) 1745 .addReg(MI->getOperand(4).getReg())); 1746 1747 return; 1748 } 1749 case ARM::CONSTPOOL_ENTRY: { 1750 if (Subtarget->genExecuteOnly()) 1751 llvm_unreachable("execute-only should not generate constant pools"); 1752 1753 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool 1754 /// in the function. The first operand is the ID# for this instruction, the 1755 /// second is the index into the MachineConstantPool that this is, the third 1756 /// is the size in bytes of this constant pool entry. 1757 /// The required alignment is specified on the basic block holding this MI. 1758 unsigned LabelId = (unsigned)MI->getOperand(0).getImm(); 1759 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex(); 1760 1761 // If this is the first entry of the pool, mark it. 1762 if (!InConstantPool) { 1763 OutStreamer->emitDataRegion(MCDR_DataRegion); 1764 InConstantPool = true; 1765 } 1766 1767 OutStreamer->emitLabel(GetCPISymbol(LabelId)); 1768 1769 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx]; 1770 if (MCPE.isMachineConstantPoolEntry()) 1771 emitMachineConstantPoolValue(MCPE.Val.MachineCPVal); 1772 else 1773 emitGlobalConstant(DL, MCPE.Val.ConstVal); 1774 return; 1775 } 1776 case ARM::JUMPTABLE_ADDRS: 1777 emitJumpTableAddrs(MI); 1778 return; 1779 case ARM::JUMPTABLE_INSTS: 1780 emitJumpTableInsts(MI); 1781 return; 1782 case ARM::JUMPTABLE_TBB: 1783 case ARM::JUMPTABLE_TBH: 1784 emitJumpTableTBInst(MI, MI->getOpcode() == ARM::JUMPTABLE_TBB ? 1 : 2); 1785 return; 1786 case ARM::t2BR_JT: { 1787 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr) 1788 .addReg(ARM::PC) 1789 .addReg(MI->getOperand(0).getReg()) 1790 // Add predicate operands. 1791 .addImm(ARMCC::AL) 1792 .addReg(0)); 1793 return; 1794 } 1795 case ARM::t2TBB_JT: 1796 case ARM::t2TBH_JT: { 1797 unsigned Opc = MI->getOpcode() == ARM::t2TBB_JT ? ARM::t2TBB : ARM::t2TBH; 1798 // Lower and emit the PC label, then the instruction itself. 1799 OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm())); 1800 EmitToStreamer(*OutStreamer, MCInstBuilder(Opc) 1801 .addReg(MI->getOperand(0).getReg()) 1802 .addReg(MI->getOperand(1).getReg()) 1803 // Add predicate operands. 1804 .addImm(ARMCC::AL) 1805 .addReg(0)); 1806 return; 1807 } 1808 case ARM::tTBB_JT: 1809 case ARM::tTBH_JT: { 1810 1811 bool Is8Bit = MI->getOpcode() == ARM::tTBB_JT; 1812 Register Base = MI->getOperand(0).getReg(); 1813 Register Idx = MI->getOperand(1).getReg(); 1814 assert(MI->getOperand(1).isKill() && "We need the index register as scratch!"); 1815 1816 // Multiply up idx if necessary. 1817 if (!Is8Bit) 1818 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri) 1819 .addReg(Idx) 1820 .addReg(ARM::CPSR) 1821 .addReg(Idx) 1822 .addImm(1) 1823 // Add predicate operands. 1824 .addImm(ARMCC::AL) 1825 .addReg(0)); 1826 1827 if (Base == ARM::PC) { 1828 // TBB [base, idx] = 1829 // ADDS idx, idx, base 1830 // LDRB idx, [idx, #4] ; or LDRH if TBH 1831 // LSLS idx, #1 1832 // ADDS pc, pc, idx 1833 1834 // When using PC as the base, it's important that there is no padding 1835 // between the last ADDS and the start of the jump table. The jump table 1836 // is 4-byte aligned, so we ensure we're 4 byte aligned here too. 1837 // 1838 // FIXME: Ideally we could vary the LDRB index based on the padding 1839 // between the sequence and jump table, however that relies on MCExprs 1840 // for load indexes which are currently not supported. 1841 OutStreamer->emitCodeAlignment(Align(4), &getSubtargetInfo()); 1842 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr) 1843 .addReg(Idx) 1844 .addReg(Idx) 1845 .addReg(Base) 1846 // Add predicate operands. 1847 .addImm(ARMCC::AL) 1848 .addReg(0)); 1849 1850 unsigned Opc = Is8Bit ? ARM::tLDRBi : ARM::tLDRHi; 1851 EmitToStreamer(*OutStreamer, MCInstBuilder(Opc) 1852 .addReg(Idx) 1853 .addReg(Idx) 1854 .addImm(Is8Bit ? 4 : 2) 1855 // Add predicate operands. 1856 .addImm(ARMCC::AL) 1857 .addReg(0)); 1858 } else { 1859 // TBB [base, idx] = 1860 // LDRB idx, [base, idx] ; or LDRH if TBH 1861 // LSLS idx, #1 1862 // ADDS pc, pc, idx 1863 1864 unsigned Opc = Is8Bit ? ARM::tLDRBr : ARM::tLDRHr; 1865 EmitToStreamer(*OutStreamer, MCInstBuilder(Opc) 1866 .addReg(Idx) 1867 .addReg(Base) 1868 .addReg(Idx) 1869 // Add predicate operands. 1870 .addImm(ARMCC::AL) 1871 .addReg(0)); 1872 } 1873 1874 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri) 1875 .addReg(Idx) 1876 .addReg(ARM::CPSR) 1877 .addReg(Idx) 1878 .addImm(1) 1879 // Add predicate operands. 1880 .addImm(ARMCC::AL) 1881 .addReg(0)); 1882 1883 OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm())); 1884 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr) 1885 .addReg(ARM::PC) 1886 .addReg(ARM::PC) 1887 .addReg(Idx) 1888 // Add predicate operands. 1889 .addImm(ARMCC::AL) 1890 .addReg(0)); 1891 return; 1892 } 1893 case ARM::tBR_JTr: 1894 case ARM::BR_JTr: { 1895 // mov pc, target 1896 MCInst TmpInst; 1897 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ? 1898 ARM::MOVr : ARM::tMOVr; 1899 TmpInst.setOpcode(Opc); 1900 TmpInst.addOperand(MCOperand::createReg(ARM::PC)); 1901 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); 1902 // Add predicate operands. 1903 TmpInst.addOperand(MCOperand::createImm(ARMCC::AL)); 1904 TmpInst.addOperand(MCOperand::createReg(0)); 1905 // Add 's' bit operand (always reg0 for this) 1906 if (Opc == ARM::MOVr) 1907 TmpInst.addOperand(MCOperand::createReg(0)); 1908 EmitToStreamer(*OutStreamer, TmpInst); 1909 return; 1910 } 1911 case ARM::BR_JTm_i12: { 1912 // ldr pc, target 1913 MCInst TmpInst; 1914 TmpInst.setOpcode(ARM::LDRi12); 1915 TmpInst.addOperand(MCOperand::createReg(ARM::PC)); 1916 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); 1917 TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm())); 1918 // Add predicate operands. 1919 TmpInst.addOperand(MCOperand::createImm(ARMCC::AL)); 1920 TmpInst.addOperand(MCOperand::createReg(0)); 1921 EmitToStreamer(*OutStreamer, TmpInst); 1922 return; 1923 } 1924 case ARM::BR_JTm_rs: { 1925 // ldr pc, target 1926 MCInst TmpInst; 1927 TmpInst.setOpcode(ARM::LDRrs); 1928 TmpInst.addOperand(MCOperand::createReg(ARM::PC)); 1929 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); 1930 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg())); 1931 TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm())); 1932 // Add predicate operands. 1933 TmpInst.addOperand(MCOperand::createImm(ARMCC::AL)); 1934 TmpInst.addOperand(MCOperand::createReg(0)); 1935 EmitToStreamer(*OutStreamer, TmpInst); 1936 return; 1937 } 1938 case ARM::BR_JTadd: { 1939 // add pc, target, idx 1940 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr) 1941 .addReg(ARM::PC) 1942 .addReg(MI->getOperand(0).getReg()) 1943 .addReg(MI->getOperand(1).getReg()) 1944 // Add predicate operands. 1945 .addImm(ARMCC::AL) 1946 .addReg(0) 1947 // Add 's' bit operand (always reg0 for this) 1948 .addReg(0)); 1949 return; 1950 } 1951 case ARM::SPACE: 1952 OutStreamer->emitZeros(MI->getOperand(1).getImm()); 1953 return; 1954 case ARM::TRAP: { 1955 // Non-Darwin binutils don't yet support the "trap" mnemonic. 1956 // FIXME: Remove this special case when they do. 1957 if (!Subtarget->isTargetMachO()) { 1958 uint32_t Val = 0xe7ffdefeUL; 1959 OutStreamer->AddComment("trap"); 1960 ATS.emitInst(Val); 1961 return; 1962 } 1963 break; 1964 } 1965 case ARM::TRAPNaCl: { 1966 uint32_t Val = 0xe7fedef0UL; 1967 OutStreamer->AddComment("trap"); 1968 ATS.emitInst(Val); 1969 return; 1970 } 1971 case ARM::tTRAP: { 1972 // Non-Darwin binutils don't yet support the "trap" mnemonic. 1973 // FIXME: Remove this special case when they do. 1974 if (!Subtarget->isTargetMachO()) { 1975 uint16_t Val = 0xdefe; 1976 OutStreamer->AddComment("trap"); 1977 ATS.emitInst(Val, 'n'); 1978 return; 1979 } 1980 break; 1981 } 1982 case ARM::t2Int_eh_sjlj_setjmp: 1983 case ARM::t2Int_eh_sjlj_setjmp_nofp: 1984 case ARM::tInt_eh_sjlj_setjmp: { 1985 // Two incoming args: GPR:$src, GPR:$val 1986 // mov $val, pc 1987 // adds $val, #7 1988 // str $val, [$src, #4] 1989 // movs r0, #0 1990 // b LSJLJEH 1991 // movs r0, #1 1992 // LSJLJEH: 1993 Register SrcReg = MI->getOperand(0).getReg(); 1994 Register ValReg = MI->getOperand(1).getReg(); 1995 MCSymbol *Label = OutContext.createTempSymbol("SJLJEH"); 1996 OutStreamer->AddComment("eh_setjmp begin"); 1997 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr) 1998 .addReg(ValReg) 1999 .addReg(ARM::PC) 2000 // Predicate. 2001 .addImm(ARMCC::AL) 2002 .addReg(0)); 2003 2004 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi3) 2005 .addReg(ValReg) 2006 // 's' bit operand 2007 .addReg(ARM::CPSR) 2008 .addReg(ValReg) 2009 .addImm(7) 2010 // Predicate. 2011 .addImm(ARMCC::AL) 2012 .addReg(0)); 2013 2014 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tSTRi) 2015 .addReg(ValReg) 2016 .addReg(SrcReg) 2017 // The offset immediate is #4. The operand value is scaled by 4 for the 2018 // tSTR instruction. 2019 .addImm(1) 2020 // Predicate. 2021 .addImm(ARMCC::AL) 2022 .addReg(0)); 2023 2024 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8) 2025 .addReg(ARM::R0) 2026 .addReg(ARM::CPSR) 2027 .addImm(0) 2028 // Predicate. 2029 .addImm(ARMCC::AL) 2030 .addReg(0)); 2031 2032 const MCExpr *SymbolExpr = MCSymbolRefExpr::create(Label, OutContext); 2033 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tB) 2034 .addExpr(SymbolExpr) 2035 .addImm(ARMCC::AL) 2036 .addReg(0)); 2037 2038 OutStreamer->AddComment("eh_setjmp end"); 2039 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8) 2040 .addReg(ARM::R0) 2041 .addReg(ARM::CPSR) 2042 .addImm(1) 2043 // Predicate. 2044 .addImm(ARMCC::AL) 2045 .addReg(0)); 2046 2047 OutStreamer->emitLabel(Label); 2048 return; 2049 } 2050 2051 case ARM::Int_eh_sjlj_setjmp_nofp: 2052 case ARM::Int_eh_sjlj_setjmp: { 2053 // Two incoming args: GPR:$src, GPR:$val 2054 // add $val, pc, #8 2055 // str $val, [$src, #+4] 2056 // mov r0, #0 2057 // add pc, pc, #0 2058 // mov r0, #1 2059 Register SrcReg = MI->getOperand(0).getReg(); 2060 Register ValReg = MI->getOperand(1).getReg(); 2061 2062 OutStreamer->AddComment("eh_setjmp begin"); 2063 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri) 2064 .addReg(ValReg) 2065 .addReg(ARM::PC) 2066 .addImm(8) 2067 // Predicate. 2068 .addImm(ARMCC::AL) 2069 .addReg(0) 2070 // 's' bit operand (always reg0 for this). 2071 .addReg(0)); 2072 2073 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::STRi12) 2074 .addReg(ValReg) 2075 .addReg(SrcReg) 2076 .addImm(4) 2077 // Predicate. 2078 .addImm(ARMCC::AL) 2079 .addReg(0)); 2080 2081 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi) 2082 .addReg(ARM::R0) 2083 .addImm(0) 2084 // Predicate. 2085 .addImm(ARMCC::AL) 2086 .addReg(0) 2087 // 's' bit operand (always reg0 for this). 2088 .addReg(0)); 2089 2090 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri) 2091 .addReg(ARM::PC) 2092 .addReg(ARM::PC) 2093 .addImm(0) 2094 // Predicate. 2095 .addImm(ARMCC::AL) 2096 .addReg(0) 2097 // 's' bit operand (always reg0 for this). 2098 .addReg(0)); 2099 2100 OutStreamer->AddComment("eh_setjmp end"); 2101 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi) 2102 .addReg(ARM::R0) 2103 .addImm(1) 2104 // Predicate. 2105 .addImm(ARMCC::AL) 2106 .addReg(0) 2107 // 's' bit operand (always reg0 for this). 2108 .addReg(0)); 2109 return; 2110 } 2111 case ARM::Int_eh_sjlj_longjmp: { 2112 // ldr sp, [$src, #8] 2113 // ldr $scratch, [$src, #4] 2114 // ldr r7, [$src] 2115 // bx $scratch 2116 Register SrcReg = MI->getOperand(0).getReg(); 2117 Register ScratchReg = MI->getOperand(1).getReg(); 2118 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12) 2119 .addReg(ARM::SP) 2120 .addReg(SrcReg) 2121 .addImm(8) 2122 // Predicate. 2123 .addImm(ARMCC::AL) 2124 .addReg(0)); 2125 2126 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12) 2127 .addReg(ScratchReg) 2128 .addReg(SrcReg) 2129 .addImm(4) 2130 // Predicate. 2131 .addImm(ARMCC::AL) 2132 .addReg(0)); 2133 2134 const MachineFunction &MF = *MI->getParent()->getParent(); 2135 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>(); 2136 2137 if (STI.isTargetDarwin() || STI.isTargetWindows()) { 2138 // These platforms always use the same frame register 2139 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12) 2140 .addReg(STI.getFramePointerReg()) 2141 .addReg(SrcReg) 2142 .addImm(0) 2143 // Predicate. 2144 .addImm(ARMCC::AL) 2145 .addReg(0)); 2146 } else { 2147 // If the calling code might use either R7 or R11 as 2148 // frame pointer register, restore it into both. 2149 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12) 2150 .addReg(ARM::R7) 2151 .addReg(SrcReg) 2152 .addImm(0) 2153 // Predicate. 2154 .addImm(ARMCC::AL) 2155 .addReg(0)); 2156 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12) 2157 .addReg(ARM::R11) 2158 .addReg(SrcReg) 2159 .addImm(0) 2160 // Predicate. 2161 .addImm(ARMCC::AL) 2162 .addReg(0)); 2163 } 2164 2165 assert(Subtarget->hasV4TOps()); 2166 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX) 2167 .addReg(ScratchReg) 2168 // Predicate. 2169 .addImm(ARMCC::AL) 2170 .addReg(0)); 2171 return; 2172 } 2173 case ARM::tInt_eh_sjlj_longjmp: { 2174 // ldr $scratch, [$src, #8] 2175 // mov sp, $scratch 2176 // ldr $scratch, [$src, #4] 2177 // ldr r7, [$src] 2178 // bx $scratch 2179 Register SrcReg = MI->getOperand(0).getReg(); 2180 Register ScratchReg = MI->getOperand(1).getReg(); 2181 2182 const MachineFunction &MF = *MI->getParent()->getParent(); 2183 const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>(); 2184 2185 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi) 2186 .addReg(ScratchReg) 2187 .addReg(SrcReg) 2188 // The offset immediate is #8. The operand value is scaled by 4 for the 2189 // tLDR instruction. 2190 .addImm(2) 2191 // Predicate. 2192 .addImm(ARMCC::AL) 2193 .addReg(0)); 2194 2195 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr) 2196 .addReg(ARM::SP) 2197 .addReg(ScratchReg) 2198 // Predicate. 2199 .addImm(ARMCC::AL) 2200 .addReg(0)); 2201 2202 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi) 2203 .addReg(ScratchReg) 2204 .addReg(SrcReg) 2205 .addImm(1) 2206 // Predicate. 2207 .addImm(ARMCC::AL) 2208 .addReg(0)); 2209 2210 if (STI.isTargetDarwin() || STI.isTargetWindows()) { 2211 // These platforms always use the same frame register 2212 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi) 2213 .addReg(STI.getFramePointerReg()) 2214 .addReg(SrcReg) 2215 .addImm(0) 2216 // Predicate. 2217 .addImm(ARMCC::AL) 2218 .addReg(0)); 2219 } else { 2220 // If the calling code might use either R7 or R11 as 2221 // frame pointer register, restore it into both. 2222 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi) 2223 .addReg(ARM::R7) 2224 .addReg(SrcReg) 2225 .addImm(0) 2226 // Predicate. 2227 .addImm(ARMCC::AL) 2228 .addReg(0)); 2229 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi) 2230 .addReg(ARM::R11) 2231 .addReg(SrcReg) 2232 .addImm(0) 2233 // Predicate. 2234 .addImm(ARMCC::AL) 2235 .addReg(0)); 2236 } 2237 2238 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX) 2239 .addReg(ScratchReg) 2240 // Predicate. 2241 .addImm(ARMCC::AL) 2242 .addReg(0)); 2243 return; 2244 } 2245 case ARM::tInt_WIN_eh_sjlj_longjmp: { 2246 // ldr.w r11, [$src, #0] 2247 // ldr.w sp, [$src, #8] 2248 // ldr.w pc, [$src, #4] 2249 2250 Register SrcReg = MI->getOperand(0).getReg(); 2251 2252 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12) 2253 .addReg(ARM::R11) 2254 .addReg(SrcReg) 2255 .addImm(0) 2256 // Predicate 2257 .addImm(ARMCC::AL) 2258 .addReg(0)); 2259 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12) 2260 .addReg(ARM::SP) 2261 .addReg(SrcReg) 2262 .addImm(8) 2263 // Predicate 2264 .addImm(ARMCC::AL) 2265 .addReg(0)); 2266 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12) 2267 .addReg(ARM::PC) 2268 .addReg(SrcReg) 2269 .addImm(4) 2270 // Predicate 2271 .addImm(ARMCC::AL) 2272 .addReg(0)); 2273 return; 2274 } 2275 case ARM::PATCHABLE_FUNCTION_ENTER: 2276 LowerPATCHABLE_FUNCTION_ENTER(*MI); 2277 return; 2278 case ARM::PATCHABLE_FUNCTION_EXIT: 2279 LowerPATCHABLE_FUNCTION_EXIT(*MI); 2280 return; 2281 case ARM::PATCHABLE_TAIL_CALL: 2282 LowerPATCHABLE_TAIL_CALL(*MI); 2283 return; 2284 case ARM::SpeculationBarrierISBDSBEndBB: { 2285 // Print DSB SYS + ISB 2286 MCInst TmpInstDSB; 2287 TmpInstDSB.setOpcode(ARM::DSB); 2288 TmpInstDSB.addOperand(MCOperand::createImm(0xf)); 2289 EmitToStreamer(*OutStreamer, TmpInstDSB); 2290 MCInst TmpInstISB; 2291 TmpInstISB.setOpcode(ARM::ISB); 2292 TmpInstISB.addOperand(MCOperand::createImm(0xf)); 2293 EmitToStreamer(*OutStreamer, TmpInstISB); 2294 return; 2295 } 2296 case ARM::t2SpeculationBarrierISBDSBEndBB: { 2297 // Print DSB SYS + ISB 2298 MCInst TmpInstDSB; 2299 TmpInstDSB.setOpcode(ARM::t2DSB); 2300 TmpInstDSB.addOperand(MCOperand::createImm(0xf)); 2301 TmpInstDSB.addOperand(MCOperand::createImm(ARMCC::AL)); 2302 TmpInstDSB.addOperand(MCOperand::createReg(0)); 2303 EmitToStreamer(*OutStreamer, TmpInstDSB); 2304 MCInst TmpInstISB; 2305 TmpInstISB.setOpcode(ARM::t2ISB); 2306 TmpInstISB.addOperand(MCOperand::createImm(0xf)); 2307 TmpInstISB.addOperand(MCOperand::createImm(ARMCC::AL)); 2308 TmpInstISB.addOperand(MCOperand::createReg(0)); 2309 EmitToStreamer(*OutStreamer, TmpInstISB); 2310 return; 2311 } 2312 case ARM::SpeculationBarrierSBEndBB: { 2313 // Print SB 2314 MCInst TmpInstSB; 2315 TmpInstSB.setOpcode(ARM::SB); 2316 EmitToStreamer(*OutStreamer, TmpInstSB); 2317 return; 2318 } 2319 case ARM::t2SpeculationBarrierSBEndBB: { 2320 // Print SB 2321 MCInst TmpInstSB; 2322 TmpInstSB.setOpcode(ARM::t2SB); 2323 EmitToStreamer(*OutStreamer, TmpInstSB); 2324 return; 2325 } 2326 2327 case ARM::SEH_StackAlloc: 2328 ATS.emitARMWinCFIAllocStack(MI->getOperand(0).getImm(), 2329 MI->getOperand(1).getImm()); 2330 return; 2331 2332 case ARM::SEH_SaveRegs: 2333 case ARM::SEH_SaveRegs_Ret: 2334 ATS.emitARMWinCFISaveRegMask(MI->getOperand(0).getImm(), 2335 MI->getOperand(1).getImm()); 2336 return; 2337 2338 case ARM::SEH_SaveSP: 2339 ATS.emitARMWinCFISaveSP(MI->getOperand(0).getImm()); 2340 return; 2341 2342 case ARM::SEH_SaveFRegs: 2343 ATS.emitARMWinCFISaveFRegs(MI->getOperand(0).getImm(), 2344 MI->getOperand(1).getImm()); 2345 return; 2346 2347 case ARM::SEH_SaveLR: 2348 ATS.emitARMWinCFISaveLR(MI->getOperand(0).getImm()); 2349 return; 2350 2351 case ARM::SEH_Nop: 2352 case ARM::SEH_Nop_Ret: 2353 ATS.emitARMWinCFINop(MI->getOperand(0).getImm()); 2354 return; 2355 2356 case ARM::SEH_PrologEnd: 2357 ATS.emitARMWinCFIPrologEnd(/*Fragment=*/false); 2358 return; 2359 2360 case ARM::SEH_EpilogStart: 2361 ATS.emitARMWinCFIEpilogStart(ARMCC::AL); 2362 return; 2363 2364 case ARM::SEH_EpilogEnd: 2365 ATS.emitARMWinCFIEpilogEnd(); 2366 return; 2367 } 2368 2369 MCInst TmpInst; 2370 LowerARMMachineInstrToMCInst(MI, TmpInst, *this); 2371 2372 EmitToStreamer(*OutStreamer, TmpInst); 2373 } 2374 2375 //===----------------------------------------------------------------------===// 2376 // Target Registry Stuff 2377 //===----------------------------------------------------------------------===// 2378 2379 // Force static initialization. 2380 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMAsmPrinter() { 2381 RegisterAsmPrinter<ARMAsmPrinter> X(getTheARMLETarget()); 2382 RegisterAsmPrinter<ARMAsmPrinter> Y(getTheARMBETarget()); 2383 RegisterAsmPrinter<ARMAsmPrinter> A(getTheThumbLETarget()); 2384 RegisterAsmPrinter<ARMAsmPrinter> B(getTheThumbBETarget()); 2385 } 2386