blob: d9b1f7ce450ce3e60baceacaa395959f927f5496 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000019namespace llvm {
20
Chris Lattnerf815aeb2002-12-03 20:56:42 +000021namespace {
22 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
23 void startFunction(MachineFunction &F) {
24 std::cout << "\n**** Writing machine code for function: "
25 << F.getFunction()->getName() << "\n";
26 }
27 void finishFunction(MachineFunction &F) {
28 std::cout << "\n";
29 }
Chris Lattnere0e72172003-05-09 03:27:41 +000030 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000031 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
32 }
Chris Lattnere0e72172003-05-09 03:27:41 +000033 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000034 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000035 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000036 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000037
38 void emitByte(unsigned char B) {
39 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
40 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000041 void emitWord(unsigned W) {
42 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000043 }
44
Chris Lattnerefc84a42003-06-01 23:22:11 +000045 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
46 uint64_t getGlobalValueAddress(const std::string &Name) { return 0; }
47 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
48 uint64_t getCurrentPCValue() { return 0; }
49
50 // forceCompilationOf - Force the compilation of the specified function, and
51 // return its address, because we REALLY need the address now.
52 //
53 // FIXME: This is JIT specific!
54 //
55 virtual uint64_t forceCompilationOf(Function *F) {
56 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000057 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000058 };
Chris Lattnerf815aeb2002-12-03 20:56:42 +000059
Chris Lattnerefc84a42003-06-01 23:22:11 +000060 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000061 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000062 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000063 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000064 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000065 unsigned values[4];
66
67 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000068 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000069 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000070 openActual();
71 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000072
73 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000074 o << "\n";
75 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000076 }
77
78 void openActual() {
79 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000080 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000081 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
82 abort();
83 }
84 }
85
86 void startFunction(MachineFunction &F) {
87 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000088 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000089 }
90 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000091 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000092 }
93
Misha Brukmand720da22003-06-03 20:00:49 +000094 void emitConstantPool(MachineConstantPool *MCP) {
95 MCE.emitConstantPool(MCP);
96 }
97
Misha Brukman3432d1d2003-05-27 22:43:19 +000098 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000099 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000100 }
101
102 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000103 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000104 }
105
106 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000107 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000108 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000109
110 values[counter] = (unsigned int) B;
111 if (++counter % 4 == 0 && counter != 0) {
112 o << std::hex;
113 for (unsigned i=0; i<4; ++i) {
114 if (values[i] < 16) o << "0";
115 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000116 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000117
118 o << std::dec << "\t";
119 for (unsigned i=0; i<4; ++i) {
120 for (int j=7; j>=0; --j) {
121 o << ((values[i] >> j) & 1);
122 }
123 o << " ";
124 }
125
126 o << "\n";
127
128 unsigned instr = 0;
129 for (unsigned i=0; i<4; ++i)
130 instr |= values[i] << (i*8);
131
132 o << "--- * --- * --- * --- * ---\n";
133 counter %= 4;
134 }
135 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000136
Chris Lattnerefc84a42003-06-01 23:22:11 +0000137 void emitWord(unsigned W) {
138 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000139 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000140 uint64_t getGlobalValueAddress(GlobalValue *V) {
141 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000142 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000143 uint64_t getGlobalValueAddress(const std::string &Name) {
144 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000145 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000146 uint64_t getConstantPoolEntryAddress(unsigned Num) {
147 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000148 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000149 uint64_t getCurrentPCValue() {
150 return MCE.getCurrentPCValue();
151 }
152 // forceCompilationOf - Force the compilation of the specified function, and
153 // return its address, because we REALLY need the address now.
154 //
155 // FIXME: This is JIT specific!
156 //
157 virtual uint64_t forceCompilationOf(Function *F) {
158 return MCE.forceCompilationOf(F);
159 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000160 };
161}
162
Brian Gaeked0fde302003-11-11 22:41:34 +0000163/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
164/// code emitter, which just prints the opcodes and fields out the cout. This
165/// can be used for debugging users of the MachineCodeEmitter interface.
166///
167MachineCodeEmitter *
168MachineCodeEmitter::createDebugEmitter() {
169 return new DebugMachineCodeEmitter();
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}
Brian Gaeked0fde302003-11-11 22:41:34 +0000176
177} // End llvm namespace