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 //===----------------------------------------------------------------------===//
20 #include "InstPrinter/PPCInstPrinter.h"
21 #include "MCTargetDesc/PPCMCExpr.h"
22 #include "MCTargetDesc/PPCPredicates.h"
23 #include "PPCMachineFunctionInfo.h"
24 #include "PPCSubtarget.h"
25 #include "PPCTargetMachine.h"
26 #include "PPCTargetStreamer.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/StackMaps.h"
37 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DebugInfo.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Mangler.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/MC/MCAsmInfo.h"
44 #include "llvm/MC/MCContext.h"
45 #include "llvm/MC/MCExpr.h"
46 #include "llvm/MC/MCInst.h"
47 #include "llvm/MC/MCInstBuilder.h"
48 #include "llvm/MC/MCSectionELF.h"
49 #include "llvm/MC/MCSectionMachO.h"
50 #include "llvm/MC/MCStreamer.h"
51 #include "llvm/MC/MCSymbolELF.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/Support/ELF.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/MathExtras.h"
56 #include "llvm/Support/TargetRegistry.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Target/TargetInstrInfo.h"
59 #include "llvm/Target/TargetOptions.h"
60 #include "llvm/Target/TargetRegisterInfo.h"
63 #define DEBUG_TYPE "asmprinter"
66 class PPCAsmPrinter : public AsmPrinter {
68 MapVector<MCSymbol *, MCSymbol *> TOC;
69 const PPCSubtarget *Subtarget;
73 explicit PPCAsmPrinter(TargetMachine &TM,
74 std::unique_ptr<MCStreamer> Streamer)
75 : AsmPrinter(TM, std::move(Streamer)), SM(*this) {}
77 const char *getPassName() const override {
78 return "PowerPC Assembly Printer";
81 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
83 virtual bool doInitialization(Module &M) override {
86 return AsmPrinter::doInitialization(M);
89 void EmitInstruction(const MachineInstr *MI) override;
91 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
93 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
94 unsigned AsmVariant, const char *ExtraCode,
95 raw_ostream &O) override;
96 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
97 unsigned AsmVariant, const char *ExtraCode,
98 raw_ostream &O) override;
100 void EmitEndOfAsmFile(Module &M) override;
102 void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI);
103 void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI);
104 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
105 bool runOnMachineFunction(MachineFunction &MF) override {
106 Subtarget = &MF.getSubtarget<PPCSubtarget>();
107 return AsmPrinter::runOnMachineFunction(MF);
111 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
112 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
114 explicit PPCLinuxAsmPrinter(TargetMachine &TM,
115 std::unique_ptr<MCStreamer> Streamer)
116 : PPCAsmPrinter(TM, std::move(Streamer)) {}
118 const char *getPassName() const override {
119 return "Linux PPC Assembly Printer";
122 bool doFinalization(Module &M) override;
123 void EmitStartOfAsmFile(Module &M) override;
125 void EmitFunctionEntryLabel() override;
127 void EmitFunctionBodyStart() override;
128 void EmitFunctionBodyEnd() override;
131 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
133 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
135 explicit PPCDarwinAsmPrinter(TargetMachine &TM,
136 std::unique_ptr<MCStreamer> Streamer)
137 : PPCAsmPrinter(TM, std::move(Streamer)) {}
139 const char *getPassName() const override {
140 return "Darwin PPC Assembly Printer";
143 bool doFinalization(Module &M) override;
144 void EmitStartOfAsmFile(Module &M) override;
146 } // end of anonymous namespace
148 /// stripRegisterPrefix - This method strips the character prefix from a
149 /// register name so that only the number is left. Used by for linux asm.
150 static const char *stripRegisterPrefix(const char *RegName) {
151 switch (RegName[0]) {
156 if (RegName[1] == 's')
159 case 'c': if (RegName[1] == 'r') return RegName + 2;
165 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
167 const DataLayout &DL = getDataLayout();
168 const MachineOperand &MO = MI->getOperand(OpNo);
170 switch (MO.getType()) {
171 case MachineOperand::MO_Register: {
172 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
173 // Linux assembler (Others?) does not take register mnemonics.
174 // FIXME - What about special registers used in mfspr/mtspr?
175 if (!Subtarget->isDarwin())
176 RegName = stripRegisterPrefix(RegName);
180 case MachineOperand::MO_Immediate:
184 case MachineOperand::MO_MachineBasicBlock:
185 MO.getMBB()->getSymbol()->print(O, MAI);
187 case MachineOperand::MO_ConstantPoolIndex:
188 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
191 case MachineOperand::MO_BlockAddress:
192 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
194 case MachineOperand::MO_GlobalAddress: {
195 // Computing the address of a global symbol, not calling it.
196 const GlobalValue *GV = MO.getGlobal();
197 MCSymbol *SymToPrint;
199 // External or weakly linked global variables need non-lazily-resolved stubs
200 if (Subtarget->hasLazyResolverStub(GV)) {
201 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
202 MachineModuleInfoImpl::StubValueTy &StubSym =
203 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(
205 if (!StubSym.getPointer())
206 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
207 !GV->hasInternalLinkage());
209 SymToPrint = getSymbol(GV);
212 SymToPrint->print(O, MAI);
214 printOffset(MO.getOffset(), O);
219 O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
224 /// PrintAsmOperand - Print out an operand for an inline asm expression.
226 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
228 const char *ExtraCode, raw_ostream &O) {
229 // Does this asm operand have a single letter operand modifier?
230 if (ExtraCode && ExtraCode[0]) {
231 if (ExtraCode[1] != 0) return true; // Unknown modifier.
233 switch (ExtraCode[0]) {
235 // See if this is a generic print operand
236 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
237 case 'c': // Don't print "$" before a global var name or constant.
238 break; // PPC never has a prefix.
239 case 'L': // Write second word of DImode reference.
240 // Verify that this operand has two consecutive registers.
241 if (!MI->getOperand(OpNo).isReg() ||
242 OpNo+1 == MI->getNumOperands() ||
243 !MI->getOperand(OpNo+1).isReg())
245 ++OpNo; // Return the high-part.
248 // Write 'i' if an integer constant, otherwise nothing. Used to print
250 if (MI->getOperand(OpNo).isImm())
256 printOperand(MI, OpNo, O);
260 // At the moment, all inline asm memory operands are a single register.
261 // In any case, the output of this routine should always be just one
262 // assembler operand.
264 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
266 const char *ExtraCode,
268 if (ExtraCode && ExtraCode[0]) {
269 if (ExtraCode[1] != 0) return true; // Unknown modifier.
271 switch (ExtraCode[0]) {
272 default: return true; // Unknown modifier.
273 case 'y': // A memory reference for an X-form instruction
275 const char *RegName = "r0";
276 if (!Subtarget->isDarwin())
277 RegName = stripRegisterPrefix(RegName);
278 O << RegName << ", ";
279 printOperand(MI, OpNo, O);
282 case 'U': // Print 'u' for update form.
283 case 'X': // Print 'x' for indexed form.
285 // FIXME: Currently for PowerPC memory operands are always loaded
286 // into a register, so we never get an update or indexed form.
287 // This is bad even for offset forms, since even if we know we
288 // have a value in -16(r1), we will generate a load into r<n>
289 // and then load from 0(r<n>). Until that issue is fixed,
290 // tolerate 'U' and 'X' but don't output anything.
291 assert(MI->getOperand(OpNo).isReg());
297 assert(MI->getOperand(OpNo).isReg());
299 printOperand(MI, OpNo, O);
304 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
305 /// exists for it. If not, create one. Then return a symbol that references
307 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
308 MCSymbol *&TOCEntry = TOC[Sym];
310 TOCEntry = createTempSymbol("C");
314 void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) {
315 SM.serializeToStackMapSection();
318 void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
319 unsigned NumNOPBytes = MI.getOperand(1).getImm();
321 SM.recordStackMap(MI);
322 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
324 // Scan ahead to trim the shadow.
325 const MachineBasicBlock &MBB = *MI.getParent();
326 MachineBasicBlock::const_iterator MII(MI);
328 while (NumNOPBytes > 0) {
329 if (MII == MBB.end() || MII->isCall() ||
330 MII->getOpcode() == PPC::DBG_VALUE ||
331 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
332 MII->getOpcode() == TargetOpcode::STACKMAP)
339 for (unsigned i = 0; i < NumNOPBytes; i += 4)
340 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
343 // Lower a patchpoint of the form:
344 // [<def>], <id>, <numBytes>, <target>, <numArgs>
345 void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
346 SM.recordPatchPoint(MI);
347 PatchPointOpers Opers(&MI);
349 unsigned EncodedBytes = 0;
350 const MachineOperand &CalleeMO =
351 Opers.getMetaOper(PatchPointOpers::TargetPos);
353 if (CalleeMO.isImm()) {
354 int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm();
356 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
357 "High 16 bits of call target should be zero.");
358 unsigned ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
360 // Materialize the jump address:
361 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
363 .addImm((CallTarget >> 32) & 0xFFFF));
365 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
368 .addImm(32).addImm(16));
370 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
373 .addImm((CallTarget >> 16) & 0xFFFF));
375 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
378 .addImm(CallTarget & 0xFFFF));
380 // Save the current TOC pointer before the remote call.
381 int TOCSaveOffset = Subtarget->isELFv2ABI() ? 24 : 40;
382 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
384 .addImm(TOCSaveOffset)
388 // If we're on ELFv1, then we need to load the actual function pointer
389 // from the function descriptor.
390 if (!Subtarget->isELFv2ABI()) {
391 // Load the new TOC pointer and the function address, but not r11
392 // (needing this is rare, and loading it here would prevent passing it
393 // via a 'nest' parameter.
394 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
397 .addReg(ScratchReg));
399 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
402 .addReg(ScratchReg));
406 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
407 .addReg(ScratchReg));
409 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
412 // Restore the TOC pointer after the call.
413 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
415 .addImm(TOCSaveOffset)
419 } else if (CalleeMO.isGlobal()) {
420 const GlobalValue *GValue = CalleeMO.getGlobal();
421 MCSymbol *MOSymbol = getSymbol(GValue);
422 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
424 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
429 // Each instruction is 4 bytes.
433 unsigned NumBytes = Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
434 assert(NumBytes >= EncodedBytes &&
435 "Patchpoint can't request size less than the length of a call.");
436 assert((NumBytes - EncodedBytes) % 4 == 0 &&
437 "Invalid number of NOP bytes requested!");
438 for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
439 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
442 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
443 /// call to __tls_get_addr to the current output stream.
444 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
445 MCSymbolRefExpr::VariantKind VK) {
446 StringRef Name = "__tls_get_addr";
447 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol(Name);
448 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
450 assert(MI->getOperand(0).isReg() &&
451 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
452 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
453 "GETtls[ld]ADDR[32] must define GPR3");
454 assert(MI->getOperand(1).isReg() &&
455 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
456 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
457 "GETtls[ld]ADDR[32] must read GPR3");
459 if (!Subtarget->isPPC64() && !Subtarget->isDarwin() &&
460 isPositionIndependent())
461 Kind = MCSymbolRefExpr::VK_PLT;
462 const MCSymbolRefExpr *TlsRef =
463 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
464 const MachineOperand &MO = MI->getOperand(2);
465 const GlobalValue *GValue = MO.getGlobal();
466 MCSymbol *MOSymbol = getSymbol(GValue);
467 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
468 EmitToStreamer(*OutStreamer,
469 MCInstBuilder(Subtarget->isPPC64() ?
470 PPC::BL8_NOP_TLS : PPC::BL_TLS)
475 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
476 /// the current output stream.
478 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
480 bool isPPC64 = Subtarget->isPPC64();
481 bool isDarwin = TM.getTargetTriple().isOSDarwin();
482 const Module *M = MF->getFunction()->getParent();
483 PICLevel::Level PL = M->getPICLevel();
485 // Lower multi-instruction pseudo operations.
486 switch (MI->getOpcode()) {
488 case TargetOpcode::DBG_VALUE:
489 llvm_unreachable("Should be handled target independently");
490 case TargetOpcode::STACKMAP:
491 return LowerSTACKMAP(SM, *MI);
492 case TargetOpcode::PATCHPOINT:
493 return LowerPATCHPOINT(SM, *MI);
495 case PPC::MoveGOTtoLR: {
496 // Transform %LR = MoveGOTtoLR
497 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
498 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
499 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
501 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
502 MCSymbol *GOTSymbol =
503 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
504 const MCExpr *OffsExpr =
505 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol,
506 MCSymbolRefExpr::VK_PPC_LOCAL,
508 MCConstantExpr::create(4, OutContext),
512 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
515 case PPC::MovePCtoLR:
516 case PPC::MovePCtoLR8: {
517 // Transform %LR = MovePCtoLR
518 // Into this, where the label is the PIC base:
521 MCSymbol *PICBase = MF->getPICBaseSymbol();
524 EmitToStreamer(*OutStreamer,
525 MCInstBuilder(PPC::BL)
526 // FIXME: We would like an efficient form for this, so we
527 // don't have to do a lot of extra uniquing.
528 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
531 OutStreamer->EmitLabel(PICBase);
534 case PPC::UpdateGBR: {
535 // Transform %Rd = UpdateGBR(%Rt, %Ri)
536 // Into: lwz %Rt, .L0$poff - .L0$pb(%Ri)
538 // Get the offset from the GOT Base Register to the GOT
539 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
540 MCSymbol *PICOffset =
541 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol();
542 TmpInst.setOpcode(PPC::LWZ);
544 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
546 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
547 MCSymbolRefExpr::VK_None,
549 const MCOperand TR = TmpInst.getOperand(1);
550 const MCOperand PICR = TmpInst.getOperand(0);
552 // Step 1: lwz %Rt, .L$poff - .L$pb(%Ri)
553 TmpInst.getOperand(1) =
554 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
555 TmpInst.getOperand(0) = TR;
556 TmpInst.getOperand(2) = PICR;
557 EmitToStreamer(*OutStreamer, TmpInst);
559 TmpInst.setOpcode(PPC::ADD4);
560 TmpInst.getOperand(0) = PICR;
561 TmpInst.getOperand(1) = TR;
562 TmpInst.getOperand(2) = PICR;
563 EmitToStreamer(*OutStreamer, TmpInst);
567 // Transform %R3 = LWZtoc <ga:@min1>, %R2
568 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
570 // Change the opcode to LWZ, and the global address operand to be a
571 // reference to the GOT entry we will synthesize later.
572 TmpInst.setOpcode(PPC::LWZ);
573 const MachineOperand &MO = MI->getOperand(1);
575 // Map symbol -> label of TOC entry
576 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
577 MCSymbol *MOSymbol = nullptr;
579 MOSymbol = getSymbol(MO.getGlobal());
581 MOSymbol = GetCPISymbol(MO.getIndex());
583 MOSymbol = GetJTISymbol(MO.getIndex());
584 else if (MO.isBlockAddress())
585 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
587 if (PL == PICLevel::SmallPIC) {
589 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT,
591 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
593 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
596 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None,
599 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
601 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
602 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
604 EmitToStreamer(*OutStreamer, TmpInst);
611 // Transform %X3 = LDtoc <ga:@min1>, %X2
612 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
614 // Change the opcode to LD, and the global address operand to be a
615 // reference to the TOC entry we will synthesize later.
616 TmpInst.setOpcode(PPC::LD);
617 const MachineOperand &MO = MI->getOperand(1);
619 // Map symbol -> label of TOC entry
620 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
621 MCSymbol *MOSymbol = nullptr;
623 MOSymbol = getSymbol(MO.getGlobal());
625 MOSymbol = GetCPISymbol(MO.getIndex());
627 MOSymbol = GetJTISymbol(MO.getIndex());
628 else if (MO.isBlockAddress())
629 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
631 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
634 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
636 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
637 EmitToStreamer(*OutStreamer, TmpInst);
641 case PPC::ADDIStocHA: {
642 // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
643 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
645 // Change the opcode to ADDIS8. If the global address is external, has
646 // common linkage, is a non-local function address, or is a jump table
647 // address, then generate a TOC entry and reference that. Otherwise
648 // reference the symbol directly.
649 TmpInst.setOpcode(PPC::ADDIS8);
650 const MachineOperand &MO = MI->getOperand(2);
651 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
652 MO.isBlockAddress()) &&
653 "Invalid operand for ADDIStocHA!");
654 MCSymbol *MOSymbol = nullptr;
655 bool GlobalToc = false;
658 const GlobalValue *GV = MO.getGlobal();
659 MOSymbol = getSymbol(GV);
660 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
661 GlobalToc = (GVFlags & PPCII::MO_NLP_FLAG);
662 } else if (MO.isCPI()) {
663 MOSymbol = GetCPISymbol(MO.getIndex());
664 } else if (MO.isJTI()) {
665 MOSymbol = GetJTISymbol(MO.getIndex());
666 } else if (MO.isBlockAddress()) {
667 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
670 if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
671 TM.getCodeModel() == CodeModel::Large)
672 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
675 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA,
677 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
678 EmitToStreamer(*OutStreamer, TmpInst);
682 // Transform %Xd = LDtocL <ga:@sym>, %Xs
683 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
685 // Change the opcode to LD. If the global address is external, has
686 // common linkage, or is a jump table address, then reference the
687 // associated TOC entry. Otherwise reference the symbol directly.
688 TmpInst.setOpcode(PPC::LD);
689 const MachineOperand &MO = MI->getOperand(1);
690 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
691 MO.isBlockAddress()) &&
692 "Invalid operand for LDtocL!");
693 MCSymbol *MOSymbol = nullptr;
696 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
697 else if (MO.isBlockAddress()) {
698 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
699 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
701 else if (MO.isCPI()) {
702 MOSymbol = GetCPISymbol(MO.getIndex());
703 if (TM.getCodeModel() == CodeModel::Large)
704 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
706 else if (MO.isGlobal()) {
707 const GlobalValue *GV = MO.getGlobal();
708 MOSymbol = getSymbol(GV);
710 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
711 assert((GVFlags & PPCII::MO_NLP_FLAG) &&
712 "LDtocL used on symbol that could be accessed directly is "
713 "invalid. Must match ADDIStocHA."));
714 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
718 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
720 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
721 EmitToStreamer(*OutStreamer, TmpInst);
724 case PPC::ADDItocL: {
725 // Transform %Xd = ADDItocL %Xs, <ga:@sym>
726 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
728 // Change the opcode to ADDI8. If the global address is external, then
729 // generate a TOC entry and reference that. Otherwise reference the
731 TmpInst.setOpcode(PPC::ADDI8);
732 const MachineOperand &MO = MI->getOperand(2);
733 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
734 MCSymbol *MOSymbol = nullptr;
737 const GlobalValue *GV = MO.getGlobal();
739 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
741 !(GVFlags & PPCII::MO_NLP_FLAG) &&
742 "Interposable definitions must use indirect access."));
743 MOSymbol = getSymbol(GV);
744 } else if (MO.isCPI()) {
745 MOSymbol = GetCPISymbol(MO.getIndex());
749 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
751 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
752 EmitToStreamer(*OutStreamer, TmpInst);
755 case PPC::ADDISgotTprelHA: {
756 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym>
757 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
758 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
759 const MachineOperand &MO = MI->getOperand(2);
760 const GlobalValue *GValue = MO.getGlobal();
761 MCSymbol *MOSymbol = getSymbol(GValue);
762 const MCExpr *SymGotTprel =
763 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA,
765 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
766 .addReg(MI->getOperand(0).getReg())
767 .addReg(MI->getOperand(1).getReg())
768 .addExpr(SymGotTprel));
771 case PPC::LDgotTprelL:
772 case PPC::LDgotTprelL32: {
773 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
774 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
776 // Change the opcode to LD.
777 TmpInst.setOpcode(isPPC64 ? PPC::LD : PPC::LWZ);
778 const MachineOperand &MO = MI->getOperand(1);
779 const GlobalValue *GValue = MO.getGlobal();
780 MCSymbol *MOSymbol = getSymbol(GValue);
782 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO,
784 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
785 EmitToStreamer(*OutStreamer, TmpInst);
789 case PPC::PPC32PICGOT: {
790 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
791 MCSymbol *GOTRef = OutContext.createTempSymbol();
792 MCSymbol *NextInstr = OutContext.createTempSymbol();
794 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
795 // FIXME: We would like an efficient form for this, so we don't have to do
796 // a lot of extra uniquing.
797 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
798 const MCExpr *OffsExpr =
799 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
800 MCSymbolRefExpr::create(GOTRef, OutContext),
802 OutStreamer->EmitLabel(GOTRef);
803 OutStreamer->EmitValue(OffsExpr, 4);
804 OutStreamer->EmitLabel(NextInstr);
805 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
806 .addReg(MI->getOperand(0).getReg()));
807 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
808 .addReg(MI->getOperand(1).getReg())
810 .addReg(MI->getOperand(0).getReg()));
811 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
812 .addReg(MI->getOperand(0).getReg())
813 .addReg(MI->getOperand(1).getReg())
814 .addReg(MI->getOperand(0).getReg()));
817 case PPC::PPC32GOT: {
818 MCSymbol *GOTSymbol =
819 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
820 const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
821 GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
822 const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
823 GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
824 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
825 .addReg(MI->getOperand(0).getReg())
826 .addExpr(SymGotTlsL));
827 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
828 .addReg(MI->getOperand(0).getReg())
829 .addReg(MI->getOperand(0).getReg())
830 .addExpr(SymGotTlsHA));
833 case PPC::ADDIStlsgdHA: {
834 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym>
835 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
836 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
837 const MachineOperand &MO = MI->getOperand(2);
838 const GlobalValue *GValue = MO.getGlobal();
839 MCSymbol *MOSymbol = getSymbol(GValue);
840 const MCExpr *SymGotTlsGD =
841 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA,
843 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
844 .addReg(MI->getOperand(0).getReg())
845 .addReg(MI->getOperand(1).getReg())
846 .addExpr(SymGotTlsGD));
849 case PPC::ADDItlsgdL:
850 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym>
851 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l
852 case PPC::ADDItlsgdL32: {
853 // Transform: %Rd = ADDItlsgdL32 %Rs, <ga:@sym>
854 // Into: %Rd = ADDI %Rs, sym@got@tlsgd
855 const MachineOperand &MO = MI->getOperand(2);
856 const GlobalValue *GValue = MO.getGlobal();
857 MCSymbol *MOSymbol = getSymbol(GValue);
858 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
859 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
860 : MCSymbolRefExpr::VK_PPC_GOT_TLSGD,
862 EmitToStreamer(*OutStreamer,
863 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
864 .addReg(MI->getOperand(0).getReg())
865 .addReg(MI->getOperand(1).getReg())
866 .addExpr(SymGotTlsGD));
869 case PPC::GETtlsADDR:
870 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym>
871 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
872 case PPC::GETtlsADDR32: {
873 // Transform: %R3 = GETtlsADDR32 %R3, <ga:@sym>
874 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
875 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
878 case PPC::ADDIStlsldHA: {
879 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym>
880 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha
881 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
882 const MachineOperand &MO = MI->getOperand(2);
883 const GlobalValue *GValue = MO.getGlobal();
884 MCSymbol *MOSymbol = getSymbol(GValue);
885 const MCExpr *SymGotTlsLD =
886 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA,
888 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
889 .addReg(MI->getOperand(0).getReg())
890 .addReg(MI->getOperand(1).getReg())
891 .addExpr(SymGotTlsLD));
894 case PPC::ADDItlsldL:
895 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym>
896 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l
897 case PPC::ADDItlsldL32: {
898 // Transform: %Rd = ADDItlsldL32 %Rs, <ga:@sym>
899 // Into: %Rd = ADDI %Rs, sym@got@tlsld
900 const MachineOperand &MO = MI->getOperand(2);
901 const GlobalValue *GValue = MO.getGlobal();
902 MCSymbol *MOSymbol = getSymbol(GValue);
903 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
904 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
905 : MCSymbolRefExpr::VK_PPC_GOT_TLSLD,
907 EmitToStreamer(*OutStreamer,
908 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
909 .addReg(MI->getOperand(0).getReg())
910 .addReg(MI->getOperand(1).getReg())
911 .addExpr(SymGotTlsLD));
914 case PPC::GETtlsldADDR:
915 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym>
916 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
917 case PPC::GETtlsldADDR32: {
918 // Transform: %R3 = GETtlsldADDR32 %R3, <ga:@sym>
919 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
920 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
923 case PPC::ADDISdtprelHA:
924 // Transform: %Xd = ADDISdtprelHA %Xs, <ga:@sym>
925 // Into: %Xd = ADDIS8 %Xs, sym@dtprel@ha
926 case PPC::ADDISdtprelHA32: {
927 // Transform: %Rd = ADDISdtprelHA32 %Rs, <ga:@sym>
928 // Into: %Rd = ADDIS %Rs, sym@dtprel@ha
929 const MachineOperand &MO = MI->getOperand(2);
930 const GlobalValue *GValue = MO.getGlobal();
931 MCSymbol *MOSymbol = getSymbol(GValue);
932 const MCExpr *SymDtprel =
933 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA,
937 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS)
938 .addReg(MI->getOperand(0).getReg())
939 .addReg(MI->getOperand(1).getReg())
940 .addExpr(SymDtprel));
943 case PPC::ADDIdtprelL:
944 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym>
945 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l
946 case PPC::ADDIdtprelL32: {
947 // Transform: %Rd = ADDIdtprelL32 %Rs, <ga:@sym>
948 // Into: %Rd = ADDI %Rs, sym@dtprel@l
949 const MachineOperand &MO = MI->getOperand(2);
950 const GlobalValue *GValue = MO.getGlobal();
951 MCSymbol *MOSymbol = getSymbol(GValue);
952 const MCExpr *SymDtprel =
953 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO,
955 EmitToStreamer(*OutStreamer,
956 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
957 .addReg(MI->getOperand(0).getReg())
958 .addReg(MI->getOperand(1).getReg())
959 .addExpr(SymDtprel));
964 if (!Subtarget->hasMFOCRF()) {
965 // Transform: %R3 = MFOCRF %CR7
966 // Into: %R3 = MFCR ;; cr7
968 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
969 OutStreamer->AddComment(PPCInstPrinter::
970 getRegisterName(MI->getOperand(1).getReg()));
971 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
972 .addReg(MI->getOperand(0).getReg()));
978 if (!Subtarget->hasMFOCRF()) {
979 // Transform: %CR7 = MTOCRF %R3
980 // Into: MTCRF mask, %R3 ;; cr7
982 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
983 unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
984 ->getEncodingValue(MI->getOperand(0).getReg());
985 OutStreamer->AddComment(PPCInstPrinter::
986 getRegisterName(MI->getOperand(0).getReg()));
987 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
989 .addReg(MI->getOperand(1).getReg()));
997 // Verify alignment is legal, so we don't create relocations
998 // that can't be supported.
999 // FIXME: This test is currently disabled for Darwin. The test
1000 // suite shows a handful of test cases that fail this check for
1001 // Darwin. Those need to be investigated before this sanity test
1002 // can be enabled for those subtargets.
1003 if (!Subtarget->isDarwin()) {
1004 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1005 const MachineOperand &MO = MI->getOperand(OpNum);
1006 if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4)
1007 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
1009 // Now process the instruction normally.
1014 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
1015 EmitToStreamer(*OutStreamer, TmpInst);
1018 void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) {
1019 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1020 PPCTargetStreamer *TS =
1021 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1024 TS->emitAbiVersion(2);
1027 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1028 !isPositionIndependent())
1029 return AsmPrinter::EmitStartOfAsmFile(M);
1031 if (M.getPICLevel() == PICLevel::SmallPIC)
1032 return AsmPrinter::EmitStartOfAsmFile(M);
1034 OutStreamer->SwitchSection(OutContext.getELFSection(
1035 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC));
1037 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1038 MCSymbol *CurrentPos = OutContext.createTempSymbol();
1040 OutStreamer->EmitLabel(CurrentPos);
1042 // The GOT pointer points to the middle of the GOT, in order to reference the
1043 // entire 64kB range. 0x8000 is the midpoint.
1044 const MCExpr *tocExpr =
1045 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
1046 MCConstantExpr::create(0x8000, OutContext),
1049 OutStreamer->EmitAssignment(TOCSym, tocExpr);
1051 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1054 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
1055 // linux/ppc32 - Normal entry label.
1056 if (!Subtarget->isPPC64() &&
1057 (!isPositionIndependent() ||
1058 MF->getFunction()->getParent()->getPICLevel() == PICLevel::SmallPIC))
1059 return AsmPrinter::EmitFunctionEntryLabel();
1061 if (!Subtarget->isPPC64()) {
1062 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1063 if (PPCFI->usesPICBase()) {
1064 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol();
1065 MCSymbol *PICBase = MF->getPICBaseSymbol();
1066 OutStreamer->EmitLabel(RelocSymbol);
1068 const MCExpr *OffsExpr =
1069 MCBinaryExpr::createSub(
1070 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1072 MCSymbolRefExpr::create(PICBase, OutContext),
1074 OutStreamer->EmitValue(OffsExpr, 4);
1075 OutStreamer->EmitLabel(CurrentFnSym);
1078 return AsmPrinter::EmitFunctionEntryLabel();
1081 // ELFv2 ABI - Normal entry label.
1082 if (Subtarget->isELFv2ABI()) {
1083 // In the Large code model, we allow arbitrary displacements between
1084 // the text section and its associated TOC section. We place the
1085 // full 8-byte offset to the TOC in memory immediatedly preceding
1086 // the function global entry point.
1087 if (TM.getCodeModel() == CodeModel::Large
1088 && !MF->getRegInfo().use_empty(PPC::X2)) {
1089 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1091 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1092 MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol();
1093 const MCExpr *TOCDeltaExpr =
1094 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1095 MCSymbolRefExpr::create(GlobalEPSymbol,
1099 OutStreamer->EmitLabel(PPCFI->getTOCOffsetSymbol());
1100 OutStreamer->EmitValue(TOCDeltaExpr, 8);
1102 return AsmPrinter::EmitFunctionEntryLabel();
1105 // Emit an official procedure descriptor.
1106 MCSectionSubPair Current = OutStreamer->getCurrentSection();
1107 MCSectionELF *Section = OutStreamer->getContext().getELFSection(
1108 ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1109 OutStreamer->SwitchSection(Section);
1110 OutStreamer->EmitLabel(CurrentFnSym);
1111 OutStreamer->EmitValueToAlignment(8);
1112 MCSymbol *Symbol1 = CurrentFnSymForSize;
1113 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
1115 OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
1117 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1118 // Generates a R_PPC64_TOC relocation for TOC base insertion.
1119 OutStreamer->EmitValue(
1120 MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext),
1122 // Emit a null environment pointer.
1123 OutStreamer->EmitIntValue(0, 8 /* size */);
1124 OutStreamer->SwitchSection(Current.first, Current.second);
1127 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
1128 const DataLayout &DL = getDataLayout();
1130 bool isPPC64 = DL.getPointerSizeInBits() == 64;
1132 PPCTargetStreamer &TS =
1133 static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
1136 MCSectionELF *Section;
1139 Section = OutStreamer->getContext().getELFSection(
1140 ".toc", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1142 Section = OutStreamer->getContext().getELFSection(
1143 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1144 OutStreamer->SwitchSection(Section);
1146 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
1147 E = TOC.end(); I != E; ++I) {
1148 OutStreamer->EmitLabel(I->second);
1149 MCSymbol *S = I->first;
1153 OutStreamer->EmitSymbolValue(S, 4);
1157 return AsmPrinter::doFinalization(M);
1160 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
1161 void PPCLinuxAsmPrinter::EmitFunctionBodyStart() {
1162 // In the ELFv2 ABI, in functions that use the TOC register, we need to
1163 // provide two entry points. The ABI guarantees that when calling the
1164 // local entry point, r2 is set up by the caller to contain the TOC base
1165 // for this function, and when calling the global entry point, r12 is set
1166 // up by the caller to hold the address of the global entry point. We
1167 // thus emit a prefix sequence along the following lines:
1171 // # global entry point
1172 // addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
1173 // addi r2,r2,(.TOC.-.Lfunc_gepNN)@l
1175 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1176 // # local entry point, followed by function body
1178 // For the Large code model, we create
1181 // .quad .TOC.-.Lfunc_gepNN # done by EmitFunctionEntryLabel
1184 // # global entry point
1185 // ld r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
1188 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1189 // # local entry point, followed by function body
1191 // This ensures we have r2 set up correctly while executing the function
1192 // body, no matter which entry point is called.
1193 if (Subtarget->isELFv2ABI()
1194 // Only do all that if the function uses r2 in the first place.
1195 && !MF->getRegInfo().use_empty(PPC::X2)) {
1196 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1198 MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol();
1199 OutStreamer->EmitLabel(GlobalEntryLabel);
1200 const MCSymbolRefExpr *GlobalEntryLabelExp =
1201 MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
1203 if (TM.getCodeModel() != CodeModel::Large) {
1204 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1205 const MCExpr *TOCDeltaExpr =
1206 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1207 GlobalEntryLabelExp, OutContext);
1209 const MCExpr *TOCDeltaHi =
1210 PPCMCExpr::createHa(TOCDeltaExpr, false, OutContext);
1211 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1214 .addExpr(TOCDeltaHi));
1216 const MCExpr *TOCDeltaLo =
1217 PPCMCExpr::createLo(TOCDeltaExpr, false, OutContext);
1218 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
1221 .addExpr(TOCDeltaLo));
1223 MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol();
1224 const MCExpr *TOCOffsetDeltaExpr =
1225 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
1226 GlobalEntryLabelExp, OutContext);
1228 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
1230 .addExpr(TOCOffsetDeltaExpr)
1232 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
1238 MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol();
1239 OutStreamer->EmitLabel(LocalEntryLabel);
1240 const MCSymbolRefExpr *LocalEntryLabelExp =
1241 MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
1242 const MCExpr *LocalOffsetExp =
1243 MCBinaryExpr::createSub(LocalEntryLabelExp,
1244 GlobalEntryLabelExp, OutContext);
1246 PPCTargetStreamer *TS =
1247 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1250 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
1254 /// EmitFunctionBodyEnd - Print the traceback table before the .size
1257 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
1258 // Only the 64-bit target requires a traceback table. For now,
1259 // we only emit the word of zeroes that GDB requires to find
1260 // the end of the function, and zeroes for the eight-byte
1261 // mandatory fields.
1262 // FIXME: We should fill in the eight-byte mandatory fields as described in
1263 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
1264 // currently make use of these fields).
1265 if (Subtarget->isPPC64()) {
1266 OutStreamer->EmitIntValue(0, 4/*size*/);
1267 OutStreamer->EmitIntValue(0, 8/*size*/);
1271 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
1272 static const char *const CPUDirectives[] = {
1292 // FIXME: why is power8 missing here?
1298 // Get the numerically largest directive.
1299 // FIXME: How should we merge darwin directives?
1300 unsigned Directive = PPC::DIR_NONE;
1301 for (const Function &F : M) {
1302 const PPCSubtarget &STI = TM.getSubtarget<PPCSubtarget>(F);
1303 unsigned FDir = STI.getDarwinDirective();
1304 Directive = Directive > FDir ? FDir : STI.getDarwinDirective();
1305 if (STI.hasMFOCRF() && Directive < PPC::DIR_970)
1306 Directive = PPC::DIR_970;
1307 if (STI.hasAltivec() && Directive < PPC::DIR_7400)
1308 Directive = PPC::DIR_7400;
1309 if (STI.isPPC64() && Directive < PPC::DIR_64)
1310 Directive = PPC::DIR_64;
1313 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
1315 assert(Directive < array_lengthof(CPUDirectives) &&
1316 "CPUDirectives[] might not be up-to-date!");
1317 PPCTargetStreamer &TStreamer =
1318 *static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1319 TStreamer.emitMachine(CPUDirectives[Directive]);
1321 // Prime text sections so they are adjacent. This reduces the likelihood a
1322 // large data or debug section causes a branch to exceed 16M limit.
1323 const TargetLoweringObjectFileMachO &TLOFMacho =
1324 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1325 OutStreamer->SwitchSection(TLOFMacho.getTextCoalSection());
1326 if (TM.getRelocationModel() == Reloc::PIC_) {
1327 OutStreamer->SwitchSection(
1328 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
1329 MachO::S_SYMBOL_STUBS |
1330 MachO::S_ATTR_PURE_INSTRUCTIONS,
1331 32, SectionKind::getText()));
1332 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
1333 OutStreamer->SwitchSection(
1334 OutContext.getMachOSection("__TEXT","__symbol_stub1",
1335 MachO::S_SYMBOL_STUBS |
1336 MachO::S_ATTR_PURE_INSTRUCTIONS,
1337 16, SectionKind::getText()));
1339 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1342 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1343 bool isPPC64 = getDataLayout().getPointerSizeInBits() == 64;
1345 // Darwin/PPC always uses mach-o.
1346 const TargetLoweringObjectFileMachO &TLOFMacho =
1347 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1348 MachineModuleInfoMachO &MMIMacho =
1349 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1351 if (MAI->doesSupportExceptionHandling() && MMI) {
1352 // Add the (possibly multiple) personalities to the set of global values.
1353 // Only referenced functions get into the Personalities list.
1354 for (const Function *Personality : MMI->getPersonalities()) {
1357 getSymbolWithGlobalValueBase(Personality, "$non_lazy_ptr");
1358 MachineModuleInfoImpl::StubValueTy &StubSym =
1359 MMIMacho.getGVStubEntry(NLPSym);
1361 MachineModuleInfoImpl::StubValueTy(getSymbol(Personality), true);
1366 // Output stubs for dynamically-linked functions.
1367 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1369 // Output macho stubs for external and common global variables.
1370 if (!Stubs.empty()) {
1371 // Switch with ".non_lazy_symbol_pointer" directive.
1372 OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1373 EmitAlignment(isPPC64 ? 3 : 2);
1375 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1377 OutStreamer->EmitLabel(Stubs[i].first);
1378 // .indirect_symbol _foo
1379 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1380 OutStreamer->EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
1383 // External to current translation unit.
1384 OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
1386 // Internal to current translation unit.
1388 // When we place the LSDA into the TEXT section, the type info pointers
1389 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1390 // However, sometimes the types are local to the file. So we need to
1391 // fill in the value for the NLP in those cases.
1392 OutStreamer->EmitValue(MCSymbolRefExpr::create(MCSym.getPointer(),
1394 isPPC64 ? 8 : 4/*size*/);
1398 OutStreamer->AddBlankLine();
1401 // Funny Darwin hack: This flag tells the linker that no global symbols
1402 // contain code that falls through to other global symbols (e.g. the obvious
1403 // implementation of multiple entry points). If this doesn't occur, the
1404 // linker can safely perform dead code stripping. Since LLVM never generates
1405 // code that does this, it is always safe to set.
1406 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1408 return AsmPrinter::doFinalization(M);
1411 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1412 /// for a MachineFunction to the given output stream, in a format that the
1413 /// Darwin assembler can deal with.
1416 createPPCAsmPrinterPass(TargetMachine &tm,
1417 std::unique_ptr<MCStreamer> &&Streamer) {
1418 if (tm.getTargetTriple().isMacOSX())
1419 return new PPCDarwinAsmPrinter(tm, std::move(Streamer));
1420 return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
1423 // Force static initialization.
1424 extern "C" void LLVMInitializePowerPCAsmPrinter() {
1425 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1426 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1427 TargetRegistry::RegisterAsmPrinter(ThePPC64LETarget, createPPCAsmPrinterPass);