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.
26 #include "expression.h"
28 struct symbol
*current_fn
;
30 static struct symbol
*degenerate(struct expression
*expr
);
31 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
33 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
35 struct symbol
*sym
= expr
->symbol
;
36 struct symbol
*base_type
;
39 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
43 examine_symbol_type(sym
);
45 base_type
= sym
->ctype
.base_type
;
47 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
51 /* The type of a symbol is the symbol itself! */
54 /* enums can be turned into plain values */
55 if (sym
->type
!= SYM_ENUM
) {
56 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
58 addr
->symbol_name
= expr
->symbol_name
;
59 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
60 expr
->type
= EXPR_PREOP
;
64 } else if (base_type
->bit_size
< bits_in_int
) {
65 /* ugly - we need to force sizeof for these guys */
66 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
67 e
->value
= sym
->value
;
69 expr
->type
= EXPR_PREOP
;
73 expr
->type
= EXPR_VALUE
;
74 expr
->value
= sym
->value
;
76 expr
->ctype
= base_type
;
80 static struct symbol
*evaluate_string(struct expression
*expr
)
82 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
83 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
84 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
85 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
86 unsigned int length
= expr
->string
->length
;
88 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
89 sym
->bit_size
= bits_in_char
* length
;
90 sym
->ctype
.alignment
= 1;
91 sym
->ctype
.modifiers
= MOD_STATIC
;
92 sym
->ctype
.base_type
= array
;
93 sym
->initializer
= initstr
;
96 initstr
->string
= expr
->string
;
98 array
->array_size
= sym
->array_size
;
99 array
->bit_size
= bits_in_char
* length
;
100 array
->ctype
.alignment
= 1;
101 array
->ctype
.modifiers
= MOD_STATIC
;
102 array
->ctype
.base_type
= &char_ctype
;
105 addr
->ctype
= &lazy_ptr_ctype
;
107 expr
->type
= EXPR_PREOP
;
114 static inline struct symbol
*integer_promotion(struct symbol
*type
)
116 unsigned long mod
= type
->ctype
.modifiers
;
119 if (type
->type
== SYM_NODE
)
120 type
= type
->ctype
.base_type
;
121 if (type
->type
== SYM_ENUM
)
122 type
= type
->ctype
.base_type
;
123 width
= type
->bit_size
;
124 if (type
->type
== SYM_BITFIELD
)
125 type
= type
->ctype
.base_type
;
126 mod
= type
->ctype
.modifiers
;
127 if (width
< bits_in_int
)
130 /* If char/short has as many bits as int, it still gets "promoted" */
131 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
133 if (mod
& MOD_UNSIGNED
)
140 * integer part of usual arithmetic conversions:
141 * integer promotions are applied
142 * if left and right are identical, we are done
143 * if signedness is the same, convert one with lower rank
144 * unless unsigned argument has rank lower than signed one, convert the
146 * if signed argument is bigger than unsigned one, convert the unsigned.
147 * otherwise, convert signed.
149 * Leaving aside the integer promotions, that is equivalent to
150 * if identical, don't convert
151 * if left is bigger than right, convert right
152 * if right is bigger than left, convert right
153 * otherwise, if signedness is the same, convert one with lower rank
154 * otherwise convert the signed one.
156 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
158 unsigned long lmod
, rmod
;
160 left
= integer_promotion(left
);
161 right
= integer_promotion(right
);
166 if (left
->bit_size
> right
->bit_size
)
169 if (right
->bit_size
> left
->bit_size
)
172 lmod
= left
->ctype
.modifiers
;
173 rmod
= right
->ctype
.modifiers
;
174 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
175 if (lmod
& MOD_UNSIGNED
)
177 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
185 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
187 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
191 * This gets called for implicit casts in assignments and
192 * integer promotion. We often want to try to move the
193 * cast down, because the ops involved may have been
194 * implicitly cast up, and we can get rid of the casts
197 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
199 struct expression
*expr
;
202 * See if we can simplify the op. Move the cast down.
206 if (old
->op
== '~') {
208 old
->unop
= cast_to(old
->unop
, type
);
213 case EXPR_IMPLIED_CAST
:
214 if (old
->ctype
->bit_size
>= type
->bit_size
) {
215 struct expression
*orig
= old
->cast_expression
;
216 if (same_cast_type(orig
->ctype
, type
))
218 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
220 old
->cast_type
= type
;
230 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
232 expr
->cast_type
= type
;
233 expr
->cast_expression
= old
;
237 static int is_type_type(struct symbol
*type
)
239 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
242 static int is_ptr_type(struct symbol
*type
)
244 if (type
->type
== SYM_NODE
)
245 type
= type
->ctype
.base_type
;
246 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
249 static inline int is_float_type(struct symbol
*type
)
251 if (type
->type
== SYM_NODE
)
252 type
= type
->ctype
.base_type
;
253 return type
->ctype
.base_type
== &fp_type
;
256 static inline int is_byte_type(struct symbol
*type
)
258 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
261 static inline int is_string_type(struct symbol
*type
)
263 if (type
->type
== SYM_NODE
)
264 type
= type
->ctype
.base_type
;
265 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
268 static struct symbol
*bad_expr_type(struct expression
*expr
)
270 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
271 switch (expr
->type
) {
274 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
275 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
279 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
288 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
290 struct expression
*left
= *lp
, *right
= *rp
;
291 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
293 if (ltype
->type
== SYM_NODE
)
294 ltype
= ltype
->ctype
.base_type
;
295 if (rtype
->type
== SYM_NODE
)
296 rtype
= rtype
->ctype
.base_type
;
297 if (is_float_type(ltype
)) {
298 if (is_int_type(rtype
))
300 if (is_float_type(rtype
)) {
301 unsigned long lmod
= ltype
->ctype
.modifiers
;
302 unsigned long rmod
= rtype
->ctype
.modifiers
;
303 lmod
&= MOD_LONG
| MOD_LONGLONG
;
304 rmod
&= MOD_LONG
| MOD_LONGLONG
;
314 if (!is_float_type(rtype
) || !is_int_type(ltype
))
317 *lp
= cast_to(left
, rtype
);
320 *rp
= cast_to(right
, ltype
);
324 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
326 struct expression
*left
= *lp
, *right
= *rp
;
327 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
329 if (ltype
->type
== SYM_NODE
)
330 ltype
= ltype
->ctype
.base_type
;
331 if (rtype
->type
== SYM_NODE
)
332 rtype
= rtype
->ctype
.base_type
;
333 if (is_int_type(ltype
) && is_int_type(rtype
)) {
334 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
336 /* Don't bother promoting same-size entities, it only adds clutter */
337 if (ltype
->bit_size
!= ctype
->bit_size
)
338 *lp
= cast_to(left
, ctype
);
339 if (rtype
->bit_size
!= ctype
->bit_size
)
340 *rp
= cast_to(right
, ctype
);
346 static int restricted_value(struct expression
*v
, struct symbol
*type
)
348 if (v
->type
!= EXPR_VALUE
)
355 static int restricted_binop(int op
, struct symbol
*type
)
364 case SPECIAL_NOTEQUAL
:
365 case SPECIAL_AND_ASSIGN
:
366 case SPECIAL_OR_ASSIGN
:
367 case SPECIAL_XOR_ASSIGN
:
374 static int restricted_unop(int op
, struct symbol
*type
)
376 if (op
== '~' && type
->bit_size
>= bits_in_int
)
383 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
385 struct expression
*left
= *lp
, *right
= *rp
;
386 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
387 struct symbol
*type
= NULL
;
389 if (ltype
->type
== SYM_NODE
)
390 ltype
= ltype
->ctype
.base_type
;
391 if (ltype
->type
== SYM_ENUM
)
392 ltype
= ltype
->ctype
.base_type
;
393 if (rtype
->type
== SYM_NODE
)
394 rtype
= rtype
->ctype
.base_type
;
395 if (rtype
->type
== SYM_ENUM
)
396 rtype
= rtype
->ctype
.base_type
;
397 if (is_restricted_type(ltype
)) {
398 if (is_restricted_type(rtype
)) {
402 if (!restricted_value(right
, ltype
))
405 } else if (is_restricted_type(rtype
)) {
406 if (!restricted_value(left
, rtype
))
411 if (restricted_binop(op
, type
))
416 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
418 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
419 if (!ctype
&& float_ok
)
420 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
422 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
427 return bad_expr_type(expr
);
430 static inline int lvalue_expression(struct expression
*expr
)
432 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
435 static int ptr_object_size(struct symbol
*ptr_type
)
437 if (ptr_type
->type
== SYM_NODE
)
438 ptr_type
= ptr_type
->ctype
.base_type
;
439 if (ptr_type
->type
== SYM_PTR
)
440 ptr_type
= ptr_type
->ctype
.base_type
;
441 return ptr_type
->bit_size
;
444 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
446 struct expression
*i
= *ip
;
447 struct symbol
*ptr_type
= ctype
;
450 if (ptr_type
->type
== SYM_NODE
)
451 ptr_type
= ptr_type
->ctype
.base_type
;
453 if (!is_int_type(i
->ctype
))
454 return bad_expr_type(expr
);
456 examine_symbol_type(ctype
);
458 if (!ctype
->ctype
.base_type
) {
459 warning(expr
->pos
, "missing type information");
463 /* Get the size of whatever the pointer points to */
464 bit_size
= ptr_object_size(ctype
);
466 if (i
->type
== EXPR_VALUE
) {
467 i
->value
*= bit_size
>> 3;
468 } else if (bit_size
> bits_in_char
) {
469 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
470 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
472 val
->ctype
= size_t_ctype
;
473 val
->value
= bit_size
>> 3;
476 mul
->ctype
= size_t_ctype
;
487 static struct symbol
*evaluate_add(struct expression
*expr
)
489 struct expression
*left
= expr
->left
, *right
= expr
->right
;
490 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
492 if (is_ptr_type(ltype
))
493 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
495 if (is_ptr_type(rtype
))
496 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
498 return evaluate_arith(expr
, 1);
501 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
502 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
503 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
505 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
506 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
509 unsigned long mod1
, mod2
, diff
;
510 unsigned long as1
, as2
;
512 struct symbol
*base1
, *base2
;
514 if (target
== source
)
516 if (!target
|| !source
)
517 return "different types";
519 * Peel of per-node information.
520 * FIXME! Check alignment and context too here!
522 mod1
= target
->ctype
.modifiers
;
523 as1
= target
->ctype
.as
;
524 mod2
= source
->ctype
.modifiers
;
525 as2
= source
->ctype
.as
;
526 if (target
->type
== SYM_NODE
) {
527 target
= target
->ctype
.base_type
;
530 if (target
->type
== SYM_PTR
) {
534 mod1
|= target
->ctype
.modifiers
;
535 as1
|= target
->ctype
.as
;
537 if (source
->type
== SYM_NODE
) {
538 source
= source
->ctype
.base_type
;
541 if (source
->type
== SYM_PTR
) {
545 mod2
|= source
->ctype
.modifiers
;
546 as2
|= source
->ctype
.as
;
548 if (target
->type
== SYM_ENUM
) {
549 target
= target
->ctype
.base_type
;
553 if (source
->type
== SYM_ENUM
) {
554 source
= source
->ctype
.base_type
;
559 if (target
== source
)
561 if (!target
|| !source
)
562 return "different types";
564 type1
= target
->type
;
565 base1
= target
->ctype
.base_type
;
567 type2
= source
->type
;
568 base2
= source
->ctype
.base_type
;
571 * Pointers to functions compare as the function itself
573 if (type1
== SYM_PTR
&& base1
) {
574 switch (base1
->type
) {
578 base1
= base1
->ctype
.base_type
;
583 if (type2
== SYM_PTR
&& base2
) {
584 switch (base2
->type
) {
588 base2
= base2
->ctype
.base_type
;
594 /* Arrays degenerate to pointers for type comparisons */
595 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
596 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
598 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
599 return "different base types";
601 /* Must be same address space to be comparable */
603 return "different address spaces";
605 /* Ignore differences in storage types or addressability */
606 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
607 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
610 return "different type sizes";
611 if (diff
& ~MOD_SIGNEDNESS
)
612 return "different modifiers";
614 /* Differs in signedness only.. */
617 * Warn if both are explicitly signed ("unsigned" is obvously
618 * always explicit, and since we know one of them has to be
619 * unsigned, we check if the signed one was explicit).
621 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
622 return "different explicit signedness";
625 * "char" matches both "unsigned char" and "signed char",
626 * so if the explicit test didn't trigger, then we should
627 * not warn about a char.
629 if (!(mod1
& MOD_CHAR
))
630 return "different signedness";
634 if (type1
== SYM_FN
) {
636 struct symbol
*arg1
, *arg2
;
637 if (base1
->variadic
!= base2
->variadic
)
638 return "incompatible variadic arguments";
639 PREPARE_PTR_LIST(target
->arguments
, arg1
);
640 PREPARE_PTR_LIST(source
->arguments
, arg2
);
644 diff
= type_difference(arg1
, arg2
, 0, 0);
646 static char argdiff
[80];
647 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
656 FINISH_PTR_LIST(arg2
);
657 FINISH_PTR_LIST(arg1
);
666 static int is_null_ptr(struct expression
*expr
)
668 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
670 if (!is_ptr_type(expr
->ctype
))
671 warning(expr
->pos
, "Using plain integer as NULL pointer");
675 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
677 /* NULL expression? Just return the type of the "other side" */
686 * Ignore differences in "volatile" and "const"ness when
687 * subtracting pointers
689 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
691 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
693 const char *typediff
;
694 struct symbol
*ctype
;
695 struct symbol
*ltype
, *rtype
;
696 struct expression
*r
= *rp
;
698 ltype
= degenerate(l
);
699 rtype
= degenerate(r
);
702 * If it is an integer subtract: the ptr add case will do the
705 if (!is_ptr_type(rtype
))
706 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
709 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
711 ctype
= common_ptr_type(l
, r
);
713 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
717 examine_symbol_type(ctype
);
719 /* Figure out the base type we point to */
720 if (ctype
->type
== SYM_NODE
)
721 ctype
= ctype
->ctype
.base_type
;
722 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
723 warning(expr
->pos
, "subtraction of functions? Share your drugs");
726 ctype
= ctype
->ctype
.base_type
;
728 expr
->ctype
= ssize_t_ctype
;
729 if (ctype
->bit_size
> bits_in_char
) {
730 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
731 struct expression
*div
= expr
;
732 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
734 val
->ctype
= size_t_ctype
;
735 val
->value
= ctype
->bit_size
>> 3;
738 sub
->ctype
= ssize_t_ctype
;
747 return ssize_t_ctype
;
750 static struct symbol
*evaluate_sub(struct expression
*expr
)
752 struct expression
*left
= expr
->left
;
753 struct symbol
*ltype
= left
->ctype
;
755 if (is_ptr_type(ltype
))
756 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
758 return evaluate_arith(expr
, 1);
761 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
763 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
765 struct symbol
*ctype
;
770 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
771 warning(expr
->pos
, "assignment expression in conditional");
773 ctype
= evaluate_expression(expr
);
775 if (is_safe_type(ctype
))
776 warning(expr
->pos
, "testing a 'safe expression'");
782 static struct symbol
*evaluate_logical(struct expression
*expr
)
784 if (!evaluate_conditional(expr
->left
, 0))
786 if (!evaluate_conditional(expr
->right
, 0))
789 expr
->ctype
= &bool_ctype
;
793 static struct symbol
*evaluate_shift(struct expression
*expr
)
795 struct expression
*left
= expr
->left
, *right
= expr
->right
;
796 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
798 if (ltype
->type
== SYM_NODE
)
799 ltype
= ltype
->ctype
.base_type
;
800 if (rtype
->type
== SYM_NODE
)
801 rtype
= rtype
->ctype
.base_type
;
802 if (is_int_type(ltype
) && is_int_type(rtype
)) {
803 struct symbol
*ctype
= integer_promotion(ltype
);
804 if (ltype
->bit_size
!= ctype
->bit_size
)
805 expr
->left
= cast_to(expr
->left
, ctype
);
807 ctype
= integer_promotion(rtype
);
808 if (rtype
->bit_size
!= ctype
->bit_size
)
809 expr
->right
= cast_to(expr
->right
, ctype
);
812 return bad_expr_type(expr
);
815 static struct symbol
*evaluate_binop(struct expression
*expr
)
818 // addition can take ptr+int, fp and int
820 return evaluate_add(expr
);
822 // subtraction can take ptr-ptr, fp and int
824 return evaluate_sub(expr
);
826 // Arithmetic operations can take fp and int
828 return evaluate_arith(expr
, 1);
830 // shifts do integer promotions, but that's it.
831 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
832 return evaluate_shift(expr
);
834 // The rest are integer operations
835 // '%', '&', '^', '|'
837 return evaluate_arith(expr
, 0);
841 static struct symbol
*evaluate_comma(struct expression
*expr
)
843 expr
->ctype
= expr
->right
->ctype
;
847 static int modify_for_unsigned(int op
)
850 op
= SPECIAL_UNSIGNED_LT
;
852 op
= SPECIAL_UNSIGNED_GT
;
853 else if (op
== SPECIAL_LTE
)
854 op
= SPECIAL_UNSIGNED_LTE
;
855 else if (op
== SPECIAL_GTE
)
856 op
= SPECIAL_UNSIGNED_GTE
;
860 static struct symbol
*evaluate_compare(struct expression
*expr
)
862 struct expression
*left
= expr
->left
, *right
= expr
->right
;
863 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
864 struct symbol
*ctype
;
867 if (is_type_type(ltype
) && is_type_type(rtype
))
870 if (is_safe_type(ltype
) || is_safe_type(rtype
))
871 warning(expr
->pos
, "testing a 'safe expression'");
874 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
875 // FIXME! Check the types for compatibility
879 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
881 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
882 expr
->op
= modify_for_unsigned(expr
->op
);
886 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
890 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
897 expr
->ctype
= &bool_ctype
;
902 * FIXME!! This should do casts, array degeneration etc..
904 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
906 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
908 if (ltype
->type
== SYM_NODE
)
909 ltype
= ltype
->ctype
.base_type
;
911 if (rtype
->type
== SYM_NODE
)
912 rtype
= rtype
->ctype
.base_type
;
914 if (ltype
->type
== SYM_PTR
) {
915 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
919 if (rtype
->type
== SYM_PTR
) {
920 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
927 * NOTE! The degenerate case of "x ? : y", where we don't
928 * have a true case, this will possibly promote "x" to the
929 * same type as "y", and thus _change_ the conditional
930 * test in the expression. But since promotion is "safe"
931 * for testing, that's ok.
933 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
935 struct expression
**true;
936 struct symbol
*ctype
, *ltype
, *rtype
;
937 const char * typediff
;
939 if (!evaluate_conditional(expr
->conditional
, 0))
941 if (!evaluate_expression(expr
->cond_false
))
944 ctype
= degenerate(expr
->conditional
);
945 rtype
= degenerate(expr
->cond_false
);
947 true = &expr
->conditional
;
949 if (expr
->cond_true
) {
950 if (!evaluate_expression(expr
->cond_true
))
952 ltype
= degenerate(expr
->cond_true
);
953 true = &expr
->cond_true
;
956 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
959 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
962 ctype
= compatible_float_binop(true, &expr
->cond_false
);
965 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
969 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
972 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
980 /* FP assignments can not do modulo or bit operations */
981 static int compatible_float_op(int op
)
984 op
== SPECIAL_ADD_ASSIGN
||
985 op
== SPECIAL_SUB_ASSIGN
||
986 op
== SPECIAL_MUL_ASSIGN
||
987 op
== SPECIAL_DIV_ASSIGN
;
990 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
991 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
993 const char *typediff
;
997 if (is_int_type(target
)) {
998 if (is_int_type(source
)) {
999 if (target
->bit_size
!= source
->bit_size
)
1001 if (target
->bit_offset
!= source
->bit_offset
)
1005 if (is_float_type(source
))
1007 } else if (is_float_type(target
)) {
1008 if (!compatible_float_op(op
)) {
1009 warning(expr
->pos
, "invalid assignment");
1012 if (is_int_type(source
))
1014 if (is_float_type(source
)) {
1015 if (target
->bit_size
!= source
->bit_size
)
1019 } else if (is_restricted_type(target
)) {
1020 if (restricted_binop(op
, target
)) {
1021 warning(expr
->pos
, "bad restricted assignment");
1024 if (!restricted_value(*rp
, target
))
1026 } else if (is_ptr_type(target
)) {
1027 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1028 evaluate_ptr_add(expr
, target
, rp
);
1032 warning(expr
->pos
, "invalid pointer assignment");
1035 } else if (op
!= '=') {
1036 warning(expr
->pos
, "invalid assignment");
1040 /* It's ok if the target is more volatile or const than the source */
1041 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1045 /* Pointer destination? */
1047 target_as
= t
->ctype
.as
;
1048 if (t
->type
== SYM_NODE
) {
1049 t
= t
->ctype
.base_type
;
1050 target_as
|= t
->ctype
.as
;
1052 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1053 struct expression
*right
= *rp
;
1054 struct symbol
*s
= source
;
1057 // NULL pointer is always ok
1058 if (is_null_ptr(right
))
1061 /* "void *" matches anything as long as the address space is ok */
1062 source_as
= s
->ctype
.as
;
1063 if (s
->type
== SYM_NODE
) {
1064 s
= s
->ctype
.base_type
;
1065 source_as
|= s
->ctype
.as
;
1067 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1068 s
= s
->ctype
.base_type
;
1069 t
= t
->ctype
.base_type
;
1070 if (s
== &void_ctype
|| t
== &void_ctype
)
1075 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1076 info(expr
->pos
, " expected %s", show_typename(target
));
1077 info(expr
->pos
, " got %s", show_typename(source
));
1078 *rp
= cast_to(*rp
, target
);
1081 *rp
= cast_to(*rp
, target
);
1085 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1087 if (type
->ctype
.modifiers
& MOD_CONST
)
1088 warning(left
->pos
, "assignment to const expression");
1089 if (type
->type
== SYM_NODE
)
1090 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1093 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1095 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1096 struct expression
*where
= expr
;
1097 struct symbol
*ltype
, *rtype
;
1099 if (!lvalue_expression(left
)) {
1100 warning(expr
->pos
, "not an lvalue");
1104 ltype
= left
->ctype
;
1106 rtype
= degenerate(right
);
1108 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1111 evaluate_assign_to(left
, ltype
);
1113 expr
->ctype
= ltype
;
1117 static void examine_fn_arguments(struct symbol
*fn
)
1121 FOR_EACH_PTR(fn
->arguments
, s
) {
1122 struct symbol
*arg
= evaluate_symbol(s
);
1123 /* Array/function arguments silently degenerate into pointers */
1129 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1130 if (arg
->type
== SYM_ARRAY
)
1131 ptr
->ctype
= arg
->ctype
;
1133 ptr
->ctype
.base_type
= arg
;
1134 ptr
->ctype
.as
|= s
->ctype
.as
;
1135 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1137 s
->ctype
.base_type
= ptr
;
1139 s
->ctype
.modifiers
= 0;
1142 examine_symbol_type(s
);
1149 } END_FOR_EACH_PTR(s
);
1152 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1154 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1155 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1157 newsym
->ctype
.as
= as
;
1158 newsym
->ctype
.modifiers
= mod
;
1164 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1166 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1167 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1169 node
->ctype
.base_type
= ptr
;
1170 ptr
->bit_size
= bits_in_pointer
;
1171 ptr
->ctype
.alignment
= pointer_alignment
;
1173 node
->bit_size
= bits_in_pointer
;
1174 node
->ctype
.alignment
= pointer_alignment
;
1177 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1178 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1179 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1181 if (sym
->type
== SYM_NODE
) {
1182 ptr
->ctype
.as
|= sym
->ctype
.as
;
1183 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1184 sym
= sym
->ctype
.base_type
;
1186 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1187 ptr
->ctype
.as
|= sym
->ctype
.as
;
1188 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1189 sym
= sym
->ctype
.base_type
;
1191 ptr
->ctype
.base_type
= sym
;
1196 /* Arrays degenerate into pointers on pointer arithmetic */
1197 static struct symbol
*degenerate(struct expression
*expr
)
1199 struct symbol
*ctype
, *base
;
1203 ctype
= expr
->ctype
;
1207 if (ctype
->type
== SYM_NODE
)
1208 base
= ctype
->ctype
.base_type
;
1210 * Arrays degenerate into pointers to the entries, while
1211 * functions degenerate into pointers to themselves.
1212 * If array was part of non-lvalue compound, we create a copy
1213 * of that compound first and then act as if we were dealing with
1214 * the corresponding field in there.
1216 switch (base
->type
) {
1218 if (expr
->type
== EXPR_SLICE
) {
1219 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1220 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1222 a
->ctype
.base_type
= expr
->base
->ctype
;
1223 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1224 a
->array_size
= expr
->base
->ctype
->array_size
;
1226 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1228 e0
->ctype
= &lazy_ptr_ctype
;
1230 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1233 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1235 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1237 e2
->right
= expr
->base
;
1239 e2
->ctype
= expr
->base
->ctype
;
1241 if (expr
->r_bitpos
) {
1242 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1245 e3
->right
= alloc_const_expression(expr
->pos
,
1246 expr
->r_bitpos
>> 3);
1247 e3
->ctype
= &lazy_ptr_ctype
;
1252 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1255 e4
->ctype
= &lazy_ptr_ctype
;
1258 expr
->type
= EXPR_PREOP
;
1262 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1263 warning(expr
->pos
, "strange non-value function or array");
1266 *expr
= *expr
->unop
;
1267 ctype
= create_pointer(expr
, ctype
, 1);
1268 expr
->ctype
= ctype
;
1275 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1277 struct expression
*op
= expr
->unop
;
1278 struct symbol
*ctype
;
1280 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1281 warning(expr
->pos
, "not addressable");
1288 * symbol expression evaluation is lazy about the type
1289 * of the sub-expression, so we may have to generate
1290 * the type here if so..
1292 if (expr
->ctype
== &lazy_ptr_ctype
) {
1293 ctype
= create_pointer(expr
, ctype
, 0);
1294 expr
->ctype
= ctype
;
1300 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1302 struct expression
*op
= expr
->unop
;
1303 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1305 /* Simplify: *&(expr) => (expr) */
1306 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1311 /* Dereferencing a node drops all the node information. */
1312 if (ctype
->type
== SYM_NODE
)
1313 ctype
= ctype
->ctype
.base_type
;
1315 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1316 target
= ctype
->ctype
.base_type
;
1318 switch (ctype
->type
) {
1320 warning(expr
->pos
, "cannot derefence this type");
1323 merge_type(node
, ctype
);
1324 if (ctype
->type
!= SYM_ARRAY
)
1327 * Dereferencing a pointer to an array results in a
1328 * degenerate dereference: the expression becomes
1329 * just a pointer to the entry, and the derefence
1334 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1335 target
->bit_size
= bits_in_pointer
;
1336 target
->ctype
.alignment
= pointer_alignment
;
1337 merge_type(target
, ctype
->ctype
.base_type
);
1341 if (!lvalue_expression(op
)) {
1342 warning(op
->pos
, "non-lvalue array??");
1346 /* Do the implied "addressof" on the array */
1350 * When an array is dereferenced, we need to pick
1351 * up the attributes of the original node too..
1353 merge_type(node
, op
->ctype
);
1354 merge_type(node
, ctype
);
1358 node
->bit_size
= target
->bit_size
;
1359 node
->array_size
= target
->array_size
;
1366 * Unary post-ops: x++ and x--
1368 static struct symbol
*evaluate_postop(struct expression
*expr
)
1370 struct expression
*op
= expr
->unop
;
1371 struct symbol
*ctype
= op
->ctype
;
1373 if (!lvalue_expression(expr
->unop
)) {
1374 warning(expr
->pos
, "need lvalue expression for ++/--");
1377 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1378 warning(expr
->pos
, "bad operation on restricted");
1382 evaluate_assign_to(op
, ctype
);
1384 expr
->ctype
= ctype
;
1386 if (is_ptr_type(ctype
))
1387 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1392 static struct symbol
*evaluate_sign(struct expression
*expr
)
1394 struct symbol
*ctype
= expr
->unop
->ctype
;
1395 if (is_int_type(ctype
)) {
1396 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1397 if (rtype
->bit_size
!= ctype
->bit_size
)
1398 expr
->unop
= cast_to(expr
->unop
, rtype
);
1400 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1401 /* no conversions needed */
1402 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1403 /* no conversions needed */
1405 return bad_expr_type(expr
);
1407 if (expr
->op
== '+')
1408 *expr
= *expr
->unop
;
1409 expr
->ctype
= ctype
;
1413 static struct symbol
*evaluate_preop(struct expression
*expr
)
1415 struct symbol
*ctype
= expr
->unop
->ctype
;
1419 *expr
= *expr
->unop
;
1425 return evaluate_sign(expr
);
1428 return evaluate_dereference(expr
);
1431 return evaluate_addressof(expr
);
1433 case SPECIAL_INCREMENT
:
1434 case SPECIAL_DECREMENT
:
1436 * From a type evaluation standpoint the pre-ops are
1437 * the same as the postops
1439 return evaluate_postop(expr
);
1442 if (is_safe_type(ctype
))
1443 warning(expr
->pos
, "testing a 'safe expression'");
1444 if (is_float_type(ctype
)) {
1445 struct expression
*arg
= expr
->unop
;
1446 expr
->type
= EXPR_BINOP
;
1447 expr
->op
= SPECIAL_EQUAL
;
1449 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1450 expr
->right
->ctype
= ctype
;
1451 expr
->right
->fvalue
= 0;
1453 ctype
= &bool_ctype
;
1459 expr
->ctype
= ctype
;
1463 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1465 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1466 struct ptr_list
*list
= head
;
1472 for (i
= 0; i
< list
->nr
; i
++) {
1473 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1475 if (sym
->ident
!= ident
)
1477 *offset
= sym
->offset
;
1480 struct symbol
*ctype
= sym
->ctype
.base_type
;
1484 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1486 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1489 *offset
+= sym
->offset
;
1493 } while ((list
= list
->next
) != head
);
1497 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1499 struct expression
*add
;
1502 * Create a new add-expression
1504 * NOTE! Even if we just add zero, we need a new node
1505 * for the member pointer, since it has a different
1506 * type than the original pointer. We could make that
1507 * be just a cast, but the fact is, a node is a node,
1508 * so we might as well just do the "add zero" here.
1510 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1513 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1514 add
->right
->ctype
= &int_ctype
;
1515 add
->right
->value
= offset
;
1518 * The ctype of the pointer will be lazily evaluated if
1519 * we ever take the address of this member dereference..
1521 add
->ctype
= &lazy_ptr_ctype
;
1525 /* structure/union dereference */
1526 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1529 struct symbol
*ctype
, *member
;
1530 struct expression
*deref
= expr
->deref
, *add
;
1531 struct ident
*ident
= expr
->member
;
1535 if (!evaluate_expression(deref
))
1538 warning(expr
->pos
, "bad member name");
1542 ctype
= deref
->ctype
;
1543 address_space
= ctype
->ctype
.as
;
1544 mod
= ctype
->ctype
.modifiers
;
1545 if (ctype
->type
== SYM_NODE
) {
1546 ctype
= ctype
->ctype
.base_type
;
1547 address_space
|= ctype
->ctype
.as
;
1548 mod
|= ctype
->ctype
.modifiers
;
1550 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1551 warning(expr
->pos
, "expected structure or union");
1555 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1557 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1558 const char *name
= "<unnamed>";
1561 name
= ctype
->ident
->name
;
1562 namelen
= ctype
->ident
->len
;
1564 warning(expr
->pos
, "no member '%s' in %s %.*s",
1565 show_ident(ident
), type
, namelen
, name
);
1570 * The member needs to take on the address space and modifiers of
1571 * the "parent" type.
1573 member
= convert_to_as_mod(member
, address_space
, mod
);
1574 ctype
= member
->ctype
.base_type
;
1576 if (!lvalue_expression(deref
)) {
1577 if (deref
->type
!= EXPR_SLICE
) {
1581 expr
->base
= deref
->base
;
1582 expr
->r_bitpos
= deref
->r_bitpos
;
1584 expr
->r_bitpos
+= offset
<< 3;
1585 expr
->type
= EXPR_SLICE
;
1586 expr
->r_nrbits
= member
->bit_size
;
1587 expr
->r_bitpos
+= member
->bit_offset
;
1588 expr
->ctype
= member
;
1592 deref
= deref
->unop
;
1593 expr
->deref
= deref
;
1595 add
= evaluate_offset(deref
, offset
);
1596 expr
->type
= EXPR_PREOP
;
1600 expr
->ctype
= member
;
1604 static int is_promoted(struct expression
*expr
)
1607 switch (expr
->type
) {
1610 case EXPR_CONDITIONAL
:
1634 static struct symbol
*evaluate_cast(struct expression
*);
1636 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1638 struct symbol
*sym
= expr
->cast_type
;
1640 sym
= evaluate_expression(expr
->cast_expression
);
1644 * Expressions of restricted types will possibly get
1645 * promoted - check that here
1647 if (is_restricted_type(sym
)) {
1648 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1652 examine_symbol_type(sym
);
1653 if (is_bitfield_type(sym
)) {
1654 warning(expr
->pos
, "trying to examine bitfield type");
1660 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1662 struct symbol
*type
;
1665 type
= evaluate_type_information(expr
);
1669 size
= type
->bit_size
;
1671 warning(expr
->pos
, "cannot size expression");
1672 expr
->type
= EXPR_VALUE
;
1673 expr
->value
= size
>> 3;
1674 expr
->ctype
= size_t_ctype
;
1675 return size_t_ctype
;
1678 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1680 struct symbol
*type
;
1683 type
= evaluate_type_information(expr
);
1687 if (type
->type
== SYM_NODE
)
1688 type
= type
->ctype
.base_type
;
1691 switch (type
->type
) {
1695 type
= type
->ctype
.base_type
;
1699 warning(expr
->pos
, "expected pointer expression");
1702 size
= type
->bit_size
;
1705 expr
->type
= EXPR_VALUE
;
1706 expr
->value
= size
>> 3;
1707 expr
->ctype
= size_t_ctype
;
1708 return size_t_ctype
;
1711 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1713 struct symbol
*type
;
1715 type
= evaluate_type_information(expr
);
1719 expr
->type
= EXPR_VALUE
;
1720 expr
->value
= type
->ctype
.alignment
;
1721 expr
->ctype
= size_t_ctype
;
1722 return size_t_ctype
;
1725 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1727 struct expression
*expr
;
1728 struct symbol_list
*argument_types
= fn
->arguments
;
1729 struct symbol
*argtype
;
1732 PREPARE_PTR_LIST(argument_types
, argtype
);
1733 FOR_EACH_PTR (head
, expr
) {
1734 struct expression
**p
= THIS_ADDRESS(expr
);
1735 struct symbol
*ctype
, *target
;
1736 ctype
= evaluate_expression(expr
);
1741 ctype
= degenerate(expr
);
1744 if (!target
&& ctype
->bit_size
< bits_in_int
)
1745 target
= &int_ctype
;
1747 static char where
[30];
1748 examine_symbol_type(target
);
1749 sprintf(where
, "argument %d", i
);
1750 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1754 NEXT_PTR_LIST(argtype
);
1755 } END_FOR_EACH_PTR(expr
);
1756 FINISH_PTR_LIST(argtype
);
1760 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1762 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1764 struct expression
*entry
= *ep
;
1765 struct expression
**parent
, *reuse
= NULL
;
1766 unsigned long offset
;
1768 unsigned long from
, to
;
1769 int accept_string
= is_byte_type(ctype
);
1774 if (entry
->type
== EXPR_INDEX
) {
1775 from
= entry
->idx_from
;
1776 to
= entry
->idx_to
+1;
1777 parent
= &entry
->idx_expression
;
1779 entry
= entry
->idx_expression
;
1782 offset
= from
* (ctype
->bit_size
>>3);
1784 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1785 reuse
->type
= EXPR_POS
;
1786 reuse
->ctype
= ctype
;
1787 reuse
->init_offset
= offset
;
1788 reuse
->init_nr
= to
- from
;
1789 reuse
->init_expr
= entry
;
1790 parent
= &reuse
->init_expr
;
1795 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1796 sym
= evaluate_expression(entry
);
1797 to
= from
+ get_expression_value(sym
->array_size
);
1799 evaluate_initializer(ctype
, parent
);
1804 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1806 struct expression
*entry
;
1809 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1810 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1811 } END_FOR_EACH_PTR(entry
);
1814 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1815 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1817 if (expression_list_size(expr
->expr_list
) != 1) {
1818 warning(expr
->pos
, "unexpected compound initializer");
1821 evaluate_array_initializer(ctype
, expr
);
1825 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1829 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1830 if (sym
->ident
== ident
)
1832 } END_FOR_EACH_PTR(sym
);
1836 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1838 struct expression
*entry
= *ep
;
1839 struct expression
**parent
;
1840 struct expression
*reuse
= NULL
;
1841 unsigned long offset
;
1844 error(entry
->pos
, "unknown named initializer");
1848 if (entry
->type
== EXPR_IDENTIFIER
) {
1850 entry
= entry
->ident_expression
;
1854 offset
= sym
->offset
;
1857 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1858 reuse
->type
= EXPR_POS
;
1860 reuse
->init_offset
= offset
;
1862 reuse
->init_expr
= entry
;
1863 parent
= &reuse
->init_expr
;
1867 evaluate_initializer(sym
, parent
);
1871 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1873 struct expression
*entry
;
1876 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1877 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1878 if (entry
->type
== EXPR_IDENTIFIER
) {
1879 struct ident
*ident
= entry
->expr_ident
;
1880 /* We special-case the "already right place" case */
1881 if (!sym
|| sym
->ident
!= ident
) {
1882 RESET_PTR_LIST(sym
);
1886 if (sym
->ident
== ident
)
1892 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1895 } END_FOR_EACH_PTR(entry
);
1896 FINISH_PTR_LIST(sym
);
1900 * Initializers are kind of like assignments. Except
1901 * they can be a hell of a lot more complex.
1903 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1905 struct expression
*expr
= *ep
;
1908 * Simple non-structure/array initializers are the simple
1909 * case, and look (and parse) largely like assignments.
1911 switch (expr
->type
) {
1913 int is_string
= expr
->type
== EXPR_STRING
;
1914 struct symbol
*rtype
= evaluate_expression(expr
);
1918 * char array[] = "string"
1919 * should _not_ degenerate.
1921 if (!is_string
|| !is_string_type(ctype
))
1922 rtype
= degenerate(expr
);
1923 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1928 case EXPR_INITIALIZER
:
1929 expr
->ctype
= ctype
;
1930 if (ctype
->type
== SYM_NODE
)
1931 ctype
= ctype
->ctype
.base_type
;
1933 switch (ctype
->type
) {
1936 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1939 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1942 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1945 evaluate_scalar_initializer(ctype
, expr
);
1949 case EXPR_IDENTIFIER
:
1950 if (ctype
->type
== SYM_NODE
)
1951 ctype
= ctype
->ctype
.base_type
;
1952 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1953 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1957 evaluate_one_struct_initializer(ctype
, ep
,
1958 find_struct_ident(ctype
, expr
->expr_ident
));
1962 if (ctype
->type
== SYM_NODE
)
1963 ctype
= ctype
->ctype
.base_type
;
1964 if (ctype
->type
!= SYM_ARRAY
) {
1965 error(expr
->pos
, "expected array");
1968 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1973 * An EXPR_POS expression has already been evaluated, and we don't
1974 * need to do anything more
1980 static int get_as(struct symbol
*sym
)
1988 mod
= sym
->ctype
.modifiers
;
1989 if (sym
->type
== SYM_NODE
) {
1990 sym
= sym
->ctype
.base_type
;
1991 as
|= sym
->ctype
.as
;
1992 mod
|= sym
->ctype
.modifiers
;
1996 * At least for now, allow casting to a "unsigned long".
1997 * That's how we do things like pointer arithmetic and
1998 * store pointers to registers.
2000 if (sym
== &ulong_ctype
)
2003 if (sym
&& sym
->type
== SYM_PTR
) {
2004 sym
= sym
->ctype
.base_type
;
2005 as
|= sym
->ctype
.as
;
2006 mod
|= sym
->ctype
.modifiers
;
2008 if (mod
& MOD_FORCE
)
2013 static struct symbol
*evaluate_cast(struct expression
*expr
)
2015 struct expression
*target
= expr
->cast_expression
;
2016 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2022 expr
->ctype
= ctype
;
2023 expr
->cast_type
= ctype
;
2026 * Special case: a cast can be followed by an
2027 * initializer, in which case we need to pass
2028 * the type value down to that initializer rather
2029 * than trying to evaluate it as an expression
2031 * A more complex case is when the initializer is
2032 * dereferenced as part of a post-fix expression.
2033 * We need to produce an expression that can be dereferenced.
2035 if (target
->type
== EXPR_INITIALIZER
) {
2036 struct symbol
*sym
= expr
->cast_type
;
2037 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2039 sym
->initializer
= expr
->cast_expression
;
2040 evaluate_symbol(sym
);
2042 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2045 expr
->type
= EXPR_PREOP
;
2053 evaluate_expression(target
);
2057 * You can always throw a value away by casting to
2058 * "void" - that's an implicit "force". Note that
2059 * the same is _not_ true of "void *".
2061 if (ctype
== &void_ctype
)
2065 if (type
== SYM_NODE
) {
2066 type
= ctype
->ctype
.base_type
->type
;
2067 if (ctype
->ctype
.base_type
== &void_ctype
)
2070 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2071 warning(expr
->pos
, "cast to non-scalar");
2073 if (!target
->ctype
) {
2074 warning(expr
->pos
, "cast from unknown type");
2078 type
= target
->ctype
->type
;
2079 if (type
== SYM_NODE
)
2080 type
= target
->ctype
->ctype
.base_type
->type
;
2081 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2082 warning(expr
->pos
, "cast from non-scalar");
2084 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2085 warning(expr
->pos
, "cast removes address space of expression");
2087 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2088 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2089 if (t1
->type
== SYM_NODE
)
2090 t1
= t1
->ctype
.base_type
;
2091 if (t2
->type
== SYM_NODE
)
2092 t2
= t2
->ctype
.base_type
;
2094 if (t1
->type
== SYM_RESTRICT
)
2095 warning(expr
->pos
, "cast to restricted type");
2096 if (t2
->type
== SYM_RESTRICT
)
2097 warning(expr
->pos
, "cast from restricted type");
2102 * Casts of constant values are special: they
2103 * can be NULL, and thus need to be simplified
2106 if (target
->type
== EXPR_VALUE
)
2107 cast_value(expr
, ctype
, target
, target
->ctype
);
2114 * Evaluate a call expression with a symbol. This
2115 * should expand inline functions, and evaluate
2118 static int evaluate_symbol_call(struct expression
*expr
)
2120 struct expression
*fn
= expr
->fn
;
2121 struct symbol
*ctype
= fn
->ctype
;
2123 if (fn
->type
!= EXPR_PREOP
)
2126 if (ctype
->op
&& ctype
->op
->evaluate
)
2127 return ctype
->op
->evaluate(expr
);
2129 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2131 struct symbol
*curr
= current_fn
;
2132 current_fn
= ctype
->ctype
.base_type
;
2133 examine_fn_arguments(current_fn
);
2135 ret
= inline_function(expr
, ctype
);
2137 /* restore the old function */
2145 static struct symbol
*evaluate_call(struct expression
*expr
)
2148 struct symbol
*ctype
, *sym
;
2149 struct expression
*fn
= expr
->fn
;
2150 struct expression_list
*arglist
= expr
->args
;
2152 if (!evaluate_expression(fn
))
2154 sym
= ctype
= fn
->ctype
;
2155 if (ctype
->type
== SYM_NODE
)
2156 ctype
= ctype
->ctype
.base_type
;
2157 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2158 ctype
= ctype
->ctype
.base_type
;
2159 if (!evaluate_arguments(sym
, ctype
, arglist
))
2161 if (ctype
->type
!= SYM_FN
) {
2162 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2165 args
= expression_list_size(expr
->args
);
2166 fnargs
= symbol_list_size(ctype
->arguments
);
2168 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2169 if (args
> fnargs
&& !ctype
->variadic
)
2170 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2171 if (sym
->type
== SYM_NODE
) {
2172 if (evaluate_symbol_call(expr
))
2175 expr
->ctype
= ctype
->ctype
.base_type
;
2179 struct symbol
*evaluate_expression(struct expression
*expr
)
2186 switch (expr
->type
) {
2189 warning(expr
->pos
, "value expression without a type");
2192 return evaluate_string(expr
);
2194 return evaluate_symbol_expression(expr
);
2196 if (!evaluate_expression(expr
->left
))
2198 if (!evaluate_expression(expr
->right
))
2200 return evaluate_binop(expr
);
2202 return evaluate_logical(expr
);
2204 evaluate_expression(expr
->left
);
2205 if (!evaluate_expression(expr
->right
))
2207 return evaluate_comma(expr
);
2209 if (!evaluate_expression(expr
->left
))
2211 if (!evaluate_expression(expr
->right
))
2213 return evaluate_compare(expr
);
2214 case EXPR_ASSIGNMENT
:
2215 if (!evaluate_expression(expr
->left
))
2217 if (!evaluate_expression(expr
->right
))
2219 return evaluate_assignment(expr
);
2221 if (!evaluate_expression(expr
->unop
))
2223 return evaluate_preop(expr
);
2225 if (!evaluate_expression(expr
->unop
))
2227 return evaluate_postop(expr
);
2229 case EXPR_IMPLIED_CAST
:
2230 return evaluate_cast(expr
);
2232 return evaluate_sizeof(expr
);
2233 case EXPR_PTRSIZEOF
:
2234 return evaluate_ptrsizeof(expr
);
2236 return evaluate_alignof(expr
);
2238 return evaluate_member_dereference(expr
);
2240 return evaluate_call(expr
);
2242 case EXPR_CONDITIONAL
:
2243 return evaluate_conditional_expression(expr
);
2244 case EXPR_STATEMENT
:
2245 expr
->ctype
= evaluate_statement(expr
->statement
);
2249 expr
->ctype
= &ptr_ctype
;
2253 /* Evaluate the type of the symbol .. */
2254 evaluate_symbol(expr
->symbol
);
2255 /* .. but the type of the _expression_ is a "type" */
2256 expr
->ctype
= &type_ctype
;
2259 /* These can not exist as stand-alone expressions */
2260 case EXPR_INITIALIZER
:
2261 case EXPR_IDENTIFIER
:
2264 warning(expr
->pos
, "internal front-end error: initializer in expression");
2267 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2273 static void check_duplicates(struct symbol
*sym
)
2275 struct symbol
*next
= sym
;
2277 while ((next
= next
->same_symbol
) != NULL
) {
2278 const char *typediff
;
2279 evaluate_symbol(next
);
2280 typediff
= type_difference(sym
, next
, 0, 0);
2282 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2283 show_ident(sym
->ident
),
2284 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2290 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2292 struct symbol
*base_type
;
2297 sym
= examine_symbol_type(sym
);
2298 base_type
= sym
->ctype
.base_type
;
2302 /* Evaluate the initializers */
2303 if (sym
->initializer
)
2304 evaluate_initializer(sym
, &sym
->initializer
);
2306 /* And finally, evaluate the body of the symbol too */
2307 if (base_type
->type
== SYM_FN
) {
2308 struct symbol
*curr
= current_fn
;
2310 current_fn
= base_type
;
2312 examine_fn_arguments(base_type
);
2313 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2315 if (base_type
->stmt
)
2316 evaluate_statement(base_type
->stmt
);
2324 void evaluate_symbol_list(struct symbol_list
*list
)
2328 FOR_EACH_PTR(list
, sym
) {
2329 check_duplicates(sym
);
2330 evaluate_symbol(sym
);
2331 } END_FOR_EACH_PTR(sym
);
2334 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2336 struct expression
*expr
= stmt
->expression
;
2337 struct symbol
*ctype
, *fntype
;
2339 evaluate_expression(expr
);
2340 ctype
= degenerate(expr
);
2341 fntype
= current_fn
->ctype
.base_type
;
2342 if (!fntype
|| fntype
== &void_ctype
) {
2343 if (expr
&& ctype
!= &void_ctype
)
2344 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2349 warning(stmt
->pos
, "return with no return value");
2354 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2358 static void evaluate_if_statement(struct statement
*stmt
)
2360 if (!stmt
->if_conditional
)
2363 evaluate_conditional(stmt
->if_conditional
, 0);
2364 evaluate_statement(stmt
->if_true
);
2365 evaluate_statement(stmt
->if_false
);
2368 static void evaluate_iterator(struct statement
*stmt
)
2370 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2371 evaluate_conditional(stmt
->iterator_post_condition
,1);
2372 evaluate_statement(stmt
->iterator_pre_statement
);
2373 evaluate_statement(stmt
->iterator_statement
);
2374 evaluate_statement(stmt
->iterator_post_statement
);
2377 struct symbol
*evaluate_statement(struct statement
*stmt
)
2382 switch (stmt
->type
) {
2384 return evaluate_return_expression(stmt
);
2386 case STMT_EXPRESSION
:
2387 if (!evaluate_expression(stmt
->expression
))
2389 return degenerate(stmt
->expression
);
2391 case STMT_COMPOUND
: {
2392 struct statement
*s
;
2393 struct symbol
*type
= NULL
;
2396 /* Evaluate each symbol in the compound statement */
2397 FOR_EACH_PTR(stmt
->syms
, sym
) {
2398 evaluate_symbol(sym
);
2399 } END_FOR_EACH_PTR(sym
);
2400 evaluate_symbol(stmt
->ret
);
2403 * Then, evaluate each statement, making the type of the
2404 * compound statement be the type of the last statement
2407 FOR_EACH_PTR(stmt
->stmts
, s
) {
2408 type
= evaluate_statement(s
);
2409 } END_FOR_EACH_PTR(s
);
2415 evaluate_if_statement(stmt
);
2418 evaluate_iterator(stmt
);
2421 evaluate_expression(stmt
->switch_expression
);
2422 evaluate_statement(stmt
->switch_statement
);
2425 evaluate_expression(stmt
->case_expression
);
2426 evaluate_expression(stmt
->case_to
);
2427 evaluate_statement(stmt
->case_statement
);
2430 return evaluate_statement(stmt
->label_statement
);
2432 evaluate_expression(stmt
->goto_expression
);
2437 /* FIXME! Do the asm parameter evaluation! */
2440 evaluate_expression(stmt
->expression
);