MachineCodeEmitter.cpp revision d720da23dbe2f2b4f9293a2edecd8b74ebd4b188
1//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===// 2// 3// This file implements the MachineCodeEmitter interface. 4// 5//===----------------------------------------------------------------------===// 6 7#include "llvm/CodeGen/MachineCodeEmitter.h" 8#include "llvm/CodeGen/MachineFunction.h" 9#include "llvm/Function.h" 10#include <fstream> 11 12namespace { 13 struct DebugMachineCodeEmitter : public MachineCodeEmitter { 14 void startFunction(MachineFunction &F) { 15 std::cout << "\n**** Writing machine code for function: " 16 << F.getFunction()->getName() << "\n"; 17 } 18 void finishFunction(MachineFunction &F) { 19 std::cout << "\n"; 20 } 21 void startFunctionStub(const Function &F, unsigned StubSize) { 22 std::cout << "\n--- Function stub for function: " << F.getName() << "\n"; 23 } 24 void *finishFunctionStub(const Function &F) { 25 std::cout << "\n"; 26 return 0; 27 } 28 29 void emitByte(unsigned char B) { 30 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " "; 31 } 32 void emitWord(unsigned W) { 33 std::cout << "0x" << std::hex << W << std::dec << " "; 34 } 35 36 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; } 37 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; } 38 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; } 39 uint64_t getCurrentPCValue() { return 0; } 40 41 // forceCompilationOf - Force the compilation of the specified function, and 42 // return its address, because we REALLY need the address now. 43 // 44 // FIXME: This is JIT specific! 45 // 46 virtual uint64_t forceCompilationOf(Function *F) { 47 return 0; 48 } 49 }; 50} 51 52 53/// createDebugMachineCodeEmitter - Return a dynamically allocated machine 54/// code emitter, which just prints the opcodes and fields out the cout. This 55/// can be used for debugging users of the MachineCodeEmitter interface. 56/// 57MachineCodeEmitter *MachineCodeEmitter::createDebugEmitter() { 58 return new DebugMachineCodeEmitter(); 59} 60 61namespace { 62 class FilePrinterEmitter : public MachineCodeEmitter { 63 std::ofstream actual; 64 std::ostream &o; 65 MachineCodeEmitter &MCE; 66 unsigned counter; 67 unsigned values[4]; 68 69 public: 70 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os) 71 : o(os), MCE(M), counter(0) { 72 openActual(); 73 } 74 75 ~FilePrinterEmitter() { 76 o << "\n"; 77 actual.close(); 78 } 79 80 void openActual() { 81 actual.open("lli.actual.obj"); 82 if (!actual.good()) { 83 std::cerr << "Cannot open 'lli.actual.obj' for writing\n"; 84 abort(); 85 } 86 } 87 88 void startFunction(MachineFunction &F) { 89 // resolve any outstanding calls 90 MCE.startFunction(F); 91 } 92 void finishFunction(MachineFunction &F) { 93 MCE.finishFunction(F); 94 } 95 96 void emitConstantPool(MachineConstantPool *MCP) { 97 MCE.emitConstantPool(MCP); 98 } 99 100 void startFunctionStub(const Function &F, unsigned StubSize) { 101 MCE.startFunctionStub(F, StubSize); 102 } 103 104 void *finishFunctionStub(const Function &F) { 105 return MCE.finishFunctionStub(F); 106 } 107 108 void emitByte(unsigned char B) { 109 MCE.emitByte(B); 110 actual << B; actual.flush(); 111 112 values[counter] = (unsigned int) B; 113 if (++counter % 4 == 0 && counter != 0) { 114 o << std::hex; 115 for (unsigned i=0; i<4; ++i) { 116 if (values[i] < 16) o << "0"; 117 o << values[i] << " "; 118 } 119 120 o << std::dec << "\t"; 121 for (unsigned i=0; i<4; ++i) { 122 for (int j=7; j>=0; --j) { 123 o << ((values[i] >> j) & 1); 124 } 125 o << " "; 126 } 127 128 o << "\n"; 129 130 unsigned instr = 0; 131 for (unsigned i=0; i<4; ++i) 132 instr |= values[i] << (i*8); 133 134 o << "--- * --- * --- * --- * ---\n"; 135 counter %= 4; 136 } 137 } 138 139 void emitWord(unsigned W) { 140 MCE.emitWord(W); 141 } 142 uint64_t getGlobalValueAddress(GlobalValue *V) { 143 return MCE.getGlobalValueAddress(V); 144 } 145 uint64_t getGlobalValueAddress(const std::string &Name) { 146 return MCE.getGlobalValueAddress(Name); 147 } 148 uint64_t getConstantPoolEntryAddress(unsigned Num) { 149 return MCE.getConstantPoolEntryAddress(Num); 150 } 151 uint64_t getCurrentPCValue() { 152 return MCE.getCurrentPCValue(); 153 } 154 // forceCompilationOf - Force the compilation of the specified function, and 155 // return its address, because we REALLY need the address now. 156 // 157 // FIXME: This is JIT specific! 158 // 159 virtual uint64_t forceCompilationOf(Function *F) { 160 return MCE.forceCompilationOf(F); 161 } 162 }; 163} 164 165MachineCodeEmitter * 166MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) { 167 return new FilePrinterEmitter(MCE, std::cerr); 168} 169