Exemple #1
0
def test_sub_field_type_wrong():
    bd = bdl(a=(uw(8), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "a", sw(8))
    assert not check(sf)

    bd = bdl(a=(bdl(c=(uw(8), True)), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "a", bdl(c=(uw(9), True)))
    assert not check(sf)
Exemple #2
0
def test_sub_field_non_exist():
    bd = bdl(a=(uw(8), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "c", uw(8))
    assert not check(sf)

    bd = bdl(a=(bdl(c=(uw(8), True)), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "c", bdl(c=(uw(8), True)))
    assert not check(sf)
Exemple #3
0
def test_type_eq():
    assert equal(UnknownType(), UnknownType())
    assert equal(ClockType(), ClockType())
    assert equal(uw(10), uw(10))
    assert equal(sw(10), sw(10))
    assert equal(vec(uw(10), 8), vec(uw(10), 8))
    assert equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), True)),
                 bdl(a=(vec(uw(10), 8), False), b=(uw(10), True)))
Exemple #4
0
def test_neg():
    basis_tester(neg_basis_cases)
    encounter_error_tester(neg_type_wrong_cases)
    encounter_error_tester(neg_width_wrong_cases)
    serialize_equal(Neg(u(20, w(5)), sw(6)),
                    'neg(UInt<5>("h14"))')
    serialize_equal(Neg(s(-20, w(6)), sw(7)),
                    'neg(SInt<6>("h-14"))')
Exemple #5
0
def test_init_register_reset_wrong():
    r1 = DefInitRegister("r1", uw(8), n("clock", ClockType()), n("r", sw(1)),
                         u(5, w(8)))
    assert not check(r1)

    r2 = DefInitRegister("r2", sw(8), n("clock", ClockType()), s(0, w(1)),
                         s(5, w(8)))
    assert not check(r2)
Exemple #6
0
def test_sub():
    basis_tester(sub_basis_cases)
    encounter_error_tester(sub_type_wrong_cases)
    encounter_error_tester(sub_width_wrong_cases)
    serialize_equal(Sub([u(20, w(5)), u(15, w(4))], sw(6)),
                    'sub(UInt<5>("h14"), UInt<4>("hf"))')
    serialize_equal(Sub([s(-20, w(6)), s(-15, w(5))], sw(7)),
                    'sub(SInt<6>("h-14"), SInt<5>("h-f"))')
Exemple #7
0
def test_mux_tf_value_type_wrong():
    mux = Mux(n("c", uw(1)), n("a", uw(7)), n("b", uw(8)), uw(8))
    assert not check(mux)

    mux = Mux(n("c", uw(1)), n("a", uw(8)), n("b", sw(8)), uw(8))
    assert not check(mux)

    mux = Mux(n("c", uw(1)), n("a", uw(8)), n("b", uw(8)), sw(8))
    assert not check(mux)
Exemple #8
0
def test_mux_basis():
    mux = Mux(n("c", uw(1)), n("a", uw(8)), n("b", uw(8)), uw(8))
    assert check(mux)
    serialize_equal(mux, "mux(c, a, b)")

    mux = Mux(u(1, w(1)), n("b", vec(sw(8), 10)), n("c", vec(sw(8), 10)),
              vec(sw(8), 10))
    assert check(mux)
    serialize_equal(mux, 'mux(UInt<1>("h1"), b, c)')
Exemple #9
0
def test_assint():
    basis_tester(assint_basis_cases)
    encounter_error_tester(assint_type_wrong_cases)
    encounter_error_tester(assint_width_wrong_cases)
    serialize_equal(AsSInt(u(20, w(5)), sw(5)),
                    'asSInt(UInt<5>("h14"))')
    serialize_equal(AsSInt(s(-20, w(6)), sw(5)),
                    'asSInt(SInt<6>("h-14"))')
    serialize_equal(AsSInt(n("clock", ClockType()), sw(1)),
                    'asSInt(clock)')
