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
//------------------------------------------------------------------------------
# include "ExecutionContext.h"
2012-10-05 16:09:51 +04:00
# include "cling/Interpreter/StoredValueRef.h"
2013-10-31 02:40:00 +04:00
# include "cling/Interpreter/Interpreter.h" // FIXME: Remove when at_exit is ready
2012-09-05 13:37:39 +04:00
2013-02-22 19:59:54 +04:00
# include "clang/AST/Type.h"
2013-04-24 20:28:08 +04:00
# include "llvm/IR/Constants.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-09-05 13:37:39 +04:00
# include "llvm/PassManager.h"
2012-11-17 20:28:14 +04:00
# include "llvm/ADT/SmallPtrSet.h"
2013-02-22 19:59:54 +04:00
# include "llvm/ExecutionEngine/GenericValue.h"
2012-09-05 13:37:39 +04:00
# include "llvm/ExecutionEngine/JIT.h"
# include "llvm/Support/raw_ostream.h"
# include "llvm/Support/DynamicLibrary.h"
using namespace cling ;
std : : set < std : : string > ExecutionContext : : m_unresolvedSymbols ;
std : : vector < ExecutionContext : : LazyFunctionCreatorFunc_t >
ExecutionContext : : m_lazyFuncCreator ;
2012-10-25 18:50:54 +04:00
bool ExecutionContext : : m_LazyFuncCreatorDiagsSuppressed = false ;
2012-10-25 13:17:45 +04:00
2012-11-17 16:09:08 +04:00
// Keep in source: OwningPtr<ExecutionEngine> needs #include ExecutionEngine
2013-06-25 11:21:14 +04:00
ExecutionContext : : ExecutionContext ( llvm : : Module * m )
2014-01-22 18:07:11 +04:00
: m_CxaAtExitRemapped ( false )
2013-06-25 11:21:14 +04:00
{
assert ( m & & " llvm::Module must not be null! " ) ;
2013-09-19 13:24:31 +04:00
m_AtExitFuncs . reserve ( 256 ) ;
2013-06-25 11:21:14 +04:00
InitializeBuilder ( m ) ;
}
2012-11-17 16:09:08 +04:00
// Keep in source: ~OwningPtr<ExecutionEngine> needs #include ExecutionEngine
2013-10-03 18:58:48 +04:00
ExecutionContext : : ~ ExecutionContext ( ) { }
void ExecutionContext : : shuttingDown ( ) {
2013-09-19 13:24:31 +04:00
for ( size_t I = 0 , N = m_AtExitFuncs . size ( ) ; I < N ; + + I ) {
const CXAAtExitElement & AEE = m_AtExitFuncs [ N - I - 1 ] ;
( * AEE . m_Func ) ( AEE . m_Arg ) ;
}
}
2012-09-05 13:37:39 +04:00
2013-10-31 02:40:00 +04:00
void ExecutionContext : : remapCXAAtExit ( ) {
assert ( ! m_CxaAtExitRemapped & & " __cxa_at_exit already remapped. " ) ;
llvm : : Function * clingAtExit
= m_engine - > FindFunctionNamed ( " cling_cxa_atexit " ) ;
2013-11-01 21:31:18 +04:00
assert ( clingAtExit & & " cling_cxa_atexit must exist. " ) ;
llvm : : Function * atExit = m_engine - > FindFunctionNamed ( " __cxa_atexit " ) ;
if ( ! atExit ) {
// Inject __cxa_atexit into module
llvm : : Type * retTy = 0 ;
llvm : : Type * voidPtrTy = 0 ;
if ( sizeof ( int ) = = 4 ) {
retTy = llvm : : Type : : getInt32Ty ( llvm : : getGlobalContext ( ) ) ;
voidPtrTy = llvm : : Type : : getInt32PtrTy ( llvm : : getGlobalContext ( ) ) ;
} else if ( sizeof ( int ) = = 8 ) {
retTy = llvm : : Type : : getInt64Ty ( llvm : : getGlobalContext ( ) ) ;
voidPtrTy = llvm : : Type : : getInt64PtrTy ( llvm : : getGlobalContext ( ) ) ;
} else {
assert ( retTy & & " Unsupported sizeof(int)! " ) ;
retTy = llvm : : Type : : getInt64Ty ( llvm : : getGlobalContext ( ) ) ;
voidPtrTy = llvm : : Type : : getInt64PtrTy ( llvm : : getGlobalContext ( ) ) ;
}
llvm : : SmallVector < llvm : : Type * , 3 > argTy ;
argTy . push_back ( voidPtrTy ) ;
argTy . push_back ( voidPtrTy ) ;
argTy . push_back ( voidPtrTy ) ;
llvm : : FunctionType * cxaatexitTy
= llvm : : FunctionType : : get ( retTy , argTy , false /*varArg*/ ) ;
llvm : : Function * atexitFunc
= llvm : : Function : : Create ( cxaatexitTy , llvm : : GlobalValue : : InternalLinkage ,
" __cxa_atexit " , 0 /*module*/ ) ;
m_engine - > addGlobalMapping ( atexitFunc , clingAtExit ) ;
}
2013-10-31 02:40:00 +04:00
void * clingAtExitAddr = m_engine - > getPointerToFunction ( clingAtExit ) ;
assert ( clingAtExitAddr & & " cannot find cling_cxa_atexit " ) ;
m_engine - > updateGlobalMapping ( atExit , clingAtExitAddr ) ;
m_CxaAtExitRemapped = true ;
}
2013-06-25 11:21:14 +04:00
void ExecutionContext : : InitializeBuilder ( llvm : : Module * m ) {
2012-09-05 13:37:39 +04:00
//
// Create an execution engine to use.
//
assert ( m & & " Module cannot be null " ) ;
2012-11-17 15:29:35 +04:00
// Note: Engine takes ownership of the module.
2012-09-05 13:37:39 +04:00
llvm : : EngineBuilder builder ( m ) ;
2012-11-17 15:29:35 +04:00
2012-09-05 13:37:39 +04:00
std : : string errMsg ;
builder . setErrorStr ( & errMsg ) ;
2013-08-12 21:18:53 +04:00
builder . setOptLevel ( llvm : : CodeGenOpt : : Less ) ;
2012-09-05 13:37:39 +04:00
builder . setEngineKind ( llvm : : EngineKind : : JIT ) ;
builder . setAllocateGVsWithCode ( false ) ;
2012-11-17 16:09:08 +04:00
// EngineBuilder uses default c'ted TargetOptions, too:
llvm : : TargetOptions TargetOpts ;
TargetOpts . NoFramePointerElim = 1 ;
TargetOpts . JITEmitDebugInfo = 1 ;
builder . setTargetOptions ( TargetOpts ) ;
m_engine . reset ( builder . create ( ) ) ;
2012-11-17 15:29:35 +04:00
if ( ! m_engine )
llvm : : errs ( ) < < " cling::ExecutionContext::InitializeBuilder(): " < < errMsg ;
assert ( m_engine & & " Cannot create module! " ) ;
2012-09-05 13:37:39 +04:00
2012-11-17 15:29:35 +04:00
// install lazy function creators
2012-09-05 13:37:39 +04:00
m_engine - > InstallLazyFunctionCreator ( NotifyLazyFunctionCreators ) ;
}
2013-09-19 13:24:31 +04:00
int ExecutionContext : : CXAAtExit ( void ( * func ) ( void * ) , void * arg , void * dso ,
void * clangDecl ) {
// Register a CXAAtExit function
clang : : Decl * LastTLD = ( clang : : Decl * ) clangDecl ;
m_AtExitFuncs . push_back ( CXAAtExitElement ( func , arg , dso , LastTLD ) ) ;
return 0 ; // happiness
}
2012-09-05 13:37:39 +04:00
void unresolvedSymbol ( )
{
// throw exception?
2012-11-17 20:28:14 +04:00
llvm : : errs ( ) < < " ExecutionContext: calling unresolved symbol, "
" see previous error message! \n " ;
2012-09-05 13:37:39 +04:00
}
void * ExecutionContext : : HandleMissingFunction ( const std : : string & mangled_name )
{
// 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 ) {
llvm : : errs ( ) < < " ExecutionContext: use of undefined symbol ' "
< < mangled_name < < " '! \n " ;
}
2012-09-05 13:37:39 +04:00
// Avoid "ISO C++ forbids casting between pointer-to-function and
// pointer-to-object":
return ( void * ) reinterpret_cast < size_t > ( unresolvedSymbol ) ;
}
void *
ExecutionContext : : NotifyLazyFunctionCreators ( const std : : string & mangled_name )
{
for ( std : : vector < LazyFunctionCreatorFunc_t > : : iterator it
= m_lazyFuncCreator . begin ( ) , et = m_lazyFuncCreator . end ( ) ;
it ! = et ; + + it ) {
void * ret = ( void * ) ( ( LazyFunctionCreatorFunc_t ) * it ) ( mangled_name ) ;
2012-10-25 16:58:50 +04:00
if ( ret )
return ret ;
2012-09-05 13:37:39 +04:00
}
2012-10-25 16:58:50 +04:00
2012-10-25 18:10:41 +04:00
if ( m_LazyFuncCreatorDiagsSuppressed )
2012-10-25 16:58:50 +04:00
return 0 ;
2012-09-05 13:37:39 +04:00
return HandleMissingFunction ( mangled_name ) ;
}
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 " ) ;
2012-11-17 20:28:14 +04:00
if ( funcsToFreeUnique . insert ( func ) ) {
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 ) ;
}
}
2012-11-16 00:55:43 +04:00
ExecutionContext : : ExecutionResult
2012-09-05 13:37:39 +04:00
ExecutionContext : : executeFunction ( llvm : : StringRef funcname ,
2013-12-04 14:26:02 +04:00
Interpreter & interp ,
2012-10-05 16:09:51 +04:00
clang : : QualType retType ,
StoredValueRef * returnValue )
2012-09-05 13:37:39 +04:00
{
// Call a function without arguments, or with an SRet argument, see SRet below
// We don't care whether something was unresolved before.
m_unresolvedSymbols . clear ( ) ;
2012-11-17 20:28:14 +04:00
llvm : : Function * f = m_engine - > FindFunctionNamed ( funcname . str ( ) . c_str ( ) ) ;
2012-09-05 13:37:39 +04:00
if ( ! f ) {
2012-11-17 20:28:14 +04:00
llvm : : errs ( ) < < " ExecutionContext::executeFunction: "
" could not find function named " < < funcname < < ' \n ' ;
2012-11-16 00:55:43 +04:00
return kExeFunctionNotCompiled ;
2012-09-05 13:37:39 +04:00
}
m_engine - > getPointerToFunction ( f ) ;
// check if there is any unresolved symbol in the list
if ( ! m_unresolvedSymbols . empty ( ) ) {
2012-11-17 20:28:14 +04:00
llvm : : SmallVector < llvm : : Function * , 100 > funcsToFree ;
2012-09-05 13:37:39 +04:00
for ( std : : set < std : : string > : : const_iterator i = m_unresolvedSymbols . begin ( ) ,
e = m_unresolvedSymbols . end ( ) ; i ! = e ; + + i ) {
2012-11-17 20:28:14 +04:00
llvm : : errs ( ) < < " ExecutionContext::executeFunction: symbol ' " < < * i
< < " ' unresolved while linking function ' " < < funcname
< < " '! \n " ;
2012-09-05 13:37:39 +04:00
llvm : : Function * ff = m_engine - > FindFunctionNamed ( i - > c_str ( ) ) ;
2013-10-02 11:24:32 +04:00
// i could also reference a global variable, in which case ff == 0.
if ( ff )
funcsToFree . push_back ( ff ) ;
2012-09-05 13:37:39 +04:00
}
2012-11-17 20:28:14 +04:00
freeCallersOfUnresolvedSymbols ( funcsToFree , m_engine . get ( ) ) ;
2012-09-05 13:37:39 +04:00
m_unresolvedSymbols . clear ( ) ;
2012-11-16 00:55:43 +04:00
return kExeUnresolvedSymbols ;
2012-09-05 13:37:39 +04:00
}
std : : vector < llvm : : GenericValue > args ;
bool wantReturn = ( returnValue ) ;
2012-10-05 16:09:51 +04:00
StoredValueRef aggregateRet ;
2012-09-05 13:37:39 +04:00
if ( f - > hasStructRetAttr ( ) ) {
// Function expects to receive the storage for the returned aggregate as
2012-10-05 16:09:51 +04:00
// first argument. Allocate returnValue:
2013-12-04 14:26:02 +04:00
aggregateRet = StoredValueRef : : allocate ( interp , retType ,
f - > getReturnType ( ) ) ;
2012-10-05 16:09:51 +04:00
if ( returnValue ) {
* returnValue = aggregateRet ;
} else {
returnValue = & aggregateRet ;
}
Add support in TClingCallFunc for calling virtual functions: when we have a
CallFunc representation of a member function, we have its concrete address, and
if there is derived class there is no way how to evaluate the vtable and call the
correct virtual.
In order to evaluate the member's vtable, we either have to calculate it (which
is platform/implementation dependent) or create a trampoline function which calls
the target function. Thus codegen will generate code for vtable evaluation. The
trampolines are created for functions that could be possibly virtual - non-static
member functions, declared as virutal. A trampoline looks like this:
void unique_name(Base* This, MyClass2* a, MyClass3* b, double c, MyClass* ret) {
if (ret)
*ret = This->function(*a, *b, c);
else
This->function(*a, *b, c);
}
where we pass in explicitly the address that CallFunc is given (This), followed
by set of arguments (possibly needing conversion) and if the target is non-void
a last argument that is used to provide a storage for the return result.
The given arguments and the required by *LLVM* IR may differ - in that case
simple argument conversions are done. For now primary pointers and builtins, which
covers pretty much entire LLVM type system.
Note that the first implementation is very rough and suboptimal, however it is
very good starting point (milestone):
* Common code is extracted out in its separate routines (here a lot of refactoring more
could be done).
* cling::Value now stores besides clang::QualType, describing (soft-of inaccurately)
the llvm::GenericValue, LLVM-type which is the actual description of the generic value.
* cling::Value naming conventions improved;
* cling::Interpreter::getLLVMType is added temporarily and soon will be removed.
* TClingCallFunc now preallocates space for the this ptr and the return result.
(instead of multiple array copies)
* TClingCallFunc now stores not llvm::GenericValues but cling::StoredValueRefs
because the type conversions need llvm::GenericValue's description (which
essentially is the type).
* Does better default argument evaluation: it could be even *more betterer* if
we query Sema to fold for us the constant expressions instead of decompiling them
to source and using cling for evaluation.
* Return storage implemented.
And, yes that is the longest commit message in my life...
Implementation of argument conversion is revised.
git-svn-id: http://root.cern.ch/svn/root/trunk@48537 27541ba8-7e3a-0410-8455-c3a389f83636
2013-02-11 21:30:03 +04:00
args . push_back ( returnValue - > get ( ) . getGV ( ) ) ;
2012-09-05 13:37:39 +04:00
// will get set as arg0, must not assign.
wantReturn = false ;
}
if ( wantReturn ) {
2012-10-05 16:09:51 +04:00
llvm : : GenericValue gvRet = m_engine - > runFunction ( f , args ) ;
// rescue the ret value (which might be aggregate) from the stack
2013-12-04 14:26:02 +04:00
* returnValue = StoredValueRef : : bitwiseCopy ( interp , Value ( gvRet , retType ) ) ;
2012-09-05 13:37:39 +04:00
} else {
m_engine - > runFunction ( f , args ) ;
}
2012-11-16 00:55:43 +04:00
return kExeSuccess ;
2012-09-05 13:37:39 +04:00
}
2012-11-20 20:24:02 +04:00
ExecutionContext : : ExecutionResult
2012-09-05 13:37:39 +04:00
ExecutionContext : : runStaticInitializersOnce ( llvm : : Module * m ) {
assert ( m & & " Module must not be null " ) ;
assert ( m_engine & & " Code generation did not create an engine! " ) ;
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
GV - > eraseFromParent ( ) ;
2012-11-20 20:24:02 +04:00
if ( InitList = = 0 )
return kExeSuccess ;
2012-09-05 13:37:39 +04:00
2012-11-20 20:24:02 +04:00
// We don't care whether something was unresolved before.
m_unresolvedSymbols . clear ( ) ;
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 ) ) {
m_engine - > getPointerToFunction ( F ) ;
// check if there is any unresolved symbol in the list
if ( ! m_unresolvedSymbols . empty ( ) ) {
llvm : : SmallVector < llvm : : Function * , 100 > funcsToFree ;
for ( std : : set < std : : string > : : const_iterator i = m_unresolvedSymbols . begin ( ) ,
e = m_unresolvedSymbols . end ( ) ; i ! = e ; + + i ) {
llvm : : errs ( ) < < " ExecutionContext::runStaticInitializersOnce: symbol ' " < < * i
< < " ' unresolved while linking static initializer ' "
< < F - > getName ( ) < < " '! \n " ;
llvm : : Function * ff = m_engine - > FindFunctionNamed ( i - > c_str ( ) ) ;
assert ( ff & & " cannot find function to free " ) ;
funcsToFree . push_back ( ff ) ;
}
freeCallersOfUnresolvedSymbols ( funcsToFree , m_engine . get ( ) ) ;
m_unresolvedSymbols . clear ( ) ;
return kExeUnresolvedSymbols ;
}
m_engine - > runFunction ( F , std : : vector < llvm : : GenericValue > ( ) ) ;
}
2012-09-05 13:37:39 +04:00
}
2012-11-20 20:24:02 +04:00
return kExeSuccess ;
2012-09-05 13:37:39 +04:00
}
void
ExecutionContext : : runStaticDestructorsOnce ( llvm : : Module * m ) {
assert ( m & & " Module must not be null " ) ;
assert ( m_engine & & " Code generation did not create an engine! " ) ;
llvm : : GlobalVariable * gdtors
= m - > getGlobalVariable ( " llvm.global_dtors " , true ) ;
if ( gdtors ) {
m_engine - > runStaticConstructorsDestructors ( true ) ;
}
2013-09-19 13:24:31 +04:00
// 'Unload' the cxa_atexit entities.
for ( size_t I = 0 , E = m_AtExitFuncs . size ( ) ; I < E ; + + I ) {
const CXAAtExitElement & AEE = m_AtExitFuncs [ E - I - 1 ] ;
( * AEE . m_Func ) ( AEE . m_Arg ) ;
}
m_AtExitFuncs . clear ( ) ;
2012-09-05 13:37:39 +04:00
}
void
ExecutionContext : : installLazyFunctionCreator ( LazyFunctionCreatorFunc_t fp )
{
m_lazyFuncCreator . push_back ( fp ) ;
}
2012-09-24 13:57:43 +04:00
bool ExecutionContext : : addSymbol ( const char * symbolName , void * symbolAddress ) {
2012-09-05 13:37:39 +04:00
2012-09-24 13:57:43 +04:00
void * actualAddress
2012-09-05 13:37:39 +04:00
= llvm : : sys : : DynamicLibrary : : SearchForAddressOfSymbol ( symbolName ) ;
2012-09-24 13:57:43 +04:00
if ( actualAddress )
2012-09-05 13:37:39 +04:00
return false ;
llvm : : sys : : DynamicLibrary : : AddSymbol ( symbolName , symbolAddress ) ;
return true ;
}
2012-09-24 13:57:43 +04:00
2012-09-24 16:11:46 +04:00
void * ExecutionContext : : getAddressOfGlobal ( llvm : : Module * m ,
const char * symbolName ,
bool * fromJIT /*=0*/ ) const {
2012-11-19 02:27:00 +04:00
// Return a symbol's address, and whether it was jitted.
void * address
= llvm : : sys : : DynamicLibrary : : SearchForAddressOfSymbol ( symbolName ) ;
if ( address ) {
if ( fromJIT ) * fromJIT = false ;
} else {
if ( fromJIT ) * fromJIT = true ;
llvm : : GlobalVariable * gvar = m - > getGlobalVariable ( symbolName , true ) ;
if ( ! gvar )
return 0 ;
2012-10-23 12:46:38 +04:00
2012-11-19 02:27:00 +04:00
address = m_engine - > getPointerToGlobal ( gvar ) ;
2012-09-24 13:57:43 +04:00
}
2012-11-19 02:27:00 +04:00
return address ;
}
2013-12-05 19:22:34 +04:00
void *
ExecutionContext : : getPointerToGlobalFromJIT ( const llvm : : GlobalValue & GV ) const {
if ( void * addr = m_engine - > getPointerToGlobalIfAvailable ( & GV ) )
return addr ;
// Function not yet codegened by the JIT, force this to happen now.
return m_engine - > getPointerToGlobal ( & GV ) ;
}