blob: 6f34ca5c1341584d309587ef52e7de88468fa6af [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
16#include "X86ATTAsmPrinter.h"
17#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000019#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000020#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000021#include "llvm/CallingConv.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000022#include "llvm/Module.h"
23#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000026#include <iostream>
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028
29/// runOnMachineFunction - This uses the printMachineInstruction()
30/// method to print assembly for each instruction.
31///
32bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyea348582006-07-27 02:05:13 +000033 if (Subtarget->isTargetDarwin()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000034 // Let PassManager know we need debug information and relay
35 // the MachineDebugInfo address on to DwarfWriter.
36 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
37 }
Evan Cheng3c992d22006-03-07 02:02:57 +000038
Chris Lattner8b8b9512005-11-21 07:51:23 +000039 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040 O << "\n\n";
41
42 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000043 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000044
45 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000046 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000047 unsigned CC = F->getCallingConv();
48
49 // Populate function information map. Actually, We don't want to populate
50 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000051 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
52 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000053
54 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
55
Evan Cheng2338c5c2006-02-07 08:38:37 +000056 switch (F->getLinkage()) {
57 default: assert(0 && "Unknown linkage type!");
58 case Function::InternalLinkage: // Symbols default to internal.
Jim Laskey563321a2006-09-06 18:34:40 +000059 SwitchToTextSection(TAI->getTextSection(), F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000060 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
61 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000062 case Function::DLLExportLinkage:
63 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
64 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +000065 case Function::ExternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +000066 SwitchToTextSection(TAI->getTextSection(), F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000067 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000068 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000069 break;
70 case Function::WeakLinkage:
71 case Function::LinkOnceLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +000072 if (Subtarget->isTargetDarwin()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +000073 SwitchToTextSection(
74 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
Evan Chengf1616da2006-02-22 23:59:57 +000075 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000076 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikovbcb97702006-09-17 20:25:45 +000077 } else if (Subtarget->isTargetCygwin()) {
Evan Cheng932ad512006-05-25 21:59:08 +000078 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
79 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
80 << ",\"ax\"\n";
81 SwitchToTextSection("", F);
82 O << "\t.weak " << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000083 } else {
84 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
85 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
86 << ",\"ax\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +000087 SwitchToTextSection("", F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000088 O << "\t.weak " << CurrentFnName << "\n";
89 }
90 break;
91 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000092 O << CurrentFnName << ":\n";
93
Jim Laskeyea348582006-07-27 02:05:13 +000094 if (Subtarget->isTargetDarwin()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +000095 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +000096 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +000097 }
98
Chris Lattnerb36cbd02005-07-01 22:44:09 +000099 // Print out code for the function.
100 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
101 I != E; ++I) {
102 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000103 if (I->pred_begin() != I->pred_end()) {
104 printBasicBlockLabel(I, true);
105 O << '\n';
106 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000107 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
108 II != E; ++II) {
109 // Print the assembly for the instruction.
110 O << "\t";
111 printMachineInstruction(II);
112 }
113 }
Evan Cheng67afece2006-08-28 22:14:16 +0000114
115 // Print out jump tables referenced by the function
116 // Mac OS X requires at least one non-local (e.g. L1) labels before local
117 // lables that are used in jump table expressions (e.g. LBB1_1-LJT1_0).
118 EmitJumpTableInfo(MF.getJumpTableInfo());
119
Jim Laskey563321a2006-09-06 18:34:40 +0000120 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000121 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122
Jim Laskeyea348582006-07-27 02:05:13 +0000123 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000124 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000125 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000126 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000127
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000128 // We didn't modify anything.
129 return false;
130}
131
Chris Lattnera3b8c572006-02-06 23:41:19 +0000132void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
133 const char *Modifier) {
134 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000135 const MRegisterInfo &RI = *TM.getRegisterInfo();
136 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000137 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000138 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
139 "Virtual registers should not make it this far!");
140 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000141 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000142 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
144 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
145 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000146 Reg = getX86SubSuperRegister(Reg, VT);
147 }
148 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000149 O << (char)tolower(*Name);
150 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000151 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000152
Chris Lattner63b3d712006-05-04 17:21:20 +0000153 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000154 if (!Modifier || strcmp(Modifier, "debug") != 0)
155 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000156 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000157 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000158 case MachineOperand::MO_MachineBasicBlock:
159 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000160 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000161 case MachineOperand::MO_JumpTableIndex: {
162 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
163 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000164 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000165 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000167 TM.getRelocationModel() == Reloc::PIC_)
168 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 if (Subtarget->is64Bit())
170 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000171 return;
172 }
Evan Chenga09bd812006-02-26 08:28:12 +0000173 case MachineOperand::MO_ConstantPoolIndex: {
174 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
175 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000176 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000177 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000178 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000179 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000180 O << "-\"L" << getFunctionNumber() << "$pb\"";
181 int Offset = MO.getOffset();
182 if (Offset > 0)
183 O << "+" << Offset;
184 else if (Offset < 0)
185 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000186
187 if (Subtarget->is64Bit())
188 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000189 return;
190 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000191 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000192 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000193 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000194 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000195
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000196 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000197 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000198
Evan Cheng25ab6902006-09-08 06:48:29 +0000199 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
200 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000201
Chris Lattnere87e1152006-09-26 03:57:53 +0000202 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000203
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000205 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000206 // Link-once, External, or Weakly-linked global variables need
207 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000209 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000210 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000211 FnStubs.insert(Name);
212 O << "L" << Name << "$stub";
213 } else {
214 GVStubs.insert(Name);
215 O << "L" << Name << "$non_lazy_ptr";
216 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000217 } else {
218 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000219 O << "__imp_";
220 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000222 }
223
Chris Lattner35d86fe2006-07-26 21:12:04 +0000224 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000225 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000226 } else {
227 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000228 O << "__imp_";
229 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000231 }
232
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000233 int Offset = MO.getOffset();
234 if (Offset > 0)
235 O << "+" << Offset;
236 else if (Offset < 0)
237 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000238
239 if (!isCallOp &&
240 Subtarget->is64Bit()) {
241 if (isExt && TM.getRelocationModel() != Reloc::Static)
242 O << "@GOTPCREL";
243 O << "(%rip)";
244 }
245
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000246 return;
247 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000248 case MachineOperand::MO_ExternalSymbol: {
249 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000250 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000252 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000253 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000254 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000255 FnStubs.insert(Name);
256 O << "L" << Name << "$stub";
257 return;
258 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000259 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000260 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000261
262 if (!isCallOp &&
263 Subtarget->is64Bit())
264 O << "(%rip)";
265
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000266 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000267 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000268 default:
269 O << "<unknown operand type>"; return;
270 }
271}
272
Nate Begeman391c5d22005-11-30 18:54:35 +0000273void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000274 unsigned char value = MI->getOperand(Op).getImmedValue();
275 assert(value <= 7 && "Invalid ssecc argument!");
276 switch (value) {
277 case 0: O << "eq"; break;
278 case 1: O << "lt"; break;
279 case 2: O << "le"; break;
280 case 3: O << "unord"; break;
281 case 4: O << "neq"; break;
282 case 5: O << "nlt"; break;
283 case 6: O << "nle"; break;
284 case 7: O << "ord"; break;
285 }
286}
287
Evan Cheng25ab6902006-09-08 06:48:29 +0000288void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
289 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000290 assert(isMem(MI, Op) && "Invalid memory reference!");
291
292 const MachineOperand &BaseReg = MI->getOperand(Op);
293 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
294 const MachineOperand &IndexReg = MI->getOperand(Op+2);
295 const MachineOperand &DispSpec = MI->getOperand(Op+3);
296
297 if (BaseReg.isFrameIndex()) {
298 O << "[frame slot #" << BaseReg.getFrameIndex();
299 if (DispSpec.getImmedValue())
300 O << " + " << DispSpec.getImmedValue();
301 O << "]";
302 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 }
304
Evan Chengc9676de2006-08-29 22:14:48 +0000305 if (DispSpec.isGlobalAddress() ||
306 DispSpec.isConstantPoolIndex() ||
307 DispSpec.isJumpTableIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000308 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000309 } else {
310 int DispVal = DispSpec.getImmedValue();
311 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
312 O << DispVal;
313 }
314
315 if (IndexReg.getReg() || BaseReg.getReg()) {
316 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 if (BaseReg.getReg()) {
318 printOperand(MI, Op, Modifier);
319 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000320
321 if (IndexReg.getReg()) {
322 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000324 if (ScaleVal != 1)
325 O << "," << ScaleVal;
326 }
327
328 O << ")";
329 }
330}
331
Evan Cheng7ccced62006-02-18 00:15:05 +0000332void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
333 O << "\"L" << getFunctionNumber() << "$pb\"\n";
334 O << "\"L" << getFunctionNumber() << "$pb\":";
335}
336
Evan Cheng62f27002006-04-28 23:11:40 +0000337
Evan Cheng55c25f22006-04-28 23:19:39 +0000338bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000339 const char Mode) {
340 const MRegisterInfo &RI = *TM.getRegisterInfo();
341 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000342 switch (Mode) {
343 default: return true; // Unknown mode.
344 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000345 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000346 break;
347 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000348 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000349 break;
350 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000351 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000352 break;
353 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000354 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000355 break;
356 }
357
Evan Cheng8f7f7122006-05-05 05:40:20 +0000358 O << '%';
359 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
360 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000361 return false;
362}
363
Evan Cheng3d48a902006-04-28 21:19:05 +0000364/// PrintAsmOperand - Print out an operand for an inline asm expression.
365///
366bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
367 unsigned AsmVariant,
368 const char *ExtraCode) {
369 // Does this asm operand have a single letter operand modifier?
370 if (ExtraCode && ExtraCode[0]) {
371 if (ExtraCode[1] != 0) return true; // Unknown modifier.
372
373 switch (ExtraCode[0]) {
374 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000375 case 'b': // Print QImode register
376 case 'h': // Print QImode high register
377 case 'w': // Print HImode register
378 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000379 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000380 }
381 }
382
383 printOperand(MI, OpNo);
384 return false;
385}
386
387bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
388 unsigned OpNo,
389 unsigned AsmVariant,
390 const char *ExtraCode) {
391 if (ExtraCode && ExtraCode[0])
392 return true; // Unknown modifier.
393 printMemReference(MI, OpNo);
394 return false;
395}
396
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000397/// printMachineInstruction -- Print out a single X86 LLVM instruction
398/// MI in Intel syntax to the current output stream.
399///
400void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
401 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000402
Evan Cheng8f7f7122006-05-05 05:40:20 +0000403 // See if a truncate instruction can be turned into a nop.
404 switch (MI->getOpcode()) {
405 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000406 case X86::TRUNC_64to32:
407 case X86::TRUNC_64to16:
408 case X86::TRUNC_32to16:
409 case X86::TRUNC_32to8:
410 case X86::TRUNC_16to8:
411 case X86::TRUNC_32_to8:
412 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000413 const MachineOperand &MO0 = MI->getOperand(0);
414 const MachineOperand &MO1 = MI->getOperand(1);
415 unsigned Reg0 = MO0.getReg();
416 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 unsigned Opc = MI->getOpcode();
418 if (Opc == X86::TRUNC_64to32)
419 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
420 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000421 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000422 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000423 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000424 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000425 if (Reg0 != Reg1)
426 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000427 break;
428 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 case X86::PsMOVZX64rr32:
430 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
431 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000432 }
433
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000434 // Call the autogenerated instruction printer routines.
435 printInstruction(MI);
436}
437
438// Include the auto-generated portion of the assembly writer.
439#include "X86GenAsmWriter.inc"
440