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())))
Beispiel #2
0
    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())))
Beispiel #3
0
    def test_smoke(self):
        obj = FieldCompositeModel("obj")
        a = obj.add_field(FieldScalarModel("a", 8, False, True))
        b = obj.add_field(FieldScalarModel("a", 8, False, True))
        obj.add_constraint(
            ConstraintBlockModel("c", [
                ConstraintExprModel(
                    ExprBinModel(a.expr(), BinExprType.Lt, b.expr()))
            ]))

        rand = Randomizer(RandState(0))
        randstate = RandState(0)

        rand.do_randomize(randstate, SourceInfo("", -1), [obj])

        self.assertLess(a.val, b.val)
Beispiel #4
0
    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])
Beispiel #5
0
    def test_wide_var(self):
        obj = FieldCompositeModel("obj")
        a = obj.add_field(FieldScalarModel("a", 1024, False, True))
        obj.add_constraint(
            ConstraintBlockModel("c", [
                ConstraintExprModel(
                    ExprBinModel(
                        a.expr(), BinExprType.Gt,
                        ExprLiteralModel(0x80000000000000000, False, 72)))
            ]))

        randstate = RandState(0)
        rand = Randomizer(randstate)

        rand.do_randomize(randstate, SourceInfo("", -1), [obj])

        print("a=" + hex(int(a.val)))
        self.assertGreater(a.val, ValueScalar(0x80000000000000000))
Beispiel #6
0
def pop_constraint_scope():
    for e in pop_exprs():
        constraint_scope_stack[-1].constraint_l.append(ConstraintExprModel(e))
    return constraint_scope_stack.pop()
Beispiel #7
0
def push_constraint_stmt(s):
    for e in pop_exprs():
        constraint_scope_stack[-1].constraint_l.append(ConstraintExprModel(e))
    constraint_scope_stack[-1].constraint_l.append(s)
Beispiel #8
0
 def visit_constraint_expr(self, c: ConstraintExprModel):
     if self.do_copy_level > 0:
         ret = ConstraintExprModel(self.expr(c.e))
         self.constraints.append(ret)
     else:
         super().visit_constraint_expr(c)
Beispiel #9
0
def pop_constraint_scope():
    for e in pop_exprs():
        c = ConstraintExprModel(e)
        c.srcinfo = e.srcinfo
        constraint_scope_stack[-1].constraint_l.append(c)
    return constraint_scope_stack.pop()
Beispiel #10
0
def push_constraint_stmt(s):
    for e in pop_exprs():
        c = ConstraintExprModel(e)
        c.srcinfo = e.srcinfo
        constraint_scope_stack[-1].constraint_l.append(c)
    constraint_scope_stack[-1].constraint_l.append(s)
    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)