delete some unused or repeat code
authoryu.dongliang <18588496441@163.com>
Wed, 9 Oct 2024 10:38:30 +0000 (18:38 +0800)
committeryu.dongliang <18588496441@163.com>
Wed, 9 Oct 2024 10:38:30 +0000 (18:38 +0800)
24 files changed:
core/scf_basic_block.c
core/scf_basic_block.h
core/scf_dag.c
core/scf_dag.h
core/scf_label.c
core/scf_optimizer.c
core/scf_optimizer_active_vars.c
core/scf_optimizer_auto_gc.c
core/scf_optimizer_auto_gc_find.c
core/scf_optimizer_basic_block.c
core/scf_optimizer_call.c
core/scf_optimizer_common_expr.c
core/scf_optimizer_dag.c
core/scf_optimizer_dominators.c
core/scf_optimizer_generate_loads_saves.c
core/scf_optimizer_group.c
core/scf_optimizer_loads_saves.c
core/scf_optimizer_loop.c
core/scf_optimizer_pointer_alias.c
core/scf_optimizer_pointer_aliases.c
core/scf_pointer_alias.c
core/scf_type.c
core/scf_variable.c
native/x64/scf_x64_reg.c

index fddd9f58257560de19c7865d6cd1479d4f5bb5f7..c91480515c63e550d10ad81e39f1d3f75b585580 100644 (file)
@@ -335,7 +335,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                        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);
                        }
@@ -602,7 +602,7 @@ static int _bb_vars(scf_basic_block_t* bb)
        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;
@@ -647,44 +647,6 @@ int scf_basic_block_vars(scf_basic_block_t* bb, scf_list_t* bb_list_head)
        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;
@@ -695,8 +657,8 @@ static int _bb_init_pointer_aliases(scf_dn_status_t* ds_pointer, scf_dag_node_t*
        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)
@@ -721,13 +683,13 @@ static int _bb_init_pointer_aliases(scf_dn_status_t* ds_pointer, scf_dag_node_t*
                }
                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;
@@ -1011,7 +973,7 @@ int scf_basic_block_inited_by3ac(scf_basic_block_t* bb)
                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);
 
index 356d9b762a9819b95b7567fb240e0143d76c2906..ad02ed3c90a0413e6ee68895b839db7d2be5590b 100644 (file)
@@ -123,8 +123,7 @@ void                scf_basic_block_print(scf_basic_block_t* bb, scf_list_t* sen
 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);
index 3b3c273af04a9eecd53333aee829524772da2481..85ab1d0e66fbb837a538a984110356dc6124c47b 100644 (file)
@@ -100,7 +100,6 @@ scf_dn_status_t* scf_dn_status_null()
                return NULL;
 
        ds->refs = 1;
-
        return ds;
 }
 
@@ -122,7 +121,7 @@ scf_dn_status_t* scf_dn_status_alloc(scf_dag_node_t* dn)
        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;
@@ -145,14 +144,14 @@ int scf_dn_status_copy_dn(scf_dn_status_t* dst, scf_dn_status_t* src)
                        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;
@@ -176,7 +175,7 @@ int scf_dn_status_copy_alias(scf_dn_status_t* dst, scf_dn_status_t* src)
                        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++;
                }
        }
@@ -243,6 +242,7 @@ scf_dn_status_t* scf_dn_status_ref(scf_dn_status_t* ds)
 {
        if (ds)
                ds->refs++;
+       return ds;
 }
 
 void scf_dn_status_free(scf_dn_status_t* ds)
@@ -257,17 +257,13 @@ 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);
@@ -398,7 +394,7 @@ void scf_dag_node_free(scf_dag_node_t* dn)
        }
 }
 
-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;
@@ -431,17 +427,17 @@ static int _dn_status_cmp_dn_indexes(const void* p0, const void* p1,
        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;
@@ -797,56 +793,6 @@ int scf_dag_find_roots(scf_list_t* h, scf_vector_t* roots)
        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;
@@ -889,19 +835,23 @@ int scf_dn_status_alias_index(scf_dn_status_t* ds, scf_dag_node_t* dn_index, int
        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;
 
@@ -909,7 +859,11 @@ void scf_dn_status_vector_clear_by_dn(scf_vector_t* vec, scf_dag_node_t* dn)
                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;
        }
 }
 
