From a5147201662d0314e1c95d56d7c3b581df243883 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sun, 3 Dec 2023 20:07:52 +0800 Subject: [PATCH] 1, delete some repeated code, 2, fix: x64 native code error for 'examples/x64_sib.c' --- core/scf_3ac.c | 10 ---- core/scf_3ac.h | 1 - core/scf_operator_handler_3ac.c | 68 ++------------------------- core/scf_optimizer_basic_block.c | 31 ------------ examples/x64_sib.c | 12 +++++ native/x64/scf_x64_inst_util.c | 6 +-- native/x64/scf_x64_peephole.c | 3 +- parse/scf_operator_handler_semantic.c | 2 +- parse/scf_parse.c | 12 ++--- 9 files changed, 28 insertions(+), 117 deletions(-) create mode 100644 examples/x64_sib.c diff --git a/core/scf_3ac.c b/core/scf_3ac.c index 3d1ff12..edc8cca 100644 --- a/core/scf_3ac.c +++ b/core/scf_3ac.c @@ -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); } diff --git a/core/scf_3ac.h b/core/scf_3ac.h index bab4f81..a1c0b09 100644 --- a/core/scf_3ac.h +++ b/core/scf_3ac.h @@ -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; }; diff --git a/core/scf_operator_handler_3ac.c b/core/scf_operator_handler_3ac.c index 011accb..bcc4c7d 100644 --- a/core/scf_operator_handler_3ac.c +++ b/core/scf_operator_handler_3ac.c @@ -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"); diff --git a/core/scf_optimizer_basic_block.c b/core/scf_optimizer_basic_block.c index df9cf5c..6ed586f 100644 --- a/core/scf_optimizer_basic_block.c +++ b/core/scf_optimizer_basic_block.c @@ -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 index 0000000..7a75a31 --- /dev/null +++ b/examples/x64_sib.c @@ -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; +} diff --git a/native/x64/scf_x64_inst_util.c b/native/x64/scf_x64_inst_util.c index ac87331..995fa3a 100644 --- a/native/x64/scf_x64_inst_util.c +++ b/native/x64/scf_x64_inst_util.c @@ -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); diff --git a/native/x64/scf_x64_peephole.c b/native/x64/scf_x64_peephole.c index bedc6b8..74f9dbf 100644 --- a/native/x64/scf_x64_peephole.c +++ b/native/x64/scf_x64_peephole.c @@ -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; } diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index 735fb50..4beba45 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -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); diff --git a/parse/scf_parse.c b/parse/scf_parse.c index a393ed1..6852dda 100644 --- a/parse/scf_parse.c +++ b/parse/scf_parse.c @@ -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); -- 2.25.1