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