index 12fa97438e1e69084848c058bdc4427a0bdc0b59..9ca0cdc16dc830e2a279f70d13df50f2485846c4 100644 (file)
@@ -100,38 +100,29 @@ scf_dn_status_t*  scf_dn_status_ref  (scf_dn_status_t* ds);
 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);
 
@@ -142,8 +133,8 @@ int               scf_ds_for_assign_member     (scf_dn_status_t** pds, scf_dag_n
 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)
 {
index 60406c1ac1d752586b2fdfbc5af12b1e24fcf7bb..63bdb9e33bbe74d18a05480fdd60c4ebc2daf996 100644 (file)
@@ -3,19 +3,26 @@
 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;
@@ -23,7 +30,5 @@ void scf_label_free(scf_label_t* l)
 
                l->node = NULL;
                free(l);
-               l = NULL;
        }
 }
-
index d0ee2ce00890cbfc8e2123347f8b0507e7ef4d33..15dee643a853bb7409cd4b4ee470c39d1313adf4 100644 (file)
@@ -106,17 +106,16 @@ int scf_optimize(scf_ast_t* ast, scf_vector_t* functions)
 
                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
index 809db8451f67e92d7d1994c8dc414481a56b56cf..8432bc7c131b5f584859185fae2d09be103539ac 100644 (file)
@@ -110,4 +110,3 @@ scf_optimizer_t  scf_optimizer_active_vars =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index 9192bbef6c234995e6fa737eeab448d1024a21c5..dbbb96c67ed39880f2b94260b50297de97112f2b 100644 (file)
@@ -672,10 +672,10 @@ static int _find_ds_malloced(scf_basic_block_t* bb, void* data)
 {
        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;
        }
@@ -1088,13 +1088,13 @@ static int _bb_split_prev_add_free(scf_ast_t* ast, scf_function_t* f, scf_basic_
                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);
@@ -1126,10 +1126,10 @@ static int _bb_split_prevs(scf_basic_block_t* bb, scf_dn_status_t* ds, scf_vecto
        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);
@@ -1387,10 +1387,10 @@ static int _bb_prevs_malloced(scf_basic_block_t* bb, scf_vector_t* ds_malloced)
 
                        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);
@@ -1435,13 +1435,13 @@ static int _bb_need_ref(scf_dn_status_t* ds_obj, scf_vector_t* ds_malloced,
                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;
        }
 
@@ -1449,75 +1449,18 @@ static int _bb_need_ref(scf_dn_status_t* ds_obj, scf_vector_t* ds_malloced,
        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)
@@ -1652,14 +1595,14 @@ static int _optimize_auto_gc_bb(scf_ast_t* ast, scf_function_t* f, scf_basic_blo
                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)
 
@@ -1719,7 +1662,7 @@ _ref:
                                        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 {
@@ -1765,7 +1708,7 @@ _ref:
                                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 {
@@ -1827,7 +1770,6 @@ static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_vector_t* fu
                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)
@@ -1845,7 +1787,6 @@ static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_vector_t* fu
                        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);
@@ -1890,7 +1831,7 @@ static int _optimize_auto_gc(scf_ast_t* ast, scf_function_t* f, scf_vector_t* fu
 
                                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;
@@ -1938,4 +1879,3 @@ scf_optimizer_t  scf_optimizer_auto_gc =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index aba934809f0fc5d1f1c1f6a70e8c518b81e7ed63..64095053f6173f563d59cedec0387d4613c6131d 100644 (file)
@@ -3,10 +3,10 @@
 
 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;
 }
@@ -15,7 +15,7 @@ static int _bb_add_ds(scf_basic_block_t* bb, scf_dn_status_t* ds_obj)
 {
        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)
@@ -23,10 +23,10 @@ static int _bb_add_ds(scf_basic_block_t* bb, scf_dn_status_t* 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;
@@ -43,9 +43,9 @@ static int _bb_del_ds(scf_basic_block_t* bb, scf_dn_status_t* ds_obj)
 {
        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);
@@ -54,7 +54,7 @@ static int _bb_del_ds(scf_basic_block_t* bb, scf_dn_status_t* ds_obj)
                }
        }
 
-       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)
@@ -390,15 +390,15 @@ static int _bb_find_ds_alias(scf_dn_status_t* ds_obj, scf_3ac_code_t* c, scf_bas
                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;
        }
 
