607480c75dfac7c1bc7685ac3bd0c4b535dec540
[oota-llvm.git] / lib / CodeGen / MachineCodeEmitter.cpp
1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
2 //
3 // This file implements the MachineCodeEmitter interface.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/CodeGen/MachineCodeEmitter.h"
8 #include "llvm/CodeGen/MachineFunction.h"
9 #include "llvm/Function.h"
10 #include <iostream>
11 #include <fstream>
12
13 namespace {
14   struct DebugMachineCodeEmitter : public MachineCodeEmitter {
15     void startFunction(MachineFunction &F) {
16       std::cout << "\n**** Writing machine code for function: "
17                 << F.getFunction()->getName() << "\n";
18     }
19     void finishFunction(MachineFunction &F) {
20       std::cout << "\n";
21     }
22     void startBasicBlock(MachineBasicBlock &BB) {
23       std::cout << "\n--- Basic Block: " << BB.getBasicBlock()->getName()<<"\n";
24     }
25
26     void startFunctionStub(const Function &F, unsigned StubSize) {
27       std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
28     }
29     void *finishFunctionStub(const Function &F) {
30       std::cout << "\n";
31       return 0;
32     }
33     
34     void emitByte(unsigned char B) {
35       std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
36     }
37     void emitPCRelativeDisp(Value *V) {
38       std::cout << "<disp %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
39     }
40     void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
41       std::cout << "<addr %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
42     }
43     void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
44       std::cout << "<addr %" << Name << ": 0xXX 0xXX 0xXX 0xXX> ";
45     }
46
47     void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
48       std::cout << "<addr const#" << ConstantNum;
49       if (Offset) std::cout << " + " << Offset;
50       std::cout << "> ";
51     }
52   };
53 }
54
55
56 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
57 /// code emitter, which just prints the opcodes and fields out the cout.  This
58 /// can be used for debugging users of the MachineCodeEmitter interface.
59 ///
60 MachineCodeEmitter *MachineCodeEmitter::createDebugMachineCodeEmitter() {
61   return new DebugMachineCodeEmitter();
62 }
63
64 namespace {
65   class FilePrinterMachineCodeEmitter : public MachineCodeEmitter {
66     std::ofstream f, actual;
67     std::ostream &o;
68     MachineCodeEmitter *MCE;
69     unsigned counter;
70     bool mustClose;
71     unsigned values[4];
72     
73   public:
74     FilePrinterMachineCodeEmitter() :
75       f("lli.out"), o(f), counter(0), mustClose(true)
76     {
77       if (! f.good()) {
78         std::cerr << "Cannot open 'lli.out' for writing\n";
79         abort();
80       }
81       openActual();
82     }
83
84     FilePrinterMachineCodeEmitter(MachineCodeEmitter &M, std::ostream &os) :
85       o(os), MCE(&M), counter(0)
86     {
87       FilePrinterMachineCodeEmitter();
88       mustClose = false;
89       openActual();
90     }
91
92     ~FilePrinterMachineCodeEmitter() { 
93       o << "\n";
94       actual.close();
95       if (mustClose) f.close();
96     }
97
98     void openActual() {
99       actual.open("lli.actual.obj");
100       if (! actual.good()) {
101         std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
102         abort();
103       }
104     }
105
106     void startFunction(MachineFunction &F) {
107       // resolve any outstanding calls
108       if (MCE) MCE->startFunction(F);
109     }
110     void finishFunction(MachineFunction &F) {
111       if (MCE) MCE->finishFunction(F);
112     }
113
114     void startBasicBlock(MachineBasicBlock &BB) {
115       // if any instructions were waiting for the address of this block,
116       // let them fix their addresses now
117       if (MCE) MCE->startBasicBlock(BB);
118     }
119
120     void startFunctionStub(const Function &F, unsigned StubSize) {
121       //
122       if (MCE) MCE->startFunctionStub(F, StubSize);
123     }
124
125     void *finishFunctionStub(const Function &F) {
126       if (MCE) return MCE->finishFunctionStub(F);
127       else return 0;
128     }
129     
130     void emitByte(unsigned char B) {
131       if (MCE) MCE->emitByte(B);
132       actual << B; actual.flush();
133
134       values[counter] = (unsigned int) B;
135       if (++counter % 4 == 0 && counter != 0) {
136         o << std::hex;
137         for (unsigned i=0; i<4; ++i) {
138           if (values[i] < 16) o << "0";
139           o << values[i] << " ";
140         }
141
142         o << std::dec << "\t";
143         for (unsigned i=0; i<4; ++i) {
144           for (int j=7; j>=0; --j) {
145             o << ((values[i] >> j) & 1);
146           }
147           o << " ";
148         }
149
150         o << "\n";
151
152         unsigned instr = 0;
153         for (unsigned i=0; i<4; ++i)
154           instr |= values[i] << (i*8);
155
156         o << "--- * --- * --- * --- * ---\n";
157         counter %= 4;
158       }
159     }
160     void emitPCRelativeDisp(Value *V) {
161       if (MCE) MCE->emitPCRelativeDisp(V);
162     }
163
164     void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
165       if (MCE) MCE->emitGlobalAddress(V, isPCRelative);
166     }
167     void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
168       if (MCE) MCE->emitGlobalAddress(Name, isPCRelative);
169     }
170
171     void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
172       if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset);
173     }
174
175     virtual void saveBBreference(BasicBlock* BB, MachineInstr &MI) {
176       if (MCE) MCE->saveBBreference(BB, MI);
177     }
178
179   };
180 }
181
182 MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter
183 (MachineCodeEmitter &MCE) {
184   return new FilePrinterMachineCodeEmitter(MCE, std::cerr);
185 }