shithub: scc

Download patch

ref: 6d3d7af1e88c1e466579c95b6538d0f2c9bebd8a
parent: 27f24349d56b76da526aa232dbb686ad792a055a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Mar 15 11:26:55 EDT 2022

cc2-qbe: Fix parameters handling

The fix in 688c9c87 that solved memory corruption problems
also modified the behaviour of the local symbol list.
It is simpler and it makes the target code independent
of the order in the list.

--- a/src/cmd/cc/cc2/target/qbe/cgen.c
+++ b/src/cmd/cc/cc2/target/qbe/cgen.c
@@ -432,19 +432,25 @@
 	Symbol *p;
 
 	/* allocate stack space for parameters */
-	for (p = locals; p && (p->type.flags & PARF) != 0; p = p->next)
+	for (p = locals; p; p = p->next) {
+		if ((p->type.flags & PARF) == 0)
+			continue;
 		code(ASALLOC, label2node(&aux, p), NULL, NULL);
+	}
 
 	/* allocate stack space for local variables) */
-	for ( ; p && p->id != TMPSYM; p = p->next) {
-		if (p->kind != SAUTO)
+	for (p = locals; p; p = p->next) {
+		if ((p->type.flags & PARF) != 0)
 			continue;
+		if (p->kind != SAUTO || p->id == TMPSYM)
+			continue;
 		code(ASALLOC, label2node(&aux, p), NULL, NULL);
 	}
+
 	/* store formal parameters in parameters */
 	for (p = locals; p; p = p->next) {
 		if ((p->type.flags & PARF) == 0)
-			break;
+			continue;
 		code(ASFORM, label2node(&aux, p), NULL, NULL);
 	}
 	return NULL;
--- a/src/cmd/cc/cc2/target/qbe/code.c
+++ b/src/cmd/cc/cc2/target/qbe/code.c
@@ -358,10 +358,12 @@
 	printf("function %s %s(", size2stack(&curfun->rtype), symname(curfun));
 
 	/* declare formal parameters */
-	for (sep = "", p = locals; p; p = p->next, sep = ",") {
+	sep = "";
+	for (p = locals; p; p = p->next) {
 		if ((p->type.flags & PARF) == 0)
-			break;
+			continue;
 		printf("%s%s %s.val", sep, size2stack(&p->type), symname(p));
+		sep = ",";
 	}
 	printf("%s)\n{\n", (curfun->type.flags&ELLIPS) ? ", ..." : "");