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 "X86MachineFunctionInfo.h"
21 #include "X86Subtarget.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Module.h"
26 #include "llvm/Type.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/Target/TargetAsmInfo.h"
33 Statistic<> llvm::EmittedInsts("asm-printer",
34 "Number of machine instrs printed");
36 static X86FunctionInfo calculateFunctionInfo(const Function *F,
37 const TargetData *TD) {
41 switch (F->getCallingConv()) {
42 case CallingConv::X86_StdCall:
43 Info.setDecorationStyle(StdCall);
45 case CallingConv::X86_FastCall:
46 Info.setDecorationStyle(FastCall);
52 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
54 Size += TD->getTypeSize(AI->getType());
56 // Size should be aligned to DWORD boundary
57 Size = ((Size + 3)/4)*4;
59 // We're not supporting tooooo huge arguments :)
60 Info.setBytesToPopOnReturn((unsigned int)Size);
65 /// decorateName - Query FunctionInfoMap and use this information for various
67 void X86SharedAsmPrinter::decorateName(std::string &Name,
68 const GlobalValue *GV) {
69 const Function *F = dyn_cast<Function>(GV);
72 // We don't want to decorate non-stdcall or non-fastcall functions right now
73 unsigned CC = F->getCallingConv();
74 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
77 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
79 const X86FunctionInfo *Info;
80 if (info_item == FunctionInfoMap.end()) {
81 // Calculate apropriate function info and populate map
82 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
83 Info = &FunctionInfoMap[F];
85 Info = &info_item->second;
88 switch (Info->getDecorationStyle()) {
92 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
93 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
96 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
97 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
106 assert(0 && "Unsupported DecorationStyle");
111 bool X86SharedAsmPrinter::doInitialization(Module &M) {
112 if (Subtarget->isTargetDarwin()) {
113 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
114 if (!Subtarget->is64Bit())
115 X86PICStyle = PICStyle::Stub;
117 // Emit initial debug information.
121 return AsmPrinter::doInitialization(M);
124 bool X86SharedAsmPrinter::doFinalization(Module &M) {
125 // Note: this code is not shared by the Intel printer as it is too different
126 // from how MASM does things. When making changes here don't forget to look
127 // at X86IntelAsmPrinter::doFinalization().
128 const TargetData *TD = TM.getTargetData();
130 // Print out module-level global variables here.
131 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
133 if (!I->hasInitializer()) continue; // External global require no code
135 // Check to see if this is a special global used by LLVM, if so, emit it.
136 if (EmitSpecialLLVMGlobal(I))
139 std::string name = Mang->getValueName(I);
140 Constant *C = I->getInitializer();
141 unsigned Size = TD->getTypeSize(C->getType());
142 unsigned Align = getPreferredAlignmentLog(I);
144 if (C->isNullValue() && /* FIXME: Verify correct */
145 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
146 I->hasLinkOnceLinkage() ||
147 (Subtarget->isTargetDarwin() &&
148 I->hasExternalLinkage() && !I->hasSection()))) {
149 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
150 if (I->hasExternalLinkage()) {
151 O << "\t.globl\t" << name << "\n";
152 O << "\t.zerofill __DATA__, __common, " << name << ", "
153 << Size << ", " << Align;
155 SwitchToDataSection(TAI->getDataSection(), I);
156 if (TAI->getLCOMMDirective() != NULL) {
157 if (I->hasInternalLinkage()) {
158 O << TAI->getLCOMMDirective() << name << "," << Size;
159 if (Subtarget->isTargetDarwin())
160 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
162 O << TAI->getCOMMDirective() << name << "," << Size;
164 if (!Subtarget->isTargetCygwin()) {
165 if (I->hasInternalLinkage())
166 O << "\t.local\t" << name << "\n";
168 O << TAI->getCOMMDirective() << name << "," << Size;
169 if (TAI->getCOMMDirectiveTakesAlignment())
170 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
173 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
175 switch (I->getLinkage()) {
176 case GlobalValue::LinkOnceLinkage:
177 if (Subtarget->isTargetDarwin()) {
178 O << "\t.globl " << name << "\n"
179 << "\t.weak_definition " << name << "\n";
180 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
181 } else if (Subtarget->isTargetCygwin()) {
182 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
183 << "\t.globl " << name << "\n"
184 << "\t.linkonce same_size\n";
186 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
187 << "\t.weak " << name << "\n";
190 case GlobalValue::WeakLinkage:
191 if (Subtarget->isTargetDarwin()) {
192 O << "\t.globl " << name << "\n"
193 << "\t.weak_definition " << name << "\n";
194 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
195 } else if (Subtarget->isTargetCygwin()) {
196 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
197 << "\t.weak " << name << "\n";
199 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
200 << "\t.weak " << name << "\n";
203 case GlobalValue::AppendingLinkage:
204 // FIXME: appending linkage variables should go into a section of
205 // their name or something. For now, just emit them as external.
206 case GlobalValue::DLLExportLinkage:
207 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
209 case GlobalValue::ExternalLinkage:
210 // If external or appending, declare as a global symbol
211 O << "\t.globl " << name << "\n";
213 case GlobalValue::InternalLinkage:
214 SwitchToDataSection(TAI->getDataSection(), I);
217 assert(0 && "Unknown linkage type!");
220 EmitAlignment(Align, I);
221 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
223 if (TAI->hasDotTypeDotSizeDirective())
224 O << "\t.size " << name << ", " << Size << "\n";
226 EmitGlobalConstant(C);
231 // Output linker support code for dllexported globals
232 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
233 SwitchToDataSection(".section .drectve", 0);
236 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
237 e = DLLExportedGVs.end();
239 O << "\t.ascii \" -export:" << *i << ",data\"\n";
242 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
243 SwitchToDataSection(".section .drectve", 0);
246 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
247 e = DLLExportedFns.end();
249 O << "\t.ascii \" -export:" << *i << "\"\n";
252 if (Subtarget->isTargetDarwin()) {
253 SwitchToDataSection("", 0);
255 // Output stubs for dynamically-linked functions
257 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
259 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
260 "self_modifying_code+pure_instructions,5", 0);
261 O << "L" << *i << "$stub:\n";
262 O << "\t.indirect_symbol " << *i << "\n";
263 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
268 // Output stubs for external and common global variables.
269 if (GVStubs.begin() != GVStubs.end())
271 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
272 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
274 O << "L" << *i << "$non_lazy_ptr:\n";
275 O << "\t.indirect_symbol " << *i << "\n";
279 // Emit initial debug information.
282 // Funny Darwin hack: This flag tells the linker that no global symbols
283 // contain code that falls through to other global symbols (e.g. the obvious
284 // implementation of multiple entry points). If this doesn't occur, the
285 // linker can safely perform dead code stripping. Since LLVM never
286 // generates code that does this, it is always safe to set.
287 O << "\t.subsections_via_symbols\n";
290 AsmPrinter::doFinalization(M);
291 return false; // success
294 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
295 /// for a MachineFunction to the given output stream, using the given target
296 /// machine description.
298 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
299 X86TargetMachine &tm) {
300 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
302 if (Subtarget->isFlavorIntel()) {
303 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
305 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());