Skip to content

Frame

Frame dataclass

Frame(
    code: Statement,
    *,
    parent: Self | None = None,
    has_parent_access: bool = False,
    lineno_offset: int = 0,
    globals: dict[str, Any] = dict(),
    entries: dict[SSAValue, ValueType] = dict()
)

Bases: FrameABC[ValueType]

Interpreter frame.

entries class-attribute instance-attribute

entries: dict[SSAValue, ValueType] = field(
    default_factory=dict, kw_only=True
)

SSA values and their corresponding values.

globals class-attribute instance-attribute

globals: dict[str, Any] = field(
    default_factory=dict, kw_only=True
)

Global variables this frame has access to.

get

get(key: SSAValue) -> ValueType

Get the value for the given SSAValue.

Parameters:

Name Type Description Default
key(SSAValue)

The key to get the value for.

required

Returns:

Name Type Description
ValueType ValueType

The value.

Raises:

Type Description
InterpreterError

If the value is not found. This will be catched by the interpreter.

Source code in src/kirin/interp/frame.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def get(self, key: SSAValue) -> ValueType:
    """Get the value for the given [`SSAValue`][kirin.ir.SSAValue].

    Args:
        key(SSAValue): The key to get the value for.

    Returns:
        ValueType: The value.

    Raises:
        InterpreterError: If the value is not found. This will be catched by the interpreter.
    """
    err = InterpreterError(f"SSAValue {key} not found")
    value = self.entries.get(key, err)
    if isinstance(value, InterpreterError):
        if self.has_parent_access and self.parent:
            return self.parent.get(key)
        else:
            raise err
    else:
        return value

get_casted

get_casted(
    key: SSAValue, type_: type[ExpectedType]
) -> ExpectedType

Same as get except it forces the linter to think the value is of the expected type.

Parameters:

Name Type Description Default
key(SSAValue)

The key to get the value for.

required
type_(type)

The expected type.

required

Returns:

Name Type Description
ExpectedType ExpectedType

The value.

Source code in src/kirin/interp/frame.py
136
137
138
139
140
141
142
143
144
145
146
147
def get_casted(self, key: SSAValue, type_: type[ExpectedType]) -> ExpectedType:
    """Same as [`get`][kirin.interp.frame.Frame.get] except it
    forces the linter to think the value is of the expected type.

    Args:
        key(SSAValue): The key to get the value for.
        type_(type): The expected type.

    Returns:
        ExpectedType: The value.
    """
    return self.get(key)  # type: ignore

get_typed

get_typed(
    key: SSAValue, type_: type[ExpectedType]
) -> ExpectedType

Similar to get but also checks the type.

Parameters:

Name Type Description Default
key(SSAValue)

The key to get the value for.

required
type_(type)

The expected type.

required

Returns:

Name Type Description
ExpectedType ExpectedType

The value.

Raises:

Type Description
InterpreterError

If the value is not of the expected type.

Source code in src/kirin/interp/frame.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def get_typed(self, key: SSAValue, type_: type[ExpectedType]) -> ExpectedType:
    """Similar to [`get`][kirin.interp.frame.Frame.get] but also checks the type.

    Args:
        key(SSAValue): The key to get the value for.
        type_(type): The expected type.

    Returns:
        ExpectedType: The value.

    Raises:
        InterpreterError: If the value is not of the expected type.
    """
    value = self.get(key)
    if not isinstance(value, type_):
        raise InterpreterError(f"expected {type_}, got {type(value)}")
    return value

set

set(key: SSAValue, value: ValueType) -> None

Set the value for the given SSAValue key. See also set_values.

Parameters:

Name Type Description Default
key(SSAValue)

The key to set the value for.

required
value(ValueType)

The value.

required
Source code in src/kirin/interp/frame.py
167
168
def set(self, key: SSAValue, value: ValueType) -> None:
    self.entries[key] = value

FrameABC dataclass

FrameABC(
    code: Statement,
    *,
    parent: Self | None = None,
    has_parent_access: bool = False,
    lineno_offset: int = 0
)

Bases: ABC, Generic[ValueType]

Abstract base class for the IR interpreter frame.

