Implement a new struct btype (inherits from struct type) with id BITFLD, to contain...
authorNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 14:07:05 +0000 (01:07 +1100)
committerNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 14:08:18 +0000 (01:08 +1100)
c0.h
c01.c
c02.c
c03.c
ccom.h
test/n.sh

diff --git a/c0.h b/c0.h
index fbc8304..503171b 100644 (file)
--- a/c0.h
+++ b/c0.h
@@ -173,7 +173,7 @@ extern int mossym;
 #define        FENUM   03
 #define        FUNION  04
 #define        FKIND   07
-#define        FFIELD  020
+/*#define      FFIELD  020*/
 #define        FINIT   040
 #define        FLABL   0100
 
diff --git a/c01.c b/c01.c
index f940721..f588ed3 100644 (file)
--- a/c01.c
+++ b/c01.c
@@ -189,7 +189,7 @@ void build(op) int op; {
 #define np2 ((struct nnode *)p2)
                structident(p1, np2);
                t2 = np2->nn_type;
-               if (t2==INT && np2->nn_nmlist->nl_flag&FFIELD)
+               if (t2==INT && np2->nn_nmlist->/*nl_flag&FFIELD*/nl_dtype->t_id==BITFLD)
                        t2 = UNSIGN;
                t = incref0(t2);
                chkw(p1, -1);
@@ -197,13 +197,13 @@ void build(op) int op; {
                *cp++ = (struct node *)block(PLUS, t, np2->nn_subsp, np2->nn_strp,
                   p1, (struct node *)cblock(np2->nn_nmlist->nl_offset));
                build(STAR);
-               if (np2->nn_nmlist->nl_flag&FFIELD)
+               if (np2->nn_nmlist->/*nl_flag&FFIELD*/nl_dtype->t_id==BITFLD)
 #if 1
                        cp[-1] = (struct node *)block(FSEL, UNSIGN, (int *)NULL, (union str *)NULL, 
-                           cp[-1], (struct node *)np2->nn_nmlist->nl_strp);
+                           cp[-1], (struct node *)/*np2->nn_nmlist->nl_strp*/&((struct btype *)np2->nn_nmlist->nl_dtype)->bt_F);
 #else
                        *cp++ = (struct node *)block(FSEL, UNSIGN, (int *)NULL, (union str *)NULL, 
-                           *--cp, (struct node *)np2->nn_nmlist->nl_strp);
+                           *--cp, (struct node *)/*np2->nn_nmlist->nl_strp*/&((struct btype *)np2->nn_nmlist->nl_dtype)->bt_F);
 #endif
                return;
 #undef np2
