ad4e8a6c44671686f4635bd5f053a4710d319286
[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     bool Intrn = MF.getFunction()->hasInternalLinkage();
62     OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
63     OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::C_STAT : COFF::C_EXT);
64     OutStreamer.EmitCOFFSymbolType(COFF::DT_FCN << COFF::N_BTSHFT);
65     OutStreamer.EndCOFFSymbolDef();
66   }
67
68   // Have common code print out the function header with linkage info etc.
69   EmitFunctionHeader();
70
71   // Emit the rest of the function body.
72   EmitFunctionBody();
73
74   // We didn't modify anything.
75   return false;
76 }
77
78 /// printSymbolOperand - Print a raw symbol reference operand.  This handles
79 /// jump tables, constant pools, global address and external symbols, all of
80 /// which print to a label with various suffixes for relocation types etc.
81 void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO,
82                                        raw_ostream &O) {
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(), O);
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(), O);
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(O);
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(O);
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                                     raw_ostream &O) {
211   const MachineOperand &MO = MI->getOperand(OpNo);
212   switch (MO.getType()) {
213   default: llvm_unreachable("Unknown pcrel immediate operand");
214   case MachineOperand::MO_Immediate:
215     O << MO.getImm();
216     return;
217   case MachineOperand::MO_MachineBasicBlock:
218     O << *MO.getMBB()->getSymbol();
219     return;
220   case MachineOperand::MO_GlobalAddress:
221   case MachineOperand::MO_ExternalSymbol:
222     printSymbolOperand(MO, O);
223     return;
224   }
225 }
226
227
228 void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
229                                  raw_ostream &O, const char *Modifier) {
230   const MachineOperand &MO = MI->getOperand(OpNo);
231   switch (MO.getType()) {
232   default: llvm_unreachable("unknown operand type!");
233   case MachineOperand::MO_Register: {
234     O << '%';
235     unsigned Reg = MO.getReg();
236     if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
237       EVT VT = (strcmp(Modifier+6,"64") == 0) ?
238         MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
239                     ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
240       Reg = getX86SubSuperRegister(Reg, VT);
241     }
242     O << X86ATTInstPrinter::getRegisterName(Reg);
243     return;
244   }
245
246   case MachineOperand::MO_Immediate:
247     O << '$' << MO.getImm();
248     return;
249
250   case MachineOperand::MO_JumpTableIndex:
251   case MachineOperand::MO_ConstantPoolIndex:
252   case MachineOperand::MO_GlobalAddress: 
253   case MachineOperand::MO_ExternalSymbol: {
254     O << '$';
255     printSymbolOperand(MO, O);
256     break;
257   }
258   }
259 }
260
261 void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
262                                raw_ostream &O) {
263   unsigned char value = MI->getOperand(Op).getImm();
264   assert(value <= 7 && "Invalid ssecc argument!");
265   switch (value) {
266   case 0: O << "eq"; break;
267   case 1: O << "lt"; break;
268   case 2: O << "le"; break;
269   case 3: O << "unord"; break;
270   case 4: O << "neq"; break;
271   case 5: O << "nlt"; break;
272   case 6: O << "nle"; break;
273   case 7: O << "ord"; break;
274   }
275 }
276
277 void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
278                                          raw_ostream &O, const char *Modifier) {
279   const MachineOperand &BaseReg  = MI->getOperand(Op);
280   const MachineOperand &IndexReg = MI->getOperand(Op+2);
281   const MachineOperand &DispSpec = MI->getOperand(Op+3);
282
283   // If we really don't want to print out (rip), don't.
284   bool HasBaseReg = BaseReg.getReg() != 0;
285   if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
286       BaseReg.getReg() == X86::RIP)
287     HasBaseReg = false;
288   
289   // HasParenPart - True if we will print out the () part of the mem ref.
290   bool HasParenPart = IndexReg.getReg() || HasBaseReg;
291   
292   if (DispSpec.isImm()) {
293     int DispVal = DispSpec.getImm();
294     if (DispVal || !HasParenPart)
295       O << DispVal;
296   } else {
297     assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
298            DispSpec.isJTI() || DispSpec.isSymbol());
299     printSymbolOperand(MI->getOperand(Op+3), O);
300   }
301
302   if (HasParenPart) {
303     assert(IndexReg.getReg() != X86::ESP &&
304            "X86 doesn't allow scaling by ESP");
305
306     O << '(';
307     if (HasBaseReg)
308       printOperand(MI, Op, O, Modifier);
309
310     if (IndexReg.getReg()) {
311       O << ',';
312       printOperand(MI, Op+2, O, Modifier);
313       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
314       if (ScaleVal != 1)
315         O << ',' << ScaleVal;
316     }
317     O << ')';
318   }
319 }
320
321 void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
322                                       raw_ostream &O, const char *Modifier) {
323   assert(isMem(MI, Op) && "Invalid memory reference!");
324   const MachineOperand &Segment = MI->getOperand(Op+4);
325   if (Segment.getReg()) {
326     printOperand(MI, Op+4, O, Modifier);
327     O << ':';
328   }
329   printLeaMemReference(MI, Op, O, Modifier);
330 }
331
332 void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op,
333                                   raw_ostream &O) {
334   PrintPICBaseSymbol(O);
335   O << '\n';
336   PrintPICBaseSymbol(O);
337   O << ':';
338 }
339
340 bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
341                                       raw_ostream &O) {
342   unsigned Reg = MO.getReg();
343   switch (Mode) {
344   default: return true;  // Unknown mode.
345   case 'b': // Print QImode register
346     Reg = getX86SubSuperRegister(Reg, MVT::i8);
347     break;
348   case 'h': // Print QImode high register
349     Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
350     break;
351   case 'w': // Print HImode register
352     Reg = getX86SubSuperRegister(Reg, MVT::i16);
353     break;
354   case 'k': // Print SImode register
355     Reg = getX86SubSuperRegister(Reg, MVT::i32);
356     break;
357   case 'q': // Print DImode register
358     Reg = getX86SubSuperRegister(Reg, MVT::i64);
359     break;
360   }
361
362   O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
363   return false;
364 }
365
366 /// PrintAsmOperand - Print out an operand for an inline asm expression.
367 ///
368 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
369                                     unsigned AsmVariant,
370                                     const char *ExtraCode, raw_ostream &O) {
371   // Does this asm operand have a single letter operand modifier?
372   if (ExtraCode && ExtraCode[0]) {
373     if (ExtraCode[1] != 0) return true; // Unknown modifier.
374
375     const MachineOperand &MO = MI->getOperand(OpNo);
376     
377     switch (ExtraCode[0]) {
378     default: return true;  // Unknown modifier.
379     case 'a': // This is an address.  Currently only 'i' and 'r' are expected.
380       if (MO.isImm()) {
381         O << MO.getImm();
382         return false;
383       } 
384       if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
385         printSymbolOperand(MO, O);
386         return false;
387       }
388       if (MO.isReg()) {
389         O << '(';
390         printOperand(MI, OpNo, O);
391         O << ')';
392         return false;
393       }
394       return true;
395
396     case 'c': // Don't print "$" before a global var name or constant.
397       if (MO.isImm())
398         O << MO.getImm();
399       else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
400         printSymbolOperand(MO, O);
401       else
402         printOperand(MI, OpNo, O);
403       return false;
404
405     case 'A': // Print '*' before a register (it must be a register)
406       if (MO.isReg()) {
407         O << '*';
408         printOperand(MI, OpNo, O);
409         return false;
410       }
411       return true;
412
413     case 'b': // Print QImode register
414     case 'h': // Print QImode high register
415     case 'w': // Print HImode register
416     case 'k': // Print SImode register
417     case 'q': // Print DImode register
418       if (MO.isReg())
419         return printAsmMRegister(MO, ExtraCode[0], O);
420       printOperand(MI, OpNo, O);
421       return false;
422
423     case 'P': // This is the operand of a call, treat specially.
424       print_pcrel_imm(MI, OpNo, O);
425       return false;
426
427     case 'n':  // Negate the immediate or print a '-' before the operand.
428       // Note: this is a temporary solution. It should be handled target
429       // independently as part of the 'MC' work.
430       if (MO.isImm()) {
431         O << -MO.getImm();
432         return false;
433       }
434       O << '-';
435     }
436   }
437
438   printOperand(MI, OpNo, O);
439   return false;
440 }
441
442 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
443                                           unsigned OpNo, unsigned AsmVariant,
444                                           const char *ExtraCode,
445                                           raw_ostream &O) {
446   if (ExtraCode && ExtraCode[0]) {
447     if (ExtraCode[1] != 0) return true; // Unknown modifier.
448
449     switch (ExtraCode[0]) {
450     default: return true;  // Unknown modifier.
451     case 'b': // Print QImode register
452     case 'h': // Print QImode high register
453     case 'w': // Print HImode register
454     case 'k': // Print SImode register
455     case 'q': // Print SImode register
456       // These only apply to registers, ignore on mem.
457       break;
458     case 'P': // Don't print @PLT, but do print as memory.
459       printMemReference(MI, OpNo, O, "no-rip");
460       return false;
461     }
462   }
463   printMemReference(MI, OpNo, O);
464   return false;
465 }
466
467 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
468   if (Subtarget->isTargetDarwin())
469     OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
470 }
471
472
473 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
474   if (Subtarget->isTargetDarwin()) {
475     // All darwin targets use mach-o.
476     MachineModuleInfoMachO &MMIMacho =
477       MMI->getObjFileInfo<MachineModuleInfoMachO>();
478     
479     // Output stubs for dynamically-linked functions.
480     MachineModuleInfoMachO::SymbolListTy Stubs;
481
482     Stubs = MMIMacho.GetFnStubList();
483     if (!Stubs.empty()) {
484       const MCSection *TheSection = 
485         OutContext.getMachOSection("__IMPORT", "__jump_table",
486                                    MCSectionMachO::S_SYMBOL_STUBS |
487                                    MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
488                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
489                                    5, SectionKind::getMetadata());
490       OutStreamer.SwitchSection(TheSection);
491
492       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
493         // L_foo$stub:
494         OutStreamer.EmitLabel(Stubs[i].first);
495         //   .indirect_symbol _foo
496         OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
497                                         MCSA_IndirectSymbol);
498         // hlt; hlt; hlt; hlt; hlt     hlt = 0xf4 = -12.
499         const char HltInsts[] = { -12, -12, -12, -12, -12 };
500         OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
501       }
502       
503       Stubs.clear();
504       OutStreamer.AddBlankLine();
505     }
506
507     // Output stubs for external and common global variables.
508     Stubs = MMIMacho.GetGVStubList();
509     if (!Stubs.empty()) {
510       const MCSection *TheSection = 
511         OutContext.getMachOSection("__IMPORT", "__pointers",
512                                    MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
513                                    SectionKind::getMetadata());
514       OutStreamer.SwitchSection(TheSection);
515
516       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
517         // L_foo$non_lazy_ptr:
518         OutStreamer.EmitLabel(Stubs[i].first);
519         // .indirect_symbol _foo
520         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
521         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
522                                         MCSA_IndirectSymbol);
523         // .long 0
524         if (MCSym.getInt())
525           // External to current translation unit.
526           OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
527         else
528           // Internal to current translation unit.
529           //
530           // When we place the LSDA into the TEXT section, the type info
531           // pointers need to be indirect and pc-rel. We accomplish this by
532           // using NLPs.  However, sometimes the types are local to the file. So
533           // we need to fill in the value for the NLP in those cases.
534           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
535                                                         OutContext),
536                                 4/*size*/, 0/*addrspace*/);
537       }
538       Stubs.clear();
539       OutStreamer.AddBlankLine();
540     }
541
542     Stubs = MMIMacho.GetHiddenGVStubList();
543     if (!Stubs.empty()) {
544       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
545       EmitAlignment(2);
546
547       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
548         // L_foo$non_lazy_ptr:
549         OutStreamer.EmitLabel(Stubs[i].first);
550         // .long _foo
551         OutStreamer.EmitValue(MCSymbolRefExpr::
552                               Create(Stubs[i].second.getPointer(),
553                                      OutContext),
554                               4/*size*/, 0/*addrspace*/);
555       }
556       Stubs.clear();
557       OutStreamer.AddBlankLine();
558     }
559
560     // Funny Darwin hack: This flag tells the linker that no global symbols
561     // contain code that falls through to other global symbols (e.g. the obvious
562     // implementation of multiple entry points).  If this doesn't occur, the
563     // linker can safely perform dead code stripping.  Since LLVM never
564     // generates code that does this, it is always safe to set.
565     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
566   }
567
568   if (Subtarget->isTargetCOFF()) {
569     X86COFFMachineModuleInfo &COFFMMI =
570       MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
571
572     // Emit type information for external functions
573     typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
574     for (externals_iterator I = COFFMMI.externals_begin(),
575                             E = COFFMMI.externals_end();
576                             I != E; ++I) {
577       OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
578       OutStreamer.EmitCOFFSymbolStorageClass(COFF::C_EXT);
579       OutStreamer.EmitCOFFSymbolType(COFF::DT_FCN << COFF::N_BTSHFT);
580       OutStreamer.EndCOFFSymbolDef();
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 }