1, fix: DAG optimize error for code 'for (pp = &h; *pp; pp = &(*pp)->next)',
authoryu.dongliang <18588496441@163.com>
Mon, 7 Aug 2023 08:38:55 +0000 (16:38 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 7 Aug 2023 08:40:56 +0000 (16:40 +0800)
2, update scf_eda.proto

core/scf_basic_block.c
core/scf_dag.c
core/scf_dag.h
core/scf_optimizer_basic_block.c
native/eda/scf_eda.pb-c.c
native/eda/scf_eda.pb-c.h
native/eda/scf_eda.proto
native/eda/scf_eda_pb.c
native/eda/scf_eda_pb.h

index 92766c8ce6496555ad86e294059120d91c475855..a41f99bbdd153cc3f4ed39ec7833e0b887575c43 100644 (file)
@@ -312,7 +312,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                                        scf_variable_t* v  = dn->var;
 
                                        if (v && v->w)
-                                               printf("exit  active: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
+                                               printf("exit  active: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
                                }
                        }
 
@@ -333,7 +333,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                                        scf_variable_t* v  = dn->var;
 
                                        if (v && v->w)
-                                               printf("exit  alias:  v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
+                                               printf("exit  alias:  v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
                                }
                        }
 
@@ -377,7 +377,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                                        scf_variable_t* v  = dn->var;
 
                                        if (v && v->w)
-                                               printf("saves:        v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
+                                               printf("saves:        v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
                                }
                        }
 
@@ -388,7 +388,7 @@ void scf_basic_block_print_list(scf_list_t* h)
                                        scf_variable_t* v  = dn->var;
 
                                        if (v && v->w)
-                                               printf("resaves:      v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
+                                               printf("resaves:      v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
                                }
                        }
 
index 7e7b44d2c4e4513d93cec9d36568c306345548b9..97b7d2ab437050ac570521ee8dbc61f80c997af1 100644 (file)
@@ -515,9 +515,9 @@ int scf_dag_node_same(scf_dag_node_t* dag_node, const scf_node_t* node)
                return 0;
 
        if (SCF_OP_ADDRESS_OF == node->type) {
-               scf_logd("type: %d, %d, node: %p, %p\n", dag_node->type, node->type, dag_node, node);
-               scf_logd("var: %p, %p\n", dag_node->var, node->var);
-//             scf_logi("var: %#lx, %#lx\n", 0xffff & (uintptr_t)dag_node->var, 0xffff & (uintptr_t)node->var);
+               scf_logd("type: %d, %d, node: %#lx, %#lx, ", dag_node->type, node->type, 0xffff & (uintptr_t)dag_node, 0xffff & (uintptr_t)node);
+//             scf_loge("var: %p, %p\n", dag_node->var, node->var);
+//             printf("var: %#lx, %#lx\n", 0xffff & (uintptr_t)dag_node->var, 0xffff & (uintptr_t)node->var);
        }
 
        if (scf_type_is_var(node->type)) {
@@ -532,7 +532,8 @@ int scf_dag_node_same(scf_dag_node_t* dag_node, const scf_node_t* node)
                        || SCF_OP_INC      == node->type
                        || SCF_OP_DEC      == node->type
                        || SCF_OP_INC_POST == node->type
-                       || SCF_OP_DEC_POST == node->type) {
+                       || SCF_OP_DEC_POST == node->type
+                       || SCF_OP_ADDRESS_OF == node->type) {
                if (dag_node->var == _scf_operand_get((scf_node_t*)node))
                        return 1;
                return 0;
@@ -861,6 +862,8 @@ int scf_dag_get_node(scf_list_t* h, const scf_node_t* node, scf_dag_node_t** pp)
                        return -ENOMEM;
 
                scf_list_add_tail(h, &dn->list);
+
+               dn->old = *pp;
        } else {
                dn->var->local_flag |= v->local_flag;
                dn->var->tmp_flag   |= v->tmp_flag;
@@ -912,6 +915,7 @@ int scf_dag_dn_same(scf_dag_node_t* dn0, scf_dag_node_t* dn1)
                if (0 == scf_dag_dn_same(child0, child1))
                        return 0;
        }
+
        return 1;
 }
 
