blob: b97462918dd788a60db6c770c71236bac41155af [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"
Devang Patelf667ab42009-06-25 00:47:42 +000026#include "llvm/MDNode.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000027#include "llvm/Type.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000030#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000031#include "llvm/MC/MCInst.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000032#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000033#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000035#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000037#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetAsmInfo.h"
39#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
Chris Lattner19b1bd52009-06-19 00:47:33 +000044static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
45 cl::Hidden);
46
Chris Lattner2e845952009-06-24 19:44:36 +000047//===----------------------------------------------------------------------===//
48// Primitive Helper Functions.
49//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000050
51void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000053 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000055 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 else
57 assert(0 && "Don't know how to print PIC label!\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
Chris Lattner2e845952009-06-24 19:44:36 +000060/// PrintUnmangledNameSafely - Print out the printable characters in the name.
61/// Don't print things like \\n or \\0.
62static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
63 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
64 Name != E; ++Name)
65 if (isprint(*Name))
66 OS << *Name;
67}
Chris Lattner14f791a2009-06-24 19:19:16 +000068
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000069static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
70 const TargetData *TD) {
71 X86MachineFunctionInfo Info;
72 uint64_t Size = 0;
73
74 switch (F->getCallingConv()) {
75 case CallingConv::X86_StdCall:
76 Info.setDecorationStyle(StdCall);
77 break;
78 case CallingConv::X86_FastCall:
79 Info.setDecorationStyle(FastCall);
80 break;
81 default:
82 return Info;
83 }
84
85 unsigned argNum = 1;
86 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
87 AI != AE; ++AI, ++argNum) {
88 const Type* Ty = AI->getType();
89
90 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000091 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000092 Ty = cast<PointerType>(Ty)->getElementType();
93
94 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000095 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000096 }
97
98 // We're not supporting tooooo huge arguments :)
99 Info.setBytesToPopOnReturn((unsigned int)Size);
100 return Info;
101}
102
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000103/// decorateName - Query FunctionInfoMap and use this information for various
104/// name decoration.
105void X86ATTAsmPrinter::decorateName(std::string &Name,
106 const GlobalValue *GV) {
107 const Function *F = dyn_cast<Function>(GV);
108 if (!F) return;
109
110 // We don't want to decorate non-stdcall or non-fastcall functions right now
111 unsigned CC = F->getCallingConv();
112 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
113 return;
114
115 // Decorate names only when we're targeting Cygwin/Mingw32 targets
116 if (!Subtarget->isTargetCygMing())
117 return;
118
119 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
120
121 const X86MachineFunctionInfo *Info;
122 if (info_item == FunctionInfoMap.end()) {
123 // Calculate apropriate function info and populate map
124 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
125 Info = &FunctionInfoMap[F];
126 } else {
127 Info = &info_item->second;
128 }
129
130 const FunctionType *FT = F->getFunctionType();
131 switch (Info->getDecorationStyle()) {
132 case None:
133 break;
134 case StdCall:
135 // "Pure" variadic functions do not receive @0 suffix.
136 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
137 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
138 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
139 break;
140 case FastCall:
141 // "Pure" variadic functions do not receive @0 suffix.
142 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
143 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
144 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
145
146 if (Name[0] == '_') {
147 Name[0] = '@';
148 } else {
149 Name = '@' + Name;
150 }
151 break;
152 default:
153 assert(0 && "Unsupported DecorationStyle");
154 }
155}
156
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000157void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000158 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000161 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000163 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 switch (F->getLinkage()) {
165 default: assert(0 && "Unknown linkage type!");
166 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000167 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000175 case Function::LinkOnceAnyLinkage:
176 case Function::LinkOnceODRLinkage:
177 case Function::WeakAnyLinkage:
178 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000179 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << '\n';
182 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << CurrentFnName << "\n"
185 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 }
189 break;
190 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000191
192 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000195 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 else if (Subtarget->isTargetCygMing()) {
197 O << "\t.def\t " << CurrentFnName
198 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000199 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
201 << ";\t.endef\n";
202 }
203
204 O << CurrentFnName << ":\n";
205 // Add some workaround for linkonce linkage on Cygwin\MinGW
206 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000207 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000209}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000211/// runOnMachineFunction - This uses the printMachineInstruction()
212/// method to print assembly for each instruction.
213///
214bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
215 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000216 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217 unsigned CC = F->getCallingConv();
218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219 SetupMachineFunction(MF);
220 O << "\n\n";
221
222 // Populate function information map. Actually, We don't want to populate
223 // non-stdcall or non-fastcall functions' information right now.
224 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
225 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
226
227 // Print out constants referenced by the function
228 EmitConstantPool(MF.getConstantPool());
229
230 if (F->hasDLLExportLinkage())
231 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
232
233 // Print the 'header' of function
234 emitFunctionHeader(MF);
235
236 // Emit pre-function debug and/or EH information.
237 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000238 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000241 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
243 I != E; ++I) {
244 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000245 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
246 // This is an entry block or a block that's only reachable via a
247 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
248 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000249 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 O << '\n';
251 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000252 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
253 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000255 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000256 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 printMachineInstruction(II);
258 }
259 }
260
Dale Johannesenf35771f2008-04-08 00:37:56 +0000261 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
262 // If the function is empty, then we need to emit *something*. Otherwise,
263 // the function's label might be associated with something that it wasn't
264 // meant to be associated with. We emit a noop in this situation.
265 // We are assuming inline asms are code.
266 O << "\tnop\n";
267 }
268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000270 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000272 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000273 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000274 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275
276 // Print out jump tables referenced by the function.
277 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000278
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000279 O.flush();
280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 // We didn't modify anything.
282 return false;
283}
284
Chris Lattner357a0ca2009-06-20 19:34:09 +0000285/// print_pcrel_imm - This is used to print an immediate value that ends up
286/// being encoded as a pc-relative value. These print slightly differently, for
287/// example, a $ is not emitted.
288void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
289 const MachineOperand &MO = MI->getOperand(OpNo);
290 switch (MO.getType()) {
291 default: assert(0 && "Unknown pcrel immediate operand");
292 case MachineOperand::MO_Immediate:
293 O << MO.getImm();
294 return;
295 case MachineOperand::MO_MachineBasicBlock:
296 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
297 return;
298
299 case MachineOperand::MO_GlobalAddress: {
300 const GlobalValue *GV = MO.getGlobal();
301 std::string Name = Mang->getValueName(GV);
302 decorateName(Name, GV);
303
304 bool needCloseParen = false;
305 if (Name[0] == '$') {
306 // The name begins with a dollar-sign. In order to avoid having it look
307 // like an integer immediate to the assembler, enclose it in parens.
308 O << '(';
309 needCloseParen = true;
310 }
311
Chris Lattner48837612009-07-09 05:27:35 +0000312 // Handle dllimport linkage.
313 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
314 O << "__imp_";
315
316 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
317 FnStubs.insert(Name);
318 printSuffixedName(Name, "$stub");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000319 } else {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000320 O << Name;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000321 }
322
Chris Lattner48837612009-07-09 05:27:35 +0000323 // Assemble call via PLT for externally visible symbols.
324 if (MO.getTargetFlags() == X86II::MO_PLT)
325 O << "@PLT";
326
327 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
328 // Save function name for later type emission
329 CygMingStubs.insert(Name);
330
Chris Lattner357a0ca2009-06-20 19:34:09 +0000331 printOffset(MO.getOffset());
332
333 if (needCloseParen)
334 O << ')';
335 return;
336 }
337
338 case MachineOperand::MO_ExternalSymbol: {
339 bool needCloseParen = false;
340 std::string Name(TAI->getGlobalPrefix());
341 Name += MO.getSymbolName();
Chris Lattner48837612009-07-09 05:27:35 +0000342
Chris Lattner357a0ca2009-06-20 19:34:09 +0000343 if (Name[0] == '$') {
344 // The name begins with a dollar-sign. In order to avoid having it look
345 // like an integer immediate to the assembler, enclose it in parens.
346 O << '(';
347 needCloseParen = true;
348 }
349
Chris Lattner48837612009-07-09 05:27:35 +0000350 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
351 FnStubs.insert(Name);
352 printSuffixedName(Name, "$stub");
353 } else {
354 O << Name;
355 }
Chris Lattner357a0ca2009-06-20 19:34:09 +0000356
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000357 if (MO.getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) {
358 O << " + [.-";
359 PrintPICBaseSymbol();
360 O << ']';
Chris Lattner357a0ca2009-06-20 19:34:09 +0000361 }
362
Chris Lattner8e8afe42009-07-09 05:02:21 +0000363 if (MO.getTargetFlags() == X86II::MO_PLT)
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000364 O << "@PLT";
365
Chris Lattner357a0ca2009-06-20 19:34:09 +0000366 if (needCloseParen)
367 O << ')';
368
369 return;
370 }
371 }
372}
373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000375 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 switch (MO.getType()) {
Chris Lattner797a0782009-06-27 05:46:24 +0000378 default: assert(0 && "unknown operand type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000380 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 "Virtual registers should not make it this far!");
382 O << '%';
383 unsigned Reg = MO.getReg();
384 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000385 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
387 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
388 Reg = getX86SubSuperRegister(Reg, VT);
389 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000390 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 return;
392 }
393
394 case MachineOperand::MO_Immediate:
Chris Lattner85dbcd52009-07-09 00:39:19 +0000395 if (!Modifier || (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000397 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 case MachineOperand::MO_JumpTableIndex: {
Chris Lattner85dbcd52009-07-09 00:39:19 +0000400 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000402 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000403 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000404 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 }
406 case MachineOperand::MO_ConstantPoolIndex: {
Chris Lattner85dbcd52009-07-09 00:39:19 +0000407 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000409 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000410 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411
Chris Lattner40f56902009-06-26 20:00:05 +0000412 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000413 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 }
415 case MachineOperand::MO_GlobalAddress: {
Chris Lattner85dbcd52009-07-09 00:39:19 +0000416 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000417 const GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000419 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000420
Chris Lattner797a0782009-06-27 05:46:24 +0000421 bool needCloseParen = false;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000422 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 O << '$';
424 else if (Name[0] == '$') {
425 // The name begins with a dollar-sign. In order to avoid having it look
426 // like an integer immediate to the assembler, enclose it in parens.
427 O << '(';
428 needCloseParen = true;
429 }
430
Chris Lattner0cc2b792009-07-09 05:42:07 +0000431 // Handle dllimport linkage.
432 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
433 O << "__imp_";
434
Chris Lattner13a04722009-07-09 04:43:12 +0000435 if (Subtarget->isPICStyleStub()) {
Chris Lattner50315f22009-06-25 17:58:52 +0000436 // DARWIN/X86-32 in != static mode.
437
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 // Link-once, declaration, or Weakly-linked global variables need
439 // non-lazily-resolved stubs
Chris Lattnerebcb9242009-07-09 05:55:04 +0000440 if (!GV->isDeclaration() && !GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 O << Name;
Chris Lattnerebcb9242009-07-09 05:55:04 +0000442 } else if (!GV->hasHiddenVisibility()) {
443 GVStubs.insert(Name);
444 printSuffixedName(Name, "$non_lazy_ptr");
445 //assert(MO.getTargetFlags() == 0 || MO_PIC_BASE_OFFSET);
446 } else if (!GV->isDeclaration() && !GV->hasCommonLinkage())
447 // Definition is not definitely in the current translation unit.
448 O << Name;
449 else {
450 HiddenGVStubs.insert(Name);
451 printSuffixedName(Name, "$non_lazy_ptr");
452 //assert(MO.getTargetFlags() == 0 || MO_PIC_BASE_OFFSET);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
457
Chris Lattnerdabfe572009-06-21 02:22:53 +0000458 if (needCloseParen)
459 O << ')';
460
Chris Lattner0cc2b792009-07-09 05:42:07 +0000461 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000462 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
Chris Lattner797a0782009-06-27 05:46:24 +0000464 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerce003822009-06-26 21:47:27 +0000465 /// NOTE: MO_ExternalSymbol in a non-pcrel_imm context is *only* generated
466 /// by _GLOBAL_OFFSET_TABLE_ on X86-32. All others are call operands, which
467 /// are pcrel_imm's.
Chris Lattner0cc2b792009-07-09 05:42:07 +0000468 assert(!Subtarget->is64Bit());
Chris Lattnerce003822009-06-26 21:47:27 +0000469 // These are never used as memory operands.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000470 assert(Modifier == 0 || strcmp(Modifier, "mem"));
Chris Lattnerce003822009-06-26 21:47:27 +0000471 O << '$';
472 O << TAI->getGlobalPrefix();
473 O << MO.getSymbolName();
Chris Lattner797a0782009-06-27 05:46:24 +0000474 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 }
Chris Lattner797a0782009-06-27 05:46:24 +0000476
477 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 default:
Chris Lattner797a0782009-06-27 05:46:24 +0000479 assert(0 && "Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000480 case X86II::MO_NO_FLAG: // No flag.
481 case X86II::MO_DLLIMPORT: // Prefix, not a suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000482 break;
483 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
484 O << " + [.-";
485 PrintPICBaseSymbol();
486 O << ']';
487 break;
488 case X86II::MO_PIC_BASE_OFFSET:
489 O << '-';
490 PrintPICBaseSymbol();
491 break;
492 case X86II::MO_TLSGD: O << "@TLSGD"; break;
493 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
494 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
495 case X86II::MO_TPOFF: O << "@TPOFF"; break;
496 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
497 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
498 case X86II::MO_GOT: O << "@GOT"; break;
499 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 }
501}
502
503void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000504 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 assert(value <= 7 && "Invalid ssecc argument!");
506 switch (value) {
507 case 0: O << "eq"; break;
508 case 1: O << "lt"; break;
509 case 2: O << "le"; break;
510 case 3: O << "unord"; break;
511 case 4: O << "neq"; break;
512 case 5: O << "nlt"; break;
513 case 6: O << "nle"; break;
514 case 7: O << "ord"; break;
515 }
516}
517
Rafael Espindolabca99f72009-04-08 21:14:34 +0000518void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000519 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000520 const MachineOperand &BaseReg = MI->getOperand(Op);
521 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 const MachineOperand &DispSpec = MI->getOperand(Op+3);
523
Chris Lattner791fd482009-07-09 00:27:29 +0000524 // If we really don't want to print out (rip), don't.
525 bool HasBaseReg = BaseReg.getReg() != 0;
526 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
527 BaseReg.getReg() == X86::RIP)
528 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000529
530 // HasParenPart - True if we will print out the () part of the mem ref.
531 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
532
533 if (DispSpec.isImm()) {
534 int DispVal = DispSpec.getImm();
535 if (DispVal || !HasParenPart)
536 O << DispVal;
537 } else {
538 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
539 DispSpec.isJTI() || DispSpec.isSymbol());
540 printOperand(MI, Op+3, "mem");
541 }
542
543 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000544 assert(IndexReg.getReg() != X86::ESP &&
545 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000546
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000547 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000548 if (HasBaseReg)
549 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550
551 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000552 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000553 printOperand(MI, Op+2, Modifier);
554 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000556 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000558 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 }
560}
561
Rafael Espindolabca99f72009-04-08 21:14:34 +0000562void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000563 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000564 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000565 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000566 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000567 printOperand(MI, Op+4, Modifier);
568 O << ':';
569 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000570 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000571}
572
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000573void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000574 const MachineBasicBlock *MBB) const {
575 if (!TAI->getSetDirective())
576 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000577
578 // We don't need .set machinery if we have GOT-style relocations
579 if (Subtarget->isPICStyleGOT())
580 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000581
Evan Cheng6fb06762007-11-09 01:32:10 +0000582 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
583 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000584 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000585 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000586 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000587 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000588 else {
589 O << '-';
590 PrintPICBaseSymbol();
591 O << '\n';
592 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000593}
594
Chris Lattner14f791a2009-06-24 19:19:16 +0000595
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000597 PrintPICBaseSymbol();
598 O << '\n';
599 PrintPICBaseSymbol();
600 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601}
602
603
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000604void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
605 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000606 unsigned uid) const
607{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000608 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
609 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
610
611 O << JTEntryDirective << ' ';
612
613 if (TM.getRelocationModel() == Reloc::PIC_) {
614 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
615 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
616 << '_' << uid << "_set_" << MBB->getNumber();
617 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000618 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000619 O << "@GOTOFF";
620 } else
621 assert(0 && "Don't know how to print MBB label for this PIC mode");
622 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000623 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000624}
625
Chris Lattner8bb96e42009-06-15 04:42:32 +0000626bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 unsigned Reg = MO.getReg();
628 switch (Mode) {
629 default: return true; // Unknown mode.
630 case 'b': // Print QImode register
631 Reg = getX86SubSuperRegister(Reg, MVT::i8);
632 break;
633 case 'h': // Print QImode high register
634 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
635 break;
636 case 'w': // Print HImode register
637 Reg = getX86SubSuperRegister(Reg, MVT::i16);
638 break;
639 case 'k': // Print SImode register
640 Reg = getX86SubSuperRegister(Reg, MVT::i32);
641 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000642 case 'q': // Print DImode register
643 Reg = getX86SubSuperRegister(Reg, MVT::i64);
644 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 }
646
Evan Cheng00d04a72008-07-07 22:21:06 +0000647 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 return false;
649}
650
651/// PrintAsmOperand - Print out an operand for an inline asm expression.
652///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000653bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000654 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 const char *ExtraCode) {
656 // Does this asm operand have a single letter operand modifier?
657 if (ExtraCode && ExtraCode[0]) {
658 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000659
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 switch (ExtraCode[0]) {
661 default: return true; // Unknown modifier.
662 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000663 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 return false;
665 case 'b': // Print QImode register
666 case 'h': // Print QImode high register
667 case 'w': // Print HImode register
668 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000669 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000670 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
672 printOperand(MI, OpNo);
673 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000674
Dale Johannesen375b88b2009-07-07 23:28:22 +0000675 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000676 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000678
Dale Johannesen375b88b2009-07-07 23:28:22 +0000679 case 'n': { // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000680 // Note: this is a temporary solution. It should be handled target
681 // independently as part of the 'MC' work.
682 const MachineOperand &MO = MI->getOperand(OpNo);
683 if (MO.isImm()) {
684 O << -MO.getImm();
685 return false;
686 }
687 O << '-';
688 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 }
690 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 printOperand(MI, OpNo);
693 return false;
694}
695
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000696bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000698 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000700 if (ExtraCode && ExtraCode[0]) {
701 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000702
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000703 switch (ExtraCode[0]) {
704 default: return true; // Unknown modifier.
705 case 'b': // Print QImode register
706 case 'h': // Print QImode high register
707 case 'w': // Print HImode register
708 case 'k': // Print SImode register
709 case 'q': // Print SImode register
710 // These only apply to registers, ignore on mem.
711 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000712 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000713 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000714 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000715 }
716 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 printMemReference(MI, OpNo);
718 return false;
719}
720
Chris Lattnerf5da5902009-06-20 07:03:18 +0000721static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
722 // Convert registers in the addr mode according to subreg64.
723 for (unsigned i = 0; i != 4; ++i) {
724 if (!MI->getOperand(i).isReg()) continue;
725
726 unsigned Reg = MI->getOperand(i).getReg();
727 if (Reg == 0) continue;
728
729 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
730 }
731}
732
Bill Wendlingb3b11262009-02-06 21:45:08 +0000733/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
734/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735///
736void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
737 ++EmittedInsts;
738
Chris Lattner19b1bd52009-06-19 00:47:33 +0000739 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000740 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
741 O << "\t";
742 printInlineAsm(MI);
743 return;
744 } else if (MI->isLabel()) {
745 printLabel(MI);
746 return;
747 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
748 printDeclare(MI);
749 return;
750 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
751 printImplicitDef(MI);
752 return;
753 }
754
Chris Lattnere67184e2009-06-19 23:59:57 +0000755 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000756 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000757
758 TmpInst.setOpcode(MI->getOpcode());
759
760 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
761 const MachineOperand &MO = MI->getOperand(i);
762
763 MCOperand MCOp;
764 if (MO.isReg()) {
765 MCOp.MakeReg(MO.getReg());
766 } else if (MO.isImm()) {
767 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000768 } else if (MO.isMBB()) {
769 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000770 } else {
771 assert(0 && "Unimp");
772 }
773
774 TmpInst.addOperand(MCOp);
775 }
776
Chris Lattner6b9f7742009-06-20 07:59:10 +0000777 switch (TmpInst.getOpcode()) {
778 case X86::LEA64_32r:
779 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000780 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000781 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000782 }
783
Chris Lattner19b1bd52009-06-19 00:47:33 +0000784 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000785 printInstruction(&TmpInst);
786 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000787 }
788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 // Call the autogenerated instruction printer routines.
790 printInstruction(MI);
791}
792
Devang Patelc20079e2009-06-20 01:00:07 +0000793/// doInitialization
794bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000795 if (NewAsmPrinter) {
796 Context = new MCContext();
797 // FIXME: Send this to "O" instead of outs(). For now, we force it to
798 // stdout to make it easy to compare.
799 Streamer = createAsmStreamer(*Context, outs());
800 }
801
Devang Patelc20079e2009-06-20 01:00:07 +0000802 return AsmPrinter::doInitialization(M);
803}
804
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000806 const TargetData *TD = TM.getTargetData();
807
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (!GVar->hasInitializer())
809 return; // External global require no code
810
811 // Check to see if this is a special global used by LLVM, if so, emit it.
812 if (EmitSpecialLLVMGlobal(GVar)) {
813 if (Subtarget->isTargetDarwin() &&
814 TM.getRelocationModel() == Reloc::Static) {
815 if (GVar->getName() == "llvm.global_ctors")
816 O << ".reference .constructors_used\n";
817 else if (GVar->getName() == "llvm.global_dtors")
818 O << ".reference .destructors_used\n";
819 }
820 return;
821 }
822
823 std::string name = Mang->getValueName(GVar);
824 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000825 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000826 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000828 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 unsigned Align = TD->getPreferredAlignmentLog(GVar);
830
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000831 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832
833 if (Subtarget->isTargetELF())
834 O << "\t.type\t" << name << ",@object\n";
835
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000836 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000837
Evan Chengcf84b142009-02-18 02:19:52 +0000838 if (C->isNullValue() && !GVar->hasSection() &&
839 !(Subtarget->isTargetDarwin() &&
840 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000841 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000842 if (GVar->hasExternalLinkage()) {
843 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000847 return;
848 }
849 }
850
851 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000852 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000854
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000856 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000857 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000859 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000861 O << "\t.globl " << name << '\n'
862 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000864 O << name << ":";
865 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000866 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000867 PrintUnmangledNameSafely(GVar, O);
868 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000869 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000870 EmitGlobalConstant(C);
871 return;
872 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000874 if (TAI->getCOMMDirectiveTakesAlignment())
875 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876 }
877 } else {
878 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000879 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000880 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000881 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000882 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000884 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000885 }
Evan Cheng11db8142009-03-24 00:17:40 +0000886 if (VerboseAsm) {
887 O << "\t\t" << TAI->getCommentString() << ' ';
888 PrintUnmangledNameSafely(GVar, O);
889 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000890 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000891 return;
892 }
893 }
894
895 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000896 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000897 case GlobalValue::LinkOnceAnyLinkage:
898 case GlobalValue::LinkOnceODRLinkage:
899 case GlobalValue::WeakAnyLinkage:
900 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000901 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000902 O << "\t.globl " << name << '\n'
903 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000904 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000905 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000906 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000907 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000908 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000909 }
910 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000911 case GlobalValue::DLLExportLinkage:
912 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000913 // FIXME: appending linkage variables should go into a section of
914 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000915 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000916 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000917 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000918 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000919 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000920 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000921 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000922 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000923 assert(0 && "Unknown linkage type!");
924 }
925
926 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000927 O << name << ":";
928 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000929 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000930 PrintUnmangledNameSafely(GVar, O);
931 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000932 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000933 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000934 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000935
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000936 EmitGlobalConstant(C);
937}
938
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000939bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000940 // Print out module-level global variables here.
941 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000942 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000943 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000945 if (I->hasDLLExportLinkage())
946 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000947 }
948
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000949 if (Subtarget->isTargetDarwin()) {
950 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000951
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000952 // Add the (possibly multiple) personalities to the set of global value
953 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000954 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000955 const std::vector<Function*> &Personalities = MMI->getPersonalities();
956 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
957 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +0000958 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000959 std::string Name = Mang->getValueName(Personalities[i]);
960 decorateName(Name, Personalities[i]);
961 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +0000962 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000963 }
964
Chris Lattner2e860262009-06-24 18:24:09 +0000965 // Output stubs for dynamically-linked functions
966 if (!FnStubs.empty()) {
967 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
968 I != E; ++I) {
969 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
970 "self_modifying_code+pure_instructions,5", 0);
971 const char *Name = I->getKeyData();
972 printSuffixedName(Name, "$stub");
973 O << ":\n"
974 "\t.indirect_symbol " << Name << "\n"
975 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
976 }
977 O << '\n';
978 }
979
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000980 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000981 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000982 SwitchToDataSection(
983 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +0000984 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
985 I != E; ++I) {
986 const char *Name = I->getKeyData();
987 printSuffixedName(Name, "$non_lazy_ptr");
988 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
989 }
990 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000991
Evan Chenga65854f2008-12-05 01:06:39 +0000992 if (!HiddenGVStubs.empty()) {
993 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000994 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +0000995 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
996 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +0000997 const char *Name = I->getKeyData();
998 printSuffixedName(Name, "$non_lazy_ptr");
999 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1000 }
Evan Chenga65854f2008-12-05 01:06:39 +00001001 }
1002
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001003 // Funny Darwin hack: This flag tells the linker that no global symbols
1004 // contain code that falls through to other global symbols (e.g. the obvious
1005 // implementation of multiple entry points). If this doesn't occur, the
1006 // linker can safely perform dead code stripping. Since LLVM never
1007 // generates code that does this, it is always safe to set.
1008 O << "\t.subsections_via_symbols\n";
1009 } else if (Subtarget->isTargetCygMing()) {
1010 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001011 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001012 i != e; ++i) {
1013 O << "\t.def\t " << i->getKeyData()
1014 << ";\t.scl\t" << COFF::C_EXT
1015 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1016 << ";\t.endef\n";
1017 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001018 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001019
Chris Lattnerdb191f02009-06-24 18:52:01 +00001020
1021 // Output linker support code for dllexported globals on windows.
1022 if (!DLLExportedGVs.empty()) {
1023 SwitchToDataSection(".section .drectve");
1024
1025 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1026 e = DLLExportedGVs.end(); i != e; ++i)
1027 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1028 }
1029
1030 if (!DLLExportedFns.empty()) {
1031 SwitchToDataSection(".section .drectve");
1032
1033 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1034 e = DLLExportedFns.end();
1035 i != e; ++i)
1036 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1037 }
1038
Chris Lattnerdb191f02009-06-24 18:52:01 +00001039 // Do common shutdown.
1040 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001041
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001042 if (NewAsmPrinter) {
1043 Streamer->Finish();
1044
1045 delete Streamer;
1046 delete Context;
1047 Streamer = 0;
1048 Context = 0;
1049 }
1050
Chris Lattnerdb191f02009-06-24 18:52:01 +00001051 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001052}
1053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054// Include the auto-generated portion of the assembly writer.
1055#include "X86GenAsmWriter.inc"