def get_model(self): if self._int_field_info.model is None: enums = None if not self.is_enum else self.t.enum_i.enums type_t = self.t.get_model() # Save the user-visible (Python) name for later use if hasattr(self.t, "name"): type_t.name = self.t.tname else: type_t.name = "<primitive>" self._int_field_info.model = FieldArrayModel( "<unknown-array>", type_t, self.is_scalar, enums, self.t.width if self.is_scalar else -1, self.t.is_signed if self.is_scalar else -1, self._int_field_info.is_rand, self.is_rand_sz) if self.init_sz > 0: if self.is_enum: ei: EnumInfo = self.t.enum_i ev = ei.v2e_m[ei.enums[0]] for i in range(self.init_sz): self.append(ev) elif self.is_scalar: for i in range(self.init_sz): self.append(0) else: for i in range(self.init_sz): self.append(type(self.t)()) elif self.init is not None: self.extend(self.init) return self._int_field_info.model
def get_model(self): if self._int_field_info.model is None: enums = None if not self.is_enum else self.t.enum_i.enums self._int_field_info.model = FieldArrayModel( "<unknown>", self.is_scalar, enums, self.t.width if self.is_scalar else -1, self.t.is_signed if self.is_scalar else -1, self._int_field_info.is_rand, self.is_rand_sz) if self.init_sz > 0: if self.is_enum: ei : EnumInfo = self.t.enum_i ev = ei.v2e_m[ei.enums[0]] for i in range(self.init_sz): self.append(ev) elif self.is_scalar: for i in range(self.init_sz): self.append(0) else: for i in range(self.init_sz): self.append(type(self.t)()) elif self.init is not None: self.extend(self.init) return self._int_field_info.model
def test_incr(self): obj = FieldCompositeModel("obj") arr = obj.add_field( FieldArrayModel( "arr", None, # type_t True, # is_scalar None, # not an enum-type list 32, False, True, False)) for i in range(10): arr.add_field() obj.add_constraint( ConstraintBlockModel("XX", [ ConstraintExprModel( ExprBinModel(ExprFieldRefModel(arr.size), BinExprType.Eq, ExprLiteralModel(10, False, 32))) ])) foreach = ConstraintForeachModel(ExprFieldRefModel(arr)) foreach.addConstraint( ConstraintImpliesModel( ExprBinModel(ExprFieldRefModel(foreach.index), BinExprType.Gt, ExprLiteralModel(0, False, 32)), [ ConstraintExprModel( ExprBinModel( ExprArraySubscriptModel( ExprFieldRefModel(arr), ExprFieldRefModel(foreach.index)), BinExprType.Eq, ExprBinModel( ExprArraySubscriptModel( ExprFieldRefModel(arr), ExprBinModel( ExprFieldRefModel(foreach.index), BinExprType.Sub, ExprLiteralModel(1, False, 32))), BinExprType.Add, ExprLiteralModel( 1, False, 32)))) ])) obj.add_constraint(ConstraintBlockModel("c", [foreach])) # print("Object: " + ModelPrettyPrinter.print(obj)) # # constraints = ArrayConstraintBuilder.build(obj) # for c in constraints: # print("Constraint: " + ModelPrettyPrinter.print(c)) # print("Object(1): " + ModelPrettyPrinter.print(obj)) # # ConstraintOverrideRollbackVisitor.rollback(obj) # print("Object(2): " + ModelPrettyPrinter.print(obj)) Randomizer.do_randomize([obj]) for f in arr.field_l: print("" + f.name + ": " + str(int(f.get_val())))
def visit_field_scalar_array(self, f:FieldArrayModel): if self.phase == 0: # TODO: this logic is for rand-sized array fields if f.is_rand_sz: size_bound = self.bound_m[f.size] range_l = size_bound.domain.range_l max_size = int(range_l[-1][1]) # TODO: how do we manage a max size here? if max_size > 100000: raise Exception("Max size for array " + f.name + " (" + str(max_size) + " exceeds 100000") if len(f.field_l) < max_size: # Extend the size appropriately print("Note: Extending array to size " + str(max_size)) for i in range(max_size-len(f.field_l)): f.add_field() elif self.phase == 1: if not f.is_scalar: # Need to recurse into sub-fields for non-scalar arrays for sf in f.field_l: sf.accept(self)
def get_model(self): if self._int_field_info.model is None: self._int_field_info.model = FieldArrayModel( "<unknown>", self.is_scalar, self.t.width if self.is_scalar else -1, self.t.is_signed if self.is_scalar else -1, self._int_field_info.is_rand, self.is_rand_sz) if self.init_sz > 0: for i in range(self.init_sz): if self.is_scalar: self.append(0) else: self.append(type(self.t)()) return self._int_field_info.model
def test_smoke(self): obj = FieldCompositeModel("obj") arr = obj.add_field( FieldArrayModel( "arr", None, # type_t True, None, # not an enum-type 32, False, True, True)) # for i in range(10): # arr.add_field() obj.add_constraint( ConstraintBlockModel("XX", [ ConstraintExprModel( ExprBinModel(ExprFieldRefModel(arr.size), BinExprType.Eq, ExprLiteralModel(10, False, 32))) ])) foreach = ConstraintForeachModel(ExprFieldRefModel(arr)) foreach.addConstraint( ConstraintExprModel( ExprBinModel( ExprArraySubscriptModel(ExprFieldRefModel(arr), ExprFieldRefModel(foreach.index)), BinExprType.Lt, ExprLiteralModel(10, False, 32)))) obj.add_constraint(ConstraintBlockModel("c", [foreach])) # print("Object: " + ModelPrettyPrinter.print(obj)) # # constraints = ArrayConstraintBuilder.build(obj) # for c in constraints: # print("Constraint: " + ModelPrettyPrinter.print(c)) # print("Object(1): " + ModelPrettyPrinter.print(obj)) # # ConstraintOverrideRollbackVisitor.rollback(obj) # print("Object(2): " + ModelPrettyPrinter.print(obj)) randstate = RandState(0) Randomizer.do_randomize(randstate, SourceInfo("", -1), [obj]) for f in arr.field_l: print("" + f.name + ": " + str(int(f.get_val())))