shithub: scc

Download patch

ref: 3f1b3e7a7b4735ce4b796b174986d52dccdc1fa7
parent: 1c90dd8f2c195e66a1ca9dadfd412a2ccc6dfb55
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Nov 4 05:24:54 EDT 2021

cc2/qbe: Change load() to return Node

Receiving a pointer to the struct were we want to store the
result needs a lot of temporary variables in the code. If
we make that load() returns a struct instead of a pointer to
struct we can remove a lot of these temporaries and
simplify the code. The code generated is pretty similar
because returning structs usually mean passing an additional
pointer to the function called where it can write the results.

--- a/src/cmd/cc/cc2/target/qbe/cgen.c
+++ b/src/cmd/cc/cc2/target/qbe/cgen.c
@@ -120,16 +120,16 @@
 	return np;
 }
 
-static Node *
-load(Type *tp, Node *np, Node *new)
+static Node
+load(Type *tp, Node *np)
 {
 	int op;
+	Node *new;
 	int flags = tp->flags;
 
-	if (flags & (AGGRF|FUNF)) {
-		*new = *np;
-		return new;
-	}
+	if (flags & (AGGRF|FUNF))
+		return *np;
+
 	switch (tp->size) {
 	case 1:
 		op = ASLDSB;
@@ -153,9 +153,10 @@
 	if ((flags & (INTF|SIGNF)) == INTF && tp->size < 8)
 		++op;
 
-	code(op, tmpnode(new, tp), np, NULL);
+	new = tmpnode(NULL, tp);
+	code(op, new, np, NULL);
 
-	return new;
+	return *new;
 }
 
 static Node *rhs(Node *np, Node *new);
@@ -332,7 +333,7 @@
 	if (islhs)
 		*ret = *addr;
 	else
-		load(&np->type, addr, ret);
+		*ret = load(&np->type, addr);
 
 	return ret;
 }
@@ -503,7 +504,8 @@
 	case OMEM:
 	case OREG:
 	case OAUTO:
-		return load(tp, np, ret);
+		*ret = load(tp, np);
+		return ret;
 	case ONEG:
 	case OAND:
 	case OOR:
@@ -618,7 +620,8 @@
 		rhs(l, &aux1);
 		return rhs(r, ret);
 	case OPTR:
-		return load(tp, rhs(l, &aux1), ret);
+		*ret = load(tp, rhs(l, &aux1));
+		return ret;
 	case OADDR:
 		lhs(l, ret);
 		ret->type = *tp;