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
= sym
->ctype
.base_type
;
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
, difmod
, oldas
, newas
;
200 old
= base_type(old
, &oldmod
, &oldas
);
201 new = base_type(new, &newmod
, &newas
);
202 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
203 if (old
== new && oldas
== newas
&& !difmod
)
205 if ((oldmod
| newmod
) & MOD_NOCAST
) {
206 const char *tofrom
= "to/from";
207 if (!(newmod
& MOD_NOCAST
))
209 if (!(oldmod
& MOD_NOCAST
))
211 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
217 * This gets called for implicit casts in assignments and
218 * integer promotion. We often want to try to move the
219 * cast down, because the ops involved may have been
220 * implicitly cast up, and we can get rid of the casts
223 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
225 struct expression
*expr
;
227 if (is_same_type(old
, type
))
231 * See if we can simplify the op. Move the cast down.
235 if (old
->op
== '~') {
237 old
->unop
= cast_to(old
->unop
, type
);
242 case EXPR_IMPLIED_CAST
:
243 if (old
->ctype
->bit_size
>= type
->bit_size
) {
244 struct expression
*orig
= old
->cast_expression
;
245 if (same_cast_type(orig
->ctype
, type
))
247 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
249 old
->cast_type
= type
;
259 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
261 expr
->cast_type
= type
;
262 expr
->cast_expression
= old
;
266 static int is_type_type(struct symbol
*type
)
268 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
271 int is_ptr_type(struct symbol
*type
)
273 if (type
->type
== SYM_NODE
)
274 type
= type
->ctype
.base_type
;
275 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
278 static inline int is_float_type(struct symbol
*type
)
280 if (type
->type
== SYM_NODE
)
281 type
= type
->ctype
.base_type
;
282 return type
->ctype
.base_type
== &fp_type
;
285 static inline int is_byte_type(struct symbol
*type
)
287 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
290 static inline int is_string_type(struct symbol
*type
)
292 if (type
->type
== SYM_NODE
)
293 type
= type
->ctype
.base_type
;
294 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
297 static struct symbol
*bad_expr_type(struct expression
*expr
)
299 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
300 switch (expr
->type
) {
303 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
304 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
308 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
317 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
319 struct expression
*left
= *lp
, *right
= *rp
;
320 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
322 if (ltype
->type
== SYM_NODE
)
323 ltype
= ltype
->ctype
.base_type
;
324 if (rtype
->type
== SYM_NODE
)
325 rtype
= rtype
->ctype
.base_type
;
326 if (is_float_type(ltype
)) {
327 if (is_int_type(rtype
))
329 if (is_float_type(rtype
)) {
330 unsigned long lmod
= ltype
->ctype
.modifiers
;
331 unsigned long rmod
= rtype
->ctype
.modifiers
;
332 lmod
&= MOD_LONG
| MOD_LONGLONG
;
333 rmod
&= MOD_LONG
| MOD_LONGLONG
;
343 if (!is_float_type(rtype
) || !is_int_type(ltype
))
346 *lp
= cast_to(left
, rtype
);
349 *rp
= cast_to(right
, ltype
);
353 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
355 struct expression
*left
= *lp
, *right
= *rp
;
356 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
358 if (ltype
->type
== SYM_NODE
)
359 ltype
= ltype
->ctype
.base_type
;
360 if (rtype
->type
== SYM_NODE
)
361 rtype
= rtype
->ctype
.base_type
;
362 if (is_int_type(ltype
) && is_int_type(rtype
)) {
363 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
365 *lp
= cast_to(left
, ctype
);
366 *rp
= cast_to(right
, ctype
);
372 static int restricted_value(struct expression
*v
, struct symbol
*type
)
374 if (v
->type
!= EXPR_VALUE
)
381 static int restricted_binop(int op
, struct symbol
*type
)
390 case SPECIAL_NOTEQUAL
:
391 case SPECIAL_AND_ASSIGN
:
392 case SPECIAL_OR_ASSIGN
:
393 case SPECIAL_XOR_ASSIGN
:
400 static int restricted_unop(int op
, struct symbol
*type
)
402 if (op
== '~' && type
->bit_size
>= bits_in_int
)
409 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
411 struct expression
*left
= *lp
, *right
= *rp
;
412 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
413 struct symbol
*type
= NULL
;
415 if (ltype
->type
== SYM_NODE
)
416 ltype
= ltype
->ctype
.base_type
;
417 if (ltype
->type
== SYM_ENUM
)
418 ltype
= ltype
->ctype
.base_type
;
419 if (rtype
->type
== SYM_NODE
)
420 rtype
= rtype
->ctype
.base_type
;
421 if (rtype
->type
== SYM_ENUM
)
422 rtype
= rtype
->ctype
.base_type
;
423 if (is_restricted_type(ltype
)) {
424 if (is_restricted_type(rtype
)) {
428 if (!restricted_value(right
, ltype
))
431 } else if (is_restricted_type(rtype
)) {
432 if (!restricted_value(left
, rtype
))
437 if (restricted_binop(op
, type
))
442 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
444 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
445 if (!ctype
&& float_ok
)
446 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
448 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
453 return bad_expr_type(expr
);
456 static inline int lvalue_expression(struct expression
*expr
)
458 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
461 static int ptr_object_size(struct symbol
*ptr_type
)
463 if (ptr_type
->type
== SYM_NODE
)
464 ptr_type
= ptr_type
->ctype
.base_type
;
465 if (ptr_type
->type
== SYM_PTR
)
466 ptr_type
= ptr_type
->ctype
.base_type
;
467 return ptr_type
->bit_size
;
470 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
472 struct expression
*i
= *ip
;
473 struct symbol
*ptr_type
= ctype
;
476 if (ptr_type
->type
== SYM_NODE
)
477 ptr_type
= ptr_type
->ctype
.base_type
;
479 if (!is_int_type(i
->ctype
))
480 return bad_expr_type(expr
);
482 examine_symbol_type(ctype
);
484 if (!ctype
->ctype
.base_type
) {
485 warning(expr
->pos
, "missing type information");
489 /* Get the size of whatever the pointer points to */
490 bit_size
= ptr_object_size(ctype
);
492 if (bit_size
> bits_in_char
) {
493 int multiply
= bit_size
>> 3;
494 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
496 if (i
->type
== EXPR_VALUE
) {
497 val
->value
= i
->value
* multiply
;
498 val
->ctype
= size_t_ctype
;
501 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
503 val
->ctype
= size_t_ctype
;
504 val
->value
= bit_size
>> 3;
507 mul
->ctype
= size_t_ctype
;
519 static struct symbol
*evaluate_add(struct expression
*expr
)
521 struct expression
*left
= expr
->left
, *right
= expr
->right
;
522 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
524 if (is_ptr_type(ltype
))
525 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
527 if (is_ptr_type(rtype
))
528 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
530 return evaluate_arith(expr
, 1);
533 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
534 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
537 unsigned long mod1
, mod2
, diff
;
538 unsigned long as1
, as2
;
540 struct symbol
*base1
, *base2
;
542 if (target
== source
)
544 if (!target
|| !source
)
545 return "different types";
547 * Peel of per-node information.
548 * FIXME! Check alignment and context too here!
550 mod1
= target
->ctype
.modifiers
;
551 as1
= target
->ctype
.as
;
552 mod2
= source
->ctype
.modifiers
;
553 as2
= source
->ctype
.as
;
554 if (target
->type
== SYM_NODE
) {
555 target
= target
->ctype
.base_type
;
558 if (target
->type
== SYM_PTR
) {
562 mod1
|= target
->ctype
.modifiers
;
563 as1
|= target
->ctype
.as
;
565 if (source
->type
== SYM_NODE
) {
566 source
= source
->ctype
.base_type
;
569 if (source
->type
== SYM_PTR
) {
573 mod2
|= source
->ctype
.modifiers
;
574 as2
|= source
->ctype
.as
;
576 if (target
->type
== SYM_ENUM
) {
577 target
= target
->ctype
.base_type
;
581 if (source
->type
== SYM_ENUM
) {
582 source
= source
->ctype
.base_type
;
587 if (target
== source
)
589 if (!target
|| !source
)
590 return "different types";
592 type1
= target
->type
;
593 base1
= target
->ctype
.base_type
;
595 type2
= source
->type
;
596 base2
= source
->ctype
.base_type
;
599 * Pointers to functions compare as the function itself
601 if (type1
== SYM_PTR
&& base1
) {
602 switch (base1
->type
) {
606 base1
= base1
->ctype
.base_type
;
611 if (type2
== SYM_PTR
&& base2
) {
612 switch (base2
->type
) {
616 base2
= base2
->ctype
.base_type
;
622 /* Arrays degenerate to pointers for type comparisons */
623 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
624 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
626 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
627 return "different base types";
629 /* Must be same address space to be comparable */
631 return "different address spaces";
633 /* Ignore differences in storage types or addressability */
634 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
635 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
638 return "different type sizes";
639 if (diff
& ~MOD_SIGNEDNESS
)
640 return "different modifiers";
642 /* Differs in signedness only.. */
645 * Warn if both are explicitly signed ("unsigned" is obvously
646 * always explicit, and since we know one of them has to be
647 * unsigned, we check if the signed one was explicit).
649 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
650 return "different explicit signedness";
653 * "char" matches both "unsigned char" and "signed char",
654 * so if the explicit test didn't trigger, then we should
655 * not warn about a char.
657 if (!(mod1
& MOD_CHAR
))
658 return "different signedness";
662 if (type1
== SYM_FN
) {
664 struct symbol
*arg1
, *arg2
;
665 if (base1
->variadic
!= base2
->variadic
)
666 return "incompatible variadic arguments";
667 PREPARE_PTR_LIST(target
->arguments
, arg1
);
668 PREPARE_PTR_LIST(source
->arguments
, arg2
);
672 diff
= type_difference(arg1
, arg2
, 0, 0);
674 static char argdiff
[80];
675 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
684 FINISH_PTR_LIST(arg2
);
685 FINISH_PTR_LIST(arg1
);
694 static int is_null_ptr(struct expression
*expr
)
696 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
698 if (!is_ptr_type(expr
->ctype
))
699 warning(expr
->pos
, "Using plain integer as NULL pointer");
703 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
705 /* NULL expression? Just return the type of the "other side" */
714 * Ignore differences in "volatile" and "const"ness when
715 * subtracting pointers
717 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
719 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
721 const char *typediff
;
722 struct symbol
*ctype
;
723 struct symbol
*ltype
, *rtype
;
724 struct expression
*r
= *rp
;
726 ltype
= degenerate(l
);
727 rtype
= degenerate(r
);
730 * If it is an integer subtract: the ptr add case will do the
733 if (!is_ptr_type(rtype
))
734 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
737 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
739 ctype
= common_ptr_type(l
, r
);
741 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
745 examine_symbol_type(ctype
);
747 /* Figure out the base type we point to */
748 if (ctype
->type
== SYM_NODE
)
749 ctype
= ctype
->ctype
.base_type
;
750 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
751 warning(expr
->pos
, "subtraction of functions? Share your drugs");
754 ctype
= ctype
->ctype
.base_type
;
756 expr
->ctype
= ssize_t_ctype
;
757 if (ctype
->bit_size
> bits_in_char
) {
758 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
759 struct expression
*div
= expr
;
760 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
761 unsigned long value
= ctype
->bit_size
>> 3;
763 val
->ctype
= size_t_ctype
;
766 if (value
& (value
-1)) {
767 if (Wptr_subtraction_blows
)
768 warning(expr
->pos
, "potentially expensive pointer subtraction");
772 sub
->ctype
= ssize_t_ctype
;
781 return ssize_t_ctype
;
784 static struct symbol
*evaluate_sub(struct expression
*expr
)
786 struct expression
*left
= expr
->left
;
787 struct symbol
*ltype
= left
->ctype
;
789 if (is_ptr_type(ltype
))
790 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
792 return evaluate_arith(expr
, 1);
795 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
797 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
799 struct symbol
*ctype
;
804 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
805 warning(expr
->pos
, "assignment expression in conditional");
807 ctype
= evaluate_expression(expr
);
809 if (is_safe_type(ctype
))
810 warning(expr
->pos
, "testing a 'safe expression'");
816 static struct symbol
*evaluate_logical(struct expression
*expr
)
818 if (!evaluate_conditional(expr
->left
, 0))
820 if (!evaluate_conditional(expr
->right
, 0))
823 expr
->ctype
= &bool_ctype
;
827 static struct symbol
*evaluate_shift(struct expression
*expr
)
829 struct expression
*left
= expr
->left
, *right
= expr
->right
;
830 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
832 if (ltype
->type
== SYM_NODE
)
833 ltype
= ltype
->ctype
.base_type
;
834 if (rtype
->type
== SYM_NODE
)
835 rtype
= rtype
->ctype
.base_type
;
836 if (is_int_type(ltype
) && is_int_type(rtype
)) {
837 struct symbol
*ctype
= integer_promotion(ltype
);
838 expr
->left
= cast_to(expr
->left
, ctype
);
840 ctype
= integer_promotion(rtype
);
841 expr
->right
= cast_to(expr
->right
, ctype
);
844 return bad_expr_type(expr
);
847 static struct symbol
*evaluate_binop(struct expression
*expr
)
850 // addition can take ptr+int, fp and int
852 return evaluate_add(expr
);
854 // subtraction can take ptr-ptr, fp and int
856 return evaluate_sub(expr
);
858 // Arithmetic operations can take fp and int
860 return evaluate_arith(expr
, 1);
862 // shifts do integer promotions, but that's it.
863 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
864 return evaluate_shift(expr
);
866 // The rest are integer operations
867 // '%', '&', '^', '|'
869 return evaluate_arith(expr
, 0);
873 static struct symbol
*evaluate_comma(struct expression
*expr
)
875 expr
->ctype
= expr
->right
->ctype
;
879 static int modify_for_unsigned(int op
)
882 op
= SPECIAL_UNSIGNED_LT
;
884 op
= SPECIAL_UNSIGNED_GT
;
885 else if (op
== SPECIAL_LTE
)
886 op
= SPECIAL_UNSIGNED_LTE
;
887 else if (op
== SPECIAL_GTE
)
888 op
= SPECIAL_UNSIGNED_GTE
;
892 static struct symbol
*evaluate_compare(struct expression
*expr
)
894 struct expression
*left
= expr
->left
, *right
= expr
->right
;
895 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
896 struct symbol
*ctype
;
899 if (is_type_type(ltype
) && is_type_type(rtype
))
902 if (is_safe_type(ltype
) || is_safe_type(rtype
))
903 warning(expr
->pos
, "testing a 'safe expression'");
906 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
907 // FIXME! Check the types for compatibility
908 expr
->op
= modify_for_unsigned(expr
->op
);
912 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
914 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
915 expr
->op
= modify_for_unsigned(expr
->op
);
919 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
923 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
925 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
926 expr
->op
= modify_for_unsigned(expr
->op
);
933 expr
->ctype
= &bool_ctype
;
938 * FIXME!! This should do casts, array degeneration etc..
940 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
942 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
944 if (ltype
->type
== SYM_NODE
)
945 ltype
= ltype
->ctype
.base_type
;
947 if (rtype
->type
== SYM_NODE
)
948 rtype
= rtype
->ctype
.base_type
;
950 if (ltype
->type
== SYM_PTR
) {
951 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
955 if (rtype
->type
== SYM_PTR
) {
956 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
963 * NOTE! The degenerate case of "x ? : y", where we don't
964 * have a true case, this will possibly promote "x" to the
965 * same type as "y", and thus _change_ the conditional
966 * test in the expression. But since promotion is "safe"
967 * for testing, that's ok.
969 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
971 struct expression
**true;
972 struct symbol
*ctype
, *ltype
, *rtype
;
973 const char * typediff
;
975 if (!evaluate_conditional(expr
->conditional
, 0))
977 if (!evaluate_expression(expr
->cond_false
))
980 ctype
= degenerate(expr
->conditional
);
981 rtype
= degenerate(expr
->cond_false
);
983 true = &expr
->conditional
;
985 if (expr
->cond_true
) {
986 if (!evaluate_expression(expr
->cond_true
))
988 ltype
= degenerate(expr
->cond_true
);
989 true = &expr
->cond_true
;
992 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
995 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
998 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1001 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1005 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1008 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1012 expr
->ctype
= ctype
;
1016 /* FP assignments can not do modulo or bit operations */
1017 static int compatible_float_op(int op
)
1020 op
== SPECIAL_ADD_ASSIGN
||
1021 op
== SPECIAL_SUB_ASSIGN
||
1022 op
== SPECIAL_MUL_ASSIGN
||
1023 op
== SPECIAL_DIV_ASSIGN
;
1026 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1027 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1029 const char *typediff
;
1033 if (is_int_type(target
)) {
1034 if (is_int_type(source
))
1036 if (is_float_type(source
))
1038 } else if (is_float_type(target
)) {
1039 if (!compatible_float_op(op
)) {
1040 warning(expr
->pos
, "invalid assignment");
1043 if (is_int_type(source
))
1045 if (is_float_type(source
))
1047 } else if (is_restricted_type(target
)) {
1048 if (restricted_binop(op
, target
)) {
1049 warning(expr
->pos
, "bad restricted assignment");
1052 if (!restricted_value(*rp
, target
))
1054 } else if (is_ptr_type(target
)) {
1055 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1056 evaluate_ptr_add(expr
, target
, rp
);
1060 warning(expr
->pos
, "invalid pointer assignment");
1063 } else if (op
!= '=') {
1064 warning(expr
->pos
, "invalid assignment");
1068 /* It's ok if the target is more volatile or const than the source */
1069 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1073 /* Pointer destination? */
1075 target_as
= t
->ctype
.as
;
1076 if (t
->type
== SYM_NODE
) {
1077 t
= t
->ctype
.base_type
;
1078 target_as
|= t
->ctype
.as
;
1080 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1081 struct expression
*right
= *rp
;
1082 struct symbol
*s
= source
;
1085 // NULL pointer is always ok
1086 if (is_null_ptr(right
))
1089 /* "void *" matches anything as long as the address space is ok */
1090 source_as
= s
->ctype
.as
;
1091 if (s
->type
== SYM_NODE
) {
1092 s
= s
->ctype
.base_type
;
1093 source_as
|= s
->ctype
.as
;
1095 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1096 s
= s
->ctype
.base_type
;
1097 t
= t
->ctype
.base_type
;
1098 if (s
== &void_ctype
|| t
== &void_ctype
)
1103 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1104 info(expr
->pos
, " expected %s", show_typename(target
));
1105 info(expr
->pos
, " got %s", show_typename(source
));
1106 *rp
= cast_to(*rp
, target
);
1109 *rp
= cast_to(*rp
, target
);
1113 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1115 if (type
->ctype
.modifiers
& MOD_CONST
)
1116 warning(left
->pos
, "assignment to const expression");
1117 if (type
->type
== SYM_NODE
)
1118 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1121 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1123 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1124 struct expression
*where
= expr
;
1125 struct symbol
*ltype
, *rtype
;
1127 if (!lvalue_expression(left
)) {
1128 warning(expr
->pos
, "not an lvalue");
1132 ltype
= left
->ctype
;
1134 rtype
= degenerate(right
);
1136 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1139 evaluate_assign_to(left
, ltype
);
1141 expr
->ctype
= ltype
;
1145 static void examine_fn_arguments(struct symbol
*fn
)
1149 FOR_EACH_PTR(fn
->arguments
, s
) {
1150 struct symbol
*arg
= evaluate_symbol(s
);
1151 /* Array/function arguments silently degenerate into pointers */
1157 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1158 if (arg
->type
== SYM_ARRAY
)
1159 ptr
->ctype
= arg
->ctype
;
1161 ptr
->ctype
.base_type
= arg
;
1162 ptr
->ctype
.as
|= s
->ctype
.as
;
1163 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1165 s
->ctype
.base_type
= ptr
;
1167 s
->ctype
.modifiers
= 0;
1170 examine_symbol_type(s
);
1177 } END_FOR_EACH_PTR(s
);
1180 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1182 /* Take the modifiers of the pointer, and apply them to the member */
1183 mod
|= sym
->ctype
.modifiers
;
1184 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1185 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1187 newsym
->ctype
.as
= as
;
1188 newsym
->ctype
.modifiers
= mod
;
1194 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1196 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1197 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1199 node
->ctype
.base_type
= ptr
;
1200 ptr
->bit_size
= bits_in_pointer
;
1201 ptr
->ctype
.alignment
= pointer_alignment
;
1203 node
->bit_size
= bits_in_pointer
;
1204 node
->ctype
.alignment
= pointer_alignment
;
1207 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1208 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1209 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1211 if (sym
->type
== SYM_NODE
) {
1212 ptr
->ctype
.as
|= sym
->ctype
.as
;
1213 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1214 sym
= sym
->ctype
.base_type
;
1216 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1217 ptr
->ctype
.as
|= sym
->ctype
.as
;
1218 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1219 sym
= sym
->ctype
.base_type
;
1221 ptr
->ctype
.base_type
= sym
;
1226 /* Arrays degenerate into pointers on pointer arithmetic */
1227 static struct symbol
*degenerate(struct expression
*expr
)
1229 struct symbol
*ctype
, *base
;
1233 ctype
= expr
->ctype
;
1237 if (ctype
->type
== SYM_NODE
)
1238 base
= ctype
->ctype
.base_type
;
1240 * Arrays degenerate into pointers to the entries, while
1241 * functions degenerate into pointers to themselves.
1242 * If array was part of non-lvalue compound, we create a copy
1243 * of that compound first and then act as if we were dealing with
1244 * the corresponding field in there.
1246 switch (base
->type
) {
1248 if (expr
->type
== EXPR_SLICE
) {
1249 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1250 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1252 a
->ctype
.base_type
= expr
->base
->ctype
;
1253 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1254 a
->array_size
= expr
->base
->ctype
->array_size
;
1256 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1258 e0
->ctype
= &lazy_ptr_ctype
;
1260 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1263 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1265 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1267 e2
->right
= expr
->base
;
1269 e2
->ctype
= expr
->base
->ctype
;
1271 if (expr
->r_bitpos
) {
1272 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1275 e3
->right
= alloc_const_expression(expr
->pos
,
1276 expr
->r_bitpos
>> 3);
1277 e3
->ctype
= &lazy_ptr_ctype
;
1282 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1285 e4
->ctype
= &lazy_ptr_ctype
;
1288 expr
->type
= EXPR_PREOP
;
1292 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1293 warning(expr
->pos
, "strange non-value function or array");
1296 *expr
= *expr
->unop
;
1297 ctype
= create_pointer(expr
, ctype
, 1);
1298 expr
->ctype
= ctype
;
1305 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1307 struct expression
*op
= expr
->unop
;
1308 struct symbol
*ctype
;
1310 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1311 warning(expr
->pos
, "not addressable");
1317 if (expr
->type
== EXPR_SYMBOL
) {
1318 struct symbol
*sym
= expr
->symbol
;
1319 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1323 * symbol expression evaluation is lazy about the type
1324 * of the sub-expression, so we may have to generate
1325 * the type here if so..
1327 if (expr
->ctype
== &lazy_ptr_ctype
) {
1328 ctype
= create_pointer(expr
, ctype
, 0);
1329 expr
->ctype
= ctype
;
1335 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1337 struct expression
*op
= expr
->unop
;
1338 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1340 /* Simplify: *&(expr) => (expr) */
1341 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1346 /* Dereferencing a node drops all the node information. */
1347 if (ctype
->type
== SYM_NODE
)
1348 ctype
= ctype
->ctype
.base_type
;
1350 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1351 target
= ctype
->ctype
.base_type
;
1353 switch (ctype
->type
) {
1355 warning(expr
->pos
, "cannot derefence this type");
1358 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1359 merge_type(node
, ctype
);
1363 if (!lvalue_expression(op
)) {
1364 warning(op
->pos
, "non-lvalue array??");
1368 /* Do the implied "addressof" on the array */
1372 * When an array is dereferenced, we need to pick
1373 * up the attributes of the original node too..
1375 merge_type(node
, op
->ctype
);
1376 merge_type(node
, ctype
);
1380 node
->bit_size
= target
->bit_size
;
1381 node
->array_size
= target
->array_size
;
1388 * Unary post-ops: x++ and x--
1390 static struct symbol
*evaluate_postop(struct expression
*expr
)
1392 struct expression
*op
= expr
->unop
;
1393 struct symbol
*ctype
= op
->ctype
;
1395 if (!lvalue_expression(expr
->unop
)) {
1396 warning(expr
->pos
, "need lvalue expression for ++/--");
1399 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1400 warning(expr
->pos
, "bad operation on restricted");
1404 evaluate_assign_to(op
, ctype
);
1406 expr
->ctype
= ctype
;
1408 if (is_ptr_type(ctype
))
1409 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1414 static struct symbol
*evaluate_sign(struct expression
*expr
)
1416 struct symbol
*ctype
= expr
->unop
->ctype
;
1417 if (is_int_type(ctype
)) {
1418 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1419 expr
->unop
= cast_to(expr
->unop
, rtype
);
1421 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1422 /* no conversions needed */
1423 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1424 /* no conversions needed */
1426 return bad_expr_type(expr
);
1428 if (expr
->op
== '+')
1429 *expr
= *expr
->unop
;
1430 expr
->ctype
= ctype
;
1434 static struct symbol
*evaluate_preop(struct expression
*expr
)
1436 struct symbol
*ctype
= expr
->unop
->ctype
;
1440 *expr
= *expr
->unop
;
1446 return evaluate_sign(expr
);
1449 return evaluate_dereference(expr
);
1452 return evaluate_addressof(expr
);
1454 case SPECIAL_INCREMENT
:
1455 case SPECIAL_DECREMENT
:
1457 * From a type evaluation standpoint the pre-ops are
1458 * the same as the postops
1460 return evaluate_postop(expr
);
1463 if (is_safe_type(ctype
))
1464 warning(expr
->pos
, "testing a 'safe expression'");
1465 if (is_float_type(ctype
)) {
1466 struct expression
*arg
= expr
->unop
;
1467 expr
->type
= EXPR_BINOP
;
1468 expr
->op
= SPECIAL_EQUAL
;
1470 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1471 expr
->right
->ctype
= ctype
;
1472 expr
->right
->fvalue
= 0;
1474 ctype
= &bool_ctype
;
1480 expr
->ctype
= ctype
;
1484 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1486 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1487 struct ptr_list
*list
= head
;
1493 for (i
= 0; i
< list
->nr
; i
++) {
1494 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1496 if (sym
->ident
!= ident
)
1498 *offset
= sym
->offset
;
1501 struct symbol
*ctype
= sym
->ctype
.base_type
;
1505 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1507 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1510 *offset
+= sym
->offset
;
1514 } while ((list
= list
->next
) != head
);
1518 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1520 struct expression
*add
;
1523 * Create a new add-expression
1525 * NOTE! Even if we just add zero, we need a new node
1526 * for the member pointer, since it has a different
1527 * type than the original pointer. We could make that
1528 * be just a cast, but the fact is, a node is a node,
1529 * so we might as well just do the "add zero" here.
1531 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1534 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1535 add
->right
->ctype
= &int_ctype
;
1536 add
->right
->value
= offset
;
1539 * The ctype of the pointer will be lazily evaluated if
1540 * we ever take the address of this member dereference..
1542 add
->ctype
= &lazy_ptr_ctype
;
1546 /* structure/union dereference */
1547 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1550 struct symbol
*ctype
, *member
;
1551 struct expression
*deref
= expr
->deref
, *add
;
1552 struct ident
*ident
= expr
->member
;
1556 if (!evaluate_expression(deref
))
1559 warning(expr
->pos
, "bad member name");
1563 ctype
= deref
->ctype
;
1564 address_space
= ctype
->ctype
.as
;
1565 mod
= ctype
->ctype
.modifiers
;
1566 if (ctype
->type
== SYM_NODE
) {
1567 ctype
= ctype
->ctype
.base_type
;
1568 address_space
|= ctype
->ctype
.as
;
1569 mod
|= ctype
->ctype
.modifiers
;
1571 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1572 warning(expr
->pos
, "expected structure or union");
1576 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1578 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1579 const char *name
= "<unnamed>";
1582 name
= ctype
->ident
->name
;
1583 namelen
= ctype
->ident
->len
;
1585 warning(expr
->pos
, "no member '%s' in %s %.*s",
1586 show_ident(ident
), type
, namelen
, name
);
1591 * The member needs to take on the address space and modifiers of
1592 * the "parent" type.
1594 member
= convert_to_as_mod(member
, address_space
, mod
);
1595 ctype
= member
->ctype
.base_type
;
1597 if (!lvalue_expression(deref
)) {
1598 if (deref
->type
!= EXPR_SLICE
) {
1602 expr
->base
= deref
->base
;
1603 expr
->r_bitpos
= deref
->r_bitpos
;
1605 expr
->r_bitpos
+= offset
<< 3;
1606 expr
->type
= EXPR_SLICE
;
1607 expr
->r_nrbits
= member
->bit_size
;
1608 expr
->r_bitpos
+= member
->bit_offset
;
1609 expr
->ctype
= member
;
1613 deref
= deref
->unop
;
1614 expr
->deref
= deref
;
1616 add
= evaluate_offset(deref
, offset
);
1617 expr
->type
= EXPR_PREOP
;
1621 expr
->ctype
= member
;
1625 static int is_promoted(struct expression
*expr
)
1628 switch (expr
->type
) {
1631 case EXPR_CONDITIONAL
:
1655 static struct symbol
*evaluate_cast(struct expression
*);
1657 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1659 struct symbol
*sym
= expr
->cast_type
;
1661 sym
= evaluate_expression(expr
->cast_expression
);
1665 * Expressions of restricted types will possibly get
1666 * promoted - check that here
1668 if (is_restricted_type(sym
)) {
1669 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1673 examine_symbol_type(sym
);
1674 if (is_bitfield_type(sym
)) {
1675 warning(expr
->pos
, "trying to examine bitfield type");
1681 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1683 struct symbol
*type
;
1686 type
= evaluate_type_information(expr
);
1690 size
= type
->bit_size
;
1691 if ((size
< 0) || (size
& 7))
1692 warning(expr
->pos
, "cannot size expression");
1693 expr
->type
= EXPR_VALUE
;
1694 expr
->value
= size
>> 3;
1695 expr
->ctype
= size_t_ctype
;
1696 return size_t_ctype
;
1699 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1701 struct symbol
*type
;
1704 type
= evaluate_type_information(expr
);
1708 if (type
->type
== SYM_NODE
)
1709 type
= type
->ctype
.base_type
;
1712 switch (type
->type
) {
1716 type
= type
->ctype
.base_type
;
1720 warning(expr
->pos
, "expected pointer expression");
1723 size
= type
->bit_size
;
1726 expr
->type
= EXPR_VALUE
;
1727 expr
->value
= size
>> 3;
1728 expr
->ctype
= size_t_ctype
;
1729 return size_t_ctype
;
1732 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1734 struct symbol
*type
;
1736 type
= evaluate_type_information(expr
);
1740 expr
->type
= EXPR_VALUE
;
1741 expr
->value
= type
->ctype
.alignment
;
1742 expr
->ctype
= size_t_ctype
;
1743 return size_t_ctype
;
1746 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1748 struct expression
*expr
;
1749 struct symbol_list
*argument_types
= fn
->arguments
;
1750 struct symbol
*argtype
;
1753 PREPARE_PTR_LIST(argument_types
, argtype
);
1754 FOR_EACH_PTR (head
, expr
) {
1755 struct expression
**p
= THIS_ADDRESS(expr
);
1756 struct symbol
*ctype
, *target
;
1757 ctype
= evaluate_expression(expr
);
1762 ctype
= degenerate(expr
);
1765 if (!target
&& ctype
->bit_size
< bits_in_int
)
1766 target
= &int_ctype
;
1768 static char where
[30];
1769 examine_symbol_type(target
);
1770 sprintf(where
, "argument %d", i
);
1771 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1775 NEXT_PTR_LIST(argtype
);
1776 } END_FOR_EACH_PTR(expr
);
1777 FINISH_PTR_LIST(argtype
);
1781 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1783 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1785 struct expression
*entry
= *ep
;
1786 struct expression
**parent
, *reuse
= NULL
;
1787 unsigned long offset
;
1789 unsigned long from
, to
;
1790 int accept_string
= is_byte_type(ctype
);
1795 if (entry
->type
== EXPR_INDEX
) {
1796 from
= entry
->idx_from
;
1797 to
= entry
->idx_to
+1;
1798 parent
= &entry
->idx_expression
;
1800 entry
= entry
->idx_expression
;
1803 offset
= from
* (ctype
->bit_size
>>3);
1805 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1806 reuse
->type
= EXPR_POS
;
1807 reuse
->ctype
= ctype
;
1808 reuse
->init_offset
= offset
;
1809 reuse
->init_nr
= to
- from
;
1810 reuse
->init_expr
= entry
;
1811 parent
= &reuse
->init_expr
;
1816 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1817 sym
= evaluate_expression(entry
);
1818 to
= from
+ get_expression_value(sym
->array_size
);
1820 evaluate_initializer(ctype
, parent
);
1825 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1827 struct expression
*entry
;
1830 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1831 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1832 } END_FOR_EACH_PTR(entry
);
1835 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1836 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1838 if (expression_list_size(expr
->expr_list
) != 1) {
1839 warning(expr
->pos
, "unexpected compound initializer");
1842 evaluate_array_initializer(ctype
, expr
);
1846 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1850 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1851 if (sym
->ident
== ident
)
1853 } END_FOR_EACH_PTR(sym
);
1857 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1859 struct expression
*entry
= *ep
;
1860 struct expression
**parent
;
1861 struct expression
*reuse
= NULL
;
1862 unsigned long offset
;
1865 error(entry
->pos
, "unknown named initializer");
1869 if (entry
->type
== EXPR_IDENTIFIER
) {
1871 entry
= entry
->ident_expression
;
1875 offset
= sym
->offset
;
1878 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1879 reuse
->type
= EXPR_POS
;
1881 reuse
->init_offset
= offset
;
1883 reuse
->init_expr
= entry
;
1884 parent
= &reuse
->init_expr
;
1888 evaluate_initializer(sym
, parent
);
1892 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1894 struct expression
*entry
;
1897 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1898 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1899 if (entry
->type
== EXPR_IDENTIFIER
) {
1900 struct ident
*ident
= entry
->expr_ident
;
1901 /* We special-case the "already right place" case */
1902 if (!sym
|| sym
->ident
!= ident
) {
1903 RESET_PTR_LIST(sym
);
1907 if (sym
->ident
== ident
)
1913 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1916 } END_FOR_EACH_PTR(entry
);
1917 FINISH_PTR_LIST(sym
);
1921 * Initializers are kind of like assignments. Except
1922 * they can be a hell of a lot more complex.
1924 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1926 struct expression
*expr
= *ep
;
1929 * Simple non-structure/array initializers are the simple
1930 * case, and look (and parse) largely like assignments.
1932 switch (expr
->type
) {
1934 int is_string
= expr
->type
== EXPR_STRING
;
1935 struct symbol
*rtype
= evaluate_expression(expr
);
1939 * char array[] = "string"
1940 * should _not_ degenerate.
1942 if (!is_string
|| !is_string_type(ctype
))
1943 rtype
= degenerate(expr
);
1944 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1949 case EXPR_INITIALIZER
:
1950 expr
->ctype
= ctype
;
1951 if (ctype
->type
== SYM_NODE
)
1952 ctype
= ctype
->ctype
.base_type
;
1954 switch (ctype
->type
) {
1957 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1960 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1963 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1966 evaluate_scalar_initializer(ctype
, expr
);
1970 case EXPR_IDENTIFIER
:
1971 if (ctype
->type
== SYM_NODE
)
1972 ctype
= ctype
->ctype
.base_type
;
1973 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1974 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1978 evaluate_one_struct_initializer(ctype
, ep
,
1979 find_struct_ident(ctype
, expr
->expr_ident
));
1983 if (ctype
->type
== SYM_NODE
)
1984 ctype
= ctype
->ctype
.base_type
;
1985 if (ctype
->type
!= SYM_ARRAY
) {
1986 error(expr
->pos
, "expected array");
1989 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1994 * An EXPR_POS expression has already been evaluated, and we don't
1995 * need to do anything more
2001 static int get_as(struct symbol
*sym
)
2009 mod
= sym
->ctype
.modifiers
;
2010 if (sym
->type
== SYM_NODE
) {
2011 sym
= sym
->ctype
.base_type
;
2012 as
|= sym
->ctype
.as
;
2013 mod
|= sym
->ctype
.modifiers
;
2017 * At least for now, allow casting to a "unsigned long".
2018 * That's how we do things like pointer arithmetic and
2019 * store pointers to registers.
2021 if (sym
== &ulong_ctype
)
2024 if (sym
&& sym
->type
== SYM_PTR
) {
2025 sym
= sym
->ctype
.base_type
;
2026 as
|= sym
->ctype
.as
;
2027 mod
|= sym
->ctype
.modifiers
;
2029 if (mod
& MOD_FORCE
)
2034 static struct symbol
*evaluate_cast(struct expression
*expr
)
2036 struct expression
*target
= expr
->cast_expression
;
2037 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2043 expr
->ctype
= ctype
;
2044 expr
->cast_type
= ctype
;
2047 * Special case: a cast can be followed by an
2048 * initializer, in which case we need to pass
2049 * the type value down to that initializer rather
2050 * than trying to evaluate it as an expression
2052 * A more complex case is when the initializer is
2053 * dereferenced as part of a post-fix expression.
2054 * We need to produce an expression that can be dereferenced.
2056 if (target
->type
== EXPR_INITIALIZER
) {
2057 struct symbol
*sym
= expr
->cast_type
;
2058 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2060 sym
->initializer
= expr
->cast_expression
;
2061 evaluate_symbol(sym
);
2063 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2066 expr
->type
= EXPR_PREOP
;
2074 evaluate_expression(target
);
2078 * You can always throw a value away by casting to
2079 * "void" - that's an implicit "force". Note that
2080 * the same is _not_ true of "void *".
2082 if (ctype
== &void_ctype
)
2086 if (type
== SYM_NODE
) {
2087 type
= ctype
->ctype
.base_type
->type
;
2088 if (ctype
->ctype
.base_type
== &void_ctype
)
2091 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2092 warning(expr
->pos
, "cast to non-scalar");
2094 if (!target
->ctype
) {
2095 warning(expr
->pos
, "cast from unknown type");
2099 type
= target
->ctype
->type
;
2100 if (type
== SYM_NODE
)
2101 type
= target
->ctype
->ctype
.base_type
->type
;
2102 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2103 warning(expr
->pos
, "cast from non-scalar");
2105 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2106 warning(expr
->pos
, "cast removes address space of expression");
2108 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2109 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2110 if (t1
->type
== SYM_NODE
)
2111 t1
= t1
->ctype
.base_type
;
2112 if (t2
->type
== SYM_NODE
)
2113 t2
= t2
->ctype
.base_type
;
2115 if (t1
->type
== SYM_RESTRICT
)
2116 warning(expr
->pos
, "cast to restricted type");
2117 if (t2
->type
== SYM_RESTRICT
)
2118 warning(expr
->pos
, "cast from restricted type");
2123 * Casts of constant values are special: they
2124 * can be NULL, and thus need to be simplified
2127 if (target
->type
== EXPR_VALUE
)
2128 cast_value(expr
, ctype
, target
, target
->ctype
);
2135 * Evaluate a call expression with a symbol. This
2136 * should expand inline functions, and evaluate
2139 static int evaluate_symbol_call(struct expression
*expr
)
2141 struct expression
*fn
= expr
->fn
;
2142 struct symbol
*ctype
= fn
->ctype
;
2144 if (fn
->type
!= EXPR_PREOP
)
2147 if (ctype
->op
&& ctype
->op
->evaluate
)
2148 return ctype
->op
->evaluate(expr
);
2150 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2152 struct symbol
*curr
= current_fn
;
2153 current_fn
= ctype
->ctype
.base_type
;
2154 examine_fn_arguments(current_fn
);
2156 ret
= inline_function(expr
, ctype
);
2158 /* restore the old function */
2166 static struct symbol
*evaluate_call(struct expression
*expr
)
2169 struct symbol
*ctype
, *sym
;
2170 struct expression
*fn
= expr
->fn
;
2171 struct expression_list
*arglist
= expr
->args
;
2173 if (!evaluate_expression(fn
))
2175 sym
= ctype
= fn
->ctype
;
2176 if (ctype
->type
== SYM_NODE
)
2177 ctype
= ctype
->ctype
.base_type
;
2178 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2179 ctype
= ctype
->ctype
.base_type
;
2180 if (!evaluate_arguments(sym
, ctype
, arglist
))
2182 if (ctype
->type
!= SYM_FN
) {
2183 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2186 args
= expression_list_size(expr
->args
);
2187 fnargs
= symbol_list_size(ctype
->arguments
);
2189 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2190 if (args
> fnargs
&& !ctype
->variadic
)
2191 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2192 if (sym
->type
== SYM_NODE
) {
2193 if (evaluate_symbol_call(expr
))
2196 expr
->ctype
= ctype
->ctype
.base_type
;
2200 struct symbol
*evaluate_expression(struct expression
*expr
)
2207 switch (expr
->type
) {
2210 warning(expr
->pos
, "value expression without a type");
2213 return evaluate_string(expr
);
2215 return evaluate_symbol_expression(expr
);
2217 if (!evaluate_expression(expr
->left
))
2219 if (!evaluate_expression(expr
->right
))
2221 return evaluate_binop(expr
);
2223 return evaluate_logical(expr
);
2225 evaluate_expression(expr
->left
);
2226 if (!evaluate_expression(expr
->right
))
2228 return evaluate_comma(expr
);
2230 if (!evaluate_expression(expr
->left
))
2232 if (!evaluate_expression(expr
->right
))
2234 return evaluate_compare(expr
);
2235 case EXPR_ASSIGNMENT
:
2236 if (!evaluate_expression(expr
->left
))
2238 if (!evaluate_expression(expr
->right
))
2240 return evaluate_assignment(expr
);
2242 if (!evaluate_expression(expr
->unop
))
2244 return evaluate_preop(expr
);
2246 if (!evaluate_expression(expr
->unop
))
2248 return evaluate_postop(expr
);
2250 case EXPR_IMPLIED_CAST
:
2251 return evaluate_cast(expr
);
2253 return evaluate_sizeof(expr
);
2254 case EXPR_PTRSIZEOF
:
2255 return evaluate_ptrsizeof(expr
);
2257 return evaluate_alignof(expr
);
2259 return evaluate_member_dereference(expr
);
2261 return evaluate_call(expr
);
2263 case EXPR_CONDITIONAL
:
2264 return evaluate_conditional_expression(expr
);
2265 case EXPR_STATEMENT
:
2266 expr
->ctype
= evaluate_statement(expr
->statement
);
2270 expr
->ctype
= &ptr_ctype
;
2274 /* Evaluate the type of the symbol .. */
2275 evaluate_symbol(expr
->symbol
);
2276 /* .. but the type of the _expression_ is a "type" */
2277 expr
->ctype
= &type_ctype
;
2280 /* These can not exist as stand-alone expressions */
2281 case EXPR_INITIALIZER
:
2282 case EXPR_IDENTIFIER
:
2285 warning(expr
->pos
, "internal front-end error: initializer in expression");
2288 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2294 static void check_duplicates(struct symbol
*sym
)
2297 struct symbol
*next
= sym
;
2299 while ((next
= next
->same_symbol
) != NULL
) {
2300 const char *typediff
;
2301 evaluate_symbol(next
);
2303 typediff
= type_difference(sym
, next
, 0, 0);
2305 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2306 show_ident(sym
->ident
),
2307 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2312 unsigned long mod
= sym
->ctype
.modifiers
;
2313 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2315 if (!(mod
& MOD_TOPLEVEL
))
2317 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2321 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2323 struct symbol
*base_type
;
2328 sym
= examine_symbol_type(sym
);
2329 base_type
= sym
->ctype
.base_type
;
2333 /* Evaluate the initializers */
2334 if (sym
->initializer
)
2335 evaluate_initializer(sym
, &sym
->initializer
);
2337 /* And finally, evaluate the body of the symbol too */
2338 if (base_type
->type
== SYM_FN
) {
2339 struct symbol
*curr
= current_fn
;
2341 current_fn
= base_type
;
2343 examine_fn_arguments(base_type
);
2344 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2346 if (base_type
->stmt
)
2347 evaluate_statement(base_type
->stmt
);
2355 void evaluate_symbol_list(struct symbol_list
*list
)
2359 FOR_EACH_PTR(list
, sym
) {
2360 check_duplicates(sym
);
2361 evaluate_symbol(sym
);
2362 } END_FOR_EACH_PTR(sym
);
2365 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2367 struct expression
*expr
= stmt
->expression
;
2368 struct symbol
*ctype
, *fntype
;
2370 evaluate_expression(expr
);
2371 ctype
= degenerate(expr
);
2372 fntype
= current_fn
->ctype
.base_type
;
2373 if (!fntype
|| fntype
== &void_ctype
) {
2374 if (expr
&& ctype
!= &void_ctype
)
2375 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2380 warning(stmt
->pos
, "return with no return value");
2385 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2389 static void evaluate_if_statement(struct statement
*stmt
)
2391 if (!stmt
->if_conditional
)
2394 evaluate_conditional(stmt
->if_conditional
, 0);
2395 evaluate_statement(stmt
->if_true
);
2396 evaluate_statement(stmt
->if_false
);
2399 static void evaluate_iterator(struct statement
*stmt
)
2401 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2402 evaluate_conditional(stmt
->iterator_post_condition
,1);
2403 evaluate_statement(stmt
->iterator_pre_statement
);
2404 evaluate_statement(stmt
->iterator_statement
);
2405 evaluate_statement(stmt
->iterator_post_statement
);
2408 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2410 switch (*constraint
) {
2411 case '=': /* Assignment */
2412 case '+': /* Update */
2415 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2419 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2421 switch (*constraint
) {
2422 case '=': /* Assignment */
2423 case '+': /* Update */
2424 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2428 static void evaluate_asm_statement(struct statement
*stmt
)
2430 struct expression
*expr
;
2433 expr
= stmt
->asm_string
;
2434 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2435 warning(stmt
->pos
, "need constant string for inline asm");
2440 FOR_EACH_PTR(stmt
->asm_outputs
, 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 output constraint is not a string");
2453 *THIS_ADDRESS(expr
) = NULL
;
2456 verify_output_constraint(expr
, expr
->string
->data
);
2459 case 2: /* Expression */
2461 if (!evaluate_expression(expr
))
2463 if (!lvalue_expression(expr
))
2464 warning(expr
->pos
, "asm output is not an lvalue");
2465 evaluate_assign_to(expr
, expr
->ctype
);
2468 } END_FOR_EACH_PTR(expr
);
2471 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2472 struct ident
*ident
;
2475 case 0: /* Identifier */
2477 ident
= (struct ident
*)expr
;
2480 case 1: /* Constraint */
2482 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2483 warning(expr
->pos
, "asm input constraint is not a string");
2484 *THIS_ADDRESS(expr
) = NULL
;
2487 verify_input_constraint(expr
, expr
->string
->data
);
2490 case 2: /* Expression */
2492 if (!evaluate_expression(expr
))
2496 } END_FOR_EACH_PTR(expr
);
2498 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2500 warning(stmt
->pos
, "bad asm output");
2503 if (expr
->type
== EXPR_STRING
)
2505 warning(expr
->pos
, "asm clobber is not a string");
2506 } END_FOR_EACH_PTR(expr
);
2509 struct symbol
*evaluate_statement(struct statement
*stmt
)
2514 switch (stmt
->type
) {
2516 return evaluate_return_expression(stmt
);
2518 case STMT_EXPRESSION
:
2519 if (!evaluate_expression(stmt
->expression
))
2521 return degenerate(stmt
->expression
);
2523 case STMT_COMPOUND
: {
2524 struct statement
*s
;
2525 struct symbol
*type
= NULL
;
2528 /* Evaluate each symbol in the compound statement */
2529 FOR_EACH_PTR(stmt
->syms
, sym
) {
2530 evaluate_symbol(sym
);
2531 } END_FOR_EACH_PTR(sym
);
2532 evaluate_symbol(stmt
->ret
);
2535 * Then, evaluate each statement, making the type of the
2536 * compound statement be the type of the last statement
2539 FOR_EACH_PTR(stmt
->stmts
, s
) {
2540 type
= evaluate_statement(s
);
2541 } END_FOR_EACH_PTR(s
);
2547 evaluate_if_statement(stmt
);
2550 evaluate_iterator(stmt
);
2553 evaluate_expression(stmt
->switch_expression
);
2554 evaluate_statement(stmt
->switch_statement
);
2557 evaluate_expression(stmt
->case_expression
);
2558 evaluate_expression(stmt
->case_to
);
2559 evaluate_statement(stmt
->case_statement
);
2562 return evaluate_statement(stmt
->label_statement
);
2564 evaluate_expression(stmt
->goto_expression
);
2569 evaluate_asm_statement(stmt
);
2572 evaluate_expression(stmt
->expression
);
2575 evaluate_expression(stmt
->range_expression
);
2576 evaluate_expression(stmt
->range_low
);
2577 evaluate_expression(stmt
->range_high
);