aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Interpreter/Interpreter.cpp
diff options
context:
space:
mode:
authorAnubhab Ghosh <anubhabghosh.me@gmail.com>2023-01-15 20:40:44 +0530
committerAnubhab Ghosh <anubhabghosh.me@gmail.com>2023-03-29 08:04:50 +0530
commitd978730d8e2c10c76867b83bec2f1143d895ee7d (patch)
treedaccd01a5112b6fbd3dcf036df61490a58d2fa55 /clang/lib/Interpreter/Interpreter.cpp
parent504df554d2ccd86b1d11d25a8e20134f2ad3a43f (diff)
downloadllvm-d978730d8e2c10c76867b83bec2f1143d895ee7d.zip
llvm-d978730d8e2c10c76867b83bec2f1143d895ee7d.tar.gz
llvm-d978730d8e2c10c76867b83bec2f1143d895ee7d.tar.bz2
[clang-repl] Add a command to load dynamic libraries
This commit adds the %lib <file> command to load a dynamic library to be used by the currently running interpreted code. For example `%lib libSDL2.so`. Differential Revision: https://reviews.llvm.org/D141824
Diffstat (limited to 'clang/lib/Interpreter/Interpreter.cpp')
-rw-r--r--clang/lib/Interpreter/Interpreter.cpp45
1 files changed, 36 insertions, 9 deletions
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 3f0842c..76d5f16 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -29,6 +29,7 @@
#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/TargetParser/Host.h"
@@ -203,10 +204,13 @@ const CompilerInstance *Interpreter::getCompilerInstance() const {
return IncrParser->getCI();
}
-const llvm::orc::LLJIT *Interpreter::getExecutionEngine() const {
- if (IncrExecutor)
- return IncrExecutor->getExecutionEngine();
- return nullptr;
+llvm::Expected<llvm::orc::LLJIT &> Interpreter::getExecutionEngine() {
+ if (!IncrExecutor) {
+ if (auto Err = CreateExecutor())
+ return Err;
+ }
+
+ return IncrExecutor->GetExecutionEngine();
}
llvm::Expected<PartialTranslationUnit &>
@@ -214,14 +218,21 @@ Interpreter::Parse(llvm::StringRef Code) {
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<IncrementalExecutor>(*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<IncrementalExecutor>(*TSCtx, Err, TI);
-
+ auto Err = CreateExecutor();
if (Err)
return Err;
}
@@ -283,3 +294,19 @@ llvm::Error Interpreter::Undo(unsigned N) {
}
return llvm::Error::success();
}
+
+llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
+ auto EE = getExecutionEngine();
+ if (!EE)
+ return EE.takeError();
+
+ auto &DL = EE->getDataLayout();
+
+ if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load(
+ name, DL.getGlobalPrefix()))
+ EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+ else
+ return DLSG.takeError();
+
+ return llvm::Error::success();
+}