Exemple #10
0
def test_sub_field_basis():
    bd = bdl(a=(uw(8), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "a", uw(8))
    assert check(sf)
    serialize_equal(sf, "bd.a")

    bd = bdl(a=(bdl(c=(uw(8), True)), False), b=(sw(8), False))
    sf = SubField(n("bd", bd), "a", bdl(c=(uw(8), True)))
    assert check(sf)
    serialize_equal(sf, "bd.a")
Exemple #11
0
def test_connect_type_wrong():
    cn = Connect(n("a", uw(8)), n("b", sw(8)))
    assert not check(cn)

    cn = Connect(n("a", sw(8)), n("b", uw(8)))
    assert not check(cn)

    cn = Connect(n("a", uw(8)), s(20, w(8)))
    assert not check(cn)

    cn = Connect(n("a", sw(8)), n(-20, w(8)))
    assert not check(cn)
Exemple #12
0
def test_conditionally_type_wrong():
    s1 = EmptyStmt()
    s2 = Connect(n("a", uw(8)), n("b", uw(8)))
    cn = Conditionally(n("a", sw(1)), s1, s2)
    assert not check(cn)

    s1 = Block([
        Connect(n("a", uw(8)), n("b", uw(8))),
        Connect(n("c", sw(8)), n("d", sw(8))),
    ])
    s2 = EmptyStmt()
    cn = Conditionally(u(1, w(2)), s1, s2)
    assert not check(cn)
Exemple #13
0
def test_connect_basis():
    cn = Connect(n("a", uw(8)), n("b", uw(8)))
    assert check(cn)
    serialize_stmt_equal(cn, "a <= b")

    cn = Connect(n("a", sw(8)), n("b", sw(8)))
    assert check(cn)
    serialize_stmt_equal(cn, "a <= b")

    cn = Connect(n("a", uw(8)), u(20, w(8)))
    assert check(cn)
    serialize_stmt_equal(cn, 'a <= UInt<8>("h14")')

    cn = Connect(n("a", sw(8)), s(-20, w(8)))
    assert check(cn)
    serialize_stmt_equal(cn, 'a <= SInt<8>("h-14")')
Exemple #14
0
def test_rem():
    basis_tester(rem_basis_cases)
    encounter_error_tester(rem_type_wrong_cases)
    encounter_error_tester(rem_width_wrong_cases)
    serialize_equal(Rem([u(20, w(5)), u(15, w(4))], uw(4)),
                    'rem(UInt<5>("h14"), UInt<4>("hf"))')
    serialize_equal(Rem([s(-20, w(6)), s(-15, w(5))], sw(5)),
                    'rem(SInt<6>("h-14"), SInt<5>("h-f"))')
Exemple #15
0
def test_sub_access_idx_non_uint():
    vc = vec(uw(8), 10)
    sa = SubAccess(n("vc", vc), s(2, w(3)), uw(8))
    assert not check(sa)

    vc = vec(uw(8), 10)
    sa = SubAccess(n("vc", vc), n("a", sw(8)), uw(8))
    assert not check(sa)
Exemple #16
0
def test_sub_index_type_wrong():
    vc = vec(uw(8), 10)
    si = SubIndex(n("vc", vc), 5, sw(8))
    assert not check(si)

    vc = vec(vec(uw(8), 10), 20)
    si = SubIndex(n("vc", vc), 19, vec(uw(8), 20))
    assert not check(si)
Exemple #17
0
def test_wire_basis():
    wire = DefWire("w1", uw(8))
    assert check(wire)
    serialize_stmt_equal(wire, 'wire w1 : UInt<8>')

    wire = DefWire("w2", bdl(a=(uw(8), True), b=(sw(8), False)))
    assert check(wire)
    serialize_stmt_equal(wire, 'wire w2 : {flip a : UInt<8>, b : SInt<8>}')
Exemple #18
0
def test_mul():
    basis_tester(mul_basis_cases)
    encounter_error_tester(mul_type_wrong_cases)
    encounter_error_tester(mul_width_wrong_cases)
    serialize_equal(Mul([u(20, w(5)), u(15, w(4))], uw(9)),
                    'mul(UInt<5>("h14"), UInt<4>("hf"))')
    serialize_equal(Mul([s(-20, w(6)), s(-15, w(5))], sw(11)),
                    'mul(SInt<6>("h-14"), SInt<5>("h-f"))')
Exemple #19
0
def test_mux_cond_type_wrong():
    mux = Mux(n("c", uw(2)), n("a", uw(8)), n("b", uw(8)), uw(8))
    assert not check(mux)

    mux = Mux(n("c", sw(1)), n("a", uw(8)), n("b", uw(8)), uw(8))
    assert not check(mux)

    mux = Mux(n("c", vec(uw(1), 1)), n("a", uw(8)), n("b", uw(8)), uw(8))
    assert not check(mux)
Exemple #20
0
def s_gen():
    if random.randint(0, 1):
        rand_s_value = random.randint(-1024, 1024)
        rand_s_value_width = signed_num_bin_len(rand_s_value)
        rand_s_width = random.randint(rand_s_value_width,
                                      2 * rand_s_value_width)
        return s(rand_s_value, w(rand_s_width))
    else:
        rand_ns_name = name_gen()
        rand_ns_width = random.randint(1, 20)
        return n(rand_ns_name, sw(rand_ns_width))
Exemple #21
0
def test_conditionally_basis():
    s1 = EmptyStmt()
    s2 = Connect(n("a", uw(8)), n("b", uw(8)))
    cn = Conditionally(n("a", uw(1)), s1, s2)
    assert check(cn)
    serialize_stmt_equal(cn, "when a :\n" "  skip\n" "else :\n" "  a <= b")

    s1 = Block([
        Connect(n("a", uw(8)), n("b", uw(8))),
        Connect(n("c", sw(8)), n("d", sw(8))),
    ])
    s2 = EmptyStmt()
    cn = Conditionally(u(1, w(1)), s1, s2)
    assert check(cn)
    serialize_stmt_equal(
        cn, 'when UInt<1>("h1") :\n'
        '  a <= b\n'
        '  c <= d\n'
        'else :\n'
        '  skip')
Exemple #22
0
def test_init_register_basis():
    r1 = DefInitRegister("r1", uw(8), n("clock", ClockType()), n("r", uw(1)),
                         u(5, w(8)))
    assert check(r1)
    serialize_stmt_equal(
        r1, 'reg r1 : UInt<8>, clock with :\n'
        '  reset => (r, UInt<8>("h5"))')

    r2 = DefInitRegister("r2", sw(8), n("clock", ClockType()), u(0, w(1)),
                         s(5, w(8)))
    assert check(r2)
    serialize_stmt_equal(
        r2, 'reg r2 : SInt<8>, clock with :\n'
        '  reset => (UInt<1>("h0"), SInt<8>("h5"))')
Exemple #23
0
def type_wrong_cases_2_args_gen(op):
    wrong_cases = [
        op_args(op, UnknownType, UnknownType).tpe(lambda x, y: uw(32)),
        op_args(op, UnknownType, UIntType).tpe(lambda x, y: uw(32)),
        op_args(op, UnknownType, SIntType).tpe(lambda x, y: sw(32)),
        op_args(op, UnknownType, VectorType).tpe(lambda x, y: sw(32)),
        op_args(op, UnknownType, BundleType).tpe(lambda x, y: sw(32)),
        op_args(op, UIntType, VectorType).tpe(lambda x, y: uw(32)),
        op_args(op, UIntType, BundleType).tpe(lambda x, y: uw(32)),
        op_args(op, UIntType, UnknownType).tpe(lambda x, y: uw(32)),
        op_args(op, SIntType, VectorType).tpe(lambda x, y: sw(32)),
        op_args(op, SIntType, BundleType).tpe(lambda x, y: sw(32)),
        op_args(op, SIntType, UnknownType).tpe(lambda x, y: sw(32)),
        op_args(op, VectorType, UIntType).tpe(lambda x, y: uw(32)),
        op_args(op, VectorType, SIntType).tpe(lambda x, y: sw(32)),
        op_args(op, VectorType, VectorType).tpe(lambda x, y: uw(32)),
        op_args(op, VectorType, BundleType).tpe(lambda x, y: uw(32)),
        op_args(op, BundleType, UIntType).tpe(lambda x, y: uw(32)),
        op_args(op, BundleType, SIntType).tpe(lambda x, y: uw(32)),
        op_args(op, BundleType, VectorType).tpe(lambda x, y: uw(32)),
        op_args(op, BundleType, UnknownType).tpe(lambda x, y: uw(32)),
        op_args(op, BundleType, BundleType).tpe(lambda x, y: uw(32)),
    ]
    return wrong_cases
Exemple #24
0
            class B:
                @staticmethod
                def tpe(res_type):
                    return OpCase(Shr) \
                        .arg_types(*arg_types) \
                        .const_arg_types(*const_types) \
                        .res_type(res_type)

            return B

    return A


shr_basis_cases = [
    args(UIntType).const(int).tpe(lambda x, y: uw(max(1, width(x) - y))),
    args(SIntType).const(int).tpe(lambda x, y: sw(max(1, width(x) - y))),
]

shr_type_wrong_cases = [
    args(UnknownType).const(int).tpe(lambda x, y: uw(max(1, y))),
    args(VectorType).const(int).tpe(lambda x, y: uw(max(1, y))),
    args(BundleType).const(int).tpe(lambda x, y: uw(max(1, y))),
]

shr_width_wrong_cases = [
    args(UIntType).const(int).tpe(lambda x, y: uw(max(1, width(x) - y) + 1)),
    args(SIntType).const(int).tpe(lambda x, y: sw(max(1, width(x) - y) + 1)),
    args(UIntType).const(int).tpe(lambda x, y: uw(max(1, width(x) - y) - 1)),
    args(SIntType).const(int).tpe(lambda x, y: sw(max(1, width(x) - y) - 1)),
]
Exemple #25
0
def args(*arg_types):
    class C:
        @staticmethod
        def tpe(res_type):
            return OpCase(Leq).arg_types(*arg_types).res_type(res_type)

    return C


leq_basis_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(1)),
    args(SIntType, SIntType).tpe(lambda x, y: uw(1)),
]

