blob: 887235ce7b25544235f9c164935e6848a2cec19b [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"
Edwin Török675d5622009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000038#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetAsmInfo.h"
40#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041using namespace llvm;
42
43STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
Chris Lattner19b1bd52009-06-19 00:47:33 +000045static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
46 cl::Hidden);
47
Chris Lattner2e845952009-06-24 19:44:36 +000048//===----------------------------------------------------------------------===//
49// Primitive Helper Functions.
50//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000051
52void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000056 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 else
Edwin Török151026f2009-07-12 07:15:17 +000058 LLVM_UNREACHABLE( "Don't know how to print PIC label!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
Chris Lattner2e845952009-06-24 19:44:36 +000061/// PrintUnmangledNameSafely - Print out the printable characters in the name.
62/// Don't print things like \\n or \\0.
63static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
64 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
65 Name != E; ++Name)
66 if (isprint(*Name))
67 OS << *Name;
68}
Chris Lattner14f791a2009-06-24 19:19:16 +000069
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000070static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
71 const TargetData *TD) {
72 X86MachineFunctionInfo Info;
73 uint64_t Size = 0;
74
75 switch (F->getCallingConv()) {
76 case CallingConv::X86_StdCall:
77 Info.setDecorationStyle(StdCall);
78 break;
79 case CallingConv::X86_FastCall:
80 Info.setDecorationStyle(FastCall);
81 break;
82 default:
83 return Info;
84 }
85
86 unsigned argNum = 1;
87 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
88 AI != AE; ++AI, ++argNum) {
89 const Type* Ty = AI->getType();
90
91 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000092 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000093 Ty = cast<PointerType>(Ty)->getElementType();
94
95 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000096 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000097 }
98
99 // We're not supporting tooooo huge arguments :)
100 Info.setBytesToPopOnReturn((unsigned int)Size);
101 return Info;
102}
103
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000104/// decorateName - Query FunctionInfoMap and use this information for various
105/// name decoration.
106void X86ATTAsmPrinter::decorateName(std::string &Name,
107 const GlobalValue *GV) {
108 const Function *F = dyn_cast<Function>(GV);
109 if (!F) return;
110
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000111 // Save function name for later type emission.
112 if (Subtarget->isTargetCygMing() && F->isDeclaration())
113 CygMingStubs.insert(Name);
114
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000115 // We don't want to decorate non-stdcall or non-fastcall functions right now
116 unsigned CC = F->getCallingConv();
117 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
118 return;
119
120 // Decorate names only when we're targeting Cygwin/Mingw32 targets
121 if (!Subtarget->isTargetCygMing())
122 return;
123
124 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
125
126 const X86MachineFunctionInfo *Info;
127 if (info_item == FunctionInfoMap.end()) {
128 // Calculate apropriate function info and populate map
129 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
130 Info = &FunctionInfoMap[F];
131 } else {
132 Info = &info_item->second;
133 }
134
135 const FunctionType *FT = F->getFunctionType();
136 switch (Info->getDecorationStyle()) {
137 case None:
138 break;
139 case StdCall:
140 // "Pure" variadic functions do not receive @0 suffix.
141 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
142 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
143 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
144 break;
145 case FastCall:
146 // "Pure" variadic functions do not receive @0 suffix.
147 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
148 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
149 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
150
151 if (Name[0] == '_') {
152 Name[0] = '@';
153 } else {
154 Name = '@' + Name;
155 }
156 break;
157 default:
Edwin Török675d5622009-07-11 20:10:48 +0000158 LLVM_UNREACHABLE( "Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000159 }
160}
161
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000162void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000163 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000166 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000168 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 switch (F->getLinkage()) {
Edwin Török675d5622009-07-11 20:10:48 +0000170 default: LLVM_UNREACHABLE( "Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000172 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000173 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 break;
175 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000177 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000180 case Function::LinkOnceAnyLinkage:
181 case Function::LinkOnceODRLinkage:
182 case Function::WeakAnyLinkage:
183 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000184 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << CurrentFnName << '\n';
187 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << "\n"
190 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 }
194 break;
195 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000196
197 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198
199 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000200 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 else if (Subtarget->isTargetCygMing()) {
202 O << "\t.def\t " << CurrentFnName
203 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000204 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
206 << ";\t.endef\n";
207 }
208
209 O << CurrentFnName << ":\n";
210 // Add some workaround for linkonce linkage on Cygwin\MinGW
211 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000212 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000216/// runOnMachineFunction - This uses the printMachineInstruction()
217/// method to print assembly for each instruction.
218///
219bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
220 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000221 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000222 unsigned CC = F->getCallingConv();
223
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000224 SetupMachineFunction(MF);
225 O << "\n\n";
226
227 // Populate function information map. Actually, We don't want to populate
228 // non-stdcall or non-fastcall functions' information right now.
229 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
230 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
231
232 // Print out constants referenced by the function
233 EmitConstantPool(MF.getConstantPool());
234
235 if (F->hasDLLExportLinkage())
236 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
237
238 // Print the 'header' of function
239 emitFunctionHeader(MF);
240
241 // Emit pre-function debug and/or EH information.
242 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000243 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000244
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000246 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
248 I != E; ++I) {
249 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000250 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
251 // This is an entry block or a block that's only reachable via a
252 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
253 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000254 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 O << '\n';
256 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000257 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
258 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000260 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000261 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 printMachineInstruction(II);
263 }
264 }
265
Dale Johannesenf35771f2008-04-08 00:37:56 +0000266 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
267 // If the function is empty, then we need to emit *something*. Otherwise,
268 // the function's label might be associated with something that it wasn't
269 // meant to be associated with. We emit a noop in this situation.
270 // We are assuming inline asms are code.
271 O << "\tnop\n";
272 }
273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000275 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000277 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000278 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000279 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280
281 // Print out jump tables referenced by the function.
282 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000283
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000284 O.flush();
285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 // We didn't modify anything.
287 return false;
288}
289
Chris Lattner357a0ca2009-06-20 19:34:09 +0000290/// print_pcrel_imm - This is used to print an immediate value that ends up
291/// being encoded as a pc-relative value. These print slightly differently, for
292/// example, a $ is not emitted.
293void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
294 const MachineOperand &MO = MI->getOperand(OpNo);
295 switch (MO.getType()) {
Edwin Török675d5622009-07-11 20:10:48 +0000296 default: LLVM_UNREACHABLE( "Unknown pcrel immediate operand");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000297 case MachineOperand::MO_Immediate:
298 O << MO.getImm();
299 return;
300 case MachineOperand::MO_MachineBasicBlock:
301 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
302 return;
303
304 case MachineOperand::MO_GlobalAddress: {
305 const GlobalValue *GV = MO.getGlobal();
306 std::string Name = Mang->getValueName(GV);
307 decorateName(Name, GV);
308
309 bool needCloseParen = false;
310 if (Name[0] == '$') {
311 // The name begins with a dollar-sign. In order to avoid having it look
312 // like an integer immediate to the assembler, enclose it in parens.
313 O << '(';
314 needCloseParen = true;
315 }
316
Chris Lattner48837612009-07-09 05:27:35 +0000317 // Handle dllimport linkage.
318 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner877a0bc2009-07-10 07:50:52 +0000319 O << "__imp_" << Name;
320 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner48837612009-07-09 05:27:35 +0000321 FnStubs.insert(Name);
322 printSuffixedName(Name, "$stub");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000323 } else {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000324 O << Name;
Chris Lattner357a0ca2009-06-20 19:34:09 +0000325 }
326
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000327 if (needCloseParen)
328 O << ')';
329
Chris Lattner48837612009-07-09 05:27:35 +0000330 // Assemble call via PLT for externally visible symbols.
331 if (MO.getTargetFlags() == X86II::MO_PLT)
332 O << "@PLT";
333
Chris Lattner357a0ca2009-06-20 19:34:09 +0000334 printOffset(MO.getOffset());
335
Chris Lattner357a0ca2009-06-20 19:34:09 +0000336 return;
337 }
338
339 case MachineOperand::MO_ExternalSymbol: {
340 bool needCloseParen = false;
341 std::string Name(TAI->getGlobalPrefix());
342 Name += MO.getSymbolName();
Chris Lattner48837612009-07-09 05:27:35 +0000343
Chris Lattner357a0ca2009-06-20 19:34:09 +0000344 if (Name[0] == '$') {
345 // The name begins with a dollar-sign. In order to avoid having it look
346 // like an integer immediate to the assembler, enclose it in parens.
347 O << '(';
348 needCloseParen = true;
349 }
350
Chris Lattner48837612009-07-09 05:27:35 +0000351 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
352 FnStubs.insert(Name);
353 printSuffixedName(Name, "$stub");
354 } else {
355 O << Name;
356 }
Chris Lattner357a0ca2009-06-20 19:34:09 +0000357
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000358 if (MO.getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS) {
359 O << " + [.-";
360 PrintPICBaseSymbol();
361 O << ']';
Chris Lattner357a0ca2009-06-20 19:34:09 +0000362 }
363
Chris Lattner8e8afe42009-07-09 05:02:21 +0000364 if (MO.getTargetFlags() == X86II::MO_PLT)
Chris Lattner13d6c2d2009-06-25 17:38:33 +0000365 O << "@PLT";
366
Chris Lattner357a0ca2009-06-20 19:34:09 +0000367 if (needCloseParen)
368 O << ')';
369
370 return;
371 }
372 }
373}
374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
Chris Lattner207a0ca2009-07-13 21:41:08 +0000376void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
377 switch (MO.getType()) {
378 default: LLVM_UNREACHABLE("unknown symbol type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 case MachineOperand::MO_JumpTableIndex: {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000380 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000381 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000382 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 }
384 case MachineOperand::MO_ConstantPoolIndex: {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000385 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000386 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000387 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000388 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 }
390 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000391 const GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000393 decorateName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000394
Chris Lattner797a0782009-06-27 05:46:24 +0000395 bool needCloseParen = false;
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000396 if (Name[0] == '$') {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 // The name begins with a dollar-sign. In order to avoid having it look
398 // like an integer immediate to the assembler, enclose it in parens.
399 O << '(';
400 needCloseParen = true;
401 }
Chris Lattner207a0ca2009-07-13 21:41:08 +0000402
Chris Lattner0cc2b792009-07-09 05:42:07 +0000403 // Handle dllimport linkage.
Chris Lattnera3bde622009-07-09 06:59:17 +0000404 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) {
405 O << "__imp_" << Name;
406 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
407 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
408 GVStubs.insert(Name);
409 printSuffixedName(Name, "$non_lazy_ptr");
410 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
411 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
412 HiddenGVStubs.insert(Name);
413 printSuffixedName(Name, "$non_lazy_ptr");
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000414 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
415 FnStubs.insert(Name);
416 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 }
Chris Lattner207a0ca2009-07-13 21:41:08 +0000420
Chris Lattnerdabfe572009-06-21 02:22:53 +0000421 if (needCloseParen)
422 O << ')';
423
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000424 // Assemble call via PLT for externally visible symbols.
425 if (MO.getTargetFlags() == X86II::MO_PLT)
426 O << "@PLT";
427
Chris Lattner0cc2b792009-07-09 05:42:07 +0000428 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000429 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 }
Chris Lattner797a0782009-06-27 05:46:24 +0000431 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerce003822009-06-26 21:47:27 +0000432 /// NOTE: MO_ExternalSymbol in a non-pcrel_imm context is *only* generated
433 /// by _GLOBAL_OFFSET_TABLE_ on X86-32. All others are call operands, which
434 /// are pcrel_imm's.
Chris Lattner0cc2b792009-07-09 05:42:07 +0000435 assert(!Subtarget->is64Bit());
Chris Lattnerce003822009-06-26 21:47:27 +0000436 O << TAI->getGlobalPrefix();
437 O << MO.getSymbolName();
Chris Lattner797a0782009-06-27 05:46:24 +0000438 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 }
Chris Lattner797a0782009-06-27 05:46:24 +0000440
441 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 default:
Edwin Török675d5622009-07-11 20:10:48 +0000443 LLVM_UNREACHABLE( "Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000444 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000445 break;
446 case X86II::MO_DARWIN_NONLAZY:
447 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
448 case X86II::MO_DLLIMPORT:
449 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000450 break;
451 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
452 O << " + [.-";
453 PrintPICBaseSymbol();
454 O << ']';
455 break;
456 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000457 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
458 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000459 O << '-';
460 PrintPICBaseSymbol();
461 break;
462 case X86II::MO_TLSGD: O << "@TLSGD"; break;
463 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
464 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
465 case X86II::MO_TPOFF: O << "@TPOFF"; break;
466 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
467 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
468 case X86II::MO_GOT: O << "@GOT"; break;
469 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 }
471}
472
Chris Lattner207a0ca2009-07-13 21:41:08 +0000473
474void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
475 const char *Modifier) {
476 const MachineOperand &MO = MI->getOperand(OpNo);
477 switch (MO.getType()) {
478 default: LLVM_UNREACHABLE("unknown operand type!");
479 case MachineOperand::MO_Register: {
480 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
481 "Virtual registers should not make it this far!");
482 O << '%';
483 unsigned Reg = MO.getReg();
484 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
485 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
486 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
487 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
488 Reg = getX86SubSuperRegister(Reg, VT);
489 }
490 O << TRI->getAsmName(Reg);
491 return;
492 }
493
494 case MachineOperand::MO_Immediate:
495 O << '$' << MO.getImm();
496 return;
497
498 case MachineOperand::MO_JumpTableIndex:
499 case MachineOperand::MO_ConstantPoolIndex:
500 case MachineOperand::MO_GlobalAddress:
501 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000502 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000503 printSymbolOperand(MO);
504 break;
505 }
506 }
507}
508
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000510 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 assert(value <= 7 && "Invalid ssecc argument!");
512 switch (value) {
513 case 0: O << "eq"; break;
514 case 1: O << "lt"; break;
515 case 2: O << "le"; break;
516 case 3: O << "unord"; break;
517 case 4: O << "neq"; break;
518 case 5: O << "nlt"; break;
519 case 6: O << "nle"; break;
520 case 7: O << "ord"; break;
521 }
522}
523
Rafael Espindolabca99f72009-04-08 21:14:34 +0000524void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000525 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000526 const MachineOperand &BaseReg = MI->getOperand(Op);
527 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 const MachineOperand &DispSpec = MI->getOperand(Op+3);
529
Chris Lattner791fd482009-07-09 00:27:29 +0000530 // If we really don't want to print out (rip), don't.
531 bool HasBaseReg = BaseReg.getReg() != 0;
532 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
533 BaseReg.getReg() == X86::RIP)
534 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000535
536 // HasParenPart - True if we will print out the () part of the mem ref.
537 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
538
539 if (DispSpec.isImm()) {
540 int DispVal = DispSpec.getImm();
541 if (DispVal || !HasParenPart)
542 O << DispVal;
543 } else {
544 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
545 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000546 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000547 }
548
549 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000550 assert(IndexReg.getReg() != X86::ESP &&
551 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000552
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000553 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000554 if (HasBaseReg)
555 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556
557 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000558 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000559 printOperand(MI, Op+2, Modifier);
560 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000562 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000564 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 }
566}
567
Rafael Espindolabca99f72009-04-08 21:14:34 +0000568void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000569 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000570 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000571 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000572 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000573 printOperand(MI, Op+4, Modifier);
574 O << ':';
575 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000576 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000577}
578
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000579void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000580 const MachineBasicBlock *MBB) const {
581 if (!TAI->getSetDirective())
582 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000583
584 // We don't need .set machinery if we have GOT-style relocations
585 if (Subtarget->isPICStyleGOT())
586 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000587
Evan Cheng6fb06762007-11-09 01:32:10 +0000588 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
589 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000590 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000591 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000592 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000593 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000594 else {
595 O << '-';
596 PrintPICBaseSymbol();
597 O << '\n';
598 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000599}
600
Chris Lattner14f791a2009-06-24 19:19:16 +0000601
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000603 PrintPICBaseSymbol();
604 O << '\n';
605 PrintPICBaseSymbol();
606 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607}
608
609
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000610void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
611 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000612 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000613 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
614 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
615
616 O << JTEntryDirective << ' ';
617
Chris Lattner2e9393c2009-07-10 21:00:45 +0000618 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000619 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
620 << '_' << uid << "_set_" << MBB->getNumber();
621 } else if (Subtarget->isPICStyleGOT()) {
622 printBasicBlockLabel(MBB, false, false, false);
623 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000624 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000625 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000626}
627
Chris Lattner8bb96e42009-06-15 04:42:32 +0000628bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 unsigned Reg = MO.getReg();
630 switch (Mode) {
631 default: return true; // Unknown mode.
632 case 'b': // Print QImode register
633 Reg = getX86SubSuperRegister(Reg, MVT::i8);
634 break;
635 case 'h': // Print QImode high register
636 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
637 break;
638 case 'w': // Print HImode register
639 Reg = getX86SubSuperRegister(Reg, MVT::i16);
640 break;
641 case 'k': // Print SImode register
642 Reg = getX86SubSuperRegister(Reg, MVT::i32);
643 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000644 case 'q': // Print DImode register
645 Reg = getX86SubSuperRegister(Reg, MVT::i64);
646 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 }
648
Evan Cheng00d04a72008-07-07 22:21:06 +0000649 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 return false;
651}
652
653/// PrintAsmOperand - Print out an operand for an inline asm expression.
654///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000655bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000656 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 const char *ExtraCode) {
658 // Does this asm operand have a single letter operand modifier?
659 if (ExtraCode && ExtraCode[0]) {
660 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000661
Chris Lattnerb6047e62009-07-13 21:48:33 +0000662 const MachineOperand &MO = MI->getOperand(OpNo);
663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 switch (ExtraCode[0]) {
665 default: return true; // Unknown modifier.
666 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000667 if (MO.isImm())
668 O << MO.getImm();
669 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
670 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000671 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000672 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000674
675 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000676 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000677 O << '*';
678 printOperand(MI, OpNo);
679 return false;
680 }
681 return true;
682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 case 'b': // Print QImode register
684 case 'h': // Print QImode high register
685 case 'w': // Print HImode register
686 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000687 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000688 if (MO.isReg())
689 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 printOperand(MI, OpNo);
691 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000692
Dale Johannesen375b88b2009-07-07 23:28:22 +0000693 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000694 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000696
Chris Lattnerb6047e62009-07-13 21:48:33 +0000697 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000698 // Note: this is a temporary solution. It should be handled target
699 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000700 if (MO.isImm()) {
701 O << -MO.getImm();
702 return false;
703 }
704 O << '-';
705 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 printOperand(MI, OpNo);
709 return false;
710}
711
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000712bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000714 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000716 if (ExtraCode && ExtraCode[0]) {
717 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000718
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000719 switch (ExtraCode[0]) {
720 default: return true; // Unknown modifier.
721 case 'b': // Print QImode register
722 case 'h': // Print QImode high register
723 case 'w': // Print HImode register
724 case 'k': // Print SImode register
725 case 'q': // Print SImode register
726 // These only apply to registers, ignore on mem.
727 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000728 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000729 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000730 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000731 }
732 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 printMemReference(MI, OpNo);
734 return false;
735}
736
Chris Lattnerf5da5902009-06-20 07:03:18 +0000737static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
738 // Convert registers in the addr mode according to subreg64.
739 for (unsigned i = 0; i != 4; ++i) {
740 if (!MI->getOperand(i).isReg()) continue;
741
742 unsigned Reg = MI->getOperand(i).getReg();
743 if (Reg == 0) continue;
744
745 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
746 }
747}
748
Bill Wendlingb3b11262009-02-06 21:45:08 +0000749/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
750/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751///
752void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
753 ++EmittedInsts;
754
Chris Lattner19b1bd52009-06-19 00:47:33 +0000755 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000756 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
757 O << "\t";
758 printInlineAsm(MI);
759 return;
760 } else if (MI->isLabel()) {
761 printLabel(MI);
762 return;
763 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
764 printDeclare(MI);
765 return;
766 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
767 printImplicitDef(MI);
768 return;
769 }
770
Chris Lattnere67184e2009-06-19 23:59:57 +0000771 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000772 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000773
774 TmpInst.setOpcode(MI->getOpcode());
775
776 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
777 const MachineOperand &MO = MI->getOperand(i);
778
779 MCOperand MCOp;
780 if (MO.isReg()) {
781 MCOp.MakeReg(MO.getReg());
782 } else if (MO.isImm()) {
783 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000784 } else if (MO.isMBB()) {
785 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000786 } else {
Edwin Török675d5622009-07-11 20:10:48 +0000787 LLVM_UNREACHABLE( "Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000788 }
789
790 TmpInst.addOperand(MCOp);
791 }
792
Chris Lattner6b9f7742009-06-20 07:59:10 +0000793 switch (TmpInst.getOpcode()) {
794 case X86::LEA64_32r:
795 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000796 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000797 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000798 }
799
Chris Lattner19b1bd52009-06-19 00:47:33 +0000800 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000801 printInstruction(&TmpInst);
802 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000803 }
804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // Call the autogenerated instruction printer routines.
806 printInstruction(MI);
807}
808
Devang Patelc20079e2009-06-20 01:00:07 +0000809/// doInitialization
810bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000811 if (NewAsmPrinter) {
812 Context = new MCContext();
813 // FIXME: Send this to "O" instead of outs(). For now, we force it to
814 // stdout to make it easy to compare.
815 Streamer = createAsmStreamer(*Context, outs());
816 }
817
Devang Patelc20079e2009-06-20 01:00:07 +0000818 return AsmPrinter::doInitialization(M);
819}
820
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000822 const TargetData *TD = TM.getTargetData();
823
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 if (!GVar->hasInitializer())
825 return; // External global require no code
826
827 // Check to see if this is a special global used by LLVM, if so, emit it.
828 if (EmitSpecialLLVMGlobal(GVar)) {
829 if (Subtarget->isTargetDarwin() &&
830 TM.getRelocationModel() == Reloc::Static) {
831 if (GVar->getName() == "llvm.global_ctors")
832 O << ".reference .constructors_used\n";
833 else if (GVar->getName() == "llvm.global_dtors")
834 O << ".reference .destructors_used\n";
835 }
836 return;
837 }
838
839 std::string name = Mang->getValueName(GVar);
840 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000841 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000842 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000844 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 unsigned Align = TD->getPreferredAlignmentLog(GVar);
846
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000847 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848
849 if (Subtarget->isTargetELF())
850 O << "\t.type\t" << name << ",@object\n";
851
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000852 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000853
Evan Chengcf84b142009-02-18 02:19:52 +0000854 if (C->isNullValue() && !GVar->hasSection() &&
855 !(Subtarget->isTargetDarwin() &&
856 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000857 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858 if (GVar->hasExternalLinkage()) {
859 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000860 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 return;
864 }
865 }
866
867 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000868 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000870
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000871 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000872 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000875 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000877 O << "\t.globl " << name << '\n'
878 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000879 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000880 O << name << ":";
881 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000882 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000883 PrintUnmangledNameSafely(GVar, O);
884 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000885 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886 EmitGlobalConstant(C);
887 return;
888 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000890 if (TAI->getCOMMDirectiveTakesAlignment())
891 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 }
893 } else {
894 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000895 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000896 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000897 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000898 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000899 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000900 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000901 }
Evan Cheng11db8142009-03-24 00:17:40 +0000902 if (VerboseAsm) {
903 O << "\t\t" << TAI->getCommentString() << ' ';
904 PrintUnmangledNameSafely(GVar, O);
905 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000906 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000907 return;
908 }
909 }
910
911 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000912 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000913 case GlobalValue::LinkOnceAnyLinkage:
914 case GlobalValue::LinkOnceODRLinkage:
915 case GlobalValue::WeakAnyLinkage:
916 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000917 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000918 O << "\t.globl " << name << '\n'
919 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000920 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000921 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000922 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000923 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000924 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000925 }
926 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000927 case GlobalValue::DLLExportLinkage:
928 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000929 // FIXME: appending linkage variables should go into a section of
930 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000931 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000932 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000933 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000934 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000935 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000936 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000937 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000938 default:
Edwin Török675d5622009-07-11 20:10:48 +0000939 LLVM_UNREACHABLE( "Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000940 }
941
942 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000943 O << name << ":";
944 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000945 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000946 PrintUnmangledNameSafely(GVar, O);
947 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000948 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000949 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000950 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000951
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000952 EmitGlobalConstant(C);
953}
954
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000955bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000956 // Print out module-level global variables here.
957 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000958 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000959 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000960
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000961 if (I->hasDLLExportLinkage())
962 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000963 }
964
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000965 if (Subtarget->isTargetDarwin()) {
966 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000967
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000968 // Add the (possibly multiple) personalities to the set of global value
969 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000970 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000971 const std::vector<Function*> &Personalities = MMI->getPersonalities();
972 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
973 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +0000974 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000975 std::string Name = Mang->getValueName(Personalities[i]);
976 decorateName(Name, Personalities[i]);
977 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +0000978 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000979 }
980
Chris Lattner2e860262009-06-24 18:24:09 +0000981 // Output stubs for dynamically-linked functions
982 if (!FnStubs.empty()) {
983 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
984 I != E; ++I) {
985 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
986 "self_modifying_code+pure_instructions,5", 0);
987 const char *Name = I->getKeyData();
988 printSuffixedName(Name, "$stub");
989 O << ":\n"
990 "\t.indirect_symbol " << Name << "\n"
991 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
992 }
993 O << '\n';
994 }
995
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000996 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000997 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000998 SwitchToDataSection(
999 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001000 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1001 I != E; ++I) {
1002 const char *Name = I->getKeyData();
1003 printSuffixedName(Name, "$non_lazy_ptr");
1004 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1005 }
1006 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001007
Evan Chenga65854f2008-12-05 01:06:39 +00001008 if (!HiddenGVStubs.empty()) {
1009 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001010 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001011 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1012 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001013 const char *Name = I->getKeyData();
1014 printSuffixedName(Name, "$non_lazy_ptr");
1015 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1016 }
Evan Chenga65854f2008-12-05 01:06:39 +00001017 }
1018
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001019 // Funny Darwin hack: This flag tells the linker that no global symbols
1020 // contain code that falls through to other global symbols (e.g. the obvious
1021 // implementation of multiple entry points). If this doesn't occur, the
1022 // linker can safely perform dead code stripping. Since LLVM never
1023 // generates code that does this, it is always safe to set.
1024 O << "\t.subsections_via_symbols\n";
1025 } else if (Subtarget->isTargetCygMing()) {
1026 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001027 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001028 i != e; ++i) {
1029 O << "\t.def\t " << i->getKeyData()
1030 << ";\t.scl\t" << COFF::C_EXT
1031 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1032 << ";\t.endef\n";
1033 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001034 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001035
Chris Lattnerdb191f02009-06-24 18:52:01 +00001036
1037 // Output linker support code for dllexported globals on windows.
1038 if (!DLLExportedGVs.empty()) {
1039 SwitchToDataSection(".section .drectve");
1040
1041 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1042 e = DLLExportedGVs.end(); i != e; ++i)
1043 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1044 }
1045
1046 if (!DLLExportedFns.empty()) {
1047 SwitchToDataSection(".section .drectve");
1048
1049 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1050 e = DLLExportedFns.end();
1051 i != e; ++i)
1052 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1053 }
1054
Chris Lattnerdb191f02009-06-24 18:52:01 +00001055 // Do common shutdown.
1056 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001057
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001058 if (NewAsmPrinter) {
1059 Streamer->Finish();
1060
1061 delete Streamer;
1062 delete Context;
1063 Streamer = 0;
1064 Context = 0;
1065 }
1066
Chris Lattnerdb191f02009-06-24 18:52:01 +00001067 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001068}
1069
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070// Include the auto-generated portion of the assembly writer.
1071#include "X86GenAsmWriter.inc"