blob: de12d7cdeec111b6441d46e5e380bf23b7283bd6 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattnerb36cbd02005-07-01 22:44:09 +000010// 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
12// assembly language.
13// This printer is the output mechanism used by `llc'.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "X86AsmPrinter.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000018#include "X86ATTAsmPrinter.h"
Anton Korobeynikovd05ca652007-01-16 16:41:57 +000019#include "X86COFF.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000020#include "X86IntelAsmPrinter.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000021#include "X86MachineFunctionInfo.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000022#include "X86Subtarget.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/CallingConv.h"
Chris Lattnera046e0d2005-12-13 04:53:51 +000025#include "llvm/Constants.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000026#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000027#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000028#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000029#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000030#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattnere87e1152006-09-26 03:57:53 +000034static X86FunctionInfo calculateFunctionInfo(const Function *F,
35 const TargetData *TD) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +000036 X86FunctionInfo Info;
Chris Lattnere87e1152006-09-26 03:57:53 +000037 uint64_t Size = 0;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000038
39 switch (F->getCallingConv()) {
Chris Lattnere87e1152006-09-26 03:57:53 +000040 case CallingConv::X86_StdCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000041 Info.setDecorationStyle(StdCall);
42 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000043 case CallingConv::X86_FastCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000044 Info.setDecorationStyle(FastCall);
45 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000046 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000047 return Info;
48 }
49
Chris Lattnere87e1152006-09-26 03:57:53 +000050 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
51 AI != AE; ++AI)
52 Size += TD->getTypeSize(AI->getType());
Anton Korobeynikovf8248682006-09-20 22:03:51 +000053
Anton Korobeynikov54b1cc62006-10-14 20:53:35 +000054 // Size should be aligned to DWORD boundary
55 Size = ((Size + 3)/4)*4;
56
Anton Korobeynikovf8248682006-09-20 22:03:51 +000057 // We're not supporting tooooo huge arguments :)
Chris Lattnere87e1152006-09-26 03:57:53 +000058 Info.setBytesToPopOnReturn((unsigned int)Size);
Anton Korobeynikovf8248682006-09-20 22:03:51 +000059 return Info;
60}
61
62
Chris Lattnere87e1152006-09-26 03:57:53 +000063/// decorateName - Query FunctionInfoMap and use this information for various
64/// name decoration.
65void X86SharedAsmPrinter::decorateName(std::string &Name,
66 const GlobalValue *GV) {
67 const Function *F = dyn_cast<Function>(GV);
68 if (!F) return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000069
70 // We don't want to decorate non-stdcall or non-fastcall functions right now
Chris Lattnere87e1152006-09-26 03:57:53 +000071 unsigned CC = F->getCallingConv();
72 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
Anton Korobeynikovf8248682006-09-20 22:03:51 +000073 return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000074
75 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
76
Chris Lattnere87e1152006-09-26 03:57:53 +000077 const X86FunctionInfo *Info;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000078 if (info_item == FunctionInfoMap.end()) {
79 // Calculate apropriate function info and populate map
80 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
81 Info = &FunctionInfoMap[F];
82 } else {
Chris Lattnere87e1152006-09-26 03:57:53 +000083 Info = &info_item->second;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000084 }
85
86 switch (Info->getDecorationStyle()) {
Chris Lattnere87e1152006-09-26 03:57:53 +000087 case None:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000088 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000089 case StdCall:
90 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
Anton Korobeynikovf8248682006-09-20 22:03:51 +000091 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Anton Korobeynikovf8248682006-09-20 22:03:51 +000092 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000093 case FastCall:
94 if (!F->isVarArg()) // Variadic functions do not receive @0 suffix.
Anton Korobeynikovf8248682006-09-20 22:03:51 +000095 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Chris Lattnere87e1152006-09-26 03:57:53 +000096
Anton Korobeynikovf8248682006-09-20 22:03:51 +000097 if (Name[0] == '_') {
98 Name[0] = '@';
99 } else {
100 Name = '@' + Name;
101 }
102 break;
Chris Lattnere87e1152006-09-26 03:57:53 +0000103 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000104 assert(0 && "Unsupported DecorationStyle");
105 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000106}
107
Jim Laskey563321a2006-09-06 18:34:40 +0000108/// doInitialization
Evan Cheng25ab6902006-09-08 06:48:29 +0000109bool X86SharedAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000110 if (Subtarget->isTargetELF() ||
111 Subtarget->isTargetCygMing() ||
112 Subtarget->isTargetDarwin()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000113 // Emit initial debug information.
114 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000115 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000116
Reid Spencer5dc81f62005-01-23 03:52:14 +0000117 return AsmPrinter::doInitialization(M);
118}
119
Chris Lattnerac5701c2004-10-04 07:24:48 +0000120bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000121 // 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().
Owen Andersona69571c2006-05-03 01:29:57 +0000124 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000125
Chris Lattnerac5701c2004-10-04 07:24:48 +0000126 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000127 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
128 I != E; ++I) {
Rafael Espindola3e69a7e2006-12-09 23:14:08 +0000129 if (!I->hasInitializer())
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000130 continue; // External global require no code
Chris Lattner9787c642005-11-21 22:48:18 +0000131
Chris Lattnerd1239b72005-12-13 06:32:50 +0000132 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000133 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000134 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000135
Chris Lattner9787c642005-11-21 22:48:18 +0000136 std::string name = Mang->getValueName(I);
137 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000138 unsigned Size = TD->getTypeSize(C->getType());
Devang Patelf9c197e2006-10-24 20:32:14 +0000139 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000140
Chris Lattner8e13e902007-01-17 17:44:25 +0000141 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";
146
147 if (C->isNullValue()) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000148 if (I->hasExternalLinkage()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000149 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000150 O << "\t.globl\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000151 O << Directive << "__DATA__, __common, " << name << ", "
Evan Cheng17ef92e2006-02-15 01:56:23 +0000152 << Size << ", " << Align;
Chris Lattner8e13e902007-01-17 17:44:25 +0000153 continue;
154 }
155 }
156
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.
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000161 if (!NoZerosInBSS && TAI->getBSSSection())
162 SwitchToDataSection(TAI->getBSSSection(), I);
163 else
164 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskey563321a2006-09-06 18:34:40 +0000165 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000166 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000167 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000168 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +0000169 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000170 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000171 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000172 } else {
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000173 if (!Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000174 if (I->hasInternalLinkage())
175 O << "\t.local\t" << name << "\n";
176 }
Jim Laskey563321a2006-09-06 18:34:40 +0000177 O << TAI->getCOMMDirective() << name << "," << Size;
178 if (TAI->getCOMMDirectiveTakesAlignment())
179 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000180 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000181 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
182 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000183 }
Chris Lattner9787c642005-11-21 22:48:18 +0000184 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000185
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." +
195 name +
196 ",\"aw\"");
197 SwitchToDataSection(SectionName.c_str(), I);
198 O << "\t.globl " << name << "\n"
199 << "\t.linkonce same_size\n";
200 } else {
201 std::string SectionName("\t.section\t.llvm.linkonce.d." +
202 name +
203 ",\"aw\",@progbits");
204 SwitchToDataSection(SectionName.c_str(), I);
205 O << "\t.weak " << name << "\n";
206 }
207 break;
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(),""));
213 // FALL THROUGH
214 case GlobalValue::ExternalLinkage:
215 // If external or appending, declare as a global symbol
216 O << "\t.globl " << name << "\n";
217 // FALL THROUGH
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);
223 break;
224 }
225 }
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();
231
232 if (Subtarget->isTargetCygMing()) {
233 SectionName += ",\"aw\"";
234 } else {
235 assert(!Subtarget->isTargetDarwin());
236 SectionName += ",\"aw\",@progbits";
237 }
238
239 SwitchToDataSection(SectionName.c_str());
240 } else {
241 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
242 SwitchToDataSection(TAI->getBSSSection(), I);
243 else
244 SwitchToDataSection(TAI->getDataSection(), I);
245 }
246
247 break;
248 }
249 default:
250 assert(0 && "Unknown linkage type!");
251 }
252
253 EmitAlignment(Align, I);
254 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
255 << "\n";
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
259 // reference!
260 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
261 if (GV->hasExternalWeakLinkage())
262 ExtWeakSymbols.insert(GV);
263
264 EmitGlobalConstant(C);
265 O << '\n';
Chris Lattnera35a8e82005-11-21 22:39:40 +0000266 }
267
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000268 // Output linker support code for dllexported globals
269 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000270 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000271 }
272
273 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
274 e = DLLExportedGVs.end();
275 i != e; ++i) {
276 O << "\t.ascii \" -export:" << *i << ",data\"\n";
277 }
278
279 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000280 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000281 }
282
283 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
284 e = DLLExportedFns.end();
285 i != e; ++i) {
286 O << "\t.ascii \" -export:" << *i << "\"\n";
287 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000288
Jim Laskeyea348582006-07-27 02:05:13 +0000289 if (Subtarget->isTargetDarwin()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000290 SwitchToDataSection("");
Nate Begeman73213f62005-07-12 01:37:28 +0000291
Nate Begeman72b286b2005-07-08 00:23:26 +0000292 // Output stubs for dynamically-linked functions
293 unsigned j = 1;
294 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000295 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000296 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
297 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000298 O << "L" << *i << "$stub:\n";
299 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000300 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000301 }
302
303 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000304
Evan Cheng2338c5c2006-02-07 08:38:37 +0000305 // Output stubs for external and common global variables.
306 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000307 SwitchToDataSection(
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000308 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000309 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
310 i != e; ++i) {
311 O << "L" << *i << "$non_lazy_ptr:\n";
312 O << "\t.indirect_symbol " << *i << "\n";
313 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000314 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000315
Reid Spencer02b85112006-10-30 22:32:30 +0000316 // Emit final debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000317 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000318
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
Jim Laskey99db0442006-03-23 18:09:44 +0000322 // linker can safely perform dead code stripping. Since LLVM never
323 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000324 O << "\t.subsections_via_symbols\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000325 } else if (Subtarget->isTargetCygMing()) {
326 // Emit type information for external functions
327 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
328 i != e; ++i) {
329 O << "\t.def\t " << *i
330 << ";\t.scl\t" << COFF::C_EXT
331 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
332 << ";\t.endef\n";
333 }
334
335 // Emit final debug information.
336 DW.EndModule();
337 } else if (Subtarget->isTargetELF()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000338 // Emit final debug information.
339 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000340 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000341
Chris Lattnerac5701c2004-10-04 07:24:48 +0000342 AsmPrinter::doFinalization(M);
343 return false; // success
344}
345
Chris Lattnerac5701c2004-10-04 07:24:48 +0000346/// 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.
349///
Evan Chengc4c62572006-03-13 23:20:37 +0000350FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000351 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000352 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000353
Jim Laskey05a059d2006-09-07 12:23:47 +0000354 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000355 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000356 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000357 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000358 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000359}