scf_dag_node_t* scf_dag_node_alloc(int type, scf_variable_t* var, const scf_node_t* node)
{
- scf_dag_node_t* dag_node = calloc(1, sizeof(scf_dag_node_t));
- if (!dag_node)
+ scf_dag_node_t* dn = calloc(1, sizeof(scf_dag_node_t));
+ if (!dn)
return NULL;
- dag_node->type = type;
+ dn->type = type;
if (var)
- dag_node->var = scf_variable_ref(var);
+ dn->var = scf_variable_ref(var);
else
- dag_node->var = NULL;
+ dn->var = NULL;
- dag_node->node = (scf_node_t*)node;
+ dn->node = (scf_node_t*)node;
#if 1
if (SCF_OP_CALL == type) {
- scf_logw("dag_node: %#lx, dag_node->type: %d", 0xffff & (uintptr_t)dag_node, dag_node->type);
+ scf_logw("dn: %#lx, dn->type: %d", 0xffff & (uintptr_t)dn, dn->type);
if (var) {
printf(", var: %#lx, var->type: %d", 0xffff & (uintptr_t)var, var->type);
if (var->w)
printf(", v_%d_%d/%s", var->w->line, var->w->pos, var->w->text->data);
- else {
- //printf(", v_%#lx", 0xffff & (uintptr_t)var);
- }
}
printf("\n");
}
#endif
- return dag_node;
+ return dn;
}
int scf_dag_node_add_child(scf_dag_node_t* parent, scf_dag_node_t* child)
return 0;
}
-void scf_dag_node_free(scf_dag_node_t* dag_node)
+void scf_dag_node_free(scf_dag_node_t* dn)
{
- if (dag_node) {
-
- if (dag_node->var)
- scf_variable_free(dag_node->var);
+ if (dn) {
+ if (dn->var)
+ scf_variable_free(dn->var);
- if (dag_node->parents)
- scf_vector_free(dag_node->parents);
+ if (dn->parents)
+ scf_vector_free(dn->parents);
- if (dag_node->childs)
- scf_vector_free(dag_node->childs);
+ if (dn->childs)
+ scf_vector_free(dn->childs);
- free(dag_node);
- dag_node = NULL;
+ free(dn);
+ dn = NULL;
}
}
}
}
-int scf_dag_node_same(scf_dag_node_t* dag_node, const scf_node_t* node)
+int scf_dag_node_same(scf_dag_node_t* dn, const scf_node_t* node)
{
int i;
if (node->split_flag) {
- if (dag_node->var != _scf_operand_get(node))
+ if (dn->var != _scf_operand_get(node))
return 0;
split = node;
scf_logd("split type: %d, node: %#lx, var: %#lx\n", split->type, 0xffff & (uintptr_t)split, 0xffff & (uintptr_t)split->var);
scf_logd("node type: %d, node: %#lx, var: %#lx\n", node->type, 0xffff & (uintptr_t)node, 0xffff & (uintptr_t)node->var);
- scf_logd("dag type: %d, node: %#lx, var: %#lx\n", dag_node->type, 0xffff & (uintptr_t)dag_node, 0xffff & (uintptr_t)dag_node->var);
+ scf_logd("dag type: %d, node: %#lx, var: %#lx\n", dn->type, 0xffff & (uintptr_t)dn, 0xffff & (uintptr_t)dn->var);
}
- if (dag_node->type != node->type)
+ if (dn->type != node->type)
return 0;
if (SCF_OP_ADDRESS_OF == node->type) {
- scf_logd("type: %d, %d, node: %#lx, %#lx, ", dag_node->type, node->type, 0xffff & (uintptr_t)dag_node, 0xffff & (uintptr_t)node);
-// scf_loge("var: %p, %p\n", dag_node->var, node->var);
-// printf("var: %#lx, %#lx\n", 0xffff & (uintptr_t)dag_node->var, 0xffff & (uintptr_t)node->var);
+ scf_logd("type: %d, %d, node: %#lx, %#lx, ", dn->type, node->type, 0xffff & (uintptr_t)dn, 0xffff & (uintptr_t)node);
}
if (scf_type_is_var(node->type)) {
- if (dag_node->var == node->var)
+ if (dn->var == node->var)
return 1;
else
return 0;
|| SCF_OP_INC_POST == node->type
|| SCF_OP_DEC_POST == node->type
|| SCF_OP_ADDRESS_OF == node->type) {
- if (dag_node->var == _scf_operand_get((scf_node_t*)node))
+ if (dn->var == _scf_operand_get((scf_node_t*)node))
return 1;
return 0;
}
- if (!dag_node->childs)
+ if (!dn->childs)
return 0;
if (SCF_OP_TYPE_CAST == node->type) {
- scf_dag_node_t* dn0 = dag_node->childs->data[0];
+ scf_dag_node_t* dn0 = dn->childs->data[0];
scf_variable_t* vn1 = _scf_operand_get(node->nodes[1]);
scf_node_t* n1 = node->nodes[1];
n1 = n1->nodes[0];
if (scf_dag_node_same(dn0, n1)
- && scf_variable_same_type(dag_node->var, node->result))
+ && scf_variable_same_type(dn->var, node->result))
return 1;
else {
scf_logd("var: %#lx, %#lx, type: %d, %d, node: %#lx, %#lx, same: %d\n",
0xffff & (uintptr_t)dn0->var,
0xffff & (uintptr_t)vn1,
- dag_node->var->type, node->result->type,
- 0xffff & (uintptr_t)dag_node,
+ dn->var->type, node->result->type,
+ 0xffff & (uintptr_t)dn,
0xffff & (uintptr_t)node,
- scf_variable_same_type(dag_node->var, node->result)
- );
+ scf_variable_same_type(dn->var, node->result));
return 0;
}
} else if (SCF_OP_ARRAY_INDEX == node->type) {
- assert(3 == dag_node->childs->size);
+ assert(3 == dn->childs->size);
assert(2 == node->nb_nodes);
goto cmp_childs;
if (SCF_OP_ARRAY_INDEX == node->nodes[0]->type) {
assert(2 == node->nodes[0]->nb_nodes);
- if (!dag_node->childs || 3 != dag_node->childs->size)
+ if (!dn->childs || 3 != dn->childs->size)
return 0;
node = node->nodes[0];
} else if (SCF_OP_POINTER == node->nodes[0]->type) {
assert(2 == node->nodes[0]->nb_nodes);
- if (!dag_node->childs || 2 != dag_node->childs->size)
+ if (!dn->childs || 2 != dn->childs->size)
return 0;
node = node->nodes[0];
}
}
- if (dag_node->childs->size != node->nb_nodes) {
- if (SCF_OP_ADDRESS_OF == node->type) {
- scf_loge("node: %p, %p, size: %d, %d\n", dag_node, node, dag_node->childs->size, node->nb_nodes);
- }
+ if (dn->childs->size != node->nb_nodes)
return 0;
- }
cmp_childs:
for (i = 0; i < node->nb_nodes; i++) {
- scf_dag_node_t* dag_child = dag_node->childs->data[i];
- scf_node_t* child = node->nodes[i];
+ scf_node_t* child = node->nodes[i];
while (SCF_OP_EXPR == child->type)
child = child->nodes[0];
- if (0 == scf_dag_node_same(dag_child, child))
+ if (0 == scf_dag_node_same(dn->childs->data[i], child))
return 0;
}
- for (i = 0; i < dag_node->childs->size; i++) {
- scf_dag_node_t* child = dag_node->childs->data[i];
+ for (i = 0; i < dn->childs->size; i++) {
+ scf_dag_node_t* child = dn->childs->data[i];
scf_dag_node_t* parent = NULL;
assert(child->parents);
int j;
for (j = 0; j < child->parents->size; j++) {
- if (dag_node == child->parents->data[j])
+ if (dn == child->parents->data[j])
break;
}
assert(j < child->parents->size);
for (++j; j < child->parents->size; j++) {
parent = child->parents->data[j];
- if (scf_type_is_assign(parent->type)
- || SCF_OP_INC == parent->type
+ if (SCF_OP_INC == parent->type
|| SCF_OP_DEC == parent->type
|| SCF_OP_3AC_SETZ == parent->type
|| SCF_OP_3AC_SETNZ == parent->type
|| SCF_OP_3AC_SETGT == parent->type
|| SCF_OP_3AC_SETGE == parent->type)
return 0;
+
+ if (scf_type_is_assign(parent->type)) {
+ if (child == parent->childs->data[0])
+ return 0;
+ }
}
}
- if (SCF_OP_CALL == dag_node->type) {
+ if (SCF_OP_CALL == dn->type) {
scf_variable_t* v0 = _scf_operand_get(node);
- scf_variable_t* v1 = dag_node->var;
+ scf_variable_t* v1 = dn->var;
if (split)
v0 = _scf_operand_get(split);
scf_dag_node_t* scf_dag_find_node(scf_list_t* h, const scf_node_t* node)
{
+ scf_list_t* l;
scf_node_t* origin = (scf_node_t*)node;
while (SCF_OP_EXPR == origin->type)
origin = origin->nodes[0];
- scf_list_t* l;
for (l = scf_list_tail(h); l != scf_list_sentinel(h); l = scf_list_prev(l)) {
- scf_dag_node_t* dag_node = scf_list_data(l, scf_dag_node_t, list);
+ scf_dag_node_t* dn = scf_list_data(l, scf_dag_node_t, list);
- if (scf_dag_node_same(dag_node, origin))
- return dag_node;
+ if (scf_dag_node_same(dn, origin))
+ return dn;
}
return NULL;
scf_list_t* l;
for (l = scf_list_head(h); l != scf_list_sentinel(h); l = scf_list_next(l)) {
- scf_dag_node_t* dag_node = scf_list_data(l, scf_dag_node_t, list);
+ scf_dag_node_t* dn = scf_list_data(l, scf_dag_node_t, list);
- if (!dag_node->childs) {
- int ret = scf_vector_add_unique(leafs, dag_node);
+ if (!dn->childs) {
+ int ret = scf_vector_add_unique(leafs, dn);
if (ret < 0)
return ret;
}
*pds = ds;
return 0;
}
-
scf_function_t* scf_function_alloc(scf_lex_word_t* w)
{
+ assert(w);
+
scf_function_t* f = calloc(1, sizeof(scf_function_t));
- assert(f);
+ if (!f)
+ return NULL;
f->node.type = SCF_FUNCTION;
+ f->op_type = -1;
- assert(w);
- f->node.w = scf_lex_word_clone(w);
+ scf_list_init(&f->basic_block_list_head);
+ scf_list_init(&f->dag_list_head);
f->scope = scf_scope_alloc(w, "function");
+ if (!f->scope)
+ goto _scope_error;
+
+ f->node.w = scf_lex_word_clone(w);
+ if (!f->node.w)
+ goto _word_error;
+
+ f->rets = scf_vector_alloc();
+ if (!f->rets)
+ goto _ret_error;
+
+ f->argv = scf_vector_alloc();
+ if (!f->argv)
+ goto _argv_error;
- f->rets = scf_vector_alloc();
- f->argv = scf_vector_alloc();
f->callee_functions = scf_vector_alloc();
+ if (!f->callee_functions)
+ goto _callee_error;
+
f->caller_functions = scf_vector_alloc();
- f->jmps = scf_vector_alloc();
- f->dfs_tree = scf_vector_alloc();
- f->bb_loops = scf_vector_alloc();
+ if (!f->caller_functions)
+ goto _caller_error;
- f->text_relas = scf_vector_alloc();
- f->data_relas = scf_vector_alloc();
+ f->jmps = scf_vector_alloc();
+ if (!f->jmps)
+ goto _jmps_error;
- f->op_type = -1;
+ f->dfs_tree = scf_vector_alloc();
+ if (!f->dfs_tree)
+ goto _dfs_tree_error;
+
+ f->bb_loops = scf_vector_alloc();
+ if (!f->bb_loops)
+ goto _loop_error;
+
+ f->bb_groups = scf_vector_alloc();
+ if (!f->bb_groups)
+ goto _group_error;
+
+ f->text_relas = scf_vector_alloc();
+ if (!f->text_relas)
+ goto _text_rela_error;
+
+ f->data_relas = scf_vector_alloc();
+ if (!f->data_relas)
+ goto _data_rela_error;
- scf_list_init(&f->basic_block_list_head);
- scf_list_init(&f->dag_list_head);
return f;
+
+_data_rela_error:
+ scf_vector_free(f->text_relas);
+_text_rela_error:
+ scf_vector_free(f->bb_groups);
+_group_error:
+ scf_vector_free(f->bb_loops);
+_loop_error:
+ scf_vector_free(f->dfs_tree);
+_dfs_tree_error:
+ scf_vector_free(f->jmps);
+_jmps_error:
+ scf_vector_free(f->caller_functions);
+_caller_error:
+ scf_vector_free(f->callee_functions);
+_callee_error:
+ scf_vector_free(f->argv);
+_argv_error:
+ scf_vector_free(f->rets);
+_ret_error:
+ scf_lex_word_free(f->node.w);
+_word_error:
+ scf_scope_free(f->scope);
+_scope_error:
+ free(f);
+ return NULL;
}
void scf_function_free(scf_function_t* f)
{
- assert(f);
- assert(f->scope);
+ if (f) {
+ scf_scope_free(f->scope);
+ f->scope = NULL;
- scf_scope_free(f->scope);
- f->scope = NULL;
-
- if (f->signature) {
- scf_string_free(f->signature);
- f->signature = NULL;
- }
-
- if (f->w_start) {
- scf_lex_word_free(f->w_start);
- f->w_start = NULL;
- }
+ if (f->signature) {
+ scf_string_free(f->signature);
+ f->signature = NULL;
+ }
- if (f->w_end) {
- scf_lex_word_free(f->w_end);
- f->w_end = NULL;
- }
+ if (f->rets) {
+ scf_vector_clear(f->rets, ( void (*)(void*) ) scf_variable_free);
+ scf_vector_free (f->rets);
+ }
- if (f->rets) {
- scf_vector_clear(f->rets, ( void (*)(void*) ) scf_variable_free);
- scf_vector_free (f->rets);
- }
+ if (f->argv) {
+ scf_vector_clear(f->argv, ( void (*)(void*) ) scf_variable_free);
+ scf_vector_free (f->argv);
+ f->argv = NULL;
+ }
- if (f->argv) {
- scf_vector_clear(f->argv, ( void (*)(void*) ) scf_variable_free);
- scf_vector_free (f->argv);
- f->argv = NULL;
- }
+ if (f->callee_functions)
+ scf_vector_free(f->callee_functions);
- if (f->callee_functions)
- scf_vector_free(f->callee_functions);
+ if (f->caller_functions)
+ scf_vector_free(f->caller_functions);
- if (f->caller_functions)
- scf_vector_free(f->caller_functions);
+ if (f->jmps) {
+ scf_vector_free(f->jmps);
+ f->jmps = NULL;
+ }
- if (f->jmps) {
- scf_vector_free(f->jmps);
- f->jmps = NULL;
- }
+ if (f->text_relas) {
+ scf_vector_free(f->text_relas);
+ f->text_relas = NULL;
+ }
- if (f->text_relas) {
- scf_vector_free(f->text_relas);
- f->text_relas = NULL;
- }
+ if (f->data_relas) {
+ scf_vector_free(f->data_relas);
+ f->data_relas = NULL;
+ }
- if (f->data_relas) {
- scf_vector_free(f->data_relas);
- f->data_relas = NULL;
+ scf_node_free((scf_node_t*)f);
}
-
- scf_node_free((scf_node_t*)f);
}
int scf_function_same(scf_function_t* f0, scf_function_t* f1)