blob: e3b6c27dcba8afafa284cf912251494d82c77097 [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"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000029#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetAsmInfo.h"
34#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0729ccf2008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 std::string label;
43 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 else
48 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000075 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000076 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
Duncan Sandsd68f13b2009-01-12 20:38:59 +000079 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000080 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000088/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000089static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000090 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000150void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000153 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000155 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000156
Devang Patel93698d92008-10-01 23:18:38 +0000157 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000158 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000159 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 switch (F->getLinkage()) {
161 default: assert(0 && "Unknown linkage type!");
162 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000163 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000164 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 break;
166 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000171 case Function::LinkOnceAnyLinkage:
172 case Function::LinkOnceODRLinkage:
173 case Function::WeakAnyLinkage:
174 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
178 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 }
185 break;
186 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
200 O << CurrentFnName << ":\n";
201 // Add some workaround for linkonce linkage on Cygwin\MinGW
202 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000203 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000205}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000207/// runOnMachineFunction - This uses the printMachineInstruction()
208/// method to print assembly for each instruction.
209///
210bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
211 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000212 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000213 unsigned CC = F->getCallingConv();
214
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000215 SetupMachineFunction(MF);
216 O << "\n\n";
217
218 // Populate function information map. Actually, We don't want to populate
219 // non-stdcall or non-fastcall functions' information right now.
220 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
221 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
222
223 // Print out constants referenced by the function
224 EmitConstantPool(MF.getConstantPool());
225
226 if (F->hasDLLExportLinkage())
227 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
228
229 // Print the 'header' of function
230 emitFunctionHeader(MF);
231
232 // Emit pre-function debug and/or EH information.
233 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000234 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000237 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
239 I != E; ++I) {
240 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000241 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000242 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 O << '\n';
244 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000245 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
246 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000248 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 printMachineInstruction(II);
251 }
252 }
253
Dale Johannesenf35771f2008-04-08 00:37:56 +0000254 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
255 // If the function is empty, then we need to emit *something*. Otherwise,
256 // the function's label might be associated with something that it wasn't
257 // meant to be associated with. We emit a noop in this situation.
258 // We are assuming inline asms are code.
259 O << "\tnop\n";
260 }
261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000263 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000265 // Emit post-function debug information.
266 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000267 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268
269 // Print out jump tables referenced by the function.
270 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000271
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000272 O.flush();
273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 // We didn't modify anything.
275 return false;
276}
277
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000278static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
280}
281
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000282static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
283 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
284 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
285}
286
287static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
289}
290
291void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
292 const char *Modifier, bool NotRIPRel) {
293 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 switch (MO.getType()) {
295 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000296 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 "Virtual registers should not make it this far!");
298 O << '%';
299 unsigned Reg = MO.getReg();
300 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000301 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
303 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
304 Reg = getX86SubSuperRegister(Reg, VT);
305 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000306 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 return;
308 }
309
310 case MachineOperand::MO_Immediate:
Evan Cheng75952172009-03-04 06:48:53 +0000311 if (Modifier && !strcmp(Modifier, "call"))
312 O << '*';
313 else if (!Modifier ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
315 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000316 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 return;
318 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000319 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 return;
321 case MachineOperand::MO_JumpTableIndex: {
322 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
323 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000324 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000325 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
327 if (TM.getRelocationModel() == Reloc::PIC_) {
328 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000329 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
330 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 else if (Subtarget->isPICStyleGOT())
332 O << "@GOTOFF";
333 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
336 O << "(%rip)";
337 return;
338 }
339 case MachineOperand::MO_ConstantPoolIndex: {
340 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
341 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000342 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000343 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 if (TM.getRelocationModel() == Reloc::PIC_) {
346 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000347 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
348 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 else if (Subtarget->isPICStyleGOT())
350 O << "@GOTOFF";
351 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000352
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000353 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
355 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
356 O << "(%rip)";
357 return;
358 }
359 case MachineOperand::MO_GlobalAddress: {
360 bool isCallOp = Modifier && !strcmp(Modifier, "call");
361 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
362 bool needCloseParen = false;
363
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000364 const GlobalValue *GV = MO.getGlobal();
365 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
366 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000367 // If GV is an alias then use the aliasee for determining
368 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000369 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000370 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000371 }
372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 bool isThreadLocal = GVar && GVar->isThreadLocal();
374
375 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000376 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000377
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 if (!isMemOp && !isCallOp)
379 O << '$';
380 else if (Name[0] == '$') {
381 // The name begins with a dollar-sign. In order to avoid having it look
382 // like an integer immediate to the assembler, enclose it in parens.
383 O << '(';
384 needCloseParen = true;
385 }
386
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000387 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 // Link-once, declaration, or Weakly-linked global variables need
389 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000390 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // Dynamically-resolved functions need a stub for the function.
392 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000393 // Function stubs are no longer needed for Mac OS X 10.5 and up.
394 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
395 O << Name;
396 } else {
397 FnStubs.insert(Name);
398 printSuffixedName(Name, "$stub");
399 }
Evan Chenga65854f2008-12-05 01:06:39 +0000400 } else if (GV->hasHiddenVisibility()) {
401 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
402 // Definition is not definitely in the current translation unit.
403 O << Name;
404 else {
405 HiddenGVStubs.insert(Name);
406 printSuffixedName(Name, "$non_lazy_ptr");
407 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 } else {
409 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000410 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 }
412 } else {
413 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000414 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 O << Name;
416 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000419 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 } else {
421 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000422 O << "__imp_";
423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 O << Name;
425
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000426 if (isCallOp) {
427 if (shouldPrintPLT(TM, Subtarget)) {
428 // Assemble call via PLT for externally visible symbols
429 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000430 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 O << "@PLT";
432 }
433 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
434 // Save function name for later type emission
435 FnStubs.insert(Name);
436 }
437 }
438
439 if (GV->hasExternalWeakLinkage())
440 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000441
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000442 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443
444 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000445 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
446 switch (model) {
447 case TLSModel::GeneralDynamic:
448 O << "@TLSGD";
449 break;
450 case TLSModel::LocalDynamic:
451 // O << "@TLSLD"; // local dynamic not implemented
452 O << "@TLSGD";
453 break;
454 case TLSModel::InitialExec:
455 if (Subtarget->is64Bit())
456 O << "@TLSGD"; // 64 bit intial exec not implemented
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 else
Rafael Espindola7b620af2009-02-27 13:37:18 +0000458 O << "@INDNTPOFF";
459 break;
460 case TLSModel::LocalExec:
461 if (Subtarget->is64Bit())
462 O << "@TLSGD"; // 64 bit local exec not implemented
463 else
464 O << "@NTPOFF";
465 break;
466 default:
467 assert (0 && "Unknown TLS model");
468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000470 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
472 O << "@GOT";
473 else
474 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000475 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
476 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000477 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 O << "@GOTPCREL";
479
480 if (needCloseParen) {
481 needCloseParen = false;
482 O << ')';
483 }
484
485 // Use rip when possible to reduce code size, except when
486 // index or base register are also part of the address. e.g.
487 // foo(%rip)(%rcx,%rax,4) is not legal
488 O << "(%rip)";
489 }
490 }
491
492 if (needCloseParen)
493 O << ')';
494
495 return;
496 }
497 case MachineOperand::MO_ExternalSymbol: {
498 bool isCallOp = Modifier && !strcmp(Modifier, "call");
499 bool needCloseParen = false;
500 std::string Name(TAI->getGlobalPrefix());
501 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000502 // Print function stub suffix unless it's Mac OS X 10.5 and up.
503 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
504 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000506 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 return;
508 }
509 if (!isCallOp)
510 O << '$';
511 else if (Name[0] == '$') {
512 // The name begins with a dollar-sign. In order to avoid having it look
513 // like an integer immediate to the assembler, enclose it in parens.
514 O << '(';
515 needCloseParen = true;
516 }
517
518 O << Name;
519
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000520 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 std::string GOTName(TAI->getGlobalPrefix());
522 GOTName+="_GLOBAL_OFFSET_TABLE_";
523 if (Name == GOTName)
524 // HACK! Emit extra offset to PC during printing GOT offset to
525 // compensate for the size of popl instruction. The resulting code
526 // should look like:
527 // call .piclabel
528 // piclabel:
529 // popl %some_register
530 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
531 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000532 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533
534 if (isCallOp)
535 O << "@PLT";
536 }
537
538 if (needCloseParen)
539 O << ')';
540
541 if (!isCallOp && Subtarget->isPICStyleRIPRel())
542 O << "(%rip)";
543
544 return;
545 }
546 default:
547 O << "<unknown operand type>"; return;
548 }
549}
550
551void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000552 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 assert(value <= 7 && "Invalid ssecc argument!");
554 switch (value) {
555 case 0: O << "eq"; break;
556 case 1: O << "lt"; break;
557 case 2: O << "le"; break;
558 case 3: O << "unord"; break;
559 case 4: O << "neq"; break;
560 case 5: O << "nlt"; break;
561 case 6: O << "nle"; break;
562 case 7: O << "ord"; break;
563 }
564}
565
566void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
567 const char *Modifier){
568 assert(isMem(MI, Op) && "Invalid memory reference!");
569 MachineOperand BaseReg = MI->getOperand(Op);
570 MachineOperand IndexReg = MI->getOperand(Op+2);
571 const MachineOperand &DispSpec = MI->getOperand(Op+3);
572
573 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000574 if (DispSpec.isGlobal() ||
575 DispSpec.isCPI() ||
576 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 printOperand(MI, Op+3, "mem", NotRIPRel);
578 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000579 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
581 O << DispVal;
582 }
583
584 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000585 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000587
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 // There are cases where we can end up with ESP/RSP in the indexreg slot.
589 // If this happens, swap the base/index register to support assemblers that
590 // don't work when the index is *SP.
591 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
592 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
593 std::swap(BaseReg, IndexReg);
594 std::swap(BaseRegOperand, IndexRegOperand);
595 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000596
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000597 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 if (BaseReg.getReg())
599 printOperand(MI, Op+BaseRegOperand, Modifier);
600
601 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000602 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 printOperand(MI, Op+IndexRegOperand, Modifier);
604 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000605 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000607 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 }
609}
610
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000611void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000612 const MachineBasicBlock *MBB) const {
613 if (!TAI->getSetDirective())
614 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000615
616 // We don't need .set machinery if we have GOT-style relocations
617 if (Subtarget->isPICStyleGOT())
618 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000619
Evan Cheng6fb06762007-11-09 01:32:10 +0000620 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
621 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000622 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000623 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000624 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000625 << '_' << uid << '\n';
626 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000627 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000628}
629
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000631 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000632 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633}
634
635
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000636void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
637 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000638 unsigned uid) const
639{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000640 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
641 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
642
643 O << JTEntryDirective << ' ';
644
645 if (TM.getRelocationModel() == Reloc::PIC_) {
646 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
647 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
648 << '_' << uid << "_set_" << MBB->getNumber();
649 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000650 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000651 O << "@GOTOFF";
652 } else
653 assert(0 && "Don't know how to print MBB label for this PIC mode");
654 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000655 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000656}
657
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000658bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 unsigned Reg = MO.getReg();
661 switch (Mode) {
662 default: return true; // Unknown mode.
663 case 'b': // Print QImode register
664 Reg = getX86SubSuperRegister(Reg, MVT::i8);
665 break;
666 case 'h': // Print QImode high register
667 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
668 break;
669 case 'w': // Print HImode register
670 Reg = getX86SubSuperRegister(Reg, MVT::i16);
671 break;
672 case 'k': // Print SImode register
673 Reg = getX86SubSuperRegister(Reg, MVT::i32);
674 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000675 case 'q': // Print DImode register
676 Reg = getX86SubSuperRegister(Reg, MVT::i64);
677 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 }
679
Evan Cheng00d04a72008-07-07 22:21:06 +0000680 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 return false;
682}
683
684/// PrintAsmOperand - Print out an operand for an inline asm expression.
685///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000686bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000687 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 const char *ExtraCode) {
689 // Does this asm operand have a single letter operand modifier?
690 if (ExtraCode && ExtraCode[0]) {
691 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 switch (ExtraCode[0]) {
694 default: return true; // Unknown modifier.
695 case 'c': // Don't print "$" before a global var name or constant.
696 printOperand(MI, OpNo, "mem");
697 return false;
698 case 'b': // Print QImode register
699 case 'h': // Print QImode high register
700 case 'w': // Print HImode register
701 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000702 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000703 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
705 printOperand(MI, OpNo);
706 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 case 'P': // Don't print @PLT, but do print as memory.
709 printOperand(MI, OpNo, "mem");
710 return false;
711 }
712 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000713
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 printOperand(MI, OpNo);
715 return false;
716}
717
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000718bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000720 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000722 if (ExtraCode && ExtraCode[0]) {
723 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000724
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000725 switch (ExtraCode[0]) {
726 default: return true; // Unknown modifier.
727 case 'b': // Print QImode register
728 case 'h': // Print QImode high register
729 case 'w': // Print HImode register
730 case 'k': // Print SImode register
731 case 'q': // Print SImode register
732 // These only apply to registers, ignore on mem.
733 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000734 case 'P': // Don't print @PLT, but do print as memory.
735 printOperand(MI, OpNo, "mem");
736 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000737 }
738 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 printMemReference(MI, OpNo);
740 return false;
741}
742
Bill Wendlingb3b11262009-02-06 21:45:08 +0000743/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
744/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745///
746void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
747 ++EmittedInsts;
748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 // Call the autogenerated instruction printer routines.
750 printInstruction(MI);
751}
752
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000753/// doInitialization
754bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000755
Evan Cheng5cda7762008-07-09 06:36:53 +0000756 bool Result = AsmPrinter::doInitialization(M);
757
Dale Johannesen06545922008-07-09 20:55:35 +0000758 if (TAI->doesSupportDebugInformation()) {
759 // Let PassManager know we need debug information and relay
760 // the MachineModuleInfo address on to DwarfWriter.
761 // AsmPrinter::doInitialization did this analysis.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000762 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
763 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000764 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000765 }
766
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000767 // Darwin wants symbols to be quoted if they have complex names.
768 if (Subtarget->isTargetDarwin())
769 Mang->setUseQuotes(true);
770
771 return Result;
772}
773
774
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000775void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000776 const TargetData *TD = TM.getTargetData();
777
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000778 if (!GVar->hasInitializer())
779 return; // External global require no code
780
781 // Check to see if this is a special global used by LLVM, if so, emit it.
782 if (EmitSpecialLLVMGlobal(GVar)) {
783 if (Subtarget->isTargetDarwin() &&
784 TM.getRelocationModel() == Reloc::Static) {
785 if (GVar->getName() == "llvm.global_ctors")
786 O << ".reference .constructors_used\n";
787 else if (GVar->getName() == "llvm.global_dtors")
788 O << ".reference .destructors_used\n";
789 }
790 return;
791 }
792
793 std::string name = Mang->getValueName(GVar);
794 Constant *C = GVar->getInitializer();
795 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000796 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 unsigned Align = TD->getPreferredAlignmentLog(GVar);
798
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000799 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000800
801 if (Subtarget->isTargetELF())
802 O << "\t.type\t" << name << ",@object\n";
803
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000804 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000805
Evan Chengcf84b142009-02-18 02:19:52 +0000806 if (C->isNullValue() && !GVar->hasSection() &&
807 !(Subtarget->isTargetDarwin() &&
808 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000809 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 if (GVar->hasExternalLinkage()) {
811 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 return;
816 }
817 }
818
819 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000820 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000822
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000824 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000827 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000828 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << "\t.globl " << name << '\n'
830 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000831 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000832 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000833 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 EmitGlobalConstant(C);
836 return;
837 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000839 if (TAI->getCOMMDirectiveTakesAlignment())
840 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 }
842 } else {
843 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000844 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000845 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 return;
855 }
856 }
857
858 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000859 case GlobalValue::CommonAnyLinkage:
860 case GlobalValue::CommonODRLinkage:
861 case GlobalValue::LinkOnceAnyLinkage:
862 case GlobalValue::LinkOnceODRLinkage:
863 case GlobalValue::WeakAnyLinkage:
864 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 O << "\t.globl " << name << '\n'
867 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000868 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000870 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000871 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000872 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000873 }
874 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000875 case GlobalValue::DLLExportLinkage:
876 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000877 // FIXME: appending linkage variables should go into a section of
878 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000879 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000881 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000882 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000883 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000884 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000885 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000886 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000887 assert(0 && "Unknown linkage type!");
888 }
889
890 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000894 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000895 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000897 EmitGlobalConstant(C);
898}
899
Evan Cheng76443dc2008-07-08 00:55:58 +0000900/// printGVStub - Print stub for a global value.
901///
902void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000903 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000904 O << ":\n\t.indirect_symbol ";
905 if (Prefix) O << Prefix;
906 O << GV << "\n\t.long\t0\n";
907}
908
Evan Chenga65854f2008-12-05 01:06:39 +0000909/// printHiddenGVStub - Print stub for a hidden global value.
910///
911void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
912 EmitAlignment(2);
913 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
914 if (Prefix) O << Prefix;
915 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
916}
917
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000918
919bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000920 // Print out module-level global variables here.
921 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000922 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000923 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000924
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000925 if (I->hasDLLExportLinkage())
926 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000927
928 // If the global is a extern weak symbol, remember to emit the weak
929 // reference!
930 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
931 // not used. But currently it's the only way to deal with extern weak
932 // initializers hidden deep inside constant expressions.
933 if (I->hasExternalWeakLinkage())
934 ExtWeakSymbols.insert(I);
935 }
936
937 for (Module::const_iterator I = M.begin(), E = M.end();
938 I != E; ++I) {
939 // If the global is a extern weak symbol, remember to emit the weak
940 // reference!
941 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
942 // not used. But currently it's the only way to deal with extern weak
943 // initializers hidden deep inside constant expressions.
944 if (I->hasExternalWeakLinkage())
945 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000946 }
947
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000949 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000950 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000951
952 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
953 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000954 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000956
957 if (!DLLExportedFns.empty()) {
958 SwitchToDataSection(".section .drectve");
959 }
960
961 for (StringSet<>::iterator i = DLLExportedFns.begin(),
962 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000963 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000964 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000965
966 if (Subtarget->isTargetDarwin()) {
967 SwitchToDataSection("");
968
969 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000970 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000971 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000972 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
973 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000974 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000975 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000976 O << ":\n"
977 "\t.indirect_symbol " << p << "\n"
978 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000979 }
980
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000981 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000982
Evan Cheng76443dc2008-07-08 00:55:58 +0000983 // Print global value stubs.
984 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000985 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
986 // Add the (possibly multiple) personalities to the set of global values.
987 // Only referenced functions get into the Personalities list.
988 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000989 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000990 E = Personalities.end(); I != E; ++I) {
991 if (!*I)
992 continue;
993 if (!InStubSection) {
994 SwitchToDataSection(
995 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
996 InStubSection = true;
997 }
998 printGVStub((*I)->getNameStart(), "_");
999 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001000 }
1001
1002 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +00001003 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001004 SwitchToDataSection(
1005 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1006 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001007 i != e; ++i)
1008 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001009
Evan Chenga65854f2008-12-05 01:06:39 +00001010 if (!HiddenGVStubs.empty()) {
1011 SwitchToSection(TAI->getDataSection());
1012 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1013 i != e; ++i)
1014 printHiddenGVStub(i->getKeyData());
1015 }
1016
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001017 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001018 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001019 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001020
1021 // Funny Darwin hack: This flag tells the linker that no global symbols
1022 // contain code that falls through to other global symbols (e.g. the obvious
1023 // implementation of multiple entry points). If this doesn't occur, the
1024 // linker can safely perform dead code stripping. Since LLVM never
1025 // generates code that does this, it is always safe to set.
1026 O << "\t.subsections_via_symbols\n";
1027 } else if (Subtarget->isTargetCygMing()) {
1028 // Emit type information for external functions
1029 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1030 i != e; ++i) {
1031 O << "\t.def\t " << i->getKeyData()
1032 << ";\t.scl\t" << COFF::C_EXT
1033 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1034 << ";\t.endef\n";
1035 }
1036
1037 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001038 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001039 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001040 } else if (Subtarget->isTargetELF()) {
1041 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001042 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001043 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001044 }
1045
1046 return AsmPrinter::doFinalization(M);
1047}
1048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049// Include the auto-generated portion of the assembly writer.
1050#include "X86GenAsmWriter.inc"