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 expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
77 sym
->ctype
.modifiers
= MOD_STATIC
;
78 sym
->ctype
.base_type
= array
;
79 sym
->initializer
= initstr
;
82 initstr
->string
= expr
->string
;
84 array
->array_size
= sym
->array_size
;
85 array
->bit_size
= bits_in_char
* length
;
86 array
->ctype
.alignment
= 1;
87 array
->ctype
.modifiers
= MOD_STATIC
;
88 array
->ctype
.base_type
= &char_ctype
;
91 addr
->ctype
= &lazy_ptr_ctype
;
93 expr
->type
= EXPR_PREOP
;
100 static inline struct symbol
*integer_promotion(struct symbol
*type
)
102 unsigned long mod
= type
->ctype
.modifiers
;
105 if (type
->type
== SYM_NODE
)
106 type
= type
->ctype
.base_type
;
107 if (type
->type
== SYM_ENUM
)
108 type
= type
->ctype
.base_type
;
109 width
= type
->bit_size
;
110 if (type
->type
== SYM_BITFIELD
)
111 type
= type
->ctype
.base_type
;
112 mod
= type
->ctype
.modifiers
;
113 if (width
< bits_in_int
)
116 /* If char/short has as many bits as int, it still gets "promoted" */
117 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
119 if (mod
& MOD_UNSIGNED
)
126 * integer part of usual arithmetic conversions:
127 * integer promotions are applied
128 * if left and right are identical, we are done
129 * if signedness is the same, convert one with lower rank
130 * unless unsigned argument has rank lower than signed one, convert the
132 * if signed argument is bigger than unsigned one, convert the unsigned.
133 * otherwise, convert signed.
135 * Leaving aside the integer promotions, that is equivalent to
136 * if identical, don't convert
137 * if left is bigger than right, convert right
138 * if right is bigger than left, convert right
139 * otherwise, if signedness is the same, convert one with lower rank
140 * otherwise convert the signed one.
142 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
144 unsigned long lmod
, rmod
;
146 left
= integer_promotion(left
);
147 right
= integer_promotion(right
);
152 if (left
->bit_size
> right
->bit_size
)
155 if (right
->bit_size
> left
->bit_size
)
158 lmod
= left
->ctype
.modifiers
;
159 rmod
= right
->ctype
.modifiers
;
160 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
161 if (lmod
& MOD_UNSIGNED
)
163 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
171 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
173 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
176 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
178 unsigned long mod
, as
;
182 mod
|= node
->ctype
.modifiers
;
183 as
|= node
->ctype
.as
;
184 if (node
->type
== SYM_NODE
) {
185 node
= node
->ctype
.base_type
;
190 *modp
= mod
& ~MOD_IGNORE
;
195 static int is_same_type(struct expression
*expr
, struct symbol
*new)
197 struct symbol
*old
= expr
->ctype
;
198 unsigned long oldmod
, newmod
, oldas
, newas
;
200 old
= base_type(old
, &oldmod
, &oldas
);
201 new = base_type(new, &newmod
, &newas
);
203 /* Same base type, same address space? */
204 if (old
== new && oldas
== newas
) {
205 unsigned long difmod
;
207 /* Check the modifier bits. */
208 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
210 /* Exact same type? */
215 * Not the same type, but differs only in "const".
216 * Don't warn about MOD_NOCAST.
218 if (difmod
== MOD_CONST
)
221 if ((oldmod
| newmod
) & MOD_NOCAST
) {
222 const char *tofrom
= "to/from";
223 if (!(newmod
& MOD_NOCAST
))
225 if (!(oldmod
& MOD_NOCAST
))
227 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
233 * This gets called for implicit casts in assignments and
234 * integer promotion. We often want to try to move the
235 * cast down, because the ops involved may have been
236 * implicitly cast up, and we can get rid of the casts
239 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
241 struct expression
*expr
;
243 if (is_same_type(old
, type
))
247 * See if we can simplify the op. Move the cast down.
251 if (old
->op
== '~') {
253 old
->unop
= cast_to(old
->unop
, type
);
258 case EXPR_IMPLIED_CAST
:
259 if (old
->ctype
->bit_size
>= type
->bit_size
) {
260 struct expression
*orig
= old
->cast_expression
;
261 if (same_cast_type(orig
->ctype
, type
))
263 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
265 old
->cast_type
= type
;
275 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
277 expr
->cast_type
= type
;
278 expr
->cast_expression
= old
;
282 static int is_type_type(struct symbol
*type
)
284 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
287 int is_ptr_type(struct symbol
*type
)
289 if (type
->type
== SYM_NODE
)
290 type
= type
->ctype
.base_type
;
291 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
294 static inline int is_float_type(struct symbol
*type
)
296 if (type
->type
== SYM_NODE
)
297 type
= type
->ctype
.base_type
;
298 return type
->ctype
.base_type
== &fp_type
;
301 static inline int is_byte_type(struct symbol
*type
)
303 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
306 static inline int is_string_type(struct symbol
*type
)
308 if (type
->type
== SYM_NODE
)
309 type
= type
->ctype
.base_type
;
310 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
313 static struct symbol
*bad_expr_type(struct expression
*expr
)
315 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
316 switch (expr
->type
) {
319 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
320 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
324 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
333 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
335 struct expression
*left
= *lp
, *right
= *rp
;
336 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
338 if (ltype
->type
== SYM_NODE
)
339 ltype
= ltype
->ctype
.base_type
;
340 if (rtype
->type
== SYM_NODE
)
341 rtype
= rtype
->ctype
.base_type
;
342 if (is_float_type(ltype
)) {
343 if (is_int_type(rtype
))
345 if (is_float_type(rtype
)) {
346 unsigned long lmod
= ltype
->ctype
.modifiers
;
347 unsigned long rmod
= rtype
->ctype
.modifiers
;
348 lmod
&= MOD_LONG
| MOD_LONGLONG
;
349 rmod
&= MOD_LONG
| MOD_LONGLONG
;
359 if (!is_float_type(rtype
) || !is_int_type(ltype
))
362 *lp
= cast_to(left
, rtype
);
365 *rp
= cast_to(right
, ltype
);
369 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
371 struct expression
*left
= *lp
, *right
= *rp
;
372 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
374 if (ltype
->type
== SYM_NODE
)
375 ltype
= ltype
->ctype
.base_type
;
376 if (rtype
->type
== SYM_NODE
)
377 rtype
= rtype
->ctype
.base_type
;
378 if (is_int_type(ltype
) && is_int_type(rtype
)) {
379 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
381 *lp
= cast_to(left
, ctype
);
382 *rp
= cast_to(right
, ctype
);
388 static int restricted_value(struct expression
*v
, struct symbol
*type
)
390 if (v
->type
!= EXPR_VALUE
)
397 static int restricted_binop(int op
, struct symbol
*type
)
406 case SPECIAL_NOTEQUAL
:
407 case SPECIAL_AND_ASSIGN
:
408 case SPECIAL_OR_ASSIGN
:
409 case SPECIAL_XOR_ASSIGN
:
416 static int restricted_unop(int op
, struct symbol
*type
)
418 if (op
== '~' && type
->bit_size
>= bits_in_int
)
425 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
427 struct expression
*left
= *lp
, *right
= *rp
;
428 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
429 struct symbol
*type
= NULL
;
431 if (ltype
->type
== SYM_NODE
)
432 ltype
= ltype
->ctype
.base_type
;
433 if (ltype
->type
== SYM_ENUM
)
434 ltype
= ltype
->ctype
.base_type
;
435 if (rtype
->type
== SYM_NODE
)
436 rtype
= rtype
->ctype
.base_type
;
437 if (rtype
->type
== SYM_ENUM
)
438 rtype
= rtype
->ctype
.base_type
;
439 if (is_restricted_type(ltype
)) {
440 if (is_restricted_type(rtype
)) {
444 if (!restricted_value(right
, ltype
))
447 } else if (is_restricted_type(rtype
)) {
448 if (!restricted_value(left
, rtype
))
453 if (restricted_binop(op
, type
))
458 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
460 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
461 if (!ctype
&& float_ok
)
462 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
464 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
469 return bad_expr_type(expr
);
472 static inline int lvalue_expression(struct expression
*expr
)
474 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
477 static int ptr_object_size(struct symbol
*ptr_type
)
479 if (ptr_type
->type
== SYM_NODE
)
480 ptr_type
= ptr_type
->ctype
.base_type
;
481 if (ptr_type
->type
== SYM_PTR
)
482 ptr_type
= get_base_type(ptr_type
);
483 return ptr_type
->bit_size
;
486 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
488 struct expression
*i
= *ip
;
489 struct symbol
*ptr_type
= ctype
;
492 if (ptr_type
->type
== SYM_NODE
)
493 ptr_type
= ptr_type
->ctype
.base_type
;
495 if (!is_int_type(i
->ctype
))
496 return bad_expr_type(expr
);
498 examine_symbol_type(ctype
);
500 if (!ctype
->ctype
.base_type
) {
501 warning(expr
->pos
, "missing type information");
505 /* Get the size of whatever the pointer points to */
506 bit_size
= ptr_object_size(ctype
);
508 if (bit_size
> bits_in_char
) {
509 int multiply
= bit_size
>> 3;
510 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
512 if (i
->type
== EXPR_VALUE
) {
513 val
->value
= i
->value
* multiply
;
514 val
->ctype
= size_t_ctype
;
517 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
519 val
->ctype
= size_t_ctype
;
520 val
->value
= bit_size
>> 3;
523 mul
->ctype
= size_t_ctype
;
535 static struct symbol
*evaluate_add(struct expression
*expr
)
537 struct expression
*left
= expr
->left
, *right
= expr
->right
;
538 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
540 if (is_ptr_type(ltype
))
541 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
543 if (is_ptr_type(rtype
))
544 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
546 return evaluate_arith(expr
, 1);
549 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
550 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
553 unsigned long mod1
, mod2
, diff
;
554 unsigned long as1
, as2
;
556 struct symbol
*base1
, *base2
;
558 if (target
== source
)
560 if (!target
|| !source
)
561 return "different types";
563 * Peel of per-node information.
564 * FIXME! Check alignment and context too here!
566 mod1
= target
->ctype
.modifiers
;
567 as1
= target
->ctype
.as
;
568 mod2
= source
->ctype
.modifiers
;
569 as2
= source
->ctype
.as
;
570 if (target
->type
== SYM_NODE
) {
571 target
= target
->ctype
.base_type
;
574 if (target
->type
== SYM_PTR
) {
578 mod1
|= target
->ctype
.modifiers
;
579 as1
|= target
->ctype
.as
;
581 if (source
->type
== SYM_NODE
) {
582 source
= source
->ctype
.base_type
;
585 if (source
->type
== SYM_PTR
) {
589 mod2
|= source
->ctype
.modifiers
;
590 as2
|= source
->ctype
.as
;
592 if (target
->type
== SYM_ENUM
) {
593 target
= target
->ctype
.base_type
;
597 if (source
->type
== SYM_ENUM
) {
598 source
= source
->ctype
.base_type
;
603 if (target
== source
)
605 if (!target
|| !source
)
606 return "different types";
608 type1
= target
->type
;
609 base1
= target
->ctype
.base_type
;
611 type2
= source
->type
;
612 base2
= source
->ctype
.base_type
;
615 * Pointers to functions compare as the function itself
617 if (type1
== SYM_PTR
&& base1
) {
618 base1
= examine_symbol_type(base1
);
619 switch (base1
->type
) {
623 base1
= base1
->ctype
.base_type
;
628 if (type2
== SYM_PTR
&& base2
) {
629 base2
= examine_symbol_type(base2
);
630 switch (base2
->type
) {
634 base2
= base2
->ctype
.base_type
;
640 /* Arrays degenerate to pointers for type comparisons */
641 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
642 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
644 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
645 return "different base types";
647 /* Must be same address space to be comparable */
649 return "different address spaces";
651 /* Ignore differences in storage types or addressability */
652 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
653 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
656 return "different type sizes";
657 if (diff
& ~MOD_SIGNEDNESS
)
658 return "different modifiers";
660 /* Differs in signedness only.. */
663 * Warn if both are explicitly signed ("unsigned" is obvously
664 * always explicit, and since we know one of them has to be
665 * unsigned, we check if the signed one was explicit).
667 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
668 return "different explicit signedness";
671 * "char" matches both "unsigned char" and "signed char",
672 * so if the explicit test didn't trigger, then we should
673 * not warn about a char.
675 if (!(mod1
& MOD_CHAR
))
676 return "different signedness";
680 if (type1
== SYM_FN
) {
682 struct symbol
*arg1
, *arg2
;
683 if (base1
->variadic
!= base2
->variadic
)
684 return "incompatible variadic arguments";
685 PREPARE_PTR_LIST(target
->arguments
, arg1
);
686 PREPARE_PTR_LIST(source
->arguments
, arg2
);
690 diff
= type_difference(arg1
, arg2
, 0, 0);
692 static char argdiff
[80];
693 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
702 FINISH_PTR_LIST(arg2
);
703 FINISH_PTR_LIST(arg1
);
712 static int is_null_ptr(struct expression
*expr
)
714 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
716 if (!is_ptr_type(expr
->ctype
))
717 warning(expr
->pos
, "Using plain integer as NULL pointer");
721 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
723 /* NULL expression? Just return the type of the "other side" */
732 * Ignore differences in "volatile" and "const"ness when
733 * subtracting pointers
735 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
737 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
739 const char *typediff
;
740 struct symbol
*ctype
;
741 struct symbol
*ltype
, *rtype
;
742 struct expression
*r
= *rp
;
744 ltype
= degenerate(l
);
745 rtype
= degenerate(r
);
748 * If it is an integer subtract: the ptr add case will do the
751 if (!is_ptr_type(rtype
))
752 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
755 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
757 ctype
= common_ptr_type(l
, r
);
759 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
763 examine_symbol_type(ctype
);
765 /* Figure out the base type we point to */
766 if (ctype
->type
== SYM_NODE
)
767 ctype
= ctype
->ctype
.base_type
;
768 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
769 warning(expr
->pos
, "subtraction of functions? Share your drugs");
772 ctype
= get_base_type(ctype
);
774 expr
->ctype
= ssize_t_ctype
;
775 if (ctype
->bit_size
> bits_in_char
) {
776 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
777 struct expression
*div
= expr
;
778 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
779 unsigned long value
= ctype
->bit_size
>> 3;
781 val
->ctype
= size_t_ctype
;
784 if (value
& (value
-1)) {
785 if (Wptr_subtraction_blows
)
786 warning(expr
->pos
, "potentially expensive pointer subtraction");
790 sub
->ctype
= ssize_t_ctype
;
799 return ssize_t_ctype
;
802 static struct symbol
*evaluate_sub(struct expression
*expr
)
804 struct expression
*left
= expr
->left
;
805 struct symbol
*ltype
= left
->ctype
;
807 if (is_ptr_type(ltype
))
808 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
810 return evaluate_arith(expr
, 1);
813 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
815 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
817 struct symbol
*ctype
;
822 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
823 warning(expr
->pos
, "assignment expression in conditional");
825 ctype
= evaluate_expression(expr
);
827 if (is_safe_type(ctype
))
828 warning(expr
->pos
, "testing a 'safe expression'");
834 static struct symbol
*evaluate_logical(struct expression
*expr
)
836 if (!evaluate_conditional(expr
->left
, 0))
838 if (!evaluate_conditional(expr
->right
, 0))
841 expr
->ctype
= &bool_ctype
;
845 static struct symbol
*evaluate_shift(struct expression
*expr
)
847 struct expression
*left
= expr
->left
, *right
= expr
->right
;
848 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
850 if (ltype
->type
== SYM_NODE
)
851 ltype
= ltype
->ctype
.base_type
;
852 if (rtype
->type
== SYM_NODE
)
853 rtype
= rtype
->ctype
.base_type
;
854 if (is_int_type(ltype
) && is_int_type(rtype
)) {
855 struct symbol
*ctype
= integer_promotion(ltype
);
856 expr
->left
= cast_to(expr
->left
, ctype
);
858 ctype
= integer_promotion(rtype
);
859 expr
->right
= cast_to(expr
->right
, ctype
);
862 return bad_expr_type(expr
);
865 static struct symbol
*evaluate_binop(struct expression
*expr
)
868 // addition can take ptr+int, fp and int
870 return evaluate_add(expr
);
872 // subtraction can take ptr-ptr, fp and int
874 return evaluate_sub(expr
);
876 // Arithmetic operations can take fp and int
878 return evaluate_arith(expr
, 1);
880 // shifts do integer promotions, but that's it.
881 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
882 return evaluate_shift(expr
);
884 // The rest are integer operations
885 // '%', '&', '^', '|'
887 return evaluate_arith(expr
, 0);
891 static struct symbol
*evaluate_comma(struct expression
*expr
)
893 expr
->ctype
= expr
->right
->ctype
;
897 static int modify_for_unsigned(int op
)
900 op
= SPECIAL_UNSIGNED_LT
;
902 op
= SPECIAL_UNSIGNED_GT
;
903 else if (op
== SPECIAL_LTE
)
904 op
= SPECIAL_UNSIGNED_LTE
;
905 else if (op
== SPECIAL_GTE
)
906 op
= SPECIAL_UNSIGNED_GTE
;
910 static struct symbol
*evaluate_compare(struct expression
*expr
)
912 struct expression
*left
= expr
->left
, *right
= expr
->right
;
913 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
914 struct symbol
*ctype
;
917 if (is_type_type(ltype
) && is_type_type(rtype
))
920 if (is_safe_type(ltype
) || is_safe_type(rtype
))
921 warning(expr
->pos
, "testing a 'safe expression'");
924 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
925 // FIXME! Check the types for compatibility
926 expr
->op
= modify_for_unsigned(expr
->op
);
930 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
932 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
933 expr
->op
= modify_for_unsigned(expr
->op
);
937 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
941 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
943 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
944 expr
->op
= modify_for_unsigned(expr
->op
);
951 expr
->ctype
= &bool_ctype
;
956 * FIXME!! This should do casts, array degeneration etc..
958 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
960 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
962 if (ltype
->type
== SYM_NODE
)
963 ltype
= ltype
->ctype
.base_type
;
965 if (rtype
->type
== SYM_NODE
)
966 rtype
= rtype
->ctype
.base_type
;
968 if (ltype
->type
== SYM_PTR
) {
969 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
973 if (rtype
->type
== SYM_PTR
) {
974 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
981 * NOTE! The degenerate case of "x ? : y", where we don't
982 * have a true case, this will possibly promote "x" to the
983 * same type as "y", and thus _change_ the conditional
984 * test in the expression. But since promotion is "safe"
985 * for testing, that's ok.
987 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
989 struct expression
**true;
990 struct symbol
*ctype
, *ltype
, *rtype
;
991 const char * typediff
;
993 if (!evaluate_conditional(expr
->conditional
, 0))
995 if (!evaluate_expression(expr
->cond_false
))
998 ctype
= degenerate(expr
->conditional
);
999 rtype
= degenerate(expr
->cond_false
);
1001 true = &expr
->conditional
;
1003 if (expr
->cond_true
) {
1004 if (!evaluate_expression(expr
->cond_true
))
1006 ltype
= degenerate(expr
->cond_true
);
1007 true = &expr
->cond_true
;
1010 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1013 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1016 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1019 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1023 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1026 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1030 expr
->ctype
= ctype
;
1034 /* FP assignments can not do modulo or bit operations */
1035 static int compatible_float_op(int op
)
1038 op
== SPECIAL_ADD_ASSIGN
||
1039 op
== SPECIAL_SUB_ASSIGN
||
1040 op
== SPECIAL_MUL_ASSIGN
||
1041 op
== SPECIAL_DIV_ASSIGN
;
1044 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1045 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1047 const char *typediff
;
1051 if (is_int_type(target
)) {
1052 if (is_int_type(source
))
1054 if (is_float_type(source
))
1056 } else if (is_float_type(target
)) {
1057 if (!compatible_float_op(op
)) {
1058 warning(expr
->pos
, "invalid assignment");
1061 if (is_int_type(source
))
1063 if (is_float_type(source
))
1065 } else if (is_restricted_type(target
)) {
1066 if (restricted_binop(op
, target
)) {
1067 warning(expr
->pos
, "bad restricted assignment");
1070 if (!restricted_value(*rp
, target
))
1072 } else if (is_ptr_type(target
)) {
1073 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1074 evaluate_ptr_add(expr
, target
, rp
);
1078 warning(expr
->pos
, "invalid pointer assignment");
1081 } else if (op
!= '=') {
1082 warning(expr
->pos
, "invalid assignment");
1086 /* It's ok if the target is more volatile or const than the source */
1087 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1091 /* Pointer destination? */
1093 target_as
= t
->ctype
.as
;
1094 if (t
->type
== SYM_NODE
) {
1095 t
= t
->ctype
.base_type
;
1096 target_as
|= t
->ctype
.as
;
1098 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1099 struct expression
*right
= *rp
;
1100 struct symbol
*s
= source
;
1103 // NULL pointer is always ok
1104 if (is_null_ptr(right
))
1107 /* "void *" matches anything as long as the address space is ok */
1108 source_as
= s
->ctype
.as
;
1109 if (s
->type
== SYM_NODE
) {
1110 s
= s
->ctype
.base_type
;
1111 source_as
|= s
->ctype
.as
;
1113 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1114 s
= get_base_type(s
);
1115 t
= get_base_type(t
);
1116 if (s
== &void_ctype
|| t
== &void_ctype
)
1121 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1122 info(expr
->pos
, " expected %s", show_typename(target
));
1123 info(expr
->pos
, " got %s", show_typename(source
));
1124 *rp
= cast_to(*rp
, target
);
1127 *rp
= cast_to(*rp
, target
);
1131 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1133 if (type
->ctype
.modifiers
& MOD_CONST
)
1134 warning(left
->pos
, "assignment to const expression");
1135 if (type
->type
== SYM_NODE
)
1136 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1139 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1141 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1142 struct expression
*where
= expr
;
1143 struct symbol
*ltype
, *rtype
;
1145 if (!lvalue_expression(left
)) {
1146 warning(expr
->pos
, "not an lvalue");
1150 ltype
= left
->ctype
;
1152 rtype
= degenerate(right
);
1154 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1157 evaluate_assign_to(left
, ltype
);
1159 expr
->ctype
= ltype
;
1163 static void examine_fn_arguments(struct symbol
*fn
)
1167 FOR_EACH_PTR(fn
->arguments
, s
) {
1168 struct symbol
*arg
= evaluate_symbol(s
);
1169 /* Array/function arguments silently degenerate into pointers */
1175 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1176 if (arg
->type
== SYM_ARRAY
)
1177 ptr
->ctype
= arg
->ctype
;
1179 ptr
->ctype
.base_type
= arg
;
1180 ptr
->ctype
.as
|= s
->ctype
.as
;
1181 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1183 s
->ctype
.base_type
= ptr
;
1185 s
->ctype
.modifiers
= 0;
1188 examine_symbol_type(s
);
1195 } END_FOR_EACH_PTR(s
);
1198 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1200 /* Take the modifiers of the pointer, and apply them to the member */
1201 mod
|= sym
->ctype
.modifiers
;
1202 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1203 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1205 newsym
->ctype
.as
= as
;
1206 newsym
->ctype
.modifiers
= mod
;
1212 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1214 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1215 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1217 node
->ctype
.base_type
= ptr
;
1218 ptr
->bit_size
= bits_in_pointer
;
1219 ptr
->ctype
.alignment
= pointer_alignment
;
1221 node
->bit_size
= bits_in_pointer
;
1222 node
->ctype
.alignment
= pointer_alignment
;
1225 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1226 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1227 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1229 if (sym
->type
== SYM_NODE
) {
1230 ptr
->ctype
.as
|= sym
->ctype
.as
;
1231 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1232 sym
= sym
->ctype
.base_type
;
1234 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1235 ptr
->ctype
.as
|= sym
->ctype
.as
;
1236 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1237 sym
= sym
->ctype
.base_type
;
1239 ptr
->ctype
.base_type
= sym
;
1244 /* Arrays degenerate into pointers on pointer arithmetic */
1245 static struct symbol
*degenerate(struct expression
*expr
)
1247 struct symbol
*ctype
, *base
;
1251 ctype
= expr
->ctype
;
1254 base
= examine_symbol_type(ctype
);
1255 if (ctype
->type
== SYM_NODE
)
1256 base
= ctype
->ctype
.base_type
;
1258 * Arrays degenerate into pointers to the entries, while
1259 * functions degenerate into pointers to themselves.
1260 * If array was part of non-lvalue compound, we create a copy
1261 * of that compound first and then act as if we were dealing with
1262 * the corresponding field in there.
1264 switch (base
->type
) {
1266 if (expr
->type
== EXPR_SLICE
) {
1267 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1268 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1270 a
->ctype
.base_type
= expr
->base
->ctype
;
1271 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1272 a
->array_size
= expr
->base
->ctype
->array_size
;
1274 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1276 e0
->ctype
= &lazy_ptr_ctype
;
1278 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1281 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1283 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1285 e2
->right
= expr
->base
;
1287 e2
->ctype
= expr
->base
->ctype
;
1289 if (expr
->r_bitpos
) {
1290 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1293 e3
->right
= alloc_const_expression(expr
->pos
,
1294 expr
->r_bitpos
>> 3);
1295 e3
->ctype
= &lazy_ptr_ctype
;
1300 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1303 e4
->ctype
= &lazy_ptr_ctype
;
1306 expr
->type
= EXPR_PREOP
;
1310 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1311 warning(expr
->pos
, "strange non-value function or array");
1314 *expr
= *expr
->unop
;
1315 ctype
= create_pointer(expr
, ctype
, 1);
1316 expr
->ctype
= ctype
;
1323 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1325 struct expression
*op
= expr
->unop
;
1326 struct symbol
*ctype
;
1328 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1329 warning(expr
->pos
, "not addressable");
1335 if (expr
->type
== EXPR_SYMBOL
) {
1336 struct symbol
*sym
= expr
->symbol
;
1337 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1341 * symbol expression evaluation is lazy about the type
1342 * of the sub-expression, so we may have to generate
1343 * the type here if so..
1345 if (expr
->ctype
== &lazy_ptr_ctype
) {
1346 ctype
= create_pointer(expr
, ctype
, 0);
1347 expr
->ctype
= ctype
;
1353 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1355 struct expression
*op
= expr
->unop
;
1356 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1358 /* Simplify: *&(expr) => (expr) */
1359 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1364 /* Dereferencing a node drops all the node information. */
1365 if (ctype
->type
== SYM_NODE
)
1366 ctype
= ctype
->ctype
.base_type
;
1368 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1369 target
= ctype
->ctype
.base_type
;
1371 switch (ctype
->type
) {
1373 warning(expr
->pos
, "cannot derefence this type");
1376 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1377 merge_type(node
, ctype
);
1381 if (!lvalue_expression(op
)) {
1382 warning(op
->pos
, "non-lvalue array??");
1386 /* Do the implied "addressof" on the array */
1390 * When an array is dereferenced, we need to pick
1391 * up the attributes of the original node too..
1393 merge_type(node
, op
->ctype
);
1394 merge_type(node
, ctype
);
1398 node
->bit_size
= target
->bit_size
;
1399 node
->array_size
= target
->array_size
;
1406 * Unary post-ops: x++ and x--
1408 static struct symbol
*evaluate_postop(struct expression
*expr
)
1410 struct expression
*op
= expr
->unop
;
1411 struct symbol
*ctype
= op
->ctype
;
1413 if (!lvalue_expression(expr
->unop
)) {
1414 warning(expr
->pos
, "need lvalue expression for ++/--");
1417 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1418 warning(expr
->pos
, "bad operation on restricted");
1422 evaluate_assign_to(op
, ctype
);
1424 expr
->ctype
= ctype
;
1426 if (is_ptr_type(ctype
))
1427 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1432 static struct symbol
*evaluate_sign(struct expression
*expr
)
1434 struct symbol
*ctype
= expr
->unop
->ctype
;
1435 if (is_int_type(ctype
)) {
1436 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1437 expr
->unop
= cast_to(expr
->unop
, rtype
);
1439 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1440 /* no conversions needed */
1441 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1442 /* no conversions needed */
1444 return bad_expr_type(expr
);
1446 if (expr
->op
== '+')
1447 *expr
= *expr
->unop
;
1448 expr
->ctype
= ctype
;
1452 static struct symbol
*evaluate_preop(struct expression
*expr
)
1454 struct symbol
*ctype
= expr
->unop
->ctype
;
1458 *expr
= *expr
->unop
;
1464 return evaluate_sign(expr
);
1467 return evaluate_dereference(expr
);
1470 return evaluate_addressof(expr
);
1472 case SPECIAL_INCREMENT
:
1473 case SPECIAL_DECREMENT
:
1475 * From a type evaluation standpoint the pre-ops are
1476 * the same as the postops
1478 return evaluate_postop(expr
);
1481 if (is_safe_type(ctype
))
1482 warning(expr
->pos
, "testing a 'safe expression'");
1483 if (is_float_type(ctype
)) {
1484 struct expression
*arg
= expr
->unop
;
1485 expr
->type
= EXPR_BINOP
;
1486 expr
->op
= SPECIAL_EQUAL
;
1488 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1489 expr
->right
->ctype
= ctype
;
1490 expr
->right
->fvalue
= 0;
1492 ctype
= &bool_ctype
;
1498 expr
->ctype
= ctype
;
1502 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1504 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1505 struct ptr_list
*list
= head
;
1511 for (i
= 0; i
< list
->nr
; i
++) {
1512 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1514 if (sym
->ident
!= ident
)
1516 *offset
= sym
->offset
;
1519 struct symbol
*ctype
= sym
->ctype
.base_type
;
1523 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1525 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1528 *offset
+= sym
->offset
;
1532 } while ((list
= list
->next
) != head
);
1536 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1538 struct expression
*add
;
1541 * Create a new add-expression
1543 * NOTE! Even if we just add zero, we need a new node
1544 * for the member pointer, since it has a different
1545 * type than the original pointer. We could make that
1546 * be just a cast, but the fact is, a node is a node,
1547 * so we might as well just do the "add zero" here.
1549 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1552 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1553 add
->right
->ctype
= &int_ctype
;
1554 add
->right
->value
= offset
;
1557 * The ctype of the pointer will be lazily evaluated if
1558 * we ever take the address of this member dereference..
1560 add
->ctype
= &lazy_ptr_ctype
;
1564 /* structure/union dereference */
1565 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1568 struct symbol
*ctype
, *member
;
1569 struct expression
*deref
= expr
->deref
, *add
;
1570 struct ident
*ident
= expr
->member
;
1574 if (!evaluate_expression(deref
))
1577 warning(expr
->pos
, "bad member name");
1581 ctype
= deref
->ctype
;
1582 address_space
= ctype
->ctype
.as
;
1583 mod
= ctype
->ctype
.modifiers
;
1584 if (ctype
->type
== SYM_NODE
) {
1585 ctype
= ctype
->ctype
.base_type
;
1586 address_space
|= ctype
->ctype
.as
;
1587 mod
|= ctype
->ctype
.modifiers
;
1589 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1590 warning(expr
->pos
, "expected structure or union");
1594 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1596 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1597 const char *name
= "<unnamed>";
1600 name
= ctype
->ident
->name
;
1601 namelen
= ctype
->ident
->len
;
1603 warning(expr
->pos
, "no member '%s' in %s %.*s",
1604 show_ident(ident
), type
, namelen
, name
);
1609 * The member needs to take on the address space and modifiers of
1610 * the "parent" type.
1612 member
= convert_to_as_mod(member
, address_space
, mod
);
1613 ctype
= get_base_type(member
);
1615 if (!lvalue_expression(deref
)) {
1616 if (deref
->type
!= EXPR_SLICE
) {
1620 expr
->base
= deref
->base
;
1621 expr
->r_bitpos
= deref
->r_bitpos
;
1623 expr
->r_bitpos
+= offset
<< 3;
1624 expr
->type
= EXPR_SLICE
;
1625 expr
->r_nrbits
= member
->bit_size
;
1626 expr
->r_bitpos
+= member
->bit_offset
;
1627 expr
->ctype
= member
;
1631 deref
= deref
->unop
;
1632 expr
->deref
= deref
;
1634 add
= evaluate_offset(deref
, offset
);
1635 expr
->type
= EXPR_PREOP
;
1639 expr
->ctype
= member
;
1643 static int is_promoted(struct expression
*expr
)
1646 switch (expr
->type
) {
1649 case EXPR_CONDITIONAL
:
1673 static struct symbol
*evaluate_cast(struct expression
*);
1675 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1677 struct symbol
*sym
= expr
->cast_type
;
1679 sym
= evaluate_expression(expr
->cast_expression
);
1683 * Expressions of restricted types will possibly get
1684 * promoted - check that here
1686 if (is_restricted_type(sym
)) {
1687 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1691 examine_symbol_type(sym
);
1692 if (is_bitfield_type(sym
)) {
1693 warning(expr
->pos
, "trying to examine bitfield type");
1699 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1701 struct symbol
*type
;
1704 type
= evaluate_type_information(expr
);
1708 size
= type
->bit_size
;
1709 if ((size
< 0) || (size
& 7))
1710 warning(expr
->pos
, "cannot size expression");
1711 expr
->type
= EXPR_VALUE
;
1712 expr
->value
= size
>> 3;
1713 expr
->ctype
= size_t_ctype
;
1714 return size_t_ctype
;
1717 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1719 struct symbol
*type
;
1722 type
= evaluate_type_information(expr
);
1726 if (type
->type
== SYM_NODE
)
1727 type
= type
->ctype
.base_type
;
1730 switch (type
->type
) {
1734 type
= get_base_type(type
);
1738 warning(expr
->pos
, "expected pointer expression");
1741 size
= type
->bit_size
;
1744 expr
->type
= EXPR_VALUE
;
1745 expr
->value
= size
>> 3;
1746 expr
->ctype
= size_t_ctype
;
1747 return size_t_ctype
;
1750 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1752 struct symbol
*type
;
1754 type
= evaluate_type_information(expr
);
1758 expr
->type
= EXPR_VALUE
;
1759 expr
->value
= type
->ctype
.alignment
;
1760 expr
->ctype
= size_t_ctype
;
1761 return size_t_ctype
;
1764 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1766 struct expression
*expr
;
1767 struct symbol_list
*argument_types
= fn
->arguments
;
1768 struct symbol
*argtype
;
1771 PREPARE_PTR_LIST(argument_types
, argtype
);
1772 FOR_EACH_PTR (head
, expr
) {
1773 struct expression
**p
= THIS_ADDRESS(expr
);
1774 struct symbol
*ctype
, *target
;
1775 ctype
= evaluate_expression(expr
);
1780 ctype
= degenerate(expr
);
1783 if (!target
&& ctype
->bit_size
< bits_in_int
)
1784 target
= &int_ctype
;
1786 static char where
[30];
1787 examine_symbol_type(target
);
1788 sprintf(where
, "argument %d", i
);
1789 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1793 NEXT_PTR_LIST(argtype
);
1794 } END_FOR_EACH_PTR(expr
);
1795 FINISH_PTR_LIST(argtype
);
1799 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1801 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1803 struct expression
*entry
= *ep
;
1804 struct expression
**parent
, *reuse
= NULL
;
1805 unsigned long offset
;
1807 unsigned long from
, to
;
1808 int accept_string
= is_byte_type(ctype
);
1813 if (entry
->type
== EXPR_INDEX
) {
1814 from
= entry
->idx_from
;
1815 to
= entry
->idx_to
+1;
1816 parent
= &entry
->idx_expression
;
1818 entry
= entry
->idx_expression
;
1821 offset
= from
* (ctype
->bit_size
>>3);
1823 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1824 reuse
->type
= EXPR_POS
;
1825 reuse
->ctype
= ctype
;
1826 reuse
->init_offset
= offset
;
1827 reuse
->init_nr
= to
- from
;
1828 reuse
->init_expr
= entry
;
1829 parent
= &reuse
->init_expr
;
1834 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1835 sym
= evaluate_expression(entry
);
1836 to
= from
+ get_expression_value(sym
->array_size
);
1838 evaluate_initializer(ctype
, parent
);
1843 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1845 struct expression
*entry
;
1848 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1849 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1850 } END_FOR_EACH_PTR(entry
);
1853 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1854 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1856 if (expression_list_size(expr
->expr_list
) != 1) {
1857 warning(expr
->pos
, "unexpected compound initializer");
1860 evaluate_array_initializer(ctype
, expr
);
1864 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1868 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1869 if (sym
->ident
== ident
)
1871 } END_FOR_EACH_PTR(sym
);
1875 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1877 struct expression
*entry
= *ep
;
1878 struct expression
**parent
;
1879 struct expression
*reuse
= NULL
;
1880 unsigned long offset
;
1883 error(entry
->pos
, "unknown named initializer");
1887 if (entry
->type
== EXPR_IDENTIFIER
) {
1889 entry
= entry
->ident_expression
;
1893 offset
= sym
->offset
;
1896 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1897 reuse
->type
= EXPR_POS
;
1899 reuse
->init_offset
= offset
;
1901 reuse
->init_expr
= entry
;
1902 parent
= &reuse
->init_expr
;
1906 evaluate_initializer(sym
, parent
);
1910 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1912 struct expression
*entry
;
1915 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1916 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1917 if (entry
->type
== EXPR_IDENTIFIER
) {
1918 struct ident
*ident
= entry
->expr_ident
;
1919 /* We special-case the "already right place" case */
1920 if (!sym
|| sym
->ident
!= ident
) {
1921 RESET_PTR_LIST(sym
);
1925 if (sym
->ident
== ident
)
1931 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1934 } END_FOR_EACH_PTR(entry
);
1935 FINISH_PTR_LIST(sym
);
1939 * Initializers are kind of like assignments. Except
1940 * they can be a hell of a lot more complex.
1942 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1944 struct expression
*expr
= *ep
;
1947 * Simple non-structure/array initializers are the simple
1948 * case, and look (and parse) largely like assignments.
1950 switch (expr
->type
) {
1952 int is_string
= expr
->type
== EXPR_STRING
;
1953 struct symbol
*rtype
= evaluate_expression(expr
);
1957 * char array[] = "string"
1958 * should _not_ degenerate.
1960 if (!is_string
|| !is_string_type(ctype
))
1961 rtype
= degenerate(expr
);
1962 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1967 case EXPR_INITIALIZER
:
1968 expr
->ctype
= ctype
;
1969 if (ctype
->type
== SYM_NODE
)
1970 ctype
= ctype
->ctype
.base_type
;
1972 switch (ctype
->type
) {
1975 evaluate_array_initializer(get_base_type(ctype
), expr
);
1978 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1981 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1984 evaluate_scalar_initializer(ctype
, expr
);
1988 case EXPR_IDENTIFIER
:
1989 if (ctype
->type
== SYM_NODE
)
1990 ctype
= ctype
->ctype
.base_type
;
1991 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1992 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1996 evaluate_one_struct_initializer(ctype
, ep
,
1997 find_struct_ident(ctype
, expr
->expr_ident
));
2001 if (ctype
->type
== SYM_NODE
)
2002 ctype
= ctype
->ctype
.base_type
;
2003 if (ctype
->type
!= SYM_ARRAY
) {
2004 error(expr
->pos
, "expected array");
2007 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2012 * An EXPR_POS expression has already been evaluated, and we don't
2013 * need to do anything more
2019 static int get_as(struct symbol
*sym
)
2027 mod
= sym
->ctype
.modifiers
;
2028 if (sym
->type
== SYM_NODE
) {
2029 sym
= sym
->ctype
.base_type
;
2030 as
|= sym
->ctype
.as
;
2031 mod
|= sym
->ctype
.modifiers
;
2035 * At least for now, allow casting to a "unsigned long".
2036 * That's how we do things like pointer arithmetic and
2037 * store pointers to registers.
2039 if (sym
== &ulong_ctype
)
2042 if (sym
&& sym
->type
== SYM_PTR
) {
2043 sym
= get_base_type(sym
);
2044 as
|= sym
->ctype
.as
;
2045 mod
|= sym
->ctype
.modifiers
;
2047 if (mod
& MOD_FORCE
)
2052 static struct symbol
*evaluate_cast(struct expression
*expr
)
2054 struct expression
*target
= expr
->cast_expression
;
2055 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2061 expr
->ctype
= ctype
;
2062 expr
->cast_type
= ctype
;
2065 * Special case: a cast can be followed by an
2066 * initializer, in which case we need to pass
2067 * the type value down to that initializer rather
2068 * than trying to evaluate it as an expression
2070 * A more complex case is when the initializer is
2071 * dereferenced as part of a post-fix expression.
2072 * We need to produce an expression that can be dereferenced.
2074 if (target
->type
== EXPR_INITIALIZER
) {
2075 struct symbol
*sym
= expr
->cast_type
;
2076 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2078 sym
->initializer
= expr
->cast_expression
;
2079 evaluate_symbol(sym
);
2081 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2084 expr
->type
= EXPR_PREOP
;
2092 evaluate_expression(target
);
2096 * You can always throw a value away by casting to
2097 * "void" - that's an implicit "force". Note that
2098 * the same is _not_ true of "void *".
2100 if (ctype
== &void_ctype
)
2104 if (type
== SYM_NODE
) {
2105 type
= ctype
->ctype
.base_type
->type
;
2106 if (ctype
->ctype
.base_type
== &void_ctype
)
2109 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2110 warning(expr
->pos
, "cast to non-scalar");
2112 if (!target
->ctype
) {
2113 warning(expr
->pos
, "cast from unknown type");
2117 type
= target
->ctype
->type
;
2118 if (type
== SYM_NODE
)
2119 type
= target
->ctype
->ctype
.base_type
->type
;
2120 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2121 warning(expr
->pos
, "cast from non-scalar");
2123 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2124 warning(expr
->pos
, "cast removes address space of expression");
2126 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2127 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2128 if (t1
->type
== SYM_NODE
)
2129 t1
= t1
->ctype
.base_type
;
2130 if (t2
->type
== SYM_NODE
)
2131 t2
= t2
->ctype
.base_type
;
2133 if (t1
->type
== SYM_RESTRICT
)
2134 warning(expr
->pos
, "cast to restricted type");
2135 if (t2
->type
== SYM_RESTRICT
)
2136 warning(expr
->pos
, "cast from restricted type");
2141 * Casts of constant values are special: they
2142 * can be NULL, and thus need to be simplified
2145 if (target
->type
== EXPR_VALUE
)
2146 cast_value(expr
, ctype
, target
, target
->ctype
);
2153 * Evaluate a call expression with a symbol. This
2154 * should expand inline functions, and evaluate
2157 static int evaluate_symbol_call(struct expression
*expr
)
2159 struct expression
*fn
= expr
->fn
;
2160 struct symbol
*ctype
= fn
->ctype
;
2162 if (fn
->type
!= EXPR_PREOP
)
2165 if (ctype
->op
&& ctype
->op
->evaluate
)
2166 return ctype
->op
->evaluate(expr
);
2168 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2170 struct symbol
*curr
= current_fn
;
2171 current_fn
= ctype
->ctype
.base_type
;
2172 examine_fn_arguments(current_fn
);
2174 ret
= inline_function(expr
, ctype
);
2176 /* restore the old function */
2184 static struct symbol
*evaluate_call(struct expression
*expr
)
2187 struct symbol
*ctype
, *sym
;
2188 struct expression
*fn
= expr
->fn
;
2189 struct expression_list
*arglist
= expr
->args
;
2191 if (!evaluate_expression(fn
))
2193 sym
= ctype
= fn
->ctype
;
2194 if (ctype
->type
== SYM_NODE
)
2195 ctype
= ctype
->ctype
.base_type
;
2196 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2197 ctype
= get_base_type(ctype
);
2198 if (!evaluate_arguments(sym
, ctype
, arglist
))
2200 if (ctype
->type
!= SYM_FN
) {
2201 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2204 args
= expression_list_size(expr
->args
);
2205 fnargs
= symbol_list_size(ctype
->arguments
);
2207 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2208 if (args
> fnargs
&& !ctype
->variadic
)
2209 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2210 if (sym
->type
== SYM_NODE
) {
2211 if (evaluate_symbol_call(expr
))
2214 expr
->ctype
= ctype
->ctype
.base_type
;
2218 struct symbol
*evaluate_expression(struct expression
*expr
)
2225 switch (expr
->type
) {
2228 warning(expr
->pos
, "value expression without a type");
2231 return evaluate_string(expr
);
2233 return evaluate_symbol_expression(expr
);
2235 if (!evaluate_expression(expr
->left
))
2237 if (!evaluate_expression(expr
->right
))
2239 return evaluate_binop(expr
);
2241 return evaluate_logical(expr
);
2243 evaluate_expression(expr
->left
);
2244 if (!evaluate_expression(expr
->right
))
2246 return evaluate_comma(expr
);
2248 if (!evaluate_expression(expr
->left
))
2250 if (!evaluate_expression(expr
->right
))
2252 return evaluate_compare(expr
);
2253 case EXPR_ASSIGNMENT
:
2254 if (!evaluate_expression(expr
->left
))
2256 if (!evaluate_expression(expr
->right
))
2258 return evaluate_assignment(expr
);
2260 if (!evaluate_expression(expr
->unop
))
2262 return evaluate_preop(expr
);
2264 if (!evaluate_expression(expr
->unop
))
2266 return evaluate_postop(expr
);
2268 case EXPR_IMPLIED_CAST
:
2269 return evaluate_cast(expr
);
2271 return evaluate_sizeof(expr
);
2272 case EXPR_PTRSIZEOF
:
2273 return evaluate_ptrsizeof(expr
);
2275 return evaluate_alignof(expr
);
2277 return evaluate_member_dereference(expr
);
2279 return evaluate_call(expr
);
2281 case EXPR_CONDITIONAL
:
2282 return evaluate_conditional_expression(expr
);
2283 case EXPR_STATEMENT
:
2284 expr
->ctype
= evaluate_statement(expr
->statement
);
2288 expr
->ctype
= &ptr_ctype
;
2292 /* Evaluate the type of the symbol .. */
2293 evaluate_symbol(expr
->symbol
);
2294 /* .. but the type of the _expression_ is a "type" */
2295 expr
->ctype
= &type_ctype
;
2298 /* These can not exist as stand-alone expressions */
2299 case EXPR_INITIALIZER
:
2300 case EXPR_IDENTIFIER
:
2303 warning(expr
->pos
, "internal front-end error: initializer in expression");
2306 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2312 static void check_duplicates(struct symbol
*sym
)
2315 struct symbol
*next
= sym
;
2317 while ((next
= next
->same_symbol
) != NULL
) {
2318 const char *typediff
;
2319 evaluate_symbol(next
);
2321 typediff
= type_difference(sym
, next
, 0, 0);
2323 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2324 show_ident(sym
->ident
),
2325 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2330 unsigned long mod
= sym
->ctype
.modifiers
;
2331 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2333 if (!(mod
& MOD_TOPLEVEL
))
2335 if (sym
->ident
== &main_ident
)
2337 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2341 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2343 struct symbol
*base_type
;
2348 sym
= examine_symbol_type(sym
);
2349 base_type
= get_base_type(sym
);
2353 /* Evaluate the initializers */
2354 if (sym
->initializer
)
2355 evaluate_initializer(sym
, &sym
->initializer
);
2357 /* And finally, evaluate the body of the symbol too */
2358 if (base_type
->type
== SYM_FN
) {
2359 struct symbol
*curr
= current_fn
;
2361 current_fn
= base_type
;
2363 examine_fn_arguments(base_type
);
2364 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2366 if (base_type
->stmt
)
2367 evaluate_statement(base_type
->stmt
);
2375 void evaluate_symbol_list(struct symbol_list
*list
)
2379 FOR_EACH_PTR(list
, sym
) {
2380 check_duplicates(sym
);
2381 evaluate_symbol(sym
);
2382 } END_FOR_EACH_PTR(sym
);
2385 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2387 struct expression
*expr
= stmt
->expression
;
2388 struct symbol
*ctype
, *fntype
;
2390 evaluate_expression(expr
);
2391 ctype
= degenerate(expr
);
2392 fntype
= current_fn
->ctype
.base_type
;
2393 if (!fntype
|| fntype
== &void_ctype
) {
2394 if (expr
&& ctype
!= &void_ctype
)
2395 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2400 warning(stmt
->pos
, "return with no return value");
2405 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2409 static void evaluate_if_statement(struct statement
*stmt
)
2411 if (!stmt
->if_conditional
)
2414 evaluate_conditional(stmt
->if_conditional
, 0);
2415 evaluate_statement(stmt
->if_true
);
2416 evaluate_statement(stmt
->if_false
);
2419 static void evaluate_iterator(struct statement
*stmt
)
2421 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2422 evaluate_conditional(stmt
->iterator_post_condition
,1);
2423 evaluate_statement(stmt
->iterator_pre_statement
);
2424 evaluate_statement(stmt
->iterator_statement
);
2425 evaluate_statement(stmt
->iterator_post_statement
);
2428 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2430 switch (*constraint
) {
2431 case '=': /* Assignment */
2432 case '+': /* Update */
2435 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2439 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2441 switch (*constraint
) {
2442 case '=': /* Assignment */
2443 case '+': /* Update */
2444 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2448 static void evaluate_asm_statement(struct statement
*stmt
)
2450 struct expression
*expr
;
2453 expr
= stmt
->asm_string
;
2454 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2455 warning(stmt
->pos
, "need constant string for inline asm");
2460 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2461 struct ident
*ident
;
2464 case 0: /* Identifier */
2466 ident
= (struct ident
*)expr
;
2469 case 1: /* Constraint */
2471 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2472 warning(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2473 *THIS_ADDRESS(expr
) = NULL
;
2476 verify_output_constraint(expr
, expr
->string
->data
);
2479 case 2: /* Expression */
2481 if (!evaluate_expression(expr
))
2483 if (!lvalue_expression(expr
))
2484 warning(expr
->pos
, "asm output is not an lvalue");
2485 evaluate_assign_to(expr
, expr
->ctype
);
2488 } END_FOR_EACH_PTR(expr
);
2491 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2492 struct ident
*ident
;
2495 case 0: /* Identifier */
2497 ident
= (struct ident
*)expr
;
2500 case 1: /* Constraint */
2502 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2503 warning(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2504 *THIS_ADDRESS(expr
) = NULL
;
2507 verify_input_constraint(expr
, expr
->string
->data
);
2510 case 2: /* Expression */
2512 if (!evaluate_expression(expr
))
2516 } END_FOR_EACH_PTR(expr
);
2518 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2520 warning(stmt
->pos
, "bad asm output");
2523 if (expr
->type
== EXPR_STRING
)
2525 warning(expr
->pos
, "asm clobber is not a string");
2526 } END_FOR_EACH_PTR(expr
);
2529 struct symbol
*evaluate_statement(struct statement
*stmt
)
2534 switch (stmt
->type
) {
2536 return evaluate_return_expression(stmt
);
2538 case STMT_EXPRESSION
:
2539 if (!evaluate_expression(stmt
->expression
))
2541 return degenerate(stmt
->expression
);
2543 case STMT_COMPOUND
: {
2544 struct statement
*s
;
2545 struct symbol
*type
= NULL
;
2548 /* Evaluate each symbol in the compound statement */
2549 FOR_EACH_PTR(stmt
->syms
, sym
) {
2550 evaluate_symbol(sym
);
2551 } END_FOR_EACH_PTR(sym
);
2552 evaluate_symbol(stmt
->ret
);
2555 * Then, evaluate each statement, making the type of the
2556 * compound statement be the type of the last statement
2559 FOR_EACH_PTR(stmt
->stmts
, s
) {
2560 type
= evaluate_statement(s
);
2561 } END_FOR_EACH_PTR(s
);
2567 evaluate_if_statement(stmt
);
2570 evaluate_iterator(stmt
);
2573 evaluate_expression(stmt
->switch_expression
);
2574 evaluate_statement(stmt
->switch_statement
);
2577 evaluate_expression(stmt
->case_expression
);
2578 evaluate_expression(stmt
->case_to
);
2579 evaluate_statement(stmt
->case_statement
);
2582 return evaluate_statement(stmt
->label_statement
);
2584 evaluate_expression(stmt
->goto_expression
);
2589 evaluate_asm_statement(stmt
);
2592 evaluate_expression(stmt
->expression
);
2595 evaluate_expression(stmt
->range_expression
);
2596 evaluate_expression(stmt
->range_low
);
2597 evaluate_expression(stmt
->range_high
);