Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / 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 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         */
7
8 #include        "target_sizes.h"
9 #include        "idf.h"
10 #include        "arith.h"
11 #include        "type.h"
12 #include        "label.h"
13 #include        "expr.h"
14 #include        "sizes.h"
15 #include        "Lpars.h"
16 #include        "assert.h"
17
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   */
23
24 cstbin(expp, oper, expr)
25         register struct expr **expp, *expr;
26 {
27         /*      The operation oper is performed on the constant
28                 expressions *expp(ld) and expr(ct), and the result restored in
29                 *expp.
30         */
31         register arith o1 = (*expp)->VL_VALUE;
32         register arith o2 = expr->VL_VALUE;
33         int uns = (*expp)->ex_type->tp_unsigned;
34
35         ASSERT(is_ld_cst(*expp) && is_cp_cst(expr));
36         switch (oper)   {
37         case '*':
38                 o1 *= o2;
39                 break;
40         case '/':
41                 if (o2 == 0)    {
42                         expr_error(expr, "division by 0");
43                         break;
44                 }
45                 if (uns)        {
46                         /*      this is more of a problem than you might
47                                 think on C compilers which do not have
48                                 unsigned long.
49                         */
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
54                                 */
55                         }
56                         else    {               /* o2 <= max_long */
57                                 long half, bit, hdiv, hrem, rem;
58
59                                 half = (o1 >> 1) & ~mach_long_sign;
60                                 bit = o1 & 01;
61                                 /*      now o1 == 2 * half + bit
62                                         and half <= max_long
63                                         and bit <= max_long
64                                 */
65                                 hdiv = half / o2;
66                                 hrem = half % o2;
67                                 rem = 2 * hrem + bit;
68                                 o1 = 2 * hdiv + (rem < 0 || rem >= o2);
69                                 /*      that is the unsigned compare
70                                         rem >= o2 for o2 <= max_long
71                                 */
72                         }
73                 }
74                 else
75                         o1 /= o2;
76                 break;
77         case '%':
78                 if (o2 == 0)    {
79                         expr_error(expr, "modulo by 0");
80                         break;
81                 }
82                 if (uns)        {
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
87                                 */
88                         }
89                         else    {               /* o2 <= max_long */
90                                 long half, bit, hrem, rem;
91
92                                 half = (o1 >> 1) & ~mach_long_sign;
93                                 bit = o1 & 01;
94                                 /*      now o1 == 2 * half + bit
95                                         and half <= max_long
96                                         and bit <= max_long
97                                 */
98                                 hrem = half % o2;
99                                 rem = 2 * hrem + bit;
100                                 o1 = (rem < 0 || rem >= o2) ? rem - o2 : rem;
101                         }
102                 }
103                 else
104                         o1 %= o2;
105                 break;
106         case '+':
107                 o1 += o2;
108                 break;
109         case '-':
110                 o1 -= o2;
111                 break;
112         case LEFT:
113                 o1 <<= o2;
114                 break;
115         case RIGHT:
116                 if (o2 == 0)
117                         break;
118                 if (uns)        {
119                         o1 >>= 1;
120                         o1 &= ~mach_long_sign;
121                         o1 >>= (o2-1);
122                 }
123                 else
124                         o1 >>= o2;
125                 break;
126         case '<':
127                 {
128                         arith tmp = o1;
129
130                         o1 = o2;
131                         o2 = tmp;
132                 }
133                 /* Fall through */
134         case '>':
135                 if (uns)        {
136                         o1 = (o1 & mach_long_sign ?
137                                 (o2 & mach_long_sign ? o1 > o2 : 1) :
138                                 (o2 & mach_long_sign ? 0 : o1 > o2)
139                         );
140                 }
141                 else
142                         o1 = o1 > o2;
143                 break;
144         case LESSEQ:
145                 {
146                         arith tmp = o1;
147
148                         o1 = o2;
149                         o2 = tmp;
150                 }
151                 /* Fall through */
152         case GREATEREQ:
153                 if (uns)        {
154                         o1 = (o1 & mach_long_sign ?
155                                 (o2 & mach_long_sign ? o1 >= o2 : 1) :
156                                 (o2 & mach_long_sign ? 0 : o1 >= o2)
157                         );
158                 }
159                 else
160                         o1 = o1 >= o2;
161                 break;
162         case EQUAL:
163                 o1 = o1 == o2;
164                 break;
165         case NOTEQUAL:
166                 o1 = o1 != o2;
167                 break;
168         case '&':
169                 o1 &= o2;
170                 break;
171         case '|':
172                 o1 |= o2;
173                 break;
174         case '^':
175                 o1 ^= o2;
176                 break;
177         }
178         (*expp)->VL_VALUE = o1;
179         cut_size(*expp);
180         (*expp)->ex_flags |= expr->ex_flags;
181         (*expp)->ex_flags &= ~EX_PARENS;
182         free_expression(expr);
183 }
184
185 cut_size(expr)
186         register struct expr *expr;
187 {
188         /*      The constant value of the expression expr is made to
189                 conform to the size of the type of the expression.
190         */
191         register arith o1 = expr->VL_VALUE;
192         int uns = expr->ex_type->tp_unsigned;
193         int size = (int) expr->ex_type->tp_size;
194
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
199                 */
200                 uns = 0;
201         }
202         if (uns) {
203                 if (o1 & ~full_mask[size])
204                         expr_warning(expr,
205                                 "overflow in unsigned constant expression");
206                 o1 &= full_mask[size];
207         }
208         else {
209                 int nbits = (int) (mach_long_size - size) * 8;
210                 long remainder = o1 & ~full_mask[size];
211
212                 if (remainder != 0 && remainder != ~full_mask[size])
213                         expr_warning(expr, "overflow in constant expression");
214                 o1 <<= nbits;           /* ??? */
215                 o1 >>= nbits;
216         }
217         expr->VL_VALUE = o1;
218 }
219
220 init_cst()
221 {
222         register int i = 0;
223         register arith bt = (arith)0;
224
225         while (!(bt < 0))       {
226                 bt = (bt << 8) + 0377, i++;
227                 if (i == MAXSIZE)
228                         fatal("array full_mask too small for this machine");
229                 full_mask[i] = bt;
230         }
231         mach_long_size = i;
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];
237 }