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" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalVariable.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | 9722294 | 2003-09-05 19:39:22 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetJITInfo.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 25 | #include "Support/DynamicLinker.h" |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 28 | JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 29 | : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 30 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 31 | |
| 32 | // Initialize MCE |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 33 | MCE = createEmitter(*this); |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 35 | // Compile LLVM Code down to machine code in the intermediate representation |
| 36 | TJI.addPassesToJITCompile(PM); |
| 37 | |
| 38 | // Turn the machine code intermediate representation into bytes in memory that |
| 39 | // may be executed. |
| 40 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
| 41 | std::cerr << "lli: target '" << TM.getName() |
| 42 | << "' doesn't support machine code emission!\n"; |
| 43 | abort(); |
| 44 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 47 | JIT::~JIT() { |
| 48 | delete MCE; |
| 49 | delete &TM; |
| 50 | } |
| 51 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 52 | /// run - Start execution with the specified function and arguments. |
Chris Lattner | 05a1a30 | 2003-08-21 21:32:12 +0000 | [diff] [blame] | 53 | /// |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame^] | 54 | GenericValue JIT::runFunction(Function *F, |
| 55 | const std::vector<GenericValue> &ArgValues) { |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 56 | assert (F && "Function *F was null at entry to run()"); |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame^] | 57 | GenericValue rv; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 58 | |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame^] | 59 | if (ArgValues.size() == 3) { |
| 60 | int (*PF)(int, char **, const char **) = |
| 61 | (int(*)(int, char **, const char **))getPointerToFunction(F); |
| 62 | assert(PF && "Pointer to fn's code was null after getPointerToFunction"); |
| 63 | |
| 64 | // Call the function. |
| 65 | int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]), |
| 66 | (const char **) GVTOP (ArgValues[2])); |
| 67 | |
| 68 | rv.IntVal = ExitCode; |
| 69 | } else { |
| 70 | // FIXME: This code should handle a couple of common cases efficiently, but |
| 71 | // it should also implement the general case by code-gening a new anonymous |
| 72 | // nullary function to call. |
| 73 | assert(ArgValues.size() == 1); |
| 74 | void (*PF)(int) = (void(*)(int))getPointerToFunction(F); |
| 75 | assert(PF && "Pointer to fn's code was null after getPointerToFunction"); |
| 76 | PF(ArgValues[0].IntVal); |
| 77 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 78 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 79 | return rv; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 81 | |
| 82 | /// runJITOnFunction - Run the FunctionPassManager full of |
| 83 | /// just-in-time compilation passes on F, hopefully filling in |
| 84 | /// GlobalAddress[F] with the address of F's machine code. |
| 85 | /// |
| 86 | void JIT::runJITOnFunction(Function *F) { |
| 87 | static bool isAlreadyCodeGenerating = false; |
| 88 | assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); |
| 89 | |
| 90 | // JIT the function |
| 91 | isAlreadyCodeGenerating = true; |
| 92 | PM.run(*F); |
| 93 | isAlreadyCodeGenerating = false; |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 94 | |
| 95 | // If the function referred to a global variable that had not yet been |
| 96 | // emitted, it allocates memory for the global, but doesn't emit it yet. Emit |
| 97 | // all of these globals now. |
| 98 | while (!PendingGlobals.empty()) { |
| 99 | const GlobalVariable *GV = PendingGlobals.back(); |
| 100 | PendingGlobals.pop_back(); |
| 101 | EmitGlobalVariable(GV); |
| 102 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// getPointerToFunction - This method is used to get the address of the |
| 106 | /// specified function, compiling it if neccesary. |
| 107 | /// |
| 108 | void *JIT::getPointerToFunction(Function *F) { |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 109 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 110 | return Addr; // Check if function already code gen'd |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 111 | |
| 112 | // Make sure we read in the function if it exists in this Module |
| 113 | MP->materializeFunction(F); |
| 114 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 115 | if (F->isExternal()) { |
| 116 | void *Addr = getPointerToNamedFunction(F->getName()); |
| 117 | addGlobalMapping(F, Addr); |
| 118 | return Addr; |
| 119 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 120 | |
| 121 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 122 | |
| 123 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 124 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 125 | return Addr; |
| 126 | } |
| 127 | |
| 128 | // getPointerToFunctionOrStub - If the specified function has been |
| 129 | // code-gen'd, return a pointer to the function. If not, compile it, or use |
| 130 | // a stub to implement lazy compilation if available. |
| 131 | // |
| 132 | void *JIT::getPointerToFunctionOrStub(Function *F) { |
| 133 | // If we have already code generated the function, just return the address. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 134 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 135 | return Addr; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 136 | |
| 137 | // If the target supports "stubs" for functions, get a stub now. |
| 138 | if (void *Ptr = TJI.getJITStubForFunction(F, *MCE)) |
| 139 | return Ptr; |
| 140 | |
| 141 | // Otherwise, if the target doesn't support it, just codegen the function. |
| 142 | return getPointerToFunction(F); |
| 143 | } |
| 144 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 145 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 146 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 147 | /// Emitter. |
| 148 | void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { |
| 149 | void *Ptr = getPointerToGlobalIfAvailable(GV); |
| 150 | if (Ptr) return Ptr; |
| 151 | |
| 152 | // If the global is external, just remember the address. |
| 153 | if (GV->isExternal()) { |
| 154 | Ptr = GetAddressOfSymbol(GV->getName().c_str()); |
| 155 | if (Ptr == 0) { |
| 156 | std::cerr << "Could not resolve external global address: " |
| 157 | << GV->getName() << "\n"; |
| 158 | abort(); |
| 159 | } |
| 160 | } else { |
| 161 | // If the global hasn't been emitted to memory yet, allocate space. We will |
| 162 | // actually initialize the global after current function has finished |
| 163 | // compilation. |
| 164 | Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())]; |
| 165 | PendingGlobals.push_back(GV); |
| 166 | } |
| 167 | addGlobalMapping(GV, Ptr); |
| 168 | return Ptr; |
| 169 | } |
| 170 | |
| 171 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 172 | /// recompileAndRelinkFunction - This method is used to force a function |
| 173 | /// which has already been compiled, to be compiled again, possibly |
| 174 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 175 | /// with a branch to the new copy. If there was no old copy, this acts |
| 176 | /// just like JIT::getPointerToFunction(). |
| 177 | /// |
| 178 | void *JIT::recompileAndRelinkFunction(Function *F) { |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 179 | void *OldAddr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 180 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 181 | // If it's not already compiled there is no reason to patch it up. |
| 182 | if (OldAddr == 0) { return getPointerToFunction(F); } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 183 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 184 | // Delete the old function mapping. |
| 185 | addGlobalMapping(F, 0); |
| 186 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 187 | // Recodegen the function |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 188 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 189 | |
| 190 | // Update state, forward the old function to the new function. |
| 191 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 192 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 193 | TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
| 194 | return Addr; |
| 195 | } |