From b0a164f55045ce8bf3e9a31af6d1d140720f742a Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sat, 13 Dec 2025 16:04:59 +0800 Subject: [PATCH] use scf_ast_find_proper_function() instead of _semantic_find_proper_function2(), delete some unused code --- core/scf_ast.c | 63 +++++++++++++++++++++++++++ core/scf_ast.h | 3 +- core/scf_function.h | 2 + core/scf_optimizer_auto_gc.c | 4 ++ core/scf_pointer_alias.c | 12 ++--- core/scf_scope.c | 17 -------- core/scf_scope.h | 2 - core/scf_type_cast.h | 1 - parse/scf_operator_handler_semantic.c | 48 ++------------------ 9 files changed, 80 insertions(+), 72 deletions(-) diff --git a/core/scf_ast.c b/core/scf_ast.c index e3fd536..d7a827c 100644 --- a/core/scf_ast.c +++ b/core/scf_ast.c @@ -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; +} diff --git a/core/scf_ast.h b/core/scf_ast.h index dbd9169..0cc4cc0 100644 --- a/core/scf_ast.h +++ b/core/scf_ast.h @@ -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); diff --git a/core/scf_function.h b/core/scf_function.h index 4ca3b64..08b7818 100644 --- a/core/scf_function.h +++ b/core/scf_function.h @@ -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; diff --git a/core/scf_optimizer_auto_gc.c b/core/scf_optimizer_auto_gc.c index bdc2a93..57f2a3b 100644 --- a/core/scf_optimizer_auto_gc.c +++ b/core/scf_optimizer_auto_gc.c @@ -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; } } diff --git a/core/scf_pointer_alias.c b/core/scf_pointer_alias.c index 02af3c6..710d764 100644 --- a/core/scf_pointer_alias.c +++ b/core/scf_pointer_alias.c @@ -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); diff --git a/core/scf_scope.c b/core/scf_scope.c index 8790ebe..7a06609 100644 --- a/core/scf_scope.c +++ b/core/scf_scope.c @@ -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; diff --git a/core/scf_scope.h b/core/scf_scope.h index 119077d..53ef96a 100644 --- a/core/scf_scope.h +++ b/core/scf_scope.h @@ -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); diff --git a/core/scf_type_cast.h b/core/scf_type_cast.h index 0ccf5ff..6c4a11a 100644 --- a/core/scf_type_cast.h +++ b/core/scf_type_cast.h @@ -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 - diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index 4cf5867..5655a3b 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -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 -- 2.25.1