blob: c3bd8b4054373920f7319475d235a2b3ea353b22 [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-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"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Devang Patele4c0c0f2009-06-25 00:47:42 +000026#include "llvm/MDNode.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000027#include "llvm/Type.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Chris Lattner40e3c7a2009-06-24 05:46:28 +000030#include "llvm/MC/MCContext.h"
Chris Lattner475370b2009-06-19 00:47:33 +000031#include "llvm/MC/MCInst.h"
Chris Lattner40e3c7a2009-06-24 05:46:28 +000032#include "llvm/MC/MCStreamer.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000033#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner475370b2009-06-19 00:47:33 +000035#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000038#include "llvm/Support/raw_ostream.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000039#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000040#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
Chris Lattner475370b2009-06-19 00:47:33 +000045static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
46 cl::Hidden);
47
Chris Lattnerab162992009-06-24 19:44:36 +000048//===----------------------------------------------------------------------===//
49// Primitive Helper Functions.
50//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000051
52void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Evan Cheng071b9d52007-01-18 01:49:58 +000053 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000055 else if (Subtarget->isTargetELF())
Chris Lattner8529d282009-07-08 23:09:14 +000056 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000057 else
Chris Lattnere2959dc2009-07-13 22:28:21 +000058 LLVM_UNREACHABLE("Don't know how to print PIC label!");
Anton Korobeynikov7f705592007-01-12 19:20:47 +000059}
60
Chris Lattnerab162992009-06-24 19:44:36 +000061/// PrintUnmangledNameSafely - Print out the printable characters in the name.
62/// Don't print things like \\n or \\0.
63static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
64 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
65 Name != E; ++Name)
66 if (isprint(*Name))
67 OS << *Name;
68}
Chris Lattnerb5299dd2009-06-24 19:19:16 +000069
Anton Korobeynikov75b68822008-06-28 11:08:27 +000070static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
71 const TargetData *TD) {
72 X86MachineFunctionInfo Info;
73 uint64_t Size = 0;
74
75 switch (F->getCallingConv()) {
76 case CallingConv::X86_StdCall:
77 Info.setDecorationStyle(StdCall);
78 break;
79 case CallingConv::X86_FastCall:
80 Info.setDecorationStyle(FastCall);
81 break;
82 default:
83 return Info;
84 }
85
86 unsigned argNum = 1;
87 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
88 AI != AE; ++AI, ++argNum) {
89 const Type* Ty = AI->getType();
90
91 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000092 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000093 Ty = cast<PointerType>(Ty)->getElementType();
94
95 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000096 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000097 }
98
99 // We're not supporting tooooo huge arguments :)
100 Info.setBytesToPopOnReturn((unsigned int)Size);
101 return Info;
102}
103
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000104/// decorateName - Query FunctionInfoMap and use this information for various
105/// name decoration.
106void X86ATTAsmPrinter::decorateName(std::string &Name,
107 const GlobalValue *GV) {
108 const Function *F = dyn_cast<Function>(GV);
109 if (!F) return;
110
Chris Lattner7a4e4642009-07-10 21:57:21 +0000111 // Save function name for later type emission.
112 if (Subtarget->isTargetCygMing() && F->isDeclaration())
113 CygMingStubs.insert(Name);
114
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000115 // We don't want to decorate non-stdcall or non-fastcall functions right now
116 unsigned CC = F->getCallingConv();
117 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
118 return;
119
120 // Decorate names only when we're targeting Cygwin/Mingw32 targets
121 if (!Subtarget->isTargetCygMing())
122 return;
123
124 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
125
126 const X86MachineFunctionInfo *Info;
127 if (info_item == FunctionInfoMap.end()) {
128 // Calculate apropriate function info and populate map
129 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
130 Info = &FunctionInfoMap[F];
131 } else {
132 Info = &info_item->second;
133 }
134
135 const FunctionType *FT = F->getFunctionType();
136 switch (Info->getDecorationStyle()) {
137 case None:
138 break;
139 case StdCall:
140 // "Pure" variadic functions do not receive @0 suffix.
141 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
142 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
143 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
144 break;
145 case FastCall:
146 // "Pure" variadic functions do not receive @0 suffix.
147 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
148 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
149 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
150
151 if (Name[0] == '_') {
152 Name[0] = '@';
153 } else {
154 Name = '@' + Name;
155 }
156 break;
157 default:
Chris Lattnere2959dc2009-07-13 22:28:21 +0000158 LLVM_UNREACHABLE("Unsupported DecorationStyle");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000159 }
160}
161
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000162void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000163 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000165
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000166 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000167
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000168 SwitchToSection(TAI->SectionForGlobal(F));
Evan Cheng2338c5c2006-02-07 08:38:37 +0000169 switch (F->getLinkage()) {
Chris Lattnere2959dc2009-07-13 22:28:21 +0000170 default: LLVM_UNREACHABLE("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000172 case Function::PrivateLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000173 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000175 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000176 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000177 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000179 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000180 case Function::LinkOnceAnyLinkage:
181 case Function::LinkOnceODRLinkage:
182 case Function::WeakAnyLinkage:
183 case Function::WeakODRLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000184 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000185 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << CurrentFnName << '\n';
187 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000188 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << "\n"
190 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000191 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000192 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000193 }
194 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000195 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000196
197 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000198
199 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000200 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000201 else if (Subtarget->isTargetCygMing()) {
202 O << "\t.def\t " << CurrentFnName
203 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000204 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000205 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
206 << ";\t.endef\n";
207 }
208
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000210 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000211 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000212 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000213 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000215
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000216/// runOnMachineFunction - This uses the printMachineInstruction()
217/// method to print assembly for each instruction.
218///
219bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
220 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000221 this->MF = &MF;
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000222 unsigned CC = F->getCallingConv();
223
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000224 SetupMachineFunction(MF);
225 O << "\n\n";
226
227 // Populate function information map. Actually, We don't want to populate
228 // non-stdcall or non-fastcall functions' information right now.
229 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
230 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
231
232 // Print out constants referenced by the function
233 EmitConstantPool(MF.getConstantPool());
234
235 if (F->hasDLLExportLinkage())
236 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
237
238 // Print the 'header' of function
239 emitFunctionHeader(MF);
240
241 // Emit pre-function debug and/or EH information.
242 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000243 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000244
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000246 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000247 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
248 I != E; ++I) {
249 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000250 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
251 // This is an entry block or a block that's only reachable via a
252 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
253 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000254 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000255 O << '\n';
256 }
Bill Wendling824a7212008-01-26 09:03:52 +0000257 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
258 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000259 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000260 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000261 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000262 printMachineInstruction(II);
263 }
264 }
Evan Cheng67afece2006-08-28 22:14:16 +0000265
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000266 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
267 // If the function is empty, then we need to emit *something*. Otherwise,
268 // the function's label might be associated with something that it wasn't
269 // meant to be associated with. We emit a noop in this situation.
270 // We are assuming inline asms are code.
271 O << "\tnop\n";
272 }
273
Jim Laskey563321a2006-09-06 18:34:40 +0000274 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000275 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000276
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000277 // Emit post-function debug information.
Devang Patel5090f192009-06-19 23:21:20 +0000278 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000279 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000280
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000281 // Print out jump tables referenced by the function.
282 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000283
Dan Gohmane5f4de42008-11-07 19:49:17 +0000284 O.flush();
285
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000286 // We didn't modify anything.
287 return false;
288}
289
Chris Lattnerf666c092009-07-13 21:53:19 +0000290/// printSymbolOperand - Print a raw symbol reference operand. This handles
291/// jump tables, constant pools, global address and external symbols, all of
292/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000293void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
294 switch (MO.getType()) {
295 default: LLVM_UNREACHABLE("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000296 case MachineOperand::MO_JumpTableIndex:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000297 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000299 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000300 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000301 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000302 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000303 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000304 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000306 const GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000308 decorateName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000309
Chris Lattnerf7789c72009-06-27 05:46:24 +0000310 bool needCloseParen = false;
Chris Lattner7a4e4642009-07-10 21:57:21 +0000311 if (Name[0] == '$') {
Dan Gohman2a3250c2007-04-26 21:07:05 +0000312 // The name begins with a dollar-sign. In order to avoid having it look
313 // like an integer immediate to the assembler, enclose it in parens.
314 O << '(';
315 needCloseParen = true;
316 }
Chris Lattner174f8162009-07-13 21:41:08 +0000317
Chris Lattner35c89612009-07-09 05:42:07 +0000318 // Handle dllimport linkage.
Chris Lattner75cdf272009-07-09 06:59:17 +0000319 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) {
320 O << "__imp_" << Name;
321 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
322 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
323 GVStubs.insert(Name);
324 printSuffixedName(Name, "$non_lazy_ptr");
325 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
326 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
327 HiddenGVStubs.insert(Name);
328 printSuffixedName(Name, "$non_lazy_ptr");
Chris Lattner7a4e4642009-07-10 21:57:21 +0000329 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
330 FnStubs.insert(Name);
331 printSuffixedName(Name, "$stub");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000332 } else {
Evan Cheng25ab6902006-09-08 06:48:29 +0000333 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000334 }
Chris Lattner174f8162009-07-13 21:41:08 +0000335
Chris Lattnere3723332009-06-21 02:22:53 +0000336 if (needCloseParen)
337 O << ')';
338
Chris Lattner7a4e4642009-07-10 21:57:21 +0000339 // Assemble call via PLT for externally visible symbols.
340 if (MO.getTargetFlags() == X86II::MO_PLT)
341 O << "@PLT";
342
Chris Lattner35c89612009-07-09 05:42:07 +0000343 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000344 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000345 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000346 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner62c5c502009-07-13 22:07:30 +0000347 bool needCloseParen = false;
348 std::string Name(TAI->getGlobalPrefix());
349 Name += MO.getSymbolName();
350
351 if (Name[0] == '$') {
352 // The name begins with a dollar-sign. In order to avoid having it look
353 // like an integer immediate to the assembler, enclose it in parens.
354 O << '(';
355 needCloseParen = true;
356 }
357
358 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
359 FnStubs.insert(Name);
360 printSuffixedName(Name, "$stub");
361 } else {
362 O << Name;
363 }
364
365 if (needCloseParen)
366 O << ')';
Chris Lattnerf7789c72009-06-27 05:46:24 +0000367 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000368 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000369 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000370
371 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000372 default:
Chris Lattnere2959dc2009-07-13 22:28:21 +0000373 LLVM_UNREACHABLE("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000374 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000375 break;
376 case X86II::MO_DARWIN_NONLAZY:
377 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
378 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000379 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000380 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000381 break;
382 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
383 O << " + [.-";
384 PrintPICBaseSymbol();
385 O << ']';
386 break;
387 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000388 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
389 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000390 O << '-';
391 PrintPICBaseSymbol();
392 break;
393 case X86II::MO_TLSGD: O << "@TLSGD"; break;
394 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
395 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
396 case X86II::MO_TPOFF: O << "@TPOFF"; break;
397 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
398 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
399 case X86II::MO_GOT: O << "@GOT"; break;
400 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000401 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000402 }
403}
404
Chris Lattnerf666c092009-07-13 21:53:19 +0000405/// print_pcrel_imm - This is used to print an immediate value that ends up
406/// being encoded as a pc-relative value. These print slightly differently, for
407/// example, a $ is not emitted.
408void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
409 const MachineOperand &MO = MI->getOperand(OpNo);
410 switch (MO.getType()) {
Chris Lattnere2959dc2009-07-13 22:28:21 +0000411 default: LLVM_UNREACHABLE("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000412 case MachineOperand::MO_Immediate:
413 O << MO.getImm();
414 return;
415 case MachineOperand::MO_MachineBasicBlock:
416 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
417 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000418 case MachineOperand::MO_GlobalAddress:
419 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000420 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000421 case MachineOperand::MO_ExternalSymbol:
422 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000423 return;
424 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000425}
426
427
Chris Lattner174f8162009-07-13 21:41:08 +0000428
429void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
430 const char *Modifier) {
431 const MachineOperand &MO = MI->getOperand(OpNo);
432 switch (MO.getType()) {
433 default: LLVM_UNREACHABLE("unknown operand type!");
434 case MachineOperand::MO_Register: {
435 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
436 "Virtual registers should not make it this far!");
437 O << '%';
438 unsigned Reg = MO.getReg();
439 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
440 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
441 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
442 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
443 Reg = getX86SubSuperRegister(Reg, VT);
444 }
445 O << TRI->getAsmName(Reg);
446 return;
447 }
448
449 case MachineOperand::MO_Immediate:
450 O << '$' << MO.getImm();
451 return;
452
453 case MachineOperand::MO_JumpTableIndex:
454 case MachineOperand::MO_ConstantPoolIndex:
455 case MachineOperand::MO_GlobalAddress:
456 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000457 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000458 printSymbolOperand(MO);
459 break;
460 }
461 }
462}
463
Nate Begeman391c5d22005-11-30 18:54:35 +0000464void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000465 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000466 assert(value <= 7 && "Invalid ssecc argument!");
467 switch (value) {
468 case 0: O << "eq"; break;
469 case 1: O << "lt"; break;
470 case 2: O << "le"; break;
471 case 3: O << "unord"; break;
472 case 4: O << "neq"; break;
473 case 5: O << "nlt"; break;
474 case 6: O << "nle"; break;
475 case 7: O << "ord"; break;
476 }
477}
478
Rafael Espindola094fad32009-04-08 21:14:34 +0000479void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000480 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000481 const MachineOperand &BaseReg = MI->getOperand(Op);
482 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000483 const MachineOperand &DispSpec = MI->getOperand(Op+3);
484
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000485 // If we really don't want to print out (rip), don't.
486 bool HasBaseReg = BaseReg.getReg() != 0;
487 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
488 BaseReg.getReg() == X86::RIP)
489 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000490
491 // HasParenPart - True if we will print out the () part of the mem ref.
492 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
493
494 if (DispSpec.isImm()) {
495 int DispVal = DispSpec.getImm();
496 if (DispVal || !HasParenPart)
497 O << DispVal;
498 } else {
499 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
500 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000501 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000502 }
503
504 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000505 assert(IndexReg.getReg() != X86::ESP &&
506 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000507
Dan Gohmand19a53b2008-06-30 22:03:41 +0000508 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000509 if (HasBaseReg)
510 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000511
512 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000513 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000514 printOperand(MI, Op+2, Modifier);
515 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000516 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000517 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000518 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000519 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000520 }
521}
522
Rafael Espindola094fad32009-04-08 21:14:34 +0000523void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000524 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000525 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000526 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000527 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000528 printOperand(MI, Op+4, Modifier);
529 O << ':';
530 }
Chris Lattner18c59872009-06-27 04:16:01 +0000531 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000532}
533
Anton Korobeynikov91364382008-06-28 11:08:09 +0000534void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000535 const MachineBasicBlock *MBB) const {
536 if (!TAI->getSetDirective())
537 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000538
539 // We don't need .set machinery if we have GOT-style relocations
540 if (Subtarget->isPICStyleGOT())
541 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000542
Evan Chengcc415862007-11-09 01:32:10 +0000543 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
544 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000545 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000546 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000547 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000548 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000549 else {
550 O << '-';
551 PrintPICBaseSymbol();
552 O << '\n';
553 }
Evan Chengcc415862007-11-09 01:32:10 +0000554}
555
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000556
Evan Cheng7ccced62006-02-18 00:15:05 +0000557void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000558 PrintPICBaseSymbol();
559 O << '\n';
560 PrintPICBaseSymbol();
561 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000562}
563
Evan Cheng62f27002006-04-28 23:11:40 +0000564
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000565void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
566 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000567 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000568 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
569 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
570
571 O << JTEntryDirective << ' ';
572
Chris Lattnere2c92082009-07-10 21:00:45 +0000573 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000574 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
575 << '_' << uid << "_set_" << MBB->getNumber();
576 } else if (Subtarget->isPICStyleGOT()) {
577 printBasicBlockLabel(MBB, false, false, false);
578 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000579 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000580 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000581}
582
Chris Lattner37710712009-06-15 04:42:32 +0000583bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000584 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000585 switch (Mode) {
586 default: return true; // Unknown mode.
587 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000588 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000589 break;
590 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000591 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000592 break;
593 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000594 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000595 break;
596 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000597 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000598 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000599 case 'q': // Print DImode register
600 Reg = getX86SubSuperRegister(Reg, MVT::i64);
601 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000602 }
603
Evan Chengae270f62008-07-07 22:21:06 +0000604 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000605 return false;
606}
607
Evan Cheng3d48a902006-04-28 21:19:05 +0000608/// PrintAsmOperand - Print out an operand for an inline asm expression.
609///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000610bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000611 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000612 const char *ExtraCode) {
613 // Does this asm operand have a single letter operand modifier?
614 if (ExtraCode && ExtraCode[0]) {
615 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000616
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000617 const MachineOperand &MO = MI->getOperand(OpNo);
618
Evan Cheng3d48a902006-04-28 21:19:05 +0000619 switch (ExtraCode[0]) {
620 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000621 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000622 if (MO.isImm())
623 O << MO.getImm();
624 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
625 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000626 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000627 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000628 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000629
630 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000631 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000632 O << '*';
633 printOperand(MI, OpNo);
634 return false;
635 }
636 return true;
637
Evan Cheng62f27002006-04-28 23:11:40 +0000638 case 'b': // Print QImode register
639 case 'h': // Print QImode high register
640 case 'w': // Print HImode register
641 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000642 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000643 if (MO.isReg())
644 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000645 printOperand(MI, OpNo);
646 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000647
Dale Johannesen9fcff042009-07-07 23:28:22 +0000648 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000649 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000650 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000651
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000652 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000653 // Note: this is a temporary solution. It should be handled target
654 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000655 if (MO.isImm()) {
656 O << -MO.getImm();
657 return false;
658 }
659 O << '-';
660 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000661 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000662
Evan Cheng3d48a902006-04-28 21:19:05 +0000663 printOperand(MI, OpNo);
664 return false;
665}
666
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000667bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000668 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000669 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000670 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000671 if (ExtraCode && ExtraCode[0]) {
672 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000673
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000674 switch (ExtraCode[0]) {
675 default: return true; // Unknown modifier.
676 case 'b': // Print QImode register
677 case 'h': // Print QImode high register
678 case 'w': // Print HImode register
679 case 'k': // Print SImode register
680 case 'q': // Print SImode register
681 // These only apply to registers, ignore on mem.
682 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000683 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000684 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000685 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000686 }
687 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000688 printMemReference(MI, OpNo);
689 return false;
690}
691
Chris Lattnerc1243062009-06-20 07:03:18 +0000692static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
693 // Convert registers in the addr mode according to subreg64.
694 for (unsigned i = 0; i != 4; ++i) {
695 if (!MI->getOperand(i).isReg()) continue;
696
697 unsigned Reg = MI->getOperand(i).getReg();
698 if (Reg == 0) continue;
699
700 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
701 }
702}
703
Bill Wendlingac06d002009-02-06 21:45:08 +0000704/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
705/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000706///
707void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
708 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000709
Chris Lattner475370b2009-06-19 00:47:33 +0000710 if (NewAsmPrinter) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000711 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
712 O << "\t";
713 printInlineAsm(MI);
714 return;
715 } else if (MI->isLabel()) {
716 printLabel(MI);
717 return;
718 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
719 printDeclare(MI);
720 return;
721 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
722 printImplicitDef(MI);
723 return;
724 }
725
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000726 O << "NEW: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000727 MCInst TmpInst;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000728
729 TmpInst.setOpcode(MI->getOpcode());
730
731 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
732 const MachineOperand &MO = MI->getOperand(i);
733
734 MCOperand MCOp;
735 if (MO.isReg()) {
736 MCOp.MakeReg(MO.getReg());
737 } else if (MO.isImm()) {
738 MCOp.MakeImm(MO.getImm());
Chris Lattnerc1243062009-06-20 07:03:18 +0000739 } else if (MO.isMBB()) {
740 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000741 } else {
Chris Lattnere2959dc2009-07-13 22:28:21 +0000742 LLVM_UNREACHABLE("Unimp");
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000743 }
744
745 TmpInst.addOperand(MCOp);
746 }
747
Chris Lattnerdc479f62009-06-20 07:59:10 +0000748 switch (TmpInst.getOpcode()) {
749 case X86::LEA64_32r:
750 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerc1243062009-06-20 07:03:18 +0000751 lower_lea64_32mem(&TmpInst, 1);
Chris Lattnerdc479f62009-06-20 07:59:10 +0000752 break;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000753 }
754
Chris Lattner475370b2009-06-19 00:47:33 +0000755 // FIXME: Convert TmpInst.
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000756 printInstruction(&TmpInst);
757 O << "OLD: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000758 }
759
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000760 // Call the autogenerated instruction printer routines.
761 printInstruction(MI);
762}
763
Devang Patel66b4d3b2009-06-20 01:00:07 +0000764/// doInitialization
765bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner40e3c7a2009-06-24 05:46:28 +0000766 if (NewAsmPrinter) {
767 Context = new MCContext();
768 // FIXME: Send this to "O" instead of outs(). For now, we force it to
769 // stdout to make it easy to compare.
770 Streamer = createAsmStreamer(*Context, outs());
771 }
772
Devang Patel66b4d3b2009-06-20 01:00:07 +0000773 return AsmPrinter::doInitialization(M);
774}
775
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000776void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000777 const TargetData *TD = TM.getTargetData();
778
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000779 if (!GVar->hasInitializer())
780 return; // External global require no code
781
782 // Check to see if this is a special global used by LLVM, if so, emit it.
783 if (EmitSpecialLLVMGlobal(GVar)) {
784 if (Subtarget->isTargetDarwin() &&
785 TM.getRelocationModel() == Reloc::Static) {
786 if (GVar->getName() == "llvm.global_ctors")
787 O << ".reference .constructors_used\n";
788 else if (GVar->getName() == "llvm.global_dtors")
789 O << ".reference .destructors_used\n";
790 }
791 return;
792 }
793
794 std::string name = Mang->getValueName(GVar);
795 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000796 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000797 return;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000798 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000799 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000800 unsigned Align = TD->getPreferredAlignmentLog(GVar);
801
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000802 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000803
804 if (Subtarget->isTargetELF())
805 O << "\t.type\t" << name << ",@object\n";
806
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000807 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000808
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000809 if (C->isNullValue() && !GVar->hasSection() &&
810 !(Subtarget->isTargetDarwin() &&
811 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000812 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 if (GVar->hasExternalLinkage()) {
814 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000815 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000817 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818 return;
819 }
820 }
821
822 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000823 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000824 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000825
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000826 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000827 if (GVar->hasLocalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000829 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000831 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << "\t.globl " << name << '\n'
833 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000834 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000835 O << name << ":";
836 if (VerboseAsm) {
Evan Cheng7db860d2009-03-25 01:08:42 +0000837 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000838 PrintUnmangledNameSafely(GVar, O);
839 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000840 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000841 EmitGlobalConstant(C);
842 return;
843 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000844 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +0000845 if (TAI->getCOMMDirectiveTakesAlignment())
846 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000847 }
848 } else {
849 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000850 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000853 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000854 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000855 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000857 if (VerboseAsm) {
858 O << "\t\t" << TAI->getCommentString() << ' ';
859 PrintUnmangledNameSafely(GVar, O);
860 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000861 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000862 return;
863 }
864 }
865
866 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000867 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000868 case GlobalValue::LinkOnceAnyLinkage:
869 case GlobalValue::LinkOnceODRLinkage:
870 case GlobalValue::WeakAnyLinkage:
871 case GlobalValue::WeakODRLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000872 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000873 O << "\t.globl " << name << '\n'
874 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000875 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000876 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000877 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000878 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000879 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000880 }
881 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000882 case GlobalValue::DLLExportLinkage:
883 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000884 // FIXME: appending linkage variables should go into a section of
885 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000886 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000887 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000888 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000889 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000890 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000891 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000892 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000893 default:
Chris Lattnere2959dc2009-07-13 22:28:21 +0000894 LLVM_UNREACHABLE("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000895 }
896
897 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000898 O << name << ":";
899 if (VerboseAsm){
Evan Cheng7db860d2009-03-25 01:08:42 +0000900 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000901 PrintUnmangledNameSafely(GVar, O);
902 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000903 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000904 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000905 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000906
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000907 EmitGlobalConstant(C);
908}
909
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000910bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000911 // Print out module-level global variables here.
912 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000913 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000914 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000915
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000916 if (I->hasDLLExportLinkage())
917 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000918 }
919
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000920 if (Subtarget->isTargetDarwin()) {
921 SwitchToDataSection("");
Chris Lattner8f61f982009-06-24 18:24:09 +0000922
Chris Lattner381d4fe2009-06-24 18:17:00 +0000923 // Add the (possibly multiple) personalities to the set of global value
924 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000925 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000926 const std::vector<Function*> &Personalities = MMI->getPersonalities();
927 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
928 if (Personalities[i] == 0)
Evan Cheng77c8f762008-07-08 00:55:58 +0000929 continue;
Chris Lattner381d4fe2009-06-24 18:17:00 +0000930 std::string Name = Mang->getValueName(Personalities[i]);
931 decorateName(Name, Personalities[i]);
932 GVStubs.insert(Name);
Evan Cheng77c8f762008-07-08 00:55:58 +0000933 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000934 }
935
Chris Lattner8f61f982009-06-24 18:24:09 +0000936 // Output stubs for dynamically-linked functions
937 if (!FnStubs.empty()) {
938 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
939 I != E; ++I) {
940 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
941 "self_modifying_code+pure_instructions,5", 0);
942 const char *Name = I->getKeyData();
943 printSuffixedName(Name, "$stub");
944 O << ":\n"
945 "\t.indirect_symbol " << Name << "\n"
946 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
947 }
948 O << '\n';
949 }
950
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000951 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000952 if (!GVStubs.empty()) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000953 SwitchToDataSection(
954 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner8f61f982009-06-24 18:24:09 +0000955 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
956 I != E; ++I) {
957 const char *Name = I->getKeyData();
958 printSuffixedName(Name, "$non_lazy_ptr");
959 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
960 }
961 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000962
Evan Chengae94e592008-12-05 01:06:39 +0000963 if (!HiddenGVStubs.empty()) {
964 SwitchToSection(TAI->getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000965 EmitAlignment(2);
Chris Lattner8f61f982009-06-24 18:24:09 +0000966 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
967 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner8f61f982009-06-24 18:24:09 +0000968 const char *Name = I->getKeyData();
969 printSuffixedName(Name, "$non_lazy_ptr");
970 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
971 }
Evan Chengae94e592008-12-05 01:06:39 +0000972 }
973
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000974 // Funny Darwin hack: This flag tells the linker that no global symbols
975 // contain code that falls through to other global symbols (e.g. the obvious
976 // implementation of multiple entry points). If this doesn't occur, the
977 // linker can safely perform dead code stripping. Since LLVM never
978 // generates code that does this, it is always safe to set.
979 O << "\t.subsections_via_symbols\n";
980 } else if (Subtarget->isTargetCygMing()) {
981 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000982 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000983 i != e; ++i) {
984 O << "\t.def\t " << i->getKeyData()
985 << ";\t.scl\t" << COFF::C_EXT
986 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
987 << ";\t.endef\n";
988 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000989 }
Chris Lattner974469d2009-06-24 05:47:59 +0000990
Chris Lattner0a7befa2009-06-24 18:52:01 +0000991
992 // Output linker support code for dllexported globals on windows.
993 if (!DLLExportedGVs.empty()) {
994 SwitchToDataSection(".section .drectve");
995
996 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
997 e = DLLExportedGVs.end(); i != e; ++i)
998 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
999 }
1000
1001 if (!DLLExportedFns.empty()) {
1002 SwitchToDataSection(".section .drectve");
1003
1004 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1005 e = DLLExportedFns.end();
1006 i != e; ++i)
1007 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1008 }
1009
Chris Lattner0a7befa2009-06-24 18:52:01 +00001010 // Do common shutdown.
1011 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner974469d2009-06-24 05:47:59 +00001012
Chris Lattner40e3c7a2009-06-24 05:46:28 +00001013 if (NewAsmPrinter) {
1014 Streamer->Finish();
1015
1016 delete Streamer;
1017 delete Context;
1018 Streamer = 0;
1019 Context = 0;
1020 }
1021
Chris Lattner0a7befa2009-06-24 18:52:01 +00001022 return Changed;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001023}
1024
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001025// Include the auto-generated portion of the assembly writer.
1026#include "X86GenAsmWriter.inc"