blob: 445cc13ec24569323b6c2328a6ecdffbe0bd51b1 [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 Lattner40e3c7a2009-06-24 05:46:28 +000027#include "llvm/MC/MCStreamer.h"
Chris Lattner522e9a02009-09-02 17:35:12 +000028#include "llvm/MC/MCSectionMachO.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000029#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner475370b2009-06-19 00:47:33 +000030#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000035#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattnercf1ed752009-09-11 04:28:13 +000037#include "llvm/ADT/SmallString.h"
38#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000039using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner475370b2009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
Chris Lattner33c6aa62009-09-09 06:11:14 +000044 cl::Hidden, cl::init(false));
Chris Lattner475370b2009-06-19 00:47:33 +000045
Chris Lattnerab162992009-06-24 19:44:36 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000049
50void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner9b60e042009-08-16 04:28:14 +000051 // FIXME: the actual label generated doesn't matter here! Just mangle in
52 // something unique (the function number) with Private prefix.
Evan Cheng071b9d52007-01-18 01:49:58 +000053 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattner9b60e042009-08-16 04:28:14 +000055 else {
56 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner8529d282009-07-08 23:09:14 +000057 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattner9b60e042009-08-16 04:28:14 +000058 }
59}
60
Anton Korobeynikov75b68822008-06-28 11:08:27 +000061static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
62 const TargetData *TD) {
63 X86MachineFunctionInfo Info;
64 uint64_t Size = 0;
65
66 switch (F->getCallingConv()) {
67 case CallingConv::X86_StdCall:
68 Info.setDecorationStyle(StdCall);
69 break;
70 case CallingConv::X86_FastCall:
71 Info.setDecorationStyle(FastCall);
72 break;
73 default:
74 return Info;
75 }
76
77 unsigned argNum = 1;
78 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
79 AI != AE; ++AI, ++argNum) {
80 const Type* Ty = AI->getType();
81
82 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000083 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000084 Ty = cast<PointerType>(Ty)->getElementType();
85
86 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000087 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000088 }
89
90 // We're not supporting tooooo huge arguments :)
91 Info.setBytesToPopOnReturn((unsigned int)Size);
92 return Info;
93}
94
Chris Lattnerc08872e2009-07-15 04:55:56 +000095/// DecorateCygMingName - Query FunctionInfoMap and use this information for
96/// various name decorations for Cygwin and MingW.
Chris Lattnercf1ed752009-09-11 04:28:13 +000097void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
Chris Lattnerc08872e2009-07-15 04:55:56 +000098 const GlobalValue *GV) {
99 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
100
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000101 const Function *F = dyn_cast<Function>(GV);
102 if (!F) return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000103
Chris Lattner7a4e4642009-07-10 21:57:21 +0000104 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000105 if (F->isDeclaration())
Chris Lattnercf1ed752009-09-11 04:28:13 +0000106 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
Chris Lattner7a4e4642009-07-10 21:57:21 +0000107
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000108 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000109 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000110 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
111 return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000112
113
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000114 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000115
116 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000117 if (info_item == FunctionInfoMap.end()) {
118 // Calculate apropriate function info and populate map
119 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
120 Info = &FunctionInfoMap[F];
121 } else {
122 Info = &info_item->second;
123 }
Chris Lattnercf1ed752009-09-11 04:28:13 +0000124
125 if (Info->getDecorationStyle() == None) return;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000126 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000127
Chris Lattnercf1ed752009-09-11 04:28:13 +0000128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
132
133 if (Info->getDecorationStyle() == FastCall) {
134 if (Name[0] == '_')
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000135 Name[0] = '@';
Chris Lattnercf1ed752009-09-11 04:28:13 +0000136 else
137 Name.insert(Name.begin(), '@');
138 }
139}
140
141/// DecorateCygMingName - Query FunctionInfoMap and use this information for
142/// various name decorations for Cygwin and MingW.
143void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
144 const GlobalValue *GV) {
145 SmallString<128> NameStr(Name.begin(), Name.end());
146 DecorateCygMingName(NameStr, GV);
147 Name.assign(NameStr.begin(), NameStr.end());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000148}
149
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000150void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000151 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000152 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000153
Chris Lattnerc08872e2009-07-15 04:55:56 +0000154 if (Subtarget->isTargetCygMing())
155 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000156
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000157 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000158 EmitAlignment(FnAlign, F);
159
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000161 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000163 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000165 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000167 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000169 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000170 case Function::LinkOnceAnyLinkage:
171 case Function::LinkOnceODRLinkage:
172 case Function::WeakAnyLinkage:
173 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000174 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000175 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000176 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000177 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << "\n"
179 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000180 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000181 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 }
183 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000185
186 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000187
188 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000189 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000190 else if (Subtarget->isTargetCygMing()) {
191 O << "\t.def\t " << CurrentFnName
192 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000193 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000194 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
195 << ";\t.endef\n";
196 }
197
Dan Gohmancf20ac42009-08-13 01:36:44 +0000198 O << CurrentFnName << ':';
199 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000200 O.PadToColumn(MAI->getCommentColumn());
201 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000202 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
203 }
204 O << '\n';
205
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000206 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000207 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000208 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000209 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000210}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212/// runOnMachineFunction - This uses the printMachineInstruction()
213/// method to print assembly for each instruction.
214///
215bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
216 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000217 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000218 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000219
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000220 SetupMachineFunction(MF);
221 O << "\n\n";
222
223 // Populate function information map. Actually, We don't want to populate
224 // non-stdcall or non-fastcall functions' information right now.
225 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
226 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
227
228 // Print out constants referenced by the function
229 EmitConstantPool(MF.getConstantPool());
230
231 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000232 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000233
234 // Print the 'header' of function
235 emitFunctionHeader(MF);
236
237 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000238 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000239 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000240
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000241 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000242 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
244 I != E; ++I) {
245 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000246 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
247 // This is an entry block or a block that's only reachable via a
248 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
249 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000250 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000251 O << '\n';
252 }
Bill Wendling824a7212008-01-26 09:03:52 +0000253 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
254 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000255 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000256 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000257 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000258 printMachineInstruction(II);
259 }
260 }
Evan Cheng67afece2006-08-28 22:14:16 +0000261
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000262 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
263 // If the function is empty, then we need to emit *something*. Otherwise,
264 // the function's label might be associated with something that it wasn't
265 // meant to be associated with. We emit a noop in this situation.
266 // We are assuming inline asms are code.
267 O << "\tnop\n";
268 }
269
Chris Lattner33adcfb2009-08-22 21:43:10 +0000270 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000271 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000272
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000273 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000274 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000275 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000276
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000277 // Print out jump tables referenced by the function.
278 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000279
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000280 // We didn't modify anything.
281 return false;
282}
283
Chris Lattnerf666c092009-07-13 21:53:19 +0000284/// printSymbolOperand - Print a raw symbol reference operand. This handles
285/// jump tables, constant pools, global address and external symbols, all of
286/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000287void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
288 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000289 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000290 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000291 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000292 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000293 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000294 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000295 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000296 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000297 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000298 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000299 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000300 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000301
302 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000303 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
304 Suffix = "$stub";
305 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
306 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000307 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000308 Suffix = "$non_lazy_ptr";
309
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000310 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000311 if (Subtarget->isTargetCygMing())
312 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000313
Chris Lattner35c89612009-07-09 05:42:07 +0000314 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000315 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
316 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000317
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000318 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
319 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
320 GVStubs[Name] = Mang->getMangledName(GV);
Evan Cheng63476a82009-09-03 07:04:02 +0000321 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000322 HiddenGVStubs[Name] = Mang->getMangledName(GV);
323 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
324 FnStubs[Name] = Mang->getMangledName(GV);
325
326 // If the name begins with a dollar-sign, enclose it in parens. We do this
327 // to avoid having it look like an integer immediate to the assembler.
328 if (Name[0] == '$')
329 O << '(' << Name << ')';
330 else
331 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000332
Chris Lattner35c89612009-07-09 05:42:07 +0000333 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000334 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000336 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000337 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000338 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000339 FnStubs[Name+"$stub"] = Name;
340 Name += "$stub";
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000341 }
342
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000343 // If the name begins with a dollar-sign, enclose it in parens. We do this
344 // to avoid having it look like an integer immediate to the assembler.
345 if (Name[0] == '$')
346 O << '(' << Name << ')';
347 else
348 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000349 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000350 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000351 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000352
353 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000354 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000355 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000356 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000357 break;
358 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000359 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000360 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000361 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000362 break;
363 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
364 O << " + [.-";
365 PrintPICBaseSymbol();
366 O << ']';
367 break;
368 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000369 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
370 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000371 O << '-';
372 PrintPICBaseSymbol();
373 break;
374 case X86II::MO_TLSGD: O << "@TLSGD"; break;
375 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
376 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
377 case X86II::MO_TPOFF: O << "@TPOFF"; break;
378 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
379 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
380 case X86II::MO_GOT: O << "@GOT"; break;
381 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000382 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000383 }
384}
385
Chris Lattnerf666c092009-07-13 21:53:19 +0000386/// print_pcrel_imm - This is used to print an immediate value that ends up
387/// being encoded as a pc-relative value. These print slightly differently, for
388/// example, a $ is not emitted.
389void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
390 const MachineOperand &MO = MI->getOperand(OpNo);
391 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000392 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000393 case MachineOperand::MO_Immediate:
394 O << MO.getImm();
395 return;
396 case MachineOperand::MO_MachineBasicBlock:
Dan Gohmancf20ac42009-08-13 01:36:44 +0000397 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerf666c092009-07-13 21:53:19 +0000398 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000399 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000400 case MachineOperand::MO_ExternalSymbol:
401 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000402 return;
403 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000404}
405
406
Chris Lattner174f8162009-07-13 21:41:08 +0000407
408void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
409 const char *Modifier) {
410 const MachineOperand &MO = MI->getOperand(OpNo);
411 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000412 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000413 case MachineOperand::MO_Register: {
414 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
415 "Virtual registers should not make it this far!");
416 O << '%';
417 unsigned Reg = MO.getReg();
418 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000419 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
421 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000422 Reg = getX86SubSuperRegister(Reg, VT);
423 }
424 O << TRI->getAsmName(Reg);
425 return;
426 }
427
428 case MachineOperand::MO_Immediate:
429 O << '$' << MO.getImm();
430 return;
431
432 case MachineOperand::MO_JumpTableIndex:
433 case MachineOperand::MO_ConstantPoolIndex:
434 case MachineOperand::MO_GlobalAddress:
435 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000436 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000437 printSymbolOperand(MO);
438 break;
439 }
440 }
441}
442
Nate Begeman391c5d22005-11-30 18:54:35 +0000443void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000444 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000445 assert(value <= 7 && "Invalid ssecc argument!");
446 switch (value) {
447 case 0: O << "eq"; break;
448 case 1: O << "lt"; break;
449 case 2: O << "le"; break;
450 case 3: O << "unord"; break;
451 case 4: O << "neq"; break;
452 case 5: O << "nlt"; break;
453 case 6: O << "nle"; break;
454 case 7: O << "ord"; break;
455 }
456}
457
Rafael Espindola094fad32009-04-08 21:14:34 +0000458void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000459 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000460 const MachineOperand &BaseReg = MI->getOperand(Op);
461 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000462 const MachineOperand &DispSpec = MI->getOperand(Op+3);
463
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000464 // If we really don't want to print out (rip), don't.
465 bool HasBaseReg = BaseReg.getReg() != 0;
466 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
467 BaseReg.getReg() == X86::RIP)
468 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000469
470 // HasParenPart - True if we will print out the () part of the mem ref.
471 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
472
473 if (DispSpec.isImm()) {
474 int DispVal = DispSpec.getImm();
475 if (DispVal || !HasParenPart)
476 O << DispVal;
477 } else {
478 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
479 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000480 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000481 }
482
483 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000484 assert(IndexReg.getReg() != X86::ESP &&
485 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000486
Dan Gohmand19a53b2008-06-30 22:03:41 +0000487 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000488 if (HasBaseReg)
489 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000490
491 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000492 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000493 printOperand(MI, Op+2, Modifier);
494 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000495 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000496 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000497 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000498 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000499 }
500}
501
Rafael Espindola094fad32009-04-08 21:14:34 +0000502void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000503 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000504 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000505 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000506 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000507 printOperand(MI, Op+4, Modifier);
508 O << ':';
509 }
Chris Lattner18c59872009-06-27 04:16:01 +0000510 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000511}
512
Anton Korobeynikov91364382008-06-28 11:08:09 +0000513void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000514 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000515 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000516 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000517
518 // We don't need .set machinery if we have GOT-style relocations
519 if (Subtarget->isPICStyleGOT())
520 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000521
Chris Lattner33adcfb2009-08-22 21:43:10 +0000522 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000523 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000524 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000525 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000526 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000527 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000528 else {
529 O << '-';
530 PrintPICBaseSymbol();
531 O << '\n';
532 }
Evan Chengcc415862007-11-09 01:32:10 +0000533}
534
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000535
Evan Cheng7ccced62006-02-18 00:15:05 +0000536void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000537 PrintPICBaseSymbol();
538 O << '\n';
539 PrintPICBaseSymbol();
540 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000541}
542
Evan Cheng62f27002006-04-28 23:11:40 +0000543
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000544void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
545 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000546 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000547 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000548 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000549
550 O << JTEntryDirective << ' ';
551
Chris Lattnere2c92082009-07-10 21:00:45 +0000552 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000553 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000554 << '_' << uid << "_set_" << MBB->getNumber();
555 } else if (Subtarget->isPICStyleGOT()) {
556 printBasicBlockLabel(MBB, false, false, false);
557 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000558 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000559 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000560}
561
Chris Lattner37710712009-06-15 04:42:32 +0000562bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000563 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000564 switch (Mode) {
565 default: return true; // Unknown mode.
566 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000568 break;
569 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000571 break;
572 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000574 break;
575 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000577 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000578 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000580 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000581 }
582
Evan Chengae270f62008-07-07 22:21:06 +0000583 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000584 return false;
585}
586
Evan Cheng3d48a902006-04-28 21:19:05 +0000587/// PrintAsmOperand - Print out an operand for an inline asm expression.
588///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000589bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000590 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000591 const char *ExtraCode) {
592 // Does this asm operand have a single letter operand modifier?
593 if (ExtraCode && ExtraCode[0]) {
594 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000595
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000596 const MachineOperand &MO = MI->getOperand(OpNo);
597
Evan Cheng3d48a902006-04-28 21:19:05 +0000598 switch (ExtraCode[0]) {
599 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000600 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
601 if (MO.isImm()) {
602 O << MO.getImm();
603 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000604 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000605 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
606 printSymbolOperand(MO);
607 return false;
608 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000609 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000610 O << '(';
611 printOperand(MI, OpNo);
612 O << ')';
613 return false;
614 }
615 return true;
616
Chris Lattnerb4828722007-01-25 02:53:24 +0000617 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000618 if (MO.isImm())
619 O << MO.getImm();
620 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
621 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000622 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000623 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000624 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000625
626 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000627 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000628 O << '*';
629 printOperand(MI, OpNo);
630 return false;
631 }
632 return true;
633
Evan Cheng62f27002006-04-28 23:11:40 +0000634 case 'b': // Print QImode register
635 case 'h': // Print QImode high register
636 case 'w': // Print HImode register
637 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000638 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000639 if (MO.isReg())
640 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000641 printOperand(MI, OpNo);
642 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000643
Dale Johannesen9fcff042009-07-07 23:28:22 +0000644 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000645 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000646 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000647
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000648 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000649 // Note: this is a temporary solution. It should be handled target
650 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000651 if (MO.isImm()) {
652 O << -MO.getImm();
653 return false;
654 }
655 O << '-';
656 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000657 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000658
Evan Cheng3d48a902006-04-28 21:19:05 +0000659 printOperand(MI, OpNo);
660 return false;
661}
662
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000663bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000664 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000665 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000666 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000667 if (ExtraCode && ExtraCode[0]) {
668 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000669
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000670 switch (ExtraCode[0]) {
671 default: return true; // Unknown modifier.
672 case 'b': // Print QImode register
673 case 'h': // Print QImode high register
674 case 'w': // Print HImode register
675 case 'k': // Print SImode register
676 case 'q': // Print SImode register
677 // These only apply to registers, ignore on mem.
678 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000679 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000680 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000681 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000682 }
683 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000684 printMemReference(MI, OpNo);
685 return false;
686}
687
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000688
Chris Lattner9b60e042009-08-16 04:28:14 +0000689
Bill Wendlingac06d002009-02-06 21:45:08 +0000690/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
691/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000692///
693void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
694 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000695
Chris Lattner634cca32009-09-09 20:34:59 +0000696 processDebugLoc(MI->getDebugLoc());
697
Chris Lattner522e9a02009-09-02 17:35:12 +0000698 // Call the autogenerated instruction printer routines.
699 if (NewAsmPrinter)
700 printInstructionThroughMCStreamer(MI);
701 else
Chris Lattner9b60e042009-08-16 04:28:14 +0000702 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000703
704 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
705 EmitComments(*MI);
706 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000707}
708
Chris Lattner40bbebd2009-07-21 18:38:57 +0000709void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000710 const TargetData *TD = TM.getTargetData();
711
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000712 if (!GVar->hasInitializer())
713 return; // External global require no code
714
715 // Check to see if this is a special global used by LLVM, if so, emit it.
716 if (EmitSpecialLLVMGlobal(GVar)) {
717 if (Subtarget->isTargetDarwin() &&
718 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000719 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000720 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000721 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000722 O << ".reference .destructors_used\n";
723 }
724 return;
725 }
726
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000727 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000728 Constant *C = GVar->getInitializer();
729 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000730 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000731 unsigned Align = TD->getPreferredAlignmentLog(GVar);
732
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000733 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000734
735 if (Subtarget->isTargetELF())
736 O << "\t.type\t" << name << ",@object\n";
737
Chris Lattner26d054d2009-08-05 05:21:07 +0000738
739 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000740 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000741 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000742 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000743
Chris Lattner3b24c012009-08-04 05:35:56 +0000744 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000745 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000746 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000747 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000748 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000749 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000750 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000751 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000752 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000753 return;
754 }
755 }
756
757 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000758 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000759 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000760
Chris Lattner33adcfb2009-08-22 21:43:10 +0000761 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000762 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000764 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000765 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000766 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000767 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000768 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000770 O << name << ":";
771 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000772 O.PadToColumn(MAI->getCommentColumn());
773 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000774 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000775 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000776 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000777 EmitGlobalConstant(C);
778 return;
779 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000780 O << MAI->getCOMMDirective() << name << ',' << Size;
781 if (MAI->getCOMMDirectiveTakesAlignment())
782 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000783 }
784 } else {
785 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000786 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000787 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000788 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000789 O << MAI->getCOMMDirective() << name << ',' << Size;
790 if (MAI->getCOMMDirectiveTakesAlignment())
791 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000792 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000793 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000794 O.PadToColumn(MAI->getCommentColumn());
795 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000796 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000797 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000799 return;
800 }
801 }
802
803 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000804 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000805 case GlobalValue::LinkOnceAnyLinkage:
806 case GlobalValue::LinkOnceODRLinkage:
807 case GlobalValue::WeakAnyLinkage:
808 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000809 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000810 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000811 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000812 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000815 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000817 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818 }
819 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000820 case GlobalValue::DLLExportLinkage:
821 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000822 // FIXME: appending linkage variables should go into a section of
823 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000824 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000826 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000827 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000828 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000829 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000830 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000831 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000832 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 }
834
835 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000836 O << name << ":";
837 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000838 O.PadToColumn(MAI->getCommentColumn());
839 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000840 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000841 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000842 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000843
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000845
Chris Lattner33adcfb2009-08-22 21:43:10 +0000846 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000847 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848}
849
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000851 // Print out module-level global variables here.
852 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000853 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000854 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000855 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000856 }
857
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000858 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000859 // All darwin targets use mach-o.
860 TargetLoweringObjectFileMachO &TLOFMacho =
861 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000862
Chris Lattner381d4fe2009-06-24 18:17:00 +0000863 // Add the (possibly multiple) personalities to the set of global value
864 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000865 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000866 const std::vector<Function*> &Personalities = MMI->getPersonalities();
867 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000868 if (Personalities[i])
869 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
870 true /*private label*/)] =
871 Mang->getMangledName(Personalities[i]);
Evan Cheng77c8f762008-07-08 00:55:58 +0000872 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000873 }
874
Chris Lattner8f61f982009-06-24 18:24:09 +0000875 // Output stubs for dynamically-linked functions
876 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000877 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000878 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
879 MCSectionMachO::S_SYMBOL_STUBS |
880 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
881 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
882 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000883 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000884 for (StringMap<std::string>::iterator I = FnStubs.begin(),
885 E = FnStubs.end(); I != E; ++I)
886 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
887 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000888 O << '\n';
889 }
890
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000891 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000892 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000893 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000894 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
895 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000896 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000897 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000898 for (StringMap<std::string>::iterator I = GVStubs.begin(),
899 E = GVStubs.end(); I != E; ++I)
900 O << I->getKeyData() << ":\n\t.indirect_symbol "
901 << I->second << "\n\t.long\t0\n";
Chris Lattner8f61f982009-06-24 18:24:09 +0000902 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000903
Evan Chengae94e592008-12-05 01:06:39 +0000904 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000905 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000906 EmitAlignment(2);
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000907 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
908 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000909 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000910 << I->second << '\n';
Evan Chengae94e592008-12-05 01:06:39 +0000911 }
912
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000913 // Funny Darwin hack: This flag tells the linker that no global symbols
914 // contain code that falls through to other global symbols (e.g. the obvious
915 // implementation of multiple entry points). If this doesn't occur, the
916 // linker can safely perform dead code stripping. Since LLVM never
917 // generates code that does this, it is always safe to set.
918 O << "\t.subsections_via_symbols\n";
919 } else if (Subtarget->isTargetCygMing()) {
920 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000921 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000922 i != e; ++i) {
923 O << "\t.def\t " << i->getKeyData()
924 << ";\t.scl\t" << COFF::C_EXT
925 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
926 << ";\t.endef\n";
927 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000928 }
Chris Lattner974469d2009-06-24 05:47:59 +0000929
Chris Lattner0a7befa2009-06-24 18:52:01 +0000930
931 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +0000932 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
933 // dllexport symbols only exist on coff targets.
934 TargetLoweringObjectFileCOFF &TLOFMacho =
935 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
936
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000937 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
938 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +0000939
940 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
941 e = DLLExportedGVs.end(); i != e; ++i)
942 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +0000943
944 for (StringSet<>::iterator i = DLLExportedFns.begin(),
945 e = DLLExportedFns.end();
946 i != e; ++i)
947 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
948 }
949
Chris Lattner0a7befa2009-06-24 18:52:01 +0000950 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +0000951 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000952}
953
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000954// Include the auto-generated portion of the assembly writer.
955#include "X86GenAsmWriter.inc"