leq_type_wrong_cases = type_wrong_cases_2_args_gen(Leq) + [
    args(UIntType, UIntType).tpe(lambda x, y: sw(1)),
    args(SIntType, SIntType).tpe(lambda x, y: sw(1)),
]

leq_width_wrong_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(2)),
    args(SIntType, SIntType).tpe(lambda x, y: uw(2)),
    args(UIntType, UIntType).tpe(lambda x, y: uw(3)),
    args(SIntType, SIntType).tpe(lambda x, y: uw(3)),
]


def test_leq():
    basis_tester(leq_basis_cases)
    encounter_error_tester(leq_type_wrong_cases)
    encounter_error_tester(leq_width_wrong_cases)
Exemple #26
0
from .helper import OpCase, basis_tester, \
    encounter_error_tester, type_wrong_cases_2_args_gen, min_width


def args(*arg_types):
    class C:
        @staticmethod
        def tpe(res_type):
            return OpCase(Rem).arg_types(*arg_types).res_type(res_type)

    return C


rem_basis_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(min_width(x, y))),
    args(SIntType, SIntType).tpe(lambda x, y: sw(min_width(x, y))),
]

rem_type_wrong_cases = type_wrong_cases_2_args_gen(Rem)

rem_width_wrong_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(min_width(x, y) + 1)),
    args(SIntType, SIntType).tpe(lambda x, y: sw(min_width(x, y) + 1)),
    args(UIntType, UIntType).tpe(lambda x, y: uw(min_width(x, y) - 1)),
    args(SIntType, SIntType).tpe(lambda x, y: sw(min_width(x, y) - 1)),
]


