blob: f23322fbfead8db57b2f8a4c22edf988b0986a51 [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
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000105/// decorateName - Query FunctionInfoMap and use this information for various
106/// name decoration.
107void X86ATTAsmPrinter::decorateName(std::string &Name,
108 const GlobalValue *GV) {
109 const Function *F = dyn_cast<Function>(GV);
110 if (!F) return;
111
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000112 // Save function name for later type emission.
113 if (Subtarget->isTargetCygMing() && F->isDeclaration())
114 CygMingStubs.insert(Name);
115
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000116 // We don't want to decorate non-stdcall or non-fastcall functions right now
117 unsigned CC = F->getCallingConv();
118 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
119 return;
120
121 // Decorate names only when we're targeting Cygwin/Mingw32 targets
122 if (!Subtarget->isTargetCygMing())
123 return;
124
125 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
126
127 const X86MachineFunctionInfo *Info;
128 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
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000167 decorateName(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 Lattner0b05e542009-07-15 01:23:13 +0000310 bool isPrivate = false;
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000311
312 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
313 Suffix = "$stub";
314 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
315 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
316 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattner0b05e542009-07-15 01:23:13 +0000317 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000318 Suffix = "$non_lazy_ptr";
Chris Lattner0b05e542009-07-15 01:23:13 +0000319 isPrivate = true;
320 }
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000321
Chris Lattner0b05e542009-07-15 01:23:13 +0000322 std::string Name = Mang->getMangledName(GV, Suffix, isPrivate);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000323 decorateName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000324
Chris Lattner0cc2b792009-07-09 05:42:07 +0000325 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000326 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
327 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000328
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000329 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
330 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
331 GVStubs[Name] = Mang->getMangledName(GV);
332 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
333 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
334 HiddenGVStubs[Name] = Mang->getMangledName(GV);
335 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
336 FnStubs[Name] = Mang->getMangledName(GV);
337
338 // If the name begins with a dollar-sign, enclose it in parens. We do this
339 // to avoid having it look like an integer immediate to the assembler.
340 if (Name[0] == '$')
341 O << '(' << Name << ')';
342 else
343 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000344
Chris Lattner0cc2b792009-07-09 05:42:07 +0000345 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000346 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000348 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000349 std::string Name(TAI->getGlobalPrefix());
350 Name += MO.getSymbolName();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000351
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000352 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000353 FnStubs[Name+"$stub"] = Name;
354 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000355 }
356
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000357 // If the name begins with a dollar-sign, enclose it in parens. We do this
358 // to avoid having it look like an integer immediate to the assembler.
359 if (Name[0] == '$')
360 O << '(' << Name << ')';
361 else
362 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000363 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000365 }
Chris Lattner797a0782009-06-27 05:46:24 +0000366
367 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000369 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000370 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000371 break;
372 case X86II::MO_DARWIN_NONLAZY:
373 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
374 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000375 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000376 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000377 break;
378 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
379 O << " + [.-";
380 PrintPICBaseSymbol();
381 O << ']';
382 break;
383 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000384 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
385 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000386 O << '-';
387 PrintPICBaseSymbol();
388 break;
389 case X86II::MO_TLSGD: O << "@TLSGD"; break;
390 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
391 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
392 case X86II::MO_TPOFF: O << "@TPOFF"; break;
393 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
394 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
395 case X86II::MO_GOT: O << "@GOT"; break;
396 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000397 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
399}
400
Chris Lattnerae8f9592009-07-13 21:53:19 +0000401/// print_pcrel_imm - This is used to print an immediate value that ends up
402/// being encoded as a pc-relative value. These print slightly differently, for
403/// example, a $ is not emitted.
404void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
405 const MachineOperand &MO = MI->getOperand(OpNo);
406 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000407 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000408 case MachineOperand::MO_Immediate:
409 O << MO.getImm();
410 return;
411 case MachineOperand::MO_MachineBasicBlock:
412 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
413 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000414 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000415 case MachineOperand::MO_ExternalSymbol:
416 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000417 return;
418 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000419}
420
421
Chris Lattner207a0ca2009-07-13 21:41:08 +0000422
423void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
424 const char *Modifier) {
425 const MachineOperand &MO = MI->getOperand(OpNo);
426 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000427 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000428 case MachineOperand::MO_Register: {
429 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
430 "Virtual registers should not make it this far!");
431 O << '%';
432 unsigned Reg = MO.getReg();
433 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
434 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
435 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
436 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
437 Reg = getX86SubSuperRegister(Reg, VT);
438 }
439 O << TRI->getAsmName(Reg);
440 return;
441 }
442
443 case MachineOperand::MO_Immediate:
444 O << '$' << MO.getImm();
445 return;
446
447 case MachineOperand::MO_JumpTableIndex:
448 case MachineOperand::MO_ConstantPoolIndex:
449 case MachineOperand::MO_GlobalAddress:
450 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000451 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000452 printSymbolOperand(MO);
453 break;
454 }
455 }
456}
457
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000459 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 assert(value <= 7 && "Invalid ssecc argument!");
461 switch (value) {
462 case 0: O << "eq"; break;
463 case 1: O << "lt"; break;
464 case 2: O << "le"; break;
465 case 3: O << "unord"; break;
466 case 4: O << "neq"; break;
467 case 5: O << "nlt"; break;
468 case 6: O << "nle"; break;
469 case 7: O << "ord"; break;
470 }
471}
472
Rafael Espindolabca99f72009-04-08 21:14:34 +0000473void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000474 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000475 const MachineOperand &BaseReg = MI->getOperand(Op);
476 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 const MachineOperand &DispSpec = MI->getOperand(Op+3);
478
Chris Lattner791fd482009-07-09 00:27:29 +0000479 // If we really don't want to print out (rip), don't.
480 bool HasBaseReg = BaseReg.getReg() != 0;
481 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
482 BaseReg.getReg() == X86::RIP)
483 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000484
485 // HasParenPart - True if we will print out the () part of the mem ref.
486 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
487
488 if (DispSpec.isImm()) {
489 int DispVal = DispSpec.getImm();
490 if (DispVal || !HasParenPart)
491 O << DispVal;
492 } else {
493 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
494 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000495 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000496 }
497
498 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000499 assert(IndexReg.getReg() != X86::ESP &&
500 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000501
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000502 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000503 if (HasBaseReg)
504 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
506 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000507 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000508 printOperand(MI, Op+2, Modifier);
509 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000511 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000513 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 }
515}
516
Rafael Espindolabca99f72009-04-08 21:14:34 +0000517void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000518 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000519 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000520 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000521 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000522 printOperand(MI, Op+4, Modifier);
523 O << ':';
524 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000525 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000526}
527
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000528void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000529 const MachineBasicBlock *MBB) const {
530 if (!TAI->getSetDirective())
531 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000532
533 // We don't need .set machinery if we have GOT-style relocations
534 if (Subtarget->isPICStyleGOT())
535 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000536
Evan Cheng6fb06762007-11-09 01:32:10 +0000537 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
538 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000539 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000540 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000541 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000542 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000543 else {
544 O << '-';
545 PrintPICBaseSymbol();
546 O << '\n';
547 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000548}
549
Chris Lattner14f791a2009-06-24 19:19:16 +0000550
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000552 PrintPICBaseSymbol();
553 O << '\n';
554 PrintPICBaseSymbol();
555 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556}
557
558
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000559void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
560 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000561 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000562 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
563 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
564
565 O << JTEntryDirective << ' ';
566
Chris Lattner2e9393c2009-07-10 21:00:45 +0000567 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000568 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
569 << '_' << uid << "_set_" << MBB->getNumber();
570 } else if (Subtarget->isPICStyleGOT()) {
571 printBasicBlockLabel(MBB, false, false, false);
572 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000573 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000574 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000575}
576
Chris Lattner8bb96e42009-06-15 04:42:32 +0000577bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 unsigned Reg = MO.getReg();
579 switch (Mode) {
580 default: return true; // Unknown mode.
581 case 'b': // Print QImode register
582 Reg = getX86SubSuperRegister(Reg, MVT::i8);
583 break;
584 case 'h': // Print QImode high register
585 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
586 break;
587 case 'w': // Print HImode register
588 Reg = getX86SubSuperRegister(Reg, MVT::i16);
589 break;
590 case 'k': // Print SImode register
591 Reg = getX86SubSuperRegister(Reg, MVT::i32);
592 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000593 case 'q': // Print DImode register
594 Reg = getX86SubSuperRegister(Reg, MVT::i64);
595 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 }
597
Evan Cheng00d04a72008-07-07 22:21:06 +0000598 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 return false;
600}
601
602/// PrintAsmOperand - Print out an operand for an inline asm expression.
603///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000604bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000605 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 const char *ExtraCode) {
607 // Does this asm operand have a single letter operand modifier?
608 if (ExtraCode && ExtraCode[0]) {
609 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000610
Chris Lattnerb6047e62009-07-13 21:48:33 +0000611 const MachineOperand &MO = MI->getOperand(OpNo);
612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 switch (ExtraCode[0]) {
614 default: return true; // Unknown modifier.
615 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000616 if (MO.isImm())
617 O << MO.getImm();
618 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
619 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000620 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000621 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000623
624 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000625 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000626 O << '*';
627 printOperand(MI, OpNo);
628 return false;
629 }
630 return true;
631
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 case 'b': // Print QImode register
633 case 'h': // Print QImode high register
634 case 'w': // Print HImode register
635 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000636 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000637 if (MO.isReg())
638 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 printOperand(MI, OpNo);
640 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000641
Dale Johannesen375b88b2009-07-07 23:28:22 +0000642 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000643 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000645
Chris Lattnerb6047e62009-07-13 21:48:33 +0000646 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000647 // Note: this is a temporary solution. It should be handled target
648 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000649 if (MO.isImm()) {
650 O << -MO.getImm();
651 return false;
652 }
653 O << '-';
654 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 printOperand(MI, OpNo);
658 return false;
659}
660
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000661bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000663 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000665 if (ExtraCode && ExtraCode[0]) {
666 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000667
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000668 switch (ExtraCode[0]) {
669 default: return true; // Unknown modifier.
670 case 'b': // Print QImode register
671 case 'h': // Print QImode high register
672 case 'w': // Print HImode register
673 case 'k': // Print SImode register
674 case 'q': // Print SImode register
675 // These only apply to registers, ignore on mem.
676 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000677 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000678 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000679 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000680 }
681 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 printMemReference(MI, OpNo);
683 return false;
684}
685
Chris Lattnerf5da5902009-06-20 07:03:18 +0000686static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
687 // Convert registers in the addr mode according to subreg64.
688 for (unsigned i = 0; i != 4; ++i) {
689 if (!MI->getOperand(i).isReg()) continue;
690
691 unsigned Reg = MI->getOperand(i).getReg();
692 if (Reg == 0) continue;
693
694 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
695 }
696}
697
Bill Wendlingb3b11262009-02-06 21:45:08 +0000698/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
699/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700///
701void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
702 ++EmittedInsts;
703
Chris Lattner19b1bd52009-06-19 00:47:33 +0000704 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000705 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
706 O << "\t";
707 printInlineAsm(MI);
708 return;
709 } else if (MI->isLabel()) {
710 printLabel(MI);
711 return;
712 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
713 printDeclare(MI);
714 return;
715 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
716 printImplicitDef(MI);
717 return;
718 }
719
Chris Lattnere67184e2009-06-19 23:59:57 +0000720 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000721 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000722
723 TmpInst.setOpcode(MI->getOpcode());
724
725 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
726 const MachineOperand &MO = MI->getOperand(i);
727
728 MCOperand MCOp;
729 if (MO.isReg()) {
730 MCOp.MakeReg(MO.getReg());
731 } else if (MO.isImm()) {
732 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000733 } else if (MO.isMBB()) {
734 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000735 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000736 llvm_unreachable("Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000737 }
738
739 TmpInst.addOperand(MCOp);
740 }
741
Chris Lattner6b9f7742009-06-20 07:59:10 +0000742 switch (TmpInst.getOpcode()) {
743 case X86::LEA64_32r:
744 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000745 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000746 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000747 }
748
Chris Lattner19b1bd52009-06-19 00:47:33 +0000749 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000750 printInstruction(&TmpInst);
751 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000752 }
753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 // Call the autogenerated instruction printer routines.
755 printInstruction(MI);
756}
757
Devang Patelc20079e2009-06-20 01:00:07 +0000758/// doInitialization
759bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000760 if (NewAsmPrinter) {
761 Context = new MCContext();
762 // FIXME: Send this to "O" instead of outs(). For now, we force it to
763 // stdout to make it easy to compare.
764 Streamer = createAsmStreamer(*Context, outs());
765 }
766
Devang Patelc20079e2009-06-20 01:00:07 +0000767 return AsmPrinter::doInitialization(M);
768}
769
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000770void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000771 const TargetData *TD = TM.getTargetData();
772
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000773 if (!GVar->hasInitializer())
774 return; // External global require no code
775
776 // Check to see if this is a special global used by LLVM, if so, emit it.
777 if (EmitSpecialLLVMGlobal(GVar)) {
778 if (Subtarget->isTargetDarwin() &&
779 TM.getRelocationModel() == Reloc::Static) {
780 if (GVar->getName() == "llvm.global_ctors")
781 O << ".reference .constructors_used\n";
782 else if (GVar->getName() == "llvm.global_dtors")
783 O << ".reference .destructors_used\n";
784 }
785 return;
786 }
787
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000788 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000789 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000790 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000791 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000793 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000794 unsigned Align = TD->getPreferredAlignmentLog(GVar);
795
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000796 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797
798 if (Subtarget->isTargetELF())
799 O << "\t.type\t" << name << ",@object\n";
800
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000801 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000802
Evan Chengcf84b142009-02-18 02:19:52 +0000803 if (C->isNullValue() && !GVar->hasSection() &&
804 !(Subtarget->isTargetDarwin() &&
805 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000806 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000807 if (GVar->hasExternalLinkage()) {
808 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 return;
813 }
814 }
815
816 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000817 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000819
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000821 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << "\t.globl " << name << '\n'
827 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000828 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000829 O << name << ":";
830 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000831 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000832 PrintUnmangledNameSafely(GVar, O);
833 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 EmitGlobalConstant(C);
836 return;
837 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000839 if (TAI->getCOMMDirectiveTakesAlignment())
840 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 }
842 } else {
843 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000844 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000845 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 }
Evan Cheng11db8142009-03-24 00:17:40 +0000851 if (VerboseAsm) {
852 O << "\t\t" << TAI->getCommentString() << ' ';
853 PrintUnmangledNameSafely(GVar, O);
854 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000855 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000856 return;
857 }
858 }
859
860 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000861 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000862 case GlobalValue::LinkOnceAnyLinkage:
863 case GlobalValue::LinkOnceODRLinkage:
864 case GlobalValue::WeakAnyLinkage:
865 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000867 O << "\t.globl " << name << '\n'
868 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000870 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 }
875 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000876 case GlobalValue::DLLExportLinkage:
877 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 // FIXME: appending linkage variables should go into a section of
879 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000880 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000881 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000882 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000884 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000885 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000887 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000888 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000889 }
890
891 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000892 O << name << ":";
893 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000894 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000895 PrintUnmangledNameSafely(GVar, O);
896 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000897 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000899 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000900
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000901 EmitGlobalConstant(C);
902}
903
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000904bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000905 // Print out module-level global variables here.
906 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000907 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000908 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000910 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000911 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000912 }
913
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914 if (Subtarget->isTargetDarwin()) {
915 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000916
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000917 // Add the (possibly multiple) personalities to the set of global value
918 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000919 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000920 const std::vector<Function*> &Personalities = MMI->getPersonalities();
921 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000922 if (Personalities[i])
923 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
924 true /*private label*/)] =
925 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000926 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000927 }
928
Chris Lattner2e860262009-06-24 18:24:09 +0000929 // Output stubs for dynamically-linked functions
930 if (!FnStubs.empty()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000931 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
932 "self_modifying_code+pure_instructions,5", 0);
933 for (StringMap<std::string>::iterator I = FnStubs.begin(),
934 E = FnStubs.end(); I != E; ++I)
935 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
936 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000937 O << '\n';
938 }
939
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000940 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000941 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000942 SwitchToDataSection(
943 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
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()) {
951 SwitchToSection(TAI->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.
978 if (!DLLExportedGVs.empty()) {
979 SwitchToDataSection(".section .drectve");
980
981 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
982 e = DLLExportedGVs.end(); i != e; ++i)
983 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
984 }
985
986 if (!DLLExportedFns.empty()) {
987 SwitchToDataSection(".section .drectve");
988
989 for (StringSet<>::iterator i = DLLExportedFns.begin(),
990 e = DLLExportedFns.end();
991 i != e; ++i)
992 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
993 }
994
Chris Lattnerdb191f02009-06-24 18:52:01 +0000995 // Do common shutdown.
996 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +0000997
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000998 if (NewAsmPrinter) {
999 Streamer->Finish();
1000
1001 delete Streamer;
1002 delete Context;
1003 Streamer = 0;
1004 Context = 0;
1005 }
1006
Chris Lattnerdb191f02009-06-24 18:52:01 +00001007 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001008}
1009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010// Include the auto-generated portion of the assembly writer.
1011#include "X86GenAsmWriter.inc"