blob: ad91fad061a8af0c7af2a051d3cad7b0160d7b7a [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 ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000309 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000310 Suffix = "$non_lazy_ptr";
311
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000312 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000313 if (Subtarget->isTargetCygMing())
314 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000315
Chris Lattner0cc2b792009-07-09 05:42:07 +0000316 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000317 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
318 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000319
Chris Lattnerb3cdde62009-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 Chengba2cf3d2009-09-03 07:04:02 +0000323 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-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 Lattnerdabfe572009-06-21 02:22:53 +0000334
Chris Lattner0cc2b792009-07-09 05:42:07 +0000335 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000336 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000338 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000339 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000340 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000341 FnStubs[Name+"$stub"] = Name;
342 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000343 }
344
Chris Lattnerb3cdde62009-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 Lattner797a0782009-06-27 05:46:24 +0000351 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000353 }
Chris Lattner797a0782009-06-27 05:46:24 +0000354
355 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000357 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000358 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000359 break;
360 case X86II::MO_DARWIN_NONLAZY:
Chris Lattnera3bde622009-07-09 06:59:17 +0000361 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000362 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000363 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-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 Lattnera3bde622009-07-09 06:59:17 +0000371 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
372 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-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 Lattnere1cf8a12009-07-13 22:07:30 +0000384 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 }
386}
387
Chris Lattnerae8f9592009-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()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000394 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000395 case MachineOperand::MO_Immediate:
396 O << MO.getImm();
397 return;
398 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000399 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000400 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000401 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000402 case MachineOperand::MO_ExternalSymbol:
403 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000404 return;
405 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000406}
407
408
Chris Lattner207a0ca2009-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()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000414 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-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 Andersonac9de032009-08-10 22:56:29 +0000421 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-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 Lattner207a0ca2009-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 Lattnerb6047e62009-07-13 21:48:33 +0000438 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000439 printSymbolOperand(MO);
440 break;
441 }
442 }
443}
444
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000446 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Espindolabca99f72009-04-08 21:14:34 +0000460void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000461 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000462 const MachineOperand &BaseReg = MI->getOperand(Op);
463 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 const MachineOperand &DispSpec = MI->getOperand(Op+3);
465
Chris Lattner791fd482009-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 Lattnerec112ef2009-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 Lattnerb6047e62009-07-13 21:48:33 +0000482 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000483 }
484
485 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000486 assert(IndexReg.getReg() != X86::ESP &&
487 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000488
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000489 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000490 if (HasBaseReg)
491 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492
493 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000494 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000495 printOperand(MI, Op+2, Modifier);
496 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000498 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000500 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 }
502}
503
Rafael Espindolabca99f72009-04-08 21:14:34 +0000504void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000505 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000506 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000507 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000508 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000509 printOperand(MI, Op+4, Modifier);
510 O << ':';
511 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000512 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000513}
514
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000515void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000516 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000517 if (!MAI->getSetDirective())
Evan Cheng6fb06762007-11-09 01:32:10 +0000518 return;
Anton Korobeynikov5772c672007-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 Korobeynikovd97b85e2008-06-28 11:08:09 +0000523
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000524 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng6fb06762007-11-09 01:32:10 +0000525 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000526 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000527 if (Subtarget->isPICStyleRIPRel())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000528 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000529 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000530 else {
531 O << '-';
532 PrintPICBaseSymbol();
533 O << '\n';
534 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000535}
536
Chris Lattner14f791a2009-06-24 19:19:16 +0000537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000539 PrintPICBaseSymbol();
540 O << '\n';
541 PrintPICBaseSymbol();
542 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543}
544
545
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000546void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
547 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000548 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000549 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000550 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000551
552 O << JTEntryDirective << ' ';
553
Chris Lattner2e9393c2009-07-10 21:00:45 +0000554 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000555 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner4a948932009-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 Korobeynikov5772c672007-11-14 09:18:41 +0000560 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000561 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000562}
563
Chris Lattner8bb96e42009-06-15 04:42:32 +0000564bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 unsigned Reg = MO.getReg();
566 switch (Mode) {
567 default: return true; // Unknown mode.
568 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000569 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 break;
571 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000572 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 break;
574 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000575 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 break;
577 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000578 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000580 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000581 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000582 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 }
584
Evan Cheng00d04a72008-07-07 22:21:06 +0000585 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 return false;
587}
588
589/// PrintAsmOperand - Print out an operand for an inline asm expression.
590///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000591bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000592 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikovd97b85e2008-06-28 11:08:09 +0000597
Chris Lattnerb6047e62009-07-13 21:48:33 +0000598 const MachineOperand &MO = MI->getOperand(OpNo);
599
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 switch (ExtraCode[0]) {
601 default: return true; // Unknown modifier.
Dale Johannesenfabae3e2009-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 Johannesen82aca6d2009-08-19 23:44:01 +0000606 }
Dale Johannesenf1f04ec2009-08-25 00:16:14 +0000607 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
608 printSymbolOperand(MO);
609 return false;
610 }
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000611 if (MO.isReg()) {
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000612 O << '(';
613 printOperand(MI, OpNo);
614 O << ')';
615 return false;
616 }
617 return true;
618
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-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 Lattner207a0ca2009-07-13 21:41:08 +0000624 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000625 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000627
628 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000629 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000630 O << '*';
631 printOperand(MI, OpNo);
632 return false;
633 }
634 return true;
635
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner1fabfaa2007-10-29 03:09:07 +0000640 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000641 if (MO.isReg())
642 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 printOperand(MI, OpNo);
644 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000645
Dale Johannesen375b88b2009-07-07 23:28:22 +0000646 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000647 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000649
Chris Lattnerb6047e62009-07-13 21:48:33 +0000650 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-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 Cheng9eba17b2009-06-26 22:00:19 +0000653 if (MO.isImm()) {
654 O << -MO.getImm();
655 return false;
656 }
657 O << '-';
658 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000660
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 printOperand(MI, OpNo);
662 return false;
663}
664
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000665bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000667 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000669 if (ExtraCode && ExtraCode[0]) {
670 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000671
Chris Lattner1fabfaa2007-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 Lattnerd1595722009-01-23 22:33:40 +0000681 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000682 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000683 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000684 }
685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 printMemReference(MI, OpNo);
687 return false;
688}
689
Daniel Dunbar34a26db2009-08-31 19:14:05 +0000690
Chris Lattnerf39997f2009-08-16 04:28:14 +0000691
Bill Wendlingb3b11262009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694///
695void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
696 ++EmittedInsts;
697
Chris Lattnerb16f72e2009-09-02 17:35:12 +0000698 // Call the autogenerated instruction printer routines.
699 if (NewAsmPrinter)
700 printInstructionThroughMCStreamer(MI);
701 else
Chris Lattnerf39997f2009-08-16 04:28:14 +0000702 printInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703}
704
Chris Lattnerae982212009-07-21 18:38:57 +0000705void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000706 const TargetData *TD = TM.getTargetData();
707
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000708 if (!GVar->hasInitializer())
709 return; // External global require no code
710
711 // Check to see if this is a special global used by LLVM, if so, emit it.
712 if (EmitSpecialLLVMGlobal(GVar)) {
713 if (Subtarget->isTargetDarwin() &&
714 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000715 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000716 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000717 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000718 O << ".reference .destructors_used\n";
719 }
720 return;
721 }
722
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000723 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000724 Constant *C = GVar->getInitializer();
725 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000726 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000727 unsigned Align = TD->getPreferredAlignmentLog(GVar);
728
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000729 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000730
731 if (Subtarget->isTargetELF())
732 O << "\t.type\t" << name << ",@object\n";
733
Chris Lattner7215c7f2009-08-05 05:21:07 +0000734
735 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000736 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000737 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000738 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000739
Chris Lattnerdb727932009-08-04 05:35:56 +0000740 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000741 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000742 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000743 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000744 if (GVar->hasExternalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000745 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000746 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000747 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000748 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000749 return;
750 }
751 }
752
753 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000754 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000755 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000756
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000757 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000758 if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000759 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000760 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000761 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000762 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000763 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000764 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000765 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000766 O << name << ":";
767 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000768 O.PadToColumn(MAI->getCommentColumn());
769 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000770 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000771 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000772 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000773 EmitGlobalConstant(C);
774 return;
775 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000776 O << MAI->getCOMMDirective() << name << ',' << Size;
777 if (MAI->getCOMMDirectiveTakesAlignment())
778 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000779 }
780 } else {
781 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000782 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000783 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000784 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000785 O << MAI->getCOMMDirective() << name << ',' << Size;
786 if (MAI->getCOMMDirectiveTakesAlignment())
787 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 }
Evan Cheng11db8142009-03-24 00:17:40 +0000789 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000790 O.PadToColumn(MAI->getCommentColumn());
791 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000792 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000793 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000794 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 return;
796 }
797 }
798
799 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000800 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000801 case GlobalValue::LinkOnceAnyLinkage:
802 case GlobalValue::LinkOnceODRLinkage:
803 case GlobalValue::WeakAnyLinkage:
804 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +0000805 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000808 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000813 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000814 }
815 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000816 case GlobalValue::DLLExportLinkage:
817 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 // FIXME: appending linkage variables should go into a section of
819 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000820 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000824 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000825 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000827 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000828 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 }
830
831 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000832 O << name << ":";
833 if (VerboseAsm){
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000834 O.PadToColumn(MAI->getCommentColumn());
835 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000836 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000837 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 EmitGlobalConstant(C);
Dan Gohman2aa282f2009-08-13 01:36:44 +0000841
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000842 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman2aa282f2009-08-13 01:36:44 +0000843 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000844}
845
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000847 // Print out module-level global variables here.
848 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000849 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000850 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000851 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000852 }
853
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000854 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +0000855 // All darwin targets use mach-o.
856 TargetLoweringObjectFileMachO &TLOFMacho =
857 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +0000858
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000859 // Add the (possibly multiple) personalities to the set of global value
860 // stubs. Only referenced functions get into the Personalities list.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000861 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000862 const std::vector<Function*> &Personalities = MMI->getPersonalities();
863 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000864 if (Personalities[i])
865 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
866 true /*private label*/)] =
867 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000868 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000869 }
870
Chris Lattner2e860262009-06-24 18:24:09 +0000871 // Output stubs for dynamically-linked functions
872 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000873 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000874 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
875 MCSectionMachO::S_SYMBOL_STUBS |
876 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
877 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
878 5, SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +0000879 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000880 for (StringMap<std::string>::iterator I = FnStubs.begin(),
881 E = FnStubs.end(); I != E; ++I)
882 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
883 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000884 O << '\n';
885 }
886
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000887 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000888 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +0000889 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +0000890 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
891 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +0000892 SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +0000893 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000894 for (StringMap<std::string>::iterator I = GVStubs.begin(),
895 E = GVStubs.end(); I != E; ++I)
896 O << I->getKeyData() << ":\n\t.indirect_symbol "
897 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000898 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000899
Evan Chenga65854f2008-12-05 01:06:39 +0000900 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000901 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000902 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000903 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
904 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000905 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000906 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000907 }
908
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909 // Funny Darwin hack: This flag tells the linker that no global symbols
910 // contain code that falls through to other global symbols (e.g. the obvious
911 // implementation of multiple entry points). If this doesn't occur, the
912 // linker can safely perform dead code stripping. Since LLVM never
913 // generates code that does this, it is always safe to set.
914 O << "\t.subsections_via_symbols\n";
915 } else if (Subtarget->isTargetCygMing()) {
916 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000917 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000918 i != e; ++i) {
919 O << "\t.def\t " << i->getKeyData()
920 << ";\t.scl\t" << COFF::C_EXT
921 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
922 << ";\t.endef\n";
923 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000924 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000925
Chris Lattnerdb191f02009-06-24 18:52:01 +0000926
927 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +0000928 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
929 // dllexport symbols only exist on coff targets.
930 TargetLoweringObjectFileCOFF &TLOFMacho =
931 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
932
Chris Lattner73266f92009-08-19 05:49:37 +0000933 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
934 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +0000935
936 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
937 e = DLLExportedGVs.end(); i != e; ++i)
938 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +0000939
940 for (StringSet<>::iterator i = DLLExportedFns.begin(),
941 e = DLLExportedFns.end();
942 i != e; ++i)
943 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
944 }
945
Chris Lattnerdb191f02009-06-24 18:52:01 +0000946 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000947 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948}
949
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950// Include the auto-generated portion of the assembly writer.
951#include "X86GenAsmWriter.inc"