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"
33 static X86FunctionInfo calculateFunctionInfo(const Function *F,
34 const TargetData *TD) {
38 switch (F->getCallingConv()) {
39 case CallingConv::X86_StdCall:
40 Info.setDecorationStyle(StdCall);
42 case CallingConv::X86_FastCall:
43 Info.setDecorationStyle(FastCall);
49 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
51 Size += TD->getTypeSize(AI->getType());
53 // Size should be aligned to DWORD boundary
54 Size = ((Size + 3)/4)*4;
56 // We're not supporting tooooo huge arguments :)
57 Info.setBytesToPopOnReturn((unsigned int)Size);
62 /// decorateName - Query FunctionInfoMap and use this information for various
64 void X86SharedAsmPrinter::decorateName(std::string &Name,
65 const GlobalValue *GV) {
66 const Function *F = dyn_cast<Function>(GV);
69 // We don't want to decorate non-stdcall or non-fastcall functions right now
70 unsigned CC = F->getCallingConv();
71 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
74 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
76 const X86FunctionInfo *Info;
77 if (info_item == FunctionInfoMap.end()) {
78 // Calculate apropriate function info and populate map
79 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
80 Info = &FunctionInfoMap[F];
82 Info = &info_item->second;
85 switch (Info->getDecorationStyle()) {
89 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
90 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
93 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
94 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
103 assert(0 && "Unsupported DecorationStyle");
108 bool X86SharedAsmPrinter::doInitialization(Module &M) {
109 if (Subtarget->isTargetELF() ||
110 Subtarget->isTargetCygMing() ||
111 Subtarget->isTargetDarwin()) {
112 // Emit initial debug information.
116 return AsmPrinter::doInitialization(M);
119 bool X86SharedAsmPrinter::doFinalization(Module &M) {
120 // Note: this code is not shared by the Intel printer as it is too different
121 // from how MASM does things. When making changes here don't forget to look
122 // at X86IntelAsmPrinter::doFinalization().
123 const TargetData *TD = TM.getTargetData();
125 // Print out module-level global variables here.
126 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
128 if (!I->hasInitializer())
129 continue; // External global require no code
131 // Check to see if this is a special global used by LLVM, if so, emit it.
132 if (EmitSpecialLLVMGlobal(I))
135 std::string name = Mang->getValueName(I);
136 Constant *C = I->getInitializer();
137 unsigned Size = TD->getTypeSize(C->getType());
138 unsigned Align = TD->getPreferredAlignmentLog(I);
140 if (C->isNullValue() && /* FIXME: Verify correct */
142 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
143 I->hasLinkOnceLinkage() ||
144 (Subtarget->isTargetDarwin() &&
145 I->hasExternalLinkage()))) {
146 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
147 if (I->hasExternalLinkage()) {
148 O << "\t.globl\t" << name << "\n";
149 O << "\t.zerofill __DATA__, __common, " << name << ", "
150 << Size << ", " << Align;
152 SwitchToDataSection(TAI->getDataSection(), I);
153 if (TAI->getLCOMMDirective() != NULL) {
154 if (I->hasInternalLinkage()) {
155 O << TAI->getLCOMMDirective() << name << "," << Size;
156 if (Subtarget->isTargetDarwin())
157 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
159 O << TAI->getCOMMDirective() << name << "," << Size;
161 if (!Subtarget->isTargetCygMing()) {
162 if (I->hasInternalLinkage())
163 O << "\t.local\t" << name << "\n";
165 O << TAI->getCOMMDirective() << name << "," << Size;
166 if (TAI->getCOMMDirectiveTakesAlignment())
167 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
170 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
172 switch (I->getLinkage()) {
173 case GlobalValue::LinkOnceLinkage:
174 case GlobalValue::WeakLinkage:
175 if (Subtarget->isTargetDarwin()) {
176 O << "\t.globl " << name << "\n"
177 << "\t.weak_definition " << name << "\n";
178 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
179 } else if (Subtarget->isTargetCygMing()) {
180 std::string SectionName(".section\t.data$linkonce." +
183 SwitchToDataSection(SectionName.c_str(), I);
184 O << "\t.globl " << name << "\n"
185 << "\t.linkonce same_size\n";
187 std::string SectionName("\t.section\t.llvm.linkonce.d." +
189 ",\"aw\",@progbits");
190 SwitchToDataSection(SectionName.c_str(), I);
191 O << "\t.weak " << name << "\n";
194 case GlobalValue::AppendingLinkage:
195 // FIXME: appending linkage variables should go into a section of
196 // their name or something. For now, just emit them as external.
197 case GlobalValue::DLLExportLinkage:
198 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
200 case GlobalValue::ExternalLinkage:
201 // If external or appending, declare as a global symbol
202 O << "\t.globl " << name << "\n";
204 case GlobalValue::InternalLinkage: {
205 if (I->isConstant()) {
206 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
207 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
208 SwitchToDataSection(TAI->getCStringSection(), I);
212 // FIXME: special handling for ".ctors" & ".dtors" sections
213 if (I->hasSection() &&
214 (I->getSection() == ".ctors" ||
215 I->getSection() == ".dtors")) {
216 std::string SectionName = ".section " + I->getSection();
218 if (Subtarget->isTargetCygMing()) {
219 SectionName += ",\"aw\"";
221 assert(!Subtarget->isTargetDarwin());
222 SectionName += ",\"aw\",@progbits";
225 SwitchToDataSection(SectionName.c_str());
227 SwitchToDataSection(TAI->getDataSection(), I);
233 assert(0 && "Unknown linkage type!");
236 EmitAlignment(Align, I);
237 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
239 if (TAI->hasDotTypeDotSizeDirective())
240 O << "\t.size " << name << ", " << Size << "\n";
241 // If the initializer is a extern weak symbol, remember to emit the weak
243 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
244 if (GV->hasExternalWeakLinkage())
245 ExtWeakSymbols.insert(GV);
247 EmitGlobalConstant(C);
250 if (I->hasHiddenVisibility())
251 if (const char *Directive = TAI->getHiddenDirective())
252 O << Directive << name << "\n";
254 if (Subtarget->isTargetELF())
255 O << "\t.type " << name << ",@object\n";
258 // Output linker support code for dllexported globals
259 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
260 SwitchToDataSection(".section .drectve");
263 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
264 e = DLLExportedGVs.end();
266 O << "\t.ascii \" -export:" << *i << ",data\"\n";
269 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
270 SwitchToDataSection(".section .drectve");
273 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
274 e = DLLExportedFns.end();
276 O << "\t.ascii \" -export:" << *i << "\"\n";
279 if (Subtarget->isTargetDarwin()) {
280 SwitchToDataSection("");
282 // Output stubs for dynamically-linked functions
284 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
286 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
287 "self_modifying_code+pure_instructions,5", 0);
288 O << "L" << *i << "$stub:\n";
289 O << "\t.indirect_symbol " << *i << "\n";
290 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
295 // Output stubs for external and common global variables.
296 if (GVStubs.begin() != GVStubs.end())
298 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
299 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
301 O << "L" << *i << "$non_lazy_ptr:\n";
302 O << "\t.indirect_symbol " << *i << "\n";
306 // Emit final debug information.
309 // Funny Darwin hack: This flag tells the linker that no global symbols
310 // contain code that falls through to other global symbols (e.g. the obvious
311 // implementation of multiple entry points). If this doesn't occur, the
312 // linker can safely perform dead code stripping. Since LLVM never
313 // generates code that does this, it is always safe to set.
314 O << "\t.subsections_via_symbols\n";
315 } else if (Subtarget->isTargetCygMing()) {
316 // Emit type information for external functions
317 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
319 O << "\t.def\t " << *i
320 << ";\t.scl\t" << COFF::C_EXT
321 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
325 // Emit final debug information.
327 } else if (Subtarget->isTargetELF()) {
328 // Emit final debug information.
332 AsmPrinter::doFinalization(M);
333 return false; // success
336 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
337 /// for a MachineFunction to the given output stream, using the given target
338 /// machine description.
340 FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
341 X86TargetMachine &tm) {
342 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
344 if (Subtarget->isFlavorIntel()) {
345 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
347 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());