Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame^] | 1 | //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 2 | // |
| 3 | // This tool implements a just-in-time compiler for LLVM, allowing direct |
| 4 | // execution of LLVM bytecode in an efficient manner. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "VM.h" |
| 9 | #include "llvm/Target/TargetMachine.h" |
| 10 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 11 | #include "llvm/Function.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | |
| 13 | VM::~VM() { |
| 14 | delete MCE; |
| 15 | delete &TM; |
| 16 | } |
| 17 | |
| 18 | /// setupPassManager - Initialize the VM PassManager object with all of the |
| 19 | /// passes needed for the target to generate code. |
| 20 | /// |
| 21 | void VM::setupPassManager() { |
| 22 | // Compile LLVM Code down to machine code in the intermediate representation |
| 23 | if (TM.addPassesToJITCompile(PM)) { |
| 24 | std::cerr << "lli: target '" << TM.getName() |
| 25 | << "' doesn't support JIT compilation!\n"; |
| 26 | abort(); |
| 27 | } |
| 28 | |
| 29 | // Turn the machine code intermediate representation into bytes in memory that |
| 30 | // may be executed. |
| 31 | // |
| 32 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
| 33 | std::cerr << "lli: target '" << TM.getName() |
| 34 | << "' doesn't support machine code emission!\n"; |
| 35 | abort(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void *VM::resolveFunctionReference(void *RefAddr) { |
| 40 | Function *F = FunctionRefs[RefAddr]; |
| 41 | assert(F && "Reference address not known!"); |
| 42 | |
| 43 | void *Addr = getPointerToFunction(F); |
| 44 | assert(Addr && "Pointer to function unknown!"); |
| 45 | |
| 46 | FunctionRefs.erase(RefAddr); |
| 47 | return Addr; |
| 48 | } |
| 49 | |
| 50 | const std::string &VM::getFunctionReferencedName(void *RefAddr) { |
Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame^] | 51 | assert(FunctionRefs[RefAddr] && "Function address unknown!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 52 | return FunctionRefs[RefAddr]->getName(); |
| 53 | } |
| 54 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 55 | /// getPointerToFunction - This method is used to get the address of the |
| 56 | /// specified function, compiling it if neccesary. |
| 57 | /// |
| 58 | void *VM::getPointerToFunction(const Function *F) { |
| 59 | void *&Addr = GlobalAddress[F]; // Function already code gen'd |
| 60 | if (Addr) return Addr; |
| 61 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 62 | if (F->isExternal()) |
| 63 | return Addr = getPointerToNamedFunction(F->getName()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 65 | static bool isAlreadyCodeGenerating = false; |
| 66 | if (isAlreadyCodeGenerating) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 67 | // Generate a function stub instead of reentering... |
| 68 | void *SAddr = emitStubForFunction(*F); |
| 69 | assert(SAddr && "Target machine doesn't support function stub generation!"); |
| 70 | return SAddr; |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // FIXME: JIT all of the functions in the module. Eventually this will JIT |
| 74 | // functions on demand. This has the effect of populating all of the |
| 75 | // non-external functions into the GlobalAddress table. |
| 76 | isAlreadyCodeGenerating = true; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 77 | PM.run(getModule()); |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 78 | isAlreadyCodeGenerating = false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 79 | |
| 80 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 81 | return Addr; |
| 82 | } |