blob: 301d9afb8ae18ab439a88e99f4192edf65f063de [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"
Misha Brukman3432d1d2003-05-27 22:43:19 +000015#include <fstream>
Reid Spencer954da372004-07-04 12:19:56 +000016#include <iostream>
Chris Lattner0742b592004-02-23 18:38:20 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Chris Lattnerf815aeb2002-12-03 20:56:42 +000019namespace {
Chris Lattnerefc84a42003-06-01 23:22:11 +000020 class FilePrinterEmitter : public MachineCodeEmitter {
Misha Brukmane6aa9e32003-06-02 20:49:09 +000021 std::ofstream actual;
Misha Brukman3432d1d2003-05-27 22:43:19 +000022 std::ostream &o;
Chris Lattnerefc84a42003-06-01 23:22:11 +000023 MachineCodeEmitter &MCE;
Misha Brukman3432d1d2003-05-27 22:43:19 +000024 unsigned counter;
Misha Brukman3432d1d2003-05-27 22:43:19 +000025 unsigned values[4];
Misha Brukmanedf128a2005-04-21 22:36:52 +000026
Misha Brukman3432d1d2003-05-27 22:43:19 +000027 public:
Chris Lattnerefc84a42003-06-01 23:22:11 +000028 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
Misha Brukmane6aa9e32003-06-02 20:49:09 +000029 : o(os), MCE(M), counter(0) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000030 openActual();
31 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000032
33 ~FilePrinterEmitter() {
Misha Brukman3432d1d2003-05-27 22:43:19 +000034 o << "\n";
35 actual.close();
Misha Brukman3432d1d2003-05-27 22:43:19 +000036 }
37
38 void openActual() {
39 actual.open("lli.actual.obj");
Chris Lattnerefc84a42003-06-01 23:22:11 +000040 if (!actual.good()) {
Misha Brukman3432d1d2003-05-27 22:43:19 +000041 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
42 abort();
43 }
44 }
45
46 void startFunction(MachineFunction &F) {
47 // resolve any outstanding calls
Chris Lattnerefc84a42003-06-01 23:22:11 +000048 MCE.startFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000049 }
50 void finishFunction(MachineFunction &F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000051 MCE.finishFunction(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000052 }
53
Misha Brukmand720da22003-06-03 20:00:49 +000054 void emitConstantPool(MachineConstantPool *MCP) {
55 MCE.emitConstantPool(MCP);
56 }
Nate Begeman37efe672006-04-22 18:53:45 +000057 void initJumpTableInfo(MachineJumpTableInfo *MJTI) {
58 MCE.initJumpTableInfo(MJTI);
59 }
60 void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
61 std::map<MachineBasicBlock*,uint64_t> &MBBM) {
62 MCE.emitJumpTableInfo(MJTI, MBBM);
63 }
64
Chris Lattner3bf285a2004-11-20 23:53:26 +000065 void startFunctionStub(unsigned StubSize) {
66 MCE.startFunctionStub(StubSize);
Misha Brukman3432d1d2003-05-27 22:43:19 +000067 }
68
Chris Lattner3bf285a2004-11-20 23:53:26 +000069 void *finishFunctionStub(const Function *F) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000070 return MCE.finishFunctionStub(F);
Misha Brukman3432d1d2003-05-27 22:43:19 +000071 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000072
Misha Brukman3432d1d2003-05-27 22:43:19 +000073 void emitByte(unsigned char B) {
Chris Lattnerefc84a42003-06-01 23:22:11 +000074 MCE.emitByte(B);
Misha Brukmaneae77de2003-05-28 18:27:19 +000075 actual << B; actual.flush();
Misha Brukman3432d1d2003-05-27 22:43:19 +000076
77 values[counter] = (unsigned int) B;
78 if (++counter % 4 == 0 && counter != 0) {
79 o << std::hex;
80 for (unsigned i=0; i<4; ++i) {
81 if (values[i] < 16) o << "0";
82 o << values[i] << " ";
Misha Brukman3432d1d2003-05-27 22:43:19 +000083 }
Misha Brukman3432d1d2003-05-27 22:43:19 +000084
85 o << std::dec << "\t";
86 for (unsigned i=0; i<4; ++i) {
87 for (int j=7; j>=0; --j) {
88 o << ((values[i] >> j) & 1);
89 }
90 o << " ";
91 }
92
93 o << "\n";
94
95 unsigned instr = 0;
96 for (unsigned i=0; i<4; ++i)
97 instr |= values[i] << (i*8);
98
99 o << "--- * --- * --- * --- * ---\n";
100 counter %= 4;
101 }
102 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000103
Chris Lattnerefc84a42003-06-01 23:22:11 +0000104 void emitWord(unsigned W) {
105 MCE.emitWord(W);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000106 }
Chris Lattnerefc84a42003-06-01 23:22:11 +0000107 uint64_t getConstantPoolEntryAddress(unsigned Num) {
108 return MCE.getConstantPoolEntryAddress(Num);
Misha Brukmanda3a8b12003-05-30 20:32:45 +0000109 }
Nate Begeman37efe672006-04-22 18:53:45 +0000110 uint64_t getJumpTableEntryAddress(unsigned Num) {
111 return MCE.getJumpTableEntryAddress(Num);
112 }
Andrew Lenharthfe660392005-07-28 18:13:59 +0000113 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
114 { return MCE.allocateGlobal(size, alignment); }
115
Chris Lattnerefc84a42003-06-01 23:22:11 +0000116 uint64_t getCurrentPCValue() {
117 return MCE.getCurrentPCValue();
118 }
Chris Lattner47012c02004-11-20 03:44:39 +0000119 uint64_t getCurrentPCOffset() {
120 return MCE.getCurrentPCOffset();
121 }
122 void addRelocation(const MachineRelocation &MR) {
123 return MCE.addRelocation(MR);
124 }
Misha Brukman3432d1d2003-05-27 22:43:19 +0000125 };
126}
127
Chris Lattnerefc84a42003-06-01 23:22:11 +0000128MachineCodeEmitter *
129MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
130 return new FilePrinterEmitter(MCE, std::cerr);
Misha Brukman3432d1d2003-05-27 22:43:19 +0000131}