6ec57f42afd7600eb4a325eadb6666aa41e620a9
[Ack-5.5.git] / lang / cem / cemcom.ansi / cstoper.c
1 /*
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".
4  */
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         */
7
8 #include        "trgt_sizes.h"
9 #include        "idf.h"
10 #include        <flt_arith.h>
11 #include        "arith.h"
12 #include        "type.h"
13 #include        "label.h"
14 #include        "expr.h"
15 #include        "sizes.h"
16 #include        "Lpars.h"
17 #include        "assert.h"
18
19 arith full_mask[MAXSIZE];/* full_mask[1] == 0XFF, full_mask[2] == 0XFFFF, .. */
20 #ifndef NOCROSS
21 arith max_int;          /* maximum integer on target machine    */
22 arith max_unsigned;     /* maximum unsigned on target machine   */
23 #endif /* NOCROSS */
24 extern int ResultKnown;
25
26 cstbin(expp, oper, expr)
27         register struct expr **expp, *expr;
28 {
29         /*      The operation oper is performed on the constant
30                 expressions *expp(ld) and expr(ct), and the result restored in
31                 *expp.
32         */
33         register arith o1 = (*expp)->VL_VALUE;
34         register arith o2 = expr->VL_VALUE;
35         int uns = (*expp)->ex_type->tp_unsigned;
36
37         ASSERT(is_ld_cst(*expp) && is_cp_cst(expr));
38         switch (oper)   {
39         case '*':
40                 o1 *= o2;
41                 break;
42         case '/':
43                 if (o2 == 0)    {
44                         if (!ResultKnown)
45                                 expr_error(expr, "division by 0");
46                         else
47                                 expr_warning(expr, "division by 0");
48                         break;
49                 }
50                 if (uns)        {
51 #ifdef UNSIGNED_ARITH
52                         o1 /= (UNSIGNED_ARITH) o2;
53 #else
54                         /*      this is more of a problem than you might
55                                 think on C compilers which do not have
56                                 unsigned arith (== long (probably)).
57                         */
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
62                                 */
63                         }
64                         else    {               /* o2 <= max_arith */
65                                 arith half, bit, hdiv, hrem, rem;
66
67                                 half = (o1 >> 1) & ~arith_sign;
68                                 bit = o1 & 01;
69                                 /*      now o1 == 2 * half + bit
70                                         and half <= max_arith
71                                         and bit <= max_arith
72                                 */
73                                 hdiv = half / o2;
74                                 hrem = half % o2;
75                                 rem = 2 * hrem + bit;
76                                 o1 = 2 * hdiv + (rem < 0 || rem >= o2);
77                                 /*      that is the unsigned compare
78                                         rem >= o2 for o2 <= max_arith
79                                 */
80                         }
81 #endif
82                 }
83                 else
84                         o1 /= o2;
85                 break;
86         case '%':
87                 if (o2 == 0)    {
88                         if (!ResultKnown)
89                                 expr_error(expr, "modulo by 0");
90                         else
91                                 expr_warning(expr, "modulo by 0");
92                         break;
93                 }
94                 if (uns)        {
95 #ifdef UNSIGNED_ARITH
96                         o1 %= (UNSIGNED_ARITH) o2;
97 #else
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
102                                 */
103                         }
104                         else    {               /* o2 <= max_arith */
105                                 arith half, bit, hrem, rem;
106
107                                 half = (o1 >> 1) & ~arith_sign;
108                                 bit = o1 & 01;
109                                 /*      now o1 == 2 * half + bit
110                                         and half <= max_arith
111                                         and bit <= max_arith
112                                 */
113                                 hrem = half % o2;
114                                 rem = 2 * hrem + bit;
115                                 o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
116                         }
117 #endif
118                 }
119                 else
120                         o1 %= o2;
121                 break;
122         case '+':
123                 o1 += o2;
124                 break;
125         case '-':
126                 o1 -= o2;
127                 break;
128         case LEFT:
129                 o1 <<= o2;
130                 break;
131         case RIGHT:
132                 if (o2 == 0)
133                         break;
134                 if (uns)        {
135                         o1 = (o1 >> 1) & ~arith_sign;
136                         o1 >>= (o2 - 1);
137                 }
138                 else    o1 >>= o2;
139                 break;
140         case '<':
141                 {
142                         arith tmp = o1;
143
144                         o1 = o2;
145                         o2 = tmp;
146                 }
147                 /* Fall through */
148         case '>':
149                 if (uns)        {
150 #ifdef UNSIGNED_ARITH
151                         o1 = (UNSIGNED_ARITH) o1 > (UNSIGNED_ARITH) o2;
152 #else
153                         o1 = (o1 & arith_sign ?
154                                 (o2 & arith_sign ? o1 > o2 : 1) :
155                                 (o2 & arith_sign ? 0 : o1 > o2)
156                         );
157 #endif
158                 }
159                 else
160                         o1 = o1 > o2;
161                 break;
162         case LESSEQ:
163                 {
164                         arith tmp = o1;
165
166                         o1 = o2;
167                         o2 = tmp;
168                 }
169                 /* Fall through */
170         case GREATEREQ:
171                 if (uns)        {
172 #ifdef UNSIGNED_ARITH
173                         o1 = (UNSIGNED_ARITH) o1 >= (UNSIGNED_ARITH) o2;
174 #else
175                         o1 = (o1 & arith_sign ?
176                                 (o2 & arith_sign ? o1 >= o2 : 1) :
177                                 (o2 & arith_sign ? 0 : o1 >= o2)
178                         );
179 #endif
180                 }
181                 else
182                         o1 = o1 >= o2;
183                 break;
184         case EQUAL:
185                 o1 = o1 == o2;
186                 break;
187         case NOTEQUAL:
188                 o1 = o1 != o2;
189                 break;
190         case '&':
191                 o1 &= o2;
192                 break;
193         case '|':
194                 o1 |= o2;
195                 break;
196         case '^':
197                 o1 ^= o2;
198                 break;
199         }
200         (*expp)->VL_VALUE = o1;
201         cut_size(*expp);
202         (*expp)->ex_flags |= expr->ex_flags;
203         (*expp)->ex_flags &= ~EX_PARENS;
204         free_expression(expr);
205 }
206
207 cut_size(expr)
208         register struct expr *expr;
209 {
210         /*      The constant value of the expression expr is made to
211                 conform to the size of the type of the expression.
212         */
213         register arith o1 = expr->VL_VALUE;
214         int uns = expr->ex_type->tp_unsigned;
215         int size = (int) expr->ex_type->tp_size;
216
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
221                 */
222                 uns = 0;
223         }
224         if (uns) {
225                 if (o1 & ~full_mask[size])
226                     if (!ResultKnown)
227                         expr_warning(expr,
228                                 "overflow in unsigned constant expression");
229                 o1 &= full_mask[size];
230         }
231         else {
232                 int nbits = (int) (arith_size - size) * 8;
233                 arith remainder = o1 & ~full_mask[size];
234
235                 if (remainder != 0 && remainder != ~full_mask[size])
236                     if (!ResultKnown)
237                         expr_warning(expr,"overflow in constant expression");
238                 o1 = (o1 << nbits) >> nbits;            /* ??? */
239         }
240         expr->VL_VALUE = o1;
241 }
242
243 init_cst()
244 {
245         register int i = 0;
246         register arith bt = (arith)0;
247
248         while (!(bt < 0))       {
249                 bt = (bt << 8) + 0377, i++;
250                 if (i == MAXSIZE)
251                         fatal("array full_mask too small for this machine");
252                 full_mask[i] = bt;
253         }
254         if ((int)long_size > arith_size)
255                 fatal("sizeof (arith) insufficient on this machine");
256 #ifndef NOCROSS
257         max_int = full_mask[(int)int_size] & ~(1L << ((int)int_size * 8 - 1));
258         max_unsigned = full_mask[(int)int_size];
259 #endif /* NOCROSS */
260 }