PPCAsmPrinter.cpp revision 4d989ac93ce608057fb6b13a4068264ab037ecd5
1//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===// 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 PowerPC assembly language. This printer is 12// the output mechanism used by `llc'. 13// 14// Documentation at http://developer.apple.com/documentation/DeveloperTools/ 15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 16// 17//===----------------------------------------------------------------------===// 18 19#define DEBUG_TYPE "asmprinter" 20#include "PPC.h" 21#include "PPCTargetMachine.h" 22#include "PPCSubtarget.h" 23#include "InstPrinter/PPCInstPrinter.h" 24#include "MCTargetDesc/PPCPredicates.h" 25#include "llvm/Analysis/DebugInfo.h" 26#include "llvm/Constants.h" 27#include "llvm/DerivedTypes.h" 28#include "llvm/Module.h" 29#include "llvm/Assembly/Writer.h" 30#include "llvm/CodeGen/AsmPrinter.h" 31#include "llvm/CodeGen/MachineFunctionPass.h" 32#include "llvm/CodeGen/MachineInstr.h" 33#include "llvm/CodeGen/MachineInstrBuilder.h" 34#include "llvm/CodeGen/MachineModuleInfoImpls.h" 35#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 36#include "llvm/MC/MCAsmInfo.h" 37#include "llvm/MC/MCContext.h" 38#include "llvm/MC/MCExpr.h" 39#include "llvm/MC/MCInst.h" 40#include "llvm/MC/MCSectionMachO.h" 41#include "llvm/MC/MCStreamer.h" 42#include "llvm/MC/MCSymbol.h" 43#include "llvm/MC/MCSectionELF.h" 44#include "llvm/Target/Mangler.h" 45#include "llvm/Target/TargetRegisterInfo.h" 46#include "llvm/Target/TargetInstrInfo.h" 47#include "llvm/Target/TargetOptions.h" 48#include "llvm/Support/CommandLine.h" 49#include "llvm/Support/Debug.h" 50#include "llvm/Support/MathExtras.h" 51#include "llvm/Support/ErrorHandling.h" 52#include "llvm/Support/TargetRegistry.h" 53#include "llvm/Support/raw_ostream.h" 54#include "llvm/Support/ELF.h" 55#include "llvm/ADT/StringExtras.h" 56#include "llvm/ADT/SmallString.h" 57using namespace llvm; 58 59namespace { 60 class PPCAsmPrinter : public AsmPrinter { 61 protected: 62 DenseMap<MCSymbol*, MCSymbol*> TOC; 63 const PPCSubtarget &Subtarget; 64 uint64_t TOCLabelID; 65 public: 66 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) 67 : AsmPrinter(TM, Streamer), 68 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {} 69 70 virtual const char *getPassName() const { 71 return "PowerPC Assembly Printer"; 72 } 73 74 75 virtual void EmitInstruction(const MachineInstr *MI); 76 77 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 78 79 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 80 unsigned AsmVariant, const char *ExtraCode, 81 raw_ostream &O); 82 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 83 unsigned AsmVariant, const char *ExtraCode, 84 raw_ostream &O); 85 86 MachineLocation getDebugValueLocation(const MachineInstr *MI) const { 87 MachineLocation Location; 88 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!"); 89 // Frame address. Currently handles register +- offset only. 90 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm()) 91 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm()); 92 else { 93 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n"); 94 } 95 return Location; 96 } 97 }; 98 99 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux 100 class PPCLinuxAsmPrinter : public PPCAsmPrinter { 101 public: 102 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) 103 : PPCAsmPrinter(TM, Streamer) {} 104 105 virtual const char *getPassName() const { 106 return "Linux PPC Assembly Printer"; 107 } 108 109 bool doFinalization(Module &M); 110 111 virtual void EmitFunctionEntryLabel(); 112 }; 113 114 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac 115 /// OS X 116 class PPCDarwinAsmPrinter : public PPCAsmPrinter { 117 public: 118 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) 119 : PPCAsmPrinter(TM, Streamer) {} 120 121 virtual const char *getPassName() const { 122 return "Darwin PPC Assembly Printer"; 123 } 124 125 bool doFinalization(Module &M); 126 void EmitStartOfAsmFile(Module &M); 127 128 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs); 129 }; 130} // end of anonymous namespace 131 132/// stripRegisterPrefix - This method strips the character prefix from a 133/// register name so that only the number is left. Used by for linux asm. 134static const char *stripRegisterPrefix(const char *RegName) { 135 switch (RegName[0]) { 136 case 'r': 137 case 'f': 138 case 'v': return RegName + 1; 139 case 'c': if (RegName[1] == 'r') return RegName + 2; 140 } 141 142 return RegName; 143} 144 145void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 146 raw_ostream &O) { 147 const MachineOperand &MO = MI->getOperand(OpNo); 148 149 switch (MO.getType()) { 150 case MachineOperand::MO_Register: { 151 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg()); 152 // Linux assembler (Others?) does not take register mnemonics. 153 // FIXME - What about special registers used in mfspr/mtspr? 154 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName); 155 O << RegName; 156 return; 157 } 158 case MachineOperand::MO_Immediate: 159 O << MO.getImm(); 160 return; 161 162 case MachineOperand::MO_MachineBasicBlock: 163 O << *MO.getMBB()->getSymbol(); 164 return; 165 case MachineOperand::MO_JumpTableIndex: 166 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 167 << '_' << MO.getIndex(); 168 // FIXME: PIC relocation model 169 return; 170 case MachineOperand::MO_ConstantPoolIndex: 171 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() 172 << '_' << MO.getIndex(); 173 return; 174 case MachineOperand::MO_BlockAddress: 175 O << *GetBlockAddressSymbol(MO.getBlockAddress()); 176 return; 177 case MachineOperand::MO_ExternalSymbol: { 178 // Computing the address of an external symbol, not calling it. 179 if (TM.getRelocationModel() == Reloc::Static) { 180 O << *GetExternalSymbolSymbol(MO.getSymbolName()); 181 return; 182 } 183 184 MCSymbol *NLPSym = 185 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+ 186 MO.getSymbolName()+"$non_lazy_ptr"); 187 MachineModuleInfoImpl::StubValueTy &StubSym = 188 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym); 189 if (StubSym.getPointer() == 0) 190 StubSym = MachineModuleInfoImpl:: 191 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true); 192 193 O << *NLPSym; 194 return; 195 } 196 case MachineOperand::MO_GlobalAddress: { 197 // Computing the address of a global symbol, not calling it. 198 const GlobalValue *GV = MO.getGlobal(); 199 MCSymbol *SymToPrint; 200 201 // External or weakly linked global variables need non-lazily-resolved stubs 202 if (TM.getRelocationModel() != Reloc::Static && 203 (GV->isDeclaration() || GV->isWeakForLinker())) { 204 if (!GV->hasHiddenVisibility()) { 205 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 206 MachineModuleInfoImpl::StubValueTy &StubSym = 207 MMI->getObjFileInfo<MachineModuleInfoMachO>() 208 .getGVStubEntry(SymToPrint); 209 if (StubSym.getPointer() == 0) 210 StubSym = MachineModuleInfoImpl:: 211 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage()); 212 } else if (GV->isDeclaration() || GV->hasCommonLinkage() || 213 GV->hasAvailableExternallyLinkage()) { 214 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 215 216 MachineModuleInfoImpl::StubValueTy &StubSym = 217 MMI->getObjFileInfo<MachineModuleInfoMachO>(). 218 getHiddenGVStubEntry(SymToPrint); 219 if (StubSym.getPointer() == 0) 220 StubSym = MachineModuleInfoImpl:: 221 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage()); 222 } else { 223 SymToPrint = Mang->getSymbol(GV); 224 } 225 } else { 226 SymToPrint = Mang->getSymbol(GV); 227 } 228 229 O << *SymToPrint; 230 231 printOffset(MO.getOffset(), O); 232 return; 233 } 234 235 default: 236 O << "<unknown operand type: " << MO.getType() << ">"; 237 return; 238 } 239} 240 241/// PrintAsmOperand - Print out an operand for an inline asm expression. 242/// 243bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 244 unsigned AsmVariant, 245 const char *ExtraCode, raw_ostream &O) { 246 // Does this asm operand have a single letter operand modifier? 247 if (ExtraCode && ExtraCode[0]) { 248 if (ExtraCode[1] != 0) return true; // Unknown modifier. 249 250 switch (ExtraCode[0]) { 251 default: return true; // Unknown modifier. 252 case 'c': // Don't print "$" before a global var name or constant. 253 break; // PPC never has a prefix. 254 case 'L': // Write second word of DImode reference. 255 // Verify that this operand has two consecutive registers. 256 if (!MI->getOperand(OpNo).isReg() || 257 OpNo+1 == MI->getNumOperands() || 258 !MI->getOperand(OpNo+1).isReg()) 259 return true; 260 ++OpNo; // Return the high-part. 261 break; 262 case 'I': 263 // Write 'i' if an integer constant, otherwise nothing. Used to print 264 // addi vs add, etc. 265 if (MI->getOperand(OpNo).isImm()) 266 O << "i"; 267 return false; 268 } 269 } 270 271 printOperand(MI, OpNo, O); 272 return false; 273} 274 275// At the moment, all inline asm memory operands are a single register. 276// In any case, the output of this routine should always be just one 277// assembler operand. 278 279bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 280 unsigned AsmVariant, 281 const char *ExtraCode, 282 raw_ostream &O) { 283 if (ExtraCode && ExtraCode[0]) 284 return true; // Unknown modifier. 285 assert(MI->getOperand(OpNo).isReg()); 286 O << "0("; 287 printOperand(MI, OpNo, O); 288 O << ")"; 289 return false; 290} 291 292 293/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to 294/// the current output stream. 295/// 296void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { 297 MCInst TmpInst; 298 299 // Lower multi-instruction pseudo operations. 300 switch (MI->getOpcode()) { 301 default: break; 302 case TargetOpcode::DBG_VALUE: { 303 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return; 304 305 SmallString<32> Str; 306 raw_svector_ostream O(Str); 307 unsigned NOps = MI->getNumOperands(); 308 assert(NOps==4); 309 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: "; 310 // cast away const; DIetc do not take const operands for some reason. 311 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata())); 312 O << V.getName(); 313 O << " <- "; 314 // Frame address. Currently handles register +- offset only. 315 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm()); 316 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O); 317 O << ']'; 318 O << "+"; 319 printOperand(MI, NOps-2, O); 320 OutStreamer.EmitRawText(O.str()); 321 return; 322 } 323 324 case PPC::MovePCtoLR: 325 case PPC::MovePCtoLR8: { 326 // Transform %LR = MovePCtoLR 327 // Into this, where the label is the PIC base: 328 // bl L1$pb 329 // L1$pb: 330 MCSymbol *PICBase = MF->getPICBaseSymbol(); 331 332 // Emit the 'bl'. 333 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here. 334 335 336 // FIXME: We would like an efficient form for this, so we don't have to do 337 // a lot of extra uniquing. 338 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr:: 339 Create(PICBase, OutContext))); 340 OutStreamer.EmitInstruction(TmpInst); 341 342 // Emit the label. 343 OutStreamer.EmitLabel(PICBase); 344 return; 345 } 346 case PPC::LDtoc: { 347 // Transform %X3 = LDtoc <ga:@min1>, %X2 348 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin()); 349 350 // Change the opcode to LD, and the global address operand to be a 351 // reference to the TOC entry we will synthesize later. 352 TmpInst.setOpcode(PPC::LD); 353 const MachineOperand &MO = MI->getOperand(1); 354 assert(MO.isGlobal()); 355 356 // Map symbol -> label of TOC entry. 357 MCSymbol *&TOCEntry = TOC[Mang->getSymbol(MO.getGlobal())]; 358 if (TOCEntry == 0) 359 TOCEntry = GetTempSymbol("C", TOCLabelID++); 360 361 const MCExpr *Exp = 362 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC, 363 OutContext); 364 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp); 365 OutStreamer.EmitInstruction(TmpInst); 366 return; 367 } 368 369 case PPC::MFCRpseud: 370 case PPC::MFCR8pseud: 371 // Transform: %R3 = MFCRpseud %CR7 372 // Into: %R3 = MFCR ;; cr7 373 OutStreamer.AddComment(PPCInstPrinter:: 374 getRegisterName(MI->getOperand(1).getReg())); 375 TmpInst.setOpcode(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR); 376 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg())); 377 OutStreamer.EmitInstruction(TmpInst); 378 return; 379 case PPC::SYNC: 380 // In Book E sync is called msync, handle this special case here... 381 if (Subtarget.isBookE()) { 382 OutStreamer.EmitRawText(StringRef("\tmsync")); 383 return; 384 } 385 } 386 387 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin()); 388 OutStreamer.EmitInstruction(TmpInst); 389} 390 391void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() { 392 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label. 393 return AsmPrinter::EmitFunctionEntryLabel(); 394 395 // Emit an official procedure descriptor. 396 const MCSection *Current = OutStreamer.getCurrentSection(); 397 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd", 398 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC, 399 SectionKind::getReadOnly()); 400 OutStreamer.SwitchSection(Section); 401 OutStreamer.EmitLabel(CurrentFnSym); 402 OutStreamer.EmitValueToAlignment(8); 403 MCSymbol *Symbol1 = 404 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName())); 405 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC.@tocbase")); 406 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext), 407 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/); 408 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2, OutContext), 409 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/); 410 OutStreamer.SwitchSection(Current); 411 412 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol( 413 ".L." + Twine(CurrentFnSym->getName())); 414 OutStreamer.EmitLabel(RealFnSym); 415 CurrentFnSymForSize = RealFnSym; 416} 417 418 419bool PPCLinuxAsmPrinter::doFinalization(Module &M) { 420 const TargetData *TD = TM.getTargetData(); 421 422 bool isPPC64 = TD->getPointerSizeInBits() == 64; 423 424 if (isPPC64 && !TOC.empty()) { 425 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc", 426 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC, 427 SectionKind::getReadOnly()); 428 OutStreamer.SwitchSection(Section); 429 430 // FIXME: This is nondeterminstic! 431 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(), 432 E = TOC.end(); I != E; ++I) { 433 OutStreamer.EmitLabel(I->second); 434 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) + 435 "[TC]," + I->first->getName()); 436 } 437 } 438 439 return AsmPrinter::doFinalization(M); 440} 441 442void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) { 443 static const char *const CPUDirectives[] = { 444 "", 445 "ppc", 446 "ppc440", 447 "ppc601", 448 "ppc602", 449 "ppc603", 450 "ppc7400", 451 "ppc750", 452 "ppc970", 453 "ppcA2", 454 "ppc64" 455 }; 456 457 unsigned Directive = Subtarget.getDarwinDirective(); 458 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970) 459 Directive = PPC::DIR_970; 460 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400) 461 Directive = PPC::DIR_7400; 462 if (Subtarget.isPPC64() && Directive < PPC::DIR_64) 463 Directive = PPC::DIR_64; 464 assert(Directive <= PPC::DIR_64 && "Directive out of range."); 465 466 // FIXME: This is a total hack, finish mc'izing the PPC backend. 467 if (OutStreamer.hasRawTextSupport()) 468 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive])); 469 470 // Prime text sections so they are adjacent. This reduces the likelihood a 471 // large data or debug section causes a branch to exceed 16M limit. 472 const TargetLoweringObjectFileMachO &TLOFMacho = 473 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 474 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection()); 475 if (TM.getRelocationModel() == Reloc::PIC_) { 476 OutStreamer.SwitchSection( 477 OutContext.getMachOSection("__TEXT", "__picsymbolstub1", 478 MCSectionMachO::S_SYMBOL_STUBS | 479 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 480 32, SectionKind::getText())); 481 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) { 482 OutStreamer.SwitchSection( 483 OutContext.getMachOSection("__TEXT","__symbol_stub1", 484 MCSectionMachO::S_SYMBOL_STUBS | 485 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 486 16, SectionKind::getText())); 487 } 488 OutStreamer.SwitchSection(getObjFileLowering().getTextSection()); 489} 490 491static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) { 492 // Remove $stub suffix, add $lazy_ptr. 493 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5); 494 TmpStr += "$lazy_ptr"; 495 return Ctx.GetOrCreateSymbol(TmpStr.str()); 496} 497 498static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) { 499 // Add $tmp suffix to $stub, yielding $stub$tmp. 500 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()); 501 TmpStr += "$tmp"; 502 return Ctx.GetOrCreateSymbol(TmpStr.str()); 503} 504 505void PPCDarwinAsmPrinter:: 506EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) { 507 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64; 508 509 const TargetLoweringObjectFileMachO &TLOFMacho = 510 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 511 512 // .lazy_symbol_pointer 513 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection(); 514 515 // Output stubs for dynamically-linked functions 516 if (TM.getRelocationModel() == Reloc::PIC_) { 517 const MCSection *StubSection = 518 OutContext.getMachOSection("__TEXT", "__picsymbolstub1", 519 MCSectionMachO::S_SYMBOL_STUBS | 520 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 521 32, SectionKind::getText()); 522 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 523 OutStreamer.SwitchSection(StubSection); 524 EmitAlignment(4); 525 526 MCSymbol *Stub = Stubs[i].first; 527 MCSymbol *RawSym = Stubs[i].second.getPointer(); 528 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext); 529 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext); 530 531 OutStreamer.EmitLabel(Stub); 532 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 533 // FIXME: MCize this. 534 OutStreamer.EmitRawText(StringRef("\tmflr r0")); 535 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName())); 536 OutStreamer.EmitLabel(AnonSymbol); 537 OutStreamer.EmitRawText(StringRef("\tmflr r11")); 538 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+ 539 "-" + AnonSymbol->getName() + ")"); 540 OutStreamer.EmitRawText(StringRef("\tmtlr r0")); 541 542 if (isPPC64) 543 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) + 544 "-" + AnonSymbol->getName() + ")(r11)"); 545 else 546 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) + 547 "-" + AnonSymbol->getName() + ")(r11)"); 548 OutStreamer.EmitRawText(StringRef("\tmtctr r12")); 549 OutStreamer.EmitRawText(StringRef("\tbctr")); 550 551 OutStreamer.SwitchSection(LSPSection); 552 OutStreamer.EmitLabel(LazyPtr); 553 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 554 555 if (isPPC64) 556 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper")); 557 else 558 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper")); 559 } 560 OutStreamer.AddBlankLine(); 561 return; 562 } 563 564 const MCSection *StubSection = 565 OutContext.getMachOSection("__TEXT","__symbol_stub1", 566 MCSectionMachO::S_SYMBOL_STUBS | 567 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 568 16, SectionKind::getText()); 569 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 570 MCSymbol *Stub = Stubs[i].first; 571 MCSymbol *RawSym = Stubs[i].second.getPointer(); 572 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext); 573 574 OutStreamer.SwitchSection(StubSection); 575 EmitAlignment(4); 576 OutStreamer.EmitLabel(Stub); 577 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 578 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")"); 579 if (isPPC64) 580 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) + 581 ")(r11)"); 582 else 583 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) + 584 ")(r11)"); 585 OutStreamer.EmitRawText(StringRef("\tmtctr r12")); 586 OutStreamer.EmitRawText(StringRef("\tbctr")); 587 OutStreamer.SwitchSection(LSPSection); 588 OutStreamer.EmitLabel(LazyPtr); 589 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 590 591 if (isPPC64) 592 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper")); 593 else 594 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper")); 595 } 596 597 OutStreamer.AddBlankLine(); 598} 599 600 601bool PPCDarwinAsmPrinter::doFinalization(Module &M) { 602 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64; 603 604 // Darwin/PPC always uses mach-o. 605 const TargetLoweringObjectFileMachO &TLOFMacho = 606 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 607 MachineModuleInfoMachO &MMIMacho = 608 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 609 610 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList(); 611 if (!Stubs.empty()) 612 EmitFunctionStubs(Stubs); 613 614 if (MAI->doesSupportExceptionHandling() && MMI) { 615 // Add the (possibly multiple) personalities to the set of global values. 616 // Only referenced functions get into the Personalities list. 617 const std::vector<const Function*> &Personalities = MMI->getPersonalities(); 618 for (std::vector<const Function*>::const_iterator I = Personalities.begin(), 619 E = Personalities.end(); I != E; ++I) { 620 if (*I) { 621 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr"); 622 MachineModuleInfoImpl::StubValueTy &StubSym = 623 MMIMacho.getGVStubEntry(NLPSym); 624 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true); 625 } 626 } 627 } 628 629 // Output stubs for dynamically-linked functions. 630 Stubs = MMIMacho.GetGVStubList(); 631 632 // Output macho stubs for external and common global variables. 633 if (!Stubs.empty()) { 634 // Switch with ".non_lazy_symbol_pointer" directive. 635 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection()); 636 EmitAlignment(isPPC64 ? 3 : 2); 637 638 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 639 // L_foo$stub: 640 OutStreamer.EmitLabel(Stubs[i].first); 641 // .indirect_symbol _foo 642 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second; 643 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 644 645 if (MCSym.getInt()) 646 // External to current translation unit. 647 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/); 648 else 649 // Internal to current translation unit. 650 // 651 // When we place the LSDA into the TEXT section, the type info pointers 652 // need to be indirect and pc-rel. We accomplish this by using NLPs. 653 // However, sometimes the types are local to the file. So we need to 654 // fill in the value for the NLP in those cases. 655 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(), 656 OutContext), 657 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/); 658 } 659 660 Stubs.clear(); 661 OutStreamer.AddBlankLine(); 662 } 663 664 Stubs = MMIMacho.GetHiddenGVStubList(); 665 if (!Stubs.empty()) { 666 OutStreamer.SwitchSection(getObjFileLowering().getDataSection()); 667 EmitAlignment(isPPC64 ? 3 : 2); 668 669 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 670 // L_foo$stub: 671 OutStreamer.EmitLabel(Stubs[i].first); 672 // .long _foo 673 OutStreamer.EmitValue(MCSymbolRefExpr:: 674 Create(Stubs[i].second.getPointer(), 675 OutContext), 676 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/); 677 } 678 679 Stubs.clear(); 680 OutStreamer.AddBlankLine(); 681 } 682 683 // Funny Darwin hack: This flag tells the linker that no global symbols 684 // contain code that falls through to other global symbols (e.g. the obvious 685 // implementation of multiple entry points). If this doesn't occur, the 686 // linker can safely perform dead code stripping. Since LLVM never generates 687 // code that does this, it is always safe to set. 688 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 689 690 return AsmPrinter::doFinalization(M); 691} 692 693/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code 694/// for a MachineFunction to the given output stream, in a format that the 695/// Darwin assembler can deal with. 696/// 697static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm, 698 MCStreamer &Streamer) { 699 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>(); 700 701 if (Subtarget->isDarwin()) 702 return new PPCDarwinAsmPrinter(tm, Streamer); 703 return new PPCLinuxAsmPrinter(tm, Streamer); 704} 705 706// Force static initialization. 707extern "C" void LLVMInitializePowerPCAsmPrinter() { 708 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass); 709 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass); 710} 711