From 1ea9e58a78c7cd4b58136e420bbcc359dc1c5a10 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Mon, 7 Aug 2023 16:38:55 +0800 Subject: [PATCH] 1, fix: DAG optimize error for code 'for (pp = &h; *pp; pp = &(*pp)->next)', 2, update scf_eda.proto --- core/scf_basic_block.c | 8 +-- core/scf_dag.c | 12 ++-- core/scf_dag.h | 1 + core/scf_optimizer_basic_block.c | 94 ++++---------------------------- native/eda/scf_eda.pb-c.c | 48 ++++++++++++---- native/eda/scf_eda.pb-c.h | 4 +- native/eda/scf_eda.proto | 11 ++-- native/eda/scf_eda_pb.c | 44 ++++++++++++++- native/eda/scf_eda_pb.h | 14 +++++ 9 files changed, 129 insertions(+), 107 deletions(-) diff --git a/core/scf_basic_block.c b/core/scf_basic_block.c index 92766c8..a41f99b 100644 --- a/core/scf_basic_block.c +++ b/core/scf_basic_block.c @@ -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); } } diff --git a/core/scf_dag.c b/core/scf_dag.c index 7e7b44d..97b7d2a 100644 --- a/core/scf_dag.c +++ b/core/scf_dag.c @@ -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; } diff --git a/core/scf_dag.h b/core/scf_dag.h index ef69f22..2afa80c 100644 --- a/core/scf_dag.h +++ b/core/scf_dag.h @@ -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; diff --git a/core/scf_optimizer_basic_block.c b/core/scf_optimizer_basic_block.c index a591b09..a46b3e4 100644 --- a/core/scf_optimizer_basic_block.c +++ b/core/scf_optimizer_basic_block.c @@ -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; } diff --git a/native/eda/scf_eda.pb-c.c b/native/eda/scf_eda.pb-c.c index 09a1cc3..55150a6 100644 --- a/native/eda/scf_eda.pb-c.c +++ b/native/eda/scf_eda.pb-c.c @@ -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, diff --git a/native/eda/scf_eda.pb-c.h b/native/eda/scf_eda.pb-c.h index 99273f6..6bd25ac 100644 --- a/native/eda/scf_eda.pb-c.h +++ b/native/eda/scf_eda.pb-c.h @@ -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 diff --git a/native/eda/scf_eda.proto b/native/eda/scf_eda.proto index 2d28c72..c68bc6a 100644 --- a/native/eda/scf_eda.proto +++ b/native/eda/scf_eda.proto @@ -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 diff --git a/native/eda/scf_eda_pb.c b/native/eda/scf_eda_pb.c index 044cfde..5b86907 100644 --- a/native/eda/scf_eda_pb.c +++ b/native/eda/scf_eda_pb.c @@ -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; diff --git a/native/eda/scf_eda_pb.h b/native/eda/scf_eda_pb.h index 16aeafc..9aa491b 100644 --- a/native/eda/scf_eda_pb.h +++ b/native/eda/scf_eda_pb.h @@ -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); -- 2.25.1