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__)
def test_named_tuple_vs_tuple_compatibility(self): u32 = ConcreteType.U32 u8 = ConcreteType.U8 m = ast.Module('test') fake_pos = Pos('fake.x', 0, 0) fake_span = Span(fake_pos, fake_pos) name_def = ast.NameDef(m, fake_span, 'fake') s = ast.Struct(m, fake_span, name_def, (), (), False) named = TupleType((('x', u32), ('y', u8)), struct=s) unnamed = TupleType((u32, u8)) self.assertTrue(named.compatible_with(unnamed)) self.assertNotEqual(named, unnamed) self.assertEqual(named.tuple_names, ('x', 'y'))
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))
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)
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 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_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())
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)
# See the License for the specific language governing permissions and # limitations under the License. """(Python) helper routines for working with (C++) ConcreteType objects.""" import functools from typing import Union, Callable from xls.dslx.python import cpp_ast as ast from xls.dslx.python.cpp_concrete_type import ArrayType from xls.dslx.python.cpp_concrete_type import BitsType from xls.dslx.python.cpp_concrete_type import ConcreteType from xls.dslx.python.cpp_concrete_type import EnumType from xls.dslx.python.cpp_concrete_type import FunctionType from xls.dslx.python.cpp_concrete_type import TupleType ConcreteType.NIL = TupleType(()) Dim = Union[str, int] 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(m)
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)
def test_array_of_tuple_all_dims(self): a = ArrayType(TupleType(()), 7) self.assertEqual((7, ), a.get_all_dims())
def test_nil_tuple(self): nil = TupleType(members=()) self.assertTrue(nil.is_nil()) t = TupleType(members=(nil, )) self.assertFalse(t.is_nil())