scc

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

commit 475fdec2b580ce32a5e53a3d33c139e23d977b07
parent eadc7784288c38da564aa1f11602d0c195f5dabb
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  1 Sep 2015 20:09:06 +0200

Transform constconv() to constcode()

The work was duplicated between convert() and constconv(), and it fact,
it is a bit duplicated, but this version is better than the previous
one.

Diffstat:
Mcc1/cc1.h | 2+-
Mcc1/expr.c | 5+----
Mcc1/fold.c | 13+++++++++----
3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -362,7 +362,7 @@ extern void freetree(Node *np); /* fold.c */ extern Node *simplify(int op, Type *tp, Node *lp, Node *rp); -extern Node *constconv(Node *np, Type *newtp); +extern Node *castcode(Node *np, Type *newtp); /* expr.c */ extern Node *expr(void), *negate(Node *np), *constexpr(void); diff --git a/cc1/expr.c b/cc1/expr.c @@ -151,12 +151,9 @@ Node * convert(Node *np, Type *newtp, char iscast) { Type *oldtp = np->type; - Node *p; if (eqtype(newtp, oldtp)) return np; - if (np->constant && (p = constconv(np, newtp)) != NULL) - return p; switch (oldtp->op) { case ENUM: @@ -202,7 +199,7 @@ convert(Node *np, Type *newtp, char iscast) default: return NULL; } - return node(OCAST, newtp, np, NULL); + return castcode(np, newtp); } static Node * diff --git a/cc1/fold.c b/cc1/fold.c @@ -375,14 +375,16 @@ simplify(int op, Type *tp, Node *lp, Node *rp) } /* TODO: check validity of types */ -/* TODO: Integrate it with simplify */ Node * -constconv(Node *np, Type *newtp) +castcode(Node *np, Type *newtp) { Type *oldtp = np->type; Symbol aux, *sym, *osym = np->sym; + if (!np->constant) + goto noconstant; + switch (newtp->op) { case PTR: case INT: @@ -405,14 +407,14 @@ constconv(Node *np, Type *newtp) aux.u.u = osym->u.f; break; default: - return NULL; + goto noconstant; } break; case FLOAT: aux.u.f = (oldtp->sign) ? osym->u.i : osym->u.u; break; default: - return NULL; + goto noconstant; } sym = newsym(NS_IDEN); @@ -421,4 +423,7 @@ constconv(Node *np, Type *newtp) sym->u = aux.u; return np; + +noconstant: + return node(OCAST, newtp, np, NULL); }