From: yu.dongliang <18588496441@163.com> Date: Wed, 13 Sep 2023 08:30:03 +0000 (+0800) Subject: add a test 'examples/oop.c', and fix some bugs caused by it X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=d9f2b538268a614e6729fb02001e916ec2f00a4e;p=scf.git add a test 'examples/oop.c', and fix some bugs caused by it --- diff --git a/examples/oop.c b/examples/oop.c new file mode 100644 index 0000000..ae6d1fb --- /dev/null +++ b/examples/oop.c @@ -0,0 +1,34 @@ +int printf(const char* fmt, ...); + +int Apt(int i, int j); + +struct Aops +{ + Apt* pt0; + Apt* pt1; +}; + +int add(int i, int j) +{ + return i + j; +} + +int sub(int i, int j) +{ + return i - j; +} + +Aops aops = +{ + add, + sub, +}; + +int main() +{ + int a = aops->pt0(3, 1); + int b = aops->pt1(3, 1); + + printf("a: %d, b: %d\n", a, b); + return 0; +} diff --git a/native/x64/scf_x64_inst_util.c b/native/x64/scf_x64_inst_util.c index fd4879d..45e5483 100644 --- a/native/x64/scf_x64_inst_util.c +++ b/native/x64/scf_x64_inst_util.c @@ -287,7 +287,8 @@ scf_instruction_t* x64_make_inst_E(scf_x64_OpCode_t* OpCode, scf_register_t* r) } else if (SCF_X64_MUL == OpCode->type || SCF_X64_DIV == OpCode->type || SCF_X64_IMUL == OpCode->type - || SCF_X64_IDIV == OpCode->type) + || SCF_X64_IDIV == OpCode->type + || SCF_X64_CALL == OpCode->type) inst->src.base = (scf_register_t*)r; return inst; @@ -372,7 +373,8 @@ scf_instruction_t* x64_make_inst_M(scf_rela_t** prela, scf_x64_OpCode_t* OpCode, } else if (SCF_X64_MUL == OpCode->type || SCF_X64_DIV == OpCode->type || SCF_X64_IMUL == OpCode->type - || SCF_X64_IDIV == OpCode->type) { + || SCF_X64_IDIV == OpCode->type + || SCF_X64_CALL == OpCode->type) { inst->src.base = (scf_register_t*)r_base; inst->src.disp = offset; @@ -482,7 +484,7 @@ scf_instruction_t* x64_make_inst_G2M(scf_rela_t** prela, scf_x64_OpCode_t* OpCod offset = v_dst->offset; } - inst = _x64_make_OpCode(OpCode, v_dst->size, r_src, r_base, NULL); + inst = _x64_make_OpCode(OpCode, x64_variable_size(v_dst), r_src, r_base, NULL); if (!inst) return NULL; diff --git a/native/x64/scf_x64_peephole.c b/native/x64/scf_x64_peephole.c index 0c2ec1a..63ec4cc 100644 --- a/native/x64/scf_x64_peephole.c +++ b/native/x64/scf_x64_peephole.c @@ -331,7 +331,9 @@ static void _x64_peephole_function(scf_vector_t* tmp_insts, scf_function_t* f, i if (X64_COLOR_CONFLICT(r0->color, rdi->color) || X64_COLOR_CONFLICT(r0->color, rsi->color) || X64_COLOR_CONFLICT(r0->color, rdx->color) - || X64_COLOR_CONFLICT(r0->color, rcx->color)) + || X64_COLOR_CONFLICT(r0->color, rcx->color) + || X64_COLOR_CONFLICT(r0->color, r8->color) + || X64_COLOR_CONFLICT(r0->color, r9->color)) break; } else { if (x64_inst_data_is_reg(&inst2->src)) { diff --git a/native/x64/scf_x64_reg.c b/native/x64/scf_x64_reg.c index ad45485..85e08b9 100644 --- a/native/x64/scf_x64_reg.c +++ b/native/x64/scf_x64_reg.c @@ -1094,7 +1094,7 @@ int x64_pointer_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* member { scf_variable_t* vb = base ->var; scf_variable_t* vm = member->var; - scf_register_t* rb = NULL; + scf_register_t* rb = NULL; scf_x64_OpCode_t* lea; scf_x64_OpCode_t* mov; @@ -1116,17 +1116,12 @@ int x64_pointer_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* member } else if (vb->global_flag) { scf_rela_t* rela = NULL; - ret = x64_select_reg(&rb, base, c, f, 0); + ret = x64_select_reg(&rb, base, c, f, 1); if (ret < 0) { scf_loge("\n"); return ret; } - lea = x64_find_OpCode(SCF_X64_LEA, 8, 8, SCF_X64_E2G); - inst = x64_make_inst_M2G(&rela, lea, rb, NULL, vb); - X64_INST_ADD_CHECK(c->instructions, inst); - X64_RELA_ADD_CHECK(f->data_relas, rela, c, vb, NULL); - } else { ret = x64_select_reg(&rb, base, c, f, 0); if (ret < 0) { @@ -1183,17 +1178,12 @@ int x64_array_index_reg(x64_sib_t* sib, scf_dag_node_t* base, scf_dag_node_t* in if (0 == base->color) base->color = -1; - ret = x64_select_reg(&rb, base, c, f, 0); + ret = x64_select_reg(&rb, base, c, f, 1); if (ret < 0) { scf_loge("\n"); return ret; } - lea = x64_find_OpCode(SCF_X64_LEA, 8, 8, SCF_X64_E2G); - inst = x64_make_inst_M2G(&rela, lea, rb, NULL, vb); - X64_INST_ADD_CHECK(c->instructions, inst); - X64_RELA_ADD_CHECK(f->data_relas, rela, c, vb, NULL); - } else { ret = x64_select_reg(&rb, base, c, f, 0); if (ret < 0) { diff --git a/parse/scf_dfa_expr.c b/parse/scf_dfa_expr.c index d10b9c0..a4918c1 100644 --- a/parse/scf_dfa_expr.c +++ b/parse/scf_dfa_expr.c @@ -666,9 +666,18 @@ int _expr_multi_rets(scf_expr_t* e) if (!call) return 0; - else if (SCF_OP_CALL == call->type) { - scf_variable_t* v_pf = _scf_operand_get(call->nodes[0]); + if (SCF_OP_CALL == call->type) { + + scf_node_t* n_pf = call->nodes[0]; + + if (SCF_OP_POINTER == n_pf->type) { + assert(2 == n_pf->nb_nodes); + + n_pf = n_pf->nodes[1]; + } + + scf_variable_t* v_pf = _scf_operand_get(n_pf); scf_function_t* f = v_pf->func_ptr; if (f->rets->size <= 1)