@@ -847,13 +847,13 @@ static int _auto_gc_bb_next_find(scf_basic_block_t* bb, void* data, scf_vector_t
                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;
 
index a3ceb8706c0083658aa56fb4ac8fbcadbeb19388..27a9caf39222681f35243e806d58679d961a3ee7 100644 (file)
@@ -211,7 +211,7 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
        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;
 
@@ -266,8 +266,7 @@ static int _optimize_basic_block(scf_ast_t* ast, scf_function_t* f, scf_vector_t
        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
index 77824ad1501aa96037557e001e4d9d567583e45a..4663abd43517fd5f79ebdad90ff1130cc40711ce 100644 (file)
@@ -4,12 +4,11 @@
 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;
@@ -48,8 +47,7 @@ static int _alias_call(scf_vector_t* aliases, scf_3ac_code_t* c, scf_basic_block
                        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) {
@@ -63,42 +61,42 @@ static int _alias_call(scf_vector_t* aliases, scf_3ac_code_t* c, scf_basic_block
                        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;
index 5eaaa3c66f6bf7cf7c3d1ea6b31bda2bea1fc935..f8bb2c41a1ce19382b00937a5d16344d886c5378 100644 (file)
@@ -52,7 +52,7 @@ int __optimize_common_expr(scf_basic_block_t* bb, scf_function_t* f)
        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;
 
@@ -123,9 +123,7 @@ static int _optimize_common_expr(scf_ast_t* ast, scf_function_t* f, scf_vector_t
                        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;
 }
 
index 767f4dcbe4b6bc463f625bacd6f4d64dcf44b858..d2c08316023e6d919be56cdfb8e6fb447540d487 100644 (file)
@@ -20,7 +20,13 @@ static int _optimize_dag(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functi
 
                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;
index 9c2d52b1d1db76654a784dfcc7cf4e65906c3892..ffa7d141be00dbd20f5ddd3939c2c87401eca623 100644 (file)
@@ -5,13 +5,11 @@ static int __bb_dfs_tree(scf_basic_block_t* root, scf_vector_t* edges, int* tota
        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];
 
@@ -25,7 +23,7 @@ static int __bb_dfs_tree(scf_basic_block_t* root, scf_vector_t* edges, int* tota
                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;
@@ -48,14 +46,12 @@ static int _bb_dfs_tree(scf_list_t* bb_list_head, scf_function_t* f)
        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;
@@ -110,7 +106,6 @@ static int _bb_intersection(scf_vector_t* dst, scf_vector_t* src)
        }
 
        dst->size = k0;
-
        return 0;
 }
 
@@ -136,10 +131,9 @@ static int _bb_find_dominators(scf_list_t* bb_list_head)
        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;
 
@@ -151,7 +145,7 @@ static int _bb_find_dominators(scf_list_t* bb_list_head)
        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);
@@ -220,7 +214,7 @@ static int _bb_find_dominators(scf_list_t* bb_list_head)
                        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;
@@ -229,7 +223,6 @@ static int _bb_find_dominators(scf_list_t* bb_list_head)
                                        break;
                        }
                        if (bb->dfo < dom->dfo) {
-
                                for (; j < dominators->size; j++) {
                                        dom                 = dominators->data[j];
                                        dominators->data[j] = dom1;
@@ -298,29 +291,22 @@ static int _optimize_dominators(scf_ast_t* ast, scf_function_t* f, scf_vector_t*
        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);
index 04014b945a6a087b35b85f651b98e61e632e531f..c2d5ec292dbcd45eeff8338a37f75172839987e8 100644 (file)
@@ -27,12 +27,16 @@ static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf
        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) \
@@ -60,8 +64,7 @@ static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf
        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++;
 
@@ -133,9 +136,6 @@ static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf
        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];
 
@@ -174,13 +174,10 @@ static int _optimize_generate_loads_saves(scf_ast_t* ast, scf_function_t* f, scf
        }
 
        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];
 
@@ -229,4 +226,3 @@ scf_optimizer_t  scf_optimizer_generate_loads_saves =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index cc9f9ad54dc0512f338b4751119fac70976c7991..e44b37615f21bfe511c0f7e07a678ee4246deaaf 100644 (file)
@@ -22,7 +22,6 @@ int bbg_find_entry_exit(scf_bb_group_t* bbg)
        } else
                scf_vector_clear(bbg->exits, NULL);
 
-
        for (j = 0; j < bbg->body->size; j++) {
                bb =        bbg->body->data[j];
 
@@ -105,15 +104,10 @@ int _optimize_peep_hole(scf_bb_group_t* bbg, scf_basic_block_t* bb)
 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];
