1 //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file the shared super class printer that converts from our internal
11 // representation of machine-dependent LLVM code to Intel and AT&T format
13 // This printer is the output mechanism used by `llc'.
15 //===----------------------------------------------------------------------===//
17 #include "X86AsmPrinter.h"
18 #include "X86ATTAsmPrinter.h"
19 #include "X86IntelAsmPrinter.h"
20 #include "X86Subtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Target/TargetAsmInfo.h"
29 Statistic<> llvm::EmittedInsts("asm-printer",
30 "Number of machine instrs printed");
33 bool X86SharedAsmPrinter::doInitialization(Module &M) {
34 if (Subtarget->isTargetDarwin()) {
35 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
36 if (!Subtarget->is64Bit())
37 X86PICStyle = PICStyle::Stub;
39 // Emit initial debug information.
43 return AsmPrinter::doInitialization(M);
46 bool X86SharedAsmPrinter::doFinalization(Module &M) {
47 // Note: this code is not shared by the Intel printer as it is too different
48 // from how MASM does things. When making changes here don't forget to look
49 // at X86IntelAsmPrinter::doFinalization().
50 const TargetData *TD = TM.getTargetData();
52 // Print out module-level global variables here.
53 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
55 if (!I->hasInitializer()) continue; // External global require no code
57 // Check to see if this is a special global used by LLVM, if so, emit it.
58 if (EmitSpecialLLVMGlobal(I))
61 std::string name = Mang->getValueName(I);
62 Constant *C = I->getInitializer();
63 unsigned Size = TD->getTypeSize(C->getType());
64 unsigned Align = getPreferredAlignmentLog(I);
66 if (C->isNullValue() && /* FIXME: Verify correct */
67 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
68 I->hasLinkOnceLinkage() ||
69 (Subtarget->isTargetDarwin() &&
70 I->hasExternalLinkage() && !I->hasSection()))) {
71 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
72 if (I->hasExternalLinkage()) {
73 O << "\t.globl\t" << name << "\n";
74 O << "\t.zerofill __DATA__, __common, " << name << ", "
75 << Size << ", " << Align;
77 SwitchToDataSection(TAI->getDataSection(), I);
78 if (TAI->getLCOMMDirective() != NULL) {
79 if (I->hasInternalLinkage()) {
80 O << TAI->getLCOMMDirective() << name << "," << Size;
81 if (Subtarget->isTargetDarwin())
82 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
84 O << TAI->getCOMMDirective() << name << "," << Size;
86 if (Subtarget->TargetType != X86Subtarget::isCygwin) {
87 if (I->hasInternalLinkage())
88 O << "\t.local\t" << name << "\n";
90 O << TAI->getCOMMDirective() << name << "," << Size;
91 if (TAI->getCOMMDirectiveTakesAlignment())
92 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
95 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
97 switch (I->getLinkage()) {
98 case GlobalValue::LinkOnceLinkage:
99 case GlobalValue::WeakLinkage:
100 if (Subtarget->isTargetDarwin()) {
101 O << "\t.globl " << name << "\n"
102 << "\t.weak_definition " << name << "\n";
103 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
104 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
105 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
106 << "\t.weak " << name << "\n";
108 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
109 << "\t.weak " << name << "\n";
112 case GlobalValue::AppendingLinkage:
113 // FIXME: appending linkage variables should go into a section of
114 // their name or something. For now, just emit them as external.
115 case GlobalValue::DLLExportLinkage:
116 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
118 case GlobalValue::ExternalLinkage:
119 // If external or appending, declare as a global symbol
120 O << "\t.globl " << name << "\n";
122 case GlobalValue::InternalLinkage:
123 SwitchToDataSection(TAI->getDataSection(), I);
126 assert(0 && "Unknown linkage type!");
129 EmitAlignment(Align, I);
130 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
132 if (TAI->hasDotTypeDotSizeDirective())
133 O << "\t.size " << name << ", " << Size << "\n";
135 EmitGlobalConstant(C);
140 // Output linker support code for dllexported globals
141 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
142 SwitchToDataSection(".section .drectve", 0);
145 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
146 e = DLLExportedGVs.end();
148 O << "\t.ascii \" -export:" << *i << ",data\"\n";
151 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
152 SwitchToDataSection(".section .drectve", 0);
155 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
156 e = DLLExportedFns.end();
158 O << "\t.ascii \" -export:" << *i << "\"\n";
161 if (Subtarget->isTargetDarwin()) {
162 SwitchToDataSection("", 0);
164 // Output stubs for dynamically-linked functions
166 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
168 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
169 "self_modifying_code+pure_instructions,5", 0);
170 O << "L" << *i << "$stub:\n";
171 O << "\t.indirect_symbol " << *i << "\n";
172 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
177 // Output stubs for external and common global variables.
178 if (GVStubs.begin() != GVStubs.end())
180 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
181 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
183 O << "L" << *i << "$non_lazy_ptr:\n";
184 O << "\t.indirect_symbol " << *i << "\n";
188 // Emit initial debug information.
191 // Funny Darwin hack: This flag tells the linker that no global symbols
192 // contain code that falls through to other global symbols (e.g. the obvious
193 // implementation of multiple entry points). If this doesn't occur, the
194 // linker can safely perform dead code stripping. Since LLVM never
195 // generates code that does this, it is always safe to set.
196 O << "\t.subsections_via_symbols\n";
199 AsmPrinter::doFinalization(M);
200 return false; // success
203 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
204 /// for a MachineFunction to the given output stream, using the given target
205 /// machine description.
207 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
208 X86TargetMachine &tm) {
209 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
211 if (Subtarget->isFlavorIntel()) {
212 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
214 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());