Merging r261039:
[oota-llvm.git] / lib / Target / X86 / X86AsmPrinter.cpp
1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T 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 X86 machine code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "X86AsmPrinter.h"
16 #include "InstPrinter/X86ATTInstPrinter.h"
17 #include "MCTargetDesc/X86BaseInfo.h"
18 #include "X86InstrInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
23 #include "llvm/CodeGen/MachineValueType.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCExpr.h"
33 #include "llvm/MC/MCSectionCOFF.h"
34 #include "llvm/MC/MCSectionMachO.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/Support/COFF.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/TargetRegistry.h"
41 using namespace llvm;
42
43 //===----------------------------------------------------------------------===//
44 // Primitive Helper Functions.
45 //===----------------------------------------------------------------------===//
46
47 /// runOnMachineFunction - Emit the function body.
48 ///
49 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
50   Subtarget = &MF.getSubtarget<X86Subtarget>();
51
52   SMShadowTracker.startFunction(MF);
53
54   SetupMachineFunction(MF);
55
56   if (Subtarget->isTargetCOFF()) {
57     bool Intrn = MF.getFunction()->hasInternalLinkage();
58     OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
59     OutStreamer->EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
60                                             : COFF::IMAGE_SYM_CLASS_EXTERNAL);
61     OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
62                                                << COFF::SCT_COMPLEX_TYPE_SHIFT);
63     OutStreamer->EndCOFFSymbolDef();
64   }
65
66   // Emit the rest of the function body.
67   EmitFunctionBody();
68
69   // We didn't modify anything.
70   return false;
71 }
72
73 /// printSymbolOperand - Print a raw symbol reference operand.  This handles
74 /// jump tables, constant pools, global address and external symbols, all of
75 /// which print to a label with various suffixes for relocation types etc.
76 static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO,
77                                raw_ostream &O) {
78   switch (MO.getType()) {
79   default: llvm_unreachable("unknown symbol type!");
80   case MachineOperand::MO_ConstantPoolIndex:
81     P.GetCPISymbol(MO.getIndex())->print(O, P.MAI);
82     P.printOffset(MO.getOffset(), O);
83     break;
84   case MachineOperand::MO_GlobalAddress: {
85     const GlobalValue *GV = MO.getGlobal();
86
87     MCSymbol *GVSym;
88     if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
89       GVSym = P.getSymbolWithGlobalValueBase(GV, "$stub");
90     else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
91              MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
92              MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
93       GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
94     else
95       GVSym = P.getSymbol(GV);
96
97     // Handle dllimport linkage.
98     if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
99       GVSym =
100           P.OutContext.getOrCreateSymbol(Twine("__imp_") + GVSym->getName());
101
102     if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
103         MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
104       MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
105       MachineModuleInfoImpl::StubValueTy &StubSym =
106           P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
107       if (!StubSym.getPointer())
108         StubSym = MachineModuleInfoImpl::
109           StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
110     } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
111       MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
112       MachineModuleInfoImpl::StubValueTy &StubSym =
113           P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(
114               Sym);
115       if (!StubSym.getPointer())
116         StubSym = MachineModuleInfoImpl::
117           StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
118     } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
119       MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$stub");
120       MachineModuleInfoImpl::StubValueTy &StubSym =
121           P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
122       if (!StubSym.getPointer())
123         StubSym = MachineModuleInfoImpl::
124           StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
125     }
126
127     // If the name begins with a dollar-sign, enclose it in parens.  We do this
128     // to avoid having it look like an integer immediate to the assembler.
129     if (GVSym->getName()[0] != '$')
130       GVSym->print(O, P.MAI);
131     else {
132       O << '(';
133       GVSym->print(O, P.MAI);
134       O << ')';
135     }
136     P.printOffset(MO.getOffset(), O);
137     break;
138   }
139   }
140
141   switch (MO.getTargetFlags()) {
142   default:
143     llvm_unreachable("Unknown target flag on GV operand");
144   case X86II::MO_NO_FLAG:    // No flag.
145     break;
146   case X86II::MO_DARWIN_NONLAZY:
147   case X86II::MO_DLLIMPORT:
148   case X86II::MO_DARWIN_STUB:
149     // These affect the name of the symbol, not any suffix.
150     break;
151   case X86II::MO_GOT_ABSOLUTE_ADDRESS:
152     O << " + [.-";
153     P.MF->getPICBaseSymbol()->print(O, P.MAI);
154     O << ']';
155     break;
156   case X86II::MO_PIC_BASE_OFFSET:
157   case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
158   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
159     O << '-';
160     P.MF->getPICBaseSymbol()->print(O, P.MAI);
161     break;
162   case X86II::MO_TLSGD:     O << "@TLSGD";     break;
163   case X86II::MO_TLSLD:     O << "@TLSLD";     break;
164   case X86II::MO_TLSLDM:    O << "@TLSLDM";    break;
165   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
166   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
167   case X86II::MO_TPOFF:     O << "@TPOFF";     break;
168   case X86II::MO_DTPOFF:    O << "@DTPOFF";    break;
169   case X86II::MO_NTPOFF:    O << "@NTPOFF";    break;
170   case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
171   case X86II::MO_GOTPCREL:  O << "@GOTPCREL";  break;
172   case X86II::MO_GOT:       O << "@GOT";       break;
173   case X86II::MO_GOTOFF:    O << "@GOTOFF";    break;
174   case X86II::MO_PLT:       O << "@PLT";       break;
175   case X86II::MO_TLVP:      O << "@TLVP";      break;
176   case X86II::MO_TLVP_PIC_BASE:
177     O << "@TLVP" << '-';
178     P.MF->getPICBaseSymbol()->print(O, P.MAI);
179     break;
180   case X86II::MO_SECREL:    O << "@SECREL32";  break;
181   }
182 }
183
184 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
185                          unsigned OpNo, raw_ostream &O,
186                          const char *Modifier = nullptr, unsigned AsmVariant = 0);
187
188 /// printPCRelImm - This is used to print an immediate value that ends up
189 /// being encoded as a pc-relative value.  These print slightly differently, for
190 /// example, a $ is not emitted.
191 static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
192                           unsigned OpNo, raw_ostream &O) {
193   const MachineOperand &MO = MI->getOperand(OpNo);
194   switch (MO.getType()) {
195   default: llvm_unreachable("Unknown pcrel immediate operand");
196   case MachineOperand::MO_Register:
197     // pc-relativeness was handled when computing the value in the reg.
198     printOperand(P, MI, OpNo, O);
199     return;
200   case MachineOperand::MO_Immediate:
201     O << MO.getImm();
202     return;
203   case MachineOperand::MO_GlobalAddress:
204     printSymbolOperand(P, MO, O);
205     return;
206   }
207 }
208
209 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
210                          unsigned OpNo, raw_ostream &O, const char *Modifier,
211                          unsigned AsmVariant) {
212   const MachineOperand &MO = MI->getOperand(OpNo);
213   switch (MO.getType()) {
214   default: llvm_unreachable("unknown operand type!");
215   case MachineOperand::MO_Register: {
216     // FIXME: Enumerating AsmVariant, so we can remove magic number.
217     if (AsmVariant == 0) O << '%';
218     unsigned Reg = MO.getReg();
219     if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
220       unsigned Size = (strcmp(Modifier+6,"64") == 0) ? 64 :
221                       (strcmp(Modifier+6,"32") == 0) ? 32 :
222                       (strcmp(Modifier+6,"16") == 0) ? 16 : 8;
223       Reg = getX86SubSuperRegister(Reg, Size);
224     }
225     O << X86ATTInstPrinter::getRegisterName(Reg);
226     return;
227   }
228
229   case MachineOperand::MO_Immediate:
230     if (AsmVariant == 0) O << '$';
231     O << MO.getImm();
232     return;
233
234   case MachineOperand::MO_GlobalAddress: {
235     if (AsmVariant == 0) O << '$';
236     printSymbolOperand(P, MO, O);
237     break;
238   }
239   }
240 }
241
242 static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI,
243                                  unsigned Op, raw_ostream &O,
244                                  const char *Modifier = nullptr) {
245   const MachineOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
246   const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
247   const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
248
249   // If we really don't want to print out (rip), don't.
250   bool HasBaseReg = BaseReg.getReg() != 0;
251   if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
252       BaseReg.getReg() == X86::RIP)
253     HasBaseReg = false;
254
255   // HasParenPart - True if we will print out the () part of the mem ref.
256   bool HasParenPart = IndexReg.getReg() || HasBaseReg;
257
258   switch (DispSpec.getType()) {
259   default:
260     llvm_unreachable("unknown operand type!");
261   case MachineOperand::MO_Immediate: {
262     int DispVal = DispSpec.getImm();
263     if (DispVal || !HasParenPart)
264       O << DispVal;
265     break;
266   }
267   case MachineOperand::MO_GlobalAddress:
268   case MachineOperand::MO_ConstantPoolIndex:
269     printSymbolOperand(P, DispSpec, O);
270   }
271
272   if (Modifier && strcmp(Modifier, "H") == 0)
273     O << "+8";
274
275   if (HasParenPart) {
276     assert(IndexReg.getReg() != X86::ESP &&
277            "X86 doesn't allow scaling by ESP");
278
279     O << '(';
280     if (HasBaseReg)
281       printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier);
282
283     if (IndexReg.getReg()) {
284       O << ',';
285       printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier);
286       unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
287       if (ScaleVal != 1)
288         O << ',' << ScaleVal;
289     }
290     O << ')';
291   }
292 }
293
294 static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
295                               unsigned Op, raw_ostream &O,
296                               const char *Modifier = nullptr) {
297   assert(isMem(MI, Op) && "Invalid memory reference!");
298   const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg);
299   if (Segment.getReg()) {
300     printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier);
301     O << ':';
302   }
303   printLeaMemReference(P, MI, Op, O, Modifier);
304 }
305
306 static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI,
307                                    unsigned Op, raw_ostream &O,
308                                    const char *Modifier = nullptr,
309                                    unsigned AsmVariant = 1) {
310   const MachineOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
311   unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
312   const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
313   const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
314   const MachineOperand &SegReg   = MI->getOperand(Op+X86::AddrSegmentReg);
315
316   // If this has a segment register, print it.
317   if (SegReg.getReg()) {
318     printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant);
319     O << ':';
320   }
321
322   O << '[';
323
324   bool NeedPlus = false;
325   if (BaseReg.getReg()) {
326     printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant);
327     NeedPlus = true;
328   }
329
330   if (IndexReg.getReg()) {
331     if (NeedPlus) O << " + ";
332     if (ScaleVal != 1)
333       O << ScaleVal << '*';
334     printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant);
335     NeedPlus = true;
336   }
337
338   if (!DispSpec.isImm()) {
339     if (NeedPlus) O << " + ";
340     printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant);
341   } else {
342     int64_t DispVal = DispSpec.getImm();
343     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
344       if (NeedPlus) {
345         if (DispVal > 0)
346           O << " + ";
347         else {
348           O << " - ";
349           DispVal = -DispVal;
350         }
351       }
352       O << DispVal;
353     }
354   }
355   O << ']';
356 }
357
358 static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO,
359                               char Mode, raw_ostream &O) {
360   unsigned Reg = MO.getReg();
361   switch (Mode) {
362   default: return true;  // Unknown mode.
363   case 'b': // Print QImode register
364     Reg = getX86SubSuperRegister(Reg, 8);
365     break;
366   case 'h': // Print QImode high register
367     Reg = getX86SubSuperRegister(Reg, 8, true);
368     break;
369   case 'w': // Print HImode register
370     Reg = getX86SubSuperRegister(Reg, 16);
371     break;
372   case 'k': // Print SImode register
373     Reg = getX86SubSuperRegister(Reg, 32);
374     break;
375   case 'q':
376     // Print 64-bit register names if 64-bit integer registers are available.
377     // Otherwise, print 32-bit register names.
378     Reg = getX86SubSuperRegister(Reg, P.getSubtarget().is64Bit() ? 64 : 32);
379     break;
380   }
381
382   O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
383   return false;
384 }
385
386 /// PrintAsmOperand - Print out an operand for an inline asm expression.
387 ///
388 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
389                                     unsigned AsmVariant,
390                                     const char *ExtraCode, raw_ostream &O) {
391   // Does this asm operand have a single letter operand modifier?
392   if (ExtraCode && ExtraCode[0]) {
393     if (ExtraCode[1] != 0) return true; // Unknown modifier.
394
395     const MachineOperand &MO = MI->getOperand(OpNo);
396
397     switch (ExtraCode[0]) {
398     default:
399       // See if this is a generic print operand
400       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
401     case 'a': // This is an address.  Currently only 'i' and 'r' are expected.
402       switch (MO.getType()) {
403       default:
404         return true;
405       case MachineOperand::MO_Immediate:
406         O << MO.getImm();
407         return false;
408       case MachineOperand::MO_ConstantPoolIndex:
409       case MachineOperand::MO_JumpTableIndex:
410       case MachineOperand::MO_ExternalSymbol:
411         llvm_unreachable("unexpected operand type!");
412       case MachineOperand::MO_GlobalAddress:
413         printSymbolOperand(*this, MO, O);
414         if (Subtarget->isPICStyleRIPRel())
415           O << "(%rip)";
416         return false;
417       case MachineOperand::MO_Register:
418         O << '(';
419         printOperand(*this, MI, OpNo, O);
420         O << ')';
421         return false;
422       }
423
424     case 'c': // Don't print "$" before a global var name or constant.
425       switch (MO.getType()) {
426       default:
427         printOperand(*this, MI, OpNo, O);
428         break;
429       case MachineOperand::MO_Immediate:
430         O << MO.getImm();
431         break;
432       case MachineOperand::MO_ConstantPoolIndex:
433       case MachineOperand::MO_JumpTableIndex:
434       case MachineOperand::MO_ExternalSymbol:
435         llvm_unreachable("unexpected operand type!");
436       case MachineOperand::MO_GlobalAddress:
437         printSymbolOperand(*this, MO, O);
438         break;
439       }
440       return false;
441
442     case 'A': // Print '*' before a register (it must be a register)
443       if (MO.isReg()) {
444         O << '*';
445         printOperand(*this, MI, OpNo, O);
446         return false;
447       }
448       return true;
449
450     case 'b': // Print QImode register
451     case 'h': // Print QImode high register
452     case 'w': // Print HImode register
453     case 'k': // Print SImode register
454     case 'q': // Print DImode register
455       if (MO.isReg())
456         return printAsmMRegister(*this, MO, ExtraCode[0], O);
457       printOperand(*this, MI, OpNo, O);
458       return false;
459
460     case 'P': // This is the operand of a call, treat specially.
461       printPCRelImm(*this, MI, OpNo, O);
462       return false;
463
464     case 'n':  // Negate the immediate or print a '-' before the operand.
465       // Note: this is a temporary solution. It should be handled target
466       // independently as part of the 'MC' work.
467       if (MO.isImm()) {
468         O << -MO.getImm();
469         return false;
470       }
471       O << '-';
472     }
473   }
474
475   printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant);
476   return false;
477 }
478
479 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
480                                           unsigned OpNo, unsigned AsmVariant,
481                                           const char *ExtraCode,
482                                           raw_ostream &O) {
483   if (AsmVariant) {
484     printIntelMemReference(*this, MI, OpNo, O);
485     return false;
486   }
487
488   if (ExtraCode && ExtraCode[0]) {
489     if (ExtraCode[1] != 0) return true; // Unknown modifier.
490
491     switch (ExtraCode[0]) {
492     default: return true;  // Unknown modifier.
493     case 'b': // Print QImode register
494     case 'h': // Print QImode high register
495     case 'w': // Print HImode register
496     case 'k': // Print SImode register
497     case 'q': // Print SImode register
498       // These only apply to registers, ignore on mem.
499       break;
500     case 'H':
501       printMemReference(*this, MI, OpNo, O, "H");
502       return false;
503     case 'P': // Don't print @PLT, but do print as memory.
504       printMemReference(*this, MI, OpNo, O, "no-rip");
505       return false;
506     }
507   }
508   printMemReference(*this, MI, OpNo, O);
509   return false;
510 }
511
512 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
513   const Triple &TT = TM.getTargetTriple();
514
515   if (TT.isOSBinFormatMachO())
516     OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
517
518   if (TT.isOSBinFormatCOFF()) {
519     // Emit an absolute @feat.00 symbol.  This appears to be some kind of
520     // compiler features bitfield read by link.exe.
521     if (TT.getArch() == Triple::x86) {
522       MCSymbol *S = MMI->getContext().getOrCreateSymbol(StringRef("@feat.00"));
523       OutStreamer->BeginCOFFSymbolDef(S);
524       OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
525       OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
526       OutStreamer->EndCOFFSymbolDef();
527       // According to the PE-COFF spec, the LSB of this value marks the object
528       // for "registered SEH".  This means that all SEH handler entry points
529       // must be registered in .sxdata.  Use of any unregistered handlers will
530       // cause the process to terminate immediately.  LLVM does not know how to
531       // register any SEH handlers, so its object files should be safe.
532       OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
533       OutStreamer->EmitAssignment(
534           S, MCConstantExpr::create(int64_t(1), MMI->getContext()));
535     }
536   }
537   OutStreamer->EmitSyntaxDirective();
538 }
539
540 static void
541 emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
542                          MachineModuleInfoImpl::StubValueTy &MCSym) {
543   // L_foo$stub:
544   OutStreamer.EmitLabel(StubLabel);
545   //   .indirect_symbol _foo
546   OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
547
548   if (MCSym.getInt())
549     // External to current translation unit.
550     OutStreamer.EmitIntValue(0, 4/*size*/);
551   else
552     // Internal to current translation unit.
553     //
554     // When we place the LSDA into the TEXT section, the type info
555     // pointers need to be indirect and pc-rel. We accomplish this by
556     // using NLPs; however, sometimes the types are local to the file.
557     // We need to fill in the value for the NLP in those cases.
558     OutStreamer.EmitValue(
559         MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
560         4 /*size*/);
561 }
562
563 MCSymbol *X86AsmPrinter::GetCPISymbol(unsigned CPID) const {
564   if (Subtarget->isTargetKnownWindowsMSVC()) {
565     const MachineConstantPoolEntry &CPE =
566         MF->getConstantPool()->getConstants()[CPID];
567     if (!CPE.isMachineConstantPoolEntry()) {
568       const DataLayout &DL = MF->getDataLayout();
569       SectionKind Kind = CPE.getSectionKind(&DL);
570       const Constant *C = CPE.Val.ConstVal;
571       if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>(
572               getObjFileLowering().getSectionForConstant(DL, Kind, C))) {
573         if (MCSymbol *Sym = S->getCOMDATSymbol()) {
574           if (Sym->isUndefined())
575             OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
576           return Sym;
577         }
578       }
579     }
580   }
581
582   return AsmPrinter::GetCPISymbol(CPID);
583 }
584
585 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
586   const Triple &TT = TM.getTargetTriple();
587
588   if (TT.isOSBinFormatMachO()) {
589     // All darwin targets use mach-o.
590     MachineModuleInfoMachO &MMIMacho =
591         MMI->getObjFileInfo<MachineModuleInfoMachO>();
592
593     // Output stubs for dynamically-linked functions.
594     MachineModuleInfoMachO::SymbolListTy Stubs;
595
596     Stubs = MMIMacho.GetFnStubList();
597     if (!Stubs.empty()) {
598       MCSection *TheSection = OutContext.getMachOSection(
599           "__IMPORT", "__jump_table",
600           MachO::S_SYMBOL_STUBS | MachO::S_ATTR_SELF_MODIFYING_CODE |
601               MachO::S_ATTR_PURE_INSTRUCTIONS,
602           5, SectionKind::getMetadata());
603       OutStreamer->SwitchSection(TheSection);
604
605       for (const auto &Stub : Stubs) {
606         // L_foo$stub:
607         OutStreamer->EmitLabel(Stub.first);
608         //   .indirect_symbol _foo
609         OutStreamer->EmitSymbolAttribute(Stub.second.getPointer(),
610                                          MCSA_IndirectSymbol);
611         // hlt; hlt; hlt; hlt; hlt     hlt = 0xf4.
612         const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
613         OutStreamer->EmitBytes(StringRef(HltInsts, 5));
614       }
615
616       Stubs.clear();
617       OutStreamer->AddBlankLine();
618     }
619
620     // Output stubs for external and common global variables.
621     Stubs = MMIMacho.GetGVStubList();
622     if (!Stubs.empty()) {
623       MCSection *TheSection = OutContext.getMachOSection(
624           "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS,
625           SectionKind::getMetadata());
626       OutStreamer->SwitchSection(TheSection);
627
628       for (auto &Stub : Stubs)
629         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
630
631       Stubs.clear();
632       OutStreamer->AddBlankLine();
633     }
634
635     Stubs = MMIMacho.GetHiddenGVStubList();
636     if (!Stubs.empty()) {
637       MCSection *TheSection = OutContext.getMachOSection(
638           "__IMPORT", "__pointers", MachO::S_NON_LAZY_SYMBOL_POINTERS,
639           SectionKind::getMetadata());
640       OutStreamer->SwitchSection(TheSection);
641
642       for (auto &Stub : Stubs)
643         emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
644
645       Stubs.clear();
646       OutStreamer->AddBlankLine();
647     }
648
649     SM.serializeToStackMapSection();
650     FM.serializeToFaultMapSection();
651
652     // Funny Darwin hack: This flag tells the linker that no global symbols
653     // contain code that falls through to other global symbols (e.g. the obvious
654     // implementation of multiple entry points).  If this doesn't occur, the
655     // linker can safely perform dead code stripping.  Since LLVM never
656     // generates code that does this, it is always safe to set.
657     OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
658   }
659
660   if (TT.isKnownWindowsMSVCEnvironment() && MMI->usesVAFloatArgument()) {
661     StringRef SymbolName =
662         (TT.getArch() == Triple::x86_64) ? "_fltused" : "__fltused";
663     MCSymbol *S = MMI->getContext().getOrCreateSymbol(SymbolName);
664     OutStreamer->EmitSymbolAttribute(S, MCSA_Global);
665   }
666
667   if (TT.isOSBinFormatCOFF()) {
668     const TargetLoweringObjectFileCOFF &TLOFCOFF =
669         static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
670
671     std::string Flags;
672     raw_string_ostream FlagsOS(Flags);
673
674     for (const auto &Function : M)
675       TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Function, *Mang);
676     for (const auto &Global : M.globals())
677       TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Global, *Mang);
678     for (const auto &Alias : M.aliases())
679       TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Alias, *Mang);
680
681     FlagsOS.flush();
682
683     // Output collected flags.
684     if (!Flags.empty()) {
685       OutStreamer->SwitchSection(TLOFCOFF.getDrectveSection());
686       OutStreamer->EmitBytes(Flags);
687     }
688
689     SM.serializeToStackMapSection();
690   }
691
692   if (TT.isOSBinFormatELF()) {
693     SM.serializeToStackMapSection();
694     FM.serializeToFaultMapSection();
695   }
696 }
697
698 //===----------------------------------------------------------------------===//
699 // Target Registry Stuff
700 //===----------------------------------------------------------------------===//
701
702 // Force static initialization.
703 extern "C" void LLVMInitializeX86AsmPrinter() {
704   RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
705   RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
706 }