summaryrefslogtreecommitdiff
path: root/lib/CodeGen/MachineCodeEmitter.cpp
blob: e67b4aaa0218d775e37f3ba99a29430e4394ffd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
//
// This file implements the MachineCodeEmitter interface.
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Function.h"
#include <iostream>
#include <fstream>

namespace {
  struct DebugMachineCodeEmitter : public MachineCodeEmitter {
    void startFunction(MachineFunction &F) {
      std::cout << "\n**** Writing machine code for function: "
                << F.getFunction()->getName() << "\n";
    }
    void finishFunction(MachineFunction &F) {
      std::cout << "\n";
    }
    void startBasicBlock(MachineBasicBlock &BB) {
      std::cout << "\n--- Basic Block: " << BB.getBasicBlock()->getName()<<"\n";
    }

    void startFunctionStub(const Function &F, unsigned StubSize) {
      std::cout << "\n--- Function stub for function: " << F.getName() << "\n";
    }
    void *finishFunctionStub(const Function &F) {
      std::cout << "\n";
      return 0;
    }
    
    void emitByte(unsigned char B) {
      std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
    }
    void emitPCRelativeDisp(Value *V) {
      std::cout << "<disp %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
    }
    void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
      std::cout << "<addr %" << V->getName() << ": 0xXX 0xXX 0xXX 0xXX> ";
    }
    void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
      std::cout << "<addr %" << Name << ": 0xXX 0xXX 0xXX 0xXX> ";
    }

    void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
      std::cout << "<addr const#" << ConstantNum;
      if (Offset) std::cout << " + " << Offset;
      std::cout << "> ";
    }
  };
}


/// createDebugMachineCodeEmitter - Return a dynamically allocated machine
/// code emitter, which just prints the opcodes and fields out the cout.  This
/// can be used for debugging users of the MachineCodeEmitter interface.
///
MachineCodeEmitter *MachineCodeEmitter::createDebugMachineCodeEmitter() {
  return new DebugMachineCodeEmitter();
}

namespace {
  class FilePrinterMachineCodeEmitter : public MachineCodeEmitter {
    std::ofstream f, actual;
    std::ostream &o;
    MachineCodeEmitter *MCE;
    unsigned counter;
    bool mustClose;
    unsigned values[4];
    
  public:
    FilePrinterMachineCodeEmitter() :
      f("lli.out"), o(f), counter(0), mustClose(true)
    {
      if (! f.good()) {
        std::cerr << "Cannot open 'lli.out' for writing\n";
        abort();
      }
      openActual();
    }

    FilePrinterMachineCodeEmitter(MachineCodeEmitter &M, std::ostream &os) :
      o(os), MCE(&M), counter(0)
    {
      FilePrinterMachineCodeEmitter();
      mustClose = false;
      openActual();
    }

    ~FilePrinterMachineCodeEmitter() { 
      o << "\n";
      actual.close();
      if (mustClose) f.close();
    }

    void openActual() {
      actual.open("lli.actual.obj");
      if (! actual.good()) {
        std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
        abort();
      }
    }

    void startFunction(MachineFunction &F) {
      // resolve any outstanding calls
      if (MCE) MCE->startFunction(F);
    }
    void finishFunction(MachineFunction &F) {
      if (MCE) MCE->finishFunction(F);
    }

    void startBasicBlock(MachineBasicBlock &BB) {
      // if any instructions were waiting for the address of this block,
      // let them fix their addresses now
      if (MCE) MCE->startBasicBlock(BB);
    }

    void startFunctionStub(const Function &F, unsigned StubSize) {
      //
      if (MCE) MCE->startFunctionStub(F, StubSize);
    }

    void *finishFunctionStub(const Function &F) {
      if (MCE) return MCE->finishFunctionStub(F);
      else return 0;
    }
    
    void emitByte(unsigned char B) {
      if (MCE) MCE->emitByte(B);
      actual << B; actual.flush();

      values[counter] = (unsigned int) B;
      if (++counter % 4 == 0 && counter != 0) {
        o << std::hex;
        for (unsigned i=0; i<4; ++i) {
          if (values[i] < 16) o << "0";
          o << values[i] << " ";
        }

        o << std::dec << "\t";
        for (unsigned i=0; i<4; ++i) {
          for (int j=7; j>=0; --j) {
            o << ((values[i] >> j) & 1);
          }
          o << " ";
        }

        o << "\n";

        unsigned instr = 0;
        for (unsigned i=0; i<4; ++i)
          instr |= values[i] << (i*8);

        o << "--- * --- * --- * --- * ---\n";
        counter %= 4;
      }
    }
    void emitPCRelativeDisp(Value *V) {
      // put block in mapping BB -> { instr, address }. when BB is beginning to
      // output, find instr, set disp, overwrite instr at addr using the
      // unsigned value gotten from emitter
    }

    void emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
      if (MCE) MCE->emitGlobalAddress(V, isPCRelative);
    }
    void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
      if (MCE) MCE->emitGlobalAddress(Name, isPCRelative);
    }

    void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
      if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset);
    }
  };
}

MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter(MachineCodeEmitter &MCE) {
  return new FilePrinterMachineCodeEmitter(MCE, std::cerr);
}