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