Skip to content

Lang types

lang_types

LIST_ELEMENT_TYPE = ListLangBool | ListLangInt module-attribute

ListLangType

Source code in xdsl/frontend/listlang/lang_types.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class ListLangType:
    @staticmethod
    def from_xdsl(xdsl_type: Attribute) -> "ListLangType":
        if isa(xdsl_type, builtin.IntegerType[32]):
            return ListLangInt()
        if isa(xdsl_type, builtin.IntegerType[1]):
            return ListLangBool()
        if isa(xdsl_type, builtin.TensorType[builtin.IntegerType[32]]):
            return ListLangList(ListLangInt())
        if isa(xdsl_type, builtin.TensorType[builtin.IntegerType[1]]):
            return ListLangList(ListLangBool())
        raise ValueError("unknown type")

    def __str__(self) -> str: ...
    def xdsl(self) -> Attribute: ...
    def print(self, builder: Builder, value: SSAValue): ...
    def get_method(self, method: str) -> "Method | None":
        return None

from_xdsl(xdsl_type: Attribute) -> ListLangType staticmethod

Source code in xdsl/frontend/listlang/lang_types.py
13
14
15
16
17
18
19
20
21
22
23
@staticmethod
def from_xdsl(xdsl_type: Attribute) -> "ListLangType":
    if isa(xdsl_type, builtin.IntegerType[32]):
        return ListLangInt()
    if isa(xdsl_type, builtin.IntegerType[1]):
        return ListLangBool()
    if isa(xdsl_type, builtin.TensorType[builtin.IntegerType[32]]):
        return ListLangList(ListLangInt())
    if isa(xdsl_type, builtin.TensorType[builtin.IntegerType[1]]):
        return ListLangList(ListLangBool())
    raise ValueError("unknown type")

__str__() -> str

Source code in xdsl/frontend/listlang/lang_types.py
25
def __str__(self) -> str: ...

xdsl() -> Attribute

Source code in xdsl/frontend/listlang/lang_types.py
26
def xdsl(self) -> Attribute: ...

print(builder: Builder, value: SSAValue)

Source code in xdsl/frontend/listlang/lang_types.py
27
def print(self, builder: Builder, value: SSAValue): ...

get_method(method: str) -> Method | None

Source code in xdsl/frontend/listlang/lang_types.py
28
29
def get_method(self, method: str) -> "Method | None":
    return None

ListLangInt dataclass

Bases: ListLangType

Source code in xdsl/frontend/listlang/lang_types.py
32
33
34
35
36
37
38
39
40
41
@dataclass
class ListLangInt(ListLangType):
    def __str__(self) -> str:
        return "int"

    def xdsl(self) -> builtin.IntegerType:
        return builtin.IntegerType(32)

    def print(self, builder: Builder, value: SSAValue):
        builder.insert_op(printf.PrintFormatOp("{}", value))

__init__() -> None

__str__() -> str

Source code in xdsl/frontend/listlang/lang_types.py
34
35
def __str__(self) -> str:
    return "int"

xdsl() -> builtin.IntegerType

Source code in xdsl/frontend/listlang/lang_types.py
37
38
def xdsl(self) -> builtin.IntegerType:
    return builtin.IntegerType(32)

print(builder: Builder, value: SSAValue)

Source code in xdsl/frontend/listlang/lang_types.py
40
41
def print(self, builder: Builder, value: SSAValue):
    builder.insert_op(printf.PrintFormatOp("{}", value))

ListLangBool dataclass

Bases: ListLangType

Source code in xdsl/frontend/listlang/lang_types.py
44
45
46
47
48
49
50
51
52
53
@dataclass
class ListLangBool(ListLangType):
    def __str__(self) -> str:
        return "bool"

    def xdsl(self) -> builtin.IntegerType:
        return builtin.IntegerType(1)

    def print(self, builder: Builder, value: SSAValue):
        builder.insert_op(printf.PrintFormatOp("{}", value))

__init__() -> None

__str__() -> str

