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