shithub: scc

Download patch

ref: 57380d876cf8ee79ba67e06dca722cc54189ac27
parent: b6b38357b4e631ba81d71e372dadc5e9511f4f78
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 19 03:48:02 EDT 2014

Add apply() function

apply() applies a function to a list of nodes (usually each node is
the root of a tree). This function is going to be called in almost
of the passes.

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -95,6 +95,7 @@
 #define ADDABLE 10
 
 extern void error(unsigned nerror, ...);
-extern void genaddable(Node *list[]);
+extern void genaddable(Node *np);
 extern void generate(Symbol *sym, Node *list[]);
 extern void genstack(Symbol *fun);
+extern void apply(Node *list[], void (*fun)(Node *));
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -240,7 +240,6 @@
 generate(Symbol *sym, Node *list[])
 {
 	extern char odebug;
-	Node *np;
 	char frame = sym->u.f.locals != 0 || odebug;
 
 	emit(ADDR, sym->name);
@@ -252,8 +251,7 @@
 		emit(LD, SP, HL);
 	}
 
-	while (np = *list++)
-		cgen(np);
+	apply(list, cgen);
 
 	if (frame) {
 		emit(LD, SP, IX);
@@ -269,8 +267,8 @@
  *     STATIC => 12        (value)
  *     CONST => 20         $value
  */
-static void
-xaddable(Node *np)
+void
+genaddable(Node *np)
 {
 	Node *lp, *rp;
 
@@ -296,9 +294,9 @@
 		break;
 	default:
 		if (lp)
-			xaddable(lp);
+			genaddable(lp);
 		if (rp)
-			xaddable(rp);
+			genaddable(rp);
 		break;
 	}
 
@@ -318,13 +316,3 @@
 		++np->complex;
 	return;
 }
-
-void
-genaddable(Node *list[])
-{
-	Node *np;
-
-	while (np = *list++)
-		xaddable(np);
-}
-
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -77,6 +77,15 @@
 	.c_int = 1,
 };
 
+void
+apply(Node *list[], void (*fun)(Node *))
+{
+	Node *np;
+
+	while (np = *list++)
+		(*fun)(np);
+}
+
 static Symbol *
 parameter(char *num)
 {
@@ -396,7 +405,7 @@
 	if (!curfun)
 		error(ESYNTAX);
 	listp = NULL;
-	genaddable(listexp);
+	apply(listexp, genaddable);
 	generate(curfun, listexp);
 	curfun = NULL;
 }