scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit a677ec7436ef171644dbfc8abccd332fceeeada1
parent 3ebba91c4cc5aff7a1757f0c6b54928ca87fecb8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 19 May 2015 11:49:24 +0200

Remove bitfields of Symbol in cc1

These bitfields can be implemented better as flags.

Diffstat:
cc1/cc1.h | 20+++++++++++++-------
cc1/code.c | 16+++++++---------
cc1/decl.c | 26+++++++++++++-------------
cc1/expr.c | 2+-
cc1/stmt.c | 9++++++---
cc1/symbol.c | 4++--
6 files changed, 42 insertions(+), 35 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -29,13 +29,7 @@ struct symbol { uint8_t ctx; uint8_t ns; uint8_t token; - bool isglobal : 1; - bool isstatic : 1; - bool isauto : 1; - bool isregister : 1; - bool isdefined : 1; - bool isfield : 1; - bool isparameter : 1; + char flags; union { int i; char *s; @@ -101,6 +95,18 @@ enum { NR_NAMESPACES }; +/* symbol flags */ +enum { + ISGLOBAL = 1, + ISSTATIC = 2, + ISAUTO = 4, + ISREGISTER = 8, + ISDEFINED = 16, + ISFIELD = 32, + ISPARAM = 64, + ISEXTERN =128 +}; + /* input tokens */ enum tokens { TQUALIFIER = 128, diff --git a/cc1/code.c b/cc1/code.c @@ -159,17 +159,15 @@ emitvar(Symbol *sym) { char c; - if (sym->isstatic && !sym->isglobal) - c = 'T'; - else if (sym->isstatic && sym->isglobal) - c = 'Y'; - else if (sym->isglobal) + if (sym->flags & ISSTATIC) + c = (sym->flags & ISGLOBAL) ? 'Y' : 'T'; + else if (sym->flags & ISGLOBAL) c = 'G'; - else if (sym->isregister) + else if (sym->flags & ISREGISTER) c = 'K'; - else if (sym->isfield) + else if (sym->flags & ISFIELD) c = 'M'; - else if (sym->isparameter) + else if (sym->flags & ISPARAM) c = 'P'; else c = 'A'; @@ -271,7 +269,7 @@ emitfun(uint8_t op, void *arg) Symbol *sym = arg; printf("%c%d\tF\t%s\t{\n", - sym->isglobal ? 'G' : 'Y', sym->id, sym->name); + sym->flags & ISGLOBAL ? 'G' : 'Y', sym->id, sym->name); } static void diff --git a/cc1/decl.c b/cc1/decl.c @@ -254,7 +254,7 @@ invalid_type: static struct node * initializer(Symbol *sym) { - if (!sym->isdefined) + if (!(sym->flags & ISEXTERN)) error("'%s' initialized and declared extern", sym->name); if (accept('{')) { @@ -340,7 +340,7 @@ structdcl(void) do { sym = declarator(base, ID_EXPECTED, tagtype->ns); - sym->isfield = 1; + sym->flags |= ISFIELD; tp = sym->type; if (tp->op == FTN) error("invalid type in struct/union"); @@ -404,7 +404,7 @@ parameter(void) if ((tp = specifier(&sclass)) == voidtype) return NULL; sym = declarator(tp, ID_ACCEPTED, NS_IDEN); - sym->isparameter = 1; + sym->flags |= ISPARAM; tp = sym->type; if (tp->op == FTN) error("incorrect function type for a function parameter"); @@ -412,10 +412,10 @@ parameter(void) tp = mktype(tp->type, PTR, 0, NULL); switch (sclass) { case REGISTER: - sym->isregister = 1; + sym->flags |= ISREGISTER; break; case 0: - sym->isauto = 1; + sym->flags |= ISAUTO; break; default: error("bad storage class in function parameter"); @@ -449,13 +449,13 @@ decl(void) sym->token = TYPEIDEN; continue; case STATIC: - sym->isstatic = 1; + sym->flags |= ISSTATIC; break; case EXTERN: - sym->isdefined = 0; + sym->flags |= ISEXTERN; break; case REGISTER: - sym->isregister = 1; + sym->flags = ISREGISTER; if (isfun) goto bad_function; break; @@ -464,7 +464,7 @@ decl(void) goto bad_function; /* passtrough */ default: - sym->isauto = 1; + sym->flags |= ISAUTO; break; } if (accept('=')) @@ -517,17 +517,17 @@ extdecl(void) problems with EOF */ sym = declarator(base, ID_EXPECTED, NS_IDEN); tp = sym->type; - sym->isstatic = 1; - sym->isglobal= 1; + sym->flags |= ISSTATIC; + sym->flags |= ISGLOBAL; switch (sclass) { case REGISTER: case AUTO: error("incorrect storage class for file-scope declaration"); case STATIC: - sym->isglobal = 0; + sym->flags |= ISSTATIC; break; case EXTERN: - sym->isdefined = 0; + sym->flags |= ISEXTERN; break; case TYPEDEF: sym->token = TYPEIDEN; diff --git a/cc1/expr.c b/cc1/expr.c @@ -420,7 +420,7 @@ address(char op, Node *np) { if (!np->lvalue) error("lvalue required in unary expression"); - if (np->symbol && np->sym->isregister) + if (np->symbol && (np->sym->flags & ISREGISTER)) error("address of register variable '%s' requested", yytext); return node(op, mktype(np->type, PTR, 0, NULL), np, NULL); } diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -20,15 +20,18 @@ label(char *s, char define) if ((sym = lookup(s, NS_LABEL)) != NULL) { if (define) { - if (sym->isdefined) + if (sym->flags & ISDEFINED) error("label '%s' already defined", s); - sym->isdefined = 1; + sym->flags |= ISDEFINED; } return sym; } sym = install(s, NS_LABEL); - sym->isdefined = define; + if (define) + sym->flags |= ISDEFINED; + else + sym->flags &= ~ISDEFINED; return sym; } diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -38,7 +38,7 @@ freesyms(uint8_t ns) for (sym = tbl->head; sym; sym = next) { if (sym->ctx <= curctx) break; - if (ns == NS_LABEL && !sym->isdefined) + if (ns == NS_LABEL && !(sym->flags & ISDEFINED)) error("label '%s' is not defined", sym->name); if (ns == NS_TAG) sym->type->defined = 0; @@ -94,7 +94,7 @@ install(char *s, uint8_t ns) sym->ctx = curctx; sym->token = IDEN; sym->id = (curctx) ? ++localcnt : ++globalcnt; - sym->isdefined = 1; + sym->flags |= ISDEFINED; sym->ns = ns; tbl = &symtab[(ns > NS_STRUCTS) ? NS_STRUCTS : ns]; sym->next = tbl->head;