blob: ae4d7cecec6b6fdade0cf365e90f676cd53caf49 [file] [log] [blame]
Chris Lattnerf815aeb2002-12-03 20:56:42 +00001//===-- 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>
Misha Brukman3432d1d2003-05-27 22:43:19 +000011#include <fstream>
Chris Lattnerf815aeb2002-12-03 20:56:42 +000012
13namespace {
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 }
Chris Lattnerfd33fb82003-05-08 21:54:18 +000025
Chris Lattnere0e72172003-05-09 03:27:41 +000026 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000027 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
28 }
Chris Lattnere0e72172003-05-09 03:27:41 +000029 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000030 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000031 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000032 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000033
34 void emitByte(unsigned char B) {
35 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
36 }
37 void emitPCRelativeDisp(Value *V) {
Chris Lattnerb72d2212002-12-04 06:44:41 +000038 std::cout << "<disp %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
39 }
Chris Lattner7775df12003-01-13 00:22:37 +000040 void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
Chris Lattnerb72d2212002-12-04 06:44:41 +000041 std::cout << "<addr %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
Chris Lattnerf815aeb2002-12-03 20:56:42 +000042 }
Chris Lattner7775df12003-01-13 00:22:37 +000043 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 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000052 };
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///
60MachineCodeEmitter *MachineCodeEmitter::createDebugMachineCodeEmitter() {
61 return new DebugMachineCodeEmitter();
62}
Misha Brukman3432d1d2003-05-27 22:43:19 +000063
64namespace {
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
133 values[counter] = (unsigned int) B;
134 if (++counter % 4 == 0 && counter != 0) {
135 o << std::hex;
136 for (unsigned i=0; i<4; ++i) {
137 if (values[i] < 16) o << "0";
138 o << values[i] << " ";
139 actual << values[i];
140 }
141 actual.flush();
142
143 o << std::dec << "\t";
144 for (unsigned i=0; i<4; ++i) {
145 for (int j=7; j>=0; --j) {
146 o << ((values[i] >> j) & 1);
147 }
148 o << " ";
149 }
150
151 o << "\n";
152
153 unsigned instr = 0;
154 for (unsigned i=0; i<4; ++i)
155 instr |= values[i] << (i*8);
156
157 o << "--- * --- * --- * --- * ---\n";
158 counter %= 4;
159 }
160 }
161 void emitPCRelativeDisp(Value *V) {
162 // put block in mapping BB -> { instr, address }. when BB is beginning to
163 // output, find instr, set disp, overwrite instr at addr using the
164 // unsigned value gotten from emitter
165 }
166
167 void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
168 if (MCE) MCE->emitGlobalAddress(V, isPCRelative);
169 }
170 void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
171 if (MCE) MCE->emitGlobalAddress(Name, isPCRelative);
172 }
173
174 void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
175 if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset);
176 }
177 };
178}
179
180MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter(MachineCodeEmitter &MCE) {
181 return new FilePrinterMachineCodeEmitter(MCE, std::cerr);
182}