add a test 'examples/oop.c', and fix some bugs caused by it
authoryu.dongliang <18588496441@163.com>
Wed, 13 Sep 2023 08:30:03 +0000 (16:30 +0800)
committeryu.dongliang <18588496441@163.com>
Wed, 13 Sep 2023 08:30:03 +0000 (16:30 +0800)
examples/oop.c [new file with mode: 0644]
native/x64/scf_x64_inst_util.c
native/x64/scf_x64_peephole.c
native/x64/scf_x64_reg.c
parse/scf_dfa_expr.c

diff --git a/examples/oop.c b/examples/oop.c
new file mode 100644 (file)
index 0000000..ae6d1fb
--- /dev/null
@@ -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;
+}
index fd4879d6b68bf8cfcecc8ec9a565e409d24b1bdf..45e5483ebb3d44a01ec4be4961527e7cd4055cd6 100644 (file)
@@ -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;
 
index 0c2ec1ad7d597f54690e1a93d96a9043520d949c..63ec4cc2236c25302293eeed337d52a891e18c36 100644 (file)
@@ -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)) {
index ad45485855ae4ddb084834a10d1499fe9947f74a..85e08b9dea1341470e92a88711d506a1133c3960 100644 (file)
@@ -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) {
index d10b9c00b1173eb7649d40c645445da765c727e3..a4918c14278e357c2dab5b1d07133cf67656f108 100644 (file)
@@ -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)