commit 35e9f657a7d01cd0cc060ccaf671764227ef06b6
parent e72e0e9184bf0aa2fc6551816ad6f3f1b36a4d53
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 27 Dec 2024 12:01:19 +0100
cc2: Add insstmt()
This function allows to insert a statement after a specific
statement and it avoids using curstmt out of node.c.
Diffstat:
3 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h
@@ -266,6 +266,7 @@ extern Node *node(int op);
extern Node *addstmt(Node *np, int flags);
extern Node *delstmt(void);
extern Node *nextstmt(void);
+extern Node *insstmt(Node *, Node *, int);
/* symbol.c */
#define TMPSYM 0
diff --git a/src/cmd/scc-cc/cc2/node.c b/src/cmd/scc-cc/cc2/node.c
@@ -12,11 +12,11 @@ struct function {
Node *end;
};
-Node *curstmt;
Symbol *curfun;
static Alloc *arena;
static struct function fn;
+static Node *curstmt;
Node *
node(int op)
@@ -77,6 +77,28 @@ prforest(char *msg)
}
#endif
+/*
+ * Insert a node after `at' and if mode is SETCUR
+ * update the current statement if `at' was the
+ * current statement
+ */
+Node *
+insstmt(Node *np, Node *at, int mode)
+{
+ Node *save;
+
+ save = curstmt;
+ curstmt = at;
+ addstmt(np, KEEPCUR);
+
+ if (mode == KEEPCUR)
+ curstmt = save;
+ else
+ curstmt = (save == at) ? np : save;
+
+ return np;
+}
+
Node *
addstmt(Node *np, int mode)
{
diff --git a/src/cmd/scc-cc/cc2/parser.c b/src/cmd/scc-cc/cc2/parser.c
@@ -341,34 +341,21 @@ oreturn(char *token, union tokenop u)
/*
* Move np (which is a OCASE/ODEFAULT/OESWITCH) to be contigous with
- * the last switch table. It is a bit ugly to touch directly curstmt
- * here, but moving this function to node.c is worse, because we are
- * putting knowledge of how the text is parsed into the node
- * represtation module.
+ * the last switch table.
*/
static void
waft(Node *np)
{
Node *lastcase, *next;
struct swtch *cur;
- extern Node *curstmt;
if (swp == swtbl)
error(EWTACKU);
cur = swp - 1;
lastcase = cur->last;
- next = lastcase->next;
+ insstmt(np, cur->last, SETCUR);
- np->next = next;
- np->prev = lastcase;
-
- if (next)
- next->prev = np;
- lastcase->next = np;
-
- if (curstmt == cur->last)
- curstmt = np;
cur->last = np;
cur->nr++;
}