shithub: scc

Download patch

ref: 7a39b526a28d598345a38327f3bcb613004c7937
parent: 5ab898a530482dfdcb445a2eb80d23399dd04865
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Nov 16 12:23:07 EST 2021

cc1: Centralize hash table initialization

The hash table was initialialized in several places, and in all
of them the counterid was reset to 0. This was very dangerous
and the types emitted in irach() were using the same id than
the next types and variables declared in the code. To avoid
this problem and to minimize the dependencies we place all the
initialization in only one place located in symbol.c.

--- a/src/cmd/cc/cc1/cc1.h
+++ b/src/cmd/cc/cc1/cc1.h
@@ -297,11 +297,6 @@
 	Node *(*fun)(Symbol *);
 };
 
-struct keyword {
-	char *str;
-	unsigned char token, value;
-};
-
 struct type {
 	unsigned char op;           /* type builder operator */
 	unsigned char ns;           /* namespace for struct members */
@@ -433,10 +428,10 @@
 extern void pushctx(void), popctx(void);
 extern void killsym(Symbol *sym);
 extern Symbol *newlabel(void);
-extern void keywords(struct keyword *key, int ns);
 extern void builtins(struct builtin *builts);
 extern Symbol *newstring(char *s, size_t len);
 extern unsigned newid(void);
+extern void isyms(void);
 
 /* stmt.c */
 extern void compound(Symbol *lbreak, Symbol *lcont, Switch *sw);
@@ -453,7 +448,6 @@
 extern int addinput(char *fname, Symbol *hide, char *buffer, int fail);
 extern void delinput(void);
 extern void setsafe(int type);
-extern void ilex(void);
 extern void setloc(char *fname, unsigned line);
 #define accept(t) ((yytoken == (t)) ? next() : 0)
 
--- a/src/cmd/cc/cc1/cpp.c
+++ b/src/cmd/cc/cc1/cpp.c
@@ -66,23 +66,6 @@
 		"__SCC__",
 		NULL
 	};
-	static struct keyword keys[] = {
-		{"define", DEFINE, DEFINE},
-		{"include", INCLUDE, INCLUDE},
-		{"line", LINE, LINE},
-		{"ifdef", IFDEF, IFDEF},
-		{"if", IF, IF},
-		{"elif", ELIF, ELIF},
-		{"else", ELSE, ELSE},
-		{"ifndef", IFNDEF, IFNDEF},
-		{"endif", ENDIF, ENDIF},
-		{"undef", UNDEF, UNDEF},
-		{"pragma", PRAGMA, PRAGMA},
-		{"error", ERROR, ERROR},
-		{NULL, 0, 0}
-	};
-
-	keywords(keys, NS_CPPCLAUSES);
 
 	t = time(NULL);
 	tm = localtime(&t);
--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -24,51 +24,6 @@
 Input *input;
 
 void
-ilex(void)
-{
-	static struct keyword keys[] = {
-		{"auto", SCLASS, AUTO},
-		{"break", BREAK, BREAK},
-		{"_Bool", TYPE, BOOL},
-		{"__builtin_va_list", TYPE, VA_LIST},
-		{"case", CASE, CASE},
-		{"char", TYPE, CHAR},
-		{"const", TQUALIFIER, CONST},
-		{"continue", CONTINUE, CONTINUE},
-		{"default", DEFAULT, DEFAULT},
-		{"do", DO, DO},
-		{"double", TYPE, DOUBLE},
-		{"else", ELSE, ELSE},
-		{"enum", TYPE, ENUM},
-		{"extern", SCLASS, EXTERN},
-		{"float", TYPE, FLOAT},
-		{"for", FOR, FOR},
-		{"goto", GOTO, GOTO},
-		{"if", IF, IF},
-		{"inline", TQUALIFIER, INLINE},
-		{"int", TYPE, INT},
-		{"long", TYPE, LONG},
-		{"register", SCLASS, REGISTER},
-		{"restrict", TQUALIFIER, RESTRICT},
-		{"return", RETURN, RETURN},
-		{"short", TYPE, SHORT},
-		{"signed", TYPE, SIGNED},
-		{"sizeof", SIZEOF, SIZEOF},
-		{"static", SCLASS, STATIC},
-		{"struct", TYPE, STRUCT},
-		{"switch", SWITCH, SWITCH},
-		{"typedef", SCLASS, TYPEDEF},
-		{"union", TYPE, UNION},
-		{"unsigned", TYPE, UNSIGNED},
-		{"void", TYPE, VOID},
-		{"volatile", TQUALIFIER, VOLATILE},
-		{"while", WHILE, WHILE},
-		{NULL, 0, 0},
-	};
-	keywords(keys, NS_KEYWORD);
-}
-
-void
 setloc(char *fname, unsigned line)
 {
 	size_t len;
--- a/src/cmd/cc/cc1/main.c
+++ b/src/cmd/cc/cc1/main.c
@@ -77,11 +77,10 @@
 	if (argc > 1)
 		usage();
 
+	isyms();
 	icode();
 	iarch();
-	ilex();
 	icpp();
-	ibuilts();
 
 	for (i = 0; i < iflags.n; ++i)
 		incdir(iflags.s[i]);
--- a/src/cmd/cc/cc1/symbol.c
+++ b/src/cmd/cc/cc1/symbol.c
@@ -12,6 +12,11 @@
 #define NR_CPP_HASH 32
 #define NR_LBL_HASH 16
 
+struct keyword {
+	char *str;
+	unsigned char token, value;
+};
+
 unsigned curctx;
 static unsigned short counterid;
 
@@ -321,13 +326,6 @@
 		sym->token = key->token;
 		sym->u.token = key->value;
 	}
