Skip to content

Commit f602ce1

Browse files
committed
hdl.ast: deprecate Const.normalize.
Tracking issue #754.
1 parent fcc4f54 commit f602ce1

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

amaranth/hdl/ast.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,9 @@ class Const(Value):
582582
"""
583583
src_loc = None
584584

585+
# TODO(amaranth-0.5): remove
585586
@staticmethod
587+
@deprecated("instead of `Const.normalize(value, shape)`, use `Const(value, shape).value`")
586588
def normalize(value, shape):
587589
mask = (1 << shape.width) - 1
588590
value &= mask
@@ -601,7 +603,10 @@ def __init__(self, value, shape=None, *, src_loc_at=0):
601603
shape = Shape.cast(shape, src_loc_at=1 + src_loc_at)
602604
self.width = shape.width
603605
self.signed = shape.signed
604-
self.value = self.normalize(self.value, shape)
606+
if self.signed and self.value >> (self.width - 1):
607+
self.value |= -(1 << self.width)
608+
else:
609+
self.value &= (1 << self.width) - 1
605610

606611
def shape(self):
607612
return Shape(self.width, self.signed)

amaranth/lib/data.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ def _convert_to_int(self, value):
100100
"""Convert ``value``, which may be a dict or an array of field values, to an integer using
101101
the representation defined by this layout.
102102
103-
This method is roughly equivalent to :meth:`Const.normalize`. It is private because
104-
Amaranth does not currently have a concept of a constant initializer; this requires
105-
an RFC. It will be renamed or removed in a future version."""
103+
This method is private because Amaranth does not currently have a concept of
104+
a constant initializer; this requires an RFC. It will be renamed or removed
105+
in a future version.
106+
"""
106107
if isinstance(value, Mapping):
107108
iterator = value.items()
108109
elif isinstance(value, Sequence):
@@ -116,7 +117,7 @@ def _convert_to_int(self, value):
116117
field = self[key]
117118
if isinstance(field.shape, Layout):
118119
key_value = field.shape._convert_to_int(key_value)
119-
int_value |= Const.normalize(key_value, Shape.cast(field.shape)) << field.offset
120+
int_value |= Const(key_value, Shape.cast(field.shape)).value << field.offset
120121
return int_value
121122

122123

amaranth/sim/_pycoro.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def run(self):
6969
if isinstance(command, Value):
7070
exec(_RHSValueCompiler.compile(self.state, command, mode="curr"),
7171
self.exec_locals)
72-
response = Const.normalize(self.exec_locals["result"], command.shape())
72+
response = Const(self.exec_locals["result"], command.shape()).value
7373

7474
elif isinstance(command, Statement):
7575
exec(_StatementCompiler.compile(self.state, command),

docs/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Language changes
3333
* Changed: :meth:`Value.cast` casts :class:`ValueCastable` objects recursively.
3434
* Changed: :meth:`Value.cast` treats instances of classes derived from both :class:`enum.Enum` and :class:`int` (including :class:`enum.IntEnum`) as enumerations rather than integers.
3535
* Changed: :class:`Cat` accepts instances of classes derived from both :class:`enum.Enum` and :class:`int` (including :class:`enum.IntEnum`) without warning.
36+
* Deprecated: :meth:`Const.normalize`.
3637
* Removed: (deprecated in 0.1) casting of :class:`Shape` to and from a ``(width, signed)`` tuple.
3738
* Removed: (deprecated in 0.3) :class:`ast.UserValue`.
3839
* Removed: (deprecated in 0.3) support for ``# nmigen:`` linter instructions at the beginning of file.

0 commit comments

Comments
 (0)