def convert(self, formula): z3term = self.walk(formula) if formula.node_type in op.QUANTIFIERS: return z3.QuantifierRef(z3term, self.ctx) elif formula.node_type() in BOOLREF_SET: return z3.BoolRef(z3term, self.ctx) elif formula.node_type() in ARITHREF_SET: return z3.ArithRef(z3term, self.ctx) elif formula.node_type() in BITVECREF_SET: return z3.BitVecRef(z3term, self.ctx) elif formula.is_symbol() or formula.is_function_application(): if formula.is_function_application(): type_ = formula.function_name().symbol_type() type_ = type_.return_type else: type_ = formula.symbol_type() if type_.is_bool_type(): return z3.BoolRef(z3term, self.ctx) elif type_.is_real_type() or type_.is_int_type(): return z3.ArithRef(z3term, self.ctx) elif type_.is_array_type(): return z3.ArrayRef(z3term, self.ctx) elif type_.is_bv_type(): return z3.BitVecRef(z3term, self.ctx) else: raise NotImplementedError(formula) elif formula.node_type() in op.ARRAY_OPERATORS: return z3.ArrayRef(z3term, self.ctx) else: assert formula.is_constant(), formula type_ = formula.constant_type() if type_.is_bool_type(): return z3.BoolRef(z3term, self.ctx) elif type_.is_real_type() or type_.is_int_type(): return z3.ArithRef(z3term, self.ctx) elif type_.is_array_type(): return z3.ArrayRef(z3term, self.ctx) elif type_.is_bv_type(): return z3.BitVecRef(z3term, self.ctx) else: raise NotImplementedError(formula)
def z3_bv_to_int(x): """BitVector to Integer Z3 conversion Parameters ---------- x : z3.BitVecRef or z3.BitVecNumRef BitVector variable to be converted to Int Returns ------- z3.ArithRef z3.ArithRef is an expression that will convert the BitVec into Integer inside Z3 rather than before insertion into the solver. This function wraps Z3 C API functions to allow for a python interpretation of BitVec to Int conversions. The returned object is an expression that Z3 will evaluate as an Int rather than BitVec during solving. Example ------- If you want to convert a BitVec into an Int:: In [1]: import z3 In [2]: from pySym import pyState.z3Helpers In [3]: s = z3.Solver() In [4]: x = z3.BitVec('x',32) In [5]: y = z3.Int('y') In [6]: x = pyState.z3Helpers.z3_bv_to_int(x) In [7]: s.add(x == y) In [8]: s Out[8]: [BV2Int(x) == y] In [9]: s.check() Out[9]: sat """ return z3.ArithRef(z3.Z3_mk_bv2int(x.ctx_ref(), x.as_ast(), 0), x.ctx)