1//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#include "PPC.h"
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"
61using namespace llvm;
62
63#define DEBUG_TYPE "asmprinter"
64
65namespace {
66class PPCAsmPrinter : public AsmPrinter {
67protected:
68  MapVector<MCSymbol *, MCSymbol *> TOC;
69  const PPCSubtarget *Subtarget;
70  StackMaps SM;
71
72public:
73  explicit PPCAsmPrinter(TargetMachine &TM,
74                         std::unique_ptr<MCStreamer> Streamer)
75      : AsmPrinter(TM, std::move(Streamer)), SM(*this) {}
76
77  const char *getPassName() const override {
78    return "PowerPC Assembly Printer";
79  }
80
81    MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
82
83    virtual bool doInitialization(Module &M) override {
84      if (!TOC.empty())
85        TOC.clear();
86      return AsmPrinter::doInitialization(M);
87    }
88
89    void EmitInstruction(const MachineInstr *MI) override;
90
91    void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
92
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;
99
100    void EmitEndOfAsmFile(Module &M) override;
101
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);
108    }
109  };
110
111  /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
112  class PPCLinuxAsmPrinter : public PPCAsmPrinter {
113  public:
114    explicit PPCLinuxAsmPrinter(TargetMachine &TM,
115                                std::unique_ptr<MCStreamer> Streamer)
116        : PPCAsmPrinter(TM, std::move(Streamer)) {}
117
118    const char *getPassName() const override {
119      return "Linux PPC Assembly Printer";
120    }
121
122    bool doFinalization(Module &M) override;
123    void EmitStartOfAsmFile(Module &M) override;
124
125    void EmitFunctionEntryLabel() override;
126
127    void EmitFunctionBodyStart() override;
128    void EmitFunctionBodyEnd() override;
129  };
130
131  /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
132  /// OS X
133  class PPCDarwinAsmPrinter : public PPCAsmPrinter {
134  public:
135    explicit PPCDarwinAsmPrinter(TargetMachine &TM,
136                                 std::unique_ptr<MCStreamer> Streamer)
137        : PPCAsmPrinter(TM, std::move(Streamer)) {}
138
139    const char *getPassName() const override {
140      return "Darwin PPC Assembly Printer";
141    }
142
143    bool doFinalization(Module &M) override;
144    void EmitStartOfAsmFile(Module &M) override;
145  };
146} // end of anonymous namespace
147
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.
150static const char *stripRegisterPrefix(const char *RegName) {
151  switch (RegName[0]) {
152    case 'r':
153    case 'f':
154    case 'q': // for QPX
155    case 'v':
156      if (RegName[1] == 's')
157        return RegName + 2;
158      return RegName + 1;
159    case 'c': if (RegName[1] == 'r') return RegName + 2;
160  }
161
162  return RegName;
163}
164
165void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
166                                 raw_ostream &O) {
167  const DataLayout &DL = getDataLayout();
168  const MachineOperand &MO = MI->getOperand(OpNo);
169
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);
177    O << RegName;
178    return;
179  }
180  case MachineOperand::MO_Immediate:
181    O << MO.getImm();
182    return;
183
184  case MachineOperand::MO_MachineBasicBlock:
185    MO.getMBB()->getSymbol()->print(O, MAI);
186    return;
187  case MachineOperand::MO_ConstantPoolIndex:
188    O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
189      << MO.getIndex();
190    return;
191  case MachineOperand::MO_BlockAddress:
192    GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
193    return;
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;
198
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(
204              SymToPrint);
205      if (!StubSym.getPointer())
206        StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
207                                                     !GV->hasInternalLinkage());
208    } else {
209      SymToPrint = getSymbol(GV);
210    }
211
212    SymToPrint->print(O, MAI);
213
214    printOffset(MO.getOffset(), O);
215    return;
216  }
217
218  default:
219    O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
220    return;
221  }
222}
223
224/// PrintAsmOperand - Print out an operand for an inline asm expression.
225///
226bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
227                                    unsigned AsmVariant,
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.
232
233    switch (ExtraCode[0]) {
234    default:
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())
244        return true;
245      ++OpNo;   // Return the high-part.
246      break;
247    case 'I':
248      // Write 'i' if an integer constant, otherwise nothing.  Used to print
249      // addi vs add, etc.
250      if (MI->getOperand(OpNo).isImm())
251        O << "i";
252      return false;
253    }
254  }
255
256  printOperand(MI, OpNo, O);
257  return false;
258}
259
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.
263
264bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
265                                          unsigned AsmVariant,
266                                          const char *ExtraCode,
267                                          raw_ostream &O) {
268  if (ExtraCode && ExtraCode[0]) {
269    if (ExtraCode[1] != 0) return true; // Unknown modifier.
270
271    switch (ExtraCode[0]) {
272    default: return true;  // Unknown modifier.
273    case 'y': // A memory reference for an X-form instruction
274      {
275        const char *RegName = "r0";
276        if (!Subtarget->isDarwin())
277          RegName = stripRegisterPrefix(RegName);
278        O << RegName << ", ";
279        printOperand(MI, OpNo, O);
280        return false;
281      }
282    case 'U': // Print 'u' for update form.
283    case 'X': // Print 'x' for indexed form.
284    {
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());
292      return false;
293    }
294    }
295  }
296
297  assert(MI->getOperand(OpNo).isReg());
298  O << "0(";
299  printOperand(MI, OpNo, O);
300  O << ")";
301  return false;
302}
303
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
306/// the TOC entry.
307MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
308  MCSymbol *&TOCEntry = TOC[Sym];
309  if (!TOCEntry)
310    TOCEntry = createTempSymbol("C");
311  return TOCEntry;
312}
313
314void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) {
315  SM.serializeToStackMapSection();
316}
317
318void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
319  unsigned NumNOPBytes = MI.getOperand(1).getImm();
320
321  SM.recordStackMap(MI);
322  assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
323
324  // Scan ahead to trim the shadow.
325  const MachineBasicBlock &MBB = *MI.getParent();
326  MachineBasicBlock::const_iterator MII(MI);
327  ++MII;
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)
333      break;
334    ++MII;
335    NumNOPBytes -= 4;
336  }
337
338  // Emit nops.
339  for (unsigned i = 0; i < NumNOPBytes; i += 4)
340    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
341}
342
343// Lower a patchpoint of the form:
344// [<def>], <id>, <numBytes>, <target>, <numArgs>
345void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
346  SM.recordPatchPoint(MI);
347  PatchPointOpers Opers(&MI);
348
349  unsigned EncodedBytes = 0;
350  const MachineOperand &CalleeMO =
351    Opers.getMetaOper(PatchPointOpers::TargetPos);
352
353  if (CalleeMO.isImm()) {
354    int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm();
355    if (CallTarget) {
356      assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
357             "High 16 bits of call target should be zero.");
358      unsigned ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
359      EncodedBytes = 0;
360      // Materialize the jump address:
361      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
362                                      .addReg(ScratchReg)
363                                      .addImm((CallTarget >> 32) & 0xFFFF));
364      ++EncodedBytes;
365      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
366                                      .addReg(ScratchReg)
367                                      .addReg(ScratchReg)
368                                      .addImm(32).addImm(16));
369      ++EncodedBytes;
370      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
371                                      .addReg(ScratchReg)
372                                      .addReg(ScratchReg)
373                                      .addImm((CallTarget >> 16) & 0xFFFF));
374      ++EncodedBytes;
375      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
376                                      .addReg(ScratchReg)
377                                      .addReg(ScratchReg)
378                                      .addImm(CallTarget & 0xFFFF));
379
380      // Save the current TOC pointer before the remote call.
381      int TOCSaveOffset = Subtarget->isELFv2ABI() ? 24 : 40;
382      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
383                                      .addReg(PPC::X2)
384                                      .addImm(TOCSaveOffset)
385                                      .addReg(PPC::X1));
386      ++EncodedBytes;
387
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)
395                                        .addReg(PPC::X2)
396                                        .addImm(8)
397                                        .addReg(ScratchReg));
398        ++EncodedBytes;
399        EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
400                                        .addReg(ScratchReg)
401                                        .addImm(0)
402                                        .addReg(ScratchReg));
403        ++EncodedBytes;
404      }
405
406      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
407                                      .addReg(ScratchReg));
408      ++EncodedBytes;
409      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
410      ++EncodedBytes;
411
412      // Restore the TOC pointer after the call.
413      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
414                                      .addReg(PPC::X2)
415                                      .addImm(TOCSaveOffset)
416                                      .addReg(PPC::X1));
417      ++EncodedBytes;
418    }
419  } else if (CalleeMO.isGlobal()) {
420    const GlobalValue *GValue = CalleeMO.getGlobal();
421    MCSymbol *MOSymbol = getSymbol(GValue);
422    const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
423
424    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
425                                    .addExpr(SymVar));
426    EncodedBytes += 2;
427  }
428
429  // Each instruction is 4 bytes.
430  EncodedBytes *= 4;
431
432  // Emit padding.
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));
440}
441
442/// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
443/// call to __tls_get_addr to the current output stream.
444void 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;
449
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");
458
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)
471                 .addExpr(TlsRef)
472                 .addExpr(SymVar));
473}
474
475/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
476/// the current output stream.
477///
478void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
479  MCInst TmpInst;
480  bool isPPC64 = Subtarget->isPPC64();
481  bool isDarwin = TM.getTargetTriple().isOSDarwin();
482  const Module *M = MF->getFunction()->getParent();
483  PICLevel::Level PL = M->getPICLevel();
484
485  // Lower multi-instruction pseudo operations.
486  switch (MI->getOpcode()) {
487  default: break;
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);
494
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:
500    //      blrl
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,
507                                                      OutContext),
508                              MCConstantExpr::create(4, OutContext),
509                              OutContext);
510
511    // Emit the 'bl'.
512    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
513    return;
514  }
515  case PPC::MovePCtoLR:
516  case PPC::MovePCtoLR8: {
517    // Transform %LR = MovePCtoLR
518    // Into this, where the label is the PIC base:
519    //     bl L1$pb
520    // L1$pb:
521    MCSymbol *PICBase = MF->getPICBaseSymbol();
522
523    // Emit the 'bl'.
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)));
529
530    // Emit the label.
531    OutStreamer->EmitLabel(PICBase);
532    return;
533  }
534  case PPC::UpdateGBR: {
535    // Transform %Rd = UpdateGBR(%Rt, %Ri)
536    // Into: lwz %Rt, .L0$poff - .L0$pb(%Ri)
537    //       add %Rd, %Rt, %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);
543    const MCExpr *Exp =
544      MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
545    const MCExpr *PB =
546      MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
547                              MCSymbolRefExpr::VK_None,
548                              OutContext);
549    const MCOperand TR = TmpInst.getOperand(1);
550    const MCOperand PICR = TmpInst.getOperand(0);
551
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);
558
559    TmpInst.setOpcode(PPC::ADD4);
560    TmpInst.getOperand(0) = PICR;
561    TmpInst.getOperand(1) = TR;
562    TmpInst.getOperand(2) = PICR;
563    EmitToStreamer(*OutStreamer, TmpInst);
564    return;
565  }
566  case PPC::LWZtoc: {
567    // Transform %R3 = LWZtoc <ga:@min1>, %R2
568    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
569
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);
574
575    // Map symbol -> label of TOC entry
576    assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
577    MCSymbol *MOSymbol = nullptr;
578    if (MO.isGlobal())
579      MOSymbol = getSymbol(MO.getGlobal());
580    else if (MO.isCPI())
581      MOSymbol = GetCPISymbol(MO.getIndex());
582    else if (MO.isJTI())
583      MOSymbol = GetJTISymbol(MO.getIndex());
584    else if (MO.isBlockAddress())
585      MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
586
587    if (PL == PICLevel::SmallPIC) {
588      const MCExpr *Exp =
589        MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT,
590                                OutContext);
591      TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
592    } else {
593      MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
594
595      const MCExpr *Exp =
596        MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None,
597                                OutContext);
598      const MCExpr *PB =
599        MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
600                                                             OutContext);
601      Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
602      TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
603    }
604    EmitToStreamer(*OutStreamer, TmpInst);
605    return;
606  }
607  case PPC::LDtocJTI:
608  case PPC::LDtocCPT:
609  case PPC::LDtocBA:
610  case PPC::LDtoc: {
611    // Transform %X3 = LDtoc <ga:@min1>, %X2
612    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
613
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);
618
619    // Map symbol -> label of TOC entry
620    assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
621    MCSymbol *MOSymbol = nullptr;
622    if (MO.isGlobal())
623      MOSymbol = getSymbol(MO.getGlobal());
624    else if (MO.isCPI())
625      MOSymbol = GetCPISymbol(MO.getIndex());
626    else if (MO.isJTI())
627      MOSymbol = GetJTISymbol(MO.getIndex());
628    else if (MO.isBlockAddress())
629      MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
630
631    MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
632
633    const MCExpr *Exp =
634      MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
635                              OutContext);
636    TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
637    EmitToStreamer(*OutStreamer, TmpInst);
638    return;
639  }
640
641  case PPC::ADDIStocHA: {
642    // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
643    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
644
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;
656
657    if (MO.isGlobal()) {
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());
668    }
669
670    if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
671        TM.getCodeModel() == CodeModel::Large)
672      MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
673
674    const MCExpr *Exp =
675      MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA,
676                              OutContext);
677    TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
678    EmitToStreamer(*OutStreamer, TmpInst);
679    return;
680  }
681  case PPC::LDtocL: {
682    // Transform %Xd = LDtocL <ga:@sym>, %Xs
683    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
684
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;
694
695    if (MO.isJTI())
696      MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
697    else if (MO.isBlockAddress()) {
698      MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
699      MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
700    }
701    else if (MO.isCPI()) {
702      MOSymbol = GetCPISymbol(MO.getIndex());
703      if (TM.getCodeModel() == CodeModel::Large)
704        MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
705    }
706    else if (MO.isGlobal()) {
707      const GlobalValue *GV = MO.getGlobal();
708      MOSymbol = getSymbol(GV);
709      DEBUG(
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);
715    }
716
717    const MCExpr *Exp =
718      MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
719                              OutContext);
720    TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
721    EmitToStreamer(*OutStreamer, TmpInst);
722    return;
723  }
724  case PPC::ADDItocL: {
725    // Transform %Xd = ADDItocL %Xs, <ga:@sym>
726    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
727
728    // Change the opcode to ADDI8.  If the global address is external, then
729    // generate a TOC entry and reference that.  Otherwise reference the
730    // symbol directly.
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;
735
736    if (MO.isGlobal()) {
737      const GlobalValue *GV = MO.getGlobal();
738      DEBUG(
739        unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
740        assert (
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());
746    }
747
748    const MCExpr *Exp =
749      MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
750                              OutContext);
751    TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
752    EmitToStreamer(*OutStreamer, TmpInst);
753    return;
754  }
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,
764                              OutContext);
765    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
766                                 .addReg(MI->getOperand(0).getReg())
767                                 .addReg(MI->getOperand(1).getReg())
768                                 .addExpr(SymGotTprel));
769    return;
770  }
771  case PPC::LDgotTprelL:
772  case PPC::LDgotTprelL32: {
773    // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
774    LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
775
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);
781    const MCExpr *Exp =
782      MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO,
783                              OutContext);
784    TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
785    EmitToStreamer(*OutStreamer, TmpInst);
786    return;
787  }
788
789  case PPC::PPC32PICGOT: {
790    MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
791    MCSymbol *GOTRef = OutContext.createTempSymbol();
792    MCSymbol *NextInstr = OutContext.createTempSymbol();
793
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),
801        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())
809                                 .addImm(0)
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()));
815    return;
816  }
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));
831    return;
832  }
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,
842                              OutContext);
843    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
844                                 .addReg(MI->getOperand(0).getReg())
845                                 .addReg(MI->getOperand(1).getReg())
846                                 .addExpr(SymGotTlsGD));
847    return;
848  }
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,
861        OutContext);
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));
867    return;
868  }
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);
876    return;
877  }
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,
887                              OutContext);
888    EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
889                                 .addReg(MI->getOperand(0).getReg())
890                                 .addReg(MI->getOperand(1).getReg())
891                                 .addExpr(SymGotTlsLD));
892    return;
893  }
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,
906        OutContext);
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));
912    return;
913  }
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);
921    return;
922  }
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,
934                              OutContext);
935    EmitToStreamer(
936        *OutStreamer,
937        MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS)
938            .addReg(MI->getOperand(0).getReg())
939            .addReg(MI->getOperand(1).getReg())
940            .addExpr(SymDtprel));
941    return;
942  }
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,
954                              OutContext);
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));
960    return;
961  }
962  case PPC::MFOCRF:
963  case PPC::MFOCRF8:
964    if (!Subtarget->hasMFOCRF()) {
965      // Transform: %R3 = MFOCRF %CR7
966      // Into:      %R3 = MFCR   ;; cr7
967      unsigned NewOpcode =
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()));
973      return;
974    }
975    break;
976  case PPC::MTOCRF:
977  case PPC::MTOCRF8:
978    if (!Subtarget->hasMFOCRF()) {
979      // Transform: %CR7 = MTOCRF %R3
980      // Into:      MTCRF mask, %R3 ;; cr7
981      unsigned NewOpcode =
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)
988                                     .addImm(Mask)
989                                     .addReg(MI->getOperand(1).getReg()));
990      return;
991    }
992    break;
993  case PPC::LD:
994  case PPC::STD:
995  case PPC::LWA_32:
996  case PPC::LWA: {
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!");
1008    }
1009    // Now process the instruction normally.
1010    break;
1011  }
1012  }
1013
1014  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
1015  EmitToStreamer(*OutStreamer, TmpInst);
1016}
1017
1018void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) {
1019  if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1020    PPCTargetStreamer *TS =
1021      static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1022
1023    if (TS)
1024      TS->emitAbiVersion(2);
1025  }
1026
1027  if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1028      !isPositionIndependent())
1029    return AsmPrinter::EmitStartOfAsmFile(M);
1030
1031  if (M.getPICLevel() == PICLevel::SmallPIC)
1032    return AsmPrinter::EmitStartOfAsmFile(M);
1033
1034  OutStreamer->SwitchSection(OutContext.getELFSection(
1035      ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC));
1036
1037  MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1038  MCSymbol *CurrentPos = OutContext.createTempSymbol();
1039
1040  OutStreamer->EmitLabel(CurrentPos);
1041
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),
1047                            OutContext);
1048
1049  OutStreamer->EmitAssignment(TOCSym, tocExpr);
1050
1051  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1052}
1053
1054void 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();
1060
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);
1067
1068      const MCExpr *OffsExpr =
1069        MCBinaryExpr::createSub(
1070          MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1071                                                               OutContext),
1072                                  MCSymbolRefExpr::create(PICBase, OutContext),
1073          OutContext);
1074      OutStreamer->EmitValue(OffsExpr, 4);
1075      OutStreamer->EmitLabel(CurrentFnSym);
1076      return;
1077    } else
1078      return AsmPrinter::EmitFunctionEntryLabel();
1079  }
1080
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>();
1090
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,
1096                                                        OutContext),
1097                                OutContext);
1098
1099      OutStreamer->EmitLabel(PPCFI->getTOCOffsetSymbol());
1100      OutStreamer->EmitValue(TOCDeltaExpr, 8);
1101    }
1102    return AsmPrinter::EmitFunctionEntryLabel();
1103  }
1104
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
1114  // entry point.
1115  OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
1116                         8 /*size*/);
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),
1121    8/*size*/);
1122  // Emit a null environment pointer.
1123  OutStreamer->EmitIntValue(0, 8 /* size */);
1124  OutStreamer->SwitchSection(Current.first, Current.second);
1125}
1126
1127bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
1128  const DataLayout &DL = getDataLayout();
1129
1130  bool isPPC64 = DL.getPointerSizeInBits() == 64;
1131
1132  PPCTargetStreamer &TS =
1133      static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
1134
1135  if (!TOC.empty()) {
1136    MCSectionELF *Section;
1137
1138    if (isPPC64)
1139      Section = OutStreamer->getContext().getELFSection(
1140          ".toc", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1141        else
1142          Section = OutStreamer->getContext().getELFSection(
1143              ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1144    OutStreamer->SwitchSection(Section);
1145
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;
1150      if (isPPC64)
1151        TS.emitTCEntry(*S);
1152      else
1153        OutStreamer->EmitSymbolValue(S, 4);
1154    }
1155  }
1156
1157  return AsmPrinter::doFinalization(M);
1158}
1159
1160/// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
1161void 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:
1168  //
1169  // func:
1170  // .Lfunc_gepNN:
1171  //         # global entry point
1172  //         addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
1173  //         addi  r2,r2,(.TOC.-.Lfunc_gepNN)@l
1174  // .Lfunc_lepNN:
1175  //         .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1176  //         # local entry point, followed by function body
1177  //
1178  // For the Large code model, we create
1179  //
1180  // .Lfunc_tocNN:
1181  //         .quad .TOC.-.Lfunc_gepNN      # done by EmitFunctionEntryLabel
1182  // func:
1183  // .Lfunc_gepNN:
1184  //         # global entry point
1185  //         ld    r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
1186  //         add   r2,r2,r12
1187  // .Lfunc_lepNN:
1188  //         .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1189  //         # local entry point, followed by function body
1190  //
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>();
1197
1198    MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol();
1199    OutStreamer->EmitLabel(GlobalEntryLabel);
1200    const MCSymbolRefExpr *GlobalEntryLabelExp =
1201      MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
1202
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);
1208
1209      const MCExpr *TOCDeltaHi =
1210        PPCMCExpr::createHa(TOCDeltaExpr, false, OutContext);
1211      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1212                                   .addReg(PPC::X2)
1213                                   .addReg(PPC::X12)
1214                                   .addExpr(TOCDeltaHi));
1215
1216      const MCExpr *TOCDeltaLo =
1217        PPCMCExpr::createLo(TOCDeltaExpr, false, OutContext);
1218      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
1219                                   .addReg(PPC::X2)
1220                                   .addReg(PPC::X2)
1221                                   .addExpr(TOCDeltaLo));
1222    } else {
1223      MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol();
1224      const MCExpr *TOCOffsetDeltaExpr =
1225        MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
1226                                GlobalEntryLabelExp, OutContext);
1227
1228      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
1229                                   .addReg(PPC::X2)
1230                                   .addExpr(TOCOffsetDeltaExpr)
1231                                   .addReg(PPC::X12));
1232      EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
1233                                   .addReg(PPC::X2)
1234                                   .addReg(PPC::X2)
1235                                   .addReg(PPC::X12));
1236    }
1237
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);
1245
1246    PPCTargetStreamer *TS =
1247      static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1248
1249    if (TS)
1250      TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
1251  }
1252}
1253
1254/// EmitFunctionBodyEnd - Print the traceback table before the .size
1255/// directive.
1256///
1257void 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*/);
1268  }
1269}
1270
1271void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
1272  static const char *const CPUDirectives[] = {
1273    "",
1274    "ppc",
1275    "ppc440",
1276    "ppc601",
1277    "ppc602",
1278    "ppc603",
1279    "ppc7400",
1280    "ppc750",
1281    "ppc970",
1282    "ppcA2",
1283    "ppce500mc",
1284    "ppce5500",
1285    "power3",
1286    "power4",
1287    "power5",
1288    "power5x",
1289    "power6",
1290    "power6x",
1291    "power7",
1292    // FIXME: why is power8 missing here?
1293    "ppc64",
1294    "ppc64le",
1295    "power9"
1296  };
1297
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;
1311  }
1312
1313  assert(Directive <= PPC::DIR_64 && "Directive out of range.");
1314
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]);
1320
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()));
1338  }
1339  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1340}
1341
1342bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1343  bool isPPC64 = getDataLayout().getPointerSizeInBits() == 64;
1344
1345  // Darwin/PPC always uses mach-o.
1346  const TargetLoweringObjectFileMachO &TLOFMacho =
1347      static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1348  MachineModuleInfoMachO &MMIMacho =
1349      MMI->getObjFileInfo<MachineModuleInfoMachO>();
1350
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()) {
1355      if (Personality) {
1356        MCSymbol *NLPSym =
1357            getSymbolWithGlobalValueBase(Personality, "$non_lazy_ptr");
1358        MachineModuleInfoImpl::StubValueTy &StubSym =
1359            MMIMacho.getGVStubEntry(NLPSym);
1360        StubSym =
1361            MachineModuleInfoImpl::StubValueTy(getSymbol(Personality), true);
1362      }
1363    }
1364  }
1365
1366  // Output stubs for dynamically-linked functions.
1367  MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1368
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);
1374
1375    for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1376      // L_foo$stub:
1377      OutStreamer->EmitLabel(Stubs[i].first);
1378      //   .indirect_symbol _foo
1379      MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1380      OutStreamer->EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
1381
1382      if (MCSym.getInt())
1383        // External to current translation unit.
1384        OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
1385      else
1386        // Internal to current translation unit.
1387        //
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(),
1393                                                       OutContext),
1394                              isPPC64 ? 8 : 4/*size*/);
1395    }
1396
1397    Stubs.clear();
1398    OutStreamer->AddBlankLine();
1399  }
1400
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);
1407
1408  return AsmPrinter::doFinalization(M);
1409}
1410
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.
1414///
1415static AsmPrinter *
1416createPPCAsmPrinterPass(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));
1421}
1422
1423// Force static initialization.
1424extern "C" void LLVMInitializePowerPCAsmPrinter() {
1425  TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1426  TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1427  TargetRegistry::RegisterAsmPrinter(ThePPC64LETarget, createPPCAsmPrinterPass);
1428}
1429