def _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, var_map, synth_fun, rule_data): ph_let_bound_vars, let_bound_vars = [], [] if type(rule_data) == tuple: value = sexp_to_value(rule_data) ret = grammars.ExpressionRewrite(exprs.ConstantExpression(value)) elif rule_data[0] == 'Constant': typ = sexp_to_type(rule_data[1]) ret = grammars.NTRewrite('Constant' + str(typ), typ) elif rule_data[0] in [ 'Variable', 'InputVariable', 'LocalVariable' ]: raise NotImplementedError('Variable rules in grammars') elif type(rule_data) == str: if rule_data in [ a.variable_info.variable_name for a in arg_vars ]: (parameter_position, variable) = next((i, x) for (i, x) in enumerate(arg_vars) if x.variable_info.variable_name == rule_data) expr = exprs.FormalParameterExpression(synth_fun, variable.variable_info.variable_type, parameter_position) ret = grammars.ExpressionRewrite(expr) elif rule_data in non_terminals: ret = grammars.NTRewrite(rule_data, nt_type[rule_data]) elif rule_data in var_map: ret = grammars.ExpressionRewrite(var_map[rule_data]) else: # Could be a 0 arity function func = syn_ctx.make_function(rule_data) if func != None: ret = grammars.ExpressionRewrite(syn_ctx.make_function_expr(rule_data)) else: # Could be a let bound variable bound_var_ph = exprs.VariableExpression(exprs.VariableInfo(exprtypes.BoolType(), 'ph_' + rule_data)) ph_let_bound_vars.append(bound_var_ph) ret = grammars.ExpressionRewrite(bound_var_ph) elif type(rule_data) == list: function_name = rule_data[0] if function_name != 'let': function_args = [] for child in rule_data[1:]: ph_lbv, lbv, arg = _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, var_map, synth_fun, child) ph_let_bound_vars.extend(ph_lbv) let_bound_vars.extend(lbv) function_args.append(arg) function_arg_types = tuple([ x.type for x in function_args ]) function = syn_ctx.make_function(function_name, *function_arg_types) else: def child_processing_func(rd, syn_ctx, new_var_map): ph_lbv, lbv, a = _process_rule(non_terminals, nt_type, syn_ctx, arg_vars, new_var_map, synth_fun, rd) ph_let_bound_vars.extend(ph_lbv) let_bound_vars.extend(lbv) return a def get_return_type(r): return r.type function, function_args = sexp_to_let(rule_data, syn_ctx, child_processing_func, get_return_type, var_map) let_bound_vars.extend(function.binding_vars) assert function is not None ret = grammars.FunctionRewrite(function, *function_args) else: raise Exception('Unknown right hand side: %s' % rule_data) return ph_let_bound_vars, let_bound_vars, ret
def string_to_constant_rewrite(topsymb): if topsymb.isdigit() or (topsymb.startswith('-') and len(topsymb) >= 2 and topsymb[1:].isdigit): num = -1 * int(topsymb[1:]) if topsymb.startswith('-') else int( topsymb) return grammars.ExpressionRewrite( exprs.ConstantExpression(exprs.Value(num, exprtypes.IntType()))) elif topsymb.startswith('#x'): # bitvector num = int('0' + topsymb[1:], 16) bitsize = (len(topsymb) - 2) * 4 return grammars.ExpressionRewrite( exprs.ConstantExpression( exprs.Value(num, exprtypes.BitVectorType(bitsize)))) else: # boolean return grammars.ExpressionRewrite( exprs.ConstantExpression( exprs.Value(bool(topsymb), exprtypes.BoolType())))
def make_constant_rules(constraints): constants = set() for constraint in constraints: constants |= exprs.get_all_constants(constraint) constants.add(exprs.ConstantExpression(exprs.Value(1, exprtypes.IntType(), -1))) constants.add(exprs.ConstantExpression(exprs.Value(0, exprtypes.IntType(), -1))) const_templates = [] for const in constants: const_template = grammars.ExpressionRewrite(const) const_templates.append(const_template) return const_templates