def to_smt(self, expr_object, smt_context_object, var_subst_map): child_terms = self._children_to_smt(expr_object, smt_context_object, var_subst_map) interpretation = smt_context_object.interpretation_map[self.unknown_function_id] if (interpretation == None): # treat this as an uninterpreted function child_types = [exprs.get_expression_type(child) for child in expr_object.children] child_types = [t.get_smt_type(smt_context_object) for t in child_types] child_types.append(self.range_type.get_smt_type(smt_context_object)) fun = z3.Function(self.function_name, *child_types) return fun(*child_terms) # we have an interpretation, recurse on that return expression_to_smt(interpretation, smt_context_object, child_terms)
def instantiate(self, function_name, child_exps): canonical_function_name = self._get_canonical_function_name(function_name) if len(child_exps) == 0: arg_types = tuple() elif (not isinstance(child_exps[0], exprtypes.TypeBase)): arg_types = tuple([exprs.get_expression_type(x) for x in child_exps]) else: arg_types = tuple(child_exps) mangled_name = mangle_function_name(canonical_function_name, arg_types) cached = self.find_cached(mangled_name) if (cached != None): return cached retval = self._do_instantiation(function_name, mangled_name, arg_types) if (retval != None): self.add_to_cache(retval.mangled_function_name, retval) return retval
def make_function_expr(self, function_name_or_info, *child_exps): """Makes a typed function expression applied to the given child expressions.""" if (isinstance(function_name_or_info, str)): function_info = self.make_function(function_name_or_info, *child_exps) function_name = function_name_or_info else: assert (isinstance(function_name_or_info, semantics_types.FunctionBase)) function_info = function_name_or_info function_name = function_info.function_name if (function_info == None): raise basetypes.ArgumentError( 'Could not instantiate function named "' + function_name + '" with argument types: (' + ', '.join( [str(exprs.get_expression_type(x)) for x in child_exps]) + ')') return exprs.FunctionExpression(function_info, tuple(child_exps))
def __init__(self, expr): super().__init__(exprs.get_expression_type(expr)) self.expr = expr
def make_default_grammar(syn_ctx, theory, return_type, args): int_type = exprtypes.IntType() bool_type = exprtypes.BoolType() if theory == 'LIA': [start, start_bool, const] = ['Start', 'StartBool', 'ConstantIntegerType'] nts = [start, start_bool, const] nt_type = {start: int_type, start_bool: bool_type, const: int_type} rules = {start: [], start_bool: [], const: []} ntr_start = NTRewrite(start, int_type) ntr_startbool = NTRewrite(start_bool, bool_type) ntr_const = NTRewrite(const, int_type) [ add_func, sub_func, mul_func, div_func, mod_func ] = \ [ syn_ctx.make_function(name, int_type, int_type) for name in [ 'add', 'sub', 'mul', 'div', 'mod' ] ] ite_func = syn_ctx.make_function('ite', bool_type, int_type, int_type) [ and_func, or_func ] = \ [ syn_ctx.make_function(name, bool_type, bool_type) for name in [ 'and', 'or' ] ] not_func = syn_ctx.make_function('not', bool_type) [ eq_func, ne_func, le_func, lt_func, ge_func, gt_func ] = \ [ syn_ctx.make_function(name, int_type, int_type) for name in [ '=', 'ne', '<=', '<', '>=', '>' ] ] # Start rules: # Args for arg in args: if exprs.get_expression_type(arg) == int_type: rules[start].append(ExpressionRewrite(arg)) elif exprs.get_expression_type(arg) == bool_type: rules[start_bool].append(ExpressionRewrite(arg)) # Constants rules[start].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(1, int_type)))) rules[start].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(0, int_type)))) rules[start].append(ntr_const) # Start + Start, Start - Start, rules[start].append(FunctionRewrite(add_func, ntr_start, ntr_start)) rules[start].append(FunctionRewrite(sub_func, ntr_start, ntr_start)) # Start * Constant, Start / Constant, Start mod Constant rules[start].append(FunctionRewrite(mul_func, ntr_start, ntr_start)) rules[start].append(FunctionRewrite(div_func, ntr_start, ntr_start)) rules[start].append(FunctionRewrite(mod_func, ntr_start, ntr_start)) # ITE rules[start].append( FunctionRewrite(ite_func, ntr_startbool, ntr_start, ntr_start)) # Start bool rules # And, or, not rules[start_bool].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(False, bool_type)))) rules[start_bool].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(True, bool_type)))) rules[start_bool].append( FunctionRewrite(and_func, ntr_startbool, ntr_startbool)) rules[start_bool].append( FunctionRewrite(or_func, ntr_startbool, ntr_startbool)) rules[start_bool].append(FunctionRewrite(not_func, ntr_startbool)) # comparison ops rules[start_bool].append(FunctionRewrite(eq_func, ntr_start, ntr_start)) rules[start_bool].append(FunctionRewrite(ne_func, ntr_start, ntr_start)) rules[start_bool].append(FunctionRewrite(le_func, ntr_start, ntr_start)) rules[start_bool].append(FunctionRewrite(lt_func, ntr_start, ntr_start)) rules[start_bool].append(FunctionRewrite(ge_func, ntr_start, ntr_start)) rules[start_bool].append(FunctionRewrite(gt_func, ntr_start, ntr_start)) # Constant rules rules[const].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(1, int_type)))) rules[const].append( ExpressionRewrite( exprs.ConstantExpression(exprs.Value(0, int_type)))) rules[const].append(FunctionRewrite(add_func, ntr_const, ntr_const)) rules[const].append(FunctionRewrite(add_func, ntr_const, ntr_const)) rules[const].append(FunctionRewrite(sub_func, ntr_const, ntr_const)) if return_type == int_type: ret = Grammar(nts, nt_type, rules, start) elif return_type == bool_type: ret = Grammar(nts, nt_type, rules, start_bool) else: raise NotImplementedError ret.from_default = True return ret elif theory == 'BV': from semantics import semantics_bv from semantics import semantics_core print("ARSAYS: Default bit-vec grammar shouldn't be used!") (start, start_bool) = ('Start', 'StartBool') bv_size = 64 nts = [start, start_bool] (bv_type, bool_type) = (exprtypes.BitVectorType(bv_size), exprtypes.BoolType()) nt_type = {start: bv_type, start_bool: bool_type} rules = {start: [], start_bool: []} ntr_start = NTRewrite(start, bv_type) ntr_start_bool = NTRewrite(start_bool, bool_type) rules[start].extend( map( lambda x: ExpressionRewrite( exprs.ConstantExpression(exprs.Value(x, bv_type))), [0, 1])) for func in [ semantics_bv.BVNot(bv_size), semantics_bv.BVAdd(bv_size), semantics_bv.BVAnd(bv_size), semantics_bv.BVOr(bv_size), semantics_bv.BVNeg(bv_size), semantics_bv.BVAdd(bv_size), semantics_bv.BVMul(bv_size), semantics_bv.BVSub(bv_size), semantics_bv.BVUDiv(bv_size), semantics_bv.BVSDiv(bv_size), semantics_bv.BVSRem(bv_size), semantics_bv.BVURem(bv_size), semantics_bv.BVShl(bv_size), semantics_bv.BVLShR(bv_size), semantics_bv.BVAShR(bv_size), semantics_bv.BVUlt(bv_size), semantics_bv.BVUle(bv_size), semantics_bv.BVUge(bv_size), semantics_bv.BVUgt(bv_size), semantics_bv.BVSle(bv_size), semantics_bv.BVSlt(bv_size), semantics_bv.BVSge(bv_size), semantics_bv.BVSgt(bv_size), semantics_bv.BVXor(bv_size), semantics_bv.BVXNor(bv_size), semantics_bv.BVNand(bv_size), semantics_bv.BVNor(bv_size), # semantics_bv.BVComp(bv_size) semantics_core.EqFunction(bv_type) ]: assert all([t == bv_type for t in func.domain_types]) args = [ntr_start] * len(func.domain_types) if func.range_type == bv_type: rules[start].append(FunctionRewrite(func, *args)) elif func.range_type == bool_type: rules[start_bool].append(FunctionRewrite(func, *args)) else: assert False ite_func = semantics_core.IteFunction(bv_type) rules[start].append( FunctionRewrite(ite_func, ntr_start_bool, ntr_start, ntr_start)) if return_type == bv_type: ret = Grammar(nts, nt_type, rules, start) elif return_type == bool_type: ret = Grammar(nts, nt_type, rules, start_bool) else: raise NotImplementedError ret.from_default = True return ret elif theory == 'SLIA': raise NotImplementedError else: raise NotImplementedError
rewrite2cnt = {} rewrite2loc = {} all_seqs = [] for nth, (fun_exprs, grammar) in enumerate(fun_exprs_grammars): printProgressBar(nth, len(fun_exprs_grammars), prefix='Progress:', suffix='Complete', length=100) target_exprs = [] for fun_expr in fun_exprs: for e in exprs.get_all_exprs(fun_expr): if exprs.get_expression_size( e) > 2 and exprs.get_expression_type( e) == exprs.get_expression_type(fun_expr): target_exprs.append(e) for expr in target_exprs: deriv_seqs = get_derivation_sequences(expr, grammar) for i, deriv_seq in enumerate(deriv_seqs): if len(deriv_seq) <= 2: continue for j, deriv in enumerate(deriv_seq[2:]): if not str(deriv) in rewrite2cnt: rewrite2cnt[str(deriv)] = 0 rewrite2cnt[str(deriv)] += 1 rewrite2loc[str(deriv)] = (len(all_seqs) + i, j + 2) all_seqs = all_seqs + deriv_seqs items = [(deriv_str, cnt) for deriv_str, cnt in rewrite2cnt.items() if cnt >= occurrence_threshold] items.sort(key=lambda x: x[1], reverse=True)
def is_bool_expr(expr): return isinstance(exprs.get_expression_type(expr), exprtypes._BoolType)
def evaluate_expression(expr_object, eval_context): retval = evaluate_expression_raw(expr_object, eval_context) retval = exprs.Value(retval, exprs.get_expression_type(expr_object), -1) return retval
def fetchtype(expr): return str(exprs.get_expression_type(expr))