4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct symbol
*sym
= expr
->symbol
;
37 struct symbol
*base_type
;
40 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
44 examine_symbol_type(sym
);
46 base_type
= sym
->ctype
.base_type
;
48 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
52 /* The type of a symbol is the symbol itself! */
55 /* enums can be turned into plain values */
56 if (sym
->type
!= SYM_ENUM
) {
57 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
59 addr
->symbol_name
= expr
->symbol_name
;
60 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
61 expr
->type
= EXPR_PREOP
;
65 } else if (base_type
->bit_size
< bits_in_int
) {
66 /* ugly - we need to force sizeof for these guys */
67 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
68 e
->value
= sym
->value
;
70 expr
->type
= EXPR_PREOP
;
74 expr
->type
= EXPR_VALUE
;
75 expr
->value
= sym
->value
;
77 expr
->ctype
= base_type
;
81 static struct symbol
*evaluate_string(struct expression
*expr
)
83 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
84 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
85 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
86 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
87 unsigned int length
= expr
->string
->length
;
89 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
90 sym
->bit_size
= bits_in_char
* length
;
91 sym
->ctype
.alignment
= 1;
92 sym
->ctype
.modifiers
= MOD_STATIC
;
93 sym
->ctype
.base_type
= array
;
94 sym
->initializer
= initstr
;
97 initstr
->string
= expr
->string
;
99 array
->array_size
= sym
->array_size
;
100 array
->bit_size
= bits_in_char
* length
;
101 array
->ctype
.alignment
= 1;
102 array
->ctype
.modifiers
= MOD_STATIC
;
103 array
->ctype
.base_type
= &char_ctype
;
106 addr
->ctype
= &lazy_ptr_ctype
;
108 expr
->type
= EXPR_PREOP
;
115 static inline struct symbol
*integer_promotion(struct symbol
*type
)
117 unsigned long mod
= type
->ctype
.modifiers
;
120 if (type
->type
== SYM_NODE
)
121 type
= type
->ctype
.base_type
;
122 if (type
->type
== SYM_ENUM
)
123 type
= type
->ctype
.base_type
;
124 width
= type
->bit_size
;
125 if (type
->type
== SYM_BITFIELD
)
126 type
= type
->ctype
.base_type
;
127 mod
= type
->ctype
.modifiers
;
128 if (width
< bits_in_int
)
131 /* If char/short has as many bits as int, it still gets "promoted" */
132 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
134 if (mod
& MOD_UNSIGNED
)
141 * integer part of usual arithmetic conversions:
142 * integer promotions are applied
143 * if left and right are identical, we are done
144 * if signedness is the same, convert one with lower rank
145 * unless unsigned argument has rank lower than signed one, convert the
147 * if signed argument is bigger than unsigned one, convert the unsigned.
148 * otherwise, convert signed.
150 * Leaving aside the integer promotions, that is equivalent to
151 * if identical, don't convert
152 * if left is bigger than right, convert right
153 * if right is bigger than left, convert right
154 * otherwise, if signedness is the same, convert one with lower rank
155 * otherwise convert the signed one.
157 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
159 unsigned long lmod
, rmod
;
161 left
= integer_promotion(left
);
162 right
= integer_promotion(right
);
167 if (left
->bit_size
> right
->bit_size
)
170 if (right
->bit_size
> left
->bit_size
)
173 lmod
= left
->ctype
.modifiers
;
174 rmod
= right
->ctype
.modifiers
;
175 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
176 if (lmod
& MOD_UNSIGNED
)
178 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
186 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
188 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
191 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
193 unsigned long mod
, as
;
197 mod
|= node
->ctype
.modifiers
;
198 as
|= node
->ctype
.as
;
199 switch (node
->type
) {
202 node
= node
->ctype
.base_type
;
209 *modp
= mod
& ~MOD_IGNORE
;
214 static int is_same_type(struct symbol
*old
, struct symbol
*new)
216 unsigned long oldmod
, newmod
, oldas
, newas
;
218 old
= base_type(old
, &oldmod
, &oldas
);
219 new = base_type(new, &newmod
, &newas
);
220 return old
== new && oldmod
== newmod
&& oldas
== newas
;
224 * This gets called for implicit casts in assignments and
225 * integer promotion. We often want to try to move the
226 * cast down, because the ops involved may have been
227 * implicitly cast up, and we can get rid of the casts
230 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
232 struct expression
*expr
;
234 if (is_same_type(old
->ctype
, type
))
238 * See if we can simplify the op. Move the cast down.
242 if (old
->op
== '~') {
244 old
->unop
= cast_to(old
->unop
, type
);
249 case EXPR_IMPLIED_CAST
:
250 if (old
->ctype
->bit_size
>= type
->bit_size
) {
251 struct expression
*orig
= old
->cast_expression
;
252 if (same_cast_type(orig
->ctype
, type
))
254 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
256 old
->cast_type
= type
;
266 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
268 expr
->cast_type
= type
;
269 expr
->cast_expression
= old
;
273 static int is_type_type(struct symbol
*type
)
275 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
278 int is_ptr_type(struct symbol
*type
)
280 if (type
->type
== SYM_NODE
)
281 type
= type
->ctype
.base_type
;
282 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
285 static inline int is_float_type(struct symbol
*type
)
287 if (type
->type
== SYM_NODE
)
288 type
= type
->ctype
.base_type
;
289 return type
->ctype
.base_type
== &fp_type
;
292 static inline int is_byte_type(struct symbol
*type
)
294 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
297 static inline int is_string_type(struct symbol
*type
)
299 if (type
->type
== SYM_NODE
)
300 type
= type
->ctype
.base_type
;
301 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
304 static struct symbol
*bad_expr_type(struct expression
*expr
)
306 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
307 switch (expr
->type
) {
310 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
311 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
315 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
324 static struct symbol
*compatible_float_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_float_type(ltype
)) {
334 if (is_int_type(rtype
))
336 if (is_float_type(rtype
)) {
337 unsigned long lmod
= ltype
->ctype
.modifiers
;
338 unsigned long rmod
= rtype
->ctype
.modifiers
;
339 lmod
&= MOD_LONG
| MOD_LONGLONG
;
340 rmod
&= MOD_LONG
| MOD_LONGLONG
;
350 if (!is_float_type(rtype
) || !is_int_type(ltype
))
353 *lp
= cast_to(left
, rtype
);
356 *rp
= cast_to(right
, ltype
);
360 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
362 struct expression
*left
= *lp
, *right
= *rp
;
363 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
365 if (ltype
->type
== SYM_NODE
)
366 ltype
= ltype
->ctype
.base_type
;
367 if (rtype
->type
== SYM_NODE
)
368 rtype
= rtype
->ctype
.base_type
;
369 if (is_int_type(ltype
) && is_int_type(rtype
)) {
370 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
372 *lp
= cast_to(left
, ctype
);
373 *rp
= cast_to(right
, ctype
);
379 static int restricted_value(struct expression
*v
, struct symbol
*type
)
381 if (v
->type
!= EXPR_VALUE
)
388 static int restricted_binop(int op
, struct symbol
*type
)
397 case SPECIAL_NOTEQUAL
:
398 case SPECIAL_AND_ASSIGN
:
399 case SPECIAL_OR_ASSIGN
:
400 case SPECIAL_XOR_ASSIGN
:
407 static int restricted_unop(int op
, struct symbol
*type
)
409 if (op
== '~' && type
->bit_size
>= bits_in_int
)
416 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
418 struct expression
*left
= *lp
, *right
= *rp
;
419 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
420 struct symbol
*type
= NULL
;
422 if (ltype
->type
== SYM_NODE
)
423 ltype
= ltype
->ctype
.base_type
;
424 if (ltype
->type
== SYM_ENUM
)
425 ltype
= ltype
->ctype
.base_type
;
426 if (rtype
->type
== SYM_NODE
)
427 rtype
= rtype
->ctype
.base_type
;
428 if (rtype
->type
== SYM_ENUM
)
429 rtype
= rtype
->ctype
.base_type
;
430 if (is_restricted_type(ltype
)) {
431 if (is_restricted_type(rtype
)) {
435 if (!restricted_value(right
, ltype
))
438 } else if (is_restricted_type(rtype
)) {
439 if (!restricted_value(left
, rtype
))
444 if (restricted_binop(op
, type
))
449 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
451 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
452 if (!ctype
&& float_ok
)
453 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
455 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
460 return bad_expr_type(expr
);
463 static inline int lvalue_expression(struct expression
*expr
)
465 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
468 static int ptr_object_size(struct symbol
*ptr_type
)
470 if (ptr_type
->type
== SYM_NODE
)
471 ptr_type
= ptr_type
->ctype
.base_type
;
472 if (ptr_type
->type
== SYM_PTR
)
473 ptr_type
= ptr_type
->ctype
.base_type
;
474 return ptr_type
->bit_size
;
477 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
479 struct expression
*i
= *ip
;
480 struct symbol
*ptr_type
= ctype
;
483 if (ptr_type
->type
== SYM_NODE
)
484 ptr_type
= ptr_type
->ctype
.base_type
;
486 if (!is_int_type(i
->ctype
))
487 return bad_expr_type(expr
);
489 examine_symbol_type(ctype
);
491 if (!ctype
->ctype
.base_type
) {
492 warning(expr
->pos
, "missing type information");
496 /* Get the size of whatever the pointer points to */
497 bit_size
= ptr_object_size(ctype
);
499 if (bit_size
> bits_in_char
) {
500 int multiply
= bit_size
>> 3;
501 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
503 if (i
->type
== EXPR_VALUE
) {
504 val
->value
= i
->value
* multiply
;
505 val
->ctype
= size_t_ctype
;
508 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
510 val
->ctype
= size_t_ctype
;
511 val
->value
= bit_size
>> 3;
514 mul
->ctype
= size_t_ctype
;
526 static struct symbol
*evaluate_add(struct expression
*expr
)
528 struct expression
*left
= expr
->left
, *right
= expr
->right
;
529 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
531 if (is_ptr_type(ltype
))
532 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
534 if (is_ptr_type(rtype
))
535 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
537 return evaluate_arith(expr
, 1);
540 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
541 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
544 unsigned long mod1
, mod2
, diff
;
545 unsigned long as1
, as2
;
547 struct symbol
*base1
, *base2
;
549 if (target
== source
)
551 if (!target
|| !source
)
552 return "different types";
554 * Peel of per-node information.
555 * FIXME! Check alignment and context too here!
557 mod1
= target
->ctype
.modifiers
;
558 as1
= target
->ctype
.as
;
559 mod2
= source
->ctype
.modifiers
;
560 as2
= source
->ctype
.as
;
561 if (target
->type
== SYM_NODE
) {
562 target
= target
->ctype
.base_type
;
565 if (target
->type
== SYM_PTR
) {
569 mod1
|= target
->ctype
.modifiers
;
570 as1
|= target
->ctype
.as
;
572 if (source
->type
== SYM_NODE
) {
573 source
= source
->ctype
.base_type
;
576 if (source
->type
== SYM_PTR
) {
580 mod2
|= source
->ctype
.modifiers
;
581 as2
|= source
->ctype
.as
;
583 if (target
->type
== SYM_ENUM
) {
584 target
= target
->ctype
.base_type
;
588 if (source
->type
== SYM_ENUM
) {
589 source
= source
->ctype
.base_type
;
594 if (target
== source
)
596 if (!target
|| !source
)
597 return "different types";
599 type1
= target
->type
;
600 base1
= target
->ctype
.base_type
;
602 type2
= source
->type
;
603 base2
= source
->ctype
.base_type
;
606 * Pointers to functions compare as the function itself
608 if (type1
== SYM_PTR
&& base1
) {
609 switch (base1
->type
) {
613 base1
= base1
->ctype
.base_type
;
618 if (type2
== SYM_PTR
&& base2
) {
619 switch (base2
->type
) {
623 base2
= base2
->ctype
.base_type
;
629 /* Arrays degenerate to pointers for type comparisons */
630 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
631 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
633 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
634 return "different base types";
636 /* Must be same address space to be comparable */
638 return "different address spaces";
640 /* Ignore differences in storage types or addressability */
641 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
642 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
645 return "different type sizes";
646 if (diff
& ~MOD_SIGNEDNESS
)
647 return "different modifiers";
649 /* Differs in signedness only.. */
652 * Warn if both are explicitly signed ("unsigned" is obvously
653 * always explicit, and since we know one of them has to be
654 * unsigned, we check if the signed one was explicit).
656 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
657 return "different explicit signedness";
660 * "char" matches both "unsigned char" and "signed char",
661 * so if the explicit test didn't trigger, then we should
662 * not warn about a char.
664 if (!(mod1
& MOD_CHAR
))
665 return "different signedness";
669 if (type1
== SYM_FN
) {
671 struct symbol
*arg1
, *arg2
;
672 if (base1
->variadic
!= base2
->variadic
)
673 return "incompatible variadic arguments";
674 PREPARE_PTR_LIST(target
->arguments
, arg1
);
675 PREPARE_PTR_LIST(source
->arguments
, arg2
);
679 diff
= type_difference(arg1
, arg2
, 0, 0);
681 static char argdiff
[80];
682 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
691 FINISH_PTR_LIST(arg2
);
692 FINISH_PTR_LIST(arg1
);
701 static int is_null_ptr(struct expression
*expr
)
703 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
705 if (!is_ptr_type(expr
->ctype
))
706 warning(expr
->pos
, "Using plain integer as NULL pointer");
710 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
712 /* NULL expression? Just return the type of the "other side" */
721 * Ignore differences in "volatile" and "const"ness when
722 * subtracting pointers
724 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
726 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
728 const char *typediff
;
729 struct symbol
*ctype
;
730 struct symbol
*ltype
, *rtype
;
731 struct expression
*r
= *rp
;
733 ltype
= degenerate(l
);
734 rtype
= degenerate(r
);
737 * If it is an integer subtract: the ptr add case will do the
740 if (!is_ptr_type(rtype
))
741 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
744 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
746 ctype
= common_ptr_type(l
, r
);
748 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
752 examine_symbol_type(ctype
);
754 /* Figure out the base type we point to */
755 if (ctype
->type
== SYM_NODE
)
756 ctype
= ctype
->ctype
.base_type
;
757 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
758 warning(expr
->pos
, "subtraction of functions? Share your drugs");
761 ctype
= ctype
->ctype
.base_type
;
763 expr
->ctype
= ssize_t_ctype
;
764 if (ctype
->bit_size
> bits_in_char
) {
765 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
766 struct expression
*div
= expr
;
767 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
768 unsigned long value
= ctype
->bit_size
>> 3;
770 val
->ctype
= size_t_ctype
;
773 if (value
& (value
-1)) {
774 if (Wptr_subtraction_blows
)
775 warning(expr
->pos
, "potentially expensive pointer subtraction");
779 sub
->ctype
= ssize_t_ctype
;
788 return ssize_t_ctype
;
791 static struct symbol
*evaluate_sub(struct expression
*expr
)
793 struct expression
*left
= expr
->left
;
794 struct symbol
*ltype
= left
->ctype
;
796 if (is_ptr_type(ltype
))
797 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
799 return evaluate_arith(expr
, 1);
802 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
804 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
806 struct symbol
*ctype
;
811 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
812 warning(expr
->pos
, "assignment expression in conditional");
814 ctype
= evaluate_expression(expr
);
816 if (is_safe_type(ctype
))
817 warning(expr
->pos
, "testing a 'safe expression'");
823 static struct symbol
*evaluate_logical(struct expression
*expr
)
825 if (!evaluate_conditional(expr
->left
, 0))
827 if (!evaluate_conditional(expr
->right
, 0))
830 expr
->ctype
= &bool_ctype
;
834 static struct symbol
*evaluate_shift(struct expression
*expr
)
836 struct expression
*left
= expr
->left
, *right
= expr
->right
;
837 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
839 if (ltype
->type
== SYM_NODE
)
840 ltype
= ltype
->ctype
.base_type
;
841 if (rtype
->type
== SYM_NODE
)
842 rtype
= rtype
->ctype
.base_type
;
843 if (is_int_type(ltype
) && is_int_type(rtype
)) {
844 struct symbol
*ctype
= integer_promotion(ltype
);
845 expr
->left
= cast_to(expr
->left
, ctype
);
847 ctype
= integer_promotion(rtype
);
848 expr
->right
= cast_to(expr
->right
, ctype
);
851 return bad_expr_type(expr
);
854 static struct symbol
*evaluate_binop(struct expression
*expr
)
857 // addition can take ptr+int, fp and int
859 return evaluate_add(expr
);
861 // subtraction can take ptr-ptr, fp and int
863 return evaluate_sub(expr
);
865 // Arithmetic operations can take fp and int
867 return evaluate_arith(expr
, 1);
869 // shifts do integer promotions, but that's it.
870 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
871 return evaluate_shift(expr
);
873 // The rest are integer operations
874 // '%', '&', '^', '|'
876 return evaluate_arith(expr
, 0);
880 static struct symbol
*evaluate_comma(struct expression
*expr
)
882 expr
->ctype
= expr
->right
->ctype
;
886 static int modify_for_unsigned(int op
)
889 op
= SPECIAL_UNSIGNED_LT
;
891 op
= SPECIAL_UNSIGNED_GT
;
892 else if (op
== SPECIAL_LTE
)
893 op
= SPECIAL_UNSIGNED_LTE
;
894 else if (op
== SPECIAL_GTE
)
895 op
= SPECIAL_UNSIGNED_GTE
;
899 static struct symbol
*evaluate_compare(struct expression
*expr
)
901 struct expression
*left
= expr
->left
, *right
= expr
->right
;
902 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
903 struct symbol
*ctype
;
906 if (is_type_type(ltype
) && is_type_type(rtype
))
909 if (is_safe_type(ltype
) || is_safe_type(rtype
))
910 warning(expr
->pos
, "testing a 'safe expression'");
913 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
914 // FIXME! Check the types for compatibility
915 expr
->op
= modify_for_unsigned(expr
->op
);
919 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
921 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
922 expr
->op
= modify_for_unsigned(expr
->op
);
926 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
930 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
932 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
933 expr
->op
= modify_for_unsigned(expr
->op
);
940 expr
->ctype
= &bool_ctype
;
945 * FIXME!! This should do casts, array degeneration etc..
947 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
949 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
951 if (ltype
->type
== SYM_NODE
)
952 ltype
= ltype
->ctype
.base_type
;
954 if (rtype
->type
== SYM_NODE
)
955 rtype
= rtype
->ctype
.base_type
;
957 if (ltype
->type
== SYM_PTR
) {
958 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
962 if (rtype
->type
== SYM_PTR
) {
963 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
970 * NOTE! The degenerate case of "x ? : y", where we don't
971 * have a true case, this will possibly promote "x" to the
972 * same type as "y", and thus _change_ the conditional
973 * test in the expression. But since promotion is "safe"
974 * for testing, that's ok.
976 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
978 struct expression
**true;
979 struct symbol
*ctype
, *ltype
, *rtype
;
980 const char * typediff
;
982 if (!evaluate_conditional(expr
->conditional
, 0))
984 if (!evaluate_expression(expr
->cond_false
))
987 ctype
= degenerate(expr
->conditional
);
988 rtype
= degenerate(expr
->cond_false
);
990 true = &expr
->conditional
;
992 if (expr
->cond_true
) {
993 if (!evaluate_expression(expr
->cond_true
))
995 ltype
= degenerate(expr
->cond_true
);
996 true = &expr
->cond_true
;
999 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1002 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1005 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1008 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1012 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1015 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1019 expr
->ctype
= ctype
;
1023 /* FP assignments can not do modulo or bit operations */
1024 static int compatible_float_op(int op
)
1027 op
== SPECIAL_ADD_ASSIGN
||
1028 op
== SPECIAL_SUB_ASSIGN
||
1029 op
== SPECIAL_MUL_ASSIGN
||
1030 op
== SPECIAL_DIV_ASSIGN
;
1033 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1034 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1036 const char *typediff
;
1040 if (is_int_type(target
)) {
1041 if (is_int_type(source
))
1043 if (is_float_type(source
))
1045 } else if (is_float_type(target
)) {
1046 if (!compatible_float_op(op
)) {
1047 warning(expr
->pos
, "invalid assignment");
1050 if (is_int_type(source
))
1052 if (is_float_type(source
))
1054 } else if (is_restricted_type(target
)) {
1055 if (restricted_binop(op
, target
)) {
1056 warning(expr
->pos
, "bad restricted assignment");
1059 if (!restricted_value(*rp
, target
))
1061 } else if (is_ptr_type(target
)) {
1062 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1063 evaluate_ptr_add(expr
, target
, rp
);
1067 warning(expr
->pos
, "invalid pointer assignment");
1070 } else if (op
!= '=') {
1071 warning(expr
->pos
, "invalid assignment");
1075 /* It's ok if the target is more volatile or const than the source */
1076 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1080 /* Pointer destination? */
1082 target_as
= t
->ctype
.as
;
1083 if (t
->type
== SYM_NODE
) {
1084 t
= t
->ctype
.base_type
;
1085 target_as
|= t
->ctype
.as
;
1087 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1088 struct expression
*right
= *rp
;
1089 struct symbol
*s
= source
;
1092 // NULL pointer is always ok
1093 if (is_null_ptr(right
))
1096 /* "void *" matches anything as long as the address space is ok */
1097 source_as
= s
->ctype
.as
;
1098 if (s
->type
== SYM_NODE
) {
1099 s
= s
->ctype
.base_type
;
1100 source_as
|= s
->ctype
.as
;
1102 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1103 s
= s
->ctype
.base_type
;
1104 t
= t
->ctype
.base_type
;
1105 if (s
== &void_ctype
|| t
== &void_ctype
)
1110 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1111 info(expr
->pos
, " expected %s", show_typename(target
));
1112 info(expr
->pos
, " got %s", show_typename(source
));
1113 *rp
= cast_to(*rp
, target
);
1116 *rp
= cast_to(*rp
, target
);
1120 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1122 if (type
->ctype
.modifiers
& MOD_CONST
)
1123 warning(left
->pos
, "assignment to const expression");
1124 if (type
->type
== SYM_NODE
)
1125 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1128 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1130 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1131 struct expression
*where
= expr
;
1132 struct symbol
*ltype
, *rtype
;
1134 if (!lvalue_expression(left
)) {
1135 warning(expr
->pos
, "not an lvalue");
1139 ltype
= left
->ctype
;
1141 rtype
= degenerate(right
);
1143 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1146 evaluate_assign_to(left
, ltype
);
1148 expr
->ctype
= ltype
;
1152 static void examine_fn_arguments(struct symbol
*fn
)
1156 FOR_EACH_PTR(fn
->arguments
, s
) {
1157 struct symbol
*arg
= evaluate_symbol(s
);
1158 /* Array/function arguments silently degenerate into pointers */
1164 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1165 if (arg
->type
== SYM_ARRAY
)
1166 ptr
->ctype
= arg
->ctype
;
1168 ptr
->ctype
.base_type
= arg
;
1169 ptr
->ctype
.as
|= s
->ctype
.as
;
1170 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1172 s
->ctype
.base_type
= ptr
;
1174 s
->ctype
.modifiers
= 0;
1177 examine_symbol_type(s
);
1184 } END_FOR_EACH_PTR(s
);
1187 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1189 /* Take the modifiers of the pointer, and apply them to the member */
1190 mod
|= sym
->ctype
.modifiers
;
1191 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1192 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1194 newsym
->ctype
.as
= as
;
1195 newsym
->ctype
.modifiers
= mod
;
1201 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1203 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1204 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1206 node
->ctype
.base_type
= ptr
;
1207 ptr
->bit_size
= bits_in_pointer
;
1208 ptr
->ctype
.alignment
= pointer_alignment
;
1210 node
->bit_size
= bits_in_pointer
;
1211 node
->ctype
.alignment
= pointer_alignment
;
1214 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1215 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1216 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1218 if (sym
->type
== SYM_NODE
) {
1219 ptr
->ctype
.as
|= sym
->ctype
.as
;
1220 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1221 sym
= sym
->ctype
.base_type
;
1223 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1224 ptr
->ctype
.as
|= sym
->ctype
.as
;
1225 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1226 sym
= sym
->ctype
.base_type
;
1228 ptr
->ctype
.base_type
= sym
;
1233 /* Arrays degenerate into pointers on pointer arithmetic */
1234 static struct symbol
*degenerate(struct expression
*expr
)
1236 struct symbol
*ctype
, *base
;
1240 ctype
= expr
->ctype
;
1244 if (ctype
->type
== SYM_NODE
)
1245 base
= ctype
->ctype
.base_type
;
1247 * Arrays degenerate into pointers to the entries, while
1248 * functions degenerate into pointers to themselves.
1249 * If array was part of non-lvalue compound, we create a copy
1250 * of that compound first and then act as if we were dealing with
1251 * the corresponding field in there.
1253 switch (base
->type
) {
1255 if (expr
->type
== EXPR_SLICE
) {
1256 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1257 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1259 a
->ctype
.base_type
= expr
->base
->ctype
;
1260 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1261 a
->array_size
= expr
->base
->ctype
->array_size
;
1263 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1265 e0
->ctype
= &lazy_ptr_ctype
;
1267 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1270 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1272 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1274 e2
->right
= expr
->base
;
1276 e2
->ctype
= expr
->base
->ctype
;
1278 if (expr
->r_bitpos
) {
1279 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1282 e3
->right
= alloc_const_expression(expr
->pos
,
1283 expr
->r_bitpos
>> 3);
1284 e3
->ctype
= &lazy_ptr_ctype
;
1289 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1292 e4
->ctype
= &lazy_ptr_ctype
;
1295 expr
->type
= EXPR_PREOP
;
1299 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1300 warning(expr
->pos
, "strange non-value function or array");
1303 *expr
= *expr
->unop
;
1304 ctype
= create_pointer(expr
, ctype
, 1);
1305 expr
->ctype
= ctype
;
1312 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1314 struct expression
*op
= expr
->unop
;
1315 struct symbol
*ctype
;
1317 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1318 warning(expr
->pos
, "not addressable");
1324 if (expr
->type
== EXPR_SYMBOL
) {
1325 struct symbol
*sym
= expr
->symbol
;
1326 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1330 * symbol expression evaluation is lazy about the type
1331 * of the sub-expression, so we may have to generate
1332 * the type here if so..
1334 if (expr
->ctype
== &lazy_ptr_ctype
) {
1335 ctype
= create_pointer(expr
, ctype
, 0);
1336 expr
->ctype
= ctype
;
1342 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1344 struct expression
*op
= expr
->unop
;
1345 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1347 /* Simplify: *&(expr) => (expr) */
1348 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1353 /* Dereferencing a node drops all the node information. */
1354 if (ctype
->type
== SYM_NODE
)
1355 ctype
= ctype
->ctype
.base_type
;
1357 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1358 target
= ctype
->ctype
.base_type
;
1360 switch (ctype
->type
) {
1362 warning(expr
->pos
, "cannot derefence this type");
1365 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1366 merge_type(node
, ctype
);
1370 if (!lvalue_expression(op
)) {
1371 warning(op
->pos
, "non-lvalue array??");
1375 /* Do the implied "addressof" on the array */
1379 * When an array is dereferenced, we need to pick
1380 * up the attributes of the original node too..
1382 merge_type(node
, op
->ctype
);
1383 merge_type(node
, ctype
);
1387 node
->bit_size
= target
->bit_size
;
1388 node
->array_size
= target
->array_size
;
1395 * Unary post-ops: x++ and x--
1397 static struct symbol
*evaluate_postop(struct expression
*expr
)
1399 struct expression
*op
= expr
->unop
;
1400 struct symbol
*ctype
= op
->ctype
;
1402 if (!lvalue_expression(expr
->unop
)) {
1403 warning(expr
->pos
, "need lvalue expression for ++/--");
1406 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1407 warning(expr
->pos
, "bad operation on restricted");
1411 evaluate_assign_to(op
, ctype
);
1413 expr
->ctype
= ctype
;
1415 if (is_ptr_type(ctype
))
1416 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1421 static struct symbol
*evaluate_sign(struct expression
*expr
)
1423 struct symbol
*ctype
= expr
->unop
->ctype
;
1424 if (is_int_type(ctype
)) {
1425 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1426 expr
->unop
= cast_to(expr
->unop
, rtype
);
1428 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1429 /* no conversions needed */
1430 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1431 /* no conversions needed */
1433 return bad_expr_type(expr
);
1435 if (expr
->op
== '+')
1436 *expr
= *expr
->unop
;
1437 expr
->ctype
= ctype
;
1441 static struct symbol
*evaluate_preop(struct expression
*expr
)
1443 struct symbol
*ctype
= expr
->unop
->ctype
;
1447 *expr
= *expr
->unop
;
1453 return evaluate_sign(expr
);
1456 return evaluate_dereference(expr
);
1459 return evaluate_addressof(expr
);
1461 case SPECIAL_INCREMENT
:
1462 case SPECIAL_DECREMENT
:
1464 * From a type evaluation standpoint the pre-ops are
1465 * the same as the postops
1467 return evaluate_postop(expr
);
1470 if (is_safe_type(ctype
))
1471 warning(expr
->pos
, "testing a 'safe expression'");
1472 if (is_float_type(ctype
)) {
1473 struct expression
*arg
= expr
->unop
;
1474 expr
->type
= EXPR_BINOP
;
1475 expr
->op
= SPECIAL_EQUAL
;
1477 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1478 expr
->right
->ctype
= ctype
;
1479 expr
->right
->fvalue
= 0;
1481 ctype
= &bool_ctype
;
1487 expr
->ctype
= ctype
;
1491 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1493 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1494 struct ptr_list
*list
= head
;
1500 for (i
= 0; i
< list
->nr
; i
++) {
1501 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1503 if (sym
->ident
!= ident
)
1505 *offset
= sym
->offset
;
1508 struct symbol
*ctype
= sym
->ctype
.base_type
;
1512 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1514 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1517 *offset
+= sym
->offset
;
1521 } while ((list
= list
->next
) != head
);
1525 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1527 struct expression
*add
;
1530 * Create a new add-expression
1532 * NOTE! Even if we just add zero, we need a new node
1533 * for the member pointer, since it has a different
1534 * type than the original pointer. We could make that
1535 * be just a cast, but the fact is, a node is a node,
1536 * so we might as well just do the "add zero" here.
1538 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1541 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1542 add
->right
->ctype
= &int_ctype
;
1543 add
->right
->value
= offset
;
1546 * The ctype of the pointer will be lazily evaluated if
1547 * we ever take the address of this member dereference..
1549 add
->ctype
= &lazy_ptr_ctype
;
1553 /* structure/union dereference */
1554 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1557 struct symbol
*ctype
, *member
;
1558 struct expression
*deref
= expr
->deref
, *add
;
1559 struct ident
*ident
= expr
->member
;
1563 if (!evaluate_expression(deref
))
1566 warning(expr
->pos
, "bad member name");
1570 ctype
= deref
->ctype
;
1571 address_space
= ctype
->ctype
.as
;
1572 mod
= ctype
->ctype
.modifiers
;
1573 if (ctype
->type
== SYM_NODE
) {
1574 ctype
= ctype
->ctype
.base_type
;
1575 address_space
|= ctype
->ctype
.as
;
1576 mod
|= ctype
->ctype
.modifiers
;
1578 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1579 warning(expr
->pos
, "expected structure or union");
1583 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1585 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1586 const char *name
= "<unnamed>";
1589 name
= ctype
->ident
->name
;
1590 namelen
= ctype
->ident
->len
;
1592 warning(expr
->pos
, "no member '%s' in %s %.*s",
1593 show_ident(ident
), type
, namelen
, name
);
1598 * The member needs to take on the address space and modifiers of
1599 * the "parent" type.
1601 member
= convert_to_as_mod(member
, address_space
, mod
);
1602 ctype
= member
->ctype
.base_type
;
1604 if (!lvalue_expression(deref
)) {
1605 if (deref
->type
!= EXPR_SLICE
) {
1609 expr
->base
= deref
->base
;
1610 expr
->r_bitpos
= deref
->r_bitpos
;
1612 expr
->r_bitpos
+= offset
<< 3;
1613 expr
->type
= EXPR_SLICE
;
1614 expr
->r_nrbits
= member
->bit_size
;
1615 expr
->r_bitpos
+= member
->bit_offset
;
1616 expr
->ctype
= member
;
1620 deref
= deref
->unop
;
1621 expr
->deref
= deref
;
1623 add
= evaluate_offset(deref
, offset
);
1624 expr
->type
= EXPR_PREOP
;
1628 expr
->ctype
= member
;
1632 static int is_promoted(struct expression
*expr
)
1635 switch (expr
->type
) {
1638 case EXPR_CONDITIONAL
:
1662 static struct symbol
*evaluate_cast(struct expression
*);
1664 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1666 struct symbol
*sym
= expr
->cast_type
;
1668 sym
= evaluate_expression(expr
->cast_expression
);
1672 * Expressions of restricted types will possibly get
1673 * promoted - check that here
1675 if (is_restricted_type(sym
)) {
1676 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1680 examine_symbol_type(sym
);
1681 if (is_bitfield_type(sym
)) {
1682 warning(expr
->pos
, "trying to examine bitfield type");
1688 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1690 struct symbol
*type
;
1693 type
= evaluate_type_information(expr
);
1697 size
= type
->bit_size
;
1699 warning(expr
->pos
, "cannot size expression");
1700 expr
->type
= EXPR_VALUE
;
1701 expr
->value
= size
>> 3;
1702 expr
->ctype
= size_t_ctype
;
1703 return size_t_ctype
;
1706 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1708 struct symbol
*type
;
1711 type
= evaluate_type_information(expr
);
1715 if (type
->type
== SYM_NODE
)
1716 type
= type
->ctype
.base_type
;
1719 switch (type
->type
) {
1723 type
= type
->ctype
.base_type
;
1727 warning(expr
->pos
, "expected pointer expression");
1730 size
= type
->bit_size
;
1733 expr
->type
= EXPR_VALUE
;
1734 expr
->value
= size
>> 3;
1735 expr
->ctype
= size_t_ctype
;
1736 return size_t_ctype
;
1739 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1741 struct symbol
*type
;
1743 type
= evaluate_type_information(expr
);
1747 expr
->type
= EXPR_VALUE
;
1748 expr
->value
= type
->ctype
.alignment
;
1749 expr
->ctype
= size_t_ctype
;
1750 return size_t_ctype
;
1753 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1755 struct expression
*expr
;
1756 struct symbol_list
*argument_types
= fn
->arguments
;
1757 struct symbol
*argtype
;
1760 PREPARE_PTR_LIST(argument_types
, argtype
);
1761 FOR_EACH_PTR (head
, expr
) {
1762 struct expression
**p
= THIS_ADDRESS(expr
);
1763 struct symbol
*ctype
, *target
;
1764 ctype
= evaluate_expression(expr
);
1769 ctype
= degenerate(expr
);
1772 if (!target
&& ctype
->bit_size
< bits_in_int
)
1773 target
= &int_ctype
;
1775 static char where
[30];
1776 examine_symbol_type(target
);
1777 sprintf(where
, "argument %d", i
);
1778 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1782 NEXT_PTR_LIST(argtype
);
1783 } END_FOR_EACH_PTR(expr
);
1784 FINISH_PTR_LIST(argtype
);
1788 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1790 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1792 struct expression
*entry
= *ep
;
1793 struct expression
**parent
, *reuse
= NULL
;
1794 unsigned long offset
;
1796 unsigned long from
, to
;
1797 int accept_string
= is_byte_type(ctype
);
1802 if (entry
->type
== EXPR_INDEX
) {
1803 from
= entry
->idx_from
;
1804 to
= entry
->idx_to
+1;
1805 parent
= &entry
->idx_expression
;
1807 entry
= entry
->idx_expression
;
1810 offset
= from
* (ctype
->bit_size
>>3);
1812 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1813 reuse
->type
= EXPR_POS
;
1814 reuse
->ctype
= ctype
;
1815 reuse
->init_offset
= offset
;
1816 reuse
->init_nr
= to
- from
;
1817 reuse
->init_expr
= entry
;
1818 parent
= &reuse
->init_expr
;
1823 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1824 sym
= evaluate_expression(entry
);
1825 to
= from
+ get_expression_value(sym
->array_size
);
1827 evaluate_initializer(ctype
, parent
);
1832 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1834 struct expression
*entry
;
1837 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1838 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1839 } END_FOR_EACH_PTR(entry
);
1842 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1843 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1845 if (expression_list_size(expr
->expr_list
) != 1) {
1846 warning(expr
->pos
, "unexpected compound initializer");
1849 evaluate_array_initializer(ctype
, expr
);
1853 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1857 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1858 if (sym
->ident
== ident
)
1860 } END_FOR_EACH_PTR(sym
);
1864 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1866 struct expression
*entry
= *ep
;
1867 struct expression
**parent
;
1868 struct expression
*reuse
= NULL
;
1869 unsigned long offset
;
1872 error(entry
->pos
, "unknown named initializer");
1876 if (entry
->type
== EXPR_IDENTIFIER
) {
1878 entry
= entry
->ident_expression
;
1882 offset
= sym
->offset
;
1885 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1886 reuse
->type
= EXPR_POS
;
1888 reuse
->init_offset
= offset
;
1890 reuse
->init_expr
= entry
;
1891 parent
= &reuse
->init_expr
;
1895 evaluate_initializer(sym
, parent
);
1899 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1901 struct expression
*entry
;
1904 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1905 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1906 if (entry
->type
== EXPR_IDENTIFIER
) {
1907 struct ident
*ident
= entry
->expr_ident
;
1908 /* We special-case the "already right place" case */
1909 if (!sym
|| sym
->ident
!= ident
) {
1910 RESET_PTR_LIST(sym
);
1914 if (sym
->ident
== ident
)
1920 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1923 } END_FOR_EACH_PTR(entry
);
1924 FINISH_PTR_LIST(sym
);
1928 * Initializers are kind of like assignments. Except
1929 * they can be a hell of a lot more complex.
1931 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1933 struct expression
*expr
= *ep
;
1936 * Simple non-structure/array initializers are the simple
1937 * case, and look (and parse) largely like assignments.
1939 switch (expr
->type
) {
1941 int is_string
= expr
->type
== EXPR_STRING
;
1942 struct symbol
*rtype
= evaluate_expression(expr
);
1946 * char array[] = "string"
1947 * should _not_ degenerate.
1949 if (!is_string
|| !is_string_type(ctype
))
1950 rtype
= degenerate(expr
);
1951 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1956 case EXPR_INITIALIZER
:
1957 expr
->ctype
= ctype
;
1958 if (ctype
->type
== SYM_NODE
)
1959 ctype
= ctype
->ctype
.base_type
;
1961 switch (ctype
->type
) {
1964 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1967 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1970 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1973 evaluate_scalar_initializer(ctype
, expr
);
1977 case EXPR_IDENTIFIER
:
1978 if (ctype
->type
== SYM_NODE
)
1979 ctype
= ctype
->ctype
.base_type
;
1980 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1981 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1985 evaluate_one_struct_initializer(ctype
, ep
,
1986 find_struct_ident(ctype
, expr
->expr_ident
));
1990 if (ctype
->type
== SYM_NODE
)
1991 ctype
= ctype
->ctype
.base_type
;
1992 if (ctype
->type
!= SYM_ARRAY
) {
1993 error(expr
->pos
, "expected array");
1996 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2001 * An EXPR_POS expression has already been evaluated, and we don't
2002 * need to do anything more
2008 static int get_as(struct symbol
*sym
)
2016 mod
= sym
->ctype
.modifiers
;
2017 if (sym
->type
== SYM_NODE
) {
2018 sym
= sym
->ctype
.base_type
;
2019 as
|= sym
->ctype
.as
;
2020 mod
|= sym
->ctype
.modifiers
;
2024 * At least for now, allow casting to a "unsigned long".
2025 * That's how we do things like pointer arithmetic and
2026 * store pointers to registers.
2028 if (sym
== &ulong_ctype
)
2031 if (sym
&& sym
->type
== SYM_PTR
) {
2032 sym
= sym
->ctype
.base_type
;
2033 as
|= sym
->ctype
.as
;
2034 mod
|= sym
->ctype
.modifiers
;
2036 if (mod
& MOD_FORCE
)
2041 static struct symbol
*evaluate_cast(struct expression
*expr
)
2043 struct expression
*target
= expr
->cast_expression
;
2044 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2050 expr
->ctype
= ctype
;
2051 expr
->cast_type
= ctype
;
2054 * Special case: a cast can be followed by an
2055 * initializer, in which case we need to pass
2056 * the type value down to that initializer rather
2057 * than trying to evaluate it as an expression
2059 * A more complex case is when the initializer is
2060 * dereferenced as part of a post-fix expression.
2061 * We need to produce an expression that can be dereferenced.
2063 if (target
->type
== EXPR_INITIALIZER
) {
2064 struct symbol
*sym
= expr
->cast_type
;
2065 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2067 sym
->initializer
= expr
->cast_expression
;
2068 evaluate_symbol(sym
);
2070 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2073 expr
->type
= EXPR_PREOP
;
2081 evaluate_expression(target
);
2085 * You can always throw a value away by casting to
2086 * "void" - that's an implicit "force". Note that
2087 * the same is _not_ true of "void *".
2089 if (ctype
== &void_ctype
)
2093 if (type
== SYM_NODE
) {
2094 type
= ctype
->ctype
.base_type
->type
;
2095 if (ctype
->ctype
.base_type
== &void_ctype
)
2098 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2099 warning(expr
->pos
, "cast to non-scalar");
2101 if (!target
->ctype
) {
2102 warning(expr
->pos
, "cast from unknown type");
2106 type
= target
->ctype
->type
;
2107 if (type
== SYM_NODE
)
2108 type
= target
->ctype
->ctype
.base_type
->type
;
2109 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2110 warning(expr
->pos
, "cast from non-scalar");
2112 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2113 warning(expr
->pos
, "cast removes address space of expression");
2115 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2116 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2117 if (t1
->type
== SYM_NODE
)
2118 t1
= t1
->ctype
.base_type
;
2119 if (t2
->type
== SYM_NODE
)
2120 t2
= t2
->ctype
.base_type
;
2122 if (t1
->type
== SYM_RESTRICT
)
2123 warning(expr
->pos
, "cast to restricted type");
2124 if (t2
->type
== SYM_RESTRICT
)
2125 warning(expr
->pos
, "cast from restricted type");
2130 * Casts of constant values are special: they
2131 * can be NULL, and thus need to be simplified
2134 if (target
->type
== EXPR_VALUE
)
2135 cast_value(expr
, ctype
, target
, target
->ctype
);
2142 * Evaluate a call expression with a symbol. This
2143 * should expand inline functions, and evaluate
2146 static int evaluate_symbol_call(struct expression
*expr
)
2148 struct expression
*fn
= expr
->fn
;
2149 struct symbol
*ctype
= fn
->ctype
;
2151 if (fn
->type
!= EXPR_PREOP
)
2154 if (ctype
->op
&& ctype
->op
->evaluate
)
2155 return ctype
->op
->evaluate(expr
);
2157 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2159 struct symbol
*curr
= current_fn
;
2160 current_fn
= ctype
->ctype
.base_type
;
2161 examine_fn_arguments(current_fn
);
2163 ret
= inline_function(expr
, ctype
);
2165 /* restore the old function */
2173 static struct symbol
*evaluate_call(struct expression
*expr
)
2176 struct symbol
*ctype
, *sym
;
2177 struct expression
*fn
= expr
->fn
;
2178 struct expression_list
*arglist
= expr
->args
;
2180 if (!evaluate_expression(fn
))
2182 sym
= ctype
= fn
->ctype
;
2183 if (ctype
->type
== SYM_NODE
)
2184 ctype
= ctype
->ctype
.base_type
;
2185 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2186 ctype
= ctype
->ctype
.base_type
;
2187 if (!evaluate_arguments(sym
, ctype
, arglist
))
2189 if (ctype
->type
!= SYM_FN
) {
2190 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2193 args
= expression_list_size(expr
->args
);
2194 fnargs
= symbol_list_size(ctype
->arguments
);
2196 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2197 if (args
> fnargs
&& !ctype
->variadic
)
2198 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2199 if (sym
->type
== SYM_NODE
) {
2200 if (evaluate_symbol_call(expr
))
2203 expr
->ctype
= ctype
->ctype
.base_type
;
2207 struct symbol
*evaluate_expression(struct expression
*expr
)
2214 switch (expr
->type
) {
2217 warning(expr
->pos
, "value expression without a type");
2220 return evaluate_string(expr
);
2222 return evaluate_symbol_expression(expr
);
2224 if (!evaluate_expression(expr
->left
))
2226 if (!evaluate_expression(expr
->right
))
2228 return evaluate_binop(expr
);
2230 return evaluate_logical(expr
);
2232 evaluate_expression(expr
->left
);
2233 if (!evaluate_expression(expr
->right
))
2235 return evaluate_comma(expr
);
2237 if (!evaluate_expression(expr
->left
))
2239 if (!evaluate_expression(expr
->right
))
2241 return evaluate_compare(expr
);
2242 case EXPR_ASSIGNMENT
:
2243 if (!evaluate_expression(expr
->left
))
2245 if (!evaluate_expression(expr
->right
))
2247 return evaluate_assignment(expr
);
2249 if (!evaluate_expression(expr
->unop
))
2251 return evaluate_preop(expr
);
2253 if (!evaluate_expression(expr
->unop
))
2255 return evaluate_postop(expr
);
2257 case EXPR_IMPLIED_CAST
:
2258 return evaluate_cast(expr
);
2260 return evaluate_sizeof(expr
);
2261 case EXPR_PTRSIZEOF
:
2262 return evaluate_ptrsizeof(expr
);
2264 return evaluate_alignof(expr
);
2266 return evaluate_member_dereference(expr
);
2268 return evaluate_call(expr
);
2270 case EXPR_CONDITIONAL
:
2271 return evaluate_conditional_expression(expr
);
2272 case EXPR_STATEMENT
:
2273 expr
->ctype
= evaluate_statement(expr
->statement
);
2277 expr
->ctype
= &ptr_ctype
;
2281 /* Evaluate the type of the symbol .. */
2282 evaluate_symbol(expr
->symbol
);
2283 /* .. but the type of the _expression_ is a "type" */
2284 expr
->ctype
= &type_ctype
;
2287 /* These can not exist as stand-alone expressions */
2288 case EXPR_INITIALIZER
:
2289 case EXPR_IDENTIFIER
:
2292 warning(expr
->pos
, "internal front-end error: initializer in expression");
2295 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2301 static void check_duplicates(struct symbol
*sym
)
2303 struct symbol
*next
= sym
;
2305 while ((next
= next
->same_symbol
) != NULL
) {
2306 const char *typediff
;
2307 evaluate_symbol(next
);
2308 typediff
= type_difference(sym
, next
, 0, 0);
2310 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2311 show_ident(sym
->ident
),
2312 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2318 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2320 struct symbol
*base_type
;
2325 sym
= examine_symbol_type(sym
);
2326 base_type
= sym
->ctype
.base_type
;
2330 /* Evaluate the initializers */
2331 if (sym
->initializer
)
2332 evaluate_initializer(sym
, &sym
->initializer
);
2334 /* And finally, evaluate the body of the symbol too */
2335 if (base_type
->type
== SYM_FN
) {
2336 struct symbol
*curr
= current_fn
;
2338 current_fn
= base_type
;
2340 examine_fn_arguments(base_type
);
2341 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2343 if (base_type
->stmt
)
2344 evaluate_statement(base_type
->stmt
);
2352 void evaluate_symbol_list(struct symbol_list
*list
)
2356 FOR_EACH_PTR(list
, sym
) {
2357 check_duplicates(sym
);
2358 evaluate_symbol(sym
);
2359 } END_FOR_EACH_PTR(sym
);
2362 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2364 struct expression
*expr
= stmt
->expression
;
2365 struct symbol
*ctype
, *fntype
;
2367 evaluate_expression(expr
);
2368 ctype
= degenerate(expr
);
2369 fntype
= current_fn
->ctype
.base_type
;
2370 if (!fntype
|| fntype
== &void_ctype
) {
2371 if (expr
&& ctype
!= &void_ctype
)
2372 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2377 warning(stmt
->pos
, "return with no return value");
2382 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2386 static void evaluate_if_statement(struct statement
*stmt
)
2388 if (!stmt
->if_conditional
)
2391 evaluate_conditional(stmt
->if_conditional
, 0);
2392 evaluate_statement(stmt
->if_true
);
2393 evaluate_statement(stmt
->if_false
);
2396 static void evaluate_iterator(struct statement
*stmt
)
2398 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2399 evaluate_conditional(stmt
->iterator_post_condition
,1);
2400 evaluate_statement(stmt
->iterator_pre_statement
);
2401 evaluate_statement(stmt
->iterator_statement
);
2402 evaluate_statement(stmt
->iterator_post_statement
);
2405 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2407 switch (*constraint
) {
2408 case '=': /* Assignment */
2409 case '+': /* Update */
2412 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2416 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2418 switch (*constraint
) {
2419 case '=': /* Assignment */
2420 case '+': /* Update */
2421 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2425 static void evaluate_asm_statement(struct statement
*stmt
)
2427 struct expression
*expr
;
2430 expr
= stmt
->asm_string
;
2431 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2432 warning(stmt
->pos
, "need constant string for inline asm");
2437 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2438 struct ident
*ident
;
2441 case 0: /* Identifier */
2443 ident
= (struct ident
*)expr
;
2446 case 1: /* Constraint */
2448 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2449 warning(expr
->pos
, "asm output constraint is not a string");
2450 *THIS_ADDRESS(expr
) = NULL
;
2453 verify_output_constraint(expr
, expr
->string
->data
);
2456 case 2: /* Expression */
2458 if (!evaluate_expression(expr
))
2460 if (!lvalue_expression(expr
))
2461 warning(expr
->pos
, "asm output is not an lvalue");
2462 evaluate_assign_to(expr
, expr
->ctype
);
2465 } END_FOR_EACH_PTR(expr
);
2468 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2469 struct ident
*ident
;
2472 case 0: /* Identifier */
2474 ident
= (struct ident
*)expr
;
2477 case 1: /* Constraint */
2479 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2480 warning(expr
->pos
, "asm input constraint is not a string");
2481 *THIS_ADDRESS(expr
) = NULL
;
2484 verify_input_constraint(expr
, expr
->string
->data
);
2487 case 2: /* Expression */
2489 if (!evaluate_expression(expr
))
2493 } END_FOR_EACH_PTR(expr
);
2495 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2497 warning(stmt
->pos
, "bad asm output");
2500 if (expr
->type
== EXPR_STRING
)
2502 warning(expr
->pos
, "asm clobber is not a string");
2503 } END_FOR_EACH_PTR(expr
);
2506 struct symbol
*evaluate_statement(struct statement
*stmt
)
2511 switch (stmt
->type
) {
2513 return evaluate_return_expression(stmt
);
2515 case STMT_EXPRESSION
:
2516 if (!evaluate_expression(stmt
->expression
))
2518 return degenerate(stmt
->expression
);
2520 case STMT_COMPOUND
: {
2521 struct statement
*s
;
2522 struct symbol
*type
= NULL
;
2525 /* Evaluate each symbol in the compound statement */
2526 FOR_EACH_PTR(stmt
->syms
, sym
) {
2527 evaluate_symbol(sym
);
2528 } END_FOR_EACH_PTR(sym
);
2529 evaluate_symbol(stmt
->ret
);
2532 * Then, evaluate each statement, making the type of the
2533 * compound statement be the type of the last statement
2536 FOR_EACH_PTR(stmt
->stmts
, s
) {
2537 type
= evaluate_statement(s
);
2538 } END_FOR_EACH_PTR(s
);
2544 evaluate_if_statement(stmt
);
2547 evaluate_iterator(stmt
);
2550 evaluate_expression(stmt
->switch_expression
);
2551 evaluate_statement(stmt
->switch_statement
);
2554 evaluate_expression(stmt
->case_expression
);
2555 evaluate_expression(stmt
->case_to
);
2556 evaluate_statement(stmt
->case_statement
);
2559 return evaluate_statement(stmt
->label_statement
);
2561 evaluate_expression(stmt
->goto_expression
);
2566 evaluate_asm_statement(stmt
);
2569 evaluate_expression(stmt
->expression
);
2572 evaluate_expression(stmt
->range_expression
);
2573 evaluate_expression(stmt
->range_low
);
2574 evaluate_expression(stmt
->range_high
);