From: yu.dongliang <18588496441@163.com> Date: Sun, 27 Jul 2025 16:23:46 +0000 (+0800) Subject: split scf_parse_compile() to scf_parse_compile() and scf_parse_to_obj(), fix x64... X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=4c03064fbae3cee25700bd572a0b2041ac922d81;p=scf.git split scf_parse_compile() to scf_parse_compile() and scf_parse_to_obj(), fix x64 peephole error for global var --- diff --git a/core/scf_function.h b/core/scf_function.h index be10c7e..9765f2a 100644 --- a/core/scf_function.h +++ b/core/scf_function.h @@ -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); diff --git a/core/scf_optimizer_auto_gc_find.c b/core/scf_optimizer_auto_gc_find.c index c006e31..ae9b6f3 100644 --- a/core/scf_optimizer_auto_gc_find.c +++ b/core/scf_optimizer_auto_gc_find.c @@ -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; diff --git a/core/scf_optimizer_inline.c b/core/scf_optimizer_inline.c index 5a433ea..233fe19 100644 --- a/core/scf_optimizer_inline.c +++ b/core/scf_optimizer_inline.c @@ -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; diff --git a/elf/scf_elf_arm32.c b/elf/scf_elf_arm32.c index 1c1b260..6c85c87 100644 --- a/elf/scf_elf_arm32.c +++ b/elf/scf_elf_arm32.c @@ -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; diff --git a/elf/scf_elf_arm64.c b/elf/scf_elf_arm64.c index 55bcedb..318ee2e 100644 --- a/elf/scf_elf_arm64.c +++ b/elf/scf_elf_arm64.c @@ -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; diff --git a/elf/scf_elf_naja.c b/elf/scf_elf_naja.c index 0550fc3..a0ccf87 100644 --- a/elf/scf_elf_naja.c +++ b/elf/scf_elf_naja.c @@ -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; diff --git a/elf/scf_elf_x64.c b/elf/scf_elf_x64.c index 10abfd8..4d28fac 100644 --- a/elf/scf_elf_x64.c +++ b/elf/scf_elf_x64.c @@ -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; diff --git a/native/x64/scf_x64_inst_util.c b/native/x64/scf_x64_inst_util.c index 9b197e7..6074843 100644 --- a/native/x64/scf_x64_inst_util.c +++ b/native/x64/scf_x64_inst_util.c @@ -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; } diff --git a/native/x64/scf_x64_peephole.c b/native/x64/scf_x64_peephole.c index 976e29f..7a1d856 100644 --- a/native/x64/scf_x64_peephole.c +++ b/native/x64/scf_x64_peephole.c @@ -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, diff --git a/parse/main.c b/parse/main.c index 5b7492d..c4c5324 100644 --- a/parse/main.c +++ b/parse/main.c @@ -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; } diff --git a/parse/scf_parse.c b/parse/scf_parse.c index 66e2993..966557a 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -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; diff --git a/parse/scf_parse.h b/parse/scf_parse.h index 7f45738..216b915 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -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);