Source code in xdsl/frontend/listlang/lang_types.py
46
47
def __str__(self) -> str:
    return "bool"

xdsl() -> builtin.IntegerType

Source code in xdsl/frontend/listlang/lang_types.py
49
50
def xdsl(self) -> builtin.IntegerType:
    return builtin.IntegerType(1)

print(builder: Builder, value: SSAValue)

Source code in xdsl/frontend/listlang/lang_types.py
52
53
def print(self, builder: Builder, value: SSAValue):
    builder.insert_op(printf.PrintFormatOp("{}", value))

ListLangList dataclass

Bases: ListLangType

Source code in xdsl/frontend/listlang/lang_types.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@dataclass
class ListLangList(ListLangType):
    element_type: LIST_ELEMENT_TYPE

    def __str__(self) -> str:
        return f"list<{self.element_type}>"

    def xdsl(self) -> list_dialect.ListType:
        return list_dialect.ListType(self.element_type.xdsl())

    def print(self, builder: Builder, value: SSAValue):
        builder.insert_op(list_dialect.PrintOp(value))

    def get_method(self, method: str) -> "Method | None":
        match method:
            case "len":
                return ListLenMethod()
            case "map":
                return ListMapMethod()
            case _:
                return None

element_type: LIST_ELEMENT_TYPE instance-attribute

__init__(element_type: LIST_ELEMENT_TYPE) -> None

__str__() -> str

Source code in xdsl/frontend/listlang/lang_types.py
63
64
def __str__(self) -> str:
    return f"list<{self.element_type}>"

xdsl() -> list_dialect.ListType

Source code in xdsl/frontend/listlang/lang_types.py
66
67
def xdsl(self) -> list_dialect.ListType:
    return list_dialect.ListType(self.element_type.xdsl())

print(builder: Builder, value: SSAValue)

Source code in xdsl/frontend/listlang/lang_types.py
69
70
def print(self, builder: Builder, value: SSAValue):
    builder.insert_op(list_dialect.PrintOp(value))

get_method(method: str) -> Method | None

Source code in xdsl/frontend/listlang/lang_types.py
72
73
74
75
76
77
78
79
def get_method(self, method: str) -> "Method | None":
    match method:
        case "len":
            return ListLenMethod()
        case "map":
            return ListMapMethod()
        case _:
            return None

TypedExpression dataclass

Source code in xdsl/frontend/listlang/lang_types.py
82
83
84
85
@dataclass
class TypedExpression:
    value: SSAValue
    typ: ListLangType

value: SSAValue instance-attribute

typ: ListLangType instance-attribute

__init__(value: SSAValue, typ: ListLangType) -> None

Method

Source code in xdsl/frontend/listlang/lang_types.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Method:
    name: str

    def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
        """
        From the type on which the method was invoked, returns the types of the
        arguments of the method's lambda if there is one, or None if there is
        no lambda.
        """
        ...

    def build(
        self,
        builder: Builder,
        x: Located[TypedExpression],
        lambd: Located[tuple[Block, ListLangType]] | None,
    ) -> TypedExpression:
        """
        Builds the method's execution.

        `lambd` contains a free-standing block containing the lambda
        instructions that must be inlined as needed, and the type of the final
        expression of the block. The associated location is the location of
        the result expression.
        """
        ...

name: str instance-attribute

get_lambda_arg_type(x: ListLangType) -> Sequence[ListLangType] | None

From the type on which the method was invoked, returns the types of the arguments of the method's lambda if there is one, or None if there is no lambda.

Source code in xdsl/frontend/listlang/lang_types.py
 94
 95
 96
 97
 98
 99
100
def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
    """
    From the type on which the method was invoked, returns the types of the
    arguments of the method's lambda if there is one, or None if there is
    no lambda.
    """
    ...

build(builder: Builder, x: Located[TypedExpression], lambd: Located[tuple[Block, ListLangType]] | None) -> TypedExpression

Builds the method's execution.

lambd contains a free-standing block containing the lambda instructions that must be inlined as needed, and the type of the final expression of the block. The associated location is the location of the result expression.

