4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
26 #include "expression.h"
28 struct symbol
*current_fn
;
30 static struct symbol
*degenerate(struct expression
*expr
);
31 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
33 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
35 struct symbol
*sym
= expr
->symbol
;
36 struct symbol
*base_type
;
39 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
43 examine_symbol_type(sym
);
45 base_type
= sym
->ctype
.base_type
;
47 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
51 /* The type of a symbol is the symbol itself! */
54 /* enums can be turned into plain values */
55 if (sym
->type
!= SYM_ENUM
) {
56 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
58 addr
->symbol_name
= expr
->symbol_name
;
59 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
60 expr
->type
= EXPR_PREOP
;
64 } else if (base_type
->bit_size
< bits_in_int
) {
65 /* ugly - we need to force sizeof for these guys */
66 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
67 e
->value
= sym
->value
;
69 expr
->type
= EXPR_PREOP
;
73 expr
->type
= EXPR_VALUE
;
74 expr
->value
= sym
->value
;
76 expr
->ctype
= base_type
;
80 static struct symbol
*evaluate_string(struct expression
*expr
)
82 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
83 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
84 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
85 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
86 unsigned int length
= expr
->string
->length
;
88 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
89 sym
->bit_size
= bits_in_char
* length
;
90 sym
->ctype
.alignment
= 1;
91 sym
->ctype
.modifiers
= MOD_STATIC
;
92 sym
->ctype
.base_type
= array
;
93 sym
->initializer
= initstr
;
96 initstr
->string
= expr
->string
;
98 array
->array_size
= sym
->array_size
;
99 array
->bit_size
= bits_in_char
* length
;
100 array
->ctype
.alignment
= 1;
101 array
->ctype
.modifiers
= MOD_STATIC
;
102 array
->ctype
.base_type
= &char_ctype
;
105 addr
->ctype
= &lazy_ptr_ctype
;
107 expr
->type
= EXPR_PREOP
;
114 static inline struct symbol
*integer_promotion(struct symbol
*type
)
116 unsigned long mod
= type
->ctype
.modifiers
;
119 if (type
->type
== SYM_NODE
)
120 type
= type
->ctype
.base_type
;
121 if (type
->type
== SYM_ENUM
)
122 type
= type
->ctype
.base_type
;
123 width
= type
->bit_size
;
124 if (type
->type
== SYM_BITFIELD
)
125 type
= type
->ctype
.base_type
;
126 mod
= type
->ctype
.modifiers
;
127 if (width
< bits_in_int
)
130 /* If char/short has as many bits as int, it still gets "promoted" */
131 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
133 if (mod
& MOD_UNSIGNED
)
140 * integer part of usual arithmetic conversions:
141 * integer promotions are applied
142 * if left and right are identical, we are done
143 * if signedness is the same, convert one with lower rank
144 * unless unsigned argument has rank lower than signed one, convert the
146 * if signed argument is bigger than unsigned one, convert the unsigned.
147 * otherwise, convert signed.
149 * Leaving aside the integer promotions, that is equivalent to
150 * if identical, don't convert
151 * if left is bigger than right, convert right
152 * if right is bigger than left, convert right
153 * otherwise, if signedness is the same, convert one with lower rank
154 * otherwise convert the signed one.
156 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
158 unsigned long lmod
, rmod
;
160 left
= integer_promotion(left
);
161 right
= integer_promotion(right
);
166 if (left
->bit_size
> right
->bit_size
)
169 if (right
->bit_size
> left
->bit_size
)
172 lmod
= left
->ctype
.modifiers
;
173 rmod
= right
->ctype
.modifiers
;
174 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
175 if (lmod
& MOD_UNSIGNED
)
177 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
185 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
187 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
191 * This gets called for implicit casts in assignments and
192 * integer promotion. We often want to try to move the
193 * cast down, because the ops involved may have been
194 * implicitly cast up, and we can get rid of the casts
197 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
199 struct expression
*expr
;
202 * See if we can simplify the op. Move the cast down.
206 if (old
->op
== '~') {
208 old
->unop
= cast_to(old
->unop
, type
);
213 case EXPR_IMPLIED_CAST
:
214 if (old
->ctype
->bit_size
>= type
->bit_size
) {
215 struct expression
*orig
= old
->cast_expression
;
216 if (same_cast_type(orig
->ctype
, type
))
218 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
220 old
->cast_type
= type
;
230 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
232 expr
->cast_type
= type
;
233 expr
->cast_expression
= old
;
237 static int is_type_type(struct symbol
*type
)
239 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
242 static int is_ptr_type(struct symbol
*type
)
244 if (type
->type
== SYM_NODE
)
245 type
= type
->ctype
.base_type
;
246 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
249 static inline int is_float_type(struct symbol
*type
)
251 if (type
->type
== SYM_NODE
)
252 type
= type
->ctype
.base_type
;
253 return type
->ctype
.base_type
== &fp_type
;
256 static inline int is_byte_type(struct symbol
*type
)
258 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
261 static inline int is_string_type(struct symbol
*type
)
263 if (type
->type
== SYM_NODE
)
264 type
= type
->ctype
.base_type
;
265 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
268 static struct symbol
*bad_expr_type(struct expression
*expr
)
270 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
271 switch (expr
->type
) {
274 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
275 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
279 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
288 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
290 struct expression
*left
= *lp
, *right
= *rp
;
291 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
293 if (ltype
->type
== SYM_NODE
)
294 ltype
= ltype
->ctype
.base_type
;
295 if (rtype
->type
== SYM_NODE
)
296 rtype
= rtype
->ctype
.base_type
;
297 if (is_float_type(ltype
)) {
298 if (is_int_type(rtype
))
300 if (is_float_type(rtype
)) {
301 unsigned long lmod
= ltype
->ctype
.modifiers
;
302 unsigned long rmod
= rtype
->ctype
.modifiers
;
303 lmod
&= MOD_LONG
| MOD_LONGLONG
;
304 rmod
&= MOD_LONG
| MOD_LONGLONG
;
314 if (!is_float_type(rtype
) || !is_int_type(ltype
))
317 *lp
= cast_to(left
, rtype
);
320 *rp
= cast_to(right
, ltype
);
324 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
326 struct expression
*left
= *lp
, *right
= *rp
;
327 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
329 if (ltype
->type
== SYM_NODE
)
330 ltype
= ltype
->ctype
.base_type
;
331 if (rtype
->type
== SYM_NODE
)
332 rtype
= rtype
->ctype
.base_type
;
333 if (is_int_type(ltype
) && is_int_type(rtype
)) {
334 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
336 /* Don't bother promoting same-size entities, it only adds clutter */
337 if (ltype
->bit_size
!= ctype
->bit_size
)
338 *lp
= cast_to(left
, ctype
);
339 if (rtype
->bit_size
!= ctype
->bit_size
)
340 *rp
= cast_to(right
, ctype
);
346 static int restricted_value(struct expression
*v
, struct symbol
*type
)
348 if (v
->type
!= EXPR_VALUE
)
355 static int restricted_binop(int op
, struct symbol
*type
)
364 case SPECIAL_NOTEQUAL
:
365 case SPECIAL_AND_ASSIGN
:
366 case SPECIAL_OR_ASSIGN
:
367 case SPECIAL_XOR_ASSIGN
:
374 static int restricted_unop(int op
, struct symbol
*type
)
376 if (op
== '~' && type
->bit_size
>= bits_in_int
)
383 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
385 struct expression
*left
= *lp
, *right
= *rp
;
386 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
387 struct symbol
*type
= NULL
;
389 if (ltype
->type
== SYM_NODE
)
390 ltype
= ltype
->ctype
.base_type
;
391 if (ltype
->type
== SYM_ENUM
)
392 ltype
= ltype
->ctype
.base_type
;
393 if (rtype
->type
== SYM_NODE
)
394 rtype
= rtype
->ctype
.base_type
;
395 if (rtype
->type
== SYM_ENUM
)
396 rtype
= rtype
->ctype
.base_type
;
397 if (is_restricted_type(ltype
)) {
398 if (is_restricted_type(rtype
)) {
402 if (!restricted_value(right
, ltype
))
405 } else if (is_restricted_type(rtype
)) {
406 if (!restricted_value(left
, rtype
))
411 if (restricted_binop(op
, type
))
416 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
418 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
419 if (!ctype
&& float_ok
)
420 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
422 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
427 return bad_expr_type(expr
);
430 static inline int lvalue_expression(struct expression
*expr
)
432 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
435 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
437 struct expression
*i
= *ip
;
438 struct symbol
*ptr_type
= ctype
;
441 if (ptr_type
->type
== SYM_NODE
)
442 ptr_type
= ptr_type
->ctype
.base_type
;
444 if (!is_int_type(i
->ctype
))
445 return bad_expr_type(expr
);
447 examine_symbol_type(ctype
);
449 if (!ctype
->ctype
.base_type
) {
450 warning(expr
->pos
, "missing type information");
454 /* Get the size of whatever the pointer points to */
456 if (ptr_type
->type
== SYM_NODE
)
457 ptr_type
= ptr_type
->ctype
.base_type
;
458 if (ptr_type
->type
== SYM_PTR
)
459 ptr_type
= ptr_type
->ctype
.base_type
;
460 bit_size
= ptr_type
->bit_size
;
462 if (i
->type
== EXPR_VALUE
) {
463 i
->value
*= bit_size
>> 3;
464 } else if (bit_size
> bits_in_char
) {
465 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
466 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
468 val
->ctype
= size_t_ctype
;
469 val
->value
= bit_size
>> 3;
472 mul
->ctype
= size_t_ctype
;
483 static struct symbol
*evaluate_add(struct expression
*expr
)
485 struct expression
*left
= expr
->left
, *right
= expr
->right
;
486 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
488 if (is_ptr_type(ltype
))
489 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
491 if (is_ptr_type(rtype
))
492 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
494 return evaluate_arith(expr
, 1);
497 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
498 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
499 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
501 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
502 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
505 unsigned long mod1
, mod2
, diff
;
506 unsigned long as1
, as2
;
508 struct symbol
*base1
, *base2
;
510 if (target
== source
)
512 if (!target
|| !source
)
513 return "different types";
515 * Peel of per-node information.
516 * FIXME! Check alignment and context too here!
518 mod1
= target
->ctype
.modifiers
;
519 as1
= target
->ctype
.as
;
520 mod2
= source
->ctype
.modifiers
;
521 as2
= source
->ctype
.as
;
522 if (target
->type
== SYM_NODE
) {
523 target
= target
->ctype
.base_type
;
526 if (target
->type
== SYM_PTR
) {
530 mod1
|= target
->ctype
.modifiers
;
531 as1
|= target
->ctype
.as
;
533 if (source
->type
== SYM_NODE
) {
534 source
= source
->ctype
.base_type
;
537 if (source
->type
== SYM_PTR
) {
541 mod2
|= source
->ctype
.modifiers
;
542 as2
|= source
->ctype
.as
;
544 if (target
->type
== SYM_ENUM
) {
545 target
= target
->ctype
.base_type
;
549 if (source
->type
== SYM_ENUM
) {
550 source
= source
->ctype
.base_type
;
555 if (target
== source
)
557 if (!target
|| !source
)
558 return "different types";
560 type1
= target
->type
;
561 base1
= target
->ctype
.base_type
;
563 type2
= source
->type
;
564 base2
= source
->ctype
.base_type
;
567 * Pointers to functions compare as the function itself
569 if (type1
== SYM_PTR
&& base1
) {
570 switch (base1
->type
) {
574 base1
= base1
->ctype
.base_type
;
579 if (type2
== SYM_PTR
&& base2
) {
580 switch (base2
->type
) {
584 base2
= base2
->ctype
.base_type
;
590 /* Arrays degenerate to pointers for type comparisons */
591 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
592 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
594 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
595 return "different base types";
597 /* Must be same address space to be comparable */
599 return "different address spaces";
601 /* Ignore differences in storage types or addressability */
602 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
603 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
606 return "different type sizes";
607 if (diff
& ~MOD_SIGNEDNESS
)
608 return "different modifiers";
610 /* Differs in signedness only.. */
613 * Warn if both are explicitly signed ("unsigned" is obvously
614 * always explicit, and since we know one of them has to be
615 * unsigned, we check if the signed one was explicit).
617 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
618 return "different explicit signedness";
621 * "char" matches both "unsigned char" and "signed char",
622 * so if the explicit test didn't trigger, then we should
623 * not warn about a char.
625 if (!(mod1
& MOD_CHAR
))
626 return "different signedness";
630 if (type1
== SYM_FN
) {
632 struct symbol
*arg1
, *arg2
;
633 if (base1
->variadic
!= base2
->variadic
)
634 return "incompatible variadic arguments";
635 PREPARE_PTR_LIST(target
->arguments
, arg1
);
636 PREPARE_PTR_LIST(source
->arguments
, arg2
);
640 diff
= type_difference(arg1
, arg2
, 0, 0);
642 static char argdiff
[80];
643 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
652 FINISH_PTR_LIST(arg2
);
653 FINISH_PTR_LIST(arg1
);
662 static int is_null_ptr(struct expression
*expr
)
664 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
666 if (!is_ptr_type(expr
->ctype
))
667 warning(expr
->pos
, "Using plain integer as NULL pointer");
671 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
673 /* NULL expression? Just return the type of the "other side" */
682 * Ignore differences in "volatile" and "const"ness when
683 * subtracting pointers
685 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
687 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
689 const char *typediff
;
690 struct symbol
*ctype
;
691 struct symbol
*ltype
, *rtype
;
692 struct expression
*r
= *rp
;
694 ltype
= degenerate(l
);
695 rtype
= degenerate(r
);
698 * If it is an integer subtract: the ptr add case will do the
701 if (!is_ptr_type(rtype
))
702 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
705 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
707 ctype
= common_ptr_type(l
, r
);
709 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
713 examine_symbol_type(ctype
);
715 /* Figure out the base type we point to */
716 if (ctype
->type
== SYM_NODE
)
717 ctype
= ctype
->ctype
.base_type
;
718 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
719 warning(expr
->pos
, "subtraction of functions? Share your drugs");
722 ctype
= ctype
->ctype
.base_type
;
724 expr
->ctype
= ssize_t_ctype
;
725 if (ctype
->bit_size
> bits_in_char
) {
726 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
727 struct expression
*div
= expr
;
728 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
730 val
->ctype
= size_t_ctype
;
731 val
->value
= ctype
->bit_size
>> 3;
734 sub
->ctype
= ssize_t_ctype
;
743 return ssize_t_ctype
;
746 static struct symbol
*evaluate_sub(struct expression
*expr
)
748 struct expression
*left
= expr
->left
;
749 struct symbol
*ltype
= left
->ctype
;
751 if (is_ptr_type(ltype
))
752 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
754 return evaluate_arith(expr
, 1);
757 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
759 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
761 struct symbol
*ctype
;
766 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
767 warning(expr
->pos
, "assignment expression in conditional");
769 ctype
= evaluate_expression(expr
);
771 if (is_safe_type(ctype
))
772 warning(expr
->pos
, "testing a 'safe expression'");
778 static struct symbol
*evaluate_logical(struct expression
*expr
)
780 if (!evaluate_conditional(expr
->left
, 0))
782 if (!evaluate_conditional(expr
->right
, 0))
785 expr
->ctype
= &bool_ctype
;
789 static struct symbol
*evaluate_shift(struct expression
*expr
)
791 struct expression
*left
= expr
->left
, *right
= expr
->right
;
792 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
794 if (ltype
->type
== SYM_NODE
)
795 ltype
= ltype
->ctype
.base_type
;
796 if (rtype
->type
== SYM_NODE
)
797 rtype
= rtype
->ctype
.base_type
;
798 if (is_int_type(ltype
) && is_int_type(rtype
)) {
799 struct symbol
*ctype
= integer_promotion(ltype
);
800 if (ltype
->bit_size
!= ctype
->bit_size
)
801 expr
->left
= cast_to(expr
->left
, ctype
);
803 ctype
= integer_promotion(rtype
);
804 if (rtype
->bit_size
!= ctype
->bit_size
)
805 expr
->right
= cast_to(expr
->right
, ctype
);
808 return bad_expr_type(expr
);
811 static struct symbol
*evaluate_binop(struct expression
*expr
)
814 // addition can take ptr+int, fp and int
816 return evaluate_add(expr
);
818 // subtraction can take ptr-ptr, fp and int
820 return evaluate_sub(expr
);
822 // Arithmetic operations can take fp and int
824 return evaluate_arith(expr
, 1);
826 // shifts do integer promotions, but that's it.
827 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
828 return evaluate_shift(expr
);
830 // The rest are integer operations
831 // '%', '&', '^', '|'
833 return evaluate_arith(expr
, 0);
837 static struct symbol
*evaluate_comma(struct expression
*expr
)
839 expr
->ctype
= expr
->right
->ctype
;
843 static int modify_for_unsigned(int op
)
846 op
= SPECIAL_UNSIGNED_LT
;
848 op
= SPECIAL_UNSIGNED_GT
;
849 else if (op
== SPECIAL_LTE
)
850 op
= SPECIAL_UNSIGNED_LTE
;
851 else if (op
== SPECIAL_GTE
)
852 op
= SPECIAL_UNSIGNED_GTE
;
856 static struct symbol
*evaluate_compare(struct expression
*expr
)
858 struct expression
*left
= expr
->left
, *right
= expr
->right
;
859 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
860 struct symbol
*ctype
;
863 if (is_type_type(ltype
) && is_type_type(rtype
))
866 if (is_safe_type(ltype
) || is_safe_type(rtype
))
867 warning(expr
->pos
, "testing a 'safe expression'");
870 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
871 // FIXME! Check the types for compatibility
875 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
877 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
878 expr
->op
= modify_for_unsigned(expr
->op
);
882 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
886 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
893 expr
->ctype
= &bool_ctype
;
898 * FIXME!! This should do casts, array degeneration etc..
900 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
902 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
904 if (ltype
->type
== SYM_NODE
)
905 ltype
= ltype
->ctype
.base_type
;
907 if (rtype
->type
== SYM_NODE
)
908 rtype
= rtype
->ctype
.base_type
;
910 if (ltype
->type
== SYM_PTR
) {
911 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
915 if (rtype
->type
== SYM_PTR
) {
916 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
923 * NOTE! The degenerate case of "x ? : y", where we don't
924 * have a true case, this will possibly promote "x" to the
925 * same type as "y", and thus _change_ the conditional
926 * test in the expression. But since promotion is "safe"
927 * for testing, that's ok.
929 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
931 struct expression
**true;
932 struct symbol
*ctype
, *ltype
, *rtype
;
933 const char * typediff
;
935 if (!evaluate_conditional(expr
->conditional
, 0))
937 if (!evaluate_expression(expr
->cond_false
))
940 ctype
= degenerate(expr
->conditional
);
941 rtype
= degenerate(expr
->cond_false
);
943 true = &expr
->conditional
;
945 if (expr
->cond_true
) {
946 if (!evaluate_expression(expr
->cond_true
))
948 ltype
= degenerate(expr
->cond_true
);
949 true = &expr
->cond_true
;
952 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
955 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
958 ctype
= compatible_float_binop(true, &expr
->cond_false
);
961 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
965 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
968 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
976 /* FP assignments can not do modulo or bit operations */
977 static int compatible_float_op(int op
)
980 op
== SPECIAL_ADD_ASSIGN
||
981 op
== SPECIAL_SUB_ASSIGN
||
982 op
== SPECIAL_MUL_ASSIGN
||
983 op
== SPECIAL_DIV_ASSIGN
;
986 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
987 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
989 const char *typediff
;
993 if (is_int_type(target
)) {
994 if (is_int_type(source
)) {
995 if (target
->bit_size
!= source
->bit_size
)
997 if (target
->bit_offset
!= source
->bit_offset
)
1001 if (is_float_type(source
))
1003 } else if (is_float_type(target
)) {
1004 if (!compatible_float_op(op
)) {
1005 warning(expr
->pos
, "invalid assignment");
1008 if (is_int_type(source
))
1010 if (is_float_type(source
)) {
1011 if (target
->bit_size
!= source
->bit_size
)
1015 } else if (is_restricted_type(target
)) {
1016 if (restricted_binop(op
, target
)) {
1017 warning(expr
->pos
, "bad restricted assignment");
1020 if (!restricted_value(*rp
, target
))
1022 } else if (is_ptr_type(target
)) {
1023 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1024 evaluate_ptr_add(expr
, target
, rp
);
1028 warning(expr
->pos
, "invalid pointer assignment");
1031 } else if (op
!= '=') {
1032 warning(expr
->pos
, "invalid assignment");
1036 /* It's ok if the target is more volatile or const than the source */
1037 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1041 /* Pointer destination? */
1043 target_as
= t
->ctype
.as
;
1044 if (t
->type
== SYM_NODE
) {
1045 t
= t
->ctype
.base_type
;
1046 target_as
|= t
->ctype
.as
;
1048 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1049 struct expression
*right
= *rp
;
1050 struct symbol
*s
= source
;
1053 // NULL pointer is always ok
1054 if (is_null_ptr(right
))
1057 /* "void *" matches anything as long as the address space is ok */
1058 source_as
= s
->ctype
.as
;
1059 if (s
->type
== SYM_NODE
) {
1060 s
= s
->ctype
.base_type
;
1061 source_as
|= s
->ctype
.as
;
1063 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1064 s
= s
->ctype
.base_type
;
1065 t
= t
->ctype
.base_type
;
1066 if (s
== &void_ctype
|| t
== &void_ctype
)
1071 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1072 info(expr
->pos
, " expected %s", show_typename(target
));
1073 info(expr
->pos
, " got %s", show_typename(source
));
1074 *rp
= cast_to(*rp
, target
);
1077 *rp
= cast_to(*rp
, target
);
1081 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1083 if (type
->ctype
.modifiers
& MOD_CONST
)
1084 warning(left
->pos
, "assignment to const expression");
1085 if (type
->type
== SYM_NODE
)
1086 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1089 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1091 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1092 struct expression
*where
= expr
;
1093 struct symbol
*ltype
, *rtype
;
1095 if (!lvalue_expression(left
)) {
1096 warning(expr
->pos
, "not an lvalue");
1100 ltype
= left
->ctype
;
1102 rtype
= degenerate(right
);
1104 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1107 evaluate_assign_to(left
, ltype
);
1109 expr
->ctype
= ltype
;
1113 static void examine_fn_arguments(struct symbol
*fn
)
1117 FOR_EACH_PTR(fn
->arguments
, s
) {
1118 struct symbol
*arg
= evaluate_symbol(s
);
1119 /* Array/function arguments silently degenerate into pointers */
1125 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1126 if (arg
->type
== SYM_ARRAY
)
1127 ptr
->ctype
= arg
->ctype
;
1129 ptr
->ctype
.base_type
= arg
;
1130 ptr
->ctype
.as
|= s
->ctype
.as
;
1131 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1133 s
->ctype
.base_type
= ptr
;
1135 s
->ctype
.modifiers
= 0;
1137 examine_symbol_type(s
);
1144 } END_FOR_EACH_PTR(s
);
1147 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1149 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1150 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1152 newsym
->ctype
.as
= as
;
1153 newsym
->ctype
.modifiers
= mod
;
1159 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1161 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1162 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1164 node
->ctype
.base_type
= ptr
;
1165 ptr
->bit_size
= bits_in_pointer
;
1166 ptr
->ctype
.alignment
= pointer_alignment
;
1168 node
->bit_size
= bits_in_pointer
;
1169 node
->ctype
.alignment
= pointer_alignment
;
1172 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1173 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1174 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1175 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1177 if (sym
->type
== SYM_NODE
) {
1178 ptr
->ctype
.as
|= sym
->ctype
.as
;
1179 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1180 sym
= sym
->ctype
.base_type
;
1182 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1183 ptr
->ctype
.as
|= sym
->ctype
.as
;
1184 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1185 sym
= sym
->ctype
.base_type
;
1187 ptr
->ctype
.base_type
= sym
;
1192 /* Arrays degenerate into pointers on pointer arithmetic */
1193 static struct symbol
*degenerate(struct expression
*expr
)
1195 struct symbol
*ctype
, *base
;
1199 ctype
= expr
->ctype
;
1203 if (ctype
->type
== SYM_NODE
)
1204 base
= ctype
->ctype
.base_type
;
1206 * Arrays degenerate into pointers to the entries, while
1207 * functions degenerate into pointers to themselves.
1208 * If array was part of non-lvalue compound, we create a copy
1209 * of that compound first and then act as if we were dealing with
1210 * the corresponding field in there.
1212 switch (base
->type
) {
1214 if (expr
->type
== EXPR_SLICE
) {
1215 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1216 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1218 a
->ctype
.base_type
= expr
->base
->ctype
;
1219 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1220 a
->array_size
= expr
->base
->ctype
->array_size
;
1222 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1224 e0
->ctype
= &lazy_ptr_ctype
;
1226 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1229 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1231 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1233 e2
->right
= expr
->base
;
1235 e2
->ctype
= expr
->base
->ctype
;
1237 if (expr
->r_bitpos
) {
1238 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1241 e3
->right
= alloc_const_expression(expr
->pos
,
1242 expr
->r_bitpos
>> 3);
1243 e3
->ctype
= &lazy_ptr_ctype
;
1248 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1251 e4
->ctype
= &lazy_ptr_ctype
;
1254 expr
->type
= EXPR_PREOP
;
1258 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1259 warning(expr
->pos
, "strange non-value function or array");
1262 *expr
= *expr
->unop
;
1263 ctype
= create_pointer(expr
, ctype
, 1);
1264 expr
->ctype
= ctype
;
1271 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1273 struct expression
*op
= expr
->unop
;
1274 struct symbol
*ctype
;
1276 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1277 warning(expr
->pos
, "not addressable");
1284 * symbol expression evaluation is lazy about the type
1285 * of the sub-expression, so we may have to generate
1286 * the type here if so..
1288 if (expr
->ctype
== &lazy_ptr_ctype
) {
1289 ctype
= create_pointer(expr
, ctype
, 0);
1290 expr
->ctype
= ctype
;
1296 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1298 struct expression
*op
= expr
->unop
;
1299 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1301 /* Simplify: *&(expr) => (expr) */
1302 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1307 /* Dereferencing a node drops all the node information. */
1308 if (ctype
->type
== SYM_NODE
)
1309 ctype
= ctype
->ctype
.base_type
;
1311 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1312 target
= ctype
->ctype
.base_type
;
1314 switch (ctype
->type
) {
1316 warning(expr
->pos
, "cannot derefence this type");
1319 merge_type(node
, ctype
);
1320 if (ctype
->type
!= SYM_ARRAY
)
1323 * Dereferencing a pointer to an array results in a
1324 * degenerate dereference: the expression becomes
1325 * just a pointer to the entry, and the derefence
1330 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1331 target
->bit_size
= bits_in_pointer
;
1332 target
->ctype
.alignment
= pointer_alignment
;
1333 merge_type(target
, ctype
->ctype
.base_type
);
1337 if (!lvalue_expression(op
)) {
1338 warning(op
->pos
, "non-lvalue array??");
1342 /* Do the implied "addressof" on the array */
1346 * When an array is dereferenced, we need to pick
1347 * up the attributes of the original node too..
1349 merge_type(node
, op
->ctype
);
1350 merge_type(node
, ctype
);
1354 node
->bit_size
= target
->bit_size
;
1355 node
->array_size
= target
->array_size
;
1362 * Unary post-ops: x++ and x--
1364 static struct symbol
*evaluate_postop(struct expression
*expr
)
1366 struct expression
*op
= expr
->unop
;
1367 struct symbol
*ctype
= op
->ctype
;
1369 if (!lvalue_expression(expr
->unop
)) {
1370 warning(expr
->pos
, "need lvalue expression for ++/--");
1373 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1374 warning(expr
->pos
, "bad operation on restricted");
1378 evaluate_assign_to(op
, ctype
);
1380 expr
->ctype
= ctype
;
1384 static struct symbol
*evaluate_sign(struct expression
*expr
)
1386 struct symbol
*ctype
= expr
->unop
->ctype
;
1387 if (is_int_type(ctype
)) {
1388 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1389 if (rtype
->bit_size
!= ctype
->bit_size
)
1390 expr
->unop
= cast_to(expr
->unop
, rtype
);
1392 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1393 /* no conversions needed */
1394 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1395 /* no conversions needed */
1397 return bad_expr_type(expr
);
1399 if (expr
->op
== '+')
1400 *expr
= *expr
->unop
;
1401 expr
->ctype
= ctype
;
1405 static struct symbol
*evaluate_preop(struct expression
*expr
)
1407 struct symbol
*ctype
= expr
->unop
->ctype
;
1411 *expr
= *expr
->unop
;
1417 return evaluate_sign(expr
);
1420 return evaluate_dereference(expr
);
1423 return evaluate_addressof(expr
);
1425 case SPECIAL_INCREMENT
:
1426 case SPECIAL_DECREMENT
:
1428 * From a type evaluation standpoint the pre-ops are
1429 * the same as the postops
1431 return evaluate_postop(expr
);
1434 if (is_safe_type(ctype
))
1435 warning(expr
->pos
, "testing a 'safe expression'");
1436 if (is_float_type(ctype
)) {
1437 struct expression
*arg
= expr
->unop
;
1438 expr
->type
= EXPR_BINOP
;
1439 expr
->op
= SPECIAL_EQUAL
;
1441 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1442 expr
->right
->ctype
= ctype
;
1443 expr
->right
->fvalue
= 0;
1445 ctype
= &bool_ctype
;
1451 expr
->ctype
= ctype
;
1455 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1457 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1458 struct ptr_list
*list
= head
;
1464 for (i
= 0; i
< list
->nr
; i
++) {
1465 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1467 if (sym
->ident
!= ident
)
1469 *offset
= sym
->offset
;
1472 struct symbol
*ctype
= sym
->ctype
.base_type
;
1476 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1478 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1481 *offset
+= sym
->offset
;
1485 } while ((list
= list
->next
) != head
);
1489 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1491 struct expression
*add
;
1494 * Create a new add-expression
1496 * NOTE! Even if we just add zero, we need a new node
1497 * for the member pointer, since it has a different
1498 * type than the original pointer. We could make that
1499 * be just a cast, but the fact is, a node is a node,
1500 * so we might as well just do the "add zero" here.
1502 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1505 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1506 add
->right
->ctype
= &int_ctype
;
1507 add
->right
->value
= offset
;
1510 * The ctype of the pointer will be lazily evaluated if
1511 * we ever take the address of this member dereference..
1513 add
->ctype
= &lazy_ptr_ctype
;
1517 /* structure/union dereference */
1518 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1521 struct symbol
*ctype
, *member
;
1522 struct expression
*deref
= expr
->deref
, *add
;
1523 struct ident
*ident
= expr
->member
;
1527 if (!evaluate_expression(deref
))
1530 warning(expr
->pos
, "bad member name");
1534 ctype
= deref
->ctype
;
1535 address_space
= ctype
->ctype
.as
;
1536 mod
= ctype
->ctype
.modifiers
;
1537 if (ctype
->type
== SYM_NODE
) {
1538 ctype
= ctype
->ctype
.base_type
;
1539 address_space
|= ctype
->ctype
.as
;
1540 mod
|= ctype
->ctype
.modifiers
;
1542 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1543 warning(expr
->pos
, "expected structure or union");
1547 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1549 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1550 const char *name
= "<unnamed>";
1553 name
= ctype
->ident
->name
;
1554 namelen
= ctype
->ident
->len
;
1556 warning(expr
->pos
, "no member '%s' in %s %.*s",
1557 show_ident(ident
), type
, namelen
, name
);
1562 * The member needs to take on the address space and modifiers of
1563 * the "parent" type.
1565 member
= convert_to_as_mod(member
, address_space
, mod
);
1566 ctype
= member
->ctype
.base_type
;
1568 if (!lvalue_expression(deref
)) {
1569 if (deref
->type
!= EXPR_SLICE
) {
1573 expr
->base
= deref
->base
;
1574 expr
->r_bitpos
= deref
->r_bitpos
;
1576 expr
->r_bitpos
+= offset
<< 3;
1577 expr
->type
= EXPR_SLICE
;
1578 expr
->r_nrbits
= member
->bit_size
;
1579 expr
->r_bitpos
+= member
->bit_offset
;
1580 expr
->ctype
= member
;
1584 deref
= deref
->unop
;
1585 expr
->deref
= deref
;
1587 add
= evaluate_offset(deref
, offset
);
1588 expr
->type
= EXPR_PREOP
;
1592 expr
->ctype
= member
;
1596 static int is_promoted(struct expression
*expr
)
1599 switch (expr
->type
) {
1602 case EXPR_CONDITIONAL
:
1626 static struct symbol
*evaluate_cast(struct expression
*);
1628 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1630 struct symbol
*sym
= expr
->cast_type
;
1632 sym
= evaluate_expression(expr
->cast_expression
);
1636 * Expressions of restricted types will possibly get
1637 * promoted - check that here
1639 if (is_restricted_type(sym
)) {
1640 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1644 examine_symbol_type(sym
);
1645 if (is_bitfield_type(sym
)) {
1646 warning(expr
->pos
, "trying to examine bitfield type");
1652 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1654 struct symbol
*type
;
1657 type
= evaluate_type_information(expr
);
1661 size
= type
->bit_size
;
1663 warning(expr
->pos
, "cannot size expression");
1664 expr
->type
= EXPR_VALUE
;
1665 expr
->value
= size
>> 3;
1666 expr
->ctype
= size_t_ctype
;
1667 return size_t_ctype
;
1670 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1672 struct symbol
*type
;
1675 type
= evaluate_type_information(expr
);
1679 if (type
->type
== SYM_NODE
)
1680 type
= type
->ctype
.base_type
;
1683 switch (type
->type
) {
1687 type
= type
->ctype
.base_type
;
1691 warning(expr
->pos
, "expected pointer expression");
1694 size
= type
->bit_size
;
1697 expr
->type
= EXPR_VALUE
;
1698 expr
->value
= size
>> 3;
1699 expr
->ctype
= size_t_ctype
;
1700 return size_t_ctype
;
1703 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1705 struct symbol
*type
;
1707 type
= evaluate_type_information(expr
);
1711 expr
->type
= EXPR_VALUE
;
1712 expr
->value
= type
->ctype
.alignment
;
1713 expr
->ctype
= size_t_ctype
;
1714 return size_t_ctype
;
1717 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1719 struct expression
*expr
;
1720 struct symbol_list
*argument_types
= fn
->arguments
;
1721 struct symbol
*argtype
;
1724 PREPARE_PTR_LIST(argument_types
, argtype
);
1725 FOR_EACH_PTR (head
, expr
) {
1726 struct expression
**p
= THIS_ADDRESS(expr
);
1727 struct symbol
*ctype
, *target
;
1728 ctype
= evaluate_expression(expr
);
1733 ctype
= degenerate(expr
);
1736 if (!target
&& ctype
->bit_size
< bits_in_int
)
1737 target
= &int_ctype
;
1739 static char where
[30];
1740 examine_symbol_type(target
);
1741 sprintf(where
, "argument %d", i
);
1742 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1746 NEXT_PTR_LIST(argtype
);
1747 } END_FOR_EACH_PTR(expr
);
1748 FINISH_PTR_LIST(argtype
);
1752 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1754 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1756 struct expression
*entry
= *ep
;
1757 struct expression
**parent
, *reuse
= NULL
;
1758 unsigned long offset
;
1760 unsigned long from
, to
;
1761 int accept_string
= is_byte_type(ctype
);
1766 if (entry
->type
== EXPR_INDEX
) {
1767 from
= entry
->idx_from
;
1768 to
= entry
->idx_to
+1;
1769 parent
= &entry
->idx_expression
;
1771 entry
= entry
->idx_expression
;
1774 offset
= from
* (ctype
->bit_size
>>3);
1776 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1777 reuse
->type
= EXPR_POS
;
1778 reuse
->ctype
= ctype
;
1779 reuse
->init_offset
= offset
;
1780 reuse
->init_nr
= to
- from
;
1781 reuse
->init_expr
= entry
;
1782 parent
= &reuse
->init_expr
;
1787 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1788 sym
= evaluate_expression(entry
);
1789 to
= from
+ get_expression_value(sym
->array_size
);
1791 evaluate_initializer(ctype
, parent
);
1796 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1798 struct expression
*entry
;
1801 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1802 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1803 } END_FOR_EACH_PTR(entry
);
1806 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1807 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1809 if (expression_list_size(expr
->expr_list
) != 1) {
1810 warning(expr
->pos
, "unexpected compound initializer");
1813 evaluate_array_initializer(ctype
, expr
);
1817 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1821 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1822 if (sym
->ident
== ident
)
1824 } END_FOR_EACH_PTR(sym
);
1828 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1830 struct expression
*entry
= *ep
;
1831 struct expression
**parent
;
1832 struct expression
*reuse
= NULL
;
1833 unsigned long offset
;
1836 error(entry
->pos
, "unknown named initializer");
1840 if (entry
->type
== EXPR_IDENTIFIER
) {
1842 entry
= entry
->ident_expression
;
1846 offset
= sym
->offset
;
1849 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1850 reuse
->type
= EXPR_POS
;
1852 reuse
->init_offset
= offset
;
1854 reuse
->init_expr
= entry
;
1855 parent
= &reuse
->init_expr
;
1859 evaluate_initializer(sym
, parent
);
1863 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1865 struct expression
*entry
;
1868 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1869 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1870 if (entry
->type
== EXPR_IDENTIFIER
) {
1871 struct ident
*ident
= entry
->expr_ident
;
1872 /* We special-case the "already right place" case */
1873 if (!sym
|| sym
->ident
!= ident
) {
1874 RESET_PTR_LIST(sym
);
1878 if (sym
->ident
== ident
)
1884 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1887 } END_FOR_EACH_PTR(entry
);
1888 FINISH_PTR_LIST(sym
);
1892 * Initializers are kind of like assignments. Except
1893 * they can be a hell of a lot more complex.
1895 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1897 struct expression
*expr
= *ep
;
1900 * Simple non-structure/array initializers are the simple
1901 * case, and look (and parse) largely like assignments.
1903 switch (expr
->type
) {
1905 int is_string
= expr
->type
== EXPR_STRING
;
1906 struct symbol
*rtype
= evaluate_expression(expr
);
1910 * char array[] = "string"
1911 * should _not_ degenerate.
1913 if (!is_string
|| !is_string_type(ctype
))
1914 rtype
= degenerate(expr
);
1915 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1920 case EXPR_INITIALIZER
:
1921 expr
->ctype
= ctype
;
1922 if (ctype
->type
== SYM_NODE
)
1923 ctype
= ctype
->ctype
.base_type
;
1925 switch (ctype
->type
) {
1928 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1931 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1934 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1937 evaluate_scalar_initializer(ctype
, expr
);
1941 case EXPR_IDENTIFIER
:
1942 if (ctype
->type
== SYM_NODE
)
1943 ctype
= ctype
->ctype
.base_type
;
1944 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1945 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1949 evaluate_one_struct_initializer(ctype
, ep
,
1950 find_struct_ident(ctype
, expr
->expr_ident
));
1954 if (ctype
->type
== SYM_NODE
)
1955 ctype
= ctype
->ctype
.base_type
;
1956 if (ctype
->type
!= SYM_ARRAY
) {
1957 error(expr
->pos
, "expected array");
1960 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1965 * An EXPR_POS expression has already been evaluated, and we don't
1966 * need to do anything more
1972 static int get_as(struct symbol
*sym
)
1980 mod
= sym
->ctype
.modifiers
;
1981 if (sym
->type
== SYM_NODE
) {
1982 sym
= sym
->ctype
.base_type
;
1983 as
|= sym
->ctype
.as
;
1984 mod
|= sym
->ctype
.modifiers
;
1988 * At least for now, allow casting to a "unsigned long".
1989 * That's how we do things like pointer arithmetic and
1990 * store pointers to registers.
1992 if (sym
== &ulong_ctype
)
1995 if (sym
&& sym
->type
== SYM_PTR
) {
1996 sym
= sym
->ctype
.base_type
;
1997 as
|= sym
->ctype
.as
;
1998 mod
|= sym
->ctype
.modifiers
;
2000 if (mod
& MOD_FORCE
)
2005 static struct symbol
*evaluate_cast(struct expression
*expr
)
2007 struct expression
*target
= expr
->cast_expression
;
2008 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2014 expr
->ctype
= ctype
;
2015 expr
->cast_type
= ctype
;
2018 * Special case: a cast can be followed by an
2019 * initializer, in which case we need to pass
2020 * the type value down to that initializer rather
2021 * than trying to evaluate it as an expression
2023 * A more complex case is when the initializer is
2024 * dereferenced as part of a post-fix expression.
2025 * We need to produce an expression that can be dereferenced.
2027 if (target
->type
== EXPR_INITIALIZER
) {
2028 struct symbol
*sym
= expr
->cast_type
;
2029 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2031 sym
->initializer
= expr
->cast_expression
;
2032 evaluate_symbol(sym
);
2034 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2037 expr
->type
= EXPR_PREOP
;
2045 evaluate_expression(target
);
2049 * You can always throw a value away by casting to
2050 * "void" - that's an implicit "force". Note that
2051 * the same is _not_ true of "void *".
2053 if (ctype
== &void_ctype
)
2057 if (type
== SYM_NODE
) {
2058 type
= ctype
->ctype
.base_type
->type
;
2059 if (ctype
->ctype
.base_type
== &void_ctype
)
2062 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2063 warning(expr
->pos
, "cast to non-scalar");
2065 if (!target
->ctype
) {
2066 warning(expr
->pos
, "cast from unknown type");
2070 type
= target
->ctype
->type
;
2071 if (type
== SYM_NODE
)
2072 type
= target
->ctype
->ctype
.base_type
->type
;
2073 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2074 warning(expr
->pos
, "cast from non-scalar");
2076 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2077 warning(expr
->pos
, "cast removes address space of expression");
2079 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2080 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2081 if (t1
->type
== SYM_NODE
)
2082 t1
= t1
->ctype
.base_type
;
2083 if (t2
->type
== SYM_NODE
)
2084 t2
= t2
->ctype
.base_type
;
2086 if (t1
->type
== SYM_RESTRICT
)
2087 warning(expr
->pos
, "cast to restricted type");
2088 if (t2
->type
== SYM_RESTRICT
)
2089 warning(expr
->pos
, "cast from restricted type");
2094 * Casts of constant values are special: they
2095 * can be NULL, and thus need to be simplified
2098 if (target
->type
== EXPR_VALUE
)
2099 cast_value(expr
, ctype
, target
, target
->ctype
);
2106 * Evaluate a call expression with a symbol. This
2107 * should expand inline functions, and evaluate
2110 static int evaluate_symbol_call(struct expression
*expr
)
2112 struct expression
*fn
= expr
->fn
;
2113 struct symbol
*ctype
= fn
->ctype
;
2115 if (fn
->type
!= EXPR_PREOP
)
2118 if (ctype
->op
&& ctype
->op
->evaluate
)
2119 return ctype
->op
->evaluate(expr
);
2121 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2123 struct symbol
*curr
= current_fn
;
2124 current_fn
= ctype
->ctype
.base_type
;
2125 examine_fn_arguments(current_fn
);
2127 ret
= inline_function(expr
, ctype
);
2129 /* restore the old function */
2137 static struct symbol
*evaluate_call(struct expression
*expr
)
2140 struct symbol
*ctype
, *sym
;
2141 struct expression
*fn
= expr
->fn
;
2142 struct expression_list
*arglist
= expr
->args
;
2144 if (!evaluate_expression(fn
))
2146 sym
= ctype
= fn
->ctype
;
2147 if (ctype
->type
== SYM_NODE
)
2148 ctype
= ctype
->ctype
.base_type
;
2149 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2150 ctype
= ctype
->ctype
.base_type
;
2151 if (!evaluate_arguments(sym
, ctype
, arglist
))
2153 if (ctype
->type
!= SYM_FN
) {
2154 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2157 args
= expression_list_size(expr
->args
);
2158 fnargs
= symbol_list_size(ctype
->arguments
);
2160 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2161 if (args
> fnargs
&& !ctype
->variadic
)
2162 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2163 if (sym
->type
== SYM_NODE
) {
2164 if (evaluate_symbol_call(expr
))
2167 expr
->ctype
= ctype
->ctype
.base_type
;
2171 struct symbol
*evaluate_expression(struct expression
*expr
)
2178 switch (expr
->type
) {
2181 warning(expr
->pos
, "value expression without a type");
2184 return evaluate_string(expr
);
2186 return evaluate_symbol_expression(expr
);
2188 if (!evaluate_expression(expr
->left
))
2190 if (!evaluate_expression(expr
->right
))
2192 return evaluate_binop(expr
);
2194 return evaluate_logical(expr
);
2196 evaluate_expression(expr
->left
);
2197 if (!evaluate_expression(expr
->right
))
2199 return evaluate_comma(expr
);
2201 if (!evaluate_expression(expr
->left
))
2203 if (!evaluate_expression(expr
->right
))
2205 return evaluate_compare(expr
);
2206 case EXPR_ASSIGNMENT
:
2207 if (!evaluate_expression(expr
->left
))
2209 if (!evaluate_expression(expr
->right
))
2211 return evaluate_assignment(expr
);
2213 if (!evaluate_expression(expr
->unop
))
2215 return evaluate_preop(expr
);
2217 if (!evaluate_expression(expr
->unop
))
2219 return evaluate_postop(expr
);
2221 case EXPR_IMPLIED_CAST
:
2222 return evaluate_cast(expr
);
2224 return evaluate_sizeof(expr
);
2225 case EXPR_PTRSIZEOF
:
2226 return evaluate_ptrsizeof(expr
);
2228 return evaluate_alignof(expr
);
2230 return evaluate_member_dereference(expr
);
2232 return evaluate_call(expr
);
2234 case EXPR_CONDITIONAL
:
2235 return evaluate_conditional_expression(expr
);
2236 case EXPR_STATEMENT
:
2237 expr
->ctype
= evaluate_statement(expr
->statement
);
2241 expr
->ctype
= &ptr_ctype
;
2245 /* Evaluate the type of the symbol .. */
2246 evaluate_symbol(expr
->symbol
);
2247 /* .. but the type of the _expression_ is a "type" */
2248 expr
->ctype
= &type_ctype
;
2251 /* These can not exist as stand-alone expressions */
2252 case EXPR_INITIALIZER
:
2253 case EXPR_IDENTIFIER
:
2256 warning(expr
->pos
, "internal front-end error: initializer in expression");
2259 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2265 static void check_duplicates(struct symbol
*sym
)
2267 struct symbol
*next
= sym
;
2269 while ((next
= next
->same_symbol
) != NULL
) {
2270 const char *typediff
;
2271 evaluate_symbol(next
);
2272 typediff
= type_difference(sym
, next
, 0, 0);
2274 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2275 show_ident(sym
->ident
),
2276 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2282 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2284 struct symbol
*base_type
;
2289 sym
= examine_symbol_type(sym
);
2290 base_type
= sym
->ctype
.base_type
;
2294 /* Evaluate the initializers */
2295 if (sym
->initializer
)
2296 evaluate_initializer(sym
, &sym
->initializer
);
2298 /* And finally, evaluate the body of the symbol too */
2299 if (base_type
->type
== SYM_FN
) {
2300 struct symbol
*curr
= current_fn
;
2302 current_fn
= base_type
;
2304 examine_fn_arguments(base_type
);
2305 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2307 if (base_type
->stmt
)
2308 evaluate_statement(base_type
->stmt
);
2316 void evaluate_symbol_list(struct symbol_list
*list
)
2320 FOR_EACH_PTR(list
, sym
) {
2321 check_duplicates(sym
);
2322 evaluate_symbol(sym
);
2323 } END_FOR_EACH_PTR(sym
);
2326 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2328 struct expression
*expr
= stmt
->expression
;
2329 struct symbol
*ctype
, *fntype
;
2331 evaluate_expression(expr
);
2332 ctype
= degenerate(expr
);
2333 fntype
= current_fn
->ctype
.base_type
;
2334 if (!fntype
|| fntype
== &void_ctype
) {
2335 if (expr
&& ctype
!= &void_ctype
)
2336 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2341 warning(stmt
->pos
, "return with no return value");
2346 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2350 static void evaluate_if_statement(struct statement
*stmt
)
2352 if (!stmt
->if_conditional
)
2355 evaluate_conditional(stmt
->if_conditional
, 0);
2356 evaluate_statement(stmt
->if_true
);
2357 evaluate_statement(stmt
->if_false
);
2360 static void evaluate_iterator(struct statement
*stmt
)
2362 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2363 evaluate_conditional(stmt
->iterator_post_condition
,1);
2364 evaluate_statement(stmt
->iterator_pre_statement
);
2365 evaluate_statement(stmt
->iterator_statement
);
2366 evaluate_statement(stmt
->iterator_post_statement
);
2369 struct symbol
*evaluate_statement(struct statement
*stmt
)
2374 switch (stmt
->type
) {
2376 return evaluate_return_expression(stmt
);
2378 case STMT_EXPRESSION
:
2379 if (!evaluate_expression(stmt
->expression
))
2381 return degenerate(stmt
->expression
);
2383 case STMT_COMPOUND
: {
2384 struct statement
*s
;
2385 struct symbol
*type
= NULL
;
2388 /* Evaluate each symbol in the compound statement */
2389 FOR_EACH_PTR(stmt
->syms
, sym
) {
2390 evaluate_symbol(sym
);
2391 } END_FOR_EACH_PTR(sym
);
2392 evaluate_symbol(stmt
->ret
);
2395 * Then, evaluate each statement, making the type of the
2396 * compound statement be the type of the last statement
2399 FOR_EACH_PTR(stmt
->stmts
, s
) {
2400 type
= evaluate_statement(s
);
2401 } END_FOR_EACH_PTR(s
);
2407 evaluate_if_statement(stmt
);
2410 evaluate_iterator(stmt
);
2413 evaluate_expression(stmt
->switch_expression
);
2414 evaluate_statement(stmt
->switch_statement
);
2417 evaluate_expression(stmt
->case_expression
);
2418 evaluate_expression(stmt
->case_to
);
2419 evaluate_statement(stmt
->case_statement
);
2422 return evaluate_statement(stmt
->label_statement
);
2424 evaluate_expression(stmt
->goto_expression
);
2429 /* FIXME! Do the asm parameter evaluation! */
2432 evaluate_expression(stmt
->expression
);