Chris Lattner | 78cee7e | 2002-10-25 22:54:41 +0000 | [diff] [blame] | 1 | //===-- MFunction.cpp - Implementation code for the MFunction class -------===// |
| 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 "llvm/CodeGen/MFunction.h" |
| 9 | #include "llvm/Target/MInstructionInfo.h" |
| 10 | #include "llvm/Target/MRegisterInfo.h" |
| 11 | #include <iostream> |
| 12 | |
| 13 | static void printMRegister(unsigned RegNo, const MRegisterInfo &MRI, |
| 14 | std::ostream &OS) { |
| 15 | if (RegNo < MRegisterInfo::FirstVirtualRegister) { |
| 16 | OS << "%" << MRI[RegNo].Name; // Hard registers are prefixed with % |
| 17 | } else { |
| 18 | OS << "reg" << RegNo; // SSA registers are printed with 'reg' prefix |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static void printMInstruction(const MInstruction &MI, std::ostream &OS, |
| 23 | const MInstructionInfo &MII) { |
| 24 | const MRegisterInfo &MRI = MII.getRegisterInfo(); |
| 25 | OS << "\t"; |
| 26 | if (MI.getDestinationReg() != MRegisterInfo::NoRegister) {// Produces a value? |
| 27 | printMRegister(MI.getDestinationReg(), MRI, OS); |
| 28 | OS << " = "; |
| 29 | } |
| 30 | |
| 31 | OS << MII[MI.getOpcode()].Name << " "; |
| 32 | |
| 33 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 34 | if (i != 0) OS << ", "; |
| 35 | switch (MI.getOperandInterpretation(i)) { |
| 36 | case MOperand::Register: |
| 37 | printMRegister(MI.getRegisterOperand(i), MRI, OS); |
| 38 | break; |
| 39 | case MOperand::SignExtImmediate: |
| 40 | OS << MI.getSignExtOperand(i) << "s"; |
| 41 | break; |
| 42 | case MOperand::ZeroExtImmediate: |
| 43 | OS << MI.getZeroExtOperand(i) << "z"; |
| 44 | break; |
| 45 | case MOperand::PCRelativeDisp: |
| 46 | if (MI.getPCRelativeOperand(i) >= 0) |
| 47 | OS << "pc+" << MI.getPCRelativeOperand(i); |
| 48 | else |
| 49 | OS << "pc" << MI.getPCRelativeOperand(i); |
| 50 | break; |
| 51 | default: |
| 52 | OS << "*UNKNOWN OPERAND INTERPRETATION*"; |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | OS << "\n"; |
| 57 | } |
| 58 | |
| 59 | /// print - Provide a way to get a simple debugging dump. This dumps the |
| 60 | /// machine code in a simple "assembly" language that is not really suitable |
| 61 | /// for an assembler, but is useful for debugging. This is completely target |
| 62 | /// independant. |
| 63 | /// |
| 64 | void MFunction::print(std::ostream &OS, const MInstructionInfo &MII) const { |
| 65 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 66 | for (MBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 67 | II != IE; ++II) |
| 68 | printMInstruction(*II, OS, MII); |
| 69 | OS << "\n"; // blank line between basic blocks... |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void MFunction::dump(const MInstructionInfo &MII) const { |
| 74 | print(std::cerr, MII); |
| 75 | } |
| 76 | |