From baca204185ce5eea66321a9247e34e46e8e0d6d9 Mon Sep 17 00:00:00 2001 From: Axel Naumann Date: Tue, 4 Mar 2014 18:07:03 +0100 Subject: [PATCH] Simplify cxa_atexit(), first part. Remove trigger__cxa_atexit(), we will just remap until successful. Remove unused dso parameter. Instead of friending just provide a function. Pass a transaction, the typesafe way. --- include/cling/Interpreter/Interpreter.h | 14 +++---- include/cling/Interpreter/RuntimeUniverse.h | 5 +-- lib/Interpreter/IncrementalExecutor.cpp | 7 ++-- lib/Interpreter/IncrementalExecutor.h | 34 +++++------------ lib/Interpreter/IncrementalParser.h | 2 +- lib/Interpreter/Interpreter.cpp | 42 +++------------------ 6 files changed, 28 insertions(+), 76 deletions(-) diff --git a/include/cling/Interpreter/Interpreter.h b/include/cling/Interpreter/Interpreter.h index d047bdfc..16c0e2a5 100644 --- a/include/cling/Interpreter/Interpreter.h +++ b/include/cling/Interpreter/Interpreter.h @@ -49,9 +49,6 @@ namespace clang { namespace cling { namespace runtime { namespace internal { - int local_cxa_atexit(void (*func) (void*), void* arg, void* dso, - void* interp); - class DynamicExprInfo; template T EvaluateT(DynamicExprInfo* ExprInfo, clang::DeclContext* DC); @@ -586,11 +583,14 @@ namespace cling { /// void* getAddressOfGlobal(llvm::StringRef SymName, bool* fromJIT = 0) const; - friend class runtime::internal::LifetimeHandler; - friend int runtime::internal::local_cxa_atexit(void (*func) (void*), - void* arg, void* dso, - void* interp); + ///\brief Add an atexit function. + /// + ///\param[in] Func - Function to be called. + ///\param[in] Arg - argument passed to the function. + /// + void AddAtExitFunc(void (*Func) (void*), void* Arg); + friend class runtime::internal::LifetimeHandler; // FIXME: workaround until JIT supports exceptions static jmp_buf*& getNullDerefJump() { return m_JumpBuf; } }; diff --git a/include/cling/Interpreter/RuntimeUniverse.h b/include/cling/Interpreter/RuntimeUniverse.h index fb27547b..db8fc366 100644 --- a/include/cling/Interpreter/RuntimeUniverse.h +++ b/include/cling/Interpreter/RuntimeUniverse.h @@ -49,8 +49,7 @@ namespace cling { /// /// Implemented in Interpreter.cpp /// - int local_cxa_atexit(void (*func) (void*), void* arg, void* dso, - void* interp); + int local_cxa_atexit(void (*func) (void*), void* arg, void* interp); /// \brief Some of clang's routines rely on valid source locations and /// source ranges. This member can be looked up and source locations and @@ -183,7 +182,7 @@ extern "C" { extern "C" { int cling_cxa_atexit(void (*func) (void*), void* arg, void* dso) { - return cling::runtime::internal::local_cxa_atexit(func, arg, dso, + return cling::runtime::internal::local_cxa_atexit(func, arg, (void*)cling::runtime::gCling); } diff --git a/lib/Interpreter/IncrementalExecutor.cpp b/lib/Interpreter/IncrementalExecutor.cpp index 7812bc2d..89f6f227 100644 --- a/lib/Interpreter/IncrementalExecutor.cpp +++ b/lib/Interpreter/IncrementalExecutor.cpp @@ -116,12 +116,11 @@ void IncrementalExecutor::remapCXAAtExit() { m_CxaAtExitRemapped = true; } -int IncrementalExecutor::CXAAtExit(void (*func) (void*), void* arg, void* dso, - void* clingT) { +void IncrementalExecutor::AddAtExitFunc(void (*func) (void*), void* arg, + const cling::Transaction* clingT) { // Register a CXAAtExit function cling::Transaction* T = (cling::Transaction*)clingT; - m_AtExitFuncs.push_back(CXAAtExitElement(func, arg, dso, T)); - return 0; // happiness + m_AtExitFuncs.push_back(CXAAtExitElement(func, arg, T)); } void unresolvedSymbol() diff --git a/lib/Interpreter/IncrementalExecutor.h b/lib/Interpreter/IncrementalExecutor.h index ca4f42dd..475b7aff 100644 --- a/lib/Interpreter/IncrementalExecutor.h +++ b/lib/Interpreter/IncrementalExecutor.h @@ -25,13 +25,6 @@ namespace llvm { namespace cling { class Transaction; - namespace runtime { - namespace internal { - int local_cxa_atexit(void (*func) (void*), void* arg, void* dso, - void* interp); - } // end namespace internal - } // end namespace runtime - class StoredValueRef; class IncrementalExecutor { @@ -77,13 +70,12 @@ namespace cling { ///\param [in] func - The function to be called on exit or unloading of /// shared lib.(The destructor of the object.) ///\param [in] arg - The argument the func to be called with. - ///\param [in] dso - The dynamic shared object handle. ///\param [in] fromT - The unloading of this transaction will trigger the /// atexit function. /// - CXAAtExitElement(void (*func) (void*), void* arg, void* dso, - Transaction* fromT): - m_Func(func), m_Arg(arg), m_DSO(dso), m_FromT(fromT) {} + CXAAtExitElement(void (*func) (void*), void* arg, + const Transaction* fromT): + m_Func(func), m_Arg(arg), m_FromT(fromT) {} ///\brief The function to be called. /// @@ -93,14 +85,10 @@ namespace cling { /// void* m_Arg; - /// \brief The DSO handle. - /// - void* m_DSO; - ///\brief Clang's top level declaration, whose unloading will trigger the /// call this atexit function. /// - Transaction* m_FromT; //FIXME: Should be bound to the llvm symbol. + const Transaction* m_FromT; //FIXME: Should be bound to the llvm symbol. }; typedef llvm::SmallVector AtExitFunctions; @@ -183,19 +171,15 @@ namespace cling { return m_engine.get(); } + ///\brief Keep track of the entities whose dtor we need to call. + /// + void AddAtExitFunc(void (*func) (void*), void* arg, + const cling::Transaction* clingT); + private: static void* HandleMissingFunction(const std::string&); static void* NotifyLazyFunctionCreators(const std::string&); - ///\brief We keep track of the entities whose dtor we need to call. - /// - int CXAAtExit(void (*func) (void*), void* arg, void* dso, void* clingT); - - // This is the caller of CXAAtExit. We want to keep it private so we need - // to make the caller a friend. - friend int runtime::internal::local_cxa_atexit(void (*func) (void*), - void* arg, void* dso, - void* interp); }; } // end cling #endif // CLING_INCREMENTAL_EXECUTOR_H diff --git a/lib/Interpreter/IncrementalParser.h b/lib/Interpreter/IncrementalParser.h index db11ff48..987725d2 100644 --- a/lib/Interpreter/IncrementalParser.h +++ b/lib/Interpreter/IncrementalParser.h @@ -103,7 +103,7 @@ namespace cling { const char* llvmdir); ~IncrementalParser(); - void Initialize(); + void Initialize(); clang::CompilerInstance* getCI() const { return m_CI.get(); } clang::Parser* getParser() const { return m_Parser.get(); } clang::CodeGenerator* getCodeGenerator() const { return m_CodeGen.get(); } diff --git a/lib/Interpreter/Interpreter.cpp b/lib/Interpreter/Interpreter.cpp index 9c32e562..b2e44dae 100644 --- a/lib/Interpreter/Interpreter.cpp +++ b/lib/Interpreter/Interpreter.cpp @@ -69,46 +69,12 @@ namespace cling { namespace runtime { namespace internal { // "Declared" to the JIT in RuntimeUniverse.h - int local_cxa_atexit(void (*func) (void*), void* arg, void* dso, - void* interp) { + void local_cxa_atexit(void (*func) (void*), void* arg, void* interp) { Interpreter* cling = (cling::Interpreter*)interp; - IncrementalParser* incrP = cling->m_IncrParser.get(); - // FIXME: Bind to the module symbols. - cling::Transaction* T = incrP->getLastTransaction(); - - return cling->m_Executor->CXAAtExit(func, arg, dso, T); + cling->AddAtExitFunc(func, arg); } } // end namespace internal } // end namespace runtime -} - - -#if (!_WIN32) - // "Declared" to the JIT in RuntimeUniverse.h -namespace cling { - namespace runtime { - namespace internal { - struct trigger__cxa_atexit { - ~trigger__cxa_atexit(); - } /*S*/; - trigger__cxa_atexit::~trigger__cxa_atexit() { - if (std::getenv("bar") == (char*)-1) { - llvm::errs() << - "UNEXPECTED cling::runtime::internal::trigger__cxa_atexit\n"; - } - } - } - } -} // namespace cling - -namespace { - cling::runtime::internal::trigger__cxa_atexit ForceTheSymbol; -} - -#endif - - -namespace cling { // FIXME: workaround until JIT supports exceptions jmp_buf* Interpreter::m_JumpBuf; @@ -1078,4 +1044,8 @@ namespace cling { llvm::Module* module = m_IncrParser->getCodeGenerator()->GetModule(); return m_Executor->getAddressOfGlobal(module, SymName, fromJIT); } + + void Interpreter::AddAtExitFunc(void (*Func) (void*), void* Arg) { + m_Executor->AddAtExitFunc(Func, Arg, getLastTransaction()); + } } // namespace cling