blob: 85ba0f40e6135d395e5435aa09da18872ec4b45f [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
129 // Print out jump tables referenced by the function
130 // Mac OS X requires at least one non-local (e.g. L1) labels before local
131 // lables that are used in jump table expressions (e.g. LBB1_1-LJT1_0).
132 EmitJumpTableInfo(MF.getJumpTableInfo());
133
Jim Laskey563321a2006-09-06 18:34:40 +0000134 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000135 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000136
Jim Laskeyea348582006-07-27 02:05:13 +0000137 if (Subtarget->isTargetDarwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000138 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000139 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000140 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000141
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000142 // We didn't modify anything.
143 return false;
144}
145
Chris Lattnera3b8c572006-02-06 23:41:19 +0000146void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
147 const char *Modifier) {
148 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000149 const MRegisterInfo &RI = *TM.getRegisterInfo();
150 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000151 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000152 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
153 "Virtual registers should not make it this far!");
154 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000155 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000156 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
158 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
159 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000160 Reg = getX86SubSuperRegister(Reg, VT);
161 }
162 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000163 O << (char)tolower(*Name);
164 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000165 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000166
Chris Lattner63b3d712006-05-04 17:21:20 +0000167 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000168 if (!Modifier || strcmp(Modifier, "debug") != 0)
169 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000170 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000171 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000172 case MachineOperand::MO_MachineBasicBlock:
173 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000174 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000175 case MachineOperand::MO_JumpTableIndex: {
176 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
177 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000178 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000179 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000180 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000181 TM.getRelocationModel() == Reloc::PIC_)
182 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 if (Subtarget->is64Bit())
184 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000185 return;
186 }
Evan Chenga09bd812006-02-26 08:28:12 +0000187 case MachineOperand::MO_ConstantPoolIndex: {
188 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
189 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000190 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000191 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000193 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000194 O << "-\"L" << getFunctionNumber() << "$pb\"";
195 int Offset = MO.getOffset();
196 if (Offset > 0)
197 O << "+" << Offset;
198 else if (Offset < 0)
199 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000200
201 if (Subtarget->is64Bit())
202 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000203 return;
204 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000205 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000206 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000207 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000208 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000209
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000210 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000212
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
214 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000215
Chris Lattnere87e1152006-09-26 03:57:53 +0000216 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000217
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000219 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000220 // Link-once, External, or Weakly-linked global variables need
221 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000223 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000225 FnStubs.insert(Name);
226 O << "L" << Name << "$stub";
227 } else {
228 GVStubs.insert(Name);
229 O << "L" << Name << "$non_lazy_ptr";
230 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000231 } else {
232 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000233 O << "__imp_";
234 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000236 }
237
Chris Lattner35d86fe2006-07-26 21:12:04 +0000238 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000239 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000240 } else {
241 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000242 O << "__imp_";
243 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000245 }
246
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000247 int Offset = MO.getOffset();
248 if (Offset > 0)
249 O << "+" << Offset;
250 else if (Offset < 0)
251 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000252
253 if (!isCallOp &&
254 Subtarget->is64Bit()) {
255 if (isExt && TM.getRelocationModel() != Reloc::Static)
256 O << "@GOTPCREL";
257 O << "(%rip)";
258 }
259
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 return;
261 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000262 case MachineOperand::MO_ExternalSymbol: {
263 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000264 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000266 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000267 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000268 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000269 FnStubs.insert(Name);
270 O << "L" << Name << "$stub";
271 return;
272 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000273 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000274 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000275
276 if (!isCallOp &&
277 Subtarget->is64Bit())
278 O << "(%rip)";
279
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000280 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000281 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 default:
283 O << "<unknown operand type>"; return;
284 }
285}
286
Nate Begeman391c5d22005-11-30 18:54:35 +0000287void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000288 unsigned char value = MI->getOperand(Op).getImmedValue();
289 assert(value <= 7 && "Invalid ssecc argument!");
290 switch (value) {
291 case 0: O << "eq"; break;
292 case 1: O << "lt"; break;
293 case 2: O << "le"; break;
294 case 3: O << "unord"; break;
295 case 4: O << "neq"; break;
296 case 5: O << "nlt"; break;
297 case 6: O << "nle"; break;
298 case 7: O << "ord"; break;
299 }
300}
301
Evan Cheng25ab6902006-09-08 06:48:29 +0000302void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
303 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000304 assert(isMem(MI, Op) && "Invalid memory reference!");
305
306 const MachineOperand &BaseReg = MI->getOperand(Op);
307 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
308 const MachineOperand &IndexReg = MI->getOperand(Op+2);
309 const MachineOperand &DispSpec = MI->getOperand(Op+3);
310
311 if (BaseReg.isFrameIndex()) {
312 O << "[frame slot #" << BaseReg.getFrameIndex();
313 if (DispSpec.getImmedValue())
314 O << " + " << DispSpec.getImmedValue();
315 O << "]";
316 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000317 }
318
Evan Chengc9676de2006-08-29 22:14:48 +0000319 if (DispSpec.isGlobalAddress() ||
320 DispSpec.isConstantPoolIndex() ||
321 DispSpec.isJumpTableIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000322 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000323 } else {
324 int DispVal = DispSpec.getImmedValue();
325 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
326 O << DispVal;
327 }
328
329 if (IndexReg.getReg() || BaseReg.getReg()) {
330 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000331 if (BaseReg.getReg()) {
332 printOperand(MI, Op, Modifier);
333 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000334
335 if (IndexReg.getReg()) {
336 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000337 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000338 if (ScaleVal != 1)
339 O << "," << ScaleVal;
340 }
341
342 O << ")";
343 }
344}
345
Evan Cheng7ccced62006-02-18 00:15:05 +0000346void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
347 O << "\"L" << getFunctionNumber() << "$pb\"\n";
348 O << "\"L" << getFunctionNumber() << "$pb\":";
349}
350
Evan Cheng62f27002006-04-28 23:11:40 +0000351
Evan Cheng55c25f22006-04-28 23:19:39 +0000352bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000353 const char Mode) {
354 const MRegisterInfo &RI = *TM.getRegisterInfo();
355 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000356 switch (Mode) {
357 default: return true; // Unknown mode.
358 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000359 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000360 break;
361 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000362 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000363 break;
364 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000365 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000366 break;
367 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000368 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000369 break;
370 }
371
Evan Cheng8f7f7122006-05-05 05:40:20 +0000372 O << '%';
373 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
374 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000375 return false;
376}
377
Evan Cheng3d48a902006-04-28 21:19:05 +0000378/// PrintAsmOperand - Print out an operand for an inline asm expression.
379///
380bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
381 unsigned AsmVariant,
382 const char *ExtraCode) {
383 // Does this asm operand have a single letter operand modifier?
384 if (ExtraCode && ExtraCode[0]) {
385 if (ExtraCode[1] != 0) return true; // Unknown modifier.
386
387 switch (ExtraCode[0]) {
388 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000389 case 'b': // Print QImode register
390 case 'h': // Print QImode high register
391 case 'w': // Print HImode register
392 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000393 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000394 }
395 }
396
397 printOperand(MI, OpNo);
398 return false;
399}
400
401bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
402 unsigned OpNo,
403 unsigned AsmVariant,
404 const char *ExtraCode) {
405 if (ExtraCode && ExtraCode[0])
406 return true; // Unknown modifier.
407 printMemReference(MI, OpNo);
408 return false;
409}
410
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000411/// printMachineInstruction -- Print out a single X86 LLVM instruction
412/// MI in Intel syntax to the current output stream.
413///
414void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
415 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000416
Evan Cheng8f7f7122006-05-05 05:40:20 +0000417 // See if a truncate instruction can be turned into a nop.
418 switch (MI->getOpcode()) {
419 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 case X86::TRUNC_64to32:
421 case X86::TRUNC_64to16:
422 case X86::TRUNC_32to16:
423 case X86::TRUNC_32to8:
424 case X86::TRUNC_16to8:
425 case X86::TRUNC_32_to8:
426 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000427 const MachineOperand &MO0 = MI->getOperand(0);
428 const MachineOperand &MO1 = MI->getOperand(1);
429 unsigned Reg0 = MO0.getReg();
430 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000431 unsigned Opc = MI->getOpcode();
432 if (Opc == X86::TRUNC_64to32)
433 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
434 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000435 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000436 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000437 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000438 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000439 if (Reg0 != Reg1)
440 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000441 break;
442 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000443 case X86::PsMOVZX64rr32:
444 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
445 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000446 }
447
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000448 // Call the autogenerated instruction printer routines.
449 printInstruction(MI);
450}
451
452// Include the auto-generated portion of the assembly writer.
453#include "X86GenAsmWriter.inc"
454