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 "MCTargetDesc/PPCPredicates.h"
24 #include "llvm/Analysis/DebugInfo.h"
25 #include "llvm/Constants.h"
26 #include "llvm/DerivedTypes.h"
27 #include "llvm/Module.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
34 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCContext.h"
37 #include "llvm/MC/MCExpr.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/MC/MCSectionMachO.h"
40 #include "llvm/MC/MCStreamer.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MCSectionELF.h"
43 #include "llvm/Target/Mangler.h"
44 #include "llvm/Target/TargetRegisterInfo.h"
45 #include "llvm/Target/TargetInstrInfo.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/MathExtras.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Support/ELF.h"
54 #include "llvm/ADT/StringExtras.h"
55 #include "llvm/ADT/SmallString.h"
56 #include "InstPrinter/PPCInstPrinter.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();
114 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
116 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
118 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
119 : PPCAsmPrinter(TM, Streamer) {}
121 virtual const char *getPassName() const {
122 return "Darwin PPC Assembly Printer";
125 bool doFinalization(Module &M);
126 void EmitStartOfAsmFile(Module &M);
128 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
130 } // end of anonymous namespace
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.
134 static const char *stripRegisterPrefix(const char *RegName) {
135 switch (RegName[0]) {
138 case 'v': return RegName + 1;
139 case 'c': if (RegName[1] == 'r') return RegName + 2;
145 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
147 const MachineOperand &MO = MI->getOperand(OpNo);
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);
158 case MachineOperand::MO_Immediate:
162 case MachineOperand::MO_MachineBasicBlock:
163 O << *MO.getMBB()->getSymbol();
165 case MachineOperand::MO_JumpTableIndex:
166 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
167 << '_' << MO.getIndex();
168 // FIXME: PIC relocation model
170 case MachineOperand::MO_ConstantPoolIndex:
171 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
172 << '_' << MO.getIndex();
174 case MachineOperand::MO_BlockAddress:
175 O << *GetBlockAddressSymbol(MO.getBlockAddress());
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());
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);
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;
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");
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());
223 SymToPrint = Mang->getSymbol(GV);
226 SymToPrint = Mang->getSymbol(GV);
231 printOffset(MO.getOffset(), O);
236 O << "<unknown operand type: " << MO.getType() << ">";
241 /// PrintAsmOperand - Print out an operand for an inline asm expression.
243 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
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.
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())
260 ++OpNo; // Return the high-part.
263 // Write 'i' if an integer constant, otherwise nothing. Used to print
265 if (MI->getOperand(OpNo).isImm())
271 printOperand(MI, OpNo, O);
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.
279 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
281 const char *ExtraCode,
283 if (ExtraCode && ExtraCode[0])
284 return true; // Unknown modifier.
285 assert(MI->getOperand(OpNo).isReg());
287 printOperand(MI, OpNo, O);
293 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
294 /// the current output stream.
296 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
299 // Lower multi-instruction pseudo operations.
300 switch (MI->getOpcode()) {
302 case TargetOpcode::DBG_VALUE: {
303 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
306 raw_svector_ostream O(Str);
307 unsigned NOps = MI->getNumOperands();
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()));
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);
319 printOperand(MI, NOps-2, O);
320 OutStreamer.EmitRawText(O.str());
324 case PPC::MovePCtoLR:
325 case PPC::MovePCtoLR8: {
326 // Transform %LR = MovePCtoLR
327 // Into this, where the label is the PIC base:
330 MCSymbol *PICBase = MF->getPICBaseSymbol();
333 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here.
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);
343 OutStreamer.EmitLabel(PICBase);
347 // Transform %X3 = LDtoc <ga:@min1>, %X2
348 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
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());
356 // Map symbol -> label of TOC entry.
357 MCSymbol *&TOCEntry = TOC[Mang->getSymbol(MO.getGlobal())];
359 TOCEntry = GetTempSymbol("C", TOCLabelID++);
362 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
364 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
365 OutStreamer.EmitInstruction(TmpInst);
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);
380 // In Book E sync is called msync, handle this special case here...
381 if (Subtarget.isBookE()) {
382 OutStreamer.EmitRawText(StringRef("\tmsync"));
387 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
388 OutStreamer.EmitInstruction(TmpInst);
391 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
392 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
393 return AsmPrinter::EmitFunctionEntryLabel();
395 // Emit an official procedure descriptor.
396 const MCSection *Current = OutStreamer.getCurrentSection();
397 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
398 ELF::SHT_PROGBITS, 0, SectionKind::getReadOnly());
399 OutStreamer.SwitchSection(Section);
400 OutStreamer.EmitLabel(CurrentFnSym);
402 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
403 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC.@tocbase"));
404 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
405 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
406 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2, OutContext),
407 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
408 OutStreamer.SwitchSection(Current);
410 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
411 ".L." + Twine(CurrentFnSym->getName()));
412 OutStreamer.EmitLabel(RealFnSym);
413 CurrentFnSymForSize = RealFnSym;
417 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
418 const TargetData *TD = TM.getTargetData();
420 bool isPPC64 = TD->getPointerSizeInBits() == 64;
422 if (isPPC64 && !TOC.empty()) {
423 // FIXME 64-bit SVR4: Use MCSection here?
424 OutStreamer.EmitRawText(StringRef("\t.section\t\".toc\",\"aw\""));
426 // FIXME: This is nondeterminstic!
427 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
428 E = TOC.end(); I != E; ++I) {
429 OutStreamer.EmitLabel(I->second);
430 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
431 "[TC]," + I->first->getName());
435 return AsmPrinter::doFinalization(M);
438 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
439 static const char *const CPUDirectives[] = {
452 unsigned Directive = Subtarget.getDarwinDirective();
453 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
454 Directive = PPC::DIR_970;
455 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
456 Directive = PPC::DIR_7400;
457 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
458 Directive = PPC::DIR_64;
459 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
461 // FIXME: This is a total hack, finish mc'izing the PPC backend.
462 if (OutStreamer.hasRawTextSupport())
463 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
465 // Prime text sections so they are adjacent. This reduces the likelihood a
466 // large data or debug section causes a branch to exceed 16M limit.
467 const TargetLoweringObjectFileMachO &TLOFMacho =
468 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
469 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
470 if (TM.getRelocationModel() == Reloc::PIC_) {
471 OutStreamer.SwitchSection(
472 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
473 MCSectionMachO::S_SYMBOL_STUBS |
474 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
475 32, SectionKind::getText()));
476 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
477 OutStreamer.SwitchSection(
478 OutContext.getMachOSection("__TEXT","__symbol_stub1",
479 MCSectionMachO::S_SYMBOL_STUBS |
480 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
481 16, SectionKind::getText()));
483 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
486 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
487 // Remove $stub suffix, add $lazy_ptr.
488 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
489 TmpStr += "$lazy_ptr";
490 return Ctx.GetOrCreateSymbol(TmpStr.str());
493 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
494 // Add $tmp suffix to $stub, yielding $stub$tmp.
495 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
497 return Ctx.GetOrCreateSymbol(TmpStr.str());
500 void PPCDarwinAsmPrinter::
501 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
502 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
504 const TargetLoweringObjectFileMachO &TLOFMacho =
505 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
507 // .lazy_symbol_pointer
508 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
510 // Output stubs for dynamically-linked functions
511 if (TM.getRelocationModel() == Reloc::PIC_) {
512 const MCSection *StubSection =
513 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
514 MCSectionMachO::S_SYMBOL_STUBS |
515 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
516 32, SectionKind::getText());
517 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
518 OutStreamer.SwitchSection(StubSection);
521 MCSymbol *Stub = Stubs[i].first;
522 MCSymbol *RawSym = Stubs[i].second.getPointer();
523 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
524 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
526 OutStreamer.EmitLabel(Stub);
527 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
528 // FIXME: MCize this.
529 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
530 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
531 OutStreamer.EmitLabel(AnonSymbol);
532 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
533 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
534 "-" + AnonSymbol->getName() + ")");
535 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
538 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
539 "-" + AnonSymbol->getName() + ")(r11)");
541 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
542 "-" + AnonSymbol->getName() + ")(r11)");
543 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
544 OutStreamer.EmitRawText(StringRef("\tbctr"));
546 OutStreamer.SwitchSection(LSPSection);
547 OutStreamer.EmitLabel(LazyPtr);
548 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
551 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
553 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
555 OutStreamer.AddBlankLine();
559 const MCSection *StubSection =
560 OutContext.getMachOSection("__TEXT","__symbol_stub1",
561 MCSectionMachO::S_SYMBOL_STUBS |
562 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
563 16, SectionKind::getText());
564 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
565 MCSymbol *Stub = Stubs[i].first;
566 MCSymbol *RawSym = Stubs[i].second.getPointer();
567 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
569 OutStreamer.SwitchSection(StubSection);
571 OutStreamer.EmitLabel(Stub);
572 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
573 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
575 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
578 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
580 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
581 OutStreamer.EmitRawText(StringRef("\tbctr"));
582 OutStreamer.SwitchSection(LSPSection);
583 OutStreamer.EmitLabel(LazyPtr);
584 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
587 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
589 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
592 OutStreamer.AddBlankLine();
596 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
597 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
599 // Darwin/PPC always uses mach-o.
600 const TargetLoweringObjectFileMachO &TLOFMacho =
601 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
602 MachineModuleInfoMachO &MMIMacho =
603 MMI->getObjFileInfo<MachineModuleInfoMachO>();
605 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
607 EmitFunctionStubs(Stubs);
609 if (MAI->doesSupportExceptionHandling() && MMI) {
610 // Add the (possibly multiple) personalities to the set of global values.
611 // Only referenced functions get into the Personalities list.
612 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
613 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
614 E = Personalities.end(); I != E; ++I) {
616 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
617 MachineModuleInfoImpl::StubValueTy &StubSym =
618 MMIMacho.getGVStubEntry(NLPSym);
619 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
624 // Output stubs for dynamically-linked functions.
625 Stubs = MMIMacho.GetGVStubList();
627 // Output macho stubs for external and common global variables.
628 if (!Stubs.empty()) {
629 // Switch with ".non_lazy_symbol_pointer" directive.
630 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
631 EmitAlignment(isPPC64 ? 3 : 2);
633 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
635 OutStreamer.EmitLabel(Stubs[i].first);
636 // .indirect_symbol _foo
637 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
638 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
641 // External to current translation unit.
642 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
644 // Internal to current translation unit.
646 // When we place the LSDA into the TEXT section, the type info pointers
647 // need to be indirect and pc-rel. We accomplish this by using NLPs.
648 // However, sometimes the types are local to the file. So we need to
649 // fill in the value for the NLP in those cases.
650 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
652 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
656 OutStreamer.AddBlankLine();
659 Stubs = MMIMacho.GetHiddenGVStubList();
660 if (!Stubs.empty()) {
661 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
662 EmitAlignment(isPPC64 ? 3 : 2);
664 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
666 OutStreamer.EmitLabel(Stubs[i].first);
668 OutStreamer.EmitValue(MCSymbolRefExpr::
669 Create(Stubs[i].second.getPointer(),
671 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
675 OutStreamer.AddBlankLine();
678 // Funny Darwin hack: This flag tells the linker that no global symbols
679 // contain code that falls through to other global symbols (e.g. the obvious
680 // implementation of multiple entry points). If this doesn't occur, the
681 // linker can safely perform dead code stripping. Since LLVM never generates
682 // code that does this, it is always safe to set.
683 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
685 return AsmPrinter::doFinalization(M);
688 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
689 /// for a MachineFunction to the given output stream, in a format that the
690 /// Darwin assembler can deal with.
692 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
693 MCStreamer &Streamer) {
694 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
696 if (Subtarget->isDarwin())
697 return new PPCDarwinAsmPrinter(tm, Streamer);
698 return new PPCLinuxAsmPrinter(tm, Streamer);
701 // Force static initialization.
702 extern "C" void LLVMInitializePowerPCAsmPrinter() {
703 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
704 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);