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: 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 */
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.
17 #include "nobitfield.h"
19 #include <flt_arith.h>
31 extern char *symbol2str();
32 extern char options[];
33 extern arith flt_flt2arith();
34 extern label code_string();
36 arithbalance(e1p, oper, e2p) /* 3.1.2.5 */
37 register struct expr **e1p, **e2p;
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.
47 register int t1, t2, u1, u2;
48 int shifting = (oper == LEFT || oper == RIGHT
49 || oper == LEFTAB || oper == RIGHTAB);
52 t1 = any2arith(e1p, oper);
53 t2 = any2arith(e2p, oper);
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)
60 if (!((*e2p)->ex_flags & EX_PTRDIFF) && t2 == LONG
64 /* Now turn off ptrdiff flags */
65 (*e1p)->ex_flags &= ~EX_PTRDIFF;
66 (*e2p)->ex_flags &= ~EX_PTRDIFF;
69 /* Now t1 and t2 are either INT, LONG, FLOAT, DOUBLE, or LNGDBL */
71 /* If any operand has the type long double, the other operand
72 is converted to long double.
74 /* ??? t1 == LNGDBL, t2 == DOUBLE */
77 if (t2 == DOUBLE || t2 == FLOAT)
78 float2float(e2p, lngdbl_type);
80 int2float(e2p, lngdbl_type);
83 } else if (t2 == LNGDBL) {
85 if (t1 == DOUBLE || t1 == FLOAT)
86 float2float(e1p, lngdbl_type);
88 int2float(e1p, lngdbl_type);
92 /* If any operand has the type double, the other operand
93 is converted to double.
97 float2float(e2p, double_type);
98 else if (t2 != DOUBLE)
99 int2float(e2p, double_type);
101 } else if (t2 == DOUBLE) {
103 float2float(e1p, double_type);
104 else if (t1 != DOUBLE)
105 int2float(e1p, double_type);
109 /* If any operand has the type float, the other operand
110 is converted to float.
114 int2float(e2p, float_type);
116 } else if (t2 == FLOAT) {
118 int2float(e1p, float_type);
122 /* Now they are INT or LONG */
123 u1 = (*e1p)->ex_type->tp_unsigned;
124 u2 = (*e2p)->ex_type->tp_unsigned;
126 /* If either operand has type unsigned long int, the other
127 operand is converted to unsigned long int.
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);
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.
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);
146 /* If either operand has type long int, the other operand is con-
149 if (t1 == LONG && t2 != LONG)
150 t2 = int2int(e2p, long_type);
152 if (t2 == LONG && t1 != LONG && !shifting) /* ??? */
153 t1 = int2int(e1p, long_type);
155 u1 = (*e1p)->ex_type->tp_unsigned;
156 u2 = (*e2p)->ex_type->tp_unsigned;
158 /* If either operand has type unsigned int, the other operand
159 is converted to unsigned int.
160 Otherwise, both operands have type int.
162 if (u1 && !u2 && !shifting)
163 t2 = int2int(e2p, (t1 == LONG) ? ulong_type : uint_type);
165 if (!u1 && u2 && !shifting)
166 t1 = int2int(e1p, (t2 == LONG) ? ulong_type : uint_type);
168 if (int_size != pointer_size) {
170 (*e1p)->ex_flags |= EX_PTRDIFF;
171 (*e2p)->ex_flags |= EX_PTRDIFF;
176 relbalance(e1p, oper, e2p)
177 register struct expr **e1p, **e2p;
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
186 register struct expr *e1 = *e1p, *e2 = *e2p;
187 struct expr *tmpexpr;
189 if (e1->ex_type->tp_fund == POINTER
191 && e1->VL_VALUE == 0) {
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) {}
203 && e1->ex_type->tp_fund == VOID
204 && e2->ex_type->tp_fund == VOID) {}
206 arithbalance(e1p, oper, e2p);
209 ch3pointer(expp, oper, tp)
211 register struct type *tp;
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.
217 register struct expr *exp = *expp;
219 if (exp->ex_type->tp_fund == POINTER) {
220 if (exp->ex_type != tp)
221 ch3cast(expp, oper, tp);
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",
229 symbol2str(exp->ex_type->tp_fund));
231 ch3cast(expp, CAST, tp);
234 expr_error(exp, "%s on %s and pointer",
236 symbol2str(exp->ex_type->tp_fund)
238 ch3cast(expp, oper, tp);
243 any2arith(expp, oper)
244 register struct expr **expp;
247 /* Turns any expression into int_type, long_type,
248 float_type, double_type or lngdbl_type.
252 switch (fund = (*expp)->ex_type->tp_fund) {
255 ASSERT((*expp)->ex_type->tp_size <= int_type->tp_size);
257 if ((*expp)->ex_type->tp_unsigned
258 && (*expp)->ex_type->tp_size == int_type->tp_size) {
259 int2int(expp, uint_type);
261 int2int(expp, int_type);
269 /* we do not want this conversion for lint, since we
270 want to keep enums and ints separate
272 int2int(expp, int_type);
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);
288 #endif /* NOBITFIELD */
290 expr_error(*expp, "operator %s on non-numerical operand (%s)",
291 symbol2str(oper), symbol2str(fund));
297 return (*expp)->ex_type->tp_fund;
303 /* the (erroneous) expression *expp is replaced by an
306 register struct expr *exp = *expp;
307 int flags = exp->ex_flags;
309 free_expression(exp);
310 exp = intexpr((arith)0, INT);
311 exp->ex_flags = (flags | EX_ERROR);
316 arith2arith(tp, oper, expr)
319 register struct expr *expr;
321 /* arith2arith constructs a new expression containing a
322 run-time conversion between some arithmetic types.
324 register struct expr *new = new_expr();
326 new->ex_file = expr->ex_file;
327 new->ex_line = expr->ex_line;
329 new->ex_class = Type;
330 return new_oper(tp, new, oper, expr);
336 register struct type *tp;
338 /* The expression *expp, which is of some integral type, is
339 converted to the integral type tp.
341 register struct expr *exp = *expp;
343 if (is_cp_cst(exp)) {
344 register struct type *tp1 = exp->ex_type;
347 if (! tp1->tp_unsigned && tp->tp_unsigned) {
348 /* Avoid "unreal" overflow warnings, such as
353 extern long full_mask[];
354 long remainder = exp->VL_VALUE &
355 ~full_mask[(int)(tp->tp_size)];
357 if (remainder == 0 ||
358 remainder == ~full_mask[(int)(tp->tp_size)]) {
359 exp->VL_VALUE &= ~remainder;
365 exp = arith2arith(tp, INT2INT, exp);
368 return exp->ex_type->tp_fund;
371 /* With compile-time constants, we don't set fp_used, since this is done
372 * only when necessary in eval.c.
375 register struct expr **expp;
378 /* The expression *expp, which is of some integral type, is
379 converted to the floating type tp.
381 register struct expr *exp = *expp;
382 int uns = exp->ex_type->tp_unsigned;
384 if (is_cp_cst(exp)) {
386 exp->ex_class = Float;
387 flt_arith2flt(exp->VL_VALUE, &(exp->FL_ARITH), uns);
391 *expp = arith2arith(tp, INT2FLOAT, *expp);
399 /* The expression *expp, which is of some floating type, is
400 converted to the integral type tp.
402 register struct expr *ex = *expp;
405 arith ar = flt_flt2arith(&ex->FL_ARITH, tp->tp_unsigned);
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");
412 /* The following lines are copied from fill_int_expr */
413 ex->ex_class = Value;
414 ex->VL_CLASS = Const;
419 *expp = arith2arith(tp, FLOAT2INT, ex);
423 float2float(expp, tp)
424 register struct expr **expp;
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.
433 if (is_fp_cst(*expp))
434 (*expp)->ex_type = tp;
437 *expp = arith2arith(tp, FLOAT2FLOAT, *expp);
442 register struct expr *exp;
444 /* The expression, which must be an array, is converted
447 exp->ex_type = construct_type(POINTER, exp->ex_type->tp_up
448 , /* exp->ex_type->tp_typequal */ 0
449 , (arith)0, NO_PROTO);
452 function2pointer(exp)
453 register struct expr *exp;
455 /* The expression, which must be a function, is converted
456 to a pointer to the function.
458 exp->ex_type = construct_type(POINTER, exp->ex_type, 0,
463 register struct expr *ex;
465 /* The expression, which must be a string constant, is converted
466 to a pointer to the string-containing area.
470 lbl = code_string(ex->SG_VALUE, ex->SG_LEN);
471 ex->ex_class = Value;
472 ex->VL_CLASS = Label;
474 ex->VL_VALUE = (arith)0;
477 opnd2integral(expp, oper)
478 register struct expr **expp;
481 register int fund = (*expp)->ex_type->tp_fund;
483 if (fund != INT && fund != LONG) {
484 expr_error(*expp, "%s operand to %s",
485 symbol2str(fund), symbol2str(oper));
491 opnd2logical(expp, oper)
492 register struct expr **expp;
495 int fund = (*expp)->ex_type->tp_fund;
497 if (fund == FUNCTION || fund == ARRAY) {
498 expr_warning(*expp, "%s operand to %s",
501 if (fund == FUNCTION) function2pointer(*expp);
502 else array2pointer(*expp);
508 #endif /* NOBITFIELD */
509 switch (fund = (*expp)->ex_type->tp_fund) {
521 expr_error(*expp, "%s operand to %s",
522 symbol2str(fund), symbol2str(oper));
529 opnd2test(expp, oper)
530 register struct expr **expp;
532 opnd2logical(expp, oper);
533 if ((*expp)->ex_class == Oper) {
534 switch((*expp)->OP_OPER) {
543 case OR: /* && and || also impose a test */
544 /* It is already a test */
547 opnd2test(&((*expp)->OP_RIGHT), oper);
551 ch3bin(expp, NOTEQUAL, intexpr((arith)0, INT));
555 register struct expr **expp;
560 if (oper == SIZEOF || oper == ADDRESSOF) return;
562 switch ((*expp)->ex_type->tp_fund) {
566 /* case FLOAT: /* not necessary anymore */
567 any2arith(expp, oper);
570 array2pointer(*expp);
573 if ((*expp)->ex_class == String)
574 string2pointer(*expp);
577 function2pointer(*expp);
583 #endif /* NOBITFIELD */
588 register struct expr **expp;
590 /* To handle default argument promotions
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);
602 register struct expr **expp;
604 /* The expression to extract the bitfield value from the
605 memory word is put in the tree.
607 register struct type *tp = (*expp)->ex_type->tp_up;
608 register struct field *fd = (*expp)->ex_type->tp_field;
610 (*expp)->ex_type = word_type;
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));
618 else { /* take care of the sign bit: sign extend if needed */
619 arith other_bits = (int)word_size * 8 - fd->fd_width;
622 intexpr(other_bits - fd->fd_shift,
625 ch3bin(expp, RIGHT, intexpr(other_bits, INT));
628 #endif /* NOBITFIELD */
630 /* switch_sign_fp() negates the given floating constant expression,
631 * and frees the string representing the old value.
634 register struct expr *expr;
636 flt_umin(&(expr->FL_ARITH));