Fix the FIXME, nuke the JIT specific forceCompilationOf method.
[oota-llvm.git] / lib / CodeGen / MachineCodeEmitter.cpp
1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the MachineCodeEmitter interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/MachineCodeEmitter.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Function.h"
17 #include <fstream>
18 #include <iostream>
19
20 using namespace llvm;
21
22 namespace {
23   struct DebugMachineCodeEmitter : public MachineCodeEmitter {
24     void startFunction(MachineFunction &F) {
25       std::cout << "\n**** Writing machine code for function: "
26                 << F.getFunction()->getName() << "\n";
27     }
28     void finishFunction(MachineFunction &F) {
29       std::cout << "\n";
30     }
31     void startFunctionStub(unsigned StubSize) {
32       std::cout << "\n--- Function stub:\n";
33     }
34     void *finishFunctionStub(const Function *F) {
35       std::cout << "\n--- End of stub for Function\n";
36       return 0;
37     }
38     
39     void emitByte(unsigned char B) {
40       std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
41     }
42     void emitWord(unsigned W) {
43       std::cout << "0x" << std::hex << W << std::dec << " ";
44     }
45     void emitWordAt(unsigned W, unsigned *Ptr) {
46       std::cout << "0x" << std::hex << W << std::dec << " (at "
47                 << (void*) Ptr << ") ";
48     }
49
50     void addRelocation(const MachineRelocation &MR) {
51       std::cout << "<relocation> ";
52     }
53
54     uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
55     uint64_t getCurrentPCValue() { return 0; }
56     uint64_t getCurrentPCOffset() { return 0; }
57   };
58
59   class FilePrinterEmitter : public MachineCodeEmitter {
60     std::ofstream actual;
61     std::ostream &o;
62     MachineCodeEmitter &MCE;
63     unsigned counter;
64     unsigned values[4];
65     
66   public:
67     FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
68       : o(os), MCE(M), counter(0) {
69       openActual();
70     }
71     
72     ~FilePrinterEmitter() { 
73       o << "\n";
74       actual.close();
75     }
76
77     void openActual() {
78       actual.open("lli.actual.obj");
79       if (!actual.good()) {
80         std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
81         abort();
82       }
83     }
84
85     void startFunction(MachineFunction &F) {
86       // resolve any outstanding calls
87       MCE.startFunction(F);
88     }
89     void finishFunction(MachineFunction &F) {
90       MCE.finishFunction(F);
91     }
92
93     void emitConstantPool(MachineConstantPool *MCP) {
94       MCE.emitConstantPool(MCP);
95     }
96
97     void startFunctionStub(unsigned StubSize) {
98       MCE.startFunctionStub(StubSize);
99     }
100
101     void *finishFunctionStub(const Function *F) {
102       return MCE.finishFunctionStub(F);
103     }
104     
105     void emitByte(unsigned char B) {
106       MCE.emitByte(B);
107       actual << B; actual.flush();
108
109       values[counter] = (unsigned int) B;
110       if (++counter % 4 == 0 && counter != 0) {
111         o << std::hex;
112         for (unsigned i=0; i<4; ++i) {
113           if (values[i] < 16) o << "0";
114           o << values[i] << " ";
115         }
116
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);
121           }
122           o << " ";
123         }
124
125         o << "\n";
126
127         unsigned instr = 0;
128         for (unsigned i=0; i<4; ++i)
129           instr |= values[i] << (i*8);
130
131         o << "--- * --- * --- * --- * ---\n";
132         counter %= 4;
133       }
134     }
135
136     void emitWord(unsigned W) {
137       MCE.emitWord(W);
138     }
139     void emitWordAt(unsigned W, unsigned *Ptr) {
140       MCE.emitWordAt(W, Ptr);
141     }
142     uint64_t getConstantPoolEntryAddress(unsigned Num) {
143       return MCE.getConstantPoolEntryAddress(Num);
144     }
145     uint64_t getCurrentPCValue() {
146       return MCE.getCurrentPCValue();
147     }
148     uint64_t getCurrentPCOffset() {
149       return MCE.getCurrentPCOffset();
150     }
151     void addRelocation(const MachineRelocation &MR) {
152       return MCE.addRelocation(MR);
153     }
154   };
155 }
156
157 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
158 /// code emitter, which just prints the opcodes and fields out the cout.  This
159 /// can be used for debugging users of the MachineCodeEmitter interface.
160 ///
161 MachineCodeEmitter *
162 MachineCodeEmitter::createDebugEmitter() {
163   return new DebugMachineCodeEmitter();
164 }
165
166 MachineCodeEmitter *
167 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
168   return new FilePrinterEmitter(MCE, std::cerr);
169 }