blob: e75cfc5c309abcdd48f1996fc1949ad098637d31 [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
Chris Lattner2e845952009-06-24 19:44:36 +0000157
158
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000159void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000162 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000164 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000165
Chris Lattner2e845952009-06-24 19:44:36 +0000166 // FIXME: A function's alignment should be part of MachineFunction. There
167 // shouldn't be a policy decision here.
Devang Patel93698d92008-10-01 23:18:38 +0000168 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000169 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000170 FnAlign = 1;
Chris Lattner2e845952009-06-24 19:44:36 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 switch (F->getLinkage()) {
173 default: assert(0 && "Unknown linkage type!");
174 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000175 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000180 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000183 case Function::LinkOnceAnyLinkage:
184 case Function::LinkOnceODRLinkage:
185 case Function::WeakAnyLinkage:
186 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000187 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << '\n';
190 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << "\n"
193 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197 break;
198 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000199
200 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
202 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000203 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 else if (Subtarget->isTargetCygMing()) {
205 O << "\t.def\t " << CurrentFnName
206 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000207 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
209 << ";\t.endef\n";
210 }
211
212 O << CurrentFnName << ":\n";
213 // Add some workaround for linkonce linkage on Cygwin\MinGW
214 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000215 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219/// runOnMachineFunction - This uses the printMachineInstruction()
220/// method to print assembly for each instruction.
221///
222bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
223 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000224 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000225 unsigned CC = F->getCallingConv();
226
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000227 SetupMachineFunction(MF);
228 O << "\n\n";
229
230 // Populate function information map. Actually, We don't want to populate
231 // non-stdcall or non-fastcall functions' information right now.
232 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
233 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
234
235 // Print out constants referenced by the function
236 EmitConstantPool(MF.getConstantPool());
237
238 if (F->hasDLLExportLinkage())
239 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
240
241 // Print the 'header' of function
242 emitFunctionHeader(MF);
243
244 // Emit pre-function debug and/or EH information.
245 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000246 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
251 I != E; ++I) {
252 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000253 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
254 // This is an entry block or a block that's only reachable via a
255 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
256 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000257 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 O << '\n';
259 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000260 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
261 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000263 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 printMachineInstruction(II);
266 }
267 }
268
Dale Johannesenf35771f2008-04-08 00:37:56 +0000269 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
270 // If the function is empty, then we need to emit *something*. Otherwise,
271 // the function's label might be associated with something that it wasn't
272 // meant to be associated with. We emit a noop in this situation.
273 // We are assuming inline asms are code.
274 O << "\tnop\n";
275 }
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000278 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000280 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000281 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000282 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
284 // Print out jump tables referenced by the function.
285 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000286
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000287 O.flush();
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // We didn't modify anything.
290 return false;
291}
292
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000293static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
Chris Lattner50315f22009-06-25 17:58:52 +0000294 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_;
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000295}
296
297static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
Chris Lattner357a0ca2009-06-20 19:34:09 +0000301/// print_pcrel_imm - This is used to print an immediate value that ends up
302/// being encoded as a pc-relative value. These print slightly differently, for
303/// example, a $ is not emitted.
304void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
305 const MachineOperand &MO = MI->getOperand(OpNo);
306 switch (MO.getType()) {
307 default: assert(0 && "Unknown pcrel immediate operand");
308 case MachineOperand::MO_Immediate:
309 O << MO.getImm();
310 return;
311 case MachineOperand::MO_MachineBasicBlock:
312 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
313 return;
314
315 case MachineOperand::MO_GlobalAddress: {
316 const GlobalValue *GV = MO.getGlobal();
317 std::string Name = Mang->getValueName(GV);
318 decorateName(Name, GV);
319
320 bool needCloseParen = false;
321 if (Name[0] == '$') {
322 // The name begins with a dollar-sign. In order to avoid having it look
323 // like an integer immediate to the assembler, enclose it in parens.
324 O << '(';
325 needCloseParen = true;
326 }
327
328 if (shouldPrintStub(TM, Subtarget)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000329 // DARWIN/X86-32 in != static mode.
330
Chris Lattner357a0ca2009-06-20 19:34:09 +0000331 // Link-once, declaration, or Weakly-linked global variables need
332 // non-lazily-resolved stubs
333 if (GV->isDeclaration() || GV->isWeakForLinker()) {
334 // Dynamically-resolved functions need a stub for the function.
335 if (isa<Function>(GV)) {
336 // Function stubs are no longer needed for Mac OS X 10.5 and up.
337 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
338 O << Name;
339 } else {
340 FnStubs.insert(Name);
341 printSuffixedName(Name, "$stub");
342 }
343 } else if (GV->hasHiddenVisibility()) {
344 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
345 // Definition is not definitely in the current translation unit.
346 O << Name;
347 else {
348 HiddenGVStubs.insert(Name);
349 printSuffixedName(Name, "$non_lazy_ptr");
350 }
351 } else {
352 GVStubs.insert(Name);
353 printSuffixedName(Name, "$non_lazy_ptr");
354 }
355 } else {
356 if (GV->hasDLLImportLinkage())
357 O << "__imp_";
358 O << Name;
359 }
360 } else {
Chris Lattner50315f22009-06-25 17:58:52 +0000361 if (GV->hasDLLImportLinkage())
Chris Lattner357a0ca2009-06-20 19:34:09 +0000362 O << "__imp_";
Chris Lattner357a0ca2009-06-20 19:34:09 +0000363 O << Name;
364
365 if (shouldPrintPLT(TM, Subtarget)) {
366 // Assemble call via PLT for externally visible symbols
367 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
368 !GV->hasLocalLinkage())
369 O << "@PLT";
370 }
371 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
372 // Save function name for later type emission
373 FnStubs.insert(Name);
374 }
375
Chris Lattner357a0ca2009-06-20 19:34:09 +0000376 printOffset(MO.getOffset());
377
378 if (needCloseParen)
379 O << ')';
380 return;
381 }
382
383 case MachineOperand::MO_ExternalSymbol: {
384 bool needCloseParen = false;
385 std::string Name(TAI->getGlobalPrefix());
386 Name += MO.getSymbolName();
387 // Print function stub suffix unless it's Mac OS X 10.5 and up.
388 if (shouldPrintStub(TM, Subtarget) &&
Chris Lattner50315f22009-06-25 17:58:52 +0000389 // DARWIN/X86-32 in != static mode.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000390 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000391
Chris Lattner357a0ca2009-06-20 19:34:09 +0000392 FnStubs.insert(Name);
393 printSuffixedName(Name, "$stub");
394 return;
395 }
396
397 if (Name[0] == '$') {
398 // The name begins with a dollar-sign. In order to avoid having it look
399 // like an integer immediate to the assembler, enclose it in parens.
400 O << '(';
401 needCloseParen = true;
402 }
403
404 O << Name;
405
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000406 if (MO.getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) {
407 O << " + [.-";
408 PrintPICBaseSymbol();
409 O << ']';
Chris Lattner357a0ca2009-06-20 19:34:09 +0000410 }
411
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000412 if (shouldPrintPLT(TM, Subtarget))
413 O << "@PLT";
414
Chris Lattner357a0ca2009-06-20 19:34:09 +0000415 if (needCloseParen)
416 O << ')';
417
418 return;
419 }
420 }
421}
422
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000424 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 switch (MO.getType()) {
Chris Lattner797a0782009-06-27 05:46:24 +0000427 default: assert(0 && "unknown operand type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000429 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 "Virtual registers should not make it this far!");
431 O << '%';
432 unsigned Reg = MO.getReg();
433 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000434 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
436 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
437 Reg = getX86SubSuperRegister(Reg, VT);
438 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000439 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 return;
441 }
442
443 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000444 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000445 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000447 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 case MachineOperand::MO_JumpTableIndex: {
450 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
451 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000452 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000453 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000454 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
456 case MachineOperand::MO_ConstantPoolIndex: {
457 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
458 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000459 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000460 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461
Chris Lattner40f56902009-06-26 20:00:05 +0000462 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000463 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 }
465 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000466 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000468 const GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000470 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000471
Chris Lattner797a0782009-06-27 05:46:24 +0000472 bool needCloseParen = false;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000473 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 O << '$';
475 else if (Name[0] == '$') {
476 // The name begins with a dollar-sign. In order to avoid having it look
477 // like an integer immediate to the assembler, enclose it in parens.
478 O << '(';
479 needCloseParen = true;
480 }
481
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000482 if (shouldPrintStub(TM, Subtarget)) {
Chris Lattner50315f22009-06-25 17:58:52 +0000483 // DARWIN/X86-32 in != static mode.
484
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 // Link-once, declaration, or Weakly-linked global variables need
486 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000487 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000489 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000490 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
491 // Definition is not definitely in the current translation unit.
492 O << Name;
493 else {
494 HiddenGVStubs.insert(Name);
495 printSuffixedName(Name, "$non_lazy_ptr");
496 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 } else {
498 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000499 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 }
501 } else {
502 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000503 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 O << Name;
505 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000506
Chris Lattner14f791a2009-06-24 19:19:16 +0000507 if (TM.getRelocationModel() == Reloc::PIC_) {
508 O << '-';
509 PrintPICBaseSymbol();
510 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000512 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000513 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 }
516
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000517 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518
Chris Lattnerdabfe572009-06-21 02:22:53 +0000519 if (needCloseParen)
520 O << ')';
521
Chris Lattner797a0782009-06-27 05:46:24 +0000522 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 }
Chris Lattner797a0782009-06-27 05:46:24 +0000524 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerce003822009-06-26 21:47:27 +0000525 /// NOTE: MO_ExternalSymbol in a non-pcrel_imm context is *only* generated
526 /// by _GLOBAL_OFFSET_TABLE_ on X86-32. All others are call operands, which
527 /// are pcrel_imm's.
528 assert(!Subtarget->is64Bit() && !Subtarget->isPICStyleRIPRel());
529 // These are never used as memory operands.
530 assert(!(Modifier && !strcmp(Modifier, "mem")));
531
532 O << '$';
533 O << TAI->getGlobalPrefix();
534 O << MO.getSymbolName();
Chris Lattner797a0782009-06-27 05:46:24 +0000535 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 }
Chris Lattner797a0782009-06-27 05:46:24 +0000537
538 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 default:
Chris Lattner797a0782009-06-27 05:46:24 +0000540 assert(0 && "Unknown target flag on GV operand");
541 case X86II::MO_NO_FLAG:
542 break;
543 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
544 O << " + [.-";
545 PrintPICBaseSymbol();
546 O << ']';
547 break;
548 case X86II::MO_PIC_BASE_OFFSET:
549 O << '-';
550 PrintPICBaseSymbol();
551 break;
552 case X86II::MO_TLSGD: O << "@TLSGD"; break;
553 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
554 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
555 case X86II::MO_TPOFF: O << "@TPOFF"; break;
556 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
557 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
558 case X86II::MO_GOT: O << "@GOT"; break;
559 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 }
561}
562
563void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000564 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 assert(value <= 7 && "Invalid ssecc argument!");
566 switch (value) {
567 case 0: O << "eq"; break;
568 case 1: O << "lt"; break;
569 case 2: O << "le"; break;
570 case 3: O << "unord"; break;
571 case 4: O << "neq"; break;
572 case 5: O << "nlt"; break;
573 case 6: O << "nle"; break;
574 case 7: O << "ord"; break;
575 }
576}
577
Rafael Espindolabca99f72009-04-08 21:14:34 +0000578void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000579 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 MachineOperand BaseReg = MI->getOperand(Op);
581 MachineOperand IndexReg = MI->getOperand(Op+2);
582 const MachineOperand &DispSpec = MI->getOperand(Op+3);
583
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000584 if (DispSpec.isGlobal() ||
585 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000586 DispSpec.isJTI() ||
587 DispSpec.isSymbol()) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000588 printOperand(MI, Op+3, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000590 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
592 O << DispVal;
593 }
594
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000595 if ((IndexReg.getReg() || BaseReg.getReg()) &&
596 (Modifier == 0 || strcmp(Modifier, "no-rip"))) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000597 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 // There are cases where we can end up with ESP/RSP in the indexreg slot.
601 // If this happens, swap the base/index register to support assemblers that
602 // don't work when the index is *SP.
603 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
604 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
605 std::swap(BaseReg, IndexReg);
606 std::swap(BaseRegOperand, IndexRegOperand);
607 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000608
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 if (BaseReg.getReg())
611 printOperand(MI, Op+BaseRegOperand, Modifier);
612
613 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000614 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 printOperand(MI, Op+IndexRegOperand, Modifier);
616 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000617 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000619 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 }
621}
622
Rafael Espindolabca99f72009-04-08 21:14:34 +0000623void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000624 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000625 assert(isMem(MI, Op) && "Invalid memory reference!");
626 MachineOperand Segment = MI->getOperand(Op+4);
627 if (Segment.getReg()) {
628 printOperand(MI, Op+4, Modifier);
629 O << ':';
630 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000631 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000632}
633
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000634void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000635 const MachineBasicBlock *MBB) const {
636 if (!TAI->getSetDirective())
637 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000638
639 // We don't need .set machinery if we have GOT-style relocations
640 if (Subtarget->isPICStyleGOT())
641 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000642
Evan Cheng6fb06762007-11-09 01:32:10 +0000643 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
644 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000645 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000646 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000647 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000648 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000649 else {
650 O << '-';
651 PrintPICBaseSymbol();
652 O << '\n';
653 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000654}
655
Chris Lattner14f791a2009-06-24 19:19:16 +0000656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000658 PrintPICBaseSymbol();
659 O << '\n';
660 PrintPICBaseSymbol();
661 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662}
663
664
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000665void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
666 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000667 unsigned uid) const
668{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000669 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
670 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
671
672 O << JTEntryDirective << ' ';
673
674 if (TM.getRelocationModel() == Reloc::PIC_) {
675 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
676 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
677 << '_' << uid << "_set_" << MBB->getNumber();
678 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000679 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000680 O << "@GOTOFF";
681 } else
682 assert(0 && "Don't know how to print MBB label for this PIC mode");
683 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000684 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000685}
686
Chris Lattner8bb96e42009-06-15 04:42:32 +0000687bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 unsigned Reg = MO.getReg();
689 switch (Mode) {
690 default: return true; // Unknown mode.
691 case 'b': // Print QImode register
692 Reg = getX86SubSuperRegister(Reg, MVT::i8);
693 break;
694 case 'h': // Print QImode high register
695 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
696 break;
697 case 'w': // Print HImode register
698 Reg = getX86SubSuperRegister(Reg, MVT::i16);
699 break;
700 case 'k': // Print SImode register
701 Reg = getX86SubSuperRegister(Reg, MVT::i32);
702 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000703 case 'q': // Print DImode register
704 Reg = getX86SubSuperRegister(Reg, MVT::i64);
705 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
707
Evan Cheng00d04a72008-07-07 22:21:06 +0000708 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 return false;
710}
711
712/// PrintAsmOperand - Print out an operand for an inline asm expression.
713///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000714bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000715 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 const char *ExtraCode) {
717 // Does this asm operand have a single letter operand modifier?
718 if (ExtraCode && ExtraCode[0]) {
719 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000720
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 switch (ExtraCode[0]) {
722 default: return true; // Unknown modifier.
723 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000724 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 return false;
726 case 'b': // Print QImode register
727 case 'h': // Print QImode high register
728 case 'w': // Print HImode register
729 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000730 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000731 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
733 printOperand(MI, OpNo);
734 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000735
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000737 printOperand(MI, OpNo, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000739
740 case 'n': { // Negate the immediate or print a '-' before the operand.
741 // Note: this is a temporary solution. It should be handled target
742 // independently as part of the 'MC' work.
743 const MachineOperand &MO = MI->getOperand(OpNo);
744 if (MO.isImm()) {
745 O << -MO.getImm();
746 return false;
747 }
748 O << '-';
749 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 }
751 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000752
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 printOperand(MI, OpNo);
754 return false;
755}
756
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000757bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000759 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000761 if (ExtraCode && ExtraCode[0]) {
762 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000763
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000764 switch (ExtraCode[0]) {
765 default: return true; // Unknown modifier.
766 case 'b': // Print QImode register
767 case 'h': // Print QImode high register
768 case 'w': // Print HImode register
769 case 'k': // Print SImode register
770 case 'q': // Print SImode register
771 // These only apply to registers, ignore on mem.
772 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000773 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000774 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000775 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000776 }
777 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 printMemReference(MI, OpNo);
779 return false;
780}
781
Chris Lattnerf5da5902009-06-20 07:03:18 +0000782static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
783 // Convert registers in the addr mode according to subreg64.
784 for (unsigned i = 0; i != 4; ++i) {
785 if (!MI->getOperand(i).isReg()) continue;
786
787 unsigned Reg = MI->getOperand(i).getReg();
788 if (Reg == 0) continue;
789
790 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
791 }
792}
793
Bill Wendlingb3b11262009-02-06 21:45:08 +0000794/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
795/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796///
797void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
798 ++EmittedInsts;
799
Chris Lattner19b1bd52009-06-19 00:47:33 +0000800 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000801 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
802 O << "\t";
803 printInlineAsm(MI);
804 return;
805 } else if (MI->isLabel()) {
806 printLabel(MI);
807 return;
808 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
809 printDeclare(MI);
810 return;
811 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
812 printImplicitDef(MI);
813 return;
814 }
815
Chris Lattnere67184e2009-06-19 23:59:57 +0000816 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000817 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000818
819 TmpInst.setOpcode(MI->getOpcode());
820
821 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
822 const MachineOperand &MO = MI->getOperand(i);
823
824 MCOperand MCOp;
825 if (MO.isReg()) {
826 MCOp.MakeReg(MO.getReg());
827 } else if (MO.isImm()) {
828 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000829 } else if (MO.isMBB()) {
830 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000831 } else {
832 assert(0 && "Unimp");
833 }
834
835 TmpInst.addOperand(MCOp);
836 }
837
Chris Lattner6b9f7742009-06-20 07:59:10 +0000838 switch (TmpInst.getOpcode()) {
839 case X86::LEA64_32r:
840 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000841 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000842 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000843 }
844
Chris Lattner19b1bd52009-06-19 00:47:33 +0000845 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000846 printInstruction(&TmpInst);
847 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000848 }
849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 // Call the autogenerated instruction printer routines.
851 printInstruction(MI);
852}
853
Devang Patelc20079e2009-06-20 01:00:07 +0000854/// doInitialization
855bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000856 if (NewAsmPrinter) {
857 Context = new MCContext();
858 // FIXME: Send this to "O" instead of outs(). For now, we force it to
859 // stdout to make it easy to compare.
860 Streamer = createAsmStreamer(*Context, outs());
861 }
862
Devang Patelc20079e2009-06-20 01:00:07 +0000863 return AsmPrinter::doInitialization(M);
864}
865
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000867 const TargetData *TD = TM.getTargetData();
868
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 if (!GVar->hasInitializer())
870 return; // External global require no code
871
872 // Check to see if this is a special global used by LLVM, if so, emit it.
873 if (EmitSpecialLLVMGlobal(GVar)) {
874 if (Subtarget->isTargetDarwin() &&
875 TM.getRelocationModel() == Reloc::Static) {
876 if (GVar->getName() == "llvm.global_ctors")
877 O << ".reference .constructors_used\n";
878 else if (GVar->getName() == "llvm.global_dtors")
879 O << ".reference .destructors_used\n";
880 }
881 return;
882 }
883
884 std::string name = Mang->getValueName(GVar);
885 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000886 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000887 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000888 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000889 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 unsigned Align = TD->getPreferredAlignmentLog(GVar);
891
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000892 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000893
894 if (Subtarget->isTargetELF())
895 O << "\t.type\t" << name << ",@object\n";
896
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000897 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000898
Evan Chengcf84b142009-02-18 02:19:52 +0000899 if (C->isNullValue() && !GVar->hasSection() &&
900 !(Subtarget->isTargetDarwin() &&
901 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000902 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000903 if (GVar->hasExternalLinkage()) {
904 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000905 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000906 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000907 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000908 return;
909 }
910 }
911
912 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000913 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000914 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000915
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000916 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000917 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000918 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000919 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000920 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000921 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000922 O << "\t.globl " << name << '\n'
923 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000924 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000925 O << name << ":";
926 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000927 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000928 PrintUnmangledNameSafely(GVar, O);
929 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000930 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000931 EmitGlobalConstant(C);
932 return;
933 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000934 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000935 if (TAI->getCOMMDirectiveTakesAlignment())
936 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000937 }
938 } else {
939 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000940 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000941 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000942 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000943 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000944 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000945 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000946 }
Evan Cheng11db8142009-03-24 00:17:40 +0000947 if (VerboseAsm) {
948 O << "\t\t" << TAI->getCommentString() << ' ';
949 PrintUnmangledNameSafely(GVar, O);
950 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000951 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000952 return;
953 }
954 }
955
956 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000957 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000958 case GlobalValue::LinkOnceAnyLinkage:
959 case GlobalValue::LinkOnceODRLinkage:
960 case GlobalValue::WeakAnyLinkage:
961 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000962 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000963 O << "\t.globl " << name << '\n'
964 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000965 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000966 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000967 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000968 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000969 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000970 }
971 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000972 case GlobalValue::DLLExportLinkage:
973 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974 // FIXME: appending linkage variables should go into a section of
975 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000976 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000977 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000978 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000979 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000980 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000981 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000983 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000984 assert(0 && "Unknown linkage type!");
985 }
986
987 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000988 O << name << ":";
989 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000990 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000991 PrintUnmangledNameSafely(GVar, O);
992 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000993 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000994 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000995 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000997 EmitGlobalConstant(C);
998}
999
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001000bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001001 // Print out module-level global variables here.
1002 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001003 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001004 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001005
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001006 if (I->hasDLLExportLinkage())
1007 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001008 }
1009
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001010 if (Subtarget->isTargetDarwin()) {
1011 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001012
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001013 // Add the (possibly multiple) personalities to the set of global value
1014 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001015 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001016 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1017 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1018 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001019 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001020 std::string Name = Mang->getValueName(Personalities[i]);
1021 decorateName(Name, Personalities[i]);
1022 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001023 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001024 }
1025
Chris Lattner2e860262009-06-24 18:24:09 +00001026 // Output stubs for dynamically-linked functions
1027 if (!FnStubs.empty()) {
1028 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1029 I != E; ++I) {
1030 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1031 "self_modifying_code+pure_instructions,5", 0);
1032 const char *Name = I->getKeyData();
1033 printSuffixedName(Name, "$stub");
1034 O << ":\n"
1035 "\t.indirect_symbol " << Name << "\n"
1036 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1037 }
1038 O << '\n';
1039 }
1040
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001041 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001042 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001043 SwitchToDataSection(
1044 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001045 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1046 I != E; ++I) {
1047 const char *Name = I->getKeyData();
1048 printSuffixedName(Name, "$non_lazy_ptr");
1049 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1050 }
1051 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001052
Evan Chenga65854f2008-12-05 01:06:39 +00001053 if (!HiddenGVStubs.empty()) {
1054 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001055 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001056 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1057 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001058 const char *Name = I->getKeyData();
1059 printSuffixedName(Name, "$non_lazy_ptr");
1060 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1061 }
Evan Chenga65854f2008-12-05 01:06:39 +00001062 }
1063
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001064 // Funny Darwin hack: This flag tells the linker that no global symbols
1065 // contain code that falls through to other global symbols (e.g. the obvious
1066 // implementation of multiple entry points). If this doesn't occur, the
1067 // linker can safely perform dead code stripping. Since LLVM never
1068 // generates code that does this, it is always safe to set.
1069 O << "\t.subsections_via_symbols\n";
1070 } else if (Subtarget->isTargetCygMing()) {
1071 // Emit type information for external functions
1072 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1073 i != e; ++i) {
1074 O << "\t.def\t " << i->getKeyData()
1075 << ";\t.scl\t" << COFF::C_EXT
1076 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1077 << ";\t.endef\n";
1078 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001079 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001080
Chris Lattnerdb191f02009-06-24 18:52:01 +00001081
1082 // Output linker support code for dllexported globals on windows.
1083 if (!DLLExportedGVs.empty()) {
1084 SwitchToDataSection(".section .drectve");
1085
1086 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1087 e = DLLExportedGVs.end(); i != e; ++i)
1088 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1089 }
1090
1091 if (!DLLExportedFns.empty()) {
1092 SwitchToDataSection(".section .drectve");
1093
1094 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1095 e = DLLExportedFns.end();
1096 i != e; ++i)
1097 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1098 }
1099
Chris Lattnerdb191f02009-06-24 18:52:01 +00001100 // Do common shutdown.
1101 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001102
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001103 if (NewAsmPrinter) {
1104 Streamer->Finish();
1105
1106 delete Streamer;
1107 delete Context;
1108 Streamer = 0;
1109 Context = 0;
1110 }
1111
Chris Lattnerdb191f02009-06-24 18:52:01 +00001112 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001113}
1114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115// Include the auto-generated portion of the assembly writer.
1116#include "X86GenAsmWriter.inc"