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 "InstPrinter/X86ATTInstPrinter.h"
18 #include "X86COFFMachineModuleInfo.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86TargetMachine.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Mangler.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/TargetOptions.h"
44 //===----------------------------------------------------------------------===//
45 // Primitive Helper Functions.
46 //===----------------------------------------------------------------------===//
48 /// runOnMachineFunction - Emit the function body.
50 bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
51 SetupMachineFunction(MF);
53 if (Subtarget->isTargetCOFF()) {
54 bool Intrn = MF.getFunction()->hasInternalLinkage();
55 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
56 OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
57 : COFF::IMAGE_SYM_CLASS_EXTERNAL);
58 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
59 << COFF::SCT_COMPLEX_TYPE_SHIFT);
60 OutStreamer.EndCOFFSymbolDef();
63 // Have common code print out the function header with linkage info etc.
66 // Emit the rest of the function body.
69 // We didn't modify anything.
73 /// printSymbolOperand - Print a raw symbol reference operand. This handles
74 /// jump tables, constant pools, global address and external symbols, all of
75 /// which print to a label with various suffixes for relocation types etc.
76 static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO,
78 switch (MO.getType()) {
79 default: llvm_unreachable("unknown symbol type!");
80 case MachineOperand::MO_ConstantPoolIndex:
81 O << *P.GetCPISymbol(MO.getIndex());
82 P.printOffset(MO.getOffset(), O);
84 case MachineOperand::MO_GlobalAddress: {
85 const GlobalValue *GV = MO.getGlobal();
88 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
89 GVSym = P.getSymbolWithGlobalValueBase(GV, "$stub");
90 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
91 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
92 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
93 GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
95 GVSym = P.getSymbol(GV);
97 // Handle dllimport linkage.
98 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
100 P.OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
102 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
103 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
104 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
105 MachineModuleInfoImpl::StubValueTy &StubSym =
106 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
107 if (StubSym.getPointer() == 0)
108 StubSym = MachineModuleInfoImpl::
109 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
110 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
111 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
112 MachineModuleInfoImpl::StubValueTy &StubSym =
113 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(
115 if (StubSym.getPointer() == 0)
116 StubSym = MachineModuleInfoImpl::
117 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
118 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
119 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$stub");
120 MachineModuleInfoImpl::StubValueTy &StubSym =
121 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
122 if (StubSym.getPointer() == 0)
123 StubSym = MachineModuleInfoImpl::
124 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
127 // If the name begins with a dollar-sign, enclose it in parens. We do this
128 // to avoid having it look like an integer immediate to the assembler.
129 if (GVSym->getName()[0] != '$')
132 O << '(' << *GVSym << ')';
133 P.printOffset(MO.getOffset(), O);
138 switch (MO.getTargetFlags()) {
140 llvm_unreachable("Unknown target flag on GV operand");
141 case X86II::MO_NO_FLAG: // No flag.
143 case X86II::MO_DARWIN_NONLAZY:
144 case X86II::MO_DLLIMPORT:
145 case X86II::MO_DARWIN_STUB:
146 // These affect the name of the symbol, not any suffix.
148 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
149 O << " + [.-" << *P.MF->getPICBaseSymbol() << ']';
151 case X86II::MO_PIC_BASE_OFFSET:
152 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
153 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
154 O << '-' << *P.MF->getPICBaseSymbol();
156 case X86II::MO_TLSGD: O << "@TLSGD"; break;
157 case X86II::MO_TLSLD: O << "@TLSLD"; break;
158 case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
159 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
160 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
161 case X86II::MO_TPOFF: O << "@TPOFF"; break;
162 case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
163 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
164 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
165 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
166 case X86II::MO_GOT: O << "@GOT"; break;
167 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
168 case X86II::MO_PLT: O << "@PLT"; break;
169 case X86II::MO_TLVP: O << "@TLVP"; break;
170 case X86II::MO_TLVP_PIC_BASE:
171 O << "@TLVP" << '-' << *P.MF->getPICBaseSymbol();
173 case X86II::MO_SECREL: O << "@SECREL32"; break;
177 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
178 unsigned OpNo, raw_ostream &O,
179 const char *Modifier = 0, unsigned AsmVariant = 0);
181 /// printPCRelImm - This is used to print an immediate value that ends up
182 /// being encoded as a pc-relative value. These print slightly differently, for
183 /// example, a $ is not emitted.
184 static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
185 unsigned OpNo, raw_ostream &O) {
186 const MachineOperand &MO = MI->getOperand(OpNo);
187 switch (MO.getType()) {
188 default: llvm_unreachable("Unknown pcrel immediate operand");
189 case MachineOperand::MO_Register:
190 // pc-relativeness was handled when computing the value in the reg.
191 printOperand(P, MI, OpNo, O);
193 case MachineOperand::MO_Immediate:
196 case MachineOperand::MO_GlobalAddress:
197 printSymbolOperand(P, MO, O);
202 static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
203 unsigned OpNo, raw_ostream &O, const char *Modifier,
204 unsigned AsmVariant) {
205 const MachineOperand &MO = MI->getOperand(OpNo);
206 switch (MO.getType()) {
207 default: llvm_unreachable("unknown operand type!");
208 case MachineOperand::MO_Register: {
209 // FIXME: Enumerating AsmVariant, so we can remove magic number.
210 if (AsmVariant == 0) O << '%';
211 unsigned Reg = MO.getReg();
212 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
213 MVT::SimpleValueType VT = (strcmp(Modifier+6,"64") == 0) ?
214 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
215 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
216 Reg = getX86SubSuperRegister(Reg, VT);
218 O << X86ATTInstPrinter::getRegisterName(Reg);
222 case MachineOperand::MO_Immediate:
223 if (AsmVariant == 0) O << '$';
227 case MachineOperand::MO_GlobalAddress: {
228 if (AsmVariant == 0) O << '$';
229 printSymbolOperand(P, MO, O);
235 static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI,
236 unsigned Op, raw_ostream &O,
237 const char *Modifier = NULL) {
238 const MachineOperand &BaseReg = MI->getOperand(Op);
239 const MachineOperand &IndexReg = MI->getOperand(Op+2);
240 const MachineOperand &DispSpec = MI->getOperand(Op+3);
242 // If we really don't want to print out (rip), don't.
243 bool HasBaseReg = BaseReg.getReg() != 0;
244 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
245 BaseReg.getReg() == X86::RIP)
248 // HasParenPart - True if we will print out the () part of the mem ref.
249 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
251 switch (DispSpec.getType()) {
253 llvm_unreachable("unknown operand type!");
254 case MachineOperand::MO_Immediate: {
255 int DispVal = DispSpec.getImm();
256 if (DispVal || !HasParenPart)
260 case MachineOperand::MO_GlobalAddress:
261 case MachineOperand::MO_ConstantPoolIndex:
262 printSymbolOperand(P, MI->getOperand(Op + 3), O);
265 if (Modifier && strcmp(Modifier, "H") == 0)
269 assert(IndexReg.getReg() != X86::ESP &&
270 "X86 doesn't allow scaling by ESP");
274 printOperand(P, MI, Op, O, Modifier);
276 if (IndexReg.getReg()) {
278 printOperand(P, MI, Op+2, O, Modifier);
279 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
281 O << ',' << ScaleVal;
287 static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
288 unsigned Op, raw_ostream &O,
289 const char *Modifier = NULL) {
290 assert(isMem(MI, Op) && "Invalid memory reference!");
291 const MachineOperand &Segment = MI->getOperand(Op+4);
292 if (Segment.getReg()) {
293 printOperand(P, MI, Op+4, O, Modifier);
296 printLeaMemReference(P, MI, Op, O, Modifier);
299 static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI,
300 unsigned Op, raw_ostream &O,
301 const char *Modifier = NULL,
302 unsigned AsmVariant = 1) {
303 const MachineOperand &BaseReg = MI->getOperand(Op);
304 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
305 const MachineOperand &IndexReg = MI->getOperand(Op+2);
306 const MachineOperand &DispSpec = MI->getOperand(Op+3);
307 const MachineOperand &SegReg = MI->getOperand(Op+4);
309 // If this has a segment register, print it.
310 if (SegReg.getReg()) {
311 printOperand(P, MI, Op+4, O, Modifier, AsmVariant);
317 bool NeedPlus = false;
318 if (BaseReg.getReg()) {
319 printOperand(P, MI, Op, O, Modifier, AsmVariant);
323 if (IndexReg.getReg()) {
324 if (NeedPlus) O << " + ";
326 O << ScaleVal << '*';
327 printOperand(P, MI, Op+2, O, Modifier, AsmVariant);
331 if (!DispSpec.isImm()) {
332 if (NeedPlus) O << " + ";
333 printOperand(P, MI, Op+3, O, Modifier, AsmVariant);
335 int64_t DispVal = DispSpec.getImm();
336 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
351 static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO,
352 char Mode, raw_ostream &O) {
353 unsigned Reg = MO.getReg();
355 default: return true; // Unknown mode.
356 case 'b': // Print QImode register
357 Reg = getX86SubSuperRegister(Reg, MVT::i8);
359 case 'h': // Print QImode high register
360 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
362 case 'w': // Print HImode register
363 Reg = getX86SubSuperRegister(Reg, MVT::i16);
365 case 'k': // Print SImode register
366 Reg = getX86SubSuperRegister(Reg, MVT::i32);
368 case 'q': // Print DImode register
369 // FIXME: gcc will actually print e instead of r for 32-bit.
370 Reg = getX86SubSuperRegister(Reg, MVT::i64);
374 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
378 /// PrintAsmOperand - Print out an operand for an inline asm expression.
380 bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
382 const char *ExtraCode, raw_ostream &O) {
383 // Does this asm operand have a single letter operand modifier?
384 if (ExtraCode && ExtraCode[0]) {
385 if (ExtraCode[1] != 0) return true; // Unknown modifier.
387 const MachineOperand &MO = MI->getOperand(OpNo);
389 switch (ExtraCode[0]) {
391 // See if this is a generic print operand
392 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
393 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
394 switch (MO.getType()) {
397 case MachineOperand::MO_Immediate:
400 case MachineOperand::MO_ConstantPoolIndex:
401 case MachineOperand::MO_JumpTableIndex:
402 case MachineOperand::MO_ExternalSymbol:
403 llvm_unreachable("unexpected operand type!");
404 case MachineOperand::MO_GlobalAddress:
405 printSymbolOperand(*this, MO, O);
406 if (Subtarget->isPICStyleRIPRel())
409 case MachineOperand::MO_Register:
411 printOperand(*this, MI, OpNo, O);
416 case 'c': // Don't print "$" before a global var name or constant.
417 switch (MO.getType()) {
419 printOperand(*this, MI, OpNo, O);
421 case MachineOperand::MO_Immediate:
424 case MachineOperand::MO_ConstantPoolIndex:
425 case MachineOperand::MO_JumpTableIndex:
426 case MachineOperand::MO_ExternalSymbol:
427 llvm_unreachable("unexpected operand type!");
428 case MachineOperand::MO_GlobalAddress:
429 printSymbolOperand(*this, MO, O);
434 case 'A': // Print '*' before a register (it must be a register)
437 printOperand(*this, MI, OpNo, O);
442 case 'b': // Print QImode register
443 case 'h': // Print QImode high register
444 case 'w': // Print HImode register
445 case 'k': // Print SImode register
446 case 'q': // Print DImode register
448 return printAsmMRegister(*this, MO, ExtraCode[0], O);
449 printOperand(*this, MI, OpNo, O);
452 case 'P': // This is the operand of a call, treat specially.
453 printPCRelImm(*this, MI, OpNo, O);
456 case 'n': // Negate the immediate or print a '-' before the operand.
457 // Note: this is a temporary solution. It should be handled target
458 // independently as part of the 'MC' work.
467 printOperand(*this, MI, OpNo, O, /*Modifier*/ 0, AsmVariant);
471 bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
472 unsigned OpNo, unsigned AsmVariant,
473 const char *ExtraCode,
476 printIntelMemReference(*this, MI, OpNo, O);
480 if (ExtraCode && ExtraCode[0]) {
481 if (ExtraCode[1] != 0) return true; // Unknown modifier.
483 switch (ExtraCode[0]) {
484 default: return true; // Unknown modifier.
485 case 'b': // Print QImode register
486 case 'h': // Print QImode high register
487 case 'w': // Print HImode register
488 case 'k': // Print SImode register
489 case 'q': // Print SImode register
490 // These only apply to registers, ignore on mem.
493 printMemReference(*this, MI, OpNo, O, "H");
495 case 'P': // Don't print @PLT, but do print as memory.
496 printMemReference(*this, MI, OpNo, O, "no-rip");
500 printMemReference(*this, MI, OpNo, O);
504 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
505 if (Subtarget->isTargetMacho())
506 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
508 if (Subtarget->isTargetCOFF()) {
509 // Emit an absolute @feat.00 symbol. This appears to be some kind of
510 // compiler features bitfield read by link.exe.
511 if (!Subtarget->is64Bit()) {
512 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(StringRef("@feat.00"));
513 OutStreamer.BeginCOFFSymbolDef(S);
514 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
515 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
516 OutStreamer.EndCOFFSymbolDef();
517 // According to the PE-COFF spec, the LSB of this value marks the object
518 // for "registered SEH". This means that all SEH handler entry points
519 // must be registered in .sxdata. Use of any unregistered handlers will
520 // cause the process to terminate immediately. LLVM does not know how to
521 // register any SEH handlers, so its object files should be safe.
523 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
524 OutStreamer.EmitAssignment(
525 S, MCConstantExpr::Create(int64_t(1), MMI->getContext()));
531 void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
532 if (Subtarget->isTargetMacho()) {
533 // All darwin targets use mach-o.
534 MachineModuleInfoMachO &MMIMacho =
535 MMI->getObjFileInfo<MachineModuleInfoMachO>();
537 // Output stubs for dynamically-linked functions.
538 MachineModuleInfoMachO::SymbolListTy Stubs;
540 Stubs = MMIMacho.GetFnStubList();
541 if (!Stubs.empty()) {
542 const MCSection *TheSection =
543 OutContext.getMachOSection("__IMPORT", "__jump_table",
544 MachO::S_SYMBOL_STUBS |
545 MachO::S_ATTR_SELF_MODIFYING_CODE |
546 MachO::S_ATTR_PURE_INSTRUCTIONS,
547 5, SectionKind::getMetadata());
548 OutStreamer.SwitchSection(TheSection);
550 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
552 OutStreamer.EmitLabel(Stubs[i].first);
553 // .indirect_symbol _foo
554 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
555 MCSA_IndirectSymbol);
556 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4.
557 const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
558 OutStreamer.EmitBytes(StringRef(HltInsts, 5));
562 OutStreamer.AddBlankLine();
565 // Output stubs for external and common global variables.
566 Stubs = MMIMacho.GetGVStubList();
567 if (!Stubs.empty()) {
568 const MCSection *TheSection =
569 OutContext.getMachOSection("__IMPORT", "__pointers",
570 MachO::S_NON_LAZY_SYMBOL_POINTERS,
571 SectionKind::getMetadata());
572 OutStreamer.SwitchSection(TheSection);
574 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
575 // L_foo$non_lazy_ptr:
576 OutStreamer.EmitLabel(Stubs[i].first);
577 // .indirect_symbol _foo
578 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
579 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
580 MCSA_IndirectSymbol);
583 // External to current translation unit.
584 OutStreamer.EmitIntValue(0, 4/*size*/);
586 // Internal to current translation unit.
588 // When we place the LSDA into the TEXT section, the type info
589 // pointers need to be indirect and pc-rel. We accomplish this by
590 // using NLPs. However, sometimes the types are local to the file. So
591 // we need to fill in the value for the NLP in those cases.
592 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
593 OutContext), 4/*size*/);
596 OutStreamer.AddBlankLine();
599 Stubs = MMIMacho.GetHiddenGVStubList();
600 if (!Stubs.empty()) {
601 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
604 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
605 // L_foo$non_lazy_ptr:
606 OutStreamer.EmitLabel(Stubs[i].first);
608 OutStreamer.EmitValue(MCSymbolRefExpr::
609 Create(Stubs[i].second.getPointer(),
610 OutContext), 4/*size*/);
613 OutStreamer.AddBlankLine();
616 SM.serializeToStackMapSection();
618 // Funny Darwin hack: This flag tells the linker that no global symbols
619 // contain code that falls through to other global symbols (e.g. the obvious
620 // implementation of multiple entry points). If this doesn't occur, the
621 // linker can safely perform dead code stripping. Since LLVM never
622 // generates code that does this, it is always safe to set.
623 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
626 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing() &&
627 MMI->usesVAFloatArgument()) {
628 StringRef SymbolName = Subtarget->is64Bit() ? "_fltused" : "__fltused";
629 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(SymbolName);
630 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
633 if (Subtarget->isTargetCOFF()) {
634 X86COFFMachineModuleInfo &COFFMMI =
635 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
637 // Emit type information for external functions
638 typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
639 for (externals_iterator I = COFFMMI.externals_begin(),
640 E = COFFMMI.externals_end();
642 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
643 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
644 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
645 << COFF::SCT_COMPLEX_TYPE_SHIFT);
646 OutStreamer.EndCOFFSymbolDef();
649 // Necessary for dllexport support
650 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
652 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
653 if (I->hasDLLExportStorageClass())
654 DLLExportedFns.push_back(getSymbol(I));
656 for (Module::const_global_iterator I = M.global_begin(),
657 E = M.global_end(); I != E; ++I)
658 if (I->hasDLLExportStorageClass())
659 DLLExportedGlobals.push_back(getSymbol(I));
661 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
663 const GlobalValue *GV = I;
664 if (!GV->hasDLLExportStorageClass())
667 while (const GlobalAlias *A = dyn_cast<GlobalAlias>(GV))
668 GV = A->getAliasedGlobal();
670 if (isa<Function>(GV))
671 DLLExportedFns.push_back(getSymbol(I));
672 else if (isa<GlobalVariable>(GV))
673 DLLExportedGlobals.push_back(getSymbol(I));
676 // Output linker support code for dllexported globals on windows.
677 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
678 const TargetLoweringObjectFileCOFF &TLOFCOFF =
679 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
681 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
682 SmallString<128> name;
683 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) {
684 if (Subtarget->isTargetWindows())
688 name += DLLExportedGlobals[i]->getName();
689 if (Subtarget->isTargetWindows())
693 OutStreamer.EmitBytes(name);
696 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) {
697 if (Subtarget->isTargetWindows())
701 name += DLLExportedFns[i]->getName();
702 OutStreamer.EmitBytes(name);
707 if (Subtarget->isTargetELF()) {
708 const TargetLoweringObjectFileELF &TLOFELF =
709 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
711 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
713 // Output stubs for external and common global variables.
714 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
715 if (!Stubs.empty()) {
716 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
717 const DataLayout *TD = TM.getDataLayout();
719 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
720 OutStreamer.EmitLabel(Stubs[i].first);
721 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
722 TD->getPointerSize());
729 //===----------------------------------------------------------------------===//
730 // Target Registry Stuff
731 //===----------------------------------------------------------------------===//
733 // Force static initialization.
734 extern "C" void LLVMInitializeX86AsmPrinter() {
735 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
736 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);