blob: 31a979dcc5a782e6b647451b12f418b40fd86843 [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"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000029#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000030#include "llvm/MC/MCInst.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000031#include "llvm/MC/MCSectionMachO.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"
David Greene302008d2009-07-14 20:18:05 +000037#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/Mangler.h"
39#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000040#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
44STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
Chris Lattner19b1bd52009-06-19 00:47:33 +000046static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
47 cl::Hidden);
48
Chris Lattner2e845952009-06-24 19:44:36 +000049//===----------------------------------------------------------------------===//
50// Primitive Helper Functions.
51//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000052
53void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000055 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000057 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 else
Edwin Törökbd448e32009-07-14 16:55:14 +000059 llvm_unreachable("Don't know how to print PIC label!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
61
Chris Lattner2e845952009-06-24 19:44:36 +000062/// PrintUnmangledNameSafely - Print out the printable characters in the name.
63/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +000064static void PrintUnmangledNameSafely(const Value *V,
David Greene302008d2009-07-14 20:18:05 +000065 formatted_raw_ostream &OS) {
Daniel Dunbar23e2b802009-07-26 07:49:05 +000066 for (StringRef::iterator it = V->getName().begin(),
67 ie = V->getName().end(); it != ie; ++it)
68 if (isprint(*it))
69 OS << *it;
Chris Lattner2e845952009-06-24 19:44:36 +000070}
Chris Lattner14f791a2009-06-24 19:19:16 +000071
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000072static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
73 const TargetData *TD) {
74 X86MachineFunctionInfo Info;
75 uint64_t Size = 0;
76
77 switch (F->getCallingConv()) {
78 case CallingConv::X86_StdCall:
79 Info.setDecorationStyle(StdCall);
80 break;
81 case CallingConv::X86_FastCall:
82 Info.setDecorationStyle(FastCall);
83 break;
84 default:
85 return Info;
86 }
87
88 unsigned argNum = 1;
89 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
90 AI != AE; ++AI, ++argNum) {
91 const Type* Ty = AI->getType();
92
93 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000094 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000095 Ty = cast<PointerType>(Ty)->getElementType();
96
97 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000098 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000099 }
100
101 // We're not supporting tooooo huge arguments :)
102 Info.setBytesToPopOnReturn((unsigned int)Size);
103 return Info;
104}
105
Chris Lattner8cfe9142009-07-15 04:55:56 +0000106/// DecorateCygMingName - Query FunctionInfoMap and use this information for
107/// various name decorations for Cygwin and MingW.
108void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
109 const GlobalValue *GV) {
110 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
111
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000112 const Function *F = dyn_cast<Function>(GV);
113 if (!F) return;
114
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000115 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000116 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000117 CygMingStubs.insert(Name);
118
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000119 // We don't want to decorate non-stdcall or non-fastcall functions right now
120 unsigned CC = F->getCallingConv();
121 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
122 return;
123
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000124
125 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000126
127 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000128 if (info_item == FunctionInfoMap.end()) {
129 // Calculate apropriate function info and populate map
130 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
131 Info = &FunctionInfoMap[F];
132 } else {
133 Info = &info_item->second;
134 }
135
136 const FunctionType *FT = F->getFunctionType();
137 switch (Info->getDecorationStyle()) {
138 case None:
139 break;
140 case StdCall:
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 break;
146 case FastCall:
147 // "Pure" variadic functions do not receive @0 suffix.
148 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
149 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
150 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
151
152 if (Name[0] == '_') {
153 Name[0] = '@';
154 } else {
155 Name = '@' + Name;
156 }
157 break;
158 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000159 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000160 }
161}
162
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000163void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000164 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166
Chris Lattner8cfe9142009-07-15 04:55:56 +0000167 if (Subtarget->isTargetCygMing())
168 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Chris Lattner2931fe42009-07-29 05:09:30 +0000170 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner1b0ec672009-08-03 22:16:57 +0000171 EmitAlignment(FnAlign, F);
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000174 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000176 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case Function::ExternalLinkage:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 break;
Dale Johannesenf03574c2009-08-13 00:28:52 +0000182 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000183 case Function::LinkOnceAnyLinkage:
184 case Function::LinkOnceODRLinkage:
185 case Function::WeakAnyLinkage:
186 case Function::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
189 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000191 O << "\t.globl\t" << CurrentFnName << "\n"
192 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000194 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 break;
197 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000198
199 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000202 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else if (Subtarget->isTargetCygMing()) {
204 O << "\t.def\t " << CurrentFnName
205 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000206 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
208 << ";\t.endef\n";
209 }
210
211 O << CurrentFnName << ":\n";
212 // Add some workaround for linkonce linkage on Cygwin\MinGW
213 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000214 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000216}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218/// runOnMachineFunction - This uses the printMachineInstruction()
219/// method to print assembly for each instruction.
220///
221bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
222 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000223 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000224 unsigned CC = F->getCallingConv();
225
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000226 SetupMachineFunction(MF);
227 O << "\n\n";
228
229 // Populate function information map. Actually, We don't want to populate
230 // non-stdcall or non-fastcall functions' information right now.
231 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
232 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
233
234 // Print out constants referenced by the function
235 EmitConstantPool(MF.getConstantPool());
236
237 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000238 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000239
240 // Print the 'header' of function
241 emitFunctionHeader(MF);
242
243 // Emit pre-function debug and/or EH information.
244 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000245 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000248 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 I != E; ++I) {
251 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000252 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
253 // This is an entry block or a block that's only reachable via a
254 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
255 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000256 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 O << '\n';
258 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000259 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
260 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000262 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000263 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 printMachineInstruction(II);
265 }
266 }
267
Dale Johannesenf35771f2008-04-08 00:37:56 +0000268 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
269 // If the function is empty, then we need to emit *something*. Otherwise,
270 // the function's label might be associated with something that it wasn't
271 // meant to be associated with. We emit a noop in this situation.
272 // We are assuming inline asms are code.
273 O << "\tnop\n";
274 }
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000277 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000279 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000280 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000281 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
283 // Print out jump tables referenced by the function.
284 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 // We didn't modify anything.
287 return false;
288}
289
Chris Lattnerae8f9592009-07-13 21:53:19 +0000290/// printSymbolOperand - Print a raw symbol reference operand. This handles
291/// jump tables, constant pools, global address and external symbols, all of
292/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000293void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
294 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000295 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000296 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000297 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000299 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000300 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000301 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000302 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000303 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000304 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000306 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000307
308 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000309 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
310 Suffix = "$stub";
311 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
312 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
313 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000314 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000315 Suffix = "$non_lazy_ptr";
316
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000317 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000318 if (Subtarget->isTargetCygMing())
319 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000320
Chris Lattner0cc2b792009-07-09 05:42:07 +0000321 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000322 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
323 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000324
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000325 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
326 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
327 GVStubs[Name] = Mang->getMangledName(GV);
328 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
329 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
330 HiddenGVStubs[Name] = Mang->getMangledName(GV);
331 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
332 FnStubs[Name] = Mang->getMangledName(GV);
333
334 // If the name begins with a dollar-sign, enclose it in parens. We do this
335 // to avoid having it look like an integer immediate to the assembler.
336 if (Name[0] == '$')
337 O << '(' << Name << ')';
338 else
339 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000340
Chris Lattner0cc2b792009-07-09 05:42:07 +0000341 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000342 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000344 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000345 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000346 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000347 FnStubs[Name+"$stub"] = Name;
348 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000349 }
350
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000351 // If the name begins with a dollar-sign, enclose it in parens. We do this
352 // to avoid having it look like an integer immediate to the assembler.
353 if (Name[0] == '$')
354 O << '(' << Name << ')';
355 else
356 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000357 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000359 }
Chris Lattner797a0782009-06-27 05:46:24 +0000360
361 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000363 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000364 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000365 break;
366 case X86II::MO_DARWIN_NONLAZY:
367 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
368 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000369 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000370 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000371 break;
372 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
373 O << " + [.-";
374 PrintPICBaseSymbol();
375 O << ']';
376 break;
377 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000378 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
379 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000380 O << '-';
381 PrintPICBaseSymbol();
382 break;
383 case X86II::MO_TLSGD: O << "@TLSGD"; break;
384 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
385 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
386 case X86II::MO_TPOFF: O << "@TPOFF"; break;
387 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
388 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
389 case X86II::MO_GOT: O << "@GOT"; break;
390 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000391 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 }
393}
394
Chris Lattnerae8f9592009-07-13 21:53:19 +0000395/// print_pcrel_imm - This is used to print an immediate value that ends up
396/// being encoded as a pc-relative value. These print slightly differently, for
397/// example, a $ is not emitted.
398void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
399 const MachineOperand &MO = MI->getOperand(OpNo);
400 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000401 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000402 case MachineOperand::MO_Immediate:
403 O << MO.getImm();
404 return;
405 case MachineOperand::MO_MachineBasicBlock:
406 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
407 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000408 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000409 case MachineOperand::MO_ExternalSymbol:
410 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000411 return;
412 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000413}
414
415
Chris Lattner207a0ca2009-07-13 21:41:08 +0000416
417void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
418 const char *Modifier) {
419 const MachineOperand &MO = MI->getOperand(OpNo);
420 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000421 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000422 case MachineOperand::MO_Register: {
423 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
424 "Virtual registers should not make it this far!");
425 O << '%';
426 unsigned Reg = MO.getReg();
427 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000428 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000429 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
430 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner207a0ca2009-07-13 21:41:08 +0000431 Reg = getX86SubSuperRegister(Reg, VT);
432 }
433 O << TRI->getAsmName(Reg);
434 return;
435 }
436
437 case MachineOperand::MO_Immediate:
438 O << '$' << MO.getImm();
439 return;
440
441 case MachineOperand::MO_JumpTableIndex:
442 case MachineOperand::MO_ConstantPoolIndex:
443 case MachineOperand::MO_GlobalAddress:
444 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000445 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000446 printSymbolOperand(MO);
447 break;
448 }
449 }
450}
451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000453 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 assert(value <= 7 && "Invalid ssecc argument!");
455 switch (value) {
456 case 0: O << "eq"; break;
457 case 1: O << "lt"; break;
458 case 2: O << "le"; break;
459 case 3: O << "unord"; break;
460 case 4: O << "neq"; break;
461 case 5: O << "nlt"; break;
462 case 6: O << "nle"; break;
463 case 7: O << "ord"; break;
464 }
465}
466
Rafael Espindolabca99f72009-04-08 21:14:34 +0000467void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000468 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000469 const MachineOperand &BaseReg = MI->getOperand(Op);
470 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 const MachineOperand &DispSpec = MI->getOperand(Op+3);
472
Chris Lattner791fd482009-07-09 00:27:29 +0000473 // If we really don't want to print out (rip), don't.
474 bool HasBaseReg = BaseReg.getReg() != 0;
475 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
476 BaseReg.getReg() == X86::RIP)
477 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000478
479 // HasParenPart - True if we will print out the () part of the mem ref.
480 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
481
482 if (DispSpec.isImm()) {
483 int DispVal = DispSpec.getImm();
484 if (DispVal || !HasParenPart)
485 O << DispVal;
486 } else {
487 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
488 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000489 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000490 }
491
492 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000493 assert(IndexReg.getReg() != X86::ESP &&
494 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000495
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000496 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000497 if (HasBaseReg)
498 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499
500 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000501 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000502 printOperand(MI, Op+2, Modifier);
503 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000505 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000507 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 }
509}
510
Rafael Espindolabca99f72009-04-08 21:14:34 +0000511void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000512 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000513 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000514 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000515 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000516 printOperand(MI, Op+4, Modifier);
517 O << ':';
518 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000519 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000520}
521
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000522void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000523 const MachineBasicBlock *MBB) const {
524 if (!TAI->getSetDirective())
525 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000526
527 // We don't need .set machinery if we have GOT-style relocations
528 if (Subtarget->isPICStyleGOT())
529 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000530
Evan Cheng6fb06762007-11-09 01:32:10 +0000531 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
532 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000533 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000534 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000535 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000536 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000537 else {
538 O << '-';
539 PrintPICBaseSymbol();
540 O << '\n';
541 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000542}
543
Chris Lattner14f791a2009-06-24 19:19:16 +0000544
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000546 PrintPICBaseSymbol();
547 O << '\n';
548 PrintPICBaseSymbol();
549 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550}
551
552
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000553void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
554 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000555 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000556 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
557 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
558
559 O << JTEntryDirective << ' ';
560
Chris Lattner2e9393c2009-07-10 21:00:45 +0000561 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000562 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
563 << '_' << uid << "_set_" << MBB->getNumber();
564 } else if (Subtarget->isPICStyleGOT()) {
565 printBasicBlockLabel(MBB, false, false, false);
566 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000567 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000568 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000569}
570
Chris Lattner8bb96e42009-06-15 04:42:32 +0000571bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 unsigned Reg = MO.getReg();
573 switch (Mode) {
574 default: return true; // Unknown mode.
575 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000576 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 break;
578 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000579 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 break;
581 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000582 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 break;
584 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000585 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000587 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000588 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000589 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 }
591
Evan Cheng00d04a72008-07-07 22:21:06 +0000592 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 return false;
594}
595
596/// PrintAsmOperand - Print out an operand for an inline asm expression.
597///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000598bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000599 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 const char *ExtraCode) {
601 // Does this asm operand have a single letter operand modifier?
602 if (ExtraCode && ExtraCode[0]) {
603 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000604
Chris Lattnerb6047e62009-07-13 21:48:33 +0000605 const MachineOperand &MO = MI->getOperand(OpNo);
606
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 switch (ExtraCode[0]) {
608 default: return true; // Unknown modifier.
609 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000610 if (MO.isImm())
611 O << MO.getImm();
612 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
613 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000614 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000615 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000617
618 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000619 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000620 O << '*';
621 printOperand(MI, OpNo);
622 return false;
623 }
624 return true;
625
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 case 'b': // Print QImode register
627 case 'h': // Print QImode high register
628 case 'w': // Print HImode register
629 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000630 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000631 if (MO.isReg())
632 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 printOperand(MI, OpNo);
634 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000635
Dale Johannesen375b88b2009-07-07 23:28:22 +0000636 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000637 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000639
Chris Lattnerb6047e62009-07-13 21:48:33 +0000640 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000641 // Note: this is a temporary solution. It should be handled target
642 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000643 if (MO.isImm()) {
644 O << -MO.getImm();
645 return false;
646 }
647 O << '-';
648 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 printOperand(MI, OpNo);
652 return false;
653}
654
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000655bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000657 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000659 if (ExtraCode && ExtraCode[0]) {
660 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000661
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000662 switch (ExtraCode[0]) {
663 default: return true; // Unknown modifier.
664 case 'b': // Print QImode register
665 case 'h': // Print QImode high register
666 case 'w': // Print HImode register
667 case 'k': // Print SImode register
668 case 'q': // Print SImode register
669 // These only apply to registers, ignore on mem.
670 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000671 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000672 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000673 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000674 }
675 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 printMemReference(MI, OpNo);
677 return false;
678}
679
Chris Lattnerf5da5902009-06-20 07:03:18 +0000680static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
681 // Convert registers in the addr mode according to subreg64.
682 for (unsigned i = 0; i != 4; ++i) {
683 if (!MI->getOperand(i).isReg()) continue;
684
685 unsigned Reg = MI->getOperand(i).getReg();
686 if (Reg == 0) continue;
687
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000688 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
Chris Lattnerf5da5902009-06-20 07:03:18 +0000689 }
690}
691
Bill Wendlingb3b11262009-02-06 21:45:08 +0000692/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
693/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694///
695void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
696 ++EmittedInsts;
697
Chris Lattner19b1bd52009-06-19 00:47:33 +0000698 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000699 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
700 O << "\t";
701 printInlineAsm(MI);
702 return;
703 } else if (MI->isLabel()) {
704 printLabel(MI);
705 return;
706 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
707 printDeclare(MI);
708 return;
709 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
710 printImplicitDef(MI);
711 return;
712 }
713
Chris Lattnere67184e2009-06-19 23:59:57 +0000714 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000715 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000716
717 TmpInst.setOpcode(MI->getOpcode());
718
719 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
720 const MachineOperand &MO = MI->getOperand(i);
721
722 MCOperand MCOp;
723 if (MO.isReg()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000724 MCOp = MCOperand::CreateReg(MO.getReg());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000725 } else if (MO.isImm()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000726 MCOp = MCOperand::CreateImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000727 } else if (MO.isMBB()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000728 MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
729 MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000730 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000731 llvm_unreachable("Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000732 }
733
734 TmpInst.addOperand(MCOp);
735 }
736
Chris Lattner6b9f7742009-06-20 07:59:10 +0000737 switch (TmpInst.getOpcode()) {
738 case X86::LEA64_32r:
739 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000740 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000741 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000742 }
743
Chris Lattner19b1bd52009-06-19 00:47:33 +0000744 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000745 printInstruction(&TmpInst);
746 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000747 }
748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 // Call the autogenerated instruction printer routines.
750 printInstruction(MI);
751}
752
Chris Lattnerae982212009-07-21 18:38:57 +0000753void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000754 const TargetData *TD = TM.getTargetData();
755
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000756 if (!GVar->hasInitializer())
757 return; // External global require no code
758
759 // Check to see if this is a special global used by LLVM, if so, emit it.
760 if (EmitSpecialLLVMGlobal(GVar)) {
761 if (Subtarget->isTargetDarwin() &&
762 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000763 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000764 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000765 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000766 O << ".reference .destructors_used\n";
767 }
768 return;
769 }
770
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000771 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000772 Constant *C = GVar->getInitializer();
773 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000774 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000775 unsigned Align = TD->getPreferredAlignmentLog(GVar);
776
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000777 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000778
779 if (Subtarget->isTargetELF())
780 O << "\t.type\t" << name << ",@object\n";
781
Chris Lattner7215c7f2009-08-05 05:21:07 +0000782
783 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
784
785
786
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000787 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000788 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000789 SwitchToSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000790
Chris Lattnerdb727932009-08-04 05:35:56 +0000791 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000792 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000793 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000794 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 if (GVar->hasExternalLinkage()) {
796 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000799 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000800 return;
801 }
802 }
803
804 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000805 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000807
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000809 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000810 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000811 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << "\t.globl " << name << '\n'
815 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000816 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000817 O << name << ":";
818 if (VerboseAsm) {
Dan Gohman167ff152009-08-12 18:55:32 +0000819 O.PadToColumn(TAI->getCommentColumn(), 1);
820 O << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000821 PrintUnmangledNameSafely(GVar, O);
822 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 EmitGlobalConstant(C);
825 return;
826 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000827 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000828 if (TAI->getCOMMDirectiveTakesAlignment())
829 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 }
831 } else {
832 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000833 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000836 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 }
Evan Cheng11db8142009-03-24 00:17:40 +0000840 if (VerboseAsm) {
Dan Gohman167ff152009-08-12 18:55:32 +0000841 O.PadToColumn(TAI->getCommentColumn(), 1);
842 O << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000843 PrintUnmangledNameSafely(GVar, O);
844 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000845 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 return;
847 }
848 }
849
850 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000851 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000852 case GlobalValue::LinkOnceAnyLinkage:
853 case GlobalValue::LinkOnceODRLinkage:
854 case GlobalValue::WeakAnyLinkage:
855 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +0000856 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << "\t.globl " << name << '\n'
859 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000864 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 }
866 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000867 case GlobalValue::DLLExportLinkage:
868 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 // FIXME: appending linkage variables should go into a section of
870 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000871 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000875 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000876 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000877 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000878 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000879 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 }
881
882 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000883 O << name << ":";
884 if (VerboseAsm){
Dan Gohman167ff152009-08-12 18:55:32 +0000885 O.PadToColumn(TAI->getCommentColumn(), 1);
886 O << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000887 PrintUnmangledNameSafely(GVar, O);
888 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000893 EmitGlobalConstant(C);
894}
895
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897 // Print out module-level global variables here.
898 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000899 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000900 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000901 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000902 }
903
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000904 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +0000905 // All darwin targets use mach-o.
906 TargetLoweringObjectFileMachO &TLOFMacho =
907 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +0000908
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000909 // Add the (possibly multiple) personalities to the set of global value
910 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000911 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000912 const std::vector<Function*> &Personalities = MMI->getPersonalities();
913 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000914 if (Personalities[i])
915 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
916 true /*private label*/)] =
917 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000918 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000919 }
920
Chris Lattner2e860262009-06-24 18:24:09 +0000921 // Output stubs for dynamically-linked functions
922 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000923 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000924 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
925 MCSectionMachO::S_SYMBOL_STUBS |
926 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
927 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
928 5, SectionKind::getMetadata());
Chris Lattner92873462009-08-03 21:53:27 +0000929 SwitchToSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000930 for (StringMap<std::string>::iterator I = FnStubs.begin(),
931 E = FnStubs.end(); I != E; ++I)
932 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
933 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000934 O << '\n';
935 }
936
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000937 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000938 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000939 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000940 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
941 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +0000942 SectionKind::getMetadata());
943 SwitchToSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000944 for (StringMap<std::string>::iterator I = GVStubs.begin(),
945 E = GVStubs.end(); I != E; ++I)
946 O << I->getKeyData() << ":\n\t.indirect_symbol "
947 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000948 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000949
Evan Chenga65854f2008-12-05 01:06:39 +0000950 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000951 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000952 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000953 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
954 E = HiddenGVStubs.end(); I != E; ++I)
955 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
956 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000957 }
958
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959 // Funny Darwin hack: This flag tells the linker that no global symbols
960 // contain code that falls through to other global symbols (e.g. the obvious
961 // implementation of multiple entry points). If this doesn't occur, the
962 // linker can safely perform dead code stripping. Since LLVM never
963 // generates code that does this, it is always safe to set.
964 O << "\t.subsections_via_symbols\n";
965 } else if (Subtarget->isTargetCygMing()) {
966 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000967 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000968 i != e; ++i) {
969 O << "\t.def\t " << i->getKeyData()
970 << ";\t.scl\t" << COFF::C_EXT
971 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
972 << ";\t.endef\n";
973 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000974 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000975
Chris Lattnerdb191f02009-06-24 18:52:01 +0000976
977 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +0000978 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
979 // dllexport symbols only exist on coff targets.
980 TargetLoweringObjectFileCOFF &TLOFMacho =
981 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
982
983 SwitchToSection(TLOFMacho.getCOFFSection(".section .drectve", true,
984 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +0000985
986 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
987 e = DLLExportedGVs.end(); i != e; ++i)
988 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +0000989
990 for (StringSet<>::iterator i = DLLExportedFns.begin(),
991 e = DLLExportedFns.end();
992 i != e; ++i)
993 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
994 }
995
Chris Lattnerdb191f02009-06-24 18:52:01 +0000996 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000997 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000998}
999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000// Include the auto-generated portion of the assembly writer.
1001#include "X86GenAsmWriter.inc"