blob: 4243076d3e5811b6e3ff668ec3d2da15ea70feaa [file] [log] [blame]
Chris Lattnerf815aeb2002-12-03 20:56:42 +00001//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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>
Duraid Madina8c7bd362005-12-28 02:44:35 +000019#include <ios>
Reid Spencer954da372004-07-04 12:19:56 +000020
Chris Lattner0742b592004-02-23 18:38:20 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattnerf815aeb2002-12-03 20:56:42 +000023namespace {
24 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
25 void startFunction(MachineFunction &F) {
26 std::cout << "\n**** Writing machine code for function: "
27 << F.getFunction()->getName() << "\n";
28 }
29 void finishFunction(MachineFunction &F) {
30 std::cout << "\n";
31 }
Chris Lattner3bf285a2004-11-20 23:53:26 +000032 void startFunctionStub(unsigned StubSize) {
33 std::cout << "\n--- Function stub:\n";
Chris Lattnerfd33fb82003-05-08 21:54:18 +000034 }
Chris Lattner3bf285a2004-11-20 23:53:26 +000035 void *finishFunctionStub(const Function *F) {
36 std::cout << "\n--- End of stub for Function\n";
Chris Lattnere0e72172003-05-09 03:27:41 +000037 return 0;
Chris Lattnerfd33fb82003-05-08 21:54:18 +000038 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000039
Chris Lattnerf815aeb2002-12-03 20:56:42 +000040 void emitByte(unsigned char B) {
41 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
42 }
Chris Lattnerefc84a42003-06-01 23:22:11 +000043 void emitWord(unsigned W) {
44 std::cout << "0x" << std::hex << W << std::dec << " ";
Chris Lattner7775df12003-01-13 00:22:37 +000045 }
Brian Gaekeda8246b2004-04-23 17:11:13 +000046 void emitWordAt(unsigned W, unsigned *Ptr) {
47 std::cout << "0x" << std::hex << W << std::dec << " (at "
48 << (void*) Ptr << ") ";
49 }
Chris Lattner7775df12003-01-13 00:22:37 +000050
Chris Lattner47012c02004-11-20 03:44:39 +000051 void addRelocation(const MachineRelocation &MR) {
52 std::cout << "<relocation> ";
53 }
54
Andrew Lenharthfe660392005-07-28 18:13:59 +000055 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
56 { return 0; }
57
Chris Lattnerefc84a42003-06-01 23:22:11 +000058 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
Nate Begeman37efe672006-04-22 18:53:45 +000059 uint64_t getJumpTableEntryAddress(unsigned Num) { return 0; }
Chris Lattnerefc84a42003-06-01 23:22:11 +000060 uint64_t getCurrentPCValue() { return 0; }
Chris Lattner47012c02004-11-20 03:44:39 +000061 uint64_t getCurrentPCOffset() { return 0; }
Chris Lattnerf815aeb2002-12-03 20:56:42 +000062 };
Chris Lattnerf815aeb2002-12-03 20:56:42 +000063
Chris Lattnerefc84a42003-06-01 23:22:11 +000064 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000065 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000066 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000067 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000068 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000069 unsigned values[4];
Misha Brukmanedf128a2005-04-21 22:36:52 +000070
Misha Brukman3432d1d2003-05-27 22:43:19 +000071 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000072 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000073 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000074 openActual();
75 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000076
77 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000078 o << "\n";
79 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000080 }
81
82 void openActual() {
83 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000084 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000085 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
86 abort();
87 }
88 }
89
90 void startFunction(MachineFunction &F) {
91 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000092 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000093 }
94 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000095 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000096 }
97
Misha Brukmand720da22003-06-03 20:00:49 +000098 void emitConstantPool(MachineConstantPool *MCP) {
99 MCE.emitConstantPool(MCP);
100 }
Nate Begeman37efe672006-04-22 18:53:45 +0000101 void initJumpTableInfo(MachineJumpTableInfo *MJTI) {
102 MCE.initJumpTableInfo(MJTI);
103 }
104 void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
105 std::map<MachineBasicBlock*,uint64_t> &MBBM) {
106 MCE.emitJumpTableInfo(MJTI, MBBM);
107 }
108
Chris Lattner3bf285a2004-11-20 23:53:26 +0000109 void startFunctionStub(unsigned StubSize) {
110 MCE.startFunctionStub(StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000111 }
112
Chris Lattner3bf285a2004-11-20 23:53:26 +0000113 void *finishFunctionStub(const Function *F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000114 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000115 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000116
Misha Brukman3432d1d2003-05-27 22:43:19 +0000117 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +0000118 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +0000119 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +0000120
121 values[counter] = (unsigned int) B;
122 if (++counter % 4 == 0 && counter != 0) {
123 o << std::hex;
124 for (unsigned i=0; i<4; ++i) {
125 if (values[i] < 16) o << "0";
126 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +0000127 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000128
129 o << std::dec << "\t";
130 for (unsigned i=0; i<4; ++i) {
131 for (int j=7; j>=0; --j) {
132 o << ((values[i] >> j) & 1);
133 }
134 o << " ";
135 }
136
137 o << "\n";
138
139 unsigned instr = 0;
140 for (unsigned i=0; i<4; ++i)
141 instr |= values[i] << (i*8);
142
143 o << "--- * --- * --- * --- * ---\n";
144 counter %= 4;
145 }
146 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000147
Chris Lattnerefc84a42003-06-01 23:22:11 +0000148 void emitWord(unsigned W) {
149 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000150 }
Brian Gaekeda8246b2004-04-23 17:11:13 +0000151 void emitWordAt(unsigned W, unsigned *Ptr) {
152 MCE.emitWordAt(W, Ptr);
153 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000154 uint64_t getConstantPoolEntryAddress(unsigned Num) {
155 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000156 }
Nate Begeman37efe672006-04-22 18:53:45 +0000157 uint64_t getJumpTableEntryAddress(unsigned Num) {
158 return MCE.getJumpTableEntryAddress(Num);
159 }
Andrew Lenharthfe660392005-07-28 18:13:59 +0000160 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
161 { return MCE.allocateGlobal(size, alignment); }
162
Chris Lattnerefc84a42003-06-01 23:22:11 +0000163 uint64_t getCurrentPCValue() {
164 return MCE.getCurrentPCValue();
165 }
Chris Lattner47012c02004-11-20 03:44:39 +0000166 uint64_t getCurrentPCOffset() {
167 return MCE.getCurrentPCOffset();
168 }
169 void addRelocation(const MachineRelocation &MR) {
170 return MCE.addRelocation(MR);
171 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000172 };
173}
174
Brian Gaeked0fde302003-11-11 22:41:34 +0000175/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
176/// code emitter, which just prints the opcodes and fields out the cout. This
177/// can be used for debugging users of the MachineCodeEmitter interface.
178///
179MachineCodeEmitter *
180MachineCodeEmitter::createDebugEmitter() {
181 return new DebugMachineCodeEmitter();
182}
183
Chris Lattnerefc84a42003-06-01 23:22:11 +0000184MachineCodeEmitter *
185MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
186 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000187}