shithub: scc

Download patch

ref: 76bddee887ffd0d602cbbf5fc8e95869249943c5
parent: d3898439422b292cc2220d15e5bf227d9a203e47
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Sep 26 07:33:40 EDT 2016

[cc2] Fix wafting switches nodes

We need to have all the nodes related to a switch table
together, because otherwise it is impossible to build
a jump table. The code was wrong and it was generating
corrupted statement lists.

--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -339,20 +339,36 @@
 	push(np);
 }
 
+/*
+ * Move np (which is a OCASE/ODEFAULT/OESWITCH) to be contigous with
+ * the last switch table. It is a bit ugly to touch directly curstmt
+ * here, but moving this function to node.c is worse, because we are
+ * putting knowledge of how the text is parsed into the node
+ * represtation module.
+ */
 static void
 waft(Node *np)
 {
-	Node *p;
+	Node *lastcase, *next;;
 	struct swtch *cur;
+	extern Node *curstmt;
 
 	if (swp == swtbl)
 		error(EWTACKU);
 
 	cur = swp - 1;
-	p = cur->last;
-	np->next = p->next;
-	np->prev = p;
-	p->next = np;
+	lastcase = cur->last;
+	next = lastcase->next;
+
+	np->next = next;
+	np->prev = lastcase;
+
+	if (next)
+		next->prev = np;
+	lastcase->next = np;
+
+	if (curstmt == cur->last)
+		curstmt = np;
 	cur->last = np;
 	cur->nr++;
 }
@@ -361,13 +377,17 @@
 bswitch(char *token, union tokenop u)
 {
 	struct swtch *cur;
+	Node *np = newnode(u.op);
 
 	if (swp == &swtbl[NR_BLOCK+1])
 		error(EWTACKO);
 	cur = swp++;
 	cur->nr = 0;
-	jump(token, u);
-	cur->first = cur->last = push(pop());
+
+	eval(strtok(NULL, "\t\n"));
+	np->left = pop();
+
+	push(cur->first = cur->last = np);
 }
 
 static void
@@ -379,7 +399,7 @@
 		error(EWTACKU);
 	jump(token, u);
 	waft(pop());
-	cur = swp--;
+	cur = --swp;
 	cur->first->u.i = cur->nr;
 }