1, delete some repeated code,
authoryu.dongliang <18588496441@163.com>
Sun, 3 Dec 2023 12:07:52 +0000 (20:07 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 3 Dec 2023 12:07:58 +0000 (20:07 +0800)
2, fix: x64 native code error for 'examples/x64_sib.c'

core/scf_3ac.c
core/scf_3ac.h
core/scf_operator_handler_3ac.c
core/scf_optimizer_basic_block.c
examples/x64_sib.c [new file with mode: 0644]
native/x64/scf_x64_inst_util.c
native/x64/scf_x64_peephole.c
parse/scf_operator_handler_semantic.c
parse/scf_parse.c

index 3d1ff121bfdbee28a47038a6ee5f7b5233d7e26b..edc8cca2ba368b5f90a64143639ca50248878c2d 100644 (file)
@@ -272,7 +272,6 @@ scf_3ac_code_t* scf_3ac_code_clone(scf_3ac_code_t* c)
                        dst2->dag_node = dst->dag_node;
                        dst2->code     = dst->code;
                        dst2->bb       = dst->bb;
-                       dst2->debug_w  = dst->debug_w;
                }
        }
 
@@ -302,7 +301,6 @@ scf_3ac_code_t* scf_3ac_code_clone(scf_3ac_code_t* c)
                        src2->dag_node = src->dag_node;
                        src2->code     = src->code;
                        src2->bb       = src->bb;
-                       src2->debug_w  = src->debug_w;
                }
        }
 
@@ -1692,10 +1690,6 @@ scf_3ac_code_t* scf_3ac_code_NN(int op_type, scf_node_t** dsts, int nb_dsts, scf
                                node = node->nodes[0];
 
                        operand->node = node;
-                       if (node)
-                               operand->debug_w = node->debug_w;
-                       else
-                               operand->debug_w = NULL;
 
                        scf_vector_add(vsrc, operand);
                }
@@ -1713,10 +1707,6 @@ scf_3ac_code_t* scf_3ac_code_NN(int op_type, scf_node_t** dsts, int nb_dsts, scf
                                node = node->nodes[0];
 
                        operand->node = node;
-                       if (node)
-                               operand->debug_w = node->debug_w;
-                       else
-                               operand->debug_w = NULL;
 
                        scf_vector_add(vdst, operand);
                }
index bab4f8182045e85cec898cf50aff1a0f3aecb4b6..a1c0b09e7c7b6c05fdf90f2d16cadc88090aa49a 100644 (file)
@@ -21,7 +21,6 @@ struct scf_3ac_operand_s {
        scf_3ac_code_t*     code;      // for branch jump
        scf_basic_block_t*  bb;        // dst basic block of jmp
 
-       scf_lex_word_t*     debug_w;
        void*               rabi;
 };
 
index 011accb50cd3dabd3f7bfbd933b30ce91528b2a0..bcc4c7dd8534310d09d03d3d5f680f68e36d0ae8 100644 (file)
@@ -163,74 +163,14 @@ static int _scf_expr_calculate(scf_ast_t* ast, scf_expr_t* e, void* data)
        return 0;
 }
 
