blob: c8e9efd468b3ea019b8827b6ca31e87e9845d057 [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"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000022#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000025#include "llvm/Type.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Chris Lattner40e3c7a2009-06-24 05:46:28 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattner522e9a02009-09-02 17:35:12 +000030#include "llvm/MC/MCSectionMachO.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner475370b2009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000037#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000039using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner475370b2009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
Chris Lattner33c6aa62009-09-09 06:11:14 +000044 cl::Hidden, cl::init(false));
Chris Lattner475370b2009-06-19 00:47:33 +000045
Chris Lattnerab162992009-06-24 19:44:36 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000049
50void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner9b60e042009-08-16 04:28:14 +000051 // FIXME: the actual label generated doesn't matter here! Just mangle in
52 // something unique (the function number) with Private prefix.
Evan Cheng071b9d52007-01-18 01:49:58 +000053 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattner9b60e042009-08-16 04:28:14 +000055 else {
56 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner8529d282009-07-08 23:09:14 +000057 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattner9b60e042009-08-16 04:28:14 +000058 }
59}
60
Anton Korobeynikov75b68822008-06-28 11:08:27 +000061static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
62 const TargetData *TD) {
63 X86MachineFunctionInfo Info;
64 uint64_t Size = 0;
65
66 switch (F->getCallingConv()) {
67 case CallingConv::X86_StdCall:
68 Info.setDecorationStyle(StdCall);
69 break;
70 case CallingConv::X86_FastCall:
71 Info.setDecorationStyle(FastCall);
72 break;
73 default:
74 return Info;
75 }
76
77 unsigned argNum = 1;
78 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
79 AI != AE; ++AI, ++argNum) {
80 const Type* Ty = AI->getType();
81
82 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000083 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000084 Ty = cast<PointerType>(Ty)->getElementType();
85
86 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000087 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000088 }
89
90 // We're not supporting tooooo huge arguments :)
91 Info.setBytesToPopOnReturn((unsigned int)Size);
92 return Info;
93}
94
Chris Lattnerc08872e2009-07-15 04:55:56 +000095/// DecorateCygMingName - Query FunctionInfoMap and use this information for
96/// various name decorations for Cygwin and MingW.
97void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
98 const GlobalValue *GV) {
99 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
100
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000101 const Function *F = dyn_cast<Function>(GV);
102 if (!F) return;
103
Chris Lattner7a4e4642009-07-10 21:57:21 +0000104 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000105 if (F->isDeclaration())
Chris Lattner7a4e4642009-07-10 21:57:21 +0000106 CygMingStubs.insert(Name);
107
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000108 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000109 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000110 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
111 return;
112
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000113
114 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000115
116 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000117 if (info_item == FunctionInfoMap.end()) {
118 // Calculate apropriate function info and populate map
119 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
120 Info = &FunctionInfoMap[F];
121 } else {
122 Info = &info_item->second;
123 }
124
125 const FunctionType *FT = F->getFunctionType();
126 switch (Info->getDecorationStyle()) {
127 case None:
128 break;
129 case StdCall:
130 // "Pure" variadic functions do not receive @0 suffix.
131 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
132 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
133 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
134 break;
135 case FastCall:
136 // "Pure" variadic functions do not receive @0 suffix.
137 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
138 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
139 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
140
141 if (Name[0] == '_') {
142 Name[0] = '@';
143 } else {
144 Name = '@' + Name;
145 }
146 break;
147 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000148 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000149 }
150}
151
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000152void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000153 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000155
Chris Lattnerc08872e2009-07-15 04:55:56 +0000156 if (Subtarget->isTargetCygMing())
157 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000158
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000159 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000160 EmitAlignment(FnAlign, F);
161
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000165 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000171 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000172 case Function::LinkOnceAnyLinkage:
173 case Function::LinkOnceODRLinkage:
174 case Function::WeakAnyLinkage:
175 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000178 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 }
185 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
Dan Gohmancf20ac42009-08-13 01:36:44 +0000200 O << CurrentFnName << ':';
201 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000202 O.PadToColumn(MAI->getCommentColumn());
203 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000204 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
205 }
206 O << '\n';
207
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000208 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000219 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000220 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000221
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000222 SetupMachineFunction(MF);
223 O << "\n\n";
224
225 // Populate function information map. Actually, We don't want to populate
226 // non-stdcall or non-fastcall functions' information right now.
227 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
228 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
229
230 // Print out constants referenced by the function
231 EmitConstantPool(MF.getConstantPool());
232
233 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000234 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000235
236 // Print the 'header' of function
237 emitFunctionHeader(MF);
238
239 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000240 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000248 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
249 // This is an entry block or a block that's only reachable via a
250 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
251 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000253 O << '\n';
254 }
Bill Wendling824a7212008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 printMachineInstruction(II);
261 }
262 }
Evan Cheng67afece2006-08-28 22:14:16 +0000263
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
265 // If the function is empty, then we need to emit *something*. Otherwise,
266 // the function's label might be associated with something that it wasn't
267 // meant to be associated with. We emit a noop in this situation.
268 // We are assuming inline asms are code.
269 O << "\tnop\n";
270 }
271
Chris Lattner33adcfb2009-08-22 21:43:10 +0000272 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000276 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000278
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000281
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 // We didn't modify anything.
283 return false;
284}
285
Chris Lattnerf666c092009-07-13 21:53:19 +0000286/// printSymbolOperand - Print a raw symbol reference operand. This handles
287/// jump tables, constant pools, global address and external symbols, all of
288/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000289void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
290 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000291 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000292 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000293 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000294 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000295 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000296 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000297 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000299 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000300 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000302 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000303
304 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000305 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
306 Suffix = "$stub";
307 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
308 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000309 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000310 Suffix = "$non_lazy_ptr";
311
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000312 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000313 if (Subtarget->isTargetCygMing())
314 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000315
Chris Lattner35c89612009-07-09 05:42:07 +0000316 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000317 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
318 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000319
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000320 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
321 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
322 GVStubs[Name] = Mang->getMangledName(GV);
Evan Cheng63476a82009-09-03 07:04:02 +0000323 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000324 HiddenGVStubs[Name] = Mang->getMangledName(GV);
325 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
326 FnStubs[Name] = Mang->getMangledName(GV);
327
328 // If the name begins with a dollar-sign, enclose it in parens. We do this
329 // to avoid having it look like an integer immediate to the assembler.
330 if (Name[0] == '$')
331 O << '(' << Name << ')';
332 else
333 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000334
Chris Lattner35c89612009-07-09 05:42:07 +0000335 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000336 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000337 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000338 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000339 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000340 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000341 FnStubs[Name+"$stub"] = Name;
342 Name += "$stub";
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000343 }
344
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000345 // If the name begins with a dollar-sign, enclose it in parens. We do this
346 // to avoid having it look like an integer immediate to the assembler.
347 if (Name[0] == '$')
348 O << '(' << Name << ')';
349 else
350 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000351 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000352 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000353 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000354
355 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000356 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000357 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000358 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000359 break;
360 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000361 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000362 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000363 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000364 break;
365 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
366 O << " + [.-";
367 PrintPICBaseSymbol();
368 O << ']';
369 break;
370 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000371 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
372 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000373 O << '-';
374 PrintPICBaseSymbol();
375 break;
376 case X86II::MO_TLSGD: O << "@TLSGD"; break;
377 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
378 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
379 case X86II::MO_TPOFF: O << "@TPOFF"; break;
380 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
381 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
382 case X86II::MO_GOT: O << "@GOT"; break;
383 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000384 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000385 }
386}
387
Chris Lattnerf666c092009-07-13 21:53:19 +0000388/// print_pcrel_imm - This is used to print an immediate value that ends up
389/// being encoded as a pc-relative value. These print slightly differently, for
390/// example, a $ is not emitted.
391void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
392 const MachineOperand &MO = MI->getOperand(OpNo);
393 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000394 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000395 case MachineOperand::MO_Immediate:
396 O << MO.getImm();
397 return;
398 case MachineOperand::MO_MachineBasicBlock:
Dan Gohmancf20ac42009-08-13 01:36:44 +0000399 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerf666c092009-07-13 21:53:19 +0000400 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000401 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000402 case MachineOperand::MO_ExternalSymbol:
403 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000404 return;
405 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000406}
407
408
Chris Lattner174f8162009-07-13 21:41:08 +0000409
410void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
411 const char *Modifier) {
412 const MachineOperand &MO = MI->getOperand(OpNo);
413 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000414 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000415 case MachineOperand::MO_Register: {
416 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
417 "Virtual registers should not make it this far!");
418 O << '%';
419 unsigned Reg = MO.getReg();
420 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000421 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
423 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000424 Reg = getX86SubSuperRegister(Reg, VT);
425 }
426 O << TRI->getAsmName(Reg);
427 return;
428 }
429
430 case MachineOperand::MO_Immediate:
431 O << '$' << MO.getImm();
432 return;
433
434 case MachineOperand::MO_JumpTableIndex:
435 case MachineOperand::MO_ConstantPoolIndex:
436 case MachineOperand::MO_GlobalAddress:
437 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000438 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000439 printSymbolOperand(MO);
440 break;
441 }
442 }
443}
444
Nate Begeman391c5d22005-11-30 18:54:35 +0000445void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000446 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000447 assert(value <= 7 && "Invalid ssecc argument!");
448 switch (value) {
449 case 0: O << "eq"; break;
450 case 1: O << "lt"; break;
451 case 2: O << "le"; break;
452 case 3: O << "unord"; break;
453 case 4: O << "neq"; break;
454 case 5: O << "nlt"; break;
455 case 6: O << "nle"; break;
456 case 7: O << "ord"; break;
457 }
458}
459
Rafael Espindola094fad32009-04-08 21:14:34 +0000460void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000461 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000462 const MachineOperand &BaseReg = MI->getOperand(Op);
463 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000464 const MachineOperand &DispSpec = MI->getOperand(Op+3);
465
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000466 // If we really don't want to print out (rip), don't.
467 bool HasBaseReg = BaseReg.getReg() != 0;
468 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
469 BaseReg.getReg() == X86::RIP)
470 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000471
472 // HasParenPart - True if we will print out the () part of the mem ref.
473 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
474
475 if (DispSpec.isImm()) {
476 int DispVal = DispSpec.getImm();
477 if (DispVal || !HasParenPart)
478 O << DispVal;
479 } else {
480 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
481 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000482 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000483 }
484
485 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000486 assert(IndexReg.getReg() != X86::ESP &&
487 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000488
Dan Gohmand19a53b2008-06-30 22:03:41 +0000489 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000490 if (HasBaseReg)
491 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000492
493 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000494 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000495 printOperand(MI, Op+2, Modifier);
496 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000497 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000498 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000499 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000500 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000501 }
502}
503
Rafael Espindola094fad32009-04-08 21:14:34 +0000504void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000505 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000506 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000507 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000508 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000509 printOperand(MI, Op+4, Modifier);
510 O << ':';
511 }
Chris Lattner18c59872009-06-27 04:16:01 +0000512 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000513}
514
Anton Korobeynikov91364382008-06-28 11:08:09 +0000515void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000516 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000517 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000518 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000519
520 // We don't need .set machinery if we have GOT-style relocations
521 if (Subtarget->isPICStyleGOT())
522 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000523
Chris Lattner33adcfb2009-08-22 21:43:10 +0000524 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000525 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000526 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000527 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000528 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000529 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000530 else {
531 O << '-';
532 PrintPICBaseSymbol();
533 O << '\n';
534 }
Evan Chengcc415862007-11-09 01:32:10 +0000535}
536
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000537
Evan Cheng7ccced62006-02-18 00:15:05 +0000538void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000539 PrintPICBaseSymbol();
540 O << '\n';
541 PrintPICBaseSymbol();
542 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000543}
544
Evan Cheng62f27002006-04-28 23:11:40 +0000545
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000546void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
547 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000548 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000549 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000550 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000551
552 O << JTEntryDirective << ' ';
553
Chris Lattnere2c92082009-07-10 21:00:45 +0000554 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000555 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000556 << '_' << uid << "_set_" << MBB->getNumber();
557 } else if (Subtarget->isPICStyleGOT()) {
558 printBasicBlockLabel(MBB, false, false, false);
559 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000560 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000561 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000562}
563
Chris Lattner37710712009-06-15 04:42:32 +0000564bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000565 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000566 switch (Mode) {
567 default: return true; // Unknown mode.
568 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000570 break;
571 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000573 break;
574 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000576 break;
577 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000579 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000580 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000582 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000583 }
584
Evan Chengae270f62008-07-07 22:21:06 +0000585 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000586 return false;
587}
588
Evan Cheng3d48a902006-04-28 21:19:05 +0000589/// PrintAsmOperand - Print out an operand for an inline asm expression.
590///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000591bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000592 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000593 const char *ExtraCode) {
594 // Does this asm operand have a single letter operand modifier?
595 if (ExtraCode && ExtraCode[0]) {
596 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000597
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000598 const MachineOperand &MO = MI->getOperand(OpNo);
599
Evan Cheng3d48a902006-04-28 21:19:05 +0000600 switch (ExtraCode[0]) {
601 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000602 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
603 if (MO.isImm()) {
604 O << MO.getImm();
605 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000606 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000607 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
608 printSymbolOperand(MO);
609 return false;
610 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000611 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000612 O << '(';
613 printOperand(MI, OpNo);
614 O << ')';
615 return false;
616 }
617 return true;
618
Chris Lattnerb4828722007-01-25 02:53:24 +0000619 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000620 if (MO.isImm())
621 O << MO.getImm();
622 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
623 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000624 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000625 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000626 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000627
628 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000629 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000630 O << '*';
631 printOperand(MI, OpNo);
632 return false;
633 }
634 return true;
635
Evan Cheng62f27002006-04-28 23:11:40 +0000636 case 'b': // Print QImode register
637 case 'h': // Print QImode high register
638 case 'w': // Print HImode register
639 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000640 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000641 if (MO.isReg())
642 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000643 printOperand(MI, OpNo);
644 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000645
Dale Johannesen9fcff042009-07-07 23:28:22 +0000646 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000647 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000648 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000649
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000650 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000651 // Note: this is a temporary solution. It should be handled target
652 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000653 if (MO.isImm()) {
654 O << -MO.getImm();
655 return false;
656 }
657 O << '-';
658 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000659 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000660
Evan Cheng3d48a902006-04-28 21:19:05 +0000661 printOperand(MI, OpNo);
662 return false;
663}
664
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000665bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000666 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000667 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000668 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000669 if (ExtraCode && ExtraCode[0]) {
670 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000671
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000672 switch (ExtraCode[0]) {
673 default: return true; // Unknown modifier.
674 case 'b': // Print QImode register
675 case 'h': // Print QImode high register
676 case 'w': // Print HImode register
677 case 'k': // Print SImode register
678 case 'q': // Print SImode register
679 // These only apply to registers, ignore on mem.
680 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000681 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000682 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000683 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000684 }
685 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000686 printMemReference(MI, OpNo);
687 return false;
688}
689
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000690
Chris Lattner9b60e042009-08-16 04:28:14 +0000691
Bill Wendlingac06d002009-02-06 21:45:08 +0000692/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
693/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000694///
695void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
696 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000697
Chris Lattner634cca32009-09-09 20:34:59 +0000698 processDebugLoc(MI->getDebugLoc());
699
Chris Lattner522e9a02009-09-02 17:35:12 +0000700 // Call the autogenerated instruction printer routines.
701 if (NewAsmPrinter)
702 printInstructionThroughMCStreamer(MI);
703 else
Chris Lattner9b60e042009-08-16 04:28:14 +0000704 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000705
706 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
707 EmitComments(*MI);
708 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000709}
710
Chris Lattner40bbebd2009-07-21 18:38:57 +0000711void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000712 const TargetData *TD = TM.getTargetData();
713
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000714 if (!GVar->hasInitializer())
715 return; // External global require no code
716
717 // Check to see if this is a special global used by LLVM, if so, emit it.
718 if (EmitSpecialLLVMGlobal(GVar)) {
719 if (Subtarget->isTargetDarwin() &&
720 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000721 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000722 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000723 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000724 O << ".reference .destructors_used\n";
725 }
726 return;
727 }
728
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000729 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000730 Constant *C = GVar->getInitializer();
731 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000732 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000733 unsigned Align = TD->getPreferredAlignmentLog(GVar);
734
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000735 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000736
737 if (Subtarget->isTargetELF())
738 O << "\t.type\t" << name << ",@object\n";
739
Chris Lattner26d054d2009-08-05 05:21:07 +0000740
741 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000742 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000743 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000744 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000745
Chris Lattner3b24c012009-08-04 05:35:56 +0000746 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000747 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000748 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000749 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000750 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000751 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000752 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000753 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000754 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000755 return;
756 }
757 }
758
759 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000760 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000761 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000762
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000764 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000765 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000766 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000767 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000768 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000769 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000770 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000771 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000772 O << name << ":";
773 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000774 O.PadToColumn(MAI->getCommentColumn());
775 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000776 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000777 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000778 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000779 EmitGlobalConstant(C);
780 return;
781 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000782 O << MAI->getCOMMDirective() << name << ',' << Size;
783 if (MAI->getCOMMDirectiveTakesAlignment())
784 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000785 }
786 } else {
787 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000788 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000789 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000790 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000791 O << MAI->getCOMMDirective() << name << ',' << Size;
792 if (MAI->getCOMMDirectiveTakesAlignment())
793 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000794 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000795 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000796 O.PadToColumn(MAI->getCommentColumn());
797 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000798 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000799 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000800 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000801 return;
802 }
803 }
804
805 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000806 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000807 case GlobalValue::LinkOnceAnyLinkage:
808 case GlobalValue::LinkOnceODRLinkage:
809 case GlobalValue::WeakAnyLinkage:
810 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000811 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000812 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000813 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000814 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000815 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000817 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000819 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000820 }
821 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000822 case GlobalValue::DLLExportLinkage:
823 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000824 // FIXME: appending linkage variables should go into a section of
825 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000826 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000827 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000829 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000830 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000831 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000832 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000833 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000834 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 }
836
837 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000838 O << name << ":";
839 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000840 O.PadToColumn(MAI->getCommentColumn());
841 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000842 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000843 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000844 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000845
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000846 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000847
Chris Lattner33adcfb2009-08-22 21:43:10 +0000848 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000849 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850}
851
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000853 // Print out module-level global variables here.
854 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000855 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000856 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000857 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000858 }
859
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000860 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000861 // All darwin targets use mach-o.
862 TargetLoweringObjectFileMachO &TLOFMacho =
863 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000864
Chris Lattner381d4fe2009-06-24 18:17:00 +0000865 // Add the (possibly multiple) personalities to the set of global value
866 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000867 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000868 const std::vector<Function*> &Personalities = MMI->getPersonalities();
869 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000870 if (Personalities[i])
871 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
872 true /*private label*/)] =
873 Mang->getMangledName(Personalities[i]);
Evan Cheng77c8f762008-07-08 00:55:58 +0000874 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000875 }
876
Chris Lattner8f61f982009-06-24 18:24:09 +0000877 // Output stubs for dynamically-linked functions
878 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000879 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000880 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
881 MCSectionMachO::S_SYMBOL_STUBS |
882 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
883 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
884 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000885 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000886 for (StringMap<std::string>::iterator I = FnStubs.begin(),
887 E = FnStubs.end(); I != E; ++I)
888 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
889 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000890 O << '\n';
891 }
892
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000893 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000894 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000895 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000896 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
897 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000898 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000899 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000900 for (StringMap<std::string>::iterator I = GVStubs.begin(),
901 E = GVStubs.end(); I != E; ++I)
902 O << I->getKeyData() << ":\n\t.indirect_symbol "
903 << I->second << "\n\t.long\t0\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000904 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000905
Evan Chengae94e592008-12-05 01:06:39 +0000906 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000907 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000908 EmitAlignment(2);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000909 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
910 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000911 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000912 << I->second << '\n';
Evan Chengae94e592008-12-05 01:06:39 +0000913 }
914
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000915 // Funny Darwin hack: This flag tells the linker that no global symbols
916 // contain code that falls through to other global symbols (e.g. the obvious
917 // implementation of multiple entry points). If this doesn't occur, the
918 // linker can safely perform dead code stripping. Since LLVM never
919 // generates code that does this, it is always safe to set.
920 O << "\t.subsections_via_symbols\n";
921 } else if (Subtarget->isTargetCygMing()) {
922 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000923 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000924 i != e; ++i) {
925 O << "\t.def\t " << i->getKeyData()
926 << ";\t.scl\t" << COFF::C_EXT
927 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
928 << ";\t.endef\n";
929 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000930 }
Chris Lattner974469d2009-06-24 05:47:59 +0000931
Chris Lattner0a7befa2009-06-24 18:52:01 +0000932
933 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +0000934 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
935 // dllexport symbols only exist on coff targets.
936 TargetLoweringObjectFileCOFF &TLOFMacho =
937 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
938
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000939 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
940 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +0000941
942 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
943 e = DLLExportedGVs.end(); i != e; ++i)
944 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +0000945
946 for (StringSet<>::iterator i = DLLExportedFns.begin(),
947 e = DLLExportedFns.end();
948 i != e; ++i)
949 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
950 }
951
Chris Lattner0a7befa2009-06-24 18:52:01 +0000952 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +0000953 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000954}
955
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000956// Include the auto-generated portion of the assembly writer.
957#include "X86GenAsmWriter.inc"