shithub: scc

Download patch

ref: 42a80b685648b7cb5c417c00b7cc338dcbefc394
parent: 4698e1692871826efbc031b0d2c9991ea8ee6be5
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Feb 12 07:26:07 EST 2015

Allow that funtors in apply() returns a value

This is needed because some of the funtors can modify the tree, so
it is important that the value stored in the list of trees is modified,
so it is needed to receive some value from the funtors. It will not
be needed in some cases, but then you can return NULL.

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -111,10 +111,10 @@
 };
 
 extern void error(unsigned nerror, ...);
-extern void genaddable(Node *np);
+extern Node *genaddable(Node *np);
 extern void generate(Symbol *fun);
 extern void genstack(Symbol *fun);
-extern void apply(Node *list[], void (*fun)(Node *));
+extern void apply(Node *list[], Node *(*fun)(Node *));
 extern Symbol *parse(void);
 extern void code(char op, ...);
 extern void prtree(Node *np);
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -93,7 +93,7 @@
 }
 
 static void
-cgen(Node *np)
+cgen(Node *np, Node *parent)
 {
 	Node *lp, *rp;
 	TINT imm;
@@ -161,6 +161,13 @@
 	}
 }
 
+static Node *
+applycgen(Node *np)
+{
+	cgen(np, NULL);
+	return NULL;
+}
+
 void
 generate(Symbol *fun)
 {
@@ -176,7 +183,7 @@
 		code(LD, SP, HL);
 	}
 
-	apply(fun->u.f.body, cgen);
+	apply(fun->u.f.body, applycgen);
 
 	if (frame) {
 		code(LD, SP, IX);
@@ -186,6 +193,8 @@
 }
 
 /*
+ * This is strongly influenced by
+ * http://plan9.bell-labs.com/sys/doc/compiler.ps
  * calculate addresability as follows
  *     AUTO => 11          value+fp
  *     REGISTER => 13      register
@@ -192,13 +201,13 @@
  *     STATIC => 12        (value)
  *     CONST => 20         $value
  */
-void
+Node *
 genaddable(Node *np)
 {
 	Node *lp, *rp;
 
 	if (!np)
-		return;
+		return np;
 
 	np->complex = 0;
 	np->addable = 0;
@@ -226,7 +235,7 @@
 	}
 
 	if (np->addable > 10)
-		return;
+		return np;
 	if (lp)
 		np->complex = lp->complex;
 	if (rp) {
@@ -239,5 +248,5 @@
 	}
 	if (np->complex == 0)
 		++np->complex;
-	return;
+	return np;
 }
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -104,12 +104,12 @@
 }
 
 void
-apply(Node *list[], void (*fun)(Node *))
+apply(Node *list[], Node *(*fun)(Node *))
 {
 	Node *np;
 
-	while (np = *list++)
-		(*fun)(np);
+	while (np = *list)
+		*list++ = (*fun)(np);
 }
 
 static Symbol *