def test_rem():
    basis_tester(rem_basis_cases)
    encounter_error_tester(rem_type_wrong_cases)
Exemple #27
0
def test_type_neq():
    assert not equal(UnknownType(), ClockType())
    assert not equal(UnknownType(), uw(10))
    assert not equal(UnknownType(), sw(10))
    assert not equal(UnknownType(), vec(uw(10), 8))
    assert not equal(UnknownType(), vec(sw(10), 8))
    assert not equal(UnknownType(),
                     bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)))

    assert not equal(ClockType(), UnknownType())
    assert not equal(ClockType(), uw(10))
    assert not equal(ClockType(), sw(10))
    assert not equal(ClockType(), vec(uw(10), 8))
    assert not equal(ClockType(), vec(sw(10), 8))
    assert not equal(ClockType(),
                     bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)))

    assert not equal(uw(10), UnknownType())
    assert not equal(uw(10), ClockType())
    assert not equal(uw(10), uw(8))
    assert not equal(uw(10), sw(10))
    assert not equal(uw(10), sw(10))
    assert not equal(uw(10), vec(uw(10), 8))
    assert not equal(uw(10), vec(sw(10), 8))
    assert not equal(uw(10),
                     bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)))

    assert not equal(sw(10), UnknownType())
    assert not equal(sw(10), ClockType())
    assert not equal(sw(10), sw(8))
    assert not equal(sw(10), uw(10))
    assert not equal(sw(10), uw(10))
    assert not equal(sw(10), vec(uw(10), 8))
    assert not equal(sw(10), vec(sw(10), 8))
    assert not equal(sw(10),
                     bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)))

    assert not equal(vec(uw(10), 8), UnknownType())
    assert not equal(vec(uw(10), 8), ClockType())
    assert not equal(vec(uw(10), 8), sw(8))
    assert not equal(vec(uw(10), 8), uw(10))
    assert not equal(vec(uw(10), 8), uw(10))
    assert not equal(vec(uw(10), 8), vec(uw(8), 8))
    assert not equal(vec(uw(10), 8), vec(uw(10), 9))
    assert not equal(vec(uw(10), 8), vec(sw(10), 8))
    assert not equal(vec(uw(10), 8),
                     bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)))

    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     UnknownType())
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     ClockType())
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     sw(8))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     uw(10))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     uw(10))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     vec(sw(10), 8))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     bdl(a=(vec(uw(10), 8), True), b=(uw(10), False)))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     bdl(a=(vec(uw(10), 2), False), b=(uw(10), False)))
    assert not equal(bdl(a=(uw(3), False), b=(uw(10), False)),
                     bdl(a=(uw(3), False), b=(uw(10), False), c=(sw(2), True)))
    assert not equal(bdl(a=(vec(uw(10), 8), False), b=(uw(10), False)),
                     bdl(b=(uw(10), False), a=(vec(uw(10), 8), False)))
