blob: 7dcdd96592d8fa7019aebbce8cc1f48f5078ff25 [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"
19#include "X86IntelAsmPrinter.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "X86MachineFunctionInfo.h"
Chris Lattnera35a8e82005-11-21 22:39:40 +000021#include "X86Subtarget.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000022#include "llvm/ADT/StringExtras.h"
23#include "llvm/CallingConv.h"
Chris Lattnera046e0d2005-12-13 04:53:51 +000024#include "llvm/Constants.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000025#include "llvm/Module.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000026#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000027#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000028#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000030
Chris Lattner300d0ed2004-02-14 06:00:36 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Evan Chengc4c62572006-03-13 23:20:37 +000033Statistic<> llvm::EmittedInsts("asm-printer",
34 "Number of machine instrs printed");
Chris Lattner955f0962004-10-04 07:31:08 +000035
Anton Korobeynikovf8248682006-09-20 22:03:51 +000036static X86FunctionInfo calculateFunctionInfo(const Function* F,
37 const TargetData* TD)
38{
39 X86FunctionInfo Info;
40 uint64_t size = 0;
41
42 switch (F->getCallingConv()) {
43 case CallingConv::X86_StdCall:
44 Info.setDecorationStyle(StdCall);
45 break;
46 case CallingConv::X86_FastCall:
47 Info.setDecorationStyle(FastCall);
48 break;
49 default:
50 return Info;
51 }
52
53 for (Function::const_arg_iterator AI = F->arg_begin(),
54 AE = F->arg_end();
55 AI != AE;
56 ++AI) {
57 size += TD->getTypeSize(AI->getType());
58 }
59
60 // We're not supporting tooooo huge arguments :)
61 Info.setBytesToPopOnReturn((unsigned int)size);
62
63 return Info;
64}
65
66
67// Query FunctionInfoMap and use this information for various name decoration
68void X86SharedAsmPrinter::decorateName(std::string& Name, const GlobalValue* GV)
69{
70 const X86FunctionInfo* Info;
71 const Function* F;
72
73 if ((F = dyn_cast<Function>(GV)) == NULL) {
74 return;
75 }
76
77 unsigned CC = F->getCallingConv();
78
79 // We don't want to decorate non-stdcall or non-fastcall functions right now
80 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) {
81 return;
82 }
83
84 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
85
86 if (info_item == FunctionInfoMap.end()) {
87 // Calculate apropriate function info and populate map
88 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
89 Info = &FunctionInfoMap[F];
90 } else {
91 Info = &(info_item->second);
92 }
93
94 switch (Info->getDecorationStyle()) {
95 case None:
96 break;
97 case StdCall:
98 if (!F->isVarArg()) {
99 // Variadic functions do not receive @0 suffix
100 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
101 }
102 break;
103 case FastCall:
104 if (!F->isVarArg()) {
105 // Variadic functions do not receive @0 suffix
106 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
107 }
108 if (Name[0] == '_') {
109 Name[0] = '@';
110 } else {
111 Name = '@' + Name;
112 }
113 break;
114 default:
115 assert(0 && "Unsupported DecorationStyle");
116 }
117
118}
119
Jim Laskey563321a2006-09-06 18:34:40 +0000120/// doInitialization
Evan Cheng25ab6902006-09-08 06:48:29 +0000121bool X86SharedAsmPrinter::doInitialization(Module &M) {
Jim Laskeyea348582006-07-27 02:05:13 +0000122 if (Subtarget->isTargetDarwin()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000123 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
124 if (!Subtarget->is64Bit())
125 X86PICStyle = PICStyle::Stub;
126
Evan Chengd5948812006-03-07 02:23:26 +0000127 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000128 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000129 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000130
Reid Spencer5dc81f62005-01-23 03:52:14 +0000131 return AsmPrinter::doInitialization(M);
132}
133
Chris Lattnerac5701c2004-10-04 07:24:48 +0000134bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000135 // Note: this code is not shared by the Intel printer as it is too different
136 // from how MASM does things. When making changes here don't forget to look
137 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000138 const TargetData *TD = TM.getTargetData();
Chris Lattnerac5701c2004-10-04 07:24:48 +0000139
140 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000141 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
142 I != E; ++I) {
Chris Lattner9787c642005-11-21 22:48:18 +0000143 if (!I->hasInitializer()) continue; // External global require no code
144
Chris Lattnerd1239b72005-12-13 06:32:50 +0000145 // Check to see if this is a special global used by LLVM, if so, emit it.
Jim Laskey78098112006-03-07 22:00:35 +0000146 if (EmitSpecialLLVMGlobal(I))
Chris Lattnerd1239b72005-12-13 06:32:50 +0000147 continue;
Chris Lattnera046e0d2005-12-13 04:53:51 +0000148
Chris Lattner9787c642005-11-21 22:48:18 +0000149 std::string name = Mang->getValueName(I);
150 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000151 unsigned Size = TD->getTypeSize(C->getType());
Chris Lattnerff805b52006-02-05 01:45:04 +0000152 unsigned Align = getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000153
Evan Cheng2338c5c2006-02-07 08:38:37 +0000154 if (C->isNullValue() && /* FIXME: Verify correct */
155 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Evan Cheng17ef92e2006-02-15 01:56:23 +0000156 I->hasLinkOnceLinkage() ||
Jim Laskeyea348582006-07-27 02:05:13 +0000157 (Subtarget->isTargetDarwin() &&
Evan Cheng932ad512006-05-25 21:59:08 +0000158 I->hasExternalLinkage() && !I->hasSection()))) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000159 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Evan Cheng17ef92e2006-02-15 01:56:23 +0000160 if (I->hasExternalLinkage()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000161 O << "\t.globl\t" << name << "\n";
Evan Cheng17ef92e2006-02-15 01:56:23 +0000162 O << "\t.zerofill __DATA__, __common, " << name << ", "
163 << Size << ", " << Align;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000165 SwitchToDataSection(TAI->getDataSection(), I);
166 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000167 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000168 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000169 if (Subtarget->isTargetDarwin())
Jim Laskey563321a2006-09-06 18:34:40 +0000170 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000171 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000172 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000173 } else {
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000174 if (!Subtarget->isTargetCygwin()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000175 if (I->hasInternalLinkage())
176 O << "\t.local\t" << name << "\n";
177 }
Jim Laskey563321a2006-09-06 18:34:40 +0000178 O << TAI->getCOMMDirective() << name << "," << Size;
179 if (TAI->getCOMMDirectiveTakesAlignment())
180 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000181 }
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000182 }
Jim Laskey563321a2006-09-06 18:34:40 +0000183 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 } else {
185 switch (I->getLinkage()) {
186 case GlobalValue::LinkOnceLinkage:
187 case GlobalValue::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000188 if (Subtarget->isTargetDarwin()) {
Evan Cheng5ada3702006-02-07 23:32:58 +0000189 O << "\t.globl " << name << "\n"
190 << "\t.weak_definition " << name << "\n";
Evan Chengf8614db2006-06-04 07:24:07 +0000191 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000192 } else if (Subtarget->isTargetCygwin()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000193 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
194 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000195 } else {
Evan Cheng932ad512006-05-25 21:59:08 +0000196 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
197 << "\t.weak " << name << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000198 }
199 break;
200 case GlobalValue::AppendingLinkage:
201 // FIXME: appending linkage variables should go into a section of
202 // their name or something. For now, just emit them as external.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000203 case GlobalValue::DLLExportLinkage:
204 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
205 // FALL THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +0000206 case GlobalValue::ExternalLinkage:
207 // If external or appending, declare as a global symbol
208 O << "\t.globl " << name << "\n";
209 // FALL THROUGH
210 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000211 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000212 break;
213 default:
214 assert(0 && "Unknown linkage type!");
215 }
Chris Lattner9787c642005-11-21 22:48:18 +0000216
Evan Cheng2338c5c2006-02-07 08:38:37 +0000217 EmitAlignment(Align, I);
Jim Laskey563321a2006-09-06 18:34:40 +0000218 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
Evan Cheng5ada3702006-02-07 23:32:58 +0000219 << "\n";
Jim Laskey563321a2006-09-06 18:34:40 +0000220 if (TAI->hasDotTypeDotSizeDirective())
Evan Cheng5ada3702006-02-07 23:32:58 +0000221 O << "\t.size " << name << ", " << Size << "\n";
222
Evan Cheng2338c5c2006-02-07 08:38:37 +0000223 EmitGlobalConstant(C);
224 O << '\n';
Chris Lattner9787c642005-11-21 22:48:18 +0000225 }
Chris Lattnera35a8e82005-11-21 22:39:40 +0000226 }
227
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000228 // Output linker support code for dllexported globals
229 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
230 SwitchToDataSection(".section .drectve", 0);
231 }
232
233 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
234 e = DLLExportedGVs.end();
235 i != e; ++i) {
236 O << "\t.ascii \" -export:" << *i << ",data\"\n";
237 }
238
239 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
240 SwitchToDataSection(".section .drectve", 0);
241 }
242
243 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
244 e = DLLExportedFns.end();
245 i != e; ++i) {
246 O << "\t.ascii \" -export:" << *i << "\"\n";
247 }
248
Jim Laskeyea348582006-07-27 02:05:13 +0000249 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000250 SwitchToDataSection("", 0);
Nate Begeman73213f62005-07-12 01:37:28 +0000251
Nate Begeman72b286b2005-07-08 00:23:26 +0000252 // Output stubs for dynamically-linked functions
253 unsigned j = 1;
254 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000255 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000256 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
257 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000258 O << "L" << *i << "$stub:\n";
259 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000260 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000261 }
262
263 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000264
Evan Cheng2338c5c2006-02-07 08:38:37 +0000265 // Output stubs for external and common global variables.
266 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000267 SwitchToDataSection(
268 ".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000269 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
270 i != e; ++i) {
271 O << "L" << *i << "$non_lazy_ptr:\n";
272 O << "\t.indirect_symbol " << *i << "\n";
273 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000274 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000275
Evan Chengd5948812006-03-07 02:23:26 +0000276 // Emit initial debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000277 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000278
279 // Funny Darwin hack: This flag tells the linker that no global symbols
280 // contain code that falls through to other global symbols (e.g. the obvious
281 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000282 // linker can safely perform dead code stripping. Since LLVM never
283 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000284 O << "\t.subsections_via_symbols\n";
285 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000286
Chris Lattnerac5701c2004-10-04 07:24:48 +0000287 AsmPrinter::doFinalization(M);
288 return false; // success
289}
290
Chris Lattnerac5701c2004-10-04 07:24:48 +0000291/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
292/// for a MachineFunction to the given output stream, using the given target
293/// machine description.
294///
Evan Chengc4c62572006-03-13 23:20:37 +0000295FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000296 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000297 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000298
Jim Laskey05a059d2006-09-07 12:23:47 +0000299 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000300 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000301 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000302 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000303 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000304}