scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 2deecc6ed6f9fb6292454e00b385c63500cecd2d
parent b1d315c34637fc9de8fc97d57172f1b82024f741
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 16 Nov 2021 17:23:07 +0100

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.

Diffstat:
Msrc/cmd/cc/cc1/cc1.h | 8+-------
Msrc/cmd/cc/cc1/cpp.c | 17-----------------
Msrc/cmd/cc/cc1/lex.c | 45---------------------------------------------
Msrc/cmd/cc/cc1/main.c | 3+--
Msrc/cmd/cc/cc1/symbol.c | 76+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
5 files changed, 71 insertions(+), 78 deletions(-)

diff --git a/src/cmd/cc/cc1/cc1.h b/src/cmd/cc/cc1/cc1.h @@ -297,11 +297,6 @@ struct builtin { 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 Symbol *newsym(int ns, char *name); 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 void discard(void); 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) diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c @@ -66,23 +66,6 @@ icpp(void) "__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); diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c @@ -24,51 +24,6 @@ static int safe; 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; diff --git a/src/cmd/cc/cc1/main.c b/src/cmd/cc/cc1/main.c @@ -77,11 +77,10 @@ main(int argc, char *argv[]) if (argc > 1) usage(); + isyms(); icode(); iarch(); - ilex(); icpp(); - ibuilts(); for (i = 0; i < iflags.n; ++i) incdir(iflags.s[i]); diff --git 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 @@ keywords(struct keyword *key, int ns) 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 @@ builtins(struct builtin *built) 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