blob: c2b053edd75f927e34945804d2d2bbcd0694012e [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 Gohmand38f8762009-03-31 18:39:13 +0000241 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
242 // This is an entry block or a block that's only reachable via a
243 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
244 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000245 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 O << '\n';
247 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000248 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
249 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000251 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000252 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 printMachineInstruction(II);
254 }
255 }
256
Dale Johannesenf35771f2008-04-08 00:37:56 +0000257 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
258 // If the function is empty, then we need to emit *something*. Otherwise,
259 // the function's label might be associated with something that it wasn't
260 // meant to be associated with. We emit a noop in this situation.
261 // We are assuming inline asms are code.
262 O << "\tnop\n";
263 }
264
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000266 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000268 // Emit post-function debug information.
269 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000270 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271
272 // Print out jump tables referenced by the function.
273 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000274
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000275 O.flush();
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 // We didn't modify anything.
278 return false;
279}
280
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000281static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
283}
284
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000285static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
286 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
287 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
288}
289
290static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
292}
293
294void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
295 const char *Modifier, bool NotRIPRel) {
296 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 switch (MO.getType()) {
298 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000299 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 "Virtual registers should not make it this far!");
301 O << '%';
302 unsigned Reg = MO.getReg();
303 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000304 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
306 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
307 Reg = getX86SubSuperRegister(Reg, VT);
308 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000309 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 return;
311 }
312
313 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000314 if (!Modifier || (strcmp(Modifier, "debug") &&
315 strcmp(Modifier, "mem") &&
316 strcmp(Modifier, "call")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000318 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 return;
320 case MachineOperand::MO_MachineBasicBlock:
Evan Cheng11db8142009-03-24 00:17:40 +0000321 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 return;
323 case MachineOperand::MO_JumpTableIndex: {
324 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
325 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000326 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000327 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
329 if (TM.getRelocationModel() == Reloc::PIC_) {
330 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000331 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
332 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 else if (Subtarget->isPICStyleGOT())
334 O << "@GOTOFF";
335 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000336
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
338 O << "(%rip)";
339 return;
340 }
341 case MachineOperand::MO_ConstantPoolIndex: {
342 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
343 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000344 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000345 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
347 if (TM.getRelocationModel() == Reloc::PIC_) {
348 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000349 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
350 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 else if (Subtarget->isPICStyleGOT())
352 O << "@GOTOFF";
353 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000354
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000355 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
357 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
358 O << "(%rip)";
359 return;
360 }
361 case MachineOperand::MO_GlobalAddress: {
362 bool isCallOp = Modifier && !strcmp(Modifier, "call");
363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
364 bool needCloseParen = false;
365
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000366 const GlobalValue *GV = MO.getGlobal();
367 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
368 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000369 // If GV is an alias then use the aliasee for determining
370 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000371 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000372 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000373 }
374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 bool isThreadLocal = GVar && GVar->isThreadLocal();
376
377 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000378 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 if (!isMemOp && !isCallOp)
381 O << '$';
382 else if (Name[0] == '$') {
383 // The name begins with a dollar-sign. In order to avoid having it look
384 // like an integer immediate to the assembler, enclose it in parens.
385 O << '(';
386 needCloseParen = true;
387 }
388
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000389 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // Link-once, declaration, or Weakly-linked global variables need
391 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000392 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 // Dynamically-resolved functions need a stub for the function.
394 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000395 // Function stubs are no longer needed for Mac OS X 10.5 and up.
396 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
397 O << Name;
398 } else {
399 FnStubs.insert(Name);
400 printSuffixedName(Name, "$stub");
401 }
Evan Chenga65854f2008-12-05 01:06:39 +0000402 } else if (GV->hasHiddenVisibility()) {
403 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
404 // Definition is not definitely in the current translation unit.
405 O << Name;
406 else {
407 HiddenGVStubs.insert(Name);
408 printSuffixedName(Name, "$non_lazy_ptr");
409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 } else {
411 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000412 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 }
414 } else {
415 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000416 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 O << Name;
418 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000419
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000421 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 } else {
423 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000424 O << "__imp_";
425 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 O << Name;
427
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000428 if (isCallOp) {
429 if (shouldPrintPLT(TM, Subtarget)) {
430 // Assemble call via PLT for externally visible symbols
431 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000432 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 O << "@PLT";
434 }
435 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
436 // Save function name for later type emission
437 FnStubs.insert(Name);
438 }
439 }
440
441 if (GV->hasExternalWeakLinkage())
442 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000443
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000444 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000447 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
448 switch (model) {
449 case TLSModel::GeneralDynamic:
450 O << "@TLSGD";
451 break;
452 case TLSModel::LocalDynamic:
453 // O << "@TLSLD"; // local dynamic not implemented
454 O << "@TLSGD";
455 break;
456 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000457 if (Subtarget->is64Bit()) {
458 assert (!NotRIPRel);
459 O << "@GOTTPOFF(%rip)";
460 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000461 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000462 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000463 break;
464 case TLSModel::LocalExec:
465 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000466 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000467 else
468 O << "@NTPOFF";
469 break;
470 default:
471 assert (0 && "Unknown TLS model");
472 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000474 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
476 O << "@GOT";
477 else
478 O << "@GOTOFF";
Dan Gohmane254f322009-03-14 02:33:41 +0000479 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel) {
480 if (TM.getRelocationModel() != Reloc::Static) {
481 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
482 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483
Dan Gohmane254f322009-03-14 02:33:41 +0000484 if (needCloseParen) {
485 needCloseParen = false;
486 O << ')';
487 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 }
489
490 // Use rip when possible to reduce code size, except when
491 // index or base register are also part of the address. e.g.
492 // foo(%rip)(%rcx,%rax,4) is not legal
493 O << "(%rip)";
494 }
495 }
496
497 if (needCloseParen)
498 O << ')';
499
500 return;
501 }
502 case MachineOperand::MO_ExternalSymbol: {
503 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman96f096e2009-04-23 00:57:37 +0000504 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 bool needCloseParen = false;
506 std::string Name(TAI->getGlobalPrefix());
507 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000508 // Print function stub suffix unless it's Mac OS X 10.5 and up.
509 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
510 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000512 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 return;
514 }
Dan Gohman96f096e2009-04-23 00:57:37 +0000515 if (!isMemOp && !isCallOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 O << '$';
517 else if (Name[0] == '$') {
518 // The name begins with a dollar-sign. In order to avoid having it look
519 // like an integer immediate to the assembler, enclose it in parens.
520 O << '(';
521 needCloseParen = true;
522 }
523
524 O << Name;
525
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000526 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 std::string GOTName(TAI->getGlobalPrefix());
528 GOTName+="_GLOBAL_OFFSET_TABLE_";
529 if (Name == GOTName)
530 // HACK! Emit extra offset to PC during printing GOT offset to
531 // compensate for the size of popl instruction. The resulting code
532 // should look like:
533 // call .piclabel
534 // piclabel:
535 // popl %some_register
536 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
537 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000538 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540 if (isCallOp)
541 O << "@PLT";
542 }
543
544 if (needCloseParen)
545 O << ')';
546
547 if (!isCallOp && Subtarget->isPICStyleRIPRel())
548 O << "(%rip)";
549
550 return;
551 }
552 default:
553 O << "<unknown operand type>"; return;
554 }
555}
556
557void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000558 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 assert(value <= 7 && "Invalid ssecc argument!");
560 switch (value) {
561 case 0: O << "eq"; break;
562 case 1: O << "lt"; break;
563 case 2: O << "le"; break;
564 case 3: O << "unord"; break;
565 case 4: O << "neq"; break;
566 case 5: O << "nlt"; break;
567 case 6: O << "nle"; break;
568 case 7: O << "ord"; break;
569 }
570}
571
Rafael Espindolabca99f72009-04-08 21:14:34 +0000572void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
573 const char *Modifier){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 MachineOperand BaseReg = MI->getOperand(Op);
575 MachineOperand IndexReg = MI->getOperand(Op+2);
576 const MachineOperand &DispSpec = MI->getOperand(Op+3);
577
578 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000579 if (DispSpec.isGlobal() ||
580 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000581 DispSpec.isJTI() ||
582 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 printOperand(MI, Op+3, "mem", NotRIPRel);
584 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000585 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
587 O << DispVal;
588 }
589
590 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000591 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000593
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 // There are cases where we can end up with ESP/RSP in the indexreg slot.
595 // If this happens, swap the base/index register to support assemblers that
596 // don't work when the index is *SP.
597 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
598 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
599 std::swap(BaseReg, IndexReg);
600 std::swap(BaseRegOperand, IndexRegOperand);
601 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000602
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000603 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 if (BaseReg.getReg())
605 printOperand(MI, Op+BaseRegOperand, Modifier);
606
607 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000608 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 printOperand(MI, Op+IndexRegOperand, Modifier);
610 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000611 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000613 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 }
615}
616
Rafael Espindolabca99f72009-04-08 21:14:34 +0000617void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
618 const char *Modifier){
619 assert(isMem(MI, Op) && "Invalid memory reference!");
620 MachineOperand Segment = MI->getOperand(Op+4);
621 if (Segment.getReg()) {
622 printOperand(MI, Op+4, Modifier);
623 O << ':';
624 }
625 printLeaMemReference(MI, Op, Modifier);
626}
627
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000628void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000629 const MachineBasicBlock *MBB) const {
630 if (!TAI->getSetDirective())
631 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000632
633 // We don't need .set machinery if we have GOT-style relocations
634 if (Subtarget->isPICStyleGOT())
635 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000636
Evan Cheng6fb06762007-11-09 01:32:10 +0000637 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
638 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000639 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000640 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000641 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000642 << '_' << uid << '\n';
643 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000644 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000645}
646
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000648 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000649 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650}
651
652
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000653void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
654 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000655 unsigned uid) const
656{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000657 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
658 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
659
660 O << JTEntryDirective << ' ';
661
662 if (TM.getRelocationModel() == Reloc::PIC_) {
663 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
664 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
665 << '_' << uid << "_set_" << MBB->getNumber();
666 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000667 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000668 O << "@GOTOFF";
669 } else
670 assert(0 && "Don't know how to print MBB label for this PIC mode");
671 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000672 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000673}
674
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000675bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 unsigned Reg = MO.getReg();
678 switch (Mode) {
679 default: return true; // Unknown mode.
680 case 'b': // Print QImode register
681 Reg = getX86SubSuperRegister(Reg, MVT::i8);
682 break;
683 case 'h': // Print QImode high register
684 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
685 break;
686 case 'w': // Print HImode register
687 Reg = getX86SubSuperRegister(Reg, MVT::i16);
688 break;
689 case 'k': // Print SImode register
690 Reg = getX86SubSuperRegister(Reg, MVT::i32);
691 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000692 case 'q': // Print DImode register
693 Reg = getX86SubSuperRegister(Reg, MVT::i64);
694 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
696
Evan Cheng00d04a72008-07-07 22:21:06 +0000697 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 return false;
699}
700
701/// PrintAsmOperand - Print out an operand for an inline asm expression.
702///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000703bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000704 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 const char *ExtraCode) {
706 // Does this asm operand have a single letter operand modifier?
707 if (ExtraCode && ExtraCode[0]) {
708 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 switch (ExtraCode[0]) {
711 default: return true; // Unknown modifier.
712 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000713 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 return false;
715 case 'b': // Print QImode register
716 case 'h': // Print QImode high register
717 case 'w': // Print HImode register
718 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000719 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000720 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
722 printOperand(MI, OpNo);
723 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000724
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 case 'P': // Don't print @PLT, but do print as memory.
726 printOperand(MI, OpNo, "mem");
727 return false;
728 }
729 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 printOperand(MI, OpNo);
732 return false;
733}
734
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000735bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000737 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000739 if (ExtraCode && ExtraCode[0]) {
740 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000741
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000742 switch (ExtraCode[0]) {
743 default: return true; // Unknown modifier.
744 case 'b': // Print QImode register
745 case 'h': // Print QImode high register
746 case 'w': // Print HImode register
747 case 'k': // Print SImode register
748 case 'q': // Print SImode register
749 // These only apply to registers, ignore on mem.
750 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000751 case 'P': // Don't print @PLT, but do print as memory.
752 printOperand(MI, OpNo, "mem");
753 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000754 }
755 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 printMemReference(MI, OpNo);
757 return false;
758}
759
Bill Wendlingb3b11262009-02-06 21:45:08 +0000760/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
761/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762///
763void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
764 ++EmittedInsts;
765
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 // Call the autogenerated instruction printer routines.
767 printInstruction(MI);
768}
769
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000770/// doInitialization
771bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000772
Evan Cheng5cda7762008-07-09 06:36:53 +0000773 bool Result = AsmPrinter::doInitialization(M);
774
Dale Johannesen06545922008-07-09 20:55:35 +0000775 if (TAI->doesSupportDebugInformation()) {
776 // Let PassManager know we need debug information and relay
777 // the MachineModuleInfo address on to DwarfWriter.
778 // AsmPrinter::doInitialization did this analysis.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000779 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
780 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000781 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000782 }
783
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000784 // Darwin wants symbols to be quoted if they have complex names.
785 if (Subtarget->isTargetDarwin())
786 Mang->setUseQuotes(true);
787
788 return Result;
789}
790
791
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000793 const TargetData *TD = TM.getTargetData();
794
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 if (!GVar->hasInitializer())
796 return; // External global require no code
797
798 // Check to see if this is a special global used by LLVM, if so, emit it.
799 if (EmitSpecialLLVMGlobal(GVar)) {
800 if (Subtarget->isTargetDarwin() &&
801 TM.getRelocationModel() == Reloc::Static) {
802 if (GVar->getName() == "llvm.global_ctors")
803 O << ".reference .constructors_used\n";
804 else if (GVar->getName() == "llvm.global_dtors")
805 O << ".reference .destructors_used\n";
806 }
807 return;
808 }
809
810 std::string name = Mang->getValueName(GVar);
811 Constant *C = GVar->getInitializer();
812 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000813 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000814 unsigned Align = TD->getPreferredAlignmentLog(GVar);
815
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000816 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817
818 if (Subtarget->isTargetELF())
819 O << "\t.type\t" << name << ",@object\n";
820
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000821 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000822
Evan Chengcf84b142009-02-18 02:19:52 +0000823 if (C->isNullValue() && !GVar->hasSection() &&
824 !(Subtarget->isTargetDarwin() &&
825 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000826 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 if (GVar->hasExternalLinkage()) {
828 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 return;
833 }
834 }
835
836 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000837 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000839
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000841 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 O << "\t.globl " << name << '\n'
847 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000849 O << name << ":";
850 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000851 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000852 PrintUnmangledNameSafely(GVar, O);
853 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000854 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 EmitGlobalConstant(C);
856 return;
857 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000859 if (TAI->getCOMMDirectiveTakesAlignment())
860 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 }
862 } else {
863 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000864 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000865 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000867 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000868 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000869 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000870 }
Evan Cheng11db8142009-03-24 00:17:40 +0000871 if (VerboseAsm) {
872 O << "\t\t" << TAI->getCommentString() << ' ';
873 PrintUnmangledNameSafely(GVar, O);
874 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000875 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876 return;
877 }
878 }
879
880 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000881 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000882 case GlobalValue::LinkOnceAnyLinkage:
883 case GlobalValue::LinkOnceODRLinkage:
884 case GlobalValue::WeakAnyLinkage:
885 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 O << "\t.globl " << name << '\n'
888 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000889 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000894 }
895 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000896 case GlobalValue::DLLExportLinkage:
897 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898 // FIXME: appending linkage variables should go into a section of
899 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000900 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000901 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000902 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000903 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000904 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000905 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000906 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000907 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000908 assert(0 && "Unknown linkage type!");
909 }
910
911 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000912 O << name << ":";
913 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000914 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000915 PrintUnmangledNameSafely(GVar, O);
916 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000917 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000918 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000919 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000920
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000921 EmitGlobalConstant(C);
922}
923
Evan Cheng76443dc2008-07-08 00:55:58 +0000924/// printGVStub - Print stub for a global value.
925///
926void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000927 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000928 O << ":\n\t.indirect_symbol ";
929 if (Prefix) O << Prefix;
930 O << GV << "\n\t.long\t0\n";
931}
932
Evan Chenga65854f2008-12-05 01:06:39 +0000933/// printHiddenGVStub - Print stub for a hidden global value.
934///
935void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
936 EmitAlignment(2);
937 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
938 if (Prefix) O << Prefix;
939 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
940}
941
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000942
943bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944 // Print out module-level global variables here.
945 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000946 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000947 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000949 if (I->hasDLLExportLinkage())
950 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000951
952 // If the global is a extern weak symbol, remember to emit the weak
953 // reference!
954 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
955 // not used. But currently it's the only way to deal with extern weak
956 // initializers hidden deep inside constant expressions.
957 if (I->hasExternalWeakLinkage())
958 ExtWeakSymbols.insert(I);
959 }
960
961 for (Module::const_iterator I = M.begin(), E = M.end();
962 I != E; ++I) {
963 // If the global is a extern weak symbol, remember to emit the weak
964 // reference!
965 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
966 // not used. But currently it's the only way to deal with extern weak
967 // initializers hidden deep inside constant expressions.
968 if (I->hasExternalWeakLinkage())
969 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000970 }
971
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000972 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000973 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000974 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000975
976 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
977 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000978 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000979 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000980
981 if (!DLLExportedFns.empty()) {
982 SwitchToDataSection(".section .drectve");
983 }
984
985 for (StringSet<>::iterator i = DLLExportedFns.begin(),
986 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000987 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000988 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000989
990 if (Subtarget->isTargetDarwin()) {
991 SwitchToDataSection("");
992
993 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000994 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000995 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000996 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
997 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000998 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000999 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001000 O << ":\n"
1001 "\t.indirect_symbol " << p << "\n"
1002 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001003 }
1004
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001005 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001006
Evan Cheng76443dc2008-07-08 00:55:58 +00001007 // Print global value stubs.
1008 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001009 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1010 // Add the (possibly multiple) personalities to the set of global values.
1011 // Only referenced functions get into the Personalities list.
1012 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001013 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +00001014 E = Personalities.end(); I != E; ++I) {
1015 if (!*I)
1016 continue;
1017 if (!InStubSection) {
1018 SwitchToDataSection(
1019 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1020 InStubSection = true;
1021 }
1022 printGVStub((*I)->getNameStart(), "_");
1023 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001024 }
1025
1026 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +00001027 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001028 SwitchToDataSection(
1029 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1030 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001031 i != e; ++i)
1032 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001033
Evan Chenga65854f2008-12-05 01:06:39 +00001034 if (!HiddenGVStubs.empty()) {
1035 SwitchToSection(TAI->getDataSection());
1036 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1037 i != e; ++i)
1038 printHiddenGVStub(i->getKeyData());
1039 }
1040
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001041 // 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 // Funny Darwin hack: This flag tells the linker that no global symbols
1046 // contain code that falls through to other global symbols (e.g. the obvious
1047 // implementation of multiple entry points). If this doesn't occur, the
1048 // linker can safely perform dead code stripping. Since LLVM never
1049 // generates code that does this, it is always safe to set.
1050 O << "\t.subsections_via_symbols\n";
1051 } else if (Subtarget->isTargetCygMing()) {
1052 // Emit type information for external functions
1053 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1054 i != e; ++i) {
1055 O << "\t.def\t " << i->getKeyData()
1056 << ";\t.scl\t" << COFF::C_EXT
1057 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1058 << ";\t.endef\n";
1059 }
1060
1061 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001062 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001063 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001064 } else if (Subtarget->isTargetELF()) {
1065 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001066 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001067 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001068 }
1069
1070 return AsmPrinter::doFinalization(M);
1071}
1072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073// Include the auto-generated portion of the assembly writer.
1074#include "X86GenAsmWriter.inc"