Пример #1
0
def extract_equal(eq):
    """Transform equals in a triple: var index, value, mask"""
    if isinstance(eq.children()[0], z3.BitVecNumRef):
        rhs = eq.children()[0]
        lhs = eq.children()[1]
    else:
        rhs = eq.children()[1]
        lhs = eq.children()[0]
    if z3.is_var(lhs):
        return ResultItem(
            var=z3.get_var_index(lhs),
            value=rhs,
            mask=None)
    else:
        kind = lhs.decl().kind()
        if kind == z3.Z3_OP_EXTRACT:
            [high, low] = lhs.params()
            sort = lhs.children()[0].sort()
            val = rhs.as_long() << low
            mask = (1 << (high + 1)) - (1 << low)
            return ResultItem(
                var=z3.get_var_index(lhs.children()[0]),
                value=z3.BitVecVal(val, sort),
                mask=z3.BitVecVal(mask, sort))
        else:
            raise base.Z3NotWellFormed(
                "Bad lhs for equal  {}".format(eq))
Пример #2
0
def find_vars (ast):
    """Returns the set of all variables in a given expression"""
    vars = list()
    def add(expr):
        if z3.is_var(expr):
            vars.append(expr)

    foreach_expr(add, ast)

    return sorted(vars, lambda x, y: z3.get_var_index(x) - z3.get_var_index(y))
Пример #3
0
 def extract_eq(expr):
     kind = expr.decl().kind()
     children = expr.children()
     if kind == z3.Z3_OP_EQ:
         lhs = children[0]
         rhs = children[1]
         if z3.is_var(lhs):
             return z3.get_var_index(lhs), rhs
         elif z3.is_var(rhs):
             return z3.get_var_index(rhs), lhs
         else:
             return None
     return None
Пример #4
0
 def pp_var(self, a, d, xs):
     idx = z3.get_var_index(a)
     sz  = len(xs)
     if idx >= sz:
         return seq1('Var', (to_format(idx),))
     else:
         return to_format(xs[sz - idx - 1])
Пример #5
0
 def pp_var(self, a, d, xs):
     idx = z3.get_var_index(a)
     sz = len(xs)
     if idx >= sz:
         return seq1("Var", (to_format(idx),))
     else:
         return to_format(xs[sz - idx - 1])
Пример #6
0
    def translate(self, expr, bound_variables=[]):
        if z3.is_const(expr):
            return self.mk_const(expr)
#                raise Z3_Unexpected_Expression('Unrecognized constant')
        elif z3.is_var(expr):    # a de Bruijn indexed bound variable
            bv_length = len(bound_variables)
            return bound_variables[bv_length - z3.get_var_index(expr) - 1]
        elif z3.is_app(expr):
            args = [self.translate(expr.arg(i), bound_variables)
                for i in range(expr.num_args())]
            return self.mk_fun(expr.decl())(*args)

#            else:
#                raise Z3_Unexpected_Expression(expr)
        elif z3.is_quantifier(expr):
            num_vars = expr.num_vars()
#            vars = [language.const_dict[expr.var_name(i)]
#                for i in range(num_vars)]
            vars = [const(expr.var_name(i), self.mk_sort(expr.var_sort(i))) \
                for i in range(num_vars)]
            new_bound_variables = bound_variables + vars
            body = self.translate(expr.body(), new_bound_variables)
            if expr.is_forall():
                return forall(vars, body)
            else:
                return exists(vars, body)

        elif z3.is_func_decl(expr):
            return self.mk_fun(expr)
        else:
            print expr.kind
            raise Z3_Unexpected_Expression(expr)
Пример #7
0
    def translate(self, expr, bound_variables=[]):
        if z3.is_const(expr):
            return self.mk_const(expr)
#                raise Z3_Unexpected_Expression('Unrecognized constant')
        elif z3.is_var(expr):    # a de Bruijn indexed bound variable
            bv_length = len(bound_variables)
            return bound_variables[bv_length - z3.get_var_index(expr) - 1]
        elif z3.is_app(expr):
            args = [self.translate(expr.arg(i), bound_variables)
                for i in range(expr.num_args())]
            return self.mk_fun(expr.decl())(*args)

#            else:
#                raise Z3_Unexpected_Expression(expr)
        elif z3.is_quantifier(expr):
            num_vars = expr.num_vars()