While the IR is in SSA form which does not have the need of scoping, the frame is still useful to keep track of the current statement being interpreted and the call stack. As well as various other interpreter state based on the specific interpreter implementation.

This base class provides the minimum interface for the interpreter frame.

code instance-attribute

code: Statement

statement whose region is being interpreted, e.g a function.

current_block class-attribute instance-attribute

current_block: Block | None = field(
    default=None, init=False, compare=False, repr=False
)

Current block being interpreted.

current_stmt class-attribute instance-attribute

current_stmt: Statement | None = field(
    default=None, init=False, compare=False, repr=False
)

Current statement being interpreted.

has_parent_access class-attribute instance-attribute

has_parent_access: bool = field(
    default=False, kw_only=True, compare=True
)

If we have access to the entries of the parent frame.

parent class-attribute instance-attribute

parent: Self | None = field(
    default=None, kw_only=True, compare=True, repr=False
)

Parent frame.

get abstractmethod

get(key: SSAValue) -> ValueType

Get the value for the given SSAValue key. See also get_values.

Parameters:

Name Type Description Default
key(SSAValue)

The key to get the value for.

required

Returns:

Name Type Description
ValueType ValueType

The value.

Source code in src/kirin/interp/frame.py
48
49
50
51
52
53
54
55
56
57
58
59
@abstractmethod
def get(self, key: SSAValue) -> ValueType:
    """Get the value for the given [`SSAValue`][kirin.ir.SSAValue] key.
    See also [`get_values`][kirin.interp.frame.Frame.get_values].

    Args:
        key(SSAValue): The key to get the value for.

    Returns:
        ValueType: The value.
    """
    ...

get_values

get_values(
    keys: Iterable[SSAValue],
) -> tuple[ValueType, ...]

Get the values of the given SSAValue keys. See also get.

Parameters:

Name Type Description Default
keys(Iterable[SSAValue])

The keys to get the values for.

required

Returns:

Type Description
tuple[ValueType, ...]

tuple[ValueType, ...]: The values.

Source code in src/kirin/interp/frame.py
72
73
74
75
76
77
78
79
80
81
82
def get_values(self, keys: Iterable[SSAValue]) -> tuple[ValueType, ...]:
    """Get the values of the given [`SSAValue`][kirin.ir.SSAValue] keys.
    See also [`get`][kirin.interp.frame.Frame.get].

    Args:
        keys(Iterable[SSAValue]): The keys to get the values for.

    Returns:
        tuple[ValueType, ...]: The values.
    """
    return tuple(self.get(key) for key in keys)

set abstractmethod

set(key: SSAValue, value: ValueType) -> None

Set the value for the given SSAValue key. See also set_values.

Parameters:

Name Type Description Default
key(SSAValue)

The key to set the value for.

required
value(ValueType)

The value.

required
Source code in src/kirin/interp/frame.py
61
62
63
64
65
66
67
68
69
70
@abstractmethod
def set(self, key: SSAValue, value: ValueType) -> None:
    """Set the value for the given [`SSAValue`][kirin.ir.SSAValue] key.
    See also [`set_values`][kirin.interp.frame.Frame.set_values].

    Args:
        key(SSAValue): The key to set the value for.
        value(ValueType): The value.
    """
    ...

set_values

set_values(
    keys: Iterable[SSAValue], values: Iterable[ValueType]
) -> None

Set the values of the given SSAValue keys. This is a convenience method to set multiple values at once.

Parameters:

Name Type Description Default
keys(Iterable[SSAValue])

The keys to set the values for.

required
values(Iterable[ValueType])

The values.

required
Source code in src/kirin/interp/frame.py
84
85
86
87
88
89
90
91
92
93
def set_values(self, keys: Iterable[SSAValue], values: Iterable[ValueType]) -> None:
    """Set the values of the given [`SSAValue`][kirin.ir.SSAValue] keys.
    This is a convenience method to set multiple values at once.

    Args:
        keys(Iterable[SSAValue]): The keys to set the values for.
        values(Iterable[ValueType]): The values.
    """
    for key, value in zip(keys, values):
        self.set(key, value)