scc

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

commit d11e94df0f60fd433115496b04f555473b8d7c49
parent 39b40b941b5658db5728d641191e8eaa632e8671
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon,  6 Jan 2025 22:16:41 +0100

cc2: Add replace()

This function is used when a node has to be replaced with
some other node, and keep the list of statements valid.
Examples where it is used is in the command operator and
in the logical operators.

Diffstat:
Msrc/cmd/scc-cc/cc2/sethi.c | 26+++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c @@ -89,7 +89,6 @@ logicexpr(Node *np) prestmt(labelstmt(p, false)); prestmt(labelstmt(NULL, phi)); - delstmt(np); return sethi(tmpvar); } @@ -106,11 +105,24 @@ comma(Node *np) } } +static Node * +replace(Node *what, Node *with) +{ + Node *prev, *next; + + next = what->next, prev = what->prev; + *what = *with; + delnode(with); + what->next = next, what->prev = prev; + + return sethi(what); +} + Node * sethi(Node *np) { int op; - Node *prev, *next, *l, *r; + Node *next, *p, *l, *r; if (!np) return np; @@ -139,16 +151,12 @@ sethi(Node *np) case OCOMMA: r = np->right; comma(np->left); - next = np->next, prev = np->prev; - *np = *r; - delnode(r); - np->next = next, np->prev = prev; - return sethi(np); + return replace(np, r); case ONEG: case OAND: case OOR: - np = logicexpr(np); - break; + p = logicexpr(np); + return replace(np, p); default: np = tsethi(np); break;