diff --git a/README.md b/README.md index 33d6b32..b44b5e5 100644 --- a/README.md +++ b/README.md @@ -21,17 +21,19 @@ Windows, Mac (Intel & ARM) and Linux (manylinux, musllinux) are supported. ## How To Use ```py +import binaryen +from binaryen.type import Int32, TypeNone + # Equivalent python function def add(x, y): return x + y -func_inputs = binaryen.type.create([Int32, Int32]) mod = binaryen.Module() mod.add_function( b"add", - func_inputs, + binaryen.type.create([Int32, Int32]), Int32, [Int32], mod.block( @@ -51,6 +53,9 @@ mod.add_function( ), ) +if not mod.validate(): + raise RuntimeError("Invalid module!") + mod.add_function_export(b"add", b"add") mod.optimize() diff --git a/binaryen/__functionref.py b/binaryen/__functionref.py index e2da6ba..73c7fd5 100644 --- a/binaryen/__functionref.py +++ b/binaryen/__functionref.py @@ -10,13 +10,13 @@ def get_name(self) -> str: return lib.BinaryenFunctionGetName(self.ref) def get_num_vars(self) -> int: - return lib.BinaryenGetNumVars(self.ref) + return lib.BinaryenFunctionGetNumVars(self.ref) def get_var(self, index: int) -> BinaryenType: return lib.BinaryenFunctionGetVar(self.ref, index) - # def add_var(self, type: Type) -> int: - # return lib.BinaryenFunctionAddVar(self.ref, type) + def add_var(self, type: BinaryenType) -> int: + return lib.BinaryenFunctionAddVar(self.ref, type) def get_num_locals(self) -> int: return lib.BinaryenFunctionGetNumLocals(self.ref) diff --git a/binaryen/__global.py b/binaryen/__global.py new file mode 100644 index 0000000..2daa391 --- /dev/null +++ b/binaryen/__global.py @@ -0,0 +1,19 @@ +from .__expression import Expression +from ._binaryen import lib + + +class Global: + def __init__(self, ref): + self.ref = ref + + def get_name(self): + return str(lib.BinaryenGlobalGetName(self.ref)) + + def get_type(self): + return lib.BinaryenGlobalGetType(self.ref) + + def is_mutable(self): + return bool(lib.BinaryenGlobalIsMutable(self.ref)) + + def get_init_expr(self): + return Expression(lib.BinaryenGlobalGetInitExpr(self.ref)) diff --git a/binaryen/__module.py b/binaryen/__module.py index f57f1f7..3ec9924 100644 --- a/binaryen/__module.py +++ b/binaryen/__module.py @@ -6,14 +6,9 @@ from .__expression import Block, Expression from .__feature import Feature from .__functionref import FunctionRef +from .__global import Global from ._binaryen import ffi, lib -from .internals import ( - BinaryenGlobalRef, - BinaryenHeapType, - BinaryenLiteral, - BinaryenOp, - BinaryenType, -) +from .internals import BinaryenHeapType, BinaryenLiteral, BinaryenOp, BinaryenType from .type import TypeNone type BinaryenExportRef = Any @@ -60,10 +55,10 @@ def i32(self, value: int): def i64(self, value: int): return self.const(literal.int64(value)) - def f32(self, value: int): + def f32(self, value: float): return self.const(literal.float32(value)) - def f64(self, value: int): + def f64(self, value: float): return self.const(literal.float64(value)) def block( @@ -385,11 +380,25 @@ def add_function_export( def add_global( self, name: bytes, global_type: BinaryenType, mutable: bool, init: Expression - ) -> BinaryenGlobalRef: + ): ref = lib.BinaryenAddGlobal(self.ref, name, global_type, mutable, init.ref) - return ref + return Global(ref) + + def get_global(self, name: bytes): + ref = lib.BinaryenGetGlobal(self.ref, name) + if ref == ffi.NULL: + return None + return Global(ref) + + def remove_global(self, name: bytes): + lib.BinaryenRemoveGlobal(self.ref, name) + + def get_num_globals(self): + return int(lib.BinaryenGetNumGlobals(self.ref)) - # TODO: GetGlobal, RemoveGlobal, GetNumGlobals, GetGlobalByIndex + def get_global_by_index(self, index: int): + ref = lib.BinaryenGetGlobalByIndex(self.ref, index) + return Global(ref) # TODO: AddTag, GetTag, RemoveTag diff --git a/binaryen/internals.py b/binaryen/internals.py index d82f5d1..1da821a 100644 --- a/binaryen/internals.py +++ b/binaryen/internals.py @@ -22,4 +22,3 @@ class __BaseType: BinaryenExternalKind = __NewType("BinaryenExternalKind", __BaseType) BinaryenOp = __NewType("BinaryenOp", __BaseType) BinaryenLiteral = __NewType("BinaryenLiteral", __BaseType) -BinaryenGlobalRef = __NewType("BinaryenGlobalRef", __BaseType) diff --git a/binaryen/type/__init__.py b/binaryen/type/__init__.py index 61fb165..90eec05 100644 --- a/binaryen/type/__init__.py +++ b/binaryen/type/__init__.py @@ -6,6 +6,7 @@ from .._binaryen import ffi as __ffi from .._binaryen import lib as __lib from . import array_type, heap_type, signature_type, struct_type +from .to_str import to_str # These "types" are actually integers representing the type # e.g. none = 0, i32 = 2 etc. diff --git a/binaryen/type/to_str.py b/binaryen/type/to_str.py new file mode 100644 index 0000000..fecd5d5 --- /dev/null +++ b/binaryen/type/to_str.py @@ -0,0 +1,29 @@ +from .._binaryen import lib +from ..internals import BinaryenType + + +def to_str(type: BinaryenType) -> str: + return { + lib.BinaryenTypeNone(): "None", + lib.BinaryenTypeInt32(): "Int32", + lib.BinaryenTypeInt64(): "Int64", + lib.BinaryenTypeFloat32(): "Float32", + lib.BinaryenTypeFloat64(): "Float64", + lib.BinaryenTypeVec128(): "Vec128", + lib.BinaryenTypeFuncref(): "Funcref", + lib.BinaryenTypeExternref(): "Externref", + lib.BinaryenTypeAnyref(): "Anyref", + lib.BinaryenTypeEqref(): "Eqref", + lib.BinaryenTypeI31ref(): "I31ref", + lib.BinaryenTypeStructref(): "Structref", + lib.BinaryenTypeArrayref(): "Arrayref", + lib.BinaryenTypeStringref(): "Stringref", + lib.BinaryenTypeStringviewWTF8(): "StringviewWTF8", + lib.BinaryenTypeStringviewWTF16(): "StringviewWTF16", + lib.BinaryenTypeStringviewIter(): "StringviewIter", + lib.BinaryenTypeNullref(): "Nullref", + lib.BinaryenTypeNullExternref(): "NullExternref", + lib.BinaryenTypeNullFuncref(): "NullFuncref", + lib.BinaryenTypeUnreachable(): "Unreachable", + lib.BinaryenTypeAuto(): "Auto", + }[type] diff --git a/examples/unary.py b/examples/unary.py new file mode 100644 index 0000000..22c4d02 --- /dev/null +++ b/examples/unary.py @@ -0,0 +1,21 @@ +import binaryen + +mod = binaryen.Module() + +# f_sub = mod.unary(binaryen.operations.NegFloat32(), mod.f32(6.7)) +f_sub = mod.unary( + binaryen.operations.NegFloat32(), mod.local_get(0, binaryen.type.Float32) +) + +mod.add_function(b"float_test", binaryen.type.Float32, binaryen.type.Float32, [], f_sub) +mod.add_function_export(b"float_test", b"float_test") + +# i_sub = mod.binary(binaryen.operations.SubInt32(), mod.i32(0), mod.i32(10)) +# i_sub = mod.binary(binaryen.operations.S, mod.i32(0), mod.local_get(0, binaryen.type.Int32)) + +mod.add_function(b"int_test", binaryen.type.Int32, binaryen.type.Int32, [], i_sub) +mod.add_function_export(b"int_test", b"int_test") + +mod.optimize() + +mod.print() diff --git a/pyproject.toml b/pyproject.toml index e6ee8ad..bf2053a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "binaryen.py" -version = "117.0.0" +version = "117.1.0" description = "A Python wrapper for Binaryen" authors = [{ name = "Jonathan Hargreaves", email = "jhargreaves189@gmail.com" }] readme = "README.md" diff --git a/binaryen/binaryen_build.py b/scripts/binaryen_build.py similarity index 100% rename from binaryen/binaryen_build.py rename to scripts/binaryen_build.py diff --git a/setup.py b/setup.py index a667393..26d2480 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ from setuptools import setup setup( - cffi_modules=["binaryen/binaryen_build.py:ffibuilder"], + cffi_modules=["scripts/binaryen_build.py:ffibuilder"], )