blob: d71bc784df5199fa860dc2f75b9a4438f7cacae6 [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 Korobeynikovcea9d1d2007-01-06 18:24:26 +000083 // Change GNU linkonce to LLVM linkonce name
84 if (F->hasSection() &&
85 (F->getSection().find(".gnu.linkonce.t") != std::string::npos))
86 SwitchToTextSection(getSectionForFunction(*F).c_str(), NULL);
87 else
88 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
89
Evan Cheng2338c5c2006-02-07 08:38:37 +000090 switch (F->getLinkage()) {
91 default: assert(0 && "Unknown linkage type!");
92 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +000093 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
94 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000095 case Function::DLLExportLinkage:
96 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
97 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +000098 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +000099 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000100 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000101 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000102 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000103 case Function::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000104 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +0000105 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000106 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000107 } else if (Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000108 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000109 O << "\t.linkonce discard\n";
110 O << "\t.globl " << CurrentFnName << "\n";
111 } else {
112 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
113 O << "\t.weak " << CurrentFnName << "\n";
114 }
115 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000116 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000117 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000118 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000119 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000120 (F->getLinkage() == Function::LinkOnceLinkage ||
121 F->getLinkage() == Function::WeakLinkage))
122 O << "_llvm$workaround$fake$stub_" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000123
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000124 if (Subtarget->isTargetDarwin() ||
125 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000126 Subtarget->isTargetCygMing()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000127 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000128 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000129 }
130
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131 // Print out code for the function.
132 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
133 I != E; ++I) {
134 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000135 if (I->pred_begin() != I->pred_end()) {
136 printBasicBlockLabel(I, true);
137 O << '\n';
138 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000139 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
140 II != E; ++II) {
141 // Print the assembly for the instruction.
142 O << "\t";
143 printMachineInstruction(II);
144 }
145 }
Evan Cheng67afece2006-08-28 22:14:16 +0000146
Chris Lattner1da31ee2006-10-05 03:01:21 +0000147 // Print out jump tables referenced by the function.
148
149 // Mac OS X requires that the jump table follow the function, so that the jump
150 // table is part of the same atom that the function is in.
151 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Evan Cheng67afece2006-08-28 22:14:16 +0000152
Jim Laskey563321a2006-09-06 18:34:40 +0000153 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000154 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000155
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000156 if (Subtarget->isTargetDarwin() ||
157 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000158 Subtarget->isTargetCygMing()) {
Evan Chengd5948812006-03-07 02:23:26 +0000159 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000160 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000161 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000162
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000163 // We didn't modify anything.
164 return false;
165}
166
Chris Lattnera3b8c572006-02-06 23:41:19 +0000167void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000168 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000169 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000170 const MRegisterInfo &RI = *TM.getRegisterInfo();
171 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000172 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000173 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
174 "Virtual registers should not make it this far!");
175 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000176 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000177 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000178 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
179 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
180 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000181 Reg = getX86SubSuperRegister(Reg, VT);
182 }
183 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 O << (char)tolower(*Name);
185 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000186 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187
Chris Lattner63b3d712006-05-04 17:21:20 +0000188 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000189 if (!Modifier || strcmp(Modifier, "debug") != 0)
190 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000191 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000192 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000193 case MachineOperand::MO_MachineBasicBlock:
194 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000195 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000196 case MachineOperand::MO_JumpTableIndex: {
197 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
198 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000199 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000200 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000202 TM.getRelocationModel() == Reloc::PIC_)
203 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng28b514392006-12-05 19:50:18 +0000204 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000206 return;
207 }
Evan Chenga09bd812006-02-26 08:28:12 +0000208 case MachineOperand::MO_ConstantPoolIndex: {
209 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
210 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000211 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000212 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000214 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000215 O << "-\"L" << getFunctionNumber() << "$pb\"";
216 int Offset = MO.getOffset();
217 if (Offset > 0)
218 O << "+" << Offset;
219 else if (Offset < 0)
220 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000221
Evan Cheng28b514392006-12-05 19:50:18 +0000222 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000224 return;
225 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000226 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000227 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000228 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000229 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000230
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000231 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000233
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
235 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000236
Chris Lattnere87e1152006-09-26 03:57:53 +0000237 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000238
Evan Cheng25ab6902006-09-08 06:48:29 +0000239 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000240 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000241 // Link-once, External, or Weakly-linked global variables need
242 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000244 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000246 FnStubs.insert(Name);
247 O << "L" << Name << "$stub";
248 } else {
249 GVStubs.insert(Name);
250 O << "L" << Name << "$non_lazy_ptr";
251 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 } else {
253 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000254 O << "__imp_";
255 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000257 }
258
Chris Lattner35d86fe2006-07-26 21:12:04 +0000259 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000260 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000261 } else {
262 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000263 O << "__imp_";
264 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000266 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000267
Evan Cheng6d7d3102006-12-01 07:38:23 +0000268 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000269 ExtWeakSymbols.insert(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000270
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000271 int Offset = MO.getOffset();
272 if (Offset > 0)
273 O << "+" << Offset;
274 else if (Offset < 0)
275 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000276
Evan Cheng35c1c042006-12-05 06:43:58 +0000277 if (isMemOp && Subtarget->is64Bit()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000278 if (isExt && TM.getRelocationModel() != Reloc::Static)
Evan Cheng28b514392006-12-05 19:50:18 +0000279 O << "@GOTPCREL(%rip)";
280 else if (!NotRIPRel)
281 // Use rip when possible to reduce code size, except when index or
282 // base register are also part of the address. e.g.
283 // foo(%rip)(%rcx,%rax,4) is not legal
284 O << "(%rip)";
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 }
286
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000287 return;
288 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000289 case MachineOperand::MO_ExternalSymbol: {
290 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000291 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000293 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000294 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000295 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000296 FnStubs.insert(Name);
297 O << "L" << Name << "$stub";
298 return;
299 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000300 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000301 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000302
Evan Cheng35c1c042006-12-05 06:43:58 +0000303 if (!isCallOp && Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000304 O << "(%rip)";
305
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000307 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000308 default:
309 O << "<unknown operand type>"; return;
310 }
311}
312
Nate Begeman391c5d22005-11-30 18:54:35 +0000313void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000314 unsigned char value = MI->getOperand(Op).getImmedValue();
315 assert(value <= 7 && "Invalid ssecc argument!");
316 switch (value) {
317 case 0: O << "eq"; break;
318 case 1: O << "lt"; break;
319 case 2: O << "le"; break;
320 case 3: O << "unord"; break;
321 case 4: O << "neq"; break;
322 case 5: O << "nlt"; break;
323 case 6: O << "nle"; break;
324 case 7: O << "ord"; break;
325 }
326}
327
Evan Cheng25ab6902006-09-08 06:48:29 +0000328void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
329 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000330 assert(isMem(MI, Op) && "Invalid memory reference!");
331
332 const MachineOperand &BaseReg = MI->getOperand(Op);
333 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
334 const MachineOperand &IndexReg = MI->getOperand(Op+2);
335 const MachineOperand &DispSpec = MI->getOperand(Op+3);
336
337 if (BaseReg.isFrameIndex()) {
338 O << "[frame slot #" << BaseReg.getFrameIndex();
339 if (DispSpec.getImmedValue())
340 O << " + " << DispSpec.getImmedValue();
341 O << "]";
342 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000343 }
344
Evan Cheng28b514392006-12-05 19:50:18 +0000345 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000346 if (DispSpec.isGlobalAddress() ||
347 DispSpec.isConstantPoolIndex() ||
348 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000349 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000350 } else {
351 int DispVal = DispSpec.getImmedValue();
352 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
353 O << DispVal;
354 }
355
356 if (IndexReg.getReg() || BaseReg.getReg()) {
357 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000358 if (BaseReg.getReg()) {
359 printOperand(MI, Op, Modifier);
360 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000361
362 if (IndexReg.getReg()) {
363 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000364 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000365 if (ScaleVal != 1)
366 O << "," << ScaleVal;
367 }
368
369 O << ")";
370 }
371}
372
Evan Cheng7ccced62006-02-18 00:15:05 +0000373void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
374 O << "\"L" << getFunctionNumber() << "$pb\"\n";
375 O << "\"L" << getFunctionNumber() << "$pb\":";
376}
377
Evan Cheng62f27002006-04-28 23:11:40 +0000378
Evan Cheng55c25f22006-04-28 23:19:39 +0000379bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000380 const char Mode) {
381 const MRegisterInfo &RI = *TM.getRegisterInfo();
382 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000383 switch (Mode) {
384 default: return true; // Unknown mode.
385 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000386 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000387 break;
388 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000389 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000390 break;
391 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000392 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000393 break;
394 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000395 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000396 break;
397 }
398
Evan Cheng8f7f7122006-05-05 05:40:20 +0000399 O << '%';
400 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
401 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000402 return false;
403}
404
Evan Cheng3d48a902006-04-28 21:19:05 +0000405/// PrintAsmOperand - Print out an operand for an inline asm expression.
406///
407bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
408 unsigned AsmVariant,
409 const char *ExtraCode) {
410 // Does this asm operand have a single letter operand modifier?
411 if (ExtraCode && ExtraCode[0]) {
412 if (ExtraCode[1] != 0) return true; // Unknown modifier.
413
414 switch (ExtraCode[0]) {
415 default: return true; // Unknown modifier.
Chris Lattner0d924992006-10-31 20:12:30 +0000416 case 'c': // Don't print "$" before a global var name.
417 printOperand(MI, OpNo, "mem");
418 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000419 case 'b': // Print QImode register
420 case 'h': // Print QImode high register
421 case 'w': // Print HImode register
422 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000423 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000424 }
425 }
426
427 printOperand(MI, OpNo);
428 return false;
429}
430
431bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
432 unsigned OpNo,
433 unsigned AsmVariant,
434 const char *ExtraCode) {
435 if (ExtraCode && ExtraCode[0])
436 return true; // Unknown modifier.
437 printMemReference(MI, OpNo);
438 return false;
439}
440
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000441/// printMachineInstruction -- Print out a single X86 LLVM instruction
442/// MI in Intel syntax to the current output stream.
443///
444void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
445 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000446
Evan Cheng8f7f7122006-05-05 05:40:20 +0000447 // See if a truncate instruction can be turned into a nop.
448 switch (MI->getOpcode()) {
449 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 case X86::TRUNC_64to32:
451 case X86::TRUNC_64to16:
452 case X86::TRUNC_32to16:
453 case X86::TRUNC_32to8:
454 case X86::TRUNC_16to8:
455 case X86::TRUNC_32_to8:
456 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000457 const MachineOperand &MO0 = MI->getOperand(0);
458 const MachineOperand &MO1 = MI->getOperand(1);
459 unsigned Reg0 = MO0.getReg();
460 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000461 unsigned Opc = MI->getOpcode();
462 if (Opc == X86::TRUNC_64to32)
463 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
464 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000465 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000466 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000467 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000468 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000469 if (Reg0 != Reg1)
470 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000471 break;
472 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000473 case X86::PsMOVZX64rr32:
474 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
475 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000476 }
477
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000478 // Call the autogenerated instruction printer routines.
479 printInstruction(MI);
480}
481
482// Include the auto-generated portion of the assembly writer.
483#include "X86GenAsmWriter.inc"
484