add some free() code, delete some unused code
authoryu.dongliang <18588496441@163.com>
Mon, 18 Dec 2023 12:12:56 +0000 (20:12 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 18 Dec 2023 12:12:56 +0000 (20:12 +0800)
elf/scf_elf.c
elf/scf_elf_link.c
elf/scf_elf_x64_so.c
native/x64/scf_x64.h
native/x64/scf_x64_peephole.c
parse/scf_operator_handler_const.c
parse/scf_operator_handler_const.h
parse/scf_operator_handler_semantic.c
parse/scf_operator_handler_semantic.h

index 1764f53763ca99b8bc713ff41bab93d7a5e3667d..c1ba4c7982aecac1fbcdbe4f9ebc27179b85de2d 100644 (file)
@@ -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;
index c1de02e671ff7903337309e9b50eeb7bc3d1a90b..fefaf775c3de33b1bdbeba36bf670ff2fa790265 100644 (file)
@@ -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;
index ab4f21df670da27ebd66bf45528a8d60417fcdf4..8fa0ec8faca8c7bfcb160e8194f706889f33da43 100644 (file)
@@ -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--) {
index 6d515bf2bdb22fd615d5edb9b12da727767beece..09d75307b1ad584752b3aa104b1049e0a2a8e62d 100644 (file)
@@ -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;
 
index 74f9dbf4b98da91461e2d77f7d2b5190b814b65c..82b9b4b93b73a13d8a872a27d784d06537acd2ab 100644 (file)
@@ -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))
index c8632972d8333654d805f131b6f9727204913079..d69fb85a29aa1696045e788d0e643e027c095ed3 100644 (file)
@@ -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++) {
index 18d44b8ad51e2351a35c07e429cbfd8a13049e01..083bb957b97f99634f348d784488ebf28fca8419 100644 (file)
@@ -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);
 
index 4beba452dba134ee51419554e84b5bc3be9aa1dd..3b25d01303bb4fc819d9de9f288b9c1093c7845d 100644 (file)
@@ -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++) {
index 97d9da13056187e383d9d762f486800d8a992a6d..d35e0a81a2f59dd3a8b61944b829cec61c380420 100644 (file)
@@ -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);