Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / arith.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: arith.c,v 1.28 1994/06/27 07:58:11 ceriel Exp $ */
6 /*      A R I T H M E T I C   C O N V E R S I O N S      */
7
8 /*      This file contains the routines for the various conversions that
9         may befall operands in C. It is structurally a mess, but I haven't
10         decided yet whether I can't find the right structure or the
11         semantics of C is a mess.
12 */
13
14 #include        <alloc.h>
15 #include        "debug.h"
16 #include        "lint.h"
17 #include        "nobitfield.h"
18 #include        "idf.h"
19 #include        <flt_arith.h>
20 #include        "arith.h"
21 #include        "sizes.h"
22 #include        "type.h"
23 #include        "proto.h"
24 #include        "label.h"
25 #include        "expr.h"
26 #include        "Lpars.h"
27 #include        "field.h"
28 #include        "mes.h"
29 #include        "assert.h"
30
31 extern char *symbol2str();
32 extern char options[];
33 extern arith flt_flt2arith();
34 extern label code_string();
35
36 arithbalance(e1p, oper, e2p)    /* 3.1.2.5 */
37         register struct expr **e1p, **e2p;
38         int oper;
39 {
40         /*      The expressions *e1p and *e2p are balanced to be operands
41                 of the arithmetic operator oper.
42                 We check here if the EX_PTRDIFF flag should be retained.
43                 They are set to zero in because one of the opreands might
44                 have a floating type, in which case the flags shouldn't
45                 travel upward in the expression tree.
46         */
47         register int t1, t2, u1, u2;
48         int shifting = (oper == LEFT || oper == RIGHT
49                         || oper == LEFTAB || oper == RIGHTAB);
50         int ptrdiff = 0;
51
52         t1 = any2arith(e1p, oper);
53         t2 = any2arith(e2p, oper);
54
55         if (int_size != pointer_size) {
56                 if (ptrdiff = ((*e1p)->ex_flags & EX_PTRDIFF)
57                             || ((*e2p)->ex_flags & EX_PTRDIFF)) {
58                         if (!((*e1p)->ex_flags & EX_PTRDIFF) && t1 == LONG)
59                                 ptrdiff = 0;
60                         if (!((*e2p)->ex_flags & EX_PTRDIFF) && t2 == LONG
61                             && !shifting)
62                                 ptrdiff = 0;
63                 }
64                 /* Now turn off ptrdiff flags */
65                 (*e1p)->ex_flags &= ~EX_PTRDIFF;
66                 (*e2p)->ex_flags &= ~EX_PTRDIFF;
67         }
68
69         /* Now t1 and t2 are either INT, LONG, FLOAT, DOUBLE, or LNGDBL */
70
71         /*      If any operand has the type long double, the other operand
72                 is converted to long double.
73         */
74         /* ??? t1 == LNGDBL, t2 == DOUBLE */
75         if (t1 == LNGDBL) {
76                 if (t2 != LNGDBL) {
77                     if (t2 == DOUBLE || t2 == FLOAT)
78                         float2float(e2p, lngdbl_type);
79                     else
80                         int2float(e2p, lngdbl_type);
81                 }
82                 return;
83         } else if (t2 == LNGDBL) {
84                 if (t1 != LNGDBL)
85                     if (t1 == DOUBLE || t1 == FLOAT)
86                         float2float(e1p, lngdbl_type);
87                     else
88                         int2float(e1p, lngdbl_type);
89                 return;
90         }
91
92         /*      If any operand has the type double, the other operand
93                 is converted to double.
94         */
95         if (t1 == DOUBLE) {
96                 if (t2 == FLOAT)
97                         float2float(e2p, double_type);
98                 else if (t2 != DOUBLE)
99                         int2float(e2p, double_type);
100                 return;
101         } else if (t2 == DOUBLE) {
102                 if (t1 == FLOAT)
103                         float2float(e1p, double_type);
104                 else if (t1 != DOUBLE)
105                         int2float(e1p, double_type);
106                 return;
107         }
108
109         /*      If any operand has the type float, the other operand
110                 is converted to float.
111         */
112         if (t1 == FLOAT) {
113                 if (t2 != FLOAT)
114                         int2float(e2p, float_type);
115                 return;
116         } else if (t2 == FLOAT) {
117                 if (t1 != FLOAT)
118                         int2float(e1p, float_type);
119                 return;
120         }
121
122         /* Now they are INT or LONG */
123         u1 = (*e1p)->ex_type->tp_unsigned;
124         u2 = (*e2p)->ex_type->tp_unsigned;
125
126         /*      If either operand has type unsigned long int, the other
127                 operand is converted to unsigned long int.
128         */
129         if (t1 == LONG && u1 && (t2 != LONG || !u2))
130                 t2 = int2int(e2p, ulong_type);
131         else if (t2 == LONG && u2 && (t1 != LONG || !u1)
132                         && !shifting)   /* ??? */
133                 t1 = int2int(e1p, ulong_type);
134
135         /*      If one operand has type long int and the other has type unsigned
136                 int, if a long int can represent all values of an unsigned int,
137                 the operand of type unsigned int is converted to long int; if
138                 a long int cannot represent all values of an unsigned int,
139                 both operands are converted to unsigned long int.
140         */
141         if (t1 == LONG && t2 == INT && u2)
142                 t2 = int2int(e2p, (int_size<long_size)? long_type : ulong_type);
143         else if (t2 == LONG && t1 == INT && u1 && !shifting)    /* ??? */
144                 t1 = int2int(e1p, (int_size<long_size)? long_type : ulong_type);
145
146         /*      If either operand has type long int, the other operand is con-
147                 verted to long int.
148         */
149         if (t1 == LONG && t2 != LONG)
150                 t2 = int2int(e2p, long_type);
151         else
152         if (t2 == LONG && t1 != LONG && !shifting)      /* ??? */
153                 t1 = int2int(e1p, long_type);
154
155         u1 = (*e1p)->ex_type->tp_unsigned;
156         u2 = (*e2p)->ex_type->tp_unsigned;
157
158         /*      If either operand has type unsigned int, the other operand
159                 is converted to unsigned int.
160                 Otherwise, both operands have type int.
161         */
162         if (u1 && !u2 && !shifting)
163                 t2 = int2int(e2p, (t1 == LONG) ? ulong_type : uint_type);
164         else
165         if (!u1 && u2 && !shifting)
166                 t1 = int2int(e1p, (t2 == LONG) ? ulong_type : uint_type);
167
168         if (int_size != pointer_size) {
169                 if (ptrdiff) {
170                         (*e1p)->ex_flags |= EX_PTRDIFF;
171                         (*e2p)->ex_flags |= EX_PTRDIFF;
172                 }
173         }
174 }
175
176 relbalance(e1p, oper, e2p)
177         register struct expr **e1p, **e2p;
178 {
179         /*      The expressions *e1p and *e2p are balanced to be operands
180                 of the relational operator oper, or the ':'.
181                 Care is taken to switch the operands in case of a
182                 null-pointer constant. This is done so that ch3cast()
183                 allows assignments of a null-pointer to a function
184                 pointer.
185         */
186         register struct expr *e1 = *e1p, *e2 = *e2p;
187         struct expr *tmpexpr;
188
189         if (e1->ex_type->tp_fund == POINTER
190             && is_cp_cst(e1)
191             && e1->VL_VALUE == 0) {
192                 tmpexpr = e1;
193                 e1 = e2;
194                 e2 = tmpexpr;
195         }
196         if (e1->ex_type->tp_fund == POINTER)
197                 ch3pointer(e2p, oper, e1->ex_type);
198         else if (e2->ex_type->tp_fund == POINTER)
199                 ch3pointer(e1p, oper, e2->ex_type);
200         else if (e1->ex_type == e2->ex_type
201                 && e1->ex_type->tp_fund == ENUM) {}
202         else if (oper == ':'
203                     && e1->ex_type->tp_fund == VOID
204                     && e2->ex_type->tp_fund == VOID) {}
205         else
206                 arithbalance(e1p, oper, e2p);
207 }
208
209 ch3pointer(expp, oper, tp)
210         struct expr **expp;
211         register struct type *tp;
212 {
213         /*      Checks whether *expp may be compared to tp using oper,
214                 as described in chapter 3.3.8 and 3.3.9.
215                 tp is known to be a pointer.
216         */
217         register struct expr *exp = *expp;
218
219         if (exp->ex_type->tp_fund == POINTER)   {
220                 if (exp->ex_type != tp)
221                         ch3cast(expp, oper, tp);
222         }
223         else
224         if (is_integral_type(exp->ex_type)) {
225                 if ((oper != EQUAL && oper != NOTEQUAL && oper != ':')
226                     || !(is_cp_cst(exp) && exp->VL_VALUE == 0)) {
227                         expr_error(exp,"%s on %s and pointer",
228                                         symbol2str(oper),
229                                         symbol2str(exp->ex_type->tp_fund));
230                 }
231                 ch3cast(expp, CAST, tp);
232         }
233         else    {
234                 expr_error(exp, "%s on %s and pointer",
235                                 symbol2str(oper),
236                                 symbol2str(exp->ex_type->tp_fund)
237                         );
238                 ch3cast(expp, oper, tp);
239         }
240 }
241
242 int
243 any2arith(expp, oper)
244         register struct expr **expp;
245         register int oper;
246 {
247         /*      Turns any expression into int_type, long_type,
248                 float_type, double_type or lngdbl_type.
249         */
250         int fund;
251
252         switch (fund = (*expp)->ex_type->tp_fund)       {
253         case CHAR:
254         case SHORT:
255                 ASSERT((*expp)->ex_type->tp_size <= int_type->tp_size);
256
257                 if ((*expp)->ex_type->tp_unsigned
258                     && (*expp)->ex_type->tp_size == int_type->tp_size) {
259                         int2int(expp, uint_type);
260                 } else {
261                         int2int(expp, int_type);
262                 }
263                 break;
264         case INT:
265         case LONG:
266                 break;
267         case ENUM:
268 #ifndef LINT
269                 /*      we do not want this conversion for lint, since we
270                         want to keep enums and ints separate
271                 */
272                 int2int(expp, int_type);
273 #endif  /* LINT */
274                 break;
275         case FLOAT:
276 /*      only when it is a parameter and the default promotion should
277         occur. Hence this code is moved to any2parameter().
278                 float2float(expp, double_type);
279                 break;
280 */
281         case DOUBLE:
282         case LNGDBL:
283                 break;
284 #ifndef NOBITFIELD
285         case FIELD:
286                 field2arith(expp);
287                 break;
288 #endif /* NOBITFIELD */
289         default:
290                 expr_error(*expp, "operator %s on non-numerical operand (%s)",
291                         symbol2str(oper), symbol2str(fund));
292         case ERRONEOUS:
293                 erroneous2int(expp);
294                 break;
295         }
296
297         return (*expp)->ex_type->tp_fund;
298 }
299
300 erroneous2int(expp)
301         struct expr **expp;
302 {
303         /*      the (erroneous) expression *expp is replaced by an
304                 int expression
305         */
306         register struct expr *exp = *expp;
307         int flags = exp->ex_flags;
308         
309         free_expression(exp);
310         exp = intexpr((arith)0, INT);
311         exp->ex_flags = (flags | EX_ERROR);
312         *expp = exp;
313 }
314
315 struct expr *
316 arith2arith(tp, oper, expr)
317         struct type *tp;
318         int oper;
319         register struct expr *expr;
320 {
321         /*      arith2arith constructs a new expression containing a
322                 run-time conversion between some arithmetic types.
323         */
324         register struct expr *new = new_expr();
325         
326         new->ex_file = expr->ex_file;
327         new->ex_line = expr->ex_line;
328         new->ex_type = tp;
329         new->ex_class = Type;
330         return new_oper(tp, new, oper, expr);
331 }
332
333 int
334 int2int(expp, tp)
335         struct expr **expp;
336         register struct type *tp;
337 {
338         /*      The expression *expp, which is of some integral type, is
339                 converted to the integral type tp.
340         */
341         register struct expr *exp = *expp;
342         
343         if (is_cp_cst(exp))     {
344                 register struct type *tp1 = exp->ex_type;
345
346                 exp->ex_type = tp;
347                 if (! tp1->tp_unsigned && tp->tp_unsigned) {
348                         /*      Avoid "unreal" overflow warnings, such as
349                                 caused by f.i.:
350                                         unsigned int x = ~0;
351                                         unsigned int y = -1;
352                         */
353                         extern long full_mask[];
354                         long remainder = exp->VL_VALUE &
355                                                 ~full_mask[(int)(tp->tp_size)];
356
357                         if (remainder == 0 ||
358                             remainder == ~full_mask[(int)(tp->tp_size)]) {
359                                 exp->VL_VALUE &= ~remainder;
360                         }
361                 }
362                 cut_size(exp);
363         }
364         else    {
365                 exp = arith2arith(tp, INT2INT, exp);
366         }
367         *expp = exp;
368         return exp->ex_type->tp_fund;
369 }
370
371 /* With compile-time constants, we don't set fp_used, since this is done
372  * only when necessary in eval.c.
373  */
374 int2float(expp, tp)
375         register struct expr **expp;
376         struct type *tp;
377 {
378         /*      The expression *expp, which is of some integral type, is
379                 converted to the floating type tp.
380         */
381         register struct expr *exp = *expp;
382         int uns = exp->ex_type->tp_unsigned;
383         
384         if (is_cp_cst(exp)) {
385                 exp->ex_type = tp;
386                 exp->ex_class = Float;
387                 flt_arith2flt(exp->VL_VALUE, &(exp->FL_ARITH), uns);
388         }
389         else    {
390                 fp_used = 1;
391                 *expp = arith2arith(tp, INT2FLOAT, *expp);
392         }
393 }
394
395 float2int(expp, tp)
396         struct expr **expp;
397         struct type *tp;
398 {
399         /*      The expression *expp, which is of some floating type, is
400                 converted to the integral type tp.
401         */
402         register struct expr *ex = *expp;
403         
404         if (is_fp_cst(ex)) {
405                 arith ar = flt_flt2arith(&ex->FL_ARITH, tp->tp_unsigned);
406
407                 if (flt_status == FLT_OVFL)
408                         expr_warning(ex,"overflow in float to int conversion");
409                 else if (flt_status == FLT_UNFL)
410                         expr_warning(ex,"underflow in float to unsigned conversion");
411                 ex->ex_type = tp;
412                 /* The following lines are copied from fill_int_expr */
413                 ex->ex_class = Value;
414                 ex->VL_CLASS = Const;
415                 ex->VL_VALUE = ar;
416                 cut_size(ex);
417         } else {
418                 fp_used = 1;
419                 *expp = arith2arith(tp, FLOAT2INT, ex);
420         }
421 }
422
423 float2float(expp, tp)
424         register struct expr **expp;
425         struct type *tp;
426 {
427         /*      The expression *expp, which is of some floating type, is
428                 converted to the floating type tp.
429                 There is no need for an explicit conversion operator
430                 if the expression is a constant.
431         */
432         
433         if (is_fp_cst(*expp))
434                 (*expp)->ex_type = tp;
435         else {
436                 fp_used = 1;
437                 *expp = arith2arith(tp, FLOAT2FLOAT, *expp);
438         }
439 }
440
441 array2pointer(exp)
442         register struct expr *exp;
443 {
444         /*      The expression, which must be an array, is converted
445                 to a pointer.
446         */
447         exp->ex_type = construct_type(POINTER, exp->ex_type->tp_up
448                                     , /* exp->ex_type->tp_typequal */ 0
449                                     , (arith)0, NO_PROTO);
450 }
451
452 function2pointer(exp)
453         register struct expr *exp;
454 {
455         /*      The expression, which must be a function, is converted
456                 to a pointer to the function.
457         */
458         exp->ex_type = construct_type(POINTER, exp->ex_type, 0,
459                                      (arith)0, NO_PROTO);
460 }
461
462 string2pointer(ex)
463         register struct expr *ex;
464 {
465         /*      The expression, which must be a string constant, is converted
466                 to a pointer to the string-containing area.
467         */
468         label lbl;
469
470         lbl = code_string(ex->SG_VALUE, ex->SG_LEN);
471         ex->ex_class = Value;
472         ex->VL_CLASS = Label;
473         ex->VL_LBL = lbl;
474         ex->VL_VALUE = (arith)0;
475 }
476
477 opnd2integral(expp, oper)
478         register struct expr **expp;
479         int oper;
480 {
481         register int fund = (*expp)->ex_type->tp_fund;
482
483         if (fund != INT && fund != LONG)        {
484                 expr_error(*expp, "%s operand to %s",
485                                 symbol2str(fund), symbol2str(oper));
486                 erroneous2int(expp);
487                 /* fund = INT; */
488         }
489 }
490
491 opnd2logical(expp, oper)
492         register struct expr **expp;
493         int oper;
494 {
495         int fund = (*expp)->ex_type->tp_fund;
496
497         if (fund == FUNCTION || fund == ARRAY) {
498                 expr_warning(*expp, "%s operand to %s",
499                         symbol2str(fund),
500                         symbol2str(oper));
501                 if (fund == FUNCTION) function2pointer(*expp);
502                 else array2pointer(*expp);
503         }
504 #ifndef NOBITFIELD
505         else 
506         if (fund == FIELD)
507                 field2arith(expp);
508 #endif /* NOBITFIELD */
509         switch (fund = (*expp)->ex_type->tp_fund) {
510         case CHAR:
511         case SHORT:
512         case INT:
513         case LONG:
514         case ENUM:
515         case POINTER:
516         case FLOAT:
517         case DOUBLE:
518         case LNGDBL:
519                 break;
520         default:
521                 expr_error(*expp, "%s operand to %s",
522                         symbol2str(fund), symbol2str(oper));
523         case ERRONEOUS:
524                 erroneous2int(expp);
525                 break;
526         }
527 }
528
529 opnd2test(expp, oper)
530         register struct expr **expp;
531 {
532         opnd2logical(expp, oper);
533         if ((*expp)->ex_class == Oper) {
534                 switch((*expp)->OP_OPER) {
535                 case '<':
536                 case '>':
537                 case LESSEQ:
538                 case GREATEREQ:
539                 case EQUAL:
540                 case NOTEQUAL:
541                 case '!':
542                 case AND:
543                 case OR:        /* && and || also impose a test */
544                         /* It is already a test */
545                         return;
546                 case ',':
547                         opnd2test(&((*expp)->OP_RIGHT), oper);
548                         return;
549                 }
550         }
551         ch3bin(expp, NOTEQUAL, intexpr((arith)0, INT));
552 }
553
554 any2opnd(expp, oper)
555         register struct expr **expp;
556 {
557         if (!*expp)
558                 return;
559
560         if (oper == SIZEOF || oper == ADDRESSOF) return;
561
562         switch ((*expp)->ex_type->tp_fund)      {
563         case CHAR:
564         case SHORT:
565         case ENUM:
566         /* case FLOAT:  /* not necessary anymore */
567                 any2arith(expp, oper);
568                 break;
569         case ARRAY:
570                 array2pointer(*expp);
571                 break;
572         case POINTER:
573                 if ((*expp)->ex_class == String)
574                         string2pointer(*expp);
575                 break;
576         case FUNCTION:
577                 function2pointer(*expp);
578                 break;
579 #ifndef NOBITFIELD
580         case FIELD:
581                 field2arith(expp);
582                 break;
583 #endif /* NOBITFIELD */
584         }
585 }
586
587 any2parameter(expp)
588         register struct expr **expp;
589 {
590         /*      To handle default argument promotions
591         */
592         any2opnd(expp, '(');
593         if (int_size != pointer_size)
594             if ((*expp)->ex_flags & EX_PTRDIFF)
595                 expr_warning(*expp, "pointer difference caused long expression");
596         if ((*expp)->ex_type->tp_fund == FLOAT)
597                 float2float(expp, double_type);
598 }
599
600 #ifndef NOBITFIELD
601 field2arith(expp)
602         register struct expr **expp;
603 {
604         /*      The expression to extract the bitfield value from the
605                 memory word is put in the tree.
606         */
607         register struct type *tp = (*expp)->ex_type->tp_up;
608         register struct field *fd = (*expp)->ex_type->tp_field;
609
610         (*expp)->ex_type = word_type;
611
612         if (tp->tp_unsigned)    {       /* don't worry about the sign bit */
613                 if (fd->fd_width >= 8 * (int)word_size)
614                         (*expp)->ex_type = uword_type;
615                 ch3bin(expp, RIGHT, intexpr((arith)fd->fd_shift, INT));
616                 ch3bin(expp, '&', intexpr(fd->fd_mask, INT));
617         }
618         else    {       /* take care of the sign bit: sign extend if needed */
619                 arith other_bits = (int)word_size * 8 - fd->fd_width;
620
621                 ch3bin(expp, LEFT,
622                         intexpr(other_bits - fd->fd_shift,
623                                                 INT)
624                 );
625                 ch3bin(expp, RIGHT, intexpr(other_bits, INT));
626         }
627 }
628 #endif /* NOBITFIELD */
629
630 /*      switch_sign_fp() negates the given floating constant expression,
631  *      and frees the string representing the old value.
632  */
633 switch_sign_fp(expr)
634         register struct expr *expr;
635 {
636         flt_umin(&(expr->FL_ARITH));
637 }