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 1.6 1994/06/27 07:58:54 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 "trgt_sizes.h"
10 #include <flt_arith.h>
19 arith 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 extern int ResultKnown;
26 cstbin(expp, oper, expr)
27 register struct expr **expp, *expr;
29 /* The operation oper is performed on the constant
30 expressions *expp(ld) and expr(ct), and the result restored in
33 register arith o1 = (*expp)->VL_VALUE;
34 register arith o2 = expr->VL_VALUE;
35 int uns = (*expp)->ex_type->tp_unsigned;
37 ASSERT(is_ld_cst(*expp) && is_cp_cst(expr));
45 expr_error(expr, "division by 0");
47 expr_warning(expr, "division by 0");
52 o1 /= (UNSIGNED_ARITH) o2;
54 /* this is more of a problem than you might
55 think on C compilers which do not have
56 unsigned arith (== long (probably)).
58 if (o2 & arith_sign) {/* o2 > max_arith */
59 o1 = ! (o1 >= 0 || o1 < o2);
60 /* this is the unsigned test
61 o1 < o2 for o2 > max_arith
64 else { /* o2 <= max_arith */
65 arith half, bit, hdiv, hrem, rem;
67 half = (o1 >> 1) & ~arith_sign;
69 /* now o1 == 2 * half + bit
76 o1 = 2 * hdiv + (rem < 0 || rem >= o2);
77 /* that is the unsigned compare
78 rem >= o2 for o2 <= max_arith
89 expr_error(expr, "modulo by 0");
91 expr_warning(expr, "modulo by 0");
96 o1 %= (UNSIGNED_ARITH) o2;
98 if (o2 & arith_sign) {/* o2 > max_arith */
99 o1 = (o1 >= 0 || o1 < o2) ? o1 : o1 - o2;
100 /* this is the unsigned test
101 o1 < o2 for o2 > max_arith
104 else { /* o2 <= max_arith */
105 arith half, bit, hrem, rem;
107 half = (o1 >> 1) & ~arith_sign;
109 /* now o1 == 2 * half + bit
110 and half <= max_arith
114 rem = 2 * hrem + bit;
115 o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
135 o1 = (o1 >> 1) & ~arith_sign;
150 #ifdef UNSIGNED_ARITH
151 o1 = (UNSIGNED_ARITH) o1 > (UNSIGNED_ARITH) o2;
153 o1 = (o1 & arith_sign ?
154 (o2 & arith_sign ? o1 > o2 : 1) :
155 (o2 & arith_sign ? 0 : o1 > o2)
172 #ifdef UNSIGNED_ARITH
173 o1 = (UNSIGNED_ARITH) o1 >= (UNSIGNED_ARITH) o2;
175 o1 = (o1 & arith_sign ?
176 (o2 & arith_sign ? o1 >= o2 : 1) :
177 (o2 & arith_sign ? 0 : o1 >= o2)
200 (*expp)->VL_VALUE = o1;
202 (*expp)->ex_flags |= expr->ex_flags;
203 (*expp)->ex_flags &= ~EX_PARENS;
204 free_expression(expr);
208 register struct expr *expr;
210 /* The constant value of the expression expr is made to
211 conform to the size of the type of the expression.
213 register arith o1 = expr->VL_VALUE;
214 int uns = expr->ex_type->tp_unsigned;
215 int size = (int) expr->ex_type->tp_size;
217 ASSERT(expr->ex_class == Value);
218 if (expr->ex_type->tp_fund == POINTER) {
219 /* why warn on "ptr-3" ?
220 This quick hack fixes it
225 if (o1 & ~full_mask[size])
228 "overflow in unsigned constant expression");
229 o1 &= full_mask[size];
232 int nbits = (int) (arith_size - size) * 8;
233 arith remainder = o1 & ~full_mask[size];
235 if (remainder != 0 && remainder != ~full_mask[size])
237 expr_warning(expr,"overflow in constant expression");
238 o1 = (o1 << nbits) >> nbits; /* ??? */
246 register arith bt = (arith)0;
249 bt = (bt << 8) + 0377, i++;
251 fatal("array full_mask too small for this machine");
254 if ((int)long_size > arith_size)
255 fatal("sizeof (arith) insufficient on this machine");
257 max_int = full_mask[(int)int_size] & ~(1L << ((int)int_size * 8 - 1));
258 max_unsigned = full_mask[(int)int_size];