scc

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

commit 9ff031d254906ced8bc498a0db070afdc420f0ac
parent a2e934bedcd03584e32e7d0c2e66eb23eb6faa50
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 28 Dec 2024 21:55:31 +0100

cc2: Write addstmt() based in insstmt()

This makes more sense and the previous implmentation was only a hack
to fast implementing insstmt().

Diffstat:
Msrc/cmd/scc-cc/cc2/cc2.h | 2+-
Msrc/cmd/scc-cc/cc2/node.c | 43++++++++++++++-----------------------------
Msrc/cmd/scc-cc/cc2/parser.c | 8+++++---
3 files changed, 20 insertions(+), 33 deletions(-)

diff --git a/src/cmd/scc-cc/cc2/cc2.h b/src/cmd/scc-cc/cc2/cc2.h @@ -277,7 +277,7 @@ extern Node *node(int op); extern Node *addstmt(Range *, Node *np, int flags); extern Node *delstmt(Range *); extern Node *nextstmt(Range *, int); -extern Node *insstmt(Range *, Node *, Node *, int); +extern Node *insstmt(Range *, Node *, Node *); extern void delrange(Range *); extern Range range(Node *, Node *); diff --git a/src/cmd/scc-cc/cc2/node.c b/src/cmd/scc-cc/cc2/node.c @@ -75,51 +75,36 @@ 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(Range *rp, Node *np, Node *at, int mode) -{ - Node *save; - - save = rp->cur; - rp->cur = at; - addstmt(rp, np, KEEPCUR); - - if (mode == KEEPCUR) - rp->cur = save; - else - rp->cur = (save == at) ? np : save; - - return np; -} - -Node * -addstmt(Range *rp, Node *np, int mode) +insstmt(Range *rp, Node *np, Node *at) { Node *next; next = NULL; - if (rp->cur) { - next = rp->cur->next; + if (at) { + next = at->next; if (next) next->prev = np; - rp->cur->next = np; + at->next = np; } + np->next = next; - np->prev = rp->cur; + np->prev = at; if (!rp->begin) rp->begin = np; - if (!rp->end || !np->next) + if (!rp->end || !next) rp->end = np; + return np; +} + +Node * +addstmt(Range *rp, Node *np, int mode) +{ + insstmt(rp, np, rp->cur); if (mode == SETCUR) rp->cur = np; - return np; } diff --git a/src/cmd/scc-cc/cc2/parser.c b/src/cmd/scc-cc/cc2/parser.c @@ -346,15 +346,17 @@ oreturn(char *token, union tokenop u) static void waft(Node *np) { - Node *lastcase, *next; + Range *rp; struct swtch *cur; if (swp == swtbl) error(EWTACKU); cur = swp - 1; - lastcase = cur->last; - insstmt(fbody(), np, cur->last, SETCUR); + rp = fbody(); + insstmt(rp, np, cur->last); + if (rp->cur == cur->last) + rp->cur = np; cur->last = np; cur->nr++;