Skip to content

Commit 1fe7bd0

Browse files
committed
hdl: remove subclassing of AnyValue and Property.
This subclassing is unnecessary and makes downstream code more complex. In the new IR, they are unified into cells with the same name anyway. Even before that, this change simplifies things.
1 parent 115954b commit 1fe7bd0

File tree

5 files changed

+78
-141
lines changed

5 files changed

+78
-141
lines changed

amaranth/back/rtlil.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,13 @@ def on_value(self, value):
442442
def on_Const(self, value):
443443
return _const(value)
444444

445-
def on_AnyConst(self, value):
445+
def on_AnyValue(self, value):
446446
if value in self.s.anys:
447447
return self.s.anys[value]
448448

449449
res_shape = value.shape()
450450
res = self.s.rtlil.wire(width=res_shape.width, src=_src(value.src_loc))
451-
self.s.rtlil.cell("$anyconst", ports={
452-
"\\Y": res,
453-
}, params={
454-
"WIDTH": res_shape.width,
455-
}, src=_src(value.src_loc))
456-
self.s.anys[value] = res
457-
return res
458-
459-
def on_AnySeq(self, value):
460-
if value in self.s.anys:
461-
return self.s.anys[value]
462-
463-
res_shape = value.shape()
464-
res = self.s.rtlil.wire(width=res_shape.width, src=_src(value.src_loc))
465-
self.s.rtlil.cell("$anyseq", ports={
451+
self.s.rtlil.cell("$" + value.kind.value, ports={
466452
"\\Y": res,
467453
}, params={
468454
"WIDTH": res_shape.width,
@@ -619,10 +605,7 @@ class _LHSValueCompiler(_ValueCompiler):
619605
def on_Const(self, value):
620606
raise TypeError # :nocov:
621607

622-
def on_AnyConst(self, value):
623-
raise TypeError # :nocov:
624-
625-
def on_AnySeq(self, value):
608+
def on_AnyValue(self, value):
626609
raise TypeError # :nocov:
627610

628611
def on_Initial(self, value):
@@ -721,21 +704,17 @@ def on_Assign(self, stmt):
721704
else:
722705
self._case.assign(self.lhs_compiler(stmt.lhs), rhs_sigspec)
723706

724-
def on_property(self, stmt):
707+
def on_Property(self, stmt):
725708
self(stmt._check.eq(stmt.test))
726709
self(stmt._en.eq(1))
727710

728711
en_wire = self.rhs_compiler(stmt._en)
729712
check_wire = self.rhs_compiler(stmt._check)
730-
self.state.rtlil.cell("$" + stmt._kind, ports={
713+
self.state.rtlil.cell("$" + stmt.kind.value, ports={
731714
"\\A": check_wire,
732715
"\\EN": en_wire,
733716
}, src=_src(stmt.src_loc), name=stmt.name)
734717

735-
on_Assert = on_property
736-
on_Assume = on_property
737-
on_Cover = on_property
738-
739718
def on_Switch(self, stmt):
740719
self._check_rhs(stmt.test)
741720

amaranth/hdl/_ast.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -918,32 +918,6 @@ def __repr__(self):
918918
C = Const # shorthand
919919

920920

921-
class AnyValue(Value, DUID):
922-
def __init__(self, shape, *, src_loc_at=0):
923-
super().__init__(src_loc_at=src_loc_at)
924-
shape = Shape.cast(shape, src_loc_at=1 + src_loc_at)
925-
self.width = shape.width
926-
self.signed = shape.signed
927-
928-
def shape(self):
929-
return Shape(self.width, self.signed)
930-
931-
def _rhs_signals(self):
932-
return SignalSet()
933-
934-
935-
@final
936-
class AnyConst(AnyValue):
937-
def __repr__(self):
938-
return "(anyconst {}'{})".format(self.width, "s" if self.signed else "")
939-
940-
941-
@final
942-
class AnySeq(AnyValue):
943-
def __repr__(self):
944-
return "(anyseq {}'{})".format(self.width, "s" if self.signed else "")
945-
946-
947921
@final
948922
class Operator(Value):
949923
def __init__(self, operator, operands, *, src_loc_at=0):
@@ -1442,6 +1416,37 @@ def __repr__(self):
14421416
return f"(rst {self.domain})"
14431417

14441418

1419+
@final
1420+
class AnyValue(Value, DUID):
1421+
class Kind(Enum):
1422+
AnyConst = "anyconst"
1423+
AnySeq = "anyseq"
1424+
1425+
def __init__(self, kind, shape, *, src_loc_at=0):
1426+
super().__init__(src_loc_at=src_loc_at)
1427+
self.kind = self.Kind(kind)
1428+
shape = Shape.cast(shape, src_loc_at=1 + src_loc_at)
1429+
self.width = shape.width
1430+
self.signed = shape.signed
1431+
1432+
def shape(self):
1433+
return Shape(self.width, self.signed)
1434+
1435+
def _rhs_signals(self):
1436+
return SignalSet()
1437+
1438+
def __repr__(self):
1439+
return "({} {}'{})".format(self.kind.value, self.width, "s" if self.signed else "")
1440+
1441+
1442+
def AnyConst(shape, *, src_loc_at=0):
1443+
return AnyValue("anyconst", shape, src_loc_at=src_loc_at+1)
1444+
1445+
1446+
def AnySeq(shape, *, src_loc_at=0):
1447+
return AnyValue("anyseq", shape, src_loc_at=src_loc_at+1)
1448+
1449+
14451450
class Array(MutableSequence):
14461451
"""Addressable multiplexer.
14471452
@@ -1729,11 +1734,18 @@ class UnusedProperty(UnusedMustUse):
17291734
pass
17301735

17311736

1737+
@final
17321738
class Property(Statement, MustUse):
17331739
_MustUse__warning = UnusedProperty
17341740

1735-
def __init__(self, test, *, _check=None, _en=None, name=None, src_loc_at=0):
1741+
class Kind(Enum):
1742+
Assert = "assert"
1743+
Assume = "assume"
1744+
Cover = "cover"
1745+
1746+
def __init__(self, kind, test, *, _check=None, _en=None, name=None, src_loc_at=0):
17361747
super().__init__(src_loc_at=src_loc_at)
1748+
self.kind = self.Kind(kind)
17371749
self.test = Value.cast(test)
17381750
self._check = _check
17391751
self._en = _en
@@ -1742,10 +1754,10 @@ def __init__(self, test, *, _check=None, _en=None, name=None, src_loc_at=0):
17421754
raise TypeError("Property name must be a string or None, not {!r}"
17431755
.format(self.name))
17441756
if self._check is None:
1745-
self._check = Signal(reset_less=True, name=f"${self._kind}$check")
1757+
self._check = Signal(reset_less=True, name=f"${self.kind.value}$check")
17461758
self._check.src_loc = self.src_loc
17471759
if _en is None:
1748-
self._en = Signal(reset_less=True, name=f"${self._kind}$en")
1760+
self._en = Signal(reset_less=True, name=f"${self.kind.value}$en")
17491761
self._en.src_loc = self.src_loc
17501762

17511763
def _lhs_signals(self):
@@ -1756,23 +1768,20 @@ def _rhs_signals(self):
17561768

17571769
def __repr__(self):
17581770
if self.name is not None:
1759-
return f"({self.name}: {self._kind} {self.test!r})"
1760-
return f"({self._kind} {self.test!r})"
1771+
return f"({self.name}: {self.kind.value} {self.test!r})"
1772+
return f"({self.kind.value} {self.test!r})"
17611773

17621774

1763-
@final
1764-
class Assert(Property):
1765-
_kind = "assert"
1775+
def Assert(test, *, name=None, src_loc_at=0):
1776+
return Property("assert", test, name=name, src_loc_at=src_loc_at+1)
17661777

17671778

1768-
@final
1769-
class Assume(Property):
1770-
_kind = "assume"
1779+
def Assume(test, *, name=None, src_loc_at=0):
1780+
return Property("assume", test, name=name, src_loc_at=src_loc_at+1)
17711781

17721782

1773-
@final
1774-
class Cover(Property):
1775-
_kind = "cover"
1783+
def Cover(test, *, name=None, src_loc_at=0):
1784+
return Property("cover", test, name=name, src_loc_at=src_loc_at+1)
17761785

17771786

17781787
@final

amaranth/hdl/_dsl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def domain_name(domain):
506506
self._pop_ctrl()
507507

508508
for stmt in Statement.cast(assigns):
509-
if not isinstance(stmt, (Assign, Assert, Assume, Cover)):
509+
if not isinstance(stmt, (Assign, Property)):
510510
raise SyntaxError(
511511
"Only assignments and property checks may be appended to d.{}"
512512
.format(domain_name(domain)))

0 commit comments

Comments
 (0)