commit 8be6d9e4cd588fd6110a4252e36848214ad04b28
parent f3c4d6c3e0822ccf373935eb8851660bbd298931
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Tue, 10 Feb 2026 11:26:43 +0100
cc2: Keep label position in ternaries
When a ternary is transformed into a set of jumps new statements are
inserted before the current statement, displacing the relative position
of the label of the current statement in case of having one. For that
reason in this case we have to create a label statment and place there
the label.
Diffstat:
5 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h
@@ -282,6 +282,7 @@ Symbol *newlabel(void);
void pprint(char *s);
void deftype(Type *);
Node *labelstmt(Node *, Symbol *);
+Node *keeplabel(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,6 +137,18 @@ prestmt(Node *np)
}
Node *
+keeplabel(void)
+{
+ Symbol *label = curstmt->label;
+
+ if (label) {
+ prestmt(labelstmt(NULL, label));
+ curstmt->label = NULL;
+ }
+ return curstmt;
+}
+
+Node *
addstmt(Node *np)
{
insstmt(np, curstmt);
diff --git a/src/cmd/scc-cc/cc2/sethi.c b/src/cmd/scc-cc/cc2/sethi.c
@@ -188,6 +188,7 @@ sethi(Node *np)
comma(np->left);
return replace(np, r);
case OASK:
+ keeplabel();
return replace(np, ternary(np));
case ONEG:
case OAND:
diff --git a/tests/cc/execute/0243-ternary.c b/tests/cc/execute/0243-ternary.c
@@ -0,0 +1,12 @@
+int but, aclick;
+
+int
+main(void)
+{
+ int r;
+
+ r = 0;
+ if (aclick == 1 && but)
+ r = (but == 1) ? 0 : 1;
+ return r;
+}
diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst
@@ -233,3 +233,4 @@
0240-init.c
0241-init.c
0242-ternary.c
+0243-ternary.c