[hackers] [scc] [cc1] Remove the circular double list of types || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 13 Dec 2016 17:59:10 +0100 (CET)

commit 7aa488d64698f63f750d0415c7887d47c8510614
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Dec 13 17:41:54 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Dec 13 17:46:58 2016 +0100

    [cc1] Remove the circular double list of types
    
    We can apply the same trick we are applying in cc2
    and remove the types directly from the hash, because
    we do know that it is impossible to have some global
    type declared after a local type, and since all
    the types are pushed in a stack, poping them
    gitves the correct order.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 5caf6bb..9cd4e7a 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -293,7 +293,7 @@ struct type {
                 TINT elem; /* number of type parameters */
         } n;
         Type *next; /* local list pointer */
- Type *h_prev, *h_next; /* hash pointers */
+ Type *h_next; /* hash collision list */
 };
 
 struct symbol {
_AT_@ -358,7 +358,6 @@ extern Type *duptype(Type *base);
 extern struct limits *getlimits(Type *tp);
 extern void typesize(Type *tp);
 extern void flushtypes(void);
-extern void itypes(void);
 
 /* symbol.c */
 extern void dumpstab(char *msg);
diff --git a/cc1/types.c b/cc1/types.c
index c2c8c30..3915a16 100644
--- a/cc1/types.c
+++ b/cc1/types.c
_AT_@ -10,6 +10,9 @@ static char sccsid[] = "@(#) ./cc1/types.c";
 #include "cc1.h"
 
 #define NR_TYPE_HASH 16
+#define HASH(t) (((t)->op ^ (uintptr_t) (t)->type>>3) & NR_TYPE_HASH-1)
+
+static Type *typetab[NR_TYPE_HASH], *localtypes;
 
 /* FIXME:
  * Compiler can generate warnings here if the ranges of TINT,
_AT_@ -72,19 +75,6 @@ static struct limits limits[][4] = {
         }
 };
 
-static Type typetab[NR_TYPE_HASH], *localtypes;
-
-void
-itypes()
-{
- Type *tp;
-
- for (tp = typetab; tp < &typetab[NR_TYPE_HASH]; ++tp) {
- tp->h_next = tp;
- tp->h_prev = tp;
- }
-}
-
 struct limits *
 getlimits(Type *tp)
 {
_AT_@ -260,22 +250,23 @@ newtype(Type *base)
         tp = xmalloc(sizeof(*tp));
         *tp = *base;
         tp->id = newid();
+
         if (curctx > GLOBALCTX+1) {
                 /* it is a type defined in the body of a function */
                 tp->next = localtypes;
                 localtypes = tp;
         }
- if (tp->prop & TDEFINED)
+ if (tp->prop & TDEFINED) {
                 typesize(tp);
- tp->h_next = tp->h_prev = tp;
+ emit(OTYP, tp);
+ }
         return tp;
 }
 
 Type *
 mktype(Type *tp, int op, TINT nelem, Type *pars[])
 {
- Type *tbl, *h_next, type;
- unsigned t;
+ Type **tbl, type;
         Type *bp;
 
         if (op == PTR && tp == voidtype)
_AT_@ -284,10 +275,8 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[])
         memset(&type, 0, sizeof(type));
         type.type = tp;
         type.op = op;
- type.prop = 0;
         type.p.pars = pars;
         type.n.elem = nelem;
- type.ns = 0;
 
         switch (op) {
         case ARY:
_AT_@ -324,9 +313,8 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[])
                 abort();
         }
 
- t = (type.op ^ (uintptr_t) tp>>3) & NR_TYPE_HASH-1;
- tbl = &typetab[t];
- for (bp = tbl; bp->h_next != tbl; bp = bp->h_next) {
+ tbl = &typetab[HASH(&type)];
+ for (bp = *tbl; bp; bp = bp->h_next) {
                 if (eqtype(bp, &type, 0)) {
                         /*
                          * pars was allocated by the caller
_AT_@ -339,11 +327,8 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[])
         }
 
         bp = newtype(&type);
- h_next = tbl->h_next;
- bp->h_next = h_next;
- bp->h_prev = h_next->h_prev;
- h_next->h_prev = bp;
- tbl->h_next = bp;
+ bp->h_next = *tbl;
+ *tbl = bp;
 
         return bp;
 }
_AT_@ -398,9 +383,22 @@ flushtypes(void)
 
         for (tp = localtypes; tp; tp = next) {
                 next = tp->next;
- tp->h_prev->h_next = tp->h_next;
- tp->h_next->h_prev = tp->h_prev;
- free(tp);
+ switch (tp->op) {
+ default:
+ /*
+ * All the local types are linked after
+ * global types, and since we are
+ * unlinking them in the inverse order
+ * we do know that tp is always the head
+ * of the collision list
+ */
+ typetab[HASH(tp)] = tp->h_next;
+ case STRUCT:
+ case UNION:
+ case ENUM:
+ free(tp);
+ break;
+ }
         }
         localtypes = NULL;
 }
Received on Tue Dec 13 2016 - 17:59:10 CET

This archive was generated by hypermail 2.3.0 : Tue Dec 13 2016 - 18:00:35 CET