2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 /* $Id: cstoper.c,v 3.14 1994/06/24 12:02:51 ceriel Exp $ */
6 /* C O N S T A N T E X P R E S S I O N H A N D L I N G */
8 #include "target_sizes.h"
18 long mach_long_sign; /* sign bit of the machine long */
19 int mach_long_size; /* size of long on this machine == sizeof(long) */
20 long full_mask[MAXSIZE];/* full_mask[1] == 0XFF, full_mask[2] == 0XFFFF, .. */
21 arith max_int; /* maximum integer on target machine */
22 arith max_unsigned; /* maximum unsigned on target machine */
24 cstbin(expp, oper, expr)
25 register struct expr **expp, *expr;
27 /* The operation oper is performed on the constant
28 expressions *expp(ld) and expr(ct), and the result restored in
31 register arith o1 = (*expp)->VL_VALUE;
32 register arith o2 = expr->VL_VALUE;
33 int uns = (*expp)->ex_type->tp_unsigned;
35 ASSERT(is_ld_cst(*expp) && is_cp_cst(expr));
42 expr_error(expr, "division by 0");
46 /* this is more of a problem than you might
47 think on C compilers which do not have
50 if (o2 & mach_long_sign) {/* o2 > max_long */
51 o1 = ! (o1 >= 0 || o1 < o2);
52 /* this is the unsigned test
53 o1 < o2 for o2 > max_long
56 else { /* o2 <= max_long */
57 long half, bit, hdiv, hrem, rem;
59 half = (o1 >> 1) & ~mach_long_sign;
61 /* now o1 == 2 * half + bit
68 o1 = 2 * hdiv + (rem < 0 || rem >= o2);
69 /* that is the unsigned compare
70 rem >= o2 for o2 <= max_long
79 expr_error(expr, "modulo by 0");
83 if (o2 & mach_long_sign) {/* o2 > max_long */
84 o1 = (o1 >= 0 || o1 < o2) ? o1 : o1 - o2;
85 /* this is the unsigned test
86 o1 < o2 for o2 > max_long
89 else { /* o2 <= max_long */
90 long half, bit, hrem, rem;
92 half = (o1 >> 1) & ~mach_long_sign;
94 /* now o1 == 2 * half + bit
100 o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
120 o1 &= ~mach_long_sign;
136 o1 = (o1 & mach_long_sign ?
137 (o2 & mach_long_sign ? o1 > o2 : 1) :
138 (o2 & mach_long_sign ? 0 : o1 > o2)
154 o1 = (o1 & mach_long_sign ?
155 (o2 & mach_long_sign ? o1 >= o2 : 1) :
156 (o2 & mach_long_sign ? 0 : o1 >= o2)
178 (*expp)->VL_VALUE = o1;
180 (*expp)->ex_flags |= expr->ex_flags;
181 (*expp)->ex_flags &= ~EX_PARENS;
182 free_expression(expr);
186 register struct expr *expr;
188 /* The constant value of the expression expr is made to
189 conform to the size of the type of the expression.
191 register arith o1 = expr->VL_VALUE;
192 int uns = expr->ex_type->tp_unsigned;
193 int size = (int) expr->ex_type->tp_size;
195 ASSERT(expr->ex_class == Value);
196 if (expr->ex_type->tp_fund == POINTER) {
197 /* why warn on "ptr-3" ?
198 This quick hack fixes it
203 if (o1 & ~full_mask[size])
205 "overflow in unsigned constant expression");
206 o1 &= full_mask[size];
209 int nbits = (int) (mach_long_size - size) * 8;
210 long remainder = o1 & ~full_mask[size];
212 if (remainder != 0 && remainder != ~full_mask[size])
213 expr_warning(expr, "overflow in constant expression");
214 o1 <<= nbits; /* ??? */
223 register arith bt = (arith)0;
226 bt = (bt << 8) + 0377, i++;
228 fatal("array full_mask too small for this machine");
232 mach_long_sign = 1L << (mach_long_size * 8 - 1);
233 if ((int)long_size < mach_long_size)
234 fatal("sizeof (long) insufficient on this machine");
235 max_int = full_mask[(int)int_size] & ~(1L << ((int)int_size * 8 - 1));
236 max_unsigned = full_mask[(int)int_size];