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))
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))
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
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])
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])
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)
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('ν<sub>%s</sub>' % idx, 1) else: return to_format(xs[sz - idx - 1])
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("ν<sub>%s</sub>" % idx, 1) else: return to_format(xs[sz - idx - 1])
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)]
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()))
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
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)
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)
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)