blob: 7e48855e08d2974ea4e73259aae44297703dc02f [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"
Chris Lattner19b1bd52009-06-19 00:47:33 +000029#include "llvm/MC/MCInst.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000030#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000034#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
39STATISTIC(EmittedInsts, "Number of machine instrs printed");
40
Chris Lattner19b1bd52009-06-19 00:47:33 +000041static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
42 cl::Hidden);
43
Evan Cheng0729ccf2008-01-05 00:41:47 +000044static std::string getPICLabelString(unsigned FnNum,
45 const TargetAsmInfo *TAI,
46 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 std::string label;
48 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000049 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000051 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 else
53 assert(0 && "Don't know how to print PIC label!\n");
54
55 return label;
56}
57
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000058static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
59 const TargetData *TD) {
60 X86MachineFunctionInfo Info;
61 uint64_t Size = 0;
62
63 switch (F->getCallingConv()) {
64 case CallingConv::X86_StdCall:
65 Info.setDecorationStyle(StdCall);
66 break;
67 case CallingConv::X86_FastCall:
68 Info.setDecorationStyle(FastCall);
69 break;
70 default:
71 return Info;
72 }
73
74 unsigned argNum = 1;
75 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
76 AI != AE; ++AI, ++argNum) {
77 const Type* Ty = AI->getType();
78
79 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000080 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000081 Ty = cast<PointerType>(Ty)->getElementType();
82
83 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000084 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000085 }
86
87 // We're not supporting tooooo huge arguments :)
88 Info.setBytesToPopOnReturn((unsigned int)Size);
89 return Info;
90}
91
92/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000093/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000094static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000095 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
96 Name != E; ++Name)
97 if (isprint(*Name))
98 OS << *Name;
99}
100
101/// decorateName - Query FunctionInfoMap and use this information for various
102/// name decoration.
103void X86ATTAsmPrinter::decorateName(std::string &Name,
104 const GlobalValue *GV) {
105 const Function *F = dyn_cast<Function>(GV);
106 if (!F) return;
107
108 // We don't want to decorate non-stdcall or non-fastcall functions right now
109 unsigned CC = F->getCallingConv();
110 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
111 return;
112
113 // Decorate names only when we're targeting Cygwin/Mingw32 targets
114 if (!Subtarget->isTargetCygMing())
115 return;
116
117 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
118
119 const X86MachineFunctionInfo *Info;
120 if (info_item == FunctionInfoMap.end()) {
121 // Calculate apropriate function info and populate map
122 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
123 Info = &FunctionInfoMap[F];
124 } else {
125 Info = &info_item->second;
126 }
127
128 const FunctionType *FT = F->getFunctionType();
129 switch (Info->getDecorationStyle()) {
130 case None:
131 break;
132 case StdCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137 break;
138 case FastCall:
139 // "Pure" variadic functions do not receive @0 suffix.
140 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
141 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
142 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
143
144 if (Name[0] == '_') {
145 Name[0] = '@';
146 } else {
147 Name = '@' + Name;
148 }
149 break;
150 default:
151 assert(0 && "Unsupported DecorationStyle");
152 }
153}
154
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000155void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000158 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000160 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000161
Devang Patel93698d92008-10-01 23:18:38 +0000162 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000163 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000164 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 switch (F->getLinkage()) {
166 default: assert(0 && "Unknown linkage type!");
167 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000168 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000169 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000173 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000176 case Function::LinkOnceAnyLinkage:
177 case Function::LinkOnceODRLinkage:
178 case Function::WeakAnyLinkage:
179 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000180 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << CurrentFnName << '\n';
183 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << "\t.globl\t" << CurrentFnName << "\n"
186 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 break;
191 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000192
193 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000196 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 else if (Subtarget->isTargetCygMing()) {
198 O << "\t.def\t " << CurrentFnName
199 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000200 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
202 << ";\t.endef\n";
203 }
204
205 O << CurrentFnName << ":\n";
206 // Add some workaround for linkonce linkage on Cygwin\MinGW
207 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000208 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000210}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212/// runOnMachineFunction - This uses the printMachineInstruction()
213/// method to print assembly for each instruction.
214///
215bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
216 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000217 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218 unsigned CC = F->getCallingConv();
219
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000220 SetupMachineFunction(MF);
221 O << "\n\n";
222
223 // Populate function information map. Actually, We don't want to populate
224 // non-stdcall or non-fastcall functions' information right now.
225 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
226 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
227
228 // Print out constants referenced by the function
229 EmitConstantPool(MF.getConstantPool());
230
231 if (F->hasDLLExportLinkage())
232 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
233
234 // Print the 'header' of function
235 emitFunctionHeader(MF);
236
237 // Emit pre-function debug and/or EH information.
238 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000239 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000242 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
244 I != E; ++I) {
245 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000246 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
247 // This is an entry block or a block that's only reachable via a
248 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
249 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000250 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 O << '\n';
252 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000253 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
254 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000256 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000257 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 printMachineInstruction(II);
259 }
260 }
261
Dale Johannesenf35771f2008-04-08 00:37:56 +0000262 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
263 // If the function is empty, then we need to emit *something*. Otherwise,
264 // the function's label might be associated with something that it wasn't
265 // meant to be associated with. We emit a noop in this situation.
266 // We are assuming inline asms are code.
267 O << "\tnop\n";
268 }
269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000271 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000273 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000274 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000275 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
277 // Print out jump tables referenced by the function.
278 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000279
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000280 O.flush();
281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 // We didn't modify anything.
283 return false;
284}
285
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000286static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
288}
289
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000290static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
291 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
292 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
293}
294
295static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
297}
298
299void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
300 const char *Modifier, bool NotRIPRel) {
301 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 switch (MO.getType()) {
303 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000304 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 "Virtual registers should not make it this far!");
306 O << '%';
307 unsigned Reg = MO.getReg();
308 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000309 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
311 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
312 Reg = getX86SubSuperRegister(Reg, VT);
313 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000314 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return;
316 }
317
318 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000319 if (!Modifier || (strcmp(Modifier, "debug") &&
320 strcmp(Modifier, "mem") &&
321 strcmp(Modifier, "call")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000323 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 return;
325 case MachineOperand::MO_MachineBasicBlock:
Evan Cheng11db8142009-03-24 00:17:40 +0000326 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 return;
328 case MachineOperand::MO_JumpTableIndex: {
329 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
330 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000331 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000332 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
334 if (TM.getRelocationModel() == Reloc::PIC_) {
335 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000336 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
337 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 else if (Subtarget->isPICStyleGOT())
339 O << "@GOTOFF";
340 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
343 O << "(%rip)";
344 return;
345 }
346 case MachineOperand::MO_ConstantPoolIndex: {
347 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
348 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000349 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000350 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351
352 if (TM.getRelocationModel() == Reloc::PIC_) {
353 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000354 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
355 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 else if (Subtarget->isPICStyleGOT())
357 O << "@GOTOFF";
358 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000359
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000360 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
362 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
363 O << "(%rip)";
364 return;
365 }
366 case MachineOperand::MO_GlobalAddress: {
367 bool isCallOp = Modifier && !strcmp(Modifier, "call");
368 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
369 bool needCloseParen = false;
370
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000371 const GlobalValue *GV = MO.getGlobal();
372 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
373 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000374 // If GV is an alias then use the aliasee for determining
375 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000376 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000377 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000378 }
379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 bool isThreadLocal = GVar && GVar->isThreadLocal();
381
382 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000383 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 if (!isMemOp && !isCallOp)
386 O << '$';
387 else if (Name[0] == '$') {
388 // The name begins with a dollar-sign. In order to avoid having it look
389 // like an integer immediate to the assembler, enclose it in parens.
390 O << '(';
391 needCloseParen = true;
392 }
393
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000394 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 // Link-once, declaration, or Weakly-linked global variables need
396 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000397 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 // Dynamically-resolved functions need a stub for the function.
399 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000400 // Function stubs are no longer needed for Mac OS X 10.5 and up.
401 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
402 O << Name;
403 } else {
404 FnStubs.insert(Name);
405 printSuffixedName(Name, "$stub");
406 }
Evan Chenga65854f2008-12-05 01:06:39 +0000407 } else if (GV->hasHiddenVisibility()) {
408 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
409 // Definition is not definitely in the current translation unit.
410 O << Name;
411 else {
412 HiddenGVStubs.insert(Name);
413 printSuffixedName(Name, "$non_lazy_ptr");
414 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 } else {
416 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000417 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 }
419 } else {
420 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000421 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 O << Name;
423 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000424
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000426 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 } else {
428 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000429 O << "__imp_";
430 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 O << Name;
432
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000433 if (isCallOp) {
434 if (shouldPrintPLT(TM, Subtarget)) {
435 // Assemble call via PLT for externally visible symbols
436 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000437 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 O << "@PLT";
439 }
440 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
441 // Save function name for later type emission
442 FnStubs.insert(Name);
443 }
444 }
445
446 if (GV->hasExternalWeakLinkage())
447 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000448
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000449 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450
451 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000452 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
453 switch (model) {
454 case TLSModel::GeneralDynamic:
455 O << "@TLSGD";
456 break;
457 case TLSModel::LocalDynamic:
458 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000459 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000460 break;
461 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000462 if (Subtarget->is64Bit()) {
463 assert (!NotRIPRel);
464 O << "@GOTTPOFF(%rip)";
465 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000466 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000467 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000468 break;
469 case TLSModel::LocalExec:
470 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000471 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000472 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000473 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000474 break;
475 default:
476 assert (0 && "Unknown TLS model");
477 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000479 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
481 O << "@GOT";
482 else
483 O << "@GOTOFF";
Dan Gohmane254f322009-03-14 02:33:41 +0000484 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel) {
485 if (TM.getRelocationModel() != Reloc::Static) {
486 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
487 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488
Dan Gohmane254f322009-03-14 02:33:41 +0000489 if (needCloseParen) {
490 needCloseParen = false;
491 O << ')';
492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 }
494
495 // Use rip when possible to reduce code size, except when
496 // index or base register are also part of the address. e.g.
497 // foo(%rip)(%rcx,%rax,4) is not legal
498 O << "(%rip)";
499 }
500 }
501
502 if (needCloseParen)
503 O << ')';
504
505 return;
506 }
507 case MachineOperand::MO_ExternalSymbol: {
508 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman96f096e2009-04-23 00:57:37 +0000509 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 bool needCloseParen = false;
511 std::string Name(TAI->getGlobalPrefix());
512 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000513 // Print function stub suffix unless it's Mac OS X 10.5 and up.
514 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
515 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000517 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 return;
519 }
Dan Gohman96f096e2009-04-23 00:57:37 +0000520 if (!isMemOp && !isCallOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 O << '$';
522 else if (Name[0] == '$') {
523 // The name begins with a dollar-sign. In order to avoid having it look
524 // like an integer immediate to the assembler, enclose it in parens.
525 O << '(';
526 needCloseParen = true;
527 }
528
529 O << Name;
530
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000531 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 std::string GOTName(TAI->getGlobalPrefix());
533 GOTName+="_GLOBAL_OFFSET_TABLE_";
534 if (Name == GOTName)
535 // HACK! Emit extra offset to PC during printing GOT offset to
536 // compensate for the size of popl instruction. The resulting code
537 // should look like:
538 // call .piclabel
539 // piclabel:
540 // popl %some_register
541 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
542 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000543 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544
545 if (isCallOp)
546 O << "@PLT";
547 }
548
549 if (needCloseParen)
550 O << ')';
551
552 if (!isCallOp && Subtarget->isPICStyleRIPRel())
553 O << "(%rip)";
554
555 return;
556 }
557 default:
558 O << "<unknown operand type>"; return;
559 }
560}
561
562void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000563 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 assert(value <= 7 && "Invalid ssecc argument!");
565 switch (value) {
566 case 0: O << "eq"; break;
567 case 1: O << "lt"; break;
568 case 2: O << "le"; break;
569 case 3: O << "unord"; break;
570 case 4: O << "neq"; break;
571 case 5: O << "nlt"; break;
572 case 6: O << "nle"; break;
573 case 7: O << "ord"; break;
574 }
575}
576
Rafael Espindolabca99f72009-04-08 21:14:34 +0000577void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000578 const char *Modifier,
579 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 MachineOperand BaseReg = MI->getOperand(Op);
581 MachineOperand IndexReg = MI->getOperand(Op+2);
582 const MachineOperand &DispSpec = MI->getOperand(Op+3);
583
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000584 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000585 if (DispSpec.isGlobal() ||
586 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000587 DispSpec.isJTI() ||
588 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 printOperand(MI, Op+3, "mem", NotRIPRel);
590 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000591 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
593 O << DispVal;
594 }
595
596 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000597 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 // There are cases where we can end up with ESP/RSP in the indexreg slot.
601 // If this happens, swap the base/index register to support assemblers that
602 // don't work when the index is *SP.
603 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
604 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
605 std::swap(BaseReg, IndexReg);
606 std::swap(BaseRegOperand, IndexRegOperand);
607 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000608
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 if (BaseReg.getReg())
611 printOperand(MI, Op+BaseRegOperand, Modifier);
612
613 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000614 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 printOperand(MI, Op+IndexRegOperand, Modifier);
616 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000617 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000619 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 }
621}
622
Rafael Espindolabca99f72009-04-08 21:14:34 +0000623void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000624 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000625 assert(isMem(MI, Op) && "Invalid memory reference!");
626 MachineOperand Segment = MI->getOperand(Op+4);
627 if (Segment.getReg()) {
628 printOperand(MI, Op+4, Modifier);
629 O << ':';
630 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000631 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000632}
633
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000634void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000635 const MachineBasicBlock *MBB) const {
636 if (!TAI->getSetDirective())
637 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000638
639 // We don't need .set machinery if we have GOT-style relocations
640 if (Subtarget->isPICStyleGOT())
641 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000642
Evan Cheng6fb06762007-11-09 01:32:10 +0000643 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
644 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000645 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000646 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000647 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000648 << '_' << uid << '\n';
649 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000650 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000651}
652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000654 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000655 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656}
657
658
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000659void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
660 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000661 unsigned uid) const
662{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000663 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
664 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
665
666 O << JTEntryDirective << ' ';
667
668 if (TM.getRelocationModel() == Reloc::PIC_) {
669 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
670 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
671 << '_' << uid << "_set_" << MBB->getNumber();
672 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000673 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000674 O << "@GOTOFF";
675 } else
676 assert(0 && "Don't know how to print MBB label for this PIC mode");
677 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000678 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000679}
680
Chris Lattner8bb96e42009-06-15 04:42:32 +0000681bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 unsigned Reg = MO.getReg();
683 switch (Mode) {
684 default: return true; // Unknown mode.
685 case 'b': // Print QImode register
686 Reg = getX86SubSuperRegister(Reg, MVT::i8);
687 break;
688 case 'h': // Print QImode high register
689 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
690 break;
691 case 'w': // Print HImode register
692 Reg = getX86SubSuperRegister(Reg, MVT::i16);
693 break;
694 case 'k': // Print SImode register
695 Reg = getX86SubSuperRegister(Reg, MVT::i32);
696 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000697 case 'q': // Print DImode register
698 Reg = getX86SubSuperRegister(Reg, MVT::i64);
699 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 }
701
Evan Cheng00d04a72008-07-07 22:21:06 +0000702 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 return false;
704}
705
706/// PrintAsmOperand - Print out an operand for an inline asm expression.
707///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000708bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000709 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 const char *ExtraCode) {
711 // Does this asm operand have a single letter operand modifier?
712 if (ExtraCode && ExtraCode[0]) {
713 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 switch (ExtraCode[0]) {
716 default: return true; // Unknown modifier.
717 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000718 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 return false;
720 case 'b': // Print QImode register
721 case 'h': // Print QImode high register
722 case 'w': // Print HImode register
723 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000724 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000725 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
727 printOperand(MI, OpNo);
728 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000731 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 return false;
733 }
734 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000735
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 printOperand(MI, OpNo);
737 return false;
738}
739
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000740bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000742 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000744 if (ExtraCode && ExtraCode[0]) {
745 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000746
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000747 switch (ExtraCode[0]) {
748 default: return true; // Unknown modifier.
749 case 'b': // Print QImode register
750 case 'h': // Print QImode high register
751 case 'w': // Print HImode register
752 case 'k': // Print SImode register
753 case 'q': // Print SImode register
754 // These only apply to registers, ignore on mem.
755 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000756 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000757 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000758 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000759 }
760 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 printMemReference(MI, OpNo);
762 return false;
763}
764
Bill Wendlingb3b11262009-02-06 21:45:08 +0000765/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
766/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767///
768void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
769 ++EmittedInsts;
770
Chris Lattner19b1bd52009-06-19 00:47:33 +0000771 if (NewAsmPrinter) {
Chris Lattnere67184e2009-06-19 23:59:57 +0000772 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000773 MCInst TmpInst;
774 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000775 printInstruction(&TmpInst);
776 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000777 }
778
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 // Call the autogenerated instruction printer routines.
780 printInstruction(MI);
781}
782
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000784 const TargetData *TD = TM.getTargetData();
785
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000786 if (!GVar->hasInitializer())
787 return; // External global require no code
788
789 // Check to see if this is a special global used by LLVM, if so, emit it.
790 if (EmitSpecialLLVMGlobal(GVar)) {
791 if (Subtarget->isTargetDarwin() &&
792 TM.getRelocationModel() == Reloc::Static) {
793 if (GVar->getName() == "llvm.global_ctors")
794 O << ".reference .constructors_used\n";
795 else if (GVar->getName() == "llvm.global_dtors")
796 O << ".reference .destructors_used\n";
797 }
798 return;
799 }
800
801 std::string name = Mang->getValueName(GVar);
802 Constant *C = GVar->getInitializer();
803 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000804 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 unsigned Align = TD->getPreferredAlignmentLog(GVar);
806
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000807 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808
809 if (Subtarget->isTargetELF())
810 O << "\t.type\t" << name << ",@object\n";
811
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000812 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000813
Evan Chengcf84b142009-02-18 02:19:52 +0000814 if (C->isNullValue() && !GVar->hasSection() &&
815 !(Subtarget->isTargetDarwin() &&
816 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000817 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 if (GVar->hasExternalLinkage()) {
819 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 return;
824 }
825 }
826
827 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000828 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000830
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000831 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000832 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << "\t.globl " << name << '\n'
838 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000840 O << name << ":";
841 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000842 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000843 PrintUnmangledNameSafely(GVar, O);
844 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000845 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 EmitGlobalConstant(C);
847 return;
848 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000850 if (TAI->getCOMMDirectiveTakesAlignment())
851 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 }
853 } else {
854 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000855 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000856 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000860 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 }
Evan Cheng11db8142009-03-24 00:17:40 +0000862 if (VerboseAsm) {
863 O << "\t\t" << TAI->getCommentString() << ' ';
864 PrintUnmangledNameSafely(GVar, O);
865 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 return;
868 }
869 }
870
871 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000872 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000873 case GlobalValue::LinkOnceAnyLinkage:
874 case GlobalValue::LinkOnceODRLinkage:
875 case GlobalValue::WeakAnyLinkage:
876 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000877 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000878 O << "\t.globl " << name << '\n'
879 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000881 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000882 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000884 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000885 }
886 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000887 case GlobalValue::DLLExportLinkage:
888 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000889 // FIXME: appending linkage variables should go into a section of
890 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000891 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000894 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000895 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000896 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000897 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000898 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000899 assert(0 && "Unknown linkage type!");
900 }
901
902 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000903 O << name << ":";
904 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000905 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000906 PrintUnmangledNameSafely(GVar, O);
907 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000908 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000909 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000910 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000911
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000912 EmitGlobalConstant(C);
913}
914
Evan Cheng76443dc2008-07-08 00:55:58 +0000915/// printGVStub - Print stub for a global value.
916///
917void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000918 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000919 O << ":\n\t.indirect_symbol ";
920 if (Prefix) O << Prefix;
921 O << GV << "\n\t.long\t0\n";
922}
923
Evan Chenga65854f2008-12-05 01:06:39 +0000924/// printHiddenGVStub - Print stub for a hidden global value.
925///
926void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
927 EmitAlignment(2);
928 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
929 if (Prefix) O << Prefix;
930 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
931}
932
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000933
934bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000935 // Print out module-level global variables here.
936 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000937 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000938 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000939
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000940 if (I->hasDLLExportLinkage())
941 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000942
943 // If the global is a extern weak symbol, remember to emit the weak
944 // reference!
Mike Stump5dae8792009-05-14 23:22:47 +0000945 // FIXME: This is rather hacky, since we'll emit references to ALL weak
946 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000947 // initializers hidden deep inside constant expressions.
948 if (I->hasExternalWeakLinkage())
949 ExtWeakSymbols.insert(I);
950 }
951
952 for (Module::const_iterator I = M.begin(), E = M.end();
953 I != E; ++I) {
954 // If the global is a extern weak symbol, remember to emit the weak
955 // reference!
Mike Stump809b1092009-05-14 23:23:37 +0000956 // FIXME: This is rather hacky, since we'll emit references to ALL weak
957 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000958 // initializers hidden deep inside constant expressions.
959 if (I->hasExternalWeakLinkage())
960 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000961 }
962
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000963 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000964 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000965 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000966
967 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
968 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000969 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000970 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000971
972 if (!DLLExportedFns.empty()) {
973 SwitchToDataSection(".section .drectve");
974 }
975
976 for (StringSet<>::iterator i = DLLExportedFns.begin(),
977 e = DLLExportedFns.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() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000980
981 if (Subtarget->isTargetDarwin()) {
982 SwitchToDataSection("");
983
984 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000985 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000986 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000987 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
988 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000989 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000990 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000991 O << ":\n"
992 "\t.indirect_symbol " << p << "\n"
993 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000994 }
995
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000996 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000997
Evan Cheng76443dc2008-07-08 00:55:58 +0000998 // Print global value stubs.
999 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001000 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1001 // Add the (possibly multiple) personalities to the set of global values.
1002 // Only referenced functions get into the Personalities list.
1003 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001004 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +00001005 E = Personalities.end(); I != E; ++I) {
1006 if (!*I)
1007 continue;
1008 if (!InStubSection) {
1009 SwitchToDataSection(
1010 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1011 InStubSection = true;
1012 }
1013 printGVStub((*I)->getNameStart(), "_");
1014 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001015 }
1016
1017 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +00001018 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001019 SwitchToDataSection(
1020 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1021 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001022 i != e; ++i)
1023 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001024
Evan Chenga65854f2008-12-05 01:06:39 +00001025 if (!HiddenGVStubs.empty()) {
1026 SwitchToSection(TAI->getDataSection());
1027 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1028 i != e; ++i)
1029 printHiddenGVStub(i->getKeyData());
1030 }
1031
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001032 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001033 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001034 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001035
1036 // Funny Darwin hack: This flag tells the linker that no global symbols
1037 // contain code that falls through to other global symbols (e.g. the obvious
1038 // implementation of multiple entry points). If this doesn't occur, the
1039 // linker can safely perform dead code stripping. Since LLVM never
1040 // generates code that does this, it is always safe to set.
1041 O << "\t.subsections_via_symbols\n";
1042 } else if (Subtarget->isTargetCygMing()) {
1043 // Emit type information for external functions
1044 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1045 i != e; ++i) {
1046 O << "\t.def\t " << i->getKeyData()
1047 << ";\t.scl\t" << COFF::C_EXT
1048 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1049 << ";\t.endef\n";
1050 }
1051
1052 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001053 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001054 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001055 } else if (Subtarget->isTargetELF()) {
1056 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001057 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001058 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001059 }
1060
1061 return AsmPrinter::doFinalization(M);
1062}
1063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064// Include the auto-generated portion of the assembly writer.
1065#include "X86GenAsmWriter.inc"