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"
22 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
23 void startFunction(MachineFunction &F) {
24 std::cout << "\n**** Writing machine code for function: "
25 << F.getFunction()->getName() << "\n";
27 void finishFunction(MachineFunction &F) {
30 void startFunctionStub(const Function &F, unsigned StubSize) {
31 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
33 void *finishFunctionStub(const Function &F) {
38 void emitByte(unsigned char B) {
39 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
41 void emitWord(unsigned W) {
42 std::cout << "0x" << std::hex << W << std::dec << " ";
45 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
46 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
47 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
48 uint64_t getCurrentPCValue() { return 0; }
50 // forceCompilationOf - Force the compilation of the specified function, and
51 // return its address, because we REALLY need the address now.
53 // FIXME: This is JIT specific!
55 virtual uint64_t forceCompilationOf(Function *F) {
60 class FilePrinterEmitter : public MachineCodeEmitter {
63 MachineCodeEmitter &MCE;
68 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
69 : o(os), MCE(M), counter(0) {
73 ~FilePrinterEmitter() {
79 actual.open("lli.actual.obj");
81 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
86 void startFunction(MachineFunction &F) {
87 // resolve any outstanding calls
90 void finishFunction(MachineFunction &F) {
91 MCE.finishFunction(F);
94 void emitConstantPool(MachineConstantPool *MCP) {
95 MCE.emitConstantPool(MCP);
98 void startFunctionStub(const Function &F, unsigned StubSize) {
99 MCE.startFunctionStub(F, StubSize);
102 void *finishFunctionStub(const Function &F) {
103 return MCE.finishFunctionStub(F);
106 void emitByte(unsigned char B) {
108 actual << B; actual.flush();
110 values[counter] = (unsigned int) B;
111 if (++counter % 4 == 0 && counter != 0) {
113 for (unsigned i=0; i<4; ++i) {
114 if (values[i] < 16) o << "0";
115 o << values[i] << " ";
118 o << std::dec << "\t";
119 for (unsigned i=0; i<4; ++i) {
120 for (int j=7; j>=0; --j) {
121 o << ((values[i] >> j) & 1);
129 for (unsigned i=0; i<4; ++i)
130 instr |= values[i] << (i*8);
132 o << "--- * --- * --- * --- * ---\n";
137 void emitWord(unsigned W) {
140 uint64_t getGlobalValueAddress(GlobalValue *V) {
141 return MCE.getGlobalValueAddress(V);
143 uint64_t getGlobalValueAddress(const std::string &Name) {
144 return MCE.getGlobalValueAddress(Name);
146 uint64_t getConstantPoolEntryAddress(unsigned Num) {
147 return MCE.getConstantPoolEntryAddress(Num);
149 uint64_t getCurrentPCValue() {
150 return MCE.getCurrentPCValue();
152 // forceCompilationOf - Force the compilation of the specified function, and
153 // return its address, because we REALLY need the address now.
155 // FIXME: This is JIT specific!
157 virtual uint64_t forceCompilationOf(Function *F) {
158 return MCE.forceCompilationOf(F);
163 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
164 /// code emitter, which just prints the opcodes and fields out the cout. This
165 /// can be used for debugging users of the MachineCodeEmitter interface.
168 MachineCodeEmitter::createDebugEmitter() {
169 return new DebugMachineCodeEmitter();
173 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
174 return new FilePrinterEmitter(MCE, std::cerr);
177 } // End llvm namespace