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();
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]) {
252 // See if this is a generic print operand
253 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
254 case 'c': // Don't print "$" before a global var name or constant.
255 break; // PPC never has a prefix.
256 case 'L': // Write second word of DImode reference.
257 // Verify that this operand has two consecutive registers.
258 if (!MI->getOperand(OpNo).isReg() ||
259 OpNo+1 == MI->getNumOperands() ||
260 !MI->getOperand(OpNo+1).isReg())
262 ++OpNo; // Return the high-part.
265 // Write 'i' if an integer constant, otherwise nothing. Used to print
267 if (MI->getOperand(OpNo).isImm())
273 printOperand(MI, OpNo, O);
277 // At the moment, all inline asm memory operands are a single register.
278 // In any case, the output of this routine should always be just one
279 // assembler operand.
281 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
283 const char *ExtraCode,
285 if (ExtraCode && ExtraCode[0])
286 return true; // Unknown modifier.
287 assert(MI->getOperand(OpNo).isReg());
289 printOperand(MI, OpNo, O);
295 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
296 /// the current output stream.
298 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
301 // Lower multi-instruction pseudo operations.
302 switch (MI->getOpcode()) {
304 case TargetOpcode::DBG_VALUE: {
305 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
308 raw_svector_ostream O(Str);
309 unsigned NOps = MI->getNumOperands();
311 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
312 // cast away const; DIetc do not take const operands for some reason.
313 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
316 // Frame address. Currently handles register +- offset only.
317 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
318 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
321 printOperand(MI, NOps-2, O);
322 OutStreamer.EmitRawText(O.str());
326 case PPC::MovePCtoLR:
327 case PPC::MovePCtoLR8: {
328 // Transform %LR = MovePCtoLR
329 // Into this, where the label is the PIC base:
332 MCSymbol *PICBase = MF->getPICBaseSymbol();
335 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here.
338 // FIXME: We would like an efficient form for this, so we don't have to do
339 // a lot of extra uniquing.
340 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::
341 Create(PICBase, OutContext)));
342 OutStreamer.EmitInstruction(TmpInst);
345 OutStreamer.EmitLabel(PICBase);
351 // Transform %X3 = LDtoc <ga:@min1>, %X2
352 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
354 // Change the opcode to LD, and the global address operand to be a
355 // reference to the TOC entry we will synthesize later.
356 TmpInst.setOpcode(PPC::LD);
357 const MachineOperand &MO = MI->getOperand(1);
359 // Map symbol -> label of TOC entry
360 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
361 MCSymbol *MOSymbol = 0;
363 MOSymbol = Mang->getSymbol(MO.getGlobal());
365 MOSymbol = GetCPISymbol(MO.getIndex());
367 MOSymbol = GetJTISymbol(MO.getIndex());
368 MCSymbol *&TOCEntry = TOC[MOSymbol];
370 TOCEntry = GetTempSymbol("C", TOCLabelID++);
373 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
375 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
376 OutStreamer.EmitInstruction(TmpInst);
381 case PPC::MFCR8pseud:
382 // Transform: %R3 = MFCRpseud %CR7
383 // Into: %R3 = MFCR ;; cr7
384 OutStreamer.AddComment(PPCInstPrinter::
385 getRegisterName(MI->getOperand(1).getReg()));
386 TmpInst.setOpcode(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR);
387 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
388 OutStreamer.EmitInstruction(TmpInst);
391 // In Book E sync is called msync, handle this special case here...
392 if (Subtarget.isBookE()) {
393 OutStreamer.EmitRawText(StringRef("\tmsync"));
398 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
399 OutStreamer.EmitInstruction(TmpInst);
402 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
403 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
404 return AsmPrinter::EmitFunctionEntryLabel();
406 // Emit an official procedure descriptor.
407 const MCSection *Current = OutStreamer.getCurrentSection();
408 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
409 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
410 SectionKind::getReadOnly());
411 OutStreamer.SwitchSection(Section);
412 OutStreamer.EmitLabel(CurrentFnSym);
413 OutStreamer.EmitValueToAlignment(8);
415 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
416 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC.@tocbase"));
417 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
418 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
419 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2, OutContext),
420 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
421 OutStreamer.SwitchSection(Current);
423 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
424 ".L." + Twine(CurrentFnSym->getName()));
425 OutStreamer.EmitLabel(RealFnSym);
426 CurrentFnSymForSize = RealFnSym;
430 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
431 const TargetData *TD = TM.getTargetData();
433 bool isPPC64 = TD->getPointerSizeInBits() == 64;
435 if (isPPC64 && !TOC.empty()) {
436 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
437 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
438 SectionKind::getReadOnly());
439 OutStreamer.SwitchSection(Section);
441 // FIXME: This is nondeterminstic!
442 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
443 E = TOC.end(); I != E; ++I) {
444 OutStreamer.EmitLabel(I->second);
445 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
446 "[TC]," + I->first->getName());
450 return AsmPrinter::doFinalization(M);
453 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
454 static const char *const CPUDirectives[] = {
470 unsigned Directive = Subtarget.getDarwinDirective();
471 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
472 Directive = PPC::DIR_970;
473 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
474 Directive = PPC::DIR_7400;
475 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
476 Directive = PPC::DIR_64;
477 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
479 // FIXME: This is a total hack, finish mc'izing the PPC backend.
480 if (OutStreamer.hasRawTextSupport())
481 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
483 // Prime text sections so they are adjacent. This reduces the likelihood a
484 // large data or debug section causes a branch to exceed 16M limit.
485 const TargetLoweringObjectFileMachO &TLOFMacho =
486 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
487 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
488 if (TM.getRelocationModel() == Reloc::PIC_) {
489 OutStreamer.SwitchSection(
490 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
491 MCSectionMachO::S_SYMBOL_STUBS |
492 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
493 32, SectionKind::getText()));
494 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
495 OutStreamer.SwitchSection(
496 OutContext.getMachOSection("__TEXT","__symbol_stub1",
497 MCSectionMachO::S_SYMBOL_STUBS |
498 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
499 16, SectionKind::getText()));
501 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
504 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
505 // Remove $stub suffix, add $lazy_ptr.
506 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
507 TmpStr += "$lazy_ptr";
508 return Ctx.GetOrCreateSymbol(TmpStr.str());
511 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
512 // Add $tmp suffix to $stub, yielding $stub$tmp.
513 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
515 return Ctx.GetOrCreateSymbol(TmpStr.str());
518 void PPCDarwinAsmPrinter::
519 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
520 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
522 const TargetLoweringObjectFileMachO &TLOFMacho =
523 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
525 // .lazy_symbol_pointer
526 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
528 // Output stubs for dynamically-linked functions
529 if (TM.getRelocationModel() == Reloc::PIC_) {
530 const MCSection *StubSection =
531 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
532 MCSectionMachO::S_SYMBOL_STUBS |
533 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
534 32, SectionKind::getText());
535 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
536 OutStreamer.SwitchSection(StubSection);
539 MCSymbol *Stub = Stubs[i].first;
540 MCSymbol *RawSym = Stubs[i].second.getPointer();
541 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
542 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
544 OutStreamer.EmitLabel(Stub);
545 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
546 // FIXME: MCize this.
547 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
548 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
549 OutStreamer.EmitLabel(AnonSymbol);
550 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
551 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
552 "-" + AnonSymbol->getName() + ")");
553 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
556 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
557 "-" + AnonSymbol->getName() + ")(r11)");
559 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
560 "-" + AnonSymbol->getName() + ")(r11)");
561 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
562 OutStreamer.EmitRawText(StringRef("\tbctr"));
564 OutStreamer.SwitchSection(LSPSection);
565 OutStreamer.EmitLabel(LazyPtr);
566 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
569 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
571 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
573 OutStreamer.AddBlankLine();
577 const MCSection *StubSection =
578 OutContext.getMachOSection("__TEXT","__symbol_stub1",
579 MCSectionMachO::S_SYMBOL_STUBS |
580 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
581 16, SectionKind::getText());
582 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
583 MCSymbol *Stub = Stubs[i].first;
584 MCSymbol *RawSym = Stubs[i].second.getPointer();
585 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
587 OutStreamer.SwitchSection(StubSection);
589 OutStreamer.EmitLabel(Stub);
590 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
591 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
593 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
596 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
598 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
599 OutStreamer.EmitRawText(StringRef("\tbctr"));
600 OutStreamer.SwitchSection(LSPSection);
601 OutStreamer.EmitLabel(LazyPtr);
602 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
605 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
607 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
610 OutStreamer.AddBlankLine();
614 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
615 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
617 // Darwin/PPC always uses mach-o.
618 const TargetLoweringObjectFileMachO &TLOFMacho =
619 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
620 MachineModuleInfoMachO &MMIMacho =
621 MMI->getObjFileInfo<MachineModuleInfoMachO>();
623 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
625 EmitFunctionStubs(Stubs);
627 if (MAI->doesSupportExceptionHandling() && MMI) {
628 // Add the (possibly multiple) personalities to the set of global values.
629 // Only referenced functions get into the Personalities list.
630 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
631 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
632 E = Personalities.end(); I != E; ++I) {
634 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
635 MachineModuleInfoImpl::StubValueTy &StubSym =
636 MMIMacho.getGVStubEntry(NLPSym);
637 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
642 // Output stubs for dynamically-linked functions.
643 Stubs = MMIMacho.GetGVStubList();
645 // Output macho stubs for external and common global variables.
646 if (!Stubs.empty()) {
647 // Switch with ".non_lazy_symbol_pointer" directive.
648 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
649 EmitAlignment(isPPC64 ? 3 : 2);
651 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
653 OutStreamer.EmitLabel(Stubs[i].first);
654 // .indirect_symbol _foo
655 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
656 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
659 // External to current translation unit.
660 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
662 // Internal to current translation unit.
664 // When we place the LSDA into the TEXT section, the type info pointers
665 // need to be indirect and pc-rel. We accomplish this by using NLPs.
666 // However, sometimes the types are local to the file. So we need to
667 // fill in the value for the NLP in those cases.
668 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
670 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
674 OutStreamer.AddBlankLine();
677 Stubs = MMIMacho.GetHiddenGVStubList();
678 if (!Stubs.empty()) {
679 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
680 EmitAlignment(isPPC64 ? 3 : 2);
682 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
684 OutStreamer.EmitLabel(Stubs[i].first);
686 OutStreamer.EmitValue(MCSymbolRefExpr::
687 Create(Stubs[i].second.getPointer(),
689 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
693 OutStreamer.AddBlankLine();
696 // Funny Darwin hack: This flag tells the linker that no global symbols
697 // contain code that falls through to other global symbols (e.g. the obvious
698 // implementation of multiple entry points). If this doesn't occur, the
699 // linker can safely perform dead code stripping. Since LLVM never generates
700 // code that does this, it is always safe to set.
701 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
703 return AsmPrinter::doFinalization(M);
706 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
707 /// for a MachineFunction to the given output stream, in a format that the
708 /// Darwin assembler can deal with.
710 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
711 MCStreamer &Streamer) {
712 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
714 if (Subtarget->isDarwin())
715 return new PPCDarwinAsmPrinter(tm, Streamer);
716 return new PPCLinuxAsmPrinter(tm, Streamer);
719 // Force static initialization.
720 extern "C" void LLVMInitializePowerPCAsmPrinter() {
721 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
722 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);