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 {
65 MachineCodeEmitter &MCE;
70 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
71 : o(os), MCE(M), counter(0) {
75 ~FilePrinterEmitter() {
81 actual.open("lli.actual.obj");
83 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
88 void startFunction(MachineFunction &F) {
89 // resolve any outstanding calls
92 void finishFunction(MachineFunction &F) {
93 MCE.finishFunction(F);
96 void emitConstantPool(MachineConstantPool *MCP) {
97 MCE.emitConstantPool(MCP);
100 void startFunctionStub(const Function &F, unsigned StubSize) {
101 MCE.startFunctionStub(F, StubSize);
104 void *finishFunctionStub(const Function &F) {
105 return MCE.finishFunctionStub(F);
108 void emitByte(unsigned char B) {
110 actual << B; actual.flush();
112 values[counter] = (unsigned int) B;
113 if (++counter % 4 == 0 && counter != 0) {
115 for (unsigned i=0; i<4; ++i) {
116 if (values[i] < 16) o << "0";
117 o << values[i] << " ";
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);
131 for (unsigned i=0; i<4; ++i)
132 instr |= values[i] << (i*8);
134 o << "--- * --- * --- * --- * ---\n";
139 void emitWord(unsigned W) {
142 uint64_t getGlobalValueAddress(GlobalValue *V) {
143 return MCE.getGlobalValueAddress(V);
145 uint64_t getGlobalValueAddress(const std::string &Name) {
146 return MCE.getGlobalValueAddress(Name);
148 uint64_t getConstantPoolEntryAddress(unsigned Num) {
149 return MCE.getConstantPoolEntryAddress(Num);
151 uint64_t getCurrentPCValue() {
152 return MCE.getCurrentPCValue();
154 // forceCompilationOf - Force the compilation of the specified function, and
155 // return its address, because we REALLY need the address now.
157 // FIXME: This is JIT specific!
159 virtual uint64_t forceCompilationOf(Function *F) {
160 return MCE.forceCompilationOf(F);
166 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
167 return new FilePrinterEmitter(MCE, std::cerr);