blob: 3e80a6b7573e08c9f82991bc74f48b60a38fbe61 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
18#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/CallingConv.h"
Anton Korobeynikov5772c672007-11-14 09:18:41 +000025#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Module.h"
27#include "llvm/Support/Mangler.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetOptions.h"
30#include "llvm/ADT/Statistic.h"
31using namespace llvm;
32
33STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
Evan Cheng0729ccf2008-01-05 00:41:47 +000035static std::string getPICLabelString(unsigned FnNum,
36 const TargetAsmInfo *TAI,
37 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 std::string label;
39 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000040 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 else if (Subtarget->isTargetELF())
Evan Cheng0729ccf2008-01-05 00:41:47 +000042 label = ".Lllvm$" + utostr_32(FnNum) + "." + "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 else
44 assert(0 && "Don't know how to print PIC label!\n");
45
46 return label;
47}
48
49/// getSectionForFunction - Return the section that we should emit the
50/// specified function body into.
51std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
52 switch (F.getLinkage()) {
53 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +000054 case Function::InternalLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 case Function::DLLExportLinkage:
56 case Function::ExternalLinkage:
57 return TAI->getTextSection();
58 case Function::WeakLinkage:
59 case Function::LinkOnceLinkage:
60 if (Subtarget->isTargetDarwin()) {
61 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
62 } else if (Subtarget->isTargetCygMing()) {
63 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
64 } else {
65 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
66 ",\"ax\",@progbits";
67 }
68 }
69}
70
71/// runOnMachineFunction - This uses the printMachineInstruction()
72/// method to print assembly for each instruction.
73///
74bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
75 if (TAI->doesSupportDebugInformation()) {
76 // Let PassManager know we need debug information and relay
77 // the MachineModuleInfo address on to DwarfWriter.
Bill Wendlingd1bda4f2007-09-11 08:27:17 +000078 MMI = &getAnalysis<MachineModuleInfo>();
79 DW.SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 }
81
82 SetupMachineFunction(MF);
83 O << "\n\n";
84
85 // Print out constants referenced by the function
86 EmitConstantPool(MF.getConstantPool());
87
88 // Print out labels for the function.
89 const Function *F = MF.getFunction();
90 unsigned CC = F->getCallingConv();
91
92 // Populate function information map. Actually, We don't want to populate
93 // non-stdcall or non-fastcall functions' information right now.
94 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
95 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
96
97 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
98
99 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000100
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000101 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 switch (F->getLinkage()) {
103 default: assert(0 && "Unknown linkage type!");
104 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000105 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 break;
107 case Function::DLLExportLinkage:
108 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
109 //FALLS THROUGH
110 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000111 EmitAlignment(FnAlign, F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000112 O << "\t.globl\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 break;
114 case Function::LinkOnceLinkage:
115 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000116 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 if (Subtarget->isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 O << "\t.globl\t" << CurrentFnName << "\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000119 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanc37918d2007-10-05 15:54:58 +0000121 O << "\t.globl\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 O << "\t.linkonce discard\n";
123 } else {
Dan Gohman721e6582007-07-30 15:08:02 +0000124 O << "\t.weak\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 }
126 break;
127 }
128 if (F->hasHiddenVisibility()) {
129 if (const char *Directive = TAI->getHiddenDirective())
130 O << Directive << CurrentFnName << "\n";
131 } else if (F->hasProtectedVisibility()) {
132 if (const char *Directive = TAI->getProtectedDirective())
133 O << Directive << CurrentFnName << "\n";
134 }
135
136 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000137 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 else if (Subtarget->isTargetCygMing()) {
139 O << "\t.def\t " << CurrentFnName
140 << ";\t.scl\t" <<
141 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
142 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
143 << ";\t.endef\n";
144 }
145
146 O << CurrentFnName << ":\n";
147 // Add some workaround for linkonce linkage on Cygwin\MinGW
148 if (Subtarget->isTargetCygMing() &&
149 (F->getLinkage() == Function::LinkOnceLinkage ||
150 F->getLinkage() == Function::WeakLinkage))
151 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
152
Dale Johannesen85535762008-04-02 00:25:04 +0000153 if (TAI->doesSupportDebugInformation() ||
154 TAI->doesSupportExceptionHandling()) {
155 // Emit pre-function debug and/or EH information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 DW.BeginFunction(&MF);
157 }
158
159 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000160 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
162 I != E; ++I) {
163 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000164 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000165 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 O << '\n';
167 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000168 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
169 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 // Print the assembly for the instruction.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000171 if (II->getOpcode() != X86::LABEL)
172 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 printMachineInstruction(II);
174 }
175 }
176
Dale Johannesenf35771f2008-04-08 00:37:56 +0000177 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
178 // If the function is empty, then we need to emit *something*. Otherwise,
179 // the function's label might be associated with something that it wasn't
180 // meant to be associated with. We emit a noop in this situation.
181 // We are assuming inline asms are code.
182 O << "\tnop\n";
183 }
184
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmandd1aa3a2007-08-01 14:42:30 +0000186 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
188 if (TAI->doesSupportDebugInformation()) {
189 // Emit post-function debug information.
190 DW.EndFunction();
191 }
192
193 // Print out jump tables referenced by the function.
194 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // We didn't modify anything.
197 return false;
198}
199
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000200static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
202}
203
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000204static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
205 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
206 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
207}
208
209static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
211}
212
213void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
214 const char *Modifier, bool NotRIPRel) {
215 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 switch (MO.getType()) {
217 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000218 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 "Virtual registers should not make it this far!");
220 O << '%';
221 unsigned Reg = MO.getReg();
222 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000223 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
225 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
226 Reg = getX86SubSuperRegister(Reg, VT);
227 }
Evan Cheng3c0eda52008-03-15 00:03:38 +0000228 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 O << (char)tolower(*Name);
230 return;
231 }
232
233 case MachineOperand::MO_Immediate:
234 if (!Modifier ||
235 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
236 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000237 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 return;
239 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000240 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 return;
242 case MachineOperand::MO_JumpTableIndex: {
243 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
244 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000245 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000246 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
248 if (TM.getRelocationModel() == Reloc::PIC_) {
249 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000250 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
251 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 else if (Subtarget->isPICStyleGOT())
253 O << "@GOTOFF";
254 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
257 O << "(%rip)";
258 return;
259 }
260 case MachineOperand::MO_ConstantPoolIndex: {
261 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
262 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000263 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000264 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
266 if (TM.getRelocationModel() == Reloc::PIC_) {
267 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000268 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
269 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 else if (Subtarget->isPICStyleGOT())
271 O << "@GOTOFF";
272 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 int Offset = MO.getOffset();
275 if (Offset > 0)
276 O << "+" << Offset;
277 else if (Offset < 0)
278 O << Offset;
279
280 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
281 O << "(%rip)";
282 return;
283 }
284 case MachineOperand::MO_GlobalAddress: {
285 bool isCallOp = Modifier && !strcmp(Modifier, "call");
286 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
287 bool needCloseParen = false;
288
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000289 const GlobalValue *GV = MO.getGlobal();
290 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
291 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000292 // If GV is an alias then use the aliasee for determining
293 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000294 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
295 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
296 }
297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 bool isThreadLocal = GVar && GVar->isThreadLocal();
299
300 std::string Name = Mang->getValueName(GV);
301 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 if (!isMemOp && !isCallOp)
304 O << '$';
305 else if (Name[0] == '$') {
306 // The name begins with a dollar-sign. In order to avoid having it look
307 // like an integer immediate to the assembler, enclose it in parens.
308 O << '(';
309 needCloseParen = true;
310 }
311
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000312 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 // Link-once, declaration, or Weakly-linked global variables need
314 // non-lazily-resolved stubs
315 if (GV->isDeclaration() ||
316 GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000317 GV->hasLinkOnceLinkage() ||
318 GV->hasCommonLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 // Dynamically-resolved functions need a stub for the function.
320 if (isCallOp && isa<Function>(GV)) {
321 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000322 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 } else {
324 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000325 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 }
327 } else {
328 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000329 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 O << Name;
331 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000334 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 } else {
336 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000337 O << "__imp_";
338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 O << Name;
340
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000341 if (isCallOp) {
342 if (shouldPrintPLT(TM, Subtarget)) {
343 // Assemble call via PLT for externally visible symbols
344 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
345 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 O << "@PLT";
347 }
348 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
349 // Save function name for later type emission
350 FnStubs.insert(Name);
351 }
352 }
353
354 if (GV->hasExternalWeakLinkage())
355 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 int Offset = MO.getOffset();
358 if (Offset > 0)
359 O << "+" << Offset;
360 else if (Offset < 0)
361 O << Offset;
362
363 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000364 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 O << "@TLSGD"; // general dynamic TLS model
366 else
367 if (GV->isDeclaration())
368 O << "@INDNTPOFF"; // initial exec TLS model
369 else
370 O << "@NTPOFF"; // local exec TLS model
371 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000372 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
374 O << "@GOT";
375 else
376 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000377 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
378 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000379 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 O << "@GOTPCREL";
381
382 if (needCloseParen) {
383 needCloseParen = false;
384 O << ')';
385 }
386
387 // Use rip when possible to reduce code size, except when
388 // index or base register are also part of the address. e.g.
389 // foo(%rip)(%rcx,%rax,4) is not legal
390 O << "(%rip)";
391 }
392 }
393
394 if (needCloseParen)
395 O << ')';
396
397 return;
398 }
399 case MachineOperand::MO_ExternalSymbol: {
400 bool isCallOp = Modifier && !strcmp(Modifier, "call");
401 bool needCloseParen = false;
402 std::string Name(TAI->getGlobalPrefix());
403 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000404 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000406 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 return;
408 }
409 if (!isCallOp)
410 O << '$';
411 else if (Name[0] == '$') {
412 // The name begins with a dollar-sign. In order to avoid having it look
413 // like an integer immediate to the assembler, enclose it in parens.
414 O << '(';
415 needCloseParen = true;
416 }
417
418 O << Name;
419
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000420 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 std::string GOTName(TAI->getGlobalPrefix());
422 GOTName+="_GLOBAL_OFFSET_TABLE_";
423 if (Name == GOTName)
424 // HACK! Emit extra offset to PC during printing GOT offset to
425 // compensate for the size of popl instruction. The resulting code
426 // should look like:
427 // call .piclabel
428 // piclabel:
429 // popl %some_register
430 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
431 O << " + [.-"
Evan Cheng0729ccf2008-01-05 00:41:47 +0000432 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433
434 if (isCallOp)
435 O << "@PLT";
436 }
437
438 if (needCloseParen)
439 O << ')';
440
441 if (!isCallOp && Subtarget->isPICStyleRIPRel())
442 O << "(%rip)";
443
444 return;
445 }
446 default:
447 O << "<unknown operand type>"; return;
448 }
449}
450
451void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000452 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 assert(value <= 7 && "Invalid ssecc argument!");
454 switch (value) {
455 case 0: O << "eq"; break;
456 case 1: O << "lt"; break;
457 case 2: O << "le"; break;
458 case 3: O << "unord"; break;
459 case 4: O << "neq"; break;
460 case 5: O << "nlt"; break;
461 case 6: O << "nle"; break;
462 case 7: O << "ord"; break;
463 }
464}
465
466void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
467 const char *Modifier){
468 assert(isMem(MI, Op) && "Invalid memory reference!");
469 MachineOperand BaseReg = MI->getOperand(Op);
470 MachineOperand IndexReg = MI->getOperand(Op+2);
471 const MachineOperand &DispSpec = MI->getOperand(Op+3);
472
473 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
474 if (DispSpec.isGlobalAddress() ||
475 DispSpec.isConstantPoolIndex() ||
476 DispSpec.isJumpTableIndex()) {
477 printOperand(MI, Op+3, "mem", NotRIPRel);
478 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000479 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
481 O << DispVal;
482 }
483
484 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000485 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 // There are cases where we can end up with ESP/RSP in the indexreg slot.
489 // If this happens, swap the base/index register to support assemblers that
490 // don't work when the index is *SP.
491 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
492 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
493 std::swap(BaseReg, IndexReg);
494 std::swap(BaseRegOperand, IndexRegOperand);
495 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 O << "(";
498 if (BaseReg.getReg())
499 printOperand(MI, Op+BaseRegOperand, Modifier);
500
501 if (IndexReg.getReg()) {
502 O << ",";
503 printOperand(MI, Op+IndexRegOperand, Modifier);
504 if (ScaleVal != 1)
505 O << "," << ScaleVal;
506 }
507 O << ")";
508 }
509}
510
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000511void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000512 const MachineBasicBlock *MBB) const {
513 if (!TAI->getSetDirective())
514 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000515
516 // We don't need .set machinery if we have GOT-style relocations
517 if (Subtarget->isPICStyleGOT())
518 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000519
Evan Cheng6fb06762007-11-09 01:32:10 +0000520 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
521 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000522 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000523 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000524 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000525 << '_' << uid << '\n';
526 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000527 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000528}
529
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000531 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 O << label << "\n" << label << ":";
533}
534
535
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000536void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
537 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000538 unsigned uid) const
539{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000540 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
541 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
542
543 O << JTEntryDirective << ' ';
544
545 if (TM.getRelocationModel() == Reloc::PIC_) {
546 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
547 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
548 << '_' << uid << "_set_" << MBB->getNumber();
549 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000550 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000551 O << "@GOTOFF";
552 } else
553 assert(0 && "Don't know how to print MBB label for this PIC mode");
554 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000555 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000556}
557
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
559 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 unsigned Reg = MO.getReg();
561 switch (Mode) {
562 default: return true; // Unknown mode.
563 case 'b': // Print QImode register
564 Reg = getX86SubSuperRegister(Reg, MVT::i8);
565 break;
566 case 'h': // Print QImode high register
567 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
568 break;
569 case 'w': // Print HImode register
570 Reg = getX86SubSuperRegister(Reg, MVT::i16);
571 break;
572 case 'k': // Print SImode register
573 Reg = getX86SubSuperRegister(Reg, MVT::i32);
574 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000575 case 'q': // Print DImode register
576 Reg = getX86SubSuperRegister(Reg, MVT::i64);
577 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 }
579
580 O << '%';
Evan Cheng3c0eda52008-03-15 00:03:38 +0000581 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 O << (char)tolower(*Name);
583 return false;
584}
585
586/// PrintAsmOperand - Print out an operand for an inline asm expression.
587///
588bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000589 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 const char *ExtraCode) {
591 // Does this asm operand have a single letter operand modifier?
592 if (ExtraCode && ExtraCode[0]) {
593 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 switch (ExtraCode[0]) {
596 default: return true; // Unknown modifier.
597 case 'c': // Don't print "$" before a global var name or constant.
598 printOperand(MI, OpNo, "mem");
599 return false;
600 case 'b': // Print QImode register
601 case 'h': // Print QImode high register
602 case 'w': // Print HImode register
603 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000604 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000605 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
607 printOperand(MI, OpNo);
608 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000609
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 case 'P': // Don't print @PLT, but do print as memory.
611 printOperand(MI, OpNo, "mem");
612 return false;
613 }
614 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 printOperand(MI, OpNo);
617 return false;
618}
619
620bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
621 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000622 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000624 if (ExtraCode && ExtraCode[0]) {
625 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000626
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000627 switch (ExtraCode[0]) {
628 default: return true; // Unknown modifier.
629 case 'b': // Print QImode register
630 case 'h': // Print QImode high register
631 case 'w': // Print HImode register
632 case 'k': // Print SImode register
633 case 'q': // Print SImode register
634 // These only apply to registers, ignore on mem.
635 break;
636 }
637 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 printMemReference(MI, OpNo);
639 return false;
640}
641
642/// printMachineInstruction -- Print out a single X86 LLVM instruction
643/// MI in AT&T syntax to the current output stream.
644///
645void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
646 ++EmittedInsts;
647
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 // Call the autogenerated instruction printer routines.
649 printInstruction(MI);
650}
651
652// Include the auto-generated portion of the assembly writer.
653#include "X86GenAsmWriter.inc"