Skip to content

Ops

ops

This file contains the definition of the WebAssembly (wasm) dialect.

The purpose of this dialect is to model WebAssembly modules, as per the WebAssembly Specification 2.0.

The paragraphs prefixed by wasm> in the documentation of this dialect are excerpts from the WebAssembly Specification, which is licensed under the terms at the bottom of this file.

WasmModuleOp

Bases: IRDLOperation, WasmBinaryEncodable, WatPrintable

wasm> WebAssembly programs are organized into modules, which are the unit of deployment, loading, and compilation. A module collects definitions for types, functions, tables, memories, and globals. In addition, it can declare imports and exports and provide initialization in the form of data and element segments, or a start function.

Source code in xdsl/dialects/wasm/ops.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@irdl_op_definition
class WasmModuleOp(IRDLOperation, WasmBinaryEncodable, WatPrintable):
    """
    wasm> WebAssembly programs are organized into modules, which are the unit of
    deployment, loading, and compilation. A module collects definitions for
    types, functions, tables, memories, and globals. In addition, it can
    declare imports and exports and provide initialization in the form of
    data and element segments, or a start function.
    """

    name = "wasm.module"

    def __init__(
        self,
    ):
        super().__init__()

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        attr_dict = parser.parse_optional_attr_dict_with_keyword()

        op = cls()

        if attr_dict is not None:
            op.attributes |= attr_dict.data

        return op

    def print(self, printer: Printer):
        attr_dict = self.attributes

        if attr_dict:
            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 print_wat(self, printer: WatPrinter) -> None:
        with printer.in_parens():
            printer.print_string("module")

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

    def wat(self) -> str:
        io = StringIO()
        printer = WatPrinter(io)
        self.print_wat(printer)
        res = io.getvalue()
        return res

name = 'wasm.module' class-attribute instance-attribute

__init__()

Source code in xdsl/dialects/wasm/ops.py
44
45
46
47
def __init__(
    self,
):
    super().__init__()

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/wasm/ops.py
49
50
51
52
53
54
55
56
57
58
@classmethod
def parse(cls, parser: Parser) -> Self:
    attr_dict = parser.parse_optional_attr_dict_with_keyword()

    op = cls()

    if attr_dict is not None:
        op.attributes |= attr_dict.data

    return op

print(printer: Printer)

Source code in xdsl/dialects/wasm/ops.py
60
61
62
63
64
65
def print(self, printer: Printer):
    attr_dict = self.attributes

    if attr_dict:
        printer.print_string(" attributes ")
        printer.print_attr_dict(attr_dict)

encode(ctx: WasmBinaryEncodingContext, io: BinaryIO) -> None

Source code in xdsl/dialects/wasm/ops.py
67
68
69
70
71
72
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)

print_wat(printer: WatPrinter) -> None

Source code in xdsl/dialects/wasm/ops.py
74
75
76
def print_wat(self, printer: WatPrinter) -> None:
    with printer.in_parens():
        printer.print_string("module")

wasm() -> bytes

Source code in xdsl/dialects/wasm/ops.py
78
79
80
81
82
83
def wasm(self) -> bytes:
    ctx = WasmBinaryEncodingContext()
    io = BytesIO()
    self.encode(ctx, io)
    res = io.getvalue()
    return res

wat() -> str

Source code in xdsl/dialects/wasm/ops.py
85
86
87
88
89
90
def wat(self) -> str:
    io = StringIO()
    printer = WatPrinter(io)
    self.print_wat(printer)
    res = io.getvalue()
    return res