scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit a2a347ed21e17c6d3bbbcafc9cdf7cf7b1aa0986
parent 8773fd15cb4be505d58d95f0839ff7823e1f653f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  5 Jan 2025 16:40:14 +0100

cc2: Add branchnode()

This function avoids a lot of code repetition because creating
branch or jump nodes is something that it is going to happen
usually.

Diffstat:
Msrc/cmd/scc-cc/cc2/sethi.c | 44++++++++++++++++++++++++--------------------
1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c @@ -3,12 +3,27 @@ #include "cc2.h" static Node * -bool(Node *np, Symbol *true, Symbol *false) +branchnode(Node *cond, Symbol *sym) +{ + Node *np; + int op = cond ? OBRANCH : OJMP; + + np = node(op); + np->op = op; + np->left = sethi(cond); + np->u.sym = sym; + sym->refcnt++; + + return np; +} + +static Node * +bool(Node *cond, Symbol *true, Symbol *false) { Symbol *label; - Node *p, *l = np->left, *r = np->right; + Node *p, *l = cond->left, *r = cond->right; - switch (np->op) { + switch (cond->op) { case ONEG: l = bool(l, false, true); break; @@ -25,22 +40,14 @@ bool(Node *np, Symbol *true, Symbol *false) r = bool(r, true, false); break; default: - p = node(OBRANCH); - p->left = sethi(np); - p->u.sym = true; - true->refcnt++; - prestmt(p); - - p = node(OJMP); - p->u.sym = false; - false->refcnt++; - prestmt(p); + prestmt(branchnode(cond, true)); + prestmt(branchnode(NULL, false)); return NULL; } - np->left = l; - np->right = r; - return np; + cond->left = l; + cond->right = r; + return cond; } Node * @@ -64,10 +71,7 @@ logicexpr(Node *np) p->right = constnode(NULL, 0, tp); prestmt(labelstmt(sethi(p), false)); - p = node(OJMP); - p->u.sym = phi; - phi->refcnt++; - prestmt(p); + prestmt(branchnode(NULL, phi)); p = node(OASSIG); p->type = *tp;