Source code in xdsl/frontend/listlang/lang_types.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def build(
    self,
    builder: Builder,
    x: Located[TypedExpression],
    lambd: Located[tuple[Block, ListLangType]] | None,
) -> TypedExpression:
    """
    Builds the method's execution.

    `lambd` contains a free-standing block containing the lambda
    instructions that must be inlined as needed, and the type of the final
    expression of the block. The associated location is the location of
    the result expression.
    """
    ...

ListLenMethod

Bases: Method

Source code in xdsl/frontend/listlang/lang_types.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class ListLenMethod(Method):
    name = "len"

    def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
        return None

    def build(
        self,
        builder: Builder,
        x: Located[TypedExpression],
        lambd: Located[tuple[Block, ListLangType]] | None,
    ) -> TypedExpression:
        assert lambd is None
        assert isinstance(x.value.typ, ListLangList)

        len_op = builder.insert_op(list_dialect.LengthOp(x.value.value))
        return TypedExpression(len_op.result, ListLangInt())

name = 'len' class-attribute instance-attribute

get_lambda_arg_type(x: ListLangType) -> Sequence[ListLangType] | None

Source code in xdsl/frontend/listlang/lang_types.py
122
123
def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
    return None

build(builder: Builder, x: Located[TypedExpression], lambd: Located[tuple[Block, ListLangType]] | None) -> TypedExpression

Source code in xdsl/frontend/listlang/lang_types.py
125
126
127
128
129
130
131
132
133
134
135
def build(
    self,
    builder: Builder,
    x: Located[TypedExpression],
    lambd: Located[tuple[Block, ListLangType]] | None,
) -> TypedExpression:
    assert lambd is None
    assert isinstance(x.value.typ, ListLangList)

    len_op = builder.insert_op(list_dialect.LengthOp(x.value.value))
    return TypedExpression(len_op.result, ListLangInt())

ListMapMethod

Bases: Method

Source code in xdsl/frontend/listlang/lang_types.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class ListMapMethod(Method):
    name = "map"

    def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
        assert isinstance(x, ListLangList)
        return (x.element_type,)

    def build(
        self,
        builder: Builder,
        x: Located[TypedExpression],
        lambd: Located[tuple[Block, ListLangType]] | None,
    ) -> TypedExpression:
        assert lambd is not None
        assert isinstance(x.value.typ, ListLangList)

        if not isinstance(lambd.value[1], LIST_ELEMENT_TYPE):
            raise ParseError(lambd.loc.pos, "expected expression of list-element type")

        map_op = builder.insert_op(
            list_dialect.MapOp(
                x.value.value, Region([lambd.value[0]]), lambd.value[1].xdsl()
            )
        )

        return TypedExpression(map_op.result, ListLangList(lambd.value[1]))

name = 'map' class-attribute instance-attribute

get_lambda_arg_type(x: ListLangType) -> Sequence[ListLangType] | None

Source code in xdsl/frontend/listlang/lang_types.py
141
142
143
def get_lambda_arg_type(self, x: ListLangType) -> Sequence[ListLangType] | None:
    assert isinstance(x, ListLangList)
    return (x.element_type,)

build(builder: Builder, x: Located[TypedExpression], lambd: Located[tuple[Block, ListLangType]] | None) -> TypedExpression

Source code in xdsl/frontend/listlang/lang_types.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def build(
    self,
    builder: Builder,
    x: Located[TypedExpression],
    lambd: Located[tuple[Block, ListLangType]] | None,
) -> TypedExpression:
    assert lambd is not None
    assert isinstance(x.value.typ, ListLangList)

    if not isinstance(lambd.value[1], LIST_ELEMENT_TYPE):
        raise ParseError(lambd.loc.pos, "expected expression of list-element type")

    map_op = builder.insert_op(
        list_dialect.MapOp(
            x.value.value, Region([lambd.value[0]]), lambd.value[1].xdsl()
        )
    )

    return TypedExpression(map_op.result, ListLangList(lambd.value[1]))