blob: 6e56594a10d5abf0a2c000832b6555684530893b [file] [log] [blame]
Chris Lattnerf815aeb2002-12-03 20:56:42 +00001//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerf815aeb2002-12-03 20:56:42 +00009//
10// This file implements the MachineCodeEmitter interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineCodeEmitter.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/Function.h"
Misha Brukman3432d1d2003-05-27 22:43:19 +000017#include <fstream>
Chris Lattnerf815aeb2002-12-03 20:56:42 +000018
19namespace {
20 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
21 void startFunction(MachineFunction &F) {
22 std::cout << "\n**** Writing machine code for function: "
23 << F.getFunction()->getName() << "\n";
24 }
25 void finishFunction(MachineFunction &F) {
26 std::cout << "\n";
27 }
Chris Lattnere0e72172003-05-09 03:27:41 +000028 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000029 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
30 }
Chris Lattnere0e72172003-05-09 03:27:41 +000031 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000032 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000033 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000034 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000035
36 void emitByte(unsigned char B) {
37 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
38 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000039 void emitWord(unsigned W) {
40 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000041 }
42
Chris Lattnerefc84a42003-06-01 23:22:11 +000043 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
44 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
45 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
46 uint64_t getCurrentPCValue() { return 0; }
47
48 // forceCompilationOf - Force the compilation of the specified function, and
49 // return its address, because we REALLY need the address now.
50 //
51 // FIXME: This is JIT specific!
52 //
53 virtual uint64_t forceCompilationOf(Function *F) {
54 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000055 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000056 };
57}
58
59
60/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
61/// code emitter, which just prints the opcodes and fields out the cout. This
62/// can be used for debugging users of the MachineCodeEmitter interface.
63///
Chris Lattnerefc84a42003-06-01 23:22:11 +000064MachineCodeEmitter *MachineCodeEmitter::createDebugEmitter() {
Chris Lattnerf815aeb2002-12-03 20:56:42 +000065 return new DebugMachineCodeEmitter();
66}
Misha Brukman3432d1d2003-05-27 22:43:19 +000067
68namespace {
Chris Lattnerefc84a42003-06-01 23:22:11 +000069 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000070 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000071 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000072 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000073 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000074 unsigned values[4];
75
76 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000077 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000078 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000079 openActual();
80 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000081
82 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000083 o << "\n";
84 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000085 }
86
87 void openActual() {
88 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000089 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000090 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
91 abort();
92 }
93 }
94
95 void startFunction(MachineFunction &F) {
96 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000097 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000098 }
99 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000100 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000101 }
102
Misha Brukmand720da22003-06-03 20:00:49 +0000103 void emitConstantPool(MachineConstantPool *MCP) {
104 MCE.emitConstantPool(MCP);
105 }
106
Misha Brukman3432d1d2003-05-27 22:43:19 +0000107 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000108 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000109 }
110
111 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000112 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000113 }
114
115 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000116 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000117 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000118
119 values[counter] = (unsigned int) B;
120 if (++counter % 4 == 0 && counter != 0) {
121 o << std::hex;
122 for (unsigned i=0; i<4; ++i) {
123 if (values[i] < 16) o << "0";
124 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000125 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000126
127 o << std::dec << "\t";
128 for (unsigned i=0; i<4; ++i) {
129 for (int j=7; j>=0; --j) {
130 o << ((values[i] >> j) & 1);
131 }
132 o << " ";
133 }
134
135 o << "\n";
136
137 unsigned instr = 0;
138 for (unsigned i=0; i<4; ++i)
139 instr |= values[i] << (i*8);
140
141 o << "--- * --- * --- * --- * ---\n";
142 counter %= 4;
143 }
144 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000145
Chris Lattnerefc84a42003-06-01 23:22:11 +0000146 void emitWord(unsigned W) {
147 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000148 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000149 uint64_t getGlobalValueAddress(GlobalValue *V) {
150 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000151 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000152 uint64_t getGlobalValueAddress(const std::string &Name) {
153 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000154 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000155 uint64_t getConstantPoolEntryAddress(unsigned Num) {
156 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000157 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000158 uint64_t getCurrentPCValue() {
159 return MCE.getCurrentPCValue();
160 }
161 // forceCompilationOf - Force the compilation of the specified function, and
162 // return its address, because we REALLY need the address now.
163 //
164 // FIXME: This is JIT specific!
165 //
166 virtual uint64_t forceCompilationOf(Function *F) {
167 return MCE.forceCompilationOf(F);
168 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000169 };
170}
171
Chris Lattnerefc84a42003-06-01 23:22:11 +0000172MachineCodeEmitter *
173MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
174 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000175}