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 += TD->getTypeSize(AI->getType());
54 // Size should be aligned to DWORD boundary
55 Size = ((Size + 3)/4)*4;
57 // We're not supporting tooooo huge arguments :)
58 Info.setBytesToPopOnReturn((unsigned int)Size);
63 /// decorateName - Query FunctionInfoMap and use this information for various
65 void X86SharedAsmPrinter::decorateName(std::string &Name,
66 const GlobalValue *GV) {
67 const Function *F = dyn_cast<Function>(GV);
70 // We don't want to decorate non-stdcall or non-fastcall functions right now
71 unsigned CC = F->getCallingConv();
72 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
75 // Decorate names only when we're targeting Cygwin/Mingw32 targets
76 if (!Subtarget->isTargetCygMing())
79 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
81 const X86FunctionInfo *Info;
82 if (info_item == FunctionInfoMap.end()) {
83 // Calculate apropriate function info and populate map
84 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
85 Info = &FunctionInfoMap[F];
87 Info = &info_item->second;
90 switch (Info->getDecorationStyle()) {
94 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
95 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
98 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
99 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
101 if (Name[0] == '_') {
108 assert(0 && "Unsupported DecorationStyle");
113 bool X86SharedAsmPrinter::doInitialization(Module &M) {
114 if (Subtarget->isTargetELF() ||
115 Subtarget->isTargetCygMing() ||
116 Subtarget->isTargetDarwin()) {
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())
134 continue; // External global require no code
136 // Check to see if this is a special global used by LLVM, if so, emit it.
137 if (EmitSpecialLLVMGlobal(I)) {
138 if (Subtarget->isTargetDarwin() &&
139 TM.getRelocationModel() == Reloc::Static) {
140 if (I->getName() == "llvm.global_ctors")
141 O << ".reference .constructors_used\n";
142 else if (I->getName() == "llvm.global_dtors")
143 O << ".reference .destructors_used\n";
148 std::string name = Mang->getValueName(I);
149 Constant *C = I->getInitializer();
150 unsigned Size = TD->getTypeSize(C->getType());
151 unsigned Align = TD->getPreferredAlignmentLog(I);
153 if (I->hasHiddenVisibility())
154 if (const char *Directive = TAI->getHiddenDirective())
155 O << Directive << name << "\n";
156 if (Subtarget->isTargetELF())
157 O << "\t.type " << name << ",@object\n";
159 if (C->isNullValue()) {
160 if (I->hasExternalLinkage()) {
161 if (const char *Directive = TAI->getZeroFillDirective()) {
162 O << "\t.globl\t" << name << "\n";
163 O << Directive << "__DATA__, __common, " << name << ", "
164 << Size << ", " << Align << "\n";
169 if (!I->hasSection() &&
170 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
171 I->hasLinkOnceLinkage())) {
172 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
173 if (!NoZerosInBSS && TAI->getBSSSection())
174 SwitchToDataSection(TAI->getBSSSection(), I);
176 SwitchToDataSection(TAI->getDataSection(), I);
177 if (TAI->getLCOMMDirective() != NULL) {
178 if (I->hasInternalLinkage()) {
179 O << TAI->getLCOMMDirective() << name << "," << Size;
180 if (Subtarget->isTargetDarwin())
183 O << TAI->getCOMMDirective() << name << "," << Size;
185 if (!Subtarget->isTargetCygMing()) {
186 if (I->hasInternalLinkage())
187 O << "\t.local\t" << name << "\n";
189 O << TAI->getCOMMDirective() << name << "," << Size;
190 if (TAI->getCOMMDirectiveTakesAlignment())
191 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
193 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
198 switch (I->getLinkage()) {
199 case GlobalValue::LinkOnceLinkage:
200 case GlobalValue::WeakLinkage:
201 if (Subtarget->isTargetDarwin()) {
202 O << "\t.globl " << name << "\n"
203 << "\t.weak_definition " << name << "\n";
204 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
205 } else if (Subtarget->isTargetCygMing()) {
206 std::string SectionName(".section\t.data$linkonce." +
209 SwitchToDataSection(SectionName.c_str(), I);
210 O << "\t.globl " << name << "\n"
211 << "\t.linkonce same_size\n";
213 std::string SectionName("\t.section\t.llvm.linkonce.d." +
215 ",\"aw\",@progbits");
216 SwitchToDataSection(SectionName.c_str(), I);
217 O << "\t.weak " << name << "\n";
220 case GlobalValue::AppendingLinkage:
221 // FIXME: appending linkage variables should go into a section of
222 // their name or something. For now, just emit them as external.
223 case GlobalValue::DLLExportLinkage:
224 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
226 case GlobalValue::ExternalLinkage:
227 // If external or appending, declare as a global symbol
228 O << "\t.globl " << name << "\n";
230 case GlobalValue::InternalLinkage: {
231 if (I->isConstant()) {
232 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
233 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
234 SwitchToDataSection(TAI->getCStringSection(), I);
238 // FIXME: special handling for ".ctors" & ".dtors" sections
239 if (I->hasSection() &&
240 (I->getSection() == ".ctors" ||
241 I->getSection() == ".dtors")) {
242 std::string SectionName = ".section " + I->getSection();
244 if (Subtarget->isTargetCygMing()) {
245 SectionName += ",\"aw\"";
247 assert(!Subtarget->isTargetDarwin());
248 SectionName += ",\"aw\",@progbits";
251 SwitchToDataSection(SectionName.c_str());
253 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
254 SwitchToDataSection(TAI->getBSSSection(), I);
256 SwitchToDataSection(TAI->getDataSection(), I);
262 assert(0 && "Unknown linkage type!");
265 EmitAlignment(Align, I);
266 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
268 if (TAI->hasDotTypeDotSizeDirective())
269 O << "\t.size " << name << ", " << Size << "\n";
270 // If the initializer is a extern weak symbol, remember to emit the weak
272 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
273 if (GV->hasExternalWeakLinkage())
274 ExtWeakSymbols.insert(GV);
276 EmitGlobalConstant(C);
280 // Output linker support code for dllexported globals
281 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
282 SwitchToDataSection(".section .drectve");
285 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
286 e = DLLExportedGVs.end();
288 O << "\t.ascii \" -export:" << *i << ",data\"\n";
291 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
292 SwitchToDataSection(".section .drectve");
295 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
296 e = DLLExportedFns.end();
298 O << "\t.ascii \" -export:" << *i << "\"\n";
301 if (Subtarget->isTargetDarwin()) {
302 SwitchToDataSection("");
304 // Output stubs for dynamically-linked functions
306 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
308 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
309 "self_modifying_code+pure_instructions,5", 0);
310 O << "L" << *i << "$stub:\n";
311 O << "\t.indirect_symbol " << *i << "\n";
312 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
317 // Output stubs for external and common global variables.
318 if (GVStubs.begin() != GVStubs.end())
320 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
321 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
323 O << "L" << *i << "$non_lazy_ptr:\n";
324 O << "\t.indirect_symbol " << *i << "\n";
328 // Emit final debug information.
331 // Funny Darwin hack: This flag tells the linker that no global symbols
332 // contain code that falls through to other global symbols (e.g. the obvious
333 // implementation of multiple entry points). If this doesn't occur, the
334 // linker can safely perform dead code stripping. Since LLVM never
335 // generates code that does this, it is always safe to set.
336 O << "\t.subsections_via_symbols\n";
337 } else if (Subtarget->isTargetCygMing()) {
338 // Emit type information for external functions
339 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
341 O << "\t.def\t " << *i
342 << ";\t.scl\t" << COFF::C_EXT
343 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
347 // Emit final debug information.
349 } else if (Subtarget->isTargetELF()) {
350 // Emit final debug information.
354 AsmPrinter::doFinalization(M);
355 return false; // success
358 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
359 /// for a MachineFunction to the given output stream, using the given target
360 /// machine description.
362 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
363 X86TargetMachine &tm) {
364 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
366 if (Subtarget->isFlavorIntel()) {
367 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
369 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());