Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Amaranth version #530

Merged
merged 8 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions stubs/amaranth/hdl/ast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -411,7 +411,7 @@ class Signal(Value, DUID, metaclass=_SignalMeta):
"""Create Signal based on another.
"""
...

def shape(self) -> Shape:
...

Expand Down
2 changes: 1 addition & 1 deletion stubs/amaranth/hdl/dsl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
4 changes: 2 additions & 2 deletions stubs/amaranth/hdl/ir.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion stubs/amaranth/hdl/rec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
2 changes: 1 addition & 1 deletion stubs/amaranth/lib/coding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 5 additions & 6 deletions stubs/amaranth/lib/data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
141 changes: 130 additions & 11 deletions stubs/amaranth/lib/enum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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:
Expand All @@ -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__



2 changes: 1 addition & 1 deletion stubs/amaranth/lib/fifo.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion stubs/amaranth/lib/scheduler.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .. import *
from coreblocks.utils import HasElaborate
from transactron.utils import HasElaborate

class RoundRobin(Elaboratable):
count: int
Expand Down
12 changes: 6 additions & 6 deletions transactron/lib/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -176,7 +176,7 @@ def _(arg):
return m


class MethodProduct(Transformer, Elaboratable):
class MethodProduct(Transformer):
def __init__(
self,
targets: list[Method],
Expand Down Expand Up @@ -224,7 +224,7 @@ def _(arg):
return m


class MethodTryProduct(Transformer, Elaboratable):
class MethodTryProduct(Transformer):
def __init__(
self,
targets: list[Method],
Expand Down Expand Up @@ -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
Expand Down