scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 9b896cb060677c0ffdcceb8d0f93684dca16b017
parent 4957aa7a96d1a90272f7efd1344800d476ab9aac
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 Jan 2016 20:26:40 +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.

Diffstat:
Mcc1/arch/i386/arch.h | 16++++++++++++++++
Mcc1/arch/z80/arch.h | 16++++++++++++++++
Mcc1/cc1.h | 15---------------
Mcc1/expr.c | 22+++++++++++++++++-----
4 files changed, 49 insertions(+), 20 deletions(-)

diff --git 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 diff --git 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 diff --git 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 diff --git a/cc1/expr.c b/cc1/expr.c @@ -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;