-static int _scf_3ac_call_extern(scf_list_t* h, const char* fname, scf_node_t* d, scf_node_t** nodes, int nb_nodes)
-{
-       scf_loge("\n");
-       return -1;
-}
-
 static int _scf_3ac_code_NN(scf_list_t* h, int op_type, scf_node_t** dsts, int nb_dsts, scf_node_t** srcs, int nb_srcs)
 {
-       scf_3ac_operator_t* _3ac_op = scf_3ac_find_operator(op_type);
-       if (!_3ac_op) {
+       scf_3ac_code_t* c = scf_3ac_code_NN(op_type, dsts, nb_dsts, srcs, nb_srcs);
+       if (!c) {
                scf_loge("\n");
                return -1;
        }
 
-       scf_3ac_operand_t* operand;
-       scf_3ac_code_t*    c;
-       scf_vector_t*      vsrc = NULL;
-       scf_vector_t*      vdst = NULL;
-       scf_node_t*        node;
-
-       int i;
-
-       if (srcs) {
-               vsrc = scf_vector_alloc();
-               for (i = 0; i < nb_srcs; i++) {
-
-                       operand = scf_3ac_operand_alloc();
-
-                       node    = srcs[i];
-
-                       while (node && SCF_OP_EXPR == node->type)
-                               node = node->nodes[0];
-
-                       operand->node = node;
-                       if (node)
-                               operand->debug_w = node->debug_w;
-                       else
-                               operand->debug_w = NULL;
-
-                       scf_vector_add(vsrc, operand);
-               }
-       }
-
-       if (dsts) {
-               vdst = scf_vector_alloc();
-               for (i = 0; i < nb_dsts; i++) {
-
-                       operand = scf_3ac_operand_alloc();
-
-                       node    = dsts[i];
-
-                       while (node && SCF_OP_EXPR == node->type)
-                               node = node->nodes[0];
-
-                       operand->node = node;
-                       if (node)
-                               operand->debug_w = node->debug_w;
-                       else
-                               operand->debug_w = NULL;
-
-                       scf_vector_add(vdst, operand);
-               }
-       }
-
-       c       = scf_3ac_code_alloc();
-       c->op   = _3ac_op;
-       c->dsts = vdst;
-       c->srcs = vsrc;
        scf_list_add_tail(h, &c->list);
        return 0;
 }
@@ -715,7 +655,7 @@ static int _scf_op_error(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void*
                        scf_vector_add(argv, e->nodes[0]);
                }
 
-               ret = _scf_3ac_call_extern(d->_3ac_list_head, "printf", parent, (scf_node_t**)argv->data, argv->size);
+               //ret = _scf_3ac_call_extern(d->_3ac_list_head, "printf", parent, (scf_node_t**)argv->data, argv->size);
                scf_vector_free(argv);
                argv = NULL;
                SCF_CHECK_ERROR(ret < 0, ret, "expr calculate failed\n");
@@ -1324,7 +1264,7 @@ static int _scf_do_error(scf_ast_t* ast, scf_node_t* err, scf_handler_data_t* d)
        scf_vector_add(argv, f);
        scf_vector_add(argv, e->nodes[0]);
 
-       ret = _scf_3ac_call_extern(d->_3ac_list_head, "scf_delete", NULL, (scf_node_t**)argv->data, argv->size);
+//     ret = _scf_3ac_call_extern(d->_3ac_list_head, "scf_delete", NULL, (scf_node_t**)argv->data, argv->size);
        scf_vector_free(argv);
        argv = NULL;
        SCF_CHECK_ERROR(ret < 0, ret, "call scf_delete failed\n");
index df9cf5c0665888faadb424a4a1da3a6245170fcb..6ed586f49f407600b8b051d3947ea61191dfe567 100644 (file)
@@ -188,37 +188,6 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
                }
        }
 
