blob: af41ce468cf34bb6a2232e17fd9984b26710ca2d [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
96 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000097 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +000098 }
99
100 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000101 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000102 }
103
104 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000105 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000106 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000107
108 values[counter] = (unsigned int) B;
109 if (++counter % 4 == 0 && counter != 0) {
110 o << std::hex;
111 for (unsigned i=0; i<4; ++i) {
112 if (values[i] < 16) o << "0";
113 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000114 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000115
116 o << std::dec << "\t";
117 for (unsigned i=0; i<4; ++i) {
118 for (int j=7; j>=0; --j) {
119 o << ((values[i] >> j) & 1);
120 }
121 o << " ";
122 }
123
124 o << "\n";
125
126 unsigned instr = 0;
127 for (unsigned i=0; i<4; ++i)
128 instr |= values[i] << (i*8);
129
130 o << "--- * --- * --- * --- * ---\n";
131 counter %= 4;
132 }
133 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000134
Chris Lattnerefc84a42003-06-01 23:22:11 +0000135 void emitWord(unsigned W) {
136 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000137 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000138 uint64_t getGlobalValueAddress(GlobalValue *V) {
139 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000140 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000141 uint64_t getGlobalValueAddress(const std::string &Name) {
142 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000143 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000144 uint64_t getConstantPoolEntryAddress(unsigned Num) {
145 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000146 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000147 uint64_t getCurrentPCValue() {
148 return MCE.getCurrentPCValue();
149 }
150 // forceCompilationOf - Force the compilation of the specified function, and
151 // return its address, because we REALLY need the address now.
152 //
153 // FIXME: This is JIT specific!
154 //
155 virtual uint64_t forceCompilationOf(Function *F) {
156 return MCE.forceCompilationOf(F);
157 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000158 };
159}
160
Chris Lattnerefc84a42003-06-01 23:22:11 +0000161MachineCodeEmitter *
162MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
163 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000164}