blob: fdfd392237846c306ba8b009e6155309839f2600 [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 Lattner0742b592004-02-23 18:38:20 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattnerf815aeb2002-12-03 20:56:42 +000020namespace {
21 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
22 void startFunction(MachineFunction &F) {
23 std::cout << "\n**** Writing machine code for function: "
24 << F.getFunction()->getName() << "\n";
25 }
26 void finishFunction(MachineFunction &F) {
27 std::cout << "\n";
28 }
Chris Lattnere0e72172003-05-09 03:27:41 +000029 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000030 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
31 }
Chris Lattnere0e72172003-05-09 03:27:41 +000032 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000033 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000034 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000035 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000036
37 void emitByte(unsigned char B) {
38 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
39 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000040 void emitWord(unsigned W) {
41 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000042 }
Brian Gaekeda8246b2004-04-23 17:11:13 +000043 void emitWordAt(unsigned W, unsigned *Ptr) {
44 std::cout << "0x" << std::hex << W << std::dec << " (at "
45 << (void*) Ptr << ") ";
46 }
Chris Lattner7775df12003-01-13 00:22:37 +000047
Chris Lattnerefc84a42003-06-01 23:22:11 +000048 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
49 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
50 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
51 uint64_t getCurrentPCValue() { return 0; }
52
53 // forceCompilationOf - Force the compilation of the specified function, and
54 // return its address, because we REALLY need the address now.
55 //
56 // FIXME: This is JIT specific!
57 //
58 virtual uint64_t forceCompilationOf(Function *F) {
59 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000060 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000061 };
Chris Lattnerf815aeb2002-12-03 20:56:42 +000062
Chris Lattnerefc84a42003-06-01 23:22:11 +000063 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000064 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000065 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000066 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000067 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000068 unsigned values[4];
69
70 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000071 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000072 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000073 openActual();
74 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000075
76 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000077 o << "\n";
78 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000079 }
80
81 void openActual() {
82 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000083 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000084 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
85 abort();
86 }
87 }
88
89 void startFunction(MachineFunction &F) {
90 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000091 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000092 }
93 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000094 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000095 }
96
Misha Brukmand720da22003-06-03 20:00:49 +000097 void emitConstantPool(MachineConstantPool *MCP) {
98 MCE.emitConstantPool(MCP);
99 }
100
Misha Brukman3432d1d2003-05-27 22:43:19 +0000101 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000102 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000103 }
104
105 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000106 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000107 }
108
109 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000110 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000111 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000112
113 values[counter] = (unsigned int) B;
114 if (++counter % 4 == 0 && counter != 0) {
115 o << std::hex;
116 for (unsigned i=0; i<4; ++i) {
117 if (values[i] < 16) o << "0";
118 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000119 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000120
121 o << std::dec << "\t";
122 for (unsigned i=0; i<4; ++i) {
123 for (int j=7; j>=0; --j) {
124 o << ((values[i] >> j) & 1);
125 }
126 o << " ";
127 }
128
129 o << "\n";
130
131 unsigned instr = 0;
132 for (unsigned i=0; i<4; ++i)
133 instr |= values[i] << (i*8);
134
135 o << "--- * --- * --- * --- * ---\n";
136 counter %= 4;
137 }
138 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000139
Chris Lattnerefc84a42003-06-01 23:22:11 +0000140 void emitWord(unsigned W) {
141 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000142 }
Brian Gaekeda8246b2004-04-23 17:11:13 +0000143 void emitWordAt(unsigned W, unsigned *Ptr) {
144 MCE.emitWordAt(W, Ptr);
145 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000146 uint64_t getGlobalValueAddress(GlobalValue *V) {
147 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000148 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000149 uint64_t getGlobalValueAddress(const std::string &Name) {
150 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000151 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000152 uint64_t getConstantPoolEntryAddress(unsigned Num) {
153 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000154 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000155 uint64_t getCurrentPCValue() {
156 return MCE.getCurrentPCValue();
157 }
158 // forceCompilationOf - Force the compilation of the specified function, and
159 // return its address, because we REALLY need the address now.
160 //
161 // FIXME: This is JIT specific!
162 //
163 virtual uint64_t forceCompilationOf(Function *F) {
164 return MCE.forceCompilationOf(F);
165 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000166 };
167}
168
Brian Gaeked0fde302003-11-11 22:41:34 +0000169/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
170/// code emitter, which just prints the opcodes and fields out the cout. This
171/// can be used for debugging users of the MachineCodeEmitter interface.
172///
173MachineCodeEmitter *
174MachineCodeEmitter::createDebugEmitter() {
175 return new DebugMachineCodeEmitter();
176}
177
Chris Lattnerefc84a42003-06-01 23:22:11 +0000178MachineCodeEmitter *
179MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
180 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000181}