scc

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

commit ad92212ec3827d9e105b874cdebf4ed3fc09709c
parent ef05ad6ff2560acd1364b1d90cf52ec95603bc9e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 15 Apr 2014 11:41:53 +0200

Add constcode()

A node is generated from a constant in different places, so
it is a good idea to translate it to a function which is easier
of reading and generates less code.

Diffstat:
Mcc.h | 3++-
Mcode.c | 6++++++
Mexpr.c | 9++++-----
3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/cc.h b/cc.h @@ -238,7 +238,8 @@ extern Node *bincode(char op, Type *tp, Node *np1, Node *np2), *castcode(Node *child, Type *tp), *sizeofcode(Type *tp), - *ternarycode(Node *cond, Node *ifyes, Node *ifno); + *ternarycode(Node *cond, Node *ifyes, Node *ifno), + *constcode(Symbol *sym); #define SYM(s) ((union unode) {.sym = s}) #define OP(s) ((union unode) {.op = s}) diff --git a/code.c b/code.c @@ -198,3 +198,9 @@ ternarycode(Node *cond, Node *ifyes, Node *ifno) np->childs[2] = ifno; return np; } + +Node * +constcode(Symbol *sym) +{ + return node(emitconst, inttype, SYM(sym), 0); +} diff --git a/expr.c b/expr.c @@ -262,9 +262,9 @@ eval(Node *np, char neg) Node *ifyes, *ifno, *cmp; char op; - ifyes = node(emitconst, inttype, SYM(one), 0); - ifno = node(emitconst, inttype, SYM(zero), 0); - cmp = node(emitconst, inttype, SYM(zero), 0); + ifyes = constcode(one); + ifno = constcode(zero); + cmp = constcode(zero); op = (neg) ? OEQ : ONE; return ternarycode(compare(op, np, cmp), ifyes, ifno); @@ -323,8 +323,7 @@ primary(void) next(); break; case CONSTANT: - sym = yylval.sym; - np = node(emitconst, sym->type, SYM(sym), 0); + np = constcode(yylval.sym); next(); break; case '(':