Skip to content

Commit

Permalink
feat+chore: add PymBinary class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Sep 5, 2024
1 parent 3ab708c commit 48cedd6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 10 deletions.
3 changes: 1 addition & 2 deletions example/parse_ir_assmebly.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
assert insr.parent == bb
assert insr.parent is not bb
assert insr.kind == core.ValueKind.Instruction
for i in range(insr.operands_num):
operand = insr.get_operand(0)
for operand in insr.operands:
print(f'\t\t\tOperand | name: "{operand.name}" | type: "{operand.type}"')

print("\n")
Expand Down
8 changes: 7 additions & 1 deletion src/llvm/Core/miscClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void bindOtherClasses(nb::module_ &m) {
"This effectively advances the iterator. It returns NULL if you are on"
"the final use and no more are available.")
.def_prop_ro("user",
[](PymUse &u) { return PymUser(LLVMGetUser(u.get())); },
[](PymUse &u) { return PymValueAuto(LLVMGetUser(u.get())); },
"Obtain the user value for a user.\n",
"The returned value corresponds to a llvm::User type.")
.def_prop_ro("used_value",
Expand All @@ -1082,6 +1082,12 @@ void bindOtherClasses(nb::module_ &m) {
Attribute attr = unwrap(self.get());
return fmt::format("<Attribute str='{}'>", attr.getAsString());
})
.def("__str__",
[](PymAttribute &self) {
using namespace llvm;
Attribute attr = unwrap(self.get());
return attr.getAsString();
})
.def_prop_ro("is_enum",
[](PymAttribute &attr) {
return LLVMIsEnumAttribute(attr.get()) != 0;
Expand Down
20 changes: 15 additions & 5 deletions src/llvm/Core/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,20 @@ void bindValueClasses(nb::module_ &m) {
[](PymUser &self) {
return gen_value_repr("User", self);
})
.def_prop_ro("operands_num",
[](PymUser &u) {
return LLVMGetNumOperands(u.get());
})
.def_prop_ro("operands", // c++ extension
[](PymUser &self) {
using namespace llvm;
User *user = unwrap<User>(self.get());
std::vector<PymValue*> pymOps;
for (const auto &op : user->operands()) {
pymOps.emplace_back(PymValueAuto(wrap(op)));
}
return pymOps;
})
.def("get_operand",
[](PymUser &u, unsigned index) {
return PymValueAuto(LLVMGetOperand(u.get(), index));
Expand All @@ -1256,11 +1270,7 @@ void bindValueClasses(nb::module_ &m) {
return LLVMSetOperand(u.get(), index, v.get());
},
"index"_a, "value"_a,
"Set an operand at a specific index")
.def_prop_ro("operands_num",
[](PymUser &u) {
return LLVMGetNumOperands(u.get());
});
"Set an operand at a specific index");

ConstantClass
.def("__repr__",
Expand Down
36 changes: 36 additions & 0 deletions src/llvm/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
#include <llvm-c/Object.h>
#include <stdexcept>
#include "types_priv.h"
#include "utils_priv.h"

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

void populateObject(nb::module_ &m) {

auto BinaryClass =
nb::class_<PymBinary,
PymLLVMObject<PymBinary, LLVMBinaryRef>>
(m, "Binary", "Binary");


nb::enum_<LLVMBinaryType>(m, "BinaryType", "BinaryType")
.value("LLVMBinaryTypeArchive",
LLVMBinaryType::LLVMBinaryTypeArchive,
Expand Down Expand Up @@ -58,4 +66,32 @@ void populateObject(nb::module_ &m) {
.value("LLVMBinaryTypeOffload",
LLVMBinaryType::LLVMBinaryTypeOffload,
"Offloading fatbinary.");


BinaryClass
.def("__init__",
[](PymBinary *b, PymMemoryBuffer &memBuf, PymContext &cxt) {
char *errorMessage;
auto res = LLVMCreateBinary(memBuf.get(), cxt.get(), &errorMessage);
auto success = res != NULL;
THROW_ERORR_DISPOSE_MESSAGE_IF_FALSE(success, errorMessage);
new (b) PymBinary(res);
},
"mem_buf"_a, "context"_a.none(),
"Create a binary file from the given memory buffer.\n"
"The exact type of the binary file will be inferred automatically, and the"
"appropriate implementation selected. The context may be NULL except if"
"the resulting file is an LLVM IR file.")
.def_prop_ro("memory_buffer",
[](PymBinary &self) {
return PymMemoryBuffer(LLVMBinaryCopyMemoryBuffer(self.get()));
},
"Retrieves a copy of the memory buffer associated with this object file.\n\n"
"The returned buffer is merely a shallow copy and does not own the actual"
"backing buffer of the binary.")
.def_prop_ro("type",
[](PymBinary &self) {
return LLVMBinaryGetType(self.get());
},
"Retrieve the specific type of a binary.");
}
9 changes: 7 additions & 2 deletions src/llvm/types_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "types_priv/PymTargetMachineOptions.h"
#include "types_priv/PymLLVMObject.h"
#include "types_priv/PymDisasmContext.h"
#include "types_priv/PymBinary.h"


#define DEFINE_PY_WRAPPER_CLASS(ClassName, UnderlyingType) \
Expand Down Expand Up @@ -297,7 +298,10 @@
\
BIND_PYLLVMOBJECT_(PymDisasmContext, LLVMDisasmContextRef, PymDisasmContextObject) \
\
BIND_PYLLVMOBJECT_(PymTargetLibraryInfo, LLVMTargetLibraryInfoRef, PymTargetLibraryInfoObject)
BIND_PYLLVMOBJECT_(PymTargetLibraryInfo, LLVMTargetLibraryInfoRef, PymTargetLibraryInfoObject) \
\
BIND_PYLLVMOBJECT_(PymBinary, LLVMBinaryRef, PyBinaryObject)



// Core --------------------------------------------------------
Expand Down Expand Up @@ -380,11 +384,12 @@ DEFINE_ITERATOR_CLASS(PymFunctionIterator, PymFunction, LLVMGetNextFunction)
DEFINE_PY_WRAPPER_CLASS(PymTargetLibraryInfo, LLVMTargetLibraryInfoRef)


// TargetMachine ---------------------------------------------------------
// TargetMachine ---------------------------------------------------------------

DEFINE_PY_WRAPPER_CLASS(PymTarget, LLVMTargetRef)
DEFINE_ITERATOR_CLASS(PymTargetIterator, PymTarget, LLVMGetNextTarget)

// Object ----------------------------------------------------------------------

// TODO split the file to boost the compilation process

Expand Down
12 changes: 12 additions & 0 deletions src/llvm/types_priv/PymBinary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "PymBinary.h"

PymBinary::PymBinary(LLVMBinaryRef bin)
: obj(get_shared_obj(bin)) { }

LLVMBinaryRef PymBinary::get() const {
return obj.get();
}

SHARED_POINTER_IMPL(PymBinary, LLVMBinaryRef, LLVMOpaqueBinary, LLVMDisposeBinary)


21 changes: 21 additions & 0 deletions src/llvm/types_priv/PymBinary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef LLVMPYM_TYPES_PRIV_PYMBINARY_H
#define LLVMPYM_TYPES_PRIV_PYMBINARY_H

#include <llvm-c/Object.h>
#include <memory>
#include <unordered_map>
#include <mutex>
#include "PymLLVMObject.h"
#include "utils.h"

class PymBinary : public PymLLVMObject<PymBinary, LLVMBinaryRef> {
public:
explicit PymBinary(LLVMBinaryRef bin);
LLVMBinaryRef get() const;

private:
SHARED_POINTER_DEF(LLVMBinaryRef, LLVMOpaqueBinary);
};


#endif

0 comments on commit 48cedd6

Please sign in to comment.