shithub: scc

Download patch

ref: ce03e9ba4b39cc668444a5678a6686d70669f111
parent: 0521af76024d66c24768f7e8e3f1fbdc946fa142
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon May 9 06:07:39 EDT 2016

[cc2] Do not move curstmt in addstmt()

Addstmt() was moving the pointer to the current statement because
it was useful in parser.c, but in the majority of cases we don't want
such behaviour, and to solve the problem we had to add prevstmt().

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -230,8 +230,7 @@
 		if (np->next == NULL) {
 			Node *tmp = newnode();
 			tmp->op = ORET;
-			addstmt(tmp);
-			prevstmt();
+			addstmt(tmp, KEEPCUR);
 		}
 	}
 	l = cgen(np->left);
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -204,13 +204,15 @@
 extern void setlabel(Symbol *sym);
 
 /* node.c */
+#define SETCUR  1
+#define KEEPCUR 0
 extern void apply(Node *(*fun)(Node *));
 extern void cleannodes(void);
 extern void delnode(Node *np);
 extern void deltree(Node *np);
 extern Node *newnode(void);
-extern Node *addstmt(Node *np);
-extern Node *prevstmt(void), *nextstmt(void);
+extern Node *addstmt(Node *np, int flags);
+extern Node *nextstmt(void);
 
 
 /* symbol.c */
--- a/cc2/node.c
+++ b/cc2/node.c
@@ -46,16 +46,20 @@
 }
 
 Node *
-addstmt(Node *np)
+addstmt(Node *np, int flag)
 {
+	if (curstmt)
+		np->next = curstmt->next;
+	np->prev = curstmt;
+
 	if (!curfun->u.stmt)
 		curfun->u.stmt = np;
 	else
 		curstmt->next = np;
-	np->next = NULL;
-	np->prev = curstmt;
-	curstmt = np;
 
+	if (flag == SETCUR)
+		curstmt = np;
+
 	return np;
 }
 
@@ -81,12 +85,6 @@
 nextstmt(void)
 {
 	return curstmt = curstmt->next;
-}
-
-Node *
-prevstmt(void)
-{
-	return curstmt = curstmt->prev;
 }
 
 void
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -579,7 +579,7 @@
 	sym->kind = SLABEL;
 	sym->u.stmt = np;
 	np->label = sym;
-	addstmt(np);
+	addstmt(np, SETCUR);
 }
 
 static void
@@ -593,7 +593,7 @@
 		deltree(np);
 		return;
 	}
-	addstmt(np);
+	addstmt(np, SETCUR);
 }
 
 static void