LLVM API Documentation
00001 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This tool implements a just-in-time compiler for LLVM, allowing direct 00011 // execution of LLVM bytecode in an efficient manner. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "JIT.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/Function.h" 00019 #include "llvm/GlobalVariable.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/ModuleProvider.h" 00022 #include "llvm/CodeGen/MachineCodeEmitter.h" 00023 #include "llvm/CodeGen/MachineFunction.h" 00024 #include "llvm/ExecutionEngine/GenericValue.h" 00025 #include "llvm/Support/MutexGuard.h" 00026 #include "llvm/System/DynamicLibrary.h" 00027 #include "llvm/Target/TargetData.h" 00028 #include "llvm/Target/TargetMachine.h" 00029 #include "llvm/Target/TargetJITInfo.h" 00030 #include <iostream> 00031 using namespace llvm; 00032 00033 #ifdef __APPLE__ 00034 #include <AvailabilityMacros.h> 00035 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 00036 // __dso_handle is resolved by Mac OS X dynamic linker. 00037 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); 00038 #endif 00039 #endif 00040 00041 static struct RegisterJIT { 00042 RegisterJIT() { JIT::Register(); } 00043 } JITRegistrator; 00044 00045 namespace llvm { 00046 void LinkInJIT() { 00047 } 00048 } 00049 00050 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) 00051 : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) { 00052 setTargetData(TM.getTargetData()); 00053 00054 // Initialize MCE 00055 MCE = createEmitter(*this); 00056 00057 // Add target data 00058 MutexGuard locked(lock); 00059 FunctionPassManager& PM = state.getPM(locked); 00060 PM.add(new TargetData(*TM.getTargetData())); 00061 00062 // Compile LLVM Code down to machine code in the intermediate representation 00063 TJI.addPassesToJITCompile(PM); 00064 00065 // Turn the machine code intermediate representation into bytes in memory that 00066 // may be executed. 00067 if (TM.addPassesToEmitMachineCode(PM, *MCE)) { 00068 std::cerr << "Target '" << TM.getName() 00069 << "' doesn't support machine code emission!\n"; 00070 abort(); 00071 } 00072 } 00073 00074 JIT::~JIT() { 00075 delete MCE; 00076 delete &TM; 00077 } 00078 00079 /// run - Start execution with the specified function and arguments. 00080 /// 00081 GenericValue JIT::runFunction(Function *F, 00082 const std::vector<GenericValue> &ArgValues) { 00083 assert(F && "Function *F was null at entry to run()"); 00084 00085 void *FPtr = getPointerToFunction(F); 00086 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); 00087 const FunctionType *FTy = F->getFunctionType(); 00088 const Type *RetTy = FTy->getReturnType(); 00089 00090 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) && 00091 "Too many arguments passed into function!"); 00092 assert(FTy->getNumParams() == ArgValues.size() && 00093 "This doesn't support passing arguments through varargs (yet)!"); 00094 00095 // Handle some common cases first. These cases correspond to common `main' 00096 // prototypes. 00097 if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) { 00098 switch (ArgValues.size()) { 00099 case 3: 00100 if ((FTy->getParamType(0) == Type::IntTy || 00101 FTy->getParamType(0) == Type::UIntTy) && 00102 isa<PointerType>(FTy->getParamType(1)) && 00103 isa<PointerType>(FTy->getParamType(2))) { 00104 int (*PF)(int, char **, const char **) = 00105 (int(*)(int, char **, const char **))(intptr_t)FPtr; 00106 00107 // Call the function. 00108 GenericValue rv; 00109 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]), 00110 (const char **)GVTOP(ArgValues[2])); 00111 return rv; 00112 } 00113 break; 00114 case 2: 00115 if ((FTy->getParamType(0) == Type::IntTy || 00116 FTy->getParamType(0) == Type::UIntTy) && 00117 isa<PointerType>(FTy->getParamType(1))) { 00118 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; 00119 00120 // Call the function. 00121 GenericValue rv; 00122 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1])); 00123 return rv; 00124 } 00125 break; 00126 case 1: 00127 if (FTy->getNumParams() == 1 && 00128 (FTy->getParamType(0) == Type::IntTy || 00129 FTy->getParamType(0) == Type::UIntTy)) { 00130 GenericValue rv; 00131 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; 00132 rv.IntVal = PF(ArgValues[0].IntVal); 00133 return rv; 00134 } 00135 break; 00136 } 00137 } 00138 00139 // Handle cases where no arguments are passed first. 00140 if (ArgValues.empty()) { 00141 GenericValue rv; 00142 switch (RetTy->getTypeID()) { 00143 default: assert(0 && "Unknown return type for function call!"); 00144 case Type::BoolTyID: 00145 rv.BoolVal = ((bool(*)())(intptr_t)FPtr)(); 00146 return rv; 00147 case Type::SByteTyID: 00148 case Type::UByteTyID: 00149 rv.SByteVal = ((char(*)())(intptr_t)FPtr)(); 00150 return rv; 00151 case Type::ShortTyID: 00152 case Type::UShortTyID: 00153 rv.ShortVal = ((short(*)())(intptr_t)FPtr)(); 00154 return rv; 00155 case Type::VoidTyID: 00156 case Type::IntTyID: 00157 case Type::UIntTyID: 00158 rv.IntVal = ((int(*)())(intptr_t)FPtr)(); 00159 return rv; 00160 case Type::LongTyID: 00161 case Type::ULongTyID: 00162 rv.LongVal = ((int64_t(*)())(intptr_t)FPtr)(); 00163 return rv; 00164 case Type::FloatTyID: 00165 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); 00166 return rv; 00167 case Type::DoubleTyID: 00168 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); 00169 return rv; 00170 case Type::PointerTyID: 00171 return PTOGV(((void*(*)())(intptr_t)FPtr)()); 00172 } 00173 } 00174 00175 // Okay, this is not one of our quick and easy cases. Because we don't have a 00176 // full FFI, we have to codegen a nullary stub function that just calls the 00177 // function we are interested in, passing in constants for all of the 00178 // arguments. Make this function and return. 00179 00180 // First, create the function. 00181 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false); 00182 Function *Stub = new Function(STy, Function::InternalLinkage, "", 00183 F->getParent()); 00184 00185 // Insert a basic block. 00186 BasicBlock *StubBB = new BasicBlock("", Stub); 00187 00188 // Convert all of the GenericValue arguments over to constants. Note that we 00189 // currently don't support varargs. 00190 std::vector<Value*> Args; 00191 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { 00192 Constant *C = 0; 00193 const Type *ArgTy = FTy->getParamType(i); 00194 const GenericValue &AV = ArgValues[i]; 00195 switch (ArgTy->getTypeID()) { 00196 default: assert(0 && "Unknown argument type for function call!"); 00197 case Type::BoolTyID: C = ConstantBool::get(AV.BoolVal); break; 00198 case Type::SByteTyID: C = ConstantSInt::get(ArgTy, AV.SByteVal); break; 00199 case Type::UByteTyID: C = ConstantUInt::get(ArgTy, AV.UByteVal); break; 00200 case Type::ShortTyID: C = ConstantSInt::get(ArgTy, AV.ShortVal); break; 00201 case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break; 00202 case Type::IntTyID: C = ConstantSInt::get(ArgTy, AV.IntVal); break; 00203 case Type::UIntTyID: C = ConstantUInt::get(ArgTy, AV.UIntVal); break; 00204 case Type::LongTyID: C = ConstantSInt::get(ArgTy, AV.LongVal); break; 00205 case Type::ULongTyID: C = ConstantUInt::get(ArgTy, AV.ULongVal); break; 00206 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break; 00207 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break; 00208 case Type::PointerTyID: 00209 void *ArgPtr = GVTOP(AV); 00210 if (sizeof(void*) == 4) { 00211 C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr); 00212 } else { 00213 C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr); 00214 } 00215 C = ConstantExpr::getCast(C, ArgTy); // Cast the integer to pointer 00216 break; 00217 } 00218 Args.push_back(C); 00219 } 00220 00221 CallInst *TheCall = new CallInst(F, Args, "", StubBB); 00222 TheCall->setTailCall(); 00223 if (TheCall->getType() != Type::VoidTy) 00224 new ReturnInst(TheCall, StubBB); // Return result of the call. 00225 else 00226 new ReturnInst(StubBB); // Just return void. 00227 00228 // Finally, return the value returned by our nullary stub function. 00229 return runFunction(Stub, std::vector<GenericValue>()); 00230 } 00231 00232 /// runJITOnFunction - Run the FunctionPassManager full of 00233 /// just-in-time compilation passes on F, hopefully filling in 00234 /// GlobalAddress[F] with the address of F's machine code. 00235 /// 00236 void JIT::runJITOnFunction(Function *F) { 00237 static bool isAlreadyCodeGenerating = false; 00238 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); 00239 00240 MutexGuard locked(lock); 00241 00242 // JIT the function 00243 isAlreadyCodeGenerating = true; 00244 state.getPM(locked).run(*F); 00245 isAlreadyCodeGenerating = false; 00246 00247 // If the function referred to a global variable that had not yet been 00248 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit 00249 // all of these globals now. 00250 while (!state.getPendingGlobals(locked).empty()) { 00251 const GlobalVariable *GV = state.getPendingGlobals(locked).back(); 00252 state.getPendingGlobals(locked).pop_back(); 00253 EmitGlobalVariable(GV); 00254 } 00255 } 00256 00257 /// getPointerToFunction - This method is used to get the address of the 00258 /// specified function, compiling it if neccesary. 00259 /// 00260 void *JIT::getPointerToFunction(Function *F) { 00261 MutexGuard locked(lock); 00262 00263 if (void *Addr = getPointerToGlobalIfAvailable(F)) 00264 return Addr; // Check if function already code gen'd 00265 00266 // Make sure we read in the function if it exists in this Module 00267 if (F->hasNotBeenReadFromBytecode()) { 00268 std::string ErrorMsg; 00269 if (MP->materializeFunction(F, &ErrorMsg)) { 00270 std::cerr << "Error reading function '" << F->getName() 00271 << "' from bytecode file: " << ErrorMsg << "\n"; 00272 abort(); 00273 } 00274 } 00275 00276 if (F->isExternal()) { 00277 void *Addr = getPointerToNamedFunction(F->getName()); 00278 addGlobalMapping(F, Addr); 00279 return Addr; 00280 } 00281 00282 runJITOnFunction(F); 00283 00284 void *Addr = getPointerToGlobalIfAvailable(F); 00285 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); 00286 return Addr; 00287 } 00288 00289 /// getOrEmitGlobalVariable - Return the address of the specified global 00290 /// variable, possibly emitting it to memory if needed. This is used by the 00291 /// Emitter. 00292 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { 00293 MutexGuard locked(lock); 00294 00295 void *Ptr = getPointerToGlobalIfAvailable(GV); 00296 if (Ptr) return Ptr; 00297 00298 // If the global is external, just remember the address. 00299 if (GV->isExternal()) { 00300 #ifdef __APPLE__ 00301 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 00302 // __dso_handle is resolved by the Mac OS X dynamic linker. 00303 if (GV->getName() == "__dso_handle") 00304 return (void*)&__dso_handle; 00305 #endif 00306 #endif 00307 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str()); 00308 if (Ptr == 0) { 00309 std::cerr << "Could not resolve external global address: " 00310 << GV->getName() << "\n"; 00311 abort(); 00312 } 00313 } else { 00314 // If the global hasn't been emitted to memory yet, allocate space. We will 00315 // actually initialize the global after current function has finished 00316 // compilation. 00317 const Type *GlobalType = GV->getType()->getElementType(); 00318 size_t S = getTargetData()->getTypeSize(GlobalType); 00319 size_t A = getTargetData()->getTypeAlignment(GlobalType); 00320 if (A <= 8) { 00321 Ptr = malloc(S); 00322 } else { 00323 // Allocate S+A bytes of memory, then use an aligned pointer within that 00324 // space. 00325 Ptr = malloc(S+A); 00326 unsigned MisAligned = ((intptr_t)Ptr & (A-1)); 00327 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0); 00328 } 00329 state.getPendingGlobals(locked).push_back(GV); 00330 } 00331 addGlobalMapping(GV, Ptr); 00332 return Ptr; 00333 } 00334 00335 00336 /// recompileAndRelinkFunction - This method is used to force a function 00337 /// which has already been compiled, to be compiled again, possibly 00338 /// after it has been modified. Then the entry to the old copy is overwritten 00339 /// with a branch to the new copy. If there was no old copy, this acts 00340 /// just like JIT::getPointerToFunction(). 00341 /// 00342 void *JIT::recompileAndRelinkFunction(Function *F) { 00343 void *OldAddr = getPointerToGlobalIfAvailable(F); 00344 00345 // If it's not already compiled there is no reason to patch it up. 00346 if (OldAddr == 0) { return getPointerToFunction(F); } 00347 00348 // Delete the old function mapping. 00349 addGlobalMapping(F, 0); 00350 00351 // Recodegen the function 00352 runJITOnFunction(F); 00353 00354 // Update state, forward the old function to the new function. 00355 void *Addr = getPointerToGlobalIfAvailable(F); 00356 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); 00357 TJI.replaceMachineCodeForFunction(OldAddr, Addr); 00358 return Addr; 00359 } 00360