use scf_ast_find_proper_function() instead of _semantic_find_proper_function2(),... master
authoryu.dongliang <18588496441@163.com>
Sat, 13 Dec 2025 08:04:59 +0000 (16:04 +0800)
committeryu.dongliang <18588496441@163.com>
Sat, 13 Dec 2025 08:04:59 +0000 (16:04 +0800)
core/scf_ast.c
core/scf_ast.h
core/scf_function.h
core/scf_optimizer_auto_gc.c
core/scf_pointer_alias.c
core/scf_scope.c
core/scf_scope.h
core/scf_type_cast.h
parse/scf_operator_handler_semantic.c

index e3fd536811067bc618cb717e3a624320dec3c8ca..d7a827cd9ae2bc74402391c8470440b144aee079 100644 (file)
@@ -1,4 +1,5 @@
 #include"scf_ast.h"
+#include"scf_type_cast.h"
 
 int    scf_ast_open(scf_ast_t** past)
 {
@@ -577,3 +578,65 @@ error:
        scf_string_free(s);
        return -1;
 }
+
+int scf_ast_find_proper_function(scf_function_t** pf, scf_ast_t* ast, scf_vector_t* fvec, scf_vector_t* argv)
+{
+       scf_function_t* f;
+       scf_variable_t* v0;
+       scf_variable_t* v1;
+
+       int i;
+       int j;
+
+       for (i = 0; i < fvec->size; ) {
+               f  =        fvec->data[i];
+
+               f->score = 0;
+
+               for (j = 0; j < argv->size; j++) {
+                       v0 =     f->argv->data[j];
+                       v1 =        argv->data[j];
+
+                       if (scf_variable_is_struct_pointer(v0))
+                               continue;
+
+                       if (scf_type_cast_check(ast, v0, v1) < 0)
+                               break;
+
+                       int type = scf_find_updated_type(ast, v0, v1);
+                       if (type < 0)
+                               break;
+
+                       if (scf_variable_nb_pointers(v0) == scf_variable_nb_pointers(v1)) {
+                               if (v0->type == v1->type)
+                                       f->score += 10000;
+                               else if (type == v0->type) {
+                                       f->score += 1;
+
+                                       if (type == v1->type)
+                                               f->score += 100;
+                               }
+                       }
+               }
+
+               if (j < argv->size)
+                       assert(0 == scf_vector_del(fvec, f)); // drop invalid function
+               else
+                       i++;
+       }
+
+       if (fvec->size <= 0)
+               return -404;
+
+       int max = INT_MIN;
+       for (i = 0; i < fvec->size; i++) {
+               f  =        fvec->data[i];
+
+               if (max < f->score) {
+                       max = f->score;
+                       *pf = f;
+               }
+       }
+
+       return 0;
+}
index dbd9169bb77ea0ed2fd1dc941c39d7b7d1275472..0cc4cc024c93bff5f59b120f100741830b204aeb 100644 (file)
@@ -82,6 +82,7 @@ static inline scf_scope_t* scf_ast_current_scope(scf_ast_t* ast)
        return ast->current_block->scope;
 }
 
+int scf_ast_find_proper_function (scf_function_t** pf, scf_ast_t* ast, scf_vector_t* fvec, scf_vector_t* argv);
 int scf_ast_find_global_function (scf_function_t** pf, scf_ast_t* ast, char* name);
 int scf_ast_find_global_variable (scf_variable_t** pv, scf_ast_t* ast, char* name);
 int scf_ast_find_global_type     (scf_type_t**     pt, scf_ast_t* ast, char* name);
@@ -92,8 +93,6 @@ int scf_ast_find_variable (scf_variable_t** pv, scf_ast_t* ast, char* name);
 int scf_ast_find_type     (scf_type_t**     pt, scf_ast_t* ast, char* name);
 int scf_ast_find_type_type(scf_type_t**     pt, scf_ast_t* ast, int   type);
 
