Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialects: (wasm) Add binary encoding interface #2788

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/dialects/wasm/test_wasm_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from xdsl.dialects import wasm


def test_empty_module():
module = wasm.WasmModule()

assert module.wasm() == b"\x00asm\x01\x00\x00\x00"
22 changes: 22 additions & 0 deletions xdsl/dialects/wasm/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Helpers for encoding modules in the `wasm` dialect to the WebAssembly binary format.
"""

import abc
from typing import BinaryIO


class WasmBinaryEncodingContext:
"""
A class to store the state of encoding.
"""


class EncodingException(Exception): ...


class WasmBinaryEncodable(abc.ABC):

@abc.abstractmethod
def encode(self, ctx: WasmBinaryEncodingContext, io: BinaryIO) -> None:
raise NotImplementedError()
21 changes: 20 additions & 1 deletion xdsl/dialects/wasm/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
at the bottom of this file.
"""

from io import BytesIO
from typing import BinaryIO

from typing_extensions import Self

from xdsl.irdl import (
Expand All @@ -18,13 +21,15 @@
from xdsl.parser import Parser
from xdsl.printer import Printer

from .encoding import WasmBinaryEncodable, WasmBinaryEncodingContext

##==------------------------------------------------------------------------==##
# WebAssembly module
##==------------------------------------------------------------------------==##


@irdl_op_definition
class WasmModule(IRDLOperation):
class WasmModule(IRDLOperation, WasmBinaryEncodable):
"""
wasm> WebAssembly programs are organized into modules, which are the unit of
deployment, loading, and compilation. A module collects definitions for
Expand Down Expand Up @@ -60,6 +65,20 @@ def print(self, printer: Printer):
printer.print_string(" attributes ")
printer.print_attr_dict(attr_dict)

def encode(self, ctx: WasmBinaryEncodingContext, io: BinaryIO) -> None:
# https://webassembly.github.io/spec/core/binary/modules.html#binary-module
magic = b"\x00asm"
version = b"\x01\x00\x00\x00"
io.write(magic)
io.write(version)

def wasm(self) -> bytes:
ctx = WasmBinaryEncodingContext()
io = BytesIO()
self.encode(ctx, io)
res = io.getvalue()
return res


"""
--- Licensing Terms for the WebAssembly Specification
Expand Down
Loading