[hackers] [scc] Rewrite promote() using the rules in C99 || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 8 Jan 2016 13:13:57 +0100 (CET)

commit 9b896cb060677c0ffdcceb8d0f93684dca16b017
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Jan 6 20:26:40 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Jan 6 20:26:40 2016 +0100

    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.

diff --git a/cc1/arch/i386/arch.h b/cc1/arch/i386/arch.h
index c09c399..abe23b0 100644
--- a/cc1/arch/i386/arch.h
+++ b/cc1/arch/i386/arch.h
_AT_@ -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
diff --git a/cc1/arch/z80/arch.h b/cc1/arch/z80/arch.h
index f2bf49e..7f8b5bf 100644
--- a/cc1/arch/z80/arch.h
+++ b/cc1/arch/z80/arch.h
_AT_@ -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
diff --git a/cc1/cc1.h b/cc1/cc1.h
index b680121..cb79a73 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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
diff --git a/cc1/expr.c b/cc1/expr.c
index d28ebd1..4f7bf5c 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -54,13 +54,25 @@ promote(Node *np)
 {
         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;
Received on Fri Jan 08 2016 - 13:13:57 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 08 2016 - 13:24:19 CET