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