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 (i
->type
== EXPR_VALUE
) {
468 i
->value
*= bit_size
>> 3;
469 } else if (bit_size
> bits_in_char
) {
470 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
471 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
473 val
->ctype
= size_t_ctype
;
474 val
->value
= bit_size
>> 3;
477 mul
->ctype
= size_t_ctype
;
488 static struct symbol
*evaluate_add(struct expression
*expr
)
490 struct expression
*left
= expr
->left
, *right
= expr
->right
;
491 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
493 if (is_ptr_type(ltype
))
494 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
496 if (is_ptr_type(rtype
))
497 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
499 return evaluate_arith(expr
, 1);
502 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
503 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
504 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
506 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
507 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
510 unsigned long mod1
, mod2
, diff
;
511 unsigned long as1
, as2
;
513 struct symbol
*base1
, *base2
;
515 if (target
== source
)
517 if (!target
|| !source
)
518 return "different types";
520 * Peel of per-node information.
521 * FIXME! Check alignment and context too here!
523 mod1
= target
->ctype
.modifiers
;
524 as1
= target
->ctype
.as
;
525 mod2
= source
->ctype
.modifiers
;
526 as2
= source
->ctype
.as
;
527 if (target
->type
== SYM_NODE
) {
528 target
= target
->ctype
.base_type
;
531 if (target
->type
== SYM_PTR
) {
535 mod1
|= target
->ctype
.modifiers
;
536 as1
|= target
->ctype
.as
;
538 if (source
->type
== SYM_NODE
) {
539 source
= source
->ctype
.base_type
;
542 if (source
->type
== SYM_PTR
) {
546 mod2
|= source
->ctype
.modifiers
;
547 as2
|= source
->ctype
.as
;
549 if (target
->type
== SYM_ENUM
) {
550 target
= target
->ctype
.base_type
;
554 if (source
->type
== SYM_ENUM
) {
555 source
= source
->ctype
.base_type
;
560 if (target
== source
)
562 if (!target
|| !source
)
563 return "different types";
565 type1
= target
->type
;
566 base1
= target
->ctype
.base_type
;
568 type2
= source
->type
;
569 base2
= source
->ctype
.base_type
;
572 * Pointers to functions compare as the function itself
574 if (type1
== SYM_PTR
&& base1
) {
575 switch (base1
->type
) {
579 base1
= base1
->ctype
.base_type
;
584 if (type2
== SYM_PTR
&& base2
) {
585 switch (base2
->type
) {
589 base2
= base2
->ctype
.base_type
;
595 /* Arrays degenerate to pointers for type comparisons */
596 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
597 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
599 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
600 return "different base types";
602 /* Must be same address space to be comparable */
604 return "different address spaces";
606 /* Ignore differences in storage types or addressability */
607 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
608 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
611 return "different type sizes";
612 if (diff
& ~MOD_SIGNEDNESS
)
613 return "different modifiers";
615 /* Differs in signedness only.. */
618 * Warn if both are explicitly signed ("unsigned" is obvously
619 * always explicit, and since we know one of them has to be
620 * unsigned, we check if the signed one was explicit).
622 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
623 return "different explicit signedness";
626 * "char" matches both "unsigned char" and "signed char",
627 * so if the explicit test didn't trigger, then we should
628 * not warn about a char.
630 if (!(mod1
& MOD_CHAR
))
631 return "different signedness";
635 if (type1
== SYM_FN
) {
637 struct symbol
*arg1
, *arg2
;
638 if (base1
->variadic
!= base2
->variadic
)
639 return "incompatible variadic arguments";
640 PREPARE_PTR_LIST(target
->arguments
, arg1
);
641 PREPARE_PTR_LIST(source
->arguments
, arg2
);
645 diff
= type_difference(arg1
, arg2
, 0, 0);
647 static char argdiff
[80];
648 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
657 FINISH_PTR_LIST(arg2
);
658 FINISH_PTR_LIST(arg1
);
667 static int is_null_ptr(struct expression
*expr
)
669 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
671 if (!is_ptr_type(expr
->ctype
))
672 warning(expr
->pos
, "Using plain integer as NULL pointer");
676 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
678 /* NULL expression? Just return the type of the "other side" */
687 * Ignore differences in "volatile" and "const"ness when
688 * subtracting pointers
690 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
692 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
694 const char *typediff
;
695 struct symbol
*ctype
;
696 struct symbol
*ltype
, *rtype
;
697 struct expression
*r
= *rp
;
699 ltype
= degenerate(l
);
700 rtype
= degenerate(r
);
703 * If it is an integer subtract: the ptr add case will do the
706 if (!is_ptr_type(rtype
))
707 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
710 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
712 ctype
= common_ptr_type(l
, r
);
714 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
718 examine_symbol_type(ctype
);
720 /* Figure out the base type we point to */
721 if (ctype
->type
== SYM_NODE
)
722 ctype
= ctype
->ctype
.base_type
;
723 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
724 warning(expr
->pos
, "subtraction of functions? Share your drugs");
727 ctype
= ctype
->ctype
.base_type
;
729 expr
->ctype
= ssize_t_ctype
;
730 if (ctype
->bit_size
> bits_in_char
) {
731 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
732 struct expression
*div
= expr
;
733 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
735 val
->ctype
= size_t_ctype
;
736 val
->value
= ctype
->bit_size
>> 3;
739 sub
->ctype
= ssize_t_ctype
;
748 return ssize_t_ctype
;
751 static struct symbol
*evaluate_sub(struct expression
*expr
)
753 struct expression
*left
= expr
->left
;
754 struct symbol
*ltype
= left
->ctype
;
756 if (is_ptr_type(ltype
))
757 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
759 return evaluate_arith(expr
, 1);
762 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
764 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
766 struct symbol
*ctype
;
771 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
772 warning(expr
->pos
, "assignment expression in conditional");
774 ctype
= evaluate_expression(expr
);
776 if (is_safe_type(ctype
))
777 warning(expr
->pos
, "testing a 'safe expression'");
783 static struct symbol
*evaluate_logical(struct expression
*expr
)
785 if (!evaluate_conditional(expr
->left
, 0))
787 if (!evaluate_conditional(expr
->right
, 0))
790 expr
->ctype
= &bool_ctype
;
794 static struct symbol
*evaluate_shift(struct expression
*expr
)
796 struct expression
*left
= expr
->left
, *right
= expr
->right
;
797 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
799 if (ltype
->type
== SYM_NODE
)
800 ltype
= ltype
->ctype
.base_type
;
801 if (rtype
->type
== SYM_NODE
)
802 rtype
= rtype
->ctype
.base_type
;
803 if (is_int_type(ltype
) && is_int_type(rtype
)) {
804 struct symbol
*ctype
= integer_promotion(ltype
);
805 if (ltype
->bit_size
!= ctype
->bit_size
)
806 expr
->left
= cast_to(expr
->left
, ctype
);
808 ctype
= integer_promotion(rtype
);
809 if (rtype
->bit_size
!= ctype
->bit_size
)
810 expr
->right
= cast_to(expr
->right
, ctype
);
813 return bad_expr_type(expr
);
816 static struct symbol
*evaluate_binop(struct expression
*expr
)
819 // addition can take ptr+int, fp and int
821 return evaluate_add(expr
);
823 // subtraction can take ptr-ptr, fp and int
825 return evaluate_sub(expr
);
827 // Arithmetic operations can take fp and int
829 return evaluate_arith(expr
, 1);
831 // shifts do integer promotions, but that's it.
832 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
833 return evaluate_shift(expr
);
835 // The rest are integer operations
836 // '%', '&', '^', '|'
838 return evaluate_arith(expr
, 0);
842 static struct symbol
*evaluate_comma(struct expression
*expr
)
844 expr
->ctype
= expr
->right
->ctype
;
848 static int modify_for_unsigned(int op
)
851 op
= SPECIAL_UNSIGNED_LT
;
853 op
= SPECIAL_UNSIGNED_GT
;
854 else if (op
== SPECIAL_LTE
)
855 op
= SPECIAL_UNSIGNED_LTE
;
856 else if (op
== SPECIAL_GTE
)
857 op
= SPECIAL_UNSIGNED_GTE
;
861 static struct symbol
*evaluate_compare(struct expression
*expr
)
863 struct expression
*left
= expr
->left
, *right
= expr
->right
;
864 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
865 struct symbol
*ctype
;
868 if (is_type_type(ltype
) && is_type_type(rtype
))
871 if (is_safe_type(ltype
) || is_safe_type(rtype
))
872 warning(expr
->pos
, "testing a 'safe expression'");
875 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
876 // FIXME! Check the types for compatibility
880 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
882 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
883 expr
->op
= modify_for_unsigned(expr
->op
);
887 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
891 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
898 expr
->ctype
= &bool_ctype
;
903 * FIXME!! This should do casts, array degeneration etc..
905 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
907 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
909 if (ltype
->type
== SYM_NODE
)
910 ltype
= ltype
->ctype
.base_type
;
912 if (rtype
->type
== SYM_NODE
)
913 rtype
= rtype
->ctype
.base_type
;
915 if (ltype
->type
== SYM_PTR
) {
916 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
920 if (rtype
->type
== SYM_PTR
) {
921 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
928 * NOTE! The degenerate case of "x ? : y", where we don't
929 * have a true case, this will possibly promote "x" to the
930 * same type as "y", and thus _change_ the conditional
931 * test in the expression. But since promotion is "safe"
932 * for testing, that's ok.
934 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
936 struct expression
**true;
937 struct symbol
*ctype
, *ltype
, *rtype
;
938 const char * typediff
;
940 if (!evaluate_conditional(expr
->conditional
, 0))
942 if (!evaluate_expression(expr
->cond_false
))
945 ctype
= degenerate(expr
->conditional
);
946 rtype
= degenerate(expr
->cond_false
);
948 true = &expr
->conditional
;
950 if (expr
->cond_true
) {
951 if (!evaluate_expression(expr
->cond_true
))
953 ltype
= degenerate(expr
->cond_true
);
954 true = &expr
->cond_true
;
957 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
960 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
963 ctype
= compatible_float_binop(true, &expr
->cond_false
);
966 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
970 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
973 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
981 /* FP assignments can not do modulo or bit operations */
982 static int compatible_float_op(int op
)
985 op
== SPECIAL_ADD_ASSIGN
||
986 op
== SPECIAL_SUB_ASSIGN
||
987 op
== SPECIAL_MUL_ASSIGN
||
988 op
== SPECIAL_DIV_ASSIGN
;
991 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
992 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
994 const char *typediff
;
998 if (is_int_type(target
)) {
999 if (is_int_type(source
)) {
1000 if (target
->bit_size
!= source
->bit_size
)
1002 if (target
->bit_offset
!= source
->bit_offset
)
1006 if (is_float_type(source
))
1008 } else if (is_float_type(target
)) {
1009 if (!compatible_float_op(op
)) {
1010 warning(expr
->pos
, "invalid assignment");
1013 if (is_int_type(source
))
1015 if (is_float_type(source
)) {
1016 if (target
->bit_size
!= source
->bit_size
)
1020 } else if (is_restricted_type(target
)) {
1021 if (restricted_binop(op
, target
)) {
1022 warning(expr
->pos
, "bad restricted assignment");
1025 if (!restricted_value(*rp
, target
))
1027 } else if (is_ptr_type(target
)) {
1028 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1029 evaluate_ptr_add(expr
, target
, rp
);
1033 warning(expr
->pos
, "invalid pointer assignment");
1036 } else if (op
!= '=') {
1037 warning(expr
->pos
, "invalid assignment");
1041 /* It's ok if the target is more volatile or const than the source */
1042 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1046 /* Pointer destination? */
1048 target_as
= t
->ctype
.as
;
1049 if (t
->type
== SYM_NODE
) {
1050 t
= t
->ctype
.base_type
;
1051 target_as
|= t
->ctype
.as
;
1053 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1054 struct expression
*right
= *rp
;
1055 struct symbol
*s
= source
;
1058 // NULL pointer is always ok
1059 if (is_null_ptr(right
))
1062 /* "void *" matches anything as long as the address space is ok */
1063 source_as
= s
->ctype
.as
;
1064 if (s
->type
== SYM_NODE
) {
1065 s
= s
->ctype
.base_type
;
1066 source_as
|= s
->ctype
.as
;
1068 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1069 s
= s
->ctype
.base_type
;
1070 t
= t
->ctype
.base_type
;
1071 if (s
== &void_ctype
|| t
== &void_ctype
)
1076 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1077 info(expr
->pos
, " expected %s", show_typename(target
));
1078 info(expr
->pos
, " got %s", show_typename(source
));
1079 *rp
= cast_to(*rp
, target
);
1082 *rp
= cast_to(*rp
, target
);
1086 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1088 if (type
->ctype
.modifiers
& MOD_CONST
)
1089 warning(left
->pos
, "assignment to const expression");
1090 if (type
->type
== SYM_NODE
)
1091 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1094 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1096 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1097 struct expression
*where
= expr
;
1098 struct symbol
*ltype
, *rtype
;
1100 if (!lvalue_expression(left
)) {
1101 warning(expr
->pos
, "not an lvalue");
1105 ltype
= left
->ctype
;
1107 rtype
= degenerate(right
);
1109 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1112 evaluate_assign_to(left
, ltype
);
1114 expr
->ctype
= ltype
;
1118 static void examine_fn_arguments(struct symbol
*fn
)
1122 FOR_EACH_PTR(fn
->arguments
, s
) {
1123 struct symbol
*arg
= evaluate_symbol(s
);
1124 /* Array/function arguments silently degenerate into pointers */
1130 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1131 if (arg
->type
== SYM_ARRAY
)
1132 ptr
->ctype
= arg
->ctype
;
1134 ptr
->ctype
.base_type
= arg
;
1135 ptr
->ctype
.as
|= s
->ctype
.as
;
1136 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1138 s
->ctype
.base_type
= ptr
;
1140 s
->ctype
.modifiers
= 0;
1143 examine_symbol_type(s
);
1150 } END_FOR_EACH_PTR(s
);
1153 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1155 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1156 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1158 newsym
->ctype
.as
= as
;
1159 newsym
->ctype
.modifiers
= mod
;
1165 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1167 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1168 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1170 node
->ctype
.base_type
= ptr
;
1171 ptr
->bit_size
= bits_in_pointer
;
1172 ptr
->ctype
.alignment
= pointer_alignment
;
1174 node
->bit_size
= bits_in_pointer
;
1175 node
->ctype
.alignment
= pointer_alignment
;
1178 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1179 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1180 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1182 if (sym
->type
== SYM_NODE
) {
1183 ptr
->ctype
.as
|= sym
->ctype
.as
;
1184 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1185 sym
= sym
->ctype
.base_type
;
1187 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1188 ptr
->ctype
.as
|= sym
->ctype
.as
;
1189 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1190 sym
= sym
->ctype
.base_type
;
1192 ptr
->ctype
.base_type
= sym
;
1197 /* Arrays degenerate into pointers on pointer arithmetic */
1198 static struct symbol
*degenerate(struct expression
*expr
)
1200 struct symbol
*ctype
, *base
;
1204 ctype
= expr
->ctype
;
1208 if (ctype
->type
== SYM_NODE
)
1209 base
= ctype
->ctype
.base_type
;
1211 * Arrays degenerate into pointers to the entries, while
1212 * functions degenerate into pointers to themselves.
1213 * If array was part of non-lvalue compound, we create a copy
1214 * of that compound first and then act as if we were dealing with
1215 * the corresponding field in there.
1217 switch (base
->type
) {
1219 if (expr
->type
== EXPR_SLICE
) {
1220 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1221 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1223 a
->ctype
.base_type
= expr
->base
->ctype
;
1224 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1225 a
->array_size
= expr
->base
->ctype
->array_size
;
1227 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1229 e0
->ctype
= &lazy_ptr_ctype
;
1231 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1234 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1236 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1238 e2
->right
= expr
->base
;
1240 e2
->ctype
= expr
->base
->ctype
;
1242 if (expr
->r_bitpos
) {
1243 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1246 e3
->right
= alloc_const_expression(expr
->pos
,
1247 expr
->r_bitpos
>> 3);
1248 e3
->ctype
= &lazy_ptr_ctype
;
1253 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1256 e4
->ctype
= &lazy_ptr_ctype
;
1259 expr
->type
= EXPR_PREOP
;
1263 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1264 warning(expr
->pos
, "strange non-value function or array");
1267 *expr
= *expr
->unop
;
1268 ctype
= create_pointer(expr
, ctype
, 1);
1269 expr
->ctype
= ctype
;
1276 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1278 struct expression
*op
= expr
->unop
;
1279 struct symbol
*ctype
;
1281 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1282 warning(expr
->pos
, "not addressable");
1289 * symbol expression evaluation is lazy about the type
1290 * of the sub-expression, so we may have to generate
1291 * the type here if so..
1293 if (expr
->ctype
== &lazy_ptr_ctype
) {
1294 ctype
= create_pointer(expr
, ctype
, 0);
1295 expr
->ctype
= ctype
;
1301 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1303 struct expression
*op
= expr
->unop
;
1304 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1306 /* Simplify: *&(expr) => (expr) */
1307 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1312 /* Dereferencing a node drops all the node information. */
1313 if (ctype
->type
== SYM_NODE
)
1314 ctype
= ctype
->ctype
.base_type
;
1316 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1317 target
= ctype
->ctype
.base_type
;
1319 switch (ctype
->type
) {
1321 warning(expr
->pos
, "cannot derefence this type");
1324 merge_type(node
, ctype
);
1325 if (ctype
->type
!= SYM_ARRAY
)
1328 * Dereferencing a pointer to an array results in a
1329 * degenerate dereference: the expression becomes
1330 * just a pointer to the entry, and the derefence
1335 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1336 target
->bit_size
= bits_in_pointer
;
1337 target
->ctype
.alignment
= pointer_alignment
;
1338 merge_type(target
, ctype
->ctype
.base_type
);
1342 if (!lvalue_expression(op
)) {
1343 warning(op
->pos
, "non-lvalue array??");
1347 /* Do the implied "addressof" on the array */
1351 * When an array is dereferenced, we need to pick
1352 * up the attributes of the original node too..
1354 merge_type(node
, op
->ctype
);
1355 merge_type(node
, ctype
);
1359 node
->bit_size
= target
->bit_size
;
1360 node
->array_size
= target
->array_size
;
1367 * Unary post-ops: x++ and x--
1369 static struct symbol
*evaluate_postop(struct expression
*expr
)
1371 struct expression
*op
= expr
->unop
;
1372 struct symbol
*ctype
= op
->ctype
;
1374 if (!lvalue_expression(expr
->unop
)) {
1375 warning(expr
->pos
, "need lvalue expression for ++/--");
1378 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1379 warning(expr
->pos
, "bad operation on restricted");
1383 evaluate_assign_to(op
, ctype
);
1385 expr
->ctype
= ctype
;
1387 if (is_ptr_type(ctype
))
1388 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1393 static struct symbol
*evaluate_sign(struct expression
*expr
)
1395 struct symbol
*ctype
= expr
->unop
->ctype
;
1396 if (is_int_type(ctype
)) {
1397 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1398 if (rtype
->bit_size
!= ctype
->bit_size
)
1399 expr
->unop
= cast_to(expr
->unop
, rtype
);
1401 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1402 /* no conversions needed */
1403 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1404 /* no conversions needed */
1406 return bad_expr_type(expr
);
1408 if (expr
->op
== '+')
1409 *expr
= *expr
->unop
;
1410 expr
->ctype
= ctype
;
1414 static struct symbol
*evaluate_preop(struct expression
*expr
)
1416 struct symbol
*ctype
= expr
->unop
->ctype
;
1420 *expr
= *expr
->unop
;
1426 return evaluate_sign(expr
);
1429 return evaluate_dereference(expr
);
1432 return evaluate_addressof(expr
);
1434 case SPECIAL_INCREMENT
:
1435 case SPECIAL_DECREMENT
:
1437 * From a type evaluation standpoint the pre-ops are
1438 * the same as the postops
1440 return evaluate_postop(expr
);
1443 if (is_safe_type(ctype
))
1444 warning(expr
->pos
, "testing a 'safe expression'");
1445 if (is_float_type(ctype
)) {
1446 struct expression
*arg
= expr
->unop
;
1447 expr
->type
= EXPR_BINOP
;
1448 expr
->op
= SPECIAL_EQUAL
;
1450 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1451 expr
->right
->ctype
= ctype
;
1452 expr
->right
->fvalue
= 0;
1454 ctype
= &bool_ctype
;
1460 expr
->ctype
= ctype
;
1464 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1466 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1467 struct ptr_list
*list
= head
;
1473 for (i
= 0; i
< list
->nr
; i
++) {
1474 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1476 if (sym
->ident
!= ident
)
1478 *offset
= sym
->offset
;
1481 struct symbol
*ctype
= sym
->ctype
.base_type
;
1485 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1487 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1490 *offset
+= sym
->offset
;
1494 } while ((list
= list
->next
) != head
);
1498 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1500 struct expression
*add
;
1503 * Create a new add-expression
1505 * NOTE! Even if we just add zero, we need a new node
1506 * for the member pointer, since it has a different
1507 * type than the original pointer. We could make that
1508 * be just a cast, but the fact is, a node is a node,
1509 * so we might as well just do the "add zero" here.
1511 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1514 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1515 add
->right
->ctype
= &int_ctype
;
1516 add
->right
->value
= offset
;
1519 * The ctype of the pointer will be lazily evaluated if
1520 * we ever take the address of this member dereference..
1522 add
->ctype
= &lazy_ptr_ctype
;
1526 /* structure/union dereference */
1527 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1530 struct symbol
*ctype
, *member
;
1531 struct expression
*deref
= expr
->deref
, *add
;
1532 struct ident
*ident
= expr
->member
;
1536 if (!evaluate_expression(deref
))
1539 warning(expr
->pos
, "bad member name");
1543 ctype
= deref
->ctype
;
1544 address_space
= ctype
->ctype
.as
;
1545 mod
= ctype
->ctype
.modifiers
;
1546 if (ctype
->type
== SYM_NODE
) {
1547 ctype
= ctype
->ctype
.base_type
;
1548 address_space
|= ctype
->ctype
.as
;
1549 mod
|= ctype
->ctype
.modifiers
;
1551 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1552 warning(expr
->pos
, "expected structure or union");
1556 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1558 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1559 const char *name
= "<unnamed>";
1562 name
= ctype
->ident
->name
;
1563 namelen
= ctype
->ident
->len
;
1565 warning(expr
->pos
, "no member '%s' in %s %.*s",
1566 show_ident(ident
), type
, namelen
, name
);
1571 * The member needs to take on the address space and modifiers of
1572 * the "parent" type.
1574 member
= convert_to_as_mod(member
, address_space
, mod
);
1575 ctype
= member
->ctype
.base_type
;
1577 if (!lvalue_expression(deref
)) {
1578 if (deref
->type
!= EXPR_SLICE
) {
1582 expr
->base
= deref
->base
;
1583 expr
->r_bitpos
= deref
->r_bitpos
;
1585 expr
->r_bitpos
+= offset
<< 3;
1586 expr
->type
= EXPR_SLICE
;
1587 expr
->r_nrbits
= member
->bit_size
;
1588 expr
->r_bitpos
+= member
->bit_offset
;
1589 expr
->ctype
= member
;
1593 deref
= deref
->unop
;
1594 expr
->deref
= deref
;
1596 add
= evaluate_offset(deref
, offset
);
1597 expr
->type
= EXPR_PREOP
;
1601 expr
->ctype
= member
;
1605 static int is_promoted(struct expression
*expr
)
1608 switch (expr
->type
) {
1611 case EXPR_CONDITIONAL
:
1635 static struct symbol
*evaluate_cast(struct expression
*);
1637 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1639 struct symbol
*sym
= expr
->cast_type
;
1641 sym
= evaluate_expression(expr
->cast_expression
);
1645 * Expressions of restricted types will possibly get
1646 * promoted - check that here
1648 if (is_restricted_type(sym
)) {
1649 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1653 examine_symbol_type(sym
);
1654 if (is_bitfield_type(sym
)) {
1655 warning(expr
->pos
, "trying to examine bitfield type");
1661 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1663 struct symbol
*type
;
1666 type
= evaluate_type_information(expr
);
1670 size
= type
->bit_size
;
1672 warning(expr
->pos
, "cannot size expression");
1673 expr
->type
= EXPR_VALUE
;
1674 expr
->value
= size
>> 3;
1675 expr
->ctype
= size_t_ctype
;
1676 return size_t_ctype
;
1679 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1681 struct symbol
*type
;
1684 type
= evaluate_type_information(expr
);
1688 if (type
->type
== SYM_NODE
)
1689 type
= type
->ctype
.base_type
;
1692 switch (type
->type
) {
1696 type
= type
->ctype
.base_type
;
1700 warning(expr
->pos
, "expected pointer expression");
1703 size
= type
->bit_size
;
1706 expr
->type
= EXPR_VALUE
;
1707 expr
->value
= size
>> 3;
1708 expr
->ctype
= size_t_ctype
;
1709 return size_t_ctype
;
1712 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1714 struct symbol
*type
;
1716 type
= evaluate_type_information(expr
);
1720 expr
->type
= EXPR_VALUE
;
1721 expr
->value
= type
->ctype
.alignment
;
1722 expr
->ctype
= size_t_ctype
;
1723 return size_t_ctype
;
1726 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1728 struct expression
*expr
;
1729 struct symbol_list
*argument_types
= fn
->arguments
;
1730 struct symbol
*argtype
;
1733 PREPARE_PTR_LIST(argument_types
, argtype
);
1734 FOR_EACH_PTR (head
, expr
) {
1735 struct expression
**p
= THIS_ADDRESS(expr
);
1736 struct symbol
*ctype
, *target
;
1737 ctype
= evaluate_expression(expr
);
1742 ctype
= degenerate(expr
);
1745 if (!target
&& ctype
->bit_size
< bits_in_int
)
1746 target
= &int_ctype
;
1748 static char where
[30];
1749 examine_symbol_type(target
);
1750 sprintf(where
, "argument %d", i
);
1751 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1755 NEXT_PTR_LIST(argtype
);
1756 } END_FOR_EACH_PTR(expr
);
1757 FINISH_PTR_LIST(argtype
);
1761 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1763 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1765 struct expression
*entry
= *ep
;
1766 struct expression
**parent
, *reuse
= NULL
;
1767 unsigned long offset
;
1769 unsigned long from
, to
;
1770 int accept_string
= is_byte_type(ctype
);
1775 if (entry
->type
== EXPR_INDEX
) {
1776 from
= entry
->idx_from
;
1777 to
= entry
->idx_to
+1;
1778 parent
= &entry
->idx_expression
;
1780 entry
= entry
->idx_expression
;
1783 offset
= from
* (ctype
->bit_size
>>3);
1785 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1786 reuse
->type
= EXPR_POS
;
1787 reuse
->ctype
= ctype
;
1788 reuse
->init_offset
= offset
;
1789 reuse
->init_nr
= to
- from
;
1790 reuse
->init_expr
= entry
;
1791 parent
= &reuse
->init_expr
;
1796 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1797 sym
= evaluate_expression(entry
);
1798 to
= from
+ get_expression_value(sym
->array_size
);
1800 evaluate_initializer(ctype
, parent
);
1805 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1807 struct expression
*entry
;
1810 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1811 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1812 } END_FOR_EACH_PTR(entry
);
1815 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1816 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1818 if (expression_list_size(expr
->expr_list
) != 1) {
1819 warning(expr
->pos
, "unexpected compound initializer");
1822 evaluate_array_initializer(ctype
, expr
);
1826 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1830 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1831 if (sym
->ident
== ident
)
1833 } END_FOR_EACH_PTR(sym
);
1837 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1839 struct expression
*entry
= *ep
;
1840 struct expression
**parent
;
1841 struct expression
*reuse
= NULL
;
1842 unsigned long offset
;
1845 error(entry
->pos
, "unknown named initializer");
1849 if (entry
->type
== EXPR_IDENTIFIER
) {
1851 entry
= entry
->ident_expression
;
1855 offset
= sym
->offset
;
1858 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1859 reuse
->type
= EXPR_POS
;
1861 reuse
->init_offset
= offset
;
1863 reuse
->init_expr
= entry
;
1864 parent
= &reuse
->init_expr
;
1868 evaluate_initializer(sym
, parent
);
1872 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1874 struct expression
*entry
;
1877 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1878 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1879 if (entry
->type
== EXPR_IDENTIFIER
) {
1880 struct ident
*ident
= entry
->expr_ident
;
1881 /* We special-case the "already right place" case */
1882 if (!sym
|| sym
->ident
!= ident
) {
1883 RESET_PTR_LIST(sym
);
1887 if (sym
->ident
== ident
)
1893 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1896 } END_FOR_EACH_PTR(entry
);
1897 FINISH_PTR_LIST(sym
);
1901 * Initializers are kind of like assignments. Except
1902 * they can be a hell of a lot more complex.
1904 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1906 struct expression
*expr
= *ep
;
1909 * Simple non-structure/array initializers are the simple
1910 * case, and look (and parse) largely like assignments.
1912 switch (expr
->type
) {
1914 int is_string
= expr
->type
== EXPR_STRING
;
1915 struct symbol
*rtype
= evaluate_expression(expr
);
1919 * char array[] = "string"
1920 * should _not_ degenerate.
1922 if (!is_string
|| !is_string_type(ctype
))
1923 rtype
= degenerate(expr
);
1924 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1929 case EXPR_INITIALIZER
:
1930 expr
->ctype
= ctype
;
1931 if (ctype
->type
== SYM_NODE
)
1932 ctype
= ctype
->ctype
.base_type
;
1934 switch (ctype
->type
) {
1937 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1940 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1943 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1946 evaluate_scalar_initializer(ctype
, expr
);
1950 case EXPR_IDENTIFIER
:
1951 if (ctype
->type
== SYM_NODE
)
1952 ctype
= ctype
->ctype
.base_type
;
1953 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1954 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1958 evaluate_one_struct_initializer(ctype
, ep
,
1959 find_struct_ident(ctype
, expr
->expr_ident
));
1963 if (ctype
->type
== SYM_NODE
)
1964 ctype
= ctype
->ctype
.base_type
;
1965 if (ctype
->type
!= SYM_ARRAY
) {
1966 error(expr
->pos
, "expected array");
1969 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1974 * An EXPR_POS expression has already been evaluated, and we don't
1975 * need to do anything more
1981 static int get_as(struct symbol
*sym
)
1989 mod
= sym
->ctype
.modifiers
;
1990 if (sym
->type
== SYM_NODE
) {
1991 sym
= sym
->ctype
.base_type
;
1992 as
|= sym
->ctype
.as
;
1993 mod
|= sym
->ctype
.modifiers
;
1997 * At least for now, allow casting to a "unsigned long".
1998 * That's how we do things like pointer arithmetic and
1999 * store pointers to registers.
2001 if (sym
== &ulong_ctype
)
2004 if (sym
&& sym
->type
== SYM_PTR
) {
2005 sym
= sym
->ctype
.base_type
;
2006 as
|= sym
->ctype
.as
;
2007 mod
|= sym
->ctype
.modifiers
;
2009 if (mod
& MOD_FORCE
)
2014 static struct symbol
*evaluate_cast(struct expression
*expr
)
2016 struct expression
*target
= expr
->cast_expression
;
2017 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2023 expr
->ctype
= ctype
;
2024 expr
->cast_type
= ctype
;
2027 * Special case: a cast can be followed by an
2028 * initializer, in which case we need to pass
2029 * the type value down to that initializer rather
2030 * than trying to evaluate it as an expression
2032 * A more complex case is when the initializer is
2033 * dereferenced as part of a post-fix expression.
2034 * We need to produce an expression that can be dereferenced.
2036 if (target
->type
== EXPR_INITIALIZER
) {
2037 struct symbol
*sym
= expr
->cast_type
;
2038 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2040 sym
->initializer
= expr
->cast_expression
;
2041 evaluate_symbol(sym
);
2043 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2046 expr
->type
= EXPR_PREOP
;
2054 evaluate_expression(target
);
2058 * You can always throw a value away by casting to
2059 * "void" - that's an implicit "force". Note that
2060 * the same is _not_ true of "void *".
2062 if (ctype
== &void_ctype
)
2066 if (type
== SYM_NODE
) {
2067 type
= ctype
->ctype
.base_type
->type
;
2068 if (ctype
->ctype
.base_type
== &void_ctype
)
2071 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2072 warning(expr
->pos
, "cast to non-scalar");
2074 if (!target
->ctype
) {
2075 warning(expr
->pos
, "cast from unknown type");
2079 type
= target
->ctype
->type
;
2080 if (type
== SYM_NODE
)
2081 type
= target
->ctype
->ctype
.base_type
->type
;
2082 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2083 warning(expr
->pos
, "cast from non-scalar");
2085 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2086 warning(expr
->pos
, "cast removes address space of expression");
2088 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2089 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2090 if (t1
->type
== SYM_NODE
)
2091 t1
= t1
->ctype
.base_type
;
2092 if (t2
->type
== SYM_NODE
)
2093 t2
= t2
->ctype
.base_type
;
2095 if (t1
->type
== SYM_RESTRICT
)
2096 warning(expr
->pos
, "cast to restricted type");
2097 if (t2
->type
== SYM_RESTRICT
)
2098 warning(expr
->pos
, "cast from restricted type");
2103 * Casts of constant values are special: they
2104 * can be NULL, and thus need to be simplified
2107 if (target
->type
== EXPR_VALUE
)
2108 cast_value(expr
, ctype
, target
, target
->ctype
);
2115 * Evaluate a call expression with a symbol. This
2116 * should expand inline functions, and evaluate
2119 static int evaluate_symbol_call(struct expression
*expr
)
2121 struct expression
*fn
= expr
->fn
;
2122 struct symbol
*ctype
= fn
->ctype
;
2124 if (fn
->type
!= EXPR_PREOP
)
2127 if (ctype
->op
&& ctype
->op
->evaluate
)
2128 return ctype
->op
->evaluate(expr
);
2130 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2132 struct symbol
*curr
= current_fn
;
2133 current_fn
= ctype
->ctype
.base_type
;
2134 examine_fn_arguments(current_fn
);
2136 ret
= inline_function(expr
, ctype
);
2138 /* restore the old function */
2146 static struct symbol
*evaluate_call(struct expression
*expr
)
2149 struct symbol
*ctype
, *sym
;
2150 struct expression
*fn
= expr
->fn
;
2151 struct expression_list
*arglist
= expr
->args
;
2153 if (!evaluate_expression(fn
))
2155 sym
= ctype
= fn
->ctype
;
2156 if (ctype
->type
== SYM_NODE
)
2157 ctype
= ctype
->ctype
.base_type
;
2158 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2159 ctype
= ctype
->ctype
.base_type
;
2160 if (!evaluate_arguments(sym
, ctype
, arglist
))
2162 if (ctype
->type
!= SYM_FN
) {
2163 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2166 args
= expression_list_size(expr
->args
);
2167 fnargs
= symbol_list_size(ctype
->arguments
);
2169 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2170 if (args
> fnargs
&& !ctype
->variadic
)
2171 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2172 if (sym
->type
== SYM_NODE
) {
2173 if (evaluate_symbol_call(expr
))
2176 expr
->ctype
= ctype
->ctype
.base_type
;
2180 struct symbol
*evaluate_expression(struct expression
*expr
)
2187 switch (expr
->type
) {
2190 warning(expr
->pos
, "value expression without a type");
2193 return evaluate_string(expr
);
2195 return evaluate_symbol_expression(expr
);
2197 if (!evaluate_expression(expr
->left
))
2199 if (!evaluate_expression(expr
->right
))
2201 return evaluate_binop(expr
);
2203 return evaluate_logical(expr
);
2205 evaluate_expression(expr
->left
);
2206 if (!evaluate_expression(expr
->right
))
2208 return evaluate_comma(expr
);
2210 if (!evaluate_expression(expr
->left
))
2212 if (!evaluate_expression(expr
->right
))
2214 return evaluate_compare(expr
);
2215 case EXPR_ASSIGNMENT
:
2216 if (!evaluate_expression(expr
->left
))
2218 if (!evaluate_expression(expr
->right
))
2220 return evaluate_assignment(expr
);
2222 if (!evaluate_expression(expr
->unop
))
2224 return evaluate_preop(expr
);
2226 if (!evaluate_expression(expr
->unop
))
2228 return evaluate_postop(expr
);
2230 case EXPR_IMPLIED_CAST
:
2231 return evaluate_cast(expr
);
2233 return evaluate_sizeof(expr
);
2234 case EXPR_PTRSIZEOF
:
2235 return evaluate_ptrsizeof(expr
);
2237 return evaluate_alignof(expr
);
2239 return evaluate_member_dereference(expr
);
2241 return evaluate_call(expr
);
2243 case EXPR_CONDITIONAL
:
2244 return evaluate_conditional_expression(expr
);
2245 case EXPR_STATEMENT
:
2246 expr
->ctype
= evaluate_statement(expr
->statement
);
2250 expr
->ctype
= &ptr_ctype
;
2254 /* Evaluate the type of the symbol .. */
2255 evaluate_symbol(expr
->symbol
);
2256 /* .. but the type of the _expression_ is a "type" */
2257 expr
->ctype
= &type_ctype
;
2260 /* These can not exist as stand-alone expressions */
2261 case EXPR_INITIALIZER
:
2262 case EXPR_IDENTIFIER
:
2265 warning(expr
->pos
, "internal front-end error: initializer in expression");
2268 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2274 static void check_duplicates(struct symbol
*sym
)
2276 struct symbol
*next
= sym
;
2278 while ((next
= next
->same_symbol
) != NULL
) {
2279 const char *typediff
;
2280 evaluate_symbol(next
);
2281 typediff
= type_difference(sym
, next
, 0, 0);
2283 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2284 show_ident(sym
->ident
),
2285 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2291 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2293 struct symbol
*base_type
;
2298 sym
= examine_symbol_type(sym
);
2299 base_type
= sym
->ctype
.base_type
;
2303 /* Evaluate the initializers */
2304 if (sym
->initializer
)
2305 evaluate_initializer(sym
, &sym
->initializer
);
2307 /* And finally, evaluate the body of the symbol too */
2308 if (base_type
->type
== SYM_FN
) {
2309 struct symbol
*curr
= current_fn
;
2311 current_fn
= base_type
;
2313 examine_fn_arguments(base_type
);
2314 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2316 if (base_type
->stmt
)
2317 evaluate_statement(base_type
->stmt
);
2325 void evaluate_symbol_list(struct symbol_list
*list
)
2329 FOR_EACH_PTR(list
, sym
) {
2330 check_duplicates(sym
);
2331 evaluate_symbol(sym
);
2332 } END_FOR_EACH_PTR(sym
);
2335 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2337 struct expression
*expr
= stmt
->expression
;
2338 struct symbol
*ctype
, *fntype
;
2340 evaluate_expression(expr
);
2341 ctype
= degenerate(expr
);
2342 fntype
= current_fn
->ctype
.base_type
;
2343 if (!fntype
|| fntype
== &void_ctype
) {
2344 if (expr
&& ctype
!= &void_ctype
)
2345 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2350 warning(stmt
->pos
, "return with no return value");
2355 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2359 static void evaluate_if_statement(struct statement
*stmt
)
2361 if (!stmt
->if_conditional
)
2364 evaluate_conditional(stmt
->if_conditional
, 0);
2365 evaluate_statement(stmt
->if_true
);
2366 evaluate_statement(stmt
->if_false
);
2369 static void evaluate_iterator(struct statement
*stmt
)
2371 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2372 evaluate_conditional(stmt
->iterator_post_condition
,1);
2373 evaluate_statement(stmt
->iterator_pre_statement
);
2374 evaluate_statement(stmt
->iterator_statement
);
2375 evaluate_statement(stmt
->iterator_post_statement
);
2378 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2380 switch (*constraint
) {
2381 case '=': /* Assignment */
2382 case '+': /* Update */
2385 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2389 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2391 switch (*constraint
) {
2392 case '=': /* Assignment */
2393 case '+': /* Update */
2394 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2398 static void evaluate_asm_statement(struct statement
*stmt
)
2400 struct expression
*expr
;
2403 expr
= stmt
->asm_string
;
2404 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2405 warning(stmt
->pos
, "need constant string for inline asm");
2410 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2411 struct ident
*ident
;
2414 case 0: /* Identifier */
2416 ident
= (struct ident
*)expr
;
2419 case 1: /* Constraint */
2421 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2422 warning(expr
->pos
, "asm output constraint is not a string");
2423 *THIS_ADDRESS(expr
) = NULL
;
2426 verify_output_constraint(expr
, expr
->string
->data
);
2429 case 2: /* Expression */
2431 if (!evaluate_expression(expr
))
2433 if (!lvalue_expression(expr
))
2434 warning(expr
->pos
, "asm output is not an lvalue");
2437 } END_FOR_EACH_PTR(expr
);
2440 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2441 struct ident
*ident
;
2444 case 0: /* Identifier */
2446 ident
= (struct ident
*)expr
;
2449 case 1: /* Constraint */
2451 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2452 warning(expr
->pos
, "asm input constraint is not a string");
2453 *THIS_ADDRESS(expr
) = NULL
;
2456 verify_input_constraint(expr
, expr
->string
->data
);
2459 case 2: /* Expression */
2461 if (!evaluate_expression(expr
))
2465 } END_FOR_EACH_PTR(expr
);
2467 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2469 warning(stmt
->pos
, "bad asm output");
2472 if (expr
->type
== EXPR_STRING
)
2474 warning(expr
->pos
, "asm clobber is not a string");
2475 } END_FOR_EACH_PTR(expr
);
2478 struct symbol
*evaluate_statement(struct statement
*stmt
)
2483 switch (stmt
->type
) {
2485 return evaluate_return_expression(stmt
);
2487 case STMT_EXPRESSION
:
2488 if (!evaluate_expression(stmt
->expression
))
2490 return degenerate(stmt
->expression
);
2492 case STMT_COMPOUND
: {
2493 struct statement
*s
;
2494 struct symbol
*type
= NULL
;
2497 /* Evaluate each symbol in the compound statement */
2498 FOR_EACH_PTR(stmt
->syms
, sym
) {
2499 evaluate_symbol(sym
);
2500 } END_FOR_EACH_PTR(sym
);
2501 evaluate_symbol(stmt
->ret
);
2504 * Then, evaluate each statement, making the type of the
2505 * compound statement be the type of the last statement
2508 FOR_EACH_PTR(stmt
->stmts
, s
) {
2509 type
= evaluate_statement(s
);
2510 } END_FOR_EACH_PTR(s
);
2516 evaluate_if_statement(stmt
);
2519 evaluate_iterator(stmt
);
2522 evaluate_expression(stmt
->switch_expression
);
2523 evaluate_statement(stmt
->switch_statement
);
2526 evaluate_expression(stmt
->case_expression
);
2527 evaluate_expression(stmt
->case_to
);
2528 evaluate_statement(stmt
->case_statement
);
2531 return evaluate_statement(stmt
->label_statement
);
2533 evaluate_expression(stmt
->goto_expression
);
2538 evaluate_asm_statement(stmt
);
2541 evaluate_expression(stmt
->expression
);