shithub: scc

Download patch

ref: 71d0843d509fd2d0a14717bfc6dd30617a1aaba7
parent: 42fd6480b200aab21dc5118050b8f4753cd25967
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Sep 2 09:58:29 EDT 2015

Move cmp() from fold.c to expr.c

This movement allows to remove the ugly macro SYMICMP.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -371,6 +371,7 @@
 extern Node *exp2cond(Node *np, char neg);
 extern bool isnodecmp(int op);
 extern int negop(int op);
+extern bool cmpnode(Node *np, TUINT val);
 
 /* cpp.c */
 extern void icpp(void);
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -7,12 +7,29 @@
 #include "../inc/sizes.h"
 #include "cc1.h"
 
+Node *expr(void);
 
-#define SYMICMP(sym, val) (((sym)->type->sign) ?         \
-	(sym)->u.i == (val) : (sym)->u.u == (val))
+bool
+cmpnode(Node *np, TUINT val)
+{
+	Symbol *sym;
+	Type *tp;
 
-Node *expr(void);
+	if (!np || !np->constant)
+		return 0;
+	sym = np->sym;
+	tp = sym->type;
 
+	switch (tp->op) {
+	case PTR:
+	case INT:
+		return ((tp->sign) ? sym->u.i : sym->u.u) == val;
+	case FLOAT:
+		return sym->u.f == val;
+	}
+	return 0;
+}
+
 bool
 isnodecmp(int op)
 {
@@ -270,7 +287,7 @@
 {
 	switch (BTYPE(rp)) {
 	case INT:
-		if (rp->constant && SYMICMP(rp->sym, 0))
+		if (cmpnode(rp, 0))
 			rp = convert(rp, pvoidtype, 1);
 		break;
 	case PTR:
@@ -402,12 +419,10 @@
 	lp = eval(lp);
 	rp = eval(rp);
 
-	if (BTYPE(rp) == INT && BTYPE(lp) == PTR &&
-	    rp->constant && SYMICMP(rp->sym, 0)) {
+	if (BTYPE(rp) == INT && BTYPE(lp) == PTR && cmpnode(rp, 0))
 		rp = convert(rp, pvoidtype, 1);
-	} else if ((rp = convert(rp, lp->type, 0)) == NULL) {
+	else if ((rp = convert(rp, lp->type, 0)) == NULL)
 		errorp("incompatible types when assigning");
-	}
 
 	return node(op, lp->type, lp, rp);
 }
--- a/cc1/fold.c
+++ b/cc1/fold.c
@@ -369,27 +369,6 @@
 	}
 }
 
-static bool
-cmp(Node *np, int val)
-{
-	Symbol *sym;
-	Type *tp;
-
-	if (!np->constant)
-		return 0;
-	sym = np->sym;
-	tp = sym->type;
-
-	switch (tp->op) {
-	case PTR:
-	case INT:
-		return ((tp->sign) ? sym->u.i : sym->u.u) == val;
-	case FLOAT:
-		return sym->u.f == val;
-	}
-	return 0;
-}
-
 static TUINT
 ones(int n)
 {
@@ -420,12 +399,12 @@
 		val = 1;
 		break;
 	case OBAND:
-		if (cmp(lp, ones(lp->type->size * 8)))
+		if (cmpnode(lp, ones(lp->type->size * 8)))
 			goto free_right;
 	default:
 		return NULL;
 	}
-	if (!cmp(rp, val))
+	if (!cmpnode(rp, val))
 		return NULL;
 free_right:
 	freetree(rp);