Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1 | //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 21 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | 9722294 | 2003-09-05 19:39:22 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MutexGuard.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 26 | #include "llvm/System/DynamicLibrary.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame^] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetJITInfo.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 30 | #include <iostream> |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 33 | static struct RegisterJIT { |
| 34 | RegisterJIT() { JIT::Register(); } |
| 35 | } JITRegistrator; |
| 36 | |
Jeff Cohen | 2f51914 | 2006-03-24 02:53:49 +0000 | [diff] [blame] | 37 | namespace llvm { |
| 38 | void LinkInJIT() { |
| 39 | } |
| 40 | } |
| 41 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 42 | JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 43 | : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 44 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 45 | |
| 46 | // Initialize MCE |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 47 | MCE = createEmitter(*this); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 48 | |
Brian Gaeke | 50872d5 | 2004-04-14 17:45:52 +0000 | [diff] [blame] | 49 | // Add target data |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 50 | MutexGuard locked(lock); |
| 51 | FunctionPassManager& PM = state.getPM(locked); |
Chris Lattner | b93b034 | 2006-05-04 21:18:40 +0000 | [diff] [blame] | 52 | PM.add(new TargetData(*TM.getTargetData())); |
Brian Gaeke | 50872d5 | 2004-04-14 17:45:52 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 54 | // Compile LLVM Code down to machine code in the intermediate representation |
| 55 | TJI.addPassesToJITCompile(PM); |
| 56 | |
| 57 | // Turn the machine code intermediate representation into bytes in memory that |
| 58 | // may be executed. |
| 59 | if (TM.addPassesToEmitMachineCode(PM, *MCE)) { |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 60 | std::cerr << "Target '" << TM.getName() |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 61 | << "' doesn't support machine code emission!\n"; |
| 62 | abort(); |
| 63 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 66 | JIT::~JIT() { |
| 67 | delete MCE; |
| 68 | delete &TM; |
| 69 | } |
| 70 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 71 | /// run - Start execution with the specified function and arguments. |
Chris Lattner | 05a1a30 | 2003-08-21 21:32:12 +0000 | [diff] [blame] | 72 | /// |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 73 | GenericValue JIT::runFunction(Function *F, |
| 74 | const std::vector<GenericValue> &ArgValues) { |
Chris Lattner | b47130c | 2004-08-15 23:29:50 +0000 | [diff] [blame] | 75 | assert(F && "Function *F was null at entry to run()"); |
Chris Lattner | b47130c | 2004-08-15 23:29:50 +0000 | [diff] [blame] | 76 | |
| 77 | void *FPtr = getPointerToFunction(F); |
Chris Lattner | 7c45d78 | 2004-08-15 23:31:43 +0000 | [diff] [blame] | 78 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 79 | const FunctionType *FTy = F->getFunctionType(); |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 80 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 81 | |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 82 | assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) && |
| 83 | "Too many arguments passed into function!"); |
| 84 | assert(FTy->getNumParams() == ArgValues.size() && |
| 85 | "This doesn't support passing arguments through varargs (yet)!"); |
| 86 | |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 87 | // Handle some common cases first. These cases correspond to common `main' |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 88 | // prototypes. |
Chris Lattner | d297aea | 2004-08-15 23:34:48 +0000 | [diff] [blame] | 89 | if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) { |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 90 | switch (ArgValues.size()) { |
| 91 | case 3: |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 92 | if ((FTy->getParamType(0) == Type::IntTy || |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 93 | FTy->getParamType(0) == Type::UIntTy) && |
| 94 | isa<PointerType>(FTy->getParamType(1)) && |
| 95 | isa<PointerType>(FTy->getParamType(2))) { |
| 96 | int (*PF)(int, char **, const char **) = |
| 97 | (int(*)(int, char **, const char **))FPtr; |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 98 | |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 99 | // Call the function. |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 100 | GenericValue rv; |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 101 | rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]), |
| 102 | (const char **)GVTOP(ArgValues[2])); |
| 103 | return rv; |
| 104 | } |
| 105 | break; |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 106 | case 2: |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 107 | if ((FTy->getParamType(0) == Type::IntTy || |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 108 | FTy->getParamType(0) == Type::UIntTy) && |
| 109 | isa<PointerType>(FTy->getParamType(1))) { |
| 110 | int (*PF)(int, char **) = (int(*)(int, char **))FPtr; |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 112 | // Call the function. |
| 113 | GenericValue rv; |
| 114 | rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1])); |
| 115 | return rv; |
| 116 | } |
| 117 | break; |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 118 | case 1: |
| 119 | if (FTy->getNumParams() == 1 && |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 120 | (FTy->getParamType(0) == Type::IntTy || |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 121 | FTy->getParamType(0) == Type::UIntTy)) { |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 122 | GenericValue rv; |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 123 | int (*PF)(int) = (int(*)(int))FPtr; |
| 124 | rv.IntVal = PF(ArgValues[0].IntVal); |
| 125 | return rv; |
| 126 | } |
| 127 | break; |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Handle cases where no arguments are passed first. |
| 132 | if (ArgValues.empty()) { |
| 133 | GenericValue rv; |
| 134 | switch (RetTy->getTypeID()) { |
| 135 | default: assert(0 && "Unknown return type for function call!"); |
| 136 | case Type::BoolTyID: |
| 137 | rv.BoolVal = ((bool(*)())FPtr)(); |
Chris Lattner | d297aea | 2004-08-15 23:34:48 +0000 | [diff] [blame] | 138 | return rv; |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 139 | case Type::SByteTyID: |
| 140 | case Type::UByteTyID: |
| 141 | rv.SByteVal = ((char(*)())FPtr)(); |
| 142 | return rv; |
| 143 | case Type::ShortTyID: |
| 144 | case Type::UShortTyID: |
| 145 | rv.ShortVal = ((short(*)())FPtr)(); |
| 146 | return rv; |
| 147 | case Type::VoidTyID: |
| 148 | case Type::IntTyID: |
| 149 | case Type::UIntTyID: |
| 150 | rv.IntVal = ((int(*)())FPtr)(); |
| 151 | return rv; |
| 152 | case Type::LongTyID: |
| 153 | case Type::ULongTyID: |
| 154 | rv.LongVal = ((int64_t(*)())FPtr)(); |
| 155 | return rv; |
| 156 | case Type::FloatTyID: |
| 157 | rv.FloatVal = ((float(*)())FPtr)(); |
| 158 | return rv; |
| 159 | case Type::DoubleTyID: |
| 160 | rv.DoubleVal = ((double(*)())FPtr)(); |
| 161 | return rv; |
| 162 | case Type::PointerTyID: |
| 163 | return PTOGV(((void*(*)())FPtr)()); |
Chris Lattner | d297aea | 2004-08-15 23:34:48 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 166 | |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 167 | // Okay, this is not one of our quick and easy cases. Because we don't have a |
| 168 | // full FFI, we have to codegen a nullary stub function that just calls the |
| 169 | // function we are interested in, passing in constants for all of the |
| 170 | // arguments. Make this function and return. |
| 171 | |
| 172 | // First, create the function. |
| 173 | FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false); |
| 174 | Function *Stub = new Function(STy, Function::InternalLinkage, "", |
| 175 | F->getParent()); |
| 176 | |
| 177 | // Insert a basic block. |
| 178 | BasicBlock *StubBB = new BasicBlock("", Stub); |
| 179 | |
| 180 | // Convert all of the GenericValue arguments over to constants. Note that we |
| 181 | // currently don't support varargs. |
| 182 | std::vector<Value*> Args; |
| 183 | for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { |
| 184 | Constant *C = 0; |
| 185 | const Type *ArgTy = FTy->getParamType(i); |
| 186 | const GenericValue &AV = ArgValues[i]; |
| 187 | switch (ArgTy->getTypeID()) { |
| 188 | default: assert(0 && "Unknown argument type for function call!"); |
| 189 | case Type::BoolTyID: C = ConstantBool::get(AV.BoolVal); break; |
| 190 | case Type::SByteTyID: C = ConstantSInt::get(ArgTy, AV.SByteVal); break; |
| 191 | case Type::UByteTyID: C = ConstantUInt::get(ArgTy, AV.UByteVal); break; |
| 192 | case Type::ShortTyID: C = ConstantSInt::get(ArgTy, AV.ShortVal); break; |
| 193 | case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break; |
| 194 | case Type::IntTyID: C = ConstantSInt::get(ArgTy, AV.IntVal); break; |
| 195 | case Type::UIntTyID: C = ConstantUInt::get(ArgTy, AV.UIntVal); break; |
| 196 | case Type::LongTyID: C = ConstantSInt::get(ArgTy, AV.LongVal); break; |
| 197 | case Type::ULongTyID: C = ConstantUInt::get(ArgTy, AV.ULongVal); break; |
| 198 | case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break; |
| 199 | case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break; |
| 200 | case Type::PointerTyID: |
| 201 | void *ArgPtr = GVTOP(AV); |
| 202 | if (sizeof(void*) == 4) { |
| 203 | C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr); |
| 204 | } else { |
| 205 | C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr); |
| 206 | } |
| 207 | C = ConstantExpr::getCast(C, ArgTy); // Cast the integer to pointer |
| 208 | break; |
| 209 | } |
| 210 | Args.push_back(C); |
| 211 | } |
| 212 | |
Chris Lattner | a471e04 | 2005-05-06 06:48:54 +0000 | [diff] [blame] | 213 | CallInst *TheCall = new CallInst(F, Args, "", StubBB); |
| 214 | TheCall->setTailCall(); |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 215 | if (TheCall->getType() != Type::VoidTy) |
| 216 | new ReturnInst(TheCall, StubBB); // Return result of the call. |
| 217 | else |
| 218 | new ReturnInst(StubBB); // Just return void. |
| 219 | |
| 220 | // Finally, return the value returned by our nullary stub function. |
| 221 | return runFunction(Stub, std::vector<GenericValue>()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 223 | |
| 224 | /// runJITOnFunction - Run the FunctionPassManager full of |
| 225 | /// just-in-time compilation passes on F, hopefully filling in |
| 226 | /// GlobalAddress[F] with the address of F's machine code. |
| 227 | /// |
| 228 | void JIT::runJITOnFunction(Function *F) { |
| 229 | static bool isAlreadyCodeGenerating = false; |
| 230 | assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 231 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 232 | MutexGuard locked(lock); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 233 | |
| 234 | // JIT the function |
| 235 | isAlreadyCodeGenerating = true; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 236 | state.getPM(locked).run(*F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 237 | isAlreadyCodeGenerating = false; |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 238 | |
| 239 | // If the function referred to a global variable that had not yet been |
| 240 | // emitted, it allocates memory for the global, but doesn't emit it yet. Emit |
| 241 | // all of these globals now. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 242 | while (!state.getPendingGlobals(locked).empty()) { |
| 243 | const GlobalVariable *GV = state.getPendingGlobals(locked).back(); |
| 244 | state.getPendingGlobals(locked).pop_back(); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 245 | EmitGlobalVariable(GV); |
| 246 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /// getPointerToFunction - This method is used to get the address of the |
| 250 | /// specified function, compiling it if neccesary. |
| 251 | /// |
| 252 | void *JIT::getPointerToFunction(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 253 | MutexGuard locked(lock); |
| 254 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 255 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 256 | return Addr; // Check if function already code gen'd |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 257 | |
| 258 | // Make sure we read in the function if it exists in this Module |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 259 | if (F->hasNotBeenReadFromBytecode()) |
Chris Lattner | 0050ef8 | 2004-11-15 23:18:09 +0000 | [diff] [blame] | 260 | try { |
| 261 | MP->materializeFunction(F); |
| 262 | } catch ( std::string& errmsg ) { |
| 263 | std::cerr << "Error reading function '" << F->getName() |
| 264 | << "' from bytecode file: " << errmsg << "\n"; |
| 265 | abort(); |
| 266 | } catch (...) { |
| 267 | std::cerr << "Error reading function '" << F->getName() |
| 268 | << "from bytecode file!\n"; |
| 269 | abort(); |
| 270 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 271 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 272 | if (F->isExternal()) { |
| 273 | void *Addr = getPointerToNamedFunction(F->getName()); |
| 274 | addGlobalMapping(F, Addr); |
| 275 | return Addr; |
| 276 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 277 | |
| 278 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 279 | |
| 280 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 281 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 282 | return Addr; |
| 283 | } |
| 284 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 285 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 286 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 287 | /// Emitter. |
| 288 | void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 289 | MutexGuard locked(lock); |
| 290 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 291 | void *Ptr = getPointerToGlobalIfAvailable(GV); |
| 292 | if (Ptr) return Ptr; |
| 293 | |
| 294 | // If the global is external, just remember the address. |
| 295 | if (GV->isExternal()) { |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 296 | Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str()); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 297 | if (Ptr == 0) { |
| 298 | std::cerr << "Could not resolve external global address: " |
| 299 | << GV->getName() << "\n"; |
| 300 | abort(); |
| 301 | } |
| 302 | } else { |
| 303 | // If the global hasn't been emitted to memory yet, allocate space. We will |
| 304 | // actually initialize the global after current function has finished |
| 305 | // compilation. |
Chris Lattner | f5d438c | 2006-05-02 21:57:51 +0000 | [diff] [blame] | 306 | const Type *GlobalType = GV->getType()->getElementType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 307 | size_t S = getTargetData()->getTypeSize(GlobalType); |
| 308 | size_t A = getTargetData()->getTypeAlignment(GlobalType); |
Chris Lattner | f5d438c | 2006-05-02 21:57:51 +0000 | [diff] [blame] | 309 | if (A <= 8) { |
| 310 | Ptr = malloc(S); |
| 311 | } else { |
| 312 | // Allocate S+A bytes of memory, then use an aligned pointer within that |
| 313 | // space. |
| 314 | Ptr = malloc(S+A); |
| 315 | unsigned MisAligned = ((intptr_t)Ptr & (A-1)); |
| 316 | unsigned Offset = MisAligned ? (A-MisAligned) : 0; |
| 317 | |
| 318 | // Trim the tail off the memory block. |
| 319 | realloc(Ptr, S+Offset); |
| 320 | Ptr = (char*)Ptr + Offset; |
| 321 | } |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 322 | state.getPendingGlobals(locked).push_back(GV); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 323 | } |
| 324 | addGlobalMapping(GV, Ptr); |
| 325 | return Ptr; |
| 326 | } |
| 327 | |
| 328 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 329 | /// recompileAndRelinkFunction - This method is used to force a function |
| 330 | /// which has already been compiled, to be compiled again, possibly |
| 331 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 332 | /// with a branch to the new copy. If there was no old copy, this acts |
| 333 | /// just like JIT::getPointerToFunction(). |
| 334 | /// |
| 335 | void *JIT::recompileAndRelinkFunction(Function *F) { |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 336 | void *OldAddr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 337 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 338 | // If it's not already compiled there is no reason to patch it up. |
| 339 | if (OldAddr == 0) { return getPointerToFunction(F); } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 340 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 341 | // Delete the old function mapping. |
| 342 | addGlobalMapping(F, 0); |
| 343 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 344 | // Recodegen the function |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 345 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 346 | |
| 347 | // Update state, forward the old function to the new function. |
| 348 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 349 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 350 | TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
| 351 | return Addr; |
| 352 | } |
Misha Brukman | 895eddf | 2004-11-07 23:58:46 +0000 | [diff] [blame] | 353 | |