index ef69f22f1172808ba292d364e86d2bc4b9258c0c..2afa80ce463e3f116bba5e77ced2d9e59e389e8c 100644 (file)
@@ -25,6 +25,7 @@ struct scf_dag_node_s {
        int                                     type;   // node type
 
        scf_variable_t*         var;
+       scf_dag_node_t*     old;
        scf_node_t*         node;
 
        scf_vector_t*           parents;
index a591b090807d1a608301db4895108954dd9b3365..a46b3e45bfafda80d6bb436993815eae464e806e 100644 (file)
@@ -1,36 +1,6 @@
 #include"scf_optimizer.h"
 
-static scf_dag_node_t* _func_dag_find_dn(scf_vector_t* dag, scf_dag_node_t* dn_bb, scf_list_t* f_dag)
-{
-       scf_dag_node_t* dn_func = NULL;
-       scf_list_t*     l;
-
-       int i;
-       for (i = 0; i < dag->size; i++) {
-               dn_func   = dag->data[i];
-
-               scf_variable_t* v1 = dn_func->var;
-
-               if (dn_func->type == dn_bb->type) {
-
-                       if (dn_func->var == dn_bb->var)
-                               break;
-
-                       if (scf_dag_dn_same(dn_func, dn_bb))
-                               break;
-
-                       if (dn_bb->node) {
-                               if (scf_dag_node_like(dn_func, dn_bb->node, f_dag))
-                                       break;
-                       }
-               }
-               dn_func = NULL;
-       }
-
-       return dn_func;
-}
-
-static int _bb_dag_update(scf_basic_block_t* bb, scf_vector_t* dag, scf_list_t* f_dag)
+static int _bb_dag_update(scf_basic_block_t* bb)
 {
        scf_dag_node_t* dn;
        scf_dag_node_t* dn_bb;
@@ -83,7 +53,7 @@ static int _bb_dag_update(scf_basic_block_t* bb, scf_vector_t* dag, scf_list_t*
 
                                if (SCF_OP_ADDRESS_OF == dn->type || SCF_OP_DEREFERENCE == dn->type) {
 
-                                       dn_func = _func_dag_find_dn(dag, dn, f_dag);
+                                       dn_func = dn->old;
 
                                } else {
                                        assert(dn_bb->parents && dn_bb->parents->size > 0);
@@ -91,7 +61,7 @@ static int _bb_dag_update(scf_basic_block_t* bb, scf_vector_t* dag, scf_list_t*
                                        if (dn != dn_bb->parents->data[dn_bb->parents->size - 1])
                                                continue;
 
-                                       dn_func = _func_dag_find_dn(dag, dn_bb, f_dag);
+                                       dn_func = dn_bb->old;
                                }
 
                                if (!dn_func) {
@@ -136,7 +106,8 @@ static int _bb_dag_update(scf_basic_block_t* bb, scf_vector_t* dag, scf_list_t*
                                assert(dn->childs);
                                assert(2 == dn->childs->size);
 
-                               dn_func = _func_dag_find_dn(dag, dn, f_dag);
+                               dn_func = dn->old;
+
                                if (!dn_func) {
                                        scf_loge("\n");
                                        return -1;
@@ -182,7 +153,6 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
        scf_3ac_code_t*    c;
        scf_3ac_code_t*    c2;
        scf_vector_t*      roots;
-       scf_vector_t*      dag;
        scf_list_t*        l;
        scf_list_t*        l2;
        scf_list_t         h;
@@ -192,36 +162,11 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
 
        scf_list_init(&h);
 
-       dag = scf_vector_alloc();
-       if (!dag)
-               return -ENOMEM;
-
-       for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
-            l = scf_list_next(l)) {
-               c  = scf_list_data(l, scf_3ac_code_t, list);
-
-               if (c->dsts) {
-                       assert(1 == c->dsts->size);
-
-                       dst = c->dsts->data[0];
-
-                       assert(0 == scf_vector_add_unique(dag, dst->dag_node));
-               }
-
-               if (c->srcs) {
-                       for (i  = 0; i < c->srcs->size; i++) {
-                               src =        c->srcs->data[i];
-
-                               assert(0 == scf_vector_add_unique(dag, src->dag_node));
-                       }
-               }
-       }
-
        ret = scf_basic_block_dag2(bb, &bb->dag_list_head);
        if (ret < 0)
                return ret;
 
-       ret = _bb_dag_update(bb, dag, &f->dag_list_head);
+       ret = _bb_dag_update(bb);
        if (ret < 0) {
                scf_loge("\n");
                return ret;
@@ -239,7 +184,7 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
        scf_logd("bb: %p, roots->size: %d\n", bb, roots->size);
 
        for (i = 0; i < roots->size; i++) {
-               dn = roots->data[i];
+               dn =        roots->data[i];
 
                if (!dn)
                        continue;
@@ -289,12 +234,7 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
 
                if (c->dsts) {
                        dst = c->dsts->data[0];
-
-                       dn = _func_dag_find_dn(dag, dst->dag_node, &f->dag_list_head);
-                       if (!dn) {
-                               scf_loge("\n");
-                               return -1;
-                       }
+                       dn = dst->dag_node->old;
 
                        dst->dag_node = dn;
                }
@@ -303,17 +243,7 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
                        for (i  = 0; i < c->srcs->size; i++) {
                                src = c->srcs->data[i];
 
-                               dn = _func_dag_find_dn(dag, src->dag_node, &f->dag_list_head);
-                               if (!dn) {
-
-                                       scf_variable_t* v = src->dag_node->var;
-                                       if (v->w)
-                                               scf_loge("v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
-                                       else
-                                               scf_loge("v_%#lx\n", 0xffff & (uintptr_t)v);
-                                       scf_3ac_code_print(c, NULL);
-                                       return -1;
-                               }
+                               dn = src->dag_node->old;
 
                                src->dag_node = dn;
                        }
@@ -337,7 +267,6 @@ static int __optimize_basic_block(scf_basic_block_t* bb, scf_function_t* f)
 
        scf_dag_node_free_list(&bb->dag_list_head);
        scf_vector_free(roots);
-       scf_vector_free(dag);
        return 0;
 }
 
@@ -357,7 +286,7 @@ static int _optimize_basic_block(scf_ast_t* ast, scf_function_t* f, scf_list_t*
        int i;
 
 //     scf_basic_block_print_list(bb_list_head);
-
+#if 1
        for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
                        l = scf_list_next(l)) {
 
@@ -379,7 +308,8 @@ static int _optimize_basic_block(scf_ast_t* ast, scf_function_t* f, scf_list_t*
                        return ret;
                }
        }
-
+#endif
+//     scf_basic_block_print_list(bb_list_head);
        return 0;
 }
 
index 09a1cc3460a175c180fed4395d42c63bf8a625f0..55150a64a6ce5a5bfa9317432a32caf7ecf46056 100644 (file)
@@ -385,7 +385,7 @@ const ProtobufCMessageDescriptor scf_line__descriptor =
   (ProtobufCMessageInit) scf_line__init,
   NULL,NULL,NULL    /* reserved[123] */
 };
-static const ProtobufCFieldDescriptor scf_epin__field_descriptors[16] =
+static const ProtobufCFieldDescriptor scf_epin__field_descriptors[18] =
 {
   {
     "id",
@@ -532,9 +532,33 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[16] =
     0,NULL,NULL    /* reserved1,reserved2, etc */
   },
   {
-    "x",
+    "tr",
     13,
     PROTOBUF_C_LABEL_REQUIRED,
+    PROTOBUF_C_TYPE_DOUBLE,
+    0,   /* quantifier_offset */
+    offsetof(ScfEpin, tr),
+    NULL,
+    NULL,
+    0,             /* flags */
+    0,NULL,NULL    /* reserved1,reserved2, etc */
+  },
+  {
+    "jtr",
+    14,
+    PROTOBUF_C_LABEL_REQUIRED,
+    PROTOBUF_C_TYPE_DOUBLE,
+    0,   /* quantifier_offset */
+    offsetof(ScfEpin, jtr),
+    NULL,
+    NULL,
+    0,             /* flags */
+    0,NULL,NULL    /* reserved1,reserved2, etc */
+  },
+  {
+    "x",
+    15,
+    PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_INT32,
     0,   /* quantifier_offset */
     offsetof(ScfEpin, x),
@@ -545,7 +569,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[16] =
   },
   {
     "y",
-    14,
+    16,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_INT32,
     0,   /* quantifier_offset */
@@ -557,7 +581,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[16] =
   },
   {
     "vflag",
-    15,
+    17,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_BOOL,
     0,   /* quantifier_offset */
@@ -569,7 +593,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[16] =
   },
   {
     "pflag",
-    16,
+    18,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_BOOL,
     0,   /* quantifier_offset */
@@ -589,19 +613,21 @@ static const unsigned scf_epin__field_indices_by_name[] = {
   3,   /* field[3] = flags */
   0,   /* field[0] = id */
   9,   /* field[9] = jR */
+  13,   /* field[13] = jtr */
   2,   /* field[2] = lid */
-  15,   /* field[15] = pflag */
+  17,   /* field[17] = pflag */
   4,   /* field[4] = tos */
+  12,   /* field[12] = tr */
   10,   /* field[10] = uF */
   11,   /* field[11] = uH */
-  14,   /* field[14] = vflag */
-  12,   /* field[12] = x */
-  13,   /* field[13] = y */
+  16,   /* field[16] = vflag */
+  14,   /* field[14] = x */
+  15,   /* field[15] = y */
 };
 static const ProtobufCIntRange scf_epin__number_ranges[1 + 1] =
 {
   { 1, 0 },
-  { 0, 16 }
+  { 0, 18 }
 };
 const ProtobufCMessageDescriptor scf_epin__descriptor =
 {
@@ -611,7 +637,7 @@ const ProtobufCMessageDescriptor scf_epin__descriptor =
   "ScfEpin",
   "",
   sizeof(ScfEpin),
-  16,
+  18,
   scf_epin__field_descriptors,
   scf_epin__field_indices_by_name,
   1,  scf_epin__number_ranges,
index 99273f6e90e2096f56cad1579857122384b9f315..6bd25ac7bf1f71b74bbdfd41e3c3871c5eb62766 100644 (file)
@@ -58,6 +58,8 @@ struct  _ScfEpin
   double jr;
   double uf;
   double uh;
+  double tr;
+  double jtr;
   int32_t x;
   int32_t y;
   protobuf_c_boolean vflag;
@@ -65,7 +67,7 @@ struct  _ScfEpin
 };
 #define SCF_EPIN__INIT \
  { PROTOBUF_C_MESSAGE_INIT (&scf_epin__descriptor) \
-    , 0, 0, 0, 0, 0,NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
+    , 0, 0, 0, 0, 0,NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 
 
 struct  _ScfEconn
index 2d28c7215fca99a3dd86afdd3838ef2e1a75ffa4..c68bc6ab6418b6ee0f259a867dfb2f470463a301 100644 (file)
@@ -24,10 +24,13 @@ message scf_epin
        required double    uF    = 11;
        required double    uH    = 12;
 
-       required int32     x     = 13;
-       required int32     y     = 14;
-       required bool      vflag = 15;
-       required bool      pflag = 16;
+       required double    tr    = 13;
+       required double    jtr   = 14;
+
+       required int32     x     = 15;
+       required int32     y     = 16;
+       required bool      vflag = 17;
+       required bool      pflag = 18;
 }
 
 message scf_econn
index 044cfdeed73ab494f022c997f4979932e107ad6c..5b869072c8592b4a8b5baf442f6b75222ea6c350 100644 (file)
@@ -14,6 +14,35 @@ static int component_pins[SCF_EDA_Components_NB] =
        SCF_EDA_Transistor_NB,
 };
 
+static scf_edata_t  component_datas[] =
+{
+       {SCF_EDA_None,       0,                   0,  0, 0,         0, 0,    0,    0},
+       {SCF_EDA_Battery,    0, SCF_EDA_Battery_POS,  0, 0,         0, 0,    0,    0},
+
+       {SCF_EDA_Resistor,   0,                   0,  0, 0, 10 * 1000, 0,    0,    0},
+       {SCF_EDA_Capacitor,  0,                   0,  0, 0,         0, 0,  0.1,    0},
+       {SCF_EDA_Inductor,   0,                   0,  0, 0,         0, 0,    0, 1000},
+
+       {SCF_EDA_Diode,      0, SCF_EDA_Diode_NEG,    0, 0,       750, 0,    0,    0},
+       {SCF_EDA_Transistor, 0, SCF_EDA_Transistor_B, 0, 0,       750, 0,    0,    0},
+       {SCF_EDA_Transistor, 0, SCF_EDA_Transistor_C, 0, 0,       750, 0,    0,    0},
+};
+
+static scf_edata_t* _eda_find_data(const uint64_t type, const uint64_t model, const uint64_t pid)
+{
+       scf_edata_t* ed;
+
+       int i;
+       for (i = 0; i < sizeof(component_datas) / sizeof(component_datas[0]); i++) {
+               ed =              &component_datas[i];
+
+               if (ed->type == type && ed->model == model && ed->pid == pid)
+                       return ed;
+       }
+
+       return NULL;
+}
+
 ScfEconn* scf_econn__alloc()
 {
        ScfEconn* ec = malloc(sizeof(ScfEconn));
@@ -344,10 +373,13 @@ void scf_epin__free(ScfEpin* pin)
 
 ScfEcomponent* scf_ecomponent__alloc(uint64_t type)
 {
+       ScfEcomponent* c;
+       scf_edata_t*   ed;
+
        if (type >= SCF_EDA_Components_NB)
                return NULL;
 
-       ScfEcomponent* c = malloc(sizeof(ScfEcomponent));
+       c = malloc(sizeof(ScfEcomponent));
        if (!c)
                return NULL;
 
@@ -371,6 +403,16 @@ ScfEcomponent* scf_ecomponent__alloc(uint64_t type)
                        scf_epin__free(pin);
                        return NULL;
                }
+
+               ed = _eda_find_data(c->type, c->model, pin->id);
+               if (ed) {
+                       pin->v  = ed->v;
+                       pin->a  = ed->a;
+                       pin->r  = ed->r;
+                       pin->jr = ed->jr;
+                       pin->uf = ed->uf;
+                       pin->uh = ed->uh;
+               }
        }
 
        return c;
index 16aeafc457eba7059c9dfd86b358e5c485a71962..9aa491b2f485aca5b7de55e6164d83105b9ad40e 100644 (file)
@@ -42,6 +42,20 @@ enum {
        SCF_EDA_Transistor_NB,
 };
 
+typedef struct {
+       uint64_t  type;
+       uint64_t  model;
+       uint64_t  pid;
+
+       double    v;
+       double    a;
+       double    r;
+       double    jr;
+       double    uf;
+       double    uh;
+} scf_edata_t;
+
+
 ScfEconn*      scf_econn__alloc();
 int            scf_econn__add_cid(ScfEconn* ec, uint64_t  cid);
 int            scf_econn__del_cid(ScfEconn* ec, uint64_t  cid);