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"
21 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
22 void startFunction(MachineFunction &F) {
23 std::cout << "\n**** Writing machine code for function: "
24 << F.getFunction()->getName() << "\n";
26 void finishFunction(MachineFunction &F) {
29 void startFunctionStub(const Function &F, unsigned StubSize) {
30 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
32 void *finishFunctionStub(const Function &F) {
37 void emitByte(unsigned char B) {
38 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
40 void emitWord(unsigned W) {
41 std::cout << "0x" << std::hex << W << std::dec << " ";
43 void emitWordAt(unsigned W, unsigned *Ptr) {
44 std::cout << "0x" << std::hex << W << std::dec << " (at "
45 << (void*) Ptr << ") ";
48 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
49 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
50 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
51 uint64_t getCurrentPCValue() { return 0; }
53 // forceCompilationOf - Force the compilation of the specified function, and
54 // return its address, because we REALLY need the address now.
56 // FIXME: This is JIT specific!
58 virtual uint64_t forceCompilationOf(Function *F) {
63 class FilePrinterEmitter : public MachineCodeEmitter {
66 MachineCodeEmitter &MCE;
71 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
72 : o(os), MCE(M), counter(0) {
76 ~FilePrinterEmitter() {
82 actual.open("lli.actual.obj");
84 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
89 void startFunction(MachineFunction &F) {
90 // resolve any outstanding calls
93 void finishFunction(MachineFunction &F) {
94 MCE.finishFunction(F);
97 void emitConstantPool(MachineConstantPool *MCP) {
98 MCE.emitConstantPool(MCP);
101 void startFunctionStub(const Function &F, unsigned StubSize) {
102 MCE.startFunctionStub(F, StubSize);
105 void *finishFunctionStub(const Function &F) {
106 return MCE.finishFunctionStub(F);
109 void emitByte(unsigned char B) {
111 actual << B; actual.flush();
113 values[counter] = (unsigned int) B;
114 if (++counter % 4 == 0 && counter != 0) {
116 for (unsigned i=0; i<4; ++i) {
117 if (values[i] < 16) o << "0";
118 o << values[i] << " ";
121 o << std::dec << "\t";
122 for (unsigned i=0; i<4; ++i) {
123 for (int j=7; j>=0; --j) {
124 o << ((values[i] >> j) & 1);
132 for (unsigned i=0; i<4; ++i)
133 instr |= values[i] << (i*8);
135 o << "--- * --- * --- * --- * ---\n";
140 void emitWord(unsigned W) {
143 void emitWordAt(unsigned W, unsigned *Ptr) {
144 MCE.emitWordAt(W, Ptr);
146 uint64_t getGlobalValueAddress(GlobalValue *V) {
147 return MCE.getGlobalValueAddress(V);
149 uint64_t getGlobalValueAddress(const std::string &Name) {
150 return MCE.getGlobalValueAddress(Name);
152 uint64_t getConstantPoolEntryAddress(unsigned Num) {
153 return MCE.getConstantPoolEntryAddress(Num);
155 uint64_t getCurrentPCValue() {
156 return MCE.getCurrentPCValue();
158 // forceCompilationOf - Force the compilation of the specified function, and
159 // return its address, because we REALLY need the address now.
161 // FIXME: This is JIT specific!
163 virtual uint64_t forceCompilationOf(Function *F) {
164 return MCE.forceCompilationOf(F);
169 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
170 /// code emitter, which just prints the opcodes and fields out the cout. This
171 /// can be used for debugging users of the MachineCodeEmitter interface.
174 MachineCodeEmitter::createDebugEmitter() {
175 return new DebugMachineCodeEmitter();
179 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
180 return new FilePrinterEmitter(MCE, std::cerr);