blob: 4cd332bda20619825063c30f34b99f56bee0ec21 [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
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000110 // Save function name for later type emission.
111 if (Subtarget->isTargetCygMing() && F->isDeclaration())
112 CygMingStubs.insert(Name);
113
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000114 // We don't want to decorate non-stdcall or non-fastcall functions right now
115 unsigned CC = F->getCallingConv();
116 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
117 return;
118
119 // Decorate names only when we're targeting Cygwin/Mingw32 targets
120 if (!Subtarget->isTargetCygMing())
121 return;
122
123 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
124
125 const X86MachineFunctionInfo *Info;
126 if (info_item == FunctionInfoMap.end()) {
127 // Calculate apropriate function info and populate map
128 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
129 Info = &FunctionInfoMap[F];
130 } else {
131 Info = &info_item->second;
132 }
133
134 const FunctionType *FT = F->getFunctionType();
135 switch (Info->getDecorationStyle()) {
136 case None:
137 break;
138 case StdCall:
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 break;
144 case FastCall:
145 // "Pure" variadic functions do not receive @0 suffix.
146 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
147 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
148 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
149
150 if (Name[0] == '_') {
151 Name[0] = '@';
152 } else {
153 Name = '@' + Name;
154 }
155 break;
156 default:
157 assert(0 && "Unsupported DecorationStyle");
158 }
159}
160
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000161void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000162 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000165 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000167 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 switch (F->getLinkage()) {
169 default: assert(0 && "Unknown linkage type!");
170 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000171 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 break;
174 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000179 case Function::LinkOnceAnyLinkage:
180 case Function::LinkOnceODRLinkage:
181 case Function::WeakAnyLinkage:
182 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000183 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << "\t.globl\t" << CurrentFnName << '\n';
186 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << "\n"
189 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000191 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193 break;
194 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000195
196 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000199 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 else if (Subtarget->isTargetCygMing()) {
201 O << "\t.def\t " << CurrentFnName
202 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000203 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
205 << ";\t.endef\n";
206 }
207
208 O << CurrentFnName << ":\n";
209 // Add some workaround for linkonce linkage on Cygwin\MinGW
210 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000211 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000213}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000215/// runOnMachineFunction - This uses the printMachineInstruction()
216/// method to print assembly for each instruction.
217///
218bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
219 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000220 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000221 unsigned CC = F->getCallingConv();
222
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000223 SetupMachineFunction(MF);
224 O << "\n\n";
225
226 // Populate function information map. Actually, We don't want to populate
227 // non-stdcall or non-fastcall functions' information right now.
228 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
229 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
230
231 // Print out constants referenced by the function
232 EmitConstantPool(MF.getConstantPool());
233
234 if (F->hasDLLExportLinkage())
235 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
236
237 // Print the 'header' of function
238 emitFunctionHeader(MF);
239
240 // Emit pre-function debug and/or EH information.
241 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000242 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000245 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
247 I != E; ++I) {
248 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000249 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
250 // This is an entry block or a block that's only reachable via a
251 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
252 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000253 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 O << '\n';
255 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000256 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
257 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000259 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000260 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 printMachineInstruction(II);
262 }
263 }
264
Dale Johannesenf35771f2008-04-08 00:37:56 +0000265 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
266 // If the function is empty, then we need to emit *something*. Otherwise,
267 // the function's label might be associated with something that it wasn't
268 // meant to be associated with. We emit a noop in this situation.
269 // We are assuming inline asms are code.
270 O << "\tnop\n";
271 }
272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000274 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000276 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000277 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000278 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
280 // Print out jump tables referenced by the function.
281 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000282
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000283 O.flush();
284
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 // We didn't modify anything.
286 return false;
287}
288
Chris Lattner357a0ca2009-06-20 19:34:09 +0000289/// print_pcrel_imm - This is used to print an immediate value that ends up
290/// being encoded as a pc-relative value. These print slightly differently, for
291/// example, a $ is not emitted.
292void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
293 const MachineOperand &MO = MI->getOperand(OpNo);
294 switch (MO.getType()) {
295 default: assert(0 && "Unknown pcrel immediate operand");
296 case MachineOperand::MO_Immediate:
297 O << MO.getImm();
298 return;
299 case MachineOperand::MO_MachineBasicBlock:
300 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
301 return;
302
303 case MachineOperand::MO_GlobalAddress: {
304 const GlobalValue *GV = MO.getGlobal();
305 std::string Name = Mang->getValueName(GV);
306 decorateName(Name, GV);
307
308 bool needCloseParen = false;
309 if (Name[0] == '$') {
310 // The name begins with a dollar-sign. In order to avoid having it look
311 // like an integer immediate to the assembler, enclose it in parens.
312 O << '(';
313 needCloseParen = true;
314 }
315
Chris Lattner48837612009-07-09 05:27:35 +0000316 // Handle dllimport linkage.
317 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner877a0bc2009-07-10 07:50:52 +0000318 O << "__imp_" << Name;
319 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner48837612009-07-09 05:27:35 +0000320 FnStubs.insert(Name);
321 printSuffixedName(Name, "$stub");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000322 } else {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000323 O << Name;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000324 }
325
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000326 if (needCloseParen)
327 O << ')';
328
Chris Lattner48837612009-07-09 05:27:35 +0000329 // Assemble call via PLT for externally visible symbols.
330 if (MO.getTargetFlags() == X86II::MO_PLT)
331 O << "@PLT";
332
Chris Lattner357a0ca2009-06-20 19:34:09 +0000333 printOffset(MO.getOffset());
334
Chris Lattner357a0ca2009-06-20 19:34:09 +0000335 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 Lattner64b54552009-07-10 22:34:11 +0000395 if (!Modifier || 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");
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000417 if (!isMemOp)
418 O << '$';
419
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000420 const GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000422 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000423
Chris Lattner797a0782009-06-27 05:46:24 +0000424 bool needCloseParen = false;
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000425 if (Name[0] == '$') {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 // The name begins with a dollar-sign. In order to avoid having it look
427 // like an integer immediate to the assembler, enclose it in parens.
428 O << '(';
429 needCloseParen = true;
430 }
431
Chris Lattner0cc2b792009-07-09 05:42:07 +0000432 // Handle dllimport linkage.
Chris Lattnera3bde622009-07-09 06:59:17 +0000433 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) {
434 O << "__imp_" << Name;
435 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
436 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
437 GVStubs.insert(Name);
438 printSuffixedName(Name, "$non_lazy_ptr");
439 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
440 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
441 HiddenGVStubs.insert(Name);
442 printSuffixedName(Name, "$non_lazy_ptr");
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000443 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
444 FnStubs.insert(Name);
445 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 }
449
Chris Lattnerdabfe572009-06-21 02:22:53 +0000450 if (needCloseParen)
451 O << ')';
452
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000453 // Assemble call via PLT for externally visible symbols.
454 if (MO.getTargetFlags() == X86II::MO_PLT)
455 O << "@PLT";
456
Chris Lattner0cc2b792009-07-09 05:42:07 +0000457 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000458 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 }
Chris Lattner797a0782009-06-27 05:46:24 +0000460 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerce003822009-06-26 21:47:27 +0000461 /// NOTE: MO_ExternalSymbol in a non-pcrel_imm context is *only* generated
462 /// by _GLOBAL_OFFSET_TABLE_ on X86-32. All others are call operands, which
463 /// are pcrel_imm's.
Chris Lattner0cc2b792009-07-09 05:42:07 +0000464 assert(!Subtarget->is64Bit());
Chris Lattnerce003822009-06-26 21:47:27 +0000465 // These are never used as memory operands.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000466 assert(Modifier == 0 || strcmp(Modifier, "mem"));
Chris Lattnerce003822009-06-26 21:47:27 +0000467 O << '$';
468 O << TAI->getGlobalPrefix();
469 O << MO.getSymbolName();
Chris Lattner797a0782009-06-27 05:46:24 +0000470 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 }
Chris Lattner797a0782009-06-27 05:46:24 +0000472
473 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 default:
Chris Lattner797a0782009-06-27 05:46:24 +0000475 assert(0 && "Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000476 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000477 break;
478 case X86II::MO_DARWIN_NONLAZY:
479 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
480 case X86II::MO_DLLIMPORT:
481 // These affect the name of the symbol, not any 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:
Chris Lattnera3bde622009-07-09 06:59:17 +0000489 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
490 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000491 O << '-';
492 PrintPICBaseSymbol();
493 break;
494 case X86II::MO_TLSGD: O << "@TLSGD"; break;
495 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
496 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
497 case X86II::MO_TPOFF: O << "@TPOFF"; break;
498 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
499 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
500 case X86II::MO_GOT: O << "@GOT"; break;
501 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 }
503}
504
505void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000506 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 assert(value <= 7 && "Invalid ssecc argument!");
508 switch (value) {
509 case 0: O << "eq"; break;
510 case 1: O << "lt"; break;
511 case 2: O << "le"; break;
512 case 3: O << "unord"; break;
513 case 4: O << "neq"; break;
514 case 5: O << "nlt"; break;
515 case 6: O << "nle"; break;
516 case 7: O << "ord"; break;
517 }
518}
519
Rafael Espindolabca99f72009-04-08 21:14:34 +0000520void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000521 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000522 const MachineOperand &BaseReg = MI->getOperand(Op);
523 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 const MachineOperand &DispSpec = MI->getOperand(Op+3);
525
Chris Lattner791fd482009-07-09 00:27:29 +0000526 // If we really don't want to print out (rip), don't.
527 bool HasBaseReg = BaseReg.getReg() != 0;
528 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
529 BaseReg.getReg() == X86::RIP)
530 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000531
532 // HasParenPart - True if we will print out the () part of the mem ref.
533 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
534
535 if (DispSpec.isImm()) {
536 int DispVal = DispSpec.getImm();
537 if (DispVal || !HasParenPart)
538 O << DispVal;
539 } else {
540 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
541 DispSpec.isJTI() || DispSpec.isSymbol());
542 printOperand(MI, Op+3, "mem");
543 }
544
545 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000546 assert(IndexReg.getReg() != X86::ESP &&
547 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000548
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000549 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000550 if (HasBaseReg)
551 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552
553 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000554 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000555 printOperand(MI, Op+2, Modifier);
556 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000558 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000560 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 }
562}
563
Rafael Espindolabca99f72009-04-08 21:14:34 +0000564void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000565 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000566 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000567 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000568 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000569 printOperand(MI, Op+4, Modifier);
570 O << ':';
571 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000572 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000573}
574
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000575void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000576 const MachineBasicBlock *MBB) const {
577 if (!TAI->getSetDirective())
578 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000579
580 // We don't need .set machinery if we have GOT-style relocations
581 if (Subtarget->isPICStyleGOT())
582 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000583
Evan Cheng6fb06762007-11-09 01:32:10 +0000584 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
585 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000586 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000587 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000588 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000589 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000590 else {
591 O << '-';
592 PrintPICBaseSymbol();
593 O << '\n';
594 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000595}
596
Chris Lattner14f791a2009-06-24 19:19:16 +0000597
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000599 PrintPICBaseSymbol();
600 O << '\n';
601 PrintPICBaseSymbol();
602 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603}
604
605
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000606void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
607 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000608 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000609 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
610 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
611
612 O << JTEntryDirective << ' ';
613
Chris Lattner2e9393c2009-07-10 21:00:45 +0000614 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000615 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
616 << '_' << uid << "_set_" << MBB->getNumber();
617 } else if (Subtarget->isPICStyleGOT()) {
618 printBasicBlockLabel(MBB, false, false, false);
619 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000620 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000621 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000622}
623
Chris Lattner8bb96e42009-06-15 04:42:32 +0000624bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 unsigned Reg = MO.getReg();
626 switch (Mode) {
627 default: return true; // Unknown mode.
628 case 'b': // Print QImode register
629 Reg = getX86SubSuperRegister(Reg, MVT::i8);
630 break;
631 case 'h': // Print QImode high register
632 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
633 break;
634 case 'w': // Print HImode register
635 Reg = getX86SubSuperRegister(Reg, MVT::i16);
636 break;
637 case 'k': // Print SImode register
638 Reg = getX86SubSuperRegister(Reg, MVT::i32);
639 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000640 case 'q': // Print DImode register
641 Reg = getX86SubSuperRegister(Reg, MVT::i64);
642 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 }
644
Evan Cheng00d04a72008-07-07 22:21:06 +0000645 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 return false;
647}
648
649/// PrintAsmOperand - Print out an operand for an inline asm expression.
650///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000651bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000652 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 const char *ExtraCode) {
654 // Does this asm operand have a single letter operand modifier?
655 if (ExtraCode && ExtraCode[0]) {
656 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 switch (ExtraCode[0]) {
659 default: return true; // Unknown modifier.
660 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000661 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000663
664 case 'A': // Print '*' before a register (it must be a register)
665 if (MI->getOperand(OpNo).isReg()) {
666 O << '*';
667 printOperand(MI, OpNo);
668 return false;
669 }
670 return true;
671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 case 'b': // Print QImode register
673 case 'h': // Print QImode high register
674 case 'w': // Print HImode register
675 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000676 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000677 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
679 printOperand(MI, OpNo);
680 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000681
Dale Johannesen375b88b2009-07-07 23:28:22 +0000682 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000683 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000685
Dale Johannesen375b88b2009-07-07 23:28:22 +0000686 case 'n': { // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000687 // Note: this is a temporary solution. It should be handled target
688 // independently as part of the 'MC' work.
689 const MachineOperand &MO = MI->getOperand(OpNo);
690 if (MO.isImm()) {
691 O << -MO.getImm();
692 return false;
693 }
694 O << '-';
695 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 }
697 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000698
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 printOperand(MI, OpNo);
700 return false;
701}
702
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000703bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000705 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000707 if (ExtraCode && ExtraCode[0]) {
708 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000709
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000710 switch (ExtraCode[0]) {
711 default: return true; // Unknown modifier.
712 case 'b': // Print QImode register
713 case 'h': // Print QImode high register
714 case 'w': // Print HImode register
715 case 'k': // Print SImode register
716 case 'q': // Print SImode register
717 // These only apply to registers, ignore on mem.
718 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000719 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000720 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000721 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000722 }
723 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 printMemReference(MI, OpNo);
725 return false;
726}
727
Chris Lattnerf5da5902009-06-20 07:03:18 +0000728static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
729 // Convert registers in the addr mode according to subreg64.
730 for (unsigned i = 0; i != 4; ++i) {
731 if (!MI->getOperand(i).isReg()) continue;
732
733 unsigned Reg = MI->getOperand(i).getReg();
734 if (Reg == 0) continue;
735
736 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
737 }
738}
739
Bill Wendlingb3b11262009-02-06 21:45:08 +0000740/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
741/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742///
743void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
744 ++EmittedInsts;
745
Chris Lattner19b1bd52009-06-19 00:47:33 +0000746 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000747 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
748 O << "\t";
749 printInlineAsm(MI);
750 return;
751 } else if (MI->isLabel()) {
752 printLabel(MI);
753 return;
754 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
755 printDeclare(MI);
756 return;
757 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
758 printImplicitDef(MI);
759 return;
760 }
761
Chris Lattnere67184e2009-06-19 23:59:57 +0000762 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000763 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000764
765 TmpInst.setOpcode(MI->getOpcode());
766
767 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
768 const MachineOperand &MO = MI->getOperand(i);
769
770 MCOperand MCOp;
771 if (MO.isReg()) {
772 MCOp.MakeReg(MO.getReg());
773 } else if (MO.isImm()) {
774 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000775 } else if (MO.isMBB()) {
776 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000777 } else {
778 assert(0 && "Unimp");
779 }
780
781 TmpInst.addOperand(MCOp);
782 }
783
Chris Lattner6b9f7742009-06-20 07:59:10 +0000784 switch (TmpInst.getOpcode()) {
785 case X86::LEA64_32r:
786 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000787 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000788 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000789 }
790
Chris Lattner19b1bd52009-06-19 00:47:33 +0000791 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000792 printInstruction(&TmpInst);
793 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000794 }
795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 // Call the autogenerated instruction printer routines.
797 printInstruction(MI);
798}
799
Devang Patelc20079e2009-06-20 01:00:07 +0000800/// doInitialization
801bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000802 if (NewAsmPrinter) {
803 Context = new MCContext();
804 // FIXME: Send this to "O" instead of outs(). For now, we force it to
805 // stdout to make it easy to compare.
806 Streamer = createAsmStreamer(*Context, outs());
807 }
808
Devang Patelc20079e2009-06-20 01:00:07 +0000809 return AsmPrinter::doInitialization(M);
810}
811
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000813 const TargetData *TD = TM.getTargetData();
814
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 if (!GVar->hasInitializer())
816 return; // External global require no code
817
818 // Check to see if this is a special global used by LLVM, if so, emit it.
819 if (EmitSpecialLLVMGlobal(GVar)) {
820 if (Subtarget->isTargetDarwin() &&
821 TM.getRelocationModel() == Reloc::Static) {
822 if (GVar->getName() == "llvm.global_ctors")
823 O << ".reference .constructors_used\n";
824 else if (GVar->getName() == "llvm.global_dtors")
825 O << ".reference .destructors_used\n";
826 }
827 return;
828 }
829
830 std::string name = Mang->getValueName(GVar);
831 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000832 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000833 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000835 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 unsigned Align = TD->getPreferredAlignmentLog(GVar);
837
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000838 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839
840 if (Subtarget->isTargetELF())
841 O << "\t.type\t" << name << ",@object\n";
842
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000843 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000844
Evan Chengcf84b142009-02-18 02:19:52 +0000845 if (C->isNullValue() && !GVar->hasSection() &&
846 !(Subtarget->isTargetDarwin() &&
847 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000848 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 if (GVar->hasExternalLinkage()) {
850 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 return;
855 }
856 }
857
858 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000859 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000861
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000862 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000863 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000864 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000868 O << "\t.globl " << name << '\n'
869 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000870 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000871 O << name << ":";
872 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000873 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000874 PrintUnmangledNameSafely(GVar, O);
875 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000876 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000877 EmitGlobalConstant(C);
878 return;
879 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000880 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000881 if (TAI->getCOMMDirectiveTakesAlignment())
882 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 }
884 } else {
885 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000886 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000888 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 }
Evan Cheng11db8142009-03-24 00:17:40 +0000893 if (VerboseAsm) {
894 O << "\t\t" << TAI->getCommentString() << ' ';
895 PrintUnmangledNameSafely(GVar, O);
896 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000897 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898 return;
899 }
900 }
901
902 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000903 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000904 case GlobalValue::LinkOnceAnyLinkage:
905 case GlobalValue::LinkOnceODRLinkage:
906 case GlobalValue::WeakAnyLinkage:
907 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000908 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000909 O << "\t.globl " << name << '\n'
910 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000911 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000912 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000913 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000914 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000915 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000916 }
917 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000918 case GlobalValue::DLLExportLinkage:
919 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000920 // FIXME: appending linkage variables should go into a section of
921 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000922 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000923 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000924 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000925 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000926 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000927 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000928 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000929 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000930 assert(0 && "Unknown linkage type!");
931 }
932
933 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000934 O << name << ":";
935 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000936 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000937 PrintUnmangledNameSafely(GVar, O);
938 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000939 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000940 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000941 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000942
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000943 EmitGlobalConstant(C);
944}
945
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000946bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000947 // Print out module-level global variables here.
948 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000949 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000950 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000951
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000952 if (I->hasDLLExportLinkage())
953 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000954 }
955
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000956 if (Subtarget->isTargetDarwin()) {
957 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000958
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000959 // Add the (possibly multiple) personalities to the set of global value
960 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000961 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000962 const std::vector<Function*> &Personalities = MMI->getPersonalities();
963 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
964 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +0000965 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000966 std::string Name = Mang->getValueName(Personalities[i]);
967 decorateName(Name, Personalities[i]);
968 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +0000969 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000970 }
971
Chris Lattner2e860262009-06-24 18:24:09 +0000972 // Output stubs for dynamically-linked functions
973 if (!FnStubs.empty()) {
974 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
975 I != E; ++I) {
976 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
977 "self_modifying_code+pure_instructions,5", 0);
978 const char *Name = I->getKeyData();
979 printSuffixedName(Name, "$stub");
980 O << ":\n"
981 "\t.indirect_symbol " << Name << "\n"
982 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
983 }
984 O << '\n';
985 }
986
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000987 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000988 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000989 SwitchToDataSection(
990 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +0000991 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
992 I != E; ++I) {
993 const char *Name = I->getKeyData();
994 printSuffixedName(Name, "$non_lazy_ptr");
995 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
996 }
997 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000998
Evan Chenga65854f2008-12-05 01:06:39 +0000999 if (!HiddenGVStubs.empty()) {
1000 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001001 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001002 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1003 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001004 const char *Name = I->getKeyData();
1005 printSuffixedName(Name, "$non_lazy_ptr");
1006 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1007 }
Evan Chenga65854f2008-12-05 01:06:39 +00001008 }
1009
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001010 // Funny Darwin hack: This flag tells the linker that no global symbols
1011 // contain code that falls through to other global symbols (e.g. the obvious
1012 // implementation of multiple entry points). If this doesn't occur, the
1013 // linker can safely perform dead code stripping. Since LLVM never
1014 // generates code that does this, it is always safe to set.
1015 O << "\t.subsections_via_symbols\n";
1016 } else if (Subtarget->isTargetCygMing()) {
1017 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001018 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001019 i != e; ++i) {
1020 O << "\t.def\t " << i->getKeyData()
1021 << ";\t.scl\t" << COFF::C_EXT
1022 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1023 << ";\t.endef\n";
1024 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001025 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001026
Chris Lattnerdb191f02009-06-24 18:52:01 +00001027
1028 // Output linker support code for dllexported globals on windows.
1029 if (!DLLExportedGVs.empty()) {
1030 SwitchToDataSection(".section .drectve");
1031
1032 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1033 e = DLLExportedGVs.end(); i != e; ++i)
1034 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1035 }
1036
1037 if (!DLLExportedFns.empty()) {
1038 SwitchToDataSection(".section .drectve");
1039
1040 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1041 e = DLLExportedFns.end();
1042 i != e; ++i)
1043 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1044 }
1045
Chris Lattnerdb191f02009-06-24 18:52:01 +00001046 // Do common shutdown.
1047 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001048
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001049 if (NewAsmPrinter) {
1050 Streamer->Finish();
1051
1052 delete Streamer;
1053 delete Context;
1054 Streamer = 0;
1055 Context = 0;
1056 }
1057
Chris Lattnerdb191f02009-06-24 18:52:01 +00001058 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001059}
1060
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061// Include the auto-generated portion of the assembly writer.
1062#include "X86GenAsmWriter.inc"