shithub: scc

Download patch

ref: a639f3f1bf3ee7a1645eddac929afc4a253e0c84
parent: d02dde25b6faa53013cbbb9894f1539645093ee5
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Jun 18 05:01:22 EDT 2022

cc1: Simplify unary() and cast()

A variable was passed to decay or not decay, making complex
to know when it had to happen. This new approach uses the NDECAY
flag in the node to detect the situations where the decay was
not needed.

--- a/src/cmd/cc/cc1/expr.c
+++ b/src/cmd/cc/cc1/expr.c
@@ -540,9 +540,16 @@
 address(int op, Node *np)
 {
 	Node *new;
-	Type *tp = np->type;
+	Type *tp;
 	Symbol *sym = np->sym;
 
+	if ((np->flags & NDECAY) != 0) {
+		new = np->left;
+		free(np);
+		np = new;
+	}
+	tp = np->type;
+
 	/*
 	 * ansi c accepts & applied to a function name, and it generates
 	 * a function pointer
@@ -732,7 +739,7 @@
 	return node(op, rettype, np, par);
 }
 
-static Node *unary(int);
+static Node *unary(void);
 
 static Type *
 typeof(Node *np)
@@ -764,7 +771,7 @@
 		tp = typename();
 		break;
 	default:
-		tp = typeof(unary(0));
+		tp = typeof(unary());
 		break;
 	}
 	expect(')');
@@ -807,10 +814,10 @@
 	}
 }
 
-static Node *cast(int);
+static Node *cast(void);
 
 static Node *
-unary(int needdecay)
+unary(void)
 {
 	Node *(*fun)(int, Node *), *np;
 	int op;
@@ -825,7 +832,7 @@
 	case '*': op = OPTR;  fun = content;      break;
 	case SIZEOF:
 		next();
-		tp = (yytoken == '(') ? sizeexp() : typeof(unary(0));
+		tp = (yytoken == '(') ? sizeexp() : typeof(unary());
 		if (!(tp->prop & TDEFINED))
 			errorp("sizeof applied to an incomplete type");
 		return sizeofnode(tp);
@@ -833,26 +840,24 @@
 	case DEC:
 		op = (yytoken == INC) ? OA_ADD : OA_SUB;
 		next();
-		np = incdec(unary(1), op);
-		goto chk_decay;
+		np = incdec(unary(), op);
+		goto decay;
 	case DEFINED:
 		return defined();
 	default:
 		np = postfix(primary());
-		goto chk_decay;
+		goto decay;
 	}
 
 	next();
-	np = (*fun)(op, cast(op != OADDR));
+	np = (*fun)(op, cast());
 
-chk_decay:
-	if (needdecay)
-		np = decay(np);
-	return np;
+decay:
+	return decay(np);
 }
 
 static Node *
-cast(int needdecay)
+cast(void)
 {
 	Node *tmp, *np;
 	Type *tp;
@@ -859,7 +864,7 @@
 	static int nested;
 
 	if (!accept('('))
-		return unary(needdecay);
+		return unary();
 
 	switch (yytoken) {
 	case TQUALIFIER:
@@ -875,7 +880,7 @@
 		case ARY:
 			error("cast specifies an array type");
 		default:
-			tmp = cast(needdecay);
+			tmp = cast();
 			if ((np = convert(tmp,  tp, 1)) == NULL)
 				error("bad type conversion requested");
 			np->flags &= ~NLVAL;
@@ -901,7 +906,7 @@
 	Node *np, *(*fun)(int, Node *, Node *);
 	int op;
 
-	np = cast(1);
+	np = cast();
 	for (;;) {
 		switch (yytoken) {
 		case '*': op = OMUL; fun = arithmetic; break;
@@ -910,7 +915,7 @@
 		default: return np;
 		}
 		next();
-		np = (*fun)(op, np, cast(1));
+		np = (*fun)(op, np, cast());
 	}
 }