scc

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

commit 71d0843d509fd2d0a14717bfc6dd30617a1aaba7
parent 42fd6480b200aab21dc5118050b8f4753cd25967
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  2 Sep 2015 13:58:29 +0200

Move cmp() from fold.c to expr.c

This movement allows to remove the ugly macro SYMICMP.

Diffstat:
Mcc1/cc1.h | 1+
Mcc1/expr.c | 31+++++++++++++++++++++++--------
Mcc1/fold.c | 25++-----------------------
3 files changed, 26 insertions(+), 31 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -371,6 +371,7 @@ extern Node *eval(Node *np), *iconstexpr(void), *condition(void); extern Node *exp2cond(Node *np, char neg); extern bool isnodecmp(int op); extern int negop(int op); +extern bool cmpnode(Node *np, TUINT val); /* cpp.c */ extern void icpp(void); diff --git a/cc1/expr.c b/cc1/expr.c @@ -7,11 +7,28 @@ #include "../inc/sizes.h" #include "cc1.h" +Node *expr(void); -#define SYMICMP(sym, val) (((sym)->type->sign) ? \ - (sym)->u.i == (val) : (sym)->u.u == (val)) +bool +cmpnode(Node *np, TUINT val) +{ + Symbol *sym; + Type *tp; -Node *expr(void); + if (!np || !np->constant) + return 0; + sym = np->sym; + tp = sym->type; + + switch (tp->op) { + case PTR: + case INT: + return ((tp->sign) ? sym->u.i : sym->u.u) == val; + case FLOAT: + return sym->u.f == val; + } + return 0; +} bool isnodecmp(int op) @@ -270,7 +287,7 @@ pcompare(char op, Node *lp, Node *rp) { switch (BTYPE(rp)) { case INT: - if (rp->constant && SYMICMP(rp->sym, 0)) + if (cmpnode(rp, 0)) rp = convert(rp, pvoidtype, 1); break; case PTR: @@ -402,12 +419,10 @@ assignop(char op, Node *lp, Node *rp) lp = eval(lp); rp = eval(rp); - if (BTYPE(rp) == INT && BTYPE(lp) == PTR && - rp->constant && SYMICMP(rp->sym, 0)) { + if (BTYPE(rp) == INT && BTYPE(lp) == PTR && cmpnode(rp, 0)) rp = convert(rp, pvoidtype, 1); - } else if ((rp = convert(rp, lp->type, 0)) == NULL) { + else if ((rp = convert(rp, lp->type, 0)) == NULL) errorp("incompatible types when assigning"); - } return node(op, lp->type, lp, rp); } diff --git a/cc1/fold.c b/cc1/fold.c @@ -369,27 +369,6 @@ commutative(int *op, Node **lp, Node **rp) } } -static bool -cmp(Node *np, int val) -{ - Symbol *sym; - Type *tp; - - if (!np->constant) - return 0; - sym = np->sym; - tp = sym->type; - - switch (tp->op) { - case PTR: - case INT: - return ((tp->sign) ? sym->u.i : sym->u.u) == val; - case FLOAT: - return sym->u.f == val; - } - return 0; -} - static TUINT ones(int n) { @@ -420,12 +399,12 @@ identity(int op, Node *lp, Node *rp) val = 1; break; case OBAND: - if (cmp(lp, ones(lp->type->size * 8))) + if (cmpnode(lp, ones(lp->type->size * 8))) goto free_right; default: return NULL; } - if (!cmp(rp, val)) + if (!cmpnode(rp, val)) return NULL; free_right: freetree(rp);