Skip to content

Commit

Permalink
Remove records from assign
Browse files Browse the repository at this point in the history
  • Loading branch information
tilk committed Mar 11, 2024
1 parent a5d04dc commit 118fc3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
16 changes: 8 additions & 8 deletions test/transactions/test_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from amaranth.lib import data
from amaranth.hdl._ast import ArrayProxy, Slice

from transactron.utils._typing import LayoutLike
from transactron.utils._typing import MethodLayout
from transactron.utils import AssignType, assign
from transactron.utils.assign import AssignArg, AssignFields

Expand Down Expand Up @@ -54,15 +54,15 @@ def mkstruct(layout):
],
)
class TestAssign(TestCase):
# constructs `assign` arguments (records, proxies, dicts) which have an "inner" and "outer" part
# parameterized with a Record-like constructor and a layout of the inner part
build: Callable[[Callable[[LayoutLike], AssignArg], LayoutLike], AssignArg]
# constructs `assign` arguments (views, proxies, dicts) which have an "inner" and "outer" part
# parameterized with a constructor and a layout of the inner part
build: Callable[[Callable[[MethodLayout], AssignArg], MethodLayout], AssignArg]
# constructs field specifications for `assign`, takes field specifications for the inner part
wrap: Callable[[AssignFields], AssignFields]
# extracts the inner part of the structure
extr: Callable[[AssignArg], Record | ArrayProxy]
# Record-like constructor, takes a record layout
mk: Callable[[LayoutLike], AssignArg]
extr: Callable[[AssignArg], ArrayProxy]
# constructor, takes a layout
mk: Callable[[MethodLayout], AssignArg]

def test_rhs_exception(self):
with self.assertRaises(KeyError):
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_wrong_bits(self):
("list", layout_ab, layout_ab, ["a", "a"]),
]
)
def test_assign_a(self, name, layout1: LayoutLike, layout2: LayoutLike, atype: AssignType):
def test_assign_a(self, name, layout1: MethodLayout, layout2: MethodLayout, atype: AssignType):
lhs = self.build(self.mk, layout1)
rhs = self.build(self.mk, layout2)
alist = list(assign(lhs, rhs, fields=self.wrap(atype)))
Expand Down
7 changes: 1 addition & 6 deletions transactron/utils/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
Any,
TYPE_CHECKING,
)
from collections.abc import Iterable, Iterator, Mapping, Sequence
from collections.abc import Iterable, Iterator, Mapping
from contextlib import AbstractContextManager
from enum import Enum
from amaranth import *
from amaranth.lib.data import StructLayout, View
from amaranth.lib.wiring import Flow, Member
from amaranth.hdl import ShapeCastable, ValueCastable
from amaranth.hdl.rec import Direction, Layout

if TYPE_CHECKING:
from amaranth.hdl._ast import Statement
Expand All @@ -33,7 +32,6 @@
"ValueLike",
"ShapeLike",
"StatementLike",
"LayoutLike",
"SwitchKey",
"SrcLoc",
"MethodLayout",
Expand All @@ -60,9 +58,6 @@
ValueLike: TypeAlias = Value | int | Enum | ValueCastable
ShapeLike: TypeAlias = Shape | ShapeCastable | int | range | type[Enum]
StatementLike: TypeAlias = Union["Statement", Iterable["StatementLike"]]
LayoutLike: TypeAlias = (
Layout | Sequence[tuple[str, "ShapeLike | LayoutLike"] | tuple[str, "ShapeLike | LayoutLike", Direction]]
)
SwitchKey: TypeAlias = str | int | Enum
SrcLoc: TypeAlias = tuple[str, int]

Expand Down
34 changes: 10 additions & 24 deletions transactron/utils/assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def flatten_elems(proxy: ArrayProxy):
def assign_arg_fields(val: AssignArg) -> Optional[set[str]]:
if isinstance(val, ArrayProxy):
return arrayproxy_fields(val)
elif isinstance(val, Record):
return set(val.fields)
elif isinstance(val, data.View):
layout = val.shape()
if isinstance(layout, data.StructLayout):
Expand All @@ -57,7 +55,7 @@ def assign(
"""Safe structured assignment.
This function recursively generates assignment statements for
field-containing structures. This includes: Amaranth `Record`\\s,
field-containing structures. This includes:
Amaranth `View`\\s using `StructLayout`, Python `dict`\\s. In case of
mismatching fields or bit widths, error is raised.
Expand All @@ -68,16 +66,16 @@ def assign(
The bit width check is performed if:
- Any of `lhs` or `rhs` is a `Record` or `View`.
- Any of `lhs` or `rhs` is a `View`.
- Both `lhs` and `rhs` have an explicitly defined shape (e.g. are a
`Signal`, a field of a `Record` or a `View`).
`Signal`, a field of a `View`).
Parameters
----------
lhs : Record or View or Value-castable or dict
Record, signal or dict being assigned.
rhs : Record or View or Value-castable or dict
Record, signal or dict containing assigned values.
lhs : View or Value-castable or dict
View, signal or dict being assigned.
rhs : View or Value-castable or dict
View, signal or dict containing assigned values.
fields : AssignType or Iterable or Mapping, optional
Determines which fields will be assigned. Possible values:
Expand Down Expand Up @@ -109,18 +107,8 @@ def assign(

if lhs_fields is not None and rhs_fields is not None:
# asserts for type checking
assert (
isinstance(lhs, Record)
or isinstance(lhs, ArrayProxy)
or isinstance(lhs, Mapping)
or isinstance(lhs, data.View)
)
assert (
isinstance(rhs, Record)
or isinstance(rhs, ArrayProxy)
or isinstance(rhs, Mapping)
or isinstance(rhs, data.View)
)
assert isinstance(lhs, ArrayProxy) or isinstance(lhs, Mapping) or isinstance(lhs, data.View)
assert isinstance(rhs, ArrayProxy) or isinstance(rhs, Mapping) or isinstance(rhs, data.View)

if fields is AssignType.COMMON:
names = lhs_fields & rhs_fields
Expand Down Expand Up @@ -166,9 +154,7 @@ def has_explicit_shape(val: ValueLike):
return isinstance(val, Signal) or isinstance(val, ArrayProxy)

if (
isinstance(lhs, Record)
or isinstance(rhs, Record)
or isinstance(lhs, data.View)
isinstance(lhs, data.View)
or isinstance(rhs, data.View)
or (lhs_strict or has_explicit_shape(lhs))
and (rhs_strict or has_explicit_shape(rhs))
Expand Down

0 comments on commit 118fc3d

Please sign in to comment.