3ab75065d1ec7c57a32437eea7acbbbe306707dc
[oota-llvm.git] / lib / Target / X86 / Printer.cpp
1 //===-- 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"
9 #include "X86InstrInfo.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Function.h"
12 #include "llvm/Target/TargetMachine.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "Support/Statistic.h"
16
17 namespace {
18   struct Printer : public FunctionPass {
19     TargetMachine &TM;
20     std::ostream &O;
21
22     Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {}
23
24     bool runOnFunction(Function &F);
25   };
26 }
27
28 /// 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 ///
32 Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
33   return new Printer(TM, O);
34 }
35
36
37 /// runOnFunction - This uses the X86InstructionInfo::print method
38 /// to print assembly for each instruction.
39 bool Printer::runOnFunction (Function & F)
40 {
41   static unsigned bbnumber = 0;
42   MachineFunction & MF = MachineFunction::get (&F);
43   const MachineInstrInfo & MII = TM.getInstrInfo ();
44
45   // 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";
61           MII.print(*i_i, O, TM);
62         }
63     }
64
65   // We didn't modify anything.
66   return false;
67 }
68
69 static bool isReg(const MachineOperand &MO) {
70   return MO.getType() == MachineOperand::MO_VirtualRegister ||
71          MO.getType() == MachineOperand::MO_MachineRegister;
72 }
73
74 static bool isImmediate(const MachineOperand &MO) {
75   return MO.getType() == MachineOperand::MO_SignExtendedImmed ||
76          MO.getType() == MachineOperand::MO_UnextendedImmed;
77 }
78
79 static bool isPCRelativeDisp(const MachineOperand &MO) {
80   return MO.getType() == MachineOperand::MO_PCRelativeDisp;
81 }
82
83 static 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
89 static 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
95 static void printOp(std::ostream &O, const MachineOperand &MO,
96                     const MRegisterInfo &RI) {
97   switch (MO.getType()) {
98   case MachineOperand::MO_VirtualRegister:
99   case MachineOperand::MO_MachineRegister:
100     if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
101       O << RI.get(MO.getReg()).Name;
102     else
103       O << "%reg" << MO.getReg();
104     return;
105
106   case MachineOperand::MO_SignExtendedImmed:
107   case MachineOperand::MO_UnextendedImmed:
108     O << (int)MO.getImmedValue();
109     return;
110   case MachineOperand::MO_PCRelativeDisp:
111     O << "<" << MO.getVRegValue()->getName() << ">";
112     return;
113   default:
114     O << "<unknown op ty>"; return;    
115   }
116 }
117
118 static void printMemReference(std::ostream &O, const MachineInstr *MI,
119                               unsigned Op, const MRegisterInfo &RI) {
120   assert(isMem(MI, Op) && "Invalid memory reference!");
121   const MachineOperand &BaseReg  = MI->getOperand(Op);
122   const MachineOperand &Scale    = MI->getOperand(Op+1);
123   const MachineOperand &IndexReg = MI->getOperand(Op+2);
124   const MachineOperand &Disp     = MI->getOperand(Op+3);
125
126   O << "[";
127   bool NeedPlus = false;
128   if (BaseReg.getReg()) {
129     printOp(O, BaseReg, RI);
130     NeedPlus = true;
131   }
132
133   if (IndexReg.getReg()) {
134     if (NeedPlus) O << " + ";
135     if (IndexReg.getImmedValue() != 1)
136       O << IndexReg.getImmedValue() << "*";
137     printOp(O, IndexReg, RI);
138     NeedPlus = true;
139   }
140
141   if (Disp.getImmedValue()) {
142     if (NeedPlus) O << " + ";
143     printOp(O, Disp, RI);
144   }
145   O << "]";
146 }
147
148 // print - Print out an x86 instruction in intel syntax
149 void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
150                          const TargetMachine &TM) const {
151   unsigned Opcode = MI->getOpcode();
152   const MachineInstrDescriptor &Desc = get(Opcode);
153
154   switch (Desc.TSFlags & X86II::FormMask) {
155   case X86II::RawFrm:
156     // The accepted forms of Raw instructions are:
157     //   1. nop     - No operand required
158     //   2. jmp foo - PC relative displacement operand
159     //
160     assert(MI->getNumOperands() == 0 ||
161            (MI->getNumOperands() == 1 && isPCRelativeDisp(MI->getOperand(0))) &&
162            "Illegal raw instruction!");
163     O << getName(MI->getOpCode()) << " ";
164
165     if (MI->getNumOperands() == 1) {
166       printOp(O, MI->getOperand(0), RI);
167     }
168     O << "\n";
169     return;
170
171   case X86II::AddRegFrm: {
172     // There are currently two forms of acceptable AddRegFrm instructions.
173     // Either the instruction JUST takes a single register (like inc, dec, etc),
174     // or it takes a register and an immediate of the same size as the register
175     // (move immediate f.e.).
176     //
177     assert(isReg(MI->getOperand(0)) &&
178            (MI->getNumOperands() == 1 || 
179             (MI->getNumOperands() == 2 && isImmediate(MI->getOperand(1)))) &&
180            "Illegal form for AddRegFrm instruction!");
181
182     unsigned Reg = MI->getOperand(0).getReg();
183     
184     O << getName(MI->getOpCode()) << " ";
185     printOp(O, MI->getOperand(0), RI);
186     if (MI->getNumOperands() == 2) {
187       O << ", ";
188       printOp(O, MI->getOperand(1), RI);
189     }
190     O << "\n";
191     return;
192   }
193   case X86II::MRMDestReg: {
194     // There are two acceptable forms of MRMDestReg instructions, those with 3
195     // and 2 operands:
196     //
197     // 3 Operands: in this form, the first two registers (the destination, and
198     // the first operand) should be the same, post register allocation.  The 3rd
199     // operand is an additional input.  This should be for things like add
200     // instructions.
201     //
202     // 2 Operands: this is for things like mov that do not read a second input
203     //
204     assert(isReg(MI->getOperand(0)) &&
205            (MI->getNumOperands() == 2 || 
206             (MI->getNumOperands() == 3 && isReg(MI->getOperand(1)))) &&
207            isReg(MI->getOperand(MI->getNumOperands()-1))
208            && "Bad format for MRMDestReg!");
209     if (MI->getNumOperands() == 3 &&
210         MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
211       O << "**";
212
213     O << getName(MI->getOpCode()) << " ";
214     printOp(O, MI->getOperand(0), RI);
215     O << ", ";
216     printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
217     O << "\n";
218     return;
219   }
220
221   case X86II::MRMDestMem: {
222     // These instructions are the same as MRMDestReg, but instead of having a
223     // register reference for the mod/rm field, it's a memory reference.
224     //
225     assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
226            isReg(MI->getOperand(4)) && "Bad format for MRMDestMem!");
227
228     O << getName(MI->getOpCode()) << " <SIZE> PTR ";
229     printMemReference(O, MI, 0, RI);
230     O << ", ";
231     printOp(O, MI->getOperand(4), RI);
232     O << "\n";
233     return;
234   }
235
236   case X86II::MRMSrcReg: {
237     // There is a two forms that are acceptable for MRMSrcReg instructions,
238     // those with 3 and 2 operands:
239     //
240     // 3 Operands: in this form, the last register (the second input) is the
241     // ModR/M input.  The first two operands should be the same, post register
242     // allocation.  This is for things like: add r32, r/m32
243     //
244     // 2 Operands: this is for things like mov that do not read a second input
245     //
246     assert(isReg(MI->getOperand(0)) &&
247            isReg(MI->getOperand(1)) &&
248            (MI->getNumOperands() == 2 || 
249             (MI->getNumOperands() == 3 && isReg(MI->getOperand(2))))
250            && "Bad format for MRMDestReg!");
251     if (MI->getNumOperands() == 3 &&
252         MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
253       O << "**";
254
255     O << getName(MI->getOpCode()) << " ";
256     printOp(O, MI->getOperand(0), RI);
257     O << ", ";
258     printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
259     O << "\n";
260     return;
261   }
262
263   case X86II::MRMSrcMem: {
264     // These instructions are the same as MRMSrcReg, but instead of having a
265     // register reference for the mod/rm field, it's a memory reference.
266     //
267     assert(isReg(MI->getOperand(0)) &&
268            (MI->getNumOperands() == 1+4 && isMem(MI, 1)) || 
269            (MI->getNumOperands() == 2+4 && isReg(MI->getOperand(1)) && 
270             isMem(MI, 2))
271            && "Bad format for MRMDestReg!");
272     if (MI->getNumOperands() == 2+4 &&
273         MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
274       O << "**";
275
276     O << getName(MI->getOpCode()) << " ";
277     printOp(O, MI->getOperand(0), RI);
278     O << ", <SIZE> PTR ";
279     printMemReference(O, MI, MI->getNumOperands()-4, RI);
280     O << "\n";
281     return;
282   }
283
284   case X86II::MRMS0r: case X86II::MRMS1r:
285   case X86II::MRMS2r: case X86II::MRMS3r:
286   case X86II::MRMS4r: case X86II::MRMS5r:
287   case X86II::MRMS6r: case X86II::MRMS7r: {
288     // In this form, the following are valid formats:
289     //  1. sete r
290     //  2. cmp reg, immediate
291     //  2. shl rdest, rinput  <implicit CL or 1>
292     //  3. sbb rdest, rinput, immediate   [rdest = rinput]
293     //    
294     assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
295            isReg(MI->getOperand(0)) && "Bad MRMSxR format!");
296     assert((MI->getNumOperands() != 2 ||
297             isReg(MI->getOperand(1)) || isImmediate(MI->getOperand(1))) &&
298            "Bad MRMSxR format!");
299     assert((MI->getNumOperands() < 3 ||
300             (isReg(MI->getOperand(1)) && isImmediate(MI->getOperand(2)))) &&
301            "Bad MRMSxR format!");
302
303     if (MI->getNumOperands() > 1 && isReg(MI->getOperand(1)) && 
304         MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
305       O << "**";
306
307     O << getName(MI->getOpCode()) << " ";
308     printOp(O, MI->getOperand(0), RI);
309     if (isImmediate(MI->getOperand(MI->getNumOperands()-1))) {
310       O << ", ";
311       printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
312     }
313     O << "\n";
314
315     return;
316   }
317
318   default:
319     O << "\t\t\t-"; MI->print(O, TM); break;
320   }
321 }