scc

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

commit 9f2aff7690c4222d1ad28eef68b042f9e6e61c27
parent 17834699b2e8786effcd3fda322db20779710286
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 15 Nov 2014 07:05:42 -0500

Simplify mktype

This function had a switch with a lot of different options, which
can be simplified a lot using an array.

It is also needed share the pointer between aggregates and functions.
This is only temporal, because we want to unify them.

Diffstat:
cc1/cc1.h | 3+--
cc1/decl.c | 4++--
cc1/expr.c | 2+-
cc1/types.c | 83+++++++++++++++++++++++++++----------------------------------------------------
4 files changed, 32 insertions(+), 60 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -49,8 +49,7 @@ struct ctype { short nelem; /* number of elements in ary/ftn/strct/union */ union typeval { unsigned char rank; /* convertion rank */ - Type **pars; /* function parameters */ - Field *fields; /* aggregate fields */ + void *pars; /* parameters */ } u; }; diff --git a/cc1/decl.c b/cc1/decl.c @@ -258,7 +258,7 @@ newfield(Type *tp, Symbol *sym) register char *s, *t; s = sym->name; - for (q = p = tp->u.fields; p; q = p, p = p->next) { + for (q = p = tp->u.pars; p; q = p, p = p->next) { t = p->name; if (*s == *t && !strcmp(s, t)) error("duplicated fields '%s' and '%s'", s, t); @@ -273,7 +273,7 @@ newfield(Type *tp, Symbol *sym) p->id = sym->id; p->type = sym->type; if (!q) - tp->u.fields = p; + tp->u.pars= p; else q->next = p; diff --git a/cc1/expr.c b/cc1/expr.c @@ -298,7 +298,7 @@ field(Node *np) unexpected(); switch (np->typeop) { case STRUCT: case UNION: - for (fp = np->type->u.fields; fp; fp = fp->next) { + for (fp = np->type->u.pars; fp; fp = fp->next) { if (!strcmp(fp->name, yytext)) { next(); return fieldcode(np, fp); diff --git a/cc1/types.c b/cc1/types.c @@ -188,67 +188,40 @@ invalid_type: Type * mktype(Type *tp, uint8_t op, short nelem, void *data) { - static Type *typetab[NR_TYPE_HASH], **tbl; - static uint8_t t, def; + static Type *typetab[NR_TYPE_HASH], **tbl, type; + static uint8_t t; register Type *bp; - char letter, look; - union typeval u; + static char letters[] = { + [PTR] = L_POINTER, [ARY] = L_ARRAY, + [FTN] = L_FUNCTION, [ENUM] = L_INT, + [STRUCT] = L_STRUCT, [UNION] = L_UNION + }; - switch (op) { - case PTR: - if (tp == voidtype) - return pvoidtype; - letter = L_POINTER; - def = 1; - look = 1; - break; - case ARY: - letter = L_ARRAY; - def = nelem != 0; - look = 1; - break; - case FTN: - u.pars = data; - letter = L_FUNCTION; - def = 1; - look = 0; - break; - case ENUM: - letter = L_INT; - def = 1; - look = 0; - break; - case STRUCT: case UNION: - letter = (op == STRUCT) ? L_STRUCT : L_UNION; - def = 0; - look = 0; - u.fields = NULL; - break; - default: - fputs("internal type error, aborting\n", stderr); - abort(); - } + if (op == PTR && tp == voidtype) + return pvoidtype; + + type.type = tp; + type.op = op; + type.sign = 0; + type.letter = letters[op]; + type.nelem = nelem; + type.u.pars = data; - t = (op ^ (uint8_t) ((unsigned short) tp >> 3)) - & NR_TYPE_HASH-1; + if (op == ARY && nelem == 0 || op == STRUCT || op == UNION) + type.defined = 0; + else + type.defined = 1; + + t = (op ^ (uint8_t) ((unsigned short) tp >> 3)) & NR_TYPE_HASH-1; tbl = &typetab[t]; - if (look) { - for (bp = *tbl; bp; bp = bp->next) { - if (bp->type == tp && bp->op == op && - (op != ARY || bp->nelem == nelem)) { - return bp; - } - } + for (bp = *tbl; bp; bp = bp->next) { + if (eqtype(bp, &type)) + return bp; } - bp = xcalloc(1, sizeof(*bp)); + bp = xmalloc(sizeof(*bp)); + *bp = type; bp->next = *tbl; - bp->type = tp; - bp->op = op; - bp->letter = letter; - bp->defined = def; - bp->nelem = nelem; - bp->u = u; return *tbl = bp; } @@ -275,7 +248,7 @@ eqtype(Type *tp1, Type *tp2) return 1; case UNION: case STRUCT: { - Field *fp1 = tp1->u.fields, *fp2 = tp2->u.fields; + Field *fp1 = tp1->u.pars, *fp2 = tp2->u.pars; while (fp1 && fp2) { if (!eqtype(fp1->type, fp2->type))