shithub: scc

Download patch

ref: a5086c168c088a82d15d3809152db713c6e21436
parent: cfd05d23cb1e3716f9d59e1ad95bbb29c4da713b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 14 19:11:06 EDT 2015

Force to have a comparision in conditions

It is important for the code generator to have always
a comparision in the conditions, because it means that
the flag is set with the comparision. If the code
generator receives a constant it has to convert the
constant in a flag.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -346,7 +346,7 @@
 /* expr.c */
 extern Node *expr(void), *negate(Node *np), *constexpr(void);
 extern Node *convert(Node *np, Type *tp1, char iscast);
-extern Node *iszero(Node *np), *eval(Node *np), *iconstexpr(void);
+extern Node *eval(Node *np), *iconstexpr(void), *condition(void);
 
 /* cpp.c */
 extern void icpp(void);
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -556,6 +556,7 @@
 	case OGE:  op = OLT;  break;
 	case OLE:  op = OGT;  break;
 	case OGT:  op = OLE;  break;
+	default:   return np;
 	}
 	np->op = op;
 	return np;
@@ -617,14 +618,6 @@
 	return lp;
 }
 
-Node *
-iszero(Node *np)
-{
-	if (isnodecmp(np->op))
-		return np;
-	return compare(ONE, np, constnode(zero));
-}
-
 static Node *
 assignop(char op, Node *lp, Node *rp)
 {
@@ -1158,4 +1151,18 @@
 	}
 
 	return lp;
+}
+
+Node *
+condition(void)
+{
+	Node *np;
+
+	expect('(');
+	np = exp2cond(expr(), 0);
+	if (np->constant)
+		np = node(ONE, inttype, np, constnode(zero));
+	expect(')');
+
+	return np;
 }
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -54,23 +54,6 @@
 	expect(';');
 }
 
-static Node *
-condition(void)
-{
-	extern jmp_buf recover;
-	Node *np;
-
-	expect('(');
-	setsafe(END_COND);
-	if (!setjmp(recover))
-		np = expr();
-	else
-		np = constnode(zero);
-	np = iszero(np);
-	expect(')');
-	return np;
-}
-
 static void
 While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {