scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 950cfce031fb38e6337e087c398dc4e7c5efc467
parent 3fc0e80e34ebf76d739d77aaa0c28cda75369218
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
Date:   Mon, 25 Apr 2016 21:53:57 +0200

[cc2] Remove statements in apply()

A usage case of apply() may be optimization, and we need some way
of removing unwanted statments, and we can use the return value
of the parameter function to mark what statements must be deleted.

Diffstat:
cc2/node.c | 43+++++++++++++++++++++++++++++++++----------
1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/cc2/node.c b/cc2/node.c @@ -9,6 +9,7 @@ #define NNODES 32 +Node *curstmt; Symbol *curfun; Type rtype; @@ -18,7 +19,7 @@ struct arena { }; static struct arena *arena; -static Node *freep, *stmtp; +static Node *freep; static int inhome; Node * @@ -50,14 +51,38 @@ addstmt(Node *np) if (!curfun->u.stmt) curfun->u.stmt = np; else - stmtp->next = np; + curstmt->next = np; np->next = NULL; - np->prev = stmtp; - stmtp = np; + np->prev = curstmt; + curstmt = np; return np; } +Node * +delstmt(void) +{ + Node *next, *prev; + + next = curstmt->next; + prev = curstmt->prev; + if (next) + next->prev = prev; + if (prev) + prev->next = next; + else + curfun->u.stmt = next; + deltree(curstmt); + + return curstmt = next; +} + +Node * +nextstmt(void) +{ + return curstmt = curstmt->next; +} + void delnode(Node *np) { @@ -87,17 +112,15 @@ cleannodes(void) } arena = NULL; freep = NULL; - stmtp = NULL; + curstmt = NULL; } void apply(Node *(*fun)(Node *)) { - Node *np; - if (!curfun) return; - - for (np = curfun->u.stmt; np; np = np->next) - (*fun)(np); + curstmt = curfun->u.stmt; + while (curstmt) + (*fun)(curstmt) ? nextstmt() : delstmt(); }