scc

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

commit 1058d554d1451fa1d7b1f09ca9d6b8cfeb7e18ed
parent c60c1303e07f568b43adae5f48f71afef61afc6c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  5 Jan 2025 22:38:24 +0100

cc2: Deal with comma in sethi()

The comma operator just marks an order between different
statements, and it can be resolved with a tree transformation
moving the comma subexpressions before the current statement.
It works for any combination of operands, because the evaluation
order is not speficied an din the cases where it is speficified
like for example in boolean operators they already define
synchronization points.

Diffstat:
Msrc/cmd/scc-cc/cc2/qbe/cgen.c | 3---
Msrc/cmd/scc-cc/cc2/sethi.c | 22+++++++++++++++++++++-
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/cmd/scc-cc/cc2/qbe/cgen.c b/src/cmd/scc-cc/cc2/qbe/cgen.c @@ -607,9 +607,6 @@ rhs(Node *np) return assign(np); case OASK: return ternary(np); - case OCOMMA: - rhs(l); - return rhs(r); case OSNEG: sign = (tp->flags & SIGNF) == 0; size = tp->size == 8; diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c @@ -94,11 +94,23 @@ logicexpr(Node *np) return sethi(tmpvar); } +static void +comma(Node *np) +{ + if (np->op != OCOMMA) { + prestmt(sethi(np)); + } else { + comma(np->left); + prestmt(sethi(np->right)); + delnode(np); + } +} + Node * sethi(Node *np) { int op; - Node *next, *l, *r; + Node *prev, *next, *l, *r; if (!np) return np; @@ -124,6 +136,14 @@ sethi(Node *np) bool(np->left, np->u.sym, next->label); np->u.sym->refcnt--; return NULL; + 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); case ONEG: case OAND: case OOR: