Ejemplo n.º 1
0
static STATEMENT *reduce_statement(MODULE *module, FUNCTION *func, BLOCK *block, STATEMENT *stmt)
{
    if (stmt == NULL)
        return stmt;
    
    if (tree_is_type(stmt, STMT_ASSIGN))
    {
        EXPRESSION *expr = tree_get_child(stmt, 1);
        expr = simplify_expression(module, func, block, expr, stmt);
        tree_get_child(stmt, 1) = expr;
    }
    else if (tree_is_type(stmt, STMT_IF))
    {
        EXPRESSION *cond = tree_get_child(stmt, 0);
        cond = simplify_expression(module, func, block, cond, stmt);
        tree_get_child(stmt, 0) = cond;
        reduce_block(module, func, tree_get_child(stmt, 1));
        reduce_block(module, func, tree_get_child(stmt, 2));
    }
    else if (tree_is_type(stmt, STMT_WHILE))
    {
        EXPRESSION *cond = tree_get_child(stmt, 0);
        BLOCK *body = tree_get_child(stmt, 1);
        if (!is_atomic(cond))
        {
            EXPRESSION *old_cond = cond;
            cond = atomise_expression(module, func, block, cond, stmt);
            tree_get_child(stmt, 0) = cond;
            STATEMENT *new_assign = make_assignment(cond, CAST_TO_EXPRESSION(tree_copy(old_cond)), CAST_TO_AST(cond)->source_line);
            tree_add_child(body, new_assign);
        }
        reduce_block(module, func, body);
    }
    else if (tree_is_type(stmt, STMT_RETURN))
    {
        EXPRESSION *expr = tree_get_child(stmt, 0);
        expr = atomise_expression(module, func, block, expr, stmt);
        tree_get_child(stmt, 0) = expr;
    }
    else if (tree_is_type(stmt, STMT_RESTART))
    {
        /* Do nothing. */
    }
    else
        error("Not sure how to reduce statement of type %d\n", tree_type(stmt));
    
    return stmt;
}
Ejemplo n.º 2
0
static int reduce_graph(MODULE *module, FUNCTION *func, GRAPH *graph)
{
    int i;
    int num;
restart:
    num = tree_num_children(graph);
    for (i = 0; i < num; i++)
    {
        NODE *vertex = tree_get_child(graph, i);
        if (!vertex)
            continue;
        
        if (tree_is_type(vertex, STMT_ASSIGN))
        {
            EXPRESSION *expr = tree_get_child(vertex, 1);
            expr = simplify_expression(module, func, tree_get_child(func, 0), expr, CAST_TO_STATEMENT(vertex));
            tree_get_child(vertex, 1) = expr;
        }
        else if (tree_is_type(vertex, STMT_TEST))
        {
            EXPRESSION *expr = tree_get_child(vertex, 0);
            expr = simplify_expression(module, func, tree_get_child(func, 0), expr, CAST_TO_STATEMENT(vertex));
            tree_get_child(vertex, 0) = expr;
        }
        else if (tree_is_type(vertex, STMT_RETURN))
        {
            EXPRESSION *expr = tree_get_child(vertex, 0);
            expr = atomise_expression(module, func, tree_get_child(func, 0), expr, CAST_TO_STATEMENT(vertex));
            tree_get_child(vertex, 0) = expr;
        }
    }
    
    if (num != tree_num_children(graph))
        goto restart;
    
    return 1;
}
Ejemplo n.º 3
0
expr_ptr simplify_expression(expr_ptr&& expr)
{
	expr_ptr arg{expr.release()};
	return simplify_expression(arg);
}