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