Exemple #28
0
def args(*arg_types):
    class C:
        @staticmethod
        def tpe(res_type):
            return OpCase(Cat).arg_types(*arg_types).res_type(res_type)

    return C


cat_basis_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(sum_width(x, y))),
    args(SIntType, SIntType).tpe(lambda x, y: uw(sum_width(x, y))),
]

cat_type_wrong_cases = type_wrong_cases_2_args_gen(Cat) + [
    args(UIntType, UIntType).tpe(lambda x, y: sw(sum_width(x, y))),
    args(SIntType, SIntType).tpe(lambda x, y: sw(sum_width(x, y))),
]

cat_width_wrong_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(sum_width(x, y) + 1)),
    args(SIntType, SIntType).tpe(lambda x, y: uw(sum_width(x, y) + 1)),
    args(UIntType, UIntType).tpe(lambda x, y: uw(sum_width(x, y) - 1)),
    args(SIntType, SIntType).tpe(lambda x, y: uw(sum_width(x, y) - 1)),
]


def test_cat():
    basis_tester(cat_basis_cases)
    encounter_error_tester(cat_type_wrong_cases)
    encounter_error_tester(cat_width_wrong_cases)
Exemple #29
0
from .helper import OpCase, basis_tester, \
    encounter_error_tester, type_wrong_cases_2_args_gen, width


def args(*arg_types):
    class C:
        @staticmethod
        def tpe(res_type):
            return OpCase(Dshr).arg_types(*arg_types).res_type(res_type)

    return C


dshr_basis_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x))),
    args(SIntType, UIntType).tpe(lambda x, y: sw(width(x))),
]

dshr_type_wrong_cases = type_wrong_cases_2_args_gen(Dshr)

dshr_width_wrong_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x) + 1)),
    args(SIntType, UIntType).tpe(lambda x, y: sw(width(x) + 1)),
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x) - 1)),
    args(SIntType, UIntType).tpe(lambda x, y: sw(width(x) - 1)),
]


def test_dshr():
    basis_tester(dshr_basis_cases)
    encounter_error_tester(dshr_type_wrong_cases)
Exemple #30
0
from .helper import OpCase, basis_tester, \
    encounter_error_tester, type_wrong_cases_2_args_gen, width


def args(*arg_types):
    class C:
        @staticmethod
        def tpe(res_type):
            return OpCase(Div).arg_types(*arg_types).res_type(res_type)

    return C


div_basis_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x))),
    args(SIntType, SIntType).tpe(lambda x, y: sw(width(x) + 1)),
]

div_type_wrong_cases = type_wrong_cases_2_args_gen(Div)

div_width_wrong_cases = [
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x) + 1)),
    args(SIntType, SIntType).tpe(lambda x, y: sw(width(x) + 2)),
    args(UIntType, UIntType).tpe(lambda x, y: uw(width(x) - 1)),
    args(SIntType, SIntType).tpe(lambda x, y: sw(width(x))),
    args(SIntType, SIntType).tpe(lambda x, y: sw(1)),
]


def test_div():
    basis_tester(div_basis_cases)