1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "MCJIT.h" 11 #include "llvm/ExecutionEngine/GenericValue.h" 12 #include "llvm/ExecutionEngine/JITEventListener.h" 13 #include "llvm/ExecutionEngine/JITMemoryManager.h" 14 #include "llvm/ExecutionEngine/MCJIT.h" 15 #include "llvm/ExecutionEngine/ObjectBuffer.h" 16 #include "llvm/ExecutionEngine/ObjectImage.h" 17 #include "llvm/PassManager.h" 18 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/Function.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/Support/DynamicLibrary.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 #include "llvm/Support/MutexGuard.h" 28 29 using namespace llvm; 30 31 namespace { 32 33 static struct RegisterJIT { 34 RegisterJIT() { MCJIT::Register(); } 35 } JITRegistrator; 36 37 } 38 39 extern "C" void LLVMLinkInMCJIT() { 40 } 41 42 ExecutionEngine *MCJIT::createJIT(Module *M, 43 std::string *ErrorStr, 44 RTDyldMemoryManager *MemMgr, 45 bool GVsWithCode, 46 TargetMachine *TM) { 47 // Try to register the program as a source of symbols to resolve against. 48 // 49 // FIXME: Don't do this here. 50 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL); 51 52 return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(), 53 GVsWithCode); 54 } 55 56 MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM, 57 bool AllocateGVsWithCode) 58 : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr), 59 ObjCache(0) { 60 61 OwnedModules.addModule(m); 62 setDataLayout(TM->getDataLayout()); 63 } 64 65 MCJIT::~MCJIT() { 66 MutexGuard locked(lock); 67 // FIXME: We are managing our modules, so we do not want the base class 68 // ExecutionEngine to manage them as well. To avoid double destruction 69 // of the first (and only) module added in ExecutionEngine constructor 70 // we remove it from EE and will destruct it ourselves. 71 // 72 // It may make sense to move our module manager (based on SmallStPtr) back 73 // into EE if the JIT and Interpreter can live with it. 74 // If so, additional functions: addModule, removeModule, FindFunctionNamed, 75 // runStaticConstructorsDestructors could be moved back to EE as well. 76 // 77 Modules.clear(); 78 Dyld.deregisterEHFrames(); 79 80 LoadedObjectMap::iterator it, end = LoadedObjects.end(); 81 for (it = LoadedObjects.begin(); it != end; ++it) { 82 ObjectImage *Obj = it->second; 83 if (Obj) { 84 NotifyFreeingObject(*Obj); 85 delete Obj; 86 } 87 } 88 LoadedObjects.clear(); 89 delete TM; 90 } 91 92 void MCJIT::addModule(Module *M) { 93 MutexGuard locked(lock); 94 OwnedModules.addModule(M); 95 } 96 97 bool MCJIT::removeModule(Module *M) { 98 MutexGuard locked(lock); 99 return OwnedModules.removeModule(M); 100 } 101 102 103 104 void MCJIT::setObjectCache(ObjectCache* NewCache) { 105 MutexGuard locked(lock); 106 ObjCache = NewCache; 107 } 108 109 ObjectBufferStream* MCJIT::emitObject(Module *M) { 110 MutexGuard locked(lock); 111 112 // This must be a module which has already been added but not loaded to this 113 // MCJIT instance, since these conditions are tested by our caller, 114 // generateCodeForModule. 115 116 PassManager PM; 117 118 PM.add(new DataLayout(*TM->getDataLayout())); 119 120 // The RuntimeDyld will take ownership of this shortly 121 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream()); 122 123 // Turn the machine code intermediate representation into bytes in memory 124 // that may be executed. 125 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) { 126 report_fatal_error("Target does not support MC emission!"); 127 } 128 129 // Initialize passes. 130 PM.run(*M); 131 // Flush the output buffer to get the generated code into memory 132 CompiledObject->flush(); 133 134 // If we have an object cache, tell it about the new object. 135 // Note that we're using the compiled image, not the loaded image (as below). 136 if (ObjCache) { 137 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK 138 // to create a temporary object here and delete it after the call. 139 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer()); 140 ObjCache->notifyObjectCompiled(M, MB.get()); 141 } 142 143 return CompiledObject.take(); 144 } 145 146 void MCJIT::generateCodeForModule(Module *M) { 147 // Get a thread lock to make sure we aren't trying to load multiple times 148 MutexGuard locked(lock); 149 150 // This must be a module which has already been added to this MCJIT instance. 151 assert(OwnedModules.ownsModule(M) && 152 "MCJIT::generateCodeForModule: Unknown module."); 153 154 // Re-compilation is not supported 155 if (OwnedModules.hasModuleBeenLoaded(M)) 156 return; 157 158 OwningPtr<ObjectBuffer> ObjectToLoad; 159 // Try to load the pre-compiled object from cache if possible 160 if (0 != ObjCache) { 161 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M)); 162 if (0 != PreCompiledObject.get()) 163 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take())); 164 } 165 166 // If the cache did not contain a suitable object, compile the object 167 if (!ObjectToLoad) { 168 ObjectToLoad.reset(emitObject(M)); 169 assert(ObjectToLoad.get() && "Compilation did not produce an object."); 170 } 171 172 // Load the object into the dynamic linker. 173 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map). 174 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take()); 175 LoadedObjects[M] = LoadedObject; 176 if (!LoadedObject) 177 report_fatal_error(Dyld.getErrorString()); 178 179 // FIXME: Make this optional, maybe even move it to a JIT event listener 180 LoadedObject->registerWithDebugger(); 181 182 NotifyObjectEmitted(*LoadedObject); 183 184 OwnedModules.markModuleAsLoaded(M); 185 } 186 187 void MCJIT::finalizeLoadedModules() { 188 MutexGuard locked(lock); 189 190 // Resolve any outstanding relocations. 191 Dyld.resolveRelocations(); 192 193 OwnedModules.markAllLoadedModulesAsFinalized(); 194 195 // Register EH frame data for any module we own which has been loaded 196 Dyld.registerEHFrames(); 197 198 // Set page permissions. 199 MemMgr.finalizeMemory(); 200 } 201 202 // FIXME: Rename this. 203 void MCJIT::finalizeObject() { 204 MutexGuard locked(lock); 205 206 for (ModulePtrSet::iterator I = OwnedModules.begin_added(), 207 E = OwnedModules.end_added(); 208 I != E; ++I) { 209 Module *M = *I; 210 generateCodeForModule(M); 211 } 212 213 finalizeLoadedModules(); 214 } 215 216 void MCJIT::finalizeModule(Module *M) { 217 MutexGuard locked(lock); 218 219 // This must be a module which has already been added to this MCJIT instance. 220 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); 221 222 // If the module hasn't been compiled, just do that. 223 if (!OwnedModules.hasModuleBeenLoaded(M)) 224 generateCodeForModule(M); 225 226 finalizeLoadedModules(); 227 } 228 229 void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { 230 report_fatal_error("not yet implemented"); 231 } 232 233 uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) { 234 // Check with the RuntimeDyld to see if we already have this symbol. 235 if (Name[0] == '\1') 236 return Dyld.getSymbolLoadAddress(Name.substr(1)); 237 return Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix() 238 + Name)); 239 } 240 241 Module *MCJIT::findModuleForSymbol(const std::string &Name, 242 bool CheckFunctionsOnly) { 243 MutexGuard locked(lock); 244 245 // If it hasn't already been generated, see if it's in one of our modules. 246 for (ModulePtrSet::iterator I = OwnedModules.begin_added(), 247 E = OwnedModules.end_added(); 248 I != E; ++I) { 249 Module *M = *I; 250 Function *F = M->getFunction(Name); 251 if (F && !F->isDeclaration()) 252 return M; 253 if (!CheckFunctionsOnly) { 254 GlobalVariable *G = M->getGlobalVariable(Name); 255 if (G && !G->isDeclaration()) 256 return M; 257 // FIXME: Do we need to worry about global aliases? 258 } 259 } 260 // We didn't find the symbol in any of our modules. 261 return NULL; 262 } 263 264 uint64_t MCJIT::getSymbolAddress(const std::string &Name, 265 bool CheckFunctionsOnly) 266 { 267 MutexGuard locked(lock); 268 269 // First, check to see if we already have this symbol. 270 uint64_t Addr = getExistingSymbolAddress(Name); 271 if (Addr) 272 return Addr; 273 274 // If it hasn't already been generated, see if it's in one of our modules. 275 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly); 276 if (!M) 277 return 0; 278 279 generateCodeForModule(M); 280 281 // Check the RuntimeDyld table again, it should be there now. 282 return getExistingSymbolAddress(Name); 283 } 284 285 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { 286 MutexGuard locked(lock); 287 uint64_t Result = getSymbolAddress(Name, false); 288 if (Result != 0) 289 finalizeLoadedModules(); 290 return Result; 291 } 292 293 uint64_t MCJIT::getFunctionAddress(const std::string &Name) { 294 MutexGuard locked(lock); 295 uint64_t Result = getSymbolAddress(Name, true); 296 if (Result != 0) 297 finalizeLoadedModules(); 298 return Result; 299 } 300 301 // Deprecated. Use getFunctionAddress instead. 302 void *MCJIT::getPointerToFunction(Function *F) { 303 MutexGuard locked(lock); 304 305 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { 306 bool AbortOnFailure = !F->hasExternalWeakLinkage(); 307 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); 308 addGlobalMapping(F, Addr); 309 return Addr; 310 } 311 312 Module *M = F->getParent(); 313 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M); 314 315 // Make sure the relevant module has been compiled and loaded. 316 if (HasBeenAddedButNotLoaded) 317 generateCodeForModule(M); 318 else if (!OwnedModules.hasModuleBeenLoaded(M)) 319 // If this function doesn't belong to one of our modules, we're done. 320 return NULL; 321 322 // FIXME: Should the Dyld be retaining module information? Probably not. 323 // FIXME: Should we be using the mangler for this? Probably. 324 // 325 // This is the accessor for the target address, so make sure to check the 326 // load address of the symbol, not the local address. 327 StringRef BaseName = F->getName(); 328 if (BaseName[0] == '\1') 329 return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1)); 330 return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix() 331 + BaseName).str()); 332 } 333 334 void *MCJIT::recompileAndRelinkFunction(Function *F) { 335 report_fatal_error("not yet implemented"); 336 } 337 338 void MCJIT::freeMachineCodeForFunction(Function *F) { 339 report_fatal_error("not yet implemented"); 340 } 341 342 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet( 343 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) { 344 for (; I != E; ++I) { 345 ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors); 346 } 347 } 348 349 void MCJIT::runStaticConstructorsDestructors(bool isDtors) { 350 // Execute global ctors/dtors for each module in the program. 351 runStaticConstructorsDestructorsInModulePtrSet( 352 isDtors, OwnedModules.begin_added(), OwnedModules.end_added()); 353 runStaticConstructorsDestructorsInModulePtrSet( 354 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded()); 355 runStaticConstructorsDestructorsInModulePtrSet( 356 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized()); 357 } 358 359 Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName, 360 ModulePtrSet::iterator I, 361 ModulePtrSet::iterator E) { 362 for (; I != E; ++I) { 363 if (Function *F = (*I)->getFunction(FnName)) 364 return F; 365 } 366 return 0; 367 } 368 369 Function *MCJIT::FindFunctionNamed(const char *FnName) { 370 Function *F = FindFunctionNamedInModulePtrSet( 371 FnName, OwnedModules.begin_added(), OwnedModules.end_added()); 372 if (!F) 373 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(), 374 OwnedModules.end_loaded()); 375 if (!F) 376 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(), 377 OwnedModules.end_finalized()); 378 return F; 379 } 380 381 GenericValue MCJIT::runFunction(Function *F, 382 const std::vector<GenericValue> &ArgValues) { 383 assert(F && "Function *F was null at entry to run()"); 384 385 void *FPtr = getPointerToFunction(F); 386 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); 387 FunctionType *FTy = F->getFunctionType(); 388 Type *RetTy = FTy->getReturnType(); 389 390 assert((FTy->getNumParams() == ArgValues.size() || 391 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && 392 "Wrong number of arguments passed into function!"); 393 assert(FTy->getNumParams() == ArgValues.size() && 394 "This doesn't support passing arguments through varargs (yet)!"); 395 396 // Handle some common cases first. These cases correspond to common `main' 397 // prototypes. 398 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { 399 switch (ArgValues.size()) { 400 case 3: 401 if (FTy->getParamType(0)->isIntegerTy(32) && 402 FTy->getParamType(1)->isPointerTy() && 403 FTy->getParamType(2)->isPointerTy()) { 404 int (*PF)(int, char **, const char **) = 405 (int(*)(int, char **, const char **))(intptr_t)FPtr; 406 407 // Call the function. 408 GenericValue rv; 409 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 410 (char **)GVTOP(ArgValues[1]), 411 (const char **)GVTOP(ArgValues[2]))); 412 return rv; 413 } 414 break; 415 case 2: 416 if (FTy->getParamType(0)->isIntegerTy(32) && 417 FTy->getParamType(1)->isPointerTy()) { 418 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; 419 420 // Call the function. 421 GenericValue rv; 422 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 423 (char **)GVTOP(ArgValues[1]))); 424 return rv; 425 } 426 break; 427 case 1: 428 if (FTy->getNumParams() == 1 && 429 FTy->getParamType(0)->isIntegerTy(32)) { 430 GenericValue rv; 431 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; 432 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); 433 return rv; 434 } 435 break; 436 } 437 } 438 439 // Handle cases where no arguments are passed first. 440 if (ArgValues.empty()) { 441 GenericValue rv; 442 switch (RetTy->getTypeID()) { 443 default: llvm_unreachable("Unknown return type for function call!"); 444 case Type::IntegerTyID: { 445 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); 446 if (BitWidth == 1) 447 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); 448 else if (BitWidth <= 8) 449 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); 450 else if (BitWidth <= 16) 451 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); 452 else if (BitWidth <= 32) 453 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); 454 else if (BitWidth <= 64) 455 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); 456 else 457 llvm_unreachable("Integer types > 64 bits not supported"); 458 return rv; 459 } 460 case Type::VoidTyID: 461 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); 462 return rv; 463 case Type::FloatTyID: 464 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); 465 return rv; 466 case Type::DoubleTyID: 467 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); 468 return rv; 469 case Type::X86_FP80TyID: 470 case Type::FP128TyID: 471 case Type::PPC_FP128TyID: 472 llvm_unreachable("long double not supported yet"); 473 case Type::PointerTyID: 474 return PTOGV(((void*(*)())(intptr_t)FPtr)()); 475 } 476 } 477 478 llvm_unreachable("Full-featured argument passing not supported yet!"); 479 } 480 481 void *MCJIT::getPointerToNamedFunction(const std::string &Name, 482 bool AbortOnFailure) { 483 if (!isSymbolSearchingDisabled()) { 484 void *ptr = MemMgr.getPointerToNamedFunction(Name, false); 485 if (ptr) 486 return ptr; 487 } 488 489 /// If a LazyFunctionCreator is installed, use it to get/create the function. 490 if (LazyFunctionCreator) 491 if (void *RP = LazyFunctionCreator(Name)) 492 return RP; 493 494 if (AbortOnFailure) { 495 report_fatal_error("Program used external function '"+Name+ 496 "' which could not be resolved!"); 497 } 498 return 0; 499 } 500 501 void MCJIT::RegisterJITEventListener(JITEventListener *L) { 502 if (L == NULL) 503 return; 504 MutexGuard locked(lock); 505 EventListeners.push_back(L); 506 } 507 void MCJIT::UnregisterJITEventListener(JITEventListener *L) { 508 if (L == NULL) 509 return; 510 MutexGuard locked(lock); 511 SmallVector<JITEventListener*, 2>::reverse_iterator I= 512 std::find(EventListeners.rbegin(), EventListeners.rend(), L); 513 if (I != EventListeners.rend()) { 514 std::swap(*I, EventListeners.back()); 515 EventListeners.pop_back(); 516 } 517 } 518 void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { 519 MutexGuard locked(lock); 520 MemMgr.notifyObjectLoaded(this, &Obj); 521 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { 522 EventListeners[I]->NotifyObjectEmitted(Obj); 523 } 524 } 525 void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { 526 MutexGuard locked(lock); 527 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { 528 EventListeners[I]->NotifyFreeingObject(Obj); 529 } 530 } 531 532 uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) { 533 uint64_t Result = ParentEngine->getSymbolAddress(Name, false); 534 // If the symbols wasn't found and it begins with an underscore, try again 535 // without the underscore. 536 if (!Result && Name[0] == '_') 537 Result = ParentEngine->getSymbolAddress(Name.substr(1), false); 538 if (Result) 539 return Result; 540 return ClientMM->getSymbolAddress(Name); 541 } 542