blob: fde69697642434176a085cffb66689bc199af5e3 [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>
Reid Spencer954da372004-07-04 12:19:56 +000018#include <iostream>
19
Chris Lattner0742b592004-02-23 18:38:20 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattnerf815aeb2002-12-03 20:56:42 +000022namespace {
23 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
24 void startFunction(MachineFunction &F) {
25 std::cout << "\n**** Writing machine code for function: "
26 << F.getFunction()->getName() << "\n";
27 }
28 void finishFunction(MachineFunction &F) {
29 std::cout << "\n";
30 }
Chris Lattnere0e72172003-05-09 03:27:41 +000031 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000032 std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
33 }
Chris Lattnere0e72172003-05-09 03:27:41 +000034 void *finishFunctionStub(const Function &F) {
Chris Lattnerfd33fb82003-05-08 21:54:18 +000035 std::cout << "\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000036 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000037 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000038
39 void emitByte(unsigned char B) {
40 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
41 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000042 void emitWord(unsigned W) {
43 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000044 }
Brian Gaekeda8246b2004-04-23 17:11:13 +000045 void emitWordAt(unsigned W, unsigned *Ptr) {
46 std::cout << "0x" << std::hex << W << std::dec << " (at "
47 << (void*) Ptr << ") ";
48 }
Chris Lattner7775df12003-01-13 00:22:37 +000049
Chris Lattner47012c02004-11-20 03:44:39 +000050 void addRelocation(const MachineRelocation &MR) {
51 std::cout << "<relocation> ";
52 }
53
Chris Lattnerefc84a42003-06-01 23:22:11 +000054 uint64_t getGlobalValueAddress(GlobalValue *V) { return 0; }
Chris Lattner45730d72004-11-19 20:56:46 +000055 uint64_t getGlobalValueAddress(const char *Name) { return 0; }
Chris Lattnerefc84a42003-06-01 23:22:11 +000056 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
57 uint64_t getCurrentPCValue() { return 0; }
Chris Lattner47012c02004-11-20 03:44:39 +000058 uint64_t getCurrentPCOffset() { return 0; }
Chris Lattnerefc84a42003-06-01 23:22:11 +000059
60 // forceCompilationOf - Force the compilation of the specified function, and
61 // return its address, because we REALLY need the address now.
62 //
63 // FIXME: This is JIT specific!
64 //
65 virtual uint64_t forceCompilationOf(Function *F) {
66 return 0;
Chris Lattner7775df12003-01-13 00:22:37 +000067 }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000068 };
Chris Lattnerf815aeb2002-12-03 20:56:42 +000069
Chris Lattnerefc84a42003-06-01 23:22:11 +000070 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000071 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000072 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000073 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000074 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000075 unsigned values[4];
76
77 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000078 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000079 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000080 openActual();
81 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000082
83 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000084 o << "\n";
85 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000086 }
87
88 void openActual() {
89 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000090 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000091 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
92 abort();
93 }
94 }
95
96 void startFunction(MachineFunction &F) {
97 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000098 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000099 }
100 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000101 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000102 }
103
Misha Brukmand720da22003-06-03 20:00:49 +0000104 void emitConstantPool(MachineConstantPool *MCP) {
105 MCE.emitConstantPool(MCP);
106 }
107
Misha Brukman3432d1d2003-05-27 22:43:19 +0000108 void startFunctionStub(const Function &F, unsigned StubSize) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000109 MCE.startFunctionStub(F, StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000110 }
111
112 void *finishFunctionStub(const Function &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000113 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000114 }
115
116 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000117 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000118 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000119
120 values[counter] = (unsigned int) B;
121 if (++counter % 4 == 0 && counter != 0) {
122 o << std::hex;
123 for (unsigned i=0; i<4; ++i) {
124 if (values[i] < 16) o << "0";
125 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000126 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000127
128 o << std::dec << "\t";
129 for (unsigned i=0; i<4; ++i) {
130 for (int j=7; j>=0; --j) {
131 o << ((values[i] >> j) & 1);
132 }
133 o << " ";
134 }
135
136 o << "\n";
137
138 unsigned instr = 0;
139 for (unsigned i=0; i<4; ++i)
140 instr |= values[i] << (i*8);
141
142 o << "--- * --- * --- * --- * ---\n";
143 counter %= 4;
144 }
145 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000146
Chris Lattnerefc84a42003-06-01 23:22:11 +0000147 void emitWord(unsigned W) {
148 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000149 }
Brian Gaekeda8246b2004-04-23 17:11:13 +0000150 void emitWordAt(unsigned W, unsigned *Ptr) {
151 MCE.emitWordAt(W, Ptr);
152 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000153 uint64_t getGlobalValueAddress(GlobalValue *V) {
154 return MCE.getGlobalValueAddress(V);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000155 }
Chris Lattner45730d72004-11-19 20:56:46 +0000156 uint64_t getGlobalValueAddress(const char *Name) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000157 return MCE.getGlobalValueAddress(Name);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000158 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000159 uint64_t getConstantPoolEntryAddress(unsigned Num) {
160 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000161 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000162 uint64_t getCurrentPCValue() {
163 return MCE.getCurrentPCValue();
164 }
Chris Lattner47012c02004-11-20 03:44:39 +0000165 uint64_t getCurrentPCOffset() {
166 return MCE.getCurrentPCOffset();
167 }
168 void addRelocation(const MachineRelocation &MR) {
169 return MCE.addRelocation(MR);
170 }
171
Chris Lattnerefc84a42003-06-01 23:22:11 +0000172 // forceCompilationOf - Force the compilation of the specified function, and
173 // return its address, because we REALLY need the address now.
174 //
175 // FIXME: This is JIT specific!
176 //
177 virtual uint64_t forceCompilationOf(Function *F) {
178 return MCE.forceCompilationOf(F);
179 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000180 };
181}
182
Brian Gaeked0fde302003-11-11 22:41:34 +0000183/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
184/// code emitter, which just prints the opcodes and fields out the cout. This
185/// can be used for debugging users of the MachineCodeEmitter interface.
186///
187MachineCodeEmitter *
188MachineCodeEmitter::createDebugEmitter() {
189 return new DebugMachineCodeEmitter();
190}
191
Chris Lattnerefc84a42003-06-01 23:22:11 +0000192MachineCodeEmitter *
193MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
194 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000195}