for (i = 0; i < bb->ds_malloced->size; i++) {
ds = bb->ds_malloced->data[i];
- if (scf_vector_find_cmp(bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb->ds_freed, ds, scf_ds_cmp_same_indexes))
continue;
scf_dn_status_print(ds);
}
return 0;
}
-int scf_basic_block_dag2(scf_basic_block_t* bb, scf_list_t* dag_list_head)
+int scf_basic_block_dag(scf_basic_block_t* bb, scf_list_t* dag_list_head)
{
scf_list_t* l;
scf_3ac_code_t* c;
return scf_basic_block_inited_vars(bb, bb_list_head);
}
-int scf_basic_block_dag(scf_basic_block_t* bb, scf_list_t* bb_list_head, scf_list_t* dag_list_head)
-{
- scf_list_t* l;
- scf_3ac_code_t* c;
-
- int ret;
-
- ret = scf_basic_block_dag2(bb, dag_list_head);
- if (ret < 0) {
- scf_loge("\n");
- return ret;
- }
-
- ret = _bb_vars(bb);
- if (ret < 0) {
- scf_loge("\n");
- return ret;
- }
-
- for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_next(l)) {
- c = scf_list_data(l, scf_3ac_code_t, list);
-
- if (c->active_vars)
- scf_vector_clear(c->active_vars, (void (*)(void*))scf_dn_status_free);
- else {
- c->active_vars = scf_vector_alloc();
- if (!c->active_vars)
- return -ENOMEM;
- }
-
- ret = _copy_to_active_vars(c->active_vars, bb->var_dag_nodes);
- if (ret < 0)
- return ret;
- }
-
- return scf_basic_block_inited_vars(bb, bb_list_head);
-}
-
static int _bb_init_pointer_aliases(scf_dn_status_t* ds_pointer, scf_dag_node_t* dn_alias, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
scf_dn_status_t* ds;
int ret;
int i;
- scf_dn_status_vector_clear_by_ds(c ->dn_status_initeds, ds_pointer);
- scf_dn_status_vector_clear_by_ds(bb->dn_status_initeds, ds_pointer);
+ scf_ds_vector_clear_by_ds(c ->dn_status_initeds, ds_pointer);
+ scf_ds_vector_clear_by_ds(bb->dn_status_initeds, ds_pointer);
aliases = scf_vector_alloc();
if (!aliases)
}
ds2->inited = 1;
- ret = scf_dn_status_copy_dn(ds2, ds_pointer);
+ ret = scf_ds_copy_dn(ds2, ds_pointer);
if (ret < 0) {
scf_dn_status_free(ds2);
break;
}
- ret = scf_dn_status_copy_alias(ds2, ds);
+ ret = scf_ds_copy_alias(ds2, ds);
if (ret < 0) {
scf_dn_status_free(ds2);
break;
for (i = 0; i < c->dn_status_initeds->size; i++) {
ds = c->dn_status_initeds->data[i];
- ds2 = scf_vector_find_cmp(bb->dn_status_initeds, ds, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->dn_status_initeds, ds, scf_ds_cmp_same_indexes);
if (ds2)
scf_vector_del(bb->dn_status_initeds, ds2);
void scf_basic_block_print_list(scf_list_t* h);
int scf_basic_block_vars(scf_basic_block_t* bb, scf_list_t* bb_list_head);
-int scf_basic_block_dag (scf_basic_block_t* bb, scf_list_t* bb_list_head, scf_list_t* dag_list_head);
-int scf_basic_block_dag2(scf_basic_block_t* bb, scf_list_t* dag_list_head);
+int scf_basic_block_dag (scf_basic_block_t* bb, scf_list_t* dag_list_head);
int scf_basic_block_active_vars(scf_basic_block_t* bb);
int scf_basic_block_inited_vars(scf_basic_block_t* bb, scf_list_t* bb_list_head);
return NULL;
ds->refs = 1;
-
return ds;
}
return ds;
}
-int scf_dn_status_copy_dn(scf_dn_status_t* dst, scf_dn_status_t* src)
+int scf_ds_copy_dn(scf_dn_status_t* dst, scf_dn_status_t* src)
{
scf_dn_index_t* di;
int i;
return -ENOMEM;
for (i = 0; i < dst->dn_indexes->size; i++) {
- di = dst->dn_indexes->data[i];
+ di = dst->dn_indexes->data[i];
di->refs++;
}
}
return 0;
}
-int scf_dn_status_copy_alias(scf_dn_status_t* dst, scf_dn_status_t* src)
+int scf_ds_copy_alias(scf_dn_status_t* dst, scf_dn_status_t* src)
{
scf_dn_index_t* di;
int i;
return -ENOMEM;
for (i = 0; i < dst->alias_indexes->size; i++) {
- di = dst->alias_indexes->data[i];
+ di = dst->alias_indexes->data[i];
di->refs++;
}
}
{
if (ds)
ds->refs++;
+ return ds;
}
void scf_dn_status_free(scf_dn_status_t* ds)
return;
if (ds->dn_indexes) {
- for (i = 0; i < ds->dn_indexes->size; i++)
- scf_dn_index_free(ds->dn_indexes->data[i]);
-
- scf_vector_free(ds->dn_indexes);
+ scf_vector_clear(ds->dn_indexes, (void (*)(void*))scf_dn_index_free);
+ scf_vector_free (ds->dn_indexes);
}
if (ds->alias_indexes) {
- for (i = 0; i < ds->alias_indexes->size; i++)
- scf_dn_index_free(ds->alias_indexes->data[i]);
-
- scf_vector_free(ds->alias_indexes);
+ scf_vector_clear(ds->alias_indexes, (void (*)(void*))scf_dn_index_free);
+ scf_vector_free (ds->alias_indexes);
}
free(ds);
}
}
-static int _dn_status_cmp_dn_indexes(const void* p0, const void* p1,
+static int _ds_cmp_indexes(const void* p0, const void* p1,
int (*cmp)(const scf_dn_index_t*, const scf_dn_index_t*))
{
const scf_dn_status_t* ds0 = p0;
return 0;
}
-int scf_dn_status_cmp_same_dn_indexes(const void* p0, const void* p1)
+int scf_ds_cmp_same_indexes(const void* p0, const void* p1)
{
- return _dn_status_cmp_dn_indexes(p0, p1, scf_dn_index_same);
+ return _ds_cmp_indexes(p0, p1, scf_dn_index_same);
}
-int scf_dn_status_cmp_like_dn_indexes(const void* p0, const void* p1)
+int scf_ds_cmp_like_indexes(const void* p0, const void* p1)
{
- return _dn_status_cmp_dn_indexes(p0, p1, scf_dn_index_like);
+ return _ds_cmp_indexes(p0, p1, scf_dn_index_like);
}
-int scf_dn_status_cmp_alias(const void* p0, const void* p1)
+int scf_ds_cmp_alias(const void* p0, const void* p1)
{
const scf_dn_status_t* v0 = p0;
const scf_dn_status_t* v1 = p1;
return 0;
}
-int scf_dag_find_leafs(scf_list_t* h, scf_vector_t* leafs)
-{
- scf_dag_node_t* dn;
- scf_list_t* l;
-
- for (l = scf_list_head(h); l != scf_list_sentinel(h); l = scf_list_next(l)) {
- dn = scf_list_data(l, scf_dag_node_t, list);
-
- if (!dn->childs) {
- int ret = scf_vector_add_unique(leafs, dn);
- if (ret < 0)
- return ret;
- }
- }
- return 0;
-}
-
-int scf_dag_root_find_leafs(scf_dag_node_t* root, scf_vector_t* leafs)
-{
- if (!root->childs)
- return scf_vector_add_unique(leafs, root);
-
- scf_dag_node_t* dn;
- int i;
-
- for (i = 0; i < root->childs->size; i++) {
- dn = root->childs->data[i];
-
- int ret = scf_dag_root_find_leafs(dn, leafs);
- if (ret < 0)
- return ret;
- }
- return 0;
-}
-
-int scf_dag_vector_find_leafs(scf_vector_t* roots, scf_vector_t* leafs)
-{
- scf_dag_node_t* root;
- int i;
-
- for (i = 0; i < roots->size; i++) {
- root = roots->data[i];
-
- int ret = scf_dag_root_find_leafs(root, leafs);
- if (ret < 0)
- return ret;
- }
- return 0;
-}
-
static int _dn_status_index(scf_vector_t* indexes, scf_dag_node_t* dn_index, int type)
{
scf_dn_index_t* di;
return _dn_status_index(ds->alias_indexes, dn_index, type);
}
-void scf_dn_status_vector_clear_by_ds(scf_vector_t* vec, scf_dn_status_t* ds)
+void scf_ds_vector_clear_by_ds(scf_vector_t* vec, scf_dn_status_t* ds)
{
scf_dn_status_t* ds2;
while (1) {
- ds2 = scf_vector_find_cmp(vec, ds, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(vec, ds, scf_ds_cmp_same_indexes);
if (!ds2)
break;
+
assert(0 == scf_vector_del(vec, ds2));
+
+ scf_dn_status_free(ds2);
+ ds2 = NULL;
}
}
-void scf_dn_status_vector_clear_by_dn(scf_vector_t* vec, scf_dag_node_t* dn)
+void scf_ds_vector_clear_by_dn(scf_vector_t* vec, scf_dag_node_t* dn)
{
scf_dn_status_t* ds;
ds = scf_vector_find_cmp(vec, dn, scf_dn_status_cmp);
if (!ds)
break;
+
assert(0 == scf_vector_del(vec, ds));
+
+ scf_dn_status_free(ds);
+ ds = NULL;
}
}
void scf_dn_status_free (scf_dn_status_t* ds);
void scf_dn_status_print(scf_dn_status_t* ds);
-int scf_dn_status_copy_dn (scf_dn_status_t* dst, scf_dn_status_t* src);
-int scf_dn_status_copy_alias(scf_dn_status_t* dst, scf_dn_status_t* src);
-
scf_dag_node_t* scf_dag_node_alloc(int type, scf_variable_t* var, const scf_node_t* node);
int scf_dag_node_add_child (scf_dag_node_t* parent, scf_dag_node_t* child);
int scf_dag_node_find_child(scf_dag_node_t* parent, scf_dag_node_t* child);
-int scf_dag_node_same (scf_dag_node_t* dag_node, const scf_node_t* node);
-void scf_dag_node_free (scf_dag_node_t* dag_node);
-
-void scf_dag_node_free_list(scf_list_t* dag_list_head);
+int scf_dag_dn_same (scf_dag_node_t* dn0, scf_dag_node_t* dn1);
+int scf_dag_node_same (scf_dag_node_t* dn, const scf_node_t* node);
+void scf_dag_node_free (scf_dag_node_t* dn);
scf_dag_node_t* scf_dag_find_node (scf_list_t* h, const scf_node_t* node);
int scf_dag_get_node (scf_list_t* h, const scf_node_t* node, scf_dag_node_t** pp);
-int scf_dag_vector_find_leafs (scf_vector_t* roots, scf_vector_t* leafs);
-
-int scf_dag_root_find_leafs (scf_dag_node_t* root, scf_vector_t* leafs);
-
-int scf_dag_find_leafs (scf_list_t* h, scf_vector_t* leafs);
-
-int scf_dag_find_roots (scf_list_t* h, scf_vector_t* roots);
+int scf_dag_find_roots(scf_list_t* h, scf_vector_t* roots);
int scf_dag_expr_calculate(scf_list_t* h, scf_dag_node_t* node);
+void scf_dag_node_free_list(scf_list_t* h);
-int scf_dag_dn_same(scf_dag_node_t* dn0, scf_dag_node_t* dn1);
-
-int scf_dn_status_cmp_alias(const void* p0, const void* p1);
+int scf_ds_copy_dn (scf_dn_status_t* dst, scf_dn_status_t* src);
+int scf_ds_copy_alias(scf_dn_status_t* dst, scf_dn_status_t* src);
-int scf_dn_status_cmp_same_dn_indexes(const void* p0, const void* p1);
-int scf_dn_status_cmp_like_dn_indexes(const void* p0, const void* p1);
+int scf_ds_cmp_alias (const void* p0, const void* p1);
+int scf_ds_cmp_same_indexes(const void* p0, const void* p1);
+int scf_ds_cmp_like_indexes(const void* p0, const void* p1);
int scf_dn_status_index(scf_dn_status_t* ds, scf_dag_node_t* dn_index, int type);
int scf_ds_for_assign_dereference(scf_dn_status_t** pds, scf_dag_node_t* dn);
int scf_ds_for_assign_array_member(scf_dn_status_t** pds, scf_dag_node_t* dn_base, scf_dag_node_t* dn_index, scf_dag_node_t* dn_scale);
-void scf_dn_status_vector_clear_by_ds(scf_vector_t* vec, scf_dn_status_t* ds);
-void scf_dn_status_vector_clear_by_dn(scf_vector_t* vec, scf_dag_node_t* dn);
+void scf_ds_vector_clear_by_ds(scf_vector_t* vec, scf_dn_status_t* ds);
+void scf_ds_vector_clear_by_dn(scf_vector_t* vec, scf_dag_node_t* dn);
static int scf_dn_through_bb(scf_dag_node_t* dn)
{
scf_label_t* scf_label_alloc(scf_lex_word_t* w)
{
scf_label_t* l = calloc(1, sizeof(scf_label_t));
- assert(l);
+ if (!l)
+ return NULL;
+
+ l->w = scf_lex_word_clone(w);
+ if (!l->w) {
+ free(l);
+ return NULL;
+ }
l->refs = 1;
l->type = SCF_LABEL;
- l->w = scf_lex_word_clone(w);
return l;
}
void scf_label_free(scf_label_t* l)
{
- assert(l);
- l->refs--;
- if (0 == l->refs) {
+ if (l) {
+ if (--l->refs > 0)
+ return;
+
if (l->w) {
scf_lex_word_free(l->w);
l->w = NULL;
l->node = NULL;
free(l);
- l = NULL;
}
}
-
scf_basic_block_print_list(&f->basic_block_list_head);
+ for (j = 0; j < f->bb_loops->size; j++) {
+ bbg = f->bb_loops->data[j];
+
+ scf_bb_loop_print(bbg);
+ }
+
for (j = 0; j < f->bb_groups->size; j++) {
bbg = f->bb_groups->data[j];
- switch (bbg->loop_layers) {
- case 0:
- scf_bb_group_print(bbg);
- break;
- default:
- scf_bb_loop_print(bbg);
- break;
- };
+ scf_bb_group_print(bbg);
}
}
#endif
.flags = SCF_OPTIMIZER_LOCAL,
};
-
{
scf_dn_status_t* ds = data;
- if (!scf_vector_find_cmp(bb->ds_malloced, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (!scf_vector_find_cmp(bb->ds_malloced, ds, scf_ds_cmp_same_indexes))
return 0;
- if (scf_vector_find_cmp(bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes)) {
+ if (scf_vector_find_cmp(bb->ds_freed, ds, scf_ds_cmp_same_indexes)) {
scf_loge("error free dn: \n");
return -1;
}
for (k = 0; k < bb2->ds_malloced->size; k++) {
ds2 = bb2->ds_malloced->data[k];
- if (0 == scf_dn_status_cmp_same_dn_indexes(ds2, ds))
+ if (0 == scf_ds_cmp_same_indexes(ds2, ds))
continue;
- if (scf_vector_find_cmp(bb2->ds_freed, ds2, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb2->ds_freed, ds2, scf_ds_cmp_same_indexes))
continue;
- if (scf_vector_find_cmp(bb1->ds_malloced, ds2, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb1->ds_malloced, ds2, scf_ds_cmp_same_indexes))
continue;
scf_dn_status_t* ds3 = scf_dn_status_clone(ds2);
for (i = 0; i < bb->prevs->size; i++) {
bb_prev = bb->prevs->data[i];
- if (!scf_vector_find_cmp(bb_prev->ds_malloced, ds, scf_dn_status_cmp_like_dn_indexes))
+ if (!scf_vector_find_cmp(bb_prev->ds_malloced, ds, scf_ds_cmp_like_indexes))
continue;
- if (scf_vector_find_cmp(bb_prev->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb_prev->ds_freed, ds, scf_ds_cmp_same_indexes))
continue;
int ret = scf_vector_add(bb_split_prevs, bb_prev);
scf_dn_status_t* ds = bb_prev->ds_malloced->data[j];
- if (scf_vector_find_cmp(bb_prev->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb_prev->ds_freed, ds, scf_ds_cmp_same_indexes))
continue;
- if (scf_vector_find_cmp(ds_malloced, ds, scf_dn_status_cmp_like_dn_indexes))
+ if (scf_vector_find_cmp(ds_malloced, ds, scf_ds_cmp_like_indexes))
continue;
scf_dn_status_t* ds2 = scf_dn_status_clone(ds);
if (!ds_alias->dag_node)
continue;
- if (scf_vector_find_cmp(ds_malloced, ds_alias, scf_dn_status_cmp_like_dn_indexes)) {
+ if (scf_vector_find_cmp(ds_malloced, ds_alias, scf_ds_cmp_like_indexes)) {
need = 1;
break;
}
}
- if (scf_vector_find_cmp(ds_malloced, ds_obj, scf_dn_status_cmp_like_dn_indexes)) {
+ if (scf_vector_find_cmp(ds_malloced, ds_obj, scf_ds_cmp_like_indexes)) {
need = 1;
}
return need;
}
-static int _bb_split_prevs_need_free(scf_dn_status_t* ds_obj, scf_vector_t* ds_malloced, scf_vector_t* bb_split_prevs,
- scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
+static int _bb_split_prevs_need_free(scf_dn_status_t* ds_obj, scf_vector_t* ds_malloced, scf_vector_t* bb_split_prevs, scf_basic_block_t* bb)
{
- scf_dn_status_t* ds_obj2;
- scf_dn_status_t* ds_alias;
- scf_dn_index_t* di;
- scf_vector_t* aliases;
-
- int ret;
- int i;
- int need = 0;
-
- aliases = scf_vector_alloc();
- if (!aliases)
- return -ENOMEM;
-
-#define SPLIT_PREVS_NEED_FREE(malloced, obj, split_prevs) \
- do { \
- scf_dn_status_t* ds; \
- \
- if (scf_vector_find_cmp(malloced, obj, scf_dn_status_cmp_like_dn_indexes)) { \
- \
- if (_bb_split_prevs(bb, obj, split_prevs) < 0) { \
- ret = -ENOMEM; \
- goto error; \
- } \
- \
- need = 1; \
- } \
- } while (0)
-
-#if 0
- ret = __bb_find_ds_alias(aliases, ds_obj, c, bb, bb_list_head);
- if (ret < 0)
- return ret;
-
-#if 0
- printf("\n");
- int k;
- for (k = 0; k < ds_malloced->size; k++) {
- ds_alias = ds_malloced->data[k];
-
- scf_loge("## k: %d, ds_malloced: %p, size: %d\n", k, ds_malloced, ds_malloced->size);
- scf_dn_status_print(ds_alias);
- }
-#endif
- for (i = 0; i < aliases->size; i++) {
- ds_alias = aliases->data[i];
-
- if (!ds_alias->dag_node)
- continue;
+ if (scf_vector_find_cmp(ds_malloced, ds_obj, scf_ds_cmp_like_indexes)) {
+ int ret = _bb_split_prevs(bb, ds_obj, bb_split_prevs);
+ if (ret < 0)
+ return ret;
- SPLIT_PREVS_NEED_FREE(ds_malloced, ds_alias, bb_split_prevs);
+ return 1;
}
- // ds = scf_vector_find_cmp(malloced, obj, scf_dn_status_cmp_same_dn_indexes); \
- if (ds) {\
- assert(0 == scf_vector_del(malloced, ds)); \
- scf_dn_status_free(ds); \
- } \
-#endif
-#endif
- SPLIT_PREVS_NEED_FREE(ds_malloced, ds_obj, bb_split_prevs);
- ret = need;
-error:
-// scf_vector_clear(aliases, ( void (*)(void*) ) scf_dn_status_free);
- scf_vector_free (aliases);
- return ret;
+ return 0;
}
static int _optimize_auto_gc_bb(scf_ast_t* ast, scf_function_t* f, scf_basic_block_t* bb, scf_list_t* bb_list_head)
if (!bb_split_prevs)
return -ENOMEM;
- ret = _bb_split_prevs_need_free(ds_obj, ds_malloced, bb_split_prevs, c, bb, bb_list_head);
+ ret = _bb_split_prevs_need_free(ds_obj, ds_malloced, bb_split_prevs, bb);
if (ret < 0) {
scf_loge("\n");
return ret;
}
if (ret) {
- if (!scf_vector_find_cmp(ds_assigned, ds_obj, scf_dn_status_cmp_like_dn_indexes)
+ if (!scf_vector_find_cmp(ds_assigned, ds_obj, scf_ds_cmp_like_indexes)
&& bb_split_prevs->size > 0
&& bb_split_prevs->size < bb->prevs->size)
ret->auto_gc_flag = 1;
}
- if (!scf_vector_find_cmp(ds_malloced, ds_obj, scf_dn_status_cmp_like_dn_indexes)) {
+ if (!scf_vector_find_cmp(ds_malloced, ds_obj, scf_ds_cmp_like_indexes)) {
assert(0 == scf_vector_add(ds_malloced, ds_obj));
} else {
if (ret < 0)
return ret;
- if (!scf_vector_find_cmp(ds_malloced, ds_obj, scf_dn_status_cmp_like_dn_indexes)) {
+ if (!scf_vector_find_cmp(ds_malloced, ds_obj, scf_ds_cmp_like_indexes)) {
assert(0 == scf_vector_add(ds_malloced, ds_obj));
} else {
bb = scf_list_data(l, scf_basic_block_t, list);
for (l = scf_list_next(l); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
-
bb2 = scf_list_data(l, scf_basic_block_t, list);
if (!bb2->auto_ref_flag && !bb2->auto_free_flag)
return -ENOMEM;
for (l2 = scf_list_prev(l); l2 != scf_list_prev(start); l2 = scf_list_prev(l2)) {
-
bb = scf_list_data(l2, scf_basic_block_t, list);
ret = scf_basic_block_active_vars(bb);
if (bb->auto_free_flag
&& bb2->auto_ref_flag
- && 0 == scf_dn_status_cmp_same_dn_indexes(bb->ds_auto_gc, bb2->ds_auto_gc)) {
+ && 0 == scf_ds_cmp_same_indexes(bb->ds_auto_gc, bb2->ds_auto_gc)) {
scf_basic_block_t* bb3;
scf_basic_block_t* bb4;
.flags = SCF_OPTIMIZER_LOCAL,
};
-
static int _bb_find_ds(scf_basic_block_t* bb, scf_dn_status_t* ds_obj)
{
- if (scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_ds_cmp_same_indexes))
return 0;
- if (scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_ds_cmp_same_indexes))
return 1;
return 0;
}
{
scf_dn_status_t* ds2;
- ds2 = scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_ds_cmp_same_indexes);
if (ds2) {
scf_vector_del(bb->ds_freed, ds2);
if (ds2 != ds_obj)
ds2 = NULL;
}
- ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_ds_cmp_same_indexes);
if (!ds2) {
- ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_dn_status_cmp_like_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_ds_cmp_like_indexes);
if (!ds2) {
assert(0 == scf_vector_add(bb->ds_malloced, ds_obj));
return 0;
{
scf_dn_status_t* ds2;
- if (!scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_dn_status_cmp_same_dn_indexes)) {
+ if (!scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_ds_cmp_same_indexes)) {
- ds2 = scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_dn_status_cmp_like_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_ds_cmp_like_indexes);
if (!ds2) {
ds2 = scf_dn_status_clone(ds_obj);
assert(ds2);
}
}
- ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_ds_cmp_same_indexes);
if (ds2) {
scf_vector_del(bb->ds_malloced, ds2);
if (ds2 != ds_obj)
if (!ds_alias->dag_node)
continue;
- if (scf_vector_find_cmp(bb->ds_malloced, ds_alias, scf_dn_status_cmp_same_dn_indexes)
- && !scf_vector_find_cmp(bb->ds_freed, ds_alias, scf_dn_status_cmp_same_dn_indexes)) {
+ if (scf_vector_find_cmp(bb->ds_malloced, ds_alias, scf_ds_cmp_same_indexes)
+ && !scf_vector_find_cmp(bb->ds_freed, ds_alias, scf_ds_cmp_same_indexes)) {
need = 1;
break;
}
}
- if (scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_dn_status_cmp_same_dn_indexes)
- && !scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_dn_status_cmp_same_dn_indexes)) {
+ if (scf_vector_find_cmp(bb->ds_malloced, ds_obj, scf_ds_cmp_same_indexes)
+ && !scf_vector_find_cmp(bb->ds_freed, ds_obj, scf_ds_cmp_same_indexes)) {
need = 1;
}
for (k = 0; k < bb->ds_malloced->size; k++) {
ds = bb->ds_malloced->data[k];
- if (scf_vector_find_cmp(bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(bb->ds_freed, ds, scf_ds_cmp_same_indexes))
continue;
- if (scf_vector_find_cmp(next_bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ if (scf_vector_find_cmp(next_bb->ds_freed, ds, scf_ds_cmp_same_indexes))
continue;
- ds2 = scf_vector_find_cmp(next_bb->ds_malloced, ds, scf_dn_status_cmp_like_dn_indexes);
+ ds2 = scf_vector_find_cmp(next_bb->ds_malloced, ds, scf_ds_cmp_like_indexes);
if (ds2) {
uint32_t tmp = ds2->ret;
if (!roots)
return -ENOMEM;
- ret = scf_basic_block_dag2(bb, &bb->dag_list_head);
+ ret = scf_basic_block_dag(bb, &bb->dag_list_head);
if (ret < 0)
goto error;
scf_logd("------- %s() ------\n", f->node.w->text->data);
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);
if (bb->jmp_flag
|| bb->end_flag
static int _alias_call(scf_vector_t* aliases, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
scf_3ac_operand_t* src;
+ scf_dn_status_t* ds;
scf_dag_node_t* dn;
scf_variable_t* v;
- scf_vector_t* dn_pointers;
- scf_dn_status_t* status;
scf_dag_node_t* dn_pointer;
- scf_dag_node_t* dn_alias;
+ scf_vector_t* dn_pointers;
int i;
int j;
dn_pointer = dn_pointers->data[j];
v = dn_pointer->var;
- scf_logd("i: %d, dn_pointers->size: %d, pointer: v_%d_%d/%s\n",
- i, dn_pointers->size, v->w->line, v->w->pos, v->w->text->data);
+ scf_logd("i: %d, dn_pointers->size: %d, pointer: v_%d_%d/%s\n", i, dn_pointers->size, v->w->line, v->w->pos, v->w->text->data);
ret = __alias_dereference(aliases, dn_pointer, c, bb, bb_list_head);
if (ret < 0) {
if (dn != dn_pointer && dn->var->nb_pointers > 1) {
scf_logd("pointer: v_%d_%d/%s, SCF_DN_ALIAS_ALLOC\n", v->w->line, v->w->pos, v->w->text->data);
- status = calloc(1, sizeof(scf_dn_status_t));
- if (!status) {
+ ds = calloc(1, sizeof(scf_dn_status_t));
+ if (!ds) {
scf_vector_free(dn_pointers);
return -ENOMEM;
}
- ret = scf_vector_add(aliases, status);
+ ret = scf_vector_add(aliases, ds);
if (ret < 0) {
- scf_dn_status_free(status);
+ scf_dn_status_free(ds);
scf_vector_free(dn_pointers);
return ret;
}
- status->dag_node = dn_pointer;
- status->alias_type = SCF_DN_ALIAS_ALLOC;
+ ds->dag_node = dn_pointer;
+ ds->alias_type = SCF_DN_ALIAS_ALLOC;
}
- for ( ; k < aliases->size; k++) {
- status = aliases->data[k];
+ for ( ; k < aliases->size; k++) {
+ ds = aliases->data[k];
- if (SCF_DN_ALIAS_VAR != status->alias_type)
+ if (SCF_DN_ALIAS_VAR != ds->alias_type)
continue;
- dn_alias = status->alias;
-
- if (0 == dn_alias->var->nb_pointers)
+ if (0 == ds->alias->var->nb_pointers)
continue;
- ret = scf_vector_add_unique(dn_pointers, dn_alias);
+ ret = scf_vector_add_unique(dn_pointers, ds->alias);
if (ret < 0) {
scf_vector_free(dn_pointers);
return ret;
}
}
}
+
scf_vector_free(dn_pointers);
+ dn_pointers = NULL;
}
return 0;
if (!roots)
return -ENOMEM;
- ret = scf_basic_block_dag2(bb, &bb->dag_list_head);
+ ret = scf_basic_block_dag(bb, &bb->dag_list_head);
if (ret < 0)
goto error;
return ret;
}
- scf_loge("-------------------------\n");
- scf_basic_block_print_list(bb_list_head);
- scf_loge("-------------------------\n");
+// scf_basic_block_print_list(bb_list_head);
return 0;
}
bb->index = f->nb_basic_blocks++;
- int ret = scf_basic_block_dag(bb, bb_list_head, &f->dag_list_head);
+ int ret = scf_basic_block_dag(bb, &f->dag_list_head);
+ if (ret < 0) {
+ scf_loge("\n");
+ return ret;
+ }
+
+ ret = scf_basic_block_vars(bb, bb_list_head);
if (ret < 0) {
scf_loge("\n");
return ret;
scf_basic_block_t* bb;
scf_bb_edge_t* edge;
- int i;
- int ret;
-
assert(!root->jmp_flag);
root->visited_flag = 1;
+ int i;
for (i = 0; i < root->nexts->size; ++i) {
bb = root->nexts->data[i];
edge->start = root;
edge->end = bb;
- ret = scf_vector_add(edges, edge);
+ int ret = scf_vector_add(edges, edge);
if (ret < 0) {
free(edge);
return ret;
if (scf_list_empty(bb_list_head))
return 0;
- scf_list_t* l;
scf_basic_block_t* bb;
+ scf_list_t* l;
int total = 0;
- for (l = scf_list_tail(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_prev(l)) {
-
+ for (l = scf_list_tail(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_prev(l)) {
bb = scf_list_data(l, scf_basic_block_t, list);
bb->visited_flag = 0;
}
dst->size = k0;
-
return 0;
}
if (!all)
return -ENOMEM;
- 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);
+
if (bb->jmp_flag)
continue;
scf_vector_qsort(all, _bb_cmp_dfo);
for (i = 0; i < all->size; i++) {
- bb = all->data[i];
+ bb = all->data[i];
scf_vector_qsort(bb->prevs, _bb_cmp_dfo);
scf_vector_qsort(bb->nexts, _bb_cmp_dfo);
scf_basic_block_t* dom1 = bb;
for (j = 0; j < dominators->size; j++) {
- dom = dominators->data[j];
+ dom = dominators->data[j];
if (bb->dfo == dom->dfo)
break;
break;
}
if (bb->dfo < dom->dfo) {
-
for (; j < dominators->size; j++) {
dom = dominators->data[j];
dominators->data[j] = dom1;
if (!f)
return -EINVAL;
- scf_list_t* bb_list_head = &f->basic_block_list_head;
- scf_list_t* l;
- scf_bb_edge_t* edge;
- scf_basic_block_t* bb;
-
- int ret;
- int i;
+ scf_bb_edge_t* edge;
+ scf_list_t* bb_list_head = &f->basic_block_list_head;
if (scf_list_empty(bb_list_head))
return 0;
scf_vector_clear(f->dfs_tree, free);
- ret = _bb_dfs_tree(bb_list_head, f);
+ int ret = _bb_dfs_tree(bb_list_head, f);
if (ret < 0)
return ret;
+ int i;
for (i = 0; i < f->dfs_tree->size; i++) {
edge = f->dfs_tree->data[i];
-
- scf_logd("bb_%p_%d --> bb_%p_%d\n",
- edge->start, edge->start->dfo,
- edge->end, edge->end->dfo);
+ scf_logd("bb_%p_%d --> bb_%p_%d\n", edge->start, edge->start->dfo, edge->end, edge->end->dfo);
}
ret = _bb_find_dominators(bb_list_head);
scf_basic_block_t* bb;
scf_basic_block_t* post;
scf_bb_group_t* bbg;
+
+ scf_dn_status_t* ds;
scf_dag_node_t* dn;
scf_3ac_code_t* load;
scf_3ac_code_t* save;
scf_3ac_code_t* c;
int i;
+ int j;
+ int k;
int ret;
#define SCF_OPTIMIZER_LOAD(load_type, h) \
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)) {
-
- 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++;
for (i = 0; i < f->bb_groups->size; i++) {
bbg = f->bb_groups->data[i];
- scf_dn_status_t* ds;
- int j;
- int k;
for (j = 0; j < bbg->body->size; j++) {
bb = bbg->body->data[j];
}
for (i = 0; i < f->bb_loops->size; i++) {
- bbg = f->bb_loops->data[i];
+ bbg = f->bb_loops->data[i];
qsort(bbg->body->data, bbg->body->size, sizeof(void*), _bb_index_cmp);
- scf_dn_status_t* ds;
- int j;
- int k;
for (j = 0; j < bbg->body->size; j++) {
bb = bbg->body->data[j];
.flags = SCF_OPTIMIZER_LOCAL,
};
-
} else
scf_vector_clear(bbg->exits, NULL);
-
for (j = 0; j < bbg->body->size; j++) {
bb = bbg->body->data[j];
static int _optimize_bbg_loads_saves(scf_function_t* f)
{
scf_basic_block_t* bb;
- scf_basic_block_t* bb2;
- scf_basic_block_t* entry;
- scf_basic_block_t* exit;
scf_bb_group_t* bbg;
- scf_dag_node_t* dn;
int i;
int j;
- int k;
for (i = 0; i < f->bb_groups->size; i++) {
bbg = f->bb_groups->data[i];
.flags = SCF_OPTIMIZER_LOCAL,
};
-
.flags = SCF_OPTIMIZER_LOCAL,
};
-
int nblocks = 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);
bb->index = nblocks++;
continue;
}
- scf_vector_t* loop = scf_vector_alloc();
- if (!loop)
+ scf_bb_group_t* bbg = scf_bb_group_alloc();
+ if (!bbg)
return -ENOMEM;
- ret = __bb_dfs_loop(bb_list_head, bb, dom, loop);
+ ret = __bb_dfs_loop(bb_list_head, bb, dom, bbg->body);
if (ret < 0) {
- scf_vector_free(loop);
+ scf_bb_group_free(bbg);
return ret;
}
-
- scf_bb_group_t* bbg = calloc(1, sizeof(scf_bb_group_t));
- if (!bbg) {
- scf_vector_free(loop);
- return -ENOMEM;
- }
bbg->loop_layers = 1;
- bbg->body = loop;
- loop = NULL;
ret = bbg_find_entry_exit(bbg);
if (ret < 0) {
entry = loop0->entries->data[k];
if (scf_vector_find(loop1->entries, entry))
- break;
+ goto found;
}
-
- if (k < loop0->entries->size)
- break;
}
-
+found:
if (j == loops->size) {
i++;
continue;
if (k < loop0->body->size)
continue;
- if (!loop0->loop_parent
- || loop0->loop_parent->body->size > loop1->body->size)
+ if (!loop0->loop_parent || loop0->loop_parent->body->size > loop1->body->size)
loop0->loop_parent = loop1;
if (loop1->loop_layers <= loop0->loop_layers + 1)
jmp = scf_list_data(scf_list_next(&bbg->entry->list), scf_basic_block_t, list);
if (jmp->jmp_flag) {
-
if (!jmp->jcc_flag)
return -EINVAL;
} else
bbg = NULL;
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);
if (bb->jmp_flag || bb->end_flag)
bb2 = bbg2->body->data[j];
if (bb == bb2)
- break;
+ goto found;
}
- if (j < bbg2->body->size)
- break;
}
-
+found:
if (i < f->bb_loops->size) {
if (bbg) {
if (scf_vector_add(f->bb_groups, bbg) < 0) {
if (scf_list_empty(bb_list_head))
return 0;
- if (!f->bb_loops) {
- f->bb_loops = scf_vector_alloc();
- if (!f->bb_loops)
- return -ENOMEM;
- } else
- scf_vector_clear(f->bb_loops, ( void (*)(void*) ) scf_vector_free);
-
- if (!f->bb_groups) {
- f ->bb_groups= scf_vector_alloc();
- if (!f->bb_groups)
- return -ENOMEM;
- } else
- scf_vector_clear(f->bb_groups, ( void (*)(void*) ) scf_vector_free);
+ scf_vector_clear(f->bb_loops, ( void (*)(void*) )scf_bb_group_free);
+ scf_vector_clear(f->bb_groups, ( void (*)(void*) )scf_bb_group_free);
int ret = _bb_dfs_loop(bb_list_head, f->bb_loops);
if (ret < 0) {
int ret;
do {
- start = scf_list_head(&bb->code_list_head);
+ start = scf_list_head (&bb->code_list_head);
end = scf_list_sentinel(&bb->code_list_head);
- ret = __optimize_alias_bb(&end, start, bb, bb_list_head);
-
+ ret = __optimize_alias_bb(&end, start, bb, bb_list_head);
if (ret < 0) {
scf_loge("\n");
return ret;
scf_basic_block_mov_code(scf_list_next(end), bb2, bb);
-// SCF_XCHG(bb2->dn_status_initeds, bb->dn_status_initeds);
-// scf_basic_block_inited_by3ac(bb);
-
scf_list_add_front(&bb->list, &bb2->list);
bb = bb2;
.flags = SCF_OPTIMIZER_LOCAL,
};
-
scf_basic_block_t* bb;
scf_dn_status_t* ds2;
+ int ret;
int i;
int j;
- int ret;
assert(!root->jmp_flag);
ds2 = bb->dn_status_initeds->data[j];
if (like) {
- if (0 == scf_dn_status_cmp_like_dn_indexes(ds, ds2))
+ if (0 == scf_ds_cmp_like_indexes(ds, ds2))
break;
} else {
- if (0 == scf_dn_status_cmp_same_dn_indexes(ds, ds2))
+ if (0 == scf_ds_cmp_same_indexes(ds, ds2))
break;
}
}
static int __bb_dfs_check_initeds(scf_basic_block_t* root, scf_basic_block_t* obj)
{
scf_basic_block_t* bb;
-
int i;
- int ret;
assert(!root->jmp_flag);
if (bb == obj)
return -1;
- ret = __bb_dfs_check_initeds(bb, obj);
+ int ret = __bb_dfs_check_initeds(bb, obj);
if ( ret < 0)
return ret;
}
bb2->visited_flag = 1;
}
- l = scf_list_head(bb_list_head);
+ l = scf_list_head(bb_list_head);
bb2 = scf_list_data(l, scf_basic_block_t, list);
return __bb_dfs_check_initeds(bb2, bb);
return 0;
}
-static int _bb_pointer_aliases(scf_vector_t* aliases, scf_list_t* bb_list_head, scf_basic_block_t* bb, scf_dn_status_t* ds_pointer)
+static int _find_aliases_from_initeds(scf_vector_t* aliases, scf_vector_t* initeds, scf_dn_status_t* ds_pointer)
{
- scf_vector_t* initeds;
scf_basic_block_t* bb2;
scf_dn_status_t* ds;
scf_dn_status_t* ds2;
- int ret;
int i;
int j;
- initeds = scf_vector_alloc();
- if (!initeds)
- return -ENOMEM;
-
- ret = _bb_pointer_initeds(initeds, bb_list_head, bb, ds_pointer);
- if (ret < 0) {
- scf_vector_free(initeds);
- return ret;
- }
-
for (i = 0; i < initeds->size; i++) {
bb2 = initeds->data[i];
for (j = 0; j < bb2->dn_status_initeds->size; j++) {
ds = bb2->dn_status_initeds->data[j];
- if (scf_dn_status_cmp_like_dn_indexes(ds, ds_pointer))
+ if (scf_ds_cmp_like_indexes(ds, ds_pointer))
continue;
- if (scf_vector_find_cmp(aliases, ds, scf_dn_status_cmp_alias))
+ if (scf_vector_find_cmp(aliases, ds, scf_ds_cmp_alias))
continue;
ds2 = scf_dn_status_clone(ds);
- if (!ds2) {
- scf_vector_free(initeds);
+ if (!ds2)
return -ENOMEM;
- }
- ret = scf_vector_add(aliases, ds2);
+ int ret = scf_vector_add(aliases, ds2);
if (ret < 0) {
scf_dn_status_free(ds2);
- scf_vector_free(initeds);
return ret;
}
}
return 0;
}
-static int _bb_pointer_aliases_leak(scf_vector_t* aliases, scf_list_t* bb_list_head, scf_basic_block_t* bb, scf_dn_status_t* ds_pointer)
+static int _bb_pointer_aliases(scf_vector_t* aliases, scf_list_t* bb_list_head, scf_basic_block_t* bb, scf_dn_status_t* ds_pointer)
{
- scf_vector_t* initeds;
- scf_basic_block_t* bb2;
- scf_dn_status_t* ds;
- scf_dn_status_t* ds2;
-
- int ret;
- int i;
- int j;
-
- initeds = scf_vector_alloc();
+ scf_vector_t* initeds = scf_vector_alloc();
if (!initeds)
return -ENOMEM;
- ret = _bb_pointer_initeds_leak(initeds, bb_list_head, bb, ds_pointer);
+ int ret = _bb_pointer_initeds(initeds, bb_list_head, bb, ds_pointer);
if (ret < 0) {
scf_vector_free(initeds);
return ret;
}
- for (i = 0; i < initeds->size; i++) {
- bb2 = initeds->data[i];
+ ret = _find_aliases_from_initeds(aliases, initeds, ds_pointer);
- for (j = 0; j < bb2->dn_status_initeds->size; j++) {
- ds = bb2->dn_status_initeds->data[j];
-
- if (scf_dn_status_cmp_like_dn_indexes(ds, ds_pointer))
- continue;
-
- if (scf_vector_find_cmp(aliases, ds, scf_dn_status_cmp_alias))
- continue;
+ scf_vector_free(initeds);
+ return ret;
+}
- ds2 = scf_dn_status_clone(ds);
- if (!ds2) {
- scf_vector_free(initeds);
- return -ENOMEM;
- }
+static int _bb_pointer_aliases_leak(scf_vector_t* aliases, scf_list_t* bb_list_head, scf_basic_block_t* bb, scf_dn_status_t* ds_pointer)
+{
+ scf_vector_t* initeds = scf_vector_alloc();
+ if (!initeds)
+ return -ENOMEM;
- ret = scf_vector_add(aliases, ds2);
- if (ret < 0) {
- scf_dn_status_free(ds2);
- scf_vector_free(initeds);
- return ret;
- }
- }
+ int ret = _bb_pointer_initeds_leak(initeds, bb_list_head, bb, ds_pointer);
+ if (ret < 0) {
+ scf_vector_free(initeds);
+ return ret;
}
- return 0;
+ ret = _find_aliases_from_initeds(aliases, initeds, ds_pointer);
+
+ scf_vector_free(initeds);
+ return ret;
}
-int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
+static int _find_aliases_from_current(scf_vector_t* aliases, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_dn_status_t* ds_pointer)
{
scf_dn_status_t* ds;
scf_dn_status_t* ds2;
- scf_dn_status_t* ds3;
scf_3ac_code_t* c2;
scf_list_t* l2;
if (!c2->dn_status_initeds)
continue;
- ds2 = scf_vector_find_cmp(c2->dn_status_initeds, ds_pointer, scf_dn_status_cmp_same_dn_indexes);
+ ds2 = scf_vector_find_cmp(c2->dn_status_initeds, ds_pointer, scf_ds_cmp_same_indexes);
if (!ds2)
continue;
if (!ds)
return -ENOMEM;
- int ret = scf_dn_status_copy_alias(ds, ds2);
+ int ret = scf_ds_copy_alias(ds, ds2);
if (ret < 0) {
scf_dn_status_free(ds);
return ret;
return ret;
}
- return 0;
+ return 1;
}
+ return 0;
+}
+
+int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
+{
+ int ret = _find_aliases_from_current(aliases, c, bb, ds_pointer);
+ if (ret < 0)
+ return ret;
+
+ if (ret > 0)
+ return 0;
+
return _bb_pointer_aliases_leak(aliases, bb_list_head, bb, ds_pointer);
}
if (!ds2)
return -ENOMEM;
- if (scf_dn_status_copy_alias(ds2, ds) < 0) {
+ if (scf_ds_copy_alias(ds2, ds) < 0) {
scf_dn_status_free(ds2);
return -ENOMEM;
}
int scf_pointer_alias_ds(scf_vector_t* aliases, scf_dn_status_t* ds_pointer, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
- scf_dn_status_t* ds;
- scf_dn_status_t* ds2;
- scf_dn_status_t* ds3;
- scf_3ac_code_t* c2;
- scf_list_t* l2;
-
- for (l2 = &c->list; l2 != scf_list_sentinel(&bb->code_list_head); l2 = scf_list_prev(l2)) {
-
- c2 = scf_list_data(l2, scf_3ac_code_t, list);
-
- if (!c2->dn_status_initeds)
- continue;
-
- ds2 = scf_vector_find_cmp(c2->dn_status_initeds, ds_pointer, scf_dn_status_cmp_same_dn_indexes);
- if (!ds2)
- continue;
-
- ds = scf_dn_status_null();
- if (!ds)
- return -ENOMEM;
-
- int ret = scf_dn_status_copy_alias(ds, ds2);
- if (ret < 0) {
- scf_dn_status_free(ds);
- return ret;
- }
-
- ret = scf_vector_add(aliases, ds);
- if (ret < 0) {
- scf_dn_status_free(ds);
- return ret;
- }
+ int ret = _find_aliases_from_current(aliases, c, bb, ds_pointer);
+ if (ret < 0)
+ return ret;
+ if (ret > 0)
return 0;
- }
return _bb_pointer_aliases(aliases, bb_list_head, bb, ds_pointer);
}
assert(aliases2->size > 0);
ds = aliases2->data[0];
- ret = scf_dn_status_copy_alias(_ds, ds);
+ ret = scf_ds_copy_alias(_ds, ds);
if (ret < 0)
goto error;
- ret = scf_dn_status_copy_alias(_ds2, ds);
+ ret = scf_ds_copy_alias(_ds2, ds);
if (ret < 0)
goto error;
static int _bb_dereference_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_pointer, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
- scf_dn_status_t* ds_pointer2 = NULL;
- scf_dn_status_t* ds = NULL;
- scf_3ac_code_t* c2;
+ scf_dn_status_t* ds = NULL;
+ scf_dn_status_t* ds2 = NULL;
int count = 0;
int ret;
if (!dst) \
return -ENOMEM; \
\
- ret = scf_dn_status_copy_alias(dst, src); \
+ ret = scf_ds_copy_alias(dst, src); \
if (ret < 0) { \
scf_dn_status_free(dst); \
return ret; \
dst->alias_indexes = NULL; \
} while (0)
- SCF_DS_POINTER_FROM_ALIAS(ds_pointer2, ds);
+ SCF_DS_POINTER_FROM_ALIAS(ds2, ds);
- ret = _dn_status_alias_dereference(aliases, ds_pointer2, c, bb, bb_list_head);
+ ret = _dn_status_alias_dereference(aliases, ds2, c, bb, bb_list_head);
- scf_dn_status_free(ds_pointer2);
- ds_pointer2 = NULL;
+ scf_dn_status_free(ds2);
+ ds2 = NULL;
if (ret < 0) {
scf_loge("\n");
for (i = 0; i < aliases2->size; i++) {
ds = aliases2->data[i];
- SCF_DS_POINTER_FROM_ALIAS(ds_pointer2, ds);
+ SCF_DS_POINTER_FROM_ALIAS(ds2, ds);
- ret = _dn_status_alias_dereference(aliases, ds_pointer2, c, bb, bb_list_head);
+ ret = _dn_status_alias_dereference(aliases, ds2, c, bb, bb_list_head);
- scf_dn_status_free(ds_pointer2);
- ds_pointer2 = NULL;
+ scf_dn_status_free(ds2);
+ ds2 = NULL;
if (ret < 0) {
scf_loge("\n");
ret = 0;
error:
scf_vector_clear(aliases2, ( void (*)(void*) )scf_dn_status_free);
- scf_vector_free(aliases2);
+ scf_vector_free (aliases2);
return ret;
}
if (!c2->dn_status_initeds)
continue;
- ds = scf_vector_find_cmp(c2->dn_status_initeds, ds_pointer, scf_dn_status_cmp_like_dn_indexes);
+ ds = scf_vector_find_cmp(c2->dn_status_initeds, ds_pointer, scf_ds_cmp_like_indexes);
if (ds && ds->alias) {
int ret = _dn_status_alias_dereference(aliases, ds, c, bb, bb_list_head);
scf_dn_status_free(ds);
-
return ret;
}
ret = scf_vector_add(aliases, ds2);
if (ret < 0) {
- scf_loge("\n");
scf_dn_status_free(ds2);
return ret;
}
return 0;
}
+static int _pointer_alias_dereference2(scf_vector_t* aliases, scf_dn_status_t* ds, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
+{
+ scf_vector_t* tmp = scf_vector_alloc();
+ if (!tmp)
+ return -ENOMEM;
+
+ int ret = scf_pointer_alias_ds(tmp, ds, c, bb, bb_list_head);
+ if (ret < 0)
+ goto error;
+
+ int j;
+ for (j = 0; j < tmp->size; j++) {
+ ds = tmp->data[j];
+
+ ret = scf_vector_add(aliases, ds);
+ if (ret < 0)
+ goto error;
+
+ scf_dn_status_ref(ds);
+ }
+
+ ret = tmp->size;
+error:
+ scf_vector_clear(tmp, (void (*)(void*))scf_dn_status_free);
+ scf_vector_free (tmp);
+ return ret;
+}
+
static int _pointer_alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_alias, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
scf_dn_status_t* ds;
scf_dn_status_t* ds2;
scf_dag_node_t* dn_child;
scf_vector_t* aliases2 = NULL;
- scf_vector_t* aliases3 = NULL;
assert(dn_alias->childs && 1 == dn_alias->childs->size);
for (i = 0; i < aliases2->size; i++) {
ds = aliases2->data[i];
- if (!ds)
- continue;
+ assert(ds);
ds2 = scf_dn_status_null();
if (!ds2) {
goto error;
}
- ret = scf_dn_status_copy_alias(ds2, ds);
+ ret = scf_ds_copy_alias(ds2, ds);
if (ret < 0) {
scf_dn_status_free(ds2);
goto error;
ds2->alias = NULL;
ds2->alias_indexes = NULL;
- aliases3 = scf_vector_alloc();
- if (!aliases3) {
- scf_loge("\n");
-
- scf_dn_status_free(ds2);
- ret = -ENOMEM;
- goto error;
- }
-
- ret = scf_pointer_alias_ds(aliases3, ds2, c, bb, bb_list_head);
-
+ ret = _pointer_alias_dereference2(aliases, ds2, c, bb, bb_list_head);
if (ret < 0) {
scf_dn_status_free(ds2);
- scf_vector_free(aliases3);
goto error;
}
- if (0 == aliases3->size) {
-
+ if (0 == ret) {
ds2->alias = ds2->dag_node;
ds2->alias_indexes = ds2->dn_indexes;
ds2->dag_node = NULL;
ret = scf_vector_add(aliases, ds2);
if (ret < 0) {
scf_dn_status_free(ds2);
- scf_vector_free(aliases3);
goto error;
}
-
- scf_dn_status_ref(ds);
-
- } else {
+ } else
scf_dn_status_free(ds2);
- ds2 = NULL;
-
- int j;
- for (j = 0; j < aliases3->size; j++) {
- ds = aliases3->data[j];
-
- ret = scf_vector_add(aliases, ds);
- if (ret < 0) {
- scf_vector_free(aliases3);
- goto error;
- }
-
- scf_dn_status_ref(ds);
- }
- }
-
- scf_vector_clear(aliases3, (void (*)(void*))scf_dn_status_free);
- scf_vector_free(aliases3);
- aliases3 = NULL;
+ ds2 = NULL;
}
error:
scf_vector_clear(aliases2, (void (*)(void*))scf_dn_status_free);
- scf_vector_free(aliases2);
- aliases2 = NULL;
+ scf_vector_free (aliases2);
return ret;
}
int n = sizeof(type_abbrevs) / sizeof(type_abbrevs[0]);
for (i = 0; i < n; i++) {
-
t = &type_abbrevs[i];
if (!strcmp(t->name, name))
return NULL;
}
-
scf_variable_t* scf_variable_alloc(scf_lex_word_t* w, scf_type_t* t)
{
- scf_variable_t* var = calloc(1, sizeof(scf_variable_t));
- assert(var);
+ scf_variable_t* v = calloc(1, sizeof(scf_variable_t));
+ if (!v)
+ return NULL;
- var->refs = 1;
- var->type = t->type;
+ v->refs = 1;
+ v->type = t->type;
- var->const_flag = t->node.const_flag;
- var->nb_pointers = t->nb_pointers;
- var->func_ptr = t->func_ptr;
+ v->const_flag = t->node.const_flag;
+ v->nb_pointers = t->nb_pointers;
+ v->func_ptr = t->func_ptr;
- if (var->nb_pointers > 0)
- var->size = sizeof(void*);
+ if (v->nb_pointers > 0)
+ v->size = sizeof(void*);
else
- var->size = t->size;
+ v->size = t->size;
- if (var->nb_pointers > 1)
- var->data_size = sizeof(void*);
+ if (v->nb_pointers > 1)
+ v->data_size = sizeof(void*);
else
- var->data_size = t->size;
+ v->data_size = t->size;
- var->offset = t->offset;
+ v->offset = t->offset;
if (w) {
- var->w = scf_lex_word_clone(w);
+ v->w = scf_lex_word_clone(w);
+ if (!v->w) {
+ free(v);
+ return NULL;
+ }
if (scf_lex_is_const(w)) {
- var->const_flag = 1;
- var->const_literal_flag = 1;
+ v->const_flag = 1;
+ v->const_literal_flag = 1;
switch (w->type) {
case SCF_LEX_WORD_CONST_CHAR:
- var->data.u32 = w->data.u32;
+ v->data.u32 = w->data.u32;
break;
+
case SCF_LEX_WORD_CONST_STRING:
- var->data.s = scf_string_clone(w->data.s);
+ v->data.s = scf_string_clone(w->data.s);
+ if (!v->data.s) {
+ scf_lex_word_free(v->w);
+ free(v);
+ return NULL;
+ }
break;
case SCF_LEX_WORD_CONST_INT:
- var->data.i = w->data.i;
+ v->data.i = w->data.i;
break;
case SCF_LEX_WORD_CONST_U32:
- var->data.u32 = w->data.u32;
+ v->data.u32 = w->data.u32;
break;
case SCF_LEX_WORD_CONST_FLOAT:
- var->data.f = w->data.f;
+ v->data.f = w->data.f;
break;
case SCF_LEX_WORD_CONST_DOUBLE:
- var->data.d = w->data.d;
+ v->data.d = w->data.d;
break;
case SCF_LEX_WORD_CONST_COMPLEX:
- var->data.z = w->data.z;
+ v->data.z = w->data.z;
break;
case SCF_LEX_WORD_CONST_I64:
- var->data.i64 = w->data.i64;
+ v->data.i64 = w->data.i64;
break;
case SCF_LEX_WORD_CONST_U64:
- var->data.u64 = w->data.u64;
+ v->data.u64 = w->data.u64;
break;
default:
break;
};
}
- } else {
- var->w = NULL;
}
- return var;
+ return v;
}
scf_variable_t* scf_variable_clone(scf_variable_t* v)
v2->data_size = v->data_size;
v2->offset = v->offset;
- if (SCF_STRUCT >= v->type) {
- if (v->data.p) {
- v2->data.p = malloc(v->size);
- if (!v2->data.p) {
- scf_variable_free(v2);
- return NULL;
- }
-
- memcpy(v2->data.p, v->data.p, v->size);
- }
-
- } else if (v->nb_dimentions > 0) {
+ if (scf_variable_is_struct(v) || scf_variable_is_array(v)) {
int size = scf_variable_size(v);
memcpy(v2->data.p, v->data.p, size);
}
+
+ } else if (scf_variable_const_string(v)) {
+
+ v2->data.s = scf_string_clone(v->data.s);
+ if (!v2->data.s) {
+ scf_variable_free(v2);
+ return NULL;
+ }
} else
memcpy(&v2->data, &v->data, sizeof(v->data));
assert(0 == v->refs);
- if (v->w) {
- scf_lex_word_free(v->w);
- v->w = NULL;
- }
-
if (v->signature) {
scf_string_free(v->signature);
v->signature = NULL;
}
+ if (v->dimentions) {
+ free(v->dimentions);
+ v->dimentions = NULL;
+ }
+
+ if (scf_variable_is_struct(v) || scf_variable_is_array(v)) {
+ if (v->data.p) {
+ free(v->data.p);
+ v->data.p = NULL;
+ }
+
+ } else if (scf_variable_const_string(v)) {
+ if (v->data.s) {
+ scf_string_free(v->data.s);
+ v->data.s = NULL;
+ }
+ }
+
+ if (v->w) {
+ scf_lex_word_free(v->w);
+ v->w = NULL;
+ }
+
free(v);
+ v = NULL;
}
}
-void scf_variable_add_array_dimention(scf_variable_t* var, int dimention_size)
+void scf_variable_add_array_dimention(scf_variable_t* v, int dimention_size)
{
- assert(var);
+ assert(v);
- void* p = realloc(var->dimentions, sizeof(int) * (var->nb_dimentions + 1));
+ void* p = realloc(v->dimentions, sizeof(int) * (v->nb_dimentions + 1));
assert(p);
- var->dimentions = p;
- var->dimentions[var->nb_dimentions++] = dimention_size;
+
+ v->dimentions = p;
+ v->dimentions[v->nb_dimentions++] = dimention_size;
}
void scf_variable_get_array_member(scf_variable_t* array, int index, scf_variable_t* member)
memcpy(array->data.p + index * member->size, &(member->data.i), member->size);
}
-void scf_variable_print(scf_variable_t* var)
+void scf_variable_print(scf_variable_t* v)
{
- assert(var);
+ assert(v);
- if (var->nb_pointers > 0) {
- printf("%s(),%d, print var: name: %s, type: %d, value: %p\n",
- __func__, __LINE__, var->w->text->data, var->type, var->data.p);
- return;
+ if (v->nb_pointers > 0) {
+ printf("print var: name: %s, type: %d, value: %p\n", v->w->text->data, v->type, v->data.p);
+ return;
}
- if (SCF_VAR_CHAR == var->type || SCF_VAR_INT == var->type) {
- printf("%s(),%d, print var: name: %s, type: %d, value: %d\n",
- __func__, __LINE__, var->w->text->data, var->type, var->data.i);
-
- //assert(0);
+ if (SCF_VAR_CHAR == v->type || SCF_VAR_INT == v->type)
+ printf("print var: name: %s, type: %d, value: %d\n", v->w->text->data, v->type, v->data.i);
- } else if (SCF_VAR_DOUBLE == var->type) {
- printf("%s(),%d, print var: name: %s, type: %d, value: %lg\n",
- __func__, __LINE__, var->w->text->data, var->type, var->data.d);
- } else {
- printf("%s(),%d, print var: name: %s, type: %d\n",
- __func__, __LINE__, var->w->text->data, var->type);
- }
+ else if (SCF_VAR_DOUBLE == v->type)
+ printf("print var: name: %s, type: %d, value: %lg\n", v->w->text->data, v->type, v->data.d);
+ else
+ printf("print var: name: %s, type: %d\n", v->w->text->data, v->type);
}
int scf_variable_same_type(scf_variable_t* v0, scf_variable_t* v1)
return capacity * v->size;
}
-
-
return 0;
}
-static int _x64_overflow_reg3(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_function_t* f)
-{
- scf_variable_t* v;
- scf_variable_t* v2;
- scf_register_t* r2;
- scf_dn_status_t* ds2;
- scf_dag_node_t* dn2;
-
- int i;
- int j;
-
- for (i = 0; i < sizeof(x64_registers) / sizeof(x64_registers[0]); i++) {
-
- r2 = &(x64_registers[i]);
-
- if (SCF_X64_REG_RSP == r2->id || SCF_X64_REG_RBP == r2->id)
- continue;
-
- if (!X64_COLOR_CONFLICT(r->color, r2->color))
- continue;
-
- for (j = 0; j < r2->dag_nodes->size; ) {
- dn2 = r2->dag_nodes->data[j];
-
- if (dn2 == dn) {
- j++;
- continue;
- }
-
- ds2 = scf_vector_find_cmp(c->active_vars, dn2, scf_dn_status_cmp);
-
- if (!ds2 || !ds2->active) {
- assert(0 == scf_vector_del(r2->dag_nodes, dn2));
-#if 1
- v2 = dn2->var;
- if (v2->w)
- scf_logw("drop inactive v2_%d_%d/%s, bp_offset: %d\n", v2->w->line, v2->w->pos, v2->w->text->data, v2->bp_offset);
- else
- scf_logw("drop inactive v2_%#lx, bp_offset: %d\n", 0xffff & (uintptr_t)v2, v2->bp_offset);
-#endif
- dn2->loaded = 0;
- dn2->color = -1;
- j++;
- continue;
- }
-#if 0
- v = dn->var;
- if (v->w)
- scf_logi("v_%d_%d/%s, bp_offset: %d\n", v->w->line, v->w->pos, v->w->text->data, v->bp_offset);
- else
- scf_logi("v_%#lx, bp_offset: %d\n", 0xffff & (uintptr_t)v, v->bp_offset);
-#endif
- int ret = x64_save_var(dn2, c, f);
- if (ret < 0)
- return ret;
- }
- }
-
- r->used = 1;
- return 0;
-}
-
int x64_reg_used(scf_register_t* r, scf_dag_node_t* dn)
{
scf_register_t* r2;
if (dn->color > 0) {
r = x64_find_register_color(dn->color);
#if 1
- ret = _x64_overflow_reg3(r, dn, c, f);
+ ret = x64_overflow_reg2(r, dn, c, f);
if (ret < 0) {
scf_loge("\n");
return -1;