From f421ba3248c25abba9de5ba11f2f2afd2b907807 Mon Sep 17 00:00:00 2001 From: Meow King Date: Fri, 9 Aug 2024 19:40:50 +0800 Subject: [PATCH] fix: bind some API that related to a function pointer whose pointed function accepts a void pointer parameter --- docs/source/index.rst | 3 -- src/llvm/Core/enum.cpp | 1 + src/llvm/Core/miscClasses.cpp | 73 ++++++++++++++++++++--------------- src/llvm/Disassembler.cpp | 2 +- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 850cfb8..3f14e16 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -97,9 +97,6 @@ Status "``TargetMachine.h``", "``target_machine``" "", "``utils``" -- All functions accepts or return a void pointer (`void *`) is not bound (including - lambda functions/function pointers). Though very few API accepts or return a void pointer. - Table of contents ----------------- diff --git a/src/llvm/Core/enum.cpp b/src/llvm/Core/enum.cpp index 73de109..f28ff64 100644 --- a/src/llvm/Core/enum.cpp +++ b/src/llvm/Core/enum.cpp @@ -399,4 +399,5 @@ void bindEnums(nb::module_ &m) { .value("All", PymLLVMFastMathFlags::All); // TODO LLVMFastMathFlags + } diff --git a/src/llvm/Core/miscClasses.cpp b/src/llvm/Core/miscClasses.cpp index 07fcb2c..7995e37 100644 --- a/src/llvm/Core/miscClasses.cpp +++ b/src/llvm/Core/miscClasses.cpp @@ -3,9 +3,12 @@ #include #include #include +#include + #include #include #include + #include #include #include @@ -1194,15 +1197,14 @@ void bindOtherClasses(nb::module_ &m) { [](PymDiagnosticInfo &self) { return ""; }) - // .def(nb::init<>()) // NOTE currently no constructor function for python, we'll see - .def_prop_ro("description", - [](PymDiagnosticInfo &d) { - char *diagInfoDesc = LLVMGetDiagInfoDescription(d.get()); - std::string diagInfoDescCopy(diagInfoDesc); - LLVMDisposeMessage(diagInfoDesc); - return diagInfoDescCopy; - }, - "Return a string representation of the DiagnosticInfo.") + .def("__str__", + [](PymDiagnosticInfo &d) { + char *diagInfoDesc = LLVMGetDiagInfoDescription(d.get()); + std::string diagInfoDescCopy(diagInfoDesc); + LLVMDisposeMessage(diagInfoDesc); + return diagInfoDescCopy; + }, + "Return a string representation of the DiagnosticInfo.") .def_prop_ro("severity", [](PymDiagnosticInfo &d) { return LLVMGetDiagInfoSeverity(d.get()); @@ -1256,9 +1258,7 @@ void bindOtherClasses(nb::module_ &m) { "Obtain the global context instance.") .def_prop_ro("diagnostic_context", [](PymContext &c) { - // FIXME The function cannot work correctly (always None) since - // `LLVMContextSetDiagnosticHandler` cannot, which set - // the diagnostic context + // FIXME the returned value is nb::capsule, which is meaningless return LLVMContextGetDiagnosticContext(c.get()); }, "Get the diagnostic context of this context.") @@ -1279,26 +1279,34 @@ void bindOtherClasses(nb::module_ &m) { "will be available in the IR.\n" "This can be used to save memory and runtime, " "especially in release mode.")) - // .def("set_diagnostic_handler", - // [](PymContext &c, - // std::function handler, - // nb::any diagnosticContext){ - // static std::function *callback = - // new std::function(std::move(handler)); - // return LLVMContextSetDiagnosticHandler - // (c.get(), - // [](LLVMDiagnosticInfoRef di, void * v) { - // if (callback) { - // (*callback)(PymDiagnosticInfo(di), nb::any(v)); // FIXME - // } - // }, - // &diagnosticContext); - // }, - // "handler"_a, "diagnostic_context"_a, - // "Set the diagnostic handler for this context.") - // .def("get_diagnostic_handler", FIXME - // [](PymContext &c) { return LLVMContextGetDiagnosticHandler(c.get()); }, - // "Get the diagnostic handler of this context.") + .def("set_diagnostic_handler", + [](PymContext &c, + // NOTE how to use `nb::any` here? + std::function handler, + nb::any diagnosticContext){ + static std::function *callback = + new std::function(std::move(handler)); + return LLVMContextSetDiagnosticHandler + (c.get(), + [](LLVMDiagnosticInfoRef di, void * v) { + if (callback) { + (*callback)(PymDiagnosticInfo(di), nb::capsule(v)); + } + }, + &diagnosticContext); + }, + "handler"_a, "diagnostic_context"_a, + "Set the diagnostic handler for this context.") + .def("get_diagnostic_handler", + [](PymContext &c) { + auto funcPtr = LLVMContextGetDiagnosticHandler(c.get()); + std::function fn = + [funcPtr](PymDiagnosticInfo di, nb::object obj) { + funcPtr(di.get(), &obj); + }; + return fn; + }, + "Get the diagnostic handler of this context.") .def("set_yield_callback", // FIXME [](PymContext &c, LLVMYieldCallback callback, void *opaqueHandle){ return LLVMContextSetYieldCallback(c.get(), callback, opaqueHandle); @@ -1831,3 +1839,4 @@ void bindOtherClasses(nb::module_ &m) { "index"_a, "Returns the metadata for a module flag entry at a specific index."); } + diff --git a/src/llvm/Disassembler.cpp b/src/llvm/Disassembler.cpp index fc1fd12..2389064 100644 --- a/src/llvm/Disassembler.cpp +++ b/src/llvm/Disassembler.cpp @@ -15,6 +15,6 @@ void populateDisassembler(nb::module_ &m) { // DisasmContextClass // .def("__init__", // [](PymDisasmContext *dc, const char *tripleName, nb::any DisInfo, - // int tagType, LLVM)); + // int tagType, LLVMO)); }