8bf9968c0620a7f97d1034a899df8f971fa8d35f
[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
16 namespace {
17   struct Printer : public FunctionPass {
18     TargetMachine &TM;
19     std::ostream &O;
20
21     Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {}
22
23     bool runOnFunction(Function &F);
24   };
25 }
26
27 /// createX86CodePrinterPass - Print out the specified machine code function to
28 /// the specified stream.  This function should work regardless of whether or
29 /// not the function is in SSA form or not.
30 ///
31 Pass *createX86CodePrinterPass(TargetMachine &TM, std::ostream &O) {
32   return new Printer(TM, O);
33 }
34
35
36 /// runOnFunction - This uses the X86InstructionInfo::print method
37 /// to print assembly for each instruction.
38 bool Printer::runOnFunction (Function & F)
39 {
40   static unsigned bbnumber = 0;
41   MachineFunction & MF = MachineFunction::get (&F);
42   const MachineInstrInfo & MII = TM.getInstrInfo ();
43
44   O << "; x86 printing only sorta implemented so far!\n";
45
46   // Print out labels for the function.
47   O << "\t.globl\t" << F.getName () << "\n";
48   O << "\t.type\t" << F.getName () << ", @function\n";
49   O << F.getName () << ":\n";
50
51   // Print out code for the function.
52   for (MachineFunction::const_iterator bb_i = MF.begin (), bb_e = MF.end ();
53        bb_i != bb_e; ++bb_i)
54     {
55       // Print a label for the basic block.
56       O << ".BB" << bbnumber++ << ":\n";
57       for (MachineBasicBlock::const_iterator i_i = bb_i->begin (), i_e =
58            bb_i->end (); i_i != i_e; ++i_i)
59         {
60           // Print the assembly for the instruction.
61           O << "\t";
62           MII.print(*i_i, O, TM);
63         }
64     }
65
66   // We didn't modify anything.
67   return false;
68 }
69
70 static void printOp(std::ostream &O, const MachineOperand &MO,
71                     const MRegisterInfo &RI) {
72   switch (MO.getType()) {
73   case MachineOperand::MO_VirtualRegister:
74   case MachineOperand::MO_MachineRegister:
75     if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
76       O << RI.get(MO.getReg()).Name;
77     else
78       O << "%reg" << MO.getReg();
79     return;
80     
81   default:
82     O << "<unknown op ty>"; return;    
83   }
84 }
85
86 static inline void toHexDigit(std::ostream &O, unsigned char V) {
87   if (V >= 10)
88     O << (char)('A'+V-10);
89   else
90     O << (char)('0'+V);
91 }
92
93 static std::ostream &toHex(std::ostream &O, unsigned char V) {
94   toHexDigit(O, V >> 4);
95   toHexDigit(O, V & 0xF);
96   return O;
97 }
98
99
100 // print - Print out an x86 instruction in intel syntax
101 void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
102                          const TargetMachine &TM) const {
103   unsigned Opcode = MI->getOpcode();
104   const MachineInstrDescriptor &Desc = get(Opcode);
105
106   if (Desc.TSFlags & X86II::TB)
107     O << "0F ";
108
109   switch (Desc.TSFlags & X86II::FormMask) {
110   case X86II::OtherFrm:
111     O << "\t";
112     O << "-"; MI->print(O, TM);
113     break;
114   case X86II::RawFrm:
115     toHex(O, getBaseOpcodeFor(Opcode)) << "\t";
116     O << getName(MI->getOpCode()) << " ";
117
118     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
119       if (i) O << ", ";
120       printOp(O, MI->getOperand(i), RI);
121     }
122     O << "\n";
123     return;
124
125
126   case X86II::AddRegFrm:
127     O << "\t-"; MI->print(O, TM); break;
128
129   case X86II::MRMDestReg:
130     // There are two acceptable forms of MRMDestReg instructions, those with 3
131     // and 2 operands:
132     //
133     // 3 Operands: in this form, the first two registers (the destination, and
134     // the first operand) should be the same, post register allocation.  The 3rd
135     // operand is an additional input.  This should be for things like add
136     // instructions.
137     //
138     // 2 Operands: this is for things like mov that do not read a second input
139     //
140     assert(((MI->getNumOperands() == 3 && 
141              (MI->getOperand(0).getType()==MachineOperand::MO_VirtualRegister||
142               MI->getOperand(0).getType()==MachineOperand::MO_MachineRegister)
143              &&
144              (MI->getOperand(1).getType()==MachineOperand::MO_VirtualRegister||
145               MI->getOperand(1).getType()==MachineOperand::MO_MachineRegister))
146             ||
147             (MI->getNumOperands() == 2 && 
148              (MI->getOperand(0).getType()==MachineOperand::MO_VirtualRegister||
149               MI->getOperand(0).getType()==MachineOperand::MO_MachineRegister)
150              && (MI->getOperand(MI->getNumOperands()-1).getType() ==
151                  MachineOperand::MO_VirtualRegister||
152                  MI->getOperand(MI->getNumOperands()-1).getType() ==
153                  MachineOperand::MO_MachineRegister)))
154            && "Bad format for MRMDestReg!");
155     if (MI->getNumOperands() == 3 &&
156         MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
157       O << "**";
158
159     O << "\t";
160     O << getName(MI->getOpCode()) << " ";
161     printOp(O, MI->getOperand(0), RI);
162     O << ", ";
163     printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
164     O << "\n";
165     return;
166   case X86II::MRMDestMem:
167   case X86II::MRMSrcReg:
168   case X86II::MRMSrcMem:
169   default:
170     O << "\t-"; MI->print(O, TM); break;
171   }
172 }