From 417edcad567ba06d36552de8f47939b0d4f02b8c Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Mon, 18 Dec 2023 20:12:56 +0800 Subject: [PATCH] add some free() code, delete some unused code --- elf/scf_elf.c | 12 ++++++-- elf/scf_elf_link.c | 43 ++++++++++++++++++++------- elf/scf_elf_x64_so.c | 2 +- native/x64/scf_x64.h | 6 ++-- native/x64/scf_x64_peephole.c | 11 ++++++- parse/scf_operator_handler_const.c | 12 ++++---- parse/scf_operator_handler_const.h | 2 +- parse/scf_operator_handler_semantic.c | 16 +++++----- parse/scf_operator_handler_semantic.h | 2 +- 9 files changed, 72 insertions(+), 34 deletions(-) diff --git a/elf/scf_elf.c b/elf/scf_elf.c index 1764f53..c1ba4c7 100644 --- a/elf/scf_elf.c +++ b/elf/scf_elf.c @@ -54,10 +54,13 @@ int scf_elf_open2(scf_elf_context_t* elf, const char* machine) int scf_elf_open(scf_elf_context_t** pelf, const char* machine, const char* path, const char* mode) { - scf_elf_context_t* elf = calloc(1, sizeof(scf_elf_context_t)); - assert(elf); - + scf_elf_context_t* elf; int i; + + elf = calloc(1, sizeof(scf_elf_context_t)); + if (!elf) + return -ENOMEM; + for (i = 0; elf_ops_array[i]; i++) { if (!strcmp(elf_ops_array[i]->machine, machine)) { elf->ops = elf_ops_array[i]; @@ -67,12 +70,14 @@ int scf_elf_open(scf_elf_context_t** pelf, const char* machine, const char* path if (!elf->ops) { scf_loge("\n"); + free(elf); return -1; } elf->fp = fopen(path, mode); if (!elf->fp) { scf_loge("\n"); + free(elf); return -1; } @@ -83,6 +88,7 @@ int scf_elf_open(scf_elf_context_t** pelf, const char* machine, const char* path scf_loge("\n"); + fclose(elf->fp); free(elf); elf = NULL; return -1; diff --git a/elf/scf_elf_link.c b/elf/scf_elf_link.c index c1de02e..fefaf77 100644 --- a/elf/scf_elf_link.c +++ b/elf/scf_elf_link.c @@ -1,5 +1,14 @@ #include"scf_elf_link.h" +void scf_ar_sym_free(scf_ar_sym_t* sym) +{ + if (sym) { + if (sym->name) + scf_string_free(sym->name); + free(sym); + } +} + int __scf_elf_file_open(scf_elf_file_t** pfile) { scf_elf_file_t* ef = calloc(1, sizeof(scf_elf_file_t)); @@ -336,8 +345,10 @@ static int ar_symbols(scf_ar_file_t* ar) return -ENOMEM; ret = fread(buf, ar_size, 1, ar->fp); - if (ret != 1) - return -1; + if (ret != 1) { + ret = -1; + goto error; + } for (i = 0; i < sizeof(nb_syms); i++) { nb_syms <<= 8; @@ -356,12 +367,15 @@ static int ar_symbols(scf_ar_file_t* ar) } sym = calloc(1, sizeof(scf_ar_sym_t)); - if (!sym) - return -ENOMEM; + if (!sym) { + ret = -ENOMEM; + goto error; + } if (scf_vector_add(ar->symbols, sym) < 0) { free(sym); - return -ENOMEM; + ret = -ENOMEM; + goto error; } sym->offset = offset; @@ -381,14 +395,21 @@ static int ar_symbols(scf_ar_file_t* ar) k++; sym->name = scf_string_cstr_len(buf + j, k); - if (!sym->name) - return -ENOMEM; + if (!sym->name) { + ret = -ENOMEM; + goto error; + } j += k + 1; } assert(j == ar_size); return 0; + +error: + scf_vector_clear(ar->symbols, ( void (*)(void*) )scf_ar_sym_free); + free(buf); + return ret; } int scf_ar_file_open(scf_ar_file_t** par, const char* path) @@ -414,7 +435,7 @@ int scf_ar_file_open(scf_ar_file_t** par, const char* path) ar->fp = fopen(path, "rb"); if (!ar->fp) { ret = -1; - goto error; + goto open_error; } ret = ar_symbols(ar); @@ -425,9 +446,11 @@ int scf_ar_file_open(scf_ar_file_t** par, const char* path) return 0; error: - free(ar->files); + fclose(ar->fp); +open_error: + scf_vector_free(ar->files); file_error: - free(ar->symbols); + scf_vector_free(ar->symbols); sym_error: free(ar); return ret; diff --git a/elf/scf_elf_x64_so.c b/elf/scf_elf_x64_so.c index ab4f21d..8fa0ec8 100644 --- a/elf/scf_elf_x64_so.c +++ b/elf/scf_elf_x64_so.c @@ -430,7 +430,7 @@ int __x64_elf_add_dyn (elf_native_t* x64) { elf_section_t* s; elf_sym_t* sym; - Elf64_Rela* rela; + Elf64_Rela* rela; int i; for (i = x64->symbols->size - 1; i >= 0; i--) { diff --git a/native/x64/scf_x64.h b/native/x64/scf_x64.h index 6d515bf..09d7530 100644 --- a/native/x64/scf_x64.h +++ b/native/x64/scf_x64.h @@ -50,11 +50,11 @@ typedef struct { } scf_x64_context_t; typedef struct { - scf_dag_node_t* dag_node; + scf_dag_node_t* dag_node; - scf_register_t* reg; + scf_register_t* reg; - scf_x64_OpCode_t* OpCode; + scf_x64_OpCode_t* OpCode; } x64_rcg_node_t; diff --git a/native/x64/scf_x64_peephole.c b/native/x64/scf_x64_peephole.c index 74f9dbf..82b9b4b 100644 --- a/native/x64/scf_x64_peephole.c +++ b/native/x64/scf_x64_peephole.c @@ -290,12 +290,14 @@ static void _x64_peephole_function(scf_vector_t* tmp_insts, scf_function_t* f, i int i; int j; + int k; for (i = tmp_insts->size - 1; i >= 0; i--) { inst = tmp_insts->data[i]; scf_register_t* r0; scf_register_t* r1; + scf_register_t* r2; if (SCF_X64_MOV != inst->OpCode->type) continue; @@ -304,7 +306,14 @@ static void _x64_peephole_function(scf_vector_t* tmp_insts, scf_function_t* f, i r0 = inst->dst.base; - if (X64_COLOR_CONFLICT(rax->color, r0->color)) + for (k = 0; k < X64_ABI_RET_NB; k++) { + r2 = x64_find_register_type_id_bytes(0, x64_abi_ret_regs[k], 8); + + if (X64_COLOR_CONFLICT(r2->color, r0->color)) + break; + } + + if (k < X64_ABI_RET_NB) continue; } else if (!x64_inst_data_is_local(&inst->dst)) diff --git a/parse/scf_operator_handler_const.c b/parse/scf_operator_handler_const.c index c863297..d69fb85 100644 --- a/parse/scf_operator_handler_const.c +++ b/parse/scf_operator_handler_const.c @@ -56,7 +56,7 @@ static int _scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* } } - scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type); if (!h) { scf_loge("\n"); goto _error; @@ -75,7 +75,7 @@ static int _scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* } } - scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_const_operator_handler(node->op->type); if (!h) { scf_loge("\n"); goto _error; @@ -196,7 +196,7 @@ static int _scf_op_const_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, } } - scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_const_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -294,7 +294,7 @@ static int _scf_op_const_node(scf_ast_t* ast, scf_node_t* node, scf_handler_data } } - scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_const_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -342,7 +342,7 @@ static int _scf_op_const_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, vo } } - scf_operator_handler_t* h = scf_find_const_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_const_operator_handler(op->type); if (!h) return -1; @@ -1025,7 +1025,7 @@ scf_operator_handler_t const_operator_handlers[] = { {{NULL, NULL}, SCF_OP_FOR, _scf_op_const_for}, }; -scf_operator_handler_t* scf_find_const_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type) +scf_operator_handler_t* scf_find_const_operator_handler(const int type) { int i; for (i = 0; i < sizeof(const_operator_handlers) / sizeof(const_operator_handlers[0]); i++) { diff --git a/parse/scf_operator_handler_const.h b/parse/scf_operator_handler_const.h index 18d44b8..083bb95 100644 --- a/parse/scf_operator_handler_const.h +++ b/parse/scf_operator_handler_const.h @@ -3,7 +3,7 @@ #include"scf_operator_handler.h" -scf_operator_handler_t* scf_find_const_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type); +scf_operator_handler_t* scf_find_const_operator_handler(const int type); int scf_function_const_opt(scf_ast_t* ast, scf_function_t* f); diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index 4beba45..3b25d01 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -578,7 +578,7 @@ static int _scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* } } - h = scf_find_semantic_operator_handler(node->op->type, -1, -1, -1); + h = scf_find_semantic_operator_handler(node->op->type); if (!h) { scf_loge("\n"); goto _error; @@ -602,7 +602,7 @@ static int _scf_expr_calculate_internal(scf_ast_t* ast, scf_node_t* node, void* } } - h = scf_find_semantic_operator_handler(node->op->type, -1, -1, -1); + h = scf_find_semantic_operator_handler(node->op->type); if (!h) { scf_loge("\n"); goto _error; @@ -1026,7 +1026,7 @@ static int _scf_op_semantic_block(scf_ast_t* ast, scf_node_t** nodes, int nb_nod } } - scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type); if (!h) { scf_loge("op->type: %d, va: %d, %d, %d\n", op->type, SCF_OP_VA_START, SCF_OP_VA_ARG, SCF_OP_VA_END); return -1; @@ -1246,7 +1246,7 @@ static int _scf_op_semantic_node(scf_ast_t* ast, scf_node_t* node, scf_handler_d } } - scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -1309,7 +1309,7 @@ static int _scf_op_semantic_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, } } - scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -1352,7 +1352,7 @@ static int _scf_op_semantic_repeat(scf_ast_t* ast, scf_node_t** nodes, int nb_no scf_variable_t** pret = d->pret; - scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -1418,7 +1418,7 @@ static int _scf_op_semantic_while(scf_ast_t* ast, scf_node_t** nodes, int nb_nod } } - scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type, -1, -1, -1); + scf_operator_handler_t* h = scf_find_semantic_operator_handler(op->type); if (!h) { scf_loge("\n"); return -1; @@ -2935,7 +2935,7 @@ scf_operator_handler_t semantic_operator_handlers[] = { {{NULL, NULL}, SCF_OP_FOR, _scf_op_semantic_for}, }; -scf_operator_handler_t* scf_find_semantic_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type) +scf_operator_handler_t* scf_find_semantic_operator_handler(const int type) { int i; for (i = 0; i < sizeof(semantic_operator_handlers) / sizeof(semantic_operator_handlers[0]); i++) { diff --git a/parse/scf_operator_handler_semantic.h b/parse/scf_operator_handler_semantic.h index 97d9da1..d35e0a8 100644 --- a/parse/scf_operator_handler_semantic.h +++ b/parse/scf_operator_handler_semantic.h @@ -3,7 +3,7 @@ #include"scf_operator_handler.h" -scf_operator_handler_t* scf_find_semantic_operator_handler(const int type, const int src0_type, const int src1_type, const int ret_type); +scf_operator_handler_t* scf_find_semantic_operator_handler(const int type); int scf_function_semantic_analysis(scf_ast_t* ast, scf_function_t* f); -- 2.25.1