#            vars = [language.const_dict[expr.var_name(i)]
#                for i in range(num_vars)]
            vars = [const(expr.var_name(i), self.mk_sort(expr.var_sort(i))) \
                for i in range(num_vars)]
            new_bound_variables = bound_variables + vars
            body = self.translate(expr.body(), new_bound_variables)
            if expr.is_forall():
                return forall(vars, body)
            else:
                return exists(vars, body)

        elif z3.is_func_decl(expr):
            return self.mk_fun(expr)
        else:
            print expr.kind
            raise Z3_Unexpected_Expression(expr)
Пример #8
0
 def pp_var(self, a, d, xs):
     idx = z3.get_var_index(a)
     sz  = len(xs)
     if idx >= sz:
         # 957 is the greek letter nu
         return to_format('&#957;<sub>%s</sub>' % idx, 1)
     else:
         return to_format(xs[sz - idx - 1])
Пример #9
0
 def pp_var(self, a, d, xs):
     idx = z3.get_var_index(a)
     sz = len(xs)
     if idx >= sz:
         # 957 is the greek letter nu
         return to_format("&#957;<sub>%s</sub>" % idx, 1)
     else:
         return to_format(xs[sz - idx - 1])
Пример #10
0
 def convert(t, values):
     if z3.is_int_value(t):
         return t.as_long()
     if z3.is_app(t):
         func = globals()[t.decl().name()]
         return func(
             *[convert(t.arg(i), values) for i in range(t.num_args())])
     elif z3.is_var(t):
         return values[z3.get_var_index(t)]
Пример #11
0
def check_chc_head(expr):
    if u_predicate(expr):
        decl = expr.decl()
        if decl is not None:
            known_vars = Set([])
            for kid in expr.children():
                if not z3.is_var(kid):
                    raise Exception("illegal head: " +
                                    "argument {} is not a variable {}".format(
                                        expr.sexpr(), kid.sexpr()))
                index = z3.get_var_index(kid)
                if index in known_vars:
                    raise Exception("illegal head: non-distinct arguments, " +
                                    "{} is used twice in {}".format(
                                        kid.sexpr(), expr.sexpr()))
                known_vars.add(z3.get_var_index(kid))
        return False
    elif expr == z3.BoolVal(False):
        return True
    else:
        raise Exception("illegal head: {}".format(expr.sexpr()))
Пример #12
0
def rec_go(formula, var_list, reduction_type, q_type, bit_places, polarity):
    """Recursively go through the formula and apply approximations.
    """
    # Constant
    if z3.is_const(formula):
        pass

    # Variable
    elif z3.is_var(formula):
        order = - z3.get_var_index(formula) - 1

        # Process if it is bit-vector variable
        if type(formula) == z3.BitVecRef:
            # Update max bit-vector width
            global max_bit_width
            if max_bit_width < formula.size():
                max_bit_width = formula.size()

            # Approximate if var is quantified in the right way
            if var_list[order][1] == q_type:
                formula = approximate(formula, reduction_type, bit_places)

    # Quantified formula
    elif type(formula) == z3.QuantifierRef:
        formula = qform_process(formula,
                                list(var_list),
                                reduction_type,
                                q_type,
                                bit_places,
                                polarity)

    # Complex formula
    else:
        formula = cform_process(formula,
                                list(var_list),
                                reduction_type,
                                q_type,
                                bit_places,
                                polarity)

    return formula
Пример #13
0
def bound_var_sort (quant, var):
  if z3.is_var (var):
    var = z3.get_var_index ()
  return quant.var_sort (quant.num_vars () - 1 - var)
Пример #14
0
 def var_name(self,rule,i):
     """ yields the name of a variable """
     if z3.is_var(i): i = z3.get_var_index()
     # (variables are numbered backwards within the predicates)
     return rule.var_name(rule.num_vars()-1-i)
Пример #15
0
def bound_var_sort (quant, var):
  if z3.is_var (var):
    var = z3.get_var_index ()
  return quant.var_sort (quant.num_vars () - 1 - var)
Пример #16
0
 def var_name(self, rule, i):
     """ yields the name of a variable """
     if z3.is_var(i):
         i = z3.get_var_index()
     # (variables are numbered backwards within the predicates)
     return rule.var_name(rule.num_vars() - 1 - i)