blob: 69d6b38c7875e799ae352cc3986f0787af2d5428 [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"
Misha Brukman3432d1d2003-05-27 22:43:19 +000010#include <fstream>
Chris Lattnerf815aeb2002-12-03 20:56:42 +000011
12namespace {
13 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
14 void startFunction(MachineFunction &F) {
15 std::cout << "\n**** Writing machine code for function: "
16 << F.getFunction()->getName() << "\n";
17 }
18 void finishFunction(MachineFunction &F) {
19 std::cout << "\n";
20 }
Chris Lattnere0e72172003-05-09 03:27:41 +000021 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000022 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
23 }
Chris Lattnere0e72172003-05-09 03:27:41 +000024 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000025 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000026 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000027 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000028
29 void emitByte(unsigned char B) {
30 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
31 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000032 void emitWord(unsigned W) {
33 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000034 }
35
Chris Lattnerefc84a42003-06-01 23:22:11 +000036 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
37 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
38 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
39 uint64_t getCurrentPCValue() { return 0; }
40
41 // forceCompilationOf - Force the compilation of the specified function, and
42 // return its address, because we REALLY need the address now.
43 //
44 // FIXME: This is JIT specific!
45 //
46 virtual uint64_t forceCompilationOf(Function *F) {
47 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000048 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000049 };
50}
51
52
53/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
54/// code emitter, which just prints the opcodes and fields out the cout. This
55/// can be used for debugging users of the MachineCodeEmitter interface.
56///
Chris Lattnerefc84a42003-06-01 23:22:11 +000057MachineCodeEmitter *MachineCodeEmitter::createDebugEmitter() {
Chris Lattnerf815aeb2002-12-03 20:56:42 +000058 return new DebugMachineCodeEmitter();
59}
Misha Brukman3432d1d2003-05-27 22:43:19 +000060
61namespace {
Chris Lattnerefc84a42003-06-01 23:22:11 +000062 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukman3432d1d2003-05-27 22:43:19 +000063 std::ofstream f, actual;
64 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000065 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000066 unsigned counter;
67 bool mustClose;
68 unsigned values[4];
69
70 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000071 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
72 : f("lli.out"), o(os), MCE(M), counter(0), mustClose(false) {
73 if (!f.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000074 std::cerr << "Cannot open 'lli.out' for writing\n";
75 abort();
76 }
77 openActual();
78 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000079
80 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000081 o << "\n";
82 actual.close();
83 if (mustClose) f.close();
84 }
85
86 void openActual() {
87 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000088 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000089 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
90 abort();
91 }
92 }
93
94 void startFunction(MachineFunction &F) {
95 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000096 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000097 }
98 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000099 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000100 }
101
102 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000103 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000104 }
105
106 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000107 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000108 }
109
110 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000111 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000112 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000113
114 values[counter] = (unsigned int) B;
115 if (++counter % 4 == 0 && counter != 0) {
116 o << std::hex;
117 for (unsigned i=0; i<4; ++i) {
118 if (values[i] < 16) o << "0";
119 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000120 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000121
122 o << std::dec << "\t";
123 for (unsigned i=0; i<4; ++i) {
124 for (int j=7; j>=0; --j) {
125 o << ((values[i] >> j) & 1);
126 }
127 o << " ";
128 }
129
130 o << "\n";
131
132 unsigned instr = 0;
133 for (unsigned i=0; i<4; ++i)
134 instr |= values[i] << (i*8);
135
136 o << "--- * --- * --- * --- * ---\n";
137 counter %= 4;
138 }
139 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000140
Chris Lattnerefc84a42003-06-01 23:22:11 +0000141 void emitWord(unsigned W) {
142 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000143 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000144 uint64_t getGlobalValueAddress(GlobalValue *V) {
145 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000146 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000147 uint64_t getGlobalValueAddress(const std::string &Name) {
148 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000149 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000150 uint64_t getConstantPoolEntryAddress(unsigned Num) {
151 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000152 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000153 uint64_t getCurrentPCValue() {
154 return MCE.getCurrentPCValue();
155 }
156 // forceCompilationOf - Force the compilation of the specified function, and
157 // return its address, because we REALLY need the address now.
158 //
159 // FIXME: This is JIT specific!
160 //
161 virtual uint64_t forceCompilationOf(Function *F) {
162 return MCE.forceCompilationOf(F);
163 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000164 };
165}
166
Chris Lattnerefc84a42003-06-01 23:22:11 +0000167MachineCodeEmitter *
168MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
169 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000170}