@@ -155,4 +149,3 @@ scf_optimizer_t  scf_optimizer_group =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index 7c8ee29b382f6b769a268ebd3ba15a96a24ca929..75bdecdbc5323b1a9b9ca0006f8b32f1519eb8d6 100644 (file)
@@ -32,4 +32,3 @@ scf_optimizer_t  scf_optimizer_loads_saves =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index 60e2daa8f1eeba8951181f6f46d27453000144fa..6a6f50e1642dead54dc6787f7c8f6eb97f32d58d 100644 (file)
@@ -115,7 +115,6 @@ static int _bb_dfs_loop(scf_list_t* bb_list_head, scf_vector_t* loops)
        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++;
@@ -142,24 +141,16 @@ static int _bb_dfs_loop(scf_list_t* bb_list_head, scf_vector_t* loops)
                                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) {
@@ -208,13 +199,10 @@ static int __bb_loop_merge(scf_vector_t* loops)
                                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;
@@ -369,8 +357,7 @@ static int __bb_loop_layers(scf_function_t* f)
                        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)
@@ -457,7 +444,6 @@ static int _bb_loop_add_pre_post(scf_function_t* f)
                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
@@ -709,7 +695,6 @@ static int _bb_not_in_loop(scf_function_t* f, scf_list_t* bb_list_head)
 
        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)
@@ -722,12 +707,10 @@ static int _bb_not_in_loop(scf_function_t* f, scf_list_t* bb_list_head)
                                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) {
@@ -794,19 +777,8 @@ static int _optimize_loop(scf_ast_t* ast, scf_function_t* f, scf_vector_t* funct
        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) {
index dd836fd898c14308700353d1663fac8dc9f0e441..71a4ce327e660cbf61fb9c8d4757f1dc871f5fb1 100644 (file)
@@ -507,11 +507,10 @@ static int _optimize_alias_bb(scf_basic_block_t* bb, scf_list_t* bb_list_head)
        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;
@@ -539,9 +538,6 @@ static int _optimize_alias_bb(scf_basic_block_t* bb, scf_list_t* bb_list_head)
 
                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;
index 64fe848df06874809e5889cf9187a8ae5b9b47f4..eb47d7e399606e6c47bea3719777c45fd6bec9a2 100644 (file)
@@ -185,4 +185,3 @@ scf_optimizer_t  scf_optimizer_pointer_aliases =
 
        .flags    = SCF_OPTIMIZER_LOCAL,
 };
-
index fe189abdb7b6dcfa94bbf964ba377981a7e43469..b31907c4b6c60770c84ac26de33f0fbd51cedfc7 100644 (file)
@@ -6,9 +6,9 @@ static int __bb_dfs_initeds(scf_basic_block_t* root, scf_dn_status_t* ds, scf_ve
        scf_basic_block_t* bb;
        scf_dn_status_t*   ds2;
 
+       int ret;
        int i;
        int j;
-       int ret;
 
        assert(!root->jmp_flag);
 
@@ -26,10 +26,10 @@ static int __bb_dfs_initeds(scf_basic_block_t* root, scf_dn_status_t* ds, scf_ve
                        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;
                        }
                }
@@ -55,9 +55,7 @@ static int __bb_dfs_initeds(scf_basic_block_t* root, scf_dn_status_t* ds, scf_ve
 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);
 
@@ -78,7 +76,7 @@ static int __bb_dfs_check_initeds(scf_basic_block_t* root, scf_basic_block_t* ob
                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;
        }
@@ -123,7 +121,7 @@ static int _bb_dfs_check_initeds(scf_list_t* bb_list_head, scf_basic_block_t* bb
                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);
@@ -297,49 +295,34 @@ static int _bb_pointer_initeds_leak(scf_vector_t* initeds, scf_list_t* bb_list_h
        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;
                        }
                }
@@ -348,62 +331,46 @@ static int _bb_pointer_aliases(scf_vector_t* aliases, scf_list_t* bb_list_head,
        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;
 
@@ -414,7 +381,7 @@ int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer
                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;
 
@@ -422,7 +389,7 @@ int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer
                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;
@@ -434,9 +401,21 @@ int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer
                        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);
 }
 
@@ -471,7 +450,7 @@ int _bb_copy_aliases(scf_basic_block_t* bb, scf_dag_node_t* dn_pointer, scf_dag_
                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;
                }
@@ -540,41 +519,12 @@ int _bb_copy_aliases2(scf_basic_block_t* bb, scf_vector_t* aliases)
 
 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);
 }
