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 "PPCPredicates.h"
22 #include "PPCTargetMachine.h"
23 #include "PPCSubtarget.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/DwarfWriter.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/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCContext.h"
36 #include "llvm/MC/MCSectionMachO.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Target/Mangler.h"
40 #include "llvm/Target/TargetLoweringObjectFile.h"
41 #include "llvm/Target/TargetRegisterInfo.h"
42 #include "llvm/Target/TargetInstrInfo.h"
43 #include "llvm/Target/TargetOptions.h"
44 #include "llvm/Target/TargetRegistry.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/Debug.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/FormattedStream.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringSet.h"
52 #include "llvm/ADT/SmallString.h"
56 class PPCAsmPrinter : public AsmPrinter {
58 DenseMap<const MCSymbol*, const MCSymbol*> TOC;
59 const PPCSubtarget &Subtarget;
62 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
63 const MCAsmInfo *T, bool V)
64 : AsmPrinter(O, TM, T, V),
65 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
67 virtual const char *getPassName() const {
68 return "PowerPC Assembly Printer";
71 PPCTargetMachine &getTM() {
72 return static_cast<PPCTargetMachine&>(TM);
75 unsigned enumRegToMachineReg(unsigned enumReg) {
77 default: llvm_unreachable("Unhandled register!");
78 case PPC::CR0: return 0;
79 case PPC::CR1: return 1;
80 case PPC::CR2: return 2;
81 case PPC::CR3: return 3;
82 case PPC::CR4: return 4;
83 case PPC::CR5: return 5;
84 case PPC::CR6: return 6;
85 case PPC::CR7: return 7;
90 /// printInstruction - This method is automatically generated by tablegen
91 /// from the instruction set description. This method returns true if the
92 /// machine instruction was sufficiently described to print it, otherwise it
94 void printInstruction(const MachineInstr *MI);
95 static const char *getRegisterName(unsigned RegNo);
98 virtual void EmitInstruction(const MachineInstr *MI);
99 void printOp(const MachineOperand &MO);
101 /// stripRegisterPrefix - This method strips the character prefix from a
102 /// register name so that only the number is left. Used by for linux asm.
103 const char *stripRegisterPrefix(const char *RegName) {
104 switch (RegName[0]) {
107 case 'v': return RegName + 1;
108 case 'c': if (RegName[1] == 'r') return RegName + 2;
114 /// printRegister - Print register according to target requirements.
116 void printRegister(const MachineOperand &MO, bool R0AsZero) {
117 unsigned RegNo = MO.getReg();
118 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
120 // If we should use 0 for R0.
121 if (R0AsZero && RegNo == PPC::R0) {
126 const char *RegName = getRegisterName(RegNo);
127 // Linux assembler (Others?) does not take register mnemonics.
128 // FIXME - What about special registers used in mfspr/mtspr?
129 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
133 void printOperand(const MachineInstr *MI, unsigned OpNo) {
134 const MachineOperand &MO = MI->getOperand(OpNo);
136 printRegister(MO, false);
137 } else if (MO.isImm()) {
144 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
145 unsigned AsmVariant, const char *ExtraCode);
146 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
147 unsigned AsmVariant, const char *ExtraCode);
150 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
151 char value = MI->getOperand(OpNo).getImm();
152 value = (value << (32-5)) >> (32-5);
155 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
156 unsigned char value = MI->getOperand(OpNo).getImm();
157 assert(value <= 31 && "Invalid u5imm argument!");
158 O << (unsigned int)value;
160 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
161 unsigned char value = MI->getOperand(OpNo).getImm();
162 assert(value <= 63 && "Invalid u6imm argument!");
163 O << (unsigned int)value;
165 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
166 O << (short)MI->getOperand(OpNo).getImm();
168 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
169 O << (unsigned short)MI->getOperand(OpNo).getImm();
171 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
172 if (MI->getOperand(OpNo).isImm()) {
173 O << (short)(MI->getOperand(OpNo).getImm()*4);
176 printOp(MI->getOperand(OpNo));
177 if (TM.getRelocationModel() == Reloc::PIC_)
178 O << "-\"L" << getFunctionNumber() << "$pb\")";
183 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
184 // Branches can take an immediate operand. This is used by the branch
185 // selection pass to print $+8, an eight byte displacement from the PC.
186 if (MI->getOperand(OpNo).isImm()) {
187 O << "$+" << MI->getOperand(OpNo).getImm()*4;
189 printOp(MI->getOperand(OpNo));
192 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
193 const MachineOperand &MO = MI->getOperand(OpNo);
194 if (TM.getRelocationModel() != Reloc::Static) {
195 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
196 GlobalValue *GV = MO.getGlobal();
197 if (GV->isDeclaration() || GV->isWeakForLinker()) {
198 // Dynamically-resolved functions need a stub for the function.
199 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
200 const MCSymbol *&StubSym =
201 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
203 StubSym = GetGlobalValueSymbol(GV);
208 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
209 SmallString<128> TempNameStr;
210 TempNameStr += StringRef(MO.getSymbolName());
211 TempNameStr += StringRef("$stub");
213 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
214 const MCSymbol *&StubSym =
215 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
217 StubSym = GetExternalSymbolSymbol(MO.getSymbolName());
223 printOp(MI->getOperand(OpNo));
225 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
226 O << (int)MI->getOperand(OpNo).getImm()*4;
228 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
229 O << "\"L" << getFunctionNumber() << "$pb\"\n";
230 O << "\"L" << getFunctionNumber() << "$pb\":";
232 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
233 if (MI->getOperand(OpNo).isImm()) {
234 printS16ImmOperand(MI, OpNo);
236 if (Subtarget.isDarwin()) O << "ha16(";
237 printOp(MI->getOperand(OpNo));
238 if (TM.getRelocationModel() == Reloc::PIC_)
239 O << "-\"L" << getFunctionNumber() << "$pb\"";
240 if (Subtarget.isDarwin())
246 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
247 if (MI->getOperand(OpNo).isImm()) {
248 printS16ImmOperand(MI, OpNo);
250 if (Subtarget.isDarwin()) O << "lo16(";
251 printOp(MI->getOperand(OpNo));
252 if (TM.getRelocationModel() == Reloc::PIC_)
253 O << "-\"L" << getFunctionNumber() << "$pb\"";
254 if (Subtarget.isDarwin())
260 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
261 unsigned CCReg = MI->getOperand(OpNo).getReg();
262 unsigned RegNo = enumRegToMachineReg(CCReg);
263 O << (0x80 >> RegNo);
265 // The new addressing mode printers.
266 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
267 printSymbolLo(MI, OpNo);
269 if (MI->getOperand(OpNo+1).isReg() &&
270 MI->getOperand(OpNo+1).getReg() == PPC::R0)
273 printOperand(MI, OpNo+1);
276 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
277 if (MI->getOperand(OpNo).isImm())
278 printS16X4ImmOperand(MI, OpNo);
280 printSymbolLo(MI, OpNo);
282 if (MI->getOperand(OpNo+1).isReg() &&
283 MI->getOperand(OpNo+1).getReg() == PPC::R0)
286 printOperand(MI, OpNo+1);
290 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
291 // When used as the base register, r0 reads constant zero rather than
292 // the value contained in the register. For this reason, the darwin
293 // assembler requires that we print r0 as 0 (no r) when used as the base.
294 const MachineOperand &MO = MI->getOperand(OpNo);
295 printRegister(MO, true);
297 printOperand(MI, OpNo+1);
300 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
301 const MachineOperand &MO = MI->getOperand(OpNo);
303 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
305 const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
307 // Map symbol -> label of TOC entry.
308 const MCSymbol *&TOCEntry = TOC[Sym];
310 TOCEntry = OutContext.
311 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
314 O << *TOCEntry << "@toc";
317 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
318 const char *Modifier);
321 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
322 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
324 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
325 const MCAsmInfo *T, bool V)
326 : PPCAsmPrinter(O, TM, T, V){}
328 virtual const char *getPassName() const {
329 return "Linux PPC Assembly Printer";
332 bool doFinalization(Module &M);
334 virtual void EmitFunctionEntryLabel();
336 void getAnalysisUsage(AnalysisUsage &AU) const {
337 AU.setPreservesAll();
338 AU.addRequired<MachineModuleInfo>();
339 AU.addRequired<DwarfWriter>();
340 PPCAsmPrinter::getAnalysisUsage(AU);
344 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
346 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
347 formatted_raw_ostream &OS;
349 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
350 const MCAsmInfo *T, bool V)
351 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
353 virtual const char *getPassName() const {
354 return "Darwin PPC Assembly Printer";
357 bool doFinalization(Module &M);
358 void EmitStartOfAsmFile(Module &M);
360 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
362 void getAnalysisUsage(AnalysisUsage &AU) const {
363 AU.setPreservesAll();
364 AU.addRequired<MachineModuleInfo>();
365 AU.addRequired<DwarfWriter>();
366 PPCAsmPrinter::getAnalysisUsage(AU);
369 } // end of anonymous namespace
371 // Include the auto-generated portion of the assembly writer
372 #include "PPCGenAsmWriter.inc"
374 void PPCAsmPrinter::printOp(const MachineOperand &MO) {
375 switch (MO.getType()) {
376 case MachineOperand::MO_Immediate:
377 llvm_unreachable("printOp() does not handle immediate values");
379 case MachineOperand::MO_MachineBasicBlock:
380 O << *MO.getMBB()->getSymbol(OutContext);
382 case MachineOperand::MO_JumpTableIndex:
383 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
384 << '_' << MO.getIndex();
385 // FIXME: PIC relocation model
387 case MachineOperand::MO_ConstantPoolIndex:
388 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
389 << '_' << MO.getIndex();
391 case MachineOperand::MO_BlockAddress:
392 O << *GetBlockAddressSymbol(MO.getBlockAddress());
394 case MachineOperand::MO_ExternalSymbol: {
395 // Computing the address of an external symbol, not calling it.
396 if (TM.getRelocationModel() == Reloc::Static) {
397 O << *GetExternalSymbolSymbol(MO.getSymbolName());
401 const MCSymbol *NLPSym =
402 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
403 MO.getSymbolName()+"$non_lazy_ptr");
404 const MCSymbol *&StubSym =
405 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
407 StubSym = GetExternalSymbolSymbol(MO.getSymbolName());
412 case MachineOperand::MO_GlobalAddress: {
413 // Computing the address of a global symbol, not calling it.
414 GlobalValue *GV = MO.getGlobal();
415 MCSymbol *SymToPrint;
417 // External or weakly linked global variables need non-lazily-resolved stubs
418 if (TM.getRelocationModel() != Reloc::Static &&
419 (GV->isDeclaration() || GV->isWeakForLinker())) {
420 if (!GV->hasHiddenVisibility()) {
421 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
422 const MCSymbol *&StubSym =
423 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(SymToPrint);
425 StubSym = GetGlobalValueSymbol(GV);
426 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
427 GV->hasAvailableExternallyLinkage()) {
428 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
430 const MCSymbol *&StubSym =
431 MMI->getObjFileInfo<MachineModuleInfoMachO>().
432 getHiddenGVStubEntry(SymToPrint);
434 StubSym = GetGlobalValueSymbol(GV);
436 SymToPrint = GetGlobalValueSymbol(GV);
439 SymToPrint = GetGlobalValueSymbol(GV);
444 printOffset(MO.getOffset());
449 O << "<unknown operand type: " << MO.getType() << ">";
454 /// PrintAsmOperand - Print out an operand for an inline asm expression.
456 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
458 const char *ExtraCode) {
459 // Does this asm operand have a single letter operand modifier?
460 if (ExtraCode && ExtraCode[0]) {
461 if (ExtraCode[1] != 0) return true; // Unknown modifier.
463 switch (ExtraCode[0]) {
464 default: return true; // Unknown modifier.
465 case 'c': // Don't print "$" before a global var name or constant.
466 // PPC never has a prefix.
467 printOperand(MI, OpNo);
469 case 'L': // Write second word of DImode reference.
470 // Verify that this operand has two consecutive registers.
471 if (!MI->getOperand(OpNo).isReg() ||
472 OpNo+1 == MI->getNumOperands() ||
473 !MI->getOperand(OpNo+1).isReg())
475 ++OpNo; // Return the high-part.
478 // Write 'i' if an integer constant, otherwise nothing. Used to print
480 if (MI->getOperand(OpNo).isImm())
486 printOperand(MI, OpNo);
490 // At the moment, all inline asm memory operands are a single register.
491 // In any case, the output of this routine should always be just one
492 // assembler operand.
494 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
496 const char *ExtraCode) {
497 if (ExtraCode && ExtraCode[0])
498 return true; // Unknown modifier.
499 assert (MI->getOperand(OpNo).isReg());
501 printOperand(MI, OpNo);
506 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
507 const char *Modifier) {
508 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
509 unsigned Code = MI->getOperand(OpNo).getImm();
510 if (!strcmp(Modifier, "cc")) {
511 switch ((PPC::Predicate)Code) {
512 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
513 case PPC::PRED_LT: O << "lt"; return;
514 case PPC::PRED_LE: O << "le"; return;
515 case PPC::PRED_EQ: O << "eq"; return;
516 case PPC::PRED_GE: O << "ge"; return;
517 case PPC::PRED_GT: O << "gt"; return;
518 case PPC::PRED_NE: O << "ne"; return;
519 case PPC::PRED_UN: O << "un"; return;
520 case PPC::PRED_NU: O << "nu"; return;
524 assert(!strcmp(Modifier, "reg") &&
525 "Need to specify 'cc' or 'reg' as predicate op modifier!");
526 // Don't print the register for 'always'.
527 if (Code == PPC::PRED_ALWAYS) return;
528 printOperand(MI, OpNo+1);
533 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
534 /// the current output stream.
536 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
537 // Check for slwi/srwi mnemonics.
538 if (MI->getOpcode() == PPC::RLWINM) {
539 unsigned char SH = MI->getOperand(2).getImm();
540 unsigned char MB = MI->getOperand(3).getImm();
541 unsigned char ME = MI->getOperand(4).getImm();
542 bool useSubstituteMnemonic = false;
543 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
544 O << "\tslwi "; useSubstituteMnemonic = true;
546 if (SH <= 31 && MB == (32-SH) && ME == 31) {
547 O << "\tsrwi "; useSubstituteMnemonic = true;
550 if (useSubstituteMnemonic) {
554 O << ", " << (unsigned int)SH;
559 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
560 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
568 if (MI->getOpcode() == PPC::RLDICR) {
569 unsigned char SH = MI->getOperand(2).getImm();
570 unsigned char ME = MI->getOperand(3).getImm();
571 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
577 O << ", " << (unsigned int)SH;
582 printInstruction(MI);
585 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
586 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
587 return AsmPrinter::EmitFunctionEntryLabel();
589 // Emit an official procedure descriptor.
590 // FIXME 64-bit SVR4: Use MCSection here!
591 O << "\t.section\t\".opd\",\"aw\"\n";
593 OutStreamer.EmitLabel(CurrentFnSym);
594 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
595 O << "\t.previous\n";
596 O << ".L." << *CurrentFnSym << ":\n";
600 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
601 const TargetData *TD = TM.getTargetData();
603 bool isPPC64 = TD->getPointerSizeInBits() == 64;
605 if (isPPC64 && !TOC.empty()) {
606 // FIXME 64-bit SVR4: Use MCSection here?
607 O << "\t.section\t\".toc\",\"aw\"\n";
609 // FIXME: This is nondeterminstic!
610 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
611 E = TOC.end(); I != E; ++I) {
612 O << *I->second << ":\n";
613 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
617 return AsmPrinter::doFinalization(M);
620 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
621 static const char *const CPUDirectives[] = {
633 unsigned Directive = Subtarget.getDarwinDirective();
634 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
635 Directive = PPC::DIR_970;
636 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
637 Directive = PPC::DIR_7400;
638 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
639 Directive = PPC::DIR_64;
640 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
641 O << "\t.machine " << CPUDirectives[Directive] << '\n';
643 // Prime text sections so they are adjacent. This reduces the likelihood a
644 // large data or debug section causes a branch to exceed 16M limit.
645 TargetLoweringObjectFileMachO &TLOFMacho =
646 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
647 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
648 if (TM.getRelocationModel() == Reloc::PIC_) {
649 OutStreamer.SwitchSection(
650 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
651 MCSectionMachO::S_SYMBOL_STUBS |
652 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
653 32, SectionKind::getText()));
654 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
655 OutStreamer.SwitchSection(
656 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
657 MCSectionMachO::S_SYMBOL_STUBS |
658 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
659 16, SectionKind::getText()));
661 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
664 static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
665 // Remove $stub suffix, add $lazy_ptr.
666 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
667 TmpStr += "$lazy_ptr";
668 return Ctx.GetOrCreateSymbol(TmpStr.str());
671 static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
672 // Add $tmp suffix to $stub, yielding $stub$tmp.
673 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
675 return Ctx.GetOrCreateSymbol(TmpStr.str());
678 void PPCDarwinAsmPrinter::
679 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
680 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
682 TargetLoweringObjectFileMachO &TLOFMacho =
683 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
685 // .lazy_symbol_pointer
686 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
688 // Output stubs for dynamically-linked functions
689 if (TM.getRelocationModel() == Reloc::PIC_) {
690 const MCSection *StubSection =
691 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
692 MCSectionMachO::S_SYMBOL_STUBS |
693 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
694 32, SectionKind::getText());
695 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
696 OutStreamer.SwitchSection(StubSection);
699 const MCSymbol *Stub = Stubs[i].first;
700 const MCSymbol *RawSym = Stubs[i].second;
701 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
702 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
705 O << "\t.indirect_symbol " << *RawSym << '\n';
707 O << "\tbcl 20,31," << *AnonSymbol << '\n';
708 O << *AnonSymbol << ":\n";
710 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
713 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
714 << '-' << *AnonSymbol << ")(r11)\n";
715 O << "\tmtctr r12\n";
718 OutStreamer.SwitchSection(LSPSection);
719 O << *LazyPtr << ":\n";
720 O << "\t.indirect_symbol " << *RawSym << '\n';
721 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
727 const MCSection *StubSection =
728 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
729 MCSectionMachO::S_SYMBOL_STUBS |
730 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
731 16, SectionKind::getText());
732 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
733 const MCSymbol *Stub = Stubs[i].first;
734 const MCSymbol *RawSym = Stubs[i].second;
735 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
737 OutStreamer.SwitchSection(StubSection);
740 O << "\t.indirect_symbol " << *RawSym << '\n';
741 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
742 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
744 O << "\tmtctr r12\n";
746 OutStreamer.SwitchSection(LSPSection);
747 O << *LazyPtr << ":\n";
748 O << "\t.indirect_symbol " << *RawSym << '\n';
749 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
756 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
757 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
759 // Darwin/PPC always uses mach-o.
760 TargetLoweringObjectFileMachO &TLOFMacho =
761 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
762 MachineModuleInfoMachO &MMIMacho =
763 MMI->getObjFileInfo<MachineModuleInfoMachO>();
765 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
767 EmitFunctionStubs(Stubs);
769 if (MAI->doesSupportExceptionHandling() && MMI) {
770 // Add the (possibly multiple) personalities to the set of global values.
771 // Only referenced functions get into the Personalities list.
772 const std::vector<Function *> &Personalities = MMI->getPersonalities();
773 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
774 E = Personalities.end(); I != E; ++I) {
776 const MCSymbol *NLPSym =
777 GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
778 const MCSymbol *&StubSym = MMIMacho.getGVStubEntry(NLPSym);
779 StubSym = GetGlobalValueSymbol(*I);
784 // Output stubs for dynamically-linked functions.
785 Stubs = MMIMacho.GetGVStubList();
787 // Output macho stubs for external and common global variables.
788 if (!Stubs.empty()) {
789 // Switch with ".non_lazy_symbol_pointer" directive.
790 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
791 EmitAlignment(isPPC64 ? 3 : 2);
793 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
794 O << *Stubs[i].first << ":\n";
795 O << "\t.indirect_symbol " << *Stubs[i].second << '\n';
796 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
800 Stubs = MMIMacho.GetHiddenGVStubList();
801 if (!Stubs.empty()) {
802 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
803 EmitAlignment(isPPC64 ? 3 : 2);
805 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
806 O << *Stubs[i].first << ":\n";
807 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << *Stubs[i].second << '\n';
811 // Funny Darwin hack: This flag tells the linker that no global symbols
812 // contain code that falls through to other global symbols (e.g. the obvious
813 // implementation of multiple entry points). If this doesn't occur, the
814 // linker can safely perform dead code stripping. Since LLVM never generates
815 // code that does this, it is always safe to set.
816 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
818 return AsmPrinter::doFinalization(M);
823 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
824 /// for a MachineFunction to the given output stream, in a format that the
825 /// Darwin assembler can deal with.
827 static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
829 const MCAsmInfo *tai,
831 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
833 if (Subtarget->isDarwin())
834 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
835 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
838 // Force static initialization.
839 extern "C" void LLVMInitializePowerPCAsmPrinter() {
840 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
841 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);