blob: ea003c3319382d0303cfb46ebcf05b6cd3ef0709 [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
101 switch (F->getLinkage()) {
102 default: assert(0 && "Unknown linkage type!");
103 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng45c1edb2008-02-28 00:43:03 +0000104 EmitAlignment(4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 break;
106 case Function::DLLExportLinkage:
107 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
108 //FALLS THROUGH
109 case Function::ExternalLinkage:
Evan Cheng45c1edb2008-02-28 00:43:03 +0000110 EmitAlignment(4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 O << "\t.globl\t" << CurrentFnName << "\n";
112 break;
113 case Function::LinkOnceLinkage:
114 case Function::WeakLinkage:
Evan Cheng45c1edb2008-02-28 00:43:03 +0000115 EmitAlignment(4, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 if (Subtarget->isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 O << "\t.globl\t" << CurrentFnName << "\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000118 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanc37918d2007-10-05 15:54:58 +0000120 O << "\t.globl\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 O << "\t.linkonce discard\n";
122 } else {
Dan Gohman721e6582007-07-30 15:08:02 +0000123 O << "\t.weak\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 }
125 break;
126 }
127 if (F->hasHiddenVisibility()) {
128 if (const char *Directive = TAI->getHiddenDirective())
129 O << Directive << CurrentFnName << "\n";
130 } else if (F->hasProtectedVisibility()) {
131 if (const char *Directive = TAI->getProtectedDirective())
132 O << Directive << CurrentFnName << "\n";
133 }
134
135 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000136 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 else if (Subtarget->isTargetCygMing()) {
138 O << "\t.def\t " << CurrentFnName
139 << ";\t.scl\t" <<
140 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
141 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
142 << ";\t.endef\n";
143 }
144
145 O << CurrentFnName << ":\n";
146 // Add some workaround for linkonce linkage on Cygwin\MinGW
147 if (Subtarget->isTargetCygMing() &&
148 (F->getLinkage() == Function::LinkOnceLinkage ||
149 F->getLinkage() == Function::WeakLinkage))
150 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
151
152 if (TAI->doesSupportDebugInformation()) {
153 // Emit pre-function debug information.
154 DW.BeginFunction(&MF);
155 }
156
Bill Wendlingb5880a72008-01-26 09:03:52 +0000157 if (Subtarget->isTargetDarwin()) {
158 // If the function is empty, then we need to emit *something*. Otherwise,
159 // the function's label might be associated with something that it wasn't
160 // meant to be associated with. We emit a noop in this situation.
161 MachineFunction::iterator I = MF.begin();
162
163 if (++I == MF.end() && MF.front().empty())
164 O << "\tnop\n";
165 }
166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // Print out code for the function.
168 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
169 I != E; ++I) {
170 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000171 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000172 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 O << '\n';
174 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000175 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
176 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 printMachineInstruction(II);
179 }
180 }
181
182 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmandd1aa3a2007-08-01 14:42:30 +0000183 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184
185 if (TAI->doesSupportDebugInformation()) {
186 // Emit post-function debug information.
187 DW.EndFunction();
188 }
189
190 // Print out jump tables referenced by the function.
191 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
192
193 // We didn't modify anything.
194 return false;
195}
196
197static inline bool printGOT(TargetMachine &TM, const X86Subtarget* ST) {
198 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
199}
200
201static inline bool printStub(TargetMachine &TM, const X86Subtarget* ST) {
202 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
203}
204
205void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
206 const char *Modifier, bool NotRIPRel) {
207 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 switch (MO.getType()) {
209 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000210 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 "Virtual registers should not make it this far!");
212 O << '%';
213 unsigned Reg = MO.getReg();
214 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
215 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
216 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
217 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
218 Reg = getX86SubSuperRegister(Reg, VT);
219 }
Evan Cheng3c0eda52008-03-15 00:03:38 +0000220 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 O << (char)tolower(*Name);
222 return;
223 }
224
225 case MachineOperand::MO_Immediate:
226 if (!Modifier ||
227 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
228 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000229 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return;
231 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000232 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return;
234 case MachineOperand::MO_JumpTableIndex: {
235 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
236 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000237 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000238 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239
240 if (TM.getRelocationModel() == Reloc::PIC_) {
241 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000242 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
243 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 else if (Subtarget->isPICStyleGOT())
245 O << "@GOTOFF";
246 }
247
248 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
249 O << "(%rip)";
250 return;
251 }
252 case MachineOperand::MO_ConstantPoolIndex: {
253 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
254 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000255 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000256 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257
258 if (TM.getRelocationModel() == Reloc::PIC_) {
259 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000260 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
261 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 else if (Subtarget->isPICStyleGOT())
263 O << "@GOTOFF";
264 }
265
266 int Offset = MO.getOffset();
267 if (Offset > 0)
268 O << "+" << Offset;
269 else if (Offset < 0)
270 O << Offset;
271
272 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
273 O << "(%rip)";
274 return;
275 }
276 case MachineOperand::MO_GlobalAddress: {
277 bool isCallOp = Modifier && !strcmp(Modifier, "call");
278 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
279 bool needCloseParen = false;
280
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000281 const GlobalValue *GV = MO.getGlobal();
282 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
283 if (!GVar) {
284 // If GV is an alias - use aliasee for determing thread-localness
285 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
286 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
287 }
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 bool isThreadLocal = GVar && GVar->isThreadLocal();
290
291 std::string Name = Mang->getValueName(GV);
292 X86SharedAsmPrinter::decorateName(Name, GV);
293
294 if (!isMemOp && !isCallOp)
295 O << '$';
296 else if (Name[0] == '$') {
297 // The name begins with a dollar-sign. In order to avoid having it look
298 // like an integer immediate to the assembler, enclose it in parens.
299 O << '(';
300 needCloseParen = true;
301 }
302
303 if (printStub(TM, Subtarget)) {
304 // Link-once, declaration, or Weakly-linked global variables need
305 // non-lazily-resolved stubs
306 if (GV->isDeclaration() ||
307 GV->hasWeakLinkage() ||
308 GV->hasLinkOnceLinkage()) {
309 // Dynamically-resolved functions need a stub for the function.
310 if (isCallOp && isa<Function>(GV)) {
311 FnStubs.insert(Name);
312 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
313 } else {
314 GVStubs.insert(Name);
315 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
316 }
317 } else {
318 if (GV->hasDLLImportLinkage())
319 O << "__imp_";
320 O << Name;
321 }
322
323 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000324 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 } else {
326 if (GV->hasDLLImportLinkage()) {
327 O << "__imp_";
328 }
329 O << Name;
330
331 if (isCallOp && isa<Function>(GV)) {
332 if (printGOT(TM, Subtarget)) {
333 // Assemble call via PLT for non-local symbols
334 if (!(GV->hasHiddenVisibility() || GV->hasProtectedVisibility()) ||
335 GV->isDeclaration())
336 O << "@PLT";
337 }
338 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
339 // Save function name for later type emission
340 FnStubs.insert(Name);
341 }
342 }
343
344 if (GV->hasExternalWeakLinkage())
345 ExtWeakSymbols.insert(GV);
346
347 int Offset = MO.getOffset();
348 if (Offset > 0)
349 O << "+" << Offset;
350 else if (Offset < 0)
351 O << Offset;
352
353 if (isThreadLocal) {
354 if (TM.getRelocationModel() == Reloc::PIC_)
355 O << "@TLSGD"; // general dynamic TLS model
356 else
357 if (GV->isDeclaration())
358 O << "@INDNTPOFF"; // initial exec TLS model
359 else
360 O << "@NTPOFF"; // local exec TLS model
361 } else if (isMemOp) {
362 if (printGOT(TM, Subtarget)) {
363 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
364 O << "@GOT";
365 else
366 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000367 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
368 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000369 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 O << "@GOTPCREL";
371
372 if (needCloseParen) {
373 needCloseParen = false;
374 O << ')';
375 }
376
377 // Use rip when possible to reduce code size, except when
378 // index or base register are also part of the address. e.g.
379 // foo(%rip)(%rcx,%rax,4) is not legal
380 O << "(%rip)";
381 }
382 }
383
384 if (needCloseParen)
385 O << ')';
386
387 return;
388 }
389 case MachineOperand::MO_ExternalSymbol: {
390 bool isCallOp = Modifier && !strcmp(Modifier, "call");
391 bool needCloseParen = false;
392 std::string Name(TAI->getGlobalPrefix());
393 Name += MO.getSymbolName();
394 if (isCallOp && printStub(TM, Subtarget)) {
395 FnStubs.insert(Name);
396 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
397 return;
398 }
399 if (!isCallOp)
400 O << '$';
401 else if (Name[0] == '$') {
402 // The name begins with a dollar-sign. In order to avoid having it look
403 // like an integer immediate to the assembler, enclose it in parens.
404 O << '(';
405 needCloseParen = true;
406 }
407
408 O << Name;
409
410 if (printGOT(TM, Subtarget)) {
411 std::string GOTName(TAI->getGlobalPrefix());
412 GOTName+="_GLOBAL_OFFSET_TABLE_";
413 if (Name == GOTName)
414 // HACK! Emit extra offset to PC during printing GOT offset to
415 // compensate for the size of popl instruction. The resulting code
416 // should look like:
417 // call .piclabel
418 // piclabel:
419 // popl %some_register
420 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
421 O << " + [.-"
Evan Cheng0729ccf2008-01-05 00:41:47 +0000422 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
424 if (isCallOp)
425 O << "@PLT";
426 }
427
428 if (needCloseParen)
429 O << ')';
430
431 if (!isCallOp && Subtarget->isPICStyleRIPRel())
432 O << "(%rip)";
433
434 return;
435 }
436 default:
437 O << "<unknown operand type>"; return;
438 }
439}
440
441void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000442 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 assert(value <= 7 && "Invalid ssecc argument!");
444 switch (value) {
445 case 0: O << "eq"; break;
446 case 1: O << "lt"; break;
447 case 2: O << "le"; break;
448 case 3: O << "unord"; break;
449 case 4: O << "neq"; break;
450 case 5: O << "nlt"; break;
451 case 6: O << "nle"; break;
452 case 7: O << "ord"; break;
453 }
454}
455
456void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
457 const char *Modifier){
458 assert(isMem(MI, Op) && "Invalid memory reference!");
459 MachineOperand BaseReg = MI->getOperand(Op);
460 MachineOperand IndexReg = MI->getOperand(Op+2);
461 const MachineOperand &DispSpec = MI->getOperand(Op+3);
462
463 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
464 if (DispSpec.isGlobalAddress() ||
465 DispSpec.isConstantPoolIndex() ||
466 DispSpec.isJumpTableIndex()) {
467 printOperand(MI, Op+3, "mem", NotRIPRel);
468 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000469 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
471 O << DispVal;
472 }
473
474 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000475 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
477
478 // There are cases where we can end up with ESP/RSP in the indexreg slot.
479 // If this happens, swap the base/index register to support assemblers that
480 // don't work when the index is *SP.
481 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
482 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
483 std::swap(BaseReg, IndexReg);
484 std::swap(BaseRegOperand, IndexRegOperand);
485 }
486
487 O << "(";
488 if (BaseReg.getReg())
489 printOperand(MI, Op+BaseRegOperand, Modifier);
490
491 if (IndexReg.getReg()) {
492 O << ",";
493 printOperand(MI, Op+IndexRegOperand, Modifier);
494 if (ScaleVal != 1)
495 O << "," << ScaleVal;
496 }
497 O << ")";
498 }
499}
500
Evan Cheng6fb06762007-11-09 01:32:10 +0000501void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
502 const MachineBasicBlock *MBB) const {
503 if (!TAI->getSetDirective())
504 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000505
506 // We don't need .set machinery if we have GOT-style relocations
507 if (Subtarget->isPICStyleGOT())
508 return;
509
Evan Cheng6fb06762007-11-09 01:32:10 +0000510 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
511 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000512 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000513 if (Subtarget->isPICStyleRIPRel())
514 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
515 << '_' << uid << '\n';
516 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000517 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000518}
519
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000521 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 O << label << "\n" << label << ":";
523}
524
525
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000526void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
527 const MachineBasicBlock *MBB,
528 unsigned uid) const
529{
530 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
531 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
532
533 O << JTEntryDirective << ' ';
534
535 if (TM.getRelocationModel() == Reloc::PIC_) {
536 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
537 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
538 << '_' << uid << "_set_" << MBB->getNumber();
539 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000540 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000541 O << "@GOTOFF";
542 } else
543 assert(0 && "Don't know how to print MBB label for this PIC mode");
544 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000545 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000546}
547
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
549 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 unsigned Reg = MO.getReg();
551 switch (Mode) {
552 default: return true; // Unknown mode.
553 case 'b': // Print QImode register
554 Reg = getX86SubSuperRegister(Reg, MVT::i8);
555 break;
556 case 'h': // Print QImode high register
557 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
558 break;
559 case 'w': // Print HImode register
560 Reg = getX86SubSuperRegister(Reg, MVT::i16);
561 break;
562 case 'k': // Print SImode register
563 Reg = getX86SubSuperRegister(Reg, MVT::i32);
564 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000565 case 'q': // Print DImode register
566 Reg = getX86SubSuperRegister(Reg, MVT::i64);
567 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 }
569
570 O << '%';
Evan Cheng3c0eda52008-03-15 00:03:38 +0000571 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 O << (char)tolower(*Name);
573 return false;
574}
575
576/// PrintAsmOperand - Print out an operand for an inline asm expression.
577///
578bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
579 unsigned AsmVariant,
580 const char *ExtraCode) {
581 // Does this asm operand have a single letter operand modifier?
582 if (ExtraCode && ExtraCode[0]) {
583 if (ExtraCode[1] != 0) return true; // Unknown modifier.
584
585 switch (ExtraCode[0]) {
586 default: return true; // Unknown modifier.
587 case 'c': // Don't print "$" before a global var name or constant.
588 printOperand(MI, OpNo, "mem");
589 return false;
590 case 'b': // Print QImode register
591 case 'h': // Print QImode high register
592 case 'w': // Print HImode register
593 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000594 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000595 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
597 printOperand(MI, OpNo);
598 return false;
599
600 case 'P': // Don't print @PLT, but do print as memory.
601 printOperand(MI, OpNo, "mem");
602 return false;
603 }
604 }
605
606 printOperand(MI, OpNo);
607 return false;
608}
609
610bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
611 unsigned OpNo,
612 unsigned AsmVariant,
613 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000614 if (ExtraCode && ExtraCode[0]) {
615 if (ExtraCode[1] != 0) return true; // Unknown modifier.
616
617 switch (ExtraCode[0]) {
618 default: return true; // Unknown modifier.
619 case 'b': // Print QImode register
620 case 'h': // Print QImode high register
621 case 'w': // Print HImode register
622 case 'k': // Print SImode register
623 case 'q': // Print SImode register
624 // These only apply to registers, ignore on mem.
625 break;
626 }
627 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 printMemReference(MI, OpNo);
629 return false;
630}
631
632/// printMachineInstruction -- Print out a single X86 LLVM instruction
633/// MI in AT&T syntax to the current output stream.
634///
635void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
636 ++EmittedInsts;
637
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 // Call the autogenerated instruction printer routines.
639 printInstruction(MI);
640}
641
642// Include the auto-generated portion of the assembly writer.
643#include "X86GenAsmWriter.inc"
644