2011-04-22 11:07:06 +08:00
|
|
|
//===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
|
2010-11-18 00:06:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCJIT.h"
|
2011-04-05 07:04:39 +08:00
|
|
|
#include "MCJITMemoryManager.h"
|
2011-03-23 02:05:27 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2011-03-19 06:48:41 +08:00
|
|
|
#include "llvm/Function.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
2011-03-22 09:06:42 +08:00
|
|
|
#include "llvm/ExecutionEngine/JITMemoryManager.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2011-03-22 09:06:42 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2011-03-19 06:48:41 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static struct RegisterJIT {
|
|
|
|
RegisterJIT() { MCJIT::Register(); }
|
|
|
|
} JITRegistrator;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMLinkInMCJIT() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutionEngine *MCJIT::createJIT(Module *M,
|
|
|
|
std::string *ErrorStr,
|
|
|
|
JITMemoryManager *JMM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool GVsWithCode,
|
2011-05-14 05:51:29 +08:00
|
|
|
TargetMachine *TM) {
|
2010-11-18 00:06:43 +08:00
|
|
|
// Try to register the program as a source of symbols to resolve against.
|
|
|
|
//
|
|
|
|
// FIXME: Don't do this here.
|
|
|
|
sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
|
|
|
|
|
|
|
|
// If the target supports JIT code generation, create the JIT.
|
|
|
|
if (TargetJITInfo *TJ = TM->getJITInfo())
|
2011-05-13 02:21:23 +08:00
|
|
|
return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM, M), OptLevel,
|
2011-04-05 07:04:39 +08:00
|
|
|
GVsWithCode);
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
if (ErrorStr)
|
|
|
|
*ErrorStr = "target does not support JIT code generation";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:48:41 +08:00
|
|
|
MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
|
2011-04-05 07:04:39 +08:00
|
|
|
RTDyldMemoryManager *MM, CodeGenOpt::Level OptLevel,
|
2010-11-18 00:06:43 +08:00
|
|
|
bool AllocateGVsWithCode)
|
2011-04-05 07:04:39 +08:00
|
|
|
: ExecutionEngine(m), TM(tm), MemMgr(MM), M(m), OS(Buffer), Dyld(MM) {
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2011-10-01 00:40:10 +08:00
|
|
|
setTargetData(TM->getTargetData());
|
2011-03-19 06:48:41 +08:00
|
|
|
PM.add(new TargetData(*TM->getTargetData()));
|
|
|
|
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory
|
|
|
|
// that may be executed.
|
|
|
|
if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) {
|
|
|
|
report_fatal_error("Target does not support MC emission!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize passes.
|
|
|
|
// FIXME: When we support multiple modules, we'll want to move the code
|
|
|
|
// gen and finalization out of the constructor here and do it more
|
|
|
|
// on-demand as part of getPointerToFunction().
|
|
|
|
PM.run(*M);
|
|
|
|
// Flush the output buffer so the SmallVector gets its data.
|
|
|
|
OS.flush();
|
2011-03-22 09:06:42 +08:00
|
|
|
|
|
|
|
// Load the object into the dynamic linker.
|
|
|
|
// FIXME: It would be nice to avoid making yet another copy.
|
|
|
|
MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(),
|
|
|
|
Buffer.size()));
|
2011-03-24 03:51:34 +08:00
|
|
|
if (Dyld.loadObject(MB))
|
|
|
|
report_fatal_error(Dyld.getErrorString());
|
2011-04-13 23:28:10 +08:00
|
|
|
// Resolve any relocations.
|
|
|
|
Dyld.resolveRelocations();
|
2010-11-18 00:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MCJIT::~MCJIT() {
|
2011-04-05 07:04:39 +08:00
|
|
|
delete MemMgr;
|
2010-11-18 00:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
|
|
|
|
report_fatal_error("not yet implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MCJIT::getPointerToFunction(Function *F) {
|
2011-03-23 02:05:27 +08:00
|
|
|
if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
|
|
|
|
bool AbortOnFailure = !F->hasExternalWeakLinkage();
|
|
|
|
void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
|
|
|
|
addGlobalMapping(F, Addr);
|
|
|
|
return Addr;
|
|
|
|
}
|
|
|
|
|
2011-05-19 07:53:21 +08:00
|
|
|
// FIXME: Should we be using the mangler for this? Probably.
|
|
|
|
StringRef BaseName = F->getName();
|
|
|
|
if (BaseName[0] == '\1')
|
2011-05-19 08:45:05 +08:00
|
|
|
return (void*)Dyld.getSymbolAddress(BaseName.substr(1));
|
|
|
|
return (void*)Dyld.getSymbolAddress((TM->getMCAsmInfo()->getGlobalPrefix()
|
|
|
|
+ BaseName).str());
|
2010-11-18 00:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MCJIT::recompileAndRelinkFunction(Function *F) {
|
|
|
|
report_fatal_error("not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCJIT::freeMachineCodeForFunction(Function *F) {
|
|
|
|
report_fatal_error("not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericValue MCJIT::runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues) {
|
2011-03-23 02:05:27 +08:00
|
|
|
assert(F && "Function *F was null at entry to run()");
|
|
|
|
|
2011-03-19 06:48:41 +08:00
|
|
|
void *FPtr = getPointerToFunction(F);
|
2011-03-23 02:05:27 +08:00
|
|
|
assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *FTy = F->getFunctionType();
|
|
|
|
Type *RetTy = FTy->getReturnType();
|
2011-03-23 02:05:27 +08:00
|
|
|
|
|
|
|
assert((FTy->getNumParams() == ArgValues.size() ||
|
|
|
|
(FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
|
|
|
|
"Wrong number of arguments passed into function!");
|
|
|
|
assert(FTy->getNumParams() == ArgValues.size() &&
|
|
|
|
"This doesn't support passing arguments through varargs (yet)!");
|
|
|
|
|
|
|
|
// Handle some common cases first. These cases correspond to common `main'
|
|
|
|
// prototypes.
|
|
|
|
if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
|
|
|
|
switch (ArgValues.size()) {
|
|
|
|
case 3:
|
|
|
|
if (FTy->getParamType(0)->isIntegerTy(32) &&
|
|
|
|
FTy->getParamType(1)->isPointerTy() &&
|
|
|
|
FTy->getParamType(2)->isPointerTy()) {
|
|
|
|
int (*PF)(int, char **, const char **) =
|
|
|
|
(int(*)(int, char **, const char **))(intptr_t)FPtr;
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
GenericValue rv;
|
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(char **)GVTOP(ArgValues[1]),
|
|
|
|
(const char **)GVTOP(ArgValues[2])));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (FTy->getParamType(0)->isIntegerTy(32) &&
|
|
|
|
FTy->getParamType(1)->isPointerTy()) {
|
|
|
|
int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
GenericValue rv;
|
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
|
|
|
|
(char **)GVTOP(ArgValues[1])));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (FTy->getNumParams() == 1 &&
|
|
|
|
FTy->getParamType(0)->isIntegerTy(32)) {
|
|
|
|
GenericValue rv;
|
|
|
|
int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
|
|
|
|
rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle cases where no arguments are passed first.
|
|
|
|
if (ArgValues.empty()) {
|
|
|
|
GenericValue rv;
|
|
|
|
switch (RetTy->getTypeID()) {
|
|
|
|
default: llvm_unreachable("Unknown return type for function call!");
|
|
|
|
case Type::IntegerTyID: {
|
|
|
|
unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
|
|
|
|
if (BitWidth == 1)
|
|
|
|
rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
|
|
|
|
else if (BitWidth <= 8)
|
|
|
|
rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
|
|
|
|
else if (BitWidth <= 16)
|
|
|
|
rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
|
|
|
|
else if (BitWidth <= 32)
|
|
|
|
rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
|
|
|
|
else if (BitWidth <= 64)
|
|
|
|
rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
|
|
|
|
else
|
|
|
|
llvm_unreachable("Integer types > 64 bits not supported");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
case Type::VoidTyID:
|
|
|
|
rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
|
|
|
|
return rv;
|
|
|
|
case Type::FloatTyID:
|
|
|
|
rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
|
|
|
|
return rv;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
|
|
|
|
return rv;
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
case Type::FP128TyID:
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
llvm_unreachable("long double not supported yet");
|
|
|
|
return rv;
|
|
|
|
case Type::PointerTyID:
|
|
|
|
return PTOGV(((void*(*)())(intptr_t)FPtr)());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 09:42:07 +08:00
|
|
|
assert(0 && "Full-featured argument passing not supported yet!");
|
2010-11-18 00:06:43 +08:00
|
|
|
return GenericValue();
|
|
|
|
}
|