scc

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

commit 511baa28dc19e114dc4ddb08c865e68cbfc8a624
parent 9f2aff7690c4222d1ad28eef68b042f9e6e61c27
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 15 Nov 2014 12:40:27 -0500

Unify rank and nelemt

These fields are not going to be used at the same time ever, so they
can be joined in a union. In the same patch we remove the union of pars,
because an union of only one element is a bit silly.

Diffstat:
Mcc1/cc1.h | 10+++++-----
Mcc1/decl.c | 4++--
Mcc1/expr.c | 6+++---
Mcc1/types.c | 42+++++++++++++++++++++---------------------
4 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -46,11 +46,11 @@ struct ctype { bool sign : 1; /* sign type */ struct ctype *type; /* base type */ struct ctype *next; /* next element in the hash */ - short nelem; /* number of elements in ary/ftn/strct/union */ - union typeval { - unsigned char rank; /* convertion rank */ - void *pars; /* parameters */ - } u; + union { + unsigned char rank; /* convertion rank */ + short elem; /* number of type parameters */ + } n; + void *pars; /* type parameters */ }; 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.pars; p; q = p, p = p->next) { + for (q = p = tp->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.pars= p; + tp->pars= p; else q->next = p; diff --git a/cc1/expr.c b/cc1/expr.c @@ -32,7 +32,7 @@ promote(Node *np) if (options.npromote) return np; tp = np->type; - r = tp->u.rank; + r = tp->n.rank; if (r > RANK_UINT || tp == inttype || tp == uinttype) return np; return castcode(np, (r == RANK_UINT) ? uinttype : inttype); @@ -51,7 +51,7 @@ typeconv(Node **p1, Node **p2) tp1 = np1->type; tp2 = np2->type; if (tp1 != tp2) { - if ((n = tp1->u.rank - tp2->u.rank) > 0) + if ((n = tp1->n.rank - tp2->n.rank) > 0) np2 = castcode(np2, tp1); else if (n < 0) np1 = castcode(np1, tp2); @@ -298,7 +298,7 @@ field(Node *np) unexpected(); switch (np->typeop) { case STRUCT: case UNION: - for (fp = np->type->u.pars; fp; fp = fp->next) { + for (fp = np->type->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 @@ -23,96 +23,96 @@ Type .op = INT, .letter = L_BOOL, .defined = 1, - .u.rank = RANK_BOOL + .n.rank = RANK_BOOL }, *schartype = &(Type) { .op = INT, .letter = L_SCHAR, .defined = 1, - .u.rank = RANK_SCHAR + .n.rank = RANK_SCHAR }, *uchartype = &(Type) { .op = INT, .letter = L_UCHAR, .sign = 1, .defined = 1, - .u.rank = RANK_UCHAR + .n.rank = RANK_UCHAR }, *chartype = &(Type) { .op = INT, .letter = L_CHAR, .sign = 1, .defined = 1, - .u.rank = RANK_CHAR + .n.rank = RANK_CHAR }, *ushortype = &(Type) { .op = INT, .letter = L_USHORT, .defined = 1, - .u.rank = RANK_USHORT + .n.rank = RANK_USHORT }, *shortype = &(Type) { .op = INT, .letter = L_SHORT, .defined = 1, - .u.rank = RANK_SHORT + .n.rank = RANK_SHORT }, *uinttype = &(Type) { .op = INT, .letter = L_UINT, .sign = 1, .defined = 1, - .u.rank = RANK_UINT + .n.rank = RANK_UINT }, *inttype = &(Type) { .op = INT, .letter = L_INT, .defined = 1, - .u.rank = RANK_INT + .n.rank = RANK_INT }, *longtype = &(Type) { .op = INT, .letter = L_LONG, .defined = 1, - .u.rank = RANK_LONG + .n.rank = RANK_LONG }, *ulongtype = &(Type) { .op = INT, .letter = L_ULONG, .sign = 1, .defined = 1, - .u.rank = RANK_ULONG + .n.rank = RANK_ULONG }, *ullongtype = &(Type) { .op = INT, .letter = L_ULLONG, .sign = 1, .defined = 1, - .u.rank = RANK_ULLONG + .n.rank = RANK_ULLONG }, *llongtype = &(Type) { .op = INT, .letter = L_LLONG, .defined = 1, - .u.rank = RANK_LLONG + .n.rank = RANK_LLONG }, *floattype = &(Type) { .op = FLOAT, .letter = L_FLOAT, .defined = 1, - .u.rank = RANK_FLOAT + .n.rank = RANK_FLOAT }, *doubletype = &(Type) { .op = FLOAT, .letter = L_DOUBLE, .defined = 1, - .u.rank = RANK_DOUBLE + .n.rank = RANK_DOUBLE }, *ldoubletype = &(Type) { .op = FLOAT, .letter = L_LDOUBLE, .defined = 1, - .u.rank = RANK_LDOUBLE + .n.rank = RANK_LDOUBLE }; Type * @@ -204,8 +204,8 @@ mktype(Type *tp, uint8_t op, short nelem, void *data) type.op = op; type.sign = 0; type.letter = letters[op]; - type.nelem = nelem; - type.u.pars = data; + type.pars = data; + type.n.elem = nelem; if (op == ARY && nelem == 0 || op == STRUCT || op == UNION) type.defined = 0; @@ -233,22 +233,22 @@ eqtype(Type *tp1, Type *tp2) if (tp1 == tp2) return 1; - if (tp1->op != tp2->op || tp1->nelem != tp2->nelem) + if (tp1->op != tp2->op || tp1->n.elem != tp2->n.elem) return 0; switch (tp1->op) { case PTR: return eqtype(tp1->type, tp2->type); /* TODO: use the same struct for function parameters and fields */ case FTN: - p1 = tp1->u.pars, p2 = tp2->u.pars; - for (n = tp1->nelem; n != 0; --n) { + p1 = tp1->pars, p2 = tp2->pars; + for (n = tp1->n.elem; n != 0; --n) { if (!eqtype(*p1++, *p2++)) return 0; } return 1; case UNION: case STRUCT: { - Field *fp1 = tp1->u.pars, *fp2 = tp2->u.pars; + Field *fp1 = tp1->pars, *fp2 = tp2->pars; while (fp1 && fp2) { if (!eqtype(fp1->type, fp2->type))