scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 662f5dda9ab4c51c2339dcde074ab76b5c49afb0
parent 2051da00e5d1a5232fa186eebdd1a662eb770fad
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 12 Dec 2016 14:42:07 +0100

[cc1] Free the types defined in functions

We were not freeing ever the types defined locally,
and since all the symbols in cc2 were strictly freed
at the end of the function we had a problem of dangling
identifiers.

Diffstat:
Mcc1/cc1.h | 2++
Mcc1/decl.c | 1+
Mcc1/tests/test016.c | 16++++++++--------
Mcc1/types.c | 22+++++++++++++++++++++-
4 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -292,6 +292,7 @@ struct type { unsigned char rank; /* convertion rank */ TINT elem; /* number of type parameters */ } n; + Type *next; /* local list pointer */ Type *h_prev, *h_next; /* hash pointers */ }; @@ -356,6 +357,7 @@ extern Type *mktype(Type *tp, int op, TINT nelem, Type *data[]); 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 */ diff --git a/cc1/decl.c b/cc1/decl.c @@ -907,6 +907,7 @@ decl(void) emit(OFUN, sym); compound(NULL, NULL, NULL); emit(OEFUN, NULL); + flushtypes(); curfun = ocurfun; } diff --git a/cc1/tests/test016.c b/cc1/tests/test016.c @@ -31,15 +31,15 @@ G11 I F "func2 { \ A12 I "x -A13 P "p -A15 P "pp +A14 P "p +A17 P "pp A12 #I1 :I - A13 A12 'P :P - A15 A13 'P :P - y L17 A13 #P0 =I - A15 @P @I #I0 :I -L17 - A13 #P0 :P + A14 A12 'P :P + A17 A14 'P :P + y L19 A14 #P0 =I + A17 @P @I #I0 :I +L19 + A14 #P0 :P h A12 } */ diff --git a/cc1/types.c b/cc1/types.c @@ -72,7 +72,7 @@ static struct limits limits[][4] = { } }; -static Type typetab[NR_TYPE_HASH]; +static Type typetab[NR_TYPE_HASH], *localtypes; void itypes() @@ -333,6 +333,12 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[]) h_next->h_prev = bp; tbl->h_next = bp; + if (curctx > GLOBALCTX+1) { + /* it is a type defined in the body of a function */ + bp->next = localtypes; + localtypes = bp; + } + return bp; } @@ -378,3 +384,17 @@ eqtype(Type *tp1, Type *tp2, int equiv) abort(); } } + +void +flushtypes(void) +{ + Type *tp, *next; + + 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); + } + localtypes = NULL; +}