LLVM  9.0.0svn
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"
18 #include "X86InstrInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "llvm/BinaryFormat/COFF.h"
21 #include "llvm/BinaryFormat/ELF.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/MC/MCCodeEmitter.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCSectionCOFF.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCSectionMachO.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/Support/Debug.h"
41 using namespace llvm;
42 
44  std::unique_ptr<MCStreamer> Streamer)
45  : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {}
46 
47 //===----------------------------------------------------------------------===//
48 // Primitive Helper Functions.
49 //===----------------------------------------------------------------------===//
50 
51 /// runOnMachineFunction - Emit the function body.
52 ///
54  Subtarget = &MF.getSubtarget<X86Subtarget>();
55 
56  SMShadowTracker.startFunction(MF);
57  CodeEmitter.reset(TM.getTarget().createMCCodeEmitter(
58  *Subtarget->getInstrInfo(), *Subtarget->getRegisterInfo(),
59  MF.getContext()));
60 
61  EmitFPOData =
62  Subtarget->isTargetWin32() && MF.getMMI().getModule()->getCodeViewFlag();
63 
65 
66  if (Subtarget->isTargetCOFF()) {
67  bool Local = MF.getFunction().hasLocalLinkage();
68  OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
69  OutStreamer->EmitCOFFSymbolStorageClass(
73  OutStreamer->EndCOFFSymbolDef();
74  }
75 
76  // Emit the rest of the function body.
78 
79  // Emit the XRay table for this function.
80  emitXRayTable();
81 
82  EmitFPOData = false;
83 
84  // We didn't modify anything.
85  return false;
86 }
87 
89  if (EmitFPOData) {
90  if (auto *XTS =
91  static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()))
92  XTS->emitFPOProc(
95  }
96 }
97 
99  if (EmitFPOData) {
100  if (auto *XTS =
101  static_cast<X86TargetStreamer *>(OutStreamer->getTargetStreamer()))
102  XTS->emitFPOEndProc();
103  }
104 }
105 
106 /// printSymbolOperand - Print a raw symbol reference operand. This handles
107 /// jump tables, constant pools, global address and external symbols, all of
108 /// which print to a label with various suffixes for relocation types etc.
110  raw_ostream &O) {
111  switch (MO.getType()) {
112  default: llvm_unreachable("unknown symbol type!");
114  P.GetCPISymbol(MO.getIndex())->print(O, P.MAI);
115  P.printOffset(MO.getOffset(), O);
116  break;
118  const GlobalValue *GV = MO.getGlobal();
119 
120  MCSymbol *GVSym;
123  GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
124  else
125  GVSym = P.getSymbol(GV);
126 
127  // Handle dllimport linkage.
129  GVSym =
130  P.OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName());
131  else if (MO.getTargetFlags() == X86II::MO_COFFSTUB)
132  GVSym =
133  P.OutContext.getOrCreateSymbol(Twine(".refptr.") + GVSym->getName());
134 
137  MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
139  P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
140  if (!StubSym.getPointer())
141  StubSym = MachineModuleInfoImpl::
142  StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
143  }
144 
145  // If the name begins with a dollar-sign, enclose it in parens. We do this
146  // to avoid having it look like an integer immediate to the assembler.
147  if (GVSym->getName()[0] != '$')
148  GVSym->print(O, P.MAI);
149  else {
150  O << '(';
151  GVSym->print(O, P.MAI);
152  O << ')';
153  }
154  P.printOffset(MO.getOffset(), O);
155  break;
156  }
157  }
158 
159  switch (MO.getTargetFlags()) {
160  default:
161  llvm_unreachable("Unknown target flag on GV operand");
162  case X86II::MO_NO_FLAG: // No flag.
163  break;
165  case X86II::MO_DLLIMPORT:
166  case X86II::MO_COFFSTUB:
167  // These affect the name of the symbol, not any suffix.
168  break;
170  O << " + [.-";
171  P.MF->getPICBaseSymbol()->print(O, P.MAI);
172  O << ']';
173  break;
176  O << '-';
177  P.MF->getPICBaseSymbol()->print(O, P.MAI);
178  break;
179  case X86II::MO_TLSGD: O << "@TLSGD"; break;
180  case X86II::MO_TLSLD: O << "@TLSLD"; break;
181  case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
182  case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
183  case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
184  case X86II::MO_TPOFF: O << "@TPOFF"; break;
185  case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
186  case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
187  case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
188  case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
189  case X86II::MO_GOT: O << "@GOT"; break;
190  case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
191  case X86II::MO_PLT: O << "@PLT"; break;
192  case X86II::MO_TLVP: O << "@TLVP"; break;
194  O << "@TLVP" << '-';
195  P.MF->getPICBaseSymbol()->print(O, P.MAI);
196  break;
197  case X86II::MO_SECREL: O << "@SECREL32"; break;
198  }
199 }
200 
201 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
202  unsigned OpNo, raw_ostream &O,
203  const char *Modifier = nullptr, unsigned AsmVariant = 0);
204 
205 /// printPCRelImm - This is used to print an immediate value that ends up
206 /// being encoded as a pc-relative value. These print slightly differently, for
207 /// example, a $ is not emitted.
208 static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
209  unsigned OpNo, raw_ostream &O) {
210  const MachineOperand &MO = MI->getOperand(OpNo);
211  switch (MO.getType()) {
212  default: llvm_unreachable("Unknown pcrel immediate operand");
214  // pc-relativeness was handled when computing the value in the reg.
215  printOperand(P, MI, OpNo, O);
216  return;
218  O << MO.getImm();
219  return;
221  printSymbolOperand(P, MO, O);
222  return;
223  }
224 }
225 
226 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
227  unsigned OpNo, raw_ostream &O, const char *Modifier,
228  unsigned AsmVariant) {
229  const MachineOperand &MO = MI->getOperand(OpNo);
230  switch (MO.getType()) {
231  default: llvm_unreachable("unknown operand type!");
233  // FIXME: Enumerating AsmVariant, so we can remove magic number.
234  if (AsmVariant == 0) O << '%';
235  unsigned Reg = MO.getReg();
236  if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
237  unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 :
238  (strcmp(Modifier+6,"32") == 0) ? 32 :
239  (strcmp(Modifier+6,"16") == 0) ? 16 : 8;
240  Reg = getX86SubSuperRegister(Reg, Size);
241  }
243  return;
244  }
245 
247  if (AsmVariant == 0) O << '$';
248  O << MO.getImm();
249  return;
250 
252  if (AsmVariant == 0) O << '$';
253  printSymbolOperand(P, MO, O);
254  break;
255  }
258  Sym->print(O, P.MAI);
259  break;
260  }
261  }
262 }
263 
265  unsigned Op, raw_ostream &O,
266  const char *Modifier = nullptr) {
267  const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
268  const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
269  const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
270 
271  // If we really don't want to print out (rip), don't.
272  bool HasBaseReg = BaseReg.getReg() != 0;
273  if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
274  BaseReg.getReg() == X86::RIP)
275  HasBaseReg = false;
276 
277  // HasParenPart - True if we will print out the () part of the mem ref.
278  bool HasParenPart = IndexReg.getReg() || HasBaseReg;
279 
280  switch (DispSpec.getType()) {
281  default:
282  llvm_unreachable("unknown operand type!");
284  int DispVal = DispSpec.getImm();
285  if (DispVal || !HasParenPart)
286  O << DispVal;
287  break;
288  }
291  printSymbolOperand(P, DispSpec, O);
292  }
293 
294  if (Modifier && strcmp(Modifier, "H") == 0)
295  O << "+8";
296 
297  if (HasParenPart) {
298  assert(IndexReg.getReg() != X86::ESP &&
299  "X86 doesn't allow scaling by ESP");
300 
301  O << '(';
302  if (HasBaseReg)
303  printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier);
304 
305  if (IndexReg.getReg()) {
306  O << ',';
307  printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier);
308  unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
309  if (ScaleVal != 1)
310  O << ',' << ScaleVal;
311  }
312  O << ')';
313  }
314 }
315 
316 static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
317  unsigned Op, raw_ostream &O,
318  const char *Modifier = nullptr) {
319  assert(isMem(*MI, Op) && "Invalid memory reference!");
320  const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg);
321  if (Segment.getReg()) {
322  printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier);
323  O << ':';
324  }
325  printLeaMemReference(P, MI, Op, O, Modifier);
326 }
327 
329  unsigned Op, raw_ostream &O,
330  const char *Modifier = nullptr,
331  unsigned AsmVariant = 1) {
332  const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
333  unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
334  const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
335  const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
336  const MachineOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
337 
338  // If this has a segment register, print it.
339  if (SegReg.getReg()) {
340  printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant);
341  O << ':';
342  }
343 
344  O << '[';
345 
346  bool NeedPlus = false;
347  if (BaseReg.getReg()) {
348  printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant);
349  NeedPlus = true;
350  }
351 
352  if (IndexReg.getReg()) {
353  if (NeedPlus) O << " + ";
354  if (ScaleVal != 1)
355  O << ScaleVal << '*';
356  printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant);
357  NeedPlus = true;
358  }
359 
360  if (!DispSpec.isImm()) {
361  if (NeedPlus) O << " + ";
362  printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant);
363  } else {
364  int64_t DispVal = DispSpec.getImm();
365  if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
366  if (NeedPlus) {
367  if (DispVal > 0)
368  O << " + ";
369  else {
370  O << " - ";
371  DispVal = -DispVal;
372  }
373  }
374  O << DispVal;
375  }
376  }
377  O << ']';
378 }
379 
381  char Mode, raw_ostream &O) {
382  unsigned Reg = MO.getReg();
383  bool EmitPercent = true;
384 
385  if (!X86::GR8RegClass.contains(Reg) &&
386  !X86::GR16RegClass.contains(Reg) &&
387  !X86::GR32RegClass.contains(Reg) &&
388  !X86::GR64RegClass.contains(Reg))
389  return true;
390 
391  switch (Mode) {
392  default: return true; // Unknown mode.
393  case 'b': // Print QImode register
394  Reg = getX86SubSuperRegister(Reg, 8);
395  break;
396  case 'h': // Print QImode high register
397  Reg = getX86SubSuperRegister(Reg, 8, true);
398  break;
399  case 'w': // Print HImode register
400  Reg = getX86SubSuperRegister(Reg, 16);
401  break;
402  case 'k': // Print SImode register
403  Reg = getX86SubSuperRegister(Reg, 32);
404  break;
405  case 'V':
406  EmitPercent = false;
408  case 'q':
409  // Print 64-bit register names if 64-bit integer registers are available.
410  // Otherwise, print 32-bit register names.
411  Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 64 : 32);
412  break;
413  }
414 
415  if (EmitPercent)
416  O << '%';
417 
419  return false;
420 }
421 
422 /// PrintAsmOperand - Print out an operand for an inline asm expression.
423 ///
424 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
425  unsigned AsmVariant,
426  const char *ExtraCode, raw_ostream &O) {
427  // Does this asm operand have a single letter operand modifier?
428  if (ExtraCode && ExtraCode[0]) {
429  if (ExtraCode[1] != 0) return true; // Unknown modifier.
430 
431  const MachineOperand &MO = MI->getOperand(OpNo);
432 
433  switch (ExtraCode[0]) {
434  default:
435  // See if this is a generic print operand
436  return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
437  case 'a': // This is an address. Currently only 'i' and 'r' are expected.
438  switch (MO.getType()) {
439  default:
440  return true;
442  O << MO.getImm();
443  return false;
447  llvm_unreachable("unexpected operand type!");
449  printSymbolOperand(*this, MO, O);
450  if (Subtarget->isPICStyleRIPRel())
451  O << "(%rip)";
452  return false;
454  O << '(';
455  printOperand(*this, MI, OpNo, O);
456  O << ')';
457  return false;
458  }
459 
460  case 'c': // Don't print "$" before a global var name or constant.
461  switch (MO.getType()) {
462  default:
463  printOperand(*this, MI, OpNo, O);
464  break;
466  O << MO.getImm();
467  break;
471  llvm_unreachable("unexpected operand type!");
473  printSymbolOperand(*this, MO, O);
474  break;
475  }
476  return false;
477 
478  case 'A': // Print '*' before a register (it must be a register)
479  if (MO.isReg()) {
480  O << '*';
481  printOperand(*this, MI, OpNo, O);
482  return false;
483  }
484  return true;
485 
486  case 'b': // Print QImode register
487  case 'h': // Print QImode high register
488  case 'w': // Print HImode register
489  case 'k': // Print SImode register
490  case 'q': // Print DImode register
491  case 'V': // Print native register without '%'
492  if (MO.isReg())
493  return printAsmMRegister(*this, MO, ExtraCode[0], O);
494  printOperand(*this, MI, OpNo, O);
495  return false;
496 
497  case 'P': // This is the operand of a call, treat specially.
498  printPCRelImm(*this, MI, OpNo, O);
499  return false;
500 
501  case 'n': // Negate the immediate or print a '-' before the operand.
502  // Note: this is a temporary solution. It should be handled target
503  // independently as part of the 'MC' work.
504  if (MO.isImm()) {
505  O << -MO.getImm();
506  return false;
507  }
508  O << '-';
509  }
510  }
511 
512  printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant);
513  return false;
514 }
515 
517  unsigned OpNo, unsigned AsmVariant,
518  const char *ExtraCode,
519  raw_ostream &O) {
520  if (AsmVariant) {
521  printIntelMemReference(*this, MI, OpNo, O);
522  return false;
523  }
524 
525  if (ExtraCode && ExtraCode[0]) {
526  if (ExtraCode[1] != 0) return true; // Unknown modifier.
527 
528  switch (ExtraCode[0]) {
529  default: return true; // Unknown modifier.
530  case 'b': // Print QImode register
531  case 'h': // Print QImode high register
532  case 'w': // Print HImode register
533  case 'k': // Print SImode register
534  case 'q': // Print SImode register
535  // These only apply to registers, ignore on mem.
536  break;
537  case 'H':
538  printMemReference(*this, MI, OpNo, O, "H");
539  return false;
540  case 'P': // Don't print @PLT, but do print as memory.
541  printMemReference(*this, MI, OpNo, O, "no-rip");
542  return false;
543  }
544  }
545  printMemReference(*this, MI, OpNo, O);
546  return false;
547 }
548 
550  const Triple &TT = TM.getTargetTriple();
551 
552  if (TT.isOSBinFormatELF()) {
553  // Assemble feature flags that may require creation of a note section.
554  unsigned FeatureFlagsAnd = 0;
555  if (M.getModuleFlag("cf-protection-branch"))
556  FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT;
557  if (M.getModuleFlag("cf-protection-return"))
558  FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK;
559 
560  if (FeatureFlagsAnd) {
561  // Emit a .note.gnu.property section with the flags.
562  if (!TT.isArch32Bit() && !TT.isArch64Bit())
563  llvm_unreachable("CFProtection used on invalid architecture!");
564  MCSection *Cur = OutStreamer->getCurrentSectionOnly();
566  ".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
567  OutStreamer->SwitchSection(Nt);
568 
569  // Emitting note header.
570  int WordSize = TT.isArch64Bit() ? 8 : 4;
571  EmitAlignment(WordSize == 4 ? 2 : 3);
572  OutStreamer->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
573  OutStreamer->EmitIntValue(8 + WordSize, 4 /*size*/); // Elf_Prop size
574  OutStreamer->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4 /*size*/);
575  OutStreamer->EmitBytes(StringRef("GNU", 4)); // note name
576 
577  // Emitting an Elf_Prop for the CET properties.
579  OutStreamer->EmitIntValue(4, 4); // data size
580  OutStreamer->EmitIntValue(FeatureFlagsAnd, 4); // data
581  EmitAlignment(WordSize == 4 ? 2 : 3); // padding
582 
583  OutStreamer->endSection(Nt);
584  OutStreamer->SwitchSection(Cur);
585  }
586  }
587 
588  if (TT.isOSBinFormatMachO())
589  OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
590 
591  if (TT.isOSBinFormatCOFF()) {
592  // Emit an absolute @feat.00 symbol. This appears to be some kind of
593  // compiler features bitfield read by link.exe.
594  MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
595  OutStreamer->BeginCOFFSymbolDef(S);
596  OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
597  OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
598  OutStreamer->EndCOFFSymbolDef();
599  int64_t Feat00Flags = 0;
600 
601  if (TT.getArch() == Triple::x86) {
602  // According to the PE-COFF spec, the LSB of this value marks the object
603  // for "registered SEH". This means that all SEH handler entry points
604  // must be registered in .sxdata. Use of any unregistered handlers will
605  // cause the process to terminate immediately. LLVM does not know how to
606  // register any SEH handlers, so its object files should be safe.
607  Feat00Flags |= 1;
608  }
609 
610  if (M.getModuleFlag("cfguardtable"))
611  Feat00Flags |= 0x800; // Object is CFG-aware.
612 
613  OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
614  OutStreamer->EmitAssignment(
615  S, MCConstantExpr::create(Feat00Flags, MMI->getContext()));
616  }
617  OutStreamer->EmitSyntaxDirective();
618 
619  // If this is not inline asm and we're in 16-bit
620  // mode prefix assembly with .code16.
621  bool is16 = TT.getEnvironment() == Triple::CODE16;
622  if (M.getModuleInlineAsm().empty() && is16)
623  OutStreamer->EmitAssemblerFlag(MCAF_Code16);
624 }
625 
626 static void
629  // L_foo$stub:
630  OutStreamer.EmitLabel(StubLabel);
631  // .indirect_symbol _foo
632  OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
633 
634  if (MCSym.getInt())
635  // External to current translation unit.
636  OutStreamer.EmitIntValue(0, 4/*size*/);
637  else
638  // Internal to current translation unit.
639  //
640  // When we place the LSDA into the TEXT section, the type info
641  // pointers need to be indirect and pc-rel. We accomplish this by
642  // using NLPs; however, sometimes the types are local to the file.
643  // We need to fill in the value for the NLP in those cases.
644  OutStreamer.EmitValue(
645  MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
646  4 /*size*/);
647 }
648 
650 
651  MachineModuleInfoMachO &MMIMacho =
653 
654  // Output stubs for dynamically-linked functions.
656 
657  // Output stubs for external and common global variables.
658  Stubs = MMIMacho.GetGVStubList();
659  if (!Stubs.empty()) {
660  OutStreamer.SwitchSection(MMI->getContext().getMachOSection(
661  "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS,
663 
664  for (auto &Stub : Stubs)
665  emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
666 
667  Stubs.clear();
668  OutStreamer.AddBlankLine();
669  }
670 }
671 
673  const Triple &TT = TM.getTargetTriple();
674 
675  if (TT.isOSBinFormatMachO()) {
676  // Mach-O uses non-lazy symbol stubs to encode per-TU information into
677  // global table for symbol lookup.
679 
680  // Emit stack and fault map information.
681  emitStackMaps(SM);
683 
684  // This flag tells the linker that no global symbols contain code that fall
685  // through to other global symbols (e.g. an implementation of multiple entry
686  // points). If this doesn't occur, the linker can safely perform dead code
687  // stripping. Since LLVM never generates code that does this, it is always
688  // safe to set.
689  OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
690  } else if (TT.isOSBinFormatCOFF()) {
691  if (MMI->usesMSVCFloatingPoint()) {
692  // In Windows' libcmt.lib, there is a file which is linked in only if the
693  // symbol _fltused is referenced. Linking this in causes some
694  // side-effects:
695  //
696  // 1. For x86-32, it will set the x87 rounding mode to 53-bit instead of
697  // 64-bit mantissas at program start.
698  //
699  // 2. It links in support routines for floating-point in scanf and printf.
700  //
701  // MSVC emits an undefined reference to _fltused when there are any
702  // floating point operations in the program (including calls). A program
703  // that only has: `scanf("%f", &global_float);` may fail to trigger this,
704  // but oh well...that's a documented issue.
706  (TT.getArch() == Triple::x86) ? "__fltused" : "_fltused";
707  MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName);
708  OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
709  return;
710  }
711  emitStackMaps(SM);
712  } else if (TT.isOSBinFormatELF()) {
713  emitStackMaps(SM);
715  }
716 }
717 
718 //===----------------------------------------------------------------------===//
719 // Target Registry Stuff
720 //===----------------------------------------------------------------------===//
721 
722 // Force static initialization.
723 extern "C" void LLVMInitializeX86AsmPrinter() {
726 }
unsigned getTargetFlags() const
MO_SECREL - On a symbol operand this indicates that the immediate is the offset from beginning of sec...
Definition: X86BaseInfo.h:225
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:206
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
bool is64Bit() const
Is this x86_64? (disregarding specific ABI / programming model)
Definition: X86Subtarget.h:521
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")
void LLVMInitializeX86AsmPrinter()
bool hasLocalLinkage() const
Definition: GlobalValue.h:435
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:235
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
SI Whole Quad Mode
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:322
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:64
MO_TLSLD - On a symbol operand this indicates that the immediate is the offset of the GOT entry with ...
Definition: X86BaseInfo.h:133
virtual void AddBlankLine()
AddBlankLine - Emit a blank line to a .s file to pretty it up.
Definition: MCStreamer.h:332
MO_TLVP_PIC_BASE - On a symbol operand this indicates that the immediate is some TLS offset from the ...
Definition: X86BaseInfo.h:219
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:602
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
const X86InstrInfo * getInstrInfo() const override
Definition: X86Subtarget.h:480
unsigned getReg() const
getReg - Returns the register number.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - Emit the function body.
Address of indexed Jump Table for switch.
unsigned Reg
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
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:107
MO_DTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry with...
Definition: X86BaseInfo.h:176
static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO, raw_ostream &O)
printSymbolOperand - Print a raw symbol reference operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
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:250
MO_DARWIN_NONLAZY_PIC_BASE - On a symbol operand "FOO", this indicates that the reference is actually...
Definition: X86BaseInfo.h:207
Definition: BitVector.h:937
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI, unsigned OpNo, raw_ostream &O)
printPCRelImm - This is used to print an immediate value that ends up being encoded as a pc-relative ...
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
bool isPICStyleRIPRel() const
Definition: X86Subtarget.h:775
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:160
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:51
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:272
MO_GOT - On a symbol operand this indicates that the immediate is the offset to the GOT entry for the...
Definition: X86BaseInfo.h:92
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:289
IntType getInt() const
const MCContext & getContext() const
bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition: Triple.cpp:1272
static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI, unsigned Op, raw_ostream &O, const char *Modifier=nullptr)
MO_GOTTPOFF - On a symbol operand this indicates that the immediate is the offset of the GOT entry wi...
Definition: X86BaseInfo.h:151
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)
static void printOperand(X86AsmPrinter &P, const MachineInstr *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr, unsigned AsmVariant=0)
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.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS) override
PrintAsmOperand - Print out an operand for an inline asm expression.
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:123
MO_DARWIN_NONLAZY - On a symbol operand "FOO", this indicates that the reference is actually to the "...
Definition: X86BaseInfo.h:202
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:154
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:99
unsigned getCodeViewFlag() const
Returns the CodeView Version by checking module flags.
Definition: Module.cpp:468
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:188
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition: AsmPrinter.h:112
MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a relocation of: SYMBOL_LABEL + [...
Definition: X86BaseInfo.h:80
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:84
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:607
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:192
MO_TLVP - On a symbol operand this indicates that the immediate is some TLS offset.
Definition: X86BaseInfo.h:213
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:143
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...
void EmitAlignment(unsigned NumBits, const GlobalObject *GV=nullptr) const
Emit an alignment directive to the specified power of two boundary.
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:81
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
bool hasInternalLinkage() const
Definition: GlobalValue.h:433
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:50
const Triple & getTargetTriple() const
MO_NTPOFF - On a symbol operand this indicates that the immediate is the negative thread-pointer offs...
Definition: X86BaseInfo.h:184
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:612
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:123
const X86RegisterInfo * getRegisterInfo() const override
Definition: X86Subtarget.h:490
MO_TPOFF - On a symbol operand this indicates that the immediate is the thread-pointer offset for the...
Definition: X86BaseInfo.h:168
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.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:430
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:49
MachineOperand class - Representation of each machine instruction operand.
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:32
static SectionKind getMetadata()
Definition: SectionKind.h:178
EnvironmentType getEnvironment() const
getEnvironment - Get the parsed environment type of this triple.
Definition: Triple.h:307
bool isTargetWin32() const
Definition: X86Subtarget.h:772
int64_t getImm() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
unsigned getX86SubSuperRegister(unsigned, unsigned, bool High=false)
Returns the sub or super register of a specific X86 register.
static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI, unsigned Op, raw_ostream &O, const char *Modifier=nullptr)
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:99
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:160
.type _foo,
Definition: MCDirectives.h:30
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:122
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1268
static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI, unsigned Op, raw_ostream &O, const char *Modifier=nullptr, unsigned AsmVariant=1)
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
AddrSegmentReg - The operand # of the segment in the memory operand.
Definition: X86BaseInfo.h:38
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:123
bool isTargetCOFF() const
Definition: X86Subtarget.h:729
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:114
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:247
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:388
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
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:346
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:58
MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the immediate should get the value of th...
Definition: X86BaseInfo.h:85
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Address of indexed Constant in Constant Pool.
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:413
Target & getTheX86_64Target()
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
This class can be derived from and used by targets to hold private target-specific information for ea...
const X86Subtarget & getSubtarget() const
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
MO_DLLIMPORT - On a symbol operand "FOO", this indicates that the reference is actually to the "__imp...
Definition: X86BaseInfo.h:197
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