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 getConstantPoolEntryAddress(unsigned Num) { return 0; }
55 uint64_t getCurrentPCValue() { return 0; }
56 uint64_t getCurrentPCOffset() { return 0; }
58 // forceCompilationOf - Force the compilation of the specified function, and
59 // return its address, because we REALLY need the address now.
61 // FIXME: This is JIT specific!
63 virtual uint64_t forceCompilationOf(Function *F) {
68 class FilePrinterEmitter : public MachineCodeEmitter {
71 MachineCodeEmitter &MCE;
76 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
77 : o(os), MCE(M), counter(0) {
81 ~FilePrinterEmitter() {
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 emitConstantPool(MachineConstantPool *MCP) {
103 MCE.emitConstantPool(MCP);
106 void startFunctionStub(unsigned StubSize) {
107 MCE.startFunctionStub(StubSize);
110 void *finishFunctionStub(const Function *F) {
111 return MCE.finishFunctionStub(F);
114 void emitByte(unsigned char B) {
116 actual << B; actual.flush();
118 values[counter] = (unsigned int) B;
119 if (++counter % 4 == 0 && counter != 0) {
121 for (unsigned i=0; i<4; ++i) {
122 if (values[i] < 16) o << "0";
123 o << values[i] << " ";
126 o << std::dec << "\t";
127 for (unsigned i=0; i<4; ++i) {
128 for (int j=7; j>=0; --j) {
129 o << ((values[i] >> j) & 1);
137 for (unsigned i=0; i<4; ++i)
138 instr |= values[i] << (i*8);
140 o << "--- * --- * --- * --- * ---\n";
145 void emitWord(unsigned W) {
148 void emitWordAt(unsigned W, unsigned *Ptr) {
149 MCE.emitWordAt(W, Ptr);
151 uint64_t getConstantPoolEntryAddress(unsigned Num) {
152 return MCE.getConstantPoolEntryAddress(Num);
154 uint64_t getCurrentPCValue() {
155 return MCE.getCurrentPCValue();
157 uint64_t getCurrentPCOffset() {
158 return MCE.getCurrentPCOffset();
160 void addRelocation(const MachineRelocation &MR) {
161 return MCE.addRelocation(MR);
164 // forceCompilationOf - Force the compilation of the specified function, and
165 // return its address, because we REALLY need the address now.
167 // FIXME: This is JIT specific!
169 virtual uint64_t forceCompilationOf(Function *F) {
170 return MCE.forceCompilationOf(F);
175 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
176 /// code emitter, which just prints the opcodes and fields out the cout. This
177 /// can be used for debugging users of the MachineCodeEmitter interface.
180 MachineCodeEmitter::createDebugEmitter() {
181 return new DebugMachineCodeEmitter();
185 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
186 return new FilePrinterEmitter(MCE, std::cerr);