#include"scf_ast.h"
+#include"scf_type_cast.h"
int scf_ast_open(scf_ast_t** past)
{
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;
+}
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);
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);
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;
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);
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;
}
}
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);
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);
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;
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);
int scf_cast_to_double(scf_ast_t* ast, scf_variable_t** pret, scf_variable_t* src);
#endif
-
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;
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
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