1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
3 // This file implements the MachineCodeEmitter interface.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/CodeGen/MachineCodeEmitter.h"
8 #include "llvm/CodeGen/MachineFunction.h"
9 #include "llvm/Function.h"
13 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
14 void startFunction(MachineFunction &F) {
15 std::cout << "\n**** Writing machine code for function: "
16 << F.getFunction()->getName() << "\n";
18 void finishFunction(MachineFunction &F) {
21 void startFunctionStub(const Function &F, unsigned StubSize) {
22 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
24 void *finishFunctionStub(const Function &F) {
29 void emitByte(unsigned char B) {
30 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
32 void emitWord(unsigned W) {
33 std::cout << "0x" << std::hex << W << std::dec << " ";
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; }
41 // forceCompilationOf - Force the compilation of the specified function, and
42 // return its address, because we REALLY need the address now.
44 // FIXME: This is JIT specific!
46 virtual uint64_t forceCompilationOf(Function *F) {
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.
57 MachineCodeEmitter *MachineCodeEmitter::createDebugEmitter() {
58 return new DebugMachineCodeEmitter();
62 class FilePrinterEmitter : public MachineCodeEmitter {
63 std::ofstream f, actual;
65 MachineCodeEmitter &MCE;
71 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
72 : f("lli.out"), o(os), MCE(M), counter(0), mustClose(false) {
74 std::cerr << "Cannot open 'lli.out' for writing\n";
80 ~FilePrinterEmitter() {
83 if (mustClose) f.close();
87 actual.open("lli.actual.obj");
89 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
94 void startFunction(MachineFunction &F) {
95 // resolve any outstanding calls
98 void finishFunction(MachineFunction &F) {
99 MCE.finishFunction(F);
102 void startFunctionStub(const Function &F, unsigned StubSize) {
103 MCE.startFunctionStub(F, StubSize);
106 void *finishFunctionStub(const Function &F) {
107 return MCE.finishFunctionStub(F);
110 void emitByte(unsigned char B) {
112 actual << B; actual.flush();
114 values[counter] = (unsigned int) B;
115 if (++counter % 4 == 0 && counter != 0) {
117 for (unsigned i=0; i<4; ++i) {
118 if (values[i] < 16) o << "0";
119 o << values[i] << " ";
122 o << std::dec << "\t";
123 for (unsigned i=0; i<4; ++i) {
124 for (int j=7; j>=0; --j) {
125 o << ((values[i] >> j) & 1);
133 for (unsigned i=0; i<4; ++i)
134 instr |= values[i] << (i*8);
136 o << "--- * --- * --- * --- * ---\n";
141 void emitWord(unsigned W) {
143 assert(0 && "FilePrinterEmitter::emitWord not implemented!");
145 uint64_t getGlobalValueAddress(GlobalValue *V) {
146 return MCE.getGlobalValueAddress(V);
148 uint64_t getGlobalValueAddress(const std::string &Name) {
149 return MCE.getGlobalValueAddress(Name);
151 uint64_t getConstantPoolEntryAddress(unsigned Num) {
152 return MCE.getConstantPoolEntryAddress(Num);
154 uint64_t getCurrentPCValue() {
155 return MCE.getCurrentPCValue();
157 // forceCompilationOf - Force the compilation of the specified function, and
158 // return its address, because we REALLY need the address now.
160 // FIXME: This is JIT specific!
162 virtual uint64_t forceCompilationOf(Function *F) {
163 return MCE.forceCompilationOf(F);
169 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
170 return new FilePrinterEmitter(MCE, std::cerr);