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
, 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
= get_base_type(ptr_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 base1
= examine_symbol_type(base1
);
603 switch (base1
->type
) {
607 base1
= base1
->ctype
.base_type
;
612 if (type2
== SYM_PTR
&& base2
) {
613 base2
= examine_symbol_type(base2
);
614 switch (base2
->type
) {
618 base2
= base2
->ctype
.base_type
;
624 /* Arrays degenerate to pointers for type comparisons */
625 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
626 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
628 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
629 return "different base types";
631 /* Must be same address space to be comparable */
633 return "different address spaces";
635 /* Ignore differences in storage types or addressability */
636 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
637 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
640 return "different type sizes";
641 if (diff
& ~MOD_SIGNEDNESS
)
642 return "different modifiers";
644 /* Differs in signedness only.. */
647 * Warn if both are explicitly signed ("unsigned" is obvously
648 * always explicit, and since we know one of them has to be
649 * unsigned, we check if the signed one was explicit).
651 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
652 return "different explicit signedness";
655 * "char" matches both "unsigned char" and "signed char",
656 * so if the explicit test didn't trigger, then we should
657 * not warn about a char.
659 if (!(mod1
& MOD_CHAR
))
660 return "different signedness";
664 if (type1
== SYM_FN
) {
666 struct symbol
*arg1
, *arg2
;
667 if (base1
->variadic
!= base2
->variadic
)
668 return "incompatible variadic arguments";
669 PREPARE_PTR_LIST(target
->arguments
, arg1
);
670 PREPARE_PTR_LIST(source
->arguments
, arg2
);
674 diff
= type_difference(arg1
, arg2
, 0, 0);
676 static char argdiff
[80];
677 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
686 FINISH_PTR_LIST(arg2
);
687 FINISH_PTR_LIST(arg1
);
696 static int is_null_ptr(struct expression
*expr
)
698 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
700 if (!is_ptr_type(expr
->ctype
))
701 warning(expr
->pos
, "Using plain integer as NULL pointer");
705 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
707 /* NULL expression? Just return the type of the "other side" */
716 * Ignore differences in "volatile" and "const"ness when
717 * subtracting pointers
719 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
721 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
723 const char *typediff
;
724 struct symbol
*ctype
;
725 struct symbol
*ltype
, *rtype
;
726 struct expression
*r
= *rp
;
728 ltype
= degenerate(l
);
729 rtype
= degenerate(r
);
732 * If it is an integer subtract: the ptr add case will do the
735 if (!is_ptr_type(rtype
))
736 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
739 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
741 ctype
= common_ptr_type(l
, r
);
743 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
747 examine_symbol_type(ctype
);
749 /* Figure out the base type we point to */
750 if (ctype
->type
== SYM_NODE
)
751 ctype
= ctype
->ctype
.base_type
;
752 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
753 warning(expr
->pos
, "subtraction of functions? Share your drugs");
756 ctype
= get_base_type(ctype
);
758 expr
->ctype
= ssize_t_ctype
;
759 if (ctype
->bit_size
> bits_in_char
) {
760 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
761 struct expression
*div
= expr
;
762 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
763 unsigned long value
= ctype
->bit_size
>> 3;
765 val
->ctype
= size_t_ctype
;
768 if (value
& (value
-1)) {
769 if (Wptr_subtraction_blows
)
770 warning(expr
->pos
, "potentially expensive pointer subtraction");
774 sub
->ctype
= ssize_t_ctype
;
783 return ssize_t_ctype
;
786 static struct symbol
*evaluate_sub(struct expression
*expr
)
788 struct expression
*left
= expr
->left
;
789 struct symbol
*ltype
= left
->ctype
;
791 if (is_ptr_type(ltype
))
792 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
794 return evaluate_arith(expr
, 1);
797 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
799 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
801 struct symbol
*ctype
;
806 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
807 warning(expr
->pos
, "assignment expression in conditional");
809 ctype
= evaluate_expression(expr
);
811 if (is_safe_type(ctype
))
812 warning(expr
->pos
, "testing a 'safe expression'");
818 static struct symbol
*evaluate_logical(struct expression
*expr
)
820 if (!evaluate_conditional(expr
->left
, 0))
822 if (!evaluate_conditional(expr
->right
, 0))
825 expr
->ctype
= &bool_ctype
;
829 static struct symbol
*evaluate_shift(struct expression
*expr
)
831 struct expression
*left
= expr
->left
, *right
= expr
->right
;
832 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
834 if (ltype
->type
== SYM_NODE
)
835 ltype
= ltype
->ctype
.base_type
;
836 if (rtype
->type
== SYM_NODE
)
837 rtype
= rtype
->ctype
.base_type
;
838 if (is_int_type(ltype
) && is_int_type(rtype
)) {
839 struct symbol
*ctype
= integer_promotion(ltype
);
840 expr
->left
= cast_to(expr
->left
, ctype
);
842 ctype
= integer_promotion(rtype
);
843 expr
->right
= cast_to(expr
->right
, ctype
);
846 return bad_expr_type(expr
);
849 static struct symbol
*evaluate_binop(struct expression
*expr
)
852 // addition can take ptr+int, fp and int
854 return evaluate_add(expr
);
856 // subtraction can take ptr-ptr, fp and int
858 return evaluate_sub(expr
);
860 // Arithmetic operations can take fp and int
862 return evaluate_arith(expr
, 1);
864 // shifts do integer promotions, but that's it.
865 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
866 return evaluate_shift(expr
);
868 // The rest are integer operations
869 // '%', '&', '^', '|'
871 return evaluate_arith(expr
, 0);
875 static struct symbol
*evaluate_comma(struct expression
*expr
)
877 expr
->ctype
= expr
->right
->ctype
;
881 static int modify_for_unsigned(int op
)
884 op
= SPECIAL_UNSIGNED_LT
;
886 op
= SPECIAL_UNSIGNED_GT
;
887 else if (op
== SPECIAL_LTE
)
888 op
= SPECIAL_UNSIGNED_LTE
;
889 else if (op
== SPECIAL_GTE
)
890 op
= SPECIAL_UNSIGNED_GTE
;
894 static struct symbol
*evaluate_compare(struct expression
*expr
)
896 struct expression
*left
= expr
->left
, *right
= expr
->right
;
897 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
898 struct symbol
*ctype
;
901 if (is_type_type(ltype
) && is_type_type(rtype
))
904 if (is_safe_type(ltype
) || is_safe_type(rtype
))
905 warning(expr
->pos
, "testing a 'safe expression'");
908 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
909 // FIXME! Check the types for compatibility
910 expr
->op
= modify_for_unsigned(expr
->op
);
914 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
916 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
917 expr
->op
= modify_for_unsigned(expr
->op
);
921 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
925 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
927 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
928 expr
->op
= modify_for_unsigned(expr
->op
);
935 expr
->ctype
= &bool_ctype
;
940 * FIXME!! This should do casts, array degeneration etc..
942 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
944 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
946 if (ltype
->type
== SYM_NODE
)
947 ltype
= ltype
->ctype
.base_type
;
949 if (rtype
->type
== SYM_NODE
)
950 rtype
= rtype
->ctype
.base_type
;
952 if (ltype
->type
== SYM_PTR
) {
953 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
957 if (rtype
->type
== SYM_PTR
) {
958 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
965 * NOTE! The degenerate case of "x ? : y", where we don't
966 * have a true case, this will possibly promote "x" to the
967 * same type as "y", and thus _change_ the conditional
968 * test in the expression. But since promotion is "safe"
969 * for testing, that's ok.
971 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
973 struct expression
**true;
974 struct symbol
*ctype
, *ltype
, *rtype
;
975 const char * typediff
;
977 if (!evaluate_conditional(expr
->conditional
, 0))
979 if (!evaluate_expression(expr
->cond_false
))
982 ctype
= degenerate(expr
->conditional
);
983 rtype
= degenerate(expr
->cond_false
);
985 true = &expr
->conditional
;
987 if (expr
->cond_true
) {
988 if (!evaluate_expression(expr
->cond_true
))
990 ltype
= degenerate(expr
->cond_true
);
991 true = &expr
->cond_true
;
994 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
997 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1000 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1003 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1007 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1010 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1014 expr
->ctype
= ctype
;
1018 /* FP assignments can not do modulo or bit operations */
1019 static int compatible_float_op(int op
)
1022 op
== SPECIAL_ADD_ASSIGN
||
1023 op
== SPECIAL_SUB_ASSIGN
||
1024 op
== SPECIAL_MUL_ASSIGN
||
1025 op
== SPECIAL_DIV_ASSIGN
;
1028 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1029 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1031 const char *typediff
;
1035 if (is_int_type(target
)) {
1036 if (is_int_type(source
))
1038 if (is_float_type(source
))
1040 } else if (is_float_type(target
)) {
1041 if (!compatible_float_op(op
)) {
1042 warning(expr
->pos
, "invalid assignment");
1045 if (is_int_type(source
))
1047 if (is_float_type(source
))
1049 } else if (is_restricted_type(target
)) {
1050 if (restricted_binop(op
, target
)) {
1051 warning(expr
->pos
, "bad restricted assignment");
1054 if (!restricted_value(*rp
, target
))
1056 } else if (is_ptr_type(target
)) {
1057 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1058 evaluate_ptr_add(expr
, target
, rp
);
1062 warning(expr
->pos
, "invalid pointer assignment");
1065 } else if (op
!= '=') {
1066 warning(expr
->pos
, "invalid assignment");
1070 /* It's ok if the target is more volatile or const than the source */
1071 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1075 /* Pointer destination? */
1077 target_as
= t
->ctype
.as
;
1078 if (t
->type
== SYM_NODE
) {
1079 t
= t
->ctype
.base_type
;
1080 target_as
|= t
->ctype
.as
;
1082 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1083 struct expression
*right
= *rp
;
1084 struct symbol
*s
= source
;
1087 // NULL pointer is always ok
1088 if (is_null_ptr(right
))
1091 /* "void *" matches anything as long as the address space is ok */
1092 source_as
= s
->ctype
.as
;
1093 if (s
->type
== SYM_NODE
) {
1094 s
= s
->ctype
.base_type
;
1095 source_as
|= s
->ctype
.as
;
1097 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1098 s
= get_base_type(s
);
1099 t
= get_base_type(t
);
1100 if (s
== &void_ctype
|| t
== &void_ctype
)
1105 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1106 info(expr
->pos
, " expected %s", show_typename(target
));
1107 info(expr
->pos
, " got %s", show_typename(source
));
1108 *rp
= cast_to(*rp
, target
);
1111 *rp
= cast_to(*rp
, target
);
1115 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1117 if (type
->ctype
.modifiers
& MOD_CONST
)
1118 warning(left
->pos
, "assignment to const expression");
1119 if (type
->type
== SYM_NODE
)
1120 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1123 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1125 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1126 struct expression
*where
= expr
;
1127 struct symbol
*ltype
, *rtype
;
1129 if (!lvalue_expression(left
)) {
1130 warning(expr
->pos
, "not an lvalue");
1134 ltype
= left
->ctype
;
1136 rtype
= degenerate(right
);
1138 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1141 evaluate_assign_to(left
, ltype
);
1143 expr
->ctype
= ltype
;
1147 static void examine_fn_arguments(struct symbol
*fn
)
1151 FOR_EACH_PTR(fn
->arguments
, s
) {
1152 struct symbol
*arg
= evaluate_symbol(s
);
1153 /* Array/function arguments silently degenerate into pointers */
1159 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1160 if (arg
->type
== SYM_ARRAY
)
1161 ptr
->ctype
= arg
->ctype
;
1163 ptr
->ctype
.base_type
= arg
;
1164 ptr
->ctype
.as
|= s
->ctype
.as
;
1165 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1167 s
->ctype
.base_type
= ptr
;
1169 s
->ctype
.modifiers
= 0;
1172 examine_symbol_type(s
);
1179 } END_FOR_EACH_PTR(s
);
1182 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1184 /* Take the modifiers of the pointer, and apply them to the member */
1185 mod
|= sym
->ctype
.modifiers
;
1186 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1187 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1189 newsym
->ctype
.as
= as
;
1190 newsym
->ctype
.modifiers
= mod
;
1196 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1198 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1199 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1201 node
->ctype
.base_type
= ptr
;
1202 ptr
->bit_size
= bits_in_pointer
;
1203 ptr
->ctype
.alignment
= pointer_alignment
;
1205 node
->bit_size
= bits_in_pointer
;
1206 node
->ctype
.alignment
= pointer_alignment
;
1209 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1210 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1211 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1213 if (sym
->type
== SYM_NODE
) {
1214 ptr
->ctype
.as
|= sym
->ctype
.as
;
1215 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1216 sym
= sym
->ctype
.base_type
;
1218 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1219 ptr
->ctype
.as
|= sym
->ctype
.as
;
1220 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1221 sym
= sym
->ctype
.base_type
;
1223 ptr
->ctype
.base_type
= sym
;
1228 /* Arrays degenerate into pointers on pointer arithmetic */
1229 static struct symbol
*degenerate(struct expression
*expr
)
1231 struct symbol
*ctype
, *base
;
1235 ctype
= expr
->ctype
;
1238 base
= examine_symbol_type(ctype
);
1239 if (ctype
->type
== SYM_NODE
)
1240 base
= ctype
->ctype
.base_type
;
1242 * Arrays degenerate into pointers to the entries, while
1243 * functions degenerate into pointers to themselves.
1244 * If array was part of non-lvalue compound, we create a copy
1245 * of that compound first and then act as if we were dealing with
1246 * the corresponding field in there.
1248 switch (base
->type
) {
1250 if (expr
->type
== EXPR_SLICE
) {
1251 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1252 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1254 a
->ctype
.base_type
= expr
->base
->ctype
;
1255 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1256 a
->array_size
= expr
->base
->ctype
->array_size
;
1258 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1260 e0
->ctype
= &lazy_ptr_ctype
;
1262 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1265 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1267 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1269 e2
->right
= expr
->base
;
1271 e2
->ctype
= expr
->base
->ctype
;
1273 if (expr
->r_bitpos
) {
1274 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1277 e3
->right
= alloc_const_expression(expr
->pos
,
1278 expr
->r_bitpos
>> 3);
1279 e3
->ctype
= &lazy_ptr_ctype
;
1284 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1287 e4
->ctype
= &lazy_ptr_ctype
;
1290 expr
->type
= EXPR_PREOP
;
1294 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1295 warning(expr
->pos
, "strange non-value function or array");
1298 *expr
= *expr
->unop
;
1299 ctype
= create_pointer(expr
, ctype
, 1);
1300 expr
->ctype
= ctype
;
1307 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1309 struct expression
*op
= expr
->unop
;
1310 struct symbol
*ctype
;
1312 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1313 warning(expr
->pos
, "not addressable");
1319 if (expr
->type
== EXPR_SYMBOL
) {
1320 struct symbol
*sym
= expr
->symbol
;
1321 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1325 * symbol expression evaluation is lazy about the type
1326 * of the sub-expression, so we may have to generate
1327 * the type here if so..
1329 if (expr
->ctype
== &lazy_ptr_ctype
) {
1330 ctype
= create_pointer(expr
, ctype
, 0);
1331 expr
->ctype
= ctype
;
1337 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1339 struct expression
*op
= expr
->unop
;
1340 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1342 /* Simplify: *&(expr) => (expr) */
1343 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1348 /* Dereferencing a node drops all the node information. */
1349 if (ctype
->type
== SYM_NODE
)
1350 ctype
= ctype
->ctype
.base_type
;
1352 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1353 target
= ctype
->ctype
.base_type
;
1355 switch (ctype
->type
) {
1357 warning(expr
->pos
, "cannot derefence this type");
1360 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1361 merge_type(node
, ctype
);
1365 if (!lvalue_expression(op
)) {
1366 warning(op
->pos
, "non-lvalue array??");
1370 /* Do the implied "addressof" on the array */
1374 * When an array is dereferenced, we need to pick
1375 * up the attributes of the original node too..
1377 merge_type(node
, op
->ctype
);
1378 merge_type(node
, ctype
);
1382 node
->bit_size
= target
->bit_size
;
1383 node
->array_size
= target
->array_size
;
1390 * Unary post-ops: x++ and x--
1392 static struct symbol
*evaluate_postop(struct expression
*expr
)
1394 struct expression
*op
= expr
->unop
;
1395 struct symbol
*ctype
= op
->ctype
;
1397 if (!lvalue_expression(expr
->unop
)) {
1398 warning(expr
->pos
, "need lvalue expression for ++/--");
1401 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1402 warning(expr
->pos
, "bad operation on restricted");
1406 evaluate_assign_to(op
, ctype
);
1408 expr
->ctype
= ctype
;
1410 if (is_ptr_type(ctype
))
1411 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1416 static struct symbol
*evaluate_sign(struct expression
*expr
)
1418 struct symbol
*ctype
= expr
->unop
->ctype
;
1419 if (is_int_type(ctype
)) {
1420 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1421 expr
->unop
= cast_to(expr
->unop
, rtype
);
1423 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1424 /* no conversions needed */
1425 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1426 /* no conversions needed */
1428 return bad_expr_type(expr
);
1430 if (expr
->op
== '+')
1431 *expr
= *expr
->unop
;
1432 expr
->ctype
= ctype
;
1436 static struct symbol
*evaluate_preop(struct expression
*expr
)
1438 struct symbol
*ctype
= expr
->unop
->ctype
;
1442 *expr
= *expr
->unop
;
1448 return evaluate_sign(expr
);
1451 return evaluate_dereference(expr
);
1454 return evaluate_addressof(expr
);
1456 case SPECIAL_INCREMENT
:
1457 case SPECIAL_DECREMENT
:
1459 * From a type evaluation standpoint the pre-ops are
1460 * the same as the postops
1462 return evaluate_postop(expr
);
1465 if (is_safe_type(ctype
))
1466 warning(expr
->pos
, "testing a 'safe expression'");
1467 if (is_float_type(ctype
)) {
1468 struct expression
*arg
= expr
->unop
;
1469 expr
->type
= EXPR_BINOP
;
1470 expr
->op
= SPECIAL_EQUAL
;
1472 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1473 expr
->right
->ctype
= ctype
;
1474 expr
->right
->fvalue
= 0;
1476 ctype
= &bool_ctype
;
1482 expr
->ctype
= ctype
;
1486 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1488 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1489 struct ptr_list
*list
= head
;
1495 for (i
= 0; i
< list
->nr
; i
++) {
1496 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1498 if (sym
->ident
!= ident
)
1500 *offset
= sym
->offset
;
1503 struct symbol
*ctype
= sym
->ctype
.base_type
;
1507 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1509 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1512 *offset
+= sym
->offset
;
1516 } while ((list
= list
->next
) != head
);
1520 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1522 struct expression
*add
;
1525 * Create a new add-expression
1527 * NOTE! Even if we just add zero, we need a new node
1528 * for the member pointer, since it has a different
1529 * type than the original pointer. We could make that
1530 * be just a cast, but the fact is, a node is a node,
1531 * so we might as well just do the "add zero" here.
1533 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1536 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1537 add
->right
->ctype
= &int_ctype
;
1538 add
->right
->value
= offset
;
1541 * The ctype of the pointer will be lazily evaluated if
1542 * we ever take the address of this member dereference..
1544 add
->ctype
= &lazy_ptr_ctype
;
1548 /* structure/union dereference */
1549 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1552 struct symbol
*ctype
, *member
;
1553 struct expression
*deref
= expr
->deref
, *add
;
1554 struct ident
*ident
= expr
->member
;
1558 if (!evaluate_expression(deref
))
1561 warning(expr
->pos
, "bad member name");
1565 ctype
= deref
->ctype
;
1566 address_space
= ctype
->ctype
.as
;
1567 mod
= ctype
->ctype
.modifiers
;
1568 if (ctype
->type
== SYM_NODE
) {
1569 ctype
= ctype
->ctype
.base_type
;
1570 address_space
|= ctype
->ctype
.as
;
1571 mod
|= ctype
->ctype
.modifiers
;
1573 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1574 warning(expr
->pos
, "expected structure or union");
1578 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1580 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1581 const char *name
= "<unnamed>";
1584 name
= ctype
->ident
->name
;
1585 namelen
= ctype
->ident
->len
;
1587 warning(expr
->pos
, "no member '%s' in %s %.*s",
1588 show_ident(ident
), type
, namelen
, name
);
1593 * The member needs to take on the address space and modifiers of
1594 * the "parent" type.
1596 member
= convert_to_as_mod(member
, address_space
, mod
);
1597 ctype
= get_base_type(member
);
1599 if (!lvalue_expression(deref
)) {
1600 if (deref
->type
!= EXPR_SLICE
) {
1604 expr
->base
= deref
->base
;
1605 expr
->r_bitpos
= deref
->r_bitpos
;
1607 expr
->r_bitpos
+= offset
<< 3;
1608 expr
->type
= EXPR_SLICE
;
1609 expr
->r_nrbits
= member
->bit_size
;
1610 expr
->r_bitpos
+= member
->bit_offset
;
1611 expr
->ctype
= member
;
1615 deref
= deref
->unop
;
1616 expr
->deref
= deref
;
1618 add
= evaluate_offset(deref
, offset
);
1619 expr
->type
= EXPR_PREOP
;
1623 expr
->ctype
= member
;
1627 static int is_promoted(struct expression
*expr
)
1630 switch (expr
->type
) {
1633 case EXPR_CONDITIONAL
:
1657 static struct symbol
*evaluate_cast(struct expression
*);
1659 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1661 struct symbol
*sym
= expr
->cast_type
;
1663 sym
= evaluate_expression(expr
->cast_expression
);
1667 * Expressions of restricted types will possibly get
1668 * promoted - check that here
1670 if (is_restricted_type(sym
)) {
1671 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1675 examine_symbol_type(sym
);
1676 if (is_bitfield_type(sym
)) {
1677 warning(expr
->pos
, "trying to examine bitfield type");
1683 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1685 struct symbol
*type
;
1688 type
= evaluate_type_information(expr
);
1692 size
= type
->bit_size
;
1693 if ((size
< 0) || (size
& 7))
1694 warning(expr
->pos
, "cannot size expression");
1695 expr
->type
= EXPR_VALUE
;
1696 expr
->value
= size
>> 3;
1697 expr
->ctype
= size_t_ctype
;
1698 return size_t_ctype
;
1701 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1703 struct symbol
*type
;
1706 type
= evaluate_type_information(expr
);
1710 if (type
->type
== SYM_NODE
)
1711 type
= type
->ctype
.base_type
;
1714 switch (type
->type
) {
1718 type
= get_base_type(type
);
1722 warning(expr
->pos
, "expected pointer expression");
1725 size
= type
->bit_size
;
1728 expr
->type
= EXPR_VALUE
;
1729 expr
->value
= size
>> 3;
1730 expr
->ctype
= size_t_ctype
;
1731 return size_t_ctype
;
1734 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1736 struct symbol
*type
;
1738 type
= evaluate_type_information(expr
);
1742 expr
->type
= EXPR_VALUE
;
1743 expr
->value
= type
->ctype
.alignment
;
1744 expr
->ctype
= size_t_ctype
;
1745 return size_t_ctype
;
1748 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1750 struct expression
*expr
;
1751 struct symbol_list
*argument_types
= fn
->arguments
;
1752 struct symbol
*argtype
;
1755 PREPARE_PTR_LIST(argument_types
, argtype
);
1756 FOR_EACH_PTR (head
, expr
) {
1757 struct expression
**p
= THIS_ADDRESS(expr
);
1758 struct symbol
*ctype
, *target
;
1759 ctype
= evaluate_expression(expr
);
1764 ctype
= degenerate(expr
);
1767 if (!target
&& ctype
->bit_size
< bits_in_int
)
1768 target
= &int_ctype
;
1770 static char where
[30];
1771 examine_symbol_type(target
);
1772 sprintf(where
, "argument %d", i
);
1773 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1777 NEXT_PTR_LIST(argtype
);
1778 } END_FOR_EACH_PTR(expr
);
1779 FINISH_PTR_LIST(argtype
);
1783 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1785 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1787 struct expression
*entry
= *ep
;
1788 struct expression
**parent
, *reuse
= NULL
;
1789 unsigned long offset
;
1791 unsigned long from
, to
;
1792 int accept_string
= is_byte_type(ctype
);
1797 if (entry
->type
== EXPR_INDEX
) {
1798 from
= entry
->idx_from
;
1799 to
= entry
->idx_to
+1;
1800 parent
= &entry
->idx_expression
;
1802 entry
= entry
->idx_expression
;
1805 offset
= from
* (ctype
->bit_size
>>3);
1807 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1808 reuse
->type
= EXPR_POS
;
1809 reuse
->ctype
= ctype
;
1810 reuse
->init_offset
= offset
;
1811 reuse
->init_nr
= to
- from
;
1812 reuse
->init_expr
= entry
;
1813 parent
= &reuse
->init_expr
;
1818 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1819 sym
= evaluate_expression(entry
);
1820 to
= from
+ get_expression_value(sym
->array_size
);
1822 evaluate_initializer(ctype
, parent
);
1827 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1829 struct expression
*entry
;
1832 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1833 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1834 } END_FOR_EACH_PTR(entry
);
1837 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1838 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1840 if (expression_list_size(expr
->expr_list
) != 1) {
1841 warning(expr
->pos
, "unexpected compound initializer");
1844 evaluate_array_initializer(ctype
, expr
);
1848 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1852 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1853 if (sym
->ident
== ident
)
1855 } END_FOR_EACH_PTR(sym
);
1859 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1861 struct expression
*entry
= *ep
;
1862 struct expression
**parent
;
1863 struct expression
*reuse
= NULL
;
1864 unsigned long offset
;
1867 error(entry
->pos
, "unknown named initializer");
1871 if (entry
->type
== EXPR_IDENTIFIER
) {
1873 entry
= entry
->ident_expression
;
1877 offset
= sym
->offset
;
1880 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1881 reuse
->type
= EXPR_POS
;
1883 reuse
->init_offset
= offset
;
1885 reuse
->init_expr
= entry
;
1886 parent
= &reuse
->init_expr
;
1890 evaluate_initializer(sym
, parent
);
1894 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1896 struct expression
*entry
;
1899 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1900 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1901 if (entry
->type
== EXPR_IDENTIFIER
) {
1902 struct ident
*ident
= entry
->expr_ident
;
1903 /* We special-case the "already right place" case */
1904 if (!sym
|| sym
->ident
!= ident
) {
1905 RESET_PTR_LIST(sym
);
1909 if (sym
->ident
== ident
)
1915 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1918 } END_FOR_EACH_PTR(entry
);
1919 FINISH_PTR_LIST(sym
);
1923 * Initializers are kind of like assignments. Except
1924 * they can be a hell of a lot more complex.
1926 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1928 struct expression
*expr
= *ep
;
1931 * Simple non-structure/array initializers are the simple
1932 * case, and look (and parse) largely like assignments.
1934 switch (expr
->type
) {
1936 int is_string
= expr
->type
== EXPR_STRING
;
1937 struct symbol
*rtype
= evaluate_expression(expr
);
1941 * char array[] = "string"
1942 * should _not_ degenerate.
1944 if (!is_string
|| !is_string_type(ctype
))
1945 rtype
= degenerate(expr
);
1946 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1951 case EXPR_INITIALIZER
:
1952 expr
->ctype
= ctype
;
1953 if (ctype
->type
== SYM_NODE
)
1954 ctype
= ctype
->ctype
.base_type
;
1956 switch (ctype
->type
) {
1959 evaluate_array_initializer(get_base_type(ctype
), expr
);
1962 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1965 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1968 evaluate_scalar_initializer(ctype
, expr
);
1972 case EXPR_IDENTIFIER
:
1973 if (ctype
->type
== SYM_NODE
)
1974 ctype
= ctype
->ctype
.base_type
;
1975 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1976 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1980 evaluate_one_struct_initializer(ctype
, ep
,
1981 find_struct_ident(ctype
, expr
->expr_ident
));
1985 if (ctype
->type
== SYM_NODE
)
1986 ctype
= ctype
->ctype
.base_type
;
1987 if (ctype
->type
!= SYM_ARRAY
) {
1988 error(expr
->pos
, "expected array");
1991 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1996 * An EXPR_POS expression has already been evaluated, and we don't
1997 * need to do anything more
2003 static int get_as(struct symbol
*sym
)
2011 mod
= sym
->ctype
.modifiers
;
2012 if (sym
->type
== SYM_NODE
) {
2013 sym
= sym
->ctype
.base_type
;
2014 as
|= sym
->ctype
.as
;
2015 mod
|= sym
->ctype
.modifiers
;
2019 * At least for now, allow casting to a "unsigned long".
2020 * That's how we do things like pointer arithmetic and
2021 * store pointers to registers.
2023 if (sym
== &ulong_ctype
)
2026 if (sym
&& sym
->type
== SYM_PTR
) {
2027 sym
= get_base_type(sym
);
2028 as
|= sym
->ctype
.as
;
2029 mod
|= sym
->ctype
.modifiers
;
2031 if (mod
& MOD_FORCE
)
2036 static struct symbol
*evaluate_cast(struct expression
*expr
)
2038 struct expression
*target
= expr
->cast_expression
;
2039 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2045 expr
->ctype
= ctype
;
2046 expr
->cast_type
= ctype
;
2049 * Special case: a cast can be followed by an
2050 * initializer, in which case we need to pass
2051 * the type value down to that initializer rather
2052 * than trying to evaluate it as an expression
2054 * A more complex case is when the initializer is
2055 * dereferenced as part of a post-fix expression.
2056 * We need to produce an expression that can be dereferenced.
2058 if (target
->type
== EXPR_INITIALIZER
) {
2059 struct symbol
*sym
= expr
->cast_type
;
2060 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2062 sym
->initializer
= expr
->cast_expression
;
2063 evaluate_symbol(sym
);
2065 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2068 expr
->type
= EXPR_PREOP
;
2076 evaluate_expression(target
);
2080 * You can always throw a value away by casting to
2081 * "void" - that's an implicit "force". Note that
2082 * the same is _not_ true of "void *".
2084 if (ctype
== &void_ctype
)
2088 if (type
== SYM_NODE
) {
2089 type
= ctype
->ctype
.base_type
->type
;
2090 if (ctype
->ctype
.base_type
== &void_ctype
)
2093 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2094 warning(expr
->pos
, "cast to non-scalar");
2096 if (!target
->ctype
) {
2097 warning(expr
->pos
, "cast from unknown type");
2101 type
= target
->ctype
->type
;
2102 if (type
== SYM_NODE
)
2103 type
= target
->ctype
->ctype
.base_type
->type
;
2104 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2105 warning(expr
->pos
, "cast from non-scalar");
2107 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2108 warning(expr
->pos
, "cast removes address space of expression");
2110 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2111 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2112 if (t1
->type
== SYM_NODE
)
2113 t1
= t1
->ctype
.base_type
;
2114 if (t2
->type
== SYM_NODE
)
2115 t2
= t2
->ctype
.base_type
;
2117 if (t1
->type
== SYM_RESTRICT
)
2118 warning(expr
->pos
, "cast to restricted type");
2119 if (t2
->type
== SYM_RESTRICT
)
2120 warning(expr
->pos
, "cast from restricted type");
2125 * Casts of constant values are special: they
2126 * can be NULL, and thus need to be simplified
2129 if (target
->type
== EXPR_VALUE
)
2130 cast_value(expr
, ctype
, target
, target
->ctype
);
2137 * Evaluate a call expression with a symbol. This
2138 * should expand inline functions, and evaluate
2141 static int evaluate_symbol_call(struct expression
*expr
)
2143 struct expression
*fn
= expr
->fn
;
2144 struct symbol
*ctype
= fn
->ctype
;
2146 if (fn
->type
!= EXPR_PREOP
)
2149 if (ctype
->op
&& ctype
->op
->evaluate
)
2150 return ctype
->op
->evaluate(expr
);
2152 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2154 struct symbol
*curr
= current_fn
;
2155 current_fn
= ctype
->ctype
.base_type
;
2156 examine_fn_arguments(current_fn
);
2158 ret
= inline_function(expr
, ctype
);
2160 /* restore the old function */
2168 static struct symbol
*evaluate_call(struct expression
*expr
)
2171 struct symbol
*ctype
, *sym
;
2172 struct expression
*fn
= expr
->fn
;
2173 struct expression_list
*arglist
= expr
->args
;
2175 if (!evaluate_expression(fn
))
2177 sym
= ctype
= fn
->ctype
;
2178 if (ctype
->type
== SYM_NODE
)
2179 ctype
= ctype
->ctype
.base_type
;
2180 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2181 ctype
= get_base_type(ctype
);
2182 if (!evaluate_arguments(sym
, ctype
, arglist
))
2184 if (ctype
->type
!= SYM_FN
) {
2185 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2188 args
= expression_list_size(expr
->args
);
2189 fnargs
= symbol_list_size(ctype
->arguments
);
2191 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2192 if (args
> fnargs
&& !ctype
->variadic
)
2193 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2194 if (sym
->type
== SYM_NODE
) {
2195 if (evaluate_symbol_call(expr
))
2198 expr
->ctype
= ctype
->ctype
.base_type
;
2202 struct symbol
*evaluate_expression(struct expression
*expr
)
2209 switch (expr
->type
) {
2212 warning(expr
->pos
, "value expression without a type");
2215 return evaluate_string(expr
);
2217 return evaluate_symbol_expression(expr
);
2219 if (!evaluate_expression(expr
->left
))
2221 if (!evaluate_expression(expr
->right
))
2223 return evaluate_binop(expr
);
2225 return evaluate_logical(expr
);
2227 evaluate_expression(expr
->left
);
2228 if (!evaluate_expression(expr
->right
))
2230 return evaluate_comma(expr
);
2232 if (!evaluate_expression(expr
->left
))
2234 if (!evaluate_expression(expr
->right
))
2236 return evaluate_compare(expr
);
2237 case EXPR_ASSIGNMENT
:
2238 if (!evaluate_expression(expr
->left
))
2240 if (!evaluate_expression(expr
->right
))
2242 return evaluate_assignment(expr
);
2244 if (!evaluate_expression(expr
->unop
))
2246 return evaluate_preop(expr
);
2248 if (!evaluate_expression(expr
->unop
))
2250 return evaluate_postop(expr
);
2252 case EXPR_IMPLIED_CAST
:
2253 return evaluate_cast(expr
);
2255 return evaluate_sizeof(expr
);
2256 case EXPR_PTRSIZEOF
:
2257 return evaluate_ptrsizeof(expr
);
2259 return evaluate_alignof(expr
);
2261 return evaluate_member_dereference(expr
);
2263 return evaluate_call(expr
);
2265 case EXPR_CONDITIONAL
:
2266 return evaluate_conditional_expression(expr
);
2267 case EXPR_STATEMENT
:
2268 expr
->ctype
= evaluate_statement(expr
->statement
);
2272 expr
->ctype
= &ptr_ctype
;
2276 /* Evaluate the type of the symbol .. */
2277 evaluate_symbol(expr
->symbol
);
2278 /* .. but the type of the _expression_ is a "type" */
2279 expr
->ctype
= &type_ctype
;
2282 /* These can not exist as stand-alone expressions */
2283 case EXPR_INITIALIZER
:
2284 case EXPR_IDENTIFIER
:
2287 warning(expr
->pos
, "internal front-end error: initializer in expression");
2290 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2296 static void check_duplicates(struct symbol
*sym
)
2299 struct symbol
*next
= sym
;
2301 while ((next
= next
->same_symbol
) != NULL
) {
2302 const char *typediff
;
2303 evaluate_symbol(next
);
2305 typediff
= type_difference(sym
, next
, 0, 0);
2307 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2308 show_ident(sym
->ident
),
2309 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2314 unsigned long mod
= sym
->ctype
.modifiers
;
2315 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2317 if (!(mod
& MOD_TOPLEVEL
))
2319 if (sym
->ident
== &main_ident
)
2321 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2325 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2327 struct symbol
*base_type
;
2332 sym
= examine_symbol_type(sym
);
2333 base_type
= get_base_type(sym
);
2337 /* Evaluate the initializers */
2338 if (sym
->initializer
)
2339 evaluate_initializer(sym
, &sym
->initializer
);
2341 /* And finally, evaluate the body of the symbol too */
2342 if (base_type
->type
== SYM_FN
) {
2343 struct symbol
*curr
= current_fn
;
2345 current_fn
= base_type
;
2347 examine_fn_arguments(base_type
);
2348 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2350 if (base_type
->stmt
)
2351 evaluate_statement(base_type
->stmt
);
2359 void evaluate_symbol_list(struct symbol_list
*list
)
2363 FOR_EACH_PTR(list
, sym
) {
2364 check_duplicates(sym
);
2365 evaluate_symbol(sym
);
2366 } END_FOR_EACH_PTR(sym
);
2369 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2371 struct expression
*expr
= stmt
->expression
;
2372 struct symbol
*ctype
, *fntype
;
2374 evaluate_expression(expr
);
2375 ctype
= degenerate(expr
);
2376 fntype
= current_fn
->ctype
.base_type
;
2377 if (!fntype
|| fntype
== &void_ctype
) {
2378 if (expr
&& ctype
!= &void_ctype
)
2379 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2384 warning(stmt
->pos
, "return with no return value");
2389 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2393 static void evaluate_if_statement(struct statement
*stmt
)
2395 if (!stmt
->if_conditional
)
2398 evaluate_conditional(stmt
->if_conditional
, 0);
2399 evaluate_statement(stmt
->if_true
);
2400 evaluate_statement(stmt
->if_false
);
2403 static void evaluate_iterator(struct statement
*stmt
)
2405 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2406 evaluate_conditional(stmt
->iterator_post_condition
,1);
2407 evaluate_statement(stmt
->iterator_pre_statement
);
2408 evaluate_statement(stmt
->iterator_statement
);
2409 evaluate_statement(stmt
->iterator_post_statement
);
2412 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2414 switch (*constraint
) {
2415 case '=': /* Assignment */
2416 case '+': /* Update */
2419 warning(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2423 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2425 switch (*constraint
) {
2426 case '=': /* Assignment */
2427 case '+': /* Update */
2428 warning(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2432 static void evaluate_asm_statement(struct statement
*stmt
)
2434 struct expression
*expr
;
2437 expr
= stmt
->asm_string
;
2438 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2439 warning(stmt
->pos
, "need constant string for inline asm");
2444 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2445 struct ident
*ident
;
2448 case 0: /* Identifier */
2450 ident
= (struct ident
*)expr
;
2453 case 1: /* Constraint */
2455 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2456 warning(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2457 *THIS_ADDRESS(expr
) = NULL
;
2460 verify_output_constraint(expr
, expr
->string
->data
);
2463 case 2: /* Expression */
2465 if (!evaluate_expression(expr
))
2467 if (!lvalue_expression(expr
))
2468 warning(expr
->pos
, "asm output is not an lvalue");
2469 evaluate_assign_to(expr
, expr
->ctype
);
2472 } END_FOR_EACH_PTR(expr
);
2475 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2476 struct ident
*ident
;
2479 case 0: /* Identifier */
2481 ident
= (struct ident
*)expr
;
2484 case 1: /* Constraint */
2486 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2487 warning(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2488 *THIS_ADDRESS(expr
) = NULL
;
2491 verify_input_constraint(expr
, expr
->string
->data
);
2494 case 2: /* Expression */
2496 if (!evaluate_expression(expr
))
2500 } END_FOR_EACH_PTR(expr
);
2502 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2504 warning(stmt
->pos
, "bad asm output");
2507 if (expr
->type
== EXPR_STRING
)
2509 warning(expr
->pos
, "asm clobber is not a string");
2510 } END_FOR_EACH_PTR(expr
);
2513 struct symbol
*evaluate_statement(struct statement
*stmt
)
2518 switch (stmt
->type
) {
2520 return evaluate_return_expression(stmt
);
2522 case STMT_EXPRESSION
:
2523 if (!evaluate_expression(stmt
->expression
))
2525 return degenerate(stmt
->expression
);
2527 case STMT_COMPOUND
: {
2528 struct statement
*s
;
2529 struct symbol
*type
= NULL
;
2532 /* Evaluate each symbol in the compound statement */
2533 FOR_EACH_PTR(stmt
->syms
, sym
) {
2534 evaluate_symbol(sym
);
2535 } END_FOR_EACH_PTR(sym
);
2536 evaluate_symbol(stmt
->ret
);
2539 * Then, evaluate each statement, making the type of the
2540 * compound statement be the type of the last statement
2543 FOR_EACH_PTR(stmt
->stmts
, s
) {
2544 type
= evaluate_statement(s
);
2545 } END_FOR_EACH_PTR(s
);
2551 evaluate_if_statement(stmt
);
2554 evaluate_iterator(stmt
);
2557 evaluate_expression(stmt
->switch_expression
);
2558 evaluate_statement(stmt
->switch_statement
);
2561 evaluate_expression(stmt
->case_expression
);
2562 evaluate_expression(stmt
->case_to
);
2563 evaluate_statement(stmt
->case_statement
);
2566 return evaluate_statement(stmt
->label_statement
);
2568 evaluate_expression(stmt
->goto_expression
);
2573 evaluate_asm_statement(stmt
);
2576 evaluate_expression(stmt
->expression
);
2579 evaluate_expression(stmt
->range_expression
);
2580 evaluate_expression(stmt
->range_low
);
2581 evaluate_expression(stmt
->range_high
);