shithub: scc

Download patch

ref: 9b896cb060677c0ffdcceb8d0f93684dca16b017
parent: 4957aa7a96d1a90272f7efd1344800d476ab9aac
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jan 6 15:26:40 EST 2016

Rewrite promote() using the rules in C99

C99 indicates that signed and unsigned types of the same
size has the same rank, and promotions will be done
to int or unsigned depending of the ranges of the original
type and the ranges of int and unsigned.

--- a/cc1/arch/i386/arch.h
+++ b/cc1/arch/i386/arch.h
@@ -1,4 +1,20 @@
 
+#define RANK_BOOL    0
+#define RANK_SCHAR   1
+#define RANK_UCHAR   1
+#define RANK_CHAR    1
+#define RANK_SHORT   2
+#define RANK_USHORT  2
+#define RANK_INT     3
+#define RANK_UINT    3
+#define RANK_LONG    4
+#define RANK_ULONG   4
+#define RANK_LLONG   5
+#define RANK_ULLONG  5
+#define RANK_FLOAT   6
+#define RANK_DOUBLE  7
+#define RANK_LDOUBLE 8
+
 #define TINT        long long
 #define TUINT       unsigned long long
 #define TFLOAT      double
--- a/cc1/arch/z80/arch.h
+++ b/cc1/arch/z80/arch.h
@@ -1,4 +1,20 @@
 
+#define RANK_BOOL    0
+#define RANK_SCHAR   1
+#define RANK_UCHAR   1
+#define RANK_CHAR    1
+#define RANK_SHORT   2
+#define RANK_USHORT  2
+#define RANK_INT     3
+#define RANK_UINT    3
+#define RANK_LONG    4
+#define RANK_ULONG   4
+#define RANK_LLONG   5
+#define RANK_ULLONG  5
+#define RANK_FLOAT   6
+#define RANK_DOUBLE  7
+#define RANK_LDOUBLE 8
+
 #define TINT        long long
 #define TUINT       unsigned long long
 #define TFLOAT      double
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -8,21 +8,6 @@
 
 #define GLOBALCTX 0
 
-#define RANK_BOOL    0
-#define RANK_SCHAR   1
-#define RANK_UCHAR   2
-#define RANK_CHAR    3
-#define RANK_SHORT   4
-#define RANK_USHORT  5
-#define RANK_INT     6
-#define RANK_UINT    7
-#define RANK_LONG    8
-#define RANK_ULONG   9
-#define RANK_LLONG   10
-#define RANK_ULLONG  11
-#define RANK_FLOAT   12
-#define RANK_DOUBLE  13
-#define RANK_LDOUBLE 15
 
 /*
  * Definition of structures
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -54,13 +54,25 @@
 {
 	Type *tp;
 	Node *new;
-	unsigned r, ur = uinttype->n.rank;
+	unsigned r;
+	struct limits *lim, *ilim;
 
 	tp = np->type;
-	r = tp->n.rank;
-	if  (r > ur || tp == inttype || tp == uinttype)
-		return np;
-	tp = (r == ur) ? uinttype : inttype;
+
+	switch (tp->op) {
+	case INT:
+		if (tp->n.rank >= inttype->n.rank)
+			return np;
+		lim = getlimits(tp);
+		ilim = getlimits(inttype);
+		tp = (lim->max.i <= ilim->max.i) ? inttype : uinttype;
+		break;
+	case FLOAT:
+		tp = doubletype;
+		break;
+	default:
+		abort();
+	}
 	if ((new = convert(np, tp, 1)) != NULL)
 		return new;
 	return np;