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