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/MCInstBuilder.h"
41 #include "llvm/MC/MCSectionMachO.h"
42 #include "llvm/MC/MCStreamer.h"
43 #include "llvm/MC/MCSymbol.h"
44 #include "llvm/MC/MCSectionELF.h"
45 #include "llvm/Target/Mangler.h"
46 #include "llvm/Target/TargetRegisterInfo.h"
47 #include "llvm/Target/TargetInstrInfo.h"
48 #include "llvm/Target/TargetOptions.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/MathExtras.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/TargetRegistry.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Support/ELF.h"
56 #include "llvm/ADT/StringExtras.h"
57 #include "llvm/ADT/SmallString.h"
58 #include "llvm/ADT/MapVector.h"
62 class PPCAsmPrinter : public AsmPrinter {
64 MapVector<MCSymbol*, MCSymbol*> TOC;
65 const PPCSubtarget &Subtarget;
68 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
69 : AsmPrinter(TM, Streamer),
70 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
72 virtual const char *getPassName() const {
73 return "PowerPC Assembly Printer";
77 virtual void EmitInstruction(const MachineInstr *MI);
79 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
81 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
82 unsigned AsmVariant, const char *ExtraCode,
84 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
85 unsigned AsmVariant, const char *ExtraCode,
88 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
89 MachineLocation Location;
90 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
91 // Frame address. Currently handles register +- offset only.
92 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
93 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
95 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
101 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
102 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
104 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
105 : PPCAsmPrinter(TM, Streamer) {}
107 virtual const char *getPassName() const {
108 return "Linux PPC Assembly Printer";
111 bool doFinalization(Module &M);
113 virtual void EmitFunctionEntryLabel();
115 void EmitFunctionBodyEnd();
118 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
120 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
122 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
123 : PPCAsmPrinter(TM, Streamer) {}
125 virtual const char *getPassName() const {
126 return "Darwin PPC Assembly Printer";
129 bool doFinalization(Module &M);
130 void EmitStartOfAsmFile(Module &M);
132 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
134 } // end of anonymous namespace
136 /// stripRegisterPrefix - This method strips the character prefix from a
137 /// register name so that only the number is left. Used by for linux asm.
138 static const char *stripRegisterPrefix(const char *RegName) {
139 switch (RegName[0]) {
142 case 'v': return RegName + 1;
143 case 'c': if (RegName[1] == 'r') return RegName + 2;
149 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
151 const MachineOperand &MO = MI->getOperand(OpNo);
153 switch (MO.getType()) {
154 case MachineOperand::MO_Register: {
155 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
156 // Linux assembler (Others?) does not take register mnemonics.
157 // FIXME - What about special registers used in mfspr/mtspr?
158 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
162 case MachineOperand::MO_Immediate:
166 case MachineOperand::MO_MachineBasicBlock:
167 O << *MO.getMBB()->getSymbol();
169 case MachineOperand::MO_JumpTableIndex:
170 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
171 << '_' << MO.getIndex();
172 // FIXME: PIC relocation model
174 case MachineOperand::MO_ConstantPoolIndex:
175 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
176 << '_' << MO.getIndex();
178 case MachineOperand::MO_BlockAddress:
179 O << *GetBlockAddressSymbol(MO.getBlockAddress());
181 case MachineOperand::MO_ExternalSymbol: {
182 // Computing the address of an external symbol, not calling it.
183 if (TM.getRelocationModel() == Reloc::Static) {
184 O << *GetExternalSymbolSymbol(MO.getSymbolName());
189 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
190 MO.getSymbolName()+"$non_lazy_ptr");
191 MachineModuleInfoImpl::StubValueTy &StubSym =
192 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
193 if (StubSym.getPointer() == 0)
194 StubSym = MachineModuleInfoImpl::
195 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
200 case MachineOperand::MO_GlobalAddress: {
201 // Computing the address of a global symbol, not calling it.
202 const GlobalValue *GV = MO.getGlobal();
203 MCSymbol *SymToPrint;
205 // External or weakly linked global variables need non-lazily-resolved stubs
206 if (TM.getRelocationModel() != Reloc::Static &&
207 (GV->isDeclaration() || GV->isWeakForLinker())) {
208 if (!GV->hasHiddenVisibility()) {
209 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
210 MachineModuleInfoImpl::StubValueTy &StubSym =
211 MMI->getObjFileInfo<MachineModuleInfoMachO>()
212 .getGVStubEntry(SymToPrint);
213 if (StubSym.getPointer() == 0)
214 StubSym = MachineModuleInfoImpl::
215 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
216 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
217 GV->hasAvailableExternallyLinkage()) {
218 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
220 MachineModuleInfoImpl::StubValueTy &StubSym =
221 MMI->getObjFileInfo<MachineModuleInfoMachO>().
222 getHiddenGVStubEntry(SymToPrint);
223 if (StubSym.getPointer() == 0)
224 StubSym = MachineModuleInfoImpl::
225 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
227 SymToPrint = Mang->getSymbol(GV);
230 SymToPrint = Mang->getSymbol(GV);
235 printOffset(MO.getOffset(), O);
240 O << "<unknown operand type: " << MO.getType() << ">";
245 /// PrintAsmOperand - Print out an operand for an inline asm expression.
247 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
249 const char *ExtraCode, raw_ostream &O) {
250 // Does this asm operand have a single letter operand modifier?
251 if (ExtraCode && ExtraCode[0]) {
252 if (ExtraCode[1] != 0) return true; // Unknown modifier.
254 switch (ExtraCode[0]) {
256 // See if this is a generic print operand
257 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
258 case 'c': // Don't print "$" before a global var name or constant.
259 break; // PPC never has a prefix.
260 case 'L': // Write second word of DImode reference.
261 // Verify that this operand has two consecutive registers.
262 if (!MI->getOperand(OpNo).isReg() ||
263 OpNo+1 == MI->getNumOperands() ||
264 !MI->getOperand(OpNo+1).isReg())
266 ++OpNo; // Return the high-part.
269 // Write 'i' if an integer constant, otherwise nothing. Used to print
271 if (MI->getOperand(OpNo).isImm())
277 printOperand(MI, OpNo, O);
281 // At the moment, all inline asm memory operands are a single register.
282 // In any case, the output of this routine should always be just one
283 // assembler operand.
285 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
287 const char *ExtraCode,
289 if (ExtraCode && ExtraCode[0]) {
290 if (ExtraCode[1] != 0) return true; // Unknown modifier.
292 switch (ExtraCode[0]) {
293 default: return true; // Unknown modifier.
294 case 'y': // A memory reference for an X-form instruction
296 const char *RegName = "r0";
297 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
298 O << RegName << ", ";
299 printOperand(MI, OpNo, O);
305 assert(MI->getOperand(OpNo).isReg());
307 printOperand(MI, OpNo, O);
313 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
314 /// the current output stream.
316 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
319 // Lower multi-instruction pseudo operations.
320 switch (MI->getOpcode()) {
322 case TargetOpcode::DBG_VALUE: {
323 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
326 raw_svector_ostream O(Str);
327 unsigned NOps = MI->getNumOperands();
329 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
330 // cast away const; DIetc do not take const operands for some reason.
331 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
334 // Frame address. Currently handles register +- offset only.
335 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
336 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
339 printOperand(MI, NOps-2, O);
340 OutStreamer.EmitRawText(O.str());
344 case PPC::MovePCtoLR:
345 case PPC::MovePCtoLR8: {
346 // Transform %LR = MovePCtoLR
347 // Into this, where the label is the PIC base:
350 MCSymbol *PICBase = MF->getPICBaseSymbol();
353 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL_Darwin) // Darwin vs SVR4 doesn't matter here.
354 // FIXME: We would like an efficient form for this, so we don't have to do
355 // a lot of extra uniquing.
356 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
359 OutStreamer.EmitLabel(PICBase);
365 // Transform %X3 = LDtoc <ga:@min1>, %X2
366 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
368 // Change the opcode to LD, and the global address operand to be a
369 // reference to the TOC entry we will synthesize later.
370 TmpInst.setOpcode(PPC::LD);
371 const MachineOperand &MO = MI->getOperand(1);
373 // Map symbol -> label of TOC entry
374 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
375 MCSymbol *MOSymbol = 0;
377 MOSymbol = Mang->getSymbol(MO.getGlobal());
379 MOSymbol = GetCPISymbol(MO.getIndex());
381 MOSymbol = GetJTISymbol(MO.getIndex());
382 MCSymbol *&TOCEntry = TOC[MOSymbol];
383 // To avoid name clash check if the name already exists.
384 while (TOCEntry == 0) {
385 if (OutContext.LookupSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
386 "C" + Twine(TOCLabelID++)) == 0) {
387 TOCEntry = GetTempSymbol("C", TOCLabelID);
392 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
394 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
395 OutStreamer.EmitInstruction(TmpInst);
400 case PPC::MFCR8pseud:
401 // Transform: %R3 = MFCRpseud %CR7
402 // Into: %R3 = MFCR ;; cr7
403 OutStreamer.AddComment(PPCInstPrinter::
404 getRegisterName(MI->getOperand(1).getReg()));
405 OutStreamer.EmitInstruction(MCInstBuilder(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR)
406 .addReg(MI->getOperand(0).getReg()));
409 // In Book E sync is called msync, handle this special case here...
410 if (Subtarget.isBookE()) {
411 OutStreamer.EmitRawText(StringRef("\tmsync"));
416 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
417 OutStreamer.EmitInstruction(TmpInst);
420 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
421 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
422 return AsmPrinter::EmitFunctionEntryLabel();
424 // Emit an official procedure descriptor.
425 const MCSection *Current = OutStreamer.getCurrentSection();
426 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
427 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
428 SectionKind::getReadOnly());
429 OutStreamer.SwitchSection(Section);
430 OutStreamer.EmitLabel(CurrentFnSym);
431 OutStreamer.EmitValueToAlignment(8);
433 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
434 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
436 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
437 8/*size*/, 0/*addrspace*/);
438 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
439 // Generates a R_PPC64_TOC relocation for TOC base insertion.
440 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
441 MCSymbolRefExpr::VK_PPC_TOC, OutContext),
442 8/*size*/, 0/*addrspace*/);
443 // Emit a null environment pointer.
444 OutStreamer.EmitIntValue(0, 8 /* size */, 0 /* addrspace */);
445 OutStreamer.SwitchSection(Current);
447 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
448 ".L." + Twine(CurrentFnSym->getName()));
449 OutStreamer.EmitLabel(RealFnSym);
450 CurrentFnSymForSize = RealFnSym;
454 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
455 const DataLayout *TD = TM.getDataLayout();
457 bool isPPC64 = TD->getPointerSizeInBits() == 64;
459 if (isPPC64 && !TOC.empty()) {
460 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
461 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
462 SectionKind::getReadOnly());
463 OutStreamer.SwitchSection(Section);
465 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
466 E = TOC.end(); I != E; ++I) {
467 OutStreamer.EmitLabel(I->second);
468 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
469 OutStreamer.EmitTCEntry(*S);
473 return AsmPrinter::doFinalization(M);
476 /// EmitFunctionBodyEnd - Print the traceback table before the .size
479 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
480 // Only the 64-bit target requires a traceback table. For now,
481 // we only emit the word of zeroes that GDB requires to find
482 // the end of the function, and zeroes for the eight-byte
484 // FIXME: We should fill in the eight-byte mandatory fields as described in
485 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
486 // currently make use of these fields).
487 if (Subtarget.isPPC64()) {
488 OutStreamer.EmitIntValue(0, 4/*size*/);
489 OutStreamer.EmitIntValue(0, 8/*size*/);
493 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
494 static const char *const CPUDirectives[] = {
512 unsigned Directive = Subtarget.getDarwinDirective();
513 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
514 Directive = PPC::DIR_970;
515 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
516 Directive = PPC::DIR_7400;
517 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
518 Directive = PPC::DIR_64;
519 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
521 // FIXME: This is a total hack, finish mc'izing the PPC backend.
522 if (OutStreamer.hasRawTextSupport())
523 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
525 // Prime text sections so they are adjacent. This reduces the likelihood a
526 // large data or debug section causes a branch to exceed 16M limit.
527 const TargetLoweringObjectFileMachO &TLOFMacho =
528 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
529 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
530 if (TM.getRelocationModel() == Reloc::PIC_) {
531 OutStreamer.SwitchSection(
532 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
533 MCSectionMachO::S_SYMBOL_STUBS |
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
535 32, SectionKind::getText()));
536 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
537 OutStreamer.SwitchSection(
538 OutContext.getMachOSection("__TEXT","__symbol_stub1",
539 MCSectionMachO::S_SYMBOL_STUBS |
540 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
541 16, SectionKind::getText()));
543 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
546 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
547 // Remove $stub suffix, add $lazy_ptr.
548 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
549 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
552 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
553 // Add $tmp suffix to $stub, yielding $stub$tmp.
554 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
557 void PPCDarwinAsmPrinter::
558 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
559 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
561 const TargetLoweringObjectFileMachO &TLOFMacho =
562 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
564 // .lazy_symbol_pointer
565 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
567 // Output stubs for dynamically-linked functions
568 if (TM.getRelocationModel() == Reloc::PIC_) {
569 const MCSection *StubSection =
570 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
571 MCSectionMachO::S_SYMBOL_STUBS |
572 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
573 32, SectionKind::getText());
574 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
575 OutStreamer.SwitchSection(StubSection);
578 MCSymbol *Stub = Stubs[i].first;
579 MCSymbol *RawSym = Stubs[i].second.getPointer();
580 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
581 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
583 OutStreamer.EmitLabel(Stub);
584 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
587 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
588 // FIXME: MCize this.
589 OutStreamer.EmitRawText("\tbcl 20, 31, " + Twine(AnonSymbol->getName()));
590 OutStreamer.EmitLabel(AnonSymbol);
592 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
593 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
595 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LazyPtr, OutContext),
596 MCSymbolRefExpr::Create(AnonSymbol, OutContext),
598 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
603 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
605 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
606 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
607 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
609 .addExpr(Sub).addExpr(Sub)
612 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
614 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
616 OutStreamer.SwitchSection(LSPSection);
617 OutStreamer.EmitLabel(LazyPtr);
618 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
620 MCSymbol *DyldStubBindingHelper =
621 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
623 // .quad dyld_stub_binding_helper
624 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
626 // .long dyld_stub_binding_helper
627 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
630 OutStreamer.AddBlankLine();
634 const MCSection *StubSection =
635 OutContext.getMachOSection("__TEXT","__symbol_stub1",
636 MCSectionMachO::S_SYMBOL_STUBS |
637 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
638 16, SectionKind::getText());
639 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
640 MCSymbol *Stub = Stubs[i].first;
641 MCSymbol *RawSym = Stubs[i].second.getPointer();
642 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
644 OutStreamer.SwitchSection(StubSection);
646 OutStreamer.EmitLabel(Stub);
647 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
648 // lis r11, ha16(LazyPtr)
649 const MCExpr *LazyPtrHa16 =
650 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_HA16,
652 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
654 .addExpr(LazyPtrHa16));
656 const MCExpr *LazyPtrLo16 =
657 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_LO16,
659 // ldu r12, lo16(LazyPtr)(r11)
660 // lwzu r12, lo16(LazyPtr)(r11)
661 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
663 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
667 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
669 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
671 OutStreamer.SwitchSection(LSPSection);
672 OutStreamer.EmitLabel(LazyPtr);
673 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
675 MCSymbol *DyldStubBindingHelper =
676 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
678 // .quad dyld_stub_binding_helper
679 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
681 // .long dyld_stub_binding_helper
682 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
686 OutStreamer.AddBlankLine();
690 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
691 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
693 // Darwin/PPC always uses mach-o.
694 const TargetLoweringObjectFileMachO &TLOFMacho =
695 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
696 MachineModuleInfoMachO &MMIMacho =
697 MMI->getObjFileInfo<MachineModuleInfoMachO>();
699 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
701 EmitFunctionStubs(Stubs);
703 if (MAI->doesSupportExceptionHandling() && MMI) {
704 // Add the (possibly multiple) personalities to the set of global values.
705 // Only referenced functions get into the Personalities list.
706 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
707 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
708 E = Personalities.end(); I != E; ++I) {
710 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
711 MachineModuleInfoImpl::StubValueTy &StubSym =
712 MMIMacho.getGVStubEntry(NLPSym);
713 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
718 // Output stubs for dynamically-linked functions.
719 Stubs = MMIMacho.GetGVStubList();
721 // Output macho stubs for external and common global variables.
722 if (!Stubs.empty()) {
723 // Switch with ".non_lazy_symbol_pointer" directive.
724 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
725 EmitAlignment(isPPC64 ? 3 : 2);
727 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
729 OutStreamer.EmitLabel(Stubs[i].first);
730 // .indirect_symbol _foo
731 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
732 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
735 // External to current translation unit.
736 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
738 // Internal to current translation unit.
740 // When we place the LSDA into the TEXT section, the type info pointers
741 // need to be indirect and pc-rel. We accomplish this by using NLPs.
742 // However, sometimes the types are local to the file. So we need to
743 // fill in the value for the NLP in those cases.
744 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
746 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
750 OutStreamer.AddBlankLine();
753 Stubs = MMIMacho.GetHiddenGVStubList();
754 if (!Stubs.empty()) {
755 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
756 EmitAlignment(isPPC64 ? 3 : 2);
758 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
760 OutStreamer.EmitLabel(Stubs[i].first);
762 OutStreamer.EmitValue(MCSymbolRefExpr::
763 Create(Stubs[i].second.getPointer(),
765 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
769 OutStreamer.AddBlankLine();
772 // Funny Darwin hack: This flag tells the linker that no global symbols
773 // contain code that falls through to other global symbols (e.g. the obvious
774 // implementation of multiple entry points). If this doesn't occur, the
775 // linker can safely perform dead code stripping. Since LLVM never generates
776 // code that does this, it is always safe to set.
777 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
779 return AsmPrinter::doFinalization(M);
782 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
783 /// for a MachineFunction to the given output stream, in a format that the
784 /// Darwin assembler can deal with.
786 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
787 MCStreamer &Streamer) {
788 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
790 if (Subtarget->isDarwin())
791 return new PPCDarwinAsmPrinter(tm, Streamer);
792 return new PPCLinuxAsmPrinter(tm, Streamer);
795 // Force static initialization.
796 extern "C" void LLVMInitializePowerPCAsmPrinter() {
797 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
798 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);