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 __init__(self, e): if not in_constraint_scope(): raise Exception( "Attempting to use if_then constraint outside constraint scope" ) to_expr(e) self.stmt = ConstraintImpliesModel(pop_expr())
def __init__(self, e): if not in_constraint_scope(): raise Exception( "Attempting to use if_then constraint outside constraint scope" ) to_expr(e) self.stmt = ConstraintImpliesModel(pop_expr()) if in_srcinfo_mode(): self.stmt.srcinfo = SourceInfo.mk()
def visit_constraint_implies(self, c: ConstraintImpliesModel): if self.do_copy_level > 0: ret = ConstraintImpliesModel(self.expr(c.cond)) with ConstraintCollector(self, ret): for cs in c.constraint_l: cs.accept(self) self.constraints.append(ret) else: super().visit_constraint_implies(c)
def test_simple(self): a = FieldScalarModel("a", 16, False, True) b = FieldScalarModel("b", 16, False, True) c = FieldScalarModel("c", 16, False, True) l = ExprLiteralModel(10, False, 8) ab_c = ConstraintBlockModel("ab_c", [ ConstraintImpliesModel( ExprBinModel(ExprFieldRefModel(a), BinExprType.Lt, ExprFieldRefModel(b)), [ ConstraintExprModel( ExprBinModel(ExprFieldRefModel(c), BinExprType.Eq, l)) ]) ]) copy = ConstraintCopyBuilder.copy(ab_c) self.assertEquals(1, len(copy)) self.assertIsNot(ab_c, copy[0])
def visit_constraint_dist(self, c): # We replace the dist constraint with an equivalent # set of hard and soft constraints scope = ConstraintDistScopeModel(c) ranges = ExprRangelistModel() for w in c.weights: if w.rng_rhs is not None: # two-value range ranges.add_range(ExprRangeModel(w.rng_lhs, w.rng_rhs)) else: # single value ranges.add_range(w.rng_lhs) # First, create an 'in' constraint to restrict # values to the appropriate value set in_c = ConstraintExprModel(ExprInModel(c.lhs, ranges)) scope.addConstraint(in_c) # Now, we need to add exclusion constraints for any # zero weights # (!w) -> (lhs != [val]) # (!w) -> (lns not in [rng]) for w in c.weights: if w.rng_rhs is not None: scope.addConstraint( ConstraintImpliesModel( ExprBinModel(w.weight, BinExprType.Eq, ExprLiteralModel(0, False, 8)), [ ConstraintExprModel( ExprUnaryModel( UnaryExprType.Not, ExprBinModel( ExprBinModel(c.lhs, BinExprType.Ge, w.rng_lhs), BinExprType.And, ExprBinModel(c.lhs, BinExprType.Le, w.rng_rhs)))) ])) else: scope.addConstraint( ConstraintImpliesModel( ExprBinModel(w.weight, BinExprType.Eq, ExprLiteralModel(0, False, 8)), [ ExprUnaryModel( UnaryExprType.Not, ExprBinModel(c.lhs, BinExprType.Eq, w.rng_lhs)) ])) # Form a list of non-zero weighted tuples of weight/range # Sort in ascending order weight_l = [] total_w = 0 for i, w in enumerate(c.weights): weight = int(w.weight.val()) total_w += weight if weight > 0: weight_l.append((weight, i)) weight_l.sort(key=lambda w: w[0]) seed_v = self.rng.randint(1, total_w) # Find the first range i = 0 while i < len(weight_l): seed_v -= weight_l[i][0] if seed_v <= 0: break i += 1 if i >= len(weight_l): i = len(weight_l) - 1 scope.target_range = weight_l[i][1] target_w = c.weights[weight_l[i][1]] dist_soft_c = None if target_w.rng_rhs is not None: dist_soft_c = ConstraintSoftModel( ExprBinModel( ExprBinModel(c.lhs, BinExprType.Ge, target_w.rng_lhs), BinExprType.And, ExprBinModel(c.lhs, BinExprType.Le, target_w.rng_rhs))) else: dist_soft_c = ConstraintSoftModel( ExprBinModel(c.lhs, BinExprType.Eq, target_w.rng_lhs)) # Give dist constraints a high priority to allow # them to override all user-defined soft constraints dist_soft_c.priority = 1000000 scope.set_dist_soft_c(dist_soft_c) self.override_constraint(scope)