diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -28,7 +28,7 @@ namespace orc { class LLJIT; class ThreadSafeContext; -} +} // namespace orc } // namespace llvm namespace clang { @@ -52,12 +52,15 @@ Interpreter(std::unique_ptr CI, llvm::Error &Err); + llvm::Error CreateExecutor(); + public: ~Interpreter(); static llvm::Expected> create(std::unique_ptr CI); const CompilerInstance *getCompilerInstance() const; - const llvm::orc::LLJIT *getExecutionEngine() const; + llvm::Expected GetExecutionEngine(); + llvm::Expected Parse(llvm::StringRef Code); llvm::Error Execute(PartialTranslationUnit &T); llvm::Error ParseAndExecute(llvm::StringRef Code) { @@ -72,6 +75,9 @@ /// Undo N previous incremental inputs. llvm::Error Undo(unsigned N = 1); + /// Link a dyanmic library + llvm::Error LoadDynamicLibrary(const char *name); + /// \returns the \c JITTargetAddress of a \c GlobalDecl. This interface uses /// the CodeGenModule's internal mangling cache to avoid recomputing the /// mangled name. diff --git a/clang/lib/Interpreter/IncrementalExecutor.h b/clang/lib/Interpreter/IncrementalExecutor.h --- a/clang/lib/Interpreter/IncrementalExecutor.h +++ b/clang/lib/Interpreter/IncrementalExecutor.h @@ -53,7 +53,8 @@ llvm::Error cleanUp(); llvm::Expected getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const; - llvm::orc::LLJIT *getExecutionEngine() const { return Jit.get(); } + + llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; } }; } // end namespace clang diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -29,10 +29,13 @@ #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Lex/PreprocessorOptions.h" +#include "llvm/ExecutionEngine/Orc/LLJIT.h" #include "llvm/IR/Module.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Host.h" +#include + using namespace clang; // FIXME: Figure out how to unify with namespace init_convenience from @@ -203,10 +206,13 @@ return IncrParser->getCI(); } -const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const { - if (IncrExecutor) - return IncrExecutor->getExecutionEngine(); - return nullptr; +llvm::Expected Interpreter::GetExecutionEngine() { + if (!IncrExecutor) { + if (auto Err = CreateExecutor()) + return Err; + } + + return IncrExecutor->GetExecutionEngine(); } llvm::Expected @@ -214,14 +220,21 @@ return IncrParser->Parse(Code); } +llvm::Error Interpreter::CreateExecutor() { + const clang::TargetInfo &TI = + getCompilerInstance()->getASTContext().getTargetInfo(); + llvm::Error Err = llvm::Error::success(); + auto Executor = std::make_unique(*TSCtx, Err, TI); + if (!Err) + IncrExecutor = std::move(Executor); + + return Err; +} + llvm::Error Interpreter::Execute(PartialTranslationUnit &T) { assert(T.TheModule); if (!IncrExecutor) { - const clang::TargetInfo &TI = - getCompilerInstance()->getASTContext().getTargetInfo(); - llvm::Error Err = llvm::Error::success(); - IncrExecutor = std::make_unique(*TSCtx, Err, TI); - + auto Err = CreateExecutor(); if (Err) return Err; } @@ -283,3 +296,30 @@ } return llvm::Error::success(); } + +llvm::Error Interpreter::LoadDynamicLibrary(const char *name) { + auto EE = GetExecutionEngine(); + if (!EE) + return EE.takeError(); + + auto &DL = EE->getDataLayout(); + + std::stringstream filename; +#if defined(LLVM_ON_UNIX) + filename << "lib"; +#endif + filename << name; +#if defined(_WIN32) + filename << ".dll"; +#elif defined(LLVM_ON_UNIX) + filename << ".so"; +#endif + + if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load( + filename.str().c_str(), DL.getGlobalPrefix())) + EE->getMainJITDylib().addGenerator(std::move(*DLSG)); + else + return DLSG.takeError(); + + return llvm::Error::success(); +} diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp --- a/clang/tools/clang-repl/ClangRepl.cpp +++ b/clang/tools/clang-repl/ClangRepl.cpp @@ -123,6 +123,13 @@ } continue; } + if (Line->rfind(R"(%lib )", 0) == 0) { + if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) { + llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: "); + HasError = true; + } + continue; + } if (auto Err = Interp->ParseAndExecute(*Line)) { llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");