LLVM  10.0.0
X86AsmPrinter.cpp
Go to the documentation of this file.
1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to X86 machine code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "X86AsmPrinter.h"
19 #include "X86InstrInfo.h"
20 #include "X86MachineFunctionInfo.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/BinaryFormat/ELF.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/InlineAsm.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/MC/MCCodeEmitter.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCExpr.h"
34 #include "llvm/MC/MCSectionCOFF.h"
35 #include "llvm/MC/MCSectionELF.h"
36 #include "llvm/MC/MCSectionMachO.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Support/Debug.h"
43 using namespace llvm;
44 
46  std::unique_ptr<MCStreamer> Streamer)
47  : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {}
48 
49 //===----------------------------------------------------------------------===//
50 // Primitive Helper Functions.
51 //===----------------------------------------------------------------------===//
52 
53 /// runOnMachineFunction - Emit the function body.
54 ///
56  Subtarget = &MF.getSubtarget<X86Subtarget>();
57 
58  SMShadowTracker.startFunction(MF);
59  CodeEmitter.reset(TM.getTarget().createMCCodeEmitter(
60  *Subtarget->getInstrInfo(), *Subtarget->getRegisterInfo(),
61  MF.getContext()));
62 
63  EmitFPOData =
64  Subtarget->isTargetWin32() && MF.getMMI().getModule()->getCodeViewFlag();
65 
67 
68  if (Subtarget->isTargetCOFF()) {
69  bool Local = MF.getFunction().hasLocalLinkage();
70  OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
71  OutStreamer->EmitCOFFSymbolStorageClass(
75  OutStreamer->EndCOFFSymbolDef();
76  }
77 
78  // Emit the rest of the function body.
80 
81  // Emit the XRay table for this function.
82  emitXRayTable();
83 
84  EmitFPOData = false;
85 
86  // We didn't modify anything.
87  return false;
88 }
89 
91  if (EmitFPOData) {
92  if (auto *XTS =
93  static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()))
94  XTS->emitFPOProc(
97  }
98 }
99 
101  if (EmitFPOData) {
102  if (auto *XTS =
103  static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()))
104  XTS->emitFPOEndProc();
105  }
106 }
107 
108 /// PrintSymbolOperand - Print a raw symbol reference operand. This handles
109 /// jump tables, constant pools, global address and external symbols, all of
110 /// which print to a label with various suffixes for relocation types etc.
111 void X86AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
112  raw_ostream &O) {
113  switch (MO.getType()) {
114  default: llvm_unreachable("unknown symbol type!");
116  GetCPISymbol(MO.getIndex())->print(O, MAI);
117  printOffset(MO.getOffset(), O);
118  break;
120  const GlobalValue *GV = MO.getGlobal();
121 
122  MCSymbol *GVSym;
125  GVSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
126  else
127  GVSym = getSymbol(GV);
128 
129  // Handle dllimport linkage.
131  GVSym = OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName());
132  else if (MO.getTargetFlags() == X86II::MO_COFFSTUB)
133  GVSym =
134  OutContext.getOrCreateSymbol(Twine(".refptr.") + GVSym->getName());
135 
138  MCSymbol *Sym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
140  MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
141  if (!StubSym.getPointer())
143  !GV->hasInternalLinkage());
144  }
145 
146  // If the name begins with a dollar-sign, enclose it in parens. We do this
147  // to avoid having it look like an integer immediate to the assembler.
148  if (GVSym->getName()[0] != '$')
149  GVSym->print(O, MAI);
150  else {
151  O << '(';
152  GVSym->print(O, MAI);
153  O << ')';
154  }
155  printOffset(MO.getOffset(), O);
156  break;
157  }
158  }
159 
160  switch (MO.getTargetFlags()) {
161  default:
162  llvm_unreachable("Unknown target flag on GV operand");
163  case X86II::MO_NO_FLAG: // No flag.
164  break;
166  case X86II::MO_DLLIMPORT:
167  case X86II::MO_COFFSTUB:
168  // These affect the name of the symbol, not any suffix.
169  break;
171  O << " + [.-";
172  MF->getPICBaseSymbol()->print(O, MAI);
173  O << ']';
174  break;
177  O << '-';
178  MF->getPICBaseSymbol()->print(O, MAI);
179  break;
180  case X86II::MO_TLSGD: O << "@TLSGD"; break;
181  case X86II::MO_TLSLD: O << "@TLSLD"; break;
182  case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
183  case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
184  case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
185  case X86II::MO_TPOFF: O << "@TPOFF"; break;
186  case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
187  case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
188  case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
189  case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
190  case X86II::MO_GOT: O << "@GOT"; break;
191  case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
192  case X86II::MO_PLT: O << "@PLT"; break;
193  case X86II::MO_TLVP: O << "@TLVP"; break;
195  O << "@TLVP" << '-';
196  MF->getPICBaseSymbol()->print(O, MAI);
197  break;
198  case X86II::MO_SECREL: O << "@SECREL32"; break;
199  }
200 }
201 
202 void X86AsmPrinter::PrintOperand(const MachineInstr *MI, unsigned OpNo,
203  raw_ostream &O) {
204  const MachineOperand &MO = MI->getOperand(OpNo);
205  const bool IsATT = MI->getInlineAsmDialect() == InlineAsm::AD_ATT;
206  switch (MO.getType()) {
207  default: llvm_unreachable("unknown operand type!");
209  if (IsATT)
210  O << '%';
212  return;
213  }
214 
216  if (IsATT)
217  O << '$';
218  O << MO.getImm();
219  return;
220 
223  switch (MI->getInlineAsmDialect()) {
224  case InlineAsm::AD_ATT:
225  O << '$';
226  break;
227  case InlineAsm::AD_Intel:
228  O << "offset ";
229  break;
230  }
231  PrintSymbolOperand(MO, O);
232  break;
233  }
236  Sym->print(O, MAI);
237  break;
238  }
239  }
240 }
241 
242 /// PrintModifiedOperand - Print subregisters based on supplied modifier,
243 /// deferring to PrintOperand() if no modifier was supplied or if operand is not
244 /// a register.
245 void X86AsmPrinter::PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo,
246  raw_ostream &O, const char *Modifier) {
247  const MachineOperand &MO = MI->getOperand(OpNo);
248  if (!Modifier || MO.getType() != MachineOperand::MO_Register)
249  return PrintOperand(MI, OpNo, O);
251  O << '%';
252  Register Reg = MO.getReg();
253  if (strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
254  unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 :
255  (strcmp(Modifier+6,"32") == 0) ? 32 :
256  (strcmp(Modifier+6,"16") == 0) ? 16 : 8;
257  Reg = getX86SubSuperRegister(Reg, Size);
258  }
260 }
261 
262 /// PrintPCRelImm - This is used to print an immediate value that ends up
263 /// being encoded as a pc-relative value. These print slightly differently, for
264 /// example, a $ is not emitted.
265 void X86AsmPrinter::PrintPCRelImm(const MachineInstr *MI, unsigned OpNo,
266  raw_ostream &O) {
267  const MachineOperand &MO = MI->getOperand(OpNo);
268  switch (MO.getType()) {
269  default: llvm_unreachable("Unknown pcrel immediate operand");
271  // pc-relativeness was handled when computing the value in the reg.
272  PrintOperand(MI, OpNo, O);
273  return;
275  O << MO.getImm();
276  return;
278  PrintSymbolOperand(MO, O);
279  return;
280  }
281 }
282 
283 void X86AsmPrinter::PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo,
284  raw_ostream &O, const char *Modifier) {
285  const MachineOperand &BaseReg = MI->getOperand(OpNo + X86::AddrBaseReg);
286  const MachineOperand &IndexReg = MI->getOperand(OpNo + X86::AddrIndexReg);
287  const MachineOperand &DispSpec = MI->getOperand(OpNo + X86::AddrDisp);
288 
289  // If we really don't want to print out (rip), don't.
290  bool HasBaseReg = BaseReg.getReg() != 0;
291  if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
292  BaseReg.getReg() == X86::RIP)
293  HasBaseReg = false;
294 
295  // HasParenPart - True if we will print out the () part of the mem ref.
296  bool HasParenPart = IndexReg.getReg() || HasBaseReg;
297 
298  switch (DispSpec.getType()) {
299  default:
300  llvm_unreachable("unknown operand type!");
302  int DispVal = DispSpec.getImm();
303  if (DispVal || !HasParenPart)
304  O << DispVal;
305  break;
306  }
309  PrintSymbolOperand(DispSpec, O);
310  break;
311  }
312 
313  if (Modifier && strcmp(Modifier, "H") == 0)
314  O << "+8";
315 
316  if (HasParenPart) {
317  assert(IndexReg.getReg() != X86::ESP &&
318  "X86 doesn't allow scaling by ESP");
319 
320  O << '(';
321  if (HasBaseReg)
322  PrintModifiedOperand(MI, OpNo + X86::AddrBaseReg, O, Modifier);
323 
324  if (IndexReg.getReg()) {
325  O << ',';
326  PrintModifiedOperand(MI, OpNo + X86::AddrIndexReg, O, Modifier);
327  unsigned ScaleVal = MI->getOperand(OpNo + X86::AddrScaleAmt).getImm();
328  if (ScaleVal != 1)
329  O << ',' << ScaleVal;
330  }
331  O << ')';
332  }
333 }
334 
335 void X86AsmPrinter::PrintMemReference(const MachineInstr *MI, unsigned OpNo,
336  raw_ostream &O, const char *Modifier) {
337  assert(isMem(*MI, OpNo) && "Invalid memory reference!");
338  const MachineOperand &Segment = MI->getOperand(OpNo + X86::AddrSegmentReg);
339  if (Segment.getReg()) {
340  PrintModifiedOperand(MI, OpNo + X86::AddrSegmentReg, O, Modifier);
341  O << ':';
342  }
343  PrintLeaMemReference(MI, OpNo, O, Modifier);
344 }
345 
346 
347 void X86AsmPrinter::PrintIntelMemReference(const MachineInstr *MI,
348  unsigned OpNo, raw_ostream &O,
349  const char *Modifier) {
350  const MachineOperand &BaseReg = MI->getOperand(OpNo + X86::AddrBaseReg);
351  unsigned ScaleVal = MI->getOperand(OpNo + X86::AddrScaleAmt).getImm();
352  const MachineOperand &IndexReg = MI->getOperand(OpNo + X86::AddrIndexReg);
353  const MachineOperand &DispSpec = MI->getOperand(OpNo + X86::AddrDisp);
354  const MachineOperand &SegReg = MI->getOperand(OpNo + X86::AddrSegmentReg);
355 
356  // If we really don't want to print out (rip), don't.
357  bool HasBaseReg = BaseReg.getReg() != 0;
358  if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
359  BaseReg.getReg() == X86::RIP)
360  HasBaseReg = false;
361 
362  // If this has a segment register, print it.
363  if (SegReg.getReg()) {
364  PrintOperand(MI, OpNo + X86::AddrSegmentReg, O);
365  O << ':';
366  }
367 
368  O << '[';
369 
370  bool NeedPlus = false;
371  if (HasBaseReg) {
372  PrintOperand(MI, OpNo + X86::AddrBaseReg, O);
373  NeedPlus = true;
374  }
375 
376  if (IndexReg.getReg()) {
377  if (NeedPlus) O << " + ";
378  if (ScaleVal != 1)
379  O << ScaleVal << '*';
380  PrintOperand(MI, OpNo + X86::AddrIndexReg, O);
381  NeedPlus = true;
382  }
383 
384  if (!DispSpec.isImm()) {
385  if (NeedPlus) O << " + ";
386  PrintOperand(MI, OpNo + X86::AddrDisp, O);
387  } else {
388  int64_t DispVal = DispSpec.getImm();
389  if (DispVal || (!IndexReg.getReg() && !HasBaseReg)) {
390  if (NeedPlus) {
391  if (DispVal > 0)
392  O << " + ";
393  else {
394  O << " - ";
395  DispVal = -DispVal;
396  }
397  }
398  O << DispVal;
399  }
400  }
401  O << ']';
402 }
403 
405  char Mode, raw_ostream &O) {
406  Register Reg = MO.getReg();
407  bool EmitPercent = true;
408 
409  if (!X86::GR8RegClass.contains(Reg) &&
410  !X86::GR16RegClass.contains(Reg) &&
411  !X86::GR32RegClass.contains(Reg) &&
412  !X86::GR64RegClass.contains(Reg))
413  return true;
414 
415  switch (Mode) {
416  default: return true; // Unknown mode.
417  case 'b': // Print QImode register
418  Reg = getX86SubSuperRegister(Reg, 8);
419  break;
420  case 'h': // Print QImode high register
421  Reg = getX86SubSuperRegister(Reg, 8, true);
422  break;
423  case 'w': // Print HImode register
424  Reg = getX86SubSuperRegister(Reg, 16);
425  break;
426  case 'k': // Print SImode register
427  Reg = getX86SubSuperRegister(Reg, 32);
428  break;
429  case 'V':
430  EmitPercent = false;
432  case 'q':
433  // Print 64-bit register names if 64-bit integer registers are available.
434  // Otherwise, print 32-bit register names.
435  Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 64 : 32);
436  break;
437  }
438 
439  if (EmitPercent)
440  O << '%';
441 
443  return false;
444 }
445 
446 /// PrintAsmOperand - Print out an operand for an inline asm expression.
447 ///
448 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
449  const char *ExtraCode, raw_ostream &O) {
450  // Does this asm operand have a single letter operand modifier?
451  if (ExtraCode && ExtraCode[0]) {
452  if (ExtraCode[1] != 0) return true; // Unknown modifier.
453 
454  const MachineOperand &MO = MI->getOperand(OpNo);
455 
456  switch (ExtraCode[0]) {
457  default:
458  // See if this is a generic print operand
459  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
460  case 'a': // This is an address. Currently only 'i' and 'r' are expected.
461  switch (MO.getType()) {
462  default:
463  return true;
465  O << MO.getImm();
466  return false;
470  llvm_unreachable("unexpected operand type!");
472  PrintSymbolOperand(MO, O);
473  if (Subtarget->isPICStyleRIPRel())
474  O << "(%rip)";
475  return false;
477  O << '(';
478  PrintOperand(MI, OpNo, O);
479  O << ')';
480  return false;
481  }
482 
483  case 'c': // Don't print "$" before a global var name or constant.
484  switch (MO.getType()) {
485  default:
486  PrintOperand(MI, OpNo, O);
487  break;
489  O << MO.getImm();
490  break;
494  llvm_unreachable("unexpected operand type!");
496  PrintSymbolOperand(MO, O);
497  break;
498  }
499  return false;
500 
501  case 'A': // Print '*' before a register (it must be a register)
502  if (MO.isReg()) {
503  O << '*';
504  PrintOperand(MI, OpNo, O);
505  return false;
506  }
507  return true;
508 
509  case 'b': // Print QImode register
510  case 'h': // Print QImode high register
511  case 'w': // Print HImode register
512  case 'k': // Print SImode register
513  case 'q': // Print DImode register
514  case 'V': // Print native register without '%'
515  if (MO.isReg())
516  return printAsmMRegister(*this, MO, ExtraCode[0], O);
517  PrintOperand(MI, OpNo, O);
518  return false;
519 
520  case 'P': // This is the operand of a call, treat specially.
521  PrintPCRelImm(MI, OpNo, O);
522  return false;
523 
524  case 'n': // Negate the immediate or print a '-' before the operand.
525  // Note: this is a temporary solution. It should be handled target
526  // independently as part of the 'MC' work.
527  if (MO.isImm()) {
528  O << -MO.getImm();
529  return false;
530  }
531  O << '-';
532  }
533  }
534 
535  PrintOperand(MI, OpNo, O);
536  return false;
537 }
538 
540  const char *ExtraCode,
541  raw_ostream &O) {
542  if (ExtraCode && ExtraCode[0]) {
543  if (ExtraCode[1] != 0) return true; // Unknown modifier.
544 
545  switch (ExtraCode[0]) {
546  default: return true; // Unknown modifier.
547  case 'b': // Print QImode register
548  case 'h': // Print QImode high register
549  case 'w': // Print HImode register
550  case 'k': // Print SImode register
551  case 'q': // Print SImode register
552  // These only apply to registers, ignore on mem.
553  break;
554  case 'H':
556  return true; // Unsupported modifier in Intel inline assembly.
557  } else {
558  PrintMemReference(MI, OpNo, O, "H");
559  }
560  return false;
561  case 'P': // Don't print @PLT, but do print as memory.
563  PrintIntelMemReference(MI, OpNo, O, "no-rip");
564  } else {
565  PrintMemReference(MI, OpNo, O, "no-rip");
566  }
567  return false;
568  }
569  }
571  PrintIntelMemReference(MI, OpNo, O, nullptr);
572  } else {
573  PrintMemReference(MI, OpNo, O, nullptr);
574  }
575  return false;
576 }
577 
579  const Triple &TT = TM.getTargetTriple();
580 
581  if (TT.isOSBinFormatELF()) {
582  // Assemble feature flags that may require creation of a note section.
583  unsigned FeatureFlagsAnd = 0;
584  if (M.getModuleFlag("cf-protection-branch"))
585  FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT;
586  if (M.getModuleFlag("cf-protection-return"))
587  FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK;
588 
589  if (FeatureFlagsAnd) {
590  // Emit a .note.gnu.property section with the flags.
591  if (!TT.isArch32Bit() && !TT.isArch64Bit())
592  llvm_unreachable("CFProtection used on invalid architecture!");
593  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
595  ".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
596  OutStreamer->SwitchSection(Nt);
597 
598  // Emitting note header.
599  int WordSize = TT.isArch64Bit() ? 8 : 4;
600  EmitAlignment(WordSize == 4 ? Align(4) : Align(8));
601  OutStreamer->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
602  OutStreamer->EmitIntValue(8 + WordSize, 4 /*size*/); // Elf_Prop size
603  OutStreamer->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4 /*size*/);
604  OutStreamer->EmitBytes(StringRef("GNU", 4)); // note name
605 
606  // Emitting an Elf_Prop for the CET properties.
608  OutStreamer->EmitIntValue(4, 4); // data size
609  OutStreamer->EmitIntValue(FeatureFlagsAnd, 4); // data
610  EmitAlignment(WordSize == 4 ? Align(4) : Align(8)); // padding
611 
612  OutStreamer->endSection(Nt);
613  OutStreamer->SwitchSection(Cur);
614  }
615  }
616 
617  if (TT.isOSBinFormatMachO())
618  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
619 
620  if (TT.isOSBinFormatCOFF()) {
621  // Emit an absolute @feat.00 symbol. This appears to be some kind of
622  // compiler features bitfield read by link.exe.
623  MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
624  OutStreamer->BeginCOFFSymbolDef(S);
625  OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
626  OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
627  OutStreamer->EndCOFFSymbolDef();
628  int64_t Feat00Flags = 0;
629 
630  if (TT.getArch() == Triple::x86) {
631  // According to the PE-COFF spec, the LSB of this value marks the object
632  // for "registered SEH". This means that all SEH handler entry points
633  // must be registered in .sxdata. Use of any unregistered handlers will
634  // cause the process to terminate immediately. LLVM does not know how to
635  // register any SEH handlers, so its object files should be safe.
636  Feat00Flags |= 1;
637  }
638 
639  if (M.getModuleFlag("cfguard"))
640  Feat00Flags |= 0x800; // Object is CFG-aware.
641 
642  OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
643  OutStreamer->EmitAssignment(
644  S, MCConstantExpr::create(Feat00Flags, MMI->getContext()));
645  }
646  OutStreamer->EmitSyntaxDirective();
647 
648  // If this is not inline asm and we're in 16-bit
649  // mode prefix assembly with .code16.
650  bool is16 = TT.getEnvironment() == Triple::CODE16;
651  if (M.getModuleInlineAsm().empty() && is16)
652  OutStreamer->EmitAssemblerFlag(MCAF_Code16);
653 }
654 
655 static void
658  // L_foo$stub:
659  OutStreamer.EmitLabel(StubLabel);
660  // .indirect_symbol _foo
661  OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
662 
663  if (MCSym.getInt())
664  // External to current translation unit.
665  OutStreamer.EmitIntValue(0, 4/*size*/);
666  else
667  // Internal to current translation unit.
668  //
669  // When we place the LSDA into the TEXT section, the type info
670  // pointers need to be indirect and pc-rel. We accomplish this by
671  // using NLPs; however, sometimes the types are local to the file.
672  // We need to fill in the value for the NLP in those cases.
673  OutStreamer.EmitValue(
674  MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
675  4 /*size*/);
676 }
677 
679 
680  MachineModuleInfoMachO &MMIMacho =
682 
683  // Output stubs for dynamically-linked functions.
685 
686  // Output stubs for external and common global variables.
687  Stubs = MMIMacho.GetGVStubList();
688  if (!Stubs.empty()) {
689  OutStreamer.SwitchSection(MMI->getContext().getMachOSection(
690  "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS,
692 
693  for (auto &Stub : Stubs)
694  emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
695 
696  Stubs.clear();
697  OutStreamer.AddBlankLine();
698  }
699 }
700 
702  const Triple &TT = TM.getTargetTriple();
703 
704  if (TT.isOSBinFormatMachO()) {
705  // Mach-O uses non-lazy symbol stubs to encode per-TU information into
706  // global table for symbol lookup.
708 
709  // Emit stack and fault map information.
710  emitStackMaps(SM);
712 
713  // This flag tells the linker that no global symbols contain code that fall
714  // through to other global symbols (e.g. an implementation of multiple entry
715  // points). If this doesn't occur, the linker can safely perform dead code
716  // stripping. Since LLVM never generates code that does this, it is always
717  // safe to set.
718  OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
719  } else if (TT.isOSBinFormatCOFF()) {
720  if (MMI->usesMSVCFloatingPoint()) {
721  // In Windows' libcmt.lib, there is a file which is linked in only if the
722  // symbol _fltused is referenced. Linking this in causes some
723  // side-effects:
724  //
725  // 1. For x86-32, it will set the x87 rounding mode to 53-bit instead of
726  // 64-bit mantissas at program start.
727  //
728  // 2. It links in support routines for floating-point in scanf and printf.
729  //
730  // MSVC emits an undefined reference to _fltused when there are any
731  // floating point operations in the program (including calls). A program
732  // that only has: `scanf("%f", &global_float);` may fail to trigger this,
733  // but oh well...that's a documented issue.
735  (TT.getArch() == Triple::x86) ? "__fltused" : "_fltused";
736  MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName);
737  OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
738  return;
739  }
740  emitStackMaps(SM);
741  } else if (TT.isOSBinFormatELF()) {
742  emitStackMaps(SM);
744  }
745 }
746 
747 //===----------------------------------------------------------------------===//
748 // Target Registry Stuff
749 //===----------------------------------------------------------------------===//
750 
751 // Force static initialization.
755 }
unsigned getTargetFlags() const
MO_SECREL - On a symbol operand this indicates that the immediate is the offset from beginning of sec...
Definition: X86BaseInfo.h:520
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:214
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
bool is64Bit() const
Is this x86_64? (disregarding specific ABI / programming model)
Definition: X86Subtarget.h:551
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
SymbolListTy GetGVStubList()
Accessor methods to return the set of stubs in sorted order.
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
Definition: MachO.h:131
static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO, char Mode, raw_ostream &O)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
static const char * getRegisterName(unsigned RegNo)
MO_COFFSTUB - On a symbol operand "FOO", this indicates that the reference is actually to the "...
Definition: X86BaseInfo.h:530
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:95
SI Whole Quad Mode
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
PointerTy getPointer() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
MO_TLSLD - On a symbol operand this indicates that the immediate is the offset of the GOT entry with ...
Definition: X86BaseInfo.h:428
virtual void AddBlankLine()
AddBlankLine - Emit a blank line to a .s file to pretty it up.
Definition: MCStreamer.h:349
MO_TLVP_PIC_BASE - On a symbol operand this indicates that the immediate is some TLS offset from the ...
Definition: X86BaseInfo.h:514
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:624
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:90
const X86InstrInfo * getInstrInfo() const override
Definition: X86Subtarget.h:510
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - Emit the function body.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86AsmPrinter()
Address of indexed Jump Table for switch.
unsigned Reg
void EmitAlignment(Align Alignment, const GlobalObject *GV=nullptr) const
Emit an alignment directive to the specified power of two boundary.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
PrintAsmOperand - Print out an operand for an inline asm expression.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:98
MachineModuleInfo & getMMI() const
MO_GOTPCREL - On a symbol operand this indicates that the immediate is offset to the GOT entry for th...
Definition: X86BaseInfo.h:402
MO_DTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry with...
Definition: X86BaseInfo.h:471
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MCRegister getX86SubSuperRegister(MCRegister, unsigned, bool High=false)
Returns the sub or super register of a specific X86 register.
No complex type; simple scalar variable.
Definition: COFF.h:259
return AArch64::GPR64RegClass contains(Reg)
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
MCContext & getContext() const
Definition: MCStreamer.h:266
MO_DARWIN_NONLAZY_PIC_BASE - On a symbol operand "FOO", this indicates that the reference is actually...
Definition: X86BaseInfo.h:502
Definition: BitVector.h:937
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
bool isPICStyleRIPRel() const
Definition: X86Subtarget.h:803
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Name of external global symbol.
static bool isMem(const MachineInstr &MI, unsigned Op)
Definition: X86InstrInfo.h:125
void emitXRayTable()
Emit a table with all XRay instrumentation points.
void EmitFunctionBody()
This method emits the body and trailer for a function.
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:52
MCSectionMachO * getMachOSection(StringRef Segment, StringRef Section, unsigned TypeAndAttributes, unsigned Reserved2, SectionKind K, const char *BeginSymName=nullptr)
Return the MCSection for the specified mach-o section.
Definition: MCContext.cpp:286
MO_GOT - On a symbol operand this indicates that the immediate is the offset to the GOT entry for the...
Definition: X86BaseInfo.h:387
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:297
IntType getInt() const
const MCContext & getContext() const
bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition: Triple.cpp:1302
MO_GOTTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry wi...
Definition: X86BaseInfo.h:446
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
static void emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, MachineModuleInfoImpl::StubValueTy &MCSym)
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:265
void printOffset(int64_t Offset, raw_ostream &OS) const
This is just convenient handler for printing offsets.
MCCodeEmitter * createMCCodeEmitter(const MCInstrInfo &II, const MCRegisterInfo &MRI, MCContext &Ctx) const
createMCCodeEmitter - Create a target specific code emitter.
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:131
MO_DARWIN_NONLAZY - On a symbol operand "FOO", this indicates that the reference is actually to the "...
Definition: X86BaseInfo.h:497
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:162
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:101
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
unsigned getCodeViewFlag() const
Returns the CodeView Version by checking module flags.
Definition: Module.cpp:484
MCContext & getContext() const
static void emitNonLazyStubs(MachineModuleInfo *MMI, MCStreamer &OutStreamer)
#define P(N)
Address of a global value.
Streaming machine code generation interface.
Definition: MCStreamer.h:197
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:121
MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a relocation of: SYMBOL_LABEL + [...
Definition: X86BaseInfo.h:375
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:86
PointerIntPair - This class implements a pair of a pointer and small integer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:629
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:310
MO_GOTNTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry w...
Definition: X86BaseInfo.h:487
MO_TLVP - On a symbol operand this indicates that the immediate is some TLS offset.
Definition: X86BaseInfo.h:508
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
const GlobalValue * getGlobal() const
MO_TLSLDM - On a symbol operand this indicates that the immediate is the offset of the GOT entry with...
Definition: X86BaseInfo.h:438
void EmitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:83
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:80
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
bool hasInternalLinkage() const
Definition: GlobalValue.h:443
Address of a basic block.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:51
const Triple & getTargetTriple() const
MO_NTPOFF - On a symbol operand this indicates that the immediate is the negative thread-pointer offs...
Definition: X86BaseInfo.h:479
Ty & getObjFileInfo()
Keep track of various per-function pieces of information for backends that would like to do so...
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:634
const Target & getTarget() const
void emitStackMaps(StackMaps &SM)
Emit the stack maps.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
MO_TLSGD - On a symbol operand this indicates that the immediate is the offset of the GOT entry with ...
Definition: X86BaseInfo.h:418
const X86RegisterInfo * getRegisterInfo() const override
Definition: X86Subtarget.h:520
MO_TPOFF - On a symbol operand this indicates that the immediate is the thread-pointer offset for the...
Definition: X86BaseInfo.h:463
void EmitFunctionBodyStart() override
Targets can override this to emit stuff before the first basic block in the function.
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
InlineAsm::AsmDialect getInlineAsmDialect() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:449
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:47
MachineOperand class - Representation of each machine instruction operand.
AddrSegmentReg - The operand # of the segment in the memory operand.
Definition: X86BaseInfo.h:38
Module.h This file contains the declarations for the Module class.
void EmitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
.indirect_symbol (MachO)
Definition: MCDirectives.h:33
static SectionKind getMetadata()
Definition: SectionKind.h:178
EnvironmentType getEnvironment() const
getEnvironment - Get the parsed environment type of this triple.
Definition: Triple.h:315
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
bool isTargetWin32() const
Definition: X86Subtarget.h:800
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:130
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
Representation of each machine instruction.
Definition: MachineInstr.h:63
MO_GOTOFF - On a symbol operand this indicates that the immediate is the offset to the location of th...
Definition: X86BaseInfo.h:394
virtual void SetupMachineFunction(MachineFunction &MF)
This should be called when a new MachineFunction is being processed from runOnMachineFunction.
MO_INDNTPOFF - On a symbol operand this indicates that the immediate is the absolute address of the G...
Definition: X86BaseInfo.h:455
.type _foo,
Definition: MCDirectives.h:30
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:130
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1298
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
void EmitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
bool usesMSVCFloatingPoint() const
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:128
bool isTargetCOFF() const
Definition: X86Subtarget.h:761
Target & getTheX86_32Target()
uint32_t Size
Definition: Profile.cpp:46
const Module * getModule() const
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
std::vector< std::pair< MCSymbol *, StubValueTy > > SymbolListTy
MO_PLT - On a symbol operand this indicates that the immediate is offset to the PLT entry of symbol n...
Definition: X86BaseInfo.h:409
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:249
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:196
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:424
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:279
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:400
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the immediate should get the value of th...
Definition: X86BaseInfo.h:380
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Address of indexed Constant in Constant Pool.
Register getReg() const
getReg - Returns the register number.
X86AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
MCSymbol * getSymbolWithGlobalValueBase(const GlobalValue *GV, StringRef Suffix) const
Return the MCSymbol for a private symbol with global value name as its base, with the specified suffi...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
Target & getTheX86_64Target()
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const X86Subtarget & getSubtarget() const
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MO_DLLIMPORT - On a symbol operand "FOO", this indicates that the reference is actually to the "__imp...
Definition: X86BaseInfo.h:492
This class contains meta information specific to a module.
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59
A function that returns a base type.
Definition: COFF.h:261