scc

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

commit ea6f348b4e6b23fdbb93896aac72ea6ace1d8639
parent 912db5e91a1141ac0d91c484ffe4472fad91bad7
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 12 Aug 2016 11:01:00 +0200

[cc2] Add constnode()

This function it is going to be extensively used
because it is common to do some operation with some
constant value.

Diffstat:
Mcc2/arch/qbe/cgen.c | 12+++---------
Mcc2/cc2.h | 2+-
Mcc2/code.c | 11+++++++++++
3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -388,19 +388,13 @@ sethi(Node *np) break; case OCPL: np->op = OAND; - rp = newnode(OCONST); - rp->type = np->type; - rp->u.i = 0; - rp->u.i = ~np->u.i; + rp = constnode(~(TUINT) 0, &np->type); goto binary; case ONEG: np->op = OSUB; rp = lp; - lp = newnode(OCONST); - lp->type = np->type; - if (np->type.flags & INTF) - lp->u.i = 0; - else + lp = constnode(0, &np->type); + if ((np->type.flags & INTF) == 0) lp->u.f = 0.0; default: binary: diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -216,7 +216,7 @@ extern void writeout(void), endinit(void), newfun(void); extern void code(int op, Node *to, Node *from1, Node *from2); extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *); extern void setlabel(Symbol *sym), getbblocks(void); -extern Node *label2node(Symbol *sym); +extern Node *label2node(Symbol *sym), *constnode(TUINT n, Type *tp); extern Symbol *newlabel(void); /* node.c */ diff --git a/cc2/code.c b/cc2/code.c @@ -77,6 +77,17 @@ label2node(Symbol *sym) return np; } +Node * +constnode(TUINT n, Type *tp) +{ + Node *np; + + np = newnode(OCONST); + np->type = *tp; + np->u.i = n; + return np; +} + void setlabel(Symbol *sym) {