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