Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 1 | //===-- JIT.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 | // |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 10 | // This tool implements a just-in-time compiler for LLVM, allowing direct |
| 11 | // execution of LLVM bytecode in an efficient manner. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 15 | #include "JIT.h" |
| 16 | #include "llvm/Function.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | 9722294 | 2003-09-05 19:39:22 +0000 | [diff] [blame] | 20 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 22 | #include "llvm/Target/TargetJITInfo.h" |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 25 | JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 26 | : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 27 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 28 | |
| 29 | // Initialize MCE |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 30 | MCE = createEmitter(*this); |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 32 | // Compile LLVM Code down to machine code in the intermediate representation |
| 33 | TJI.addPassesToJITCompile(PM); |
| 34 | |
| 35 | // Turn the machine code intermediate representation into bytes in memory that |
| 36 | // may be executed. |
| 37 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
| 38 | std::cerr << "lli: target '" << TM.getName() |
| 39 | << "' doesn't support machine code emission!\n"; |
| 40 | abort(); |
| 41 | } |
Misha Brukman | 4e8c999 | 2003-06-06 06:59:55 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 43 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 46 | JIT::~JIT() { |
| 47 | delete MCE; |
| 48 | delete &TM; |
| 49 | } |
| 50 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 51 | /// run - Start execution with the specified function and arguments. |
Chris Lattner | 05a1a30 | 2003-08-21 21:32:12 +0000 | [diff] [blame] | 52 | /// |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 53 | GenericValue JIT::run(Function *F, const std::vector<GenericValue> &ArgValues) { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 54 | assert (F && "Function *F was null at entry to run()"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 55 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 56 | int (*PF)(int, char **, const char **) = |
| 57 | (int(*)(int, char **, const char **))getPointerToFunction(F); |
| 58 | assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 59 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 60 | // Call the function. |
| 61 | int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]), |
| 62 | (const char **) GVTOP (ArgValues[2])); |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 63 | |
| 64 | // Run any atexit handlers now! |
| 65 | runAtExitHandlers(); |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 66 | |
| 67 | GenericValue rv; |
| 68 | rv.IntVal = ExitCode; |
| 69 | return rv; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame^] | 71 | |
| 72 | /// runJITOnFunction - Run the FunctionPassManager full of |
| 73 | /// just-in-time compilation passes on F, hopefully filling in |
| 74 | /// GlobalAddress[F] with the address of F's machine code. |
| 75 | /// |
| 76 | void JIT::runJITOnFunction(Function *F) { |
| 77 | static bool isAlreadyCodeGenerating = false; |
| 78 | assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); |
| 79 | |
| 80 | // JIT the function |
| 81 | isAlreadyCodeGenerating = true; |
| 82 | PM.run(*F); |
| 83 | isAlreadyCodeGenerating = false; |
| 84 | } |
| 85 | |
| 86 | /// getPointerToFunction - This method is used to get the address of the |
| 87 | /// specified function, compiling it if neccesary. |
| 88 | /// |
| 89 | void *JIT::getPointerToFunction(Function *F) { |
| 90 | void *&Addr = GlobalAddress[F]; // Check if function already code gen'd |
| 91 | if (Addr) return Addr; |
| 92 | |
| 93 | // Make sure we read in the function if it exists in this Module |
| 94 | MP->materializeFunction(F); |
| 95 | |
| 96 | if (F->isExternal()) |
| 97 | return Addr = getPointerToNamedFunction(F->getName()); |
| 98 | |
| 99 | runJITOnFunction(F); |
| 100 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 101 | return Addr; |
| 102 | } |
| 103 | |
| 104 | // getPointerToFunctionOrStub - If the specified function has been |
| 105 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 106 | // a stub to implement lazy compilation if available. |
| 107 | // |
| 108 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 109 | // If we have already code generated the function, just return the address. |
| 110 | std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F); |
| 111 | if (I != GlobalAddress.end()) return I->second; |
| 112 | |
| 113 | // If the target supports "stubs" for functions, get a stub now. |
| 114 | if (void *Ptr = TJI.getJITStubForFunction(F, *MCE)) |
| 115 | return Ptr; |
| 116 | |
| 117 | // Otherwise, if the target doesn't support it, just codegen the function. |
| 118 | return getPointerToFunction(F); |
| 119 | } |
| 120 | |
| 121 | /// recompileAndRelinkFunction - This method is used to force a function |
| 122 | /// which has already been compiled, to be compiled again, possibly |
| 123 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 124 | /// with a branch to the new copy. If there was no old copy, this acts |
| 125 | /// just like JIT::getPointerToFunction(). |
| 126 | /// |
| 127 | void *JIT::recompileAndRelinkFunction(Function *F) { |
| 128 | void *&Addr = GlobalAddress[F]; // Check if function already code gen'd |
| 129 | |
| 130 | // If it's not already compiled (this is kind of weird) there is no |
| 131 | // reason to patch it up. |
| 132 | if (!Addr) { return getPointerToFunction (F); } |
| 133 | |
| 134 | void *OldAddr = Addr; |
| 135 | Addr = 0; |
| 136 | MachineFunction::destruct(F); |
| 137 | runJITOnFunction(F); |
| 138 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 139 | TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
| 140 | return Addr; |
| 141 | } |