blob: 127f228263f4be3620da8cd37891568177f4f183 [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 Lattner14f791a2009-06-24 19:19:16 +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
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000285static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
Chris Lattner50315f22009-06-25 17:58:52 +0000286 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_;
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000287}
288
289static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
291}
292
Chris Lattner357a0ca2009-06-20 19:34:09 +0000293/// print_pcrel_imm - This is used to print an immediate value that ends up
294/// being encoded as a pc-relative value. These print slightly differently, for
295/// example, a $ is not emitted.
296void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
297 const MachineOperand &MO = MI->getOperand(OpNo);
298 switch (MO.getType()) {
299 default: assert(0 && "Unknown pcrel immediate operand");
300 case MachineOperand::MO_Immediate:
301 O << MO.getImm();
302 return;
303 case MachineOperand::MO_MachineBasicBlock:
304 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
305 return;
306
307 case MachineOperand::MO_GlobalAddress: {
308 const GlobalValue *GV = MO.getGlobal();
309 std::string Name = Mang->getValueName(GV);
310 decorateName(Name, GV);
311
312 bool needCloseParen = false;
313 if (Name[0] == '$') {
314 // The name begins with a dollar-sign. In order to avoid having it look
315 // like an integer immediate to the assembler, enclose it in parens.
316 O << '(';
317 needCloseParen = true;
318 }
319
320 if (shouldPrintStub(TM, Subtarget)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000321 // DARWIN/X86-32 in != static mode.
322
Chris Lattner357a0ca2009-06-20 19:34:09 +0000323 // Link-once, declaration, or Weakly-linked global variables need
324 // non-lazily-resolved stubs
325 if (GV->isDeclaration() || GV->isWeakForLinker()) {
326 // Dynamically-resolved functions need a stub for the function.
327 if (isa<Function>(GV)) {
328 // Function stubs are no longer needed for Mac OS X 10.5 and up.
329 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
330 O << Name;
331 } else {
332 FnStubs.insert(Name);
333 printSuffixedName(Name, "$stub");
334 }
335 } else if (GV->hasHiddenVisibility()) {
336 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
337 // Definition is not definitely in the current translation unit.
338 O << Name;
339 else {
340 HiddenGVStubs.insert(Name);
341 printSuffixedName(Name, "$non_lazy_ptr");
342 }
343 } else {
344 GVStubs.insert(Name);
345 printSuffixedName(Name, "$non_lazy_ptr");
346 }
347 } else {
348 if (GV->hasDLLImportLinkage())
349 O << "__imp_";
350 O << Name;
351 }
352 } else {
Chris Lattner50315f22009-06-25 17:58:52 +0000353 if (GV->hasDLLImportLinkage())
Chris Lattner357a0ca2009-06-20 19:34:09 +0000354 O << "__imp_";
Chris Lattner357a0ca2009-06-20 19:34:09 +0000355 O << Name;
356
357 if (shouldPrintPLT(TM, Subtarget)) {
358 // Assemble call via PLT for externally visible symbols
359 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
360 !GV->hasLocalLinkage())
361 O << "@PLT";
362 }
363 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
364 // Save function name for later type emission
365 FnStubs.insert(Name);
366 }
367
Chris Lattner357a0ca2009-06-20 19:34:09 +0000368 printOffset(MO.getOffset());
369
370 if (needCloseParen)
371 O << ')';
372 return;
373 }
374
375 case MachineOperand::MO_ExternalSymbol: {
376 bool needCloseParen = false;
377 std::string Name(TAI->getGlobalPrefix());
378 Name += MO.getSymbolName();
379 // Print function stub suffix unless it's Mac OS X 10.5 and up.
380 if (shouldPrintStub(TM, Subtarget) &&
Chris Lattner50315f22009-06-25 17:58:52 +0000381 // DARWIN/X86-32 in != static mode.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000382 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000383
Chris Lattner357a0ca2009-06-20 19:34:09 +0000384 FnStubs.insert(Name);
385 printSuffixedName(Name, "$stub");
386 return;
387 }
388
389 if (Name[0] == '$') {
390 // The name begins with a dollar-sign. In order to avoid having it look
391 // like an integer immediate to the assembler, enclose it in parens.
392 O << '(';
393 needCloseParen = true;
394 }
395
396 O << Name;
397
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000398 if (MO.getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) {
399 O << " + [.-";
400 PrintPICBaseSymbol();
401 O << ']';
Chris Lattner357a0ca2009-06-20 19:34:09 +0000402 }
403
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000404 if (shouldPrintPLT(TM, Subtarget))
405 O << "@PLT";
406
Chris Lattner357a0ca2009-06-20 19:34:09 +0000407 if (needCloseParen)
408 O << ')';
409
410 return;
411 }
412 }
413}
414
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000416 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 switch (MO.getType()) {
Chris Lattner797a0782009-06-27 05:46:24 +0000419 default: assert(0 && "unknown operand type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000421 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 "Virtual registers should not make it this far!");
423 O << '%';
424 unsigned Reg = MO.getReg();
425 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000426 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
428 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
429 Reg = getX86SubSuperRegister(Reg, VT);
430 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000431 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 return;
433 }
434
435 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000436 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000437 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000439 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 case MachineOperand::MO_JumpTableIndex: {
442 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
443 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000444 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000445 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000446 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 }
448 case MachineOperand::MO_ConstantPoolIndex: {
449 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
450 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000451 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000452 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453
Chris Lattner40f56902009-06-26 20:00:05 +0000454 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000455 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
457 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000458 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000460 const GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000462 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000463
Chris Lattner797a0782009-06-27 05:46:24 +0000464 bool needCloseParen = false;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000465 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 O << '$';
467 else if (Name[0] == '$') {
468 // The name begins with a dollar-sign. In order to avoid having it look
469 // like an integer immediate to the assembler, enclose it in parens.
470 O << '(';
471 needCloseParen = true;
472 }
473
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000474 if (shouldPrintStub(TM, Subtarget)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000475 // DARWIN/X86-32 in != static mode.
476
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 // Link-once, declaration, or Weakly-linked global variables need
478 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000479 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000481 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000482 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
483 // Definition is not definitely in the current translation unit.
484 O << Name;
485 else {
486 HiddenGVStubs.insert(Name);
487 printSuffixedName(Name, "$non_lazy_ptr");
488 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 } else {
490 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000491 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493 } else {
494 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000495 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 O << Name;
497 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000498
Chris Lattner14f791a2009-06-24 19:19:16 +0000499 if (TM.getRelocationModel() == Reloc::PIC_) {
500 O << '-';
501 PrintPICBaseSymbol();
502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000504 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000505 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 }
508
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000509 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510
Chris Lattnerdabfe572009-06-21 02:22:53 +0000511 if (needCloseParen)
512 O << ')';
513
Chris Lattner797a0782009-06-27 05:46:24 +0000514 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 }
Chris Lattner797a0782009-06-27 05:46:24 +0000516 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerce003822009-06-26 21:47:27 +0000517 /// NOTE: MO_ExternalSymbol in a non-pcrel_imm context is *only* generated
518 /// by _GLOBAL_OFFSET_TABLE_ on X86-32. All others are call operands, which
519 /// are pcrel_imm's.
520 assert(!Subtarget->is64Bit() && !Subtarget->isPICStyleRIPRel());
521 // These are never used as memory operands.
522 assert(!(Modifier && !strcmp(Modifier, "mem")));
523
524 O << '$';
525 O << TAI->getGlobalPrefix();
526 O << MO.getSymbolName();
Chris Lattner797a0782009-06-27 05:46:24 +0000527 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 }
Chris Lattner797a0782009-06-27 05:46:24 +0000529
530 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 default:
Chris Lattner797a0782009-06-27 05:46:24 +0000532 assert(0 && "Unknown target flag on GV operand");
533 case X86II::MO_NO_FLAG:
534 break;
535 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
536 O << " + [.-";
537 PrintPICBaseSymbol();
538 O << ']';
539 break;
540 case X86II::MO_PIC_BASE_OFFSET:
541 O << '-';
542 PrintPICBaseSymbol();
543 break;
544 case X86II::MO_TLSGD: O << "@TLSGD"; break;
545 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
546 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
547 case X86II::MO_TPOFF: O << "@TPOFF"; break;
548 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
549 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
550 case X86II::MO_GOT: O << "@GOT"; break;
551 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 }
553}
554
555void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000556 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 assert(value <= 7 && "Invalid ssecc argument!");
558 switch (value) {
559 case 0: O << "eq"; break;
560 case 1: O << "lt"; break;
561 case 2: O << "le"; break;
562 case 3: O << "unord"; break;
563 case 4: O << "neq"; break;
564 case 5: O << "nlt"; break;
565 case 6: O << "nle"; break;
566 case 7: O << "ord"; break;
567 }
568}
569
Rafael Espindolabca99f72009-04-08 21:14:34 +0000570void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000571 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 MachineOperand BaseReg = MI->getOperand(Op);
573 MachineOperand IndexReg = MI->getOperand(Op+2);
574 const MachineOperand &DispSpec = MI->getOperand(Op+3);
575
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000576 if (DispSpec.isGlobal() ||
577 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000578 DispSpec.isJTI() ||
579 DispSpec.isSymbol()) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000580 printOperand(MI, Op+3, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000582 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
584 O << DispVal;
585 }
586
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000587 if ((IndexReg.getReg() || BaseReg.getReg()) &&
588 (Modifier == 0 || strcmp(Modifier, "no-rip"))) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000589 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000591
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 // There are cases where we can end up with ESP/RSP in the indexreg slot.
593 // If this happens, swap the base/index register to support assemblers that
594 // don't work when the index is *SP.
595 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
596 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
597 std::swap(BaseReg, IndexReg);
598 std::swap(BaseRegOperand, IndexRegOperand);
599 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000601 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 if (BaseReg.getReg())
603 printOperand(MI, Op+BaseRegOperand, Modifier);
604
605 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000606 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 printOperand(MI, Op+IndexRegOperand, Modifier);
608 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000611 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
613}
614
Rafael Espindolabca99f72009-04-08 21:14:34 +0000615void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000616 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000617 assert(isMem(MI, Op) && "Invalid memory reference!");
618 MachineOperand Segment = MI->getOperand(Op+4);
619 if (Segment.getReg()) {
620 printOperand(MI, Op+4, Modifier);
621 O << ':';
622 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000623 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000624}
625
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000626void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000627 const MachineBasicBlock *MBB) const {
628 if (!TAI->getSetDirective())
629 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000630
631 // We don't need .set machinery if we have GOT-style relocations
632 if (Subtarget->isPICStyleGOT())
633 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000634
Evan Cheng6fb06762007-11-09 01:32:10 +0000635 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
636 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000637 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000638 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000639 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000640 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000641 else {
642 O << '-';
643 PrintPICBaseSymbol();
644 O << '\n';
645 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000646}
647
Chris Lattner14f791a2009-06-24 19:19:16 +0000648
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000650 PrintPICBaseSymbol();
651 O << '\n';
652 PrintPICBaseSymbol();
653 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654}
655
656
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000657void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
658 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000659 unsigned uid) const
660{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000661 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
662 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
663
664 O << JTEntryDirective << ' ';
665
666 if (TM.getRelocationModel() == Reloc::PIC_) {
667 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
668 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
669 << '_' << uid << "_set_" << MBB->getNumber();
670 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000671 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000672 O << "@GOTOFF";
673 } else
674 assert(0 && "Don't know how to print MBB label for this PIC mode");
675 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000676 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000677}
678
Chris Lattner8bb96e42009-06-15 04:42:32 +0000679bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 unsigned Reg = MO.getReg();
681 switch (Mode) {
682 default: return true; // Unknown mode.
683 case 'b': // Print QImode register
684 Reg = getX86SubSuperRegister(Reg, MVT::i8);
685 break;
686 case 'h': // Print QImode high register
687 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
688 break;
689 case 'w': // Print HImode register
690 Reg = getX86SubSuperRegister(Reg, MVT::i16);
691 break;
692 case 'k': // Print SImode register
693 Reg = getX86SubSuperRegister(Reg, MVT::i32);
694 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000695 case 'q': // Print DImode register
696 Reg = getX86SubSuperRegister(Reg, MVT::i64);
697 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
699
Evan Cheng00d04a72008-07-07 22:21:06 +0000700 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 return false;
702}
703
704/// PrintAsmOperand - Print out an operand for an inline asm expression.
705///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000706bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000707 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 const char *ExtraCode) {
709 // Does this asm operand have a single letter operand modifier?
710 if (ExtraCode && ExtraCode[0]) {
711 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000712
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 switch (ExtraCode[0]) {
714 default: return true; // Unknown modifier.
715 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000716 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 return false;
718 case 'b': // Print QImode register
719 case 'h': // Print QImode high register
720 case 'w': // Print HImode register
721 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000722 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000723 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
725 printOperand(MI, OpNo);
726 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000727
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000729 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000731
732 case 'n': { // Negate the immediate or print a '-' before the operand.
733 // Note: this is a temporary solution. It should be handled target
734 // independently as part of the 'MC' work.
735 const MachineOperand &MO = MI->getOperand(OpNo);
736 if (MO.isImm()) {
737 O << -MO.getImm();
738 return false;
739 }
740 O << '-';
741 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 }
743 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 printOperand(MI, OpNo);
746 return false;
747}
748
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000749bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000751 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000753 if (ExtraCode && ExtraCode[0]) {
754 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000755
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000756 switch (ExtraCode[0]) {
757 default: return true; // Unknown modifier.
758 case 'b': // Print QImode register
759 case 'h': // Print QImode high register
760 case 'w': // Print HImode register
761 case 'k': // Print SImode register
762 case 'q': // Print SImode register
763 // These only apply to registers, ignore on mem.
764 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000765 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000766 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000767 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000768 }
769 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 printMemReference(MI, OpNo);
771 return false;
772}
773
Chris Lattnerf5da5902009-06-20 07:03:18 +0000774static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
775 // Convert registers in the addr mode according to subreg64.
776 for (unsigned i = 0; i != 4; ++i) {
777 if (!MI->getOperand(i).isReg()) continue;
778
779 unsigned Reg = MI->getOperand(i).getReg();
780 if (Reg == 0) continue;
781
782 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
783 }
784}
785
Bill Wendlingb3b11262009-02-06 21:45:08 +0000786/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
787/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788///
789void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
790 ++EmittedInsts;
791
Chris Lattner19b1bd52009-06-19 00:47:33 +0000792 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000793 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
794 O << "\t";
795 printInlineAsm(MI);
796 return;
797 } else if (MI->isLabel()) {
798 printLabel(MI);
799 return;
800 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
801 printDeclare(MI);
802 return;
803 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
804 printImplicitDef(MI);
805 return;
806 }
807
Chris Lattnere67184e2009-06-19 23:59:57 +0000808 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000809 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000810
811 TmpInst.setOpcode(MI->getOpcode());
812
813 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
814 const MachineOperand &MO = MI->getOperand(i);
815
816 MCOperand MCOp;
817 if (MO.isReg()) {
818 MCOp.MakeReg(MO.getReg());
819 } else if (MO.isImm()) {
820 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000821 } else if (MO.isMBB()) {
822 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000823 } else {
824 assert(0 && "Unimp");
825 }
826
827 TmpInst.addOperand(MCOp);
828 }
829
Chris Lattner6b9f7742009-06-20 07:59:10 +0000830 switch (TmpInst.getOpcode()) {
831 case X86::LEA64_32r:
832 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000833 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000834 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000835 }
836
Chris Lattner19b1bd52009-06-19 00:47:33 +0000837 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000838 printInstruction(&TmpInst);
839 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000840 }
841
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 // Call the autogenerated instruction printer routines.
843 printInstruction(MI);
844}
845
Devang Patelc20079e2009-06-20 01:00:07 +0000846/// doInitialization
847bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000848 if (NewAsmPrinter) {
849 Context = new MCContext();
850 // FIXME: Send this to "O" instead of outs(). For now, we force it to
851 // stdout to make it easy to compare.
852 Streamer = createAsmStreamer(*Context, outs());
853 }
854
Devang Patelc20079e2009-06-20 01:00:07 +0000855 return AsmPrinter::doInitialization(M);
856}
857
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000859 const TargetData *TD = TM.getTargetData();
860
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 if (!GVar->hasInitializer())
862 return; // External global require no code
863
864 // Check to see if this is a special global used by LLVM, if so, emit it.
865 if (EmitSpecialLLVMGlobal(GVar)) {
866 if (Subtarget->isTargetDarwin() &&
867 TM.getRelocationModel() == Reloc::Static) {
868 if (GVar->getName() == "llvm.global_ctors")
869 O << ".reference .constructors_used\n";
870 else if (GVar->getName() == "llvm.global_dtors")
871 O << ".reference .destructors_used\n";
872 }
873 return;
874 }
875
876 std::string name = Mang->getValueName(GVar);
877 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000878 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000879 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000881 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000882 unsigned Align = TD->getPreferredAlignmentLog(GVar);
883
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000884 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000885
886 if (Subtarget->isTargetELF())
887 O << "\t.type\t" << name << ",@object\n";
888
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000889 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000890
Evan Chengcf84b142009-02-18 02:19:52 +0000891 if (C->isNullValue() && !GVar->hasSection() &&
892 !(Subtarget->isTargetDarwin() &&
893 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000894 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000895 if (GVar->hasExternalLinkage()) {
896 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000897 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000899 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000900 return;
901 }
902 }
903
904 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000905 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000906 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000907
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000908 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000909 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000910 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000911 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000912 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000913 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000914 O << "\t.globl " << name << '\n'
915 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000916 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000917 O << name << ":";
918 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000919 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000920 PrintUnmangledNameSafely(GVar, O);
921 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000922 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000923 EmitGlobalConstant(C);
924 return;
925 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000926 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000927 if (TAI->getCOMMDirectiveTakesAlignment())
928 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000929 }
930 } else {
931 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000932 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000933 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000934 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000935 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000936 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000937 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000938 }
Evan Cheng11db8142009-03-24 00:17:40 +0000939 if (VerboseAsm) {
940 O << "\t\t" << TAI->getCommentString() << ' ';
941 PrintUnmangledNameSafely(GVar, O);
942 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000943 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000944 return;
945 }
946 }
947
948 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000949 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000950 case GlobalValue::LinkOnceAnyLinkage:
951 case GlobalValue::LinkOnceODRLinkage:
952 case GlobalValue::WeakAnyLinkage:
953 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000954 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000955 O << "\t.globl " << name << '\n'
956 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000957 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000958 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000959 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000960 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000961 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000962 }
963 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000964 case GlobalValue::DLLExportLinkage:
965 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000966 // FIXME: appending linkage variables should go into a section of
967 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000968 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000969 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000970 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000971 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000972 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000973 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000975 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000976 assert(0 && "Unknown linkage type!");
977 }
978
979 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000980 O << name << ":";
981 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000982 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000983 PrintUnmangledNameSafely(GVar, O);
984 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000985 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000986 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000987 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000988
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000989 EmitGlobalConstant(C);
990}
991
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000992bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000993 // Print out module-level global variables here.
994 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000995 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000997
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000998 if (I->hasDLLExportLinkage())
999 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001000 }
1001
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001002 if (Subtarget->isTargetDarwin()) {
1003 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001004
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001005 // Add the (possibly multiple) personalities to the set of global value
1006 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001007 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001008 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1009 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1010 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001011 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001012 std::string Name = Mang->getValueName(Personalities[i]);
1013 decorateName(Name, Personalities[i]);
1014 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001015 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001016 }
1017
Chris Lattner2e860262009-06-24 18:24:09 +00001018 // Output stubs for dynamically-linked functions
1019 if (!FnStubs.empty()) {
1020 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1021 I != E; ++I) {
1022 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1023 "self_modifying_code+pure_instructions,5", 0);
1024 const char *Name = I->getKeyData();
1025 printSuffixedName(Name, "$stub");
1026 O << ":\n"
1027 "\t.indirect_symbol " << Name << "\n"
1028 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1029 }
1030 O << '\n';
1031 }
1032
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001033 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001034 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001035 SwitchToDataSection(
1036 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001037 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1038 I != E; ++I) {
1039 const char *Name = I->getKeyData();
1040 printSuffixedName(Name, "$non_lazy_ptr");
1041 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1042 }
1043 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001044
Evan Chenga65854f2008-12-05 01:06:39 +00001045 if (!HiddenGVStubs.empty()) {
1046 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001047 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001048 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1049 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001050 const char *Name = I->getKeyData();
1051 printSuffixedName(Name, "$non_lazy_ptr");
1052 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1053 }
Evan Chenga65854f2008-12-05 01:06:39 +00001054 }
1055
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001056 // Funny Darwin hack: This flag tells the linker that no global symbols
1057 // contain code that falls through to other global symbols (e.g. the obvious
1058 // implementation of multiple entry points). If this doesn't occur, the
1059 // linker can safely perform dead code stripping. Since LLVM never
1060 // generates code that does this, it is always safe to set.
1061 O << "\t.subsections_via_symbols\n";
1062 } else if (Subtarget->isTargetCygMing()) {
1063 // Emit type information for external functions
1064 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1065 i != e; ++i) {
1066 O << "\t.def\t " << i->getKeyData()
1067 << ";\t.scl\t" << COFF::C_EXT
1068 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1069 << ";\t.endef\n";
1070 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001071 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001072
Chris Lattnerdb191f02009-06-24 18:52:01 +00001073
1074 // Output linker support code for dllexported globals on windows.
1075 if (!DLLExportedGVs.empty()) {
1076 SwitchToDataSection(".section .drectve");
1077
1078 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1079 e = DLLExportedGVs.end(); i != e; ++i)
1080 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1081 }
1082
1083 if (!DLLExportedFns.empty()) {
1084 SwitchToDataSection(".section .drectve");
1085
1086 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1087 e = DLLExportedFns.end();
1088 i != e; ++i)
1089 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1090 }
1091
Chris Lattnerdb191f02009-06-24 18:52:01 +00001092 // Do common shutdown.
1093 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001094
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001095 if (NewAsmPrinter) {
1096 Streamer->Finish();
1097
1098 delete Streamer;
1099 delete Context;
1100 Streamer = 0;
1101 Context = 0;
1102 }
1103
Chris Lattnerdb191f02009-06-24 18:52:01 +00001104 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001105}
1106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107// Include the auto-generated portion of the assembly writer.
1108#include "X86GenAsmWriter.inc"