blob: 70f6a27022f6bda195c1a014876fb240703c7d90 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000023#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000025#include "llvm/Type.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
Dan Gohman2aa282f2009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattnerb16f72e2009-09-02 17:35:12 +000030#include "llvm/MC/MCSectionMachO.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000037#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner19b1bd52009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
44 cl::Hidden);
45
Chris Lattner2e845952009-06-24 19:44:36 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000049
50void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattnerf39997f2009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerf39997f2009-08-16 04:28:14 +000055 else {
56 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner021ad452009-07-08 23:09:14 +000057 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattnerf39997f2009-08-16 04:28:14 +000058 }
59}
60
Anton Korobeynikovab6c6a42008-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 Pateld222f862008-09-25 21:00:45 +000083 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000084 Ty = cast<PointerType>(Ty)->getElementType();
85
86 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000087 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-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 Lattner8cfe9142009-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 Korobeynikovab6c6a42008-06-28 11:08:27 +0000101 const Function *F = dyn_cast<Function>(GV);
102 if (!F) return;
103
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000104 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000105 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000106 CygMingStubs.insert(Name);
107
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000108 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel5838baa2009-09-02 08:44:58 +0000109 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000110 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
111 return;
112
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000113
114 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000115
116 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-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:
Edwin Törökbd448e32009-07-14 16:55:14 +0000148 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000149 }
150}
151
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000152void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000153 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
Chris Lattner8cfe9142009-07-15 04:55:56 +0000156 if (Subtarget->isTargetCygMing())
157 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Chris Lattner73266f92009-08-19 05:49:37 +0000159 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner1b0ec672009-08-03 22:16:57 +0000160 EmitAlignment(FnAlign, F);
161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000165 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 break;
167 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 case Function::ExternalLinkage:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
Dale Johannesenf03574c2009-08-13 00:28:52 +0000171 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000172 case Function::LinkOnceAnyLinkage:
173 case Function::LinkOnceODRLinkage:
174 case Function::WeakAnyLinkage:
175 case Function::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000178 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 }
185 break;
186 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
Dan Gohman2aa282f2009-08-13 01:36:44 +0000200 O << CurrentFnName << ':';
201 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000202 O.PadToColumn(MAI->getCommentColumn());
203 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000204 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
205 }
206 O << '\n';
207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 // Add some workaround for linkonce linkage on Cygwin\MinGW
209 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Anton Korobeynikov30948e32008-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 Wendlingb3b11262009-02-06 21:45:08 +0000219 this->MF = &MF;
Sandeep Patel5838baa2009-09-02 08:44:58 +0000220 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000221
Anton Korobeynikov30948e32008-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 Lattnerb3cdde62009-07-14 18:17:16 +0000234 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-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 Lattnera5ef4d32009-08-22 21:43:10 +0000240 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohmand38f8762009-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 Cheng11db8142009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 O << '\n';
254 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 printMachineInstruction(II);
261 }
262 }
263
Dale Johannesenf35771f2008-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 Lattnera5ef4d32009-08-22 21:43:10 +0000272 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000276 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 // We didn't modify anything.
283 return false;
284}
285
Chris Lattnerae8f9592009-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 Lattner207a0ca2009-07-13 21:41:08 +0000289void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
290 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000291 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000292 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000293 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000294 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000295 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000296 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000297 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000299 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000300 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000302 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000303
304 const char *Suffix = "";
Chris Lattnerb3cdde62009-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 ||
309 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000310 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000311 Suffix = "$non_lazy_ptr";
312
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000313 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000314 if (Subtarget->isTargetCygMing())
315 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000316
Chris Lattner0cc2b792009-07-09 05:42:07 +0000317 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000318 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
319 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000320
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000321 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
322 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
323 GVStubs[Name] = Mang->getMangledName(GV);
324 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
325 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
326 HiddenGVStubs[Name] = Mang->getMangledName(GV);
327 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
328 FnStubs[Name] = Mang->getMangledName(GV);
329
330 // If the name begins with a dollar-sign, enclose it in parens. We do this
331 // to avoid having it look like an integer immediate to the assembler.
332 if (Name[0] == '$')
333 O << '(' << Name << ')';
334 else
335 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000336
Chris Lattner0cc2b792009-07-09 05:42:07 +0000337 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000338 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000340 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000341 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000342 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000343 FnStubs[Name+"$stub"] = Name;
344 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000345 }
346
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000347 // If the name begins with a dollar-sign, enclose it in parens. We do this
348 // to avoid having it look like an integer immediate to the assembler.
349 if (Name[0] == '$')
350 O << '(' << Name << ')';
351 else
352 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000353 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000355 }
Chris Lattner797a0782009-06-27 05:46:24 +0000356
357 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000359 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000360 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000361 break;
362 case X86II::MO_DARWIN_NONLAZY:
363 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
364 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000365 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000366 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000367 break;
368 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
369 O << " + [.-";
370 PrintPICBaseSymbol();
371 O << ']';
372 break;
373 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000374 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
375 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000376 O << '-';
377 PrintPICBaseSymbol();
378 break;
379 case X86II::MO_TLSGD: O << "@TLSGD"; break;
380 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
381 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
382 case X86II::MO_TPOFF: O << "@TPOFF"; break;
383 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
384 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
385 case X86II::MO_GOT: O << "@GOT"; break;
386 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000387 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 }
389}
390
Chris Lattnerae8f9592009-07-13 21:53:19 +0000391/// print_pcrel_imm - This is used to print an immediate value that ends up
392/// being encoded as a pc-relative value. These print slightly differently, for
393/// example, a $ is not emitted.
394void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
395 const MachineOperand &MO = MI->getOperand(OpNo);
396 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000397 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000398 case MachineOperand::MO_Immediate:
399 O << MO.getImm();
400 return;
401 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000402 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000403 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000404 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000405 case MachineOperand::MO_ExternalSymbol:
406 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000407 return;
408 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000409}
410
411
Chris Lattner207a0ca2009-07-13 21:41:08 +0000412
413void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
414 const char *Modifier) {
415 const MachineOperand &MO = MI->getOperand(OpNo);
416 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000417 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000418 case MachineOperand::MO_Register: {
419 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
420 "Virtual registers should not make it this far!");
421 O << '%';
422 unsigned Reg = MO.getReg();
423 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000424 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000425 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
426 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner207a0ca2009-07-13 21:41:08 +0000427 Reg = getX86SubSuperRegister(Reg, VT);
428 }
429 O << TRI->getAsmName(Reg);
430 return;
431 }
432
433 case MachineOperand::MO_Immediate:
434 O << '$' << MO.getImm();
435 return;
436
437 case MachineOperand::MO_JumpTableIndex:
438 case MachineOperand::MO_ConstantPoolIndex:
439 case MachineOperand::MO_GlobalAddress:
440 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000441 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000442 printSymbolOperand(MO);
443 break;
444 }
445 }
446}
447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000449 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 assert(value <= 7 && "Invalid ssecc argument!");
451 switch (value) {
452 case 0: O << "eq"; break;
453 case 1: O << "lt"; break;
454 case 2: O << "le"; break;
455 case 3: O << "unord"; break;
456 case 4: O << "neq"; break;
457 case 5: O << "nlt"; break;
458 case 6: O << "nle"; break;
459 case 7: O << "ord"; break;
460 }
461}
462
Rafael Espindolabca99f72009-04-08 21:14:34 +0000463void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000464 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000465 const MachineOperand &BaseReg = MI->getOperand(Op);
466 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 const MachineOperand &DispSpec = MI->getOperand(Op+3);
468
Chris Lattner791fd482009-07-09 00:27:29 +0000469 // If we really don't want to print out (rip), don't.
470 bool HasBaseReg = BaseReg.getReg() != 0;
471 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
472 BaseReg.getReg() == X86::RIP)
473 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000474
475 // HasParenPart - True if we will print out the () part of the mem ref.
476 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
477
478 if (DispSpec.isImm()) {
479 int DispVal = DispSpec.getImm();
480 if (DispVal || !HasParenPart)
481 O << DispVal;
482 } else {
483 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
484 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000485 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000486 }
487
488 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000489 assert(IndexReg.getReg() != X86::ESP &&
490 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000491
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000492 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000493 if (HasBaseReg)
494 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495
496 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000497 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000498 printOperand(MI, Op+2, Modifier);
499 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000501 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000503 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 }
505}
506
Rafael Espindolabca99f72009-04-08 21:14:34 +0000507void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000508 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000509 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000510 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000511 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000512 printOperand(MI, Op+4, Modifier);
513 O << ':';
514 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000515 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000516}
517
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000518void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000519 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000520 if (!MAI->getSetDirective())
Evan Cheng6fb06762007-11-09 01:32:10 +0000521 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000522
523 // We don't need .set machinery if we have GOT-style relocations
524 if (Subtarget->isPICStyleGOT())
525 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000526
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000527 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng6fb06762007-11-09 01:32:10 +0000528 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000529 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000530 if (Subtarget->isPICStyleRIPRel())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000531 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000532 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000533 else {
534 O << '-';
535 PrintPICBaseSymbol();
536 O << '\n';
537 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000538}
539
Chris Lattner14f791a2009-06-24 19:19:16 +0000540
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000542 PrintPICBaseSymbol();
543 O << '\n';
544 PrintPICBaseSymbol();
545 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546}
547
548
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000549void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
550 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000551 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000552 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000553 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000554
555 O << JTEntryDirective << ' ';
556
Chris Lattner2e9393c2009-07-10 21:00:45 +0000557 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000558 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner4a948932009-07-10 20:47:30 +0000559 << '_' << uid << "_set_" << MBB->getNumber();
560 } else if (Subtarget->isPICStyleGOT()) {
561 printBasicBlockLabel(MBB, false, false, false);
562 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000563 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000564 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000565}
566
Chris Lattner8bb96e42009-06-15 04:42:32 +0000567bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 unsigned Reg = MO.getReg();
569 switch (Mode) {
570 default: return true; // Unknown mode.
571 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000572 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 break;
574 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000575 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 break;
577 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000578 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 break;
580 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000581 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000583 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000584 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000585 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 }
587
Evan Cheng00d04a72008-07-07 22:21:06 +0000588 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 return false;
590}
591
592/// PrintAsmOperand - Print out an operand for an inline asm expression.
593///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000594bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 const char *ExtraCode) {
597 // Does this asm operand have a single letter operand modifier?
598 if (ExtraCode && ExtraCode[0]) {
599 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600
Chris Lattnerb6047e62009-07-13 21:48:33 +0000601 const MachineOperand &MO = MI->getOperand(OpNo);
602
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 switch (ExtraCode[0]) {
604 default: return true; // Unknown modifier.
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000605 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
606 if (MO.isImm()) {
607 O << MO.getImm();
608 return false;
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000609 }
Dale Johannesenf1f04ec2009-08-25 00:16:14 +0000610 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
611 printSymbolOperand(MO);
612 return false;
613 }
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000614 if (MO.isReg()) {
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000615 O << '(';
616 printOperand(MI, OpNo);
617 O << ')';
618 return false;
619 }
620 return true;
621
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000623 if (MO.isImm())
624 O << MO.getImm();
625 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
626 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000627 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000628 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000630
631 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000632 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000633 O << '*';
634 printOperand(MI, OpNo);
635 return false;
636 }
637 return true;
638
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 case 'b': // Print QImode register
640 case 'h': // Print QImode high register
641 case 'w': // Print HImode register
642 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000643 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000644 if (MO.isReg())
645 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 printOperand(MI, OpNo);
647 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000648
Dale Johannesen375b88b2009-07-07 23:28:22 +0000649 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000650 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000652
Chris Lattnerb6047e62009-07-13 21:48:33 +0000653 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000654 // Note: this is a temporary solution. It should be handled target
655 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000656 if (MO.isImm()) {
657 O << -MO.getImm();
658 return false;
659 }
660 O << '-';
661 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 printOperand(MI, OpNo);
665 return false;
666}
667
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000668bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000670 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000672 if (ExtraCode && ExtraCode[0]) {
673 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000674
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000675 switch (ExtraCode[0]) {
676 default: return true; // Unknown modifier.
677 case 'b': // Print QImode register
678 case 'h': // Print QImode high register
679 case 'w': // Print HImode register
680 case 'k': // Print SImode register
681 case 'q': // Print SImode register
682 // These only apply to registers, ignore on mem.
683 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000684 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000685 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000686 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000687 }
688 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 printMemReference(MI, OpNo);
690 return false;
691}
692
Daniel Dunbar34a26db2009-08-31 19:14:05 +0000693
Chris Lattnerf39997f2009-08-16 04:28:14 +0000694
Bill Wendlingb3b11262009-02-06 21:45:08 +0000695/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
696/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697///
698void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
699 ++EmittedInsts;
700
Chris Lattnerb16f72e2009-09-02 17:35:12 +0000701 // Call the autogenerated instruction printer routines.
702 if (NewAsmPrinter)
703 printInstructionThroughMCStreamer(MI);
704 else
Chris Lattnerf39997f2009-08-16 04:28:14 +0000705 printInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706}
707
Chris Lattnerae982212009-07-21 18:38:57 +0000708void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000709 const TargetData *TD = TM.getTargetData();
710
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000711 if (!GVar->hasInitializer())
712 return; // External global require no code
713
714 // Check to see if this is a special global used by LLVM, if so, emit it.
715 if (EmitSpecialLLVMGlobal(GVar)) {
716 if (Subtarget->isTargetDarwin() &&
717 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000718 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000719 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000720 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000721 O << ".reference .destructors_used\n";
722 }
723 return;
724 }
725
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000726 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000727 Constant *C = GVar->getInitializer();
728 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000729 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000730 unsigned Align = TD->getPreferredAlignmentLog(GVar);
731
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000732 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000733
734 if (Subtarget->isTargetELF())
735 O << "\t.type\t" << name << ",@object\n";
736
Chris Lattner7215c7f2009-08-05 05:21:07 +0000737
738 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000739 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000740 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000741 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000742
Chris Lattnerdb727932009-08-04 05:35:56 +0000743 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000744 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000745 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000746 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000747 if (GVar->hasExternalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000748 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000749 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000750 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000751 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000752 return;
753 }
754 }
755
756 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000757 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000758 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000759
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000760 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000761 if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000762 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000763 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000764 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000765 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000766 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000767 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000768 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000769 O << name << ":";
770 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000771 O.PadToColumn(MAI->getCommentColumn());
772 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000773 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000774 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000775 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000776 EmitGlobalConstant(C);
777 return;
778 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000779 O << MAI->getCOMMDirective() << name << ',' << Size;
780 if (MAI->getCOMMDirectiveTakesAlignment())
781 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000782 }
783 } else {
784 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000785 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000786 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000787 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000788 O << MAI->getCOMMDirective() << name << ',' << Size;
789 if (MAI->getCOMMDirectiveTakesAlignment())
790 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000791 }
Evan Cheng11db8142009-03-24 00:17:40 +0000792 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000793 O.PadToColumn(MAI->getCommentColumn());
794 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000795 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000796 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 return;
799 }
800 }
801
802 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000803 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000804 case GlobalValue::LinkOnceAnyLinkage:
805 case GlobalValue::LinkOnceODRLinkage:
806 case GlobalValue::WeakAnyLinkage:
807 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +0000808 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000810 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000811 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000816 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 }
818 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000819 case GlobalValue::DLLExportLinkage:
820 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 // FIXME: appending linkage variables should go into a section of
822 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000823 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000827 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000828 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000830 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000831 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 }
833
834 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000835 O << name << ":";
836 if (VerboseAsm){
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000837 O.PadToColumn(MAI->getCommentColumn());
838 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000839 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000840 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000841 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000842
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 EmitGlobalConstant(C);
Dan Gohman2aa282f2009-08-13 01:36:44 +0000844
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000845 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman2aa282f2009-08-13 01:36:44 +0000846 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000847}
848
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000850 // Print out module-level global variables here.
851 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000852 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000853 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000854 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000855 }
856
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000857 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +0000858 // All darwin targets use mach-o.
859 TargetLoweringObjectFileMachO &TLOFMacho =
860 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +0000861
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000862 // Add the (possibly multiple) personalities to the set of global value
863 // stubs. Only referenced functions get into the Personalities list.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000864 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000865 const std::vector<Function*> &Personalities = MMI->getPersonalities();
866 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000867 if (Personalities[i])
868 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
869 true /*private label*/)] =
870 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000871 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000872 }
873
Chris Lattner2e860262009-06-24 18:24:09 +0000874 // Output stubs for dynamically-linked functions
875 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000876 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000877 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
878 MCSectionMachO::S_SYMBOL_STUBS |
879 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
880 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
881 5, SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +0000882 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000883 for (StringMap<std::string>::iterator I = FnStubs.begin(),
884 E = FnStubs.end(); I != E; ++I)
885 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
886 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000887 O << '\n';
888 }
889
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000890 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000891 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000892 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000893 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
894 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +0000895 SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +0000896 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000897 for (StringMap<std::string>::iterator I = GVStubs.begin(),
898 E = GVStubs.end(); I != E; ++I)
899 O << I->getKeyData() << ":\n\t.indirect_symbol "
900 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000901 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000902
Evan Chenga65854f2008-12-05 01:06:39 +0000903 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000904 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000905 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000906 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
907 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000908 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000909 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000910 }
911
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000912 // Funny Darwin hack: This flag tells the linker that no global symbols
913 // contain code that falls through to other global symbols (e.g. the obvious
914 // implementation of multiple entry points). If this doesn't occur, the
915 // linker can safely perform dead code stripping. Since LLVM never
916 // generates code that does this, it is always safe to set.
917 O << "\t.subsections_via_symbols\n";
918 } else if (Subtarget->isTargetCygMing()) {
919 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000920 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000921 i != e; ++i) {
922 O << "\t.def\t " << i->getKeyData()
923 << ";\t.scl\t" << COFF::C_EXT
924 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
925 << ";\t.endef\n";
926 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000927 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000928
Chris Lattnerdb191f02009-06-24 18:52:01 +0000929
930 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +0000931 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
932 // dllexport symbols only exist on coff targets.
933 TargetLoweringObjectFileCOFF &TLOFMacho =
934 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
935
Chris Lattner73266f92009-08-19 05:49:37 +0000936 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
937 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +0000938
939 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
940 e = DLLExportedGVs.end(); i != e; ++i)
941 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +0000942
943 for (StringSet<>::iterator i = DLLExportedFns.begin(),
944 e = DLLExportedFns.end();
945 i != e; ++i)
946 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
947 }
948
Chris Lattnerdb191f02009-06-24 18:52:01 +0000949 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000950 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000951}
952
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953// Include the auto-generated portion of the assembly writer.
954#include "X86GenAsmWriter.inc"