LLVM  3.7.0
PPCAsmPrinter.cpp
Go to the documentation of this file.
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"
21 #include "MCTargetDesc/PPCMCExpr.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/SmallString.h"
29 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/CodeGen/StackMaps.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DebugInfo.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/Mangler.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/MC/MCAsmInfo.h"
45 #include "llvm/MC/MCContext.h"
46 #include "llvm/MC/MCExpr.h"
47 #include "llvm/MC/MCInst.h"
48 #include "llvm/MC/MCInstBuilder.h"
49 #include "llvm/MC/MCSectionELF.h"
50 #include "llvm/MC/MCSectionMachO.h"
51 #include "llvm/MC/MCStreamer.h"
52 #include "llvm/MC/MCSymbolELF.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ELF.h"
63 using namespace llvm;
64 
65 #define DEBUG_TYPE "asmprinter"
66 
67 namespace {
68  class PPCAsmPrinter : public AsmPrinter {
69  protected:
71  const PPCSubtarget *Subtarget;
72  StackMaps SM;
73  public:
74  explicit PPCAsmPrinter(TargetMachine &TM,
75  std::unique_ptr<MCStreamer> Streamer)
76  : AsmPrinter(TM, std::move(Streamer)), SM(*this) {}
77 
78  const char *getPassName() const override {
79  return "PowerPC Assembly Printer";
80  }
81 
82  MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
83 
84  void EmitInstruction(const MachineInstr *MI) override;
85 
86  void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
87 
88  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
89  unsigned AsmVariant, const char *ExtraCode,
90  raw_ostream &O) override;
91  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
92  unsigned AsmVariant, const char *ExtraCode,
93  raw_ostream &O) override;
94 
95  void EmitEndOfAsmFile(Module &M) override;
96 
97  void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
98  const MachineInstr &MI);
99  void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
100  const MachineInstr &MI);
101  void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
102  bool runOnMachineFunction(MachineFunction &MF) override {
103  Subtarget = &MF.getSubtarget<PPCSubtarget>();
105  }
106  };
107 
108  /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
109  class PPCLinuxAsmPrinter : public PPCAsmPrinter {
110  public:
111  explicit PPCLinuxAsmPrinter(TargetMachine &TM,
112  std::unique_ptr<MCStreamer> Streamer)
113  : PPCAsmPrinter(TM, std::move(Streamer)) {}
114 
115  const char *getPassName() const override {
116  return "Linux PPC Assembly Printer";
117  }
118 
119  bool doFinalization(Module &M) override;
120  void EmitStartOfAsmFile(Module &M) override;
121 
122  void EmitFunctionEntryLabel() override;
123 
124  void EmitFunctionBodyStart() override;
125  void EmitFunctionBodyEnd() override;
126  };
127 
128  /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
129  /// OS X
130  class PPCDarwinAsmPrinter : public PPCAsmPrinter {
131  public:
132  explicit PPCDarwinAsmPrinter(TargetMachine &TM,
133  std::unique_ptr<MCStreamer> Streamer)
134  : PPCAsmPrinter(TM, std::move(Streamer)) {}
135 
136  const char *getPassName() const override {
137  return "Darwin PPC Assembly Printer";
138  }
139 
140  bool doFinalization(Module &M) override;
141  void EmitStartOfAsmFile(Module &M) override;
142 
143  void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
144  };
145 } // end of anonymous namespace
146 
147 /// stripRegisterPrefix - This method strips the character prefix from a
148 /// register name so that only the number is left. Used by for linux asm.
149 static const char *stripRegisterPrefix(const char *RegName) {
150  switch (RegName[0]) {
151  case 'r':
152  case 'f':
153  case 'q': // for QPX
154  case 'v':
155  if (RegName[1] == 's')
156  return RegName + 2;
157  return RegName + 1;
158  case 'c': if (RegName[1] == 'r') return RegName + 2;
159  }
160 
161  return RegName;
162 }
163 
164 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
165  raw_ostream &O) {
166  const DataLayout *DL = TM.getDataLayout();
167  const MachineOperand &MO = MI->getOperand(OpNo);
168 
169  switch (MO.getType()) {
171  const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
172  // Linux assembler (Others?) does not take register mnemonics.
173  // FIXME - What about special registers used in mfspr/mtspr?
174  if (!Subtarget->isDarwin())
175  RegName = stripRegisterPrefix(RegName);
176  O << RegName;
177  return;
178  }
180  O << MO.getImm();
181  return;
182 
184  MO.getMBB()->getSymbol()->print(O, MAI);
185  return;
187  O << DL->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
188  << '_' << MO.getIndex();
189  return;
191  GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
192  return;
194  // Computing the address of a global symbol, not calling it.
195  const GlobalValue *GV = MO.getGlobal();
196  MCSymbol *SymToPrint;
197 
198  // External or weakly linked global variables need non-lazily-resolved stubs
199  if (TM.getRelocationModel() != Reloc::Static &&
201  if (!GV->hasHiddenVisibility()) {
202  SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
204  MMI->getObjFileInfo<MachineModuleInfoMachO>()
205  .getGVStubEntry(SymToPrint);
206  if (!StubSym.getPointer())
207  StubSym = MachineModuleInfoImpl::
208  StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
209  } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
211  SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
212 
214  MMI->getObjFileInfo<MachineModuleInfoMachO>().
215  getHiddenGVStubEntry(SymToPrint);
216  if (!StubSym.getPointer())
217  StubSym = MachineModuleInfoImpl::
218  StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
219  } else {
220  SymToPrint = getSymbol(GV);
221  }
222  } else {
223  SymToPrint = getSymbol(GV);
224  }
225 
226  SymToPrint->print(O, MAI);
227 
228  printOffset(MO.getOffset(), O);
229  return;
230  }
231 
232  default:
233  O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
234  return;
235  }
236 }
237 
238 /// PrintAsmOperand - Print out an operand for an inline asm expression.
239 ///
240 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
241  unsigned AsmVariant,
242  const char *ExtraCode, raw_ostream &O) {
243  // Does this asm operand have a single letter operand modifier?
244  if (ExtraCode && ExtraCode[0]) {
245  if (ExtraCode[1] != 0) return true; // Unknown modifier.
246 
247  switch (ExtraCode[0]) {
248  default:
249  // See if this is a generic print operand
250  return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
251  case 'c': // Don't print "$" before a global var name or constant.
252  break; // PPC never has a prefix.
253  case 'L': // Write second word of DImode reference.
254  // Verify that this operand has two consecutive registers.
255  if (!MI->getOperand(OpNo).isReg() ||
256  OpNo+1 == MI->getNumOperands() ||
257  !MI->getOperand(OpNo+1).isReg())
258  return true;
259  ++OpNo; // Return the high-part.
260  break;
261  case 'I':
262  // Write 'i' if an integer constant, otherwise nothing. Used to print
263  // addi vs add, etc.
264  if (MI->getOperand(OpNo).isImm())
265  O << "i";
266  return false;
267  }
268  }
269 
270  printOperand(MI, OpNo, O);
271  return false;
272 }
273 
274 // At the moment, all inline asm memory operands are a single register.
275 // In any case, the output of this routine should always be just one
276 // assembler operand.
277 
278 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
279  unsigned AsmVariant,
280  const char *ExtraCode,
281  raw_ostream &O) {
282  if (ExtraCode && ExtraCode[0]) {
283  if (ExtraCode[1] != 0) return true; // Unknown modifier.
284 
285  switch (ExtraCode[0]) {
286  default: return true; // Unknown modifier.
287  case 'y': // A memory reference for an X-form instruction
288  {
289  const char *RegName = "r0";
290  if (!Subtarget->isDarwin())
291  RegName = stripRegisterPrefix(RegName);
292  O << RegName << ", ";
293  printOperand(MI, OpNo, O);
294  return false;
295  }
296  case 'U': // Print 'u' for update form.
297  case 'X': // Print 'x' for indexed form.
298  {
299  // FIXME: Currently for PowerPC memory operands are always loaded
300  // into a register, so we never get an update or indexed form.
301  // This is bad even for offset forms, since even if we know we
302  // have a value in -16(r1), we will generate a load into r<n>
303  // and then load from 0(r<n>). Until that issue is fixed,
304  // tolerate 'U' and 'X' but don't output anything.
305  assert(MI->getOperand(OpNo).isReg());
306  return false;
307  }
308  }
309  }
310 
311  assert(MI->getOperand(OpNo).isReg());
312  O << "0(";
313  printOperand(MI, OpNo, O);
314  O << ")";
315  return false;
316 }
317 
318 
319 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
320 /// exists for it. If not, create one. Then return a symbol that references
321 /// the TOC entry.
322 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
323  MCSymbol *&TOCEntry = TOC[Sym];
324  if (!TOCEntry)
325  TOCEntry = createTempSymbol("C");
326  return TOCEntry;
327 }
328 
329 void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) {
330  SM.serializeToStackMapSection();
331 }
332 
333 void PPCAsmPrinter::LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM,
334  const MachineInstr &MI) {
335  unsigned NumNOPBytes = MI.getOperand(1).getImm();
336 
337  SM.recordStackMap(MI);
338  assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
339 
340  // Scan ahead to trim the shadow.
341  const MachineBasicBlock &MBB = *MI.getParent();
343  ++MII;
344  while (NumNOPBytes > 0) {
345  if (MII == MBB.end() || MII->isCall() ||
346  MII->getOpcode() == PPC::DBG_VALUE ||
347  MII->getOpcode() == TargetOpcode::PATCHPOINT ||
348  MII->getOpcode() == TargetOpcode::STACKMAP)
349  break;
350  ++MII;
351  NumNOPBytes -= 4;
352  }
353 
354  // Emit nops.
355  for (unsigned i = 0; i < NumNOPBytes; i += 4)
356  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::NOP));
357 }
358 
359 // Lower a patchpoint of the form:
360 // [<def>], <id>, <numBytes>, <target>, <numArgs>
361 void PPCAsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM,
362  const MachineInstr &MI) {
363  SM.recordPatchPoint(MI);
364  PatchPointOpers Opers(&MI);
365 
366  unsigned EncodedBytes = 0;
367  const MachineOperand &CalleeMO =
368  Opers.getMetaOper(PatchPointOpers::TargetPos);
369 
370  if (CalleeMO.isImm()) {
371  int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm();
372  if (CallTarget) {
373  assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
374  "High 16 bits of call target should be zero.");
375  unsigned ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
376  EncodedBytes = 0;
377  // Materialize the jump address:
378  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LI8)
379  .addReg(ScratchReg)
380  .addImm((CallTarget >> 32) & 0xFFFF));
381  ++EncodedBytes;
382  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::RLDIC)
383  .addReg(ScratchReg)
384  .addReg(ScratchReg)
385  .addImm(32).addImm(16));
386  ++EncodedBytes;
387  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::ORIS8)
388  .addReg(ScratchReg)
389  .addReg(ScratchReg)
390  .addImm((CallTarget >> 16) & 0xFFFF));
391  ++EncodedBytes;
392  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::ORI8)
393  .addReg(ScratchReg)
394  .addReg(ScratchReg)
395  .addImm(CallTarget & 0xFFFF));
396 
397  // Save the current TOC pointer before the remote call.
398  int TOCSaveOffset = Subtarget->isELFv2ABI() ? 24 : 40;
399  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::STD)
400  .addReg(PPC::X2)
401  .addImm(TOCSaveOffset)
402  .addReg(PPC::X1));
403  ++EncodedBytes;
404 
405 
406  // If we're on ELFv1, then we need to load the actual function pointer
407  // from the function descriptor.
408  if (!Subtarget->isELFv2ABI()) {
409  // Load the new TOC pointer and the function address, but not r11
410  // (needing this is rare, and loading it here would prevent passing it
411  // via a 'nest' parameter.
412  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD)
413  .addReg(PPC::X2)
414  .addImm(8)
415  .addReg(ScratchReg));
416  ++EncodedBytes;
417  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD)
418  .addReg(ScratchReg)
419  .addImm(0)
420  .addReg(ScratchReg));
421  ++EncodedBytes;
422  }
423 
424  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::MTCTR8)
425  .addReg(ScratchReg));
426  ++EncodedBytes;
427  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::BCTRL8));
428  ++EncodedBytes;
429 
430  // Restore the TOC pointer after the call.
431  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD)
432  .addReg(PPC::X2)
433  .addImm(TOCSaveOffset)
434  .addReg(PPC::X1));
435  ++EncodedBytes;
436  }
437  } else if (CalleeMO.isGlobal()) {
438  const GlobalValue *GValue = CalleeMO.getGlobal();
439  MCSymbol *MOSymbol = getSymbol(GValue);
440  const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
441 
442  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::BL8_NOP)
443  .addExpr(SymVar));
444  EncodedBytes += 2;
445  }
446 
447  // Each instruction is 4 bytes.
448  EncodedBytes *= 4;
449 
450  // Emit padding.
451  unsigned NumBytes = Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
452  assert(NumBytes >= EncodedBytes &&
453  "Patchpoint can't request size less than the length of a call.");
454  assert((NumBytes - EncodedBytes) % 4 == 0 &&
455  "Invalid number of NOP bytes requested!");
456  for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
457  EmitToStreamer(OutStreamer, MCInstBuilder(PPC::NOP));
458 }
459 
460 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
461 /// call to __tls_get_addr to the current output stream.
462 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
464  StringRef Name = "__tls_get_addr";
465  MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol(Name);
467 
468  assert(MI->getOperand(0).isReg() &&
469  ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
470  (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
471  "GETtls[ld]ADDR[32] must define GPR3");
472  assert(MI->getOperand(1).isReg() &&
473  ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
474  (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
475  "GETtls[ld]ADDR[32] must read GPR3");
476 
477  if (!Subtarget->isPPC64() && !Subtarget->isDarwin() &&
478  TM.getRelocationModel() == Reloc::PIC_)
480  const MCSymbolRefExpr *TlsRef =
481  MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
482  const MachineOperand &MO = MI->getOperand(2);
483  const GlobalValue *GValue = MO.getGlobal();
484  MCSymbol *MOSymbol = getSymbol(GValue);
485  const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
486  EmitToStreamer(*OutStreamer,
487  MCInstBuilder(Subtarget->isPPC64() ?
488  PPC::BL8_NOP_TLS : PPC::BL_TLS)
489  .addExpr(TlsRef)
490  .addExpr(SymVar));
491 }
492 
493 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
494 /// the current output stream.
495 ///
496 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
497  MCInst TmpInst;
498  bool isPPC64 = Subtarget->isPPC64();
499  bool isDarwin = TM.getTargetTriple().isOSDarwin();
500  const Module *M = MF->getFunction()->getParent();
502 
503  // Lower multi-instruction pseudo operations.
504  switch (MI->getOpcode()) {
505  default: break;
507  llvm_unreachable("Should be handled target independently");
509  return LowerSTACKMAP(*OutStreamer, SM, *MI);
511  return LowerPATCHPOINT(*OutStreamer, SM, *MI);
512 
513  case PPC::MoveGOTtoLR: {
514  // Transform %LR = MoveGOTtoLR
515  // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
516  // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
517  // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
518  // blrl
519  // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
520  MCSymbol *GOTSymbol =
521  OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
522  const MCExpr *OffsExpr =
525  OutContext),
526  MCConstantExpr::create(4, OutContext),
527  OutContext);
528 
529  // Emit the 'bl'.
530  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
531  return;
532  }
533  case PPC::MovePCtoLR:
534  case PPC::MovePCtoLR8: {
535  // Transform %LR = MovePCtoLR
536  // Into this, where the label is the PIC base:
537  // bl L1$pb
538  // L1$pb:
539  MCSymbol *PICBase = MF->getPICBaseSymbol();
540 
541  // Emit the 'bl'.
542  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
543  // FIXME: We would like an efficient form for this, so we don't have to do
544  // a lot of extra uniquing.
545  .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
546 
547  // Emit the label.
548  OutStreamer->EmitLabel(PICBase);
549  return;
550  }
551  case PPC::UpdateGBR: {
552  // Transform %Rd = UpdateGBR(%Rt, %Ri)
553  // Into: lwz %Rt, .L0$poff - .L0$pb(%Ri)
554  // add %Rd, %Rt, %Ri
555  // Get the offset from the GOT Base Register to the GOT
556  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
557  MCSymbol *PICOffset =
558  MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol();
559  TmpInst.setOpcode(PPC::LWZ);
560  const MCExpr *Exp =
561  MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
562  const MCExpr *PB =
563  MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
565  OutContext);
566  const MCOperand TR = TmpInst.getOperand(1);
567  const MCOperand PICR = TmpInst.getOperand(0);
568 
569  // Step 1: lwz %Rt, .L$poff - .L$pb(%Ri)
570  TmpInst.getOperand(1) =
571  MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
572  TmpInst.getOperand(0) = TR;
573  TmpInst.getOperand(2) = PICR;
574  EmitToStreamer(*OutStreamer, TmpInst);
575 
576  TmpInst.setOpcode(PPC::ADD4);
577  TmpInst.getOperand(0) = PICR;
578  TmpInst.getOperand(1) = TR;
579  TmpInst.getOperand(2) = PICR;
580  EmitToStreamer(*OutStreamer, TmpInst);
581  return;
582  }
583  case PPC::LWZtoc: {
584  // Transform %R3 = LWZtoc <ga:@min1>, %R2
585  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
586 
587  // Change the opcode to LWZ, and the global address operand to be a
588  // reference to the GOT entry we will synthesize later.
589  TmpInst.setOpcode(PPC::LWZ);
590  const MachineOperand &MO = MI->getOperand(1);
591 
592  // Map symbol -> label of TOC entry
593  assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
594  MCSymbol *MOSymbol = nullptr;
595  if (MO.isGlobal())
596  MOSymbol = getSymbol(MO.getGlobal());
597  else if (MO.isCPI())
598  MOSymbol = GetCPISymbol(MO.getIndex());
599  else if (MO.isJTI())
600  MOSymbol = GetJTISymbol(MO.getIndex());
601  else if (MO.isBlockAddress())
602  MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
603 
604  if (PL == PICLevel::Small) {
605  const MCExpr *Exp =
607  OutContext);
608  TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
609  } else {
610  MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
611 
612  const MCExpr *Exp =
614  OutContext);
615  const MCExpr *PB =
616  MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
617  OutContext);
618  Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
619  TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
620  }
621  EmitToStreamer(*OutStreamer, TmpInst);
622  return;
623  }
624  case PPC::LDtocJTI:
625  case PPC::LDtocCPT:
626  case PPC::LDtocBA:
627  case PPC::LDtoc: {
628  // Transform %X3 = LDtoc <ga:@min1>, %X2
629  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
630 
631  // Change the opcode to LD, and the global address operand to be a
632  // reference to the TOC entry we will synthesize later.
633  TmpInst.setOpcode(PPC::LD);
634  const MachineOperand &MO = MI->getOperand(1);
635 
636  // Map symbol -> label of TOC entry
637  assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
638  MCSymbol *MOSymbol = nullptr;
639  if (MO.isGlobal())
640  MOSymbol = getSymbol(MO.getGlobal());
641  else if (MO.isCPI())
642  MOSymbol = GetCPISymbol(MO.getIndex());
643  else if (MO.isJTI())
644  MOSymbol = GetJTISymbol(MO.getIndex());
645  else if (MO.isBlockAddress())
646  MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
647 
648  MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
649 
650  const MCExpr *Exp =
652  OutContext);
653  TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
654  EmitToStreamer(*OutStreamer, TmpInst);
655  return;
656  }
657 
658  case PPC::ADDIStocHA: {
659  // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
660  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
661 
662  // Change the opcode to ADDIS8. If the global address is external, has
663  // common linkage, is a non-local function address, or is a jump table
664  // address, then generate a TOC entry and reference that. Otherwise
665  // reference the symbol directly.
666  TmpInst.setOpcode(PPC::ADDIS8);
667  const MachineOperand &MO = MI->getOperand(2);
668  assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
669  MO.isBlockAddress()) &&
670  "Invalid operand for ADDIStocHA!");
671  MCSymbol *MOSymbol = nullptr;
672  bool IsExternal = false;
673  bool IsNonLocalFunction = false;
674  bool IsCommon = false;
675  bool IsAvailExt = false;
676 
677  if (MO.isGlobal()) {
678  const GlobalValue *GV = MO.getGlobal();
679  MOSymbol = getSymbol(GV);
680  IsExternal = GV->isDeclaration();
681  IsCommon = GV->hasCommonLinkage();
682  IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() &&
684  IsAvailExt = GV->hasAvailableExternallyLinkage();
685  } else if (MO.isCPI())
686  MOSymbol = GetCPISymbol(MO.getIndex());
687  else if (MO.isJTI())
688  MOSymbol = GetJTISymbol(MO.getIndex());
689  else if (MO.isBlockAddress())
690  MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
691 
692  if (IsExternal || IsNonLocalFunction || IsCommon || IsAvailExt ||
693  MO.isJTI() || MO.isBlockAddress() ||
694  TM.getCodeModel() == CodeModel::Large)
695  MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
696 
697  const MCExpr *Exp =
699  OutContext);
700  TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
701  EmitToStreamer(*OutStreamer, TmpInst);
702  return;
703  }
704  case PPC::LDtocL: {
705  // Transform %Xd = LDtocL <ga:@sym>, %Xs
706  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
707 
708  // Change the opcode to LD. If the global address is external, has
709  // common linkage, or is a jump table address, then reference the
710  // associated TOC entry. Otherwise reference the symbol directly.
711  TmpInst.setOpcode(PPC::LD);
712  const MachineOperand &MO = MI->getOperand(1);
713  assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
714  MO.isBlockAddress()) &&
715  "Invalid operand for LDtocL!");
716  MCSymbol *MOSymbol = nullptr;
717 
718  if (MO.isJTI())
719  MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
720  else if (MO.isBlockAddress()) {
721  MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
722  MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
723  }
724  else if (MO.isCPI()) {
725  MOSymbol = GetCPISymbol(MO.getIndex());
726  if (TM.getCodeModel() == CodeModel::Large)
727  MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
728  }
729  else if (MO.isGlobal()) {
730  const GlobalValue *GValue = MO.getGlobal();
731  MOSymbol = getSymbol(GValue);
732  if (GValue->getType()->getElementType()->isFunctionTy() ||
733  GValue->isDeclaration() || GValue->hasCommonLinkage() ||
734  GValue->hasAvailableExternallyLinkage() ||
735  TM.getCodeModel() == CodeModel::Large)
736  MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
737  }
738 
739  const MCExpr *Exp =
741  OutContext);
742  TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
743  EmitToStreamer(*OutStreamer, TmpInst);
744  return;
745  }
746  case PPC::ADDItocL: {
747  // Transform %Xd = ADDItocL %Xs, <ga:@sym>
748  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
749 
750  // Change the opcode to ADDI8. If the global address is external, then
751  // generate a TOC entry and reference that. Otherwise reference the
752  // symbol directly.
753  TmpInst.setOpcode(PPC::ADDI8);
754  const MachineOperand &MO = MI->getOperand(2);
755  assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
756  MCSymbol *MOSymbol = nullptr;
757  bool IsExternal = false;
758  bool IsNonLocalFunction = false;
759 
760  if (MO.isGlobal()) {
761  const GlobalValue *GV = MO.getGlobal();
762  MOSymbol = getSymbol(GV);
763  IsExternal = GV->isDeclaration();
764  IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() &&
766  } else if (MO.isCPI())
767  MOSymbol = GetCPISymbol(MO.getIndex());
768 
769  if (IsNonLocalFunction || IsExternal ||
770  TM.getCodeModel() == CodeModel::Large)
771  MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
772 
773  const MCExpr *Exp =
775  OutContext);
776  TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
777  EmitToStreamer(*OutStreamer, TmpInst);
778  return;
779  }
780  case PPC::ADDISgotTprelHA: {
781  // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym>
782  // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
783  assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
784  const MachineOperand &MO = MI->getOperand(2);
785  const GlobalValue *GValue = MO.getGlobal();
786  MCSymbol *MOSymbol = getSymbol(GValue);
787  const MCExpr *SymGotTprel =
789  OutContext);
790  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
791  .addReg(MI->getOperand(0).getReg())
792  .addReg(MI->getOperand(1).getReg())
793  .addExpr(SymGotTprel));
794  return;
795  }
796  case PPC::LDgotTprelL:
797  case PPC::LDgotTprelL32: {
798  // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
799  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
800 
801  // Change the opcode to LD.
802  TmpInst.setOpcode(isPPC64 ? PPC::LD : PPC::LWZ);
803  const MachineOperand &MO = MI->getOperand(1);
804  const GlobalValue *GValue = MO.getGlobal();
805  MCSymbol *MOSymbol = getSymbol(GValue);
806  const MCExpr *Exp =
808  OutContext);
809  TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
810  EmitToStreamer(*OutStreamer, TmpInst);
811  return;
812  }
813 
814  case PPC::PPC32PICGOT: {
815  MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
816  MCSymbol *GOTRef = OutContext.createTempSymbol();
817  MCSymbol *NextInstr = OutContext.createTempSymbol();
818 
819  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
820  // FIXME: We would like an efficient form for this, so we don't have to do
821  // a lot of extra uniquing.
822  .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
823  const MCExpr *OffsExpr =
824  MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
825  MCSymbolRefExpr::create(GOTRef, OutContext),
826  OutContext);
827  OutStreamer->EmitLabel(GOTRef);
828  OutStreamer->EmitValue(OffsExpr, 4);
829  OutStreamer->EmitLabel(NextInstr);
830  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
831  .addReg(MI->getOperand(0).getReg()));
832  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
833  .addReg(MI->getOperand(1).getReg())
834  .addImm(0)
835  .addReg(MI->getOperand(0).getReg()));
836  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
837  .addReg(MI->getOperand(0).getReg())
838  .addReg(MI->getOperand(1).getReg())
839  .addReg(MI->getOperand(0).getReg()));
840  return;
841  }
842  case PPC::PPC32GOT: {
843  MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
844  const MCExpr *SymGotTlsL =
846  OutContext);
847  const MCExpr *SymGotTlsHA =
849  OutContext);
850  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
851  .addReg(MI->getOperand(0).getReg())
852  .addExpr(SymGotTlsL));
853  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
854  .addReg(MI->getOperand(0).getReg())
855  .addReg(MI->getOperand(0).getReg())
856  .addExpr(SymGotTlsHA));
857  return;
858  }
859  case PPC::ADDIStlsgdHA: {
860  // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym>
861  // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
862  assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
863  const MachineOperand &MO = MI->getOperand(2);
864  const GlobalValue *GValue = MO.getGlobal();
865  MCSymbol *MOSymbol = getSymbol(GValue);
866  const MCExpr *SymGotTlsGD =
868  OutContext);
869  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
870  .addReg(MI->getOperand(0).getReg())
871  .addReg(MI->getOperand(1).getReg())
872  .addExpr(SymGotTlsGD));
873  return;
874  }
875  case PPC::ADDItlsgdL:
876  // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym>
877  // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l
878  case PPC::ADDItlsgdL32: {
879  // Transform: %Rd = ADDItlsgdL32 %Rs, <ga:@sym>
880  // Into: %Rd = ADDI %Rs, sym@got@tlsgd
881  const MachineOperand &MO = MI->getOperand(2);
882  const GlobalValue *GValue = MO.getGlobal();
883  MCSymbol *MOSymbol = getSymbol(GValue);
884  const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
885  MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
887  OutContext);
888  EmitToStreamer(*OutStreamer,
889  MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
890  .addReg(MI->getOperand(0).getReg())
891  .addReg(MI->getOperand(1).getReg())
892  .addExpr(SymGotTlsGD));
893  return;
894  }
895  case PPC::GETtlsADDR:
896  // Transform: %X3 = GETtlsADDR %X3, <ga:@sym>
897  // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
898  case PPC::GETtlsADDR32: {
899  // Transform: %R3 = GETtlsADDR32 %R3, <ga:@sym>
900  // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
901  EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
902  return;
903  }
904  case PPC::ADDIStlsldHA: {
905  // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym>
906  // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha
907  assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
908  const MachineOperand &MO = MI->getOperand(2);
909  const GlobalValue *GValue = MO.getGlobal();
910  MCSymbol *MOSymbol = getSymbol(GValue);
911  const MCExpr *SymGotTlsLD =
913  OutContext);
914  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
915  .addReg(MI->getOperand(0).getReg())
916  .addReg(MI->getOperand(1).getReg())
917  .addExpr(SymGotTlsLD));
918  return;
919  }
920  case PPC::ADDItlsldL:
921  // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym>
922  // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l
923  case PPC::ADDItlsldL32: {
924  // Transform: %Rd = ADDItlsldL32 %Rs, <ga:@sym>
925  // Into: %Rd = ADDI %Rs, sym@got@tlsld
926  const MachineOperand &MO = MI->getOperand(2);
927  const GlobalValue *GValue = MO.getGlobal();
928  MCSymbol *MOSymbol = getSymbol(GValue);
929  const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
930  MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
932  OutContext);
933  EmitToStreamer(*OutStreamer,
934  MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
935  .addReg(MI->getOperand(0).getReg())
936  .addReg(MI->getOperand(1).getReg())
937  .addExpr(SymGotTlsLD));
938  return;
939  }
940  case PPC::GETtlsldADDR:
941  // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym>
942  // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
943  case PPC::GETtlsldADDR32: {
944  // Transform: %R3 = GETtlsldADDR32 %R3, <ga:@sym>
945  // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
946  EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
947  return;
948  }
949  case PPC::ADDISdtprelHA:
950  // Transform: %Xd = ADDISdtprelHA %X3, <ga:@sym>
951  // Into: %Xd = ADDIS8 %X3, sym@dtprel@ha
952  case PPC::ADDISdtprelHA32: {
953  // Transform: %Rd = ADDISdtprelHA32 %R3, <ga:@sym>
954  // Into: %Rd = ADDIS %R3, sym@dtprel@ha
955  const MachineOperand &MO = MI->getOperand(2);
956  const GlobalValue *GValue = MO.getGlobal();
957  MCSymbol *MOSymbol = getSymbol(GValue);
958  const MCExpr *SymDtprel =
960  OutContext);
961  EmitToStreamer(
962  *OutStreamer,
963  MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS)
964  .addReg(MI->getOperand(0).getReg())
965  .addReg(Subtarget->isPPC64() ? PPC::X3 : PPC::R3)
966  .addExpr(SymDtprel));
967  return;
968  }
969  case PPC::ADDIdtprelL:
970  // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym>
971  // Into: %Xd = ADDI8 %Xs, sym@dtprel@l
972  case PPC::ADDIdtprelL32: {
973  // Transform: %Rd = ADDIdtprelL32 %Rs, <ga:@sym>
974  // Into: %Rd = ADDI %Rs, sym@dtprel@l
975  const MachineOperand &MO = MI->getOperand(2);
976  const GlobalValue *GValue = MO.getGlobal();
977  MCSymbol *MOSymbol = getSymbol(GValue);
978  const MCExpr *SymDtprel =
980  OutContext);
981  EmitToStreamer(*OutStreamer,
982  MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
983  .addReg(MI->getOperand(0).getReg())
984  .addReg(MI->getOperand(1).getReg())
985  .addExpr(SymDtprel));
986  return;
987  }
988  case PPC::MFOCRF:
989  case PPC::MFOCRF8:
990  if (!Subtarget->hasMFOCRF()) {
991  // Transform: %R3 = MFOCRF %CR7
992  // Into: %R3 = MFCR ;; cr7
993  unsigned NewOpcode =
994  MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
995  OutStreamer->AddComment(PPCInstPrinter::
996  getRegisterName(MI->getOperand(1).getReg()));
997  EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
998  .addReg(MI->getOperand(0).getReg()));
999  return;
1000  }
1001  break;
1002  case PPC::MTOCRF:
1003  case PPC::MTOCRF8:
1004  if (!Subtarget->hasMFOCRF()) {
1005  // Transform: %CR7 = MTOCRF %R3
1006  // Into: MTCRF mask, %R3 ;; cr7
1007  unsigned NewOpcode =
1008  MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
1009  unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
1010  ->getEncodingValue(MI->getOperand(0).getReg());
1011  OutStreamer->AddComment(PPCInstPrinter::
1012  getRegisterName(MI->getOperand(0).getReg()));
1013  EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1014  .addImm(Mask)
1015  .addReg(MI->getOperand(1).getReg()));
1016  return;
1017  }
1018  break;
1019  case PPC::LD:
1020  case PPC::STD:
1021  case PPC::LWA_32:
1022  case PPC::LWA: {
1023  // Verify alignment is legal, so we don't create relocations
1024  // that can't be supported.
1025  // FIXME: This test is currently disabled for Darwin. The test
1026  // suite shows a handful of test cases that fail this check for
1027  // Darwin. Those need to be investigated before this sanity test
1028  // can be enabled for those subtargets.
1029  if (!Subtarget->isDarwin()) {
1030  unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1031  const MachineOperand &MO = MI->getOperand(OpNum);
1032  if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4)
1033  llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
1034  }
1035  // Now process the instruction normally.
1036  break;
1037  }
1038  }
1039 
1040  LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
1041  EmitToStreamer(*OutStreamer, TmpInst);
1042 }
1043 
1044 void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) {
1045  if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1047  static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1048 
1049  if (TS)
1050  TS->emitAbiVersion(2);
1051  }
1052 
1053  if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1054  TM.getRelocationModel() != Reloc::PIC_)
1056 
1057  if (M.getPICLevel() == PICLevel::Small)
1059 
1060  OutStreamer->SwitchSection(OutContext.getELFSection(
1062 
1063  MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1064  MCSymbol *CurrentPos = OutContext.createTempSymbol();
1065 
1066  OutStreamer->EmitLabel(CurrentPos);
1067 
1068  // The GOT pointer points to the middle of the GOT, in order to reference the
1069  // entire 64kB range. 0x8000 is the midpoint.
1070  const MCExpr *tocExpr =
1071  MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
1072  MCConstantExpr::create(0x8000, OutContext),
1073  OutContext);
1074 
1075  OutStreamer->EmitAssignment(TOCSym, tocExpr);
1076 
1077  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1078 }
1079 
1080 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
1081  // linux/ppc32 - Normal entry label.
1082  if (!Subtarget->isPPC64() &&
1083  (TM.getRelocationModel() != Reloc::PIC_ ||
1084  MF->getFunction()->getParent()->getPICLevel() == PICLevel::Small))
1086 
1087  if (!Subtarget->isPPC64()) {
1088  const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1089  if (PPCFI->usesPICBase()) {
1090  MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol();
1091  MCSymbol *PICBase = MF->getPICBaseSymbol();
1092  OutStreamer->EmitLabel(RelocSymbol);
1093 
1094  const MCExpr *OffsExpr =
1096  MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1097  OutContext),
1098  MCSymbolRefExpr::create(PICBase, OutContext),
1099  OutContext);
1100  OutStreamer->EmitValue(OffsExpr, 4);
1101  OutStreamer->EmitLabel(CurrentFnSym);
1102  return;
1103  } else
1105  }
1106 
1107  // ELFv2 ABI - Normal entry label.
1108  if (Subtarget->isELFv2ABI())
1110 
1111  // Emit an official procedure descriptor.
1112  MCSectionSubPair Current = OutStreamer->getCurrentSection();
1113  MCSectionELF *Section = OutStreamer->getContext().getELFSection(
1115  OutStreamer->SwitchSection(Section);
1116  OutStreamer->EmitLabel(CurrentFnSym);
1117  OutStreamer->EmitValueToAlignment(8);
1118  MCSymbol *Symbol1 = CurrentFnSymForSize;
1119  // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
1120  // entry point.
1121  OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
1122  8 /*size*/);
1123  MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1124  // Generates a R_PPC64_TOC relocation for TOC base insertion.
1125  OutStreamer->EmitValue(
1127  8/*size*/);
1128  // Emit a null environment pointer.
1129  OutStreamer->EmitIntValue(0, 8 /* size */);
1130  OutStreamer->SwitchSection(Current.first, Current.second);
1131 }
1132 
1133 
1134 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
1135  const DataLayout *TD = TM.getDataLayout();
1136 
1137  bool isPPC64 = TD->getPointerSizeInBits() == 64;
1138 
1139  PPCTargetStreamer &TS =
1140  static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
1141 
1142  if (!TOC.empty()) {
1144 
1145  if (isPPC64)
1146  Section = OutStreamer->getContext().getELFSection(
1148  else
1149  Section = OutStreamer->getContext().getELFSection(
1151  OutStreamer->SwitchSection(Section);
1152 
1153  for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
1154  E = TOC.end(); I != E; ++I) {
1155  OutStreamer->EmitLabel(I->second);
1156  MCSymbol *S = I->first;
1157  if (isPPC64)
1158  TS.emitTCEntry(*S);
1159  else
1160  OutStreamer->EmitSymbolValue(S, 4);
1161  }
1162  }
1163 
1164  return AsmPrinter::doFinalization(M);
1165 }
1166 
1167 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
1168 void PPCLinuxAsmPrinter::EmitFunctionBodyStart() {
1169  // In the ELFv2 ABI, in functions that use the TOC register, we need to
1170  // provide two entry points. The ABI guarantees that when calling the
1171  // local entry point, r2 is set up by the caller to contain the TOC base
1172  // for this function, and when calling the global entry point, r12 is set
1173  // up by the caller to hold the address of the global entry point. We
1174  // thus emit a prefix sequence along the following lines:
1175  //
1176  // func:
1177  // # global entry point
1178  // addis r2,r12,(.TOC.-func)@ha
1179  // addi r2,r2,(.TOC.-func)@l
1180  // .localentry func, .-func
1181  // # local entry point, followed by function body
1182  //
1183  // This ensures we have r2 set up correctly while executing the function
1184  // body, no matter which entry point is called.
1185  if (Subtarget->isELFv2ABI()
1186  // Only do all that if the function uses r2 in the first place.
1187  && !MF->getRegInfo().use_empty(PPC::X2)) {
1188 
1189  MCSymbol *GlobalEntryLabel = OutContext.createTempSymbol();
1190  OutStreamer->EmitLabel(GlobalEntryLabel);
1191  const MCSymbolRefExpr *GlobalEntryLabelExp =
1192  MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
1193 
1194  MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1195  const MCExpr *TOCDeltaExpr =
1196  MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1197  GlobalEntryLabelExp, OutContext);
1198 
1199  const MCExpr *TOCDeltaHi =
1200  PPCMCExpr::createHa(TOCDeltaExpr, false, OutContext);
1201  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1202  .addReg(PPC::X2)
1203  .addReg(PPC::X12)
1204  .addExpr(TOCDeltaHi));
1205 
1206  const MCExpr *TOCDeltaLo =
1207  PPCMCExpr::createLo(TOCDeltaExpr, false, OutContext);
1208  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
1209  .addReg(PPC::X2)
1210  .addReg(PPC::X2)
1211  .addExpr(TOCDeltaLo));
1212 
1213  MCSymbol *LocalEntryLabel = OutContext.createTempSymbol();
1214  OutStreamer->EmitLabel(LocalEntryLabel);
1215  const MCSymbolRefExpr *LocalEntryLabelExp =
1216  MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
1217  const MCExpr *LocalOffsetExp =
1218  MCBinaryExpr::createSub(LocalEntryLabelExp,
1219  GlobalEntryLabelExp, OutContext);
1220 
1221  PPCTargetStreamer *TS =
1222  static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1223 
1224  if (TS)
1225  TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
1226  }
1227 }
1228 
1229 /// EmitFunctionBodyEnd - Print the traceback table before the .size
1230 /// directive.
1231 ///
1232 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
1233  // Only the 64-bit target requires a traceback table. For now,
1234  // we only emit the word of zeroes that GDB requires to find
1235  // the end of the function, and zeroes for the eight-byte
1236  // mandatory fields.
1237  // FIXME: We should fill in the eight-byte mandatory fields as described in
1238  // the PPC64 ELF ABI (this is a low-priority item because GDB does not
1239  // currently make use of these fields).
1240  if (Subtarget->isPPC64()) {
1241  OutStreamer->EmitIntValue(0, 4/*size*/);
1242  OutStreamer->EmitIntValue(0, 8/*size*/);
1243  }
1244 }
1245 
1246 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
1247  static const char *const CPUDirectives[] = {
1248  "",
1249  "ppc",
1250  "ppc440",
1251  "ppc601",
1252  "ppc602",
1253  "ppc603",
1254  "ppc7400",
1255  "ppc750",
1256  "ppc970",
1257  "ppcA2",
1258  "ppce500mc",
1259  "ppce5500",
1260  "power3",
1261  "power4",
1262  "power5",
1263  "power5x",
1264  "power6",
1265  "power6x",
1266  "power7",
1267  "ppc64",
1268  "ppc64le"
1269  };
1270 
1271  // Get the numerically largest directive.
1272  // FIXME: How should we merge darwin directives?
1273  unsigned Directive = PPC::DIR_NONE;
1274  for (const Function &F : M) {
1275  const PPCSubtarget &STI = TM.getSubtarget<PPCSubtarget>(F);
1276  unsigned FDir = STI.getDarwinDirective();
1277  Directive = Directive > FDir ? FDir : STI.getDarwinDirective();
1278  if (STI.hasMFOCRF() && Directive < PPC::DIR_970)
1279  Directive = PPC::DIR_970;
1280  if (STI.hasAltivec() && Directive < PPC::DIR_7400)
1281  Directive = PPC::DIR_7400;
1282  if (STI.isPPC64() && Directive < PPC::DIR_64)
1283  Directive = PPC::DIR_64;
1284  }
1285 
1286  assert(Directive <= PPC::DIR_64 && "Directive out of range.");
1287 
1288  assert(Directive < array_lengthof(CPUDirectives) &&
1289  "CPUDirectives[] might not be up-to-date!");
1290  PPCTargetStreamer &TStreamer =
1291  *static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1292  TStreamer.emitMachine(CPUDirectives[Directive]);
1293 
1294  // Prime text sections so they are adjacent. This reduces the likelihood a
1295  // large data or debug section causes a branch to exceed 16M limit.
1296  const TargetLoweringObjectFileMachO &TLOFMacho =
1297  static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1298  OutStreamer->SwitchSection(TLOFMacho.getTextCoalSection());
1299  if (TM.getRelocationModel() == Reloc::PIC_) {
1300  OutStreamer->SwitchSection(
1301  OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
1304  32, SectionKind::getText()));
1305  } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
1306  OutStreamer->SwitchSection(
1307  OutContext.getMachOSection("__TEXT","__symbol_stub1",
1310  16, SectionKind::getText()));
1311  }
1312  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1313 }
1314 
1315 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
1316  // Remove $stub suffix, add $lazy_ptr.
1317  StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
1318  return Ctx.getOrCreateSymbol(NoStub + "$lazy_ptr");
1319 }
1320 
1321 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
1322  // Add $tmp suffix to $stub, yielding $stub$tmp.
1323  return Ctx.getOrCreateSymbol(Sym->getName() + "$tmp");
1324 }
1325 
1326 void PPCDarwinAsmPrinter::
1327 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
1328  bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
1329 
1330  // Construct a local MCSubtargetInfo and shadow EmitToStreamer here.
1331  // This is because the MachineFunction won't exist (but have not yet been
1332  // freed) and since we're at the global level we can use the default
1333  // constructed subtarget.
1334  std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
1335  TM.getTargetTriple().str(), TM.getTargetCPU(),
1336  TM.getTargetFeatureString()));
1337  auto EmitToStreamer = [&STI] (MCStreamer &S, const MCInst &Inst) {
1338  S.EmitInstruction(Inst, *STI);
1339  };
1340 
1341  const TargetLoweringObjectFileMachO &TLOFMacho =
1342  static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1343 
1344  // .lazy_symbol_pointer
1345  MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
1346 
1347  // Output stubs for dynamically-linked functions
1348  if (TM.getRelocationModel() == Reloc::PIC_) {
1349  MCSection *StubSection = OutContext.getMachOSection(
1350  "__TEXT", "__picsymbolstub1",
1353  for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1354  OutStreamer->SwitchSection(StubSection);
1355  EmitAlignment(4);
1356 
1357  MCSymbol *Stub = Stubs[i].first;
1358  MCSymbol *RawSym = Stubs[i].second.getPointer();
1359  MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
1360  MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
1361 
1362  OutStreamer->EmitLabel(Stub);
1363  OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
1364 
1365  const MCExpr *Anon = MCSymbolRefExpr::create(AnonSymbol, OutContext);
1366  const MCExpr *LazyPtrExpr = MCSymbolRefExpr::create(LazyPtr, OutContext);
1367  const MCExpr *Sub =
1368  MCBinaryExpr::createSub(LazyPtrExpr, Anon, OutContext);
1369 
1370  // mflr r0
1371  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
1372  // bcl 20, 31, AnonSymbol
1373  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCLalways).addExpr(Anon));
1374  OutStreamer->EmitLabel(AnonSymbol);
1375  // mflr r11
1376  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
1377  // addis r11, r11, ha16(LazyPtr - AnonSymbol)
1378  const MCExpr *SubHa16 = PPCMCExpr::createHa(Sub, true, OutContext);
1379  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1380  .addReg(PPC::R11)
1381  .addReg(PPC::R11)
1382  .addExpr(SubHa16));
1383  // mtlr r0
1384  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
1385 
1386  // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
1387  // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
1388  const MCExpr *SubLo16 = PPCMCExpr::createLo(Sub, true, OutContext);
1389  EmitToStreamer(*OutStreamer, MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
1390  .addReg(PPC::R12)
1391  .addExpr(SubLo16).addExpr(SubLo16)
1392  .addReg(PPC::R11));
1393  // mtctr r12
1394  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
1395  // bctr
1396  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTR));
1397 
1398  OutStreamer->SwitchSection(LSPSection);
1399  OutStreamer->EmitLabel(LazyPtr);
1400  OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
1401 
1402  MCSymbol *DyldStubBindingHelper =
1403  OutContext.getOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
1404  if (isPPC64) {
1405  // .quad dyld_stub_binding_helper
1406  OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 8);
1407  } else {
1408  // .long dyld_stub_binding_helper
1409  OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 4);
1410  }
1411  }
1412  OutStreamer->AddBlankLine();
1413  return;
1414  }
1415 
1416  MCSection *StubSection = OutContext.getMachOSection(
1417  "__TEXT", "__symbol_stub1",
1420  for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1421  MCSymbol *Stub = Stubs[i].first;
1422  MCSymbol *RawSym = Stubs[i].second.getPointer();
1423  MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
1424  const MCExpr *LazyPtrExpr = MCSymbolRefExpr::create(LazyPtr, OutContext);
1425 
1426  OutStreamer->SwitchSection(StubSection);
1427  EmitAlignment(4);
1428  OutStreamer->EmitLabel(Stub);
1429  OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
1430 
1431  // lis r11, ha16(LazyPtr)
1432  const MCExpr *LazyPtrHa16 =
1433  PPCMCExpr::createHa(LazyPtrExpr, true, OutContext);
1434  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LIS)
1435  .addReg(PPC::R11)
1436  .addExpr(LazyPtrHa16));
1437 
1438  // ldu r12, lo16(LazyPtr)(r11)
1439  // lwzu r12, lo16(LazyPtr)(r11)
1440  const MCExpr *LazyPtrLo16 =
1441  PPCMCExpr::createLo(LazyPtrExpr, true, OutContext);
1442  EmitToStreamer(*OutStreamer, MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
1443  .addReg(PPC::R12)
1444  .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
1445  .addReg(PPC::R11));
1446 
1447  // mtctr r12
1448  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
1449  // bctr
1450  EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTR));
1451 
1452  OutStreamer->SwitchSection(LSPSection);
1453  OutStreamer->EmitLabel(LazyPtr);
1454  OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
1455 
1456  MCSymbol *DyldStubBindingHelper =
1457  OutContext.getOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
1458  if (isPPC64) {
1459  // .quad dyld_stub_binding_helper
1460  OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 8);
1461  } else {
1462  // .long dyld_stub_binding_helper
1463  OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 4);
1464  }
1465  }
1466 
1467  OutStreamer->AddBlankLine();
1468 }
1469 
1470 
1471 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1472  bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
1473 
1474  // Darwin/PPC always uses mach-o.
1475  const TargetLoweringObjectFileMachO &TLOFMacho =
1476  static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1477  MachineModuleInfoMachO &MMIMacho =
1478  MMI->getObjFileInfo<MachineModuleInfoMachO>();
1479 
1481  if (!Stubs.empty())
1482  EmitFunctionStubs(Stubs);
1483 
1484  if (MAI->doesSupportExceptionHandling() && MMI) {
1485  // Add the (possibly multiple) personalities to the set of global values.
1486  // Only referenced functions get into the Personalities list.
1487  const std::vector<const Function*> &Personalities = MMI->getPersonalities();
1488  for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
1489  E = Personalities.end(); I != E; ++I) {
1490  if (*I) {
1491  MCSymbol *NLPSym = getSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
1493  MMIMacho.getGVStubEntry(NLPSym);
1494  StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(*I), true);
1495  }
1496  }
1497  }
1498 
1499  // Output stubs for dynamically-linked functions.
1500  Stubs = MMIMacho.GetGVStubList();
1501 
1502  // Output macho stubs for external and common global variables.
1503  if (!Stubs.empty()) {
1504  // Switch with ".non_lazy_symbol_pointer" directive.
1505  OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1506  EmitAlignment(isPPC64 ? 3 : 2);
1507 
1508  for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1509  // L_foo$stub:
1510  OutStreamer->EmitLabel(Stubs[i].first);
1511  // .indirect_symbol _foo
1512  MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1513  OutStreamer->EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
1514 
1515  if (MCSym.getInt())
1516  // External to current translation unit.
1517  OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
1518  else
1519  // Internal to current translation unit.
1520  //
1521  // When we place the LSDA into the TEXT section, the type info pointers
1522  // need to be indirect and pc-rel. We accomplish this by using NLPs.
1523  // However, sometimes the types are local to the file. So we need to
1524  // fill in the value for the NLP in those cases.
1525  OutStreamer->EmitValue(MCSymbolRefExpr::create(MCSym.getPointer(),
1526  OutContext),
1527  isPPC64 ? 8 : 4/*size*/);
1528  }
1529 
1530  Stubs.clear();
1531  OutStreamer->AddBlankLine();
1532  }
1533 
1534  Stubs = MMIMacho.GetHiddenGVStubList();
1535  if (!Stubs.empty()) {
1536  OutStreamer->SwitchSection(getObjFileLowering().getDataSection());
1537  EmitAlignment(isPPC64 ? 3 : 2);
1538 
1539  for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1540  // L_foo$stub:
1541  OutStreamer->EmitLabel(Stubs[i].first);
1542  // .long _foo
1543  OutStreamer->EmitValue(MCSymbolRefExpr::
1544  create(Stubs[i].second.getPointer(),
1545  OutContext),
1546  isPPC64 ? 8 : 4/*size*/);
1547  }
1548 
1549  Stubs.clear();
1550  OutStreamer->AddBlankLine();
1551  }
1552 
1553  // Funny Darwin hack: This flag tells the linker that no global symbols
1554  // contain code that falls through to other global symbols (e.g. the obvious
1555  // implementation of multiple entry points). If this doesn't occur, the
1556  // linker can safely perform dead code stripping. Since LLVM never generates
1557  // code that does this, it is always safe to set.
1559 
1560  return AsmPrinter::doFinalization(M);
1561 }
1562 
1563 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1564 /// for a MachineFunction to the given output stream, in a format that the
1565 /// Darwin assembler can deal with.
1566 ///
1567 static AsmPrinter *
1569  std::unique_ptr<MCStreamer> &&Streamer) {
1570  if (tm.getTargetTriple().isMacOSX())
1571  return new PPCDarwinAsmPrinter(tm, std::move(Streamer));
1572  return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
1573 }
1574 
1575 // Force static initialization.
1580 }
MCSection * getNonLazySymbolPointerSection() const
VectorType::iterator iterator
Definition: MapVector.h:39
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag)
Note in the output the specified Flag.
Definition: MCStreamer.cpp:666
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
virtual void EmitStartOfAsmFile(Module &)
This virtual method can be overridden by targets that want to emit something at the start of their fi...
Definition: AsmPrinter.h:283
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
virtual void AddComment(const Twine &T)
Add a textual command.
Definition: MCStreamer.h:252
const GlobalValue * getGlobal() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:51
bool hasMFOCRF() const
Definition: PPCSubtarget.h:228
void EmitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
Definition: MCStreamer.cpp:115
MachineBasicBlock * getMBB() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
virtual void AddBlankLine()
AddBlankLine - Emit a blank line to a .s file to pretty it up.
Definition: MCStreamer.h:266
bool doFinalization(Module &M) override
Shut down the asmprinter.
MCSection * getLazySymbolPointerSection() const
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:129
MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:311
A Stackmap instruction captures the location of live variables at its position in the instruction str...
const char * getPrivateGlobalPrefix() const
Definition: DataLayout.h:281
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
static MCSymbol * GetAnonSym(MCSymbol *Sym, MCContext &Ctx)
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:261
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:32
MachineBasicBlock reference.
F(f)
std::vector< std::pair< MCSymbol *, StubValueTy > > SymbolListTy
bool hasAltivec() const
Definition: PPCSubtarget.h:221
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Definition: MCStreamer.cpp:639
const Triple & getTargetTriple() const
bool isMacOSX() const
isMacOSX - Is this a Mac OS X triple.
Definition: Triple.h:394
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
bool hasCommonLinkage() const
Definition: GlobalValue.h:282
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
PPCFunctionInfo - This class is derived from MachineFunction private PowerPC target-specific informat...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
static MCSymbol * GetLazyPtr(MCSymbol *Sym, MCContext &Ctx)
bool hasInternalLinkage() const
Definition: GlobalValue.h:278
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:274
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:159
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:353
MCContext & getContext() const
Definition: MCStreamer.h:210
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, uint64_t Operand, uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor)
Print Opcode's operand number OperandIdx which has value Operand.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
Definition: MCStreamer.cpp:596
Context object for machine code objects.
Definition: MCContext.h:48
static std::string getRegisterName(const TargetRegisterInfo *TRI, unsigned Reg)
Definition: MIParser.cpp:236
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:201
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:514
R32 = MFOCRF(CRREG, INFLAG) - Represents the MFOCRF instruction.
virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset)=0
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:44
int64_t getImm() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:446
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:79
Type * getElementType() const
Definition: DerivedTypes.h:323
SymbolListTy GetFnStubList()
Accessor methods to return the set of stubs in sorted order.
MCInstBuilder & addExpr(const MCExpr *Val)
Add a new MCExpr operand.
Definition: MCInstBuilder.h:50
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:267
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:120
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:188
LLVM_CONSTEXPR size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:247
DBG_VALUE - a mapping of the llvm.dbg.value intrinsic.
Definition: TargetOpcodes.h:69
Address of a global value.
Streaming machine code generation interface.
Definition: MCStreamer.h:157
MCInstBuilder & addReg(unsigned Reg)
Add a new register operand.
Definition: MCInstBuilder.h:32
Patchable call instruction - this instruction represents a call to a constant address, followed by a series of NOPs.
MCTargetStreamer * getTargetStreamer()
Definition: MCStreamer.h:212
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
PointerIntPair - This class implements a pair of a pointer and small integer.
void LLVMInitializePowerPCAsmPrinter()
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:701
bool hasHiddenVisibility() const
Definition: GlobalValue.h:141
CHAIN,FLAG = MTCTR(VAL, CHAIN[, INFLAG]) - Directly corresponds to a MTCTR instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
virtual void emitAbiVersion(int AbiVersion)=0
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
static const char * getRegisterName(unsigned RegNo)
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:688
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
Address of a basic block.
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:49
int64_t getOffset() const
Return the offset from the symbol in this operand.
MI-level patchpoint operands.
Definition: StackMaps.h:40
static const PPCMCExpr * createLo(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:49
MCSection * getTextCoalSection() const
IntType getInt() const
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
void recordPatchPoint(const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
Definition: StackMaps.cpp:350
static AsmPrinter * createPPCAsmPrinterPass(TargetMachine &tm, std::unique_ptr< MCStreamer > &&Streamer)
createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code for a MachineFunction to t...
PointerTy getPointer() const
void setOpcode(unsigned Op)
Definition: MCInst.h:158
PICLevel::Level getPICLevel() const
Returns the PIC level (small or large model)
Definition: Module.cpp:471
bool isFunctionTy() const
isFunctionTy - True if this is an instance of FunctionType.
Definition: Type.h:205
virtual void EmitLabel(MCSymbol *Symbol)
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:203
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
Target ThePPC32Target
Target ThePPC64LETarget
void recordStackMap(const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Definition: StackMaps.cpp:342
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bundle_iterator< const MachineInstr, const_instr_iterator > const_iterator
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:111
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:329
MCSectionELF - This represents a section on linux, lots of unix variants and some bare metal systems...
Definition: MCSectionELF.h:30
virtual void EmitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
Definition: AsmPrinter.cpp:588
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
void EmitValue(const MCExpr *Value, unsigned Size, const SMLoc &Loc=SMLoc())
Definition: MCStreamer.cpp:110
bool isPPC64() const
isPPC64 - Return true if we are generating code for 64-bit pointer mode.
unsigned getReg() const
getReg - Returns the register number.
unsigned getAlignment() const
Definition: Globals.cpp:63
const ARM::ArchExtKind Kind
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
virtual void emitTCEntry(const MCSymbol &S)=0
static const PPCMCExpr * createHa(const MCExpr *Expr, bool isDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:59
unsigned getDarwinDirective() const
getDarwinDirective - Returns the -m directive specified for the cpu.
Definition: PPCSubtarget.h:152
Target ThePPC64Target
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
static TraceState * TS
Primary interface to the complete machine description for the target machine.
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP, bool isDarwin)
static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn)
RegisterAsmPrinter - Register an AsmPrinter implementation for the given target.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
const BlockAddress * getBlockAddress() const
Address of indexed Constant in Constant Pool.
virtual void emitMachine(StringRef CPU)=0
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
MCSymbol * getPICOffsetSymbol() const
MachineModuleInfoImpl - This class can be derived from and used by targets to hold private target-spe...
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164
static const char * stripRegisterPrefix(const char *RegName)
stripRegisterPrefix - This method strips the character prefix from a register name so that only the n...
static SectionKind getText()
Definition: SectionKind.h:207