Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 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" |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 9 | #include "X86InstrInfo.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
| 12 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 15 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 16 | namespace { |
| 17 | struct Printer : public FunctionPass { |
| 18 | TargetMachine &TM; |
| 19 | std::ostream &O; |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 20 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 21 | Printer(TargetMachine &tm, std::ostream &o) : TM(tm), O(o) {} |
| 22 | |
| 23 | bool runOnFunction(Function &F); |
| 24 | }; |
| 25 | } |
| 26 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 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 | |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 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 (); |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 44 | O << "; x86 printing only sorta implemented so far!\n"; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 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"; |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 62 | MII.print(*i_i, O, TM); |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | // We didn't modify anything. |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 70 | static void printOp(std::ostream &O, const MachineOperand &MO, |
| 71 | const MRegisterInfo &RI) { |
| 72 | switch (MO.getType()) { |
| 73 | case MachineOperand::MO_VirtualRegister: |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 74 | case MachineOperand::MO_MachineRegister: |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 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 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame^] | 100 | static bool isReg(const MachineOperand &MO) { |
| 101 | return MO.getType()==MachineOperand::MO_VirtualRegister || |
| 102 | MO.getType()==MachineOperand::MO_MachineRegister; |
| 103 | } |
| 104 | |
| 105 | |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 106 | // print - Print out an x86 instruction in intel syntax |
Chris Lattner | 927dd09 | 2002-11-17 23:20:37 +0000 | [diff] [blame] | 107 | void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O, |
| 108 | const TargetMachine &TM) const { |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 109 | unsigned Opcode = MI->getOpcode(); |
| 110 | const MachineInstrDescriptor &Desc = get(Opcode); |
| 111 | |
| 112 | if (Desc.TSFlags & X86II::TB) |
| 113 | O << "0F "; |
| 114 | |
| 115 | switch (Desc.TSFlags & X86II::FormMask) { |
| 116 | case X86II::OtherFrm: |
| 117 | O << "\t"; |
| 118 | O << "-"; MI->print(O, TM); |
| 119 | break; |
| 120 | case X86II::RawFrm: |
| 121 | toHex(O, getBaseOpcodeFor(Opcode)) << "\t"; |
| 122 | O << getName(MI->getOpCode()) << " "; |
| 123 | |
| 124 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 125 | if (i) O << ", "; |
| 126 | printOp(O, MI->getOperand(i), RI); |
| 127 | } |
| 128 | O << "\n"; |
| 129 | return; |
| 130 | |
| 131 | |
| 132 | case X86II::AddRegFrm: |
| 133 | O << "\t-"; MI->print(O, TM); break; |
| 134 | |
| 135 | case X86II::MRMDestReg: |
| 136 | // There are two acceptable forms of MRMDestReg instructions, those with 3 |
| 137 | // and 2 operands: |
| 138 | // |
| 139 | // 3 Operands: in this form, the first two registers (the destination, and |
| 140 | // the first operand) should be the same, post register allocation. The 3rd |
| 141 | // operand is an additional input. This should be for things like add |
| 142 | // instructions. |
| 143 | // |
| 144 | // 2 Operands: this is for things like mov that do not read a second input |
| 145 | // |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame^] | 146 | assert(isReg(MI->getOperand(0)) && |
| 147 | (MI->getNumOperands() == 2 || |
| 148 | (MI->getNumOperands() == 3 && isReg(MI->getOperand(1)))) && |
| 149 | isReg(MI->getOperand(MI->getNumOperands()-1)) |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 150 | && "Bad format for MRMDestReg!"); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 151 | if (MI->getNumOperands() == 3 && |
| 152 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 153 | O << "**"; |
| 154 | |
| 155 | O << "\t"; |
| 156 | O << getName(MI->getOpCode()) << " "; |
| 157 | printOp(O, MI->getOperand(0), RI); |
| 158 | O << ", "; |
| 159 | printOp(O, MI->getOperand(MI->getNumOperands()-1), RI); |
| 160 | O << "\n"; |
| 161 | return; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 162 | case X86II::MRMSrcReg: |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame^] | 163 | // There is a two forms that are acceptable for MRMSrcReg instructions, |
| 164 | // those with 3 and 2 operands: |
| 165 | // |
| 166 | // 3 Operands: in this form, the last register (the second input) is the |
| 167 | // ModR/M input. The first two operands should be the same, post register |
| 168 | // allocation. This is for things like: add r32, r/m32 |
| 169 | // |
| 170 | // 2 Operands: this is for things like mov that do not read a second input |
| 171 | // |
| 172 | assert(isReg(MI->getOperand(0)) && |
| 173 | isReg(MI->getOperand(1)) && |
| 174 | (MI->getNumOperands() == 2 || |
| 175 | (MI->getNumOperands() == 3 && isReg(MI->getOperand(2)))) |
| 176 | && "Bad format for MRMDestReg!"); |
| 177 | if (MI->getNumOperands() == 3 && |
| 178 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 179 | O << "**"; |
| 180 | |
| 181 | O << "\t"; |
| 182 | O << getName(MI->getOpCode()) << " "; |
| 183 | printOp(O, MI->getOperand(0), RI); |
| 184 | O << ", "; |
| 185 | printOp(O, MI->getOperand(MI->getNumOperands()-1), RI); |
| 186 | O << "\n"; |
| 187 | return; |
| 188 | |
| 189 | case X86II::MRMDestMem: |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 190 | case X86II::MRMSrcMem: |
| 191 | default: |
| 192 | O << "\t-"; MI->print(O, TM); break; |
| 193 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 194 | } |