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 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
77 const X86FunctionInfo *Info;
78 if (info_item == FunctionInfoMap.end()) {
79 // Calculate apropriate function info and populate map
80 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
81 Info = &FunctionInfoMap[F];
83 Info = &info_item->second;
86 switch (Info->getDecorationStyle()) {
90 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
91 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
94 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
95 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
104 assert(0 && "Unsupported DecorationStyle");
109 bool X86SharedAsmPrinter::doInitialization(Module &M) {
110 if (Subtarget->isTargetELF() ||
111 Subtarget->isTargetCygMing() ||
112 Subtarget->isTargetDarwin()) {
113 // Emit initial debug information.
117 return AsmPrinter::doInitialization(M);
120 bool X86SharedAsmPrinter::doFinalization(Module &M) {
121 // Note: this code is not shared by the Intel printer as it is too different
122 // from how MASM does things. When making changes here don't forget to look
123 // at X86IntelAsmPrinter::doFinalization().
124 const TargetData *TD = TM.getTargetData();
126 // Print out module-level global variables here.
127 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
129 if (!I->hasInitializer())
130 continue; // External global require no code
132 // Check to see if this is a special global used by LLVM, if so, emit it.
133 if (EmitSpecialLLVMGlobal(I))
136 std::string name = Mang->getValueName(I);
137 Constant *C = I->getInitializer();
138 unsigned Size = TD->getTypeSize(C->getType());
139 unsigned Align = TD->getPreferredAlignmentLog(I);
141 if (I->hasHiddenVisibility())
142 if (const char *Directive = TAI->getHiddenDirective())
143 O << Directive << name << "\n";
144 if (Subtarget->isTargetELF())
145 O << "\t.type " << name << ",@object\n";
147 if (C->isNullValue()) {
148 if (I->hasExternalLinkage()) {
149 if (const char *Directive = TAI->getZeroFillDirective()) {
150 O << "\t.globl\t" << name << "\n";
151 O << Directive << "__DATA__, __common, " << name << ", "
152 << Size << ", " << Align << "\n";
157 if (!I->hasSection() &&
158 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
159 I->hasLinkOnceLinkage())) {
160 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
161 if (!NoZerosInBSS && TAI->getBSSSection())
162 SwitchToDataSection(TAI->getBSSSection(), I);
164 SwitchToDataSection(TAI->getDataSection(), I);
165 if (TAI->getLCOMMDirective() != NULL) {
166 if (I->hasInternalLinkage()) {
167 O << TAI->getLCOMMDirective() << name << "," << Size;
168 if (Subtarget->isTargetDarwin())
171 O << TAI->getCOMMDirective() << name << "," << Size;
173 if (!Subtarget->isTargetCygMing()) {
174 if (I->hasInternalLinkage())
175 O << "\t.local\t" << name << "\n";
177 O << TAI->getCOMMDirective() << name << "," << Size;
178 if (TAI->getCOMMDirectiveTakesAlignment())
179 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
181 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
186 switch (I->getLinkage()) {
187 case GlobalValue::LinkOnceLinkage:
188 case GlobalValue::WeakLinkage:
189 if (Subtarget->isTargetDarwin()) {
190 O << "\t.globl " << name << "\n"
191 << "\t.weak_definition " << name << "\n";
192 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
193 } else if (Subtarget->isTargetCygMing()) {
194 std::string SectionName(".section\t.data$linkonce." +
197 SwitchToDataSection(SectionName.c_str(), I);
198 O << "\t.globl " << name << "\n"
199 << "\t.linkonce same_size\n";
201 std::string SectionName("\t.section\t.llvm.linkonce.d." +
203 ",\"aw\",@progbits");
204 SwitchToDataSection(SectionName.c_str(), I);
205 O << "\t.weak " << name << "\n";
208 case GlobalValue::AppendingLinkage:
209 // FIXME: appending linkage variables should go into a section of
210 // their name or something. For now, just emit them as external.
211 case GlobalValue::DLLExportLinkage:
212 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
214 case GlobalValue::ExternalLinkage:
215 // If external or appending, declare as a global symbol
216 O << "\t.globl " << name << "\n";
218 case GlobalValue::InternalLinkage: {
219 if (I->isConstant()) {
220 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
221 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
222 SwitchToDataSection(TAI->getCStringSection(), I);
226 // FIXME: special handling for ".ctors" & ".dtors" sections
227 if (I->hasSection() &&
228 (I->getSection() == ".ctors" ||
229 I->getSection() == ".dtors")) {
230 std::string SectionName = ".section " + I->getSection();
232 if (Subtarget->isTargetCygMing()) {
233 SectionName += ",\"aw\"";
235 assert(!Subtarget->isTargetDarwin());
236 SectionName += ",\"aw\",@progbits";
239 SwitchToDataSection(SectionName.c_str());
241 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
242 SwitchToDataSection(TAI->getBSSSection(), I);
244 SwitchToDataSection(TAI->getDataSection(), I);
250 assert(0 && "Unknown linkage type!");
253 EmitAlignment(Align, I);
254 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
256 if (TAI->hasDotTypeDotSizeDirective())
257 O << "\t.size " << name << ", " << Size << "\n";
258 // If the initializer is a extern weak symbol, remember to emit the weak
260 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
261 if (GV->hasExternalWeakLinkage())
262 ExtWeakSymbols.insert(GV);
264 EmitGlobalConstant(C);
268 // Output linker support code for dllexported globals
269 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
270 SwitchToDataSection(".section .drectve");
273 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
274 e = DLLExportedGVs.end();
276 O << "\t.ascii \" -export:" << *i << ",data\"\n";
279 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
280 SwitchToDataSection(".section .drectve");
283 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
284 e = DLLExportedFns.end();
286 O << "\t.ascii \" -export:" << *i << "\"\n";
289 if (Subtarget->isTargetDarwin()) {
290 SwitchToDataSection("");
292 // Output stubs for dynamically-linked functions
294 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
296 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
297 "self_modifying_code+pure_instructions,5", 0);
298 O << "L" << *i << "$stub:\n";
299 O << "\t.indirect_symbol " << *i << "\n";
300 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
305 // Output stubs for external and common global variables.
306 if (GVStubs.begin() != GVStubs.end())
308 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
309 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
311 O << "L" << *i << "$non_lazy_ptr:\n";
312 O << "\t.indirect_symbol " << *i << "\n";
316 // Emit final debug information.
319 // Funny Darwin hack: This flag tells the linker that no global symbols
320 // contain code that falls through to other global symbols (e.g. the obvious
321 // implementation of multiple entry points). If this doesn't occur, the
322 // linker can safely perform dead code stripping. Since LLVM never
323 // generates code that does this, it is always safe to set.
324 O << "\t.subsections_via_symbols\n";
325 } else if (Subtarget->isTargetCygMing()) {
326 // Emit type information for external functions
327 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
329 O << "\t.def\t " << *i
330 << ";\t.scl\t" << COFF::C_EXT
331 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
335 // Emit final debug information.
337 } else if (Subtarget->isTargetELF()) {
338 // Emit final debug information.
342 AsmPrinter::doFinalization(M);
343 return false; // success
346 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
347 /// for a MachineFunction to the given output stream, using the given target
348 /// machine description.
350 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
351 X86TargetMachine &tm) {
352 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
354 if (Subtarget->isFlavorIntel()) {
355 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
357 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());