shithub: scc

Download patch

ref: 1537ee3c105f0ba568d4a3b40adf4021da40b7c9
parent: fe8c715e58c377228207548a27aed474839feca5
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Oct 4 17:07:40 EDT 2015

Move keyword initialization to more specific places

It is better if each module initializes the own keywords,
instead of having a function that initializes the keywords
for all the modules.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -46,6 +46,10 @@
 	} min;
 };
 
+struct keyword {
+	char *str;
+	unsigned char token, value;
+};
 
 struct type {
 	unsigned char op;           /* type builder operator */
@@ -345,9 +349,9 @@
 extern Symbol *install(int ns, Symbol *sym);
 extern Symbol *newsym(int ns);
 extern void pushctx(void), popctx(void);
-extern void ikeywords(void);
 extern void killsym(Symbol *sym);
 extern Symbol *newlabel(void);
+extern void keywords(struct keyword *key, int ns);
 
 /* stmt.c */
 extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch);
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -37,12 +37,27 @@
 	static char sdate[17], stime[14];
 	struct tm *tm;
 	time_t t;
-	char **bp, *list[] = {
+	static char **bp, *list[] = {
 		"__STDC__",
 		"__STDC_HOSTED__",
 		"__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}
+	};
 
 	t = time(NULL);
 	tm = localtime(&t);
@@ -57,6 +72,7 @@
 
 	for (bp = list; *bp; ++bp)
 		defmacro(*bp)->u.s = "-1#1";
+	keywords(keys, NS_CPPCLAUSES);
 }
 
 static void
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -39,6 +39,44 @@
 void
 ilex(char *fname)
 {
+	static struct keyword keys[] = {
+		{"auto", SCLASS, AUTO},
+		{"break", BREAK, BREAK},
+		{"_Bool", TYPE, BOOL},
+		{"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},
+	};
 	FILE *fp;
 
 	if (!fname) {
@@ -52,6 +90,7 @@
 	}
 	allocinput(fname, fp);
 	*input->begin = '\0';
+	keywords(keys, NS_KEYWORD);
 }
 
 bool
--- a/cc1/main.c
+++ b/cc1/main.c
@@ -75,7 +75,6 @@
 		usage();
 
 	icpp();
-	ikeywords();
 	ilex(*argv);
 
 	if (onlycpp) {
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -282,11 +282,6 @@
 	return linkhash(sym);
 }
 
-struct keyword {
-	char *str;
-	unsigned char token, value;
-};
-
 void
 keywords(struct keyword *key, int ns)
 {
@@ -304,63 +299,4 @@
 	 */
 	counterid = 0;
 	head = NULL;
-}
-
-void
-ikeywords(void)
-{
-	static struct keyword ckeywords[] = {
-		{"auto", SCLASS, AUTO},
-		{"break", BREAK, BREAK},
-		{"_Bool", TYPE, BOOL},
-		{"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},
-	}, cppclauses[] = {
-		{"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(ckeywords, NS_KEYWORD);
-	keywords(cppclauses, NS_CPPCLAUSES);
 }