JIT.cpp revision 4fe16d607d11e29d742208894909733f5ad01f8f
1//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// 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//===----------------------------------------------------------------------===// 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 "JIT.h" 16#include "llvm/Constants.h" 17#include "llvm/DerivedTypes.h" 18#include "llvm/Function.h" 19#include "llvm/GlobalVariable.h" 20#include "llvm/Instructions.h" 21#include "llvm/ModuleProvider.h" 22#include "llvm/CodeGen/MachineCodeEmitter.h" 23#include "llvm/CodeGen/MachineFunction.h" 24#include "llvm/ExecutionEngine/GenericValue.h" 25#include "llvm/Support/MutexGuard.h" 26#include "llvm/System/DynamicLibrary.h" 27#include "llvm/Target/TargetData.h" 28#include "llvm/Target/TargetMachine.h" 29#include "llvm/Target/TargetJITInfo.h" 30using namespace llvm; 31 32#ifdef __APPLE__ 33#include <AvailabilityMacros.h> 34#if defined(MAC_OS_X_VERSION_10_4) && \ 35 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \ 36 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \ 37 __APPLE_CC__ >= 5330)) 38// __dso_handle is resolved by Mac OS X dynamic linker. 39extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); 40#endif 41#endif 42 43static struct RegisterJIT { 44 RegisterJIT() { JIT::Register(); } 45} JITRegistrator; 46 47namespace llvm { 48 void LinkInJIT() { 49 } 50} 51 52JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) 53 : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) { 54 setTargetData(TM.getTargetData()); 55 56 // Initialize MCE 57 MCE = createEmitter(*this); 58 59 // Add target data 60 MutexGuard locked(lock); 61 FunctionPassManager &PM = state.getPM(locked); 62 PM.add(new TargetData(*TM.getTargetData())); 63 64 // Turn the machine code intermediate representation into bytes in memory that 65 // may be executed. 66 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) { 67 cerr << "Target does not support machine code emission!\n"; 68 abort(); 69 } 70 71 // Initialize passes. 72 PM.doInitialization(); 73} 74 75JIT::~JIT() { 76 delete MCE; 77 delete &TM; 78} 79 80/// run - Start execution with the specified function and arguments. 81/// 82GenericValue JIT::runFunction(Function *F, 83 const std::vector<GenericValue> &ArgValues) { 84 assert(F && "Function *F was null at entry to run()"); 85 86 void *FPtr = getPointerToFunction(F); 87 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); 88 const FunctionType *FTy = F->getFunctionType(); 89 const Type *RetTy = FTy->getReturnType(); 90 91 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) && 92 "Too many arguments passed into function!"); 93 assert(FTy->getNumParams() == ArgValues.size() && 94 "This doesn't support passing arguments through varargs (yet)!"); 95 96 // Handle some common cases first. These cases correspond to common `main' 97 // prototypes. 98 if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) { 99 switch (ArgValues.size()) { 100 case 3: 101 if ((FTy->getParamType(0) == Type::Int32Ty || 102 FTy->getParamType(0) == Type::Int32Ty) && 103 isa<PointerType>(FTy->getParamType(1)) && 104 isa<PointerType>(FTy->getParamType(2))) { 105 int (*PF)(int, char **, const char **) = 106 (int(*)(int, char **, const char **))(intptr_t)FPtr; 107 108 // Call the function. 109 GenericValue rv; 110 rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1]), 111 (const char **)GVTOP(ArgValues[2])); 112 return rv; 113 } 114 break; 115 case 2: 116 if ((FTy->getParamType(0) == Type::Int32Ty || 117 FTy->getParamType(0) == Type::Int32Ty) && 118 isa<PointerType>(FTy->getParamType(1))) { 119 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; 120 121 // Call the function. 122 GenericValue rv; 123 rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1])); 124 return rv; 125 } 126 break; 127 case 1: 128 if (FTy->getNumParams() == 1 && 129 (FTy->getParamType(0) == Type::Int32Ty || 130 FTy->getParamType(0) == Type::Int32Ty)) { 131 GenericValue rv; 132 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; 133 rv.Int32Val = PF(ArgValues[0].Int32Val); 134 return rv; 135 } 136 break; 137 } 138 } 139 140 // Handle cases where no arguments are passed first. 141 if (ArgValues.empty()) { 142 GenericValue rv; 143 switch (RetTy->getTypeID()) { 144 default: assert(0 && "Unknown return type for function call!"); 145 case Type::Int1TyID: 146 rv.Int1Val = ((bool(*)())(intptr_t)FPtr)(); 147 return rv; 148 case Type::Int8TyID: 149 rv.Int8Val = ((char(*)())(intptr_t)FPtr)(); 150 return rv; 151 case Type::Int16TyID: 152 rv.Int16Val = ((short(*)())(intptr_t)FPtr)(); 153 return rv; 154 case Type::VoidTyID: 155 case Type::Int32TyID: 156 rv.Int32Val = ((int(*)())(intptr_t)FPtr)(); 157 return rv; 158 case Type::Int64TyID: 159 rv.Int64Val = ((int64_t(*)())(intptr_t)FPtr)(); 160 return rv; 161 case Type::FloatTyID: 162 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); 163 return rv; 164 case Type::DoubleTyID: 165 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); 166 return rv; 167 case Type::PointerTyID: 168 return PTOGV(((void*(*)())(intptr_t)FPtr)()); 169 } 170 } 171 172 // Okay, this is not one of our quick and easy cases. Because we don't have a 173 // full FFI, we have to codegen a nullary stub function that just calls the 174 // function we are interested in, passing in constants for all of the 175 // arguments. Make this function and return. 176 177 // First, create the function. 178 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false); 179 Function *Stub = new Function(STy, Function::InternalLinkage, "", 180 F->getParent()); 181 182 // Insert a basic block. 183 BasicBlock *StubBB = new BasicBlock("", Stub); 184 185 // Convert all of the GenericValue arguments over to constants. Note that we 186 // currently don't support varargs. 187 std::vector<Value*> Args; 188 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { 189 Constant *C = 0; 190 const Type *ArgTy = FTy->getParamType(i); 191 const GenericValue &AV = ArgValues[i]; 192 switch (ArgTy->getTypeID()) { 193 default: assert(0 && "Unknown argument type for function call!"); 194 case Type::Int1TyID: C = ConstantInt::get(AV.Int1Val); break; 195 case Type::Int8TyID: C = ConstantInt::get(ArgTy, AV.Int8Val); break; 196 case Type::Int16TyID: C = ConstantInt::get(ArgTy, AV.Int16Val); break; 197 case Type::Int32TyID: C = ConstantInt::get(ArgTy, AV.Int32Val); break; 198 case Type::Int64TyID: C = ConstantInt::get(ArgTy, AV.Int64Val); break; 199 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break; 200 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break; 201 case Type::PointerTyID: 202 void *ArgPtr = GVTOP(AV); 203 if (sizeof(void*) == 4) { 204 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr); 205 } else { 206 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr); 207 } 208 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer 209 break; 210 } 211 Args.push_back(C); 212 } 213 214 CallInst *TheCall = new CallInst(F, Args, "", StubBB); 215 TheCall->setTailCall(); 216 if (TheCall->getType() != Type::VoidTy) 217 new ReturnInst(TheCall, StubBB); // Return result of the call. 218 else 219 new ReturnInst(StubBB); // Just return void. 220 221 // Finally, return the value returned by our nullary stub function. 222 return runFunction(Stub, std::vector<GenericValue>()); 223} 224 225/// runJITOnFunction - Run the FunctionPassManager full of 226/// just-in-time compilation passes on F, hopefully filling in 227/// GlobalAddress[F] with the address of F's machine code. 228/// 229void JIT::runJITOnFunction(Function *F) { 230 static bool isAlreadyCodeGenerating = false; 231 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); 232 233 MutexGuard locked(lock); 234 235 // JIT the function 236 isAlreadyCodeGenerating = true; 237 state.getPM(locked).run(*F); 238 isAlreadyCodeGenerating = false; 239 240 // If the function referred to a global variable that had not yet been 241 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit 242 // all of these globals now. 243 while (!state.getPendingGlobals(locked).empty()) { 244 const GlobalVariable *GV = state.getPendingGlobals(locked).back(); 245 state.getPendingGlobals(locked).pop_back(); 246 EmitGlobalVariable(GV); 247 } 248} 249 250/// getPointerToFunction - This method is used to get the address of the 251/// specified function, compiling it if neccesary. 252/// 253void *JIT::getPointerToFunction(Function *F) { 254 MutexGuard locked(lock); 255 256 if (void *Addr = getPointerToGlobalIfAvailable(F)) 257 return Addr; // Check if function already code gen'd 258 259 // Make sure we read in the function if it exists in this Module. 260 if (F->hasNotBeenReadFromBytecode()) { 261 // Determine the module provider this function is provided by. 262 Module *M = F->getParent(); 263 ModuleProvider *MP = 0; 264 for (unsigned i = 0, e = Modules.size(); i != e; ++i) { 265 if (Modules[i]->getModule() == M) { 266 MP = Modules[i]; 267 break; 268 } 269 } 270 assert(MP && "Function isn't in a module we know about!"); 271 272 std::string ErrorMsg; 273 if (MP->materializeFunction(F, &ErrorMsg)) { 274 cerr << "Error reading function '" << F->getName() 275 << "' from bytecode file: " << ErrorMsg << "\n"; 276 abort(); 277 } 278 } 279 280 if (F->isExternal()) { 281 void *Addr = getPointerToNamedFunction(F->getName()); 282 addGlobalMapping(F, Addr); 283 return Addr; 284 } 285 286 runJITOnFunction(F); 287 288 void *Addr = getPointerToGlobalIfAvailable(F); 289 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); 290 return Addr; 291} 292 293/// getOrEmitGlobalVariable - Return the address of the specified global 294/// variable, possibly emitting it to memory if needed. This is used by the 295/// Emitter. 296void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { 297 MutexGuard locked(lock); 298 299 void *Ptr = getPointerToGlobalIfAvailable(GV); 300 if (Ptr) return Ptr; 301 302 // If the global is external, just remember the address. 303 if (GV->isExternal()) { 304#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_4) && \ 305 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \ 306 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \ 307 __APPLE_CC__ >= 5330)) 308 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead 309 // of atexit). It passes the address of linker generated symbol __dso_handle 310 // to the function. 311 // This configuration change happened at version 5330. 312 if (GV->getName() == "__dso_handle") 313 return (void*)&__dso_handle; 314#endif 315 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str()); 316 if (Ptr == 0) { 317 cerr << "Could not resolve external global address: " 318 << GV->getName() << "\n"; 319 abort(); 320 } 321 } else { 322 // If the global hasn't been emitted to memory yet, allocate space. We will 323 // actually initialize the global after current function has finished 324 // compilation. 325 const Type *GlobalType = GV->getType()->getElementType(); 326 size_t S = getTargetData()->getTypeSize(GlobalType); 327 size_t A = getTargetData()->getTypeAlignment(GlobalType); 328 if (A <= 8) { 329 Ptr = malloc(S); 330 } else { 331 // Allocate S+A bytes of memory, then use an aligned pointer within that 332 // space. 333 Ptr = malloc(S+A); 334 unsigned MisAligned = ((intptr_t)Ptr & (A-1)); 335 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0); 336 } 337 state.getPendingGlobals(locked).push_back(GV); 338 } 339 addGlobalMapping(GV, Ptr); 340 return Ptr; 341} 342 343 344/// recompileAndRelinkFunction - This method is used to force a function 345/// which has already been compiled, to be compiled again, possibly 346/// after it has been modified. Then the entry to the old copy is overwritten 347/// with a branch to the new copy. If there was no old copy, this acts 348/// just like JIT::getPointerToFunction(). 349/// 350void *JIT::recompileAndRelinkFunction(Function *F) { 351 void *OldAddr = getPointerToGlobalIfAvailable(F); 352 353 // If it's not already compiled there is no reason to patch it up. 354 if (OldAddr == 0) { return getPointerToFunction(F); } 355 356 // Delete the old function mapping. 357 addGlobalMapping(F, 0); 358 359 // Recodegen the function 360 runJITOnFunction(F); 361 362 // Update state, forward the old function to the new function. 363 void *Addr = getPointerToGlobalIfAvailable(F); 364 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); 365 TJI.replaceMachineCodeForFunction(OldAddr, Addr); 366 return Addr; 367} 368 369