blob: 2cec3736a38976b8914e3074d62dde5be514f2b4 [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"
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"
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ökbd448e32009-07-14 16:55:14 +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.
David Greene302008d2009-07-14 20:18:05 +000063static void PrintUnmangledNameSafely(const Value *V,
64 formatted_raw_ostream &OS) {
Chris Lattner2e845952009-06-24 19:44:36 +000065 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
66 Name != E; ++Name)
67 if (isprint(*Name))
68 OS << *Name;
69}
Chris Lattner14f791a2009-06-24 19:19:16 +000070
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000071static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
72 const TargetData *TD) {
73 X86MachineFunctionInfo Info;
74 uint64_t Size = 0;
75
76 switch (F->getCallingConv()) {
77 case CallingConv::X86_StdCall:
78 Info.setDecorationStyle(StdCall);
79 break;
80 case CallingConv::X86_FastCall:
81 Info.setDecorationStyle(FastCall);
82 break;
83 default:
84 return Info;
85 }
86
87 unsigned argNum = 1;
88 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
89 AI != AE; ++AI, ++argNum) {
90 const Type* Ty = AI->getType();
91
92 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000093 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000094 Ty = cast<PointerType>(Ty)->getElementType();
95
96 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000097 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000098 }
99
100 // We're not supporting tooooo huge arguments :)
101 Info.setBytesToPopOnReturn((unsigned int)Size);
102 return Info;
103}
104
Chris Lattner8cfe9142009-07-15 04:55:56 +0000105/// DecorateCygMingName - Query FunctionInfoMap and use this information for
106/// various name decorations for Cygwin and MingW.
107void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
108 const GlobalValue *GV) {
109 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
110
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000111 const Function *F = dyn_cast<Function>(GV);
112 if (!F) return;
113
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000114 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000115 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000116 CygMingStubs.insert(Name);
117
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000118 // We don't want to decorate non-stdcall or non-fastcall functions right now
119 unsigned CC = F->getCallingConv();
120 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
121 return;
122
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000123
124 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000125
126 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000127 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ökbd448e32009-07-14 16:55:14 +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
Chris Lattner8cfe9142009-07-15 04:55:56 +0000166 if (Subtarget->isTargetCygMing())
167 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000169 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000171 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000173 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000174 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 break;
176 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000178 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000179 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000181 case Function::LinkOnceAnyLinkage:
182 case Function::LinkOnceODRLinkage:
183 case Function::WeakAnyLinkage:
184 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000185 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << CurrentFnName << '\n';
188 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << "\t.globl\t" << CurrentFnName << "\n"
191 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000193 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195 break;
196 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000197
198 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
200 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000201 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 else if (Subtarget->isTargetCygMing()) {
203 O << "\t.def\t " << CurrentFnName
204 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000205 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
207 << ";\t.endef\n";
208 }
209
210 O << CurrentFnName << ":\n";
211 // Add some workaround for linkonce linkage on Cygwin\MinGW
212 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000213 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000215}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217/// runOnMachineFunction - This uses the printMachineInstruction()
218/// method to print assembly for each instruction.
219///
220bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
221 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000222 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000223 unsigned CC = F->getCallingConv();
224
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000225 SetupMachineFunction(MF);
226 O << "\n\n";
227
228 // Populate function information map. Actually, We don't want to populate
229 // non-stdcall or non-fastcall functions' information right now.
230 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
231 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
232
233 // Print out constants referenced by the function
234 EmitConstantPool(MF.getConstantPool());
235
236 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000237 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000238
239 // Print the 'header' of function
240 emitFunctionHeader(MF);
241
242 // Emit pre-function debug and/or EH information.
243 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000244 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000247 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
249 I != E; ++I) {
250 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000251 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
252 // This is an entry block or a block that's only reachable via a
253 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
254 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000255 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 O << '\n';
257 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000258 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
259 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000261 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000262 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 printMachineInstruction(II);
264 }
265 }
266
Dale Johannesenf35771f2008-04-08 00:37:56 +0000267 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
268 // If the function is empty, then we need to emit *something*. Otherwise,
269 // the function's label might be associated with something that it wasn't
270 // meant to be associated with. We emit a noop in this situation.
271 // We are assuming inline asms are code.
272 O << "\tnop\n";
273 }
274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000276 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000278 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000279 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000280 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
282 // Print out jump tables referenced by the function.
283 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000284
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000285 O.flush();
286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // We didn't modify anything.
288 return false;
289}
290
Chris Lattnerae8f9592009-07-13 21:53:19 +0000291/// printSymbolOperand - Print a raw symbol reference operand. This handles
292/// jump tables, constant pools, global address and external symbols, all of
293/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000294void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
295 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000296 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000297 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000298 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000299 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000300 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000301 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000302 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000303 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000304 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000305 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000307 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000308
309 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000310 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
311 Suffix = "$stub";
312 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
313 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
314 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000315 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000316 Suffix = "$non_lazy_ptr";
317
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000318 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000319 if (Subtarget->isTargetCygMing())
320 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000321
Chris Lattner0cc2b792009-07-09 05:42:07 +0000322 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000323 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
324 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000325
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000326 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
327 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
328 GVStubs[Name] = Mang->getMangledName(GV);
329 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
330 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
331 HiddenGVStubs[Name] = Mang->getMangledName(GV);
332 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
333 FnStubs[Name] = Mang->getMangledName(GV);
334
335 // If the name begins with a dollar-sign, enclose it in parens. We do this
336 // to avoid having it look like an integer immediate to the assembler.
337 if (Name[0] == '$')
338 O << '(' << Name << ')';
339 else
340 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000341
Chris Lattner0cc2b792009-07-09 05:42:07 +0000342 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000343 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000345 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000346 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000347 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000348 FnStubs[Name+"$stub"] = Name;
349 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000350 }
351
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000352 // If the name begins with a dollar-sign, enclose it in parens. We do this
353 // to avoid having it look like an integer immediate to the assembler.
354 if (Name[0] == '$')
355 O << '(' << Name << ')';
356 else
357 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000358 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000360 }
Chris Lattner797a0782009-06-27 05:46:24 +0000361
362 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000364 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000365 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000366 break;
367 case X86II::MO_DARWIN_NONLAZY:
368 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
369 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000370 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000371 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000372 break;
373 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
374 O << " + [.-";
375 PrintPICBaseSymbol();
376 O << ']';
377 break;
378 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000379 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
380 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000381 O << '-';
382 PrintPICBaseSymbol();
383 break;
384 case X86II::MO_TLSGD: O << "@TLSGD"; break;
385 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
386 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
387 case X86II::MO_TPOFF: O << "@TPOFF"; break;
388 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
389 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
390 case X86II::MO_GOT: O << "@GOT"; break;
391 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000392 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 }
394}
395
Chris Lattnerae8f9592009-07-13 21:53:19 +0000396/// print_pcrel_imm - This is used to print an immediate value that ends up
397/// being encoded as a pc-relative value. These print slightly differently, for
398/// example, a $ is not emitted.
399void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
400 const MachineOperand &MO = MI->getOperand(OpNo);
401 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000402 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000403 case MachineOperand::MO_Immediate:
404 O << MO.getImm();
405 return;
406 case MachineOperand::MO_MachineBasicBlock:
407 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
408 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000409 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000410 case MachineOperand::MO_ExternalSymbol:
411 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000412 return;
413 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000414}
415
416
Chris Lattner207a0ca2009-07-13 21:41:08 +0000417
418void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
419 const char *Modifier) {
420 const MachineOperand &MO = MI->getOperand(OpNo);
421 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000422 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000423 case MachineOperand::MO_Register: {
424 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
425 "Virtual registers should not make it this far!");
426 O << '%';
427 unsigned Reg = MO.getReg();
428 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
429 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
430 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
431 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
432 Reg = getX86SubSuperRegister(Reg, VT);
433 }
434 O << TRI->getAsmName(Reg);
435 return;
436 }
437
438 case MachineOperand::MO_Immediate:
439 O << '$' << MO.getImm();
440 return;
441
442 case MachineOperand::MO_JumpTableIndex:
443 case MachineOperand::MO_ConstantPoolIndex:
444 case MachineOperand::MO_GlobalAddress:
445 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000446 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000447 printSymbolOperand(MO);
448 break;
449 }
450 }
451}
452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000454 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 assert(value <= 7 && "Invalid ssecc argument!");
456 switch (value) {
457 case 0: O << "eq"; break;
458 case 1: O << "lt"; break;
459 case 2: O << "le"; break;
460 case 3: O << "unord"; break;
461 case 4: O << "neq"; break;
462 case 5: O << "nlt"; break;
463 case 6: O << "nle"; break;
464 case 7: O << "ord"; break;
465 }
466}
467
Rafael Espindolabca99f72009-04-08 21:14:34 +0000468void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000469 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000470 const MachineOperand &BaseReg = MI->getOperand(Op);
471 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 const MachineOperand &DispSpec = MI->getOperand(Op+3);
473
Chris Lattner791fd482009-07-09 00:27:29 +0000474 // If we really don't want to print out (rip), don't.
475 bool HasBaseReg = BaseReg.getReg() != 0;
476 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
477 BaseReg.getReg() == X86::RIP)
478 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000479
480 // HasParenPart - True if we will print out the () part of the mem ref.
481 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
482
483 if (DispSpec.isImm()) {
484 int DispVal = DispSpec.getImm();
485 if (DispVal || !HasParenPart)
486 O << DispVal;
487 } else {
488 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
489 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000490 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000491 }
492
493 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000494 assert(IndexReg.getReg() != X86::ESP &&
495 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000496
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000497 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000498 if (HasBaseReg)
499 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500
501 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000502 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000503 printOperand(MI, Op+2, Modifier);
504 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000506 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000508 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 }
510}
511
Rafael Espindolabca99f72009-04-08 21:14:34 +0000512void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000513 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000514 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000515 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000516 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000517 printOperand(MI, Op+4, Modifier);
518 O << ':';
519 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000520 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000521}
522
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000523void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000524 const MachineBasicBlock *MBB) const {
525 if (!TAI->getSetDirective())
526 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000527
528 // We don't need .set machinery if we have GOT-style relocations
529 if (Subtarget->isPICStyleGOT())
530 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000531
Evan Cheng6fb06762007-11-09 01:32:10 +0000532 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
533 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000534 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000535 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000536 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000537 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000538 else {
539 O << '-';
540 PrintPICBaseSymbol();
541 O << '\n';
542 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000543}
544
Chris Lattner14f791a2009-06-24 19:19:16 +0000545
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000547 PrintPICBaseSymbol();
548 O << '\n';
549 PrintPICBaseSymbol();
550 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551}
552
553
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000554void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
555 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000556 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000557 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
558 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
559
560 O << JTEntryDirective << ' ';
561
Chris Lattner2e9393c2009-07-10 21:00:45 +0000562 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000563 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
564 << '_' << uid << "_set_" << MBB->getNumber();
565 } else if (Subtarget->isPICStyleGOT()) {
566 printBasicBlockLabel(MBB, false, false, false);
567 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000568 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000569 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000570}
571
Chris Lattner8bb96e42009-06-15 04:42:32 +0000572bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 unsigned Reg = MO.getReg();
574 switch (Mode) {
575 default: return true; // Unknown mode.
576 case 'b': // Print QImode register
577 Reg = getX86SubSuperRegister(Reg, MVT::i8);
578 break;
579 case 'h': // Print QImode high register
580 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
581 break;
582 case 'w': // Print HImode register
583 Reg = getX86SubSuperRegister(Reg, MVT::i16);
584 break;
585 case 'k': // Print SImode register
586 Reg = getX86SubSuperRegister(Reg, MVT::i32);
587 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000588 case 'q': // Print DImode register
589 Reg = getX86SubSuperRegister(Reg, MVT::i64);
590 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 }
592
Evan Cheng00d04a72008-07-07 22:21:06 +0000593 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 return false;
595}
596
597/// PrintAsmOperand - Print out an operand for an inline asm expression.
598///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000599bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 const char *ExtraCode) {
602 // Does this asm operand have a single letter operand modifier?
603 if (ExtraCode && ExtraCode[0]) {
604 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000605
Chris Lattnerb6047e62009-07-13 21:48:33 +0000606 const MachineOperand &MO = MI->getOperand(OpNo);
607
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 switch (ExtraCode[0]) {
609 default: return true; // Unknown modifier.
610 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000611 if (MO.isImm())
612 O << MO.getImm();
613 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
614 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000615 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000616 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000618
619 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000620 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000621 O << '*';
622 printOperand(MI, OpNo);
623 return false;
624 }
625 return true;
626
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 case 'b': // Print QImode register
628 case 'h': // Print QImode high register
629 case 'w': // Print HImode register
630 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000631 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000632 if (MO.isReg())
633 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 printOperand(MI, OpNo);
635 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000636
Dale Johannesen375b88b2009-07-07 23:28:22 +0000637 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000638 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000640
Chris Lattnerb6047e62009-07-13 21:48:33 +0000641 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000642 // Note: this is a temporary solution. It should be handled target
643 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000644 if (MO.isImm()) {
645 O << -MO.getImm();
646 return false;
647 }
648 O << '-';
649 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000651
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 printOperand(MI, OpNo);
653 return false;
654}
655
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000656bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000658 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000660 if (ExtraCode && ExtraCode[0]) {
661 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000662
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000663 switch (ExtraCode[0]) {
664 default: return true; // Unknown modifier.
665 case 'b': // Print QImode register
666 case 'h': // Print QImode high register
667 case 'w': // Print HImode register
668 case 'k': // Print SImode register
669 case 'q': // Print SImode register
670 // These only apply to registers, ignore on mem.
671 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000672 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000673 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000674 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000675 }
676 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 printMemReference(MI, OpNo);
678 return false;
679}
680
Chris Lattnerf5da5902009-06-20 07:03:18 +0000681static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
682 // Convert registers in the addr mode according to subreg64.
683 for (unsigned i = 0; i != 4; ++i) {
684 if (!MI->getOperand(i).isReg()) continue;
685
686 unsigned Reg = MI->getOperand(i).getReg();
687 if (Reg == 0) continue;
688
689 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
690 }
691}
692
Bill Wendlingb3b11262009-02-06 21:45:08 +0000693/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
694/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695///
696void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
697 ++EmittedInsts;
698
Chris Lattner19b1bd52009-06-19 00:47:33 +0000699 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000700 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
701 O << "\t";
702 printInlineAsm(MI);
703 return;
704 } else if (MI->isLabel()) {
705 printLabel(MI);
706 return;
707 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
708 printDeclare(MI);
709 return;
710 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
711 printImplicitDef(MI);
712 return;
713 }
714
Chris Lattnere67184e2009-06-19 23:59:57 +0000715 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000716 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000717
718 TmpInst.setOpcode(MI->getOpcode());
719
720 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
721 const MachineOperand &MO = MI->getOperand(i);
722
723 MCOperand MCOp;
724 if (MO.isReg()) {
725 MCOp.MakeReg(MO.getReg());
726 } else if (MO.isImm()) {
727 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000728 } else if (MO.isMBB()) {
729 MCOp.MakeMBBLabel(getFunctionNumber(), 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
Devang Patelc20079e2009-06-20 01:00:07 +0000753/// doInitialization
754bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000755 if (NewAsmPrinter) {
756 Context = new MCContext();
757 // FIXME: Send this to "O" instead of outs(). For now, we force it to
758 // stdout to make it easy to compare.
759 Streamer = createAsmStreamer(*Context, outs());
760 }
761
Devang Patelc20079e2009-06-20 01:00:07 +0000762 return AsmPrinter::doInitialization(M);
763}
764
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000765void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000766 const TargetData *TD = TM.getTargetData();
767
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000768 if (!GVar->hasInitializer())
769 return; // External global require no code
770
771 // Check to see if this is a special global used by LLVM, if so, emit it.
772 if (EmitSpecialLLVMGlobal(GVar)) {
773 if (Subtarget->isTargetDarwin() &&
774 TM.getRelocationModel() == Reloc::Static) {
775 if (GVar->getName() == "llvm.global_ctors")
776 O << ".reference .constructors_used\n";
777 else if (GVar->getName() == "llvm.global_dtors")
778 O << ".reference .destructors_used\n";
779 }
780 return;
781 }
782
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000783 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000784 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000785 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000786 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000787 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000788 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000789 unsigned Align = TD->getPreferredAlignmentLog(GVar);
790
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000791 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792
793 if (Subtarget->isTargetELF())
794 O << "\t.type\t" << name << ",@object\n";
795
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000796 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000797
Evan Chengcf84b142009-02-18 02:19:52 +0000798 if (C->isNullValue() && !GVar->hasSection() &&
799 !(Subtarget->isTargetDarwin() &&
800 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000801 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000802 if (GVar->hasExternalLinkage()) {
803 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000804 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000806 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000807 return;
808 }
809 }
810
811 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000812 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000814
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000816 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000817 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000819 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << "\t.globl " << name << '\n'
822 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000824 O << name << ":";
825 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000826 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000827 PrintUnmangledNameSafely(GVar, O);
828 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 EmitGlobalConstant(C);
831 return;
832 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000834 if (TAI->getCOMMDirectiveTakesAlignment())
835 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 }
837 } else {
838 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000839 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000840 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 }
Evan Cheng11db8142009-03-24 00:17:40 +0000846 if (VerboseAsm) {
847 O << "\t\t" << TAI->getCommentString() << ' ';
848 PrintUnmangledNameSafely(GVar, O);
849 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000850 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 return;
852 }
853 }
854
855 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000856 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000857 case GlobalValue::LinkOnceAnyLinkage:
858 case GlobalValue::LinkOnceODRLinkage:
859 case GlobalValue::WeakAnyLinkage:
860 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << "\t.globl " << name << '\n'
863 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000868 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 }
870 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000871 case GlobalValue::DLLExportLinkage:
872 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000873 // FIXME: appending linkage variables should go into a section of
874 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000875 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000877 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000879 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000880 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000881 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000882 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000883 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000884 }
885
886 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000887 O << name << ":";
888 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000889 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000890 PrintUnmangledNameSafely(GVar, O);
891 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000892 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000893 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000894 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000895
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896 EmitGlobalConstant(C);
897}
898
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000899bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000900 // Print out module-level global variables here.
901 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000902 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000903 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000904
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000905 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000906 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000907 }
908
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909 if (Subtarget->isTargetDarwin()) {
910 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000911
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000912 // Add the (possibly multiple) personalities to the set of global value
913 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000915 const std::vector<Function*> &Personalities = MMI->getPersonalities();
916 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000917 if (Personalities[i])
918 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
919 true /*private label*/)] =
920 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000921 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000922 }
923
Chris Lattner2e860262009-06-24 18:24:09 +0000924 // Output stubs for dynamically-linked functions
925 if (!FnStubs.empty()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000926 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
927 "self_modifying_code+pure_instructions,5", 0);
928 for (StringMap<std::string>::iterator I = FnStubs.begin(),
929 E = FnStubs.end(); I != E; ++I)
930 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
931 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000932 O << '\n';
933 }
934
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000935 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000936 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000937 SwitchToDataSection(
938 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000939 for (StringMap<std::string>::iterator I = GVStubs.begin(),
940 E = GVStubs.end(); I != E; ++I)
941 O << I->getKeyData() << ":\n\t.indirect_symbol "
942 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000943 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944
Evan Chenga65854f2008-12-05 01:06:39 +0000945 if (!HiddenGVStubs.empty()) {
946 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000947 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000948 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
949 E = HiddenGVStubs.end(); I != E; ++I)
950 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
951 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000952 }
953
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000954 // Funny Darwin hack: This flag tells the linker that no global symbols
955 // contain code that falls through to other global symbols (e.g. the obvious
956 // implementation of multiple entry points). If this doesn't occur, the
957 // linker can safely perform dead code stripping. Since LLVM never
958 // generates code that does this, it is always safe to set.
959 O << "\t.subsections_via_symbols\n";
960 } else if (Subtarget->isTargetCygMing()) {
961 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000962 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000963 i != e; ++i) {
964 O << "\t.def\t " << i->getKeyData()
965 << ";\t.scl\t" << COFF::C_EXT
966 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
967 << ";\t.endef\n";
968 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000969 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000970
Chris Lattnerdb191f02009-06-24 18:52:01 +0000971
972 // Output linker support code for dllexported globals on windows.
973 if (!DLLExportedGVs.empty()) {
974 SwitchToDataSection(".section .drectve");
975
976 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
977 e = DLLExportedGVs.end(); i != e; ++i)
978 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
979 }
980
981 if (!DLLExportedFns.empty()) {
982 SwitchToDataSection(".section .drectve");
983
984 for (StringSet<>::iterator i = DLLExportedFns.begin(),
985 e = DLLExportedFns.end();
986 i != e; ++i)
987 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
988 }
989
Chris Lattnerdb191f02009-06-24 18:52:01 +0000990 // Do common shutdown.
991 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +0000992
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000993 if (NewAsmPrinter) {
994 Streamer->Finish();
995
996 delete Streamer;
997 delete Context;
998 Streamer = 0;
999 Context = 0;
1000 }
1001
Chris Lattnerdb191f02009-06-24 18:52:01 +00001002 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001003}
1004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005// Include the auto-generated portion of the assembly writer.
1006#include "X86GenAsmWriter.inc"