blob: 47bdc8b7799cf5025ac15bcd46c753a602727ba4 [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 Brukmane6aa9e32003-06-02 20:49:09 +000063 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000064 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000065 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000066 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000067 unsigned values[4];
68
69 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000070 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000071 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000072 openActual();
73 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000074
75 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000076 o << "\n";
77 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000078 }
79
80 void openActual() {
81 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000082 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000083 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
84 abort();
85 }
86 }
87
88 void startFunction(MachineFunction &F) {
89 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000090 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000091 }
92 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000093 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000094 }
95
Misha Brukmand720da22003-06-03 20:00:49 +000096 void emitConstantPool(MachineConstantPool *MCP) {
97 MCE.emitConstantPool(MCP);
98 }
99
Misha Brukman3432d1d2003-05-27 22:43:19 +0000100 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000101 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000102 }
103
104 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000105 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000106 }
107
108 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000109 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000110 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000111
112 values[counter] = (unsigned int) B;
113 if (++counter % 4 == 0 && counter != 0) {
114 o << std::hex;
115 for (unsigned i=0; i<4; ++i) {
116 if (values[i] < 16) o << "0";
117 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000118 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000119
120 o << std::dec << "\t";
121 for (unsigned i=0; i<4; ++i) {
122 for (int j=7; j>=0; --j) {
123 o << ((values[i] >> j) & 1);
124 }
125 o << " ";
126 }
127
128 o << "\n";
129
130 unsigned instr = 0;
131 for (unsigned i=0; i<4; ++i)
132 instr |= values[i] << (i*8);
133
134 o << "--- * --- * --- * --- * ---\n";
135 counter %= 4;
136 }
137 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000138
Chris Lattnerefc84a42003-06-01 23:22:11 +0000139 void emitWord(unsigned W) {
140 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000141 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000142 uint64_t getGlobalValueAddress(GlobalValue *V) {
143 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000144 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000145 uint64_t getGlobalValueAddress(const std::string &Name) {
146 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000147 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000148 uint64_t getConstantPoolEntryAddress(unsigned Num) {
149 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000150 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000151 uint64_t getCurrentPCValue() {
152 return MCE.getCurrentPCValue();
153 }
154 // forceCompilationOf - Force the compilation of the specified function, and
155 // return its address, because we REALLY need the address now.
156 //
157 // FIXME: This is JIT specific!
158 //
159 virtual uint64_t forceCompilationOf(Function *F) {
160 return MCE.forceCompilationOf(F);
161 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000162 };
163}
164
Chris Lattnerefc84a42003-06-01 23:22:11 +0000165MachineCodeEmitter *
166MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
167 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000168}