blob: 1260cedfcdaa288e93593c8a1360c26b95a41f82 [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"
David Greene71847812009-07-14 20:18:05 +000037#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000038#include "llvm/Support/Mangler.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
Torok Edwinc23197a2009-07-14 16:55:14 +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.
Daniel Dunbar93b67e42009-07-26 07:49:05 +000063static void PrintUnmangledNameSafely(const Value *V,
David Greene71847812009-07-14 20:18:05 +000064 formatted_raw_ostream &OS) {
Daniel Dunbar93b67e42009-07-26 07:49:05 +000065 for (StringRef::iterator it = V->getName().begin(),
66 ie = V->getName().end(); it != ie; ++it)
67 if (isprint(*it))
68 OS << *it;
Chris Lattnerab162992009-06-24 19:44:36 +000069}
Chris Lattnerb5299dd2009-06-24 19:19:16 +000070
Anton Korobeynikov75b68822008-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 Patel05988662008-09-25 21:00:45 +000093 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000094 Ty = cast<PointerType>(Ty)->getElementType();
95
96 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000097 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000098 }
99
100 // We're not supporting tooooo huge arguments :)
101 Info.setBytesToPopOnReturn((unsigned int)Size);
102 return Info;
103}
104
Chris Lattnerc08872e2009-07-15 04:55:56 +0000105/// DecorateCygMingName - Query FunctionInfoMap and use this information for
106/// various name decorations for Cygwin and MingW.
107void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
108 const GlobalValue *GV) {
109 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
110
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000111 const Function *F = dyn_cast<Function>(GV);
112 if (!F) return;
113
Chris Lattner7a4e4642009-07-10 21:57:21 +0000114 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000115 if (F->isDeclaration())
Chris Lattner7a4e4642009-07-10 21:57:21 +0000116 CygMingStubs.insert(Name);
117
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000118 // We don't want to decorate non-stdcall or non-fastcall functions right now
119 unsigned CC = F->getCallingConv();
120 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
121 return;
122
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000123
124 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000125
126 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000127 if (info_item == FunctionInfoMap.end()) {
128 // Calculate apropriate function info and populate map
129 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
130 Info = &FunctionInfoMap[F];
131 } else {
132 Info = &info_item->second;
133 }
134
135 const FunctionType *FT = F->getFunctionType();
136 switch (Info->getDecorationStyle()) {
137 case None:
138 break;
139 case StdCall:
140 // "Pure" variadic functions do not receive @0 suffix.
141 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
142 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
143 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
144 break;
145 case FastCall:
146 // "Pure" variadic functions do not receive @0 suffix.
147 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
148 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
149 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
150
151 if (Name[0] == '_') {
152 Name[0] = '@';
153 } else {
154 Name = '@' + Name;
155 }
156 break;
157 default:
Torok Edwinc23197a2009-07-14 16:55:14 +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
Chris Lattnerc08872e2009-07-15 04:55:56 +0000166 if (Subtarget->isTargetCygMing())
167 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000168
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000169 SwitchToSection(TAI->SectionForGlobal(F));
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000171 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000172 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000173 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000174 case Function::LinkerPrivateLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000176 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000177 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000178 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000179 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000181 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000182 case Function::LinkOnceAnyLinkage:
183 case Function::LinkOnceODRLinkage:
184 case Function::WeakAnyLinkage:
185 case Function::WeakODRLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000186 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000187 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
189 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000190 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000191 O << "\t.globl\t" << CurrentFnName << "\n"
192 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000193 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000194 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000195 }
196 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000197 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000198
199 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000200
201 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000202 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000203 else if (Subtarget->isTargetCygMing()) {
204 O << "\t.def\t " << CurrentFnName
205 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000206 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000207 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
208 << ";\t.endef\n";
209 }
210
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000212 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000214 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000215 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000216}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000217
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000218/// runOnMachineFunction - This uses the printMachineInstruction()
219/// method to print assembly for each instruction.
220///
221bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
222 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000223 this->MF = &MF;
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000224 unsigned CC = F->getCallingConv();
225
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000226 SetupMachineFunction(MF);
227 O << "\n\n";
228
229 // Populate function information map. Actually, We don't want to populate
230 // non-stdcall or non-fastcall functions' information right now.
231 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
232 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
233
234 // Print out constants referenced by the function
235 EmitConstantPool(MF.getConstantPool());
236
237 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000238 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000239
240 // Print the 'header' of function
241 emitFunctionHeader(MF);
242
243 // Emit pre-function debug and/or EH information.
244 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000245 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000246
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000247 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000248 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000249 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 I != E; ++I) {
251 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000252 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
253 // This is an entry block or a block that's only reachable via a
254 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
255 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000256 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000257 O << '\n';
258 }
Bill Wendling824a7212008-01-26 09:03:52 +0000259 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
260 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000262 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000263 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000264 printMachineInstruction(II);
265 }
266 }
Evan Cheng67afece2006-08-28 22:14:16 +0000267
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000268 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
269 // If the function is empty, then we need to emit *something*. Otherwise,
270 // the function's label might be associated with something that it wasn't
271 // meant to be associated with. We emit a noop in this situation.
272 // We are assuming inline asms are code.
273 O << "\tnop\n";
274 }
275
Jim Laskey563321a2006-09-06 18:34:40 +0000276 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000277 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000278
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000279 // Emit post-function debug information.
Devang Patel5090f192009-06-19 23:21:20 +0000280 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000281 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000282
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000283 // Print out jump tables referenced by the function.
284 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000285
Dan Gohmane5f4de42008-11-07 19:49:17 +0000286 O.flush();
287
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000288 // We didn't modify anything.
289 return false;
290}
291
Chris Lattnerf666c092009-07-13 21:53:19 +0000292/// printSymbolOperand - Print a raw symbol reference operand. This handles
293/// jump tables, constant pools, global address and external symbols, all of
294/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000295void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
296 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000297 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000298 case MachineOperand::MO_JumpTableIndex:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000299 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000300 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000301 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000302 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000303 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000304 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000305 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000306 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000307 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000308 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000309
310 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000311 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
312 Suffix = "$stub";
313 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
314 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
315 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000316 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000317 Suffix = "$non_lazy_ptr";
318
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000319 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000320 if (Subtarget->isTargetCygMing())
321 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000322
Chris Lattner35c89612009-07-09 05:42:07 +0000323 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000324 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
325 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000326
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000327 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
328 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
329 GVStubs[Name] = Mang->getMangledName(GV);
330 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
331 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
332 HiddenGVStubs[Name] = Mang->getMangledName(GV);
333 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
334 FnStubs[Name] = Mang->getMangledName(GV);
335
336 // If the name begins with a dollar-sign, enclose it in parens. We do this
337 // to avoid having it look like an integer immediate to the assembler.
338 if (Name[0] == '$')
339 O << '(' << Name << ')';
340 else
341 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000342
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 Lattnerf813d7d2009-07-15 03:01:23 +0000347 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000348 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000349 FnStubs[Name+"$stub"] = Name;
350 Name += "$stub";
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000351 }
352
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000353 // If the name begins with a dollar-sign, enclose it in parens. We do this
354 // to avoid having it look like an integer immediate to the assembler.
355 if (Name[0] == '$')
356 O << '(' << Name << ')';
357 else
358 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000359 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000360 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000361 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000362
363 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000364 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000365 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000366 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000367 break;
368 case X86II::MO_DARWIN_NONLAZY:
369 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
370 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000371 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000372 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000373 break;
374 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
375 O << " + [.-";
376 PrintPICBaseSymbol();
377 O << ']';
378 break;
379 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000380 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
381 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000382 O << '-';
383 PrintPICBaseSymbol();
384 break;
385 case X86II::MO_TLSGD: O << "@TLSGD"; break;
386 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
387 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
388 case X86II::MO_TPOFF: O << "@TPOFF"; break;
389 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
390 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
391 case X86II::MO_GOT: O << "@GOT"; break;
392 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000393 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000394 }
395}
396
Chris Lattnerf666c092009-07-13 21:53:19 +0000397/// print_pcrel_imm - This is used to print an immediate value that ends up
398/// being encoded as a pc-relative value. These print slightly differently, for
399/// example, a $ is not emitted.
400void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
401 const MachineOperand &MO = MI->getOperand(OpNo);
402 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000403 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000404 case MachineOperand::MO_Immediate:
405 O << MO.getImm();
406 return;
407 case MachineOperand::MO_MachineBasicBlock:
408 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
409 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000410 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000411 case MachineOperand::MO_ExternalSymbol:
412 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000413 return;
414 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000415}
416
417
Chris Lattner174f8162009-07-13 21:41:08 +0000418
419void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
420 const char *Modifier) {
421 const MachineOperand &MO = MI->getOperand(OpNo);
422 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000423 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000424 case MachineOperand::MO_Register: {
425 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
426 "Virtual registers should not make it this far!");
427 O << '%';
428 unsigned Reg = MO.getReg();
429 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
430 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
431 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
432 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
433 Reg = getX86SubSuperRegister(Reg, VT);
434 }
435 O << TRI->getAsmName(Reg);
436 return;
437 }
438
439 case MachineOperand::MO_Immediate:
440 O << '$' << MO.getImm();
441 return;
442
443 case MachineOperand::MO_JumpTableIndex:
444 case MachineOperand::MO_ConstantPoolIndex:
445 case MachineOperand::MO_GlobalAddress:
446 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000447 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000448 printSymbolOperand(MO);
449 break;
450 }
451 }
452}
453
Nate Begeman391c5d22005-11-30 18:54:35 +0000454void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000455 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000456 assert(value <= 7 && "Invalid ssecc argument!");
457 switch (value) {
458 case 0: O << "eq"; break;
459 case 1: O << "lt"; break;
460 case 2: O << "le"; break;
461 case 3: O << "unord"; break;
462 case 4: O << "neq"; break;
463 case 5: O << "nlt"; break;
464 case 6: O << "nle"; break;
465 case 7: O << "ord"; break;
466 }
467}
468
Rafael Espindola094fad32009-04-08 21:14:34 +0000469void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000470 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000471 const MachineOperand &BaseReg = MI->getOperand(Op);
472 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000473 const MachineOperand &DispSpec = MI->getOperand(Op+3);
474
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000475 // If we really don't want to print out (rip), don't.
476 bool HasBaseReg = BaseReg.getReg() != 0;
477 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
478 BaseReg.getReg() == X86::RIP)
479 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000480
481 // HasParenPart - True if we will print out the () part of the mem ref.
482 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
483
484 if (DispSpec.isImm()) {
485 int DispVal = DispSpec.getImm();
486 if (DispVal || !HasParenPart)
487 O << DispVal;
488 } else {
489 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
490 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000491 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000492 }
493
494 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000495 assert(IndexReg.getReg() != X86::ESP &&
496 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000497
Dan Gohmand19a53b2008-06-30 22:03:41 +0000498 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000499 if (HasBaseReg)
500 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000501
502 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000503 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000504 printOperand(MI, Op+2, Modifier);
505 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000506 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000507 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000508 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000509 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000510 }
511}
512
Rafael Espindola094fad32009-04-08 21:14:34 +0000513void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000514 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000515 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000516 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000517 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000518 printOperand(MI, Op+4, Modifier);
519 O << ':';
520 }
Chris Lattner18c59872009-06-27 04:16:01 +0000521 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000522}
523
Anton Korobeynikov91364382008-06-28 11:08:09 +0000524void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000525 const MachineBasicBlock *MBB) const {
526 if (!TAI->getSetDirective())
527 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000528
529 // We don't need .set machinery if we have GOT-style relocations
530 if (Subtarget->isPICStyleGOT())
531 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000532
Evan Chengcc415862007-11-09 01:32:10 +0000533 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
534 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000535 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000536 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000537 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000538 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000539 else {
540 O << '-';
541 PrintPICBaseSymbol();
542 O << '\n';
543 }
Evan Chengcc415862007-11-09 01:32:10 +0000544}
545
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000546
Evan Cheng7ccced62006-02-18 00:15:05 +0000547void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000548 PrintPICBaseSymbol();
549 O << '\n';
550 PrintPICBaseSymbol();
551 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000552}
553
Evan Cheng62f27002006-04-28 23:11:40 +0000554
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000555void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
556 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000557 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000558 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
559 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
560
561 O << JTEntryDirective << ' ';
562
Chris Lattnere2c92082009-07-10 21:00:45 +0000563 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000564 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
565 << '_' << uid << "_set_" << MBB->getNumber();
566 } else if (Subtarget->isPICStyleGOT()) {
567 printBasicBlockLabel(MBB, false, false, false);
568 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000569 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000570 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000571}
572
Chris Lattner37710712009-06-15 04:42:32 +0000573bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000574 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000575 switch (Mode) {
576 default: return true; // Unknown mode.
577 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000578 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000579 break;
580 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000581 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000582 break;
583 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000584 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000585 break;
586 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000587 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000588 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000589 case 'q': // Print DImode register
590 Reg = getX86SubSuperRegister(Reg, MVT::i64);
591 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000592 }
593
Evan Chengae270f62008-07-07 22:21:06 +0000594 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000595 return false;
596}
597
Evan Cheng3d48a902006-04-28 21:19:05 +0000598/// PrintAsmOperand - Print out an operand for an inline asm expression.
599///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000600bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000601 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000602 const char *ExtraCode) {
603 // Does this asm operand have a single letter operand modifier?
604 if (ExtraCode && ExtraCode[0]) {
605 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000606
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000607 const MachineOperand &MO = MI->getOperand(OpNo);
608
Evan Cheng3d48a902006-04-28 21:19:05 +0000609 switch (ExtraCode[0]) {
610 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000611 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000612 if (MO.isImm())
613 O << MO.getImm();
614 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
615 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000616 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000617 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000618 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000619
620 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000621 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000622 O << '*';
623 printOperand(MI, OpNo);
624 return false;
625 }
626 return true;
627
Evan Cheng62f27002006-04-28 23:11:40 +0000628 case 'b': // Print QImode register
629 case 'h': // Print QImode high register
630 case 'w': // Print HImode register
631 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000632 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000633 if (MO.isReg())
634 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000635 printOperand(MI, OpNo);
636 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000637
Dale Johannesen9fcff042009-07-07 23:28:22 +0000638 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000639 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000640 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000641
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000642 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000643 // Note: this is a temporary solution. It should be handled target
644 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000645 if (MO.isImm()) {
646 O << -MO.getImm();
647 return false;
648 }
649 O << '-';
650 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000651 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000652
Evan Cheng3d48a902006-04-28 21:19:05 +0000653 printOperand(MI, OpNo);
654 return false;
655}
656
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000657bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000658 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000659 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000660 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000661 if (ExtraCode && ExtraCode[0]) {
662 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000663
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000664 switch (ExtraCode[0]) {
665 default: return true; // Unknown modifier.
666 case 'b': // Print QImode register
667 case 'h': // Print QImode high register
668 case 'w': // Print HImode register
669 case 'k': // Print SImode register
670 case 'q': // Print SImode register
671 // These only apply to registers, ignore on mem.
672 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000673 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000674 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000675 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000676 }
677 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000678 printMemReference(MI, OpNo);
679 return false;
680}
681
Chris Lattnerc1243062009-06-20 07:03:18 +0000682static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
683 // Convert registers in the addr mode according to subreg64.
684 for (unsigned i = 0; i != 4; ++i) {
685 if (!MI->getOperand(i).isReg()) continue;
686
687 unsigned Reg = MI->getOperand(i).getReg();
688 if (Reg == 0) continue;
689
690 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
691 }
692}
693
Bill Wendlingac06d002009-02-06 21:45:08 +0000694/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
695/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000696///
697void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
698 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000699
Chris Lattner475370b2009-06-19 00:47:33 +0000700 if (NewAsmPrinter) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000701 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
702 O << "\t";
703 printInlineAsm(MI);
704 return;
705 } else if (MI->isLabel()) {
706 printLabel(MI);
707 return;
708 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
709 printDeclare(MI);
710 return;
711 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
712 printImplicitDef(MI);
713 return;
714 }
715
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000716 O << "NEW: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000717 MCInst TmpInst;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000718
719 TmpInst.setOpcode(MI->getOpcode());
720
721 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
722 const MachineOperand &MO = MI->getOperand(i);
723
724 MCOperand MCOp;
725 if (MO.isReg()) {
726 MCOp.MakeReg(MO.getReg());
727 } else if (MO.isImm()) {
728 MCOp.MakeImm(MO.getImm());
Chris Lattnerc1243062009-06-20 07:03:18 +0000729 } else if (MO.isMBB()) {
730 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000731 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000732 llvm_unreachable("Unimp");
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000733 }
734
735 TmpInst.addOperand(MCOp);
736 }
737
Chris Lattnerdc479f62009-06-20 07:59:10 +0000738 switch (TmpInst.getOpcode()) {
739 case X86::LEA64_32r:
740 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerc1243062009-06-20 07:03:18 +0000741 lower_lea64_32mem(&TmpInst, 1);
Chris Lattnerdc479f62009-06-20 07:59:10 +0000742 break;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000743 }
744
Chris Lattner475370b2009-06-19 00:47:33 +0000745 // FIXME: Convert TmpInst.
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000746 printInstruction(&TmpInst);
747 O << "OLD: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000748 }
749
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000750 // Call the autogenerated instruction printer routines.
751 printInstruction(MI);
752}
753
Devang Patel66b4d3b2009-06-20 01:00:07 +0000754/// doInitialization
755bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner40e3c7a2009-06-24 05:46:28 +0000756 if (NewAsmPrinter) {
757 Context = new MCContext();
758 // FIXME: Send this to "O" instead of outs(). For now, we force it to
759 // stdout to make it easy to compare.
760 Streamer = createAsmStreamer(*Context, outs());
761 }
762
Devang Patel66b4d3b2009-06-20 01:00:07 +0000763 return AsmPrinter::doInitialization(M);
764}
765
Chris Lattner40bbebd2009-07-21 18:38:57 +0000766void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000767 const TargetData *TD = TM.getTargetData();
768
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769 if (!GVar->hasInitializer())
770 return; // External global require no code
771
772 // Check to see if this is a special global used by LLVM, if so, emit it.
773 if (EmitSpecialLLVMGlobal(GVar)) {
774 if (Subtarget->isTargetDarwin() &&
775 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000776 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000777 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000778 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000779 O << ".reference .destructors_used\n";
780 }
781 return;
782 }
783
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000784 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000785 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000786 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000787 return;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000788 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000789 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000790 unsigned Align = TD->getPreferredAlignmentLog(GVar);
791
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000792 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793
794 if (Subtarget->isTargetELF())
795 O << "\t.type\t" << name << ",@object\n";
796
Chris Lattnerfb3431a2009-07-24 03:49:17 +0000797 const Section *TheSection = TAI->SectionForGlobal(GVar);
798 SwitchToSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000799
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000800 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000801 // Don't put things that should go in the cstring section into "comm".
802 !TheSection->hasFlag(SectionFlags::Strings)) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000803 if (GVar->hasExternalLinkage()) {
804 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000805 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000806 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000807 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 return;
809 }
810 }
811
812 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000813 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000815
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000817 if (GVar->hasLocalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000818 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000819 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000820 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000821 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000822 O << "\t.globl " << name << '\n'
823 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000824 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000825 O << name << ":";
826 if (VerboseAsm) {
Evan Cheng7db860d2009-03-25 01:08:42 +0000827 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000828 PrintUnmangledNameSafely(GVar, O);
829 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000831 EmitGlobalConstant(C);
832 return;
833 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +0000835 if (TAI->getCOMMDirectiveTakesAlignment())
836 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000837 }
838 } else {
839 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000840 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000841 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000842 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000843 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000846 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000847 if (VerboseAsm) {
848 O << "\t\t" << TAI->getCommentString() << ' ';
849 PrintUnmangledNameSafely(GVar, O);
850 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 return;
853 }
854 }
855
856 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000857 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000858 case GlobalValue::LinkOnceAnyLinkage:
859 case GlobalValue::LinkOnceODRLinkage:
860 case GlobalValue::WeakAnyLinkage:
861 case GlobalValue::WeakODRLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000862 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000863 O << "\t.globl " << name << '\n'
864 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000865 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000866 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000867 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000868 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000869 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000870 }
871 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000872 case GlobalValue::DLLExportLinkage:
873 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000874 // FIXME: appending linkage variables should go into a section of
875 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000876 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000877 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000878 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000879 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000880 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000881 case GlobalValue::LinkerPrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000882 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000883 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000884 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000885 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000886 }
887
888 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000889 O << name << ":";
890 if (VerboseAsm){
Evan Cheng7db860d2009-03-25 01:08:42 +0000891 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000892 PrintUnmangledNameSafely(GVar, O);
893 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000894 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000895 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000896 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000897
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000898 EmitGlobalConstant(C);
899}
900
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000901bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000902 // Print out module-level global variables here.
903 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000904 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000905 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000906 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000907 }
908
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000909 if (Subtarget->isTargetDarwin()) {
910 SwitchToDataSection("");
Chris Lattner8f61f982009-06-24 18:24:09 +0000911
Chris Lattner381d4fe2009-06-24 18:17:00 +0000912 // Add the (possibly multiple) personalities to the set of global value
913 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000914 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000915 const std::vector<Function*> &Personalities = MMI->getPersonalities();
916 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000917 if (Personalities[i])
918 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
919 true /*private label*/)] =
920 Mang->getMangledName(Personalities[i]);
Evan Cheng77c8f762008-07-08 00:55:58 +0000921 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000922 }
923
Chris Lattner8f61f982009-06-24 18:24:09 +0000924 // Output stubs for dynamically-linked functions
925 if (!FnStubs.empty()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000926 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
927 "self_modifying_code+pure_instructions,5", 0);
928 for (StringMap<std::string>::iterator I = FnStubs.begin(),
929 E = FnStubs.end(); I != E; ++I)
930 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
931 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000932 O << '\n';
933 }
934
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000935 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000936 if (!GVStubs.empty()) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000937 SwitchToDataSection(
938 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000939 for (StringMap<std::string>::iterator I = GVStubs.begin(),
940 E = GVStubs.end(); I != E; ++I)
941 O << I->getKeyData() << ":\n\t.indirect_symbol "
942 << I->second << "\n\t.long\t0\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000943 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000944
Evan Chengae94e592008-12-05 01:06:39 +0000945 if (!HiddenGVStubs.empty()) {
946 SwitchToSection(TAI->getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000947 EmitAlignment(2);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000948 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
949 E = HiddenGVStubs.end(); I != E; ++I)
950 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
951 << I->second << '\n';
Evan Chengae94e592008-12-05 01:06:39 +0000952 }
953
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000954 // Funny Darwin hack: This flag tells the linker that no global symbols
955 // contain code that falls through to other global symbols (e.g. the obvious
956 // implementation of multiple entry points). If this doesn't occur, the
957 // linker can safely perform dead code stripping. Since LLVM never
958 // generates code that does this, it is always safe to set.
959 O << "\t.subsections_via_symbols\n";
960 } else if (Subtarget->isTargetCygMing()) {
961 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000962 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000963 i != e; ++i) {
964 O << "\t.def\t " << i->getKeyData()
965 << ";\t.scl\t" << COFF::C_EXT
966 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
967 << ";\t.endef\n";
968 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000969 }
Chris Lattner974469d2009-06-24 05:47:59 +0000970
Chris Lattner0a7befa2009-06-24 18:52:01 +0000971
972 // Output linker support code for dllexported globals on windows.
973 if (!DLLExportedGVs.empty()) {
974 SwitchToDataSection(".section .drectve");
975
976 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
977 e = DLLExportedGVs.end(); i != e; ++i)
978 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
979 }
980
981 if (!DLLExportedFns.empty()) {
982 SwitchToDataSection(".section .drectve");
983
984 for (StringSet<>::iterator i = DLLExportedFns.begin(),
985 e = DLLExportedFns.end();
986 i != e; ++i)
987 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
988 }
989
Chris Lattner0a7befa2009-06-24 18:52:01 +0000990 // Do common shutdown.
991 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner974469d2009-06-24 05:47:59 +0000992
Chris Lattner40e3c7a2009-06-24 05:46:28 +0000993 if (NewAsmPrinter) {
994 Streamer->Finish();
995
996 delete Streamer;
997 delete Context;
998 Streamer = 0;
999 Context = 0;
1000 }
1001
Chris Lattner0a7befa2009-06-24 18:52:01 +00001002 return Changed;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001003}
1004
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001005// Include the auto-generated portion of the assembly writer.
1006#include "X86GenAsmWriter.inc"