Skip to content

Commit

Permalink
API refactoring: add multiple interpreter instance support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnimuc committed Oct 27, 2024
1 parent 3c6ce53 commit 0cf2bf1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
1 change: 1 addition & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace Cpp {
class JitCall {
public:
friend CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t);
friend JitCall MakeFunctionCallable(TInterp_t I, TCppConstFunction_t func);
enum Kind : char {
kUnknown = 0,
kGenericCall,
Expand Down
90 changes: 58 additions & 32 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,13 +1207,11 @@ namespace Cpp {
return 0;
}

intptr_t GetVariableOffset(TCppScope_t var)
{
if (!var)
intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D) {
if (!D)
return 0;

auto *D = (Decl *) var;
auto &C = getASTContext();
auto& C = I.getSema().getASTContext();

if (auto* FD = llvm::dyn_cast<FieldDecl>(D)) {
const clang::RecordDecl* RD = FD->getParent();
Expand Down Expand Up @@ -1242,7 +1240,7 @@ namespace Cpp {
compat::maybeMangleDeclName(GD, mangledName);
void* address = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
mangledName.c_str());
auto &I = getInterp();

if (!address)
address = I.getAddressOfGlobal(GD);
if (!address) {
Expand Down Expand Up @@ -1292,6 +1290,11 @@ namespace Cpp {
return 0;
}

intptr_t GetVariableOffset(TCppScope_t var) {
auto* D = static_cast<Decl*>(var);
return GetVariableOffset(getInterp(), D);
}

// Check if the Access Specifier of the variable matches the provided value.
bool CheckVariableAccess(TCppScope_t var, AccessSpecifier AS)
{
Expand Down Expand Up @@ -1453,6 +1456,10 @@ namespace Cpp {
}
}

QualType GetBuiltinType(const std::string& name, Sema& sema) {
return findBuiltinType(name, sema.getASTContext());
}

TCppType_t GetType(const std::string &name) {
QualType builtin = findBuiltinType(name, getASTContext());
if (!builtin.isNull())
Expand Down Expand Up @@ -2574,26 +2581,31 @@ namespace Cpp {
} // namespace
// End of JitCall Helper Functions

CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func) {
auto* D = (const clang::Decl*)func;
if (!D)
return {};
JitCall MakeFunctionCallable(TInterp_t I, TCppConstFunction_t func) {
auto* D = (const clang::Decl*)func;
if (!D)
return {};

auto& I = getInterp();
// FIXME: Unify with make_wrapper.
if (auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) {
if (auto Wrapper = make_dtor_wrapper(I, Dtor->getParent()))
return {JitCall::kDestructorCall, Wrapper, Dtor};
auto* interp = static_cast<compat::Interpreter*>(I);

// FIXME: Unify with make_wrapper.
if (const auto* Dtor = dyn_cast<CXXDestructorDecl>(D)) {
if (auto Wrapper = make_dtor_wrapper(*interp, Dtor->getParent()))
return {JitCall::kDestructorCall, Wrapper, Dtor};
// FIXME: else error we failed to compile the wrapper.
return {};
}

if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D))) {
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
}
// FIXME: else error we failed to compile the wrapper.
return {};
}

if (auto Wrapper = make_wrapper(I, cast<FunctionDecl>(D))) {
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func) {
return MakeFunctionCallable(&getInterp(), func);
}
// FIXME: else error we failed to compile the wrapper.
return {};
}

namespace {
static std::string MakeResourcesPath() {
Expand Down Expand Up @@ -2842,8 +2854,9 @@ namespace Cpp {
return DLM->searchLibrariesForSymbol(mangled_name, search_system);
}

bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
uint64_t address) {
bool InsertOrReplaceJitSymbol(compat::Interpreter& I,
const char* linker_mangled_name,
uint64_t address) {
// FIXME: This approach is problematic since we could replace a symbol
// whose address was already taken by clients.
//
Expand All @@ -2869,7 +2882,6 @@ namespace Cpp {
using namespace llvm;
using namespace llvm::orc;

auto& I = getInterp();
auto Symbol = compat::getSymbolAddress(I, linker_mangled_name);
llvm::orc::LLJIT& Jit = *compat::getExecutionEngine(I);
llvm::orc::ExecutionSession& ES = Jit.getExecutionSession();
Expand Down Expand Up @@ -2930,6 +2942,12 @@ namespace Cpp {
return false;
}

bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
uint64_t address) {
return InsertOrReplaceJitSymbol(getInterp(), linker_mangled_name,
address);
}

std::string ObjToString(const char *type, void *obj) {
return getInterp().toString(type, obj);
}
Expand Down Expand Up @@ -2979,9 +2997,8 @@ namespace Cpp {
// return C.getElaboratedType(ETK_None, NS, TT);
}

static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
ArrayRef<TemplateArgument> TemplateArgs,
Sema& S) {
Decl* InstantiateTemplate(TemplateDecl* TemplateD,
ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
// Create a list of template arguments.
TemplateArgumentListInfo TLI{};
for (auto TA : TemplateArgs)
Expand Down Expand Up @@ -3241,15 +3258,15 @@ namespace Cpp {
}

// FIXME: Add optional arguments to the operator new.
TCppObject_t Construct(TCppScope_t scope,
void* arena/*=nullptr*/) {
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
void* arena /*=nullptr*/) {
auto* Class = (Decl*) scope;
// FIXME: Diagnose.
if (!HasDefaultConstructor(Class))
return nullptr;

auto* const Ctor = GetDefaultConstructor(Class);
if (JitCall JC = MakeFunctionCallable(Ctor)) {
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
if (arena) {
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
return arena;
Expand All @@ -3262,15 +3279,24 @@ namespace Cpp {
return nullptr;
}

void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
Decl* Class = (Decl*)scope;
if (auto wrapper = make_dtor_wrapper(getInterp(), Class)) {
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
return Construct(getInterp(), scope, arena);
}

void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
bool withFree) {
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
(*wrapper)(This, /*nary=*/0, withFree);
return;
}
// FIXME: Diagnose.
}

void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
auto* Class = static_cast<Decl*>(scope);
Destruct(getInterp(), This, Class, withFree);
}

class StreamCaptureInfo {
std::unique_ptr<FILE, decltype(std::fclose)*> m_TempFile;
int m_FD = -1;
Expand Down

0 comments on commit 0cf2bf1

Please sign in to comment.