1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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'.
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
17 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "asmprinter"
21 #include "PPCTargetMachine.h"
22 #include "PPCSubtarget.h"
23 #include "InstPrinter/PPCInstPrinter.h"
24 #include "MCTargetDesc/PPCPredicates.h"
25 #include "llvm/Constants.h"
26 #include "llvm/DebugInfo.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"
60 class PPCAsmPrinter : public AsmPrinter {
62 DenseMap<MCSymbol*, MCSymbol*> TOC;
63 const PPCSubtarget &Subtarget;
66 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
67 : AsmPrinter(TM, Streamer),
68 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
70 virtual const char *getPassName() const {
71 return "PowerPC Assembly Printer";
75 virtual void EmitInstruction(const MachineInstr *MI);
77 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
79 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
80 unsigned AsmVariant, const char *ExtraCode,
82 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
83 unsigned AsmVariant, const char *ExtraCode,
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());
93 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
99 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
100 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
102 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
103 : PPCAsmPrinter(TM, Streamer) {}
105 virtual const char *getPassName() const {
106 return "Linux PPC Assembly Printer";
109 bool doFinalization(Module &M);
111 virtual void EmitFunctionEntryLabel();
113 void EmitFunctionBodyEnd();
116 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
118 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
120 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
121 : PPCAsmPrinter(TM, Streamer) {}
123 virtual const char *getPassName() const {
124 return "Darwin PPC Assembly Printer";
127 bool doFinalization(Module &M);
128 void EmitStartOfAsmFile(Module &M);
130 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
132 } // end of anonymous namespace
134 /// stripRegisterPrefix - This method strips the character prefix from a
135 /// register name so that only the number is left. Used by for linux asm.
136 static const char *stripRegisterPrefix(const char *RegName) {
137 switch (RegName[0]) {
140 case 'v': return RegName + 1;
141 case 'c': if (RegName[1] == 'r') return RegName + 2;
147 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
149 const MachineOperand &MO = MI->getOperand(OpNo);
151 switch (MO.getType()) {
152 case MachineOperand::MO_Register: {
153 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
154 // Linux assembler (Others?) does not take register mnemonics.
155 // FIXME - What about special registers used in mfspr/mtspr?
156 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
160 case MachineOperand::MO_Immediate:
164 case MachineOperand::MO_MachineBasicBlock:
165 O << *MO.getMBB()->getSymbol();
167 case MachineOperand::MO_JumpTableIndex:
168 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
169 << '_' << MO.getIndex();
170 // FIXME: PIC relocation model
172 case MachineOperand::MO_ConstantPoolIndex:
173 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
174 << '_' << MO.getIndex();
176 case MachineOperand::MO_BlockAddress:
177 O << *GetBlockAddressSymbol(MO.getBlockAddress());
179 case MachineOperand::MO_ExternalSymbol: {
180 // Computing the address of an external symbol, not calling it.
181 if (TM.getRelocationModel() == Reloc::Static) {
182 O << *GetExternalSymbolSymbol(MO.getSymbolName());
187 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
188 MO.getSymbolName()+"$non_lazy_ptr");
189 MachineModuleInfoImpl::StubValueTy &StubSym =
190 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
191 if (StubSym.getPointer() == 0)
192 StubSym = MachineModuleInfoImpl::
193 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
198 case MachineOperand::MO_GlobalAddress: {
199 // Computing the address of a global symbol, not calling it.
200 const GlobalValue *GV = MO.getGlobal();
201 MCSymbol *SymToPrint;
203 // External or weakly linked global variables need non-lazily-resolved stubs
204 if (TM.getRelocationModel() != Reloc::Static &&
205 (GV->isDeclaration() || GV->isWeakForLinker())) {
206 if (!GV->hasHiddenVisibility()) {
207 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
208 MachineModuleInfoImpl::StubValueTy &StubSym =
209 MMI->getObjFileInfo<MachineModuleInfoMachO>()
210 .getGVStubEntry(SymToPrint);
211 if (StubSym.getPointer() == 0)
212 StubSym = MachineModuleInfoImpl::
213 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
214 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
215 GV->hasAvailableExternallyLinkage()) {
216 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
218 MachineModuleInfoImpl::StubValueTy &StubSym =
219 MMI->getObjFileInfo<MachineModuleInfoMachO>().
220 getHiddenGVStubEntry(SymToPrint);
221 if (StubSym.getPointer() == 0)
222 StubSym = MachineModuleInfoImpl::
223 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
225 SymToPrint = Mang->getSymbol(GV);
228 SymToPrint = Mang->getSymbol(GV);
233 printOffset(MO.getOffset(), O);
238 O << "<unknown operand type: " << MO.getType() << ">";
243 /// PrintAsmOperand - Print out an operand for an inline asm expression.
245 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
247 const char *ExtraCode, raw_ostream &O) {
248 // Does this asm operand have a single letter operand modifier?
249 if (ExtraCode && ExtraCode[0]) {
250 if (ExtraCode[1] != 0) return true; // Unknown modifier.
252 switch (ExtraCode[0]) {
254 // See if this is a generic print operand
255 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
256 case 'c': // Don't print "$" before a global var name or constant.
257 break; // PPC never has a prefix.
258 case 'L': // Write second word of DImode reference.
259 // Verify that this operand has two consecutive registers.
260 if (!MI->getOperand(OpNo).isReg() ||
261 OpNo+1 == MI->getNumOperands() ||
262 !MI->getOperand(OpNo+1).isReg())
264 ++OpNo; // Return the high-part.
267 // Write 'i' if an integer constant, otherwise nothing. Used to print
269 if (MI->getOperand(OpNo).isImm())
275 printOperand(MI, OpNo, O);
279 // At the moment, all inline asm memory operands are a single register.
280 // In any case, the output of this routine should always be just one
281 // assembler operand.
283 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
285 const char *ExtraCode,
287 if (ExtraCode && ExtraCode[0])
288 return true; // Unknown modifier.
289 assert(MI->getOperand(OpNo).isReg());
291 printOperand(MI, OpNo, O);
297 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
298 /// the current output stream.
300 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
303 // Lower multi-instruction pseudo operations.
304 switch (MI->getOpcode()) {
306 case TargetOpcode::DBG_VALUE: {
307 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
310 raw_svector_ostream O(Str);
311 unsigned NOps = MI->getNumOperands();
313 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
314 // cast away const; DIetc do not take const operands for some reason.
315 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
318 // Frame address. Currently handles register +- offset only.
319 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
320 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
323 printOperand(MI, NOps-2, O);
324 OutStreamer.EmitRawText(O.str());
328 case PPC::MovePCtoLR:
329 case PPC::MovePCtoLR8: {
330 // Transform %LR = MovePCtoLR
331 // Into this, where the label is the PIC base:
334 MCSymbol *PICBase = MF->getPICBaseSymbol();
337 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here.
340 // FIXME: We would like an efficient form for this, so we don't have to do
341 // a lot of extra uniquing.
342 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::
343 Create(PICBase, OutContext)));
344 OutStreamer.EmitInstruction(TmpInst);
347 OutStreamer.EmitLabel(PICBase);
353 // Transform %X3 = LDtoc <ga:@min1>, %X2
354 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
356 // Change the opcode to LD, and the global address operand to be a
357 // reference to the TOC entry we will synthesize later.
358 TmpInst.setOpcode(PPC::LD);
359 const MachineOperand &MO = MI->getOperand(1);
361 // Map symbol -> label of TOC entry
362 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
363 MCSymbol *MOSymbol = 0;
365 MOSymbol = Mang->getSymbol(MO.getGlobal());
367 MOSymbol = GetCPISymbol(MO.getIndex());
369 MOSymbol = GetJTISymbol(MO.getIndex());
370 MCSymbol *&TOCEntry = TOC[MOSymbol];
372 TOCEntry = GetTempSymbol("C", TOCLabelID++);
375 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
377 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
378 OutStreamer.EmitInstruction(TmpInst);
383 case PPC::MFCR8pseud:
384 // Transform: %R3 = MFCRpseud %CR7
385 // Into: %R3 = MFCR ;; cr7
386 OutStreamer.AddComment(PPCInstPrinter::
387 getRegisterName(MI->getOperand(1).getReg()));
388 TmpInst.setOpcode(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR);
389 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
390 OutStreamer.EmitInstruction(TmpInst);
393 // In Book E sync is called msync, handle this special case here...
394 if (Subtarget.isBookE()) {
395 OutStreamer.EmitRawText(StringRef("\tmsync"));
400 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
401 OutStreamer.EmitInstruction(TmpInst);
404 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
405 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
406 return AsmPrinter::EmitFunctionEntryLabel();
408 // Emit an official procedure descriptor.
409 const MCSection *Current = OutStreamer.getCurrentSection();
410 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
411 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
412 SectionKind::getReadOnly());
413 OutStreamer.SwitchSection(Section);
414 OutStreamer.EmitLabel(CurrentFnSym);
415 OutStreamer.EmitValueToAlignment(8);
417 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
418 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC.@tocbase"));
419 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
420 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
421 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2, OutContext),
422 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
423 OutStreamer.SwitchSection(Current);
425 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
426 ".L." + Twine(CurrentFnSym->getName()));
427 OutStreamer.EmitLabel(RealFnSym);
428 CurrentFnSymForSize = RealFnSym;
432 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
433 const TargetData *TD = TM.getTargetData();
435 bool isPPC64 = TD->getPointerSizeInBits() == 64;
437 if (isPPC64 && !TOC.empty()) {
438 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
439 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
440 SectionKind::getReadOnly());
441 OutStreamer.SwitchSection(Section);
443 // FIXME: This is nondeterminstic!
444 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
445 E = TOC.end(); I != E; ++I) {
446 OutStreamer.EmitLabel(I->second);
447 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
448 "[TC]," + I->first->getName());
452 return AsmPrinter::doFinalization(M);
455 /// EmitFunctionBodyEnd - Print the traceback table before the .size
458 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
459 // Only the 64-bit target requires a traceback table. For now,
460 // we only emit the word of zeroes that GDB requires to find
461 // the end of the function.
462 // FIXME: Eventually we should add the eight-byte mandatory fields
463 // described in the PPC64 ELF ABI.
464 if (Subtarget.isPPC64())
465 OutStreamer.EmitIntValue(0, 4/*size*/);
468 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
469 static const char *const CPUDirectives[] = {
487 unsigned Directive = Subtarget.getDarwinDirective();
488 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
489 Directive = PPC::DIR_970;
490 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
491 Directive = PPC::DIR_7400;
492 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
493 Directive = PPC::DIR_64;
494 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
496 // FIXME: This is a total hack, finish mc'izing the PPC backend.
497 if (OutStreamer.hasRawTextSupport())
498 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
500 // Prime text sections so they are adjacent. This reduces the likelihood a
501 // large data or debug section causes a branch to exceed 16M limit.
502 const TargetLoweringObjectFileMachO &TLOFMacho =
503 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
504 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
505 if (TM.getRelocationModel() == Reloc::PIC_) {
506 OutStreamer.SwitchSection(
507 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
508 MCSectionMachO::S_SYMBOL_STUBS |
509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
510 32, SectionKind::getText()));
511 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
512 OutStreamer.SwitchSection(
513 OutContext.getMachOSection("__TEXT","__symbol_stub1",
514 MCSectionMachO::S_SYMBOL_STUBS |
515 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
516 16, SectionKind::getText()));
518 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
521 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
522 // Remove $stub suffix, add $lazy_ptr.
523 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
524 TmpStr += "$lazy_ptr";
525 return Ctx.GetOrCreateSymbol(TmpStr.str());
528 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
529 // Add $tmp suffix to $stub, yielding $stub$tmp.
530 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
532 return Ctx.GetOrCreateSymbol(TmpStr.str());
535 void PPCDarwinAsmPrinter::
536 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
537 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
539 const TargetLoweringObjectFileMachO &TLOFMacho =
540 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
542 // .lazy_symbol_pointer
543 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
545 // Output stubs for dynamically-linked functions
546 if (TM.getRelocationModel() == Reloc::PIC_) {
547 const MCSection *StubSection =
548 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
549 MCSectionMachO::S_SYMBOL_STUBS |
550 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
551 32, SectionKind::getText());
552 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
553 OutStreamer.SwitchSection(StubSection);
556 MCSymbol *Stub = Stubs[i].first;
557 MCSymbol *RawSym = Stubs[i].second.getPointer();
558 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
559 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
561 OutStreamer.EmitLabel(Stub);
562 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
563 // FIXME: MCize this.
564 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
565 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
566 OutStreamer.EmitLabel(AnonSymbol);
567 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
568 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
569 "-" + AnonSymbol->getName() + ")");
570 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
573 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
574 "-" + AnonSymbol->getName() + ")(r11)");
576 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
577 "-" + AnonSymbol->getName() + ")(r11)");
578 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
579 OutStreamer.EmitRawText(StringRef("\tbctr"));
581 OutStreamer.SwitchSection(LSPSection);
582 OutStreamer.EmitLabel(LazyPtr);
583 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
586 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
588 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
590 OutStreamer.AddBlankLine();
594 const MCSection *StubSection =
595 OutContext.getMachOSection("__TEXT","__symbol_stub1",
596 MCSectionMachO::S_SYMBOL_STUBS |
597 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
598 16, SectionKind::getText());
599 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
600 MCSymbol *Stub = Stubs[i].first;
601 MCSymbol *RawSym = Stubs[i].second.getPointer();
602 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
604 OutStreamer.SwitchSection(StubSection);
606 OutStreamer.EmitLabel(Stub);
607 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
608 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
610 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
613 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
615 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
616 OutStreamer.EmitRawText(StringRef("\tbctr"));
617 OutStreamer.SwitchSection(LSPSection);
618 OutStreamer.EmitLabel(LazyPtr);
619 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
622 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
624 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
627 OutStreamer.AddBlankLine();
631 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
632 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
634 // Darwin/PPC always uses mach-o.
635 const TargetLoweringObjectFileMachO &TLOFMacho =
636 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
637 MachineModuleInfoMachO &MMIMacho =
638 MMI->getObjFileInfo<MachineModuleInfoMachO>();
640 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
642 EmitFunctionStubs(Stubs);
644 if (MAI->doesSupportExceptionHandling() && MMI) {
645 // Add the (possibly multiple) personalities to the set of global values.
646 // Only referenced functions get into the Personalities list.
647 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
648 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
649 E = Personalities.end(); I != E; ++I) {
651 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
652 MachineModuleInfoImpl::StubValueTy &StubSym =
653 MMIMacho.getGVStubEntry(NLPSym);
654 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
659 // Output stubs for dynamically-linked functions.
660 Stubs = MMIMacho.GetGVStubList();
662 // Output macho stubs for external and common global variables.
663 if (!Stubs.empty()) {
664 // Switch with ".non_lazy_symbol_pointer" directive.
665 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
666 EmitAlignment(isPPC64 ? 3 : 2);
668 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
670 OutStreamer.EmitLabel(Stubs[i].first);
671 // .indirect_symbol _foo
672 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
673 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
676 // External to current translation unit.
677 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
679 // Internal to current translation unit.
681 // When we place the LSDA into the TEXT section, the type info pointers
682 // need to be indirect and pc-rel. We accomplish this by using NLPs.
683 // However, sometimes the types are local to the file. So we need to
684 // fill in the value for the NLP in those cases.
685 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
687 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
691 OutStreamer.AddBlankLine();
694 Stubs = MMIMacho.GetHiddenGVStubList();
695 if (!Stubs.empty()) {
696 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
697 EmitAlignment(isPPC64 ? 3 : 2);
699 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
701 OutStreamer.EmitLabel(Stubs[i].first);
703 OutStreamer.EmitValue(MCSymbolRefExpr::
704 Create(Stubs[i].second.getPointer(),
706 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
710 OutStreamer.AddBlankLine();
713 // Funny Darwin hack: This flag tells the linker that no global symbols
714 // contain code that falls through to other global symbols (e.g. the obvious
715 // implementation of multiple entry points). If this doesn't occur, the
716 // linker can safely perform dead code stripping. Since LLVM never generates
717 // code that does this, it is always safe to set.
718 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
720 return AsmPrinter::doFinalization(M);
723 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
724 /// for a MachineFunction to the given output stream, in a format that the
725 /// Darwin assembler can deal with.
727 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
728 MCStreamer &Streamer) {
729 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
731 if (Subtarget->isDarwin())
732 return new PPCDarwinAsmPrinter(tm, Streamer);
733 return new PPCLinuxAsmPrinter(tm, Streamer);
736 // Force static initialization.
737 extern "C" void LLVMInitializePowerPCAsmPrinter() {
738 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
739 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);