def concrete_type_from_dims(primitive: Token,
                            dims: Tuple[int, ...]) -> 'ConcreteType':
  """Creates a concrete type from the primitive type token and dims.

  Args:
    primitive: The token holding the primitive type as a keyword.
    dims: Dimensions to apply to the primitive type; e.g. () is scalar, (5) is
      1-D array of 5 elements having the primitive type.

  Returns:
    A concrete type object.

  Raises:
    ValueError: If the primitive keyword is unrecognized or dims are empty.
  """
  if primitive.is_keyword(Keyword.BITS) or primitive.is_keyword(Keyword.UN):
    base_type = BitsType(signed=False, size=dims[-1])
  elif primitive.is_keyword(Keyword.SN):
    base_type = BitsType(signed=True, size=dims[-1])
  else:
    assert primitive.kind == TokenKind.KEYWORD
    signedness, bits = TYPE_KEYWORDS_TO_SIGNEDNESS_AND_BITS[primitive.value]
    element_type = BitsType(signedness, bits)
    while dims:
      dims, minor = dims[:-1], dims[-1]
      element_type = ArrayType(element_type, minor)
    return element_type

  result = concrete_type_from_element_type_and_dims(base_type, dims[:-1])
  logging.vlog(4, '%r %r => %r', primitive, dims, result)
  return result
Exemple #2
0
 def test_generate_tuple_argument(self):
     rng = ast_generator.RngState(0)
     args = ast_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].get_elements()[0].get_bit_count(), 123)
     self.assertEqual(args[0].get_elements()[1].get_bit_count(), 22)
 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)
 def test_generate_mixed_bits_arguments(self):
   rng = ast_generator.RngState(0)
   args = sample_generator.generate_arguments(
       (BitsType(signed=False, size=123), BitsType(signed=True, size=22)), rng)
   self.assertLen(args, 2)
   self.assertTrue(args[0].is_ubits())
   self.assertEqual(args[0].get_bit_count(), 123)
   self.assertTrue(args[1].is_sbits())
   self.assertEqual(args[1].get_bit_count(), 22)
Exemple #5
0
 def test_equality(self):
     fake_pos = Pos('<fake>', 0, 0)
     fake_span = Span(fake_pos, fake_pos)
     p = BitsType(signed=False, size=ParametricSymbol('N', fake_span))
     c = BitsType(signed=False, size=32)
     self.assertTrue(c.__eq__(c))
     self.assertFalse(c.__ne__(c))
     self.assertTrue(p.__eq__(p))
     self.assertFalse(p.__ne__(p))
     self.assertTrue(p.__ne__(c))
     self.assertFalse(p.__eq__(c))
Exemple #6
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))
Exemple #7
0
 def test_stringify(self):
     # Test without a suffix.
     t = BitsType(signed=False, size=3)
     fake_pos = Pos('<fake>', 9, 10)
     fake_span = Span(fake_pos, fake_pos)
     e = XlsTypeError(fake_span, t, t)
     self.assertEndsWith(str(e), '@ <fake>:10:11-10:11')
Exemple #8
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, 1))
    return_type = BitsType(signed=False,
                           size=arg_types[0].get_total_bit_count() +
                           arg_types[1].get_total_bit_count())
    return FunctionType(arg_types, return_type)
Exemple #9
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))))))
def map_size(t: ConcreteType, m: ast.Module, f: Callable[[Dim],
                                                         Dim]) -> ConcreteType:
    """Runs f on all dimensions within t (transively for contained types)."""
    assert isinstance(m, ast.Module), m
    rec = functools.partial(map_size, m=m, f=f)

    if isinstance(t, ArrayType):
        return ArrayType(rec(t.get_element_type()), f(t.size))
    elif isinstance(t, BitsType):
        return BitsType(t.signed, f(t.size))
    elif isinstance(t, TupleType):
        nominal = t.get_nominal_type()
        if t.named:
            return TupleType(
                tuple((name, rec(type_)) for name, type_ in t.members),
                nominal)
        assert nominal is None, nominal
        return TupleType(tuple(rec(e) for e in t.members))
    elif isinstance(t, EnumType):
        return EnumType(t.get_nominal_type(), f(t.size))
    elif isinstance(t, FunctionType):
        mapped_params = tuple(rec(p) for p in t.params)
        mapped_return_type = rec(t.return_type)
        return FunctionType(mapped_params, mapped_return_type)
    else:
        raise NotImplementedError(t.__class__)
Exemple #11
0
 def test_generate_single_bits_arguments(self):
     rng = ast_generator.RngState(0)
     args = ast_generator.generate_arguments(
         (BitsType(signed=False, size=42), ), rng)
     self.assertLen(args, 1)
     self.assertTrue(args[0].is_ubits())
     self.assertEqual(args[0].get_bit_count(), 42)
Exemple #12
0
 def test_generate_array_argument(self):
   rng = random.Random(0)
   args = sample_generator.generate_arguments(
       (ArrayType(BitsType(signed=True, size=4), 24),), rng)
   self.assertLen(args, 1)
   self.assertTrue(args[0].is_array())
   self.assertLen(args[0].array_payload.elements, 24)
   self.assertTrue(args[0].array_payload.index(0).is_sbits())
   self.assertTrue(args[0].array_payload.index(0).get_bit_count(), 4)