-int scf_operator_function_call(scf_ast_t* ast, scf_function_t* f, const int argc, const scf_variable_t** argv, scf_variable_t** pret, scf_list_t* _3ac_list_head);
-
 int    scf_ast_open(scf_ast_t** past);
 int scf_ast_close(scf_ast_t* ast);
 
index 4ca3b64f9aeff876f28264ad03cb12f04c69f264..08b78180f4fd205bf39eceb8d80277298f3c9831 100644 (file)
@@ -27,6 +27,8 @@ struct scf_function_s {
        scf_list_t        basic_block_list_head;
        int               nb_basic_blocks;
 
+       int               score; // overloaded score
+
        scf_vector_t*     jmps;
 
        scf_list_t        dag_list_head;
index bdc2a9349c2ef6905058e4cca090a8649e6e6bc7..57f2a3ba47362a2539ad28c9a0696ba820caad53 100644 (file)
@@ -873,6 +873,8 @@ ref:
                                if (cur_bb != bb) {
                                        scf_list_del(&c->list);
                                        scf_list_add_tail(&cur_bb->code_list_head, &c->list);
+
+                                       c->basic_block = cur_bb;
                                }
 
                                ret = _auto_gc_bb_ref(ds_obj, ds_malloced, ast, f, &cur_bb);
@@ -894,6 +896,8 @@ end:
                if (cur_bb != bb) {
                        scf_list_del(&c->list);
                        scf_list_add_tail(&cur_bb->code_list_head, &c->list);
+
+                       c->basic_block = cur_bb;
                }
        }
 
