blob: 151bf119cab520ede3abb7e2aba952ce416f6e8a [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!");
54 case Function::InternalLinkage:
55 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);
100
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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 O << "\t.globl\t" << CurrentFnName << "\n";
113 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
Bill Wendlingb5880a72008-01-26 09:03:52 +0000159 if (Subtarget->isTargetDarwin()) {
160 // If the function is empty, then we need to emit *something*. Otherwise,
161 // the function's label might be associated with something that it wasn't
162 // meant to be associated with. We emit a noop in this situation.
163 MachineFunction::iterator I = MF.begin();
164
165 if (++I == MF.end() && MF.front().empty())
166 O << "\tnop\n";
167 }
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171 I != E; ++I) {
172 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000173 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000174 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 O << '\n';
176 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000177 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
178 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 printMachineInstruction(II);
181 }
182 }
183
184 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmandd1aa3a2007-08-01 14:42:30 +0000185 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 if (TAI->doesSupportDebugInformation()) {
188 // Emit post-function debug information.
189 DW.EndFunction();
190 }
191
192 // Print out jump tables referenced by the function.
193 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
194
195 // We didn't modify anything.
196 return false;
197}
198
199static inline bool printGOT(TargetMachine &TM, const X86Subtarget* ST) {
200 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
201}
202
203static inline bool printStub(TargetMachine &TM, const X86Subtarget* ST) {
204 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
205}
206
207void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
208 const char *Modifier, bool NotRIPRel) {
209 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 switch (MO.getType()) {
211 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000212 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 "Virtual registers should not make it this far!");
214 O << '%';
215 unsigned Reg = MO.getReg();
216 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
217 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
218 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
219 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
220 Reg = getX86SubSuperRegister(Reg, VT);
221 }
Evan Cheng3c0eda52008-03-15 00:03:38 +0000222 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 O << (char)tolower(*Name);
224 return;
225 }
226
227 case MachineOperand::MO_Immediate:
228 if (!Modifier ||
229 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
230 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000231 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return;
233 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000234 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 return;
236 case MachineOperand::MO_JumpTableIndex: {
237 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
238 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000239 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000240 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241
242 if (TM.getRelocationModel() == Reloc::PIC_) {
243 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000244 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
245 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 else if (Subtarget->isPICStyleGOT())
247 O << "@GOTOFF";
248 }
249
250 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
251 O << "(%rip)";
252 return;
253 }
254 case MachineOperand::MO_ConstantPoolIndex: {
255 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
256 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000257 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000258 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
260 if (TM.getRelocationModel() == Reloc::PIC_) {
261 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000262 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
263 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 else if (Subtarget->isPICStyleGOT())
265 O << "@GOTOFF";
266 }
267
268 int Offset = MO.getOffset();
269 if (Offset > 0)
270 O << "+" << Offset;
271 else if (Offset < 0)
272 O << Offset;
273
274 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
275 O << "(%rip)";
276 return;
277 }
278 case MachineOperand::MO_GlobalAddress: {
279 bool isCallOp = Modifier && !strcmp(Modifier, "call");
280 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
281 bool needCloseParen = false;
282
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000283 const GlobalValue *GV = MO.getGlobal();
284 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
285 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000286 // If GV is an alias then use the aliasee for determining
287 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000288 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
289 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
290 }
291
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 bool isThreadLocal = GVar && GVar->isThreadLocal();
293
294 std::string Name = Mang->getValueName(GV);
295 X86SharedAsmPrinter::decorateName(Name, GV);
296
297 if (!isMemOp && !isCallOp)
298 O << '$';
299 else if (Name[0] == '$') {
300 // The name begins with a dollar-sign. In order to avoid having it look
301 // like an integer immediate to the assembler, enclose it in parens.
302 O << '(';
303 needCloseParen = true;
304 }
305
306 if (printStub(TM, Subtarget)) {
307 // Link-once, declaration, or Weakly-linked global variables need
308 // non-lazily-resolved stubs
309 if (GV->isDeclaration() ||
310 GV->hasWeakLinkage() ||
311 GV->hasLinkOnceLinkage()) {
312 // Dynamically-resolved functions need a stub for the function.
313 if (isCallOp && isa<Function>(GV)) {
314 FnStubs.insert(Name);
315 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
316 } else {
317 GVStubs.insert(Name);
318 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
319 }
320 } else {
321 if (GV->hasDLLImportLinkage())
322 O << "__imp_";
323 O << Name;
324 }
325
326 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000327 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 } else {
329 if (GV->hasDLLImportLinkage()) {
330 O << "__imp_";
331 }
332 O << Name;
333
334 if (isCallOp && isa<Function>(GV)) {
335 if (printGOT(TM, Subtarget)) {
336 // Assemble call via PLT for non-local symbols
337 if (!(GV->hasHiddenVisibility() || GV->hasProtectedVisibility()) ||
338 GV->isDeclaration())
339 O << "@PLT";
340 }
341 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
342 // Save function name for later type emission
343 FnStubs.insert(Name);
344 }
345 }
346
347 if (GV->hasExternalWeakLinkage())
348 ExtWeakSymbols.insert(GV);
349
350 int Offset = MO.getOffset();
351 if (Offset > 0)
352 O << "+" << Offset;
353 else if (Offset < 0)
354 O << Offset;
355
356 if (isThreadLocal) {
357 if (TM.getRelocationModel() == Reloc::PIC_)
358 O << "@TLSGD"; // general dynamic TLS model
359 else
360 if (GV->isDeclaration())
361 O << "@INDNTPOFF"; // initial exec TLS model
362 else
363 O << "@NTPOFF"; // local exec TLS model
364 } else if (isMemOp) {
365 if (printGOT(TM, Subtarget)) {
366 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
367 O << "@GOT";
368 else
369 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000370 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
371 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000372 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 O << "@GOTPCREL";
374
375 if (needCloseParen) {
376 needCloseParen = false;
377 O << ')';
378 }
379
380 // Use rip when possible to reduce code size, except when
381 // index or base register are also part of the address. e.g.
382 // foo(%rip)(%rcx,%rax,4) is not legal
383 O << "(%rip)";
384 }
385 }
386
387 if (needCloseParen)
388 O << ')';
389
390 return;
391 }
392 case MachineOperand::MO_ExternalSymbol: {
393 bool isCallOp = Modifier && !strcmp(Modifier, "call");
394 bool needCloseParen = false;
395 std::string Name(TAI->getGlobalPrefix());
396 Name += MO.getSymbolName();
397 if (isCallOp && printStub(TM, Subtarget)) {
398 FnStubs.insert(Name);
399 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
400 return;
401 }
402 if (!isCallOp)
403 O << '$';
404 else if (Name[0] == '$') {
405 // The name begins with a dollar-sign. In order to avoid having it look
406 // like an integer immediate to the assembler, enclose it in parens.
407 O << '(';
408 needCloseParen = true;
409 }
410
411 O << Name;
412
413 if (printGOT(TM, Subtarget)) {
414 std::string GOTName(TAI->getGlobalPrefix());
415 GOTName+="_GLOBAL_OFFSET_TABLE_";
416 if (Name == GOTName)
417 // HACK! Emit extra offset to PC during printing GOT offset to
418 // compensate for the size of popl instruction. The resulting code
419 // should look like:
420 // call .piclabel
421 // piclabel:
422 // popl %some_register
423 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
424 O << " + [.-"
Evan Cheng0729ccf2008-01-05 00:41:47 +0000425 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426
427 if (isCallOp)
428 O << "@PLT";
429 }
430
431 if (needCloseParen)
432 O << ')';
433
434 if (!isCallOp && Subtarget->isPICStyleRIPRel())
435 O << "(%rip)";
436
437 return;
438 }
439 default:
440 O << "<unknown operand type>"; return;
441 }
442}
443
444void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000445 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 assert(value <= 7 && "Invalid ssecc argument!");
447 switch (value) {
448 case 0: O << "eq"; break;
449 case 1: O << "lt"; break;
450 case 2: O << "le"; break;
451 case 3: O << "unord"; break;
452 case 4: O << "neq"; break;
453 case 5: O << "nlt"; break;
454 case 6: O << "nle"; break;
455 case 7: O << "ord"; break;
456 }
457}
458
459void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
460 const char *Modifier){
461 assert(isMem(MI, Op) && "Invalid memory reference!");
462 MachineOperand BaseReg = MI->getOperand(Op);
463 MachineOperand IndexReg = MI->getOperand(Op+2);
464 const MachineOperand &DispSpec = MI->getOperand(Op+3);
465
466 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
467 if (DispSpec.isGlobalAddress() ||
468 DispSpec.isConstantPoolIndex() ||
469 DispSpec.isJumpTableIndex()) {
470 printOperand(MI, Op+3, "mem", NotRIPRel);
471 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000472 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
474 O << DispVal;
475 }
476
477 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000478 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
480
481 // There are cases where we can end up with ESP/RSP in the indexreg slot.
482 // If this happens, swap the base/index register to support assemblers that
483 // don't work when the index is *SP.
484 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
485 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
486 std::swap(BaseReg, IndexReg);
487 std::swap(BaseRegOperand, IndexRegOperand);
488 }
489
490 O << "(";
491 if (BaseReg.getReg())
492 printOperand(MI, Op+BaseRegOperand, Modifier);
493
494 if (IndexReg.getReg()) {
495 O << ",";
496 printOperand(MI, Op+IndexRegOperand, Modifier);
497 if (ScaleVal != 1)
498 O << "," << ScaleVal;
499 }
500 O << ")";
501 }
502}
503
Evan Cheng6fb06762007-11-09 01:32:10 +0000504void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
505 const MachineBasicBlock *MBB) const {
506 if (!TAI->getSetDirective())
507 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000508
509 // We don't need .set machinery if we have GOT-style relocations
510 if (Subtarget->isPICStyleGOT())
511 return;
512
Evan Cheng6fb06762007-11-09 01:32:10 +0000513 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
514 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000515 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000516 if (Subtarget->isPICStyleRIPRel())
517 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
518 << '_' << uid << '\n';
519 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000520 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000521}
522
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000524 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 O << label << "\n" << label << ":";
526}
527
528
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000529void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
530 const MachineBasicBlock *MBB,
531 unsigned uid) const
532{
533 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
534 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
535
536 O << JTEntryDirective << ' ';
537
538 if (TM.getRelocationModel() == Reloc::PIC_) {
539 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
540 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
541 << '_' << uid << "_set_" << MBB->getNumber();
542 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000543 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000544 O << "@GOTOFF";
545 } else
546 assert(0 && "Don't know how to print MBB label for this PIC mode");
547 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000548 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000549}
550
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
552 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 unsigned Reg = MO.getReg();
554 switch (Mode) {
555 default: return true; // Unknown mode.
556 case 'b': // Print QImode register
557 Reg = getX86SubSuperRegister(Reg, MVT::i8);
558 break;
559 case 'h': // Print QImode high register
560 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
561 break;
562 case 'w': // Print HImode register
563 Reg = getX86SubSuperRegister(Reg, MVT::i16);
564 break;
565 case 'k': // Print SImode register
566 Reg = getX86SubSuperRegister(Reg, MVT::i32);
567 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000568 case 'q': // Print DImode register
569 Reg = getX86SubSuperRegister(Reg, MVT::i64);
570 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 }
572
573 O << '%';
Evan Cheng3c0eda52008-03-15 00:03:38 +0000574 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 O << (char)tolower(*Name);
576 return false;
577}
578
579/// PrintAsmOperand - Print out an operand for an inline asm expression.
580///
581bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
582 unsigned AsmVariant,
583 const char *ExtraCode) {
584 // Does this asm operand have a single letter operand modifier?
585 if (ExtraCode && ExtraCode[0]) {
586 if (ExtraCode[1] != 0) return true; // Unknown modifier.
587
588 switch (ExtraCode[0]) {
589 default: return true; // Unknown modifier.
590 case 'c': // Don't print "$" before a global var name or constant.
591 printOperand(MI, OpNo, "mem");
592 return false;
593 case 'b': // Print QImode register
594 case 'h': // Print QImode high register
595 case 'w': // Print HImode register
596 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000597 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000598 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
600 printOperand(MI, OpNo);
601 return false;
602
603 case 'P': // Don't print @PLT, but do print as memory.
604 printOperand(MI, OpNo, "mem");
605 return false;
606 }
607 }
608
609 printOperand(MI, OpNo);
610 return false;
611}
612
613bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
614 unsigned OpNo,
615 unsigned AsmVariant,
616 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000617 if (ExtraCode && ExtraCode[0]) {
618 if (ExtraCode[1] != 0) return true; // Unknown modifier.
619
620 switch (ExtraCode[0]) {
621 default: return true; // Unknown modifier.
622 case 'b': // Print QImode register
623 case 'h': // Print QImode high register
624 case 'w': // Print HImode register
625 case 'k': // Print SImode register
626 case 'q': // Print SImode register
627 // These only apply to registers, ignore on mem.
628 break;
629 }
630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 printMemReference(MI, OpNo);
632 return false;
633}
634
635/// printMachineInstruction -- Print out a single X86 LLVM instruction
636/// MI in AT&T syntax to the current output stream.
637///
638void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
639 ++EmittedInsts;
640
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 // Call the autogenerated instruction printer routines.
642 printInstruction(MI);
643}
644
645// Include the auto-generated portion of the assembly writer.
646#include "X86GenAsmWriter.inc"
647