MachineCodeEmitter.cpp revision b0cc79d45b1e455b486f8ca5cc68546b7a865eb3
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 <fstream>
16#include <iostream>
17using namespace llvm;
18
19namespace {
20  class FilePrinterEmitter : public MachineCodeEmitter {
21    std::ofstream actual;
22    std::ostream &o;
23    MachineCodeEmitter &MCE;
24    unsigned counter;
25    unsigned values[4];
26
27  public:
28    FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
29      : o(os), MCE(M), counter(0) {
30      openActual();
31    }
32
33    ~FilePrinterEmitter() {
34      o << "\n";
35      actual.close();
36    }
37
38    void openActual() {
39      actual.open("lli.actual.obj");
40      if (!actual.good()) {
41        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
48      MCE.startFunction(F);
49    }
50    void finishFunction(MachineFunction &F) {
51      MCE.finishFunction(F);
52    }
53
54    void emitConstantPool(MachineConstantPool *MCP) {
55      MCE.emitConstantPool(MCP);
56    }
57    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
65    void startFunctionStub(unsigned StubSize) {
66      MCE.startFunctionStub(StubSize);
67    }
68
69    void *finishFunctionStub(const Function *F) {
70      return MCE.finishFunctionStub(F);
71    }
72
73    void emitByte(unsigned char B) {
74      MCE.emitByte(B);
75      actual << B; actual.flush();
76
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] << " ";
83        }
84
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    }
103
104    void emitWord(unsigned W) {
105      MCE.emitWord(W);
106    }
107    uint64_t getConstantPoolEntryAddress(unsigned Num) {
108      return MCE.getConstantPoolEntryAddress(Num);
109    }
110    uint64_t getJumpTableEntryAddress(unsigned Num) {
111      return MCE.getJumpTableEntryAddress(Num);
112    }
113    virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
114    { return MCE.allocateGlobal(size, alignment); }
115
116    uint64_t getCurrentPCValue() {
117      return MCE.getCurrentPCValue();
118    }
119    uint64_t getCurrentPCOffset() {
120      return MCE.getCurrentPCOffset();
121    }
122    void addRelocation(const MachineRelocation &MR) {
123      return MCE.addRelocation(MR);
124    }
125  };
126}
127
128MachineCodeEmitter *
129MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
130  return new FilePrinterEmitter(MCE, std::cerr);
131}
132