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"
20 #include "X86IntelAsmPrinter.h"
21 #include "X86MachineFunctionInfo.h"
22 #include "X86Subtarget.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/CallingConv.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Module.h"
27 #include "llvm/Type.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/Support/Mangler.h"
30 #include "llvm/Target/TargetAsmInfo.h"
31 #include "llvm/Target/TargetOptions.h"
34 static X86FunctionInfo calculateFunctionInfo(const Function *F,
35 const TargetData *TD) {
39 switch (F->getCallingConv()) {
40 case CallingConv::X86_StdCall:
41 Info.setDecorationStyle(StdCall);
43 case CallingConv::X86_FastCall:
44 Info.setDecorationStyle(FastCall);
50 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
52 // Size should be aligned to DWORD boundary
53 Size += ((TD->getTypeSize(AI->getType()) + 3)/4)*4;
55 // We're not supporting tooooo huge arguments :)
56 Info.setBytesToPopOnReturn((unsigned int)Size);
61 /// decorateName - Query FunctionInfoMap and use this information for various
63 void X86SharedAsmPrinter::decorateName(std::string &Name,
64 const GlobalValue *GV) {
65 const Function *F = dyn_cast<Function>(GV);
68 // We don't want to decorate non-stdcall or non-fastcall functions right now
69 unsigned CC = F->getCallingConv();
70 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
73 // Decorate names only when we're targeting Cygwin/Mingw32 targets
74 if (!Subtarget->isTargetCygMing())
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->isTargetELF() ||
113 Subtarget->isTargetCygMing() ||
114 Subtarget->isTargetDarwin()) {
115 // Emit initial debug information.
119 return AsmPrinter::doInitialization(M);
122 bool X86SharedAsmPrinter::doFinalization(Module &M) {
123 // Note: this code is not shared by the Intel printer as it is too different
124 // from how MASM does things. When making changes here don't forget to look
125 // at X86IntelAsmPrinter::doFinalization().
126 const TargetData *TD = TM.getTargetData();
128 // Print out module-level global variables here.
129 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
131 if (!I->hasInitializer())
132 continue; // External global require no code
134 // Check to see if this is a special global used by LLVM, if so, emit it.
135 if (EmitSpecialLLVMGlobal(I)) {
136 if (Subtarget->isTargetDarwin() &&
137 TM.getRelocationModel() == Reloc::Static) {
138 if (I->getName() == "llvm.global_ctors")
139 O << ".reference .constructors_used\n";
140 else if (I->getName() == "llvm.global_dtors")
141 O << ".reference .destructors_used\n";
146 std::string name = Mang->getValueName(I);
147 Constant *C = I->getInitializer();
148 unsigned Size = TD->getTypeSize(C->getType());
149 unsigned Align = TD->getPreferredAlignmentLog(I);
151 if (I->hasHiddenVisibility())
152 if (const char *Directive = TAI->getHiddenDirective())
153 O << Directive << name << "\n";
154 if (Subtarget->isTargetELF())
155 O << "\t.type " << name << ",@object\n";
157 if (C->isNullValue()) {
158 if (I->hasExternalLinkage()) {
159 if (const char *Directive = TAI->getZeroFillDirective()) {
160 O << "\t.globl\t" << name << "\n";
161 O << Directive << "__DATA__, __common, " << name << ", "
162 << Size << ", " << Align << "\n";
167 if (!I->hasSection() &&
168 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
169 I->hasLinkOnceLinkage())) {
170 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
171 if (!NoZerosInBSS && TAI->getBSSSection())
172 SwitchToDataSection(TAI->getBSSSection(), I);
174 SwitchToDataSection(TAI->getDataSection(), I);
175 if (TAI->getLCOMMDirective() != NULL) {
176 if (I->hasInternalLinkage()) {
177 O << TAI->getLCOMMDirective() << name << "," << Size;
178 if (Subtarget->isTargetDarwin())
181 O << TAI->getCOMMDirective() << name << "," << Size;
183 if (!Subtarget->isTargetCygMing()) {
184 if (I->hasInternalLinkage())
185 O << "\t.local\t" << name << "\n";
187 O << TAI->getCOMMDirective() << name << "," << Size;
188 if (TAI->getCOMMDirectiveTakesAlignment())
189 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
191 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
196 switch (I->getLinkage()) {
197 case GlobalValue::LinkOnceLinkage:
198 case GlobalValue::WeakLinkage:
199 if (Subtarget->isTargetDarwin()) {
200 O << "\t.globl " << name << "\n"
201 << "\t.weak_definition " << name << "\n";
202 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
203 } else if (Subtarget->isTargetCygMing()) {
204 std::string SectionName(".section\t.data$linkonce." +
207 SwitchToDataSection(SectionName.c_str(), I);
208 O << "\t.globl " << name << "\n"
209 << "\t.linkonce same_size\n";
211 std::string SectionName("\t.section\t.llvm.linkonce.d." +
213 ",\"aw\",@progbits");
214 SwitchToDataSection(SectionName.c_str(), I);
215 O << "\t.weak " << name << "\n";
218 case GlobalValue::AppendingLinkage:
219 // FIXME: appending linkage variables should go into a section of
220 // their name or something. For now, just emit them as external.
221 case GlobalValue::DLLExportLinkage:
222 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
224 case GlobalValue::ExternalLinkage:
225 // If external or appending, declare as a global symbol
226 O << "\t.globl " << name << "\n";
228 case GlobalValue::InternalLinkage: {
229 if (I->isConstant()) {
230 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
231 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
232 SwitchToDataSection(TAI->getCStringSection(), I);
236 // FIXME: special handling for ".ctors" & ".dtors" sections
237 if (I->hasSection() &&
238 (I->getSection() == ".ctors" ||
239 I->getSection() == ".dtors")) {
240 std::string SectionName = ".section " + I->getSection();
242 if (Subtarget->isTargetCygMing()) {
243 SectionName += ",\"aw\"";
245 assert(!Subtarget->isTargetDarwin());
246 SectionName += ",\"aw\",@progbits";
249 SwitchToDataSection(SectionName.c_str());
251 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
252 SwitchToDataSection(TAI->getBSSSection(), I);
254 SwitchToDataSection(TAI->getDataSection(), I);
260 assert(0 && "Unknown linkage type!");
263 EmitAlignment(Align, I);
264 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
266 if (TAI->hasDotTypeDotSizeDirective())
267 O << "\t.size " << name << ", " << Size << "\n";
268 // If the initializer is a extern weak symbol, remember to emit the weak
270 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
271 if (GV->hasExternalWeakLinkage())
272 ExtWeakSymbols.insert(GV);
274 EmitGlobalConstant(C);
278 // Output linker support code for dllexported globals
279 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
280 SwitchToDataSection(".section .drectve");
283 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
284 e = DLLExportedGVs.end();
286 O << "\t.ascii \" -export:" << *i << ",data\"\n";
289 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
290 SwitchToDataSection(".section .drectve");
293 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
294 e = DLLExportedFns.end();
296 O << "\t.ascii \" -export:" << *i << "\"\n";
299 if (Subtarget->isTargetDarwin()) {
300 SwitchToDataSection("");
302 // Output stubs for dynamically-linked functions
304 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
306 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
307 "self_modifying_code+pure_instructions,5", 0);
308 O << "L" << *i << "$stub:\n";
309 O << "\t.indirect_symbol " << *i << "\n";
310 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
315 // Output stubs for external and common global variables.
316 if (GVStubs.begin() != GVStubs.end())
318 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
319 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
321 O << "L" << *i << "$non_lazy_ptr:\n";
322 O << "\t.indirect_symbol " << *i << "\n";
326 // Emit final debug information.
329 // Funny Darwin hack: This flag tells the linker that no global symbols
330 // contain code that falls through to other global symbols (e.g. the obvious
331 // implementation of multiple entry points). If this doesn't occur, the
332 // linker can safely perform dead code stripping. Since LLVM never
333 // generates code that does this, it is always safe to set.
334 O << "\t.subsections_via_symbols\n";
335 } else if (Subtarget->isTargetCygMing()) {
336 // Emit type information for external functions
337 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
339 O << "\t.def\t " << *i
340 << ";\t.scl\t" << COFF::C_EXT
341 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
345 // Emit final debug information.
347 } else if (Subtarget->isTargetELF()) {
348 // Emit final debug information.
352 AsmPrinter::doFinalization(M);
353 return false; // success
356 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
357 /// for a MachineFunction to the given output stream, using the given target
358 /// machine description.
360 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
361 X86TargetMachine &tm) {
362 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
364 if (Subtarget->isFlavorIntel()) {
365 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
367 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());