1 //===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachineCodeEmitter interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineCodeEmitter.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Function.h"
24 struct DebugMachineCodeEmitter : public MachineCodeEmitter {
25 void startFunction(MachineFunction &F) {
26 std::cout << "\n**** Writing machine code for function: "
27 << F.getFunction()->getName() << "\n";
29 void finishFunction(MachineFunction &F) {
32 void startFunctionStub(unsigned StubSize) {
33 std::cout << "\n--- Function stub:\n";
35 void *finishFunctionStub(const Function *F) {
36 std::cout << "\n--- End of stub for Function\n";
40 void emitByte(unsigned char B) {
41 std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
43 void emitWord(unsigned W) {
44 std::cout << "0x" << std::hex << W << std::dec << " ";
46 void emitWordAt(unsigned W, unsigned *Ptr) {
47 std::cout << "0x" << std::hex << W << std::dec << " (at "
48 << (void*) Ptr << ") ";
51 void addRelocation(const MachineRelocation &MR) {
52 std::cout << "<relocation> ";
55 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
58 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
59 uint64_t getCurrentPCValue() { return 0; }
60 uint64_t getCurrentPCOffset() { return 0; }
63 class FilePrinterEmitter : public MachineCodeEmitter {
66 MachineCodeEmitter &MCE;
71 FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
72 : o(os), MCE(M), counter(0) {
76 ~FilePrinterEmitter() {
82 actual.open("lli.actual.obj");
84 std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
89 void startFunction(MachineFunction &F) {
90 // resolve any outstanding calls
93 void finishFunction(MachineFunction &F) {
94 MCE.finishFunction(F);
97 void emitConstantPool(MachineConstantPool *MCP) {
98 MCE.emitConstantPool(MCP);
101 void startFunctionStub(unsigned StubSize) {
102 MCE.startFunctionStub(StubSize);
105 void *finishFunctionStub(const Function *F) {
106 return MCE.finishFunctionStub(F);
109 void emitByte(unsigned char B) {
111 actual << B; actual.flush();
113 values[counter] = (unsigned int) B;
114 if (++counter % 4 == 0 && counter != 0) {
116 for (unsigned i=0; i<4; ++i) {
117 if (values[i] < 16) o << "0";
118 o << values[i] << " ";
121 o << std::dec << "\t";
122 for (unsigned i=0; i<4; ++i) {
123 for (int j=7; j>=0; --j) {
124 o << ((values[i] >> j) & 1);
132 for (unsigned i=0; i<4; ++i)
133 instr |= values[i] << (i*8);
135 o << "--- * --- * --- * --- * ---\n";
140 void emitWord(unsigned W) {
143 void emitWordAt(unsigned W, unsigned *Ptr) {
144 MCE.emitWordAt(W, Ptr);
146 uint64_t getConstantPoolEntryAddress(unsigned Num) {
147 return MCE.getConstantPoolEntryAddress(Num);
150 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
151 { return MCE.allocateGlobal(size, alignment); }
153 uint64_t getCurrentPCValue() {
154 return MCE.getCurrentPCValue();
156 uint64_t getCurrentPCOffset() {
157 return MCE.getCurrentPCOffset();
159 void addRelocation(const MachineRelocation &MR) {
160 return MCE.addRelocation(MR);
165 /// createDebugMachineCodeEmitter - Return a dynamically allocated machine
166 /// code emitter, which just prints the opcodes and fields out the cout. This
167 /// can be used for debugging users of the MachineCodeEmitter interface.
170 MachineCodeEmitter::createDebugEmitter() {
171 return new DebugMachineCodeEmitter();
175 MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
176 return new FilePrinterEmitter(MCE, std::cerr);