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" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | #include "llvm/Function.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame^] | 10 | #include "llvm/ModuleProvider.h" |
| 11 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 12 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 13 | |
| 14 | VM::~VM() { |
| 15 | delete MCE; |
| 16 | delete &TM; |
| 17 | } |
| 18 | |
| 19 | /// setupPassManager - Initialize the VM PassManager object with all of the |
| 20 | /// passes needed for the target to generate code. |
| 21 | /// |
| 22 | void VM::setupPassManager() { |
| 23 | // Compile LLVM Code down to machine code in the intermediate representation |
| 24 | if (TM.addPassesToJITCompile(PM)) { |
| 25 | std::cerr << "lli: target '" << TM.getName() |
| 26 | << "' doesn't support JIT compilation!\n"; |
| 27 | abort(); |
| 28 | } |
| 29 | |
| 30 | // Turn the machine code intermediate representation into bytes in memory that |
| 31 | // may be executed. |
| 32 | // |
| 33 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
| 34 | std::cerr << "lli: target '" << TM.getName() |
| 35 | << "' doesn't support machine code emission!\n"; |
| 36 | abort(); |
| 37 | } |
| 38 | } |
| 39 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 40 | /// getPointerToFunction - This method is used to get the address of the |
| 41 | /// specified function, compiling it if neccesary. |
| 42 | /// |
Brian Gaeke | 71d8478 | 2003-08-13 18:16:34 +0000 | [diff] [blame] | 43 | void *VM::getPointerToFunction(Function *F) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 44 | void *&Addr = GlobalAddress[F]; // Function already code gen'd |
| 45 | if (Addr) return Addr; |
| 46 | |
Misha Brukman | 005e5e9 | 2003-10-14 21:37:41 +0000 | [diff] [blame] | 47 | // Make sure we read in the function if it exists in this Module |
| 48 | MP->materializeFunction(F); |
| 49 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 50 | if (F->isExternal()) |
| 51 | return Addr = getPointerToNamedFunction(F->getName()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 53 | static bool isAlreadyCodeGenerating = false; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 54 | assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!"); |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 55 | |
Brian Gaeke | 71d8478 | 2003-08-13 18:16:34 +0000 | [diff] [blame] | 56 | // JIT the function |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 57 | isAlreadyCodeGenerating = true; |
Brian Gaeke | 71d8478 | 2003-08-13 18:16:34 +0000 | [diff] [blame] | 58 | PM.run(*F); |
Chris Lattner | 66a8494 | 2003-05-08 21:08:43 +0000 | [diff] [blame] | 59 | isAlreadyCodeGenerating = false; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 60 | |
| 61 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 62 | return Addr; |
| 63 | } |