blob: 3a5889d73167a87bc64bd7a04f750dfc33f7a677 [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"
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
54 Size += ((TD->getTypeSize(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) ||
96 (FT->getNumParams() == 1 && FT->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) ||
102 (FT->getNumParams() == 1 && FT->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
Reid Spencer5dc81f62005-01-23 03:52:14 +0000123 return AsmPrinter::doInitialization(M);
124}
125
Chris Lattnerac5701c2004-10-04 07:24:48 +0000126bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000127 // Note: this code is not shared by the Intel printer as it is too different
128 // from how MASM does things. When making changes here don't forget to look
129 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000130 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000131
Chris Lattnerac5701c2004-10-04 07:24:48 +0000132 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000133 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
134 I != E; ++I) {
Rafael Espindola3e69a7e2006-12-09 23:14:08 +0000135 if (!I->hasInitializer())
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000136 continue; // External global require no code
Chris Lattner9787c642005-11-21 22:48:18 +0000137
Chris Lattnerd1239b72005-12-13 06:32:50 +0000138 // Check to see if this is a special global used by LLVM, if so, emit it.
Evan Chengb267ca12007-01-30 08:04:53 +0000139 if (EmitSpecialLLVMGlobal(I)) {
140 if (Subtarget->isTargetDarwin() &&
141 TM.getRelocationModel() == Reloc::Static) {
142 if (I->getName() == "llvm.global_ctors")
143 O << ".reference .constructors_used\n";
144 else if (I->getName() == "llvm.global_dtors")
145 O << ".reference .destructors_used\n";
146 }
Chris Lattnerd1239b72005-12-13 06:32:50 +0000147 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000148 }
Chris Lattnera046e0d2005-12-13 04:53:51 +0000149
Chris Lattner9787c642005-11-21 22:48:18 +0000150 std::string name = Mang->getValueName(I);
151 Constant *C = I->getInitializer();
Evan Chengf0b5d562007-03-08 01:07:07 +0000152 const Type *Type = C->getType();
153 unsigned Size = TD->getTypeSize(Type);
Devang Patelf9c197e2006-10-24 20:32:14 +0000154 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000155
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000156 if (I->hasHiddenVisibility()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000157 if (const char *Directive = TAI->getHiddenDirective())
158 O << Directive << name << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000159 } else if (I->hasProtectedVisibility()) {
160 if (const char *Directive = TAI->getProtectedDirective())
161 O << Directive << name << "\n";
162 }
163
Chris Lattner8e13e902007-01-17 17:44:25 +0000164 if (Subtarget->isTargetELF())
165 O << "\t.type " << name << ",@object\n";
166
167 if (C->isNullValue()) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000168 if (I->hasExternalLinkage()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000169 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga0ea0532006-02-23 02:43:52 +0000170 O << "\t.globl\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000171 O << Directive << "__DATA__, __common, " << name << ", "
Bill Wendling39e9c092007-01-18 02:30:19 +0000172 << Size << ", " << Align << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000173 continue;
174 }
175 }
176
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000177 if (!I->hasSection() && !I->isThreadLocal() &&
Chris Lattner8e13e902007-01-17 17:44:25 +0000178 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
179 I->hasLinkOnceLinkage())) {
180 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000181 if (!NoZerosInBSS && TAI->getBSSSection())
182 SwitchToDataSection(TAI->getBSSSection(), I);
183 else
184 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskey563321a2006-09-06 18:34:40 +0000185 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000186 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000187 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000188 if (Subtarget->isTargetDarwin())
Evan Cheng071b9d52007-01-18 01:49:58 +0000189 O << "," << Align;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000190 } else
Jim Laskey563321a2006-09-06 18:34:40 +0000191 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng17ef92e2006-02-15 01:56:23 +0000192 } else {
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000193 if (!Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000194 if (I->hasInternalLinkage())
195 O << "\t.local\t" << name << "\n";
196 }
Jim Laskey563321a2006-09-06 18:34:40 +0000197 O << TAI->getCOMMDirective() << name << "," << Size;
198 if (TAI->getCOMMDirectiveTakesAlignment())
199 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000200 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000201 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
202 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000203 }
Chris Lattner9787c642005-11-21 22:48:18 +0000204 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000205
206 switch (I->getLinkage()) {
207 case GlobalValue::LinkOnceLinkage:
208 case GlobalValue::WeakLinkage:
209 if (Subtarget->isTargetDarwin()) {
210 O << "\t.globl " << name << "\n"
211 << "\t.weak_definition " << name << "\n";
212 SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
213 } else if (Subtarget->isTargetCygMing()) {
214 std::string SectionName(".section\t.data$linkonce." +
215 name +
216 ",\"aw\"");
217 SwitchToDataSection(SectionName.c_str(), I);
218 O << "\t.globl " << name << "\n"
219 << "\t.linkonce same_size\n";
220 } else {
221 std::string SectionName("\t.section\t.llvm.linkonce.d." +
222 name +
223 ",\"aw\",@progbits");
224 SwitchToDataSection(SectionName.c_str(), I);
225 O << "\t.weak " << name << "\n";
226 }
227 break;
228 case GlobalValue::AppendingLinkage:
229 // FIXME: appending linkage variables should go into a section of
230 // their name or something. For now, just emit them as external.
231 case GlobalValue::DLLExportLinkage:
232 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
233 // FALL THROUGH
234 case GlobalValue::ExternalLinkage:
235 // If external or appending, declare as a global symbol
236 O << "\t.globl " << name << "\n";
237 // FALL THROUGH
238 case GlobalValue::InternalLinkage: {
239 if (I->isConstant()) {
240 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
241 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
242 SwitchToDataSection(TAI->getCStringSection(), I);
243 break;
244 }
245 }
246 // FIXME: special handling for ".ctors" & ".dtors" sections
247 if (I->hasSection() &&
248 (I->getSection() == ".ctors" ||
249 I->getSection() == ".dtors")) {
250 std::string SectionName = ".section " + I->getSection();
251
252 if (Subtarget->isTargetCygMing()) {
253 SectionName += ",\"aw\"";
254 } else {
255 assert(!Subtarget->isTargetDarwin());
256 SectionName += ",\"aw\",@progbits";
257 }
258
259 SwitchToDataSection(SectionName.c_str());
260 } else {
261 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000262 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
263 TAI->getBSSSection(), I);
Evan Chengf0b5d562007-03-08 01:07:07 +0000264 else if (!I->isConstant())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000265 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
266 TAI->getDataSection(), I);
267 else if (I->isThreadLocal())
268 SwitchToDataSection(TAI->getTLSDataSection());
Evan Chengf0b5d562007-03-08 01:07:07 +0000269 else {
270 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000271 bool HasReloc = C->ContainsRelocations();
272 if (HasReloc &&
273 Subtarget->isTargetDarwin() &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000274 TM.getRelocationModel() != Reloc::Static)
275 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000276 else if (!HasReloc && Size == 4 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000277 TAI->getFourByteConstantSection())
278 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000279 else if (!HasReloc && Size == 8 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000280 TAI->getEightByteConstantSection())
281 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000282 else if (!HasReloc && Size == 16 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000283 TAI->getSixteenByteConstantSection())
284 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
285 else if (TAI->getReadOnlySection())
286 SwitchToDataSection(TAI->getReadOnlySection(), I);
287 else
288 SwitchToDataSection(TAI->getDataSection(), I);
289 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000290 }
291
292 break;
293 }
294 default:
295 assert(0 && "Unknown linkage type!");
296 }
297
298 EmitAlignment(Align, I);
299 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
300 << "\n";
301 if (TAI->hasDotTypeDotSizeDirective())
302 O << "\t.size " << name << ", " << Size << "\n";
303 // If the initializer is a extern weak symbol, remember to emit the weak
304 // reference!
305 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
306 if (GV->hasExternalWeakLinkage())
307 ExtWeakSymbols.insert(GV);
308
309 EmitGlobalConstant(C);
310 O << '\n';
Chris Lattnera35a8e82005-11-21 22:39:40 +0000311 }
312
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000313 // Output linker support code for dllexported globals
314 if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000315 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000316 }
317
318 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
319 e = DLLExportedGVs.end();
320 i != e; ++i) {
321 O << "\t.ascii \" -export:" << *i << ",data\"\n";
322 }
323
324 if (DLLExportedFns.begin() != DLLExportedFns.end()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000325 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000326 }
327
328 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
329 e = DLLExportedFns.end();
330 i != e; ++i) {
331 O << "\t.ascii \" -export:" << *i << "\"\n";
332 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000333
Jim Laskeyea348582006-07-27 02:05:13 +0000334 if (Subtarget->isTargetDarwin()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000335 SwitchToDataSection("");
Nate Begeman73213f62005-07-12 01:37:28 +0000336
Nate Begeman72b286b2005-07-08 00:23:26 +0000337 // Output stubs for dynamically-linked functions
338 unsigned j = 1;
339 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000340 i != e; ++i, ++j) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000341 SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
342 "self_modifying_code+pure_instructions,5", 0);
Nate Begeman72b286b2005-07-08 00:23:26 +0000343 O << "L" << *i << "$stub:\n";
344 O << "\t.indirect_symbol " << *i << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000345 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000346 }
347
348 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000349
Evan Cheng2338c5c2006-02-07 08:38:37 +0000350 // Output stubs for external and common global variables.
351 if (GVStubs.begin() != GVStubs.end())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000352 SwitchToDataSection(
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000353 ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000354 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
355 i != e; ++i) {
356 O << "L" << *i << "$non_lazy_ptr:\n";
357 O << "\t.indirect_symbol " << *i << "\n";
358 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000359 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000360
Reid Spencer02b85112006-10-30 22:32:30 +0000361 // Emit final debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000362 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000363
364 // Funny Darwin hack: This flag tells the linker that no global symbols
365 // contain code that falls through to other global symbols (e.g. the obvious
366 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000367 // linker can safely perform dead code stripping. Since LLVM never
368 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000369 O << "\t.subsections_via_symbols\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000370 } else if (Subtarget->isTargetCygMing()) {
371 // Emit type information for external functions
372 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
373 i != e; ++i) {
374 O << "\t.def\t " << *i
375 << ";\t.scl\t" << COFF::C_EXT
376 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
377 << ";\t.endef\n";
378 }
379
380 // Emit final debug information.
381 DW.EndModule();
382 } else if (Subtarget->isTargetELF()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000383 // Emit final debug information.
384 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000385 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000386
Chris Lattnerac5701c2004-10-04 07:24:48 +0000387 AsmPrinter::doFinalization(M);
388 return false; // success
389}
390
Chris Lattnerac5701c2004-10-04 07:24:48 +0000391/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
392/// for a MachineFunction to the given output stream, using the given target
393/// machine description.
394///
Evan Chengc4c62572006-03-13 23:20:37 +0000395FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000396 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000397 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000398
Jim Laskey05a059d2006-09-07 12:23:47 +0000399 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000400 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000401 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000402 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000403 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000404}