blob: 00ae058b61e7eef1e378e5dedb01becd820d59c3 [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000022#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000025#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000026#include "llvm/Assembly/Writer.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000027#include "llvm/MC/MCContext.h"
Chris Lattner522e9a02009-09-02 17:35:12 +000028#include "llvm/MC/MCSectionMachO.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000029#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner475370b2009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000037#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnercf1ed752009-09-11 04:28:13 +000039#include "llvm/ADT/SmallString.h"
40#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
Chris Lattner475370b2009-06-19 00:47:33 +000045static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
Chris Lattner33c6aa62009-09-09 06:11:14 +000046 cl::Hidden, cl::init(false));
Chris Lattner475370b2009-06-19 00:47:33 +000047
Chris Lattnerab162992009-06-24 19:44:36 +000048//===----------------------------------------------------------------------===//
49// Primitive Helper Functions.
50//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000051
52void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner9b60e042009-08-16 04:28:14 +000053 // FIXME: the actual label generated doesn't matter here! Just mangle in
54 // something unique (the function number) with Private prefix.
Evan Cheng071b9d52007-01-18 01:49:58 +000055 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000056 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattner9b60e042009-08-16 04:28:14 +000057 else {
58 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner8529d282009-07-08 23:09:14 +000059 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattner9b60e042009-08-16 04:28:14 +000060 }
61}
62
Anton Korobeynikov75b68822008-06-28 11:08:27 +000063static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
64 const TargetData *TD) {
65 X86MachineFunctionInfo Info;
66 uint64_t Size = 0;
67
68 switch (F->getCallingConv()) {
69 case CallingConv::X86_StdCall:
70 Info.setDecorationStyle(StdCall);
71 break;
72 case CallingConv::X86_FastCall:
73 Info.setDecorationStyle(FastCall);
74 break;
75 default:
76 return Info;
77 }
78
79 unsigned argNum = 1;
80 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
81 AI != AE; ++AI, ++argNum) {
82 const Type* Ty = AI->getType();
83
84 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000085 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000086 Ty = cast<PointerType>(Ty)->getElementType();
87
88 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000089 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000090 }
91
92 // We're not supporting tooooo huge arguments :)
93 Info.setBytesToPopOnReturn((unsigned int)Size);
94 return Info;
95}
96
Chris Lattnerc08872e2009-07-15 04:55:56 +000097/// DecorateCygMingName - Query FunctionInfoMap and use this information for
98/// various name decorations for Cygwin and MingW.
Chris Lattnercf1ed752009-09-11 04:28:13 +000099void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
Chris Lattnerc08872e2009-07-15 04:55:56 +0000100 const GlobalValue *GV) {
101 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
102
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000103 const Function *F = dyn_cast<Function>(GV);
104 if (!F) return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000105
Chris Lattner7a4e4642009-07-10 21:57:21 +0000106 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000107 if (F->isDeclaration())
Chris Lattnercf1ed752009-09-11 04:28:13 +0000108 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
Chris Lattner7a4e4642009-07-10 21:57:21 +0000109
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000110 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000111 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000112 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
113 return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000114
115
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000116 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000117
118 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000119 if (info_item == FunctionInfoMap.end()) {
120 // Calculate apropriate function info and populate map
121 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
122 Info = &FunctionInfoMap[F];
123 } else {
124 Info = &info_item->second;
125 }
Chris Lattnercf1ed752009-09-11 04:28:13 +0000126
127 if (Info->getDecorationStyle() == None) return;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000128 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000129
Chris Lattnercf1ed752009-09-11 04:28:13 +0000130 // "Pure" variadic functions do not receive @0 suffix.
131 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
132 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
133 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
134
135 if (Info->getDecorationStyle() == FastCall) {
136 if (Name[0] == '_')
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000137 Name[0] = '@';
Chris Lattnercf1ed752009-09-11 04:28:13 +0000138 else
139 Name.insert(Name.begin(), '@');
140 }
141}
142
143/// DecorateCygMingName - Query FunctionInfoMap and use this information for
144/// various name decorations for Cygwin and MingW.
145void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
146 const GlobalValue *GV) {
147 SmallString<128> NameStr(Name.begin(), Name.end());
148 DecorateCygMingName(NameStr, GV);
149 Name.assign(NameStr.begin(), NameStr.end());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000150}
151
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000152void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000153 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000155
Chris Lattnerc08872e2009-07-15 04:55:56 +0000156 if (Subtarget->isTargetCygMing())
157 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000158
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000159 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000160 EmitAlignment(FnAlign, F);
161
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000165 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000171 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000172 case Function::LinkOnceAnyLinkage:
173 case Function::LinkOnceODRLinkage:
174 case Function::WeakAnyLinkage:
175 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000178 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 }
185 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
Dan Gohmancf20ac42009-08-13 01:36:44 +0000200 O << CurrentFnName << ':';
201 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000202 O.PadToColumn(MAI->getCommentColumn());
203 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000204 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
205 }
206 O << '\n';
207
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000208 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000219 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000220 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000221
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000222 SetupMachineFunction(MF);
223 O << "\n\n";
224
225 // Populate function information map. Actually, We don't want to populate
226 // non-stdcall or non-fastcall functions' information right now.
227 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
228 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
229
230 // Print out constants referenced by the function
231 EmitConstantPool(MF.getConstantPool());
232
233 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000234 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000235
236 // Print the 'header' of function
237 emitFunctionHeader(MF);
238
239 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000240 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000248 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
249 // This is an entry block or a block that's only reachable via a
250 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
251 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000253 O << '\n';
254 }
Bill Wendling824a7212008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 printMachineInstruction(II);
261 }
262 }
Evan Cheng67afece2006-08-28 22:14:16 +0000263
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
265 // If the function is empty, then we need to emit *something*. Otherwise,
266 // the function's label might be associated with something that it wasn't
267 // meant to be associated with. We emit a noop in this situation.
268 // We are assuming inline asms are code.
269 O << "\tnop\n";
270 }
271
Chris Lattner33adcfb2009-08-22 21:43:10 +0000272 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000276 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000278
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000281
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 // We didn't modify anything.
283 return false;
284}
285
Chris Lattnerf666c092009-07-13 21:53:19 +0000286/// printSymbolOperand - Print a raw symbol reference operand. This handles
287/// jump tables, constant pools, global address and external symbols, all of
288/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000289void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
290 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000291 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000292 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000293 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000294 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000295 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000296 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000297 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000299 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000300 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000302 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000303
304 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000305 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
306 Suffix = "$stub";
307 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
308 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000309 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000310 Suffix = "$non_lazy_ptr";
311
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000312 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000313 if (Subtarget->isTargetCygMing())
314 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000315
Chris Lattner35c89612009-07-09 05:42:07 +0000316 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000317 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
318 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000319
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000320 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
321 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
322 GVStubs[Name] = Mang->getMangledName(GV);
Evan Cheng63476a82009-09-03 07:04:02 +0000323 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000324 HiddenGVStubs[Name] = Mang->getMangledName(GV);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000325 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
326 FnStubs.insert(OutContext.GetOrCreateSymbol(Name));
327 }
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000328
329 // If the name begins with a dollar-sign, enclose it in parens. We do this
330 // to avoid having it look like an integer immediate to the assembler.
331 if (Name[0] == '$')
332 O << '(' << Name << ')';
333 else
334 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000335
Chris Lattner35c89612009-07-09 05:42:07 +0000336 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000337 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000338 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000339 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000340 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000341 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000342 Name += "$stub";
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000343 FnStubs.insert(OutContext.GetOrCreateSymbol(Name));
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000344 }
345
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000346 // If the name begins with a dollar-sign, enclose it in parens. We do this
347 // to avoid having it look like an integer immediate to the assembler.
348 if (Name[0] == '$')
349 O << '(' << Name << ')';
350 else
351 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000352 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000353 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000354 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000355
356 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000357 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000358 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000359 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000360 break;
361 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000362 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000363 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000364 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000365 break;
366 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
367 O << " + [.-";
368 PrintPICBaseSymbol();
369 O << ']';
370 break;
371 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000372 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
373 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000374 O << '-';
375 PrintPICBaseSymbol();
376 break;
377 case X86II::MO_TLSGD: O << "@TLSGD"; break;
378 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
379 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
380 case X86II::MO_TPOFF: O << "@TPOFF"; break;
381 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
382 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
383 case X86II::MO_GOT: O << "@GOT"; break;
384 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000385 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000386 }
387}
388
Chris Lattnerf666c092009-07-13 21:53:19 +0000389/// print_pcrel_imm - This is used to print an immediate value that ends up
390/// being encoded as a pc-relative value. These print slightly differently, for
391/// example, a $ is not emitted.
392void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
393 const MachineOperand &MO = MI->getOperand(OpNo);
394 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000395 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000396 case MachineOperand::MO_Immediate:
397 O << MO.getImm();
398 return;
399 case MachineOperand::MO_MachineBasicBlock:
Dan Gohmancf20ac42009-08-13 01:36:44 +0000400 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerf666c092009-07-13 21:53:19 +0000401 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000402 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000403 case MachineOperand::MO_ExternalSymbol:
404 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000405 return;
406 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000407}
408
409
Chris Lattner174f8162009-07-13 21:41:08 +0000410
411void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
412 const char *Modifier) {
413 const MachineOperand &MO = MI->getOperand(OpNo);
414 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000415 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000416 case MachineOperand::MO_Register: {
417 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
418 "Virtual registers should not make it this far!");
419 O << '%';
420 unsigned Reg = MO.getReg();
421 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000422 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
424 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000425 Reg = getX86SubSuperRegister(Reg, VT);
426 }
427 O << TRI->getAsmName(Reg);
428 return;
429 }
430
431 case MachineOperand::MO_Immediate:
432 O << '$' << MO.getImm();
433 return;
434
435 case MachineOperand::MO_JumpTableIndex:
436 case MachineOperand::MO_ConstantPoolIndex:
437 case MachineOperand::MO_GlobalAddress:
438 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000439 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000440 printSymbolOperand(MO);
441 break;
442 }
443 }
444}
445
Nate Begeman391c5d22005-11-30 18:54:35 +0000446void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000447 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000448 assert(value <= 7 && "Invalid ssecc argument!");
449 switch (value) {
450 case 0: O << "eq"; break;
451 case 1: O << "lt"; break;
452 case 2: O << "le"; break;
453 case 3: O << "unord"; break;
454 case 4: O << "neq"; break;
455 case 5: O << "nlt"; break;
456 case 6: O << "nle"; break;
457 case 7: O << "ord"; break;
458 }
459}
460
Rafael Espindola094fad32009-04-08 21:14:34 +0000461void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000462 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000463 const MachineOperand &BaseReg = MI->getOperand(Op);
464 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000465 const MachineOperand &DispSpec = MI->getOperand(Op+3);
466
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000467 // If we really don't want to print out (rip), don't.
468 bool HasBaseReg = BaseReg.getReg() != 0;
469 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
470 BaseReg.getReg() == X86::RIP)
471 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000472
473 // HasParenPart - True if we will print out the () part of the mem ref.
474 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
475
476 if (DispSpec.isImm()) {
477 int DispVal = DispSpec.getImm();
478 if (DispVal || !HasParenPart)
479 O << DispVal;
480 } else {
481 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
482 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000483 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000484 }
485
486 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000487 assert(IndexReg.getReg() != X86::ESP &&
488 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000489
Dan Gohmand19a53b2008-06-30 22:03:41 +0000490 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000491 if (HasBaseReg)
492 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000493
494 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000495 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000496 printOperand(MI, Op+2, Modifier);
497 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000498 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000499 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000500 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000501 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000502 }
503}
504
Rafael Espindola094fad32009-04-08 21:14:34 +0000505void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000506 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000507 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000508 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000509 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000510 printOperand(MI, Op+4, Modifier);
511 O << ':';
512 }
Chris Lattner18c59872009-06-27 04:16:01 +0000513 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000514}
515
Anton Korobeynikov91364382008-06-28 11:08:09 +0000516void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000517 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000518 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000519 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000520
521 // We don't need .set machinery if we have GOT-style relocations
522 if (Subtarget->isPICStyleGOT())
523 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000524
Chris Lattner33adcfb2009-08-22 21:43:10 +0000525 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000526 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000527 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000528 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000529 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000530 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000531 else {
532 O << '-';
533 PrintPICBaseSymbol();
534 O << '\n';
535 }
Evan Chengcc415862007-11-09 01:32:10 +0000536}
537
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000538
Evan Cheng7ccced62006-02-18 00:15:05 +0000539void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000540 PrintPICBaseSymbol();
541 O << '\n';
542 PrintPICBaseSymbol();
543 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000544}
545
Evan Cheng62f27002006-04-28 23:11:40 +0000546
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000547void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
548 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000549 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000550 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000551 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000552
553 O << JTEntryDirective << ' ';
554
Chris Lattnere2c92082009-07-10 21:00:45 +0000555 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000556 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000557 << '_' << uid << "_set_" << MBB->getNumber();
558 } else if (Subtarget->isPICStyleGOT()) {
559 printBasicBlockLabel(MBB, false, false, false);
560 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000561 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000562 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000563}
564
Chris Lattner37710712009-06-15 04:42:32 +0000565bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000566 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000567 switch (Mode) {
568 default: return true; // Unknown mode.
569 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000571 break;
572 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000574 break;
575 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000577 break;
578 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000580 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000581 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000583 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000584 }
585
Evan Chengae270f62008-07-07 22:21:06 +0000586 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000587 return false;
588}
589
Evan Cheng3d48a902006-04-28 21:19:05 +0000590/// PrintAsmOperand - Print out an operand for an inline asm expression.
591///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000592bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000593 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000594 const char *ExtraCode) {
595 // Does this asm operand have a single letter operand modifier?
596 if (ExtraCode && ExtraCode[0]) {
597 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000598
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000599 const MachineOperand &MO = MI->getOperand(OpNo);
600
Evan Cheng3d48a902006-04-28 21:19:05 +0000601 switch (ExtraCode[0]) {
602 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000603 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
604 if (MO.isImm()) {
605 O << MO.getImm();
606 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000607 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000608 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
609 printSymbolOperand(MO);
610 return false;
611 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000612 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000613 O << '(';
614 printOperand(MI, OpNo);
615 O << ')';
616 return false;
617 }
618 return true;
619
Chris Lattnerb4828722007-01-25 02:53:24 +0000620 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000621 if (MO.isImm())
622 O << MO.getImm();
623 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
624 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000625 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000626 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000627 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000628
629 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000630 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000631 O << '*';
632 printOperand(MI, OpNo);
633 return false;
634 }
635 return true;
636
Evan Cheng62f27002006-04-28 23:11:40 +0000637 case 'b': // Print QImode register
638 case 'h': // Print QImode high register
639 case 'w': // Print HImode register
640 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000641 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000642 if (MO.isReg())
643 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000644 printOperand(MI, OpNo);
645 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000646
Dale Johannesen9fcff042009-07-07 23:28:22 +0000647 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000648 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000649 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000650
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000651 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000652 // Note: this is a temporary solution. It should be handled target
653 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000654 if (MO.isImm()) {
655 O << -MO.getImm();
656 return false;
657 }
658 O << '-';
659 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000660 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000661
Evan Cheng3d48a902006-04-28 21:19:05 +0000662 printOperand(MI, OpNo);
663 return false;
664}
665
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000666bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000667 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000668 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000669 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000670 if (ExtraCode && ExtraCode[0]) {
671 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000672
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000673 switch (ExtraCode[0]) {
674 default: return true; // Unknown modifier.
675 case 'b': // Print QImode register
676 case 'h': // Print QImode high register
677 case 'w': // Print HImode register
678 case 'k': // Print SImode register
679 case 'q': // Print SImode register
680 // These only apply to registers, ignore on mem.
681 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000682 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000683 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000684 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000685 }
686 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000687 printMemReference(MI, OpNo);
688 return false;
689}
690
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000691
Chris Lattner9b60e042009-08-16 04:28:14 +0000692
Bill Wendlingac06d002009-02-06 21:45:08 +0000693/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
694/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000695///
696void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
697 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000698
Chris Lattner634cca32009-09-09 20:34:59 +0000699 processDebugLoc(MI->getDebugLoc());
700
Chris Lattner522e9a02009-09-02 17:35:12 +0000701 // Call the autogenerated instruction printer routines.
702 if (NewAsmPrinter)
703 printInstructionThroughMCStreamer(MI);
704 else
Chris Lattner9b60e042009-08-16 04:28:14 +0000705 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000706
707 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
708 EmitComments(*MI);
709 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000710}
711
Chris Lattner40bbebd2009-07-21 18:38:57 +0000712void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000713 const TargetData *TD = TM.getTargetData();
714
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000715 if (!GVar->hasInitializer())
716 return; // External global require no code
717
718 // Check to see if this is a special global used by LLVM, if so, emit it.
719 if (EmitSpecialLLVMGlobal(GVar)) {
720 if (Subtarget->isTargetDarwin() &&
721 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000722 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000723 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000724 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000725 O << ".reference .destructors_used\n";
726 }
727 return;
728 }
729
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000730 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000731 Constant *C = GVar->getInitializer();
732 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000733 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000734 unsigned Align = TD->getPreferredAlignmentLog(GVar);
735
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000736 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000737
738 if (Subtarget->isTargetELF())
739 O << "\t.type\t" << name << ",@object\n";
740
Chris Lattner26d054d2009-08-05 05:21:07 +0000741
742 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000743 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000744 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000745 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000746
Chris Lattner3b24c012009-08-04 05:35:56 +0000747 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000748 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000749 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000750 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000751 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000752 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000753 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000754 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000755 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000756 return;
757 }
758 }
759
760 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000761 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000762 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000763
Chris Lattner33adcfb2009-08-22 21:43:10 +0000764 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000765 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000766 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000767 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000768 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000770 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000771 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000772 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000773 O << name << ":";
774 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000775 O.PadToColumn(MAI->getCommentColumn());
776 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000777 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000778 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000779 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000780 EmitGlobalConstant(C);
781 return;
782 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000783 O << MAI->getCOMMDirective() << name << ',' << Size;
784 if (MAI->getCOMMDirectiveTakesAlignment())
785 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000786 }
787 } else {
788 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000789 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000790 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000791 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000792 O << MAI->getCOMMDirective() << name << ',' << Size;
793 if (MAI->getCOMMDirectiveTakesAlignment())
794 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000795 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000796 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000797 O.PadToColumn(MAI->getCommentColumn());
798 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000799 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000800 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000801 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000802 return;
803 }
804 }
805
806 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000807 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000808 case GlobalValue::LinkOnceAnyLinkage:
809 case GlobalValue::LinkOnceODRLinkage:
810 case GlobalValue::WeakAnyLinkage:
811 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000812 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000814 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000815 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000817 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000818 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000819 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000820 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000821 }
822 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000823 case GlobalValue::DLLExportLinkage:
824 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 // FIXME: appending linkage variables should go into a section of
826 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000827 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000828 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000829 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000830 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000831 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000832 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000834 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000835 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000836 }
837
838 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000839 O << name << ":";
840 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000841 O.PadToColumn(MAI->getCommentColumn());
842 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000843 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000844 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000846
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000847 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000848
Chris Lattner33adcfb2009-08-22 21:43:10 +0000849 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000850 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000851}
852
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000854 // Print out module-level global variables here.
855 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000856 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000857 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000858 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000859 }
860
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000861 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000862 // All darwin targets use mach-o.
863 TargetLoweringObjectFileMachO &TLOFMacho =
864 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000865
Chris Lattner381d4fe2009-06-24 18:17:00 +0000866 // Add the (possibly multiple) personalities to the set of global value
867 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000868 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000869 const std::vector<Function*> &Personalities = MMI->getPersonalities();
870 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000871 if (Personalities[i])
872 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
873 true /*private label*/)] =
874 Mang->getMangledName(Personalities[i]);
Evan Cheng77c8f762008-07-08 00:55:58 +0000875 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000876 }
877
Chris Lattner8f61f982009-06-24 18:24:09 +0000878 // Output stubs for dynamically-linked functions
879 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000880 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000881 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
882 MCSectionMachO::S_SYMBOL_STUBS |
883 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
884 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
885 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000886 OutStreamer.SwitchSection(TheSection);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000887 // FIXME: This iteration order is unstable!!
888 for (SmallPtrSet<MCSymbol*, 16>::iterator I = FnStubs.begin(),
889 E = FnStubs.end(); I != E; ++I) {
890 MCSymbol *Sym = *I;
891 Sym->print(O, MAI);
892
893 O << ":\n" << "\t.indirect_symbol ";
894
895 // Get the MCSymbol without the $stub suffix.
896 Sym = OutContext.GetOrCreateSymbol(StringRef(Sym->getName()).substr(0,
897 Sym->getName().size()-5));
898 Sym->print(O, MAI);
899 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
900 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000901 O << '\n';
902 }
903
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000904 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000905 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000906 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000907 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
908 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000909 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000910 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000911 for (StringMap<std::string>::iterator I = GVStubs.begin(),
912 E = GVStubs.end(); I != E; ++I)
913 O << I->getKeyData() << ":\n\t.indirect_symbol "
914 << I->second << "\n\t.long\t0\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000915 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000916
Evan Chengae94e592008-12-05 01:06:39 +0000917 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000918 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000919 EmitAlignment(2);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000920 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
921 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000922 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000923 << I->second << '\n';
Evan Chengae94e592008-12-05 01:06:39 +0000924 }
925
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000926 // Funny Darwin hack: This flag tells the linker that no global symbols
927 // contain code that falls through to other global symbols (e.g. the obvious
928 // implementation of multiple entry points). If this doesn't occur, the
929 // linker can safely perform dead code stripping. Since LLVM never
930 // generates code that does this, it is always safe to set.
931 O << "\t.subsections_via_symbols\n";
932 } else if (Subtarget->isTargetCygMing()) {
933 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000934 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000935 i != e; ++i) {
936 O << "\t.def\t " << i->getKeyData()
937 << ";\t.scl\t" << COFF::C_EXT
938 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
939 << ";\t.endef\n";
940 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000941 }
Chris Lattner974469d2009-06-24 05:47:59 +0000942
Chris Lattner0a7befa2009-06-24 18:52:01 +0000943
944 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +0000945 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
946 // dllexport symbols only exist on coff targets.
947 TargetLoweringObjectFileCOFF &TLOFMacho =
948 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
949
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000950 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
951 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +0000952
953 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
954 e = DLLExportedGVs.end(); i != e; ++i)
955 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +0000956
957 for (StringSet<>::iterator i = DLLExportedFns.begin(),
958 e = DLLExportedFns.end();
959 i != e; ++i)
960 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
961 }
962
Chris Lattner0a7befa2009-06-24 18:52:01 +0000963 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +0000964 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000965}
966
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000967// Include the auto-generated portion of the assembly writer.
968#include "X86GenAsmWriter.inc"