1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachineCodeEmitter interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineCodeEmitter.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Function.h"
23 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
24 void startFunction(MachineFunction &F) {
25 std::cout << "\n**** Writing machine code for function: "
26 << F.getFunction()->getName() << "\n";
28 void finishFunction(MachineFunction &F) {
31 void startFunctionStub(unsigned StubSize) {
32 std::cout << "\n--- Function stub:\n";
34 void *finishFunctionStub(const Function *F) {
35 std::cout << "\n--- End of stub for Function\n";
39 void emitByte(unsigned char B) {
40 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
42 void emitWord(unsigned W) {
43 std::cout << "0x" << std::hex << W << std::dec << " ";
45 void emitWordAt(unsigned W, unsigned *Ptr) {
46 std::cout << "0x" << std::hex << W << std::dec << " (at "
47 << (void*) Ptr << ") ";
50 void addRelocation(const MachineRelocation &MR) {
51 std::cout << "<relocation> ";
54 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
55 uint64_t getGlobalValueAddress(const char *Name) { return 0; }
56 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
57 uint64_t getCurrentPCValue() { return 0; }
58 uint64_t getCurrentPCOffset() { return 0; }
60 // forceCompilationOf - Force the compilation of the specified function, and
61 // return its address, because we REALLY need the address now.
63 // FIXME: This is JIT specific!
65 virtual uint64_t forceCompilationOf(Function *F) {
70 class FilePrinterEmitter : public MachineCodeEmitter {
73 MachineCodeEmitter &MCE;
78 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
79 : o(os), MCE(M), counter(0) {
83 ~FilePrinterEmitter() {
89 actual.open("lli.actual.obj");
91 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
96 void startFunction(MachineFunction &F) {
97 // resolve any outstanding calls
100 void finishFunction(MachineFunction &F) {
101 MCE.finishFunction(F);
104 void emitConstantPool(MachineConstantPool *MCP) {
105 MCE.emitConstantPool(MCP);
108 void startFunctionStub(unsigned StubSize) {
109 MCE.startFunctionStub(StubSize);
112 void *finishFunctionStub(const Function *F) {
113 return MCE.finishFunctionStub(F);
116 void emitByte(unsigned char B) {
118 actual << B; actual.flush();
120 values[counter] = (unsigned int) B;
121 if (++counter % 4 == 0 && counter != 0) {
123 for (unsigned i=0; i<4; ++i) {
124 if (values[i] < 16) o << "0";
125 o << values[i] << " ";
128 o << std::dec << "\t";
129 for (unsigned i=0; i<4; ++i) {
130 for (int j=7; j>=0; --j) {
131 o << ((values[i] >> j) & 1);
139 for (unsigned i=0; i<4; ++i)
140 instr |= values[i] << (i*8);
142 o << "--- * --- * --- * --- * ---\n";
147 void emitWord(unsigned W) {
150 void emitWordAt(unsigned W, unsigned *Ptr) {
151 MCE.emitWordAt(W, Ptr);
153 uint64_t getGlobalValueAddress(GlobalValue *V) {
154 return MCE.getGlobalValueAddress(V);
156 uint64_t getGlobalValueAddress(const char *Name) {
157 return MCE.getGlobalValueAddress(Name);
159 uint64_t getConstantPoolEntryAddress(unsigned Num) {
160 return MCE.getConstantPoolEntryAddress(Num);
162 uint64_t getCurrentPCValue() {
163 return MCE.getCurrentPCValue();
165 uint64_t getCurrentPCOffset() {
166 return MCE.getCurrentPCOffset();
168 void addRelocation(const MachineRelocation &MR) {
169 return MCE.addRelocation(MR);
172 // forceCompilationOf - Force the compilation of the specified function, and
173 // return its address, because we REALLY need the address now.
175 // FIXME: This is JIT specific!
177 virtual uint64_t forceCompilationOf(Function *F) {
178 return MCE.forceCompilationOf(F);
183 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
184 /// code emitter, which just prints the opcodes and fields out the cout. This
185 /// can be used for debugging users of the MachineCodeEmitter interface.
188 MachineCodeEmitter::createDebugEmitter() {
189 return new DebugMachineCodeEmitter();
193 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
194 return new FilePrinterEmitter(MCE, std::cerr);