diff --git a/requirements.txt b/requirements.txt index 278864342..c08979f0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -amaranth-yosys==0.25.0.0.post77 -git+https://github.com/amaranth-lang/amaranth@ccf7aaf00db54c7647b2f0f0cfdf34835c16fa8f +amaranth-yosys==0.35.0.0.post81 +git+https://github.com/amaranth-lang/amaranth@ab6503e352825b36bb29f1a8622b9e98aac9a6c6 diff --git a/stubs/amaranth/hdl/ast.pyi b/stubs/amaranth/hdl/ast.pyi index abb57120c..b22901c29 100644 --- a/stubs/amaranth/hdl/ast.pyi +++ b/stubs/amaranth/hdl/ast.pyi @@ -6,7 +6,7 @@ from abc import ABCMeta, abstractmethod from collections.abc import Callable, MutableMapping, MutableSequence, MutableSet from typing import Any, Generic, Iterable, Iterator, Mapping, NoReturn, Optional, Sequence, TypeVar, final, overload from enum import Enum -from coreblocks.utils import ValueLike, ShapeLike, StatementLike +from transactron.utils import ValueLike, ShapeLike, StatementLike __all__ = ["Shape", "ShapeCastable", "signed", "unsigned", "Value", "Const", "C", "AnyConst", "AnySeq", "Operator", "Mux", "Part", "Slice", "Cat", "Repl", "Array", "ArrayProxy", "Signal", "ClockSignal", "ResetSignal", "ValueCastable", "Sample", "Past", "Stable", "Rose", "Fell", "Initial", "Statement", "Switch", "Property", "Assign", "Assert", "Assume", "Cover", "ValueKey", "ValueDict", "ValueSet", "SignalKey", "SignalDict", "SignalSet", "ValueLike", "ShapeLike", "StatementLike", "SwitchKey"] @@ -50,7 +50,7 @@ class Shape: ... @staticmethod - def cast(obj: ShapeLike, *, src_loc_at=...): + def cast(obj: ShapeLike, *, src_loc_at=...) -> Shape: ... def __repr__(self) -> str: @@ -411,7 +411,7 @@ class Signal(Value, DUID, metaclass=_SignalMeta): """Create Signal based on another. """ ... - + def shape(self) -> Shape: ... diff --git a/stubs/amaranth/hdl/dsl.pyi b/stubs/amaranth/hdl/dsl.pyi index 41fdd8330..9d0c07f76 100644 --- a/stubs/amaranth/hdl/dsl.pyi +++ b/stubs/amaranth/hdl/dsl.pyi @@ -5,7 +5,7 @@ This type stub file was generated by pyright. from contextlib import _GeneratorContextManager, contextmanager from typing import Callable, ContextManager, Iterator, NoReturn, OrderedDict, ParamSpec, TypeVar, Optional from typing_extensions import Self -from coreblocks.utils import HasElaborate +from transactron.utils import HasElaborate from .ast import * from .ast import Flattenable from .ir import * diff --git a/stubs/amaranth/hdl/ir.pyi b/stubs/amaranth/hdl/ir.pyi index f5c7ba509..63acd1e3c 100644 --- a/stubs/amaranth/hdl/ir.pyi +++ b/stubs/amaranth/hdl/ir.pyi @@ -2,10 +2,10 @@ This type stub file was generated by pyright. """ -from abc import ABCMeta, abstractmethod +from abc import abstractmethod from .ast import * from .cd import * -from coreblocks.utils import HasElaborate +from transactron.utils import HasElaborate __all__ = ["Elaboratable", "DriverConflict", "Fragment", "Instance"] diff --git a/stubs/amaranth/hdl/rec.pyi b/stubs/amaranth/hdl/rec.pyi index a03644901..62887dd68 100644 --- a/stubs/amaranth/hdl/rec.pyi +++ b/stubs/amaranth/hdl/rec.pyi @@ -5,7 +5,7 @@ This type stub file was generated by pyright. from enum import Enum from typing import NoReturn, OrderedDict from .ast import * -from coreblocks.utils import LayoutLike +from transactron.utils import LayoutLike __all__ = ["Direction", "DIR_NONE", "DIR_FANOUT", "DIR_FANIN", "Layout", "Record"] Direction = Enum('Direction', ('NONE', 'FANOUT', 'FANIN')) diff --git a/stubs/amaranth/lib/coding.pyi b/stubs/amaranth/lib/coding.pyi index 9fe2bc3f3..aa0f3ffd3 100644 --- a/stubs/amaranth/lib/coding.pyi +++ b/stubs/amaranth/lib/coding.pyi @@ -3,7 +3,7 @@ This type stub file was generated by pyright. """ from .. import * -from coreblocks.utils import HasElaborate +from transactron.utils import HasElaborate __all__ = ["Encoder", "Decoder", "PriorityEncoder", "PriorityDecoder", "GrayEncoder", "GrayDecoder"] class Encoder(Elaboratable): diff --git a/stubs/amaranth/lib/data.pyi b/stubs/amaranth/lib/data.pyi index 84ea4684f..46d13be39 100644 --- a/stubs/amaranth/lib/data.pyi +++ b/stubs/amaranth/lib/data.pyi @@ -3,18 +3,17 @@ This type stub file was generated by pyright. """ from abc import ABCMeta, abstractmethod -from collections.abc import Callable, Iterator, Mapping -from enum import Enum -from typing import Optional, TypeVar, Generic +from collections.abc import Iterator, Mapping +from typing import TypeVar, Generic from typing_extensions import Self from amaranth.hdl import * from amaranth.hdl.ast import Assign, ShapeCastable, ValueCastable -from coreblocks.utils._typing import ShapeLike, ValueLike +from transactron.utils._typing import ShapeLike, ValueLike __all__ = ["Field", "Layout", "StructLayout", "UnionLayout", "ArrayLayout", "FlexibleLayout", "View", "Struct", "Union"] -_T_ShapeCastable = TypeVar("_T_ShapeCastable", bound=ShapeCastable) +_T_ShapeCastable = TypeVar("_T_ShapeCastable", bound=ShapeCastable, covariant=True) class Field: @@ -186,7 +185,7 @@ class View(ValueCastable, Generic[_T_ShapeCastable]): def as_value(self) -> Value: ... - def shape(self) -> Layout: + def shape(self) -> _T_ShapeCastable: ... def eq(self, other: ValueLike) -> Assign: diff --git a/stubs/amaranth/lib/enum.pyi b/stubs/amaranth/lib/enum.pyi index 2cd19fdbd..9c2d71e73 100644 --- a/stubs/amaranth/lib/enum.pyi +++ b/stubs/amaranth/lib/enum.pyi @@ -3,18 +3,23 @@ This type stub file was generated by pyright. """ import enum as py_enum +from typing import Generic, Optional, TypeVar, overload from typing_extensions import Self from amaranth import * -from ..hdl.ast import ShapeCastable +from ..hdl.ast import Assign, ValueCastable, ShapeCastable, ValueLike -__all__ = ['EnumMeta', 'Enum', 'IntEnum', 'Flag', 'IntFlag', 'auto', 'unique'] +__all__ = ['EnumMeta', 'Enum', 'IntEnum', 'Flag', 'IntFlag', 'EnumView', 'FlagView', 'auto', 'unique'] + + +_T = TypeVar("_T") +_T_ViewClass = TypeVar("_T_ViewClass", bound=None | ValueCastable) auto = py_enum.auto unique = py_enum.unique -class EnumMeta(ShapeCastable, py_enum.EnumMeta): +class EnumMeta(ShapeCastable, py_enum.EnumMeta, Generic[_T_ViewClass]): """Subclass of the standard :class:`enum.EnumMeta` that implements the :class:`ShapeCastable` protocol. @@ -26,10 +31,10 @@ class EnumMeta(ShapeCastable, py_enum.EnumMeta): or the values of its members. """ @classmethod - def __prepare__(metacls, name, bases, shape=..., **kwargs) -> py_enum._EnumDict: + def __prepare__(metacls, name, bases, shape: Shape=..., view_class:_T_ViewClass=..., **kwargs) -> py_enum._EnumDict: ... - def __new__(cls, name, bases, namespace, shape=..., **kwargs) -> Self: + def __new__(cls, name, bases, namespace, shape: Shape=..., view_class:_T_ViewClass=..., **kwargs) -> Self: ... def as_shape(cls) -> Shape: @@ -48,35 +53,149 @@ class EnumMeta(ShapeCastable, py_enum.EnumMeta): """ ... - def __call__(cls, value) -> Value: + @overload + def __call__(cls: type[_T], value: int) -> _T: + ... + + @overload + def __call__(cls: type[_T], value: _T) -> _T: + ... + + @overload + def __call__(cls: EnumMeta[None], value: int | ValueLike) -> Value: + ... + + @overload + def __call__(cls: EnumMeta[_T_ViewClass], value: int | ValueLike) -> _T_ViewClass: + ... + + def __call__(cls, value: int | ValueLike) -> Value | ValueCastable: ... def const(cls, init) -> Const: ... - -class Enum(py_enum.Enum, metaclass=EnumMeta): +class Enum(py_enum.Enum, metaclass=EnumMeta[EnumView]): """Subclass of the standard :class:`enum.Enum` that has :class:`EnumMeta` as its metaclass.""" ... -class IntEnum(py_enum.IntEnum, metaclass=EnumMeta): +class IntEnum(py_enum.IntEnum, metaclass=EnumMeta[None]): """Subclass of the standard :class:`enum.IntEnum` that has :class:`EnumMeta` as its metaclass.""" ... -class Flag(py_enum.Flag, metaclass=EnumMeta): +class Flag(py_enum.Flag, metaclass=EnumMeta[FlagView]): """Subclass of the standard :class:`enum.Flag` that has :class:`EnumMeta` as its metaclass.""" ... -class IntFlag(py_enum.IntFlag, metaclass=EnumMeta): +class IntFlag(py_enum.IntFlag, metaclass=EnumMeta[None]): """Subclass of the standard :class:`enum.IntFlag` that has :class:`EnumMeta` as its metaclass.""" ... +class EnumView(ValueCastable, Generic[_T]): + """The view class used for :class:`Enum`. + + Wraps a :class:`Value` and only allows type-safe operations. The only operators allowed are + equality comparisons (``==`` and ``!=``) with another :class:`EnumView` of the same enum type. + """ + + def __init__(self, enum: _T, target: ValueLike): + ... + + def shape(self) -> _T: + ... + + @ValueCastable.lowermethod + def as_value(self) -> Value: + ... + + def eq(self, other: ValueLike) -> Assign: + ... + + def __eq__(self, other: FlagView[_T] | _T) -> Value: + """Compares the underlying value for equality. + + The other operand has to be either another :class:`EnumView` with the same enum type, or + a plain value of the underlying enum. + + Returns + ------- + :class:`Value` + The result of the equality comparison, as a single-bit value. + """ + ... + + def __ne__(self, other: FlagView[_T] | _T) -> Value: + ... + + + +class FlagView(EnumView[_T], Generic[_T]): + """The view class used for :class:`Flag`. + + In addition to the operations allowed by :class:`EnumView`, it allows bitwise operations among + values of the same enum type.""" + + def __invert__(self) -> FlagView[_T]: + """Inverts all flags in this value and returns another :ref:`FlagView`. + + Note that this is not equivalent to applying bitwise negation to the underlying value: + just like the Python :class:`enum.Flag` class, only bits corresponding to flags actually + defined in the enumeration are included in the result. + + Returns + ------- + :class:`FlagView` + """ + ... + + def __and__(self, other: FlagView[_T] | _T) -> FlagView[_T]: + """Performs a bitwise AND and returns another :class:`FlagView`. + + The other operand has to be either another :class:`FlagView` of the same enum type, or + a plain value of the underlying enum type. + + Returns + ------- + :class:`FlagView` + """ + ... + + def __or__(self, other: FlagView[_T] | _T) -> FlagView[_T]: + """Performs a bitwise OR and returns another :class:`FlagView`. + + The other operand has to be either another :class:`FlagView` of the same enum type, or + a plain value of the underlying enum type. + + Returns + ------- + :class:`FlagView` + """ + ... + + def __xor__(self, other: FlagView[_T] | _T) -> FlagView[_T]: + """Performs a bitwise XOR and returns another :class:`FlagView`. + + The other operand has to be either another :class:`FlagView` of the same enum type, or + a plain value of the underlying enum type. + + Returns + ------- + :class:`FlagView` + """ + ... + + __rand__ = __and__ + __ror__ = __or__ + __rxor__ = __xor__ + + + diff --git a/stubs/amaranth/lib/fifo.pyi b/stubs/amaranth/lib/fifo.pyi index 5da2d3659..a64c5e8e5 100644 --- a/stubs/amaranth/lib/fifo.pyi +++ b/stubs/amaranth/lib/fifo.pyi @@ -4,7 +4,7 @@ This type stub file was generated by pyright. from .. import * from ..asserts import * -from coreblocks.utils import HasElaborate +from transactron.utils import HasElaborate """First-in first-out queues.""" __all__ = ["FIFOInterface", "SyncFIFO", "SyncFIFOBuffered", "AsyncFIFO", "AsyncFIFOBuffered"] diff --git a/stubs/amaranth/lib/scheduler.pyi b/stubs/amaranth/lib/scheduler.pyi index 3bd76c34e..b9662f19e 100644 --- a/stubs/amaranth/lib/scheduler.pyi +++ b/stubs/amaranth/lib/scheduler.pyi @@ -1,5 +1,5 @@ from .. import * -from coreblocks.utils import HasElaborate +from transactron.utils import HasElaborate class RoundRobin(Elaboratable): count: int diff --git a/transactron/lib/transformers.py b/transactron/lib/transformers.py index 5bcf0a4d3..8053180bd 100644 --- a/transactron/lib/transformers.py +++ b/transactron/lib/transformers.py @@ -20,7 +20,7 @@ ] -class Transformer(ABC): +class Transformer(ABC, Elaboratable): """Method transformer abstract class. Method transformers construct a new method which utilizes other methods. @@ -46,7 +46,7 @@ def use(self, m: ModuleLike): return self.method -class MethodMap(Transformer, Elaboratable): +class MethodMap(Transformer): """Bidirectional map for methods. Takes a target method and creates a transformed method which calls the @@ -102,7 +102,7 @@ def _(arg): return m -class MethodFilter(Transformer, Elaboratable): +class MethodFilter(Transformer): """Method filter. Takes a target method and creates a method which calls the target method @@ -176,7 +176,7 @@ def _(arg): return m -class MethodProduct(Transformer, Elaboratable): +class MethodProduct(Transformer): def __init__( self, targets: list[Method], @@ -224,7 +224,7 @@ def _(arg): return m -class MethodTryProduct(Transformer, Elaboratable): +class MethodTryProduct(Transformer): def __init__( self, targets: list[Method], @@ -276,7 +276,7 @@ def _(arg): return m -class Collector(Transformer, Elaboratable): +class Collector(Transformer): """Single result collector. Creates method that collects results of many methods with identical