blob: ccc2965064de8d2ecbf318720153319e161ab203 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
18#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000019#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000020#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000021#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000022#include "llvm/CallingConv.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000023#include "llvm/Module.h"
24#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner95b2c7d2006-12-19 22:59:26 +000027#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000029
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
Chris Lattnerafbfded2006-10-05 02:43:52 +000032/// getSectionForFunction - Return the section that we should emit the
33/// specified function body into.
34std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
35 switch (F.getLinkage()) {
36 default: assert(0 && "Unknown linkage type!");
37 case Function::InternalLinkage:
38 case Function::DLLExportLinkage:
39 case Function::ExternalLinkage:
40 return TAI->getTextSection();
41 case Function::WeakLinkage:
42 case Function::LinkOnceLinkage:
43 if (Subtarget->isTargetDarwin()) {
44 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Anton Korobeynikov317848f2007-01-03 11:43:14 +000045 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovb1c88022006-10-18 09:12:29 +000046 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000047 } else {
48 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
49 ",\"ax\",@progbits\n";
50 }
51 }
52}
53
Chris Lattnerb36cbd02005-07-01 22:44:09 +000054/// runOnMachineFunction - This uses the printMachineInstruction()
55/// method to print assembly for each instruction.
56///
57bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +000058 if (Subtarget->isTargetDarwin() ||
59 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +000060 Subtarget->isTargetCygMing()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000061 // Let PassManager know we need debug information and relay
62 // the MachineDebugInfo address on to DwarfWriter.
63 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
64 }
Evan Cheng3c992d22006-03-07 02:02:57 +000065
Chris Lattner8b8b9512005-11-21 07:51:23 +000066 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000067 O << "\n\n";
68
69 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000070 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000071
72 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000073 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000074 unsigned CC = F->getCallingConv();
75
76 // Populate function information map. Actually, We don't want to populate
77 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000078 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
79 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000080
81 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
82
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +000083 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovcea9d1d2007-01-06 18:24:26 +000084
Evan Cheng2338c5c2006-02-07 08:38:37 +000085 switch (F->getLinkage()) {
86 default: assert(0 && "Unknown linkage type!");
87 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +000088 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
89 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000090 case Function::DLLExportLinkage:
91 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
92 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +000093 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +000094 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000095 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000096 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +000097 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +000098 case Function::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +000099 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +0000100 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000101 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000102 } else if (Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000103 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000104 O << "\t.linkonce discard\n";
105 O << "\t.globl " << CurrentFnName << "\n";
106 } else {
107 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
108 O << "\t.weak " << CurrentFnName << "\n";
109 }
110 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000111 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000112 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000113 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000114 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000115 (F->getLinkage() == Function::LinkOnceLinkage ||
116 F->getLinkage() == Function::WeakLinkage))
117 O << "_llvm$workaround$fake$stub_" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000118
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000119 if (Subtarget->isTargetDarwin() ||
120 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000121 Subtarget->isTargetCygMing()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000122 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000123 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000124 }
125
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000130 if (I->pred_begin() != I->pred_end()) {
131 printBasicBlockLabel(I, true);
132 O << '\n';
133 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000134 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
135 II != E; ++II) {
136 // Print the assembly for the instruction.
137 O << "\t";
138 printMachineInstruction(II);
139 }
140 }
Evan Cheng67afece2006-08-28 22:14:16 +0000141
Chris Lattner1da31ee2006-10-05 03:01:21 +0000142 // Print out jump tables referenced by the function.
143
144 // Mac OS X requires that the jump table follow the function, so that the jump
145 // table is part of the same atom that the function is in.
146 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Evan Cheng67afece2006-08-28 22:14:16 +0000147
Jim Laskey563321a2006-09-06 18:34:40 +0000148 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000149 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000150
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000151 if (Subtarget->isTargetDarwin() ||
152 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000153 Subtarget->isTargetCygMing()) {
Evan Chengd5948812006-03-07 02:23:26 +0000154 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000155 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000156 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000157
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000158 // We didn't modify anything.
159 return false;
160}
161
Chris Lattnera3b8c572006-02-06 23:41:19 +0000162void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000163 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000164 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165 const MRegisterInfo &RI = *TM.getRegisterInfo();
166 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000167 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000168 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
169 "Virtual registers should not make it this far!");
170 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000171 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000172 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
174 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
175 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000176 Reg = getX86SubSuperRegister(Reg, VT);
177 }
178 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000179 O << (char)tolower(*Name);
180 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000181 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000182
Chris Lattner63b3d712006-05-04 17:21:20 +0000183 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000184 if (!Modifier || strcmp(Modifier, "debug") != 0)
185 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000186 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000188 case MachineOperand::MO_MachineBasicBlock:
189 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000190 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000191 case MachineOperand::MO_JumpTableIndex: {
192 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
193 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000194 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000195 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000196 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000197 TM.getRelocationModel() == Reloc::PIC_)
198 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng28b514392006-12-05 19:50:18 +0000199 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000201 return;
202 }
Evan Chenga09bd812006-02-26 08:28:12 +0000203 case MachineOperand::MO_ConstantPoolIndex: {
204 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
205 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000206 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000207 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000209 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000210 O << "-\"L" << getFunctionNumber() << "$pb\"";
211 int Offset = MO.getOffset();
212 if (Offset > 0)
213 O << "+" << Offset;
214 else if (Offset < 0)
215 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000216
Evan Cheng28b514392006-12-05 19:50:18 +0000217 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000219 return;
220 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000221 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000222 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000223 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000224 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000225
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000226 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000228
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
230 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000231
Chris Lattnere87e1152006-09-26 03:57:53 +0000232 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000233
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000235 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000236 // Link-once, External, or Weakly-linked global variables need
237 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000239 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000241 FnStubs.insert(Name);
242 O << "L" << Name << "$stub";
243 } else {
244 GVStubs.insert(Name);
245 O << "L" << Name << "$non_lazy_ptr";
246 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000247 } else {
248 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000249 O << "__imp_";
250 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 }
253
Chris Lattner35d86fe2006-07-26 21:12:04 +0000254 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000255 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000256 } else {
257 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000258 O << "__imp_";
259 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000261 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000262
Evan Cheng6d7d3102006-12-01 07:38:23 +0000263 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000264 ExtWeakSymbols.insert(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000265
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000266 int Offset = MO.getOffset();
267 if (Offset > 0)
268 O << "+" << Offset;
269 else if (Offset < 0)
270 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000271
Evan Cheng35c1c042006-12-05 06:43:58 +0000272 if (isMemOp && Subtarget->is64Bit()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (isExt && TM.getRelocationModel() != Reloc::Static)
Evan Cheng28b514392006-12-05 19:50:18 +0000274 O << "@GOTPCREL(%rip)";
275 else if (!NotRIPRel)
276 // Use rip when possible to reduce code size, except when index or
277 // base register are also part of the address. e.g.
278 // foo(%rip)(%rcx,%rax,4) is not legal
279 O << "(%rip)";
Evan Cheng25ab6902006-09-08 06:48:29 +0000280 }
281
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 return;
283 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000284 case MachineOperand::MO_ExternalSymbol: {
285 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000286 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000288 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000289 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000290 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000291 FnStubs.insert(Name);
292 O << "L" << Name << "$stub";
293 return;
294 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000295 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000296 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000297
Evan Cheng35c1c042006-12-05 06:43:58 +0000298 if (!isCallOp && Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000299 O << "(%rip)";
300
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000302 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 default:
304 O << "<unknown operand type>"; return;
305 }
306}
307
Nate Begeman391c5d22005-11-30 18:54:35 +0000308void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000309 unsigned char value = MI->getOperand(Op).getImmedValue();
310 assert(value <= 7 && "Invalid ssecc argument!");
311 switch (value) {
312 case 0: O << "eq"; break;
313 case 1: O << "lt"; break;
314 case 2: O << "le"; break;
315 case 3: O << "unord"; break;
316 case 4: O << "neq"; break;
317 case 5: O << "nlt"; break;
318 case 6: O << "nle"; break;
319 case 7: O << "ord"; break;
320 }
321}
322
Evan Cheng25ab6902006-09-08 06:48:29 +0000323void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
324 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000325 assert(isMem(MI, Op) && "Invalid memory reference!");
326
327 const MachineOperand &BaseReg = MI->getOperand(Op);
328 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
329 const MachineOperand &IndexReg = MI->getOperand(Op+2);
330 const MachineOperand &DispSpec = MI->getOperand(Op+3);
331
332 if (BaseReg.isFrameIndex()) {
333 O << "[frame slot #" << BaseReg.getFrameIndex();
334 if (DispSpec.getImmedValue())
335 O << " + " << DispSpec.getImmedValue();
336 O << "]";
337 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000338 }
339
Evan Cheng28b514392006-12-05 19:50:18 +0000340 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000341 if (DispSpec.isGlobalAddress() ||
342 DispSpec.isConstantPoolIndex() ||
343 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000344 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000345 } else {
346 int DispVal = DispSpec.getImmedValue();
347 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
348 O << DispVal;
349 }
350
351 if (IndexReg.getReg() || BaseReg.getReg()) {
352 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000353 if (BaseReg.getReg()) {
354 printOperand(MI, Op, Modifier);
355 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000356
357 if (IndexReg.getReg()) {
358 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000360 if (ScaleVal != 1)
361 O << "," << ScaleVal;
362 }
363
364 O << ")";
365 }
366}
367
Evan Cheng7ccced62006-02-18 00:15:05 +0000368void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
369 O << "\"L" << getFunctionNumber() << "$pb\"\n";
370 O << "\"L" << getFunctionNumber() << "$pb\":";
371}
372
Evan Cheng62f27002006-04-28 23:11:40 +0000373
Evan Cheng55c25f22006-04-28 23:19:39 +0000374bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000375 const char Mode) {
376 const MRegisterInfo &RI = *TM.getRegisterInfo();
377 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000378 switch (Mode) {
379 default: return true; // Unknown mode.
380 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000381 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000382 break;
383 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000384 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000385 break;
386 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000387 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000388 break;
389 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000390 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000391 break;
392 }
393
Evan Cheng8f7f7122006-05-05 05:40:20 +0000394 O << '%';
395 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
396 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000397 return false;
398}
399
Evan Cheng3d48a902006-04-28 21:19:05 +0000400/// PrintAsmOperand - Print out an operand for an inline asm expression.
401///
402bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
403 unsigned AsmVariant,
404 const char *ExtraCode) {
405 // Does this asm operand have a single letter operand modifier?
406 if (ExtraCode && ExtraCode[0]) {
407 if (ExtraCode[1] != 0) return true; // Unknown modifier.
408
409 switch (ExtraCode[0]) {
410 default: return true; // Unknown modifier.
Chris Lattner0d924992006-10-31 20:12:30 +0000411 case 'c': // Don't print "$" before a global var name.
412 printOperand(MI, OpNo, "mem");
413 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000414 case 'b': // Print QImode register
415 case 'h': // Print QImode high register
416 case 'w': // Print HImode register
417 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000418 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000419 }
420 }
421
422 printOperand(MI, OpNo);
423 return false;
424}
425
426bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
427 unsigned OpNo,
428 unsigned AsmVariant,
429 const char *ExtraCode) {
430 if (ExtraCode && ExtraCode[0])
431 return true; // Unknown modifier.
432 printMemReference(MI, OpNo);
433 return false;
434}
435
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000436/// printMachineInstruction -- Print out a single X86 LLVM instruction
437/// MI in Intel syntax to the current output stream.
438///
439void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
440 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000441
Evan Cheng8f7f7122006-05-05 05:40:20 +0000442 // See if a truncate instruction can be turned into a nop.
443 switch (MI->getOpcode()) {
444 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000445 case X86::TRUNC_64to32:
446 case X86::TRUNC_64to16:
447 case X86::TRUNC_32to16:
448 case X86::TRUNC_32to8:
449 case X86::TRUNC_16to8:
450 case X86::TRUNC_32_to8:
451 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000452 const MachineOperand &MO0 = MI->getOperand(0);
453 const MachineOperand &MO1 = MI->getOperand(1);
454 unsigned Reg0 = MO0.getReg();
455 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 unsigned Opc = MI->getOpcode();
457 if (Opc == X86::TRUNC_64to32)
458 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
459 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000460 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000461 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000462 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000463 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000464 if (Reg0 != Reg1)
465 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000466 break;
467 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 case X86::PsMOVZX64rr32:
469 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
470 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000471 }
472
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000473 // Call the autogenerated instruction printer routines.
474 printInstruction(MI);
475}
476
477// Include the auto-generated portion of the assembly writer.
478#include "X86GenAsmWriter.inc"
479