blob: eef95d935d1dde7dd011a7080767626394132579 [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
Chris Lattnerafbfded2006-10-05 02:43:52 +000029/// getSectionForFunction - Return the section that we should emit the
30/// specified function body into.
31std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
32 switch (F.getLinkage()) {
33 default: assert(0 && "Unknown linkage type!");
34 case Function::InternalLinkage:
35 case Function::DLLExportLinkage:
36 case Function::ExternalLinkage:
37 return TAI->getTextSection();
38 case Function::WeakLinkage:
39 case Function::LinkOnceLinkage:
40 if (Subtarget->isTargetDarwin()) {
41 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
42 } else if (Subtarget->isTargetCygwin()) {
43 return "\t.section\t.llvm.linkonce.t." + CurrentFnName + ",\"ax\"\n";
44 } else {
45 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
46 ",\"ax\",@progbits\n";
47 }
48 }
49}
50
Chris Lattnerb36cbd02005-07-01 22:44:09 +000051/// runOnMachineFunction - This uses the printMachineInstruction()
52/// method to print assembly for each instruction.
53///
54bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Jim Laskeyea348582006-07-27 02:05:13 +000055 if (Subtarget->isTargetDarwin()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000056 // Let PassManager know we need debug information and relay
57 // the MachineDebugInfo address on to DwarfWriter.
58 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
59 }
Evan Cheng3c992d22006-03-07 02:02:57 +000060
Chris Lattner8b8b9512005-11-21 07:51:23 +000061 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000062 O << "\n\n";
63
64 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000065 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000066
67 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000068 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000069 unsigned CC = F->getCallingConv();
70
71 // Populate function information map. Actually, We don't want to populate
72 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000073 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
74 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000075
76 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
77
Chris Lattnerafbfded2006-10-05 02:43:52 +000078 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
79
Evan Cheng2338c5c2006-02-07 08:38:37 +000080 switch (F->getLinkage()) {
81 default: assert(0 && "Unknown linkage type!");
82 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +000083 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
84 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000085 case Function::DLLExportLinkage:
86 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
87 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +000088 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +000089 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000090 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000091 break;
92 case Function::WeakLinkage:
93 case Function::LinkOnceLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +000094 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +000095 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000096 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikovbcb97702006-09-17 20:25:45 +000097 } else if (Subtarget->isTargetCygwin()) {
Evan Cheng932ad512006-05-25 21:59:08 +000098 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Evan Cheng932ad512006-05-25 21:59:08 +000099 O << "\t.weak " << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000100 } else {
101 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000102 O << "\t.weak " << CurrentFnName << "\n";
103 }
104 break;
105 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000106 O << CurrentFnName << ":\n";
107
Jim Laskeyea348582006-07-27 02:05:13 +0000108 if (Subtarget->isTargetDarwin()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000109 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000110 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000111 }
112
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000113 // Print out code for the function.
114 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
115 I != E; ++I) {
116 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000117 if (I->pred_begin() != I->pred_end()) {
118 printBasicBlockLabel(I, true);
119 O << '\n';
120 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000121 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
122 II != E; ++II) {
123 // Print the assembly for the instruction.
124 O << "\t";
125 printMachineInstruction(II);
126 }
127 }
Evan Cheng67afece2006-08-28 22:14:16 +0000128
Chris Lattner1da31ee2006-10-05 03:01:21 +0000129 // Print out jump tables referenced by the function.
130
131 // Mac OS X requires that the jump table follow the function, so that the jump
132 // table is part of the same atom that the function is in.
133 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Evan Cheng67afece2006-08-28 22:14:16 +0000134
Jim Laskey563321a2006-09-06 18:34:40 +0000135 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000136 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000137
Jim Laskeyea348582006-07-27 02:05:13 +0000138 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000139 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000140 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000141 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000142
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000143 // We didn't modify anything.
144 return false;
145}
146
Chris Lattnera3b8c572006-02-06 23:41:19 +0000147void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
148 const char *Modifier) {
149 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000150 const MRegisterInfo &RI = *TM.getRegisterInfo();
151 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000152 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000153 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
154 "Virtual registers should not make it this far!");
155 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000156 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000157 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
159 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
160 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000161 Reg = getX86SubSuperRegister(Reg, VT);
162 }
163 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164 O << (char)tolower(*Name);
165 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000166 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000167
Chris Lattner63b3d712006-05-04 17:21:20 +0000168 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000169 if (!Modifier || strcmp(Modifier, "debug") != 0)
170 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000171 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000172 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000173 case MachineOperand::MO_MachineBasicBlock:
174 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000176 case MachineOperand::MO_JumpTableIndex: {
177 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
178 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000179 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000180 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000181 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000182 TM.getRelocationModel() == Reloc::PIC_)
183 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng25ab6902006-09-08 06:48:29 +0000184 if (Subtarget->is64Bit())
185 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000186 return;
187 }
Evan Chenga09bd812006-02-26 08:28:12 +0000188 case MachineOperand::MO_ConstantPoolIndex: {
189 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
190 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000191 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000192 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000194 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000195 O << "-\"L" << getFunctionNumber() << "$pb\"";
196 int Offset = MO.getOffset();
197 if (Offset > 0)
198 O << "+" << Offset;
199 else if (Offset < 0)
200 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000201
202 if (Subtarget->is64Bit())
203 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000204 return;
205 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000207 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000208 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000209 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000210
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000211 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000213
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
215 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000216
Chris Lattnere87e1152006-09-26 03:57:53 +0000217 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000218
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000220 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000221 // Link-once, External, or Weakly-linked global variables need
222 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000224 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000226 FnStubs.insert(Name);
227 O << "L" << Name << "$stub";
228 } else {
229 GVStubs.insert(Name);
230 O << "L" << Name << "$non_lazy_ptr";
231 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000232 } else {
233 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000234 O << "__imp_";
235 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000236 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000237 }
238
Chris Lattner35d86fe2006-07-26 21:12:04 +0000239 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000240 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000241 } else {
242 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000243 O << "__imp_";
244 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000246 }
247
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000248 int Offset = MO.getOffset();
249 if (Offset > 0)
250 O << "+" << Offset;
251 else if (Offset < 0)
252 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000253
254 if (!isCallOp &&
255 Subtarget->is64Bit()) {
256 if (isExt && TM.getRelocationModel() != Reloc::Static)
257 O << "@GOTPCREL";
258 O << "(%rip)";
259 }
260
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261 return;
262 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000263 case MachineOperand::MO_ExternalSymbol: {
264 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000265 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000267 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000268 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000269 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000270 FnStubs.insert(Name);
271 O << "L" << Name << "$stub";
272 return;
273 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000274 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000275 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000276
277 if (!isCallOp &&
278 Subtarget->is64Bit())
279 O << "(%rip)";
280
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000281 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000282 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000283 default:
284 O << "<unknown operand type>"; return;
285 }
286}
287
Nate Begeman391c5d22005-11-30 18:54:35 +0000288void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000289 unsigned char value = MI->getOperand(Op).getImmedValue();
290 assert(value <= 7 && "Invalid ssecc argument!");
291 switch (value) {
292 case 0: O << "eq"; break;
293 case 1: O << "lt"; break;
294 case 2: O << "le"; break;
295 case 3: O << "unord"; break;
296 case 4: O << "neq"; break;
297 case 5: O << "nlt"; break;
298 case 6: O << "nle"; break;
299 case 7: O << "ord"; break;
300 }
301}
302
Evan Cheng25ab6902006-09-08 06:48:29 +0000303void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
304 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305 assert(isMem(MI, Op) && "Invalid memory reference!");
306
307 const MachineOperand &BaseReg = MI->getOperand(Op);
308 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
309 const MachineOperand &IndexReg = MI->getOperand(Op+2);
310 const MachineOperand &DispSpec = MI->getOperand(Op+3);
311
312 if (BaseReg.isFrameIndex()) {
313 O << "[frame slot #" << BaseReg.getFrameIndex();
314 if (DispSpec.getImmedValue())
315 O << " + " << DispSpec.getImmedValue();
316 O << "]";
317 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318 }
319
Evan Chengc9676de2006-08-29 22:14:48 +0000320 if (DispSpec.isGlobalAddress() ||
321 DispSpec.isConstantPoolIndex() ||
322 DispSpec.isJumpTableIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000323 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000324 } else {
325 int DispVal = DispSpec.getImmedValue();
326 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
327 O << DispVal;
328 }
329
330 if (IndexReg.getReg() || BaseReg.getReg()) {
331 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 if (BaseReg.getReg()) {
333 printOperand(MI, Op, Modifier);
334 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335
336 if (IndexReg.getReg()) {
337 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000338 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000339 if (ScaleVal != 1)
340 O << "," << ScaleVal;
341 }
342
343 O << ")";
344 }
345}
346
Evan Cheng7ccced62006-02-18 00:15:05 +0000347void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
348 O << "\"L" << getFunctionNumber() << "$pb\"\n";
349 O << "\"L" << getFunctionNumber() << "$pb\":";
350}
351
Evan Cheng62f27002006-04-28 23:11:40 +0000352
Evan Cheng55c25f22006-04-28 23:19:39 +0000353bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000354 const char Mode) {
355 const MRegisterInfo &RI = *TM.getRegisterInfo();
356 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000357 switch (Mode) {
358 default: return true; // Unknown mode.
359 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000360 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000361 break;
362 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000363 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000364 break;
365 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000366 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000367 break;
368 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000369 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000370 break;
371 }
372
Evan Cheng8f7f7122006-05-05 05:40:20 +0000373 O << '%';
374 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
375 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000376 return false;
377}
378
Evan Cheng3d48a902006-04-28 21:19:05 +0000379/// PrintAsmOperand - Print out an operand for an inline asm expression.
380///
381bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
382 unsigned AsmVariant,
383 const char *ExtraCode) {
384 // Does this asm operand have a single letter operand modifier?
385 if (ExtraCode && ExtraCode[0]) {
386 if (ExtraCode[1] != 0) return true; // Unknown modifier.
387
388 switch (ExtraCode[0]) {
389 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000390 case 'b': // Print QImode register
391 case 'h': // Print QImode high register
392 case 'w': // Print HImode register
393 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000394 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000395 }
396 }
397
398 printOperand(MI, OpNo);
399 return false;
400}
401
402bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
403 unsigned OpNo,
404 unsigned AsmVariant,
405 const char *ExtraCode) {
406 if (ExtraCode && ExtraCode[0])
407 return true; // Unknown modifier.
408 printMemReference(MI, OpNo);
409 return false;
410}
411
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000412/// printMachineInstruction -- Print out a single X86 LLVM instruction
413/// MI in Intel syntax to the current output stream.
414///
415void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
416 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000417
Evan Cheng8f7f7122006-05-05 05:40:20 +0000418 // See if a truncate instruction can be turned into a nop.
419 switch (MI->getOpcode()) {
420 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 case X86::TRUNC_64to32:
422 case X86::TRUNC_64to16:
423 case X86::TRUNC_32to16:
424 case X86::TRUNC_32to8:
425 case X86::TRUNC_16to8:
426 case X86::TRUNC_32_to8:
427 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000428 const MachineOperand &MO0 = MI->getOperand(0);
429 const MachineOperand &MO1 = MI->getOperand(1);
430 unsigned Reg0 = MO0.getReg();
431 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 unsigned Opc = MI->getOpcode();
433 if (Opc == X86::TRUNC_64to32)
434 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
435 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000436 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000437 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000438 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000439 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000440 if (Reg0 != Reg1)
441 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000442 break;
443 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 case X86::PsMOVZX64rr32:
445 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
446 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000447 }
448
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000449 // Call the autogenerated instruction printer routines.
450 printInstruction(MI);
451}
452
453// Include the auto-generated portion of the assembly writer.
454#include "X86GenAsmWriter.inc"
455