blob: 338f6cdb95fa43d52e34da2f3aa583c690b0857d [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Anton Korobeynikovad7baee2007-03-31 13:11:52 +000027#include "llvm/DerivedTypes.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000028#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000029#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000030#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerd15dff22007-04-17 17:21:52 +000035static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
36 const TargetData *TD) {
37 X86MachineFunctionInfo Info;
Chris Lattnere87e1152006-09-26 03:57:53 +000038 uint64_t Size = 0;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000039
40 switch (F->getCallingConv()) {
Chris Lattnere87e1152006-09-26 03:57:53 +000041 case CallingConv::X86_StdCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000042 Info.setDecorationStyle(StdCall);
43 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000044 case CallingConv::X86_FastCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000045 Info.setDecorationStyle(FastCall);
46 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000047 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000048 return Info;
49 }
50
Chris Lattnere87e1152006-09-26 03:57:53 +000051 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
52 AI != AE; ++AI)
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +000053 // Size should be aligned to DWORD boundary
Duncan Sandsca0ed742007-11-05 00:04:43 +000054 Size += ((TD->getABITypeSize(AI->getType()) + 3)/4)*4;
Anton Korobeynikov54b1cc62006-10-14 20:53:35 +000055
Anton Korobeynikovf8248682006-09-20 22:03:51 +000056 // We're not supporting tooooo huge arguments :)
Chris Lattnere87e1152006-09-26 03:57:53 +000057 Info.setBytesToPopOnReturn((unsigned int)Size);
Anton Korobeynikovf8248682006-09-20 22:03:51 +000058 return Info;
59}
60
61
Chris Lattnere87e1152006-09-26 03:57:53 +000062/// decorateName - Query FunctionInfoMap and use this information for various
63/// name decoration.
64void X86SharedAsmPrinter::decorateName(std::string &Name,
65 const GlobalValue *GV) {
66 const Function *F = dyn_cast<Function>(GV);
67 if (!F) return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000068
69 // We don't want to decorate non-stdcall or non-fastcall functions right now
Chris Lattnere87e1152006-09-26 03:57:53 +000070 unsigned CC = F->getCallingConv();
71 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
Anton Korobeynikovf8248682006-09-20 22:03:51 +000072 return;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000073
74 // Decorate names only when we're targeting Cygwin/Mingw32 targets
75 if (!Subtarget->isTargetCygMing())
76 return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000077
78 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
79
Chris Lattnerd15dff22007-04-17 17:21:52 +000080 const X86MachineFunctionInfo *Info;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000081 if (info_item == FunctionInfoMap.end()) {
82 // Calculate apropriate function info and populate map
83 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
84 Info = &FunctionInfoMap[F];
85 } else {
Chris Lattnere87e1152006-09-26 03:57:53 +000086 Info = &info_item->second;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000087 }
Anton Korobeynikovad7baee2007-03-31 13:11:52 +000088
89 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000090 switch (Info->getDecorationStyle()) {
Chris Lattnere87e1152006-09-26 03:57:53 +000091 case None:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000092 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000093 case StdCall:
Anton Korobeynikovad7baee2007-03-31 13:11:52 +000094 // "Pure" variadic functions do not receive @0 suffix.
95 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
Duncan Sandsdc024672007-11-27 13:23:08 +000096 (FT->getNumParams() == 1 && F->isStructReturn()))
Anton Korobeynikovf8248682006-09-20 22:03:51 +000097 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Anton Korobeynikovf8248682006-09-20 22:03:51 +000098 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000099 case FastCall:
Anton Korobeynikovad7baee2007-03-31 13:11:52 +0000100 // "Pure" variadic functions do not receive @0 suffix.
101 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
Duncan Sandsdc024672007-11-27 13:23:08 +0000102 (FT->getNumParams() == 1 && F->isStructReturn()))
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000103 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Chris Lattnere87e1152006-09-26 03:57:53 +0000104
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000105 if (Name[0] == '_') {
106 Name[0] = '@';
107 } else {
108 Name = '@' + Name;
109 }
110 break;
Chris Lattnere87e1152006-09-26 03:57:53 +0000111 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000112 assert(0 && "Unsupported DecorationStyle");
113 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000114}
115
Jim Laskey563321a2006-09-06 18:34:40 +0000116/// doInitialization
Evan Cheng25ab6902006-09-08 06:48:29 +0000117bool X86SharedAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000118 if (TAI->doesSupportDebugInformation()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000119 // Emit initial debug information.
120 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000121 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000122
Dan Gohmanb8275a32007-07-25 19:33:14 +0000123 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000124
125 // Darwin wants symbols to be quoted if they have complex names.
126 if (Subtarget->isTargetDarwin())
127 Mang->setUseQuotes(true);
128
Dan Gohmanb8275a32007-07-25 19:33:14 +0000129 return Result;
Reid Spencer5dc81f62005-01-23 03:52:14 +0000130}
131
Chris Lattnerac5701c2004-10-04 07:24:48 +0000132bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000133 // Note: this code is not shared by the Intel printer as it is too different
134 // from how MASM does things. When making changes here don't forget to look
135 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000136 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000137
Chris Lattnerac5701c2004-10-04 07:24:48 +0000138 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000139 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
140 I != E; ++I) {
Rafael Espindola3e69a7e2006-12-09 23:14:08 +0000141 if (!I->hasInitializer())
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000142 continue; // External global require no code
Chris Lattner9787c642005-11-21 22:48:18 +0000143
Chris Lattnerd1239b72005-12-13 06:32:50 +0000144 // Check to see if this is a special global used by LLVM, if so, emit it.
Evan Chengb267ca12007-01-30 08:04:53 +0000145 if (EmitSpecialLLVMGlobal(I)) {
146 if (Subtarget->isTargetDarwin() &&
147 TM.getRelocationModel() == Reloc::Static) {
148 if (I->getName() == "llvm.global_ctors")
149 O << ".reference .constructors_used\n";
150 else if (I->getName() == "llvm.global_dtors")
151 O << ".reference .destructors_used\n";
152 }
Chris Lattnerd1239b72005-12-13 06:32:50 +0000153 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000154 }
Chris Lattnera046e0d2005-12-13 04:53:51 +0000155
Chris Lattner9787c642005-11-21 22:48:18 +0000156 std::string name = Mang->getValueName(I);
157 Constant *C = I->getInitializer();
Evan Chengf0b5d562007-03-08 01:07:07 +0000158 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000159 unsigned Size = TD->getABITypeSize(Type);
Devang Patelf9c197e2006-10-24 20:32:14 +0000160 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000161
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000162 if (I->hasHiddenVisibility()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000163 if (const char *Directive = TAI->getHiddenDirective())
164 O << Directive << name << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000165 } else if (I->hasProtectedVisibility()) {
166 if (const char *Directive = TAI->getProtectedDirective())
167 O << Directive << name << "\n";
168 }
169
Chris Lattner8e13e902007-01-17 17:44:25 +0000170 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000171 O << "\t.type\t" << name << ",@object\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000172
Evan Cheng76a40232007-09-21 00:41:19 +0000173 if (C->isNullValue() && !I->hasSection()) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000174 if (I->hasExternalLinkage()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000175 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000176 O << "\t.globl\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000177 O << Directive << "__DATA__, __common, " << name << ", "
Bill Wendling39e9c092007-01-18 02:30:19 +0000178 << Size << ", " << Align << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000179 continue;
180 }
181 }
182
Evan Cheng76a40232007-09-21 00:41:19 +0000183 if (!I->isThreadLocal() &&
Chris Lattner8e13e902007-01-17 17:44:25 +0000184 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
185 I->hasLinkOnceLinkage())) {
186 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000187 if (!NoZerosInBSS && TAI->getBSSSection())
188 SwitchToDataSection(TAI->getBSSSection(), I);
189 else
190 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskey563321a2006-09-06 18:34:40 +0000191 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000192 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000193 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000194 if (Subtarget->isTargetDarwin())
Evan Cheng071b9d52007-01-18 01:49:58 +0000195 O << "," << Align;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000196 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000197 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000198 } else {
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000199 if (!Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000200 if (I->hasInternalLinkage())
201 O << "\t.local\t" << name << "\n";
202 }
Jim Laskey563321a2006-09-06 18:34:40 +0000203 O << TAI->getCOMMDirective() << name << "," << Size;
204 if (TAI->getCOMMDirectiveTakesAlignment())
205 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000206 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000207 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
208 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000209 }
Chris Lattner9787c642005-11-21 22:48:18 +0000210 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000211
212 switch (I->getLinkage()) {
213 case GlobalValue::LinkOnceLinkage:
214 case GlobalValue::WeakLinkage:
215 if (Subtarget->isTargetDarwin()) {
Dan Gohman52c02532007-10-05 15:58:41 +0000216 O << "\t.globl\t" << name << "\n"
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000217 << TAI->getWeakDefDirective() << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000218 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
219 } else if (Subtarget->isTargetCygMing()) {
220 std::string SectionName(".section\t.data$linkonce." +
221 name +
222 ",\"aw\"");
223 SwitchToDataSection(SectionName.c_str(), I);
Dan Gohman52c02532007-10-05 15:58:41 +0000224 O << "\t.globl\t" << name << "\n"
Chris Lattner8e13e902007-01-17 17:44:25 +0000225 << "\t.linkonce same_size\n";
226 } else {
227 std::string SectionName("\t.section\t.llvm.linkonce.d." +
228 name +
229 ",\"aw\",@progbits");
230 SwitchToDataSection(SectionName.c_str(), I);
Dan Gohman825811d2007-07-30 15:08:02 +0000231 O << "\t.weak\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000232 }
233 break;
Chris Lattner8e13e902007-01-17 17:44:25 +0000234 case GlobalValue::DLLExportLinkage:
235 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
236 // FALL THROUGH
Chris Lattnera45d9a12007-08-13 18:42:37 +0000237 case GlobalValue::AppendingLinkage:
238 // FIXME: appending linkage variables should go into a section of
239 // their name or something. For now, just emit them as external.
Chris Lattner8e13e902007-01-17 17:44:25 +0000240 case GlobalValue::ExternalLinkage:
241 // If external or appending, declare as a global symbol
Dan Gohman52c02532007-10-05 15:58:41 +0000242 O << "\t.globl\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000243 // FALL THROUGH
244 case GlobalValue::InternalLinkage: {
245 if (I->isConstant()) {
246 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
247 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
248 SwitchToDataSection(TAI->getCStringSection(), I);
249 break;
250 }
251 }
252 // FIXME: special handling for ".ctors" & ".dtors" sections
253 if (I->hasSection() &&
254 (I->getSection() == ".ctors" ||
255 I->getSection() == ".dtors")) {
256 std::string SectionName = ".section " + I->getSection();
257
258 if (Subtarget->isTargetCygMing()) {
259 SectionName += ",\"aw\"";
260 } else {
261 assert(!Subtarget->isTargetDarwin());
262 SectionName += ",\"aw\",@progbits";
263 }
264
265 SwitchToDataSection(SectionName.c_str());
266 } else {
267 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000268 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
269 TAI->getBSSSection(), I);
Evan Chengf0b5d562007-03-08 01:07:07 +0000270 else if (!I->isConstant())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000271 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
272 TAI->getDataSection(), I);
273 else if (I->isThreadLocal())
274 SwitchToDataSection(TAI->getTLSDataSection());
Evan Chengf0b5d562007-03-08 01:07:07 +0000275 else {
276 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000277 bool HasReloc = C->ContainsRelocations();
278 if (HasReloc &&
279 Subtarget->isTargetDarwin() &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000280 TM.getRelocationModel() != Reloc::Static)
281 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000282 else if (!HasReloc && Size == 4 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000283 TAI->getFourByteConstantSection())
284 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000285 else if (!HasReloc && Size == 8 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000286 TAI->getEightByteConstantSection())
287 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000288 else if (!HasReloc && Size == 16 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000289 TAI->getSixteenByteConstantSection())
290 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
291 else if (TAI->getReadOnlySection())
292 SwitchToDataSection(TAI->getReadOnlySection(), I);
293 else
294 SwitchToDataSection(TAI->getDataSection(), I);
295 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000296 }
297
298 break;
299 }
300 default:
301 assert(0 && "Unknown linkage type!");
302 }
303
304 EmitAlignment(Align, I);
305 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
306 << "\n";
307 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman825811d2007-07-30 15:08:02 +0000308 O << "\t.size\t" << name << ", " << Size << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000309 // If the initializer is a extern weak symbol, remember to emit the weak
310 // reference!
311 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
312 if (GV->hasExternalWeakLinkage())
313 ExtWeakSymbols.insert(GV);
314
315 EmitGlobalConstant(C);
Chris Lattnera35a8e82005-11-21 22:39:40 +0000316 }
317
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000318 // Output linker support code for dllexported globals
Dan Gohmancb406c22007-10-03 19:26:29 +0000319 if (!DLLExportedGVs.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000320 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000321 }
322
323 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
324 e = DLLExportedGVs.end();
325 i != e; ++i) {
326 O << "\t.ascii \" -export:" << *i << ",data\"\n";
327 }
328
Dan Gohmancb406c22007-10-03 19:26:29 +0000329 if (!DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000330 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000331 }
332
333 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
334 e = DLLExportedFns.end();
335 i != e; ++i) {
336 O << "\t.ascii \" -export:" << *i << "\"\n";
337 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000338
Jim Laskeyea348582006-07-27 02:05:13 +0000339 if (Subtarget->isTargetDarwin()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000340 SwitchToDataSection("");
Nate Begeman73213f62005-07-12 01:37:28 +0000341
Nate Begeman72b286b2005-07-08 00:23:26 +0000342 // Output stubs for dynamically-linked functions
343 unsigned j = 1;
344 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000345 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000346 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
347 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000348 O << "L" << *i << "$stub:\n";
349 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000350 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000351 }
352
353 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000354
Bill Wendlingbd626b82007-09-16 19:21:08 +0000355 if (ExceptionHandling && TAI->doesSupportExceptionHandling() && MMI) {
Bill Wendlingd60da492007-09-11 08:27:17 +0000356 // Add the (possibly multiple) personalities to the set of global values.
357 const std::vector<Function *>& Personalities = MMI->getPersonalities();
358
359 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
360 E = Personalities.end(); I != E; ++I)
361 if (*I) GVStubs.insert("_" + (*I)->getName());
362 }
363
Evan Cheng2338c5c2006-02-07 08:38:37 +0000364 // Output stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +0000365 if (!GVStubs.empty())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000366 SwitchToDataSection(
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000367 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000368 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
369 i != e; ++i) {
370 O << "L" << *i << "$non_lazy_ptr:\n";
371 O << "\t.indirect_symbol " << *i << "\n";
372 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000373 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000374
Reid Spencer02b85112006-10-30 22:32:30 +0000375 // Emit final debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000376 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000377
378 // Funny Darwin hack: This flag tells the linker that no global symbols
379 // contain code that falls through to other global symbols (e.g. the obvious
380 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000381 // linker can safely perform dead code stripping. Since LLVM never
382 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000383 O << "\t.subsections_via_symbols\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000384 } else if (Subtarget->isTargetCygMing()) {
385 // Emit type information for external functions
386 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
387 i != e; ++i) {
388 O << "\t.def\t " << *i
389 << ";\t.scl\t" << COFF::C_EXT
390 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
391 << ";\t.endef\n";
392 }
393
394 // Emit final debug information.
395 DW.EndModule();
396 } else if (Subtarget->isTargetELF()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000397 // Emit final debug information.
398 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000399 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000400
Dan Gohmanb8275a32007-07-25 19:33:14 +0000401 return AsmPrinter::doFinalization(M);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000402}
403
Chris Lattnerac5701c2004-10-04 07:24:48 +0000404/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
405/// for a MachineFunction to the given output stream, using the given target
406/// machine description.
407///
Evan Chengc4c62572006-03-13 23:20:37 +0000408FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000409 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000410 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000411
Jim Laskey05a059d2006-09-07 12:23:47 +0000412 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000413 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000414 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000415 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000416 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000417}