-	/*
-	 * Remove all the predefined symbols from * the symbol list. It
-	 * will make faster some operations. There is no problem of memory
-	 * leakeage because this memory is not ever freed
-	 */
-	counterid = 0;
-	head = NULL;
 }
 
 void
@@ -341,6 +339,70 @@
 		sym->token = BUILTIN;
 		sym->u.fun = bp->fun;
 	}
+}
+
+void
+isyms(void)
+{
+	static struct keyword cppkeys[] = {
+		{"define", DEFINE, DEFINE},
+		{"include", INCLUDE, INCLUDE},
+		{"line", LINE, LINE},
+		{"ifdef", IFDEF, IFDEF},
+		{"if", IF, IF},
+		{"elif", ELIF, ELIF},
+		{"else", ELSE, ELSE},
+		{"ifndef", IFNDEF, IFNDEF},
+		{"endif", ENDIF, ENDIF},
+		{"undef", UNDEF, UNDEF},
+		{"pragma", PRAGMA, PRAGMA},
+		{"error", ERROR, ERROR},
+		{NULL, 0, 0}
+	};
+	static struct keyword lexkeys[] = {
+		{"auto", SCLASS, AUTO},
+		{"break", BREAK, BREAK},
+		{"_Bool", TYPE, BOOL},
+		{"__builtin_va_list", TYPE, VA_LIST},
+		{"case", CASE, CASE},
+		{"char", TYPE, CHAR},
+		{"const", TQUALIFIER, CONST},
+		{"continue", CONTINUE, CONTINUE},
+		{"default", DEFAULT, DEFAULT},
+		{"do", DO, DO},
+		{"double", TYPE, DOUBLE},
+		{"else", ELSE, ELSE},
+		{"enum", TYPE, ENUM},
+		{"extern", SCLASS, EXTERN},
+		{"float", TYPE, FLOAT},
+		{"for", FOR, FOR},
+		{"goto", GOTO, GOTO},
+		{"if", IF, IF},
+		{"inline", TQUALIFIER, INLINE},
+		{"int", TYPE, INT},
+		{"long", TYPE, LONG},
+		{"register", SCLASS, REGISTER},
+		{"restrict", TQUALIFIER, RESTRICT},
+		{"return", RETURN, RETURN},
+		{"short", TYPE, SHORT},
+		{"signed", TYPE, SIGNED},
+		{"sizeof", SIZEOF, SIZEOF},
+		{"static", SCLASS, STATIC},
+		{"struct", TYPE, STRUCT},
+		{"switch", SWITCH, SWITCH},
+		{"typedef", SCLASS, TYPEDEF},
+		{"union", TYPE, UNION},
+		{"unsigned", TYPE, UNSIGNED},
+		{"void", TYPE, VOID},
+		{"volatile", TQUALIFIER, VOLATILE},
+		{"while", WHILE, WHILE},
+		{NULL, 0, 0},
+	};
+
+	keywords(lexkeys, NS_KEYWORD);
+	keywords(cppkeys, NS_CPPCLAUSES);
+	ibuilts();
+
 	/*
 	 * Remove all the predefined symbols from * the symbol list. It
 	 * will make faster some operations. There is no problem of memory