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"
20 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
21 void startFunction(MachineFunction &F) {
22 std::cout << "\n**** Writing machine code for function: "
23 << F.getFunction()->getName() << "\n";
25 void finishFunction(MachineFunction &F) {
28 void startFunctionStub(const Function &F, unsigned StubSize) {
29 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
31 void *finishFunctionStub(const Function &F) {
36 void emitByte(unsigned char B) {
37 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
39 void emitWord(unsigned W) {
40 std::cout << "0x" << std::hex << W << std::dec << " ";
43 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
44 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
45 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
46 uint64_t getCurrentPCValue() { return 0; }
48 // forceCompilationOf - Force the compilation of the specified function, and
49 // return its address, because we REALLY need the address now.
51 // FIXME: This is JIT specific!
53 virtual uint64_t forceCompilationOf(Function *F) {
60 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
61 /// code emitter, which just prints the opcodes and fields out the cout. This
62 /// can be used for debugging users of the MachineCodeEmitter interface.
64 MachineCodeEmitter *MachineCodeEmitter::createDebugEmitter() {
65 return new DebugMachineCodeEmitter();
69 class FilePrinterEmitter : public MachineCodeEmitter {
72 MachineCodeEmitter &MCE;
77 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
78 : o(os), MCE(M), counter(0) {
82 ~FilePrinterEmitter() {
88 actual.open("lli.actual.obj");
90 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
95 void startFunction(MachineFunction &F) {
96 // resolve any outstanding calls
99 void finishFunction(MachineFunction &F) {
100 MCE.finishFunction(F);
103 void emitConstantPool(MachineConstantPool *MCP) {
104 MCE.emitConstantPool(MCP);
107 void startFunctionStub(const Function &F, unsigned StubSize) {
108 MCE.startFunctionStub(F, StubSize);
111 void *finishFunctionStub(const Function &F) {
112 return MCE.finishFunctionStub(F);
115 void emitByte(unsigned char B) {
117 actual << B; actual.flush();
119 values[counter] = (unsigned int) B;
120 if (++counter % 4 == 0 && counter != 0) {
122 for (unsigned i=0; i<4; ++i) {
123 if (values[i] < 16) o << "0";
124 o << values[i] << " ";
127 o << std::dec << "\t";
128 for (unsigned i=0; i<4; ++i) {
129 for (int j=7; j>=0; --j) {
130 o << ((values[i] >> j) & 1);
138 for (unsigned i=0; i<4; ++i)
139 instr |= values[i] << (i*8);
141 o << "--- * --- * --- * --- * ---\n";
146 void emitWord(unsigned W) {
149 uint64_t getGlobalValueAddress(GlobalValue *V) {
150 return MCE.getGlobalValueAddress(V);
152 uint64_t getGlobalValueAddress(const std::string &Name) {
153 return MCE.getGlobalValueAddress(Name);
155 uint64_t getConstantPoolEntryAddress(unsigned Num) {
156 return MCE.getConstantPoolEntryAddress(Num);
158 uint64_t getCurrentPCValue() {
159 return MCE.getCurrentPCValue();
161 // forceCompilationOf - Force the compilation of the specified function, and
162 // return its address, because we REALLY need the address now.
164 // FIXME: This is JIT specific!
166 virtual uint64_t forceCompilationOf(Function *F) {
167 return MCE.forceCompilationOf(F);
173 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
174 return new FilePrinterEmitter(MCE, std::cerr);