Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
def z3_int_to_bv(x,size=Z3_DEFAULT_BITVEC_SIZE):
    """Integer to BitVector Z3 conversion
    
    Parameters
    ----------
    x : z3.ArithRef or z3.IntNumRef
        Int variable to be converted to BitVec
    size : int, optional
        BitVec bit size. If not specified, defaults to
        pyState.z3Helpers.Z3_DEFAULT_BITVEC_SIZE


    Returns
    -------
    z3.BitVecRef
        This is the BitVec reference for the associated Int
    
    
    This function wraps Z3 C API functions to allow for a python interpretation
    of Int to BitVec conversions. The returned object is an expression that Z3
    will evaluate as an BitVec rather than Int during solving.


    Example
    -------
    If you want to convert an Int int into a BitVec::

        In [1]: import z3
    
        In [2]: from pySym import pyState.z3Helpers
    
        In [3]: s = z3.Solver()
    
        In [4]: x = z3.BitVec('x',8)
    
        In [5]: y = z3.Int('y')

        In [6]: y = pyState.z3Helpers.z3_int_to_bv(y,8)
    
        In [7]: s.add(x == y)
    
        In [8]: s
        Out[8]: [x == int2bv(y)]
    
        In [9]: s.check()
        Out[9]: sat

    """

    assert type(x) in [z3.IntNumRef,z3.ArithRef]
    assert x.is_int()
    # Converts Int to BV
    return z3.BitVecRef(z3.Z3_mk_int2bv(x.ctx_ref(),size,x.as_ast()))
Ejemplo n.º 3
0
def BVXnor(a, b):
    return z3.BitVecRef(z3.Z3_mk_bvxnor(a.ctx_ref(), a.as_ast(), b.as_ast()),
                        a.ctx)
Ejemplo n.º 4
0
def BVRedAnd(a):
    return z3.BitVecRef(z3.Z3_mk_bvredand(a.ctx_ref(), a.as_ast()), a.ctx)
Ejemplo n.º 5
0
def NAND(a, b):
    z3._check_bv_args(a, b)
    a, b = _coerce_exprs(a, b)
    return z3.BitVecRef(Z3_mk_bvnand(a.ctx_ref(), a.as_ast(), b.as_ast()), a.ctx)