MachineCodeEmitter.cpp revision fe660397e14ecba84e792ada489f2bda7269b413
1//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
2//
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//===----------------------------------------------------------------------===//
9//
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"
17#include <fstream>
18#include <iostream>
19
20using namespace llvm;
21
22namespace {
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    }
31    void startFunctionStub(unsigned StubSize) {
32      std::cout << "\n--- Function stub:\n";
33    }
34    void *finishFunctionStub(const Function *F) {
35      std::cout << "\n--- End of stub for Function\n";
36      return 0;
37    }
38
39    void emitByte(unsigned char B) {
40      std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
41    }
42    void emitWord(unsigned W) {
43      std::cout << "0x" << std::hex << W << std::dec << " ";
44    }
45    void emitWordAt(unsigned W, unsigned *Ptr) {
46      std::cout << "0x" << std::hex << W << std::dec << " (at "
47                << (void*) Ptr << ") ";
48    }
49
50    void addRelocation(const MachineRelocation &MR) {
51      std::cout << "<relocation> ";
52    }
53
54    virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
55    { return 0; }
56
57    uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
58    uint64_t getCurrentPCValue() { return 0; }
59    uint64_t getCurrentPCOffset() { return 0; }
60  };
61
62  class FilePrinterEmitter : public MachineCodeEmitter {
63    std::ofstream actual;
64    std::ostream &o;
65    MachineCodeEmitter &MCE;
66    unsigned counter;
67    unsigned values[4];
68
69  public:
70    FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
71      : o(os), MCE(M), counter(0) {
72      openActual();
73    }
74
75    ~FilePrinterEmitter() {
76      o << "\n";
77      actual.close();
78    }
79
80    void openActual() {
81      actual.open("lli.actual.obj");
82      if (!actual.good()) {
83        std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
84        abort();
85      }
86    }
87
88    void startFunction(MachineFunction &F) {
89      // resolve any outstanding calls
90      MCE.startFunction(F);
91    }
92    void finishFunction(MachineFunction &F) {
93      MCE.finishFunction(F);
94    }
95
96    void emitConstantPool(MachineConstantPool *MCP) {
97      MCE.emitConstantPool(MCP);
98    }
99
100    void startFunctionStub(unsigned StubSize) {
101      MCE.startFunctionStub(StubSize);
102    }
103
104    void *finishFunctionStub(const Function *F) {
105      return MCE.finishFunctionStub(F);
106    }
107
108    void emitByte(unsigned char B) {
109      MCE.emitByte(B);
110      actual << B; actual.flush();
111
112      values[counter] = (unsigned int) B;
113      if (++counter % 4 == 0 && counter != 0) {
114        o << std::hex;
115        for (unsigned i=0; i<4; ++i) {
116          if (values[i] < 16) o << "0";
117          o << values[i] << " ";
118        }
119
120        o << std::dec << "\t";
121        for (unsigned i=0; i<4; ++i) {
122          for (int j=7; j>=0; --j) {
123            o << ((values[i] >> j) & 1);
124          }
125          o << " ";
126        }
127
128        o << "\n";
129
130        unsigned instr = 0;
131        for (unsigned i=0; i<4; ++i)
132          instr |= values[i] << (i*8);
133
134        o << "--- * --- * --- * --- * ---\n";
135        counter %= 4;
136      }
137    }
138
139    void emitWord(unsigned W) {
140      MCE.emitWord(W);
141    }
142    void emitWordAt(unsigned W, unsigned *Ptr) {
143      MCE.emitWordAt(W, Ptr);
144    }
145    uint64_t getConstantPoolEntryAddress(unsigned Num) {
146      return MCE.getConstantPoolEntryAddress(Num);
147    }
148
149    virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
150    { return MCE.allocateGlobal(size, alignment); }
151
152    uint64_t getCurrentPCValue() {
153      return MCE.getCurrentPCValue();
154    }
155    uint64_t getCurrentPCOffset() {
156      return MCE.getCurrentPCOffset();
157    }
158    void addRelocation(const MachineRelocation &MR) {
159      return MCE.addRelocation(MR);
160    }
161  };
162}
163
164/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
165/// code emitter, which just prints the opcodes and fields out the cout.  This
166/// can be used for debugging users of the MachineCodeEmitter interface.
167///
168MachineCodeEmitter *
169MachineCodeEmitter::createDebugEmitter() {
170  return new DebugMachineCodeEmitter();
171}
172
173MachineCodeEmitter *
174MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
175  return new FilePrinterEmitter(MCE, std::cerr);
176}
177