2002-12-24 00:01:05 +00:00
|
|
|
//===-- Emitter.cpp - Write machine code to executable memory -------------===//
|
|
|
|
//
|
|
|
|
// This file defines a MachineCodeEmitter object that is used by Jello to write
|
|
|
|
// machine code to memory and remember where relocatable values lie.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-05 17:00:32 +00:00
|
|
|
#define DEBUG_TYPE "jit"
|
2002-12-24 00:01:05 +00:00
|
|
|
#include "VM.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/sys/mman.h"
|
2002-12-24 00:01:05 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-01-13 01:00:12 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
2003-07-28 19:09:06 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-08-01 22:13:59 +00:00
|
|
|
#include "Support/Debug.h"
|
2002-12-24 00:01:05 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-06-08 06:43:57 +00:00
|
|
|
#include <stdio.h>
|
2002-12-24 00:01:05 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
|
2003-07-28 19:09:06 +00:00
|
|
|
VM *TheVM = 0;
|
2002-12-24 00:01:05 +00:00
|
|
|
|
|
|
|
class Emitter : public MachineCodeEmitter {
|
2003-06-01 23:24:36 +00:00
|
|
|
// CurBlock - The start of the current block of memory. CurByte - The
|
|
|
|
// current byte being emitted to.
|
2003-05-09 03:30:07 +00:00
|
|
|
unsigned char *CurBlock, *CurByte;
|
|
|
|
|
|
|
|
// When outputting a function stub in the context of some other function, we
|
|
|
|
// save CurBlock and CurByte here.
|
|
|
|
unsigned char *SavedCurBlock, *SavedCurByte;
|
2003-06-01 23:24:36 +00:00
|
|
|
|
|
|
|
// ConstantPoolAddresses - Contains the location for each entry in the
|
|
|
|
// constant pool.
|
2003-01-13 01:00:12 +00:00
|
|
|
std::vector<void*> ConstantPoolAddresses;
|
2002-12-24 00:01:05 +00:00
|
|
|
public:
|
2003-06-01 23:24:36 +00:00
|
|
|
Emitter(VM &vm) { TheVM = &vm; }
|
2002-12-24 00:01:05 +00:00
|
|
|
|
|
|
|
virtual void startFunction(MachineFunction &F);
|
|
|
|
virtual void finishFunction(MachineFunction &F);
|
2003-01-13 01:00:12 +00:00
|
|
|
virtual void emitConstantPool(MachineConstantPool *MCP);
|
2003-05-09 03:30:07 +00:00
|
|
|
virtual void startFunctionStub(const Function &F, unsigned StubSize);
|
|
|
|
virtual void* finishFunctionStub(const Function &F);
|
2002-12-24 00:01:05 +00:00
|
|
|
virtual void emitByte(unsigned char B);
|
2003-06-01 23:24:36 +00:00
|
|
|
virtual void emitWord(unsigned W);
|
|
|
|
|
|
|
|
virtual uint64_t getGlobalValueAddress(GlobalValue *V);
|
|
|
|
virtual uint64_t getGlobalValueAddress(const std::string &Name);
|
|
|
|
virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
|
|
|
|
virtual uint64_t getCurrentPCValue();
|
|
|
|
|
|
|
|
// forceCompilationOf - Force the compilation of the specified function, and
|
|
|
|
// return its address, because we REALLY need the address now.
|
|
|
|
//
|
|
|
|
// FIXME: This is JIT specific!
|
|
|
|
//
|
|
|
|
virtual uint64_t forceCompilationOf(Function *F);
|
2002-12-24 00:01:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-06-02 03:23:16 +00:00
|
|
|
MachineCodeEmitter *VM::createEmitter(VM &V) {
|
2002-12-24 00:01:05 +00:00
|
|
|
return new Emitter(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define _POSIX_MAPPED_FILES
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2003-05-09 03:30:07 +00:00
|
|
|
// FIXME: This should be rewritten to support a real memory manager for
|
|
|
|
// executable memory pages!
|
|
|
|
static void *getMemory(unsigned NumPages) {
|
2003-06-04 19:45:25 +00:00
|
|
|
void *pa;
|
|
|
|
if (NumPages == 0) return 0;
|
|
|
|
static const long pageSize = sysconf(_SC_PAGESIZE);
|
|
|
|
|
2003-06-02 03:23:16 +00:00
|
|
|
#if defined(i386) || defined(__i386__) || defined(__x86__)
|
2003-07-28 19:26:19 +00:00
|
|
|
/* Linux and *BSD tend to have these flags named differently. */
|
2003-06-17 23:14:06 +00:00
|
|
|
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
|
|
|
|
# define MAP_ANONYMOUS MAP_ANON
|
2003-07-29 16:57:16 +00:00
|
|
|
#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
|
|
|
|
#define fd 0
|
2003-06-02 03:23:16 +00:00
|
|
|
#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
2003-07-29 16:57:16 +00:00
|
|
|
#define fd -1
|
2003-06-02 03:23:16 +00:00
|
|
|
#else
|
2003-07-29 16:57:16 +00:00
|
|
|
std::cerr << "This architecture is not supported by the JIT!\n";
|
2003-06-04 19:45:25 +00:00
|
|
|
abort();
|
2003-06-02 03:23:16 +00:00
|
|
|
#endif
|
2003-07-29 16:57:16 +00:00
|
|
|
pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
|
|
|
|
MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
|
2003-06-02 03:23:16 +00:00
|
|
|
if (pa == MAP_FAILED) {
|
|
|
|
perror("mmap");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return pa;
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Emitter::startFunction(MachineFunction &F) {
|
2003-06-01 23:24:36 +00:00
|
|
|
CurBlock = (unsigned char *)getMemory(16);
|
2002-12-24 00:01:05 +00:00
|
|
|
CurByte = CurBlock; // Start writing at the beginning of the fn.
|
2003-06-01 23:24:36 +00:00
|
|
|
TheVM->addGlobalMapping(F.getFunction(), CurBlock);
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Emitter::finishFunction(MachineFunction &F) {
|
2003-01-13 01:00:12 +00:00
|
|
|
ConstantPoolAddresses.clear();
|
2002-12-24 00:01:05 +00:00
|
|
|
NumBytes += CurByte-CurBlock;
|
|
|
|
|
2003-06-30 18:06:20 +00:00
|
|
|
DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
|
2003-06-06 06:52:35 +00:00
|
|
|
<< "] Function: " << F.getFunction()->getName()
|
2002-12-24 00:01:05 +00:00
|
|
|
<< ": " << CurByte-CurBlock << " bytes of text\n");
|
|
|
|
}
|
|
|
|
|
2003-01-13 01:00:12 +00:00
|
|
|
void Emitter::emitConstantPool(MachineConstantPool *MCP) {
|
|
|
|
const std::vector<Constant*> &Constants = MCP->getConstants();
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
|
|
|
// For now we just allocate some memory on the heap, this can be
|
|
|
|
// dramatically improved.
|
|
|
|
const Type *Ty = ((Value*)Constants[i])->getType();
|
2003-06-01 23:24:36 +00:00
|
|
|
void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
|
|
|
|
TheVM->InitializeMemory(Constants[i], Addr);
|
2003-01-13 01:00:12 +00:00
|
|
|
ConstantPoolAddresses.push_back(Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-09 03:30:07 +00:00
|
|
|
void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
|
2003-06-04 19:45:25 +00:00
|
|
|
static const long pageSize = sysconf(_SC_PAGESIZE);
|
2003-05-09 03:30:07 +00:00
|
|
|
SavedCurBlock = CurBlock; SavedCurByte = CurByte;
|
|
|
|
// FIXME: this is a huge waste of memory.
|
2003-06-04 19:45:25 +00:00
|
|
|
CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
|
2003-05-09 03:30:07 +00:00
|
|
|
CurByte = CurBlock; // Start writing at the beginning of the fn.
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Emitter::finishFunctionStub(const Function &F) {
|
|
|
|
NumBytes += CurByte-CurBlock;
|
|
|
|
DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
|
|
|
|
<< (unsigned)(intptr_t)CurBlock
|
|
|
|
<< std::dec << "] Function stub for: " << F.getName()
|
|
|
|
<< ": " << CurByte-CurBlock << " bytes of text\n");
|
|
|
|
std::swap(CurBlock, SavedCurBlock);
|
|
|
|
CurByte = SavedCurByte;
|
|
|
|
return SavedCurBlock;
|
|
|
|
}
|
|
|
|
|
2002-12-24 00:01:05 +00:00
|
|
|
void Emitter::emitByte(unsigned char B) {
|
|
|
|
*CurByte++ = B; // Write the byte to memory
|
|
|
|
}
|
|
|
|
|
2003-06-01 23:24:36 +00:00
|
|
|
void Emitter::emitWord(unsigned W) {
|
|
|
|
// FIXME: This won't work if the endianness of the host and target don't
|
|
|
|
// agree! (For a JIT this can't happen though. :)
|
|
|
|
*(unsigned*)CurByte = W;
|
|
|
|
CurByte += sizeof(unsigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
|
|
|
|
// Try looking up the function to see if it is already compiled, if not return
|
|
|
|
// 0.
|
|
|
|
return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
|
|
|
|
}
|
|
|
|
uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
|
|
|
|
return (intptr_t)TheVM->getPointerToNamedFunction(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
|
|
|
|
// in the constant pool that was last emitted with the 'emitConstantPool'
|
|
|
|
// method.
|
|
|
|
//
|
|
|
|
uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
|
|
|
|
assert(ConstantNum < ConstantPoolAddresses.size() &&
|
|
|
|
"Invalid ConstantPoolIndex!");
|
|
|
|
return (intptr_t)ConstantPoolAddresses[ConstantNum];
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrentPCValue - This returns the address that the next emitted byte
|
|
|
|
// will be output to.
|
|
|
|
//
|
|
|
|
uint64_t Emitter::getCurrentPCValue() {
|
|
|
|
return (intptr_t)CurByte;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Emitter::forceCompilationOf(Function *F) {
|
|
|
|
return (intptr_t)TheVM->getPointerToFunction(F);
|
|
|
|
}
|
|
|
|
|
2003-07-28 19:09:06 +00:00
|
|
|
// getPointerToNamedFunction - This function is used as a global wrapper to
|
|
|
|
// VM::getPointerToNamedFunction for the purpose of resolving symbols when
|
|
|
|
// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
|
|
|
|
// need to resolve function(s) that are being mis-codegenerated, so we need to
|
|
|
|
// resolve their addresses at runtime, and this is the way to do it.
|
|
|
|
extern "C" {
|
|
|
|
void *getPointerToNamedFunction(const char *Name) {
|
|
|
|
Module &M = TheVM->getModule();
|
|
|
|
if (Function *F = M.getNamedFunction(Name))
|
|
|
|
return TheVM->getPointerToFunction(F);
|
|
|
|
return TheVM->getPointerToNamedFunction(Name);
|
|
|
|
}
|
|
|
|
}
|