MachineCodeEmitter.cpp revision 3a9cfbaf861076128be6acd27826a1176abf79b3
1//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the MachineCodeEmitter interface. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/MachineCodeEmitter.h" 15#include <fstream> 16#include <iostream> 17 18using namespace llvm; 19 20namespace { 21 class FilePrinterEmitter : public MachineCodeEmitter { 22 std::ofstream actual; 23 std::ostream &o; 24 MachineCodeEmitter &MCE; 25 unsigned counter; 26 unsigned values[4]; 27 28 public: 29 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os) 30 : o(os), MCE(M), counter(0) { 31 openActual(); 32 } 33 34 ~FilePrinterEmitter() { 35 o << "\n"; 36 actual.close(); 37 } 38 39 void openActual() { 40 actual.open("lli.actual.obj"); 41 if (!actual.good()) { 42 std::cerr << "Cannot open 'lli.actual.obj' for writing\n"; 43 abort(); 44 } 45 } 46 47 void startFunction(MachineFunction &F) { 48 // resolve any outstanding calls 49 MCE.startFunction(F); 50 } 51 void finishFunction(MachineFunction &F) { 52 MCE.finishFunction(F); 53 } 54 55 void emitConstantPool(MachineConstantPool *MCP) { 56 MCE.emitConstantPool(MCP); 57 } 58 void initJumpTableInfo(MachineJumpTableInfo *MJTI) { 59 MCE.initJumpTableInfo(MJTI); 60 } 61 void emitJumpTableInfo(MachineJumpTableInfo *MJTI, 62 std::map<MachineBasicBlock*,uint64_t> &MBBM) { 63 MCE.emitJumpTableInfo(MJTI, MBBM); 64 } 65 66 void startFunctionStub(unsigned StubSize) { 67 MCE.startFunctionStub(StubSize); 68 } 69 70 void *finishFunctionStub(const Function *F) { 71 return MCE.finishFunctionStub(F); 72 } 73 74 void emitByte(unsigned char B) { 75 MCE.emitByte(B); 76 actual << B; actual.flush(); 77 78 values[counter] = (unsigned int) B; 79 if (++counter % 4 == 0 && counter != 0) { 80 o << std::hex; 81 for (unsigned i=0; i<4; ++i) { 82 if (values[i] < 16) o << "0"; 83 o << values[i] << " "; 84 } 85 86 o << std::dec << "\t"; 87 for (unsigned i=0; i<4; ++i) { 88 for (int j=7; j>=0; --j) { 89 o << ((values[i] >> j) & 1); 90 } 91 o << " "; 92 } 93 94 o << "\n"; 95 96 unsigned instr = 0; 97 for (unsigned i=0; i<4; ++i) 98 instr |= values[i] << (i*8); 99 100 o << "--- * --- * --- * --- * ---\n"; 101 counter %= 4; 102 } 103 } 104 105 void emitWord(unsigned W) { 106 MCE.emitWord(W); 107 } 108 void emitWordAt(unsigned W, unsigned *Ptr) { 109 MCE.emitWordAt(W, Ptr); 110 } 111 uint64_t getConstantPoolEntryAddress(unsigned Num) { 112 return MCE.getConstantPoolEntryAddress(Num); 113 } 114 uint64_t getJumpTableEntryAddress(unsigned Num) { 115 return MCE.getJumpTableEntryAddress(Num); 116 } 117 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) 118 { return MCE.allocateGlobal(size, alignment); } 119 120 uint64_t getCurrentPCValue() { 121 return MCE.getCurrentPCValue(); 122 } 123 uint64_t getCurrentPCOffset() { 124 return MCE.getCurrentPCOffset(); 125 } 126 void addRelocation(const MachineRelocation &MR) { 127 return MCE.addRelocation(MR); 128 } 129 }; 130} 131 132MachineCodeEmitter * 133MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) { 134 return new FilePrinterEmitter(MCE, std::cerr); 135} 136