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/DebugInfo.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/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_TLSLD: O << "@TLSLD"; break;
190 case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
191 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
192 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
193 case X86II::MO_TPOFF: O << "@TPOFF"; break;
194 case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
195 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
196 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
197 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
198 case X86II::MO_GOT: O << "@GOT"; break;
199 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
200 case X86II::MO_PLT: O << "@PLT"; break;
201 case X86II::MO_TLVP: O << "@TLVP"; break;
202 case X86II::MO_TLVP_PIC_BASE:
203 O << "@TLVP" << '-' << *MF->getPICBaseSymbol();
205 case X86II::MO_SECREL: O << "@SECREL"; break;
209 /// printPCRelImm - This is used to print an immediate value that ends up
210 /// being encoded as a pc-relative value. These print slightly differently, for
211 /// example, a $ is not emitted.
212 void X86AsmPrinter::printPCRelImm(const MachineInstr *MI, unsigned OpNo,
214 const MachineOperand &MO = MI->getOperand(OpNo);
215 switch (MO.getType()) {
216 default: llvm_unreachable("Unknown pcrel immediate operand");
217 case MachineOperand::MO_Register:
218 // pc-relativeness was handled when computing the value in the reg.
219 printOperand(MI, OpNo, O);
221 case MachineOperand::MO_Immediate:
224 case MachineOperand::MO_MachineBasicBlock:
225 O << *MO.getMBB()->getSymbol();
227 case MachineOperand::MO_GlobalAddress:
228 case MachineOperand::MO_ExternalSymbol:
229 printSymbolOperand(MO, O);
235 void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
236 raw_ostream &O, const char *Modifier,
237 unsigned AsmVariant) {
238 const MachineOperand &MO = MI->getOperand(OpNo);
239 switch (MO.getType()) {
240 default: llvm_unreachable("unknown operand type!");
241 case MachineOperand::MO_Register: {
242 // FIXME: Enumerating AsmVariant, so we can remove magic number.
243 if (AsmVariant == 0) O << '%';
244 unsigned Reg = MO.getReg();
245 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
246 MVT::SimpleValueType VT = (strcmp(Modifier+6,"64") == 0) ?
247 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
248 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
249 Reg = getX86SubSuperRegister(Reg, VT);
251 O << X86ATTInstPrinter::getRegisterName(Reg);
255 case MachineOperand::MO_Immediate:
256 O << '$' << MO.getImm();
259 case MachineOperand::MO_JumpTableIndex:
260 case MachineOperand::MO_ConstantPoolIndex:
261 case MachineOperand::MO_GlobalAddress:
262 case MachineOperand::MO_ExternalSymbol: {
264 printSymbolOperand(MO, O);
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);
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)
282 // HasParenPart - True if we will print out the () part of the mem ref.
283 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
285 if (DispSpec.isImm()) {
286 int DispVal = DispSpec.getImm();
287 if (DispVal || !HasParenPart)
290 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
291 DispSpec.isJTI() || DispSpec.isSymbol());
292 printSymbolOperand(MI->getOperand(Op+3), O);
295 if (Modifier && strcmp(Modifier, "H") == 0)
299 assert(IndexReg.getReg() != X86::ESP &&
300 "X86 doesn't allow scaling by ESP");
304 printOperand(MI, Op, O, Modifier);
306 if (IndexReg.getReg()) {
308 printOperand(MI, Op+2, O, Modifier);
309 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
311 O << ',' << ScaleVal;
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);
325 printLeaMemReference(MI, Op, O, Modifier);
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);
337 // If this has a segment register, print it.
338 if (SegReg.getReg()) {
339 printOperand(MI, Op+4, O, Modifier, AsmVariant);
345 bool NeedPlus = false;
346 if (BaseReg.getReg()) {
347 printOperand(MI, Op, O, Modifier, AsmVariant);
351 if (IndexReg.getReg()) {
352 if (NeedPlus) O << " + ";
354 O << ScaleVal << '*';
355 printOperand(MI, Op+2, O, Modifier, AsmVariant);
359 assert (DispSpec.isImm() && "Displacement is not an immediate!");
360 int64_t DispVal = DispSpec.getImm();
361 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
375 bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
377 unsigned Reg = MO.getReg();
379 default: return true; // Unknown mode.
380 case 'b': // Print QImode register
381 Reg = getX86SubSuperRegister(Reg, MVT::i8);
383 case 'h': // Print QImode high register
384 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
386 case 'w': // Print HImode register
387 Reg = getX86SubSuperRegister(Reg, MVT::i16);
389 case 'k': // Print SImode register
390 Reg = getX86SubSuperRegister(Reg, MVT::i32);
392 case 'q': // Print DImode register
393 Reg = getX86SubSuperRegister(Reg, MVT::i64);
397 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
401 /// PrintAsmOperand - Print out an operand for an inline asm expression.
403 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
405 const char *ExtraCode, raw_ostream &O) {
406 // Does this asm operand have a single letter operand modifier?
407 if (ExtraCode && ExtraCode[0]) {
408 if (ExtraCode[1] != 0) return true; // Unknown modifier.
410 const MachineOperand &MO = MI->getOperand(OpNo);
412 switch (ExtraCode[0]) {
414 // See if this is a generic print operand
415 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
416 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
421 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
422 printSymbolOperand(MO, O);
423 if (Subtarget->isPICStyleRIPRel())
429 printOperand(MI, OpNo, O);
435 case 'c': // Don't print "$" before a global var name or constant.
438 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
439 printSymbolOperand(MO, O);
441 printOperand(MI, OpNo, O);
444 case 'A': // Print '*' before a register (it must be a register)
447 printOperand(MI, OpNo, O);
452 case 'b': // Print QImode register
453 case 'h': // Print QImode high register
454 case 'w': // Print HImode register
455 case 'k': // Print SImode register
456 case 'q': // Print DImode register
458 return printAsmMRegister(MO, ExtraCode[0], O);
459 printOperand(MI, OpNo, O);
462 case 'P': // This is the operand of a call, treat specially.
463 printPCRelImm(MI, OpNo, O);
466 case 'n': // Negate the immediate or print a '-' before the operand.
467 // Note: this is a temporary solution. It should be handled target
468 // independently as part of the 'MC' work.
477 printOperand(MI, OpNo, O, /*Modifier*/ 0, AsmVariant);
481 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
482 unsigned OpNo, unsigned AsmVariant,
483 const char *ExtraCode,
486 printIntelMemReference(MI, OpNo, O);
490 if (ExtraCode && ExtraCode[0]) {
491 if (ExtraCode[1] != 0) return true; // Unknown modifier.
493 switch (ExtraCode[0]) {
494 default: return true; // Unknown modifier.
495 case 'b': // Print QImode register
496 case 'h': // Print QImode high register
497 case 'w': // Print HImode register
498 case 'k': // Print SImode register
499 case 'q': // Print SImode register
500 // These only apply to registers, ignore on mem.
503 printMemReference(MI, OpNo, O, "H");
505 case 'P': // Don't print @PLT, but do print as memory.
506 printMemReference(MI, OpNo, O, "no-rip");
510 printMemReference(MI, OpNo, O);
514 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
515 if (Subtarget->isTargetEnvMacho())
516 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
520 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
521 if (Subtarget->isTargetEnvMacho()) {
522 // All darwin targets use mach-o.
523 MachineModuleInfoMachO &MMIMacho =
524 MMI->getObjFileInfo<MachineModuleInfoMachO>();
526 // Output stubs for dynamically-linked functions.
527 MachineModuleInfoMachO::SymbolListTy Stubs;
529 Stubs = MMIMacho.GetFnStubList();
530 if (!Stubs.empty()) {
531 const MCSection *TheSection =
532 OutContext.getMachOSection("__IMPORT", "__jump_table",
533 MCSectionMachO::S_SYMBOL_STUBS |
534 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
535 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
536 5, SectionKind::getMetadata());
537 OutStreamer.SwitchSection(TheSection);
539 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
541 OutStreamer.EmitLabel(Stubs[i].first);
542 // .indirect_symbol _foo
543 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
544 MCSA_IndirectSymbol);
545 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4.
546 const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
547 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
551 OutStreamer.AddBlankLine();
554 // Output stubs for external and common global variables.
555 Stubs = MMIMacho.GetGVStubList();
556 if (!Stubs.empty()) {
557 const MCSection *TheSection =
558 OutContext.getMachOSection("__IMPORT", "__pointers",
559 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
560 SectionKind::getMetadata());
561 OutStreamer.SwitchSection(TheSection);
563 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
564 // L_foo$non_lazy_ptr:
565 OutStreamer.EmitLabel(Stubs[i].first);
566 // .indirect_symbol _foo
567 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
568 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
569 MCSA_IndirectSymbol);
572 // External to current translation unit.
573 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
575 // Internal to current translation unit.
577 // When we place the LSDA into the TEXT section, the type info
578 // pointers need to be indirect and pc-rel. We accomplish this by
579 // using NLPs. However, sometimes the types are local to the file. So
580 // we need to fill in the value for the NLP in those cases.
581 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
583 4/*size*/, 0/*addrspace*/);
586 OutStreamer.AddBlankLine();
589 Stubs = MMIMacho.GetHiddenGVStubList();
590 if (!Stubs.empty()) {
591 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
594 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
595 // L_foo$non_lazy_ptr:
596 OutStreamer.EmitLabel(Stubs[i].first);
598 OutStreamer.EmitValue(MCSymbolRefExpr::
599 Create(Stubs[i].second.getPointer(),
601 4/*size*/, 0/*addrspace*/);
604 OutStreamer.AddBlankLine();
607 // Funny Darwin hack: This flag tells the linker that no global symbols
608 // contain code that falls through to other global symbols (e.g. the obvious
609 // implementation of multiple entry points). If this doesn't occur, the
610 // linker can safely perform dead code stripping. Since LLVM never
611 // generates code that does this, it is always safe to set.
612 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
615 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing() &&
616 MMI->usesVAFloatArgument()) {
617 StringRef SymbolName = Subtarget->is64Bit() ? "_fltused" : "__fltused";
618 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(SymbolName);
619 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
622 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho()) {
623 X86COFFMachineModuleInfo &COFFMMI =
624 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
626 // Emit type information for external functions
627 typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
628 for (externals_iterator I = COFFMMI.externals_begin(),
629 E = COFFMMI.externals_end();
631 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
632 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
633 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
634 << COFF::SCT_COMPLEX_TYPE_SHIFT);
635 OutStreamer.EndCOFFSymbolDef();
638 // Necessary for dllexport support
639 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
641 const TargetLoweringObjectFileCOFF &TLOFCOFF =
642 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
644 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
645 if (I->hasDLLExportLinkage())
646 DLLExportedFns.push_back(Mang->getSymbol(I));
648 for (Module::const_global_iterator I = M.global_begin(),
649 E = M.global_end(); I != E; ++I)
650 if (I->hasDLLExportLinkage())
651 DLLExportedGlobals.push_back(Mang->getSymbol(I));
653 // Output linker support code for dllexported globals on windows.
654 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
655 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
656 SmallString<128> name;
657 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) {
658 if (Subtarget->isTargetWindows())
662 name += DLLExportedGlobals[i]->getName();
663 if (Subtarget->isTargetWindows())
667 OutStreamer.EmitBytes(name, 0);
670 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) {
671 if (Subtarget->isTargetWindows())
675 name += DLLExportedFns[i]->getName();
676 OutStreamer.EmitBytes(name, 0);
681 if (Subtarget->isTargetELF()) {
682 const TargetLoweringObjectFileELF &TLOFELF =
683 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
685 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
687 // Output stubs for external and common global variables.
688 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
689 if (!Stubs.empty()) {
690 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
691 const DataLayout *TD = TM.getDataLayout();
693 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
694 OutStreamer.EmitLabel(Stubs[i].first);
695 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
696 TD->getPointerSize(), 0);
704 X86AsmPrinter::getDebugValueLocation(const MachineInstr *MI) const {
705 MachineLocation Location;
706 assert (MI->getNumOperands() == 7 && "Invalid no. of machine operands!");
707 // Frame address. Currently handles register +- offset only.
709 if (MI->getOperand(0).isReg() && MI->getOperand(3).isImm())
710 Location.set(MI->getOperand(0).getReg(), MI->getOperand(3).getImm());
712 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
717 void X86AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
719 // Only the target-dependent form of DBG_VALUE should get here.
720 // Referencing the offset and metadata as NOps-2 and NOps-1 is
721 // probably portable to other targets; frame pointer location is not.
722 unsigned NOps = MI->getNumOperands();
724 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
725 // cast away const; DIetc do not take const operands for some reason.
726 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
727 if (V.getContext().isSubprogram())
728 O << DISubprogram(V.getContext()).getDisplayName() << ":";
731 // Frame address. Currently handles register +- offset only.
733 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
734 printOperand(MI, 0, O);
737 O << '+'; printOperand(MI, 3, O);
740 printOperand(MI, NOps-2, O);
745 //===----------------------------------------------------------------------===//
746 // Target Registry Stuff
747 //===----------------------------------------------------------------------===//
749 // Force static initialization.
750 extern "C" void LLVMInitializeX86AsmPrinter() {
751 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
752 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);