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