scc

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

commit cb8b536d8b84402c372f610efed3e1a6e852f65d
parent ad2d3ea7a4b95be09db3643e96b9e485e3296e81
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 29 Jul 2015 10:01:29 +0200

Add compile time calculation of constant ?:

Diffstat:
Mcc1/expr.c | 12++++++++++++
1 file changed, 12 insertions(+), 0 deletions(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -1065,6 +1065,18 @@ ternary(void) expect(':'); ifno = promote(ternary()); typeconv(&ifyes, &ifno); + if (cond->constant) { + TINT i = cond->sym->u.i; + + freetree(cond); + if (i == 0) { + freetree(ifyes); + return ifno; + } else { + freetree(ifno); + return ifyes; + } + } np = node(OCOLON, ifyes->type, ifyes, ifno); cond = node(OASK, np->type, cond, np); }