blob: a80e7d26642186632f6f0dd5afd9bf745d14157c [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 isReg(const MachineOperand &MO) {
70 return MO.getType() == MachineOperand::MO_VirtualRegister ||
71 MO.getType() == MachineOperand::MO_MachineRegister;
72}
73
74static bool isImmediate(const MachineOperand &MO) {
75 return MO.getType() == MachineOperand::MO_SignExtendedImmed ||
76 MO.getType() == MachineOperand::MO_UnextendedImmed;
77}
78
Chris Lattnerf8bafe82002-12-01 23:25:59 +000079static bool isPCRelativeDisp(const MachineOperand &MO) {
80 return MO.getType() == MachineOperand::MO_PCRelativeDisp;
81}
82
Chris Lattner3d3067b2002-11-21 20:44:15 +000083static bool isScale(const MachineOperand &MO) {
84 return isImmediate(MO) &&
85 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
86 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
87}
88
89static bool isMem(const MachineInstr *MI, unsigned Op) {
90 return Op+4 <= MI->getNumOperands() &&
91 isReg(MI->getOperand(Op )) && isScale(MI->getOperand(Op+1)) &&
92 isReg(MI->getOperand(Op+2)) && isImmediate(MI->getOperand(Op+3));
93}
94
Chris Lattnerf9f60882002-11-18 06:56:51 +000095static void printOp(std::ostream &O, const MachineOperand &MO,
96 const MRegisterInfo &RI) {
97 switch (MO.getType()) {
98 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +000099 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000100 O << "<" << V->getName() << ">";
101 return;
102 }
Misha Brukmane1f0d812002-11-20 18:56:41 +0000103 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000104 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
105 O << RI.get(MO.getReg()).Name;
106 else
107 O << "%reg" << MO.getReg();
108 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000109
110 case MachineOperand::MO_SignExtendedImmed:
111 case MachineOperand::MO_UnextendedImmed:
112 O << (int)MO.getImmedValue();
113 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000114 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000115 O << "<" << MO.getVRegValue()->getName() << ">";
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000116 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000117 default:
118 O << "<unknown op ty>"; return;
119 }
120}
121
Brian Gaeke86764d72002-12-05 08:30:40 +0000122static const std::string sizePtr (const MachineInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000123 switch (Desc.TSFlags & X86II::ArgMask) {
124 case X86II::Arg8: return "BYTE PTR";
125 case X86II::Arg16: return "WORD PTR";
126 case X86II::Arg32: return "DWORD PTR";
127 case X86II::Arg64: return "QWORD PTR";
128 case X86II::Arg80: return "XWORD PTR";
129 case X86II::Arg128: return "128BIT PTR"; // dunno what the real one is
Brian Gaeke86764d72002-12-05 08:30:40 +0000130 default: return "<SIZE?> PTR"; // crack being smoked
131 }
132}
133
Chris Lattner3d3067b2002-11-21 20:44:15 +0000134static void printMemReference(std::ostream &O, const MachineInstr *MI,
135 unsigned Op, const MRegisterInfo &RI) {
136 assert(isMem(MI, Op) && "Invalid memory reference!");
137 const MachineOperand &BaseReg = MI->getOperand(Op);
138 const MachineOperand &Scale = MI->getOperand(Op+1);
139 const MachineOperand &IndexReg = MI->getOperand(Op+2);
140 const MachineOperand &Disp = MI->getOperand(Op+3);
141
142 O << "[";
143 bool NeedPlus = false;
144 if (BaseReg.getReg()) {
145 printOp(O, BaseReg, RI);
146 NeedPlus = true;
147 }
148
149 if (IndexReg.getReg()) {
150 if (NeedPlus) O << " + ";
Brian Gaeke95780cc2002-12-13 07:56:18 +0000151 if (Scale.getImmedValue() != 1)
152 O << Scale.getImmedValue() << "*";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000153 printOp(O, IndexReg, RI);
154 NeedPlus = true;
155 }
156
157 if (Disp.getImmedValue()) {
158 if (NeedPlus) O << " + ";
159 printOp(O, Disp, RI);
160 }
161 O << "]";
162}
163
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000164// print - Print out an x86 instruction in intel syntax
Chris Lattner927dd092002-11-17 23:20:37 +0000165void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
166 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000167 unsigned Opcode = MI->getOpcode();
168 const MachineInstrDescriptor &Desc = get(Opcode);
169
Chris Lattner3faae2d2002-12-13 09:59:26 +0000170 if (Opcode == X86::PHI) {
171 printOp(O, MI->getOperand(0), RI);
172 O << " = phi ";
173 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
174 if (i != 1) O << ", ";
175 O << "[";
176 printOp(O, MI->getOperand(i), RI);
177 O << ", ";
178 printOp(O, MI->getOperand(i+1), RI);
179 O << "]";
180 }
181 O << "\n";
182 return;
183 }
184
185
Chris Lattnerf9f60882002-11-18 06:56:51 +0000186 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000187 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000188 // The accepted forms of Raw instructions are:
189 // 1. nop - No operand required
190 // 2. jmp foo - PC relative displacement operand
191 //
192 assert(MI->getNumOperands() == 0 ||
193 (MI->getNumOperands() == 1 && isPCRelativeDisp(MI->getOperand(0))) &&
194 "Illegal raw instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000195 O << getName(MI->getOpCode()) << " ";
196
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000197 if (MI->getNumOperands() == 1) {
198 printOp(O, MI->getOperand(0), RI);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000199 }
200 O << "\n";
201 return;
202
Chris Lattner77875d82002-11-21 02:00:20 +0000203 case X86II::AddRegFrm: {
204 // There are currently two forms of acceptable AddRegFrm instructions.
205 // Either the instruction JUST takes a single register (like inc, dec, etc),
206 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000207 // (move immediate f.e.). Note that this immediate value might be stored as
208 // an LLVM value, to represent, for example, loading the address of a global
209 // into a register.
Chris Lattner77875d82002-11-21 02:00:20 +0000210 //
211 assert(isReg(MI->getOperand(0)) &&
212 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000213 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000214 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000215 isImmediate(MI->getOperand(1))))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000216 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000217
Chris Lattner77875d82002-11-21 02:00:20 +0000218 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000219
Chris Lattner77875d82002-11-21 02:00:20 +0000220 O << getName(MI->getOpCode()) << " ";
221 printOp(O, MI->getOperand(0), RI);
222 if (MI->getNumOperands() == 2) {
223 O << ", ";
Chris Lattner675dd2c2002-11-21 17:09:01 +0000224 printOp(O, MI->getOperand(1), RI);
Chris Lattner77875d82002-11-21 02:00:20 +0000225 }
226 O << "\n";
227 return;
228 }
Chris Lattner233ad712002-11-21 01:33:44 +0000229 case X86II::MRMDestReg: {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000230 // There are two acceptable forms of MRMDestReg instructions, those with 3
231 // and 2 operands:
232 //
233 // 3 Operands: in this form, the first two registers (the destination, and
234 // the first operand) should be the same, post register allocation. The 3rd
235 // operand is an additional input. This should be for things like add
236 // instructions.
237 //
238 // 2 Operands: this is for things like mov that do not read a second input
239 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000240 assert(isReg(MI->getOperand(0)) &&
241 (MI->getNumOperands() == 2 ||
242 (MI->getNumOperands() == 3 && isReg(MI->getOperand(1)))) &&
243 isReg(MI->getOperand(MI->getNumOperands()-1))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000244 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000245 if (MI->getNumOperands() == 3 &&
246 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
247 O << "**";
248
Chris Lattnerf9f60882002-11-18 06:56:51 +0000249 O << getName(MI->getOpCode()) << " ";
250 printOp(O, MI->getOperand(0), RI);
251 O << ", ";
252 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
253 O << "\n";
254 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000255 }
Chris Lattner18042332002-11-21 21:03:39 +0000256
257 case X86II::MRMDestMem: {
258 // These instructions are the same as MRMDestReg, but instead of having a
259 // register reference for the mod/rm field, it's a memory reference.
260 //
261 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
262 isReg(MI->getOperand(4)) && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000263
Brian Gaeke86764d72002-12-05 08:30:40 +0000264 O << getName(MI->getOpCode()) << " " << sizePtr (Desc) << " ";
Chris Lattner18042332002-11-21 21:03:39 +0000265 printMemReference(O, MI, 0, RI);
266 O << ", ";
267 printOp(O, MI->getOperand(4), RI);
268 O << "\n";
269 return;
270 }
271
Chris Lattner233ad712002-11-21 01:33:44 +0000272 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000273 // There is a two forms that are acceptable for MRMSrcReg instructions,
274 // those with 3 and 2 operands:
275 //
276 // 3 Operands: in this form, the last register (the second input) is the
277 // ModR/M input. The first two operands should be the same, post register
278 // allocation. This is for things like: add r32, r/m32
279 //
280 // 2 Operands: this is for things like mov that do not read a second input
281 //
282 assert(isReg(MI->getOperand(0)) &&
283 isReg(MI->getOperand(1)) &&
284 (MI->getNumOperands() == 2 ||
285 (MI->getNumOperands() == 3 && isReg(MI->getOperand(2))))
286 && "Bad format for MRMDestReg!");
287 if (MI->getNumOperands() == 3 &&
288 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
289 O << "**";
290
Chris Lattner644e1ab2002-11-21 00:30:01 +0000291 O << getName(MI->getOpCode()) << " ";
292 printOp(O, MI->getOperand(0), RI);
293 O << ", ";
294 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
295 O << "\n";
296 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000297 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000298
Chris Lattner3d3067b2002-11-21 20:44:15 +0000299 case X86II::MRMSrcMem: {
300 // These instructions are the same as MRMSrcReg, but instead of having a
301 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000302 //
Chris Lattner3d3067b2002-11-21 20:44:15 +0000303 assert(isReg(MI->getOperand(0)) &&
304 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
305 (MI->getNumOperands() == 2+4 && isReg(MI->getOperand(1)) &&
306 isMem(MI, 2))
307 && "Bad format for MRMDestReg!");
308 if (MI->getNumOperands() == 2+4 &&
309 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
310 O << "**";
311
Chris Lattner3d3067b2002-11-21 20:44:15 +0000312 O << getName(MI->getOpCode()) << " ";
313 printOp(O, MI->getOperand(0), RI);
Brian Gaeke86764d72002-12-05 08:30:40 +0000314 O << ", " << sizePtr (Desc) << " ";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000315 printMemReference(O, MI, MI->getNumOperands()-4, RI);
316 O << "\n";
317 return;
318 }
319
Chris Lattner675dd2c2002-11-21 17:09:01 +0000320 case X86II::MRMS0r: case X86II::MRMS1r:
321 case X86II::MRMS2r: case X86II::MRMS3r:
322 case X86II::MRMS4r: case X86II::MRMS5r:
323 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000324 // In this form, the following are valid formats:
325 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000326 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000327 // 2. shl rdest, rinput <implicit CL or 1>
328 // 3. sbb rdest, rinput, immediate [rdest = rinput]
329 //
330 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
331 isReg(MI->getOperand(0)) && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000332 assert((MI->getNumOperands() != 2 ||
333 isReg(MI->getOperand(1)) || isImmediate(MI->getOperand(1))) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000334 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000335 assert((MI->getNumOperands() < 3 ||
336 (isReg(MI->getOperand(1)) && isImmediate(MI->getOperand(2)))) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000337 "Bad MRMSxR format!");
338
Chris Lattner1d53ce42002-11-21 23:30:00 +0000339 if (MI->getNumOperands() > 1 && isReg(MI->getOperand(1)) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000340 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
341 O << "**";
342
Chris Lattner675dd2c2002-11-21 17:09:01 +0000343 O << getName(MI->getOpCode()) << " ";
344 printOp(O, MI->getOperand(0), RI);
Chris Lattner1d53ce42002-11-21 23:30:00 +0000345 if (isImmediate(MI->getOperand(MI->getNumOperands()-1))) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000346 O << ", ";
Chris Lattner1d53ce42002-11-21 23:30:00 +0000347 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000348 }
349 O << "\n";
350
351 return;
352 }
353
Chris Lattnerf9f60882002-11-18 06:56:51 +0000354 default:
Chris Lattner77875d82002-11-21 02:00:20 +0000355 O << "\t\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000356 }
Chris Lattner72614082002-10-25 22:55:53 +0000357}