@@ -609,11 +559,11 @@ static int _bb_op_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_pointer, sc
        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;
 
@@ -637,9 +587,8 @@ int _dn_status_alias_dereference(scf_vector_t* aliases, scf_dn_status_t* ds_poin
 
 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;
@@ -657,7 +606,7 @@ static int _bb_dereference_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_po
                        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; \
@@ -669,12 +618,12 @@ static int _bb_dereference_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_po
                        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");
@@ -704,12 +653,12 @@ static int _bb_dereference_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_po
        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");
@@ -720,7 +669,7 @@ static int _bb_dereference_aliases(scf_vector_t* aliases, scf_dn_status_t* ds_po
        ret = 0;
 error:
        scf_vector_clear(aliases2, ( void (*)(void*) )scf_dn_status_free);
-       scf_vector_free(aliases2);
+       scf_vector_free (aliases2);
        return ret;
 }
 
@@ -767,7 +716,7 @@ int _dn_status_alias_dereference(scf_vector_t* aliases, scf_dn_status_t* ds_poin
                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) {
 
@@ -795,7 +744,6 @@ int __alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_pointer, scf_3
        int ret = _dn_status_alias_dereference(aliases, ds, c, bb, bb_list_head);
 
        scf_dn_status_free(ds);
-
        return ret;
 }
 
@@ -1089,7 +1037,6 @@ static int _pointer_alias_array_index(scf_vector_t* aliases, scf_dag_node_t* dn,
 
                ret = scf_vector_add(aliases, ds2);
                if (ret < 0) {
-                       scf_loge("\n");
                        scf_dn_status_free(ds2);
                        return ret;
                }
@@ -1116,13 +1063,40 @@ static int _pointer_alias_call(scf_vector_t* aliases, scf_dag_node_t* dn_alias)
        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);
 
@@ -1144,8 +1118,7 @@ static int _pointer_alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_
        for (i = 0; i < aliases2->size; i++) {
                ds =        aliases2->data[i];
 
-               if (!ds)
-                       continue;
+               assert(ds);
 
                ds2 = scf_dn_status_null();
                if (!ds2) {
@@ -1153,7 +1126,7 @@ static int _pointer_alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_
                        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;
@@ -1164,25 +1137,13 @@ static int _pointer_alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_
                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;
@@ -1191,39 +1152,16 @@ static int _pointer_alias_dereference(scf_vector_t* aliases, scf_dag_node_t* dn_
                        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;
 }
 
index 18510e78a649dc7eb4f54661c749efd8236f02c3..5a13a3aa093110b540717c0f015ea2cb32b4e457 100644 (file)
@@ -84,7 +84,6 @@ const char* scf_type_find_abbrev(const char* name)
        int n = sizeof(type_abbrevs) / sizeof(type_abbrevs[0]);
 
        for (i = 0; i < n; i++) {
-
                t  = &type_abbrevs[i];
 
                if (!strcmp(t->name, name))
@@ -93,4 +92,3 @@ const char* scf_type_find_abbrev(const char* name)
 
        return NULL;
 }
-
index 27387846dc348e8edf9c18b56ff8c43b1deb7a75..daeab44ab0ab8d1a594db568d569fdd2b4e24572 100644 (file)
@@ -93,74 +93,83 @@ int scf_member_add_index(scf_member_t* m, scf_variable_t* member, int index)
 
 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)
@@ -200,18 +209,7 @@ 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);
 
@@ -224,6 +222,14 @@ scf_variable_t*    scf_variable_clone(scf_variable_t* 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));
 
@@ -256,28 +262,48 @@ void scf_variable_free(scf_variable_t* v)
 
                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)
@@ -302,29 +328,22 @@ void scf_variable_set_array_member(scf_variable_t* array, int index, scf_variabl
        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)
@@ -457,5 +476,3 @@ int scf_variable_size(scf_variable_t* v)
 
        return capacity * v->size;
 }
-
-
index a82d24259289db4f0869e1fabb8f332d1c8d7446..b4bb4bae8df926751d819165e3b26130950a1094 100644 (file)
@@ -650,68 +650,6 @@ int x64_overflow_reg2(scf_register_t* r, scf_dag_node_t* dn, scf_3ac_code_t* c,
        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;
@@ -1049,7 +987,7 @@ int x64_select_reg(scf_register_t** preg, scf_dag_node_t* dn, scf_3ac_code_t* c,
        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;