blob: 9f3f76956dd41e357a716ca3a075d5bcfa5a7af2 [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"
Anton Korobeynikovea74c7e2008-01-20 14:00:07 +000028#include "llvm/ParameterAttributes.h"
Chris Lattnerc41cc832005-11-21 06:46:22 +000029#include "llvm/Type.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000030#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000031#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000032#include "llvm/Target/TargetAsmInfo.h"
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerd15dff22007-04-17 17:21:52 +000036static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
37 const TargetData *TD) {
38 X86MachineFunctionInfo Info;
Chris Lattnere87e1152006-09-26 03:57:53 +000039 uint64_t Size = 0;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000040
41 switch (F->getCallingConv()) {
Chris Lattnere87e1152006-09-26 03:57:53 +000042 case CallingConv::X86_StdCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000043 Info.setDecorationStyle(StdCall);
44 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000045 case CallingConv::X86_FastCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000046 Info.setDecorationStyle(FastCall);
47 break;
Chris Lattnere87e1152006-09-26 03:57:53 +000048 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +000049 return Info;
50 }
51
Anton Korobeynikovea74c7e2008-01-20 14:00:07 +000052 unsigned argNum = 1;
Chris Lattnere87e1152006-09-26 03:57:53 +000053 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
Anton Korobeynikovea74c7e2008-01-20 14:00:07 +000054 AI != AE; ++AI, ++argNum) {
55 const Type* Ty = AI->getType();
56
57 // 'Dereference' type in case of byval parameter attribute
58 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
59 Ty = cast<PointerType>(Ty)->getElementType();
60
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +000061 // Size should be aligned to DWORD boundary
Anton Korobeynikovea74c7e2008-01-20 14:00:07 +000062 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
63 }
64
Anton Korobeynikovf8248682006-09-20 22:03:51 +000065 // We're not supporting tooooo huge arguments :)
Chris Lattnere87e1152006-09-26 03:57:53 +000066 Info.setBytesToPopOnReturn((unsigned int)Size);
Anton Korobeynikovf8248682006-09-20 22:03:51 +000067 return Info;
68}
69
70
Chris Lattnere87e1152006-09-26 03:57:53 +000071/// decorateName - Query FunctionInfoMap and use this information for various
72/// name decoration.
73void X86SharedAsmPrinter::decorateName(std::string &Name,
74 const GlobalValue *GV) {
75 const Function *F = dyn_cast<Function>(GV);
76 if (!F) return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000077
78 // We don't want to decorate non-stdcall or non-fastcall functions right now
Chris Lattnere87e1152006-09-26 03:57:53 +000079 unsigned CC = F->getCallingConv();
80 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
Anton Korobeynikovf8248682006-09-20 22:03:51 +000081 return;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000082
83 // Decorate names only when we're targeting Cygwin/Mingw32 targets
84 if (!Subtarget->isTargetCygMing())
85 return;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000086
87 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
88
Chris Lattnerd15dff22007-04-17 17:21:52 +000089 const X86MachineFunctionInfo *Info;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000090 if (info_item == FunctionInfoMap.end()) {
91 // Calculate apropriate function info and populate map
92 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
93 Info = &FunctionInfoMap[F];
94 } else {
Chris Lattnere87e1152006-09-26 03:57:53 +000095 Info = &info_item->second;
Anton Korobeynikovf8248682006-09-20 22:03:51 +000096 }
Anton Korobeynikovad7baee2007-03-31 13:11:52 +000097
98 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000099 switch (Info->getDecorationStyle()) {
Chris Lattnere87e1152006-09-26 03:57:53 +0000100 case None:
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000101 break;
Chris Lattnere87e1152006-09-26 03:57:53 +0000102 case StdCall:
Anton Korobeynikovad7baee2007-03-31 13:11:52 +0000103 // "Pure" variadic functions do not receive @0 suffix.
104 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
Devang Patel41e23972008-03-03 21:46:28 +0000105 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000106 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000107 break;
Chris Lattnere87e1152006-09-26 03:57:53 +0000108 case FastCall:
Anton Korobeynikovad7baee2007-03-31 13:11:52 +0000109 // "Pure" variadic functions do not receive @0 suffix.
110 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
Devang Patel41e23972008-03-03 21:46:28 +0000111 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000112 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
Chris Lattnere87e1152006-09-26 03:57:53 +0000113
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000114 if (Name[0] == '_') {
115 Name[0] = '@';
116 } else {
117 Name = '@' + Name;
118 }
119 break;
Chris Lattnere87e1152006-09-26 03:57:53 +0000120 default:
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000121 assert(0 && "Unsupported DecorationStyle");
122 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000123}
124
Jim Laskey563321a2006-09-06 18:34:40 +0000125/// doInitialization
Evan Cheng25ab6902006-09-08 06:48:29 +0000126bool X86SharedAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000127 if (TAI->doesSupportDebugInformation()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000128 // Emit initial debug information.
129 DW.BeginModule(&M);
Evan Chengd5948812006-03-07 02:23:26 +0000130 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000131
Dan Gohmanb8275a32007-07-25 19:33:14 +0000132 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000133
134 // Darwin wants symbols to be quoted if they have complex names.
135 if (Subtarget->isTargetDarwin())
136 Mang->setUseQuotes(true);
137
Dan Gohmanb8275a32007-07-25 19:33:14 +0000138 return Result;
Reid Spencer5dc81f62005-01-23 03:52:14 +0000139}
140
Chris Lattnerec321b42008-02-15 19:04:54 +0000141/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Chris Lattner3502d0d2008-02-15 18:56:05 +0000142/// Don't print things like \n or \0.
Chris Lattnerec321b42008-02-15 19:04:54 +0000143static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
Chris Lattner3502d0d2008-02-15 18:56:05 +0000144 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
145 Name != E; ++Name)
146 if (isprint(*Name))
147 OS << *Name;
148}
149
Chris Lattnerac5701c2004-10-04 07:24:48 +0000150bool X86SharedAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000151 // Note: this code is not shared by the Intel printer as it is too different
152 // from how MASM does things. When making changes here don't forget to look
153 // at X86IntelAsmPrinter::doFinalization().
Owen Andersona69571c2006-05-03 01:29:57 +0000154 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000155
Chris Lattnerac5701c2004-10-04 07:24:48 +0000156 // Print out module-level global variables here.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000157 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
158 I != E; ++I) {
Rafael Espindola3e69a7e2006-12-09 23:14:08 +0000159 if (!I->hasInitializer())
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000160 continue; // External global require no code
Chris Lattner9787c642005-11-21 22:48:18 +0000161
Chris Lattnerd1239b72005-12-13 06:32:50 +0000162 // Check to see if this is a special global used by LLVM, if so, emit it.
Evan Chengb267ca12007-01-30 08:04:53 +0000163 if (EmitSpecialLLVMGlobal(I)) {
164 if (Subtarget->isTargetDarwin() &&
165 TM.getRelocationModel() == Reloc::Static) {
166 if (I->getName() == "llvm.global_ctors")
167 O << ".reference .constructors_used\n";
168 else if (I->getName() == "llvm.global_dtors")
169 O << ".reference .destructors_used\n";
170 }
Chris Lattnerd1239b72005-12-13 06:32:50 +0000171 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000172 }
Chris Lattnera046e0d2005-12-13 04:53:51 +0000173
Chris Lattner9787c642005-11-21 22:48:18 +0000174 std::string name = Mang->getValueName(I);
175 Constant *C = I->getInitializer();
Evan Chengf0b5d562007-03-08 01:07:07 +0000176 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000177 unsigned Size = TD->getABITypeSize(Type);
Devang Patelf9c197e2006-10-24 20:32:14 +0000178 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohen00b168892005-07-27 06:12:32 +0000179
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000180 if (I->hasHiddenVisibility()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000181 if (const char *Directive = TAI->getHiddenDirective())
182 O << Directive << name << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000183 } else if (I->hasProtectedVisibility()) {
184 if (const char *Directive = TAI->getProtectedDirective())
185 O << Directive << name << "\n";
186 }
187
Chris Lattner8e13e902007-01-17 17:44:25 +0000188 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000189 O << "\t.type\t" << name << ",@object\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000190
Evan Cheng76a40232007-09-21 00:41:19 +0000191 if (C->isNullValue() && !I->hasSection()) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000192 if (I->hasExternalLinkage()) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000193 if (const char *Directive = TAI->getZeroFillDirective()) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000194 O << "\t.globl " << name << "\n";
Dale Johannesen3006c392008-02-12 23:35:09 +0000195 O << Directive << "__DATA, __common, " << name << ", "
Bill Wendling39e9c092007-01-18 02:30:19 +0000196 << Size << ", " << Align << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000197 continue;
198 }
199 }
200
Evan Cheng76a40232007-09-21 00:41:19 +0000201 if (!I->isThreadLocal() &&
Dale Johannesend15d0862008-01-17 23:04:07 +0000202 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000203 I->hasLinkOnceLinkage() || I->hasCommonLinkage())) {
Chris Lattner8e13e902007-01-17 17:44:25 +0000204 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000205 if (!NoZerosInBSS && TAI->getBSSSection())
206 SwitchToDataSection(TAI->getBSSSection(), I);
207 else
208 SwitchToDataSection(TAI->getDataSection(), I);
Jim Laskey563321a2006-09-06 18:34:40 +0000209 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng17ef92e2006-02-15 01:56:23 +0000210 if (I->hasInternalLinkage()) {
Jim Laskey563321a2006-09-06 18:34:40 +0000211 O << TAI->getLCOMMDirective() << name << "," << Size;
Jim Laskeyea348582006-07-27 02:05:13 +0000212 if (Subtarget->isTargetDarwin())
Evan Cheng071b9d52007-01-18 01:49:58 +0000213 O << "," << Align;
Dale Johannesenf88c81e2008-05-16 00:52:06 +0000214 } else if (Subtarget->isTargetDarwin() && !I->hasCommonLinkage()) {
215 O << "\t.globl " << name << "\n"
216 << TAI->getWeakDefDirective() << name << "\n";
217 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
218 EmitAlignment(Align, I);
219 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
220 PrintUnmangledNameSafely(I, O);
221 O << "\n";
222 EmitGlobalConstant(C);
223 continue;
Chris Lattner7ad92d82008-01-02 19:44:55 +0000224 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000225 O << TAI->getCOMMDirective() << name << "," << Size;
Chris Lattner7ad92d82008-01-02 19:44:55 +0000226
227 // Leopard and above support aligned common symbols.
228 if (Subtarget->getDarwinVers() >= 9)
229 O << "," << Align;
230 }
Evan Cheng17ef92e2006-02-15 01:56:23 +0000231 } else {
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000232 if (!Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000233 if (I->hasInternalLinkage())
234 O << "\t.local\t" << name << "\n";
235 }
Jim Laskey563321a2006-09-06 18:34:40 +0000236 O << TAI->getCOMMDirective() << name << "," << Size;
237 if (TAI->getCOMMDirectiveTakesAlignment())
238 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng17ef92e2006-02-15 01:56:23 +0000239 }
Chris Lattner3502d0d2008-02-15 18:56:05 +0000240 O << "\t\t" << TAI->getCommentString() << " ";
Chris Lattnerec321b42008-02-15 19:04:54 +0000241 PrintUnmangledNameSafely(I, O);
Chris Lattner3502d0d2008-02-15 18:56:05 +0000242 O << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000243 continue;
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000244 }
Chris Lattner9787c642005-11-21 22:48:18 +0000245 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000246
247 switch (I->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000248 case GlobalValue::CommonLinkage:
Chris Lattner8e13e902007-01-17 17:44:25 +0000249 case GlobalValue::LinkOnceLinkage:
250 case GlobalValue::WeakLinkage:
251 if (Subtarget->isTargetDarwin()) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000252 O << "\t.globl " << name << "\n"
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000253 << TAI->getWeakDefDirective() << name << "\n";
Dale Johannesenbbcb34c2008-05-23 00:16:59 +0000254 if (!I->isConstant())
255 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
256 else {
257 const ArrayType *AT = dyn_cast<ArrayType>(Type);
258 if (AT && AT->getElementType()==Type::Int8Ty)
259 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", I);
260 else
261 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
262 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000263 } else if (Subtarget->isTargetCygMing()) {
264 std::string SectionName(".section\t.data$linkonce." +
265 name +
266 ",\"aw\"");
267 SwitchToDataSection(SectionName.c_str(), I);
Dan Gohman52c02532007-10-05 15:58:41 +0000268 O << "\t.globl\t" << name << "\n"
Chris Lattner8e13e902007-01-17 17:44:25 +0000269 << "\t.linkonce same_size\n";
270 } else {
271 std::string SectionName("\t.section\t.llvm.linkonce.d." +
272 name +
273 ",\"aw\",@progbits");
274 SwitchToDataSection(SectionName.c_str(), I);
Dan Gohman825811d2007-07-30 15:08:02 +0000275 O << "\t.weak\t" << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000276 }
277 break;
Chris Lattner8e13e902007-01-17 17:44:25 +0000278 case GlobalValue::DLLExportLinkage:
279 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
280 // FALL THROUGH
Chris Lattnera45d9a12007-08-13 18:42:37 +0000281 case GlobalValue::AppendingLinkage:
282 // FIXME: appending linkage variables should go into a section of
283 // their name or something. For now, just emit them as external.
Chris Lattner8e13e902007-01-17 17:44:25 +0000284 case GlobalValue::ExternalLinkage:
285 // If external or appending, declare as a global symbol
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000286 O << "\t.globl " << name << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000287 // FALL THROUGH
288 case GlobalValue::InternalLinkage: {
289 if (I->isConstant()) {
290 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
291 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
292 SwitchToDataSection(TAI->getCStringSection(), I);
293 break;
294 }
295 }
296 // FIXME: special handling for ".ctors" & ".dtors" sections
297 if (I->hasSection() &&
298 (I->getSection() == ".ctors" ||
299 I->getSection() == ".dtors")) {
300 std::string SectionName = ".section " + I->getSection();
301
302 if (Subtarget->isTargetCygMing()) {
303 SectionName += ",\"aw\"";
304 } else {
305 assert(!Subtarget->isTargetDarwin());
306 SectionName += ",\"aw\",@progbits";
307 }
Dale Johannesen25edeb32008-01-23 00:58:14 +0000308 SwitchToDataSection(SectionName.c_str());
309 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
310 // Honor all section names on Darwin; ObjC uses this
311 std::string SectionName = ".section " + I->getSection();
Chris Lattner8e13e902007-01-17 17:44:25 +0000312 SwitchToDataSection(SectionName.c_str());
313 } else {
314 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000315 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
316 TAI->getBSSSection(), I);
Evan Chengf0b5d562007-03-08 01:07:07 +0000317 else if (!I->isConstant())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000318 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
319 TAI->getDataSection(), I);
320 else if (I->isThreadLocal())
321 SwitchToDataSection(TAI->getTLSDataSection());
Evan Chengf0b5d562007-03-08 01:07:07 +0000322 else {
323 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000324 bool HasReloc = C->ContainsRelocations();
325 if (HasReloc &&
326 Subtarget->isTargetDarwin() &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000327 TM.getRelocationModel() != Reloc::Static)
328 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000329 else if (!HasReloc && Size == 4 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000330 TAI->getFourByteConstantSection())
331 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000332 else if (!HasReloc && Size == 8 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000333 TAI->getEightByteConstantSection())
334 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000335 else if (!HasReloc && Size == 16 &&
Evan Chengf0b5d562007-03-08 01:07:07 +0000336 TAI->getSixteenByteConstantSection())
337 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
338 else if (TAI->getReadOnlySection())
339 SwitchToDataSection(TAI->getReadOnlySection(), I);
340 else
341 SwitchToDataSection(TAI->getDataSection(), I);
342 }
Chris Lattner8e13e902007-01-17 17:44:25 +0000343 }
344
345 break;
346 }
347 default:
348 assert(0 && "Unknown linkage type!");
349 }
350
351 EmitAlignment(Align, I);
Chris Lattner3502d0d2008-02-15 18:56:05 +0000352 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
Chris Lattnerec321b42008-02-15 19:04:54 +0000353 PrintUnmangledNameSafely(I, O);
Chris Lattner3502d0d2008-02-15 18:56:05 +0000354 O << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000355 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman825811d2007-07-30 15:08:02 +0000356 O << "\t.size\t" << name << ", " << Size << "\n";
Chris Lattner8e13e902007-01-17 17:44:25 +0000357 // If the initializer is a extern weak symbol, remember to emit the weak
358 // reference!
359 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
360 if (GV->hasExternalWeakLinkage())
361 ExtWeakSymbols.insert(GV);
362
363 EmitGlobalConstant(C);
Chris Lattnera35a8e82005-11-21 22:39:40 +0000364 }
365
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000366 // Output linker support code for dllexported globals
Dan Gohmancb406c22007-10-03 19:26:29 +0000367 if (!DLLExportedGVs.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000368 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000369 }
370
371 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
372 e = DLLExportedGVs.end();
373 i != e; ++i) {
374 O << "\t.ascii \" -export:" << *i << ",data\"\n";
375 }
376
Dan Gohmancb406c22007-10-03 19:26:29 +0000377 if (!DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000378 SwitchToDataSection(".section .drectve");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000379 }
380
381 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
382 e = DLLExportedFns.end();
383 i != e; ++i) {
384 O << "\t.ascii \" -export:" << *i << "\"\n";
385 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000386
Jim Laskeyea348582006-07-27 02:05:13 +0000387 if (Subtarget->isTargetDarwin()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000388 SwitchToDataSection("");
Nate Begeman73213f62005-07-12 01:37:28 +0000389
Nate Begeman72b286b2005-07-08 00:23:26 +0000390 // Output stubs for dynamically-linked functions
391 unsigned j = 1;
392 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
Chris Lattnerb12c9fa2005-07-11 06:25:47 +0000393 i != e; ++i, ++j) {
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000394 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
Chris Lattner4632d7a2006-05-09 04:59:56 +0000395 "self_modifying_code+pure_instructions,5", 0);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000396 std::string p = *i;
397 printSuffixedName(p, "$stub");
398 O << ":\n";
399 O << "\t.indirect_symbol " << p << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000400 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000401 }
402
403 O << "\n";
Jeff Cohen00b168892005-07-27 06:12:32 +0000404
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000405 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Bill Wendlingd60da492007-09-11 08:27:17 +0000406 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000407 // Only referenced functions get into the Personalities list.
Bill Wendlingd60da492007-09-11 08:27:17 +0000408 const std::vector<Function *>& Personalities = MMI->getPersonalities();
409
410 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
411 E = Personalities.end(); I != E; ++I)
412 if (*I) GVStubs.insert("_" + (*I)->getName());
413 }
414
Evan Cheng2338c5c2006-02-07 08:38:37 +0000415 // Output stubs for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +0000416 if (!GVStubs.empty())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000417 SwitchToDataSection(
Dale Johannesenc7406ae2008-01-11 00:54:37 +0000418 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000419 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
420 i != e; ++i) {
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000421 std::string p = *i;
422 printSuffixedName(p, "$non_lazy_ptr");
423 O << ":\n";
424 O << "\t.indirect_symbol " << p << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000425 O << "\t.long\t0\n";
Nate Begeman72b286b2005-07-08 00:23:26 +0000426 }
Chris Lattnerac5701c2004-10-04 07:24:48 +0000427
Reid Spencer02b85112006-10-30 22:32:30 +0000428 // Emit final debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000429 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000430
431 // Funny Darwin hack: This flag tells the linker that no global symbols
432 // contain code that falls through to other global symbols (e.g. the obvious
433 // implementation of multiple entry points). If this doesn't occur, the
Jim Laskey99db0442006-03-23 18:09:44 +0000434 // linker can safely perform dead code stripping. Since LLVM never
435 // generates code that does this, it is always safe to set.
Evan Chengd5948812006-03-07 02:23:26 +0000436 O << "\t.subsections_via_symbols\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000437 } else if (Subtarget->isTargetCygMing()) {
438 // Emit type information for external functions
439 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
440 i != e; ++i) {
441 O << "\t.def\t " << *i
442 << ";\t.scl\t" << COFF::C_EXT
443 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
444 << ";\t.endef\n";
445 }
446
447 // Emit final debug information.
448 DW.EndModule();
449 } else if (Subtarget->isTargetELF()) {
Reid Spencer02b85112006-10-30 22:32:30 +0000450 // Emit final debug information.
451 DW.EndModule();
Evan Chengd5948812006-03-07 02:23:26 +0000452 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000453
Dan Gohmanb8275a32007-07-25 19:33:14 +0000454 return AsmPrinter::doFinalization(M);
Chris Lattnerac5701c2004-10-04 07:24:48 +0000455}
456
Chris Lattnerac5701c2004-10-04 07:24:48 +0000457/// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
458/// for a MachineFunction to the given output stream, using the given target
459/// machine description.
460///
Evan Chengc4c62572006-03-13 23:20:37 +0000461FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,
Jim Laskey563321a2006-09-06 18:34:40 +0000462 X86TargetMachine &tm) {
Jim Laskey05a059d2006-09-07 12:23:47 +0000463 const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +0000464
Jim Laskey05a059d2006-09-07 12:23:47 +0000465 if (Subtarget->isFlavorIntel()) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000466 return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo());
Jim Laskey05a059d2006-09-07 12:23:47 +0000467 } else {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000468 return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattnerac5701c2004-10-04 07:24:48 +0000469 }
Brian Gaeke9e474c42003-06-19 19:32:32 +0000470}