blob: 847355c50d7267e3ad4838a1075570b34b99d566 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0729ccf2008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 std::string label;
42 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 else
47 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000074 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000075 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
Duncan Sandsd68f13b2009-01-12 20:38:59 +000078 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000079 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000149void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000152 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000154 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000155
Devang Patel93698d92008-10-01 23:18:38 +0000156 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000157 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000158 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000162 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000163 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 break;
165 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000168 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::LinkOnceLinkage:
171 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
175 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << "\n"
178 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 break;
183 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000184
185 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000188 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 else if (Subtarget->isTargetCygMing()) {
190 O << "\t.def\t " << CurrentFnName
191 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000192 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
194 << ";\t.endef\n";
195 }
196
197 O << CurrentFnName << ":\n";
198 // Add some workaround for linkonce linkage on Cygwin\MinGW
199 if (Subtarget->isTargetCygMing() &&
200 (F->getLinkage() == Function::LinkOnceLinkage ||
201 F->getLinkage() == Function::WeakLinkage))
202 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000203}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000205/// runOnMachineFunction - This uses the printMachineInstruction()
206/// method to print assembly for each instruction.
207///
208bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
209 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000210 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000211 unsigned CC = F->getCallingConv();
212
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000213 SetupMachineFunction(MF);
214 O << "\n\n";
215
216 // Populate function information map. Actually, We don't want to populate
217 // non-stdcall or non-fastcall functions' information right now.
218 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
219 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
220
221 // Print out constants referenced by the function
222 EmitConstantPool(MF.getConstantPool());
223
224 if (F->hasDLLExportLinkage())
225 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
226
227 // Print the 'header' of function
228 emitFunctionHeader(MF);
229
230 // Emit pre-function debug and/or EH information.
231 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000232 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000233
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000235 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
237 I != E; ++I) {
238 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000239 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000240 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 O << '\n';
242 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000243 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
244 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000246 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000247 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 printMachineInstruction(II);
249 }
250 }
251
Dale Johannesenf35771f2008-04-08 00:37:56 +0000252 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
253 // If the function is empty, then we need to emit *something*. Otherwise,
254 // the function's label might be associated with something that it wasn't
255 // meant to be associated with. We emit a noop in this situation.
256 // We are assuming inline asms are code.
257 O << "\tnop\n";
258 }
259
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000261 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000263 // Emit post-function debug information.
264 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000265 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
267 // Print out jump tables referenced by the function.
268 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000269
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000270 O.flush();
271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 // We didn't modify anything.
273 return false;
274}
275
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000276static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
278}
279
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000280static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
281 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
282 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
283}
284
285static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
287}
288
289void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
290 const char *Modifier, bool NotRIPRel) {
291 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 switch (MO.getType()) {
293 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000294 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 "Virtual registers should not make it this far!");
296 O << '%';
297 unsigned Reg = MO.getReg();
298 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000299 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
301 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
302 Reg = getX86SubSuperRegister(Reg, VT);
303 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000304 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 return;
306 }
307
308 case MachineOperand::MO_Immediate:
309 if (!Modifier ||
310 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
311 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000312 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 return;
314 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000315 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 return;
317 case MachineOperand::MO_JumpTableIndex: {
318 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
319 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000320 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000321 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
323 if (TM.getRelocationModel() == Reloc::PIC_) {
324 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000325 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
326 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 else if (Subtarget->isPICStyleGOT())
328 O << "@GOTOFF";
329 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
332 O << "(%rip)";
333 return;
334 }
335 case MachineOperand::MO_ConstantPoolIndex: {
336 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
337 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000338 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000339 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
341 if (TM.getRelocationModel() == Reloc::PIC_) {
342 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000343 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
344 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 else if (Subtarget->isPICStyleGOT())
346 O << "@GOTOFF";
347 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000348
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000349 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350
351 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
352 O << "(%rip)";
353 return;
354 }
355 case MachineOperand::MO_GlobalAddress: {
356 bool isCallOp = Modifier && !strcmp(Modifier, "call");
357 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
358 bool needCloseParen = false;
359
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000360 const GlobalValue *GV = MO.getGlobal();
361 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
362 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000363 // If GV is an alias then use the aliasee for determining
364 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000365 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000366 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000367 }
368
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 bool isThreadLocal = GVar && GVar->isThreadLocal();
370
371 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000372 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 if (!isMemOp && !isCallOp)
375 O << '$';
376 else if (Name[0] == '$') {
377 // The name begins with a dollar-sign. In order to avoid having it look
378 // like an integer immediate to the assembler, enclose it in parens.
379 O << '(';
380 needCloseParen = true;
381 }
382
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000383 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 // Link-once, declaration, or Weakly-linked global variables need
385 // non-lazily-resolved stubs
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000386 if (GV->isDeclaration() || GV->mayBeOverridden()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 // Dynamically-resolved functions need a stub for the function.
388 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000389 // Function stubs are no longer needed for Mac OS X 10.5 and up.
390 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
391 O << Name;
392 } else {
393 FnStubs.insert(Name);
394 printSuffixedName(Name, "$stub");
395 }
Evan Chenga65854f2008-12-05 01:06:39 +0000396 } else if (GV->hasHiddenVisibility()) {
397 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
398 // Definition is not definitely in the current translation unit.
399 O << Name;
400 else {
401 HiddenGVStubs.insert(Name);
402 printSuffixedName(Name, "$non_lazy_ptr");
403 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 } else {
405 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000406 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 }
408 } else {
409 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000410 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 O << Name;
412 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000413
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000415 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 } else {
417 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000418 O << "__imp_";
419 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 O << Name;
421
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000422 if (isCallOp) {
423 if (shouldPrintPLT(TM, Subtarget)) {
424 // Assemble call via PLT for externally visible symbols
425 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000426 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 O << "@PLT";
428 }
429 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
430 // Save function name for later type emission
431 FnStubs.insert(Name);
432 }
433 }
434
435 if (GV->hasExternalWeakLinkage())
436 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000437
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000438 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439
440 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000441 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 O << "@TLSGD"; // general dynamic TLS model
443 else
444 if (GV->isDeclaration())
445 O << "@INDNTPOFF"; // initial exec TLS model
446 else
447 O << "@NTPOFF"; // local exec TLS model
448 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000449 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
451 O << "@GOT";
452 else
453 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000454 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
455 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000456 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 O << "@GOTPCREL";
458
459 if (needCloseParen) {
460 needCloseParen = false;
461 O << ')';
462 }
463
464 // Use rip when possible to reduce code size, except when
465 // index or base register are also part of the address. e.g.
466 // foo(%rip)(%rcx,%rax,4) is not legal
467 O << "(%rip)";
468 }
469 }
470
471 if (needCloseParen)
472 O << ')';
473
474 return;
475 }
476 case MachineOperand::MO_ExternalSymbol: {
477 bool isCallOp = Modifier && !strcmp(Modifier, "call");
478 bool needCloseParen = false;
479 std::string Name(TAI->getGlobalPrefix());
480 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000481 // Print function stub suffix unless it's Mac OS X 10.5 and up.
482 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
483 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000485 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 return;
487 }
488 if (!isCallOp)
489 O << '$';
490 else if (Name[0] == '$') {
491 // The name begins with a dollar-sign. In order to avoid having it look
492 // like an integer immediate to the assembler, enclose it in parens.
493 O << '(';
494 needCloseParen = true;
495 }
496
497 O << Name;
498
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000499 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 std::string GOTName(TAI->getGlobalPrefix());
501 GOTName+="_GLOBAL_OFFSET_TABLE_";
502 if (Name == GOTName)
503 // HACK! Emit extra offset to PC during printing GOT offset to
504 // compensate for the size of popl instruction. The resulting code
505 // should look like:
506 // call .piclabel
507 // piclabel:
508 // popl %some_register
509 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
510 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000511 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512
513 if (isCallOp)
514 O << "@PLT";
515 }
516
517 if (needCloseParen)
518 O << ')';
519
520 if (!isCallOp && Subtarget->isPICStyleRIPRel())
521 O << "(%rip)";
522
523 return;
524 }
525 default:
526 O << "<unknown operand type>"; return;
527 }
528}
529
530void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000531 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 assert(value <= 7 && "Invalid ssecc argument!");
533 switch (value) {
534 case 0: O << "eq"; break;
535 case 1: O << "lt"; break;
536 case 2: O << "le"; break;
537 case 3: O << "unord"; break;
538 case 4: O << "neq"; break;
539 case 5: O << "nlt"; break;
540 case 6: O << "nle"; break;
541 case 7: O << "ord"; break;
542 }
543}
544
545void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
546 const char *Modifier){
547 assert(isMem(MI, Op) && "Invalid memory reference!");
548 MachineOperand BaseReg = MI->getOperand(Op);
549 MachineOperand IndexReg = MI->getOperand(Op+2);
550 const MachineOperand &DispSpec = MI->getOperand(Op+3);
551
552 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000553 if (DispSpec.isGlobal() ||
554 DispSpec.isCPI() ||
555 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 printOperand(MI, Op+3, "mem", NotRIPRel);
557 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000558 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
560 O << DispVal;
561 }
562
563 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000564 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000566
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 // There are cases where we can end up with ESP/RSP in the indexreg slot.
568 // If this happens, swap the base/index register to support assemblers that
569 // don't work when the index is *SP.
570 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
571 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
572 std::swap(BaseReg, IndexReg);
573 std::swap(BaseRegOperand, IndexRegOperand);
574 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000575
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000576 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 if (BaseReg.getReg())
578 printOperand(MI, Op+BaseRegOperand, Modifier);
579
580 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000581 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 printOperand(MI, Op+IndexRegOperand, Modifier);
583 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000584 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000586 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 }
588}
589
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000590void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000591 const MachineBasicBlock *MBB) const {
592 if (!TAI->getSetDirective())
593 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000594
595 // We don't need .set machinery if we have GOT-style relocations
596 if (Subtarget->isPICStyleGOT())
597 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000598
Evan Cheng6fb06762007-11-09 01:32:10 +0000599 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
600 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000601 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000602 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000603 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000604 << '_' << uid << '\n';
605 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000606 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000607}
608
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000610 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000611 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612}
613
614
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000615void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
616 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000617 unsigned uid) const
618{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000619 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
620 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
621
622 O << JTEntryDirective << ' ';
623
624 if (TM.getRelocationModel() == Reloc::PIC_) {
625 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
626 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
627 << '_' << uid << "_set_" << MBB->getNumber();
628 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000629 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000630 O << "@GOTOFF";
631 } else
632 assert(0 && "Don't know how to print MBB label for this PIC mode");
633 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000634 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000635}
636
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000637bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 unsigned Reg = MO.getReg();
640 switch (Mode) {
641 default: return true; // Unknown mode.
642 case 'b': // Print QImode register
643 Reg = getX86SubSuperRegister(Reg, MVT::i8);
644 break;
645 case 'h': // Print QImode high register
646 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
647 break;
648 case 'w': // Print HImode register
649 Reg = getX86SubSuperRegister(Reg, MVT::i16);
650 break;
651 case 'k': // Print SImode register
652 Reg = getX86SubSuperRegister(Reg, MVT::i32);
653 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000654 case 'q': // Print DImode register
655 Reg = getX86SubSuperRegister(Reg, MVT::i64);
656 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 }
658
Evan Cheng00d04a72008-07-07 22:21:06 +0000659 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 return false;
661}
662
663/// PrintAsmOperand - Print out an operand for an inline asm expression.
664///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000665bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000666 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 const char *ExtraCode) {
668 // Does this asm operand have a single letter operand modifier?
669 if (ExtraCode && ExtraCode[0]) {
670 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 switch (ExtraCode[0]) {
673 default: return true; // Unknown modifier.
674 case 'c': // Don't print "$" before a global var name or constant.
675 printOperand(MI, OpNo, "mem");
676 return false;
677 case 'b': // Print QImode register
678 case 'h': // Print QImode high register
679 case 'w': // Print HImode register
680 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000681 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000682 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
684 printOperand(MI, OpNo);
685 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000686
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 case 'P': // Don't print @PLT, but do print as memory.
688 printOperand(MI, OpNo, "mem");
689 return false;
690 }
691 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 printOperand(MI, OpNo);
694 return false;
695}
696
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000697bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000699 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000701 if (ExtraCode && ExtraCode[0]) {
702 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000703
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000704 switch (ExtraCode[0]) {
705 default: return true; // Unknown modifier.
706 case 'b': // Print QImode register
707 case 'h': // Print QImode high register
708 case 'w': // Print HImode register
709 case 'k': // Print SImode register
710 case 'q': // Print SImode register
711 // These only apply to registers, ignore on mem.
712 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000713 case 'P': // Don't print @PLT, but do print as memory.
714 printOperand(MI, OpNo, "mem");
715 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000716 }
717 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 printMemReference(MI, OpNo);
719 return false;
720}
721
Bill Wendlingb3b11262009-02-06 21:45:08 +0000722/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
723/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724///
725void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
726 ++EmittedInsts;
727
Bill Wendlingb3b11262009-02-06 21:45:08 +0000728 if (TAI->doesSupportDebugInformation()) {
729 static DebugLoc PrevDL = DebugLoc::getUnknownLoc();
730 DebugLoc CurDL = MI->getDebugLoc();
731
732 if (!CurDL.isInvalid() && !CurDL.isUnknown() && PrevDL != CurDL) {
733 DebugLocTuple DLT = MF->getDebugLocTuple(CurDL);
734 printLabel(DW->RecordSourceLine(DLT.Line, DLT.Col, DLT.Src));
735 }
736
737 PrevDL = CurDL;
738 }
739
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 // Call the autogenerated instruction printer routines.
741 printInstruction(MI);
742}
743
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000744/// doInitialization
745bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000746
Evan Cheng5cda7762008-07-09 06:36:53 +0000747 bool Result = AsmPrinter::doInitialization(M);
748
Dale Johannesen06545922008-07-09 20:55:35 +0000749 if (TAI->doesSupportDebugInformation()) {
750 // Let PassManager know we need debug information and relay
751 // the MachineModuleInfo address on to DwarfWriter.
752 // AsmPrinter::doInitialization did this analysis.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000753 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
754 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000755 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000756 }
757
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000758 // Darwin wants symbols to be quoted if they have complex names.
759 if (Subtarget->isTargetDarwin())
760 Mang->setUseQuotes(true);
761
762 return Result;
763}
764
765
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000766void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000767 const TargetData *TD = TM.getTargetData();
768
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000769 if (!GVar->hasInitializer())
770 return; // External global require no code
771
772 // Check to see if this is a special global used by LLVM, if so, emit it.
773 if (EmitSpecialLLVMGlobal(GVar)) {
774 if (Subtarget->isTargetDarwin() &&
775 TM.getRelocationModel() == Reloc::Static) {
776 if (GVar->getName() == "llvm.global_ctors")
777 O << ".reference .constructors_used\n";
778 else if (GVar->getName() == "llvm.global_dtors")
779 O << ".reference .destructors_used\n";
780 }
781 return;
782 }
783
784 std::string name = Mang->getValueName(GVar);
785 Constant *C = GVar->getInitializer();
786 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000787 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 unsigned Align = TD->getPreferredAlignmentLog(GVar);
789
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000790 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000791
792 if (Subtarget->isTargetELF())
793 O << "\t.type\t" << name << ",@object\n";
794
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000795 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000796
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000798 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000799 if (GVar->hasExternalLinkage()) {
800 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000801 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000802 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000803 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 return;
805 }
806 }
807
808 if (!GVar->isThreadLocal() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000809 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000811
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000813 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000816 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000818 O << "\t.globl " << name << '\n'
819 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 EmitGlobalConstant(C);
825 return;
826 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000827 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000828 if (TAI->getCOMMDirectiveTakesAlignment())
829 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 }
831 } else {
832 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000833 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000836 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000840 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 return;
844 }
845 }
846
847 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000848 case GlobalValue::CommonLinkage:
849 case GlobalValue::LinkOnceLinkage:
850 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 O << "\t.globl " << name << '\n'
853 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000856 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 }
860 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000861 case GlobalValue::DLLExportLinkage:
862 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 // FIXME: appending linkage variables should go into a section of
864 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000865 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000867 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000868 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000869 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000870 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000871 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000872 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000873 assert(0 && "Unknown linkage type!");
874 }
875
876 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000877 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000879 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000881 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000882
883 // If the initializer is a extern weak symbol, remember to emit the weak
884 // reference!
885 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
886 if (GV->hasExternalWeakLinkage())
887 ExtWeakSymbols.insert(GV);
888
889 EmitGlobalConstant(C);
890}
891
Evan Cheng76443dc2008-07-08 00:55:58 +0000892/// printGVStub - Print stub for a global value.
893///
894void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000895 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000896 O << ":\n\t.indirect_symbol ";
897 if (Prefix) O << Prefix;
898 O << GV << "\n\t.long\t0\n";
899}
900
Evan Chenga65854f2008-12-05 01:06:39 +0000901/// printHiddenGVStub - Print stub for a hidden global value.
902///
903void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
904 EmitAlignment(2);
905 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
906 if (Prefix) O << Prefix;
907 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
908}
909
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000910
911bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000912 // Print out module-level global variables here.
913 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000914 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000915 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000916
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000917 if (I->hasDLLExportLinkage())
918 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
919 }
920
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000921 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000922 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000923 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000924
925 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
926 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000927 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000928 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000929
930 if (!DLLExportedFns.empty()) {
931 SwitchToDataSection(".section .drectve");
932 }
933
934 for (StringSet<>::iterator i = DLLExportedFns.begin(),
935 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000936 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000937 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000938
939 if (Subtarget->isTargetDarwin()) {
940 SwitchToDataSection("");
941
942 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000943 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000944 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000945 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
946 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000947 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000949 O << ":\n"
950 "\t.indirect_symbol " << p << "\n"
951 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000952 }
953
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000954 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955
Evan Cheng76443dc2008-07-08 00:55:58 +0000956 // Print global value stubs.
957 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000958 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
959 // Add the (possibly multiple) personalities to the set of global values.
960 // Only referenced functions get into the Personalities list.
961 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000962 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000963 E = Personalities.end(); I != E; ++I) {
964 if (!*I)
965 continue;
966 if (!InStubSection) {
967 SwitchToDataSection(
968 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
969 InStubSection = true;
970 }
971 printGVStub((*I)->getNameStart(), "_");
972 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000973 }
974
975 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000976 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000977 SwitchToDataSection(
978 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
979 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000980 i != e; ++i)
981 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000982
Evan Chenga65854f2008-12-05 01:06:39 +0000983 if (!HiddenGVStubs.empty()) {
984 SwitchToSection(TAI->getDataSection());
985 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
986 i != e; ++i)
987 printHiddenGVStub(i->getKeyData());
988 }
989
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000990 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000991 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000992 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000993
994 // Funny Darwin hack: This flag tells the linker that no global symbols
995 // contain code that falls through to other global symbols (e.g. the obvious
996 // implementation of multiple entry points). If this doesn't occur, the
997 // linker can safely perform dead code stripping. Since LLVM never
998 // generates code that does this, it is always safe to set.
999 O << "\t.subsections_via_symbols\n";
1000 } else if (Subtarget->isTargetCygMing()) {
1001 // Emit type information for external functions
1002 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1003 i != e; ++i) {
1004 O << "\t.def\t " << i->getKeyData()
1005 << ";\t.scl\t" << COFF::C_EXT
1006 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1007 << ";\t.endef\n";
1008 }
1009
1010 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001011 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001012 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001013 } else if (Subtarget->isTargetELF()) {
1014 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001015 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001016 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001017 }
1018
1019 return AsmPrinter::doFinalization(M);
1020}
1021
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022// Include the auto-generated portion of the assembly writer.
1023#include "X86GenAsmWriter.inc"