diff options
author | Chris Lattner <sabre@nondot.org> | 2002-12-24 00:01:05 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-12-24 00:01:05 +0000 |
commit | bd199fb1148b9e16c4e6f3d0ee386c2505a55b71 (patch) | |
tree | 226f64cc95160ebd8da8d2ec4cfaf2babf7c3879 /lib/ExecutionEngine/JIT/VM.cpp | |
parent | fe11a97fcde7c63109c3ad36570657807d0cd6ef (diff) |
Initial checkin of new LLI with JIT compiler
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5126 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ExecutionEngine/JIT/VM.cpp')
-rw-r--r-- | lib/ExecutionEngine/JIT/VM.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/ExecutionEngine/JIT/VM.cpp b/lib/ExecutionEngine/JIT/VM.cpp new file mode 100644 index 0000000000..f66d4d7b7e --- /dev/null +++ b/lib/ExecutionEngine/JIT/VM.cpp @@ -0,0 +1,84 @@ +//===-- jello.cpp - LLVM Just in Time Compiler ----------------------------===// +// +// This tool implements a just-in-time compiler for LLVM, allowing direct +// execution of LLVM bytecode in an efficient manner. +// +//===----------------------------------------------------------------------===// + +#include "VM.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/CodeGen/MachineCodeEmitter.h" +#include "llvm/Function.h" +#include <dlfcn.h> // dlsym access + + +VM::~VM() { + delete MCE; + delete &TM; +} + +/// setupPassManager - Initialize the VM PassManager object with all of the +/// passes needed for the target to generate code. +/// +void VM::setupPassManager() { + // Compile LLVM Code down to machine code in the intermediate representation + if (TM.addPassesToJITCompile(PM)) { + std::cerr << "lli: target '" << TM.getName() + << "' doesn't support JIT compilation!\n"; + abort(); + } + + // Turn the machine code intermediate representation into bytes in memory that + // may be executed. + // + if (TM.addPassesToEmitMachineCode(PM, *MCE)) { + std::cerr << "lli: target '" << TM.getName() + << "' doesn't support machine code emission!\n"; + abort(); + } +} + +void *VM::resolveFunctionReference(void *RefAddr) { + Function *F = FunctionRefs[RefAddr]; + assert(F && "Reference address not known!"); + + void *Addr = getPointerToFunction(F); + assert(Addr && "Pointer to function unknown!"); + + FunctionRefs.erase(RefAddr); + return Addr; +} + +const std::string &VM::getFunctionReferencedName(void *RefAddr) { + return FunctionRefs[RefAddr]->getName(); +} + +static void NoopFn() {} + +/// getPointerToFunction - This method is used to get the address of the +/// specified function, compiling it if neccesary. +/// +void *VM::getPointerToFunction(const Function *F) { + void *&Addr = GlobalAddress[F]; // Function already code gen'd + if (Addr) return Addr; + + if (F->isExternal()) { + // If it's an external function, look it up in the process image... + void *Ptr = dlsym(0, F->getName().c_str()); + if (Ptr == 0) { + std::cerr << "WARNING: Cannot resolve fn '" << F->getName() + << "' using a dummy noop function instead!\n"; + Ptr = (void*)NoopFn; + } + + return Addr = Ptr; + } + + // JIT all of the functions in the module. Eventually this will JIT functions + // on demand. This has the effect of populating all of the non-external + // functions into the GlobalAddress table. + PM.run(getModule()); + + assert(Addr && "Code generation didn't add function to GlobalAddress table!"); + return Addr; +} |