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 "InstPrinter/PPCInstPrinter.h"
22 #include "MCTargetDesc/PPCPredicates.h"
23 #include "PPCSubtarget.h"
24 #include "PPCTargetMachine.h"
25 #include "llvm/ADT/MapVector.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringExtras.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/DebugInfo.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/DerivedTypes.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/MC/MCAsmInfo.h"
40 #include "llvm/MC/MCContext.h"
41 #include "llvm/MC/MCExpr.h"
42 #include "llvm/MC/MCInst.h"
43 #include "llvm/MC/MCInstBuilder.h"
44 #include "llvm/MC/MCSectionELF.h"
45 #include "llvm/MC/MCSectionMachO.h"
46 #include "llvm/MC/MCStreamer.h"
47 #include "llvm/MC/MCSymbol.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/Debug.h"
50 #include "llvm/Support/ELF.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/MathExtras.h"
53 #include "llvm/Support/TargetRegistry.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/Mangler.h"
56 #include "llvm/Target/TargetInstrInfo.h"
57 #include "llvm/Target/TargetOptions.h"
58 #include "llvm/Target/TargetRegisterInfo.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";
76 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
78 virtual void EmitInstruction(const MachineInstr *MI);
80 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
82 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
83 unsigned AsmVariant, const char *ExtraCode,
85 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
86 unsigned AsmVariant, const char *ExtraCode,
89 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
90 MachineLocation Location;
91 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
92 // Frame address. Currently handles register +- offset only.
93 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
94 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
96 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
102 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
103 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
105 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
106 : PPCAsmPrinter(TM, Streamer) {}
108 virtual const char *getPassName() const {
109 return "Linux PPC Assembly Printer";
112 bool doFinalization(Module &M);
114 virtual void EmitFunctionEntryLabel();
116 void EmitFunctionBodyEnd();
119 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
121 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
123 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
124 : PPCAsmPrinter(TM, Streamer) {}
126 virtual const char *getPassName() const {
127 return "Darwin PPC Assembly Printer";
130 bool doFinalization(Module &M);
131 void EmitStartOfAsmFile(Module &M);
133 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
135 } // end of anonymous namespace
137 /// stripRegisterPrefix - This method strips the character prefix from a
138 /// register name so that only the number is left. Used by for linux asm.
139 static const char *stripRegisterPrefix(const char *RegName) {
140 switch (RegName[0]) {
143 case 'v': return RegName + 1;
144 case 'c': if (RegName[1] == 'r') return RegName + 2;
150 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
152 const MachineOperand &MO = MI->getOperand(OpNo);
154 switch (MO.getType()) {
155 case MachineOperand::MO_Register: {
156 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
157 // Linux assembler (Others?) does not take register mnemonics.
158 // FIXME - What about special registers used in mfspr/mtspr?
159 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
163 case MachineOperand::MO_Immediate:
167 case MachineOperand::MO_MachineBasicBlock:
168 O << *MO.getMBB()->getSymbol();
170 case MachineOperand::MO_JumpTableIndex:
171 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
172 << '_' << MO.getIndex();
173 // FIXME: PIC relocation model
175 case MachineOperand::MO_ConstantPoolIndex:
176 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
177 << '_' << MO.getIndex();
179 case MachineOperand::MO_BlockAddress:
180 O << *GetBlockAddressSymbol(MO.getBlockAddress());
182 case MachineOperand::MO_ExternalSymbol: {
183 // Computing the address of an external symbol, not calling it.
184 if (TM.getRelocationModel() == Reloc::Static) {
185 O << *GetExternalSymbolSymbol(MO.getSymbolName());
190 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
191 MO.getSymbolName()+"$non_lazy_ptr");
192 MachineModuleInfoImpl::StubValueTy &StubSym =
193 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
194 if (StubSym.getPointer() == 0)
195 StubSym = MachineModuleInfoImpl::
196 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
201 case MachineOperand::MO_GlobalAddress: {
202 // Computing the address of a global symbol, not calling it.
203 const GlobalValue *GV = MO.getGlobal();
204 MCSymbol *SymToPrint;
206 // External or weakly linked global variables need non-lazily-resolved stubs
207 if (TM.getRelocationModel() != Reloc::Static &&
208 (GV->isDeclaration() || GV->isWeakForLinker())) {
209 if (!GV->hasHiddenVisibility()) {
210 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
211 MachineModuleInfoImpl::StubValueTy &StubSym =
212 MMI->getObjFileInfo<MachineModuleInfoMachO>()
213 .getGVStubEntry(SymToPrint);
214 if (StubSym.getPointer() == 0)
215 StubSym = MachineModuleInfoImpl::
216 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
217 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
218 GV->hasAvailableExternallyLinkage()) {
219 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
221 MachineModuleInfoImpl::StubValueTy &StubSym =
222 MMI->getObjFileInfo<MachineModuleInfoMachO>().
223 getHiddenGVStubEntry(SymToPrint);
224 if (StubSym.getPointer() == 0)
225 StubSym = MachineModuleInfoImpl::
226 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
228 SymToPrint = Mang->getSymbol(GV);
231 SymToPrint = Mang->getSymbol(GV);
236 printOffset(MO.getOffset(), O);
241 O << "<unknown operand type: " << MO.getType() << ">";
246 /// PrintAsmOperand - Print out an operand for an inline asm expression.
248 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
250 const char *ExtraCode, raw_ostream &O) {
251 // Does this asm operand have a single letter operand modifier?
252 if (ExtraCode && ExtraCode[0]) {
253 if (ExtraCode[1] != 0) return true; // Unknown modifier.
255 switch (ExtraCode[0]) {
257 // See if this is a generic print operand
258 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
259 case 'c': // Don't print "$" before a global var name or constant.
260 break; // PPC never has a prefix.
261 case 'L': // Write second word of DImode reference.
262 // Verify that this operand has two consecutive registers.
263 if (!MI->getOperand(OpNo).isReg() ||
264 OpNo+1 == MI->getNumOperands() ||
265 !MI->getOperand(OpNo+1).isReg())
267 ++OpNo; // Return the high-part.
270 // Write 'i' if an integer constant, otherwise nothing. Used to print
272 if (MI->getOperand(OpNo).isImm())
278 printOperand(MI, OpNo, O);
282 // At the moment, all inline asm memory operands are a single register.
283 // In any case, the output of this routine should always be just one
284 // assembler operand.
286 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
288 const char *ExtraCode,
290 if (ExtraCode && ExtraCode[0]) {
291 if (ExtraCode[1] != 0) return true; // Unknown modifier.
293 switch (ExtraCode[0]) {
294 default: return true; // Unknown modifier.
295 case 'y': // A memory reference for an X-form instruction
297 const char *RegName = "r0";
298 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
299 O << RegName << ", ";
300 printOperand(MI, OpNo, O);
306 assert(MI->getOperand(OpNo).isReg());
308 printOperand(MI, OpNo, O);
314 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
315 /// exists for it. If not, create one. Then return a symbol that references
317 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
319 MCSymbol *&TOCEntry = TOC[Sym];
321 // To avoid name clash check if the name already exists.
322 while (TOCEntry == 0) {
323 if (OutContext.LookupSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
324 "C" + Twine(TOCLabelID++)) == 0) {
325 TOCEntry = GetTempSymbol("C", TOCLabelID);
333 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
334 /// the current output stream.
336 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
339 // Lower multi-instruction pseudo operations.
340 switch (MI->getOpcode()) {
342 case TargetOpcode::DBG_VALUE: {
343 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
346 raw_svector_ostream O(Str);
347 unsigned NOps = MI->getNumOperands();
349 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
350 // cast away const; DIetc do not take const operands for some reason.
351 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
354 // Frame address. Currently handles register +- offset only.
355 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
356 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
359 printOperand(MI, NOps-2, O);
360 OutStreamer.EmitRawText(O.str());
364 case PPC::MovePCtoLR:
365 case PPC::MovePCtoLR8: {
366 // Transform %LR = MovePCtoLR
367 // Into this, where the label is the PIC base:
370 MCSymbol *PICBase = MF->getPICBaseSymbol();
373 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL)
374 // FIXME: We would like an efficient form for this, so we don't have to do
375 // a lot of extra uniquing.
376 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
379 OutStreamer.EmitLabel(PICBase);
385 // Transform %X3 = LDtoc <ga:@min1>, %X2
386 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
388 // Change the opcode to LD, and the global address operand to be a
389 // reference to the TOC entry we will synthesize later.
390 TmpInst.setOpcode(PPC::LD);
391 const MachineOperand &MO = MI->getOperand(1);
393 // Map symbol -> label of TOC entry
394 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
395 MCSymbol *MOSymbol = 0;
397 MOSymbol = Mang->getSymbol(MO.getGlobal());
399 MOSymbol = GetCPISymbol(MO.getIndex());
401 MOSymbol = GetJTISymbol(MO.getIndex());
403 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
406 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
408 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
409 OutStreamer.EmitInstruction(TmpInst);
413 case PPC::ADDIStocHA: {
414 // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
415 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
417 // Change the opcode to ADDIS8. If the global address is external,
418 // has common linkage, is a function address, or is a jump table
419 // address, then generate a TOC entry and reference that. Otherwise
420 // reference the symbol directly.
421 TmpInst.setOpcode(PPC::ADDIS8);
422 const MachineOperand &MO = MI->getOperand(2);
423 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI()) &&
424 "Invalid operand for ADDIStocHA!");
425 MCSymbol *MOSymbol = 0;
426 bool IsExternal = false;
427 bool IsFunction = false;
428 bool IsCommon = false;
429 bool IsAvailExt = false;
432 const GlobalValue *GValue = MO.getGlobal();
433 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
434 const GlobalValue *RealGValue = GAlias ?
435 GAlias->resolveAliasedGlobal(false) : GValue;
436 MOSymbol = Mang->getSymbol(RealGValue);
437 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
438 IsExternal = GVar && !GVar->hasInitializer();
439 IsCommon = GVar && RealGValue->hasCommonLinkage();
441 IsAvailExt = GVar && RealGValue->hasAvailableExternallyLinkage();
442 } else if (MO.isCPI())
443 MOSymbol = GetCPISymbol(MO.getIndex());
445 MOSymbol = GetJTISymbol(MO.getIndex());
447 if (IsExternal || IsFunction || IsCommon || IsAvailExt || MO.isJTI())
448 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
451 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_HA,
453 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
454 OutStreamer.EmitInstruction(TmpInst);
458 // Transform %Xd = LDtocL <ga:@sym>, %Xs
459 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
461 // Change the opcode to LD. If the global address is external, has
462 // common linkage, or is a jump table address, then reference the
463 // associated TOC entry. Otherwise reference the symbol directly.
464 TmpInst.setOpcode(PPC::LD);
465 const MachineOperand &MO = MI->getOperand(1);
466 assert((MO.isGlobal() || MO.isJTI() || MO.isCPI()) &&
467 "Invalid operand for LDtocL!");
468 MCSymbol *MOSymbol = 0;
471 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
473 MOSymbol = GetCPISymbol(MO.getIndex());
474 else if (MO.isGlobal()) {
475 const GlobalValue *GValue = MO.getGlobal();
476 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
477 const GlobalValue *RealGValue = GAlias ?
478 GAlias->resolveAliasedGlobal(false) : GValue;
479 MOSymbol = Mang->getSymbol(RealGValue);
480 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
482 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
483 RealGValue->hasAvailableExternallyLinkage())
484 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
488 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
490 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
491 OutStreamer.EmitInstruction(TmpInst);
494 case PPC::ADDItocL: {
495 // Transform %Xd = ADDItocL %Xs, <ga:@sym>
496 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
498 // Change the opcode to ADDI8. If the global address is external, then
499 // generate a TOC entry and reference that. Otherwise reference the
501 TmpInst.setOpcode(PPC::ADDI8);
502 const MachineOperand &MO = MI->getOperand(2);
503 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
504 MCSymbol *MOSymbol = 0;
505 bool IsExternal = false;
506 bool IsFunction = false;
509 const GlobalValue *GValue = MO.getGlobal();
510 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
511 const GlobalValue *RealGValue = GAlias ?
512 GAlias->resolveAliasedGlobal(false) : GValue;
513 MOSymbol = Mang->getSymbol(RealGValue);
514 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
515 IsExternal = GVar && !GVar->hasInitializer();
517 } else if (MO.isCPI())
518 MOSymbol = GetCPISymbol(MO.getIndex());
520 if (IsFunction || IsExternal)
521 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
524 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
526 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
527 OutStreamer.EmitInstruction(TmpInst);
530 case PPC::ADDISgotTprelHA: {
531 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym>
532 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
533 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
534 const MachineOperand &MO = MI->getOperand(2);
535 const GlobalValue *GValue = MO.getGlobal();
536 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
537 const MCExpr *SymGotTprel =
538 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA,
540 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
541 .addReg(MI->getOperand(0).getReg())
543 .addExpr(SymGotTprel));
546 case PPC::LDgotTprelL: {
547 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
548 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
550 // Change the opcode to LD.
551 TmpInst.setOpcode(PPC::LD);
552 const MachineOperand &MO = MI->getOperand(1);
553 const GlobalValue *GValue = MO.getGlobal();
554 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
556 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO,
558 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
559 OutStreamer.EmitInstruction(TmpInst);
562 case PPC::ADDIStlsgdHA: {
563 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym>
564 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
565 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
566 const MachineOperand &MO = MI->getOperand(2);
567 const GlobalValue *GValue = MO.getGlobal();
568 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
569 const MCExpr *SymGotTlsGD =
570 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA,
572 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
573 .addReg(MI->getOperand(0).getReg())
575 .addExpr(SymGotTlsGD));
578 case PPC::ADDItlsgdL: {
579 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym>
580 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l
581 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
582 const MachineOperand &MO = MI->getOperand(2);
583 const GlobalValue *GValue = MO.getGlobal();
584 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
585 const MCExpr *SymGotTlsGD =
586 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO,
588 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
589 .addReg(MI->getOperand(0).getReg())
590 .addReg(MI->getOperand(1).getReg())
591 .addExpr(SymGotTlsGD));
594 case PPC::GETtlsADDR: {
595 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym>
596 // Into: BL8_NOP_TLSGD __tls_get_addr(sym@tlsgd)
597 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
599 StringRef Name = "__tls_get_addr";
600 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
601 const MCSymbolRefExpr *TlsRef =
602 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
603 const MachineOperand &MO = MI->getOperand(2);
604 const GlobalValue *GValue = MO.getGlobal();
605 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
606 const MCExpr *SymVar =
607 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSGD,
609 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLSGD)
614 case PPC::ADDIStlsldHA: {
615 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym>
616 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha
617 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
618 const MachineOperand &MO = MI->getOperand(2);
619 const GlobalValue *GValue = MO.getGlobal();
620 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
621 const MCExpr *SymGotTlsLD =
622 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA,
624 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
625 .addReg(MI->getOperand(0).getReg())
627 .addExpr(SymGotTlsLD));
630 case PPC::ADDItlsldL: {
631 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym>
632 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l
633 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
634 const MachineOperand &MO = MI->getOperand(2);
635 const GlobalValue *GValue = MO.getGlobal();
636 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
637 const MCExpr *SymGotTlsLD =
638 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO,
640 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
641 .addReg(MI->getOperand(0).getReg())
642 .addReg(MI->getOperand(1).getReg())
643 .addExpr(SymGotTlsLD));
646 case PPC::GETtlsldADDR: {
647 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym>
648 // Into: BL8_NOP_TLSLD __tls_get_addr(sym@tlsld)
649 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
651 StringRef Name = "__tls_get_addr";
652 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
653 const MCSymbolRefExpr *TlsRef =
654 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
655 const MachineOperand &MO = MI->getOperand(2);
656 const GlobalValue *GValue = MO.getGlobal();
657 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
658 const MCExpr *SymVar =
659 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSLD,
661 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLSLD)
666 case PPC::ADDISdtprelHA: {
667 // Transform: %Xd = ADDISdtprelHA %X3, <ga:@sym>
668 // Into: %Xd = ADDIS8 %X3, sym@dtprel@ha
669 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
670 const MachineOperand &MO = MI->getOperand(2);
671 const GlobalValue *GValue = MO.getGlobal();
672 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
673 const MCExpr *SymDtprel =
674 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL16_HA,
676 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
677 .addReg(MI->getOperand(0).getReg())
679 .addExpr(SymDtprel));
682 case PPC::ADDIdtprelL: {
683 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym>
684 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l
685 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
686 const MachineOperand &MO = MI->getOperand(2);
687 const GlobalValue *GValue = MO.getGlobal();
688 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
689 const MCExpr *SymDtprel =
690 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL16_LO,
692 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
693 .addReg(MI->getOperand(0).getReg())
694 .addReg(MI->getOperand(1).getReg())
695 .addExpr(SymDtprel));
699 case PPC::MFCR8pseud:
700 // Transform: %R3 = MFCRpseud %CR7
701 // Into: %R3 = MFCR ;; cr7
702 OutStreamer.AddComment(PPCInstPrinter::
703 getRegisterName(MI->getOperand(1).getReg()));
704 OutStreamer.EmitInstruction(MCInstBuilder(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR)
705 .addReg(MI->getOperand(0).getReg()));
708 // In Book E sync is called msync, handle this special case here...
709 if (Subtarget.isBookE()) {
710 OutStreamer.EmitRawText(StringRef("\tmsync"));
715 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
716 OutStreamer.EmitInstruction(TmpInst);
719 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
720 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
721 return AsmPrinter::EmitFunctionEntryLabel();
723 // Emit an official procedure descriptor.
724 MCSectionSubPair Current = OutStreamer.getCurrentSection();
725 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
726 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
727 SectionKind::getReadOnly());
728 OutStreamer.SwitchSection(Section);
729 OutStreamer.EmitLabel(CurrentFnSym);
730 OutStreamer.EmitValueToAlignment(8);
732 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
733 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
735 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
737 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
738 // Generates a R_PPC64_TOC relocation for TOC base insertion.
739 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
740 MCSymbolRefExpr::VK_PPC_TOC, OutContext),
742 // Emit a null environment pointer.
743 OutStreamer.EmitIntValue(0, 8 /* size */);
744 OutStreamer.SwitchSection(Current.first, Current.second);
746 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
747 ".L." + Twine(CurrentFnSym->getName()));
748 OutStreamer.EmitLabel(RealFnSym);
749 CurrentFnSymForSize = RealFnSym;
753 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
754 const DataLayout *TD = TM.getDataLayout();
756 bool isPPC64 = TD->getPointerSizeInBits() == 64;
758 if (isPPC64 && !TOC.empty()) {
759 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
760 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
761 SectionKind::getReadOnly());
762 OutStreamer.SwitchSection(Section);
764 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
765 E = TOC.end(); I != E; ++I) {
766 OutStreamer.EmitLabel(I->second);
767 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
768 OutStreamer.EmitTCEntry(*S);
772 MachineModuleInfoELF &MMIELF =
773 MMI->getObjFileInfo<MachineModuleInfoELF>();
775 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
776 if (!Stubs.empty()) {
777 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
778 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
780 OutStreamer.EmitLabel(Stubs[i].first);
782 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Stubs[i].second.getPointer(),
784 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
788 OutStreamer.AddBlankLine();
791 return AsmPrinter::doFinalization(M);
794 /// EmitFunctionBodyEnd - Print the traceback table before the .size
797 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
798 // Only the 64-bit target requires a traceback table. For now,
799 // we only emit the word of zeroes that GDB requires to find
800 // the end of the function, and zeroes for the eight-byte
802 // FIXME: We should fill in the eight-byte mandatory fields as described in
803 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
804 // currently make use of these fields).
805 if (Subtarget.isPPC64()) {
806 OutStreamer.EmitIntValue(0, 4/*size*/);
807 OutStreamer.EmitIntValue(0, 8/*size*/);
811 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
812 static const char *const CPUDirectives[] = {
835 unsigned Directive = Subtarget.getDarwinDirective();
836 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
837 Directive = PPC::DIR_970;
838 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
839 Directive = PPC::DIR_7400;
840 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
841 Directive = PPC::DIR_64;
842 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
844 // FIXME: This is a total hack, finish mc'izing the PPC backend.
845 if (OutStreamer.hasRawTextSupport()) {
846 assert(Directive < sizeof(CPUDirectives) / sizeof(*CPUDirectives) &&
847 "CPUDirectives[] might not be up-to-date!");
848 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
851 // Prime text sections so they are adjacent. This reduces the likelihood a
852 // large data or debug section causes a branch to exceed 16M limit.
853 const TargetLoweringObjectFileMachO &TLOFMacho =
854 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
855 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
856 if (TM.getRelocationModel() == Reloc::PIC_) {
857 OutStreamer.SwitchSection(
858 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
859 MCSectionMachO::S_SYMBOL_STUBS |
860 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
861 32, SectionKind::getText()));
862 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
863 OutStreamer.SwitchSection(
864 OutContext.getMachOSection("__TEXT","__symbol_stub1",
865 MCSectionMachO::S_SYMBOL_STUBS |
866 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
867 16, SectionKind::getText()));
869 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
872 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
873 // Remove $stub suffix, add $lazy_ptr.
874 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
875 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
878 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
879 // Add $tmp suffix to $stub, yielding $stub$tmp.
880 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
883 void PPCDarwinAsmPrinter::
884 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
885 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
887 const TargetLoweringObjectFileMachO &TLOFMacho =
888 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
890 // .lazy_symbol_pointer
891 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
893 // Output stubs for dynamically-linked functions
894 if (TM.getRelocationModel() == Reloc::PIC_) {
895 const MCSection *StubSection =
896 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
897 MCSectionMachO::S_SYMBOL_STUBS |
898 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
899 32, SectionKind::getText());
900 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
901 OutStreamer.SwitchSection(StubSection);
904 MCSymbol *Stub = Stubs[i].first;
905 MCSymbol *RawSym = Stubs[i].second.getPointer();
906 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
907 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
909 OutStreamer.EmitLabel(Stub);
910 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
912 const MCExpr *Anon = MCSymbolRefExpr::Create(AnonSymbol, OutContext);
915 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
916 // bcl 20, 31, AnonSymbol
917 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCLalways).addExpr(Anon));
918 OutStreamer.EmitLabel(AnonSymbol);
920 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
921 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
923 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LazyPtr, OutContext),
925 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
930 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
932 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
933 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
934 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
936 .addExpr(Sub).addExpr(Sub)
939 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
941 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
943 OutStreamer.SwitchSection(LSPSection);
944 OutStreamer.EmitLabel(LazyPtr);
945 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
947 MCSymbol *DyldStubBindingHelper =
948 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
950 // .quad dyld_stub_binding_helper
951 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
953 // .long dyld_stub_binding_helper
954 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
957 OutStreamer.AddBlankLine();
961 const MCSection *StubSection =
962 OutContext.getMachOSection("__TEXT","__symbol_stub1",
963 MCSectionMachO::S_SYMBOL_STUBS |
964 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
965 16, SectionKind::getText());
966 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
967 MCSymbol *Stub = Stubs[i].first;
968 MCSymbol *RawSym = Stubs[i].second.getPointer();
969 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
971 OutStreamer.SwitchSection(StubSection);
973 OutStreamer.EmitLabel(Stub);
974 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
975 // lis r11, ha16(LazyPtr)
976 const MCExpr *LazyPtrHa16 =
977 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_HA16,
979 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
981 .addExpr(LazyPtrHa16));
983 const MCExpr *LazyPtrLo16 =
984 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_LO16,
986 // ldu r12, lo16(LazyPtr)(r11)
987 // lwzu r12, lo16(LazyPtr)(r11)
988 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
990 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
994 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
996 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
998 OutStreamer.SwitchSection(LSPSection);
999 OutStreamer.EmitLabel(LazyPtr);
1000 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
1002 MCSymbol *DyldStubBindingHelper =
1003 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
1005 // .quad dyld_stub_binding_helper
1006 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
1008 // .long dyld_stub_binding_helper
1009 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
1013 OutStreamer.AddBlankLine();
1017 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1018 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
1020 // Darwin/PPC always uses mach-o.
1021 const TargetLoweringObjectFileMachO &TLOFMacho =
1022 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1023 MachineModuleInfoMachO &MMIMacho =
1024 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1026 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
1028 EmitFunctionStubs(Stubs);
1030 if (MAI->doesSupportExceptionHandling() && MMI) {
1031 // Add the (possibly multiple) personalities to the set of global values.
1032 // Only referenced functions get into the Personalities list.
1033 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
1034 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
1035 E = Personalities.end(); I != E; ++I) {
1037 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
1038 MachineModuleInfoImpl::StubValueTy &StubSym =
1039 MMIMacho.getGVStubEntry(NLPSym);
1040 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
1045 // Output stubs for dynamically-linked functions.
1046 Stubs = MMIMacho.GetGVStubList();
1048 // Output macho stubs for external and common global variables.
1049 if (!Stubs.empty()) {
1050 // Switch with ".non_lazy_symbol_pointer" directive.
1051 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1052 EmitAlignment(isPPC64 ? 3 : 2);
1054 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1056 OutStreamer.EmitLabel(Stubs[i].first);
1057 // .indirect_symbol _foo
1058 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1059 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
1062 // External to current translation unit.
1063 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
1065 // Internal to current translation unit.
1067 // When we place the LSDA into the TEXT section, the type info pointers
1068 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1069 // However, sometimes the types are local to the file. So we need to
1070 // fill in the value for the NLP in those cases.
1071 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1073 isPPC64 ? 8 : 4/*size*/);
1077 OutStreamer.AddBlankLine();
1080 Stubs = MMIMacho.GetHiddenGVStubList();
1081 if (!Stubs.empty()) {
1082 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
1083 EmitAlignment(isPPC64 ? 3 : 2);
1085 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1087 OutStreamer.EmitLabel(Stubs[i].first);
1089 OutStreamer.EmitValue(MCSymbolRefExpr::
1090 Create(Stubs[i].second.getPointer(),
1092 isPPC64 ? 8 : 4/*size*/);
1096 OutStreamer.AddBlankLine();
1099 // Funny Darwin hack: This flag tells the linker that no global symbols
1100 // contain code that falls through to other global symbols (e.g. the obvious
1101 // implementation of multiple entry points). If this doesn't occur, the
1102 // linker can safely perform dead code stripping. Since LLVM never generates
1103 // code that does this, it is always safe to set.
1104 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1106 return AsmPrinter::doFinalization(M);
1109 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1110 /// for a MachineFunction to the given output stream, in a format that the
1111 /// Darwin assembler can deal with.
1113 static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
1114 MCStreamer &Streamer) {
1115 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1117 if (Subtarget->isDarwin())
1118 return new PPCDarwinAsmPrinter(tm, Streamer);
1119 return new PPCLinuxAsmPrinter(tm, Streamer);
1122 // Force static initialization.
1123 extern "C" void LLVMInitializePowerPCAsmPrinter() {
1124 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1125 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);