eda
authoryu.dongliang <18588496441@163.com>
Fri, 7 Jul 2023 06:20:09 +0000 (14:20 +0800)
committeryu.dongliang <18588496441@163.com>
Fri, 7 Jul 2023 06:20:09 +0000 (14:20 +0800)
main.c
scf_eda.c [new file with mode: 0644]
scf_eda.h [new file with mode: 0644]
scf_eda.pb-c.c
scf_eda.pb-c.h
scf_eda.proto
scf_eda_inst.c [new file with mode: 0644]
scf_vector.h [new file with mode: 0644]
ses_layout.c [new file with mode: 0644]
ses_layout.h [new file with mode: 0644]

diff --git a/main.c b/main.c
index 351b568b5baee8ed46f682ecf63c3cdd655a222f..f68fb56c4f477665a0bb437219a017f21b136908 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,6 +4,8 @@
 #include"scf_eda_pb.h"
 #include"scf_def.h"
 
+
+
 int main(int argc, char* argv[])
 {
        if (argc < 2) {
@@ -41,37 +43,36 @@ int main(int argc, char* argv[])
        fclose(fp);
        fp = NULL;
 
-       ScfEboard* p = scf_eboard__unpack(NULL, len, pb);
+       ScfEcomponent* c;
+       ScfEfunction*  f;
+       ScfEpin*       p;
+
+       ScfEboard* b = scf_eboard__unpack(NULL, len, pb);
 
        size_t i;
        size_t j;
        size_t k;
        size_t l;
 
-       for (i = 0; i < p->n_functions; i++) {
-               ScfEfunction* pf = p->functions[i];
-
-               printf("f: %s\n", pf->name);
+       for (i = 0; i < b->n_functions; i++) {
+               f  =        b->functions[i];
 
-               for (l = 0; l < pf->n_components; l++) {
-                       ScfEcomponent* pc = pf->components[l];
+               printf("f: %s\n", f->name);
 
-                       printf("i: %ld, pc: %p, id: %ld, cid: %ld, n_pins: %ld\n", i, pc, pc->id, pc->type, pc->n_pins);
+               for (j = 0; j < f->n_components; j++) {
+                       c  =        f->components[j];
 
-                       for (j = 0; j < pc->n_pins; j++) {
-                               ScfEpin* pp = pc->pins[j];
+                       printf("c: %ld\n", c->id);
 
-                               printf("j: %ld, pp: %p, n_tos: %ld\n", j, pp, pp->n_tos);
+                       for (k = 0; k < c->n_pins; k++) {
+                               p  =        c->pins[k];
 
-                               for (k = 0; k + 1 < pp->n_tos; k += 2) {
-                                       printf("k: %ld, cid: %ld, pid: %ld\n", k, pp->tos[k], pp->tos[k + 1]);
-                               }
+                               printf("cid: %ld, pid: %ld\n", p->cid, p->id);
                        }
-                       printf("\n");
                }
                printf("\n\n");
        }
 
-       scf_eboard__free_unpacked(p, NULL);
+       scf_eboard__free_unpacked(b, NULL);
        return 0;
 }
diff --git a/scf_eda.c b/scf_eda.c
new file mode 100644 (file)
index 0000000..fec807a
--- /dev/null
+++ b/scf_eda.c
@@ -0,0 +1,148 @@
+#include"scf_eda.h"
+#include"scf_basic_block.h"
+#include"scf_3ac.h"
+
+int    scf_eda_open(scf_native_t* ctx, const char* arch)
+{
+       scf_eda_context_t* eda = calloc(1, sizeof(scf_eda_context_t));
+       if (!eda)
+               return -ENOMEM;
+
+       ctx->priv = eda;
+       return 0;
+}
+
+int scf_eda_close(scf_native_t* ctx)
+{
+       scf_eda_context_t* eda = ctx->priv;
+
+       if (eda) {
+               free(eda);
+               eda = NULL;
+       }
+       return 0;
+}
+
+static int _eda_make_insts_for_list(scf_native_t* ctx, scf_list_t* h, int bb_offset)
+{
+       scf_list_t* l;
+       int ret;
+
+       for (l = scf_list_head(h); l != scf_list_sentinel(h); l = scf_list_next(l)) {
+
+               scf_3ac_code_t* c = scf_list_data(l, scf_3ac_code_t, list);
+
+               eda_inst_handler_t* h = scf_eda_find_inst_handler(c->op->type);
+               if (!h) {
+                       scf_loge("3ac operator '%s' not supported\n", c->op->name);
+                       return -EINVAL;
+               }
+
+               ret = h->func(ctx, c);
+               if (ret < 0) {
+                       scf_3ac_code_print(c, NULL);
+                       scf_loge("3ac op '%s' make inst failed\n", c->op->name);
+                       return ret;
+               }
+
+               if (!c->instructions)
+                       continue;
+
+               scf_3ac_code_print(c, NULL);
+       }
+
+       return bb_offset;
+}
+
+int    _scf_eda_select_inst(scf_native_t* ctx)
+{
+       scf_eda_context_t*      eda = ctx->priv;
+       scf_function_t*     f   = eda->f;
+       scf_basic_block_t*  bb;
+       scf_bb_group_t*     bbg;
+
+       int i;
+       int j;
+       int ret = 0;
+
+       for (i  = 0; i < f->bb_groups->size; i++) {
+               bbg =        f->bb_groups->data[i];
+
+               for (j = 0; j < bbg->body->size; j++) {
+                       bb =        bbg->body->data[j];
+
+                       assert(!bb->native_flag);
+
+                       scf_loge("************ bb: %d\n", bb->index);
+                       ret = _eda_make_insts_for_list(ctx, &bb->code_list_head, 0);
+                       if (ret < 0)
+                               return ret;
+                       bb->native_flag = 1;
+                       scf_loge("************ bb: %d\n", bb->index);
+               }
+       }
+
+       for (i  = 0; i < f->bb_loops->size; i++) {
+               bbg =        f->bb_loops->data[i];
+
+               for (j = 0; j < bbg->body->size; j++) {
+                       bb =        bbg->body->data[j];
+
+                       assert(!bb->native_flag);
+
+                       ret = _eda_make_insts_for_list(ctx, &bb->code_list_head, 0);
+                       if (ret < 0)
+                               return ret;
+                       bb->native_flag = 1;
+               }
+       }
+
+       return 0;
+}
+
+int scf_eda_select_inst(scf_native_t* ctx, scf_function_t* f)
+{
+       scf_eda_context_t* eda = ctx->priv;
+       ScfEcomponent*     B   = NULL;
+
+       scf_dag_node_t*    dn;
+       scf_list_t*        l;
+
+       eda->f = f;
+
+       assert(!f->ef);
+
+       f->ef = scf_efunction__alloc(f->node.w->text->data);
+       if (!f->ef)
+               return -ENOMEM;
+
+       EDA_INST_ADD_COMPONENT(f, B, SCF_EDA_Battery);
+
+       int ret = _scf_eda_select_inst(ctx);
+       if (ret < 0)
+               return ret;
+
+       for (l = scf_list_head(&f->dag_list_head); l != scf_list_sentinel(&f->dag_list_head); l = scf_list_next(l)) {
+
+               dn = scf_list_data(l, scf_dag_node_t, list);
+
+               if (dn->var && dn->var->arg_flag) {
+
+                       int i;
+                       for (i = 0; i < dn->n_pins; i++)
+                               dn->pins[i]->flags |= SCF_EDA_PIN_IN;
+               }
+       }
+
+       return 0;
+}
+
+scf_native_ops_t       native_ops_eda = {
+       .name            = "eda",
+
+       .open            = scf_eda_open,
+       .close           = scf_eda_close,
+
+       .select_inst     = scf_eda_select_inst,
+};
+
diff --git a/scf_eda.h b/scf_eda.h
new file mode 100644 (file)
index 0000000..5dafe64
--- /dev/null
+++ b/scf_eda.h
@@ -0,0 +1,66 @@
+#ifndef SCF_EDA_H
+#define SCF_EDA_H
+
+#include"scf_native.h"
+#include"scf_eda_pb.h"
+
+#define EDA_INST_ADD_COMPONENT(_f, _c, _type) \
+       do { \
+               _c = scf_ecomponent__alloc(_type); \
+               if (!_c) \
+                       return -ENOMEM; \
+               \
+               (_c)->id = (_f)->ef->n_components; \
+               \
+               int ret = scf_efunction__add_component((_f)->ef, _c); \
+               if (ret < 0) { \
+                       scf_ecomponent__free(_c); \
+                       _c = NULL; \
+                       return ret; \
+               } \
+               \
+               for (size_t i = 0;  i < (_c)->n_pins; i++) \
+                       (_c)->pins[i]->cid = (_c)->id; \
+       } while (0)
+
+
+#define EDA_PIN_ADD_COMPONENT(_pin, _cid, _pid) \
+       do { \
+               int ret = scf_epin__add_component((_pin), (_cid), (_pid)); \
+               if (ret < 0) \
+                       return ret; \
+               \
+       } while (0)
+
+typedef struct {
+
+       scf_function_t*     f;
+
+} scf_eda_context_t;
+
+typedef struct {
+       int     type;
+       int             (*func)(scf_native_t* ctx, scf_3ac_code_t* c);
+} eda_inst_handler_t;
+
+eda_inst_handler_t* scf_eda_find_inst_handler(const int op_type);
+
+int scf_eda_open  (scf_native_t* ctx, const char* arch);
+int scf_eda_close (scf_native_t* ctx);
+int scf_eda_select(scf_native_t* ctx);
+
+static inline int eda_variable_size(scf_variable_t* v)
+{
+       if (v->nb_dimentions + v->nb_pointers > 0)
+               return 64;
+
+       if (v->type >= SCF_STRUCT)
+               return 64;
+
+       if (SCF_VAR_BIT == v->type)
+               return 1;
+
+       return v->size << 3;
+}
+
+#endif
index 190f5b83dff6d5cd2c34da35386a5ff6c7a584c1..c4ce91661ae72cf032eb36dbb423aa84085b3b6b 100644 (file)
@@ -299,7 +299,7 @@ const ProtobufCMessageDescriptor scf_eline__descriptor =
   (ProtobufCMessageInit) scf_eline__init,
   NULL,NULL,NULL    /* reserved[123] */
 };
-static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
+static const ProtobufCFieldDescriptor scf_epin__field_descriptors[7] =
 {
   {
     "tos",
@@ -326,11 +326,23 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
     0,NULL,NULL    /* reserved1,reserved2, etc */
   },
   {
-    "flags",
+    "cid",
     3,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_UINT64,
     0,   /* quantifier_offset */
+    offsetof(ScfEpin, cid),
+    NULL,
+    NULL,
+    0,             /* flags */
+    0,NULL,NULL    /* reserved1,reserved2, etc */
+  },
+  {
+    "flags",
+    4,
+    PROTOBUF_C_LABEL_REQUIRED,
+    PROTOBUF_C_TYPE_UINT64,
+    0,   /* quantifier_offset */
     offsetof(ScfEpin, flags),
     NULL,
     NULL,
@@ -339,7 +351,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
   },
   {
     "x",
-    4,
+    5,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_UINT32,
     0,   /* quantifier_offset */
@@ -351,7 +363,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
   },
   {
     "y",
-    5,
+    6,
     PROTOBUF_C_LABEL_REQUIRED,
     PROTOBUF_C_TYPE_UINT32,
     0,   /* quantifier_offset */
@@ -363,7 +375,7 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
   },
   {
     "lines",
-    6,
+    7,
     PROTOBUF_C_LABEL_REPEATED,
     PROTOBUF_C_TYPE_MESSAGE,
     offsetof(ScfEpin, n_lines),
@@ -375,17 +387,18 @@ static const ProtobufCFieldDescriptor scf_epin__field_descriptors[6] =
   },
 };
 static const unsigned scf_epin__field_indices_by_name[] = {
-  2,   /* field[2] = flags */
+  2,   /* field[2] = cid */
+  3,   /* field[3] = flags */
   1,   /* field[1] = id */
-  5,   /* field[5] = lines */
+  6,   /* field[6] = lines */
   0,   /* field[0] = tos */
-  3,   /* field[3] = x */
-  4,   /* field[4] = y */
+  4,   /* field[4] = x */
+  5,   /* field[5] = y */
 };
 static const ProtobufCIntRange scf_epin__number_ranges[1 + 1] =
 {
   { 1, 0 },
-  { 0, 6 }
+  { 0, 7 }
 };
 const ProtobufCMessageDescriptor scf_epin__descriptor =
 {
@@ -395,7 +408,7 @@ const ProtobufCMessageDescriptor scf_epin__descriptor =
   "ScfEpin",
   "",
   sizeof(ScfEpin),
-  6,
+  7,
   scf_epin__field_descriptors,
   scf_epin__field_indices_by_name,
   1,  scf_epin__number_ranges,
index 53b065e9fab83a69de770cb1c644c0677437f286..81d6c72c487bfa177e20b98390a4f74e04efae9a 100644 (file)
@@ -46,6 +46,7 @@ struct  _ScfEpin
   size_t n_tos;
   uint64_t *tos;
   uint64_t id;
+  uint64_t cid;
   uint64_t flags;
   uint32_t x;
   uint32_t y;
@@ -54,7 +55,7 @@ struct  _ScfEpin
 };
 #define SCF_EPIN__INIT \
  { PROTOBUF_C_MESSAGE_INIT (&scf_epin__descriptor) \
-    , 0,NULL, 0, 0, 0, 0, 0,NULL }
+    , 0,NULL, 0, 0, 0, 0, 0, 0,NULL }
 
 
 struct  _ScfEcomponent
index 78e95fb7e78d9917fc0e094f67142fbf17572986..d46b24677c2b02bee48d049a1c7b002cb5694750 100644 (file)
@@ -12,11 +12,12 @@ message scf_epin
 {
        repeated uint64    tos   = 1;
        required uint64    id    = 2;
-       required uint64    flags = 3;
+       required uint64    cid   = 3;
+       required uint64    flags = 4;
 
-       required uint32    x     = 4;
-       required uint32    y     = 5;
-       repeated scf_eline lines = 6;
+       required uint32    x     = 5;
+       required uint32    y     = 6;
+       repeated scf_eline lines = 7;
 }
 
 message scf_ecomponent
diff --git a/scf_eda_inst.c b/scf_eda_inst.c
new file mode 100644 (file)
index 0000000..bf7b376
--- /dev/null
@@ -0,0 +1,625 @@
+#include"scf_eda.h"
+
+#define EDA_INST_OP2_CHECK() \
+       if (!c->dsts || c->dsts->size != 1) \
+               return -EINVAL; \
+       \
+       if (!c->srcs || c->srcs->size != 1) \
+               return -EINVAL; \
+       \
+       scf_eda_context_t* eda  = ctx->priv; \
+       scf_function_t*    f    = eda->f; \
+       \
+       scf_3ac_operand_t* dst  = c->dsts->data[0]; \
+       scf_3ac_operand_t* src  = c->srcs->data[0]; \
+       \
+       if (!src || !src->dag_node) \
+               return -EINVAL; \
+       \
+       if (!dst || !dst->dag_node) \
+               return -EINVAL; \
+       \
+       if (src->dag_node->var->size != dst->dag_node->var->size) {\
+               scf_loge("size: %d, %d\n", src->dag_node->var->size, dst->dag_node->var->size); \
+               return -EINVAL; \
+       }
+
+#define EDA_INST_OP3_CHECK() \
+       if (!c->dsts || c->dsts->size != 1) \
+               return -EINVAL; \
+       \
+       if (!c->srcs || c->srcs->size != 2) \
+               return -EINVAL; \
+       \
+       scf_eda_context_t* eda  = ctx->priv; \
+       scf_function_t*    f    = eda->f; \
+       \
+       scf_3ac_operand_t* dst  = c->dsts->data[0]; \
+       scf_3ac_operand_t* src0 = c->srcs->data[0]; \
+       scf_3ac_operand_t* src1 = c->srcs->data[1]; \
+       \
+       if (!src0 || !src0->dag_node) \
+               return -EINVAL; \
+       \
+       if (!src1 || !src1->dag_node) \
+               return -EINVAL; \
+       \
+       if (!dst || !dst->dag_node) \
+               return -EINVAL; \
+       \
+       if (src0->dag_node->var->size != src1->dag_node->var->size) {\
+               scf_loge("size: %d, %d\n", src0->dag_node->var->size, src1->dag_node->var->size); \
+               return -EINVAL; \
+       }
+
+#define EDA_INST_IN_CHECK(_in, _N) \
+       do { \
+               int i; \
+               if (!(_in)->var->arg_flag) { \
+                       if ((_in)->n_pins != _N) \
+                               return -EINVAL; \
+                       \
+                       for (i = 0; i < N; i++) { \
+                               if (!(_in)->pins[i]) \
+                                       return -EINVAL; \
+                       } \
+               } \
+       } while (0)
+
+static int _eda_inst_bit_not_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       EDA_INST_OP2_CHECK()
+
+       scf_dag_node_t* in  = src->dag_node;
+       scf_dag_node_t* out = dst->dag_node;
+
+       ScfEcomponent*  T   = NULL;
+       ScfEcomponent*  R   = NULL;
+       ScfEcomponent*  B   = f->ef->components[0];
+
+       int i;
+       int N = eda_variable_size(in->var);
+
+       EDA_INST_IN_CHECK(in, N);
+
+       in ->n_pins = N;
+       out->n_pins = N;
+
+       for (i = 0; i < N; i++) {
+
+               EDA_INST_ADD_COMPONENT(f, T, SCF_EDA_Transistor);
+               EDA_INST_ADD_COMPONENT(f, R, SCF_EDA_Resistor);
+
+               in->pins[i] = T->pins[SCF_EDA_Transistor_B];
+
+               EDA_PIN_ADD_COMPONENT(T->pins[SCF_EDA_Transistor_C], R->id, 1);
+               EDA_PIN_ADD_COMPONENT(T->pins[SCF_EDA_Transistor_E], B->id, SCF_EDA_Battery_NEG);
+
+               EDA_PIN_ADD_COMPONENT(R->pins[0], B->id, SCF_EDA_Battery_POS);
+               EDA_PIN_ADD_COMPONENT(R->pins[1], T->id, SCF_EDA_Transistor_C);
+
+               out->pins[i] = T->pins[SCF_EDA_Transistor_C];
+       }
+
+       return 0;
+}
+
+static int _eda_inst_bit_and_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       EDA_INST_OP3_CHECK()
+
+       scf_dag_node_t* in0 = src0->dag_node;
+       scf_dag_node_t* in1 = src1->dag_node;
+       scf_dag_node_t* out = dst ->dag_node;
+
+       ScfEcomponent*  D0  = NULL;
+       ScfEcomponent*  D1  = NULL;
+       ScfEcomponent*  R   = NULL;
+       ScfEcomponent*  B   = f->ef->components[0];
+
+       int i;
+       int N = eda_variable_size(in0->var);
+
+       EDA_INST_IN_CHECK(in0, N);
+       EDA_INST_IN_CHECK(in1, N);
+
+       in0->n_pins = N;
+       in1->n_pins = N;
+       out->n_pins = N;
+
+       for (i = 0; i < N; i++) {
+
+               EDA_INST_ADD_COMPONENT(f, D0, SCF_EDA_Diode);
+               EDA_INST_ADD_COMPONENT(f, D1, SCF_EDA_Diode);
+               EDA_INST_ADD_COMPONENT(f, R,  SCF_EDA_Resistor);
+
+               in0->pins[i] = D0->pins[SCF_EDA_Diode_NEG];
+               in1->pins[i] = D1->pins[SCF_EDA_Diode_NEG];
+
+               EDA_PIN_ADD_COMPONENT(D0->pins[SCF_EDA_Diode_POS], D1->id, SCF_EDA_Diode_POS);
+               EDA_PIN_ADD_COMPONENT(D0->pins[SCF_EDA_Diode_POS], R ->id, 1);
+
+               EDA_PIN_ADD_COMPONENT(D1->pins[SCF_EDA_Diode_POS], D0->id, SCF_EDA_Diode_POS);
+               EDA_PIN_ADD_COMPONENT(D1->pins[SCF_EDA_Diode_POS], R ->id, 1);
+
+               EDA_PIN_ADD_COMPONENT(R->pins[1], D0->id, SCF_EDA_Diode_POS);
+               EDA_PIN_ADD_COMPONENT(R->pins[1], D1->id, SCF_EDA_Diode_POS);
+               EDA_PIN_ADD_COMPONENT(R->pins[0], B ->id, SCF_EDA_Battery_POS);
+
+               out->pins[i] = R ->pins[1];
+       }
+
+       return 0;
+}
+
+static int _eda_inst_bit_or_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       EDA_INST_OP3_CHECK()
+
+       scf_dag_node_t* in0 = src0->dag_node;
+       scf_dag_node_t* in1 = src1->dag_node;
+       scf_dag_node_t* out = dst ->dag_node;
+
+       ScfEcomponent*  D0  = NULL;
+       ScfEcomponent*  D1  = NULL;
+       ScfEcomponent*  R   = NULL;
+       ScfEcomponent*  B   = f->ef->components[0];
+
+       int i;
+       int N = eda_variable_size(in0->var);
+
+       EDA_INST_IN_CHECK(in0, N);
+       EDA_INST_IN_CHECK(in1, N);
+
+       in0->n_pins = N;
+       in1->n_pins = N;
+       out->n_pins = N;
+
+       for (i = 0; i < N; i++) {
+
+               EDA_INST_ADD_COMPONENT(f, D0, SCF_EDA_Diode);
+               EDA_INST_ADD_COMPONENT(f, D1, SCF_EDA_Diode);
+               EDA_INST_ADD_COMPONENT(f, R,  SCF_EDA_Resistor);
+
+               in0->pins[i] = D0->pins[SCF_EDA_Diode_POS];
+               in1->pins[i] = D1->pins[SCF_EDA_Diode_POS];
+
+               EDA_PIN_ADD_COMPONENT(D0->pins[SCF_EDA_Diode_NEG], D1->id, SCF_EDA_Diode_NEG);
+               EDA_PIN_ADD_COMPONENT(D0->pins[SCF_EDA_Diode_NEG], R ->id, 0);
+
+               EDA_PIN_ADD_COMPONENT(D1->pins[SCF_EDA_Diode_NEG], D0->id, SCF_EDA_Diode_NEG);
+               EDA_PIN_ADD_COMPONENT(D1->pins[SCF_EDA_Diode_NEG], R ->id, 0);
+
+               EDA_PIN_ADD_COMPONENT(R->pins[0], D0->id, SCF_EDA_Diode_NEG);
+               EDA_PIN_ADD_COMPONENT(R->pins[0], D1->id, SCF_EDA_Diode_NEG);
+               EDA_PIN_ADD_COMPONENT(R->pins[1], B ->id, SCF_EDA_Battery_NEG);
+
+               out->pins[i] = R ->pins[0];
+       }
+
+       return 0;
+}
+
+static int _eda_inst_inc_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_inc_post_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_dec_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_dec_post_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_assign_array_index_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->srcs || c->srcs->size != 4)
+               return -EINVAL;
+
+       scf_eda_context_t*  eda    = ctx->priv;
+       scf_function_t*     f      = eda->f;
+
+       scf_3ac_operand_t*  base   = c->srcs->data[0];
+       scf_3ac_operand_t*  index  = c->srcs->data[1];
+       scf_3ac_operand_t*  scale  = c->srcs->data[2];
+       scf_3ac_operand_t*  src    = c->srcs->data[3];
+
+       if (!base || !base->dag_node)
+               return -EINVAL;
+
+       if (!index || !index->dag_node)
+               return -EINVAL;
+
+       if (!scale || !scale->dag_node)
+               return -EINVAL;
+
+       if (!src || !src->dag_node)
+               return -EINVAL;
+
+       return -EINVAL;
+}
+
+static int _eda_inst_and_assign_array_index_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->srcs || c->srcs->size != 4)
+               return -EINVAL;
+
+       scf_eda_context_t*  eda    = ctx->priv;
+       scf_function_t*     f      = eda->f;
+
+       scf_3ac_operand_t*  base   = c->srcs->data[0];
+       scf_3ac_operand_t*  index  = c->srcs->data[1];
+       scf_3ac_operand_t*  scale  = c->srcs->data[2];
+       scf_3ac_operand_t*  src    = c->srcs->data[3];
+
+       if (!base || !base->dag_node)
+               return -EINVAL;
+
+       if (!index || !index->dag_node)
+               return -EINVAL;
+
+       if (!scale || !scale->dag_node)
+               return -EINVAL;
+
+       if (!src || !src->dag_node)
+               return -EINVAL;
+
+       return -EINVAL;
+}
+
+static int _eda_inst_or_assign_array_index_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->srcs || c->srcs->size != 4)
+               return -EINVAL;
+
+       scf_eda_context_t*  eda    = ctx->priv;
+       scf_function_t*     f      = eda->f;
+
+       scf_3ac_operand_t*  base   = c->srcs->data[0];
+       scf_3ac_operand_t*  index  = c->srcs->data[1];
+       scf_3ac_operand_t*  scale  = c->srcs->data[2];
+       scf_3ac_operand_t*  src    = c->srcs->data[3];
+
+       if (!base || !base->dag_node)
+               return -EINVAL;
+
+       if (!index || !index->dag_node)
+               return -EINVAL;
+
+       if (!scale || !scale->dag_node)
+               return -EINVAL;
+
+       if (!src || !src->dag_node)
+               return -EINVAL;
+
+       return -EINVAL;
+}
+
+static int _eda_inst_array_index_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->dsts || c->dsts->size != 1)
+               return -EINVAL;
+
+       if (!c->srcs || c->srcs->size != 3)
+               return -EINVAL;
+
+       scf_eda_context_t*  eda    = ctx->priv;
+       scf_function_t*     f      = eda->f;
+
+       scf_3ac_operand_t*  dst    = c->dsts->data[0];
+       scf_3ac_operand_t*  base   = c->srcs->data[0];
+       scf_3ac_operand_t*  index  = c->srcs->data[1];
+       scf_3ac_operand_t*  scale  = c->srcs->data[2];
+
+       if (!base || !base->dag_node)
+               return -EINVAL;
+
+       if (!index || !index->dag_node)
+               return -EINVAL;
+
+       if (!scale || !scale->dag_node)
+               return -EINVAL;
+
+       if (!c->instructions) {
+               c->instructions = scf_vector_alloc();
+               if (!c->instructions)
+                       return -ENOMEM;
+       }
+
+       scf_variable_t*     vd  = dst  ->dag_node->var;
+       scf_variable_t*     vb  = base ->dag_node->var;
+       scf_variable_t*     vi  = index->dag_node->var;
+       scf_variable_t*     vs  = scale->dag_node->var;
+
+       return -EINVAL;
+}
+
+static int _eda_inst_teq_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_logic_not_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_cmp_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setz_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setnz_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setgt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setge_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setlt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_setle_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_eq_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_ne_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_gt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_ge_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_lt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_le_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_cast_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->dsts || c->dsts->size != 1)
+               return -EINVAL;
+
+       if (!c->srcs || c->srcs->size != 1)
+               return -EINVAL;
+
+       scf_eda_context_t* eda = ctx->priv;
+       scf_function_t*    f   = eda->f;
+       scf_3ac_operand_t* src = c->srcs->data[0];
+       scf_3ac_operand_t* dst = c->dsts->data[0];
+
+       if (!src || !src->dag_node)
+               return -EINVAL;
+
+       if (!dst || !dst->dag_node)
+               return -EINVAL;
+
+       if (0 == dst->dag_node->color)
+               return -EINVAL;
+
+       return -EINVAL;
+}
+
+static int _eda_inst_return_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       if (!c->srcs || c->srcs->size < 1)
+               return -EINVAL;
+
+       scf_eda_context_t*  eda  = ctx->priv;
+       scf_function_t*     f    = eda->f;
+       scf_3ac_operand_t*  src  = NULL;
+       scf_dag_node_t*     out  = NULL;
+
+       int i;
+       int j;
+
+       for (i  = 0; i < c->srcs->size; i++) {
+               src =        c->srcs->data[i];
+
+               out = src->dag_node;
+
+               if (out->n_pins <= 0) {
+                       scf_loge("\n");
+                       return -EINVAL;
+               }
+
+               for (j  = 0; j < out->n_pins; j++)
+                       out->pins[j]->flags |= SCF_EDA_PIN_OUT;
+       }
+
+       return 0;
+}
+
+static int _eda_inst_nop_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_end_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_goto_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jz_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jnz_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jgt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jge_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jlt_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_jle_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return -EINVAL;
+}
+
+static int _eda_inst_load_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_reload_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_save_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_and_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static int _eda_inst_or_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c)
+{
+       return 0;
+}
+
+static eda_inst_handler_t eda_inst_handlers[] =
+{
+       {SCF_OP_ARRAY_INDEX,    _eda_inst_array_index_handler},
+
+       {SCF_OP_TYPE_CAST,      _eda_inst_cast_handler},
+       {SCF_OP_LOGIC_NOT,              _eda_inst_logic_not_handler},
+       {SCF_OP_BIT_NOT,        _eda_inst_bit_not_handler},
+
+       {SCF_OP_INC,            _eda_inst_inc_handler},
+       {SCF_OP_DEC,            _eda_inst_dec_handler},
+
+       {SCF_OP_INC_POST,       _eda_inst_inc_post_handler},
+       {SCF_OP_DEC_POST,       _eda_inst_dec_post_handler},
+
+       {SCF_OP_BIT_AND,        _eda_inst_bit_and_handler},
+       {SCF_OP_BIT_OR,         _eda_inst_bit_or_handler},
+
+       {SCF_OP_3AC_TEQ,        _eda_inst_teq_handler},
+       {SCF_OP_3AC_CMP,        _eda_inst_cmp_handler},
+
+       {SCF_OP_3AC_SETZ,       _eda_inst_setz_handler},
+       {SCF_OP_3AC_SETNZ,      _eda_inst_setnz_handler},
+       {SCF_OP_3AC_SETGT,      _eda_inst_setgt_handler},
+       {SCF_OP_3AC_SETGE,      _eda_inst_setge_handler},
+       {SCF_OP_3AC_SETLT,      _eda_inst_setlt_handler},
+       {SCF_OP_3AC_SETLE,      _eda_inst_setle_handler},
+
+       {SCF_OP_EQ,                     _eda_inst_eq_handler},
+       {SCF_OP_NE,                     _eda_inst_ne_handler},
+       {SCF_OP_GT,                     _eda_inst_gt_handler},
+       {SCF_OP_GE,             _eda_inst_ge_handler},
+       {SCF_OP_LT,             _eda_inst_lt_handler},
+       {SCF_OP_LE,             _eda_inst_le_handler},
+
+       {SCF_OP_ASSIGN,                 _eda_inst_assign_handler},
+
+       {SCF_OP_AND_ASSIGN,     _eda_inst_and_assign_handler},
+       {SCF_OP_OR_ASSIGN,      _eda_inst_or_assign_handler},
+
+       {SCF_OP_RETURN,                 _eda_inst_return_handler},
+       {SCF_OP_GOTO,                   _eda_inst_goto_handler},
+
+       {SCF_OP_3AC_JZ,                 _eda_inst_jz_handler},
+       {SCF_OP_3AC_JNZ,                _eda_inst_jnz_handler},
+       {SCF_OP_3AC_JGT,                _eda_inst_jgt_handler},
+       {SCF_OP_3AC_JGE,                _eda_inst_jge_handler},
+       {SCF_OP_3AC_JLT,                _eda_inst_jlt_handler},
+       {SCF_OP_3AC_JLE,                _eda_inst_jle_handler},
+
+       {SCF_OP_3AC_NOP,                _eda_inst_nop_handler},
+       {SCF_OP_3AC_END,                _eda_inst_end_handler},
+
+       {SCF_OP_3AC_SAVE,       _eda_inst_save_handler},
+       {SCF_OP_3AC_LOAD,       _eda_inst_load_handler},
+
+       {SCF_OP_3AC_RESAVE,     _eda_inst_save_handler},
+       {SCF_OP_3AC_RELOAD,     _eda_inst_reload_handler},
+
+       {SCF_OP_3AC_ASSIGN_ARRAY_INDEX,     _eda_inst_assign_array_index_handler},
+       {SCF_OP_3AC_AND_ASSIGN_ARRAY_INDEX, _eda_inst_and_assign_array_index_handler},
+       {SCF_OP_3AC_OR_ASSIGN_ARRAY_INDEX,  _eda_inst_or_assign_array_index_handler},
+};
+
+eda_inst_handler_t* scf_eda_find_inst_handler(const int op_type)
+{
+       int i;
+       for (i = 0; i < sizeof(eda_inst_handlers) / sizeof(eda_inst_handlers[0]); i++) {
+
+               eda_inst_handler_t* h = &(eda_inst_handlers[i]);
+
+               if (op_type == h->type)
+                       return h;
+       }
+       return NULL;
+}
diff --git a/scf_vector.h b/scf_vector.h
new file mode 100644 (file)
index 0000000..d4249ca
--- /dev/null
@@ -0,0 +1,198 @@
+#ifndef SCF_VECTOR_H
+#define SCF_VECTOR_H
+
+#include"scf_def.h"
+
+typedef struct {
+       int             capacity;
+       int             size;
+       void**  data;
+} scf_vector_t;
+
+#undef  NB_MEMBER_INC
+#define NB_MEMBER_INC  16
+
+static inline scf_vector_t* scf_vector_alloc()
+{
+       scf_vector_t* v = calloc(1, sizeof(scf_vector_t));
+       if (!v)
+               return NULL;
+
+       v->data = calloc(NB_MEMBER_INC, sizeof(void*));
+       if (!v->data) {
+               free(v);
+               v = NULL;
+               return NULL;
+       }
+
+       v->capacity = NB_MEMBER_INC;
+       return v;
+}
+
+static inline scf_vector_t* scf_vector_clone(scf_vector_t* origin)
+{
+       scf_vector_t* clone = calloc(1, sizeof(scf_vector_t));
+       if (!clone)
+               return NULL;
+
+       clone->data = calloc(origin->capacity, sizeof(void*));
+       if (!clone->data) {
+               free(clone);
+               clone = NULL;
+               return NULL;
+       }
+
+       clone->capacity = origin->capacity;
+       clone->size             = origin->size;
+       memcpy(clone->data, origin->data, origin->size * sizeof(void*));
+       return clone;
+}
+
+static inline int scf_vector_cat(scf_vector_t* dst, scf_vector_t* src)
+{
+       if (!dst || !src)
+               return -EINVAL;
+
+       int size = dst->size + src->size;
+       if (size > dst->capacity) {
+
+               void* p = realloc(dst->data, sizeof(void*) * (size + NB_MEMBER_INC));
+               if (!p)
+                       return -ENOMEM;
+
+               dst->data = p;
+               dst->capacity = size + NB_MEMBER_INC;
+       }
+
+       memcpy(dst->data + dst->size * sizeof(void*), src->data, src->size * sizeof(void*));
+       dst->size += src->size;
+       return 0;
+}
+
+static inline int scf_vector_add(scf_vector_t* v, void* node)
+{
+       if (!v || !v->data)
+               return -EINVAL;
+
+       assert(v->size <= v->capacity);
+
+       if (v->size == v->capacity) {
+               void* p = realloc(v->data, sizeof(void*) * (v->capacity + NB_MEMBER_INC));
+               if (!p)
+                       return -ENOMEM;
+
+               v->data = p;
+               v->capacity += NB_MEMBER_INC;
+       }
+
+       v->data[v->size++] = node;
+       return 0;
+}
+
+static inline int scf_vector_del(scf_vector_t* v, void* node)
+{
+       if (!v || !v->data)
+               return -EINVAL;
+
+       assert(v->size <= v->capacity);
+
+       int i;
+       for (i = 0; i < v->size; i++) {
+
+               if (v->data[i] != node)
+                       continue;
+
+               int j;
+               for (j = i + 1; j < v->size; j++)
+                       v->data[j - 1] = v->data[j];
+
+               v->size--;
+
+               if (v->size + NB_MEMBER_INC * 2 < v->capacity) {
+                       void* p = realloc(v->data, sizeof(void*) * (v->capacity - NB_MEMBER_INC));
+                       if (p) {
+                               v->data = p;
+                               v->capacity -= NB_MEMBER_INC;
+                       }
+               }
+               return 0;
+       }
+
+       return -1;
+}
+
+static inline void* scf_vector_find(const scf_vector_t* v, const void* node)
+{
+       int i;
+       for (i = 0; i < v->size; i++) {
+               if (node == v->data[i])
+                       return v->data[i];
+       }
+       return NULL;
+}
+
+static inline int scf_vector_add_unique(scf_vector_t* v, void* node)
+{
+       if (!scf_vector_find(v, node))
+               return scf_vector_add(v, node);
+       return 0;
+}
+
+static inline void* scf_vector_find_cmp(const scf_vector_t* v, const void* node, int (*cmp)(const void*, const void*))
+{
+       int i;
+       for (i = 0; i < v->size; i++) {
+               if (0 == cmp(node, v->data[i]))
+                       return v->data[i];
+       }
+       return NULL;
+}
+
+static inline int scf_vector_qsort(const scf_vector_t* v, int (*cmp)(const void*, const void*))
+{
+       if (!v || !v->data || 0 == v->size || !cmp)
+               return -EINVAL;
+
+       qsort(v->data, v->size, sizeof(void*), cmp);
+       return 0;
+}
+
+static inline void scf_vector_clear(scf_vector_t* v, void (*type_free)(void*))
+{
+       if (!v || !v->data)
+               return;
+
+       if (type_free) {
+               int i;
+               for (i = 0; i < v->size; i++) {
+                       if (v->data[i]) {
+                               type_free(v->data[i]);
+                               v->data[i] = NULL;
+                       }
+               }
+       }
+
+       v->size = 0;
+
+       if (v->capacity > NB_MEMBER_INC) {
+               void* p = realloc(v->data, sizeof(void*) * NB_MEMBER_INC);
+               if (p) {
+                       v->data = p;
+                       v->capacity = NB_MEMBER_INC;
+               }
+       }
+}
+
+static inline void scf_vector_free(scf_vector_t* v)
+{
+       if (v) {
+               if (v->data)
+                       free(v->data);
+
+               free(v);
+               v = NULL;
+       }
+}
+
+#endif
+
diff --git a/ses_layout.c b/ses_layout.c
new file mode 100644 (file)
index 0000000..a802c5f
--- /dev/null
@@ -0,0 +1,87 @@
+#include"ses_layout.h"
+
+int epin_cmp(const void* v0, const void* v1)
+{
+       const uint64_t* t0 = v0;
+       const uint64_t* t1 = v1;
+
+       if (t0[0] < t1[0])
+               return -1;
+
+       if (t0[0] > t1[0])
+               return 1;
+
+       if (t0[1] < t1[1])
+               return -1;
+
+       if (t0[1] > t1[1])
+               return 1;
+
+       return 0;
+}
+
+int ses_pins_same_line(ScfEfunction* f, scf_vector_t* lines)
+{
+       ScfEcomponent* c;
+       ScfEline*      l;
+       ScfEpin*       p;
+       ScfEpin*       p2;
+
+       scf_vector_t*  vec;
+
+       size_t i;
+       size_t j;
+       size_t k;
+       size_t m;
+
+       for (i = 0; i < f->n_components; i++) {
+               c  =        f->components[i];
+
+               for (j = 0; j < c->n_pins; j++) {
+                       p  =        c->pins[j];
+
+                       qsort(p->tos, p->n_tos / 2, sizeof(uint64_t) * 2, epin_cmp);
+
+                       for (k = 0; k < lines->size; k++) {
+                               vec       = lines->data[k];
+
+                               for (m = 0; m < vec->size; m++) {
+                                       p2 =        vec->data[m];
+
+                                       if (p2->i)
+
+                               }
+                       }
+               }
+       }
+
+       return 0;
+}
+
+int ses_layout_board(ScfEboard* b, int x, int y, int w, int h)
+{
+       ScfEcomponent* c;
+       ScfEfunction*  f;
+       ScfEline*      l;
+       ScfEpin*       p;
+
+       scf_vector_t*  lines;
+
+       size_t i;
+       size_t j;
+       size_t k;
+
+       lines = scf_vector_alloc();
+       if (!lines)
+               return -ENOMEM;
+
+       for (i = 0; i < b->n_functions; i++) {
+               f  =        b->functions[i];
+
+               printf("f: %s\n", f->name);
+
+
+       }
+
+       return 0;
+}
diff --git a/ses_layout.h b/ses_layout.h
new file mode 100644 (file)
index 0000000..6dd16d7
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef SES_LAYOUT_H
+#define SES_LAYOUT_H
+
+#include"scf_eda_pb.h"
+
+int ses_layout_board(ScfEboard* b, int x, int y, int w, int h);
+
+#endif