2012-09-05 13:37:39 +04:00
//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
2014-01-07 14:08:37 +04:00
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
2012-09-05 13:37:39 +04:00
//------------------------------------------------------------------------------
2014-02-19 15:49:24 +04:00
# include "IncrementalExecutor.h"
2020-10-10 21:34:55 +03:00
# include "BackendPasses.h"
2015-02-03 23:32:09 +03:00
# include "IncrementalJIT.h"
2015-03-14 00:03:50 +03:00
# include "Threading.h"
2012-09-05 13:37:39 +04:00
2020-10-11 00:43:37 +03:00
# include "cling/Interpreter/DynamicLibraryManager.h"
Provide __cxa_atexit replacement through custom MemoryManager.
On some platforms, global destructors are registered through a call to
__cxa_atexit(dtor, 0/*args*/, __dso_handle). While __cxa_atexit can be resolved
by the regular MemoryManager, __dso_handle (representing the "current shared
library" such that the corresponding atexit function can be called on its
dlclose) can not be resolved by MCJIT. Instead, we provide our own, poining to
the ExecutionEngine, which in turn holds a "current module" that corresponds in
spirit to the shared library handle.
__cxa_atexit, on the other hand, needs to be re-wired: the interpreter needs to
destruct globals upon its destruction, and those globals from a certain module
when that module is unloaded.
Both is done through a custom MemoryManager, significantly reducing the
complexity of the previous ("JIT without MC") implementation.
The custom MemoryManager also forwards in case of a unknown symbols to the LazyFunctionCreators instead of using the generic
ExecutionEngine::InstallLazyFunctionCreator() which has no effect with MCJIT.
2015-01-08 15:54:22 +03:00
# include "cling/Interpreter/Transaction.h"
2020-10-11 00:43:37 +03:00
# include "cling/Interpreter/Value.h"
2015-02-12 20:23:04 +03:00
# include "cling/Utils/AST.h"
2016-09-10 22:04:39 +03:00
# include "cling/Utils/Output.h"
2016-12-16 21:33:42 +03:00
# include "cling/Utils/Platform.h"
2014-05-26 18:33:43 +04:00
2014-05-28 15:56:16 +04:00
# include "clang/Basic/Diagnostic.h"
2017-05-18 16:16:30 +03:00
# include "clang/Basic/TargetOptions.h"
2016-11-15 17:12:47 +03:00
# include "clang/Frontend/CompilerInstance.h"
2014-05-28 15:56:16 +04:00
2013-04-24 20:28:08 +04:00
# include "llvm/IR/Constants.h"
2014-03-15 02:11:09 +04:00
# include "llvm/IR/Instructions.h"
2013-11-01 21:31:18 +04:00
# include "llvm/IR/LLVMContext.h"
2013-04-24 20:28:08 +04:00
# include "llvm/IR/Module.h"
2012-11-17 20:28:14 +04:00
# include "llvm/ADT/SmallPtrSet.h"
2015-02-03 23:32:09 +03:00
# include "llvm/ADT/Triple.h"
# include "llvm/Support/Host.h"
# include "llvm/Support/TargetRegistry.h"
# include "llvm/Target/TargetMachine.h"
2012-09-05 13:37:39 +04:00
2019-03-13 10:12:28 +03:00
# include <iostream>
2014-03-07 18:09:02 +04:00
using namespace llvm ;
Provide __cxa_atexit replacement through custom MemoryManager.
On some platforms, global destructors are registered through a call to
__cxa_atexit(dtor, 0/*args*/, __dso_handle). While __cxa_atexit can be resolved
by the regular MemoryManager, __dso_handle (representing the "current shared
library" such that the corresponding atexit function can be called on its
dlclose) can not be resolved by MCJIT. Instead, we provide our own, poining to
the ExecutionEngine, which in turn holds a "current module" that corresponds in
spirit to the shared library handle.
__cxa_atexit, on the other hand, needs to be re-wired: the interpreter needs to
destruct globals upon its destruction, and those globals from a certain module
when that module is unloaded.
Both is done through a custom MemoryManager, significantly reducing the
complexity of the previous ("JIT without MC") implementation.
The custom MemoryManager also forwards in case of a unknown symbols to the LazyFunctionCreators instead of using the generic
ExecutionEngine::InstallLazyFunctionCreator() which has no effect with MCJIT.
2015-01-08 15:54:22 +03:00
2014-03-07 18:09:02 +04:00
namespace cling {
2012-09-05 13:37:39 +04:00
2016-12-20 17:38:19 +03:00
namespace {
2014-10-14 16:11:31 +04:00
2016-12-20 17:38:19 +03:00
static std : : unique_ptr < TargetMachine >
2017-03-15 23:55:16 +03:00
CreateHostTargetMachine ( const clang : : CompilerInstance & CI ) {
2017-03-02 13:37:04 +03:00
const clang : : TargetOptions & TargetOpts = CI . getTargetOpts ( ) ;
const clang : : CodeGenOptions & CGOpt = CI . getCodeGenOpts ( ) ;
2017-03-15 23:55:16 +03:00
const std : : string & Triple = TargetOpts . Triple ;
2016-12-20 17:38:19 +03:00
2015-02-03 23:32:09 +03:00
std : : string Error ;
2017-03-15 23:55:16 +03:00
const Target * TheTarget = TargetRegistry : : lookupTarget ( Triple , Error ) ;
2015-02-03 23:32:09 +03:00
if ( ! TheTarget ) {
2016-09-10 22:04:39 +03:00
cling : : errs ( ) < < " cling::IncrementalExecutor: unable to find target: \n "
< < Error ;
2016-07-15 17:22:08 +03:00
return std : : unique_ptr < TargetMachine > ( ) ;
2015-02-03 23:32:09 +03:00
}
2015-12-15 13:47:56 +03:00
CodeGenOpt : : Level OptLevel = CodeGenOpt : : Default ;
switch ( CGOpt . OptimizationLevel ) {
case 0 : OptLevel = CodeGenOpt : : None ; break ;
case 1 : OptLevel = CodeGenOpt : : Less ; break ;
case 2 : OptLevel = CodeGenOpt : : Default ; break ;
case 3 : OptLevel = CodeGenOpt : : Aggressive ; break ;
default : OptLevel = CodeGenOpt : : Default ;
}
2020-04-05 10:29:29 +03:00
using namespace llvm : : orc ;
auto JTMB = JITTargetMachineBuilder : : detectHost ( ) ;
if ( ! JTMB )
logAllUnhandledErrors ( JTMB . takeError ( ) , llvm : : errs ( ) ,
" Error detecting host " ) ;
JTMB - > setCodeGenOptLevel ( OptLevel ) ;
2020-10-05 09:22:33 +03:00
# ifdef _WIN32
2020-04-05 10:29:29 +03:00
JTMB - > getOptions ( ) . EmulatedTLS = false ;
2020-10-05 09:22:33 +03:00
# endif // _WIN32
2015-02-03 23:32:09 +03:00
2020-04-05 10:29:29 +03:00
# if defined(__powerpc64__) || defined(__PPC64__)
// We have to use large code model for PowerPC64 because TOC and text sections
// can be more than 2GB apart.
2021-04-23 22:40:14 +03:00
JTMB - > setCodeModel ( CodeModel : : Large ) ;
2018-03-13 13:44:57 +03:00
# endif
2020-04-05 10:29:29 +03:00
2021-04-23 22:40:14 +03:00
std : : unique_ptr < TargetMachine > TM = cantFail ( JTMB - > createTargetMachine ( ) ) ;
2021-03-09 23:22:34 +03:00
// Forcefully disable GlobalISel, it might be enabled on AArch64 without
// optimizations. In tests on an Apple M1 after the upgrade to LLVM 9, this
// new instruction selection framework emits branches / calls that expect all
// code to be reachable in +/- 128 MB. This cannot be guaranteed during JIT,
// which generates code into allocated pages on the heap and could span the
// entire address space of the process.
//
// TODO:
// 1. Try to reproduce the problem with vanilla lli of LLVM 9 to check that
// this is not related to the way Cling incrementally JITs and executes.
// 2. Figure out exactly why GlobalISel emits different branch instructions,
// and whether this is a problem in the framework or of the generated IR.
// 3. Verify if the same happens with LLVM 11/12 (whatever Cling will move to
// next), and possibly fix the underlying issue in LLVM upstream's `main`.
//
// FIXME: Lift this restriction and allow the target to enable GlobalISel,
// if deemed ready by upstream developers.
TM - > setGlobalISel ( false ) ;
return TM ;
2013-06-25 11:21:14 +04:00
}
2017-03-15 23:55:16 +03:00
2016-12-20 17:38:19 +03:00
} // anonymous namespace
2020-09-30 18:46:22 +03:00
IncrementalExecutor : : IncrementalExecutor ( clang : : DiagnosticsEngine & /*diags*/ ,
2017-08-26 05:42:17 +03:00
const clang : : CompilerInstance & CI ) :
2017-08-30 22:36:03 +03:00
m_Callbacks ( nullptr ) , m_externalIncrementalExecutor ( nullptr )
2016-12-20 17:38:19 +03:00
#if 0
: m_Diags ( diags )
# endif
{
2020-10-11 00:43:37 +03:00
m_DyLibManager . initializeDyld ( [ ] ( llvm : : StringRef ) { /*ignore*/ return false ; } ) ;
2016-12-20 17:38:19 +03:00
// MSVC doesn't support m_AtExitFuncsSpinLock=ATOMIC_FLAG_INIT; in the class definition
std : : atomic_flag_clear ( & m_AtExitFuncsSpinLock ) ;
2017-03-15 23:55:16 +03:00
std : : unique_ptr < TargetMachine > TM ( CreateHostTargetMachine ( CI ) ) ;
2021-08-05 17:57:09 +03:00
auto & TMRef = * TM ;
2020-09-05 15:00:40 +03:00
auto RetainOwnership =
[ this ] ( llvm : : orc : : VModuleKey K , std : : unique_ptr < Module > M ) - > void {
assert ( m_PendingModules . count ( K ) & & " Unable to find the module " ) ;
m_PendingModules [ K ] - > setModule ( std : : move ( M ) ) ;
m_PendingModules . erase ( K ) ;
} ;
m_JIT . reset ( new IncrementalJIT ( * this , std : : move ( TM ) , RetainOwnership ) ) ;
2021-08-05 17:57:09 +03:00
m_BackendPasses . reset ( new BackendPasses ( CI . getCodeGenOpts ( ) ,
CI . getTargetOpts ( ) ,
CI . getLangOpts ( ) ,
TMRef ,
* m_JIT ) ) ;
2016-12-20 17:38:19 +03:00
}
// Keep in source: ~unique_ptr<ClingJIT> needs ClingJIT
IncrementalExecutor : : ~ IncrementalExecutor ( ) { }
2012-11-17 16:09:08 +04:00
2019-12-13 12:25:46 +03:00
void IncrementalExecutor : : runAtExitFuncs ( ) {
2017-07-12 22:04:11 +03:00
// It is legal to register an atexit handler from within another atexit
// handler and furthor-more the standard says they need to run in reverse
// order, so this function must be recursion safe.
AtExitFunctions Local ;
{
cling : : internal : : SpinLockGuard slg ( m_AtExitFuncsSpinLock ) ;
// Check this case first, to avoid the swap all-together.
if ( m_AtExitFuncs . empty ( ) )
return ;
Local . swap ( m_AtExitFuncs ) ;
}
2017-08-24 17:22:27 +03:00
for ( auto & & Ordered : llvm : : reverse ( Local . ordered ( ) ) ) {
2017-07-12 22:04:11 +03:00
for ( auto & & AtExit : llvm : : reverse ( Ordered - > second ) )
AtExit ( ) ;
2017-08-24 17:22:27 +03:00
// The standard says that they need to run in reverse order, which means
// anything added from 'AtExit()' must now be run!
2019-12-13 12:25:46 +03:00
runAtExitFuncs ( ) ;
2017-07-12 22:04:11 +03:00
}
2013-09-19 13:24:31 +04:00
}
2012-09-05 13:37:39 +04:00
2017-09-25 14:06:10 +03:00
void IncrementalExecutor : : AddAtExitFunc ( void ( * func ) ( void * ) , void * arg ,
2020-09-05 15:00:40 +03:00
const Transaction * T ) {
2013-09-19 13:24:31 +04:00
// Register a CXAAtExit function
2015-03-14 00:03:50 +03:00
cling : : internal : : SpinLockGuard slg ( m_AtExitFuncsSpinLock ) ;
2020-09-05 15:00:40 +03:00
m_AtExitFuncs [ T ] . emplace_back ( func , arg ) ;
2013-09-19 13:24:31 +04:00
}
2012-09-05 13:37:39 +04:00
void unresolvedSymbol ( )
{
2015-07-03 11:11:50 +03:00
// This might get called recursively, or a billion of times. Do not generate
// useless output; unresolvedSymbol() is always handed out with an error
// message - that's enough.
2016-09-10 22:04:39 +03:00
//cling::errs() << "IncrementalExecutor: calling unresolved symbol, "
2015-07-03 11:11:50 +03:00
// "see previous error message!\n";
// throw exception instead?
2012-09-05 13:37:39 +04:00
}
2019-03-16 12:36:13 +03:00
void *
IncrementalExecutor : : HandleMissingFunction ( const std : : string & mangled_name ) const {
2012-09-05 13:37:39 +04:00
// Not found in the map, add the symbol in the list of unresolved symbols
2012-10-07 15:53:16 +04:00
if ( m_unresolvedSymbols . insert ( mangled_name ) . second ) {
2016-09-10 22:04:39 +03:00
//cling::errs() << "IncrementalExecutor: use of undefined symbol '"
2014-05-28 15:56:16 +04:00
// << mangled_name << "'!\n";
2012-10-07 15:53:16 +04:00
}
2012-09-05 13:37:39 +04:00
2017-01-27 22:35:13 +03:00
return utils : : FunctionToVoidPtr ( & unresolvedSymbol ) ;
2012-09-05 13:37:39 +04:00
}
2019-03-16 12:36:13 +03:00
void *
IncrementalExecutor : : NotifyLazyFunctionCreators ( const std : : string & mangled_name ) const {
for ( auto it = m_lazyFuncCreator . begin ( ) , et = m_lazyFuncCreator . end ( ) ;
2012-09-05 13:37:39 +04:00
it ! = et ; + + it ) {
void * ret = ( void * ) ( ( LazyFunctionCreatorFunc_t ) * it ) ( mangled_name ) ;
2014-02-28 20:23:09 +04:00
if ( ret )
2012-10-25 16:58:50 +04:00
return ret ;
2012-09-05 13:37:39 +04:00
}
2015-12-18 18:12:00 +03:00
void * address = nullptr ;
if ( m_externalIncrementalExecutor )
2016-09-19 03:23:34 +03:00
address = m_externalIncrementalExecutor - > getAddressOfGlobal ( mangled_name ) ;
2012-10-25 16:58:50 +04:00
2015-12-18 18:12:00 +03:00
return ( address ? address : HandleMissingFunction ( mangled_name ) ) ;
2012-09-05 13:37:39 +04:00
}
2015-02-11 11:46:44 +03:00
#if 0
// FIXME: employ to empty module dependencies *within* the *current* module.
2012-11-17 20:28:14 +04:00
static void
freeCallersOfUnresolvedSymbols ( llvm : : SmallVectorImpl < llvm : : Function * > &
funcsToFree , llvm : : ExecutionEngine * engine ) {
llvm : : SmallPtrSet < llvm : : Function * , 40 > funcsToFreeUnique ;
for ( size_t i = 0 ; i < funcsToFree . size ( ) ; + + i ) {
llvm : : Function * func = funcsToFree [ i ] ;
2013-10-02 11:24:32 +04:00
assert ( func & & " Cannot free NULL function " ) ;
2015-01-15 17:13:14 +03:00
if ( funcsToFreeUnique . insert ( func ) . second ) {
2012-11-17 20:28:14 +04:00
for ( llvm : : Value : : use_iterator IU = func - > use_begin ( ) ,
EU = func - > use_end ( ) ; IU ! = EU ; + + IU ) {
llvm : : Instruction * instUser = llvm : : dyn_cast < llvm : : Instruction > ( * IU ) ;
if ( ! instUser ) continue ;
if ( ! instUser - > getParent ( ) ) continue ;
if ( llvm : : Function * userFunc = instUser - > getParent ( ) - > getParent ( ) )
funcsToFree . push_back ( userFunc ) ;
}
}
}
for ( llvm : : SmallPtrSet < llvm : : Function * , 40 > : : iterator
I = funcsToFreeUnique . begin ( ) , E = funcsToFreeUnique . end ( ) ;
I ! = E ; + + I ) {
// This should force the JIT to recompile the function. But the stubs stay,
// and the JIT reuses the stubs now pointing nowhere, i.e. without updating
// the machine code address. Fix the JIT, or hope that MCJIT helps.
//engine->freeMachineCodeForFunction(*I);
engine - > updateGlobalMapping ( * I , 0 ) ;
}
}
2015-02-11 11:46:44 +03:00
# endif
2012-09-05 13:37:39 +04:00
2020-09-05 15:00:40 +03:00
static bool isPracticallyEmptyModule ( const llvm : : Module * M ) {
return M - > empty ( ) & & M - > global_empty ( ) & & M - > alias_empty ( ) ;
}
2014-02-19 15:49:24 +04:00
IncrementalExecutor : : ExecutionResult
2020-09-05 15:00:40 +03:00
IncrementalExecutor : : runStaticInitializersOnce ( Transaction & T ) {
2020-03-18 19:33:39 +03:00
llvm : : Module * m = T . getModule ( ) ;
assert ( m & & " Module must not be null " ) ;
2012-09-05 13:37:39 +04:00
2020-09-05 15:00:40 +03:00
if ( isPracticallyEmptyModule ( m ) )
return kExeSuccess ;
llvm : : orc : : VModuleKey K =
emitModule ( T . takeModule ( ) , T . getCompilationOpts ( ) . OptLevel ) ;
m_PendingModules [ K ] = & T ;
2015-01-09 11:40:58 +03:00
// We don't care whether something was unresolved before.
m_unresolvedSymbols . clear ( ) ;
// check if there is any unresolved symbol in the list
if ( diagnoseUnresolvedSymbols ( " static initializers " ) )
return kExeUnresolvedSymbols ;
2012-11-20 20:24:02 +04:00
llvm : : GlobalVariable * GV
= m - > getGlobalVariable ( " llvm.global_ctors " , true ) ;
// Nothing to do is good, too.
if ( ! GV ) return kExeSuccess ;
// Close similarity to
// m_engine->runStaticConstructorsDestructors(false) aka
// llvm::ExecutionEngine::runStaticConstructorsDestructors()
// is intentional; we do an extra pass to check whether the JIT
// managed to collect all the symbols needed by the niitializers.
// Should be an array of '{ i32, void ()* }' structs. The first value is
// the init priority, which we ignore.
llvm : : ConstantArray * InitList
= llvm : : dyn_cast < llvm : : ConstantArray > ( GV - > getInitializer ( ) ) ;
2014-01-22 18:07:11 +04:00
2012-11-20 20:24:02 +04:00
if ( InitList = = 0 )
return kExeSuccess ;
2012-09-05 13:37:39 +04:00
2016-07-21 22:38:38 +03:00
//SmallVector<Function*, 2> initFuncs;
2014-03-12 20:58:34 +04:00
2012-11-20 20:24:02 +04:00
for ( unsigned i = 0 , e = InitList - > getNumOperands ( ) ; i ! = e ; + + i ) {
llvm : : ConstantStruct * CS
= llvm : : dyn_cast < llvm : : ConstantStruct > ( InitList - > getOperand ( i ) ) ;
if ( CS = = 0 ) continue ;
llvm : : Constant * FP = CS - > getOperand ( 1 ) ;
if ( FP - > isNullValue ( ) )
continue ; // Found a sentinal value, ignore.
// Strip off constant expression casts.
if ( llvm : : ConstantExpr * CE = llvm : : dyn_cast < llvm : : ConstantExpr > ( FP ) )
if ( CE - > isCast ( ) )
FP = CE - > getOperand ( 0 ) ;
// Execute the ctor/dtor function!
if ( llvm : : Function * F = llvm : : dyn_cast < llvm : : Function > ( FP ) ) {
2016-07-21 22:37:05 +03:00
const llvm : : StringRef fName = F - > getName ( ) ;
executeInit ( fName ) ;
2016-07-21 22:38:38 +03:00
/*
2014-03-12 20:58:34 +04:00
initFuncs . push_back ( F ) ;
2016-07-21 22:37:05 +03:00
if ( fName . startswith ( " _GLOBAL__sub_I_ " ) ) {
2014-03-15 02:11:09 +04:00
BasicBlock & BB = F - > getEntryBlock ( ) ;
for ( BasicBlock : : iterator I = BB . begin ( ) , E = BB . end ( ) ; I ! = E ; + + I )
if ( CallInst * call = dyn_cast < CallInst > ( I ) )
initFuncs . push_back ( call - > getCalledFunction ( ) ) ;
}
2016-07-21 22:38:38 +03:00
*/
2012-11-20 20:24:02 +04:00
}
2012-09-05 13:37:39 +04:00
}
2012-11-20 20:24:02 +04:00
2016-07-21 22:38:38 +03:00
/*
2014-03-12 20:58:34 +04:00
for ( SmallVector < Function * , 2 > : : iterator I = initFuncs . begin ( ) ,
E = initFuncs . end ( ) ; I ! = E ; + + I ) {
// Cleanup also the dangling init functions. They are in the form:
// define internal void @_GLOBAL__I_aN() section "..."{
// entry:
// call void @__cxx_global_var_init(N-1)()
2014-03-15 02:10:33 +04:00
// call void @__cxx_global_var_initM()
2014-03-12 20:58:34 +04:00
// ret void
// }
//
// define internal void @__cxx_global_var_init(N-1)() section "..." {
// entry:
// call void @_ZN7MyClassC1Ev(%struct.MyClass* @n)
// ret void
// }
// Erase __cxx_global_var_init(N-1)() first.
2016-07-21 22:38:38 +03:00
( * I ) - > removeDeadConstantUsers ( ) ;
( * I ) - > eraseFromParent ( ) ;
2014-03-12 20:58:34 +04:00
}
2016-07-21 22:38:38 +03:00
*/
2014-03-12 20:58:34 +04:00
2012-11-20 20:24:02 +04:00
return kExeSuccess ;
2012-09-05 13:37:39 +04:00
}
2014-02-27 12:53:42 +04:00
void IncrementalExecutor : : runAndRemoveStaticDestructors ( Transaction * T ) {
assert ( T & & " Must be set " ) ;
// Collect all the dtors bound to this transaction.
2017-07-12 22:04:11 +03:00
AtExitFunctions : : mapped_type Local ;
2015-03-14 00:03:50 +03:00
{
cling : : internal : : SpinLockGuard slg ( m_AtExitFuncsSpinLock ) ;
2020-09-05 15:00:40 +03:00
auto Itr = m_AtExitFuncs . find ( T ) ;
2017-08-24 17:22:27 +03:00
if ( Itr = = m_AtExitFuncs . end ( ) ) return ;
2017-07-12 22:04:11 +03:00
m_AtExitFuncs . erase ( Itr , & Local ) ;
2015-03-14 00:03:50 +03:00
} // end of spin lock lifetime block.
2014-02-27 12:53:42 +04:00
2017-07-12 22:04:11 +03:00
// 'Unload' the cxa_atexit, atexit entities.
for ( auto & & AtExit : llvm : : reverse ( Local ) ) {
2017-07-12 21:40:51 +03:00
AtExit ( ) ;
2017-07-12 22:04:11 +03:00
// Run anything that was just registered in 'AtExit()'
runAndRemoveStaticDestructors ( T ) ;
}
2012-09-05 13:37:39 +04:00
}
2019-03-13 10:12:28 +03:00
static void flushOutBuffers ( ) {
// Force-flush as we might be printing on screen with printf.
std : : cout . flush ( ) ;
fflush ( stdout ) ;
}
IncrementalExecutor : : ExecutionResult
IncrementalExecutor : : executeWrapper ( llvm : : StringRef function ,
2019-03-16 12:36:13 +03:00
Value * returnValue /* =0*/ ) const {
2019-03-13 10:12:28 +03:00
// Set the value to cling::invalid.
if ( returnValue )
* returnValue = Value ( ) ;
typedef void ( * InitFun_t ) ( void * ) ;
InitFun_t fun ;
ExecutionResult res = jitInitOrWrapper ( function , fun ) ;
if ( res ! = kExeSuccess )
return res ;
EnterUserCodeRAII euc ( m_Callbacks ) ;
( * fun ) ( returnValue ) ;
flushOutBuffers ( ) ;
return kExeSuccess ;
}
2020-10-11 00:43:37 +03:00
void IncrementalExecutor : : setCallbacks ( InterpreterCallbacks * callbacks ) {
m_Callbacks = callbacks ;
m_DyLibManager . setCallbacks ( callbacks ) ;
}
2012-09-05 13:37:39 +04:00
void
2014-02-19 15:49:24 +04:00
IncrementalExecutor : : installLazyFunctionCreator ( LazyFunctionCreatorFunc_t fp )
2012-09-05 13:37:39 +04:00
{
m_lazyFuncCreator . push_back ( fp ) ;
}
2014-02-19 15:49:24 +04:00
bool
2016-10-01 07:36:12 +03:00
IncrementalExecutor : : addSymbol ( const char * Name , void * Addr ,
2019-03-16 12:36:13 +03:00
bool Jit ) const {
2016-10-01 07:36:12 +03:00
return m_JIT - > lookupSymbol ( Name , Addr , Jit ) . second ;
2012-09-05 13:37:39 +04:00
}
2012-09-24 13:57:43 +04:00
2015-01-12 11:57:30 +03:00
void * IncrementalExecutor : : getAddressOfGlobal ( llvm : : StringRef symbolName ,
2019-03-16 12:36:13 +03:00
bool * fromJIT /*=0*/ ) const {
2012-11-19 02:27:00 +04:00
// Return a symbol's address, and whether it was jitted.
2016-10-01 07:36:12 +03:00
void * address = m_JIT - > lookupSymbol ( symbolName ) . first ;
2015-01-12 11:23:03 +03:00
// It's not from the JIT if it's in a dylib.
if ( fromJIT )
* fromJIT = ! address ;
if ( ! address )
2015-12-04 14:25:14 +03:00
return ( void * ) m_JIT - > getSymbolAddress ( symbolName , false /*no dlsym*/ ) ;
2015-01-12 11:23:03 +03:00
2012-11-19 02:27:00 +04:00
return address ;
}
2013-12-05 19:22:34 +04:00
void *
2020-09-05 15:00:40 +03:00
IncrementalExecutor : : getPointerToGlobalFromJIT ( llvm : : StringRef name ) const {
// Get the function / variable pointer referenced by name.
2014-10-14 16:11:31 +04:00
// We don't care whether something was unresolved before.
m_unresolvedSymbols . clear ( ) ;
2020-09-05 15:00:40 +03:00
void * addr = ( void * ) m_JIT - > getSymbolAddress ( name , false /*no dlsym*/ ) ;
2013-12-05 19:22:34 +04:00
2020-09-05 15:00:40 +03:00
if ( diagnoseUnresolvedSymbols ( name , " symbol " ) )
2014-10-22 21:08:26 +04:00
return 0 ;
2015-02-03 23:32:09 +03:00
return addr ;
2013-12-05 19:22:34 +04:00
}
2014-10-14 16:11:31 +04:00
2014-10-16 13:20:04 +04:00
bool IncrementalExecutor : : diagnoseUnresolvedSymbols ( llvm : : StringRef trigger ,
2019-03-16 12:36:13 +03:00
llvm : : StringRef title ) const {
2014-10-14 16:11:31 +04:00
if ( m_unresolvedSymbols . empty ( ) )
return false ;
2018-11-20 21:55:35 +03:00
// Issue callback to TCling!!
for ( const std : : string & sym : m_unresolvedSymbols ) {
// We emit callback to LibraryLoadingFailed when we get error with error message.
if ( InterpreterCallbacks * C = m_Callbacks )
if ( C - > LibraryLoadingFailed ( sym , " " , false , false ) )
return false ;
}
2014-10-14 16:11:31 +04:00
llvm : : SmallVector < llvm : : Function * , 128 > funcsToFree ;
2017-07-12 21:33:07 +03:00
for ( const std : : string & sym : m_unresolvedSymbols ) {
2014-10-14 16:11:31 +04:00
#if 0
// FIXME: This causes a lot of test failures, for some reason it causes
// the call to HandleMissingFunction to be elided.
unsigned diagID = m_Diags . getCustomDiagID ( clang : : DiagnosticsEngine : : Error ,
" %0 unresolved while jitting %1 " ) ;
( void ) diagID ;
2017-07-12 21:33:07 +03:00
//m_Diags.Report(diagID) << sym << funcname; // TODO: demangle the names.
2014-10-14 16:11:31 +04:00
# endif
2017-07-12 21:33:07 +03:00
cling : : errs ( ) < < " IncrementalExecutor::executeFunction: symbol ' " < < sym
2016-09-10 22:04:39 +03:00
< < " ' unresolved while linking " ;
2015-02-12 20:23:04 +03:00
if ( trigger . find ( utils : : Synthesize : : UniquePrefix ) ! = llvm : : StringRef : : npos )
2016-09-10 22:04:39 +03:00
cling : : errs ( ) < < " [cling interface function] " ;
2015-02-12 20:23:04 +03:00
else {
if ( ! title . empty ( ) )
2016-09-10 22:04:39 +03:00
cling : : errs ( ) < < title < < " ' " ;
cling : : errs ( ) < < trigger ;
2015-02-12 20:23:04 +03:00
if ( ! title . empty ( ) )
2016-09-10 22:04:39 +03:00
cling : : errs ( ) < < " ' " ;
2015-02-12 20:23:04 +03:00
}
2016-09-10 22:04:39 +03:00
cling : : errs ( ) < < " ! \n " ;
2015-02-12 20:23:04 +03:00
// Be helpful, demangle!
2017-07-12 21:33:07 +03:00
std : : string demangledName = platform : : Demangle ( sym ) ;
2015-02-12 20:23:04 +03:00
if ( ! demangledName . empty ( ) ) {
2016-09-10 22:04:39 +03:00
cling : : errs ( )
2015-02-12 20:23:04 +03:00
< < " You are probably missing the definition of "
< < demangledName < < " \n "
< < " Maybe you need to load the corresponding shared library? \n " ;
}
2020-10-11 00:43:37 +03:00
# ifdef __APPLE__
// The JIT gives us a mangled name which has only one leading underscore on
// all platforms, for instance _ZN8TRandom34RndmEv. However, on OSX the
// linker stores this symbol as __ZN8TRandom34RndmEv (adding an extra _).
assert ( ! llvm : : StringRef ( sym ) . startswith ( " __ " ) & & " Already added! " ) ;
std : : string libName = m_DyLibManager . searchLibrariesForSymbol ( ' _ ' + sym ,
/*searchSystem=*/ true ) ;
# else
std : : string libName = m_DyLibManager . searchLibrariesForSymbol ( sym ,
/*searchSystem=*/ true ) ;
# endif //__APPLE__
if ( ! libName . empty ( ) )
cling : : errs ( ) < < " Symbol found in ' " < < libName < < " '; "
< < " did you mean to load it with '.L "
< < libName < < " '? \n " ;
2015-02-03 23:32:09 +03:00
//llvm::Function *ff = m_engine->FindFunctionNamed(i->c_str());
2014-10-14 16:11:31 +04:00
// i could also reference a global variable, in which case ff == 0.
2015-02-03 23:32:09 +03:00
//if (ff)
// funcsToFree.push_back(ff);
2014-10-14 16:11:31 +04:00
}
2015-02-03 23:32:09 +03:00
//freeCallersOfUnresolvedSymbols(funcsToFree, m_engine.get());
2014-10-14 16:11:31 +04:00
m_unresolvedSymbols . clear ( ) ;
return true ;
}
2014-03-07 18:09:02 +04:00
} // end namespace cling