From 4eabf4682161aedaa16c3df339c0875fab7c2081 Mon Sep 17 00:00:00 2001 From: Gnimuc Date: Thu, 6 Jun 2024 00:30:34 +0900 Subject: [PATCH] fix-up --- include/clang-c/CXCppInterOp.h | 20 ++++++++- lib/Interpreter/CXCppInterOp.cpp | 69 ++++++++++++++++++++------------ lib/Interpreter/CppInterOp.cpp | 21 +++++++--- 3 files changed, 77 insertions(+), 33 deletions(-) diff --git a/include/clang-c/CXCppInterOp.h b/include/clang-c/CXCppInterOp.h index cd71535fd..09dba3024 100644 --- a/include/clang-c/CXCppInterOp.h +++ b/include/clang-c/CXCppInterOp.h @@ -286,7 +286,25 @@ typedef void* CXConstFunction; CXJitCall clang_jitcall_makeFunctionCallable(CXInterpreter I, CXConstFunction func); +intptr_t clang_jitcall_getVariableOffset(CXInterpreter I, CXScope var); + +/** + * Inserts or replaces a symbol in the JIT with the one provided. This is useful + * for providing our own implementations of facilities such as printf. + * + * \param I The interpreter. + * + * \param linker_mangled_name The name of the symbol to be inserted or replaced. + * + * \param address The new address of the symbol. + * + * \returns true on failure. + */ +bool clang_jitcall_insertOrReplaceJitSymbol(CXInterpreter I, + const char* linker_mangled_name, + uint64_t address); + LLVM_CLANG_C_EXTERN_C_END #endif // LLVM_CLANG_C_CXCPPINTEROP_H -// NOLINTEND() \ No newline at end of file + // NOLINTEND() \ No newline at end of file diff --git a/lib/Interpreter/CXCppInterOp.cpp b/lib/Interpreter/CXCppInterOp.cpp index 1156c04e0..63481a634 100644 --- a/lib/Interpreter/CXCppInterOp.cpp +++ b/lib/Interpreter/CXCppInterOp.cpp @@ -1,5 +1,6 @@ #include "clang-c/CXCppInterOp.h" #include "Compatibility.h" +#include "CppInterOpInterpreter.h" #include "clang/Interpreter/CppInterOp.h" #include "llvm/ExecutionEngine/Orc/LLJIT.h" #include @@ -16,19 +17,6 @@ CXString createCXString(const std::string& str) { return Str; } -CXStringSet* createCXStringSet(const std::vector& strs) { - CXStringSet* Set = new CXStringSet; - Set->Count = strs.size(); - Set->Strings = new CXString[Set->Count]; - for (unsigned int i = 0; i < Set->Count; ++i) { - char* Str = new char[strs[i].length() + 1]; - std::strcpy(Str, strs[i].c_str()); - Set->Strings[i].data = Str && Str[0] == '\0' ? "" : Str; - Set->Strings[i].private_flags = 1; // CXS_Malloc - } - return Set; -} - CXInterpreter clang_createInterpreter(const char* const* argv, int argc) { auto I = std::make_unique(argc, argv); return I.release(); @@ -40,12 +28,18 @@ void clang_interpreter_dispose(CXInterpreter I) { CXCompilerInstance clang_interpreter_getCompilerInstance(CXInterpreter I) { return const_cast( - static_cast(I)->getCompilerInstance()); + static_cast(I)->getCI()); } LLVMOrcLLJITRef clang_interpreter_getExecutionEngine(CXInterpreter I) { +#ifdef USE_CLING + return nullptr; +#else return reinterpret_cast(const_cast( - static_cast(I)->getExecutionEngine())); + static_cast(I) + ->getExecutionEngine())); // NOLINT(cppcoreguidelines-pro-type-const-cast, + // cppcoreguidelines-pro-type-reinterpret-cast) +#endif } void clang_interpreter_addSearchPath(CXInterpreter I, const char* dir, @@ -68,7 +62,7 @@ const char* clang_interpreter_getResourceDir(CXInterpreter I) { enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code, bool silent) { - auto interp = static_cast(I); + auto* interp = static_cast(I); auto& diag = interp->getSema().getDiagnostics(); bool is_silent_old = diag.getSuppressAllDiagnostics(); @@ -79,16 +73,16 @@ enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code, if (result) return CXError_Failure; - else - return CXError_Success; + + return CXError_Success; } enum CXErrorCode clang_interpreter_process(CXInterpreter I, const char* code) { auto result = static_cast(I)->process(code); if (result) return CXError_Failure; - else - return CXError_Success; + + return CXError_Success; } CXValue clang_createValue() { @@ -103,25 +97,27 @@ CXValue clang_createValue() { void clang_value_dispose(CXValue V) { #ifdef USE_CLING - delete static_cast(V); + delete static_cast( + V); // NOLINT(cppcoreguidelines-owning-memory) #else - delete static_cast(V); + delete static_cast( + V); // NOLINT(cppcoreguidelines-owning-memory) #endif // USE_CLING } enum CXErrorCode clang_interpreter_evaluate(CXInterpreter I, const char* code, CXValue V) { #ifdef USE_CLING - auto val = static_cast(V); + auto* val = static_cast(V); #else - auto val = static_cast(V); + auto* val = static_cast(V); #endif // USE_CLING auto result = static_cast(I)->evaluate(code, *val); if (result) return CXError_Failure; - else - return CXError_Success; + + return CXError_Success; } CXString clang_interpreter_lookupLibrary(CXInterpreter I, @@ -197,3 +193,24 @@ CXJitCall clang_jitcall_makeFunctionCallable(CXInterpreter I, return reinterpret_cast( std::make_unique(J).release()); } + +namespace Cpp { +intptr_t GetVariableOffsetImpl(compat::Interpreter& I, TCppScope_t var); +} + +intptr_t clang_jitcall_getVariableOffset(CXInterpreter I, CXScope var) { + return Cpp::GetVariableOffsetImpl(*static_cast(I), var); +} + +namespace Cpp { +bool InsertOrReplaceJitSymbolImpl(compat::Interpreter& I, + const char* linker_mangled_name, + uint64_t address); +} + +bool clang_jitcall_insertOrReplaceJitSymbol(CXInterpreter I, + const char* linker_mangled_name, + uint64_t address) { + return Cpp::InsertOrReplaceJitSymbolImpl( + *static_cast(I), linker_mangled_name, address); +} \ No newline at end of file diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 269e38f0d..b21122ac0 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -1159,8 +1159,7 @@ namespace Cpp { return 0; } - intptr_t GetVariableOffset(TCppScope_t var) - { + intptr_t GetVariableOffsetImpl(compat::Interpreter& I, TCppScope_t var) { if (!var) return 0; @@ -1177,7 +1176,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) { @@ -1227,6 +1226,10 @@ namespace Cpp { return 0; } + intptr_t GetVariableOffset(TCppScope_t var) { + return GetVariableOffsetImpl(getInterp(), var); + } + // Check if the Access Specifier of the variable matches the provided value. bool CheckVariableAccess(TCppScope_t var, AccessSpecifier AS) { @@ -2765,8 +2768,9 @@ namespace Cpp { return DLM->searchLibrariesForSymbol(mangled_name, search_system); } - bool InsertOrReplaceJitSymbol(const char* linker_mangled_name, - uint64_t address) { + bool InsertOrReplaceJitSymbolImpl(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. // @@ -2792,7 +2796,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(); @@ -2853,6 +2856,12 @@ namespace Cpp { return false; } + bool InsertOrReplaceJitSymbol(const char* linker_mangled_name, + uint64_t address) { + return InsertOrReplaceJitSymbolImpl(getInterp(), linker_mangled_name, + address); + } + std::string ObjToString(const char *type, void *obj) { return getInterp().toString(type, obj); }