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"
|
2003-09-05 18:42:01 +00:00
|
|
|
#include "../GenericValue.h"
|
2002-12-24 00:01:05 +00:00
|
|
|
#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"
|
|
|
|
|
2003-06-06 06:59:55 +00:00
|
|
|
// FIXME: REMOVE THIS
|
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
|
2003-07-02 17:53:19 +00:00
|
|
|
#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
|
|
|
|
#define NO_JITS_ENABLED
|
|
|
|
#endif
|
|
|
|
|
2003-05-27 21:40:39 +00:00
|
|
|
namespace {
|
2003-07-02 17:53:19 +00:00
|
|
|
enum ArchName { x86, Sparc };
|
2003-06-17 15:43:13 +00:00
|
|
|
|
2003-07-02 17:53:19 +00:00
|
|
|
#ifndef NO_JITS_ENABLED
|
2003-06-17 15:43:13 +00:00
|
|
|
cl::opt<ArchName>
|
|
|
|
Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
|
2003-07-02 17:53:19 +00:00
|
|
|
cl::values(
|
|
|
|
#ifdef ENABLE_X86_JIT
|
|
|
|
clEnumVal(x86, " IA-32 (Pentium and above)"),
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_SPARC_JIT
|
2003-06-17 15:54:02 +00:00
|
|
|
clEnumValN(Sparc, "sparc", " Sparc-V9"),
|
2003-06-17 15:43:13 +00:00
|
|
|
#endif
|
|
|
|
0),
|
2003-07-02 17:53:19 +00:00
|
|
|
#if defined(ENABLE_X86_JIT)
|
2003-06-17 15:43:13 +00:00
|
|
|
cl::init(x86)
|
2003-07-02 17:53:19 +00:00
|
|
|
#elif defined(ENABLE_SPARC_JIT)
|
2003-06-17 15:54:02 +00:00
|
|
|
cl::init(Sparc)
|
2003-05-27 21:40:39 +00:00
|
|
|
#endif
|
2003-06-17 15:43:13 +00:00
|
|
|
);
|
2003-07-02 17:53:19 +00:00
|
|
|
#endif /* NO_JITS_ENABLED */
|
2003-05-27 21:40:39 +00:00
|
|
|
}
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2003-09-03 20:34:19 +00:00
|
|
|
/// create - Create an return a new JIT compiler if there is one available
|
|
|
|
/// for the current target. Otherwise, return null.
|
2002-12-24 00:01:05 +00:00
|
|
|
///
|
2003-09-03 20:34:19 +00:00
|
|
|
ExecutionEngine *VM::create(Module *M) {
|
2003-08-24 19:50:53 +00:00
|
|
|
TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
|
2003-05-27 21:40:39 +00:00
|
|
|
|
|
|
|
// 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 --
|
2003-07-02 17:53:19 +00:00
|
|
|
// our X86 machines are much faster at recompiling LLVM and linking LLI.
|
|
|
|
#ifdef NO_JITS_ENABLED
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2003-06-17 15:43:13 +00:00
|
|
|
switch (Arch) {
|
2003-07-02 17:53:19 +00:00
|
|
|
#ifdef ENABLE_X86_JIT
|
2003-06-17 15:43:13 +00:00
|
|
|
case x86:
|
2003-05-27 21:40:39 +00:00
|
|
|
TargetMachineAllocator = allocateX86TargetMachine;
|
2003-06-17 15:43:13 +00:00
|
|
|
break;
|
2003-07-02 17:53:19 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_SPARC_JIT
|
2003-06-17 15:54:02 +00:00
|
|
|
case Sparc:
|
2003-06-02 03:23:16 +00:00
|
|
|
TargetMachineAllocator = allocateSparcTargetMachine;
|
2003-06-17 15:43:13 +00:00
|
|
|
break;
|
2003-06-17 15:32:38 +00:00
|
|
|
#endif
|
2003-06-17 15:43:13 +00:00
|
|
|
default:
|
|
|
|
assert(0 && "-march flag not supported on this host!");
|
2003-05-27 21:40:39 +00:00
|
|
|
}
|
2003-06-17 15:43:13 +00:00
|
|
|
|
|
|
|
// Allocate a target...
|
2003-08-24 19:50:53 +00:00
|
|
|
TargetMachine *Target = TargetMachineAllocator(*M);
|
2003-06-17 15:43:13 +00:00
|
|
|
assert(Target && "Could not allocate target machine!");
|
|
|
|
|
|
|
|
// Create the virtual machine object...
|
|
|
|
return new VM(M, Target);
|
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
|
2003-06-02 03:23:16 +00:00
|
|
|
MCE = createEmitter(*this);
|
2003-05-27 21:40:39 +00:00
|
|
|
|
2002-12-24 00:01:05 +00:00
|
|
|
setupPassManager();
|
2003-06-06 06:59:55 +00:00
|
|
|
|
2003-07-02 17:53:19 +00:00
|
|
|
#ifdef ENABLE_SPARC_JIT
|
2003-06-06 06:59:55 +00:00
|
|
|
// THIS GOES BEYOND UGLY HACKS
|
|
|
|
if (TM.getName() == "UltraSparc-Native") {
|
|
|
|
extern Pass *createPreSelectionPass(TargetMachine &TM);
|
|
|
|
PassManager PM;
|
|
|
|
// Specialize LLVM code for this target machine and then
|
|
|
|
// run basic dataflow optimizations on LLVM code.
|
|
|
|
PM.add(createPreSelectionPass(TM));
|
|
|
|
PM.run(*M);
|
|
|
|
}
|
2003-06-17 15:32:38 +00:00
|
|
|
#endif
|
2003-06-06 06:59:55 +00:00
|
|
|
|
2003-05-12 02:14:34 +00:00
|
|
|
emitGlobals();
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
/// run - Start execution with the specified function and arguments.
|
2003-08-21 21:32:12 +00:00
|
|
|
///
|
2003-09-05 18:42:01 +00:00
|
|
|
GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
|
|
|
|
{
|
|
|
|
assert (F && "Function *F was null at entry to run()");
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
int (*PF)(int, char **, const char **) =
|
|
|
|
(int(*)(int, char **, const char **))getPointerToFunction(F);
|
|
|
|
assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
|
2002-12-24 00:01:05 +00:00
|
|
|
|
2003-09-05 18:42:01 +00:00
|
|
|
// Call the function.
|
|
|
|
int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
|
|
|
|
(const char **) GVTOP (ArgValues[2]));
|
2003-05-14 13:53:40 +00:00
|
|
|
|
|
|
|
// Run any atexit handlers now!
|
|
|
|
runAtExitHandlers();
|
2003-09-05 18:42:01 +00:00
|
|
|
|
|
|
|
GenericValue rv;
|
|
|
|
rv.IntVal = ExitCode;
|
|
|
|
return rv;
|
2002-12-24 00:01:05 +00:00
|
|
|
}
|