shithub: scc

Download patch

ref: ffdf7436ae4c2a8b573a325d9ca3c7f0b475a149
parent: a56f57af3f26cde6c17402877b952156c0b3c99e
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Dec 12 09:42:07 EST 2016

[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.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -292,6 +292,7 @@
 		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 *duptype(Type *base);
 extern struct limits *getlimits(Type *tp);
 extern void typesize(Type *tp);
+extern void flushtypes(void);
 extern void itypes(void);
 
 /* symbol.c */
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -907,6 +907,7 @@
 	emit(OFUN, sym);
 	compound(NULL, NULL, NULL);
 	emit(OEFUN, NULL);
+	flushtypes();
 	curfun = ocurfun;
 }
 
--- a/cc1/tests/test016.c
+++ b/cc1/tests/test016.c
@@ -31,15 +31,15 @@
 {
 \
 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
 }
 */
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -72,7 +72,7 @@
 	}
 };
 
-static Type typetab[NR_TYPE_HASH];
+static Type typetab[NR_TYPE_HASH], *localtypes;
 
 void
 itypes()
@@ -333,6 +333,12 @@
 	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;
 }
 
@@ -377,4 +383,18 @@
 	default:
 		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;
 }