Exemplo n.º 1
0
 def test_named_tuple_vs_tuple_compatibility(self):
     u32 = ConcreteType.U32
     u8 = ConcreteType.U8
     named = TupleType((('x', u32), ('y', u8)), struct=None)
     unnamed = TupleType((u32, u8))
     self.assertTrue(named.compatible_with(unnamed))
     self.assertNotEqual(named, unnamed)
     self.assertEqual(named.tuple_names, ('x', 'y'))
Exemplo n.º 2
0
    def test_arrayness(self):
        tabular = [
            # (type, is_array, element_count)
            (TupleType(members=()), False, None),
            (BitsType(signed=False, size=5), False, None),
            (ArrayType(BitsType(False, 5), 7), True, 7),
            (ArrayType(TupleType(members=()), 7), True, 7),
        ]

        for t, is_array, element_count in tabular:
            self.assertEqual(isinstance(t, ArrayType), is_array, msg=str(t))
            if is_array:
                self.assertEqual(t.size, element_count, msg=str(t))
Exemplo n.º 3
0
 def test_sign_convert_tuple_value(self):
   # type is (u8, (u16, s8)
   t = TupleType(
       (BitsType(signed=False, size=8),
        TupleType(
            (BitsType(signed=False, size=16), BitsType(signed=True, size=8)))))
   self.assertEqual(
       sample_runner.sign_convert_value(
           t,
           Value.make_tuple((Value.make_ubits(8, 0x42),
                             Value.make_tuple((Value.make_ubits(16, 0x33),
                                               Value.make_ubits(8, 0x44)))))),
       Value.make_tuple(
           (Value.make_ubits(8, 0x42),
            Value.make_tuple(
                (Value.make_ubits(16, 0x33), Value.make_sbits(8, 0x44))))))
Exemplo n.º 4
0
def fsig(arg_types: ArgTypes, name: Text, span: Span, ctx: DeduceCtx,
         _: Optional[ParametricBindings]) -> ConcreteType:
    _Checker(arg_types, name, span).len(1).is_array(0)
    t = arg_types[0].get_element_type()  # pytype: disable=attribute-error
    e = TupleType((ConcreteType.U32, t))
    return_type = ArrayType(e, arg_types[0].size)  # pytype: disable=attribute-error
    return FunctionType(arg_types, return_type)
Exemplo n.º 5
0
 def test_generate_tuple_argument(self):
     rng = random.Random(0)
     args = sample_generator.generate_arguments((TupleType((BitsType(
         signed=False, size=123), BitsType(signed=True, size=22))), ), rng)
     self.assertLen(args, 1)
     self.assertTrue(args[0].is_tuple())
     self.assertEqual(args[0].tuple_members[0].get_bit_count(), 123)
     self.assertEqual(args[0].tuple_members[1].get_bit_count(), 22)
Exemplo n.º 6
0
    def test_array_vs_multidim_bits_equality(self):
        a = ArrayType(BitsType(signed=False, size=5), 7)
        self.assertEqual(str(a), 'uN[5][7]')
        self.assertEqual(7 * 5, a.get_total_bit_count())
        self.assertEqual(7, a.size)
        self.assertEqual(5, a.get_element_type().size)  # pytype: disable=attribute-error
        self.assertEqual((7, 5), a.get_all_dims())

        self.assertEqual((), TupleType(()).get_all_dims())
Exemplo n.º 7
0
def _deduce_Struct(self: ast.Struct, ctx: DeduceCtx) -> ConcreteType:  # pytype: disable=wrong-arg-types
    """Returns the concrete type for a (potentially parametric) struct."""
    for parametric in self.parametric_bindings:
        parametric_binding_type = deduce(parametric.type_, ctx)
        assert isinstance(parametric_binding_type, ConcreteType)
        if parametric.expr:
            expr_type = deduce(parametric.expr, ctx)
            if expr_type != parametric_binding_type:
                raise XlsTypeError(
                    parametric.span,
                    parametric_binding_type,
                    expr_type,
                    suffix='Annotated type of derived parametric '
                    'value did not match inferred type.')
        ctx.type_info[parametric.name] = parametric_binding_type

    members = tuple(
        (k.identifier, resolve(deduce(m, ctx), ctx)) for k, m in self.members)
    result = ctx.type_info[self.name] = TupleType(members, self)
    logging.vlog(5, 'Deduced type for struct %s => %s; type_info: %r', self,
                 result, ctx.type_info)
    return result
Exemplo n.º 8
0
def concrete_type_from_value(value: Value) -> ConcreteType[int]:
    """Returns the concrete type of 'value'.

  Note that:
  * Non-zero-length arrays are assumed (for zero length arrays we can't
    currently deduce the type from the value because the concrete element type
    is not reified in the array value.
  * Enums are strength-reduced to their underlying bits (storage) type.

  Args:
    value: Value to determine the concrete type for.
  """
    if value.tag in (Tag.UBITS, Tag.SBITS):
        signed = value.tag == Tag.SBITS
        return BitsType(signed, value.bits_payload.bit_count)
    elif value.tag == Tag.ARRAY:
        element_type = concrete_type_from_value(value.array_payload.index(0))
        return ArrayType(element_type, len(value))
    elif value.tag == Tag.TUPLE:
        return TupleType(
            tuple(concrete_type_from_value(m) for m in value.tuple_members))
    else:
        assert value.tag == Tag.ENUM, value
        return _strength_reduce_enum(value.type_, value.bits_payload.bit_count)
Exemplo n.º 9
0
def fsig(arg_types: ArgTypes, name: Text, span: Span, ctx: DeduceCtx,
         _: Optional[ParametricBindings]) -> ConcreteType:
    _Checker(arg_types, name, span).len(2).is_uN(0).is_same(0, 1)
    return_type = TupleType((ConcreteType.U1, arg_types[0]))
    return FunctionType(arg_types, return_type)
Exemplo n.º 10
0
 def test_array_of_tuple_all_dims(self):
     a = ArrayType(TupleType(()), 7)
     self.assertEqual((7, ), a.get_all_dims())
Exemplo n.º 11
0
    def test_nil_tuple(self):
        nil = TupleType(members=())
        self.assertTrue(nil.is_nil())

        t = TupleType(members=(nil, ))
        self.assertFalse(t.is_nil())
Exemplo n.º 12
0
def _deduce_TupleTypeAnnotation(self: ast.TupleTypeAnnotation,
                                ctx: DeduceCtx) -> ConcreteType:
    members = []
    for member in self.members:
        members.append(deduce(member, ctx))
    return TupleType(tuple(members))
Exemplo n.º 13
0
def _deduce_XlsTuple(self: ast.XlsTuple, ctx: DeduceCtx) -> ConcreteType:  # pytype: disable=wrong-arg-types
    members = tuple(deduce(m, ctx) for m in self.members)
    return TupleType(members)