split scf_parse_compile() to scf_parse_compile() and scf_parse_to_obj(), fix x64...
authoryu.dongliang <18588496441@163.com>
Sun, 27 Jul 2025 16:23:46 +0000 (00:23 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 27 Jul 2025 16:23:46 +0000 (00:23 +0800)
12 files changed:
core/scf_function.h
core/scf_optimizer_auto_gc_find.c
core/scf_optimizer_inline.c
elf/scf_elf_arm32.c
elf/scf_elf_arm64.c
elf/scf_elf_naja.c
elf/scf_elf_x64.c
native/x64/scf_x64_inst_util.c
native/x64/scf_x64_peephole.c
parse/main.c
parse/scf_parse.c
parse/scf_parse.h

index be10c7e618f73d657b691e5fd0ddd37b42b02390..9765f2a410285fa13bcbdbc5145222bc3a8d47c8 100644 (file)
@@ -61,6 +61,9 @@ struct scf_function_s {
        uint32_t          void_flag :1;
        uint32_t          call_flag :1;
        uint32_t          vla_flag  :1;
+
+       uint32_t          compile_flag:1;
+       uint32_t          native_flag :1;
 };
 
 scf_function_t*        scf_function_alloc(scf_lex_word_t* w);
index c006e31adac7e4657dcd21e267847a9674da7db9..ae9b6f39f4c7c2ae8f0fc7fb7d8f48ea5ab8328e 100644 (file)
@@ -868,9 +868,12 @@ static int _auto_gc_function_find(scf_ast_t* ast, scf_function_t* f, scf_list_t*
 
 static int _optimize_auto_gc_find(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
 {
-       if (!ast || !functions || functions->size <= 0)
+       if (!ast || !functions)
                return -EINVAL;
 
+       if (functions->size <= 0)
+               return 0;
+
        scf_vector_t* fqueue = scf_vector_alloc();
        if (!fqueue)
                return -ENOMEM;
index 5a433ea70a5b204ca16c4865595b1e7b7373d34b..233fe196ef24cc911f27bfd67aa950faff231751 100644 (file)
@@ -449,7 +449,7 @@ static int _optimize_inline2(scf_ast_t* ast, scf_function_t* f)
 
 static int _optimize_inline(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
 {
-       if (!ast || !functions || functions->size <= 0)
+       if (!ast || !functions)
                return -EINVAL;
 
        int i;
index 1c1b26082f755086cacdcd51981b668edad9a6ba..6c85c87e0117d71bca3356d3546eab9f269da0e1 100644 (file)
@@ -240,7 +240,7 @@ static int _arm32_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 
                if (!strcmp(".text", s->name->data)) {
 
-                       assert(s->data_len > 0);
+                       assert(s->data_len >= 0);
                        assert(!cs);
                        cs = s;
 
index 55bcedb7838de03475e0642dd54a8b7b7d57bcb4..318ee2eae25ff71e633c3347d8fd33e27d3fc5c2 100644 (file)
@@ -251,7 +251,7 @@ static int _arm64_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 
                if (!strcmp(".text", s->name->data)) {
 
-                       assert(s->data_len > 0);
+                       assert(s->data_len >= 0);
                        assert(!cs);
                        cs = s;
 
index 0550fc38fce131b1f84a88762d8d2b7f1090a3bf..a0ccf87d39dc9b6aed3d4bf349e207b9a237927b 100644 (file)
@@ -251,7 +251,7 @@ static int _naja_elf_write_exec(scf_elf_context_t* elf, const char* sysroot)
 
                if (!strcmp(".text", s->name->data)) {
 
-                       assert(s->data_len > 0);
+                       assert(s->data_len >= 0);
                        assert(!cs);
                        cs = s;
 
index 10abfd89dabf7303466da170a85b93bfad828085..4d28faca896903d666e75b7d217870e177132495 100644 (file)
@@ -301,7 +301,7 @@ static int _x64_elf_process_sections(elf_native_t* x64, uint64_t* section_offset
 
                if (!strcmp(".text", s->name->data)) {
 
-                       assert(s->data_len > 0);
+                       assert(s->data_len >= 0);
                        assert(!*cs);
                        *cs = s;
 
index 9b197e716b9da5d754051b3554a0cdafc8ace0e0..6074843077de9cebfa0c64ac054e2f90d6ac44f2 100644 (file)
@@ -559,11 +559,22 @@ scf_instruction_t* x64_make_inst_P2G(scf_x64_OpCode_t* OpCode, scf_register_t* r
                return NULL;
        }
 
-       scf_instruction_t* inst = _x64_make_OpCode(OpCode, r_dst->bytes, r_dst, r_base, NULL);
+       scf_instruction_t* inst = NULL;
+
+       uint32_t base;
+
+       if (r_base) {
+               base = r_base->id;
+               inst = _x64_make_OpCode(OpCode, r_dst->bytes, r_dst, r_base, NULL);
+       } else {
+               base = -1;
+               inst = _x64_make_OpCode(OpCode, r_dst->bytes, r_dst, NULL, NULL);
+       }
+
        if (!inst)
                return NULL;
 
-       if (_x64_make_disp(NULL, inst, r_dst->id, r_base->id, offset) < 0) {
+       if (_x64_make_disp(NULL, inst, r_dst->id, base, offset) < 0) {
                free(inst);
                return NULL;
        }
index 976e29fbc41fbbdc5ea15b173bc0c5ca50bf3c56..7a1d856eda16954040dc619cce07384e0669174e 100644 (file)
@@ -27,7 +27,8 @@ static int _x64_peephole_mov(scf_vector_t* std_insts, scf_instruction_t* inst)
                if (SCF_X64_LEA == std->OpCode->type) {
 
                        if (scf_inst_data_same(&std->dst, &inst->src)
-                                       && x64_inst_data_is_reg(&inst->dst)) {
+                                       && x64_inst_data_is_reg(&inst->dst)
+                                       && x64_inst_data_is_local(&std->src)) {
 
                                if (std->src.index)
                                        inst2 = x64_make_inst_SIB2G((scf_x64_OpCode_t*)std->OpCode,
index 5b7492ddc0cf1ffe639e013d40d56ccbfc4b4340..c4c532488793008c930d652e65d70bde8e78d3ef 100644 (file)
@@ -232,7 +232,12 @@ int main(int argc, char* argv[])
                        exec = out;
        }
 
-       if (scf_parse_compile(parse, obj, arch, _3ac) < 0) {
+       if (scf_parse_compile(parse, arch, _3ac) < 0) {
+               scf_loge("\n");
+               return -1;
+       }
+
+       if (scf_parse_to_obj(parse, obj, arch) < 0) {
                scf_loge("\n");
                return -1;
        }
index 66e2993b7ec8726525aecd5295788fa94d5f6fa4..966557a7b92d0e5e6510f4f9f63a344dcbce6f59 100644 (file)
@@ -309,7 +309,6 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla
        int ret;
 
        if (nb_pointers > 0) {
-
                d = scf_vector_find_cmp(parse->debug->abbrevs, (void*)DW_TAG_pointer_type, _debug_abbrev_find_by_tag);
                if (!d) {
                        ret = scf_dwarf_abbrev_add_pointer_type(parse->debug->abbrevs);
@@ -352,6 +351,7 @@ static int __debug_add_type(scf_dwarf_info_entry_t** pie, scf_dwarf_abbrev_decla
                        }
 
                        d = parse->debug->abbrevs->data[parse->debug->abbrevs->size - 1];
+                       d->has_children = t->scope->vars->size > 0;
                }
 
                types = parse->debug->struct_types;
@@ -1873,6 +1873,10 @@ int scf_parse_compile_functions(scf_parse_t* parse, scf_vector_t* functions)
                if (!f->node.define_flag)
                        continue;
 
+               if (f->compile_flag)
+                       continue;
+               f->compile_flag = 1;
+
                int ret = scf_function_semantic_analysis(parse->ast, f);
                if (ret < 0)
                        return ret;
@@ -2117,6 +2121,10 @@ int scf_parse_native_functions(scf_parse_t* parse, scf_vector_t* functions, cons
                if (!f->node.define_flag)
                        continue;
 
+               if (f->native_flag)
+                       continue;
+               f->native_flag = 1;
+
                ret = scf_native_select_inst(native, f);
                if (ret < 0) {
                        scf_loge("\n");
@@ -2356,40 +2364,50 @@ int scf_parse_fill_code(scf_parse_t* parse, scf_vector_t* functions, scf_vector_
        return 0;
 }
 
-int scf_parse_compile(scf_parse_t* parse, const char* out, const char* arch, int _3ac)
+int scf_parse_compile(scf_parse_t* parse, const char* arch, int _3ac)
 {
        scf_block_t* b = parse->ast->root_block;
        if (!b)
                return -EINVAL;
 
-       int ret = 0;
-
-       scf_vector_t*  functions   = NULL;
-       scf_vector_t*  global_vars = NULL;
-       scf_string_t*  code        = NULL;
-
-       functions = scf_vector_alloc();
+       scf_vector_t* functions = scf_vector_alloc();
        if (!functions)
                return -ENOMEM;
 
-       ret = scf_node_search_bfs((scf_node_t*)b, NULL, functions, -1, _find_function);
-       if (ret < 0) {
-               scf_vector_free(functions);
-               return ret;
-       }
+       int ret = scf_node_search_bfs((scf_node_t*)b, NULL, functions, -1, _find_function);
+       if (ret < 0)
+               goto error;
 
        scf_logi("all functions: %d\n", functions->size);
 
        ret = scf_parse_compile_functions(parse, functions);
-       if (ret < 0) {
-               scf_vector_free(functions);
-               return ret;
-       }
+       if (ret < 0)
+               goto error;
 
        if (_3ac)
-               return 0;
+               goto error;
 
        ret = scf_parse_native_functions(parse, functions, arch);
+error:
+       scf_vector_free(functions);
+       return ret;
+}
+
+int scf_parse_to_obj(scf_parse_t* parse, const char* out, const char* arch)
+{
+       scf_block_t* b = parse->ast->root_block;
+       if (!b)
+               return -EINVAL;
+
+       scf_vector_t*  functions   = NULL;
+       scf_vector_t*  global_vars = NULL;
+       scf_string_t*  code        = NULL;
+
+       functions = scf_vector_alloc();
+       if (!functions)
+               return -ENOMEM;
+
+       int ret = scf_node_search_bfs((scf_node_t*)b, NULL, functions, -1, _find_function);
        if (ret < 0) {
                scf_vector_free(functions);
                return ret;
index 7f457380d6c2675044bc421fb645c11775bb98ff..216b91513cfed7508d0556dbc341c5753d000d8d 100644 (file)
@@ -123,7 +123,8 @@ int scf_parse_close(scf_parse_t*   parse);
 
 int scf_parse_file(scf_parse_t* parse, const char* path);
 
-int scf_parse_compile(scf_parse_t* parse, const char* out, const char* arch, int _3ac);
+int scf_parse_compile(scf_parse_t* parse, const char* arch, int _3ac);
+int scf_parse_to_obj (scf_parse_t* parse, const char* out, const char* arch);
 
 int _find_global_var(scf_node_t* node, void* arg, scf_vector_t* vec);
 int _find_function  (scf_node_t* node, void* arg, scf_vector_t* vec);