shithub: scc

Download patch

ref: cd61580b6cd92002ac990c82a2efc27b674597b2
parent: b06516b170729a996782aec041f680b174ab0536
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Apr 25 17:53:57 EDT 2016

[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.

--- 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 @@
 };
 
 static struct arena *arena;
-static Node *freep, *stmtp;
+static Node *freep;
 static int inhome;
 
 Node *
@@ -50,14 +51,38 @@
 	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 @@
 	}
 	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();
 }