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 << " ";
44 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
45 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
46 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
47 uint64_t getCurrentPCValue() { return 0; }
49 // forceCompilationOf - Force the compilation of the specified function, and
50 // return its address, because we REALLY need the address now.
52 // FIXME: This is JIT specific!
54 virtual uint64_t forceCompilationOf(Function *F) {
59 class FilePrinterEmitter : public MachineCodeEmitter {
62 MachineCodeEmitter &MCE;
67 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
68 : o(os), MCE(M), counter(0) {
72 ~FilePrinterEmitter() {
78 actual.open("lli.actual.obj");
80 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
85 void startFunction(MachineFunction &F) {
86 // resolve any outstanding calls
89 void finishFunction(MachineFunction &F) {
90 MCE.finishFunction(F);
93 void emitConstantPool(MachineConstantPool *MCP) {
94 MCE.emitConstantPool(MCP);
97 void startFunctionStub(const Function &F, unsigned StubSize) {
98 MCE.startFunctionStub(F, StubSize);
101 void *finishFunctionStub(const Function &F) {
102 return MCE.finishFunctionStub(F);
105 void emitByte(unsigned char B) {
107 actual << B; actual.flush();
109 values[counter] = (unsigned int) B;
110 if (++counter % 4 == 0 && counter != 0) {
112 for (unsigned i=0; i<4; ++i) {
113 if (values[i] < 16) o << "0";
114 o << values[i] << " ";
117 o << std::dec << "\t";
118 for (unsigned i=0; i<4; ++i) {
119 for (int j=7; j>=0; --j) {
120 o << ((values[i] >> j) & 1);
128 for (unsigned i=0; i<4; ++i)
129 instr |= values[i] << (i*8);
131 o << "--- * --- * --- * --- * ---\n";
136 void emitWord(unsigned W) {
139 uint64_t getGlobalValueAddress(GlobalValue *V) {
140 return MCE.getGlobalValueAddress(V);
142 uint64_t getGlobalValueAddress(const std::string &Name) {
143 return MCE.getGlobalValueAddress(Name);
145 uint64_t getConstantPoolEntryAddress(unsigned Num) {
146 return MCE.getConstantPoolEntryAddress(Num);
148 uint64_t getCurrentPCValue() {
149 return MCE.getCurrentPCValue();
151 // forceCompilationOf - Force the compilation of the specified function, and
152 // return its address, because we REALLY need the address now.
154 // FIXME: This is JIT specific!
156 virtual uint64_t forceCompilationOf(Function *F) {
157 return MCE.forceCompilationOf(F);
162 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
163 /// code emitter, which just prints the opcodes and fields out the cout. This
164 /// can be used for debugging users of the MachineCodeEmitter interface.
167 MachineCodeEmitter::createDebugEmitter() {
168 return new DebugMachineCodeEmitter();
172 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
173 return new FilePrinterEmitter(MCE, std::cerr);