2002-12-24 00:01:05 +00:00
|
|
|
//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the top-level support for creating a Just-In-Time
|
|
|
|
// compiler for the current architecture.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "VM.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
|
|
|
#include "llvm/Module.h"
|
2003-05-27 21:40:39 +00:00
|
|
|
#include "Support/CommandLine.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
Arch("march", cl::desc("Architecture: `x86' or `sparc'"), cl::Prefix,
|
|
|
|
cl::value_desc("machine architecture"));
|
|
|
|
|
|
|
|
static std::string DefaultArch =
|
|
|
|
#if defined(i386) || defined(__i386__) || defined(__x86__)
|
|
|
|
"x86";
|
|
|
|
#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
|
|
|
"sparc";
|
|
|
|
#else
|
|
|
|
"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2002-12-24 00:01:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// createJIT - Create an return a new JIT compiler if there is one available
|
|
|
|
/// for the current target. Otherwise it returns null.
|
|
|
|
///
|
|
|
|
ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
|
2003-05-27 21:40:39 +00:00
|
|
|
|
|
|
|
TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
|
|
|
|
if (Arch == "")
|
|
|
|
Arch = DefaultArch;
|
|
|
|
|
|
|
|
// Allow a command-line switch to override what *should* be the default target
|
|
|
|
// machine for this platform. This allows for debugging a Sparc JIT on X86 --
|
|
|
|
// our X86 machines are much faster at recompiling LLVM and linking lli.
|
|
|
|
if (Arch == "x86") {
|
|
|
|
TargetMachineAllocator = allocateX86TargetMachine;
|
|
|
|
} else if (Arch == "sparc") {
|
2003-06-01 23:24:36 +00:00
|
|
|
//TargetMachineAllocator = allocateSparcTargetMachine;
|
2003-05-27 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TargetMachineAllocator) {
|
|
|
|
// Allocate a target...
|
|
|
|
TargetMachine *Target = (*TargetMachineAllocator)(Config);
|
|
|
|
assert(Target && "Could not allocate target machine!");
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2003-05-27 21:40:39 +00:00
|
|
|
// Create the virtual machine object...
|
|
|
|
return new VM(M, Target);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
|
|
|
|
setTargetData(TM.getTargetData());
|
2003-05-27 21:40:39 +00:00
|
|
|
|
|
|
|
// Initialize MCE
|
|
|
|
if (Arch == "x86") {
|
|
|
|
MCE = createX86Emitter(*this);
|
|
|
|
} else if (Arch == "sparc") {
|
2003-06-01 23:24:36 +00:00
|
|
|
//MCE = createSparcEmitter(*this);
|
2003-05-27 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-12-24 00:01:05 +00:00
|
|
|
setupPassManager();
|
2003-05-12 02:14:34 +00:00
|
|
|
emitGlobals();
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
|
|
|
|
Function *F = getModule().getNamedFunction(FnName);
|
|
|
|
if (F == 0) {
|
|
|
|
std::cerr << "Could not find function '" << FnName <<"' in module!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
|
|
|
|
assert(PF != 0 && "Null pointer to function?");
|
|
|
|
|
|
|
|
// Build an argv vector...
|
|
|
|
char **Argv = (char**)CreateArgv(Args);
|
|
|
|
|
|
|
|
// Call the main function...
|
2003-05-14 13:53:40 +00:00
|
|
|
int Result = PF(Args.size(), Argv);
|
|
|
|
|
|
|
|
// Run any atexit handlers now!
|
|
|
|
runAtExitHandlers();
|
|
|
|
return Result;
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|