blob: 88dc554957e12d8e775b1bec3b2a22f1285db3c5 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===//
2//
3// This file contains a printer that converts from our internal representation
4// of LLVM code to a nice human readable form that is suitable for debuggging.
5//
6//===----------------------------------------------------------------------===//
7
8#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +00009#include "X86InstrInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010#include "llvm/Pass.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "llvm/Function.h"
12#include "llvm/Target/TargetMachine.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner233ad712002-11-21 01:33:44 +000015#include "Support/Statistic.h"
Chris Lattner72614082002-10-25 22:55:53 +000016
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000017namespace {
18 struct Printer : public FunctionPass {
19 TargetMachine &TM;
20 std::ostream &O;
Chris Lattner72614082002-10-25 22:55:53 +000021
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000022 Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {}
23
24 bool runOnFunction(Function &F);
25 };
26}
27
Chris Lattnerdbb61c62002-11-17 22:53:13 +000028/// createX86CodePrinterPass - Print out the specified machine code function to
29/// the specified stream. This function should work regardless of whether or
30/// not the function is in SSA form or not.
31///
32Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
33 return new Printer(TM, O);
34}
35
36
Brian Gaeke6559bb92002-11-14 22:32:30 +000037/// runOnFunction - This uses the X86InstructionInfo::print method
38/// to print assembly for each instruction.
39bool Printer::runOnFunction (Function & F)
40{
41 static unsigned bbnumber = 0;
42 MachineFunction & MF = MachineFunction::get (&F);
43 const MachineInstrInfo & MII = TM.getInstrInfo ();
Brian Gaeke6559bb92002-11-14 22:32:30 +000044
Brian Gaeke6559bb92002-11-14 22:32:30 +000045 // Print out labels for the function.
46 O << "\t.globl\t" << F.getName () << "\n";
47 O << "\t.type\t" << F.getName () << ", @function\n";
48 O << F.getName () << ":\n";
49
50 // Print out code for the function.
51 for (MachineFunction::const_iterator bb_i = MF.begin (), bb_e = MF.end ();
52 bb_i != bb_e; ++bb_i)
53 {
54 // Print a label for the basic block.
55 O << ".BB" << bbnumber++ << ":\n";
56 for (MachineBasicBlock::const_iterator i_i = bb_i->begin (), i_e =
57 bb_i->end (); i_i != i_e; ++i_i)
58 {
59 // Print the assembly for the instruction.
60 O << "\t";
Chris Lattner927dd092002-11-17 23:20:37 +000061 MII.print(*i_i, O, TM);
Brian Gaeke6559bb92002-11-14 22:32:30 +000062 }
63 }
64
65 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000066 return false;
67}
68
Chris Lattner3d3067b2002-11-21 20:44:15 +000069static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +000070 return MO.isImmediate() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +000071 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
72 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
73}
74
75static bool isMem(const MachineInstr *MI, unsigned Op) {
76 return Op+4 <= MI->getNumOperands() &&
Chris Lattnerd9096832002-12-15 08:01:39 +000077 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
78 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +000079}
80
Chris Lattnerf9f60882002-11-18 06:56:51 +000081static void printOp(std::ostream &O, const MachineOperand &MO,
82 const MRegisterInfo &RI) {
83 switch (MO.getType()) {
84 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +000085 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +000086 O << "<" << V->getName() << ">";
87 return;
88 }
Misha Brukmane1f0d812002-11-20 18:56:41 +000089 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +000090 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
91 O << RI.get(MO.getReg()).Name;
92 else
93 O << "%reg" << MO.getReg();
94 return;
Chris Lattner77875d82002-11-21 02:00:20 +000095
96 case MachineOperand::MO_SignExtendedImmed:
97 case MachineOperand::MO_UnextendedImmed:
98 O << (int)MO.getImmedValue();
99 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000100 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000101 O << "<" << MO.getVRegValue()->getName() << ">";
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000102 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000103 default:
104 O << "<unknown op ty>"; return;
105 }
106}
107
Brian Gaeke86764d72002-12-05 08:30:40 +0000108static const std::string sizePtr (const MachineInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000109 switch (Desc.TSFlags & X86II::ArgMask) {
110 case X86II::Arg8: return "BYTE PTR";
111 case X86II::Arg16: return "WORD PTR";
112 case X86II::Arg32: return "DWORD PTR";
113 case X86II::Arg64: return "QWORD PTR";
114 case X86II::Arg80: return "XWORD PTR";
115 case X86II::Arg128: return "128BIT PTR"; // dunno what the real one is
Brian Gaeke86764d72002-12-05 08:30:40 +0000116 default: return "<SIZE?> PTR"; // crack being smoked
117 }
118}
119
Chris Lattner3d3067b2002-11-21 20:44:15 +0000120static void printMemReference(std::ostream &O, const MachineInstr *MI,
121 unsigned Op, const MRegisterInfo &RI) {
122 assert(isMem(MI, Op) && "Invalid memory reference!");
123 const MachineOperand &BaseReg = MI->getOperand(Op);
124 const MachineOperand &Scale = MI->getOperand(Op+1);
125 const MachineOperand &IndexReg = MI->getOperand(Op+2);
126 const MachineOperand &Disp = MI->getOperand(Op+3);
127
128 O << "[";
129 bool NeedPlus = false;
130 if (BaseReg.getReg()) {
131 printOp(O, BaseReg, RI);
132 NeedPlus = true;
133 }
134
135 if (IndexReg.getReg()) {
136 if (NeedPlus) O << " + ";
Brian Gaeke95780cc2002-12-13 07:56:18 +0000137 if (Scale.getImmedValue() != 1)
138 O << Scale.getImmedValue() << "*";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000139 printOp(O, IndexReg, RI);
140 NeedPlus = true;
141 }
142
143 if (Disp.getImmedValue()) {
144 if (NeedPlus) O << " + ";
145 printOp(O, Disp, RI);
146 }
147 O << "]";
148}
149
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000150// print - Print out an x86 instruction in intel syntax
Chris Lattner927dd092002-11-17 23:20:37 +0000151void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
152 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000153 unsigned Opcode = MI->getOpcode();
154 const MachineInstrDescriptor &Desc = get(Opcode);
155
Chris Lattner3faae2d2002-12-13 09:59:26 +0000156 if (Opcode == X86::PHI) {
157 printOp(O, MI->getOperand(0), RI);
158 O << " = phi ";
159 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
160 if (i != 1) O << ", ";
161 O << "[";
162 printOp(O, MI->getOperand(i), RI);
163 O << ", ";
164 printOp(O, MI->getOperand(i+1), RI);
165 O << "]";
166 }
167 O << "\n";
168 return;
169 }
170
171
Chris Lattnerf9f60882002-11-18 06:56:51 +0000172 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000173 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000174 // The accepted forms of Raw instructions are:
175 // 1. nop - No operand required
176 // 2. jmp foo - PC relative displacement operand
177 //
178 assert(MI->getNumOperands() == 0 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000179 (MI->getNumOperands() == 1 && MI->getOperand(0).isPCRelativeDisp())&&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000180 "Illegal raw instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000181 O << getName(MI->getOpCode()) << " ";
182
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000183 if (MI->getNumOperands() == 1) {
184 printOp(O, MI->getOperand(0), RI);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000185 }
186 O << "\n";
187 return;
188
Chris Lattner77875d82002-11-21 02:00:20 +0000189 case X86II::AddRegFrm: {
190 // There are currently two forms of acceptable AddRegFrm instructions.
191 // Either the instruction JUST takes a single register (like inc, dec, etc),
192 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000193 // (move immediate f.e.). Note that this immediate value might be stored as
194 // an LLVM value, to represent, for example, loading the address of a global
195 // into a register.
Chris Lattner77875d82002-11-21 02:00:20 +0000196 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000197 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000198 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000199 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000200 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000201 MI->getOperand(1).isImmediate()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000202 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000203
Chris Lattner77875d82002-11-21 02:00:20 +0000204 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000205
Chris Lattner77875d82002-11-21 02:00:20 +0000206 O << getName(MI->getOpCode()) << " ";
207 printOp(O, MI->getOperand(0), RI);
208 if (MI->getNumOperands() == 2) {
209 O << ", ";
Chris Lattner675dd2c2002-11-21 17:09:01 +0000210 printOp(O, MI->getOperand(1), RI);
Chris Lattner77875d82002-11-21 02:00:20 +0000211 }
212 O << "\n";
213 return;
214 }
Chris Lattner233ad712002-11-21 01:33:44 +0000215 case X86II::MRMDestReg: {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000216 // There are two acceptable forms of MRMDestReg instructions, those with 3
217 // and 2 operands:
218 //
219 // 3 Operands: in this form, the first two registers (the destination, and
220 // the first operand) should be the same, post register allocation. The 3rd
221 // operand is an additional input. This should be for things like add
222 // instructions.
223 //
224 // 2 Operands: this is for things like mov that do not read a second input
225 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000226 assert(MI->getOperand(0).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000227 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000228 (MI->getNumOperands() == 3 && MI->getOperand(1).isRegister())) &&
229 MI->getOperand(MI->getNumOperands()-1).isRegister()
Misha Brukmane1f0d812002-11-20 18:56:41 +0000230 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000231 if (MI->getNumOperands() == 3 &&
232 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
233 O << "**";
234
Chris Lattnerf9f60882002-11-18 06:56:51 +0000235 O << getName(MI->getOpCode()) << " ";
236 printOp(O, MI->getOperand(0), RI);
237 O << ", ";
238 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
239 O << "\n";
240 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000241 }
Chris Lattner18042332002-11-21 21:03:39 +0000242
243 case X86II::MRMDestMem: {
244 // These instructions are the same as MRMDestReg, but instead of having a
245 // register reference for the mod/rm field, it's a memory reference.
246 //
247 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000248 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000249
Brian Gaeke86764d72002-12-05 08:30:40 +0000250 O << getName(MI->getOpCode()) << " " << sizePtr (Desc) << " ";
Chris Lattner18042332002-11-21 21:03:39 +0000251 printMemReference(O, MI, 0, RI);
252 O << ", ";
253 printOp(O, MI->getOperand(4), RI);
254 O << "\n";
255 return;
256 }
257
Chris Lattner233ad712002-11-21 01:33:44 +0000258 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000259 // There is a two forms that are acceptable for MRMSrcReg instructions,
260 // those with 3 and 2 operands:
261 //
262 // 3 Operands: in this form, the last register (the second input) is the
263 // ModR/M input. The first two operands should be the same, post register
264 // allocation. This is for things like: add r32, r/m32
265 //
266 // 2 Operands: this is for things like mov that do not read a second input
267 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000268 assert(MI->getOperand(0).isRegister() &&
269 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000270 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000271 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattner644e1ab2002-11-21 00:30:01 +0000272 && "Bad format for MRMDestReg!");
273 if (MI->getNumOperands() == 3 &&
274 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
275 O << "**";
276
Chris Lattner644e1ab2002-11-21 00:30:01 +0000277 O << getName(MI->getOpCode()) << " ";
278 printOp(O, MI->getOperand(0), RI);
279 O << ", ";
280 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
281 O << "\n";
282 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000283 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000284
Chris Lattner3d3067b2002-11-21 20:44:15 +0000285 case X86II::MRMSrcMem: {
286 // These instructions are the same as MRMSrcReg, but instead of having a
287 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000288 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000289 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000290 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000291 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000292 isMem(MI, 2))
293 && "Bad format for MRMDestReg!");
294 if (MI->getNumOperands() == 2+4 &&
295 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
296 O << "**";
297
Chris Lattner3d3067b2002-11-21 20:44:15 +0000298 O << getName(MI->getOpCode()) << " ";
299 printOp(O, MI->getOperand(0), RI);
Brian Gaeke86764d72002-12-05 08:30:40 +0000300 O << ", " << sizePtr (Desc) << " ";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000301 printMemReference(O, MI, MI->getNumOperands()-4, RI);
302 O << "\n";
303 return;
304 }
305
Chris Lattner675dd2c2002-11-21 17:09:01 +0000306 case X86II::MRMS0r: case X86II::MRMS1r:
307 case X86II::MRMS2r: case X86II::MRMS3r:
308 case X86II::MRMS4r: case X86II::MRMS5r:
309 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000310 // In this form, the following are valid formats:
311 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000312 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000313 // 2. shl rdest, rinput <implicit CL or 1>
314 // 3. sbb rdest, rinput, immediate [rdest = rinput]
315 //
316 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000317 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000318 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000319 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000320 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000321 assert((MI->getNumOperands() < 3 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000322 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000323 "Bad MRMSxR format!");
324
Chris Lattnerd9096832002-12-15 08:01:39 +0000325 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000326 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
327 O << "**";
328
Chris Lattner675dd2c2002-11-21 17:09:01 +0000329 O << getName(MI->getOpCode()) << " ";
330 printOp(O, MI->getOperand(0), RI);
Chris Lattnerd9096832002-12-15 08:01:39 +0000331 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000332 O << ", ";
Chris Lattner1d53ce42002-11-21 23:30:00 +0000333 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000334 }
335 O << "\n";
336
337 return;
338 }
339
Chris Lattnerf9f60882002-11-18 06:56:51 +0000340 default:
Chris Lattner77875d82002-11-21 02:00:20 +0000341 O << "\t\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000342 }
Chris Lattner72614082002-10-25 22:55:53 +0000343}