blob: 680a8d1ad32862a36a8be225e833843136032427 [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 }
43
Chris Lattnerefc84a42003-06-01 23:22:11 +000044 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
45 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
46 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
47 uint64_t getCurrentPCValue() { return 0; }
48
49 // forceCompilationOf - Force the compilation of the specified function, and
50 // return its address, because we REALLY need the address now.
51 //
52 // FIXME: This is JIT specific!
53 //
54 virtual uint64_t forceCompilationOf(Function *F) {
55 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000056 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000057 };
Chris Lattnerf815aeb2002-12-03 20:56:42 +000058
Chris Lattnerefc84a42003-06-01 23:22:11 +000059 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000060 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000061 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000062 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000063 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000064 unsigned values[4];
65
66 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000067 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000068 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000069 openActual();
70 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000071
72 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000073 o << "\n";
74 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000075 }
76
77 void openActual() {
78 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000079 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000080 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
81 abort();
82 }
83 }
84
85 void startFunction(MachineFunction &F) {
86 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000087 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000088 }
89 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000090 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000091 }
92
Misha Brukmand720da22003-06-03 20:00:49 +000093 void emitConstantPool(MachineConstantPool *MCP) {
94 MCE.emitConstantPool(MCP);
95 }
96
Misha Brukman3432d1d2003-05-27 22:43:19 +000097 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000098 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +000099 }
100
101 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000102 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000103 }
104
105 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000106 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000107 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000108
109 values[counter] = (unsigned int) B;
110 if (++counter % 4 == 0 && counter != 0) {
111 o << std::hex;
112 for (unsigned i=0; i<4; ++i) {
113 if (values[i] < 16) o << "0";
114 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000115 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000116
117 o << std::dec << "\t";
118 for (unsigned i=0; i<4; ++i) {
119 for (int j=7; j>=0; --j) {
120 o << ((values[i] >> j) & 1);
121 }
122 o << " ";
123 }
124
125 o << "\n";
126
127 unsigned instr = 0;
128 for (unsigned i=0; i<4; ++i)
129 instr |= values[i] << (i*8);
130
131 o << "--- * --- * --- * --- * ---\n";
132 counter %= 4;
133 }
134 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000135
Chris Lattnerefc84a42003-06-01 23:22:11 +0000136 void emitWord(unsigned W) {
137 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000138 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000139 uint64_t getGlobalValueAddress(GlobalValue *V) {
140 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000141 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000142 uint64_t getGlobalValueAddress(const std::string &Name) {
143 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000144 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000145 uint64_t getConstantPoolEntryAddress(unsigned Num) {
146 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000147 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000148 uint64_t getCurrentPCValue() {
149 return MCE.getCurrentPCValue();
150 }
151 // forceCompilationOf - Force the compilation of the specified function, and
152 // return its address, because we REALLY need the address now.
153 //
154 // FIXME: This is JIT specific!
155 //
156 virtual uint64_t forceCompilationOf(Function *F) {
157 return MCE.forceCompilationOf(F);
158 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000159 };
160}
161
Brian Gaeked0fde302003-11-11 22:41:34 +0000162/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
163/// code emitter, which just prints the opcodes and fields out the cout. This
164/// can be used for debugging users of the MachineCodeEmitter interface.
165///
166MachineCodeEmitter *
167MachineCodeEmitter::createDebugEmitter() {
168 return new DebugMachineCodeEmitter();
169}
170
Chris Lattnerefc84a42003-06-01 23:22:11 +0000171MachineCodeEmitter *
172MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
173 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000174}