@@ -397,6 +397,7 @@ void structident(p1, p2) register struct node *p1; register struct nnode *p2; {
        np = nporig = p2->nn_nmlist;
        for (;;) {
                if (namesame && p1->n_type==STRUCT+PTR && p1->n_strp == np->nl_sparent) {
+ /*fprintf(stderr, "found %p type %x strp %p\n", np, np->nl_type, np->nl_strp);*/
                        p2->nn_type = np->nl_type;
                        p2->nn_strp = np->nl_strp;
                        p2->nn_subsp = np->nl_subsp;
diff --git a/c02.c b/c02.c
index d278d72..f9208fc 100644 (file)
--- a/c02.c
+++ b/c02.c
@@ -192,7 +192,7 @@ int cinit(anp, flex, sclass) struct nmlist *anp; int flex; int sclass; {
                        st = starttree();
                        s = tree(/*0*/);
                        initflg = 0;
-                       if (np.nl_flag&FFIELD)
+                       if (np./*nl_flag&FFIELD*/nl_dtype->t_id==BITFLD)
                                error0("No field initialization");
                        *cp++ = (struct node *)nblock(&np);
                        *cp++ = s;
diff --git a/c03.c b/c03.c
index 7b8c4f1..6654800 100644 (file)
--- a/c03.c
+++ b/c03.c
@@ -217,10 +217,10 @@ int declist(sclass) int sclass; {
  /*                    ssym->nl_strp = (union str *)Dblock(sizeof(struct SS));*/
  stype = (struct stype *)Dblock(sizeof(struct stype));
  stype->st_id = STRUCT;
- ssym->nl_strp = (union str *)&stype->st_S;
- ssym->nl_dtype = (struct type *)stype;
                        /*ssym->nl_strp->S.ssize*/stype->st_ssize = 0;
                        /*ssym->nl_strp->S.memlist*/stype->st_memlist = NULL;
+ ssym->nl_strp = (union str *)&stype->st_S;
+ ssym->nl_dtype = (struct type *)stype;
                }
  /*            strp = ssym->nl_strp;*/
  else
@@ -596,22 +596,35 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
        }
        elsize = 0;
        if (skw==MOS) {
+ /*fprintf(stderr, "mos %p name %s\n", dsym, dsym->nl_name);*/
                elsize = length(/*(struct node *)dsym*/dtype);
                if ((peeksym = symbol())==COLON) {
                        elsize = 0;
                        peeksym = -1;
                        t1 = conexp();
                        a = align(/*type*/dtype, offset, t1);
-                       if (dsym->nl_flag&FFIELD) {
-                               if (dsym->nl_strp->F.bitoffs!=bitoffs
-                                || dsym->nl_strp->F.flen!=t1)
+ /*fprintf(stderr, "a bitfield strp %p\n", dsym->nl_strp);*/
+                       if (/*dsym->nl_flag&FFIELD*/dtype->t_id == BITFLD) {
+#define bdtype ((struct btype *)dtype)
+                               if (/*dsym->nl_strp->F.bitoffs*/bdtype->bt_bitoffs!=bitoffs
+                                || /*dsym->nl_strp->F.flen*/bdtype->bt_flen!=t1)
                                        redec();
+#undef bdtype
                        } else {
-                               dsym->nl_strp = (union str *)Dblock(sizeof(struct FS));
+ /*                            dsym->nl_strp = (union str *)Dblock(sizeof(struct FS));*/
+#define bdtype (*(struct btype **)&dtype)
+ bdtype = (struct btype *)Dblock(sizeof(struct btype));
+ bdtype->bt_id = BITFLD;
+ bdtype->bt_flen = t1;
+ bdtype->bt_bitoffs = bitoffs;
+ dsym->nl_strp = (union str *)&bdtype->bt_F;
+ dsym->nl_dtype = (struct type *)bdtype;
+#undef bdtype
                        }
-                       dsym->nl_flag |= FFIELD;
+ /*fprintf(stderr, "b bitfield strp %p\n", dsym->nl_strp);*/
+ /*                    dsym->nl_flag |= FFIELD;
                        dsym->nl_strp->F.bitoffs = bitoffs;
-                       dsym->nl_strp->F.flen = t1;
+                       dsym->nl_strp->F.flen = t1;*/
                        bitoffs += t1;
                } else
                        a = align(/*type*/dtype, offset, 0);
@@ -912,6 +925,8 @@ void typov() {
  * Enforce alignment restrictions in structures,
  * including bit-field considerations.
  */
+/* really means make sure aflen bits can be allocated after current position */
+/* takes size in bytes and the global bitoffs, returns size increase in bytes */
 int align(type, offset, aflen) /*int*/struct type *type; int offset; int aflen; {
        register int a, /*t,*/ flen;
  register struct type *t;
diff --git a/ccom.h b/ccom.h
index b1c0093..9f5b8a9 100644 (file)
--- a/ccom.h
+++ b/ccom.h
@@ -87,6 +87,7 @@ struct        tdim {
  *   UNLONG    9
  *   VOID      10
  *   REF       11      (used with PTR, FUNC or ARRAY)
+ *   BITFLD    12      (means it's a struct btype)
  *   PTR       020     (means it's a struct rtype)
  *   FUNC      040     (means it's a struct ftype)
  *   ARRAY     060     (means it's a struct atype)
@@ -97,10 +98,17 @@ struct      type {
 struct stype {
        struct type st_type;
 #define st_id st_type.t_id
-       struct SS st_S;
+       struct SS st_S;                 /* structure specification */
 #define st_ssize st_S.ssize
 #define st_memlist st_S.memlist
 };
+struct btype {
+       struct type bt_type;
+#define bt_id bt_type.t_id
+       struct FS bt_F;                 /* bitfield specification */
+#define bt_flen bt_F.flen
+#define bt_bitoffs bt_F.bitoffs
+};
 struct rtype {
        struct type rt_type;
 #define rt_id rt_type.t_id
@@ -459,6 +467,7 @@ struct swtab {
 #define        UNLONG  9
 #define        VOID    10
 #define REF    11
+#define BITFLD 12
 
 #define        ALIGN   01              /* pass 0 only */
 #define        TYPE    017
index 0978243..f6a9c57 100755 (executable)
--- a/test/n.sh
+++ b/test/n.sh
@@ -1,6 +1,7 @@
 #!/bin/sh
 for i in *.c
 do
+  echo $i
   ../ccom <$i >x
   diff --unified x `basename $i .c`.s
 done