Exemple #13
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())
Exemple #14
0
 def test_generate_array_argument(self):
     rng = ast_generator.RngState(0)
     args = ast_generator.generate_arguments(
         (ArrayType(BitsType(signed=True, size=4), 24), ), rng)
     self.assertLen(args, 1)
     self.assertTrue(args[0].is_array())
     self.assertLen(args[0].get_elements(), 24)
     self.assertTrue(args[0].index(0).is_sbits())
     self.assertTrue(args[0].index(0).get_bit_count(), 4)
    def _verify_constraints(self) -> None:
        """Verifies that all parametrics adhere to signature constraints.

    Take the following function signature for example:
      fn [X: u32, Y: u32 = X + X] f(x: bits[X], y: bits[Y]) -> bits[Y]

    The parametric Y has two constraints based only off the signature:
    it must match the bitwidth of the argument y and it must be equal to
    X + X. This function is responsible for computing any derived parametrics
    and asserting that their values are consistent with other constraints
    (arg types).
    """
        for binding, constraint in self.constraints.items():
            if constraint is None:
                # e.g. [X: u32]
                continue
            try:
                fn_name, fn_symbolic_bindings = self.ctx.fn_stack[-1]
                fn_ctx = (self.ctx.module.name, fn_name,
                          tuple(fn_symbolic_bindings.items()))
                result = self.ctx.interpret_expr(self.ctx.module,
                                                 self.ctx.type_info,
                                                 self.symbolic_bindings,
                                                 self.bit_widths,
                                                 constraint,
                                                 fn_ctx=fn_ctx)
            except KeyError as e:
                # We haven't seen enough bindings to evaluate this constraint.
                continue

            if binding in self.symbolic_bindings.keys():
                if result != self.symbolic_bindings[binding]:
                    raise XlsTypeError(
                        self.span,
                        BitsType(signed=False,
                                 size=self.symbolic_bindings[binding]),
                        BitsType(signed=False, size=result),
                        suffix=
                        f'Parametric constraint violated, saw {binding} = {constraint} = {result}; '
                        f'then {binding} = {self.symbolic_bindings[binding]}')
            else:
                self.symbolic_bindings[binding] = result
Exemple #16
0
 def test_stringify(self):
     u32 = BitsType(signed=False, size=32)
     tabular = [
         # type size total_bit_count str
         (ArrayType(u32, 7), 7, 32 * 7, 'uN[32][7]'),
         (u32, 32, 32, 'uN[32]'),
     ]
     for t, size, total_bit_count, s in tabular:
         self.assertEqual(t.size, size)
         self.assertEqual(t.get_total_bit_count(), total_bit_count)
         self.assertEqual(str(t), s)
Exemple #17
0
 def test_sign_convert_array_value(self):
     t = ArrayType(BitsType(signed=True, size=8), 3)
     self.assertEqual(
         sample_runner.sign_convert_value(
             t,
             Value.make_array(
                 (Value.make_ubits(8, 0x42), Value.make_ubits(8, 0x43),
                  Value.make_ubits(8, 0x44)))),
         Value.make_array(
             (Value.make_sbits(8, 0x42), Value.make_sbits(8, 0x43),
              Value.make_sbits(8, 0x44))))
Exemple #18
0
def _strength_reduce_enum(type_: ast.Enum, bit_count: int) -> ConcreteType:
    """Turns an enum to corresponding (bits) concrete type (w/signedness).

  For example, used in conversion checks.

  Args:
    type_: AST node (enum definition) to convert.
    bit_count: The bit count of the underlying bits type for the enum
      definition, as determined by type inference or interpretation.

  Returns:
    The concrete type that represents the enum's underlying bits type.
  """
    assert isinstance(type_, ast.Enum), type_
    signed = type_.signed
    assert isinstance(signed, bool), type_
    return BitsType(signed, bit_count)
    def _symbolic_bind_dims(self, param_type: ConcreteType,
                            arg_type: ConcreteType) -> None:
        """Binds parametric symbols in param_type according to arg_type."""
        # Create bindings for symbolic parameter dimensions based on argument
        # values passed.
        param_dim = param_type.size.value
        arg_dim = arg_type.size.value
        if not isinstance(param_dim, parametric_expression.ParametricSymbol):
            return

        pdim_name = param_dim.identifier
        if (pdim_name in self.symbolic_bindings
                and self.symbolic_bindings[pdim_name] != arg_dim):
            if self.constraints[pdim_name]:
                # Error on violated constraint.
                raise XlsTypeError(
                    self.span,
                    BitsType(signed=False,
                             size=self.symbolic_bindings[pdim_name]),
                    arg_type,
                    suffix=f'Parametric constraint violated, saw {pdim_name} '
                    f'= {self.constraints[pdim_name]} '
                    f'= {self.symbolic_bindings[pdim_name]}; '
                    f'then {pdim_name} = {arg_dim}')
            else:
                # Error on conflicting argument types.
                raise XlsTypeError(
                    self.span,
                    param_type,
                    arg_type,
                    suffix=
                    'Parametric value {} was bound to different values at '
                    'different places in invocation; saw: {!r}; then: {!r}'.
                    format(pdim_name, self.symbolic_bindings[pdim_name],
                           arg_dim))

        logging.vlog(2, 'Binding %r to %s', pdim_name, arg_dim)
        self.symbolic_bindings[pdim_name] = arg_dim
Exemple #20
0
def concrete_type_from_value(value: Value) -> ConcreteType:
    """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)
Exemple #21
0
 def test_array_bit_count(self):
     e = BitsType(signed=False, size=4)
     a = ArrayType(e, 3)
     self.assertEqual(a.get_total_bit_count(), 12)