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