Ejemplo n.º 1
0
def evaluate_expression_on_stack(expr_object, eval_context):
    # print("Evaluating", exprs.expression_to_string(expr_object))
    old_stack_size = eval_context.eval_stack_top
    kind = expr_object.expr_kind
    if (kind == _variable_expression):
        o = expr_object.variable_info.variable_eval_offset
        if o == exprs.VariableInfo._undefined_offset:
            value = eval_context.lookup_let_variable(expr_object)
            if value is not None:
                eval_context.push(value)
            else:
                raise basetypes.UnboundLetVariableError()
        else:
            eval_context.push(eval_context.valuation_map[o].value_object)
    elif (kind == _formal_parameter_expression):
        o = expr_object.parameter_position
        eval_context.push(eval_context.valuation_map[o].value_object)
    elif (kind == _constant_expression):
        eval_context.push(expr_object.value_object.value_object)
    elif (kind == _function_expression):
        fun_info = expr_object.function_info
        fun_info.evaluate(expr_object, eval_context)
    else:
        raise basetypes.UnhandledCaseError('Odd expression kind: %s' % kind)
    assert old_stack_size == eval_context.eval_stack_top - 1
Ejemplo n.º 2
0
def get_expression_type(expr):
    """Returns the type of the expression."""
    kind = expr.expr_kind
    if (kind == _variable_expression):
        return expr.variable_info.variable_type
    elif (kind == _formal_parameter_expression):
        return expr.parameter_type
    elif (kind == _constant_expression):
        return expr.value_object.value_type
    elif (kind == _function_expression):
        return expr.function_info.range_type
    else:
        raise basetypes.UnhandledCaseError('Odd expression kind: %s' % expr.expr_kind)
Ejemplo n.º 3
0
def get_expression_size(expr):
    """Returns the (syntactic) size of the expression."""
    kind = expr.expr_kind
    if (kind == _variable_expression or
        kind == _constant_expression or
        kind == _formal_parameter_expression):
        return 1
    elif (expr.expr_kind == ExpressionKinds.function_expression):
        retval = 1
        for child in expr.children:
            retval += get_expression_size(child)
        return retval
    else:
        raise basetypes.UnhandledCaseError('Odd expression kind: %s' % expr.expr_kind)
def expression_to_smt(expr_object, smt_context_object, var_subst_map = None):
    kind = expr_object.expr_kind
    if (kind == exprs.ExpressionKinds.variable_expression):
        ret = _to_smt_variable_expression(expr_object, smt_context_object)
    elif (kind == exprs.ExpressionKinds.formal_parameter_expression):
        ret = var_subst_map[expr_object.parameter_position]
    elif (kind == exprs.ExpressionKinds.constant_expression):
        ret = _to_smt_constant_expression(expr_object, smt_context_object)
    elif (kind == exprs.ExpressionKinds.function_expression):
        fun_info = expr_object.function_info
        ret = fun_info.to_smt(expr_object, smt_context_object, var_subst_map)
    else:
        raise basetypes.UnhandledCaseError('Odd expression kind: %s' % kind)
    return ret
def _to_smt_constant_expression(expr_object, smt_context_object):
    val_obj = expr_object.value_object
    constant_type = val_obj.value_type
    if (constant_type.type_code == exprtypes.TypeCodes.boolean_type):
        return z3.BoolVal(val_obj.value_object, smt_context_object.ctx())
    elif (constant_type.type_code == exprtypes.TypeCodes.integer_type):
        return z3.IntVal(val_obj.value_object, smt_context_object.ctx())
    elif (constant_type.type_code == exprtypes.TypeCodes.bit_vector_type):
        int_value = val_obj.value_object.value
        return z3.BitVecVal(int_value, constant_type.size,
                            smt_context_object.ctx())
    elif (constant_type.type_code == exprtypes.TypeCodes.string_type):
        return z3.StringVal(val_obj.value_object, smt_context_object.ctx())
    else:
        raise basetypes.UnhandledCaseError('Odd type code: %s' % constant_type.type_code)
Ejemplo n.º 6
0
def evaluate_expression_on_stack(expr_object, eval_context):
    # print('Evaluating:', exprs.expression_to_string(expr_object))
    old_stack_size = eval_context.eval_stack_top
    # if expr_object is not None and eval_context.valuation_map is not None and eval_context.interpretation_map is not None:
    #     key = (exprs.expression_to_string(expr_object), tuple([exprs.value_to_string(v) for v in eval_context.valuation_map]),
    #             tuple(eval_context.interpretation_map.items()))
    # else:
    #     key = None
    # key = None
    kind = expr_object.expr_kind
    # if key in evaluate_expression_on_stack.cache and (kind != _function_expression):
    # print('cached')
    # eval_context.push(evaluate_expression_on_stack.cache[key])
    # else:
    if (kind == _variable_expression):
        o = expr_object.variable_info.variable_eval_offset
        if o == exprs.VariableInfo._undefined_offset:
            value = eval_context.lookup_let_variable(expr_object)
            if value is not None:
                eval_context.push(value)
            else:
                print("Failed to lookup: {}".format(expr_object))
                raise basetypes.UnboundLetVariableError()
        else:
            eval_context.push(eval_context.valuation_map[o].value_object)
    elif (kind == _formal_parameter_expression):
        o = expr_object.parameter_position
        eval_context.push(eval_context.valuation_map[o].value_object)
    elif (kind == _constant_expression):
        eval_context.push(expr_object.value_object.value_object)
    elif (kind == _function_expression):
        fun_info = expr_object.function_info
        fun_info.evaluate(expr_object, eval_context)
    else:
        raise basetypes.UnhandledCaseError('Odd expression kind: %s' % kind)

    # if key is not None and key not in evaluate_expression_on_stack.cache:
    #     evaluate_expression_on_stack.cache[key] = eval_context.peek()
    assert old_stack_size == eval_context.eval_stack_top - 1
Ejemplo n.º 7
0
 def clone(self):
     raise basetypes.UnhandledCaseError('PointDistinctGenerator.clone()')