scc

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

commit 3ef4da68f555260111abb67615a28885bf57a7b5
parent 61adffcdcf6e9aeb0c762fb0ba3974d6c2e648c4
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Tue, 10 Feb 2026 14:38:36 +0100

cc2: Generalize keeplabel()

Having a keeplabel() call in every case where we can modify the
statement with the current label is very fragile, and we should to
search for a more general solution where we don't have to care
anymore about this problem. We could modify the parser to get rid
of labels in statements and have only label statements, but it is
much more simple to check for that case in every call to sethi().

Diffstat:
Msrc/cmd/scc-cc/cc2/cc2.h | 2+-
Msrc/cmd/scc-cc/cc2/node.c | 2+-
Msrc/cmd/scc-cc/cc2/sethi.c | 14+++++++++-----
3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h @@ -282,7 +282,7 @@ Symbol *newlabel(void); void pprint(char *s); void deftype(Type *); Node *labelstmt(Node *, Symbol *); -Node *keeplabel(void); +Node *savelabel(void); /* node.c */ void newfun(Symbol *, Node *); diff --git a/src/cmd/scc-cc/cc2/node.c b/src/cmd/scc-cc/cc2/node.c @@ -137,7 +137,7 @@ prestmt(Node *np) } Node * -keeplabel(void) +savelabel(void) { Symbol *label = curstmt->label; diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c @@ -170,7 +170,6 @@ sethi(Node *np) np = swtch(np); break; case OBRANCH: - keeplabel(); next = np->next; if (!next->label) labelstmt(next, NULL); @@ -185,17 +184,14 @@ sethi(Node *np) np->u.sym->refcnt--; return NULL; case OCOMMA: - keeplabel(); r = np->right; comma(np->left); return replace(np, r); case OASK: - keeplabel(); return replace(np, ternary(np)); case ONEG: case OAND: case OOR: - keeplabel(); return replace(np, logicexpr(np)); default: np = tsethi(np); @@ -223,9 +219,17 @@ sethi(Node *np) return np; } +static Node * +presethi(Node *np) +{ + /* just in case ... */ + savelabel(); + return sethi(np); +} + void genaddr(void) { - apply(sethi); + apply(presethi); PRTREE("after sethi"); }