//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
//
// This file implements the MachineCodeEmitter interface.
//
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Function.h"
-#include <iostream>
#include <fstream>
+#include <iostream>
+
+using namespace llvm;
namespace {
struct DebugMachineCodeEmitter : public MachineCodeEmitter {
void finishFunction(MachineFunction &F) {
std::cout << "\n";
}
- void startBasicBlock(MachineBasicBlock &BB) {
- std::cout << "\n--- Basic Block: " << BB.getBasicBlock()->getName()<<"\n";
+ void startFunctionStub(unsigned StubSize) {
+ std::cout << "\n--- Function stub:\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";
+ void *finishFunctionStub(const Function *F) {
+ std::cout << "\n--- End of stub for Function\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 emitWord(unsigned W) {
+ std::cout << "0x" << std::hex << W << std::dec << " ";
}
- 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 emitWordAt(unsigned W, unsigned *Ptr) {
+ std::cout << "0x" << std::hex << W << std::dec << " (at "
+ << (void*) Ptr << ") ";
}
- void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
- std::cout << "<addr const#" << ConstantNum;
- if (Offset) std::cout << " + " << Offset;
- std::cout << "> ";
+ void addRelocation(const MachineRelocation &MR) {
+ std::cout << "<relocation> ";
}
- };
-}
-
-/// 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();
-}
+ uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
+ uint64_t getCurrentPCValue() { return 0; }
+ uint64_t getCurrentPCOffset() { return 0; }
+ };
-namespace {
- class FilePrinterMachineCodeEmitter : public MachineCodeEmitter {
- std::ofstream f, actual;
+ class FilePrinterEmitter : public MachineCodeEmitter {
+ std::ofstream actual;
std::ostream &o;
- MachineCodeEmitter *MCE;
+ 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;
+ FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
+ : o(os), MCE(M), counter(0) {
openActual();
}
-
- ~FilePrinterMachineCodeEmitter() {
+
+ ~FilePrinterEmitter() {
o << "\n";
actual.close();
- if (mustClose) f.close();
}
void openActual() {
actual.open("lli.actual.obj");
- if (! actual.good()) {
+ 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);
+ MCE.startFunction(F);
}
void finishFunction(MachineFunction &F) {
- if (MCE) MCE->finishFunction(F);
+ 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 emitConstantPool(MachineConstantPool *MCP) {
+ MCE.emitConstantPool(MCP);
}
- void startFunctionStub(const Function &F, unsigned StubSize) {
- //
- if (MCE) MCE->startFunctionStub(F, StubSize);
+ void startFunctionStub(unsigned StubSize) {
+ MCE.startFunctionStub(StubSize);
}
- void *finishFunctionStub(const Function &F) {
- if (MCE) return MCE->finishFunctionStub(F);
- else return 0;
+ void *finishFunctionStub(const Function *F) {
+ return MCE.finishFunctionStub(F);
}
void emitByte(unsigned char B) {
- if (MCE) MCE->emitByte(B);
+ MCE.emitByte(B);
+ actual << B; actual.flush();
values[counter] = (unsigned int) B;
if (++counter % 4 == 0 && counter != 0) {
for (unsigned i=0; i<4; ++i) {
if (values[i] < 16) o << "0";
o << values[i] << " ";
- actual << values[i];
}
- actual.flush();
o << std::dec << "\t";
for (unsigned i=0; i<4; ++i) {
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 emitWord(unsigned W) {
+ MCE.emitWord(W);
}
- void emitGlobalAddress(const std::string &Name, bool isPCRelative) {
- if (MCE) MCE->emitGlobalAddress(Name, isPCRelative);
+ void emitWordAt(unsigned W, unsigned *Ptr) {
+ MCE.emitWordAt(W, Ptr);
}
-
- void emitFunctionConstantValueAddress(unsigned ConstantNum, int Offset) {
- if (MCE) MCE->emitFunctionConstantValueAddress(ConstantNum, Offset);
+ uint64_t getConstantPoolEntryAddress(unsigned Num) {
+ return MCE.getConstantPoolEntryAddress(Num);
+ }
+ uint64_t getCurrentPCValue() {
+ return MCE.getCurrentPCValue();
+ }
+ uint64_t getCurrentPCOffset() {
+ return MCE.getCurrentPCOffset();
+ }
+ void addRelocation(const MachineRelocation &MR) {
+ return MCE.addRelocation(MR);
}
};
}
-MachineCodeEmitter *MachineCodeEmitter::createFilePrinterMachineCodeEmitter(MachineCodeEmitter &MCE) {
- return new FilePrinterMachineCodeEmitter(MCE, std::cerr);
+/// 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::createDebugEmitter() {
+ return new DebugMachineCodeEmitter();
+}
+
+MachineCodeEmitter *
+MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
+ return new FilePrinterEmitter(MCE, std::cerr);
}