scc

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

commit af9f64086ce4a32c58c096e56b6ca8432d1fcf88
parent ac1f52ccad9cb7919726a64c458763eb78f5dd4a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 13 May 2014 17:23:24 +0200

Remove ns field of symbols

This field was added in order to can define a namespace for
every aggregate, but it was a bad way of doing it. It is a good
idea to create a new context only for the declaration of aggregates,
because we don't want to create symbols for them, we only want to
take the symbol name and the type in order to create the fields
of the struct.

Diffstat:
Mcc1/cc1.h | 9++++-----
Mcc1/decl.c | 44++++++++++++++++++++------------------------
Mcc1/symbol.c | 28++++++++++++++++------------
3 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -23,10 +23,9 @@ extern void warn(signed char flag, const char *fmt, ...); enum { NS_IDEN = 0, - NS_LABEL, NS_TAG, - NR_NAMESPACES, - NS_FREE + NS_LABEL, + NR_NAMESPACES }; struct funpars; @@ -71,7 +70,6 @@ struct symbol { short id; uint8_t ctx; uint8_t token; - uint8_t ns; struct { bool isglobal : 1; bool isstatic : 1; @@ -83,7 +81,7 @@ struct symbol { int i; char *s; struct symbol *sym; - uint8_t ns, token; + uint8_t token; } u; struct symbol *next; struct symbol *hash; @@ -106,6 +104,7 @@ typedef struct caselist Caselist; typedef void Ctxfun(Symbol *, Symbol *, Caselist *); extern Ctxfun compound; +extern Type *aggregate(Type *(*fun)(void)); extern void context(Ctxfun *fun, Symbol *lbreak, Symbol *lcont, Caselist *lswitch); diff --git a/cc1/decl.c b/cc1/decl.c @@ -22,8 +22,7 @@ struct dcldata { } u; }; -static struct dcldata - *declarator0(struct dcldata *dp, uint8_t ns, int8_t flags); +static struct dcldata *declarator0(struct dcldata *dp, int8_t flags); static struct dcldata * arydcl(struct dcldata *dp) @@ -50,34 +49,34 @@ fundcl(struct dcldata *dp) } static Symbol * -newiden(uint8_t ns) +newiden(void) { Symbol *sym; extern uint8_t curctx; if (yylval.sym && yylval.sym->ctx == curctx) error("redeclaration of '%s'", yytext); - sym = install(yytext, ns); + sym = install(yytext, NS_IDEN); next(); return sym; } static struct dcldata * -directdcl(struct dcldata *dp, uint8_t ns, int8_t flags) +directdcl(struct dcldata *dp, int8_t flags) { register Symbol *sym; char *err; if (accept('(')) { - dp = declarator0(dp, ns, flags); + dp = declarator0(dp, flags); expect(')'); } else if (flags) { if (yytoken != IDEN) { if (flags & ID_EXPECTED) goto expected; - sym = install("", ns); + sym = install("", NS_IDEN); } else { - sym = newiden(ns); + sym = newiden(); } dp->op = IDEN; dp->u.sym = sym; @@ -97,7 +96,7 @@ expected: } static struct dcldata* -declarator0(struct dcldata *dp, uint8_t ns, int8_t flags) +declarator0(struct dcldata *dp, int8_t flags) { uint8_t buffer[NR_DECLARATORS]; register uint8_t *bp, n, qlf; @@ -114,7 +113,7 @@ declarator0(struct dcldata *dp, uint8_t ns, int8_t flags) *bp++ = qlf; } - dp = directdcl(dp, ns, flags); + dp = directdcl(dp, flags); bp = buffer; while (n--) { @@ -132,7 +131,7 @@ too_much_declarators: } static Symbol * -declarator(Type *tp, uint8_t ns, int8_t flags) +declarator(Type *tp, int8_t flags) { struct dcldata data[NR_DECLARATORS+1]; register struct dcldata *bp; @@ -141,7 +140,7 @@ declarator(Type *tp, uint8_t ns, int8_t flags) memset(data, 0, sizeof(data)); data[NR_DECLARATORS].op = 255; - for (bp = declarator0(data, ns, flags); bp >= data; --bp) { + for (bp = declarator0(data, flags); bp >= data; --bp) { switch (bp->op) { case PTR: tp = qualifier(mktype(tp, PTR, NULL, 0), bp->u.qlf); @@ -213,7 +212,7 @@ specifier(int8_t *sclass) goto invalid_type; *p = yylval.token; if (dcl) - tp = dcl(); + tp = aggregate(dcl); else next(); } @@ -295,7 +294,7 @@ error: } static void -fielddcl(Type *base, uint8_t ns) +fielddcl(Type *base) { Type *tp; Symbol *sym; @@ -314,7 +313,7 @@ fielddcl(Type *base, uint8_t ns) if (yytoken != ';') { do { - sym = declarator(tp, ns, ID_EXPECTED); + sym = declarator(tp, ID_EXPECTED); newfield(base, sym); } while (accept(',')); } @@ -336,7 +335,6 @@ newtag(uint8_t tag) { register Symbol *sym; Type *tp; - extern uint8_t namespace; if (yytoken == IDEN) { sym = lookup(yytext, NS_TAG); @@ -351,7 +349,6 @@ newtag(uint8_t tag) sym = install("", NS_TAG); } tp = sym->type = mktype(NULL, tag, NULL, 0); - sym->u.ns = ++namespace; tp->sym = sym; return tp; @@ -363,19 +360,18 @@ static Type * structdcl(void) { Type *tp; - uint8_t ns, tag; + uint8_t tag; tag = yylval.token; next(); tp = newtag(tag); tp->u.fields = NULL; - ns = tp->sym->u.ns; if (accept('{')) { if (tp->defined) goto redefined; tp->defined = 1; while (!accept('}')) - fielddcl(tp, ns); + fielddcl(tp); } return tp; @@ -402,7 +398,7 @@ enumdcl(void) while (yytoken != '}') { if (yytoken != IDEN) goto iden_expected; - sym = newiden(NS_IDEN); + sym = newiden(); sym->type = inttype; if (accept('=')) initializer(inttype); @@ -435,7 +431,7 @@ decl(void) tp = specifier(&sclass); if (yytoken != ';') { do { - sym = declarator(tp, NS_IDEN, ID_EXPECTED); + sym = declarator(tp, ID_EXPECTED); switch (sclass) { case REGISTER: sym->s.isregister = 1; break; case STATIC: sym->s.isstatic = 1; break; @@ -461,7 +457,7 @@ typename(void) tp = specifier(&sclass); if (sclass) error("class storage in type name"); - sym = declarator(tp, NS_IDEN, 0); + sym = declarator(tp, 0); return sym->type; } @@ -494,7 +490,7 @@ extdecl(void) do { Type *tp; - sym = declarator(base, NS_IDEN, ID_EXPECTED); + sym = declarator(base, ID_EXPECTED); tp = sym->type; if (!(sclass & STATIC)) diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -10,7 +10,6 @@ #define NR_SYM_HASH 32 uint8_t curctx; -uint8_t namespace = NS_FREE + 1; short symid; static struct symtab { @@ -34,7 +33,7 @@ freesyms(uint8_t ns) static struct symtab *tbl; register Symbol *sym, *next; - tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns]; + tbl = &symtab[ns]; for (sym = tbl->head; sym; sym = next) { if (sym->ctx <= curctx) break; @@ -47,17 +46,24 @@ freesyms(uint8_t ns) } } +Type * +aggregate(Type * (*fun)(void)) +{ + Type *tp; + + ++curctx; + tp = (*fun)(); + --curctx; + freesyms(NS_IDEN); + return tp; +} + void context(Ctxfun *fun, Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { - uint8_t ns; - - ns = namespace; ++curctx; (*fun)(lbreak, lcont, lswitch); --curctx; - namespace = ns; - freesyms(NS_IDEN); freesyms(NS_TAG); } @@ -65,14 +71,13 @@ context(Ctxfun *fun, Symbol *lbreak, Symbol *lcont, Caselist *lswitch) Symbol * lookup(register char *s, uint8_t ns) { - extern union yystype yylval; static struct symtab *tbl; register Symbol *sym; - tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns]; + tbl = &symtab[ns]; for (sym = tbl->htab[hash(s)]; sym; sym = sym->hash) { register char *t = sym->name; - if (sym->ns == ns && t && *t == *s && !strcmp(t, s)) + if (*t == *s && !strcmp(t, s)) return sym; } @@ -89,9 +94,8 @@ install(char *s, uint8_t ns) sym->name = xstrdup(s); sym->ctx = curctx; sym->token = IDEN; - sym->ns = ns; sym->id = symid++; - tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns]; + tbl = &symtab[ns]; sym->next = tbl->head; tbl->head = sym;