2002-12-03 20:56:42 +00:00
|
|
|
//===-- MachineCodeEmitter.cpp - Implement the MachineCodeEmitter itf -----===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-03 20:56:42 +00:00
|
|
|
//
|
|
|
|
// This file implements the MachineCodeEmitter interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/Function.h"
|
2003-05-27 22:43:19 +00:00
|
|
|
#include <fstream>
|
2004-07-04 12:19:56 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2004-02-23 18:38:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-12-03 20:56:42 +00:00
|
|
|
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";
|
|
|
|
}
|
2004-11-20 23:53:26 +00:00
|
|
|
void startFunctionStub(unsigned StubSize) {
|
|
|
|
std::cout << "\n--- Function stub:\n";
|
2003-05-08 21:54:18 +00:00
|
|
|
}
|
2004-11-20 23:53:26 +00:00
|
|
|
void *finishFunctionStub(const Function *F) {
|
|
|
|
std::cout << "\n--- End of stub for Function\n";
|
2003-05-09 03:27:41 +00:00
|
|
|
return 0;
|
2003-05-08 21:54:18 +00:00
|
|
|
}
|
2002-12-03 20:56:42 +00:00
|
|
|
|
|
|
|
void emitByte(unsigned char B) {
|
|
|
|
std::cout << "0x" << std::hex << (unsigned int)B << std::dec << " ";
|
|
|
|
}
|
2003-06-01 23:22:11 +00:00
|
|
|
void emitWord(unsigned W) {
|
|
|
|
std::cout << "0x" << std::hex << W << std::dec << " ";
|
2003-01-13 00:22:37 +00:00
|
|
|
}
|
2004-04-23 17:11:13 +00:00
|
|
|
void emitWordAt(unsigned W, unsigned *Ptr) {
|
|
|
|
std::cout << "0x" << std::hex << W << std::dec << " (at "
|
|
|
|
<< (void*) Ptr << ") ";
|
|
|
|
}
|
2003-01-13 00:22:37 +00:00
|
|
|
|
2004-11-20 03:44:39 +00:00
|
|
|
void addRelocation(const MachineRelocation &MR) {
|
|
|
|
std::cout << "<relocation> ";
|
|
|
|
}
|
|
|
|
|
2003-06-01 23:22:11 +00:00
|
|
|
uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
|
|
|
|
uint64_t getCurrentPCValue() { return 0; }
|
2004-11-20 03:44:39 +00:00
|
|
|
uint64_t getCurrentPCOffset() { return 0; }
|
2002-12-03 20:56:42 +00:00
|
|
|
};
|
2003-05-27 22:43:19 +00:00
|
|
|
|
2003-06-01 23:22:11 +00:00
|
|
|
class FilePrinterEmitter : public MachineCodeEmitter {
|
2003-06-02 20:49:09 +00:00
|
|
|
std::ofstream actual;
|
2003-05-27 22:43:19 +00:00
|
|
|
std::ostream &o;
|
2003-06-01 23:22:11 +00:00
|
|
|
MachineCodeEmitter &MCE;
|
2003-05-27 22:43:19 +00:00
|
|
|
unsigned counter;
|
|
|
|
unsigned values[4];
|
|
|
|
|
|
|
|
public:
|
2003-06-01 23:22:11 +00:00
|
|
|
FilePrinterEmitter(MachineCodeEmitter &M, std::ostream &os)
|
2003-06-02 20:49:09 +00:00
|
|
|
: o(os), MCE(M), counter(0) {
|
2003-05-27 22:43:19 +00:00
|
|
|
openActual();
|
|
|
|
}
|
2003-06-01 23:22:11 +00:00
|
|
|
|
|
|
|
~FilePrinterEmitter() {
|
2003-05-27 22:43:19 +00:00
|
|
|
o << "\n";
|
|
|
|
actual.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void openActual() {
|
|
|
|
actual.open("lli.actual.obj");
|
2003-06-01 23:22:11 +00:00
|
|
|
if (!actual.good()) {
|
2003-05-27 22:43:19 +00:00
|
|
|
std::cerr << "Cannot open 'lli.actual.obj' for writing\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startFunction(MachineFunction &F) {
|
|
|
|
// resolve any outstanding calls
|
2003-06-01 23:22:11 +00:00
|
|
|
MCE.startFunction(F);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|
|
|
|
void finishFunction(MachineFunction &F) {
|
2003-06-01 23:22:11 +00:00
|
|
|
MCE.finishFunction(F);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2003-06-03 20:00:49 +00:00
|
|
|
void emitConstantPool(MachineConstantPool *MCP) {
|
|
|
|
MCE.emitConstantPool(MCP);
|
|
|
|
}
|
|
|
|
|
2004-11-20 23:53:26 +00:00
|
|
|
void startFunctionStub(unsigned StubSize) {
|
|
|
|
MCE.startFunctionStub(StubSize);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
2004-11-20 23:53:26 +00:00
|
|
|
void *finishFunctionStub(const Function *F) {
|
2003-06-01 23:22:11 +00:00
|
|
|
return MCE.finishFunctionStub(F);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void emitByte(unsigned char B) {
|
2003-06-01 23:22:11 +00:00
|
|
|
MCE.emitByte(B);
|
2003-05-28 18:27:19 +00:00
|
|
|
actual << B; actual.flush();
|
2003-05-27 22:43:19 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-01 23:22:11 +00:00
|
|
|
void emitWord(unsigned W) {
|
|
|
|
MCE.emitWord(W);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|
2004-04-23 17:11:13 +00:00
|
|
|
void emitWordAt(unsigned W, unsigned *Ptr) {
|
|
|
|
MCE.emitWordAt(W, Ptr);
|
|
|
|
}
|
2003-06-01 23:22:11 +00:00
|
|
|
uint64_t getConstantPoolEntryAddress(unsigned Num) {
|
|
|
|
return MCE.getConstantPoolEntryAddress(Num);
|
|
|
|
}
|
|
|
|
uint64_t getCurrentPCValue() {
|
|
|
|
return MCE.getCurrentPCValue();
|
|
|
|
}
|
2004-11-20 03:44:39 +00:00
|
|
|
uint64_t getCurrentPCOffset() {
|
|
|
|
return MCE.getCurrentPCOffset();
|
|
|
|
}
|
|
|
|
void addRelocation(const MachineRelocation &MR) {
|
|
|
|
return MCE.addRelocation(MR);
|
|
|
|
}
|
2003-05-27 22:43:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
2003-06-01 23:22:11 +00:00
|
|
|
MachineCodeEmitter *
|
|
|
|
MachineCodeEmitter::createFilePrinterEmitter(MachineCodeEmitter &MCE) {
|
|
|
|
return new FilePrinterEmitter(MCE, std::cerr);
|
2003-05-27 22:43:19 +00:00
|
|
|
}
|