From d7709a572b5a7a09be2604400ef32c78df0774ae Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Tue, 22 Sep 2026 23:33:32 +0800 Subject: [PATCH] self compile lex module test '../sysroot/include/stdio.h' ok. --- core/scf_function.c | 11 +++++++++++ core/scf_function.h | 3 +-- core/scf_operator_handler_3ac.c | 9 +++++++-- native/x64/scf_x64.c | 9 ++++++++- native/x64/scf_x64_inst_div.c | 9 +++++++++ native/x64/scf_x64_reg.c | 35 +++++++++++++++++++++++---------- native/x64/scf_x64_reg.h | 2 ++ 7 files changed, 63 insertions(+), 15 deletions(-) diff --git a/core/scf_function.c b/core/scf_function.c index ad2d786..6621a8e 100644 --- a/core/scf_function.c +++ b/core/scf_function.c @@ -47,6 +47,10 @@ scf_function_t* scf_function_alloc(scf_lex_word_t* w) if (!f->jmps) goto _jmps_error; + f->tmp_vars = scf_vector_alloc(); + if (!f->tmp_vars) + goto _tmp_vars_error; + f->bb_loops = scf_vector_alloc(); if (!f->bb_loops) goto _loop_error; @@ -72,6 +76,8 @@ _text_rela_error: _group_error: scf_vector_free(f->bb_loops); _loop_error: + scf_vector_free(f->tmp_vars); +_tmp_vars_error: scf_vector_free(f->jmps); _jmps_error: scf_vector_free(f->caller_functions); @@ -123,6 +129,11 @@ void scf_function_free(scf_function_t* f) f->jmps = NULL; } + if (f->tmp_vars) { + scf_vector_free(f->tmp_vars); + f->tmp_vars = NULL; + } + if (f->text_relas) { scf_vector_free(f->text_relas); f->text_relas = NULL; diff --git a/core/scf_function.h b/core/scf_function.h index 8b15d42..c1d75e1 100644 --- a/core/scf_function.h +++ b/core/scf_function.h @@ -10,8 +10,6 @@ struct scf_function_s { scf_string_t* signature; - scf_vector_t* typedef_vars; - scf_list_t list; // for scope scf_list_t hash; // for global search @@ -33,6 +31,7 @@ struct scf_function_s { int score; // overloaded score scf_vector_t* jmps; + scf_vector_t* tmp_vars; scf_list_t dag_list_head; diff --git a/core/scf_operator_handler_3ac.c b/core/scf_operator_handler_3ac.c index b21f159..041ead9 100644 --- a/core/scf_operator_handler_3ac.c +++ b/core/scf_operator_handler_3ac.c @@ -1259,7 +1259,10 @@ static int _scf_op_switch(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void jnext = scf_3ac_jmp_code(SCF_OP_GOTO, NULL); scf_list_add_tail(d->_3ac_list_head, &jnext->list); - scf_vector_add(up_branch_ops->_breaks, jnext); + + int ret = scf_vector_add(d->branch_ops->_breaks, jnext); + if (ret < 0) + return ret; dst = jnot->dsts->data[0]; dst->code = jnext; @@ -1296,7 +1299,9 @@ static int _scf_op_switch(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void scf_list_add_tail(d->_3ac_list_head, &cmp->list); scf_list_add_tail(d->_3ac_list_head, &jnot->list); - scf_vector_add(up_branch_ops->_breaks, jnot); + int ret = scf_vector_add(d->branch_ops->_breaks, jnot); + if (ret < 0) + return ret; } if (jnext) { diff --git a/native/x64/scf_x64.c b/native/x64/scf_x64.c index bd2d086..5b17a98 100644 --- a/native/x64/scf_x64.c +++ b/native/x64/scf_x64.c @@ -1042,7 +1042,7 @@ int _scf_x64_select_inst(scf_native_t* ctx) if (ret < 0) return ret; bb->native_flag = 1; - scf_logd("************ bb: %d\n", bb->index); + scf_logd("************ bb: %d\n\n", bb->index); ret = x64_save_bb_colors(bb->dn_colors_exit, bbg, bb); if (ret < 0) @@ -1159,6 +1159,13 @@ int scf_x64_select_inst(scf_native_t* ctx, scf_function_t* f) _x64_set_offset_for_relas(ctx, f, f->text_relas); _x64_set_offset_for_relas(ctx, f, f->data_relas); + + for (i = 0; i < f->tmp_vars->size; i++) { + v = f->tmp_vars->data[i]; + v->bp_offset = 0; + } + + scf_vector_clear(f->tmp_vars, NULL); return 0; } diff --git a/native/x64/scf_x64_inst_div.c b/native/x64/scf_x64_inst_div.c index 3134f21..8443988 100644 --- a/native/x64/scf_x64_inst_div.c +++ b/native/x64/scf_x64_inst_div.c @@ -118,6 +118,15 @@ int x64_inst_int_div(scf_dag_node_t* dst, scf_dag_node_t* src0, scf_dag_node_t* } else { scf_rela_t* rela = NULL; + if (0 == dst->var->bp_offset + && !dst->var->global_flag + && !dst->var->local_flag) { + + int ret = x64_tmp_bp_offset(dst->var, f); + if (ret < 0) + return ret; + } + mov = x64_find_OpCode(SCF_X64_MOV, dst->var->size, dst->var->size, SCF_X64_G2E); inst = x64_make_inst_G2M(&rela, mov, dst->var, NULL, result, c, f); X64_INST_ADD_CHECK(c, inst, rela); diff --git a/native/x64/scf_x64_reg.c b/native/x64/scf_x64_reg.c index f050267..145507f 100644 --- a/native/x64/scf_x64_reg.c +++ b/native/x64/scf_x64_reg.c @@ -350,6 +350,28 @@ scf_vector_t* x64_register_colors() return colors; } +int x64_tmp_bp_offset(scf_variable_t* v, scf_function_t* f) +{ + if (f->inline_flag) { + assert(!scf_vector_find(f->tmp_vars, v)); + + int ret = scf_vector_add(f->tmp_vars, v); + if (ret < 0) + return ret; + } + + int size = f->local_vars_size + x64_variable_size(v); + + if (size & 0x7) + size = (size + 7) & ~0x7; + + v->bp_offset = -size; + v->tmp_flag = 1; + + f->local_vars_size = size; + return 0; +} + int x64_save_var2(scf_dag_node_t* dn, scf_register_t* r, scf_3ac_code_t* c, scf_function_t* f) { scf_variable_t* v = dn->var; @@ -375,16 +397,9 @@ int x64_save_var2(scf_dag_node_t* dn, scf_register_t* r, scf_3ac_code_t* c, scf_ // if temp var in register, alloc it in stack if (0 == v->bp_offset && !v->global_flag && !v->local_flag) { - int local_vars_size = f->local_vars_size; - local_vars_size += var_size; - - if (local_vars_size & 0x7) - local_vars_size = (local_vars_size + 7) >> 3 << 3; - - v->bp_offset = -local_vars_size; - v->tmp_flag = 1; - - f->local_vars_size = local_vars_size; + int ret = x64_tmp_bp_offset(v, f); + if (ret < 0) + return ret; #if 0 scf_logw("r: %s, temp var, ", r->name); if (v->w) diff --git a/native/x64/scf_x64_reg.h b/native/x64/scf_x64_reg.h index 346a98a..c735c31 100644 --- a/native/x64/scf_x64_reg.h +++ b/native/x64/scf_x64_reg.h @@ -105,6 +105,8 @@ scf_register_t* x64_select_overflowed_reg(scf_dag_node_t* dn, scf_3ac_code_t int x64_reg_cached_vars(scf_register_t* r); +int x64_tmp_bp_offset(scf_variable_t* v, scf_function_t* f); + int x64_save_var(scf_dag_node_t* dn, scf_3ac_code_t* c, scf_function_t* f); int x64_save_var2(scf_dag_node_t* dn, scf_register_t* r, scf_3ac_code_t* c, scf_function_t* f); -- 2.25.1