commit e72e0e9184bf0aa2fc6551816ad6f3f1b36a4d53
parent 8198b4e6e7e36f66e5d28513e7935b2c404db8ad
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 27 Dec 2024 09:37:37 +0100
cc2: Rewrite addstmt() and delstmt()
Addstmt() was buggy and it only worked when new statements were
added at the end, that it was always the case. As we expect that
we are going to need a pointer to the last statement then we moved
aways from the u.stmt field and we represent the function with
a specific struct, that eventually will contain curstmt too.
Diffstat:
2 files changed, 36 insertions(+), 15 deletions(-)
diff --git a/src/cmd/scc-cc/cc2/node.c b/src/cmd/scc-cc/cc2/node.c
@@ -7,11 +7,16 @@
#define NNODES 32
+struct function {
+ Node *begin;
+ Node *end;
+};
+
Node *curstmt;
Symbol *curfun;
static Alloc *arena;
-
+static struct function fn;
Node *
node(int op)
@@ -66,25 +71,33 @@ prforest(char *msg)
return;
fprintf(stderr, "tree %s {\n", msg);
- for (np = curfun->u.stmt; np; np = np->next)
+ for (np = fn.begin; np; np = np->next)
prtree(np);
fputs("}\n", stderr);
}
#endif
Node *
-addstmt(Node *np, int flag)
+addstmt(Node *np, int mode)
{
- if (curstmt)
- np->next = curstmt->next;
- np->prev = curstmt;
+ Node *next;
- if (!curfun->u.stmt)
- curfun->u.stmt = np;
- else
+ next = NULL;
+ if (curstmt) {
+ next = curstmt->next;
+ if (next)
+ next->prev = np;
curstmt->next = np;
+ }
+ np->next = next;
+ np->prev = curstmt;
+
+ if (!fn.begin)
+ fn.begin = np;
+ if (!fn.end || !np->next)
+ fn.end = np;
- if (flag == SETCUR)
+ if (mode == SETCUR)
curstmt = np;
return np;
@@ -95,14 +108,20 @@ delstmt(void)
{
Node *next, *prev;
+ if (!curstmt)
+ return NULL;
+
next = curstmt->next;
prev = curstmt->prev;
if (next)
next->prev = prev;
if (prev)
prev->next = next;
- else
- curfun->u.stmt = next;
+
+ if (fn.begin == curstmt)
+ fn.begin = next;
+ if (fn.end == curstmt)
+ fn.end = prev;
deltree(curstmt);
return curstmt = next;
@@ -138,6 +157,8 @@ cleannodes(void)
arena = NULL;
}
curstmt = NULL;
+ curfun = NULL;
+ memset(&fn, 0, sizeof(fn));
}
void
diff --git a/src/cmd/scc-cc/cc2/parser.c b/src/cmd/scc-cc/cc2/parser.c
@@ -712,9 +712,9 @@ endfun(void)
void
parse(void)
{
- cleannodes(); /* remove code of previous function */
- popctx(); /* remove context of previous function */
- curfun = NULL;
+ /* clean from previous function */
+ cleannodes();
+ popctx();
endf = 0;
while (!endf && nextline())