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"
32 static X86FunctionInfo calculateFunctionInfo(const Function *F,
33 const TargetData *TD) {
37 switch (F->getCallingConv()) {
38 case CallingConv::X86_StdCall:
39 Info.setDecorationStyle(StdCall);
41 case CallingConv::X86_FastCall:
42 Info.setDecorationStyle(FastCall);
48 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
50 Size += TD->getTypeSize(AI->getType());
52 // Size should be aligned to DWORD boundary
53 Size = ((Size + 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 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
75 const X86FunctionInfo *Info;
76 if (info_item == FunctionInfoMap.end()) {
77 // Calculate apropriate function info and populate map
78 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
79 Info = &FunctionInfoMap[F];
81 Info = &info_item->second;
84 switch (Info->getDecorationStyle()) {
88 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
89 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
92 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
93 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
102 assert(0 && "Unsupported DecorationStyle");
107 bool X86SharedAsmPrinter::doInitialization(Module &M) {
108 if (Subtarget->isTargetDarwin()) {
109 if (!Subtarget->is64Bit())
110 X86PICStyle = PICStyle::Stub;
112 // Emit initial debug information.
114 } else if (Subtarget->isTargetELF() || Subtarget->isTargetCygMing()) {
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))
138 std::string name = Mang->getValueName(I);
139 Constant *C = I->getInitializer();
140 unsigned Size = TD->getTypeSize(C->getType());
141 unsigned Align = TD->getPreferredAlignmentLog(I);
143 if (C->isNullValue() && /* FIXME: Verify correct */
145 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
146 I->hasLinkOnceLinkage() ||
147 (Subtarget->isTargetDarwin() &&
148 I->hasExternalLinkage()))) {
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->isTargetCygMing()) {
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 case GlobalValue::WeakLinkage:
178 if (Subtarget->isTargetDarwin()) {
179 O << "\t.globl " << name << "\n"
180 << "\t.weak_definition " << name << "\n";
181 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
182 } else if (Subtarget->isTargetCygMing()) {
183 std::string SectionName(".section\t.data$linkonce." +
186 SwitchToDataSection(SectionName.c_str(), I);
187 O << "\t.globl " << name << "\n"
188 << "\t.linkonce same_size\n";
190 std::string SectionName("\t.section\t.llvm.linkonce.d." +
192 ",\"aw\",@progbits");
193 SwitchToDataSection(SectionName.c_str(), I);
194 O << "\t.weak " << name << "\n";
197 case GlobalValue::AppendingLinkage:
198 // FIXME: appending linkage variables should go into a section of
199 // their name or something. For now, just emit them as external.
200 case GlobalValue::DLLExportLinkage:
201 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
203 case GlobalValue::ExternalLinkage:
204 // If external or appending, declare as a global symbol
205 O << "\t.globl " << name << "\n";
207 case GlobalValue::InternalLinkage: {
208 if (I->isConstant()) {
209 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
210 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
211 SwitchToDataSection(TAI->getCStringSection(), I);
215 // FIXME: special handling for ".ctors" & ".dtors" sections
216 if (I->hasSection() &&
217 (I->getSection() == ".ctors" ||
218 I->getSection() == ".dtors")) {
219 std::string SectionName = ".section " + I->getSection();
221 if (Subtarget->isTargetCygMing()) {
222 SectionName += ",\"aw\"";
224 assert(!Subtarget->isTargetDarwin());
225 SectionName += ",\"aw\",@progbits";
228 SwitchToDataSection(SectionName.c_str());
230 SwitchToDataSection(TAI->getDataSection(), I);
236 assert(0 && "Unknown linkage type!");
239 EmitAlignment(Align, I);
240 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
242 if (TAI->hasDotTypeDotSizeDirective())
243 O << "\t.size " << name << ", " << Size << "\n";
245 // If the initializer is a extern weak symbol, remember to emit the weak
247 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
248 if (GV->hasExternalWeakLinkage())
249 ExtWeakSymbols.insert(GV);
251 EmitGlobalConstant(C);
256 // Output linker support code for dllexported globals
257 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
258 SwitchToDataSection(".section .drectve");
261 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
262 e = DLLExportedGVs.end();
264 O << "\t.ascii \" -export:" << *i << ",data\"\n";
267 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
268 SwitchToDataSection(".section .drectve");
271 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
272 e = DLLExportedFns.end();
274 O << "\t.ascii \" -export:" << *i << "\"\n";
277 if (Subtarget->isTargetDarwin()) {
278 SwitchToDataSection("");
280 // Output stubs for dynamically-linked functions
282 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
284 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
285 "self_modifying_code+pure_instructions,5", 0);
286 O << "L" << *i << "$stub:\n";
287 O << "\t.indirect_symbol " << *i << "\n";
288 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
293 // Output stubs for external and common global variables.
294 if (GVStubs.begin() != GVStubs.end())
296 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
297 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
299 O << "L" << *i << "$non_lazy_ptr:\n";
300 O << "\t.indirect_symbol " << *i << "\n";
304 // Emit final debug information.
307 // Funny Darwin hack: This flag tells the linker that no global symbols
308 // contain code that falls through to other global symbols (e.g. the obvious
309 // implementation of multiple entry points). If this doesn't occur, the
310 // linker can safely perform dead code stripping. Since LLVM never
311 // generates code that does this, it is always safe to set.
312 O << "\t.subsections_via_symbols\n";
313 } else if (Subtarget->isTargetELF() || Subtarget->isTargetCygMing()) {
314 // Emit final debug information.
318 AsmPrinter::doFinalization(M);
319 return false; // success
322 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
323 /// for a MachineFunction to the given output stream, using the given target
324 /// machine description.
326 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
327 X86TargetMachine &tm) {
328 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
330 if (Subtarget->isFlavorIntel()) {
331 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
333 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());