Chris Lattner | 6701a86 | 2003-05-14 13:26:47 +0000 | [diff] [blame] | 1 | //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // This tool implements a just-in-time compiler for LLVM, allowing direct |
| 11 | // execution of LLVM bytecode in an efficient manner. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "VM.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
| 18 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Misha Brukman | 0ca042d | 2003-11-17 20:37:02 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetJITInfo.h" |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 24 | VM::~VM() { |
| 25 | delete MCE; |
| 26 | delete &TM; |
| 27 | } |
| 28 | |
| 29 | /// setupPassManager - Initialize the VM PassManager object with all of the |
| 30 | /// passes needed for the target to generate code. |
| 31 | /// |
| 32 | void VM::setupPassManager() { |
| 33 | // Compile LLVM Code down to machine code in the intermediate representation |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 34 | TJI.addPassesToJITCompile(PM); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 35 | |
| 36 | // Turn the machine code intermediate representation into bytes in memory that |
| 37 | // may be executed. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 38 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
| 39 | std::cerr << "lli: target '" << TM.getName() |
| 40 | << "' doesn't support machine code emission!\n"; |
| 41 | abort(); |
| 42 | } |
| 43 | } |
| 44 | |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 45 | /// runJITOnFunction - Run the FunctionPassManager full of |
| 46 | /// just-in-time compilation passes on F, hopefully filling in |
| 47 | /// GlobalAddress[F] with the address of F's machine code. |
| 48 | /// |
Misha Brukman | 0ca042d | 2003-11-17 20:37:02 +0000 | [diff] [blame] | 49 | void VM::runJITOnFunction(Function *F) { |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 50 | static bool isAlreadyCodeGenerating = false; |
Misha Brukman | 0ca042d | 2003-11-17 20:37:02 +0000 | [diff] [blame] | 51 | assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 52 | |
| 53 | // JIT the function |
| 54 | isAlreadyCodeGenerating = true; |
| 55 | PM.run(*F); |
| 56 | isAlreadyCodeGenerating = false; |
| 57 | } |
| 58 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 59 | /// getPointerToFunction - This method is used to get the address of the |
| 60 | /// specified function, compiling it if neccesary. |
| 61 | /// |
Brian Gaeke | 71d8478 | 2003-08-13 18:16:34 +0000 | [diff] [blame] | 62 | void *VM::getPointerToFunction(Function *F) { |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 63 | void *&Addr = GlobalAddress[F]; // Check if function already code gen'd |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 64 | if (Addr) return Addr; |
| 65 | |
Misha Brukman | 005e5e9 | 2003-10-14 21:37:41 +0000 | [diff] [blame] | 66 | // Make sure we read in the function if it exists in this Module |
| 67 | MP->materializeFunction(F); |
| 68 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 69 | if (F->isExternal()) |
| 70 | return Addr = getPointerToNamedFunction(F->getName()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 71 | |
Misha Brukman | 0ca042d | 2003-11-17 20:37:02 +0000 | [diff] [blame] | 72 | runJITOnFunction(F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 73 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 74 | return Addr; |
| 75 | } |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 993bdce | 2003-12-12 07:12:02 +0000 | [diff] [blame] | 77 | // getPointerToFunctionOrStub - If the specified function has been |
| 78 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 79 | // a stub to implement lazy compilation if available. |
| 80 | // |
| 81 | void *VM::getPointerToFunctionOrStub(Function *F) { |
| 82 | // If we have already code generated the function, just return the address. |
| 83 | std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F); |
| 84 | if (I != GlobalAddress.end()) return I->second; |
| 85 | |
| 86 | // If the target supports "stubs" for functions, get a stub now. |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 87 | if (void *Ptr = TJI.getJITStubForFunction(F, *MCE)) |
Chris Lattner | 993bdce | 2003-12-12 07:12:02 +0000 | [diff] [blame] | 88 | return Ptr; |
| 89 | |
| 90 | // Otherwise, if the target doesn't support it, just codegen the function. |
| 91 | return getPointerToFunction(F); |
| 92 | } |
| 93 | |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 94 | /// recompileAndRelinkFunction - This method is used to force a function |
| 95 | /// which has already been compiled, to be compiled again, possibly |
| 96 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 97 | /// with a branch to the new copy. If there was no old copy, this acts |
| 98 | /// just like VM::getPointerToFunction(). |
| 99 | /// |
| 100 | void *VM::recompileAndRelinkFunction(Function *F) { |
| 101 | void *&Addr = GlobalAddress[F]; // Check if function already code gen'd |
| 102 | |
| 103 | // If it's not already compiled (this is kind of weird) there is no |
| 104 | // reason to patch it up. |
| 105 | if (!Addr) { return getPointerToFunction (F); } |
| 106 | |
| 107 | void *OldAddr = Addr; |
| 108 | Addr = 0; |
Misha Brukman | 0ca042d | 2003-11-17 20:37:02 +0000 | [diff] [blame] | 109 | MachineFunction::destruct(F); |
| 110 | runJITOnFunction(F); |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 111 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 112 | TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
Brian Gaeke | cd64ddf | 2003-10-17 18:27:00 +0000 | [diff] [blame] | 113 | return Addr; |
| 114 | } |