Change ptype to rtype, make new REF basic type ORed with PTR, FUNC or ARRAY
authorNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 09:50:09 +0000 (20:50 +1100)
committerNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 09:50:09 +0000 (20:50 +1100)
c01.c
c02.c
c03.c
c04.c
ccom.h
test/n.sh [new file with mode: 0755]

diff --git a/c01.c b/c01.c
index 7ea6e53..d82d82e 100644 (file)
--- a/c01.c
+++ b/c01.c
@@ -600,9 +600,9 @@ void error0(s, va_alist) char *s; va_dcl
  switch (type & XTYPE) {
  case PTR:
   fprintf(stderr, "ptr(");
-#define pt (*(struct ptype **)&t)
-  pt = (struct ptype *)Tblock(sizeof(struct ptype));
-  pt->pt_id = PTR;
+#define rt (*(struct rtype **)&t)
+  rt = (struct rtype *)Tblock(sizeof(struct rtype));
+  rt->rt_id = REF | PTR;
 reft:
 
   goto reft;
@@ -611,7 +611,7 @@ reft:
   fprintf(stderr, "func(");
 #define ft (*(struct ftype **)&t)
   ft = (struct ftype *)Tblock(sizeof(struct ftype));
-  ft->ft_id = FUNC;
+  ft->ft_id = REF | FUNC;
   ft->ft_arglist = NULL;
   goto reft;
 #undef ft
@@ -622,14 +622,14 @@ reft:
    fprintf(stderr, "array(?, ");
 #define at (*(struct atype **)&t)
   at = (struct atype *)Tblock(sizeof(struct atype));
-  at->at_id = PTR;
+  at->at_id = REF | PTR;
   at->at_nelt = *dimp ? *(*dimp)++ : 0;
 #undef at
  reft:
-#define pt ((struct ptype *)t)
-  pt->pt_reftype = newtype((type >> TYLEN & ~TYPE) | (type & TYPE), dimp, strp);
+#define rt ((struct rtype *)t)
+  rt->rt_reftype = newtype((type >> TYLEN & ~TYPE) | (type & TYPE), dimp, strp);
   break;
-#undef pt
+#undef rt
  default:
   fprintf(stderr, "basic(%d", type);
   if (type == STRUCT) {
diff --git a/c02.c b/c02.c
index 154b19a..d278d72 100644 (file)
--- a/c02.c
+++ b/c02.c
@@ -44,7 +44,7 @@ void extdef() {
                if ((ds=defsym)==0)
                        return;
                funcsym = ds;
-               if (/*(ds->nl_type&XTYPE)*/ds->nl_dtype->t_id==FUNC) {
+               if (/*(ds->nl_type&XTYPE)==FUNC*/ds->nl_dtype->t_id==(REF|FUNC)) {
                        if ((peeksym=symbol())==LBRACE || peeksym==KEYW
                         || (peeksym==NAME && csym->nl_class==TYPEDEF)) {
  /* create a fake local variable of same type as function's return type */
diff --git a/c03.c b/c03.c
index 21e4acb..4828cf9 100644 (file)
--- a/c03.c
+++ b/c03.c
@@ -350,16 +350,16 @@ int declare(askw, tptr, offset) int askw; struct /*nmlist*/type *tptr; int offse
 static int oldtype(dtype, dimp, strp) struct type *dtype; struct tdim *dimp; struct SS **strp; {
  int type;
  switch (dtype->t_id) {
- case PTR:
+ case REF | PTR:
   /*fprintf(stderr, "ptr(");*/
-  type = oldtype(((struct ptype *)dtype)->pt_reftype, dimp, strp);
+  type = oldtype(((struct rtype *)dtype)->rt_reftype, dimp, strp);
   if (type & BIGTYPE) {
    typov();
    type = 0;
   }
   type = (type & TYPE) | (type & ~TYPE) << TYLEN | PTR;
   break;
- case FUNC:
+ case REF | FUNC:
   /*fprintf(stderr, "func(");*/
   type = oldtype(((struct ftype *)dtype)->ft_reftype, dimp, strp);
   if (type & BIGTYPE) {
@@ -368,7 +368,7 @@ static int oldtype(dtype, dimp, strp) struct type *dtype; struct tdim *dimp; str
   }
   type = (type & TYPE) | (type & ~TYPE) << TYLEN | FUNC;
   break;
- case ARRAY:
+ case REF | ARRAY:
   /*fprintf(stderr, "array(%d, ", ((struct atype *)dtype)->at_nelt);*/
   if (dimp->rank>=5) {
    error0("Rank too large");
@@ -396,10 +396,10 @@ static int oldtype(dtype, dimp, strp) struct type *dtype; struct tdim *dimp; str
 }
 static int eqtype(struct type *t0, struct type *t1) {
  while (t0->t_id == t1->t_id) {
-  if (t0->t_id < PTR)
+  if ((t0->t_id & TYPE) != REF)
    return 1;
-  t0 = ((struct ptype *)t0)->pt_reftype;
-  t1 = ((struct ptype *)t1)->pt_reftype;
+  t0 = ((struct rtype *)t0)->rt_reftype;
+  t1 = ((struct rtype *)t1)->rt_reftype;
  }
  return 0;
 }
@@ -460,7 +460,7 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                t1 >>= TYLEN;
        }
        type |= tptr->nl_type&TYPE;*/
-       if (/*(type&XTYPE)*/dtype->t_id == FUNC) {
+       if (/*(type&XTYPE) == FUNC*/dtype->t_id == (REF|FUNC)) {
                if (skw==AUTO)
                        skw = EXTERN;
                if ((skw!=EXTERN && skw!=TYPEDEF) && absname==NULL)
@@ -501,7 +501,7 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                 */
                if (dsym->/*nl_subsp*/nl_dtype) {
                        for (/*a=0*/dt = dtype, /*t1 = dsym->nl_type*/dt1 = dsym->nl_dtype;
-                           /*a<dim.rank*/dt->t_id==ARRAY && /*(t1&XTYPE)*/dt1->t_id==ARRAY;
+                           /*a<dim.rank*/dt->t_id==(REF|ARRAY) && /*(t1&XTYPE)==ARRAY*/dt1->t_id==(REF|ARRAY);
                            /*a++*/dt=((struct atype *)dt)->at_reftype, /*t1 >>= TYLEN*/dt1=((struct atype *)dt1)->at_reftype)
 #define adt ((struct atype *)dt)
 #define adt1 ((struct atype *)dt1)
@@ -516,7 +516,7 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                                        redec();
 #undef adt
 #undef adt1
-                       if (/*a<dim.rank*/dt->t_id==ARRAY || /*(t1&XTYPE)*/dt1->t_id==ARRAY)
+                       if (/*a<dim.rank*/dt->t_id==(REF|ARRAY) || /*(t1&XTYPE)==ARRAY*/dt1->t_id==(REF|ARRAY))
                                redec();
                } /*else {
                        dp = (int *)Dblock(dim.rank*sizeof(dim.rank));
@@ -536,9 +536,9 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                        error0("structure redeclaration");
                }*/
  if (dsym->nl_class) {
-  for (dt = dtype; dt->t_id >= PTR; dt = ((struct ptype *)dt)->pt_reftype)
+  for (dt = dtype; (dt->t_id & TYPE) == REF; dt = ((struct rtype *)dt)->rt_reftype)
    ;
-  for (dt1 = dsym->nl_dtype; dt1->t_id >= PTR; dt1 = ((struct ptype *)dt1)->pt_reftype)
+  for (dt1 = dsym->nl_dtype; (dt1->t_id & TYPE) == REF; dt1 = ((struct rtype *)dt1)->rt_reftype)
    ;
   if (dt->t_id == STRUCT && dt1->t_id == STRUCT && dt != dt1)
    error0("structure redeclaration");
@@ -779,7 +779,7 @@ syntax:
 }*/
 static struct type *getail(dtype, nullok) struct type *dtype; int *nullok; {
        static struct type argtype = {INT};
-       struct ptype *pt;
+       struct rtype *rt;
        register int o;
        register struct nmlist *ds;
 
@@ -795,9 +795,9 @@ static struct type *getail(dtype, nullok) struct type *dtype; int *nullok; {
                } else
                        if ((o=symbol()) != RPARN)
                                goto syntax;
-#define ft (*(struct ftype **)&pt)
+#define ft (*(struct ftype **)&rt)
                ft = (struct ftype *)Dblock(sizeof(struct ftype));
-               ft->ft_id = FUNC;
+               ft->ft_id = REF | FUNC;
                ft->ft_arglist = NULL;
                goto gett;
 #undef ft
@@ -817,14 +817,14 @@ static struct type *getail(dtype, nullok) struct type *dtype; int *nullok; {
                        cval = 0;
                }
                *nullok = 0;
-#define at (*(struct atype **)&pt)
+#define at (*(struct atype **)&rt)
                at = (struct atype *)Dblock(sizeof(struct atype));
-               at->at_id = ARRAY;
+               at->at_id = REF | ARRAY;
                at->at_nelt = cval;
 #undef at
        gett:
-               pt->pt_reftype = getail(dtype, nullok);
-               return (struct type *)pt;
+               rt->rt_reftype = getail(dtype, nullok);
+               return (struct type *)rt;
 
        default:
                peeksym = o;
@@ -835,7 +835,7 @@ syntax:
        return(NULL);
 }
 struct type *getype(dtype, nullok, anonok) struct type *dtype; int *nullok; int anonok; {
-       struct type *t;
+       struct rtype *rt;
        register int o;
 
        defsym = 0;
@@ -843,13 +843,11 @@ getp:
        switch(o=symbol()) {
 
        case TIMES:
-#define pt (*(struct ptype **)&t)
-               pt = (struct ptype *)Dblock(sizeof(struct ptype));
-               pt->pt_id = PTR;
-               pt->pt_reftype = dtype;
-               dtype = (struct type *)pt;
+               rt = (struct rtype *)Dblock(sizeof(struct rtype));
+               rt->rt_id = REF | PTR;
+               rt->rt_reftype = dtype;
+               dtype = (struct type *)rt;
                goto getp;
-#undef pt
 
        case LPARN:
                if (!anonok || nextchar()!=')') {
@@ -895,7 +893,7 @@ int align(type, offset, aflen) /*int*/struct type *type; int offset; int aflen;
 
        flen = aflen;
        a = offset;
-       t = type;
+ /*    t = type;*/
        ftl = "Field too long";
        if (flen==0) {
                a += (NBPC+bitoffs-1) / NBPC;
@@ -903,8 +901,8 @@ int align(type, offset, aflen) /*int*/struct type *type; int offset; int aflen;
        }
  /*    while ((t&XTYPE)==ARRAY)
                t = decref0(t);*/
while (t->t_id == ARRAY)
-  t = ((struct atype *)t)->at_reftype;
for (t = type; t->t_id == (REF | ARRAY); t = ((struct atype *)t)->at_reftype)
+   ;
        if (/*t*/t->t_id!=CHAR && /*t*/t->t_id!=UNCHAR) {
                a = (a+ALIGN) & ~ALIGN;
                if (a>offset)
diff --git a/c04.c b/c04.c
index ee1616c..f06de54 100644 (file)
--- a/c04.c
+++ b/c04.c
@@ -285,14 +285,14 @@ int length(cs) struct /*node*/type *cs; {
                n *= cs->n_subsp[nd++];
        }*/
  n = 1;
- for (t = cs; t->t_id == ARRAY; t = ((struct atype *)t)->at_reftype)
+ for (t = cs; t->t_id == (REF | ARRAY); t = ((struct atype *)t)->at_reftype)
   n *= ((struct atype *)t)->at_nelt;
-       if (/*(t&~TYPE)*/t->t_id==FUNC)
+       if (/*(t&~TYPE)==FUNC*/t->t_id==(REF|FUNC))
                return(0);
  /*    if (t>=PTR)
                elsz = SZPTR;
        else*/ switch(/*t&TYPE*/t->t_id) {
- case PTR:
+ case REF | PTR:
   elsz = SZPTR;
   break;
 
diff --git a/ccom.h b/ccom.h
index c79d1fe..b1c0093 100644 (file)
--- a/ccom.h
+++ b/ccom.h
@@ -86,7 +86,8 @@ struct        tdim {
  *   UNCHAR    8
  *   UNLONG    9
  *   VOID      10
- *   PTR       020     (means it's a struct ptype)
+ *   REF       11      (used with PTR, FUNC or ARRAY)
+ *   PTR       020     (means it's a struct rtype)
  *   FUNC      040     (means it's a struct ftype)
  *   ARRAY     060     (means it's a struct atype)
  */
@@ -100,21 +101,21 @@ struct    stype {
 #define st_ssize st_S.ssize
 #define st_memlist st_S.memlist
 };
-struct ptype {
-       struct type pt_type;
-#define pt_id pt_type.t_id
-       struct type *pt_reftype;        /* referenced type */
+struct rtype {
+       struct type rt_type;
+#define rt_id rt_type.t_id
+       struct type *rt_reftype;        /* referenced type */
 };
 struct ftype {
-       struct ptype ft_ptype;
-#define ft_id ft_ptype.pt_id
-#define ft_reftype ft_ptype.pt_reftype
+       struct rtype ft_rtype;
+#define ft_id ft_rtype.rt_id
+#define ft_reftype ft_rtype.rt_reftype
        struct nmlist **ft_arglist;     /* argument list */
 };
 struct atype {
-       struct ptype at_ptype;
-#define at_id at_ptype.pt_id
-#define at_reftype at_ptype.pt_reftype
+       struct rtype at_rtype;
+#define at_id at_rtype.rt_id
+#define at_reftype at_rtype.rt_reftype
        int     at_nelt;                /* number of elements */
 };
 
@@ -457,6 +458,7 @@ struct swtab {
 #define        UNCHAR  8
 #define        UNLONG  9
 #define        VOID    10
+#define REF    11
 
 #define        ALIGN   01              /* pass 0 only */
 #define        TYPE    017
diff --git a/test/n.sh b/test/n.sh
new file mode 100755 (executable)
index 0000000..0978243
--- /dev/null
+++ b/test/n.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+for i in *.c
+do
+  ../ccom <$i >x
+  diff --unified x `basename $i .c`.s
+done