def test_py_struct_arg_no_default_value(): class B(BitStruct): def __init__(s, foo): s.foo = Bits32(foo) class A(Component): def construct(s): s.in_ = InPort(B) a = A() with expected_failure(RTLIRConversionError, 'struct B should take 0 argument'): a.elaborate() rdt.get_rtlir_dtype(a.in_)
def visit_StructInst(s, node): cls = node.struct dtype = rdt.get_rtlir_dtype(cls()) all_properties = dtype.get_all_properties() if len(all_properties) != len(node.values): raise PyMTLTypeError( s.blk, node.ast, f"BitStruct {cls.__name__} has {len(all_properties)} fields but only {len(node.values)} arguments are given!" ) all_types = zip(node.values, list(all_properties.items())) for idx, (value, (name, field)) in enumerate(all_types): s.visit(value) # Expect each argument to be a signal if not isinstance(value.Type, rt.Signal): raise PyMTLTypeError( s.blk, node.ast, f"argument #{idx} has type {value.Type} but not a signal!") v_dtype = value.Type.get_dtype() # Expect each argument to have data type which corresponds to the field if v_dtype != field: raise PyMTLTypeError( s.blk, node.ast, f"Expected argument#{idx} ( field {name} ) to have type {field}, but got {v_dtype}." ) node.Type = rt.Const(dtype)
def test_pymtl_signal(): class A(Component): def construct(s): s.in_ = InPort(Bits32) a = A() a.elaborate() assert rdt.Vector(32) == rdt.get_rtlir_dtype(a.in_)
def test_pymtl_packed_array(): class B(BitStruct): def __init__(s, foo=42): s.foo = [Bits32(foo) for _ in range(5)] class A(Component): def construct(s): s.in_ = InPort(B) a = A() a.elaborate() assert rdt.Struct( 'B', {'foo':rdt.PackedArray([5], rdt.Vector(32))}, ['foo'] ) == \ rdt.get_rtlir_dtype( a.in_ )
def test_py_struct(): class B(BitStruct): def __init__(s, foo=42): s.foo = Bits32(foo) class A(Component): def construct(s): s.in_ = InPort(B) a = A() a.elaborate() assert rdt.Struct('B', {'foo': rdt.Vector(32)}, ['foo']) == rdt.get_rtlir_dtype(a.in_)
def test_py_struct(): @bitstruct class B: foo: Bits32 class A(Component): def construct(s): s.in_ = InPort(B) a = A() a.elaborate() assert rdt.Struct('B', {'foo': rdt.Vector(32)}) == rdt.get_rtlir_dtype(a.in_)
def test_pymtl_packed_array(): @bitstruct class B: foo: [Bits32] * 5 class A(Component): def construct(s): s.in_ = InPort(B) a = A() a.elaborate() assert rdt.Struct( 'B', {'foo':rdt.PackedArray([5], rdt.Vector(32))} ) == \ rdt.get_rtlir_dtype( a.in_ )
def visit_StructInst( s, node ): cls = node.struct try: type_instance = cls() except TypeError: raise PyMTLTypeError( s.blk, node.ast, """"\ __init__ of BitStruct {} should take 0 arguments! You can achieve this by adding default values to the arguments. """.format( cls.__name__ ) ) dtype = rdt.get_rtlir_dtype( cls() ) all_properties = dtype.get_all_properties() if len( all_properties ) != len( node.values ): raise PyMTLTypeError( s.blk, node.ast, "BitStruct {} has {} fields but only {} arguments are given!". \ format(cls.__name__, len(all_properties), len(node.values)) ) all_types = zip( node.values, all_properties ) for idx, ( value, ( name, field ) ) in enumerate( all_types ): s.visit( value ) # Expect each argument to be a signal if not isinstance( value.Type, rt.Signal ): raise PyMTLTypeError( s.blk, node.ast, "argument #{} has type {} but not a signal!". \ format( idx, value.Type ) ) v_dtype = value.Type.get_dtype() # Expect each argument to have data type which corresponds to the field if v_dtype != field: raise PyMTLTypeError( s.blk, node.ast, "Expected argument#{} ( field {} ) to have type {}, but got {}.". \ format( idx, name, field, v_dtype ) ) node.Type = rt.Const( dtype )
def test_pymtl_Bits(): assert rdt.Vector(1) == rdt.get_rtlir_dtype(Bits1(0)) assert rdt.Vector(2) == rdt.get_rtlir_dtype(Bits2(0)) assert rdt.Vector(8) == rdt.get_rtlir_dtype(Bits8(0)) assert rdt.Vector(32) == rdt.get_rtlir_dtype(Bits32(0)) assert rdt.Vector(255) == rdt.get_rtlir_dtype(Bits255(0))
def test_py_list(): with expected_failure(RTLIRConversionError, 'should be a field of some struct'): rdt.get_rtlir_dtype([1, 2, 3])
def test_py_float(): with expected_failure(RTLIRConversionError): rdt.get_rtlir_dtype(3.14)
def test_py_int(): assert rdt.Vector(32) == rdt.get_rtlir_dtype(42)
def test_pymtl_packed_array(): a = CasePackedArrayStructPortOnly.DUT() a.elaborate() assert rdt.get_rtlir_dtype( a.in_ ) == \ rdt.Struct( Bits32x5Foo, {'foo':rdt.PackedArray([5], rdt.Vector(32))} )
def test_pymtl_signal(): a = CaseBits32PortOnly.DUT() a.elaborate() assert rdt.get_rtlir_dtype(a.in_) == rdt.Vector(32)
def test_py_struct(): a = CaseStructPortOnly.DUT() a.elaborate() assert rdt.get_rtlir_dtype(a.in_) == rdt.Struct(Bits32Foo, {'foo': rdt.Vector(32)})
def __init__(s, obj, value): super().__init__(rt.Const(rdt.get_rtlir_dtype(obj))) s.value = value
def test_py_string(): with expected_failure(RTLIRConversionError): rdt.get_rtlir_dtype('RTLIR')