if (SCF_OPTIMIZER_GLOBAL == opt->flags) {
- int ret = opt->optimize(ast, NULL, NULL, functions);
+ int ret = opt->optimize(ast, NULL, functions);
if (ret < 0) {
scf_loge("optimizer: %s\n", opt->name);
return ret;
if (!f->node.define_flag)
continue;
- int ret = opt->optimize(ast, f, &f->basic_block_list_head, NULL);
+ int ret = opt->optimize(ast, f, NULL);
if (ret < 0) {
scf_loge("optimizer: %s\n", opt->name);
return ret;
{
const char* name;
- int (*optimize)(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions);
+ int (*optimize)(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions);
uint32_t flags;
};
return count;
}
-static int _optimize_active_vars(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_active_vars(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
int count;
int ret;
- int i;
- for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_next(l)) {
+ if (scf_list_empty(bb_list_head))
+ return 0;
+
+ for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
bb = scf_list_data(l, scf_basic_block_t, list);
return 0;
}
-static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!ast || !f || !bb_list_head)
+ if (!ast || !f)
return -EINVAL;
if (!strcmp(f->node.w->text->data, "scf__auto_malloc"))
return 0;
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
scf_basic_block_t* bb2;
return 0;
}
-static int _optimize_auto_gc_find(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_auto_gc_find(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
if (!ast || !functions || functions->size <= 0)
return -EINVAL;
return 0;
}
-static int _optimize_basic_block(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_basic_block(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- int count;
- int ret;
- int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
// scf_logi("------- %s() ------\n", f->node.w->text->data);
-// scf_basic_block_print_list(bb_list_head);
-#if 1
- for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_next(l)) {
+
+ for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
bb = scf_list_data(l, scf_basic_block_t, list);
continue;
}
- ret = __optimize_basic_block(bb, f);
+ int ret = __optimize_basic_block(bb, f);
if (ret < 0) {
scf_loge("\n");
return ret;
}
}
-#endif
+
// scf_basic_block_print_list(bb_list_head);
return 0;
}
return 0;
}
-static int _optimize_call(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_call(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- int ret = 0;
+ if (scf_list_empty(bb_list_head))
+ return 0;
for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); ) {
if (!bb->call_flag)
continue;
- ret = _optimize_call_bb(bb, bb_list_head);
+ int ret = _optimize_call_bb(bb, bb_list_head);
if (ret < 0)
return ret;
}
- ret = 0;
-error:
- return ret;
+ return 0;
}
scf_optimizer_t scf_optimizer_call =
return 0;
}
-static int _optimize_const_teq(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_const_teq(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- int count;
- int ret;
- int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
l = scf_list_next(l)) {
if (!bb->cmp_flag)
continue;
- ret = __optimize_const_teq(bb, f);
+ int ret = __optimize_const_teq(bb, f);
if (ret < 0) {
scf_loge("\n");
return ret;
}
}
-// scf_const_teq_print_list(bb_list_head);
return 0;
}
#include"scf_optimizer.h"
-static int _optimize_dag(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_dag(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- int count;
- int ret;
- int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
f->nb_basic_blocks = 0;
- for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_next(l)) {
+ for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
- bb = scf_list_data(l, scf_basic_block_t, list);
+ bb = scf_list_data(l, scf_basic_block_t, list);
bb->index = f->nb_basic_blocks++;
- ret = scf_basic_block_dag(bb, bb_list_head, &f->dag_list_head);
+ int ret = scf_basic_block_dag(bb, bb_list_head, &f->dag_list_head);
if (ret < 0) {
scf_loge("\n");
return ret;
return ret;
}
-static int _optimize_dominators_normal(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_dominators_normal(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
int ret;
int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
+
scf_vector_clear(f->dfs_tree, free);
ret = _bb_dfs_tree(bb_list_head, f);
return ret;
}
-static int _optimize_dominators_reverse(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_dominators_reverse(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
int ret;
int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
+
scf_vector_clear(f->dfs_tree, free);
ret = _bb_dfs_tree(bb_list_head, f);
return 0;
}
-static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
+
if (scf_list_empty(bb_list_head))
return 0;
return 0;
}
-static int _optimize_group(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_group(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
+
if (scf_list_empty(bb_list_head))
return 0;
return 0;
}
-static int _optimize_inline(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_inline(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
if (!ast || !functions || functions->size <= 0)
return -EINVAL;
#include"scf_optimizer.h"
-static int _optimize_loads_saves(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_loads_saves(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- scf_dag_node_t* dn;
- int count;
- int ret;
- int i;
+ if (scf_list_empty(bb_list_head))
+ return 0;
for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
bb = scf_list_data(l, scf_basic_block_t, list);
- ret = scf_basic_block_loads_saves(bb, bb_list_head);
+ int ret = scf_basic_block_loads_saves(bb, bb_list_head);
if (ret < 0)
return ret;
}
return 0;
}
-static int _optimize_loop(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_loop(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
+
if (scf_list_empty(bb_list_head))
return 0;
return 0;
}
-static int _optimize_pointer_alias(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_pointer_alias(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
- int ret = 0;
+ if (scf_list_empty(bb_list_head))
+ return 0;
for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); ) {
if (!bb->dereference_flag)
continue;
- ret = _optimize_alias_bb(bb, bb_list_head);
+ int ret = _optimize_alias_bb(bb, bb_list_head);
if (ret < 0)
return ret;
}
// scf_basic_block_print_list(bb_list_head);
- ret = 0;
-error:
- return ret;
+ return 0;
}
scf_optimizer_t scf_optimizer_pointer_alias =
return count;
}
-static int _optimize_pointer_aliases(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list_head, scf_vector_t* functions)
+static int _optimize_pointer_aliases(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
- if (!f || !bb_list_head)
+ if (!f)
return -EINVAL;
- if (scf_list_empty(bb_list_head))
- return 0;
-
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* start;
scf_basic_block_t* end;
int count;
int ret;
+ if (scf_list_empty(bb_list_head))
+ return 0;
+
l = scf_list_head(bb_list_head);
start = scf_list_data(l, scf_basic_block_t, list);
static int __scf_op_const_call(scf_ast_t* ast, scf_function_t* f, void* data);
+static int _scf_op_const_node(scf_ast_t* ast, scf_node_t* node, scf_handler_data_t* d)
+{
+ scf_operator_t* op = node->op;
+
+ if (!op) {
+ op = scf_find_base_operator_by_type(node->type);
+ if (!op) {
+ scf_loge("\n");
+ return -1;
+ }
+ }
+
+ scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
+ if (!h)
+ return -1;
+
+ return h->func(ast, node->nodes, node->nb_nodes, d);
+}
+
static int _scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* data)
{
if (!node)
if (_scf_expr_calculate_internal(ast, node->nodes[i], d) < 0) {
scf_loge("\n");
- goto _error;
+ return -1;
}
}
scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
- goto _error;
+ return -1;
}
if (h->func(ast, node->nodes, node->nb_nodes, d) < 0) {
scf_loge("\n");
- goto _error;
+ return -1;
}
} else {
for (i = node->nb_nodes - 1; i >= 0; i--) {
if (_scf_expr_calculate_internal(ast, node->nodes[i], d) < 0) {
scf_loge("\n");
- goto _error;
+ return -1;
}
}
scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type);
if (!h) {
scf_loge("\n");
- goto _error;
+ return -1;
}
if (h->func(ast, node->nodes, node->nb_nodes, d) < 0) {
scf_loge("\n");
- goto _error;
+ return -1;
}
}
return 0;
-
-_error:
- return -1;
}
static int _scf_op_const_create(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data)
scf_handler_data_t* d = data;
- scf_block_t* b = (scf_block_t*)(nodes[0]->parent);
-
scf_block_t* prev_block = ast->current_block;
- ast->current_block = b;
+ ast->current_block = (scf_block_t*)(nodes[0]->parent);
int i = 0;
while (i < nb_nodes) {
- scf_node_t* node = nodes[i];
- scf_operator_t* op = node->op;
+ scf_node_t* node = nodes[i];
int ret;
if (SCF_FUNCTION == node->type)
ret = __scf_op_const_call(ast, (scf_function_t*)node, data);
- else {
- if (!op) {
- op = scf_find_base_operator_by_type(node->type);
- if (!op) {
- scf_loge("node->type: %d\n", node->type);
- return -1;
- }
- }
-
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
- if (!h) {
- scf_loge("\n");
- return -1;
- }
-
- ret = h->func(ast, node->nodes, node->nb_nodes, d);
- }
+ else
+ ret = _scf_op_const_node(ast, node, d);
if (ret < 0) {
scf_loge("\n");
return 0;
}
-static int _scf_op_const_node(scf_ast_t* ast, scf_node_t* node, scf_handler_data_t* d)
-{
- scf_operator_t* op = node->op;
-
- if (!op) {
- op = scf_find_base_operator_by_type(node->type);
- if (!op) {
- scf_loge("\n");
- return -1;
- }
- }
-
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
- if (!h) {
- scf_loge("\n");
- return -1;
- }
-
- int ret = h->func(ast, node->nodes, node->nb_nodes, d);
- if (ret < 0) {
- scf_loge("\n");
- return -1;
- }
-
- return 0;
-}
-
static int _scf_op_const_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data)
{
if (nb_nodes < 2) {
}
scf_handler_data_t* d = data;
+ scf_variable_t* r = NULL;
+ scf_expr_t* e = nodes[0];
- scf_expr_t* e = nodes[0];
assert(SCF_OP_EXPR == e->type);
- scf_block_t* b = (scf_block_t*)(e->parent);
-
- scf_variable_t* r = NULL;
if (_scf_expr_calculate_internal(ast, e, &r) < 0) {
scf_loge("\n");
return -1;
int i;
for (i = 1; i < nb_nodes; i++) {
- scf_node_t* node = nodes[i];
- scf_operator_t* op = node->op;
-
- if (!op) {
- op = scf_find_base_operator_by_type(node->type);
- if (!op) {
- scf_loge("\n");
- return -1;
- }
- }
-
- scf_operator_handler_t* h = scf_find_const_operator_handler(op->type);
- if (!h)
- return -1;
-
- int ret = h->func(ast, node->nodes, node->nb_nodes, d);
+ int ret = _scf_op_const_node(ast, nodes[i], d);
if (ret < 0)
return -1;
}
return 0;
}
-int scf_function_const_opt(scf_ast_t* ast, scf_function_t* f)
-{
- scf_handler_data_t d = {0};
-
- int ret = __scf_op_const_call(ast, f, &d);
-
- if (ret < 0) {
- scf_loge("\n");
- return -1;
- }
-
- return 0;
-}
-
static int _scf_op_const_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data)
{
assert(nb_nodes > 0);
return 0;
}
+
+int scf_function_const_opt(scf_ast_t* ast, scf_function_t* f)
+{
+ scf_handler_data_t d = {0};
+
+ int ret = __scf_op_const_call(ast, f, &d);
+
+ if (ret < 0) {
+ scf_loge("\n");
+ return -1;
+ }
+
+ return 0;
+}
static int _semantic_do_overloaded(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, scf_handler_data_t* d)
{
scf_function_t* f;
- scf_variable_t* v0;
- scf_variable_t* v1;
+ scf_variable_t* v;
scf_vector_t* argv;
scf_vector_t* fvec = NULL;
scf_node_t* parent = nodes[0]->parent;
for (i = 0; i < nb_nodes; i++) {
- v0 = _scf_operand_get(nodes[i]);
+ v = _scf_operand_get(nodes[i]);
- if (!t && scf_variable_is_struct_pointer(v0)) {
+ if (!t && scf_variable_is_struct_pointer(v)) {
t = NULL;
- ret = scf_ast_find_type_type(&t, ast, v0->type);
+ ret = scf_ast_find_type_type(&t, ast, v->type);
if (ret < 0)
return ret;
assert(t->scope);
}
- ret = scf_vector_add(argv, v0);
+ ret = scf_vector_add(argv, v);
if (ret < 0) {
scf_vector_free(argv);
return ret;
return 0;
}
-