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/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/Target/Mangler.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetOptions.h"
46 #include "llvm/Target/TargetRegistry.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/raw_ostream.h"
52 #include "llvm/ADT/StringExtras.h"
53 #include "llvm/ADT/StringSet.h"
54 #include "llvm/ADT/SmallString.h"
55 #include "InstPrinter/PPCInstPrinter.h"
58 // This option tells the asmprinter to use the new (experimental) MCInstPrinter
60 static cl::opt<bool> UseInstPrinter("enable-ppc-inst-printer",
66 class PPCAsmPrinter : public AsmPrinter {
68 DenseMap<MCSymbol*, MCSymbol*> TOC;
69 const PPCSubtarget &Subtarget;
72 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
73 : AsmPrinter(TM, Streamer),
74 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
76 virtual const char *getPassName() const {
77 return "PowerPC Assembly Printer";
80 unsigned enumRegToMachineReg(unsigned enumReg) {
82 default: llvm_unreachable("Unhandled register!");
83 case PPC::CR0: return 0;
84 case PPC::CR1: return 1;
85 case PPC::CR2: return 2;
86 case PPC::CR3: return 3;
87 case PPC::CR4: return 4;
88 case PPC::CR5: return 5;
89 case PPC::CR6: return 6;
90 case PPC::CR7: return 7;
95 /// printInstruction - This method is automatically generated by tablegen
96 /// from the instruction set description. This method returns true if the
97 /// machine instruction was sufficiently described to print it, otherwise it
99 void printInstruction(const MachineInstr *MI, raw_ostream &O);
100 static const char *getRegisterName(unsigned RegNo);
103 virtual void EmitInstruction(const MachineInstr *MI);
104 void printOp(const MachineOperand &MO, raw_ostream &O);
106 /// stripRegisterPrefix - This method strips the character prefix from a
107 /// register name so that only the number is left. Used by for linux asm.
108 const char *stripRegisterPrefix(const char *RegName) {
109 switch (RegName[0]) {
112 case 'v': return RegName + 1;
113 case 'c': if (RegName[1] == 'r') return RegName + 2;
119 /// printRegister - Print register according to target requirements.
121 void printRegister(const MachineOperand &MO, bool R0AsZero, raw_ostream &O){
122 unsigned RegNo = MO.getReg();
123 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
125 // If we should use 0 for R0.
126 if (R0AsZero && RegNo == PPC::R0) {
131 const char *RegName = getRegisterName(RegNo);
132 // Linux assembler (Others?) does not take register mnemonics.
133 // FIXME - What about special registers used in mfspr/mtspr?
134 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
138 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
139 const MachineOperand &MO = MI->getOperand(OpNo);
141 printRegister(MO, false, O);
142 } else if (MO.isImm()) {
149 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
150 unsigned AsmVariant, const char *ExtraCode,
152 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
153 unsigned AsmVariant, const char *ExtraCode,
157 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo,
159 char value = MI->getOperand(OpNo).getImm();
160 value = (value << (32-5)) >> (32-5);
163 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
165 unsigned char value = MI->getOperand(OpNo).getImm();
166 assert(value <= 31 && "Invalid u5imm argument!");
167 O << (unsigned int)value;
169 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
171 unsigned char value = MI->getOperand(OpNo).getImm();
172 assert(value <= 63 && "Invalid u6imm argument!");
173 O << (unsigned int)value;
175 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
177 O << (short)MI->getOperand(OpNo).getImm();
179 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
181 O << (unsigned short)MI->getOperand(OpNo).getImm();
183 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo,
185 if (MI->getOperand(OpNo).isImm()) {
186 O << (short)(MI->getOperand(OpNo).getImm()*4);
189 printOp(MI->getOperand(OpNo), O);
190 if (TM.getRelocationModel() == Reloc::PIC_)
191 O << "-\"L" << getFunctionNumber() << "$pb\")";
196 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
198 // Branches can take an immediate operand. This is used by the branch
199 // selection pass to print $+8, an eight byte displacement from the PC.
200 if (MI->getOperand(OpNo).isImm()) {
201 O << "$+" << MI->getOperand(OpNo).getImm()*4;
203 printOp(MI->getOperand(OpNo), O);
206 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
208 const MachineOperand &MO = MI->getOperand(OpNo);
209 if (TM.getRelocationModel() != Reloc::Static) {
211 const GlobalValue *GV = MO.getGlobal();
212 if (GV->isDeclaration() || GV->isWeakForLinker()) {
213 // Dynamically-resolved functions need a stub for the function.
214 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
215 MachineModuleInfoImpl::StubValueTy &StubSym =
216 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
217 if (StubSym.getPointer() == 0)
218 StubSym = MachineModuleInfoImpl::
219 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
225 SmallString<128> TempNameStr;
226 TempNameStr += StringRef(MO.getSymbolName());
227 TempNameStr += StringRef("$stub");
229 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
230 MachineModuleInfoImpl::StubValueTy &StubSym =
231 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
232 if (StubSym.getPointer() == 0)
233 StubSym = MachineModuleInfoImpl::
234 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
240 printOp(MI->getOperand(OpNo), O);
242 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
244 O << (int)MI->getOperand(OpNo).getImm()*4;
246 void printPICLabel(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
247 O << "\"L" << getFunctionNumber() << "$pb\"\n";
248 O << "\"L" << getFunctionNumber() << "$pb\":";
250 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
251 if (MI->getOperand(OpNo).isImm()) {
252 printS16ImmOperand(MI, OpNo, O);
254 if (Subtarget.isDarwin()) O << "ha16(";
255 printOp(MI->getOperand(OpNo), O);
256 if (TM.getRelocationModel() == Reloc::PIC_)
257 O << "-\"L" << getFunctionNumber() << "$pb\"";
258 if (Subtarget.isDarwin())
264 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
265 if (MI->getOperand(OpNo).isImm()) {
266 printS16ImmOperand(MI, OpNo, O);
268 if (Subtarget.isDarwin()) O << "lo16(";
269 printOp(MI->getOperand(OpNo), O);
270 if (TM.getRelocationModel() == Reloc::PIC_)
271 O << "-\"L" << getFunctionNumber() << "$pb\"";
272 if (Subtarget.isDarwin())
278 void printcrbitm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
279 unsigned CCReg = MI->getOperand(OpNo).getReg();
280 unsigned RegNo = enumRegToMachineReg(CCReg);
281 O << (0x80 >> RegNo);
283 // The new addressing mode printers.
284 void printMemRegImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
285 printSymbolLo(MI, OpNo, O);
287 if (MI->getOperand(OpNo+1).isReg() &&
288 MI->getOperand(OpNo+1).getReg() == PPC::R0)
291 printOperand(MI, OpNo+1, O);
294 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo,
296 if (MI->getOperand(OpNo).isImm())
297 printS16X4ImmOperand(MI, OpNo, O);
299 printSymbolLo(MI, OpNo, O);
301 if (MI->getOperand(OpNo+1).isReg() &&
302 MI->getOperand(OpNo+1).getReg() == PPC::R0)
305 printOperand(MI, OpNo+1, O);
309 void printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
310 // When used as the base register, r0 reads constant zero rather than
311 // the value contained in the register. For this reason, the darwin
312 // assembler requires that we print r0 as 0 (no r) when used as the base.
313 const MachineOperand &MO = MI->getOperand(OpNo);
314 printRegister(MO, true, O);
316 printOperand(MI, OpNo+1, O);
319 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo,
321 const MachineOperand &MO = MI->getOperand(OpNo);
322 assert(MO.isGlobal());
323 MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
325 // Map symbol -> label of TOC entry.
326 MCSymbol *&TOCEntry = TOC[Sym];
328 TOCEntry = OutContext.
329 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
330 "C" + Twine(LabelID++));
332 O << *TOCEntry << "@toc";
335 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
336 raw_ostream &O, const char *Modifier);
338 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
340 MachineLocation Location;
341 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
342 // Frame address. Currently handles register +- offset only.
343 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
344 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
346 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
352 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
353 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
355 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
356 : PPCAsmPrinter(TM, Streamer) {}
358 virtual const char *getPassName() const {
359 return "Linux PPC Assembly Printer";
362 bool doFinalization(Module &M);
364 virtual void EmitFunctionEntryLabel();
367 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
369 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
371 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
372 : PPCAsmPrinter(TM, Streamer) {}
374 virtual const char *getPassName() const {
375 return "Darwin PPC Assembly Printer";
378 bool doFinalization(Module &M);
379 void EmitStartOfAsmFile(Module &M);
381 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
383 } // end of anonymous namespace
385 // Include the auto-generated portion of the assembly writer
386 #include "PPCGenAsmWriter.inc"
388 void PPCAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
389 switch (MO.getType()) {
390 case MachineOperand::MO_Immediate:
391 llvm_unreachable("printOp() does not handle immediate values");
393 case MachineOperand::MO_MachineBasicBlock:
394 O << *MO.getMBB()->getSymbol();
396 case MachineOperand::MO_JumpTableIndex:
397 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
398 << '_' << MO.getIndex();
399 // FIXME: PIC relocation model
401 case MachineOperand::MO_ConstantPoolIndex:
402 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
403 << '_' << MO.getIndex();
405 case MachineOperand::MO_BlockAddress:
406 O << *GetBlockAddressSymbol(MO.getBlockAddress());
408 case MachineOperand::MO_ExternalSymbol: {
409 // Computing the address of an external symbol, not calling it.
410 if (TM.getRelocationModel() == Reloc::Static) {
411 O << *GetExternalSymbolSymbol(MO.getSymbolName());
416 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
417 MO.getSymbolName()+"$non_lazy_ptr");
418 MachineModuleInfoImpl::StubValueTy &StubSym =
419 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
420 if (StubSym.getPointer() == 0)
421 StubSym = MachineModuleInfoImpl::
422 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
427 case MachineOperand::MO_GlobalAddress: {
428 // Computing the address of a global symbol, not calling it.
429 const GlobalValue *GV = MO.getGlobal();
430 MCSymbol *SymToPrint;
432 // External or weakly linked global variables need non-lazily-resolved stubs
433 if (TM.getRelocationModel() != Reloc::Static &&
434 (GV->isDeclaration() || GV->isWeakForLinker())) {
435 if (!GV->hasHiddenVisibility()) {
436 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
437 MachineModuleInfoImpl::StubValueTy &StubSym =
438 MMI->getObjFileInfo<MachineModuleInfoMachO>()
439 .getGVStubEntry(SymToPrint);
440 if (StubSym.getPointer() == 0)
441 StubSym = MachineModuleInfoImpl::
442 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
443 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
444 GV->hasAvailableExternallyLinkage()) {
445 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
447 MachineModuleInfoImpl::StubValueTy &StubSym =
448 MMI->getObjFileInfo<MachineModuleInfoMachO>().
449 getHiddenGVStubEntry(SymToPrint);
450 if (StubSym.getPointer() == 0)
451 StubSym = MachineModuleInfoImpl::
452 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
454 SymToPrint = Mang->getSymbol(GV);
457 SymToPrint = Mang->getSymbol(GV);
462 printOffset(MO.getOffset(), O);
467 O << "<unknown operand type: " << MO.getType() << ">";
472 /// PrintAsmOperand - Print out an operand for an inline asm expression.
474 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
476 const char *ExtraCode, raw_ostream &O) {
477 // Does this asm operand have a single letter operand modifier?
478 if (ExtraCode && ExtraCode[0]) {
479 if (ExtraCode[1] != 0) return true; // Unknown modifier.
481 switch (ExtraCode[0]) {
482 default: return true; // Unknown modifier.
483 case 'c': // Don't print "$" before a global var name or constant.
484 // PPC never has a prefix.
485 printOperand(MI, OpNo, O);
487 case 'L': // Write second word of DImode reference.
488 // Verify that this operand has two consecutive registers.
489 if (!MI->getOperand(OpNo).isReg() ||
490 OpNo+1 == MI->getNumOperands() ||
491 !MI->getOperand(OpNo+1).isReg())
493 ++OpNo; // Return the high-part.
496 // Write 'i' if an integer constant, otherwise nothing. Used to print
498 if (MI->getOperand(OpNo).isImm())
504 printOperand(MI, OpNo, O);
508 // At the moment, all inline asm memory operands are a single register.
509 // In any case, the output of this routine should always be just one
510 // assembler operand.
512 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
514 const char *ExtraCode,
516 if (ExtraCode && ExtraCode[0])
517 return true; // Unknown modifier.
518 assert (MI->getOperand(OpNo).isReg());
520 printOperand(MI, OpNo, O);
525 void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
526 raw_ostream &O, const char *Modifier){
527 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
528 unsigned Code = MI->getOperand(OpNo).getImm();
529 if (!strcmp(Modifier, "cc")) {
530 switch ((PPC::Predicate)Code) {
531 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
532 case PPC::PRED_LT: O << "lt"; return;
533 case PPC::PRED_LE: O << "le"; return;
534 case PPC::PRED_EQ: O << "eq"; return;
535 case PPC::PRED_GE: O << "ge"; return;
536 case PPC::PRED_GT: O << "gt"; return;
537 case PPC::PRED_NE: O << "ne"; return;
538 case PPC::PRED_UN: O << "un"; return;
539 case PPC::PRED_NU: O << "nu"; return;
543 assert(!strcmp(Modifier, "reg") &&
544 "Need to specify 'cc' or 'reg' as predicate op modifier!");
545 // Don't print the register for 'always'.
546 if (Code == PPC::PRED_ALWAYS) return;
547 printOperand(MI, OpNo+1, O);
551 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
552 /// the current output stream.
554 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
555 if (UseInstPrinter) {
558 // Lower multi-instruction pseudo operations.
559 switch (MI->getOpcode()) {
561 case PPC::MovePCtoLR:
562 case PPC::MovePCtoLR8: {
563 // Transform %LR = MovePCtoLR
564 // Into this, where the label is the PIC base:
567 MCSymbol *PICBase = MF->getPICBaseSymbol();
570 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here.
573 // FIXME: We would like an efficient form for this, so we don't have to do
574 // a lot of extra uniquing.
575 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::
576 Create(PICBase, OutContext)));
577 OutStreamer.EmitInstruction(TmpInst);
580 OutStreamer.EmitLabel(PICBase);
584 // Transform %X3 = LDtoc <ga:@min1>, %X2
585 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
587 // Change the opcode to LD, and the global address operand to be a
588 // reference to the TOC entry we will synthesize later.
589 TmpInst.setOpcode(PPC::LD);
590 const MachineOperand &MO = MI->getOperand(1);
591 assert(MO.isGlobal());
593 // Map symbol -> label of TOC entry.
594 MCSymbol *&TOCEntry = TOC[Mang->getSymbol(MO.getGlobal())];
596 TOCEntry = OutContext.
597 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
598 "C" + Twine(LabelID++));
602 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
604 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
605 OutStreamer.EmitInstruction(TmpInst);
610 // Transform: %R3 = MFCRpseud %CR7
611 // Into: %R3 = MFCR ;; cr7
612 OutStreamer.AddComment(getRegisterName(MI->getOperand(1).getReg()));
613 TmpInst.setOpcode(PPC::MFCR);
614 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
615 OutStreamer.EmitInstruction(TmpInst);
619 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
620 OutStreamer.EmitInstruction(TmpInst);
625 SmallString<128> Str;
626 raw_svector_ostream O(Str);
628 if (MI->getOpcode() == TargetOpcode::DBG_VALUE) {
629 unsigned NOps = MI->getNumOperands();
631 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
632 // cast away const; DIetc do not take const operands for some reason.
633 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
636 // Frame address. Currently handles register +- offset only.
637 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
638 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
641 printOperand(MI, NOps-2, O);
642 OutStreamer.EmitRawText(O.str());
645 // Check for slwi/srwi mnemonics.
646 if (MI->getOpcode() == PPC::RLWINM) {
647 unsigned char SH = MI->getOperand(2).getImm();
648 unsigned char MB = MI->getOperand(3).getImm();
649 unsigned char ME = MI->getOperand(4).getImm();
650 bool useSubstituteMnemonic = false;
651 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
652 O << "\tslwi "; useSubstituteMnemonic = true;
654 if (SH <= 31 && MB == (32-SH) && ME == 31) {
655 O << "\tsrwi "; useSubstituteMnemonic = true;
658 if (useSubstituteMnemonic) {
659 printOperand(MI, 0, O);
661 printOperand(MI, 1, O);
662 O << ", " << (unsigned int)SH;
663 OutStreamer.EmitRawText(O.str());
668 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
669 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
671 printOperand(MI, 0, O);
673 printOperand(MI, 1, O);
674 OutStreamer.EmitRawText(O.str());
678 if (MI->getOpcode() == PPC::RLDICR) {
679 unsigned char SH = MI->getOperand(2).getImm();
680 unsigned char ME = MI->getOperand(3).getImm();
681 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
684 printOperand(MI, 0, O);
686 printOperand(MI, 1, O);
687 O << ", " << (unsigned int)SH;
688 OutStreamer.EmitRawText(O.str());
693 printInstruction(MI, O);
694 OutStreamer.EmitRawText(O.str());
697 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
698 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
699 return AsmPrinter::EmitFunctionEntryLabel();
701 // Emit an official procedure descriptor.
702 // FIXME 64-bit SVR4: Use MCSection here!
703 OutStreamer.EmitRawText(StringRef("\t.section\t\".opd\",\"aw\""));
704 OutStreamer.EmitRawText(StringRef("\t.align 3"));
705 OutStreamer.EmitLabel(CurrentFnSym);
706 OutStreamer.EmitRawText("\t.quad .L." + Twine(CurrentFnSym->getName()) +
708 OutStreamer.EmitRawText(StringRef("\t.previous"));
709 OutStreamer.EmitRawText(".L." + Twine(CurrentFnSym->getName()) + ":");
713 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
714 const TargetData *TD = TM.getTargetData();
716 bool isPPC64 = TD->getPointerSizeInBits() == 64;
718 if (isPPC64 && !TOC.empty()) {
719 // FIXME 64-bit SVR4: Use MCSection here?
720 OutStreamer.EmitRawText(StringRef("\t.section\t\".toc\",\"aw\""));
722 // FIXME: This is nondeterminstic!
723 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
724 E = TOC.end(); I != E; ++I) {
725 OutStreamer.EmitLabel(I->second);
726 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
727 "[TC]," + I->first->getName());
731 return AsmPrinter::doFinalization(M);
734 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
735 static const char *const CPUDirectives[] = {
747 unsigned Directive = Subtarget.getDarwinDirective();
748 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
749 Directive = PPC::DIR_970;
750 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
751 Directive = PPC::DIR_7400;
752 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
753 Directive = PPC::DIR_64;
754 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
755 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
757 // Prime text sections so they are adjacent. This reduces the likelihood a
758 // large data or debug section causes a branch to exceed 16M limit.
759 const TargetLoweringObjectFileMachO &TLOFMacho =
760 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
761 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
762 if (TM.getRelocationModel() == Reloc::PIC_) {
763 OutStreamer.SwitchSection(
764 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
765 MCSectionMachO::S_SYMBOL_STUBS |
766 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
767 32, SectionKind::getText()));
768 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
769 OutStreamer.SwitchSection(
770 OutContext.getMachOSection("__TEXT","__symbol_stub1",
771 MCSectionMachO::S_SYMBOL_STUBS |
772 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
773 16, SectionKind::getText()));
775 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
778 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
779 // Remove $stub suffix, add $lazy_ptr.
780 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
781 TmpStr += "$lazy_ptr";
782 return Ctx.GetOrCreateSymbol(TmpStr.str());
785 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
786 // Add $tmp suffix to $stub, yielding $stub$tmp.
787 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
789 return Ctx.GetOrCreateSymbol(TmpStr.str());
792 void PPCDarwinAsmPrinter::
793 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
794 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
796 const TargetLoweringObjectFileMachO &TLOFMacho =
797 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
799 // .lazy_symbol_pointer
800 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
802 // Output stubs for dynamically-linked functions
803 if (TM.getRelocationModel() == Reloc::PIC_) {
804 const MCSection *StubSection =
805 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
806 MCSectionMachO::S_SYMBOL_STUBS |
807 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
808 32, SectionKind::getText());
809 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
810 OutStreamer.SwitchSection(StubSection);
813 MCSymbol *Stub = Stubs[i].first;
814 MCSymbol *RawSym = Stubs[i].second.getPointer();
815 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
816 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
818 OutStreamer.EmitLabel(Stub);
819 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
820 // FIXME: MCize this.
821 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
822 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
823 OutStreamer.EmitLabel(AnonSymbol);
824 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
825 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
826 "-" + AnonSymbol->getName() + ")");
827 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
830 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
831 "-" + AnonSymbol->getName() + ")(r11)");
833 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
834 "-" + AnonSymbol->getName() + ")(r11)");
835 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
836 OutStreamer.EmitRawText(StringRef("\tbctr"));
838 OutStreamer.SwitchSection(LSPSection);
839 OutStreamer.EmitLabel(LazyPtr);
840 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
843 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
845 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
847 OutStreamer.AddBlankLine();
851 const MCSection *StubSection =
852 OutContext.getMachOSection("__TEXT","__symbol_stub1",
853 MCSectionMachO::S_SYMBOL_STUBS |
854 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
855 16, SectionKind::getText());
856 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
857 MCSymbol *Stub = Stubs[i].first;
858 MCSymbol *RawSym = Stubs[i].second.getPointer();
859 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
861 OutStreamer.SwitchSection(StubSection);
863 OutStreamer.EmitLabel(Stub);
864 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
865 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
867 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
870 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
872 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
873 OutStreamer.EmitRawText(StringRef("\tbctr"));
874 OutStreamer.SwitchSection(LSPSection);
875 OutStreamer.EmitLabel(LazyPtr);
876 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
879 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
881 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
884 OutStreamer.AddBlankLine();
888 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
889 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
891 // Darwin/PPC always uses mach-o.
892 const TargetLoweringObjectFileMachO &TLOFMacho =
893 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
894 MachineModuleInfoMachO &MMIMacho =
895 MMI->getObjFileInfo<MachineModuleInfoMachO>();
897 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
899 EmitFunctionStubs(Stubs);
901 if (MAI->doesSupportExceptionHandling() && MMI) {
902 // Add the (possibly multiple) personalities to the set of global values.
903 // Only referenced functions get into the Personalities list.
904 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
905 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
906 E = Personalities.end(); I != E; ++I) {
908 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
909 MachineModuleInfoImpl::StubValueTy &StubSym =
910 MMIMacho.getGVStubEntry(NLPSym);
911 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
916 // Output stubs for dynamically-linked functions.
917 Stubs = MMIMacho.GetGVStubList();
919 // Output macho stubs for external and common global variables.
920 if (!Stubs.empty()) {
921 // Switch with ".non_lazy_symbol_pointer" directive.
922 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
923 EmitAlignment(isPPC64 ? 3 : 2);
925 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
927 OutStreamer.EmitLabel(Stubs[i].first);
928 // .indirect_symbol _foo
929 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
930 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
933 // External to current translation unit.
934 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
936 // Internal to current translation unit.
938 // When we place the LSDA into the TEXT section, the type info pointers
939 // need to be indirect and pc-rel. We accomplish this by using NLPs.
940 // However, sometimes the types are local to the file. So we need to
941 // fill in the value for the NLP in those cases.
942 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
944 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
948 OutStreamer.AddBlankLine();
951 Stubs = MMIMacho.GetHiddenGVStubList();
952 if (!Stubs.empty()) {
953 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
954 EmitAlignment(isPPC64 ? 3 : 2);
956 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
958 OutStreamer.EmitLabel(Stubs[i].first);
960 OutStreamer.EmitValue(MCSymbolRefExpr::
961 Create(Stubs[i].second.getPointer(),
963 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
967 OutStreamer.AddBlankLine();
970 // Funny Darwin hack: This flag tells the linker that no global symbols
971 // contain code that falls through to other global symbols (e.g. the obvious
972 // implementation of multiple entry points). If this doesn't occur, the
973 // linker can safely perform dead code stripping. Since LLVM never generates
974 // code that does this, it is always safe to set.
975 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
977 return AsmPrinter::doFinalization(M);
980 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
981 /// for a MachineFunction to the given output stream, in a format that the
982 /// Darwin assembler can deal with.
984 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
985 MCStreamer &Streamer) {
986 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
988 if (Subtarget->isDarwin())
989 return new PPCDarwinAsmPrinter(tm, Streamer);
990 return new PPCLinuxAsmPrinter(tm, Streamer);
993 static MCInstPrinter *createPPCMCInstPrinter(const Target &T,
994 unsigned SyntaxVariant,
995 const MCAsmInfo &MAI) {
996 return new PPCInstPrinter(MAI, SyntaxVariant);
1000 // Force static initialization.
1001 extern "C" void LLVMInitializePowerPCAsmPrinter() {
1002 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1003 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1005 TargetRegistry::RegisterMCInstPrinter(ThePPC32Target, createPPCMCInstPrinter);
1006 TargetRegistry::RegisterMCInstPrinter(ThePPC64Target, createPPCMCInstPrinter);