Skip to content

Commit

Permalink
feat: add linker module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Aug 10, 2024
1 parent f421ba3 commit 70c3287
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ nanobind_add_stub(
DEPENDS llvmpym_ext
)

nanobind_add_stub(
llvmpym_ext_stub_linker
MODULE llvmpym_ext.linker
OUTPUT linker.pyi
PYTHON_PATH $<TARGET_FILE_DIR:llvmpym_ext>
DEPENDS llvmpym_ext
)


# Install directive for scikit-build-core
install(TARGETS llvmpym_ext LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME})
Expand All @@ -246,5 +254,6 @@ install(FILES
${CMAKE_BINARY_DIR}/target_machine.pyi
${CMAKE_BINARY_DIR}/bit_reader.pyi
${CMAKE_BINARY_DIR}/disassembler.pyi

${CMAKE_BINARY_DIR}/linker.pyi

DESTINATION ${SKBUILD_PROJECT_NAME}/llvmpym_ext)
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Status
"``ErrorHandling.h``", "``error_handling``"
"``ExecutionEngine``", ""
"``IRReader.h``", "``core``"
"``Linker.h``", ""
"``Linker.h``", "``linker``"
"``LLJIT.h``", ""
"``LLJITUtils.h``", ""
"``lto.h``", ""
Expand Down
13 changes: 12 additions & 1 deletion src/llvm/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ void populateDisassembler(nb::module_ &m) {
// DisasmContextClass
// .def("__init__",
// [](PymDisasmContext *dc, const char *tripleName, nb::any DisInfo,
// int tagType, LLVMO));
// int tagType,
// std::function<int(void *, uint64_t, uint64_t,
// uint64_t, uint64_t,
// int, void *)> getOpInfo,
// std::function<const char*>(void *,
// uint64_t,
// uint64_t *,
// uint64_t,
// const char **)
// ) {

// });
}

3 changes: 0 additions & 3 deletions src/llvm/ErrorHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
namespace nb = nanobind;
using namespace nb::literals;

namespace nb = nanobind;
using namespace nb::literals;

void populateErrorHandling(nb::module_ &m) {
m.def("install_fatal_error_handler",
[](std::function<void(const char*)> handler) {
Expand Down
29 changes: 29 additions & 0 deletions src/llvm/Linker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Linker.h"

#include <nanobind/nanobind.h>
#include <llvm-c/Linker.h>
#include "types_priv.h"

namespace nb = nanobind;
using namespace nb::literals;

void populateLinker(nanobind::module_ &m) {
nb::enum_<LLVMLinkerMode>(m, "LinkerMode" "LinkerMode")
// LLVMLinkerPreserveSource_Removed is deprecated
.value("DestroySource", LLVMLinkerMode::LLVMLinkerDestroySource,
"This is the default behavior.");

// TODO customize (if error, throw an runtime error containing the reason. Just
// like what llvmlite does)
m.def("link_module",
[](PymModule dest, PymModule src) {
return LLVMLinkModules2(dest.get(), src.get()) != 0;
},
"dest"_a, "src"_a,
"Links the source module into the destination module. The source module is"
"destroyed.\n"
"The return value is true if an error occurred, false otherwise.\n"
"Use the diagnostic handler to get any diagnostic message.");
}


9 changes: 9 additions & 0 deletions src/llvm/Linker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LINKER_H
#define LINKER_H

#include <nanobind/nanobind.h>

void populateLinker(nanobind::module_ &m);


#endif
1 change: 1 addition & 0 deletions src/llvmpym/linker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .llvmpym_ext.linker import *
5 changes: 5 additions & 0 deletions src/llvmpym_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/TargetMachine.h"
#include "llvm/Disassembler.h"
#include "llvm/BitReader.h"
#include "llvm/Linker.h"

namespace nb = nanobind;
using namespace nb::literals;
Expand Down Expand Up @@ -43,7 +44,11 @@ NB_MODULE(llvmpym_ext, m) {
auto bitReaderModule = m.def_submodule("bit_reader", "bit_reader");
populateBitReader(bitReaderModule);

// NOTE currently not implemented
auto disassemblerModule = m.def_submodule("disassembler", "disassembler");
populateDisassembler(disassemblerModule);

auto linkerModule = m.def_submodule("linker", "linker");
populateLinker(linkerModule);

}

0 comments on commit 70c3287

Please sign in to comment.