1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
3 // This file implements the MachineCodeEmitter interface.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/CodeGen/MachineCodeEmitter.h"
8 #include "llvm/CodeGen/MachineFunction.h"
9 #include "llvm/Function.h"
14 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
15 void startFunction(MachineFunction &F) {
16 std::cout << "\n**** Writing machine code for function: "
17 << F.getFunction()->getName() << "\n";
19 void finishFunction(MachineFunction &F) {
22 void startBasicBlock(MachineBasicBlock &BB) {
23 std::cout << "\n--- Basic Block: " << BB.getBasicBlock()->getName()<<"\n";
26 void startFunctionStub(const Function &F, unsigned StubSize) {
27 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
29 void *finishFunctionStub(const Function &F) {
34 void emitByte(unsigned char B) {
35 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
37 void emitPCRelativeDisp(Value *V) {
38 std::cout << "<disp %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
40 void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
41 std::cout << "<addr %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
43 void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
44 std::cout << "<addr %" << Name << ": 0xXX 0xXX 0xXX 0xXX> ";
47 void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
48 std::cout << "<addr const#" << ConstantNum;
49 if (Offset) std::cout << " + " << Offset;
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.
60 MachineCodeEmitter *MachineCodeEmitter::createDebugMachineCodeEmitter() {
61 return new DebugMachineCodeEmitter();
65 class FilePrinterMachineCodeEmitter : public MachineCodeEmitter {
66 std::ofstream f, actual;
68 MachineCodeEmitter *MCE;
74 FilePrinterMachineCodeEmitter() :
75 f("lli.out"), o(f), counter(0), mustClose(true)
78 std::cerr << "Cannot open 'lli.out' for writing\n";
84 FilePrinterMachineCodeEmitter(MachineCodeEmitter &M, std::ostream &os) :
85 o(os), MCE(&M), counter(0)
87 FilePrinterMachineCodeEmitter();
92 ~FilePrinterMachineCodeEmitter() {
95 if (mustClose) f.close();
99 actual.open("lli.actual.obj");
100 if (! actual.good()) {
101 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
106 void startFunction(MachineFunction &F) {
107 // resolve any outstanding calls
108 if (MCE) MCE->startFunction(F);
110 void finishFunction(MachineFunction &F) {
111 if (MCE) MCE->finishFunction(F);
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);
120 void startFunctionStub(const Function &F, unsigned StubSize) {
122 if (MCE) MCE->startFunctionStub(F, StubSize);
125 void *finishFunctionStub(const Function &F) {
126 if (MCE) return MCE->finishFunctionStub(F);
130 void emitByte(unsigned char B) {
131 if (MCE) MCE->emitByte(B);
133 values[counter] = (unsigned int) B;
134 if (++counter % 4 == 0 && counter != 0) {
136 for (unsigned i=0; i<4; ++i) {
137 if (values[i] < 16) o << "0";
138 o << values[i] << " ";
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);
154 for (unsigned i=0; i<4; ++i)
155 instr |= values[i] << (i*8);
157 o << "--- * --- * --- * --- * ---\n";
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
167 void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
168 if (MCE) MCE->emitGlobalAddress(V, isPCRelative);
170 void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
171 if (MCE) MCE->emitGlobalAddress(Name, isPCRelative);
174 void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
175 if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset);
180 MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter(MachineCodeEmitter &MCE) {
181 return new FilePrinterMachineCodeEmitter(MCE, std::cerr);