Fix PR7545 crash.
[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   case X86II::MO_TLVP:      O << "@TLVP";      break;
204   case X86II::MO_TLVP_PIC_BASE:
205     O << "@TLVP" << '-';
206     PrintPICBaseSymbol(O);
207     break;
208   }
209 }
210
211 /// print_pcrel_imm - This is used to print an immediate value that ends up
212 /// being encoded as a pc-relative value.  These print slightly differently, for
213 /// example, a $ is not emitted.
214 void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo,
215                                     raw_ostream &O) {
216   const MachineOperand &MO = MI->getOperand(OpNo);
217   switch (MO.getType()) {
218   default: llvm_unreachable("Unknown pcrel immediate operand");
219   case MachineOperand::MO_Immediate:
220     O << MO.getImm();
221     return;
222   case MachineOperand::MO_MachineBasicBlock:
223     O << *MO.getMBB()->getSymbol();
224     return;
225   case MachineOperand::MO_GlobalAddress:
226   case MachineOperand::MO_ExternalSymbol:
227     printSymbolOperand(MO, O);
228     return;
229   }
230 }
231
232
233 void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
234                                  raw_ostream &O, const char *Modifier) {
235   const MachineOperand &MO = MI->getOperand(OpNo);
236   switch (MO.getType()) {
237   default: llvm_unreachable("unknown operand type!");
238   case MachineOperand::MO_Register: {
239     O << '%';
240     unsigned Reg = MO.getReg();
241     if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
242       EVT VT = (strcmp(Modifier+6,"64") == 0) ?
243         MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
244                     ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
245       Reg = getX86SubSuperRegister(Reg, VT);
246     }
247     O << X86ATTInstPrinter::getRegisterName(Reg);
248     return;
249   }
250
251   case MachineOperand::MO_Immediate:
252     O << '$' << MO.getImm();
253     return;
254
255   case MachineOperand::MO_JumpTableIndex:
256   case MachineOperand::MO_ConstantPoolIndex:
257   case MachineOperand::MO_GlobalAddress: 
258   case MachineOperand::MO_ExternalSymbol: {
259     O << '$';
260     printSymbolOperand(MO, O);
261     break;
262   }
263   }
264 }
265
266 void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
267                                raw_ostream &O) {
268   unsigned char value = MI->getOperand(Op).getImm();
269   assert(value <= 7 && "Invalid ssecc argument!");
270   switch (value) {
271   case 0: O << "eq"; break;
272   case 1: O << "lt"; break;
273   case 2: O << "le"; break;
274   case 3: O << "unord"; break;
275   case 4: O << "neq"; break;
276   case 5: O << "nlt"; break;
277   case 6: O << "nle"; break;
278   case 7: O << "ord"; break;
279   }
280 }
281
282 void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
283                                          raw_ostream &O, const char *Modifier) {
284   const MachineOperand &BaseReg  = MI->getOperand(Op);
285   const MachineOperand &IndexReg = MI->getOperand(Op+2);
286   const MachineOperand &DispSpec = MI->getOperand(Op+3);
287
288   // If we really don't want to print out (rip), don't.
289   bool HasBaseReg = BaseReg.getReg() != 0;
290   if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
291       BaseReg.getReg() == X86::RIP)
292     HasBaseReg = false;
293   
294   // HasParenPart - True if we will print out the () part of the mem ref.
295   bool HasParenPart = IndexReg.getReg() || HasBaseReg;
296   
297   if (DispSpec.isImm()) {
298     int DispVal = DispSpec.getImm();
299     if (DispVal || !HasParenPart)
300       O << DispVal;
301   } else {
302     assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
303            DispSpec.isJTI() || DispSpec.isSymbol());
304     printSymbolOperand(MI->getOperand(Op+3), O);
305   }
306
307   if (HasParenPart) {
308     assert(IndexReg.getReg() != X86::ESP &&
309            "X86 doesn't allow scaling by ESP");
310
311     O << '(';
312     if (HasBaseReg)
313       printOperand(MI, Op, O, Modifier);
314
315     if (IndexReg.getReg()) {
316       O << ',';
317       printOperand(MI, Op+2, O, Modifier);
318       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
319       if (ScaleVal != 1)
320         O << ',' << ScaleVal;
321     }
322     O << ')';
323   }
324 }
325
326 void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
327                                       raw_ostream &O, const char *Modifier) {
328   assert(isMem(MI, Op) && "Invalid memory reference!");
329   const MachineOperand &Segment = MI->getOperand(Op+4);
330   if (Segment.getReg()) {
331     printOperand(MI, Op+4, O, Modifier);
332     O << ':';
333   }
334   printLeaMemReference(MI, Op, O, Modifier);
335 }
336
337 void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op,
338                                   raw_ostream &O) {
339   PrintPICBaseSymbol(O);
340   O << '\n';
341   PrintPICBaseSymbol(O);
342   O << ':';
343 }
344
345 bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
346                                       raw_ostream &O) {
347   unsigned Reg = MO.getReg();
348   switch (Mode) {
349   default: return true;  // Unknown mode.
350   case 'b': // Print QImode register
351     Reg = getX86SubSuperRegister(Reg, MVT::i8);
352     break;
353   case 'h': // Print QImode high register
354     Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
355     break;
356   case 'w': // Print HImode register
357     Reg = getX86SubSuperRegister(Reg, MVT::i16);
358     break;
359   case 'k': // Print SImode register
360     Reg = getX86SubSuperRegister(Reg, MVT::i32);
361     break;
362   case 'q': // Print DImode register
363     Reg = getX86SubSuperRegister(Reg, MVT::i64);
364     break;
365   }
366
367   O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
368   return false;
369 }
370
371 /// PrintAsmOperand - Print out an operand for an inline asm expression.
372 ///
373 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
374                                     unsigned AsmVariant,
375                                     const char *ExtraCode, raw_ostream &O) {
376   // Does this asm operand have a single letter operand modifier?
377   if (ExtraCode && ExtraCode[0]) {
378     if (ExtraCode[1] != 0) return true; // Unknown modifier.
379
380     const MachineOperand &MO = MI->getOperand(OpNo);
381     
382     switch (ExtraCode[0]) {
383     default: return true;  // Unknown modifier.
384     case 'a': // This is an address.  Currently only 'i' and 'r' are expected.
385       if (MO.isImm()) {
386         O << MO.getImm();
387         return false;
388       } 
389       if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
390         printSymbolOperand(MO, O);
391         return false;
392       }
393       if (MO.isReg()) {
394         O << '(';
395         printOperand(MI, OpNo, O);
396         O << ')';
397         return false;
398       }
399       return true;
400
401     case 'c': // Don't print "$" before a global var name or constant.
402       if (MO.isImm())
403         O << MO.getImm();
404       else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
405         printSymbolOperand(MO, O);
406       else
407         printOperand(MI, OpNo, O);
408       return false;
409
410     case 'A': // Print '*' before a register (it must be a register)
411       if (MO.isReg()) {
412         O << '*';
413         printOperand(MI, OpNo, O);
414         return false;
415       }
416       return true;
417
418     case 'b': // Print QImode register
419     case 'h': // Print QImode high register
420     case 'w': // Print HImode register
421     case 'k': // Print SImode register
422     case 'q': // Print DImode register
423       if (MO.isReg())
424         return printAsmMRegister(MO, ExtraCode[0], O);
425       printOperand(MI, OpNo, O);
426       return false;
427
428     case 'P': // This is the operand of a call, treat specially.
429       print_pcrel_imm(MI, OpNo, O);
430       return false;
431
432     case 'n':  // Negate the immediate or print a '-' before the operand.
433       // Note: this is a temporary solution. It should be handled target
434       // independently as part of the 'MC' work.
435       if (MO.isImm()) {
436         O << -MO.getImm();
437         return false;
438       }
439       O << '-';
440     }
441   }
442
443   printOperand(MI, OpNo, O);
444   return false;
445 }
446
447 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
448                                           unsigned OpNo, unsigned AsmVariant,
449                                           const char *ExtraCode,
450                                           raw_ostream &O) {
451   if (ExtraCode && ExtraCode[0]) {
452     if (ExtraCode[1] != 0) return true; // Unknown modifier.
453
454     switch (ExtraCode[0]) {
455     default: return true;  // Unknown modifier.
456     case 'b': // Print QImode register
457     case 'h': // Print QImode high register
458     case 'w': // Print HImode register
459     case 'k': // Print SImode register
460     case 'q': // Print SImode register
461       // These only apply to registers, ignore on mem.
462       break;
463     case 'P': // Don't print @PLT, but do print as memory.
464       printMemReference(MI, OpNo, O, "no-rip");
465       return false;
466     }
467   }
468   printMemReference(MI, OpNo, O);
469   return false;
470 }
471
472 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
473   if (Subtarget->isTargetDarwin())
474     OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
475 }
476
477
478 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
479   if (Subtarget->isTargetDarwin()) {
480     // All darwin targets use mach-o.
481     MachineModuleInfoMachO &MMIMacho =
482       MMI->getObjFileInfo<MachineModuleInfoMachO>();
483     
484     // Output stubs for dynamically-linked functions.
485     MachineModuleInfoMachO::SymbolListTy Stubs;
486
487     Stubs = MMIMacho.GetFnStubList();
488     if (!Stubs.empty()) {
489       const MCSection *TheSection = 
490         OutContext.getMachOSection("__IMPORT", "__jump_table",
491                                    MCSectionMachO::S_SYMBOL_STUBS |
492                                    MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
493                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
494                                    5, SectionKind::getMetadata());
495       OutStreamer.SwitchSection(TheSection);
496
497       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
498         // L_foo$stub:
499         OutStreamer.EmitLabel(Stubs[i].first);
500         //   .indirect_symbol _foo
501         OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
502                                         MCSA_IndirectSymbol);
503         // hlt; hlt; hlt; hlt; hlt     hlt = 0xf4 = -12.
504         const char HltInsts[] = { -12, -12, -12, -12, -12 };
505         OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
506       }
507       
508       Stubs.clear();
509       OutStreamer.AddBlankLine();
510     }
511
512     // Output stubs for external and common global variables.
513     Stubs = MMIMacho.GetGVStubList();
514     if (!Stubs.empty()) {
515       const MCSection *TheSection = 
516         OutContext.getMachOSection("__IMPORT", "__pointers",
517                                    MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
518                                    SectionKind::getMetadata());
519       OutStreamer.SwitchSection(TheSection);
520
521       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
522         // L_foo$non_lazy_ptr:
523         OutStreamer.EmitLabel(Stubs[i].first);
524         // .indirect_symbol _foo
525         MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
526         OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
527                                         MCSA_IndirectSymbol);
528         // .long 0
529         if (MCSym.getInt())
530           // External to current translation unit.
531           OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
532         else
533           // Internal to current translation unit.
534           //
535           // When we place the LSDA into the TEXT section, the type info
536           // pointers need to be indirect and pc-rel. We accomplish this by
537           // using NLPs.  However, sometimes the types are local to the file. So
538           // we need to fill in the value for the NLP in those cases.
539           OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
540                                                         OutContext),
541                                 4/*size*/, 0/*addrspace*/);
542       }
543       Stubs.clear();
544       OutStreamer.AddBlankLine();
545     }
546
547     Stubs = MMIMacho.GetHiddenGVStubList();
548     if (!Stubs.empty()) {
549       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
550       EmitAlignment(2);
551
552       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
553         // L_foo$non_lazy_ptr:
554         OutStreamer.EmitLabel(Stubs[i].first);
555         // .long _foo
556         OutStreamer.EmitValue(MCSymbolRefExpr::
557                               Create(Stubs[i].second.getPointer(),
558                                      OutContext),
559                               4/*size*/, 0/*addrspace*/);
560       }
561       Stubs.clear();
562       OutStreamer.AddBlankLine();
563     }
564
565     // Funny Darwin hack: This flag tells the linker that no global symbols
566     // contain code that falls through to other global symbols (e.g. the obvious
567     // implementation of multiple entry points).  If this doesn't occur, the
568     // linker can safely perform dead code stripping.  Since LLVM never
569     // generates code that does this, it is always safe to set.
570     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
571   }
572
573   if (Subtarget->isTargetCOFF()) {
574     X86COFFMachineModuleInfo &COFFMMI =
575       MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
576
577     // Emit type information for external functions
578     typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
579     for (externals_iterator I = COFFMMI.externals_begin(),
580                             E = COFFMMI.externals_end();
581                             I != E; ++I) {
582       OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
583       OutStreamer.EmitCOFFSymbolStorageClass(COFF::C_EXT);
584       OutStreamer.EmitCOFFSymbolType(COFF::DT_FCN << COFF::N_BTSHFT);
585       OutStreamer.EndCOFFSymbolDef();
586     }
587
588     // Necessary for dllexport support
589     std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
590
591     const TargetLoweringObjectFileCOFF &TLOFCOFF =
592       static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
593
594     for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
595       if (I->hasDLLExportLinkage())
596         DLLExportedFns.push_back(Mang->getSymbol(I));
597
598     for (Module::const_global_iterator I = M.global_begin(),
599            E = M.global_end(); I != E; ++I)
600       if (I->hasDLLExportLinkage())
601         DLLExportedGlobals.push_back(Mang->getSymbol(I));
602
603     // Output linker support code for dllexported globals on windows.
604     if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
605       OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
606       SmallString<128> name;
607       for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) {
608         if (Subtarget->isTargetWindows())
609           name = " /EXPORT:";
610         else
611           name = " -export:";
612         name += DLLExportedGlobals[i]->getName();
613         if (Subtarget->isTargetWindows())
614           name += ",DATA";
615         else
616         name += ",data";
617         OutStreamer.EmitBytes(name, 0);
618       }
619
620       for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) {
621         if (Subtarget->isTargetWindows())
622           name = " /EXPORT:";
623         else
624           name = " -export:";
625         name += DLLExportedFns[i]->getName();
626         OutStreamer.EmitBytes(name, 0);
627       }
628     }
629   }
630
631   if (Subtarget->isTargetELF()) {
632     const TargetLoweringObjectFileELF &TLOFELF =
633       static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
634
635     MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
636
637     // Output stubs for external and common global variables.
638     MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
639     if (!Stubs.empty()) {
640       OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
641       const TargetData *TD = TM.getTargetData();
642
643       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
644         OutStreamer.EmitLabel(Stubs[i].first);
645         OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
646                                     TD->getPointerSize(), 0);
647       }
648       Stubs.clear();
649     }
650   }
651 }
652
653
654 //===----------------------------------------------------------------------===//
655 // Target Registry Stuff
656 //===----------------------------------------------------------------------===//
657
658 static MCInstPrinter *createX86MCInstPrinter(const Target &T,
659                                              unsigned SyntaxVariant,
660                                              const MCAsmInfo &MAI) {
661   if (SyntaxVariant == 0)
662     return new X86ATTInstPrinter(MAI);
663   if (SyntaxVariant == 1)
664     return new X86IntelInstPrinter(MAI);
665   return 0;
666 }
667
668 // Force static initialization.
669 extern "C" void LLVMInitializeX86AsmPrinter() { 
670   RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
671   RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
672   
673   TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
674   TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
675 }