index 02af3c6fdaabfee0ab1db5538ec923317955c12d..710d764813d26df0011767628f545db50b7acbe1 100644 (file)
@@ -124,12 +124,18 @@ static int _bb_pointer_initeds(scf_vector_t* initeds, scf_list_t* bb_list_head,
                dn = ds->dag_node;
                v  = dn->var;
 
+               if (ds->dn_indexes)
+                       return 0;
+
                if (scf_variable_const(v) || scf_variable_const_string(v))
                        return 0;
 
                if (v->arg_flag)
                        return 0;
 
+               if (v->tmp_flag)
+                       return 0;
+
                if (v->global_flag) {
                        if (v->nb_pointers > 0)
                                scf_logw("global pointer '%s' is not inited, file: %s, line: %d\n", v->w->text->data, v->w->file->data, v->w->line);
@@ -147,12 +153,6 @@ static int _bb_pointer_initeds(scf_vector_t* initeds, scf_list_t* bb_list_head,
                        return 0;
                }
 
-               if (ds->dn_indexes)
-                       return 0;
-
-               if (v->tmp_flag)
-                       return 0;
-
                if (SCF_OP_ADDRESS_OF != dn->type) {
                        scf_loge("pointer '%s_%d_%d' is not inited, tmp_flag: %d, local_flag: %d, file: %s, line: %d\n",
                                        v->w->text->data, v->w->line, v->w->pos, v->tmp_flag, v->local_flag, v->w->file->data, v->w->line);
index 8790ebe13acbba2b79b48f0bbc14da283f3fc905..7a06609f51d5e76f6a2697abf8af14f6a02d39de 100644 (file)
@@ -178,23 +178,6 @@ scf_function_t*    scf_scope_find_same_function(scf_scope_t* scope, scf_function_t*
        return NULL;
 }
 
-scf_function_t*        scf_scope_find_proper_function(scf_scope_t* scope, const char* name, scf_vector_t* argv)
-{
-       scf_function_t* f;
-       scf_list_t*     l;
-
-       for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) {
-               f  = scf_list_data(l, scf_function_t, list);
-
-               if (strcmp(f->node.w->text->data, name))
-                       continue;
-
-               if (scf_function_same_argv(f->argv, argv))
-                       return f;
-       }
-       return NULL;
-}
-
 int scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, const char* name, scf_vector_t* argv)
 {
        scf_function_t* f;
index 119077df82de68e236928e110b06349d256060d0..53ef96ad5a3902223a572ca87c2c6f6d874f0fb6 100644 (file)
@@ -40,8 +40,6 @@ scf_function_t* scf_scope_find_function(scf_scope_t* scope, const char* name);
 
 scf_function_t*        scf_scope_find_same_function(scf_scope_t* scope, scf_function_t* f0);
 
-scf_function_t*        scf_scope_find_proper_function(scf_scope_t* scope, const char* name, scf_vector_t* argv);
-
 int             scf_scope_find_overloaded_functions(scf_vector_t** pfunctions, scf_scope_t* scope, const int op_type, scf_vector_t* argv);
 
 int             scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, const char* name, scf_vector_t* argv);
index 0ccf5ff05675be6649809bc509bd3f7ae5ce3aeb..6c4a11a0aa4ecd175026d3a2435229f7dbcab7c7 100644 (file)
@@ -34,4 +34,3 @@ int scf_cast_to_float(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src
 int scf_cast_to_double(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src);
 
 #endif
-
index 4cf58677d49a15ddeb2c9254277861b25d9d6943..5655a3b10b02bb942b86ef5e1b0102d053ae87e6 100644 (file)
@@ -284,55 +284,15 @@ static int _semantic_add_call(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes,
        return _semantic_add_call_rets(ast, parent, d, f);
 }
 
-static int _semantic_find_proper_function2(scf_ast_t* ast, scf_vector_t* fvec, scf_vector_t* argv, scf_function_t** pf)
-{
-       scf_function_t* f;
-       scf_variable_t* v0;
-       scf_variable_t* v1;
-
-       int i;
-       int j;
-
-       for (i = 0; i < fvec->size; i++) {
-               f  =        fvec->data[i];
-
-               if (scf_function_same_argv(f->argv, argv)) {
-                       *pf = f;
-                       return 0;
-               }
-       }
-
-       for (i = 0; i < fvec->size; i++) {
-               f  =        fvec->data[i];
-
-               for (j = 0; j < argv->size; j++) {
-
-                       v0 =     f->argv->data[j];
-                       v1 =        argv->data[j];
-
-                       if (scf_variable_is_struct_pointer(v0))
-                               continue;
-
-                       if (scf_type_cast_check(ast, v0, v1) < 0)
-                               break;
-
-                       *pf = f;
-                       return 0;
-               }
-       }
-
-       return -404;
-}
-
 static int _semantic_find_proper_function(scf_ast_t* ast, scf_type_t* t, const char* fname, scf_vector_t* argv, scf_function_t** pf)
 {
        scf_vector_t* fvec = NULL;
 
-       int ret  = scf_scope_find_like_functions(&fvec, t->scope, fname, argv);
+       int ret = scf_scope_find_like_functions(&fvec, t->scope, fname, argv);
        if (ret < 0)
                return ret;
 
-       ret = _semantic_find_proper_function2(ast, fvec, argv, pf);
+       ret = scf_ast_find_proper_function(pf, ast, fvec, argv);
 
        scf_vector_free(fvec);
        return ret;
@@ -407,7 +367,7 @@ static int _semantic_do_overloaded(scf_ast_t* ast, scf_node_t** nodes, int nb_no
                return ret;
        }
 
-       ret = _semantic_find_proper_function2(ast, fvec, argv, &f);
+       ret = scf_ast_find_proper_function(&f, ast, fvec, argv);
        if (ret < 0)
                scf_loge("\n");
        else
@@ -468,7 +428,7 @@ static int _semantic_do_overloaded_assign(scf_ast_t* ast, scf_node_t** nodes, in
                return ret;
        }
 
-       ret = _semantic_find_proper_function2(ast, fvec, argv, &f);
+       ret = scf_ast_find_proper_function(&f, ast, fvec, argv);
        if (ret < 0)
                scf_loge("\n");
        else