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