-       for (l = scf_list_head(&h); l != scf_list_sentinel(&h); l = scf_list_next(l)) {
-
-               c  = scf_list_data(l,  scf_3ac_code_t, list);
-
-               for (l2 = scf_list_head(&bb->code_list_head); l2 != scf_list_sentinel(&bb->code_list_head); l2 = scf_list_next(l2)) {
-
-                       c2  = scf_list_data(l2, scf_3ac_code_t, list);
-
-                       if (scf_3ac_code_same(c, c2)) {
-
-                               if (c->dsts) {
-                                       dst  = c ->dsts->data[0];
-                                       dst2 = c2->dsts->data[0];
-
-                                       dst->debug_w = dst2->debug_w;
-                               }
-
-                               if (c->srcs) {
-                                       for (i   = 0; i < c ->srcs->size; i++) {
-                                               src  =        c ->srcs->data[i];
-                                               src2 =        c2->srcs->data[i];
-
-                                               src->debug_w = src2->debug_w;
-                                       }
-                               }
-                               break;
-                       }
-               }
-//             assert(l2 != scf_list_sentinel(&bb->code_list_head));
-       }
-
        scf_list_clear(&bb->code_list_head, scf_3ac_code_t, list, scf_3ac_code_free);
 
        for (l = scf_list_head(&h); l != scf_list_sentinel(&h); ) {
diff --git a/examples/x64_sib.c b/examples/x64_sib.c
new file mode 100644 (file)
index 0000000..7a75a31
--- /dev/null
@@ -0,0 +1,12 @@
+int printf(const char* fmt, ...);
+
+int main()
+{
+       int a[] = {1, 2, 3, 4, 5};
+
+       int i;
+       for (i = 0; i < 5; i++)
+               printf("%d\n", a[i]);
+
+       return 0;
+}
index ac87331a89700963253ecd579d7ca33713478691..995fa3a69bcd4030ed20419012f13219dfa8286d 100644 (file)
@@ -167,7 +167,7 @@ static int _x64_make_disp(scf_rela_t** prela, scf_instruction_t* inst, uint32_t
                return 0;
        }
 
-       if (disp < 127 && disp > -128) {
+       if (disp <= 127 && disp >= -128) {
                scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE_DISP8);
                inst->code[inst->len++] = ModRM;
        } else {
@@ -183,7 +183,7 @@ static int _x64_make_disp(scf_rela_t** prela, scf_instruction_t* inst, uint32_t
                inst->code[inst->len++] = SIB;
        }
 
-       if (disp < 127 && disp > -128) {
+       if (disp <= 127 && disp >= -128) {
                inst->code[inst->len++] = (int8_t)disp;
        } else {
                uint8_t* p = (uint8_t*)&disp;
@@ -714,7 +714,7 @@ scf_instruction_t* _x64_make_inst_SIB(scf_instruction_t* inst, scf_x64_OpCode_t*
        if (SCF_X64_RM_EBP !=  r_base->id && SCF_X64_RM_ESP != r_base->id && 0 == disp)
                scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE);
        else {
-               if (disp < 127 && disp > -128)
+               if (disp <= 127 && disp >= -128)
                        scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE_DISP8);
                else
                        scf_ModRM_setMod(&ModRM, SCF_X64_MOD_BASE_DISP32);
index bedc6b8db8d136ee5a270b1dfeadfc07222f49af..74f9dbf4b98da91461e2d77f7d2b5190b814b65c 100644 (file)
@@ -368,7 +368,8 @@ static void _x64_peephole_function(scf_vector_t* tmp_insts, scf_function_t* f, i
                                        if (scf_inst_data_same(&inst->dst, &inst2->dst))
                                                break;
 
-                               } else if (SCF_X64_LEA  == inst2->OpCode->type) {
+                               } else if (SCF_X64_LEA  == inst2->OpCode->type
+                                               || SCF_X64_MOV  == inst2->OpCode->type) {
                                        if (inst2->src.base == inst->dst.base)
                                                break;
                                }
index 735fb50fc8e4efc7291255741e5a5ed0ab75bd73..4beba452dba134ee51419554e84b5bc3be9aa1dd 100644 (file)
@@ -2457,7 +2457,7 @@ static int _scf_op_semantic_assign(scf_ast_t* ast, scf_node_t** nodes, int nb_no
 
        if (!scf_variable_same_type(v0, v1)) {
 
-               if (scf_variable_is_struct_pointer(v0)) {
+               if (scf_variable_is_struct_pointer(v0) && v1->w && strcmp(v1->w->text->data, "NULL")) {
 
                        scf_type_t*     t = NULL;
                        int ret = scf_ast_find_type_type(&t, ast, v0->type);
index a393ed183038ff5458b7776653bd2b3647ea9881..6852dda452c51f1d7b0235323846569f08415727 100644 (file)
@@ -955,12 +955,12 @@ static int _fill_code_list_inst(scf_string_t* code, scf_list_t* h, int64_t offse
                                if (!dst->dag_node || !dst->dag_node->node)
                                        continue;
 
-                               if (dst->debug_w && line2 < dst->debug_w->line)
-                                       line2 = dst->debug_w->line;
-
                                node = dst->dag_node->node;
 
                                if (node->debug_w) {
+                                       if (line2 < node->debug_w->line)
+                                               line2 = node->debug_w->line;
+
                                        if (scf_type_is_var(node->type) && node->var->local_flag) {
 
                                                ret = _debug_add_var(parse, node);
@@ -982,10 +982,10 @@ static int _fill_code_list_inst(scf_string_t* code, scf_list_t* h, int64_t offse
 
                                node = src->dag_node->node;
 
-                               if (src->debug_w && line2 < src->debug_w->line)
-                                       line2 = src->debug_w->line;
-
                                if (node->debug_w) {
+                                       if (line2 < node->debug_w->line)
+                                               line2 = node->debug_w->line;
+
                                        if (scf_type_is_var(node->type) && node->var->local_flag) {
 
                                                ret = _debug_add_var(parse, node);