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
;
192 * This gets called for implicit casts in assignments and
193 * integer promotion. We often want to try to move the
194 * cast down, because the ops involved may have been
195 * implicitly cast up, and we can get rid of the casts
198 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
200 struct expression
*expr
;
203 * See if we can simplify the op. Move the cast down.
207 if (old
->op
== '~') {
209 old
->unop
= cast_to(old
->unop
, type
);
214 case EXPR_IMPLIED_CAST
:
215 if (old
->ctype
->bit_size
>= type
->bit_size
) {
216 struct expression
*orig
= old
->cast_expression
;
217 if (same_cast_type(orig
->ctype
, type
))
219 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
221 old
->cast_type
= type
;
231 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
233 expr
->cast_type
= type
;
234 expr
->cast_expression
= old
;
238 static int is_type_type(struct symbol
*type
)
240 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
243 static int is_ptr_type(struct symbol
*type
)
245 if (type
->type
== SYM_NODE
)
246 type
= type
->ctype
.base_type
;
247 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
250 static inline int is_float_type(struct symbol
*type
)
252 if (type
->type
== SYM_NODE
)
253 type
= type
->ctype
.base_type
;
254 return type
->ctype
.base_type
== &fp_type
;
257 static inline int is_byte_type(struct symbol
*type
)
259 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
262 static inline int is_string_type(struct symbol
*type
)
264 if (type
->type
== SYM_NODE
)
265 type
= type
->ctype
.base_type
;
266 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
269 static struct symbol
*bad_expr_type(struct expression
*expr
)
271 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
272 switch (expr
->type
) {
275 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
276 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
280 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
289 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
291 struct expression
*left
= *lp
, *right
= *rp
;
292 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
294 if (ltype
->type
== SYM_NODE
)
295 ltype
= ltype
->ctype
.base_type
;
296 if (rtype
->type
== SYM_NODE
)
297 rtype
= rtype
->ctype
.base_type
;
298 if (is_float_type(ltype
)) {
299 if (is_int_type(rtype
))
301 if (is_float_type(rtype
)) {
302 unsigned long lmod
= ltype
->ctype
.modifiers
;
303 unsigned long rmod
= rtype
->ctype
.modifiers
;
304 lmod
&= MOD_LONG
| MOD_LONGLONG
;
305 rmod
&= MOD_LONG
| MOD_LONGLONG
;
315 if (!is_float_type(rtype
) || !is_int_type(ltype
))
318 *lp
= cast_to(left
, rtype
);
321 *rp
= cast_to(right
, ltype
);
325 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
327 struct expression
*left
= *lp
, *right
= *rp
;
328 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
330 if (ltype
->type
== SYM_NODE
)
331 ltype
= ltype
->ctype
.base_type
;
332 if (rtype
->type
== SYM_NODE
)
333 rtype
= rtype
->ctype
.base_type
;
334 if (is_int_type(ltype
) && is_int_type(rtype
)) {
335 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
337 /* Don't bother promoting same-size entities, it only adds clutter */
338 if (ltype
->bit_size
!= ctype
->bit_size
)
339 *lp
= cast_to(left
, ctype
);
340 if (rtype
->bit_size
!= ctype
->bit_size
)
341 *rp
= cast_to(right
, ctype
);
347 static int restricted_value(struct expression
*v
, struct symbol
*type
)
349 if (v
->type
!= EXPR_VALUE
)
356 static int restricted_binop(int op
, struct symbol
*type
)
365 case SPECIAL_NOTEQUAL
:
366 case SPECIAL_AND_ASSIGN
:
367 case SPECIAL_OR_ASSIGN
:
368 case SPECIAL_XOR_ASSIGN
:
375 static int restricted_unop(int op
, struct symbol
*type
)
377 if (op
== '~' && type
->bit_size
>= bits_in_int
)
384 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
386 struct expression
*left
= *lp
, *right
= *rp
;
387 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
388 struct symbol
*type
= NULL
;
390 if (ltype
->type
== SYM_NODE
)
391 ltype
= ltype
->ctype
.base_type
;
392 if (ltype
->type
== SYM_ENUM
)
393 ltype
= ltype
->ctype
.base_type
;
394 if (rtype
->type
== SYM_NODE
)
395 rtype
= rtype
->ctype
.base_type
;
396 if (rtype
->type
== SYM_ENUM
)
397 rtype
= rtype
->ctype
.base_type
;
398 if (is_restricted_type(ltype
)) {
399 if (is_restricted_type(rtype
)) {
403 if (!restricted_value(right
, ltype
))
406 } else if (is_restricted_type(rtype
)) {
407 if (!restricted_value(left
, rtype
))
412 if (restricted_binop(op
, type
))
417 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
419 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
420 if (!ctype
&& float_ok
)
421 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
423 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
428 return bad_expr_type(expr
);
431 static inline int lvalue_expression(struct expression
*expr
)
433 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
436 static int ptr_object_size(struct symbol
*ptr_type
)
438 if (ptr_type
->type
== SYM_NODE
)
439 ptr_type
= ptr_type
->ctype
.base_type
;
440 if (ptr_type
->type
== SYM_PTR
)
441 ptr_type
= ptr_type
->ctype
.base_type
;
442 return ptr_type
->bit_size
;
445 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
447 struct expression
*i
= *ip
;
448 struct symbol
*ptr_type
= ctype
;
451 if (ptr_type
->type
== SYM_NODE
)
452 ptr_type
= ptr_type
->ctype
.base_type
;
454 if (!is_int_type(i
->ctype
))
455 return bad_expr_type(expr
);
457 examine_symbol_type(ctype
);
459 if (!ctype
->ctype
.base_type
) {
460 warning(expr
->pos
, "missing type information");
464 /* Get the size of whatever the pointer points to */
465 bit_size
= ptr_object_size(ctype
);
467 if (bit_size
> bits_in_char
) {
468 int multiply
= bit_size
>> 3;
469 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
471 if (i
->type
== EXPR_VALUE
) {
472 val
->value
= i
->value
* multiply
;
473 val
->ctype
= size_t_ctype
;
476 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
478 val
->ctype
= size_t_ctype
;
479 val
->value
= bit_size
>> 3;
482 mul
->ctype
= size_t_ctype
;
494 static struct symbol
*evaluate_add(struct expression
*expr
)
496 struct expression
*left
= expr
->left
, *right
= expr
->right
;
497 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
499 if (is_ptr_type(ltype
))
500 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
502 if (is_ptr_type(rtype
))
503 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
505 return evaluate_arith(expr
, 1);
508 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
509 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
510 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
512 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
513 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
516 unsigned long mod1
, mod2
, diff
;
517 unsigned long as1
, as2
;
519 struct symbol
*base1
, *base2
;
521 if (target
== source
)
523 if (!target
|| !source
)
524 return "different types";
526 * Peel of per-node information.
527 * FIXME! Check alignment and context too here!
529 mod1
= target
->ctype
.modifiers
;
530 as1
= target
->ctype
.as
;
531 mod2
= source
->ctype
.modifiers
;
532 as2
= source
->ctype
.as
;
533 if (target
->type
== SYM_NODE
) {
534 target
= target
->ctype
.base_type
;
537 if (target
->type
== SYM_PTR
) {
541 mod1
|= target
->ctype
.modifiers
;
542 as1
|= target
->ctype
.as
;
544 if (source
->type
== SYM_NODE
) {
545 source
= source
->ctype
.base_type
;
548 if (source
->type
== SYM_PTR
) {
552 mod2
|= source
->ctype
.modifiers
;
553 as2
|= source
->ctype
.as
;
555 if (target
->type
== SYM_ENUM
) {
556 target
= target
->ctype
.base_type
;
560 if (source
->type
== SYM_ENUM
) {
561 source
= source
->ctype
.base_type
;
566 if (target
== source
)
568 if (!target
|| !source
)
569 return "different types";
571 type1
= target
->type
;
572 base1
= target
->ctype
.base_type
;
574 type2
= source
->type
;
575 base2
= source
->ctype
.base_type
;
578 * Pointers to functions compare as the function itself
580 if (type1
== SYM_PTR
&& base1
) {
581 switch (base1
->type
) {
585 base1
= base1
->ctype
.base_type
;
590 if (type2
== SYM_PTR
&& base2
) {
591 switch (base2
->type
) {
595 base2
= base2
->ctype
.base_type
;
601 /* Arrays degenerate to pointers for type comparisons */
602 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
603 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
605 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
606 return "different base types";
608 /* Must be same address space to be comparable */
610 return "different address spaces";
612 /* Ignore differences in storage types or addressability */
613 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
614 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
617 return "different type sizes";
618 if (diff
& ~MOD_SIGNEDNESS
)
619 return "different modifiers";
621 /* Differs in signedness only.. */
624 * Warn if both are explicitly signed ("unsigned" is obvously
625 * always explicit, and since we know one of them has to be
626 * unsigned, we check if the signed one was explicit).
628 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
629 return "different explicit signedness";
632 * "char" matches both "unsigned char" and "signed char",
633 * so if the explicit test didn't trigger, then we should
634 * not warn about a char.
636 if (!(mod1
& MOD_CHAR
))
637 return "different signedness";
641 if (type1
== SYM_FN
) {
643 struct symbol
*arg1
, *arg2
;
644 if (base1
->variadic
!= base2
->variadic
)
645 return "incompatible variadic arguments";
646 PREPARE_PTR_LIST(target
->arguments
, arg1
);
647 PREPARE_PTR_LIST(source
->arguments
, arg2
);
651 diff
= type_difference(arg1
, arg2
, 0, 0);
653 static char argdiff
[80];
654 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
663 FINISH_PTR_LIST(arg2
);
664 FINISH_PTR_LIST(arg1
);
673 static int is_null_ptr(struct expression
*expr
)
675 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
677 if (!is_ptr_type(expr
->ctype
))
678 warning(expr
->pos
, "Using plain integer as NULL pointer");
682 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
684 /* NULL expression? Just return the type of the "other side" */
693 * Ignore differences in "volatile" and "const"ness when
694 * subtracting pointers
696 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
698 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
700 const char *typediff
;
701 struct symbol
*ctype
;
702 struct symbol
*ltype
, *rtype
;
703 struct expression
*r
= *rp
;
705 ltype
= degenerate(l
);
706 rtype
= degenerate(r
);
709 * If it is an integer subtract: the ptr add case will do the
712 if (!is_ptr_type(rtype
))
713 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
716 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
718 ctype
= common_ptr_type(l
, r
);
720 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
724 examine_symbol_type(ctype
);
726 /* Figure out the base type we point to */
727 if (ctype
->type
== SYM_NODE
)
728 ctype
= ctype
->ctype
.base_type
;
729 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
730 warning(expr
->pos
, "subtraction of functions? Share your drugs");
733 ctype
= ctype
->ctype
.base_type
;
735 expr
->ctype
= ssize_t_ctype
;
736 if (ctype
->bit_size
> bits_in_char
) {
737 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
738 struct expression
*div
= expr
;
739 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
740 unsigned long value
= ctype
->bit_size
>> 3;
742 val
->ctype
= size_t_ctype
;
745 if (value
& (value
-1)) {
746 if (Wptr_subtraction_blows
)
747 warning(expr
->pos
, "potentially expensive pointer subtraction");
751 sub
->ctype
= ssize_t_ctype
;
760 return ssize_t_ctype
;
763 static struct symbol
*evaluate_sub(struct expression
*expr
)
765 struct expression
*left
= expr
->left
;
766 struct symbol
*ltype
= left
->ctype
;
768 if (is_ptr_type(ltype
))
769 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
771 return evaluate_arith(expr
, 1);
774 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
776 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
778 struct symbol
*ctype
;
783 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
784 warning(expr
->pos
, "assignment expression in conditional");
786 ctype
= evaluate_expression(expr
);
788 if (is_safe_type(ctype
))
789 warning(expr
->pos
, "testing a 'safe expression'");
795 static struct symbol
*evaluate_logical(struct expression
*expr
)
797 if (!evaluate_conditional(expr
->left
, 0))
799 if (!evaluate_conditional(expr
->right
, 0))
802 expr
->ctype
= &bool_ctype
;
806 static struct symbol
*evaluate_shift(struct expression
*expr
)
808 struct expression
*left
= expr
->left
, *right
= expr
->right
;
809 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
811 if (ltype
->type
== SYM_NODE
)
812 ltype
= ltype
->ctype
.base_type
;
813 if (rtype
->type
== SYM_NODE
)
814 rtype
= rtype
->ctype
.base_type
;
815 if (is_int_type(ltype
) && is_int_type(rtype
)) {
816 struct symbol
*ctype
= integer_promotion(ltype
);
817 if (ltype
->bit_size
!= ctype
->bit_size
)
818 expr
->left
= cast_to(expr
->left
, ctype
);
820 ctype
= integer_promotion(rtype
);
821 if (rtype
->bit_size
!= ctype
->bit_size
)
822 expr
->right
= cast_to(expr
->right
, ctype
);
825 return bad_expr_type(expr
);
828 static struct symbol
*evaluate_binop(struct expression
*expr
)
831 // addition can take ptr+int, fp and int
833 return evaluate_add(expr
);
835 // subtraction can take ptr-ptr, fp and int
837 return evaluate_sub(expr
);
839 // Arithmetic operations can take fp and int
841 return evaluate_arith(expr
, 1);
843 // shifts do integer promotions, but that's it.
844 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
845 return evaluate_shift(expr
);
847 // The rest are integer operations
848 // '%', '&', '^', '|'
850 return evaluate_arith(expr
, 0);
854 static struct symbol
*evaluate_comma(struct expression
*expr
)
856 expr
->ctype
= expr
->right
->ctype
;
860 static int modify_for_unsigned(int op
)
863 op
= SPECIAL_UNSIGNED_LT
;
865 op
= SPECIAL_UNSIGNED_GT
;
866 else if (op
== SPECIAL_LTE
)
867 op
= SPECIAL_UNSIGNED_LTE
;
868 else if (op
== SPECIAL_GTE
)
869 op
= SPECIAL_UNSIGNED_GTE
;
873 static struct symbol
*evaluate_compare(struct expression
*expr
)
875 struct expression
*left
= expr
->left
, *right
= expr
->right
;
876 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
877 struct symbol
*ctype
;
880 if (is_type_type(ltype
) && is_type_type(rtype
))
883 if (is_safe_type(ltype
) || is_safe_type(rtype
))
884 warning(expr
->pos
, "testing a 'safe expression'");
887 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
888 // FIXME! Check the types for compatibility
892 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
894 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
895 expr
->op
= modify_for_unsigned(expr
->op
);
899 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
903 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
910 expr
->ctype
= &bool_ctype
;
915 * FIXME!! This should do casts, array degeneration etc..
917 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
919 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
921 if (ltype
->type
== SYM_NODE
)
922 ltype
= ltype
->ctype
.base_type
;
924 if (rtype
->type
== SYM_NODE
)
925 rtype
= rtype
->ctype
.base_type
;
927 if (ltype
->type
== SYM_PTR
) {
928 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
932 if (rtype
->type
== SYM_PTR
) {
933 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
940 * NOTE! The degenerate case of "x ? : y", where we don't
941 * have a true case, this will possibly promote "x" to the
942 * same type as "y", and thus _change_ the conditional
943 * test in the expression. But since promotion is "safe"
944 * for testing, that's ok.
946 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
948 struct expression
**true;
949 struct symbol
*ctype
, *ltype
, *rtype
;
950 const char * typediff
;
952 if (!evaluate_conditional(expr
->conditional
, 0))
954 if (!evaluate_expression(expr
->cond_false
))
957 ctype
= degenerate(expr
->conditional
);
958 rtype
= degenerate(expr
->cond_false
);
960 true = &expr
->conditional
;
962 if (expr
->cond_true
) {
963 if (!evaluate_expression(expr
->cond_true
))
965 ltype
= degenerate(expr
->cond_true
);
966 true = &expr
->cond_true
;
969 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
972 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
975 ctype
= compatible_float_binop(true, &expr
->cond_false
);
978 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
982 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
985 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
993 /* FP assignments can not do modulo or bit operations */
994 static int compatible_float_op(int op
)
997 op
== SPECIAL_ADD_ASSIGN
||
998 op
== SPECIAL_SUB_ASSIGN
||
999 op
== SPECIAL_MUL_ASSIGN
||
1000 op
== SPECIAL_DIV_ASSIGN
;
1003 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1004 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1006 const char *typediff
;
1010 if (is_int_type(target
)) {
1011 if (is_int_type(source
)) {
1012 if (target
->bit_size
!= source
->bit_size
)
1014 if (target
->bit_offset
!= source
->bit_offset
)
1018 if (is_float_type(source
))
1020 } else if (is_float_type(target
)) {
1021 if (!compatible_float_op(op
)) {
1022 warning(expr
->pos
, "invalid assignment");
1025 if (is_int_type(source
))
1027 if (is_float_type(source
)) {
1028 if (target
->bit_size
!= source
->bit_size
)
1032 } else if (is_restricted_type(target
)) {
1033 if (restricted_binop(op
, target
)) {
1034 warning(expr
->pos
, "bad restricted assignment");
1037 if (!restricted_value(*rp
, target
))
1039 } else if (is_ptr_type(target
)) {
1040 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1041 evaluate_ptr_add(expr
, target
, rp
);
1045 warning(expr
->pos
, "invalid pointer assignment");
1048 } else if (op
!= '=') {
1049 warning(expr
->pos
, "invalid assignment");
1053 /* It's ok if the target is more volatile or const than the source */
1054 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1058 /* Pointer destination? */
1060 target_as
= t
->ctype
.as
;
1061 if (t
->type
== SYM_NODE
) {
1062 t
= t
->ctype
.base_type
;
1063 target_as
|= t
->ctype
.as
;
1065 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1066 struct expression
*right
= *rp
;
1067 struct symbol
*s
= source
;
1070 // NULL pointer is always ok
1071 if (is_null_ptr(right
))
1074 /* "void *" matches anything as long as the address space is ok */
1075 source_as
= s
->ctype
.as
;
1076 if (s
->type
== SYM_NODE
) {
1077 s
= s
->ctype
.base_type
;
1078 source_as
|= s
->ctype
.as
;
1080 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1081 s
= s
->ctype
.base_type
;
1082 t
= t
->ctype
.base_type
;
1083 if (s
== &void_ctype
|| t
== &void_ctype
)
1088 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1089 info(expr
->pos
, " expected %s", show_typename(target
));
1090 info(expr
->pos
, " got %s", show_typename(source
));
1091 *rp
= cast_to(*rp
, target
);
1094 *rp
= cast_to(*rp
, target
);
1098 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1100 if (type
->ctype
.modifiers
& MOD_CONST
)
1101 warning(left
->pos
, "assignment to const expression");
1102 if (type
->type
== SYM_NODE
)
1103 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1106 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1108 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1109 struct expression
*where
= expr
;
1110 struct symbol
*ltype
, *rtype
;
1112 if (!lvalue_expression(left
)) {
1113 warning(expr
->pos
, "not an lvalue");
1117 ltype
= left
->ctype
;
1119 rtype
= degenerate(right
);
1121 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1124 evaluate_assign_to(left
, ltype
);
1126 expr
->ctype
= ltype
;
1130 static void examine_fn_arguments(struct symbol
*fn
)
1134 FOR_EACH_PTR(fn
->arguments
, s
) {
1135 struct symbol
*arg
= evaluate_symbol(s
);
1136 /* Array/function arguments silently degenerate into pointers */
1142 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1143 if (arg
->type
== SYM_ARRAY
)
1144 ptr
->ctype
= arg
->ctype
;
1146 ptr
->ctype
.base_type
= arg
;
1147 ptr
->ctype
.as
|= s
->ctype
.as
;
1148 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1150 s
->ctype
.base_type
= ptr
;
1152 s
->ctype
.modifiers
= 0;
1155 examine_symbol_type(s
);
1162 } END_FOR_EACH_PTR(s
);
1165 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1167 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1168 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1170 newsym
->ctype
.as
= as
;
1171 newsym
->ctype
.modifiers
= mod
;
1177 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1179 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1180 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1182 node
->ctype
.base_type
= ptr
;
1183 ptr
->bit_size
= bits_in_pointer
;
1184 ptr
->ctype
.alignment
= pointer_alignment
;
1186 node
->bit_size
= bits_in_pointer
;
1187 node
->ctype
.alignment
= pointer_alignment
;
1190 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1191 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1192 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1194 if (sym
->type
== SYM_NODE
) {
1195 ptr
->ctype
.as
|= sym
->ctype
.as
;
1196 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1197 sym
= sym
->ctype
.base_type
;
1199 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1200 ptr
->ctype
.as
|= sym
->ctype
.as
;
1201 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1202 sym
= sym
->ctype
.base_type
;
1204 ptr
->ctype
.base_type
= sym
;
1209 /* Arrays degenerate into pointers on pointer arithmetic */
1210 static struct symbol
*degenerate(struct expression
*expr
)
1212 struct symbol
*ctype
, *base
;
1216 ctype
= expr
->ctype
;
1220 if (ctype
->type
== SYM_NODE
)
1221 base
= ctype
->ctype
.base_type
;
1223 * Arrays degenerate into pointers to the entries, while
1224 * functions degenerate into pointers to themselves.
1225 * If array was part of non-lvalue compound, we create a copy
1226 * of that compound first and then act as if we were dealing with
1227 * the corresponding field in there.
1229 switch (base
->type
) {
1231 if (expr
->type
== EXPR_SLICE
) {
1232 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1233 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1235 a
->ctype
.base_type
= expr
->base
->ctype
;
1236 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1237 a
->array_size
= expr
->base
->ctype
->array_size
;
1239 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1241 e0
->ctype
= &lazy_ptr_ctype
;
1243 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1246 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1248 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1250 e2
->right
= expr
->base
;
1252 e2
->ctype
= expr
->base
->ctype
;
1254 if (expr
->r_bitpos
) {
1255 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1258 e3
->right
= alloc_const_expression(expr
->pos
,
1259 expr
->r_bitpos
>> 3);
1260 e3
->ctype
= &lazy_ptr_ctype
;
1265 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1268 e4
->ctype
= &lazy_ptr_ctype
;
1271 expr
->type
= EXPR_PREOP
;
1275 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1276 warning(expr
->pos
, "strange non-value function or array");
1279 *expr
= *expr
->unop
;
1280 ctype
= create_pointer(expr
, ctype
, 1);
1281 expr
->ctype
= ctype
;
1288 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1290 struct expression
*op
= expr
->unop
;
1291 struct symbol
*ctype
;
1293 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1294 warning(expr
->pos
, "not addressable");
1300 if (expr
->type
== EXPR_SYMBOL
) {
1301 struct symbol
*sym
= expr
->symbol
;
1302 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1306 * symbol expression evaluation is lazy about the type
1307 * of the sub-expression, so we may have to generate
1308 * the type here if so..
1310 if (expr
->ctype
== &lazy_ptr_ctype
) {
1311 ctype
= create_pointer(expr
, ctype
, 0);
1312 expr
->ctype
= ctype
;
1318 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1320 struct expression
*op
= expr
->unop
;
1321 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1323 /* Simplify: *&(expr) => (expr) */
1324 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1329 /* Dereferencing a node drops all the node information. */
1330 if (ctype
->type
== SYM_NODE
)
1331 ctype
= ctype
->ctype
.base_type
;
1333 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1334 target
= ctype
->ctype
.base_type
;
1336 switch (ctype
->type
) {
1338 warning(expr
->pos
, "cannot derefence this type");
1341 merge_type(node
, ctype
);
1342 if (ctype
->type
!= SYM_ARRAY
)
1345 * Dereferencing a pointer to an array results in a
1346 * degenerate dereference: the expression becomes
1347 * just a pointer to the entry, and the derefence
1352 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1353 target
->bit_size
= bits_in_pointer
;
1354 target
->ctype
.alignment
= pointer_alignment
;
1355 merge_type(target
, ctype
->ctype
.base_type
);
1359 if (!lvalue_expression(op
)) {
1360 warning(op
->pos
, "non-lvalue array??");
1364 /* Do the implied "addressof" on the array */
1368 * When an array is dereferenced, we need to pick
1369 * up the attributes of the original node too..
1371 merge_type(node
, op
->ctype
);
1372 merge_type(node
, ctype
);
1376 node
->bit_size
= target
->bit_size
;
1377 node
->array_size
= target
->array_size
;
1384 * Unary post-ops: x++ and x--
1386 static struct symbol
*evaluate_postop(struct expression
*expr
)
1388 struct expression
*op
= expr
->unop
;
1389 struct symbol
*ctype
= op
->ctype
;
1391 if (!lvalue_expression(expr
->unop
)) {
1392 warning(expr
->pos
, "need lvalue expression for ++/--");
1395 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1396 warning(expr
->pos
, "bad operation on restricted");
1400 evaluate_assign_to(op
, ctype
);
1402 expr
->ctype
= ctype
;
1404 if (is_ptr_type(ctype
))
1405 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1410 static struct symbol
*evaluate_sign(struct expression
*expr
)
1412 struct symbol
*ctype
= expr
->unop
->ctype
;
1413 if (is_int_type(ctype
)) {
1414 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1415 if (rtype
->bit_size
!= ctype
->bit_size
)
1416 expr
->unop
= cast_to(expr
->unop
, rtype
);
1418 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1419 /* no conversions needed */
1420 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1421 /* no conversions needed */
1423 return bad_expr_type(expr
);
1425 if (expr
->op
== '+')
1426 *expr
= *expr
->unop
;
1427 expr
->ctype
= ctype
;
1431 static struct symbol
*evaluate_preop(struct expression
*expr
)
1433 struct symbol
*ctype
= expr
->unop
->ctype
;
1437 *expr
= *expr
->unop
;
1443 return evaluate_sign(expr
);
1446 return evaluate_dereference(expr
);
1449 return evaluate_addressof(expr
);
1451 case SPECIAL_INCREMENT
:
1452 case SPECIAL_DECREMENT
:
1454 * From a type evaluation standpoint the pre-ops are
1455 * the same as the postops
1457 return evaluate_postop(expr
);
1460 if (is_safe_type(ctype
))
1461 warning(expr
->pos
, "testing a 'safe expression'");
1462 if (is_float_type(ctype
)) {
1463 struct expression
*arg
= expr
->unop
;
1464 expr
->type
= EXPR_BINOP
;
1465 expr
->op
= SPECIAL_EQUAL
;
1467 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1468 expr
->right
->ctype
= ctype
;
1469 expr
->right
->fvalue
= 0;
1471 ctype
= &bool_ctype
;
1477 expr
->ctype
= ctype
;
1481 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1483 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1484 struct ptr_list
*list
= head
;
1490 for (i
= 0; i
< list
->nr
; i
++) {
1491 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1493 if (sym
->ident
!= ident
)
1495 *offset
= sym
->offset
;
1498 struct symbol
*ctype
= sym
->ctype
.base_type
;
1502 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1504 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1507 *offset
+= sym
->offset
;
1511 } while ((list
= list
->next
) != head
);
1515 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1517 struct expression
*add
;
1520 * Create a new add-expression
1522 * NOTE! Even if we just add zero, we need a new node
1523 * for the member pointer, since it has a different
1524 * type than the original pointer. We could make that
1525 * be just a cast, but the fact is, a node is a node,
1526 * so we might as well just do the "add zero" here.
1528 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1531 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1532 add
->right
->ctype
= &int_ctype
;
1533 add
->right
->value
= offset
;
1536 * The ctype of the pointer will be lazily evaluated if
1537 * we ever take the address of this member dereference..
1539 add
->ctype
= &lazy_ptr_ctype
;
1543 /* structure/union dereference */
1544 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1547 struct symbol
*ctype
, *member
;
1548 struct expression
*deref
= expr
->deref
, *add
;
1549 struct ident
*ident
= expr
->member
;
1553 if (!evaluate_expression(deref
))
1556 warning(expr
->pos
, "bad member name");
1560 ctype
= deref
->ctype
;
1561 address_space
= ctype
->ctype
.as
;
1562 mod
= ctype
->ctype
.modifiers
;
1563 if (ctype
->type
== SYM_NODE
) {
1564 ctype
= ctype
->ctype
.base_type
;
1565 address_space
|= ctype
->ctype
.as
;
1566 mod
|= ctype
->ctype
.modifiers
;
1568 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1569 warning(expr
->pos
, "expected structure or union");
1573 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1575 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1576 const char *name
= "<unnamed>";
1579 name
= ctype
->ident
->name
;
1580 namelen
= ctype
->ident
->len
;
1582 warning(expr
->pos
, "no member '%s' in %s %.*s",
1583 show_ident(ident
), type
, namelen
, name
);
1588 * The member needs to take on the address space and modifiers of
1589 * the "parent" type.
1591 member
= convert_to_as_mod(member
, address_space
, mod
);
1592 ctype
= member
->ctype
.base_type
;
1594 if (!lvalue_expression(deref
)) {
1595 if (deref
->type
!= EXPR_SLICE
) {
1599 expr
->base
= deref
->base
;
1600 expr
->r_bitpos
= deref
->r_bitpos
;
1602 expr
->r_bitpos
+= offset
<< 3;
1603 expr
->type
= EXPR_SLICE
;
1604 expr
->r_nrbits
= member
->bit_size
;
1605 expr
->r_bitpos
+= member
->bit_offset
;
1606 expr
->ctype
= member
;
1610 deref
= deref
->unop
;
1611 expr
->deref
= deref
;
1613 add
= evaluate_offset(deref
, offset
);
1614 expr
->type
= EXPR_PREOP
;
1618 expr
->ctype
= member
;
1622 static int is_promoted(struct expression
*expr
)
1625 switch (expr
->type
) {
1628 case EXPR_CONDITIONAL
:
1652 static struct symbol
*evaluate_cast(struct expression
*);
1654 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1656 struct symbol
*sym
= expr
->cast_type
;
1658 sym
= evaluate_expression(expr
->cast_expression
);
1662 * Expressions of restricted types will possibly get
1663 * promoted - check that here
1665 if (is_restricted_type(sym
)) {
1666 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1670 examine_symbol_type(sym
);
1671 if (is_bitfield_type(sym
)) {
1672 warning(expr
->pos
, "trying to examine bitfield type");
1678 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1680 struct symbol
*type
;
1683 type
= evaluate_type_information(expr
);
1687 size
= type
->bit_size
;
1689 warning(expr
->pos
, "cannot size expression");
1690 expr
->type
= EXPR_VALUE
;
1691 expr
->value
= size
>> 3;
1692 expr
->ctype
= size_t_ctype
;
1693 return size_t_ctype
;
1696 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1698 struct symbol
*type
;
1701 type
= evaluate_type_information(expr
);
1705 if (type
->type
== SYM_NODE
)
1706 type
= type
->ctype
.base_type
;
1709 switch (type
->type
) {
1713 type
= type
->ctype
.base_type
;
1717 warning(expr
->pos
, "expected pointer expression");
1720 size
= type
->bit_size
;
1723 expr
->type
= EXPR_VALUE
;
1724 expr
->value
= size
>> 3;
1725 expr
->ctype
= size_t_ctype
;
1726 return size_t_ctype
;
1729 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1731 struct symbol
*type
;
1733 type
= evaluate_type_information(expr
);
1737 expr
->type
= EXPR_VALUE
;
1738 expr
->value
= type
->ctype
.alignment
;
1739 expr
->ctype
= size_t_ctype
;
1740 return size_t_ctype
;
1743 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1745 struct expression
*expr
;
1746 struct symbol_list
*argument_types
= fn
->arguments
;
1747 struct symbol
*argtype
;
1750 PREPARE_PTR_LIST(argument_types
, argtype
);
1751 FOR_EACH_PTR (head
, expr
) {
1752 struct expression
**p
= THIS_ADDRESS(expr
);
1753 struct symbol
*ctype
, *target
;
1754 ctype
= evaluate_expression(expr
);
1759 ctype
= degenerate(expr
);
1762 if (!target
&& ctype
->bit_size
< bits_in_int
)
1763 target
= &int_ctype
;
1765 static char where
[30];
1766 examine_symbol_type(target
);
1767 sprintf(where
, "argument %d", i
);
1768 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1772 NEXT_PTR_LIST(argtype
);
1773 } END_FOR_EACH_PTR(expr
);
1774 FINISH_PTR_LIST(argtype
);
1778 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1780 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1782 struct expression
*entry
= *ep
;
1783 struct expression
**parent
, *reuse
= NULL
;
1784 unsigned long offset
;
1786 unsigned long from
, to
;
1787 int accept_string
= is_byte_type(ctype
);
1792 if (entry
->type
== EXPR_INDEX
) {
1793 from
= entry
->idx_from
;
1794 to
= entry
->idx_to
+1;
1795 parent
= &entry
->idx_expression
;
1797 entry
= entry
->idx_expression
;
1800 offset
= from
* (ctype
->bit_size
>>3);
1802 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1803 reuse
->type
= EXPR_POS
;
1804 reuse
->ctype
= ctype
;
1805 reuse
->init_offset
= offset
;
1806 reuse
->init_nr
= to
- from
;
1807 reuse
->init_expr
= entry
;
1808 parent
= &reuse
->init_expr
;
1813 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1814 sym
= evaluate_expression(entry
);
1815 to
= from
+ get_expression_value(sym
->array_size
);
1817 evaluate_initializer(ctype
, parent
);
1822 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1824 struct expression
*entry
;
1827 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1828 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1829 } END_FOR_EACH_PTR(entry
);
1832 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1833 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1835 if (expression_list_size(expr
->expr_list
) != 1) {
1836 warning(expr
->pos
, "unexpected compound initializer");
1839 evaluate_array_initializer(ctype
, expr
);
1843 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1847 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1848 if (sym
->ident
== ident
)
1850 } END_FOR_EACH_PTR(sym
);
1854 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1856 struct expression
*entry
= *ep
;
1857 struct expression
**parent
;
1858 struct expression
*reuse
= NULL
;
1859 unsigned long offset
;
1862 error(entry
->pos
, "unknown named initializer");
1866 if (entry
->type
== EXPR_IDENTIFIER
) {
1868 entry
= entry
->ident_expression
;
1872 offset
= sym
->offset
;
1875 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1876 reuse
->type
= EXPR_POS
;
1878 reuse
->init_offset
= offset
;
1880 reuse
->init_expr
= entry
;
1881 parent
= &reuse
->init_expr
;
1885 evaluate_initializer(sym
, parent
);
1889 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1891 struct expression
*entry
;
1894 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1895 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1896 if (entry
->type
== EXPR_IDENTIFIER
) {
1897 struct ident
*ident
= entry
->expr_ident
;
1898 /* We special-case the "already right place" case */
1899 if (!sym
|| sym
->ident
!= ident
) {
1900 RESET_PTR_LIST(sym
);
1904 if (sym
->ident
== ident
)
1910 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1913 } END_FOR_EACH_PTR(entry
);
1914 FINISH_PTR_LIST(sym
);
1918 * Initializers are kind of like assignments. Except
1919 * they can be a hell of a lot more complex.
1921 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1923 struct expression
*expr
= *ep
;
1926 * Simple non-structure/array initializers are the simple
1927 * case, and look (and parse) largely like assignments.
1929 switch (expr
->type
) {
1931 int is_string
= expr
->type
== EXPR_STRING
;
1932 struct symbol
*rtype
= evaluate_expression(expr
);
1936 * char array[] = "string"
1937 * should _not_ degenerate.
1939 if (!is_string
|| !is_string_type(ctype
))
1940 rtype
= degenerate(expr
);
1941 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1946 case EXPR_INITIALIZER
:
1947 expr
->ctype
= ctype
;
1948 if (ctype
->type
== SYM_NODE
)
1949 ctype
= ctype
->ctype
.base_type
;
1951 switch (ctype
->type
) {
1954 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1957 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1960 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1963 evaluate_scalar_initializer(ctype
, expr
);
1967 case EXPR_IDENTIFIER
:
1968 if (ctype
->type
== SYM_NODE
)
1969 ctype
= ctype
->ctype
.base_type
;
1970 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1971 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1975 evaluate_one_struct_initializer(ctype
, ep
,
1976 find_struct_ident(ctype
, expr
->expr_ident
));
1980 if (ctype
->type
== SYM_NODE
)
1981 ctype
= ctype
->ctype
.base_type
;
1982 if (ctype
->type
!= SYM_ARRAY
) {
1983 error(expr
->pos
, "expected array");
1986 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1991 * An EXPR_POS expression has already been evaluated, and we don't
1992 * need to do anything more
1998 static int get_as(struct symbol
*sym
)
2006 mod
= sym
->ctype
.modifiers
;
2007 if (sym
->type
== SYM_NODE
) {
2008 sym
= sym
->ctype
.base_type
;
2009 as
|= sym
->ctype
.as
;
2010 mod
|= sym
->ctype
.modifiers
;
2014 * At least for now, allow casting to a "unsigned long".
2015 * That's how we do things like pointer arithmetic and
2016 * store pointers to registers.
2018 if (sym
== &ulong_ctype
)
2021 if (sym
&& sym
->type
== SYM_PTR
) {
2022 sym
= sym
->ctype
.base_type
;
2023 as
|= sym
->ctype
.as
;
2024 mod
|= sym
->ctype
.modifiers
;
2026 if (mod
& MOD_FORCE
)
2031 static struct symbol
*evaluate_cast(struct expression
*expr
)
2033 struct expression
*target
= expr
->cast_expression
;
2034 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2040 expr
->ctype
= ctype
;
2041 expr
->cast_type
= ctype
;
2044 * Special case: a cast can be followed by an
2045 * initializer, in which case we need to pass
2046 * the type value down to that initializer rather
2047 * than trying to evaluate it as an expression
2049 * A more complex case is when the initializer is
2050 * dereferenced as part of a post-fix expression.
2051 * We need to produce an expression that can be dereferenced.
2053 if (target
->type
== EXPR_INITIALIZER
) {
2054 struct symbol
*sym
= expr
->cast_type
;
2055 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2057 sym
->initializer
= expr
->cast_expression
;
2058 evaluate_symbol(sym
);
2060 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2063 expr
->type
= EXPR_PREOP
;
2071 evaluate_expression(target
);
2075 * You can always throw a value away by casting to
2076 * "void" - that's an implicit "force". Note that
2077 * the same is _not_ true of "void *".
2079 if (ctype
== &void_ctype
)
2083 if (type
== SYM_NODE
) {
2084 type
= ctype
->ctype
.base_type
->type
;
2085 if (ctype
->ctype
.base_type
== &void_ctype
)
2088 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2089 warning(expr
->pos
, "cast to non-scalar");
2091 if (!target
->ctype
) {
2092 warning(expr
->pos
, "cast from unknown type");
2096 type
= target
->ctype
->type
;
2097 if (type
== SYM_NODE
)
2098 type
= target
->ctype
->ctype
.base_type
->type
;
2099 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2100 warning(expr
->pos
, "cast from non-scalar");
2102 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2103 warning(expr
->pos
, "cast removes address space of expression");
2105 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2106 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2107 if (t1
->type
== SYM_NODE
)
2108 t1
= t1
->ctype
.base_type
;
2109 if (t2
->type
== SYM_NODE
)
2110 t2
= t2
->ctype
.base_type
;
2112 if (t1
->type
== SYM_RESTRICT
)
2113 warning(expr
->pos
, "cast to restricted type");
2114 if (t2
->type
== SYM_RESTRICT
)
2115 warning(expr
->pos
, "cast from restricted type");
2120 * Casts of constant values are special: they
2121 * can be NULL, and thus need to be simplified
2124 if (target
->type
== EXPR_VALUE
)
2125 cast_value(expr
, ctype
, target
, target
->ctype
);
2132 * Evaluate a call expression with a symbol. This
2133 * should expand inline functions, and evaluate
2136 static int evaluate_symbol_call(struct expression
*expr
)
2138 struct expression
*fn
= expr
->fn
;
2139 struct symbol
*ctype
= fn
->ctype
;
2141 if (fn
->type
!= EXPR_PREOP
)
2144 if (ctype
->op
&& ctype
->op
->evaluate
)
2145 return ctype
->op
->evaluate(expr
);
2147 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2149 struct symbol
*curr
= current_fn
;
2150 current_fn
= ctype
->ctype
.base_type
;
2151 examine_fn_arguments(current_fn
);
2153 ret
= inline_function(expr
, ctype
);
2155 /* restore the old function */
2163 static struct symbol
*evaluate_call(struct expression
*expr
)
2166 struct symbol
*ctype
, *sym
;
2167 struct expression
*fn
= expr
->fn
;
2168 struct expression_list
*arglist
= expr
->args
;
2170 if (!evaluate_expression(fn
))
2172 sym
= ctype
= fn
->ctype
;
2173 if (ctype
->type
== SYM_NODE
)
2174 ctype
= ctype
->ctype
.base_type
;
2175 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2176 ctype
= ctype
->ctype
.base_type
;
2177 if (!evaluate_arguments(sym
, ctype
, arglist
))
2179 if (ctype
->type
!= SYM_FN
) {
2180 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2183 args
= expression_list_size(expr
->args
);
2184 fnargs
= symbol_list_size(ctype
->arguments
);
2186 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2187 if (args
> fnargs
&& !ctype
->variadic
)
2188 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2189 if (sym
->type
== SYM_NODE
) {
2190 if (evaluate_symbol_call(expr
))
2193 expr
->ctype
= ctype
->ctype
.base_type
;
2197 struct symbol
*evaluate_expression(struct expression
*expr
)
2204 switch (expr
->type
) {
2207 warning(expr
->pos
, "value expression without a type");
2210 return evaluate_string(expr
);
2212 return evaluate_symbol_expression(expr
);
2214 if (!evaluate_expression(expr
->left
))
2216 if (!evaluate_expression(expr
->right
))
2218 return evaluate_binop(expr
);
2220 return evaluate_logical(expr
);
2222 evaluate_expression(expr
->left
);
2223 if (!evaluate_expression(expr
->right
))
2225 return evaluate_comma(expr
);
2227 if (!evaluate_expression(expr
->left
))
2229 if (!evaluate_expression(expr
->right
))
2231 return evaluate_compare(expr
);
2232 case EXPR_ASSIGNMENT
:
2233 if (!evaluate_expression(expr
->left
))
2235 if (!evaluate_expression(expr
->right
))
2237 return evaluate_assignment(expr
);
2239 if (!evaluate_expression(expr
->unop
))
2241 return evaluate_preop(expr
);
2243 if (!evaluate_expression(expr
->unop
))
2245 return evaluate_postop(expr
);
2247 case EXPR_IMPLIED_CAST
:
2248 return evaluate_cast(expr
);
2250 return evaluate_sizeof(expr
);
2251 case EXPR_PTRSIZEOF
:
2252 return evaluate_ptrsizeof(expr
);
2254 return evaluate_alignof(expr
);
2256 return evaluate_member_dereference(expr
);
2258 return evaluate_call(expr
);
2260 case EXPR_CONDITIONAL
:
2261 return evaluate_conditional_expression(expr
);
2262 case EXPR_STATEMENT
:
2263 expr
->ctype
= evaluate_statement(expr
->statement
);
2267 expr
->ctype
= &ptr_ctype
;
2271 /* Evaluate the type of the symbol .. */
2272 evaluate_symbol(expr
->symbol
);
2273 /* .. but the type of the _expression_ is a "type" */
2274 expr
->ctype
= &type_ctype
;
2277 /* These can not exist as stand-alone expressions */
2278 case EXPR_INITIALIZER
:
2279 case EXPR_IDENTIFIER
:
2282 warning(expr
->pos
, "internal front-end error: initializer in expression");
2285 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2291 static void check_duplicates(struct symbol
*sym
)
2293 struct symbol
*next
= sym
;
2295 while ((next
= next
->same_symbol
) != NULL
) {
2296 const char *typediff
;
2297 evaluate_symbol(next
);
2298 typediff
= type_difference(sym
, next
, 0, 0);
2300 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2301 show_ident(sym
->ident
),
2302 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2308 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2310 struct symbol
*base_type
;
2315 sym
= examine_symbol_type(sym
);
2316 base_type
= sym
->ctype
.base_type
;
2320 /* Evaluate the initializers */
2321 if (sym
->initializer
)
2322 evaluate_initializer(sym
, &sym
->initializer
);
2324 /* And finally, evaluate the body of the symbol too */
2325 if (base_type
->type
== SYM_FN
) {
2326 struct symbol
*curr
= current_fn
;
2328 current_fn
= base_type
;
2330 examine_fn_arguments(base_type
);
2331 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2333 if (base_type
->stmt
)
2334 evaluate_statement(base_type
->stmt
);
2342 void evaluate_symbol_list(struct symbol_list
*list
)
2346 FOR_EACH_PTR(list
, sym
) {
2347 check_duplicates(sym
);
2348 evaluate_symbol(sym
);
2349 } END_FOR_EACH_PTR(sym
);
2352 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2354 struct expression
*expr
= stmt
->expression
;
2355 struct symbol
*ctype
, *fntype
;
2357 evaluate_expression(expr
);
2358 ctype
= degenerate(expr
);
2359 fntype
= current_fn
->ctype
.base_type
;
2360 if (!fntype
|| fntype
== &void_ctype
) {
2361 if (expr
&& ctype
!= &void_ctype
)
2362 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2367 warning(stmt
->pos
, "return with no return value");
2372 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2376 static void evaluate_if_statement(struct statement
*stmt
)
2378 if (!stmt
->if_conditional
)
2381 evaluate_conditional(stmt
->if_conditional
, 0);
2382 evaluate_statement(stmt
->if_true
);
2383 evaluate_statement(stmt
->if_false
);
2386 static void evaluate_iterator(struct statement
*stmt
)
2388 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2389 evaluate_conditional(stmt
->iterator_post_condition
,1);
2390 evaluate_statement(stmt
->iterator_pre_statement
);
2391 evaluate_statement(stmt
->iterator_statement
);
2392 evaluate_statement(stmt
->iterator_post_statement
);
2395 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2397 switch (*constraint
) {
2398 case '=': /* Assignment */
2399 case '+': /* Update */
2402 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2406 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2408 switch (*constraint
) {
2409 case '=': /* Assignment */
2410 case '+': /* Update */
2411 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2415 static void evaluate_asm_statement(struct statement
*stmt
)
2417 struct expression
*expr
;
2420 expr
= stmt
->asm_string
;
2421 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2422 warning(stmt
->pos
, "need constant string for inline asm");
2427 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2428 struct ident
*ident
;
2431 case 0: /* Identifier */
2433 ident
= (struct ident
*)expr
;
2436 case 1: /* Constraint */
2438 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2439 warning(expr
->pos
, "asm output constraint is not a string");
2440 *THIS_ADDRESS(expr
) = NULL
;
2443 verify_output_constraint(expr
, expr
->string
->data
);
2446 case 2: /* Expression */
2448 if (!evaluate_expression(expr
))
2450 if (!lvalue_expression(expr
))
2451 warning(expr
->pos
, "asm output is not an lvalue");
2452 evaluate_assign_to(expr
, expr
->ctype
);
2455 } END_FOR_EACH_PTR(expr
);
2458 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2459 struct ident
*ident
;
2462 case 0: /* Identifier */
2464 ident
= (struct ident
*)expr
;
2467 case 1: /* Constraint */
2469 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2470 warning(expr
->pos
, "asm input constraint is not a string");
2471 *THIS_ADDRESS(expr
) = NULL
;
2474 verify_input_constraint(expr
, expr
->string
->data
);
2477 case 2: /* Expression */
2479 if (!evaluate_expression(expr
))
2483 } END_FOR_EACH_PTR(expr
);
2485 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2487 warning(stmt
->pos
, "bad asm output");
2490 if (expr
->type
== EXPR_STRING
)
2492 warning(expr
->pos
, "asm clobber is not a string");
2493 } END_FOR_EACH_PTR(expr
);
2496 struct symbol
*evaluate_statement(struct statement
*stmt
)
2501 switch (stmt
->type
) {
2503 return evaluate_return_expression(stmt
);
2505 case STMT_EXPRESSION
:
2506 if (!evaluate_expression(stmt
->expression
))
2508 return degenerate(stmt
->expression
);
2510 case STMT_COMPOUND
: {
2511 struct statement
*s
;
2512 struct symbol
*type
= NULL
;
2515 /* Evaluate each symbol in the compound statement */
2516 FOR_EACH_PTR(stmt
->syms
, sym
) {
2517 evaluate_symbol(sym
);
2518 } END_FOR_EACH_PTR(sym
);
2519 evaluate_symbol(stmt
->ret
);
2522 * Then, evaluate each statement, making the type of the
2523 * compound statement be the type of the last statement
2526 FOR_EACH_PTR(stmt
->stmts
, s
) {
2527 type
= evaluate_statement(s
);
2528 } END_FOR_EACH_PTR(s
);
2534 evaluate_if_statement(stmt
);
2537 evaluate_iterator(stmt
);
2540 evaluate_expression(stmt
->switch_expression
);
2541 evaluate_statement(stmt
->switch_statement
);
2544 evaluate_expression(stmt
->case_expression
);
2545 evaluate_expression(stmt
->case_to
);
2546 evaluate_statement(stmt
->case_statement
);
2549 return evaluate_statement(stmt
->label_statement
);
2551 evaluate_expression(stmt
->goto_expression
);
2556 evaluate_asm_statement(stmt
);
2559 evaluate_expression(stmt
->expression
);