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