1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to X86 machine code.
13 //===----------------------------------------------------------------------===//
15 #include "X86AsmPrinter.h"
16 #include "X86MCInstLower.h"
18 #include "X86COFFMachineModuleInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86TargetMachine.h"
21 #include "InstPrinter/X86ATTInstPrinter.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Type.h"
26 #include "llvm/Analysis/DebugInfo.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/Target/Mangler.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Support/COFF.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/ADT/SmallString.h"
46 //===----------------------------------------------------------------------===//
47 // Primitive Helper Functions.
48 //===----------------------------------------------------------------------===//
50 /// runOnMachineFunction - Emit the function body.
52 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
53 SetupMachineFunction(MF);
55 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho()) {
56 bool Intrn = MF.getFunction()->hasInternalLinkage();
57 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
58 OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
59 : COFF::IMAGE_SYM_CLASS_EXTERNAL);
60 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
61 << COFF::SCT_COMPLEX_TYPE_SHIFT);
62 OutStreamer.EndCOFFSymbolDef();
65 // Have common code print out the function header with linkage info etc.
68 // Emit the rest of the function body.
71 // We didn't modify anything.
75 /// printSymbolOperand - Print a raw symbol reference operand. This handles
76 /// jump tables, constant pools, global address and external symbols, all of
77 /// which print to a label with various suffixes for relocation types etc.
78 void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO,
80 switch (MO.getType()) {
81 default: llvm_unreachable("unknown symbol type!");
82 case MachineOperand::MO_JumpTableIndex:
83 O << *GetJTISymbol(MO.getIndex());
85 case MachineOperand::MO_ConstantPoolIndex:
86 O << *GetCPISymbol(MO.getIndex());
87 printOffset(MO.getOffset(), O);
89 case MachineOperand::MO_GlobalAddress: {
90 const GlobalValue *GV = MO.getGlobal();
93 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
94 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
95 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
96 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
97 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
98 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
100 GVSym = Mang->getSymbol(GV);
102 // Handle dllimport linkage.
103 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
104 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
106 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
107 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
108 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
109 MachineModuleInfoImpl::StubValueTy &StubSym =
110 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
111 if (StubSym.getPointer() == 0)
112 StubSym = MachineModuleInfoImpl::
113 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
114 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
115 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
116 MachineModuleInfoImpl::StubValueTy &StubSym =
117 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
118 if (StubSym.getPointer() == 0)
119 StubSym = MachineModuleInfoImpl::
120 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
121 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
122 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
123 MachineModuleInfoImpl::StubValueTy &StubSym =
124 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
125 if (StubSym.getPointer() == 0)
126 StubSym = MachineModuleInfoImpl::
127 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
130 // If the name begins with a dollar-sign, enclose it in parens. We do this
131 // to avoid having it look like an integer immediate to the assembler.
132 if (GVSym->getName()[0] != '$')
135 O << '(' << *GVSym << ')';
136 printOffset(MO.getOffset(), O);
139 case MachineOperand::MO_ExternalSymbol: {
140 const MCSymbol *SymToPrint;
141 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
142 SmallString<128> TempNameStr;
143 TempNameStr += StringRef(MO.getSymbolName());
144 TempNameStr += StringRef("$stub");
146 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
147 MachineModuleInfoImpl::StubValueTy &StubSym =
148 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
149 if (StubSym.getPointer() == 0) {
150 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
151 StubSym = MachineModuleInfoImpl::
152 StubValueTy(OutContext.GetOrCreateSymbol(TempNameStr.str()),
155 SymToPrint = StubSym.getPointer();
157 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
160 // If the name begins with a dollar-sign, enclose it in parens. We do this
161 // to avoid having it look like an integer immediate to the assembler.
162 if (SymToPrint->getName()[0] != '$')
165 O << '(' << *SymToPrint << '(';
170 switch (MO.getTargetFlags()) {
172 llvm_unreachable("Unknown target flag on GV operand");
173 case X86II::MO_NO_FLAG: // No flag.
175 case X86II::MO_DARWIN_NONLAZY:
176 case X86II::MO_DLLIMPORT:
177 case X86II::MO_DARWIN_STUB:
178 // These affect the name of the symbol, not any suffix.
180 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
181 O << " + [.-" << *MF->getPICBaseSymbol() << ']';
183 case X86II::MO_PIC_BASE_OFFSET:
184 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
185 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
186 O << '-' << *MF->getPICBaseSymbol();
188 case X86II::MO_TLSGD: O << "@TLSGD"; break;
189 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
190 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
191 case X86II::MO_TPOFF: O << "@TPOFF"; break;
192 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
193 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; 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();
202 case X86II::MO_SECREL: O << "@SECREL"; break;
206 /// print_pcrel_imm - This is used to print an immediate value that ends up
207 /// being encoded as a pc-relative value. These print slightly differently, for
208 /// example, a $ is not emitted.
209 void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo,
211 const MachineOperand &MO = MI->getOperand(OpNo);
212 switch (MO.getType()) {
213 default: llvm_unreachable("Unknown pcrel immediate operand");
214 case MachineOperand::MO_Register:
215 // pc-relativeness was handled when computing the value in the reg.
216 printOperand(MI, OpNo, O);
218 case MachineOperand::MO_Immediate:
221 case MachineOperand::MO_MachineBasicBlock:
222 O << *MO.getMBB()->getSymbol();
224 case MachineOperand::MO_GlobalAddress:
225 case MachineOperand::MO_ExternalSymbol:
226 printSymbolOperand(MO, O);
232 void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
233 raw_ostream &O, const char *Modifier) {
234 const MachineOperand &MO = MI->getOperand(OpNo);
235 switch (MO.getType()) {
236 default: llvm_unreachable("unknown operand type!");
237 case MachineOperand::MO_Register: {
239 unsigned Reg = MO.getReg();
240 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
241 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
242 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
243 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
244 Reg = getX86SubSuperRegister(Reg, VT);
246 O << X86ATTInstPrinter::getRegisterName(Reg);
250 case MachineOperand::MO_Immediate:
251 O << '$' << MO.getImm();
254 case MachineOperand::MO_JumpTableIndex:
255 case MachineOperand::MO_ConstantPoolIndex:
256 case MachineOperand::MO_GlobalAddress:
257 case MachineOperand::MO_ExternalSymbol: {
259 printSymbolOperand(MO, O);
265 void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
267 unsigned char value = MI->getOperand(Op).getImm();
269 default: llvm_unreachable("Invalid ssecc argument!");
270 case 0: O << "eq"; break;
271 case 1: O << "lt"; break;
272 case 2: O << "le"; break;
273 case 3: O << "unord"; break;
274 case 4: O << "neq"; break;
275 case 5: O << "nlt"; break;
276 case 6: O << "nle"; break;
277 case 7: O << "ord"; break;
278 case 8: O << "eq_uq"; break;
279 case 9: O << "nge"; break;
280 case 0xa: O << "ngt"; break;
281 case 0xb: O << "false"; break;
282 case 0xc: O << "neq_oq"; break;
283 case 0xd: O << "ge"; break;
284 case 0xe: O << "gt"; break;
285 case 0xf: O << "true"; break;
286 case 0x10: O << "eq_os"; break;
287 case 0x11: O << "lt_oq"; break;
288 case 0x12: O << "le_oq"; break;
289 case 0x13: O << "unord_s"; break;
290 case 0x14: O << "neq_us"; break;
291 case 0x15: O << "nlt_uq"; break;
292 case 0x16: O << "nle_uq"; break;
293 case 0x17: O << "ord_s"; break;
294 case 0x18: O << "eq_us"; break;
295 case 0x19: O << "nge_uq"; break;
296 case 0x1a: O << "ngt_uq"; break;
297 case 0x1b: O << "false_os"; break;
298 case 0x1c: O << "neq_os"; break;
299 case 0x1d: O << "ge_oq"; break;
300 case 0x1e: O << "gt_oq"; break;
301 case 0x1f: O << "true_us"; break;
305 void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
306 raw_ostream &O, const char *Modifier) {
307 const MachineOperand &BaseReg = MI->getOperand(Op);
308 const MachineOperand &IndexReg = MI->getOperand(Op+2);
309 const MachineOperand &DispSpec = MI->getOperand(Op+3);
311 // If we really don't want to print out (rip), don't.
312 bool HasBaseReg = BaseReg.getReg() != 0;
313 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
314 BaseReg.getReg() == X86::RIP)
317 // HasParenPart - True if we will print out the () part of the mem ref.
318 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
320 if (DispSpec.isImm()) {
321 int DispVal = DispSpec.getImm();
322 if (DispVal || !HasParenPart)
325 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
326 DispSpec.isJTI() || DispSpec.isSymbol());
327 printSymbolOperand(MI->getOperand(Op+3), O);
330 if (Modifier && strcmp(Modifier, "H") == 0)
334 assert(IndexReg.getReg() != X86::ESP &&
335 "X86 doesn't allow scaling by ESP");
339 printOperand(MI, Op, O, Modifier);
341 if (IndexReg.getReg()) {
343 printOperand(MI, Op+2, O, Modifier);
344 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
346 O << ',' << ScaleVal;
352 void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
353 raw_ostream &O, const char *Modifier) {
354 assert(isMem(MI, Op) && "Invalid memory reference!");
355 const MachineOperand &Segment = MI->getOperand(Op+4);
356 if (Segment.getReg()) {
357 printOperand(MI, Op+4, O, Modifier);
360 printLeaMemReference(MI, Op, O, Modifier);
363 void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op,
365 O << *MF->getPICBaseSymbol() << '\n';
366 O << *MF->getPICBaseSymbol() << ':';
369 bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
371 unsigned Reg = MO.getReg();
373 default: return true; // Unknown mode.
374 case 'b': // Print QImode register
375 Reg = getX86SubSuperRegister(Reg, MVT::i8);
377 case 'h': // Print QImode high register
378 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
380 case 'w': // Print HImode register
381 Reg = getX86SubSuperRegister(Reg, MVT::i16);
383 case 'k': // Print SImode register
384 Reg = getX86SubSuperRegister(Reg, MVT::i32);
386 case 'q': // Print DImode register
387 Reg = getX86SubSuperRegister(Reg, MVT::i64);
391 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
395 /// PrintAsmOperand - Print out an operand for an inline asm expression.
397 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
399 const char *ExtraCode, raw_ostream &O) {
400 // Does this asm operand have a single letter operand modifier?
401 if (ExtraCode && ExtraCode[0]) {
402 if (ExtraCode[1] != 0) return true; // Unknown modifier.
404 const MachineOperand &MO = MI->getOperand(OpNo);
406 switch (ExtraCode[0]) {
407 default: return true; // Unknown modifier.
408 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
413 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
414 printSymbolOperand(MO, O);
415 if (Subtarget->isPICStyleRIPRel())
421 printOperand(MI, OpNo, O);
427 case 'c': // Don't print "$" before a global var name or constant.
430 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
431 printSymbolOperand(MO, O);
433 printOperand(MI, OpNo, O);
436 case 'A': // Print '*' before a register (it must be a register)
439 printOperand(MI, OpNo, O);
444 case 'b': // Print QImode register
445 case 'h': // Print QImode high register
446 case 'w': // Print HImode register
447 case 'k': // Print SImode register
448 case 'q': // Print DImode register
450 return printAsmMRegister(MO, ExtraCode[0], O);
451 printOperand(MI, OpNo, O);
454 case 'P': // This is the operand of a call, treat specially.
455 print_pcrel_imm(MI, OpNo, O);
458 case 'n': // Negate the immediate or print a '-' before the operand.
459 // Note: this is a temporary solution. It should be handled target
460 // independently as part of the 'MC' work.
469 printOperand(MI, OpNo, O);
473 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
474 unsigned OpNo, unsigned AsmVariant,
475 const char *ExtraCode,
477 if (ExtraCode && ExtraCode[0]) {
478 if (ExtraCode[1] != 0) return true; // Unknown modifier.
480 switch (ExtraCode[0]) {
481 default: return true; // Unknown modifier.
482 case 'b': // Print QImode register
483 case 'h': // Print QImode high register
484 case 'w': // Print HImode register
485 case 'k': // Print SImode register
486 case 'q': // Print SImode register
487 // These only apply to registers, ignore on mem.
490 printMemReference(MI, OpNo, O, "H");
492 case 'P': // Don't print @PLT, but do print as memory.
493 printMemReference(MI, OpNo, O, "no-rip");
497 printMemReference(MI, OpNo, O);
501 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
502 if (Subtarget->isTargetEnvMacho())
503 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
507 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
508 if (Subtarget->isTargetEnvMacho()) {
509 // All darwin targets use mach-o.
510 MachineModuleInfoMachO &MMIMacho =
511 MMI->getObjFileInfo<MachineModuleInfoMachO>();
513 // Output stubs for dynamically-linked functions.
514 MachineModuleInfoMachO::SymbolListTy Stubs;
516 Stubs = MMIMacho.GetFnStubList();
517 if (!Stubs.empty()) {
518 const MCSection *TheSection =
519 OutContext.getMachOSection("__IMPORT", "__jump_table",
520 MCSectionMachO::S_SYMBOL_STUBS |
521 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
522 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
523 5, SectionKind::getMetadata());
524 OutStreamer.SwitchSection(TheSection);
526 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
528 OutStreamer.EmitLabel(Stubs[i].first);
529 // .indirect_symbol _foo
530 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
531 MCSA_IndirectSymbol);
532 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4.
533 const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
534 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
538 OutStreamer.AddBlankLine();
541 // Output stubs for external and common global variables.
542 Stubs = MMIMacho.GetGVStubList();
543 if (!Stubs.empty()) {
544 const MCSection *TheSection =
545 OutContext.getMachOSection("__IMPORT", "__pointers",
546 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
547 SectionKind::getMetadata());
548 OutStreamer.SwitchSection(TheSection);
550 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
551 // L_foo$non_lazy_ptr:
552 OutStreamer.EmitLabel(Stubs[i].first);
553 // .indirect_symbol _foo
554 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
555 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
556 MCSA_IndirectSymbol);
559 // External to current translation unit.
560 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
562 // Internal to current translation unit.
564 // When we place the LSDA into the TEXT section, the type info
565 // pointers need to be indirect and pc-rel. We accomplish this by
566 // using NLPs. However, sometimes the types are local to the file. So
567 // we need to fill in the value for the NLP in those cases.
568 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
570 4/*size*/, 0/*addrspace*/);
573 OutStreamer.AddBlankLine();
576 Stubs = MMIMacho.GetHiddenGVStubList();
577 if (!Stubs.empty()) {
578 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
581 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
582 // L_foo$non_lazy_ptr:
583 OutStreamer.EmitLabel(Stubs[i].first);
585 OutStreamer.EmitValue(MCSymbolRefExpr::
586 Create(Stubs[i].second.getPointer(),
588 4/*size*/, 0/*addrspace*/);
591 OutStreamer.AddBlankLine();
594 // Funny Darwin hack: This flag tells the linker that no global symbols
595 // contain code that falls through to other global symbols (e.g. the obvious
596 // implementation of multiple entry points). If this doesn't occur, the
597 // linker can safely perform dead code stripping. Since LLVM never
598 // generates code that does this, it is always safe to set.
599 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
602 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing() &&
603 MMI->usesVAFloatArgument()) {
604 StringRef SymbolName = Subtarget->is64Bit() ? "_fltused" : "__fltused";
605 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(SymbolName);
606 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
609 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho()) {
610 X86COFFMachineModuleInfo &COFFMMI =
611 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
613 // Emit type information for external functions
614 typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
615 for (externals_iterator I = COFFMMI.externals_begin(),
616 E = COFFMMI.externals_end();
618 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
619 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
620 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
621 << COFF::SCT_COMPLEX_TYPE_SHIFT);
622 OutStreamer.EndCOFFSymbolDef();
625 // Necessary for dllexport support
626 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
628 const TargetLoweringObjectFileCOFF &TLOFCOFF =
629 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
631 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
632 if (I->hasDLLExportLinkage())
633 DLLExportedFns.push_back(Mang->getSymbol(I));
635 for (Module::const_global_iterator I = M.global_begin(),
636 E = M.global_end(); I != E; ++I)
637 if (I->hasDLLExportLinkage())
638 DLLExportedGlobals.push_back(Mang->getSymbol(I));
640 // Output linker support code for dllexported globals on windows.
641 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
642 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
643 SmallString<128> name;
644 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) {
645 if (Subtarget->isTargetWindows())
649 name += DLLExportedGlobals[i]->getName();
650 if (Subtarget->isTargetWindows())
654 OutStreamer.EmitBytes(name, 0);
657 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) {
658 if (Subtarget->isTargetWindows())
662 name += DLLExportedFns[i]->getName();
663 OutStreamer.EmitBytes(name, 0);
668 if (Subtarget->isTargetELF()) {
669 const TargetLoweringObjectFileELF &TLOFELF =
670 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
672 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
674 // Output stubs for external and common global variables.
675 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
676 if (!Stubs.empty()) {
677 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
678 const TargetData *TD = TM.getTargetData();
680 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
681 OutStreamer.EmitLabel(Stubs[i].first);
682 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
683 TD->getPointerSize(), 0);
691 X86AsmPrinter::getDebugValueLocation(const MachineInstr *MI) const {
692 MachineLocation Location;
693 assert (MI->getNumOperands() == 7 && "Invalid no. of machine operands!");
694 // Frame address. Currently handles register +- offset only.
696 if (MI->getOperand(0).isReg() && MI->getOperand(3).isImm())
697 Location.set(MI->getOperand(0).getReg(), MI->getOperand(3).getImm());
699 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
704 void X86AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
706 // Only the target-dependent form of DBG_VALUE should get here.
707 // Referencing the offset and metadata as NOps-2 and NOps-1 is
708 // probably portable to other targets; frame pointer location is not.
709 unsigned NOps = MI->getNumOperands();
711 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
712 // cast away const; DIetc do not take const operands for some reason.
713 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
714 if (V.getContext().isSubprogram())
715 O << DISubprogram(V.getContext()).getDisplayName() << ":";
718 // Frame address. Currently handles register +- offset only.
720 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
721 printOperand(MI, 0, O);
724 O << '+'; printOperand(MI, 3, O);
727 printOperand(MI, NOps-2, O);
732 //===----------------------------------------------------------------------===//
733 // Target Registry Stuff
734 //===----------------------------------------------------------------------===//
736 // Force static initialization.
737 extern "C" void LLVMInitializeX86AsmPrinter() {
738 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
739 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);