self compile lex module test '../sysroot/include/stdio.h' ok.
authoryu.dongliang <18588496441@163.com>
Tue, 22 Sep 2026 15:33:32 +0000 (23:33 +0800)
committeryu.dongliang <18588496441@163.com>
Tue, 22 Sep 2026 15:33:32 +0000 (23:33 +0800)
core/scf_function.c
core/scf_function.h
core/scf_operator_handler_3ac.c
native/x64/scf_x64.c
native/x64/scf_x64_inst_div.c
native/x64/scf_x64_reg.c
native/x64/scf_x64_reg.h

index ad2d7865ecfd730e19638b7a94b5e50b3badae55..6621a8ed0534b41f5e68b10887b6e035b6315c56 100644 (file)
@@ -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;
index 8b15d428ef67377779c7d317feba4c8a10888ea7..c1d75e1fbf27f8411edcb546b1c0f991f67707c2 100644 (file)
@@ -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;
 
index b21f15965d516d71b1a631b659afaba61a822575..041ead98c66107e6a2fef7f415080006fb815240 100644 (file)
@@ -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) {
index bd2d0861a5d11cc25ba47a4b47a3dd660d6d7bb6..5b17a9879b92db3cfdc63bb425649af5b34d3a31 100644 (file)
@@ -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;
 }
 
index 3134f210c0560c345b6c9a1ba6e12e2ecf34c35e..84439881889f3c9ee5e8cab75db2754690fad5b5 100644 (file)
@@ -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);
index f050267c17d747db5728deb7d5168e792965dc23..145507f25739631c79dd5250e303d3ad4c148763 100644 (file)
@@ -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)
index 346a98a571e53410cf5101e79de9fee0fa625e5e..c735c31716c11691c639ef46bacbeec77e4f5640 100644 (file)
@@ -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);