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 sparse_error(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 sparse_error(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 struct symbol
*orig_type
= type
;
103 unsigned long mod
= type
->ctype
.modifiers
;
106 if (type
->type
== SYM_NODE
)
107 type
= type
->ctype
.base_type
;
108 if (type
->type
== SYM_ENUM
)
109 type
= type
->ctype
.base_type
;
110 width
= type
->bit_size
;
113 * Bitfields always promote to the base type,
114 * even if the bitfield might be bigger than
117 if (type
->type
== SYM_BITFIELD
) {
118 type
= type
->ctype
.base_type
;
121 mod
= type
->ctype
.modifiers
;
122 if (width
< bits_in_int
)
125 /* If char/short has as many bits as int, it still gets "promoted" */
126 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
127 if (mod
& MOD_UNSIGNED
)
135 * integer part of usual arithmetic conversions:
136 * integer promotions are applied
137 * if left and right are identical, we are done
138 * if signedness is the same, convert one with lower rank
139 * unless unsigned argument has rank lower than signed one, convert the
141 * if signed argument is bigger than unsigned one, convert the unsigned.
142 * otherwise, convert signed.
144 * Leaving aside the integer promotions, that is equivalent to
145 * if identical, don't convert
146 * if left is bigger than right, convert right
147 * if right is bigger than left, convert right
148 * otherwise, if signedness is the same, convert one with lower rank
149 * otherwise convert the signed one.
151 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
153 unsigned long lmod
, rmod
;
155 left
= integer_promotion(left
);
156 right
= integer_promotion(right
);
161 if (left
->bit_size
> right
->bit_size
)
164 if (right
->bit_size
> left
->bit_size
)
167 lmod
= left
->ctype
.modifiers
;
168 rmod
= right
->ctype
.modifiers
;
169 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
170 if (lmod
& MOD_UNSIGNED
)
172 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
180 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
182 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
185 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
187 unsigned long mod
, as
;
191 mod
|= node
->ctype
.modifiers
;
192 as
|= node
->ctype
.as
;
193 if (node
->type
== SYM_NODE
) {
194 node
= node
->ctype
.base_type
;
199 *modp
= mod
& ~MOD_IGNORE
;
204 static int is_same_type(struct expression
*expr
, struct symbol
*new)
206 struct symbol
*old
= expr
->ctype
;
207 unsigned long oldmod
, newmod
, oldas
, newas
;
209 old
= base_type(old
, &oldmod
, &oldas
);
210 new = base_type(new, &newmod
, &newas
);
212 /* Same base type, same address space? */
213 if (old
== new && oldas
== newas
) {
214 unsigned long difmod
;
216 /* Check the modifier bits. */
217 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
219 /* Exact same type? */
224 * Not the same type, but differs only in "const".
225 * Don't warn about MOD_NOCAST.
227 if (difmod
== MOD_CONST
)
230 if ((oldmod
| newmod
) & MOD_NOCAST
) {
231 const char *tofrom
= "to/from";
232 if (!(newmod
& MOD_NOCAST
))
234 if (!(oldmod
& MOD_NOCAST
))
236 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
242 * This gets called for implicit casts in assignments and
243 * integer promotion. We often want to try to move the
244 * cast down, because the ops involved may have been
245 * implicitly cast up, and we can get rid of the casts
248 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
250 struct expression
*expr
;
252 if (is_same_type(old
, type
))
256 * See if we can simplify the op. Move the cast down.
260 if (old
->op
== '~') {
262 old
->unop
= cast_to(old
->unop
, type
);
267 case EXPR_IMPLIED_CAST
:
268 if (old
->ctype
->bit_size
>= type
->bit_size
) {
269 struct expression
*orig
= old
->cast_expression
;
270 if (same_cast_type(orig
->ctype
, type
))
272 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
274 old
->cast_type
= type
;
284 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
286 expr
->cast_type
= type
;
287 expr
->cast_expression
= old
;
291 static int is_type_type(struct symbol
*type
)
293 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
296 int is_ptr_type(struct symbol
*type
)
298 if (type
->type
== SYM_NODE
)
299 type
= type
->ctype
.base_type
;
300 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
303 static inline int is_float_type(struct symbol
*type
)
305 if (type
->type
== SYM_NODE
)
306 type
= type
->ctype
.base_type
;
307 return type
->ctype
.base_type
== &fp_type
;
310 static inline int is_byte_type(struct symbol
*type
)
312 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
315 static inline int is_string_type(struct symbol
*type
)
317 if (type
->type
== SYM_NODE
)
318 type
= type
->ctype
.base_type
;
319 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
322 static struct symbol
*bad_expr_type(struct expression
*expr
)
324 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
325 switch (expr
->type
) {
328 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
329 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
333 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
342 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
344 struct expression
*left
= *lp
, *right
= *rp
;
345 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
347 if (ltype
->type
== SYM_NODE
)
348 ltype
= ltype
->ctype
.base_type
;
349 if (rtype
->type
== SYM_NODE
)
350 rtype
= rtype
->ctype
.base_type
;
351 if (is_float_type(ltype
)) {
352 if (is_int_type(rtype
))
354 if (is_float_type(rtype
)) {
355 unsigned long lmod
= ltype
->ctype
.modifiers
;
356 unsigned long rmod
= rtype
->ctype
.modifiers
;
357 lmod
&= MOD_LONG
| MOD_LONGLONG
;
358 rmod
&= MOD_LONG
| MOD_LONGLONG
;
368 if (!is_float_type(rtype
) || !is_int_type(ltype
))
371 *lp
= cast_to(left
, rtype
);
374 *rp
= cast_to(right
, ltype
);
378 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
380 struct expression
*left
= *lp
, *right
= *rp
;
381 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
383 if (ltype
->type
== SYM_NODE
)
384 ltype
= ltype
->ctype
.base_type
;
385 if (rtype
->type
== SYM_NODE
)
386 rtype
= rtype
->ctype
.base_type
;
387 if (is_int_type(ltype
) && is_int_type(rtype
)) {
388 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
390 *lp
= cast_to(left
, ctype
);
391 *rp
= cast_to(right
, ctype
);
397 static int restricted_value(struct expression
*v
, struct symbol
*type
)
399 if (v
->type
!= EXPR_VALUE
)
406 static int restricted_binop(int op
, struct symbol
*type
)
415 case SPECIAL_NOTEQUAL
:
416 case SPECIAL_AND_ASSIGN
:
417 case SPECIAL_OR_ASSIGN
:
418 case SPECIAL_XOR_ASSIGN
:
425 static int restricted_unop(int op
, struct symbol
*type
)
427 if (op
== '~' && type
->bit_size
>= bits_in_int
)
434 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
436 struct expression
*left
= *lp
, *right
= *rp
;
437 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
438 struct symbol
*type
= NULL
;
440 if (ltype
->type
== SYM_NODE
)
441 ltype
= ltype
->ctype
.base_type
;
442 if (ltype
->type
== SYM_ENUM
)
443 ltype
= ltype
->ctype
.base_type
;
444 if (rtype
->type
== SYM_NODE
)
445 rtype
= rtype
->ctype
.base_type
;
446 if (rtype
->type
== SYM_ENUM
)
447 rtype
= rtype
->ctype
.base_type
;
448 if (is_restricted_type(ltype
)) {
449 if (is_restricted_type(rtype
)) {
453 if (!restricted_value(right
, ltype
))
456 } else if (is_restricted_type(rtype
)) {
457 if (!restricted_value(left
, rtype
))
462 if (restricted_binop(op
, type
))
467 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
469 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
470 if (!ctype
&& float_ok
)
471 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
473 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
478 return bad_expr_type(expr
);
481 static inline int lvalue_expression(struct expression
*expr
)
483 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
486 static int ptr_object_size(struct symbol
*ptr_type
)
488 if (ptr_type
->type
== SYM_NODE
)
489 ptr_type
= ptr_type
->ctype
.base_type
;
490 if (ptr_type
->type
== SYM_PTR
)
491 ptr_type
= get_base_type(ptr_type
);
492 return ptr_type
->bit_size
;
495 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
497 struct expression
*i
= *ip
;
498 struct symbol
*ptr_type
= ctype
;
501 if (ptr_type
->type
== SYM_NODE
)
502 ptr_type
= ptr_type
->ctype
.base_type
;
504 if (!is_int_type(i
->ctype
))
505 return bad_expr_type(expr
);
507 examine_symbol_type(ctype
);
509 if (!ctype
->ctype
.base_type
) {
510 sparse_error(expr
->pos
, "missing type information");
514 /* Get the size of whatever the pointer points to */
515 bit_size
= ptr_object_size(ctype
);
517 if (bit_size
> bits_in_char
) {
518 int multiply
= bit_size
>> 3;
519 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
521 if (i
->type
== EXPR_VALUE
) {
522 val
->value
= i
->value
* multiply
;
523 val
->ctype
= size_t_ctype
;
526 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
528 val
->ctype
= size_t_ctype
;
529 val
->value
= bit_size
>> 3;
532 mul
->ctype
= size_t_ctype
;
544 static struct symbol
*evaluate_add(struct expression
*expr
)
546 struct expression
*left
= expr
->left
, *right
= expr
->right
;
547 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
549 if (is_ptr_type(ltype
))
550 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
552 if (is_ptr_type(rtype
))
553 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
555 return evaluate_arith(expr
, 1);
558 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
559 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
562 unsigned long mod1
, mod2
, diff
;
563 unsigned long as1
, as2
;
565 struct symbol
*base1
, *base2
;
567 if (target
== source
)
569 if (!target
|| !source
)
570 return "different types";
572 * Peel of per-node information.
573 * FIXME! Check alignment and context too here!
575 mod1
= target
->ctype
.modifiers
;
576 as1
= target
->ctype
.as
;
577 mod2
= source
->ctype
.modifiers
;
578 as2
= source
->ctype
.as
;
579 if (target
->type
== SYM_NODE
) {
580 target
= target
->ctype
.base_type
;
583 if (target
->type
== SYM_PTR
) {
587 mod1
|= target
->ctype
.modifiers
;
588 as1
|= target
->ctype
.as
;
590 if (source
->type
== SYM_NODE
) {
591 source
= source
->ctype
.base_type
;
594 if (source
->type
== SYM_PTR
) {
598 mod2
|= source
->ctype
.modifiers
;
599 as2
|= source
->ctype
.as
;
601 if (target
->type
== SYM_ENUM
) {
602 target
= target
->ctype
.base_type
;
606 if (source
->type
== SYM_ENUM
) {
607 source
= source
->ctype
.base_type
;
612 if (target
== source
)
614 if (!target
|| !source
)
615 return "different types";
617 type1
= target
->type
;
618 base1
= target
->ctype
.base_type
;
620 type2
= source
->type
;
621 base2
= source
->ctype
.base_type
;
624 * Pointers to functions compare as the function itself
626 if (type1
== SYM_PTR
&& base1
) {
627 base1
= examine_symbol_type(base1
);
628 switch (base1
->type
) {
632 base1
= base1
->ctype
.base_type
;
637 if (type2
== SYM_PTR
&& base2
) {
638 base2
= examine_symbol_type(base2
);
639 switch (base2
->type
) {
643 base2
= base2
->ctype
.base_type
;
649 /* Arrays degenerate to pointers for type comparisons */
650 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
651 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
653 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
654 return "different base types";
656 /* Must be same address space to be comparable */
658 return "different address spaces";
660 /* Ignore differences in storage types or addressability */
661 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
662 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
665 return "different type sizes";
666 if (diff
& ~MOD_SIGNEDNESS
)
667 return "different modifiers";
669 /* Differs in signedness only.. */
672 * Warn if both are explicitly signed ("unsigned" is obvously
673 * always explicit, and since we know one of them has to be
674 * unsigned, we check if the signed one was explicit).
676 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
677 return "different explicit signedness";
680 * "char" matches both "unsigned char" and "signed char",
681 * so if the explicit test didn't trigger, then we should
682 * not warn about a char.
684 if (!(mod1
& MOD_CHAR
))
685 return "different signedness";
689 if (type1
== SYM_FN
) {
691 struct symbol
*arg1
, *arg2
;
692 if (base1
->variadic
!= base2
->variadic
)
693 return "incompatible variadic arguments";
694 PREPARE_PTR_LIST(target
->arguments
, arg1
);
695 PREPARE_PTR_LIST(source
->arguments
, arg2
);
699 diff
= type_difference(arg1
, arg2
, 0, 0);
701 static char argdiff
[80];
702 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
711 FINISH_PTR_LIST(arg2
);
712 FINISH_PTR_LIST(arg1
);
721 static int is_null_ptr(struct expression
*expr
)
723 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
725 if (!is_ptr_type(expr
->ctype
))
726 warning(expr
->pos
, "Using plain integer as NULL pointer");
730 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
732 /* NULL expression? Just return the type of the "other side" */
741 * Ignore differences in "volatile" and "const"ness when
742 * subtracting pointers
744 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
746 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
748 const char *typediff
;
749 struct symbol
*ctype
;
750 struct symbol
*ltype
, *rtype
;
751 struct expression
*r
= *rp
;
753 ltype
= degenerate(l
);
754 rtype
= degenerate(r
);
757 * If it is an integer subtract: the ptr add case will do the
760 if (!is_ptr_type(rtype
))
761 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
764 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
766 ctype
= common_ptr_type(l
, r
);
768 sparse_error(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
772 examine_symbol_type(ctype
);
774 /* Figure out the base type we point to */
775 if (ctype
->type
== SYM_NODE
)
776 ctype
= ctype
->ctype
.base_type
;
777 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
778 sparse_error(expr
->pos
, "subtraction of functions? Share your drugs");
781 ctype
= get_base_type(ctype
);
783 expr
->ctype
= ssize_t_ctype
;
784 if (ctype
->bit_size
> bits_in_char
) {
785 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
786 struct expression
*div
= expr
;
787 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
788 unsigned long value
= ctype
->bit_size
>> 3;
790 val
->ctype
= size_t_ctype
;
793 if (value
& (value
-1)) {
794 if (Wptr_subtraction_blows
)
795 warning(expr
->pos
, "potentially expensive pointer subtraction");
799 sub
->ctype
= ssize_t_ctype
;
808 return ssize_t_ctype
;
811 static struct symbol
*evaluate_sub(struct expression
*expr
)
813 struct expression
*left
= expr
->left
;
814 struct symbol
*ltype
= left
->ctype
;
816 if (is_ptr_type(ltype
))
817 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
819 return evaluate_arith(expr
, 1);
822 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
824 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
826 struct symbol
*ctype
;
831 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
832 warning(expr
->pos
, "assignment expression in conditional");
834 ctype
= evaluate_expression(expr
);
836 if (is_safe_type(ctype
))
837 warning(expr
->pos
, "testing a 'safe expression'");
843 static struct symbol
*evaluate_logical(struct expression
*expr
)
845 if (!evaluate_conditional(expr
->left
, 0))
847 if (!evaluate_conditional(expr
->right
, 0))
850 expr
->ctype
= &bool_ctype
;
854 static struct symbol
*evaluate_shift(struct expression
*expr
)
856 struct expression
*left
= expr
->left
, *right
= expr
->right
;
857 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
859 if (ltype
->type
== SYM_NODE
)
860 ltype
= ltype
->ctype
.base_type
;
861 if (rtype
->type
== SYM_NODE
)
862 rtype
= rtype
->ctype
.base_type
;
863 if (is_int_type(ltype
) && is_int_type(rtype
)) {
864 struct symbol
*ctype
= integer_promotion(ltype
);
865 expr
->left
= cast_to(expr
->left
, ctype
);
867 ctype
= integer_promotion(rtype
);
868 expr
->right
= cast_to(expr
->right
, ctype
);
871 return bad_expr_type(expr
);
874 static struct symbol
*evaluate_binop(struct expression
*expr
)
877 // addition can take ptr+int, fp and int
879 return evaluate_add(expr
);
881 // subtraction can take ptr-ptr, fp and int
883 return evaluate_sub(expr
);
885 // Arithmetic operations can take fp and int
887 return evaluate_arith(expr
, 1);
889 // shifts do integer promotions, but that's it.
890 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
891 return evaluate_shift(expr
);
893 // The rest are integer operations
894 // '%', '&', '^', '|'
896 return evaluate_arith(expr
, 0);
900 static struct symbol
*evaluate_comma(struct expression
*expr
)
902 expr
->ctype
= expr
->right
->ctype
;
906 static int modify_for_unsigned(int op
)
909 op
= SPECIAL_UNSIGNED_LT
;
911 op
= SPECIAL_UNSIGNED_GT
;
912 else if (op
== SPECIAL_LTE
)
913 op
= SPECIAL_UNSIGNED_LTE
;
914 else if (op
== SPECIAL_GTE
)
915 op
= SPECIAL_UNSIGNED_GTE
;
919 static struct symbol
*evaluate_compare(struct expression
*expr
)
921 struct expression
*left
= expr
->left
, *right
= expr
->right
;
922 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
923 struct symbol
*ctype
;
926 if (is_type_type(ltype
) && is_type_type(rtype
))
929 if (is_safe_type(ltype
) || is_safe_type(rtype
))
930 warning(expr
->pos
, "testing a 'safe expression'");
933 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
934 // FIXME! Check the types for compatibility
935 expr
->op
= modify_for_unsigned(expr
->op
);
939 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
941 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
942 expr
->op
= modify_for_unsigned(expr
->op
);
946 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
950 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
952 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
953 expr
->op
= modify_for_unsigned(expr
->op
);
960 expr
->ctype
= &bool_ctype
;
965 * FIXME!! This should do casts, array degeneration etc..
967 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
969 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
971 if (ltype
->type
== SYM_NODE
)
972 ltype
= ltype
->ctype
.base_type
;
974 if (rtype
->type
== SYM_NODE
)
975 rtype
= rtype
->ctype
.base_type
;
977 if (ltype
->type
== SYM_PTR
) {
978 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
982 if (rtype
->type
== SYM_PTR
) {
983 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
990 * NOTE! The degenerate case of "x ? : y", where we don't
991 * have a true case, this will possibly promote "x" to the
992 * same type as "y", and thus _change_ the conditional
993 * test in the expression. But since promotion is "safe"
994 * for testing, that's ok.
996 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
998 struct expression
**true;
999 struct symbol
*ctype
, *ltype
, *rtype
;
1000 const char * typediff
;
1002 if (!evaluate_conditional(expr
->conditional
, 0))
1004 if (!evaluate_expression(expr
->cond_false
))
1007 ctype
= degenerate(expr
->conditional
);
1008 rtype
= degenerate(expr
->cond_false
);
1010 true = &expr
->conditional
;
1012 if (expr
->cond_true
) {
1013 if (!evaluate_expression(expr
->cond_true
))
1015 ltype
= degenerate(expr
->cond_true
);
1016 true = &expr
->cond_true
;
1019 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
1022 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
1025 ctype
= compatible_float_binop(true, &expr
->cond_false
);
1028 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
1032 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
1035 sparse_error(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
1039 expr
->ctype
= ctype
;
1043 /* FP assignments can not do modulo or bit operations */
1044 static int compatible_float_op(int op
)
1047 op
== SPECIAL_ADD_ASSIGN
||
1048 op
== SPECIAL_SUB_ASSIGN
||
1049 op
== SPECIAL_MUL_ASSIGN
||
1050 op
== SPECIAL_DIV_ASSIGN
;
1053 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1054 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
1056 const char *typediff
;
1060 if (is_int_type(target
)) {
1061 if (is_int_type(source
))
1063 if (is_float_type(source
))
1065 } else if (is_float_type(target
)) {
1066 if (!compatible_float_op(op
)) {
1067 sparse_error(expr
->pos
, "invalid assignment");
1070 if (is_int_type(source
))
1072 if (is_float_type(source
))
1074 } else if (is_restricted_type(target
)) {
1075 if (restricted_binop(op
, target
)) {
1076 sparse_error(expr
->pos
, "bad restricted assignment");
1079 if (!restricted_value(*rp
, target
))
1081 } else if (is_ptr_type(target
)) {
1082 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1083 evaluate_ptr_add(expr
, target
, rp
);
1087 sparse_error(expr
->pos
, "invalid pointer assignment");
1090 } else if (op
!= '=') {
1091 sparse_error(expr
->pos
, "invalid assignment");
1095 /* It's ok if the target is more volatile or const than the source */
1096 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1100 /* Pointer destination? */
1102 target_as
= t
->ctype
.as
;
1103 if (t
->type
== SYM_NODE
) {
1104 t
= t
->ctype
.base_type
;
1105 target_as
|= t
->ctype
.as
;
1107 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1108 struct expression
*right
= *rp
;
1109 struct symbol
*s
= source
;
1112 // NULL pointer is always ok
1113 if (is_null_ptr(right
))
1116 /* "void *" matches anything as long as the address space is ok */
1117 source_as
= s
->ctype
.as
;
1118 if (s
->type
== SYM_NODE
) {
1119 s
= s
->ctype
.base_type
;
1120 source_as
|= s
->ctype
.as
;
1122 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1123 s
= get_base_type(s
);
1124 t
= get_base_type(t
);
1125 if (s
== &void_ctype
|| t
== &void_ctype
)
1130 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1131 info(expr
->pos
, " expected %s", show_typename(target
));
1132 info(expr
->pos
, " got %s", show_typename(source
));
1133 *rp
= cast_to(*rp
, target
);
1136 *rp
= cast_to(*rp
, target
);
1140 static void mark_assigned(struct expression
*expr
)
1146 switch (expr
->type
) {
1151 if (sym
->type
!= SYM_NODE
)
1153 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1157 mark_assigned(expr
->left
);
1158 mark_assigned(expr
->right
);
1161 mark_assigned(expr
->cast_expression
);
1164 mark_assigned(expr
->base
);
1172 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1174 if (type
->ctype
.modifiers
& MOD_CONST
)
1175 sparse_error(left
->pos
, "assignment to const expression");
1177 /* We know left is an lvalue, so it's a "preop-*" */
1178 mark_assigned(left
->unop
);
1181 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1183 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1184 struct expression
*where
= expr
;
1185 struct symbol
*ltype
, *rtype
;
1187 if (!lvalue_expression(left
)) {
1188 sparse_error(expr
->pos
, "not an lvalue");
1192 ltype
= left
->ctype
;
1194 rtype
= degenerate(right
);
1196 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1199 evaluate_assign_to(left
, ltype
);
1201 expr
->ctype
= ltype
;
1205 static void examine_fn_arguments(struct symbol
*fn
)
1209 FOR_EACH_PTR(fn
->arguments
, s
) {
1210 struct symbol
*arg
= evaluate_symbol(s
);
1211 /* Array/function arguments silently degenerate into pointers */
1217 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1218 if (arg
->type
== SYM_ARRAY
)
1219 ptr
->ctype
= arg
->ctype
;
1221 ptr
->ctype
.base_type
= arg
;
1222 ptr
->ctype
.as
|= s
->ctype
.as
;
1223 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1225 s
->ctype
.base_type
= ptr
;
1227 s
->ctype
.modifiers
= 0;
1230 examine_symbol_type(s
);
1237 } END_FOR_EACH_PTR(s
);
1240 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1242 /* Take the modifiers of the pointer, and apply them to the member */
1243 mod
|= sym
->ctype
.modifiers
;
1244 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1245 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1247 newsym
->ctype
.as
= as
;
1248 newsym
->ctype
.modifiers
= mod
;
1254 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1256 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1257 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1259 node
->ctype
.base_type
= ptr
;
1260 ptr
->bit_size
= bits_in_pointer
;
1261 ptr
->ctype
.alignment
= pointer_alignment
;
1263 node
->bit_size
= bits_in_pointer
;
1264 node
->ctype
.alignment
= pointer_alignment
;
1267 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1268 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1269 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1271 if (sym
->type
== SYM_NODE
) {
1272 ptr
->ctype
.as
|= sym
->ctype
.as
;
1273 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1274 sym
= sym
->ctype
.base_type
;
1276 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1277 ptr
->ctype
.as
|= sym
->ctype
.as
;
1278 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1279 sym
= sym
->ctype
.base_type
;
1281 ptr
->ctype
.base_type
= sym
;
1286 /* Arrays degenerate into pointers on pointer arithmetic */
1287 static struct symbol
*degenerate(struct expression
*expr
)
1289 struct symbol
*ctype
, *base
;
1293 ctype
= expr
->ctype
;
1296 base
= examine_symbol_type(ctype
);
1297 if (ctype
->type
== SYM_NODE
)
1298 base
= ctype
->ctype
.base_type
;
1300 * Arrays degenerate into pointers to the entries, while
1301 * functions degenerate into pointers to themselves.
1302 * If array was part of non-lvalue compound, we create a copy
1303 * of that compound first and then act as if we were dealing with
1304 * the corresponding field in there.
1306 switch (base
->type
) {
1308 if (expr
->type
== EXPR_SLICE
) {
1309 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1310 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1312 a
->ctype
.base_type
= expr
->base
->ctype
;
1313 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1314 a
->array_size
= expr
->base
->ctype
->array_size
;
1316 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1318 e0
->ctype
= &lazy_ptr_ctype
;
1320 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1323 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1325 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1327 e2
->right
= expr
->base
;
1329 e2
->ctype
= expr
->base
->ctype
;
1331 if (expr
->r_bitpos
) {
1332 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1335 e3
->right
= alloc_const_expression(expr
->pos
,
1336 expr
->r_bitpos
>> 3);
1337 e3
->ctype
= &lazy_ptr_ctype
;
1342 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1345 e4
->ctype
= &lazy_ptr_ctype
;
1348 expr
->type
= EXPR_PREOP
;
1352 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1353 sparse_error(expr
->pos
, "strange non-value function or array");
1356 *expr
= *expr
->unop
;
1357 ctype
= create_pointer(expr
, ctype
, 1);
1358 expr
->ctype
= ctype
;
1365 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1367 struct expression
*op
= expr
->unop
;
1368 struct symbol
*ctype
;
1370 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1371 sparse_error(expr
->pos
, "not addressable");
1377 if (expr
->type
== EXPR_SYMBOL
) {
1378 struct symbol
*sym
= expr
->symbol
;
1379 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1383 * symbol expression evaluation is lazy about the type
1384 * of the sub-expression, so we may have to generate
1385 * the type here if so..
1387 if (expr
->ctype
== &lazy_ptr_ctype
) {
1388 ctype
= create_pointer(expr
, ctype
, 0);
1389 expr
->ctype
= ctype
;
1395 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1397 struct expression
*op
= expr
->unop
;
1398 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1400 /* Simplify: *&(expr) => (expr) */
1401 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1406 /* Dereferencing a node drops all the node information. */
1407 if (ctype
->type
== SYM_NODE
)
1408 ctype
= ctype
->ctype
.base_type
;
1410 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1411 target
= ctype
->ctype
.base_type
;
1413 switch (ctype
->type
) {
1415 sparse_error(expr
->pos
, "cannot derefence this type");
1418 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1419 merge_type(node
, ctype
);
1423 if (!lvalue_expression(op
)) {
1424 sparse_error(op
->pos
, "non-lvalue array??");
1428 /* Do the implied "addressof" on the array */
1432 * When an array is dereferenced, we need to pick
1433 * up the attributes of the original node too..
1435 merge_type(node
, op
->ctype
);
1436 merge_type(node
, ctype
);
1440 node
->bit_size
= target
->bit_size
;
1441 node
->array_size
= target
->array_size
;
1448 * Unary post-ops: x++ and x--
1450 static struct symbol
*evaluate_postop(struct expression
*expr
)
1452 struct expression
*op
= expr
->unop
;
1453 struct symbol
*ctype
= op
->ctype
;
1455 if (!lvalue_expression(expr
->unop
)) {
1456 sparse_error(expr
->pos
, "need lvalue expression for ++/--");
1459 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1460 sparse_error(expr
->pos
, "bad operation on restricted");
1464 evaluate_assign_to(op
, ctype
);
1466 expr
->ctype
= ctype
;
1468 if (is_ptr_type(ctype
))
1469 expr
->op_value
= ptr_object_size(ctype
) >> 3;
1474 static struct symbol
*evaluate_sign(struct expression
*expr
)
1476 struct symbol
*ctype
= expr
->unop
->ctype
;
1477 if (is_int_type(ctype
)) {
1478 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1479 expr
->unop
= cast_to(expr
->unop
, rtype
);
1481 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1482 /* no conversions needed */
1483 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1484 /* no conversions needed */
1486 return bad_expr_type(expr
);
1488 if (expr
->op
== '+')
1489 *expr
= *expr
->unop
;
1490 expr
->ctype
= ctype
;
1494 static struct symbol
*evaluate_preop(struct expression
*expr
)
1496 struct symbol
*ctype
= expr
->unop
->ctype
;
1500 *expr
= *expr
->unop
;
1506 return evaluate_sign(expr
);
1509 return evaluate_dereference(expr
);
1512 return evaluate_addressof(expr
);
1514 case SPECIAL_INCREMENT
:
1515 case SPECIAL_DECREMENT
:
1517 * From a type evaluation standpoint the pre-ops are
1518 * the same as the postops
1520 return evaluate_postop(expr
);
1523 if (is_safe_type(ctype
))
1524 warning(expr
->pos
, "testing a 'safe expression'");
1525 if (is_float_type(ctype
)) {
1526 struct expression
*arg
= expr
->unop
;
1527 expr
->type
= EXPR_BINOP
;
1528 expr
->op
= SPECIAL_EQUAL
;
1530 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1531 expr
->right
->ctype
= ctype
;
1532 expr
->right
->fvalue
= 0;
1534 ctype
= &bool_ctype
;
1540 expr
->ctype
= ctype
;
1544 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1546 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1547 struct ptr_list
*list
= head
;
1553 for (i
= 0; i
< list
->nr
; i
++) {
1554 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1556 if (sym
->ident
!= ident
)
1558 *offset
= sym
->offset
;
1561 struct symbol
*ctype
= sym
->ctype
.base_type
;
1565 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1567 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1570 *offset
+= sym
->offset
;
1574 } while ((list
= list
->next
) != head
);
1578 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1580 struct expression
*add
;
1583 * Create a new add-expression
1585 * NOTE! Even if we just add zero, we need a new node
1586 * for the member pointer, since it has a different
1587 * type than the original pointer. We could make that
1588 * be just a cast, but the fact is, a node is a node,
1589 * so we might as well just do the "add zero" here.
1591 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1594 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1595 add
->right
->ctype
= &int_ctype
;
1596 add
->right
->value
= offset
;
1599 * The ctype of the pointer will be lazily evaluated if
1600 * we ever take the address of this member dereference..
1602 add
->ctype
= &lazy_ptr_ctype
;
1606 /* structure/union dereference */
1607 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1610 struct symbol
*ctype
, *member
;
1611 struct expression
*deref
= expr
->deref
, *add
;
1612 struct ident
*ident
= expr
->member
;
1616 if (!evaluate_expression(deref
))
1619 sparse_error(expr
->pos
, "bad member name");
1623 ctype
= deref
->ctype
;
1624 address_space
= ctype
->ctype
.as
;
1625 mod
= ctype
->ctype
.modifiers
;
1626 if (ctype
->type
== SYM_NODE
) {
1627 ctype
= ctype
->ctype
.base_type
;
1628 address_space
|= ctype
->ctype
.as
;
1629 mod
|= ctype
->ctype
.modifiers
;
1631 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1632 sparse_error(expr
->pos
, "expected structure or union");
1636 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1638 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1639 const char *name
= "<unnamed>";
1642 name
= ctype
->ident
->name
;
1643 namelen
= ctype
->ident
->len
;
1645 sparse_error(expr
->pos
, "no member '%s' in %s %.*s",
1646 show_ident(ident
), type
, namelen
, name
);
1651 * The member needs to take on the address space and modifiers of
1652 * the "parent" type.
1654 member
= convert_to_as_mod(member
, address_space
, mod
);
1655 ctype
= get_base_type(member
);
1657 if (!lvalue_expression(deref
)) {
1658 if (deref
->type
!= EXPR_SLICE
) {
1662 expr
->base
= deref
->base
;
1663 expr
->r_bitpos
= deref
->r_bitpos
;
1665 expr
->r_bitpos
+= offset
<< 3;
1666 expr
->type
= EXPR_SLICE
;
1667 expr
->r_nrbits
= member
->bit_size
;
1668 expr
->r_bitpos
+= member
->bit_offset
;
1669 expr
->ctype
= member
;
1673 deref
= deref
->unop
;
1674 expr
->deref
= deref
;
1676 add
= evaluate_offset(deref
, offset
);
1677 expr
->type
= EXPR_PREOP
;
1681 expr
->ctype
= member
;
1685 static int is_promoted(struct expression
*expr
)
1688 switch (expr
->type
) {
1691 case EXPR_CONDITIONAL
:
1715 static struct symbol
*evaluate_cast(struct expression
*);
1717 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1719 struct symbol
*sym
= expr
->cast_type
;
1721 sym
= evaluate_expression(expr
->cast_expression
);
1725 * Expressions of restricted types will possibly get
1726 * promoted - check that here
1728 if (is_restricted_type(sym
)) {
1729 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1733 examine_symbol_type(sym
);
1734 if (is_bitfield_type(sym
)) {
1735 sparse_error(expr
->pos
, "trying to examine bitfield type");
1741 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1743 struct symbol
*type
;
1746 type
= evaluate_type_information(expr
);
1750 size
= type
->bit_size
;
1751 if ((size
< 0) || (size
& 7))
1752 sparse_error(expr
->pos
, "cannot size expression");
1753 expr
->type
= EXPR_VALUE
;
1754 expr
->value
= size
>> 3;
1755 expr
->ctype
= size_t_ctype
;
1756 return size_t_ctype
;
1759 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1761 struct symbol
*type
;
1764 type
= evaluate_type_information(expr
);
1768 if (type
->type
== SYM_NODE
)
1769 type
= type
->ctype
.base_type
;
1772 switch (type
->type
) {
1776 type
= get_base_type(type
);
1780 sparse_error(expr
->pos
, "expected pointer expression");
1783 size
= type
->bit_size
;
1786 expr
->type
= EXPR_VALUE
;
1787 expr
->value
= size
>> 3;
1788 expr
->ctype
= size_t_ctype
;
1789 return size_t_ctype
;
1792 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1794 struct symbol
*type
;
1796 type
= evaluate_type_information(expr
);
1800 expr
->type
= EXPR_VALUE
;
1801 expr
->value
= type
->ctype
.alignment
;
1802 expr
->ctype
= size_t_ctype
;
1803 return size_t_ctype
;
1806 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1808 struct expression
*expr
;
1809 struct symbol_list
*argument_types
= fn
->arguments
;
1810 struct symbol
*argtype
;
1813 PREPARE_PTR_LIST(argument_types
, argtype
);
1814 FOR_EACH_PTR (head
, expr
) {
1815 struct expression
**p
= THIS_ADDRESS(expr
);
1816 struct symbol
*ctype
, *target
;
1817 ctype
= evaluate_expression(expr
);
1822 ctype
= degenerate(expr
);
1825 if (!target
&& ctype
->bit_size
< bits_in_int
)
1826 target
= &int_ctype
;
1828 static char where
[30];
1829 examine_symbol_type(target
);
1830 sprintf(where
, "argument %d", i
);
1831 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1835 NEXT_PTR_LIST(argtype
);
1836 } END_FOR_EACH_PTR(expr
);
1837 FINISH_PTR_LIST(argtype
);
1841 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1843 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1845 struct expression
*entry
= *ep
;
1846 struct expression
**parent
, *reuse
= NULL
;
1847 unsigned long offset
;
1849 unsigned long from
, to
;
1850 int accept_string
= is_byte_type(ctype
);
1855 if (entry
->type
== EXPR_INDEX
) {
1856 from
= entry
->idx_from
;
1857 to
= entry
->idx_to
+1;
1858 parent
= &entry
->idx_expression
;
1860 entry
= entry
->idx_expression
;
1863 offset
= from
* (ctype
->bit_size
>>3);
1865 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1866 reuse
->type
= EXPR_POS
;
1867 reuse
->ctype
= ctype
;
1868 reuse
->init_offset
= offset
;
1869 reuse
->init_nr
= to
- from
;
1870 reuse
->init_expr
= entry
;
1871 parent
= &reuse
->init_expr
;
1876 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1877 sym
= evaluate_expression(entry
);
1878 to
= from
+ get_expression_value(sym
->array_size
);
1880 evaluate_initializer(ctype
, parent
);
1885 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1887 struct expression
*entry
;
1890 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1891 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1892 } END_FOR_EACH_PTR(entry
);
1895 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1896 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1898 if (expression_list_size(expr
->expr_list
) != 1) {
1899 sparse_error(expr
->pos
, "unexpected compound initializer");
1902 evaluate_array_initializer(ctype
, expr
);
1906 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1910 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1911 if (sym
->ident
== ident
)
1913 } END_FOR_EACH_PTR(sym
);
1917 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1919 struct expression
*entry
= *ep
;
1920 struct expression
**parent
;
1921 struct expression
*reuse
= NULL
;
1922 unsigned long offset
;
1925 sparse_error(entry
->pos
, "unknown named initializer");
1929 if (entry
->type
== EXPR_IDENTIFIER
) {
1931 entry
= entry
->ident_expression
;
1935 offset
= sym
->offset
;
1938 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1939 reuse
->type
= EXPR_POS
;
1941 reuse
->init_offset
= offset
;
1943 reuse
->init_expr
= entry
;
1944 parent
= &reuse
->init_expr
;
1948 evaluate_initializer(sym
, parent
);
1952 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1954 struct expression
*entry
;
1957 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1958 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1959 if (entry
->type
== EXPR_IDENTIFIER
) {
1960 struct ident
*ident
= entry
->expr_ident
;
1961 /* We special-case the "already right place" case */
1962 if (!sym
|| sym
->ident
!= ident
) {
1963 RESET_PTR_LIST(sym
);
1967 if (sym
->ident
== ident
)
1973 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1976 } END_FOR_EACH_PTR(entry
);
1977 FINISH_PTR_LIST(sym
);
1981 * Initializers are kind of like assignments. Except
1982 * they can be a hell of a lot more complex.
1984 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1986 struct expression
*expr
= *ep
;
1989 * Simple non-structure/array initializers are the simple
1990 * case, and look (and parse) largely like assignments.
1992 switch (expr
->type
) {
1994 int is_string
= expr
->type
== EXPR_STRING
;
1995 struct symbol
*rtype
= evaluate_expression(expr
);
1999 * char array[] = "string"
2000 * should _not_ degenerate.
2002 if (!is_string
|| !is_string_type(ctype
))
2003 rtype
= degenerate(expr
);
2004 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
2009 case EXPR_INITIALIZER
:
2010 expr
->ctype
= ctype
;
2011 if (ctype
->type
== SYM_NODE
)
2012 ctype
= ctype
->ctype
.base_type
;
2014 switch (ctype
->type
) {
2017 evaluate_array_initializer(get_base_type(ctype
), expr
);
2020 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
2023 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
2026 evaluate_scalar_initializer(ctype
, expr
);
2030 case EXPR_IDENTIFIER
:
2031 if (ctype
->type
== SYM_NODE
)
2032 ctype
= ctype
->ctype
.base_type
;
2033 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2034 sparse_error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2038 evaluate_one_struct_initializer(ctype
, ep
,
2039 find_struct_ident(ctype
, expr
->expr_ident
));
2043 if (ctype
->type
== SYM_NODE
)
2044 ctype
= ctype
->ctype
.base_type
;
2045 if (ctype
->type
!= SYM_ARRAY
) {
2046 sparse_error(expr
->pos
, "expected array");
2049 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2054 * An EXPR_POS expression has already been evaluated, and we don't
2055 * need to do anything more
2061 static int get_as(struct symbol
*sym
)
2069 mod
= sym
->ctype
.modifiers
;
2070 if (sym
->type
== SYM_NODE
) {
2071 sym
= sym
->ctype
.base_type
;
2072 as
|= sym
->ctype
.as
;
2073 mod
|= sym
->ctype
.modifiers
;
2077 * At least for now, allow casting to a "unsigned long".
2078 * That's how we do things like pointer arithmetic and
2079 * store pointers to registers.
2081 if (sym
== &ulong_ctype
)
2084 if (sym
&& sym
->type
== SYM_PTR
) {
2085 sym
= get_base_type(sym
);
2086 as
|= sym
->ctype
.as
;
2087 mod
|= sym
->ctype
.modifiers
;
2089 if (mod
& MOD_FORCE
)
2094 static void cast_to_as(struct expression
*e
, int as
)
2096 struct expression
*v
= e
->cast_expression
;
2098 if (!Wcast_to_address_space
)
2101 /* cast from constant 0 to pointer is OK */
2102 if (v
->type
== EXPR_VALUE
&& is_int_type(v
->ctype
) && !v
->value
)
2105 warning(e
->pos
, "cast adds address space to expression (<asn:%d>)", as
);
2108 static struct symbol
*evaluate_cast(struct expression
*expr
)
2110 struct expression
*target
= expr
->cast_expression
;
2111 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2112 struct symbol
*t1
, *t2
;
2113 enum type type1
, type2
;
2119 expr
->ctype
= ctype
;
2120 expr
->cast_type
= ctype
;
2123 * Special case: a cast can be followed by an
2124 * initializer, in which case we need to pass
2125 * the type value down to that initializer rather
2126 * than trying to evaluate it as an expression
2128 * A more complex case is when the initializer is
2129 * dereferenced as part of a post-fix expression.
2130 * We need to produce an expression that can be dereferenced.
2132 if (target
->type
== EXPR_INITIALIZER
) {
2133 struct symbol
*sym
= expr
->cast_type
;
2134 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2136 sym
->initializer
= expr
->cast_expression
;
2137 evaluate_symbol(sym
);
2139 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2142 expr
->type
= EXPR_PREOP
;
2150 evaluate_expression(target
);
2154 if (t1
->type
== SYM_NODE
)
2155 t1
= t1
->ctype
.base_type
;
2156 if (t1
->type
== SYM_ENUM
)
2157 t1
= t1
->ctype
.base_type
;
2160 * You can always throw a value away by casting to
2161 * "void" - that's an implicit "force". Note that
2162 * the same is _not_ true of "void *".
2164 if (t1
== &void_ctype
)
2168 if (type1
== SYM_ARRAY
|| type1
== SYM_UNION
|| type1
== SYM_STRUCT
)
2169 warning(expr
->pos
, "cast to non-scalar");
2173 sparse_error(expr
->pos
, "cast from unknown type");
2176 if (t2
->type
== SYM_NODE
)
2177 t2
= t2
->ctype
.base_type
;
2178 if (t2
->type
== SYM_ENUM
)
2179 t2
= t2
->ctype
.base_type
;
2182 if (type2
== SYM_ARRAY
|| type2
== SYM_UNION
|| type2
== SYM_STRUCT
)
2183 warning(expr
->pos
, "cast from non-scalar");
2185 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
) && t1
!= t2
) {
2186 if (t1
->type
== SYM_RESTRICT
)
2187 warning(expr
->pos
, "cast to restricted type");
2188 if (t2
->type
== SYM_RESTRICT
)
2189 warning(expr
->pos
, "cast from restricted type");
2192 as1
= get_as(ctype
);
2193 as2
= get_as(target
->ctype
);
2194 if (!as1
&& as2
> 0)
2195 warning(expr
->pos
, "cast removes address space of expression");
2196 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2197 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2198 if (as1
> 0 && !as2
)
2199 cast_to_as(expr
, as1
);
2202 * Casts of constant values are special: they
2203 * can be NULL, and thus need to be simplified
2206 if (target
->type
== EXPR_VALUE
)
2207 cast_value(expr
, ctype
, target
, target
->ctype
);
2214 * Evaluate a call expression with a symbol. This
2215 * should expand inline functions, and evaluate
2218 static int evaluate_symbol_call(struct expression
*expr
)
2220 struct expression
*fn
= expr
->fn
;
2221 struct symbol
*ctype
= fn
->ctype
;
2223 if (fn
->type
!= EXPR_PREOP
)
2226 if (ctype
->op
&& ctype
->op
->evaluate
)
2227 return ctype
->op
->evaluate(expr
);
2229 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2231 struct symbol
*curr
= current_fn
;
2232 current_fn
= ctype
->ctype
.base_type
;
2233 examine_fn_arguments(current_fn
);
2235 ret
= inline_function(expr
, ctype
);
2237 /* restore the old function */
2245 static struct symbol
*evaluate_call(struct expression
*expr
)
2248 struct symbol
*ctype
, *sym
;
2249 struct expression
*fn
= expr
->fn
;
2250 struct expression_list
*arglist
= expr
->args
;
2252 if (!evaluate_expression(fn
))
2254 sym
= ctype
= fn
->ctype
;
2255 if (ctype
->type
== SYM_NODE
)
2256 ctype
= ctype
->ctype
.base_type
;
2257 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2258 ctype
= get_base_type(ctype
);
2259 if (!evaluate_arguments(sym
, ctype
, arglist
))
2261 if (ctype
->type
!= SYM_FN
) {
2262 sparse_error(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2265 args
= expression_list_size(expr
->args
);
2266 fnargs
= symbol_list_size(ctype
->arguments
);
2268 sparse_error(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2269 if (args
> fnargs
&& !ctype
->variadic
)
2270 sparse_error(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2271 if (sym
->type
== SYM_NODE
) {
2272 if (evaluate_symbol_call(expr
))
2275 expr
->ctype
= ctype
->ctype
.base_type
;
2279 struct symbol
*evaluate_expression(struct expression
*expr
)
2286 switch (expr
->type
) {
2289 sparse_error(expr
->pos
, "value expression without a type");
2292 return evaluate_string(expr
);
2294 return evaluate_symbol_expression(expr
);
2296 if (!evaluate_expression(expr
->left
))
2298 if (!evaluate_expression(expr
->right
))
2300 return evaluate_binop(expr
);
2302 return evaluate_logical(expr
);
2304 evaluate_expression(expr
->left
);
2305 if (!evaluate_expression(expr
->right
))
2307 return evaluate_comma(expr
);
2309 if (!evaluate_expression(expr
->left
))
2311 if (!evaluate_expression(expr
->right
))
2313 return evaluate_compare(expr
);
2314 case EXPR_ASSIGNMENT
:
2315 if (!evaluate_expression(expr
->left
))
2317 if (!evaluate_expression(expr
->right
))
2319 return evaluate_assignment(expr
);
2321 if (!evaluate_expression(expr
->unop
))
2323 return evaluate_preop(expr
);
2325 if (!evaluate_expression(expr
->unop
))
2327 return evaluate_postop(expr
);
2329 case EXPR_IMPLIED_CAST
:
2330 return evaluate_cast(expr
);
2332 return evaluate_sizeof(expr
);
2333 case EXPR_PTRSIZEOF
:
2334 return evaluate_ptrsizeof(expr
);
2336 return evaluate_alignof(expr
);
2338 return evaluate_member_dereference(expr
);
2340 return evaluate_call(expr
);
2342 case EXPR_CONDITIONAL
:
2343 return evaluate_conditional_expression(expr
);
2344 case EXPR_STATEMENT
:
2345 expr
->ctype
= evaluate_statement(expr
->statement
);
2349 expr
->ctype
= &ptr_ctype
;
2353 /* Evaluate the type of the symbol .. */
2354 evaluate_symbol(expr
->symbol
);
2355 /* .. but the type of the _expression_ is a "type" */
2356 expr
->ctype
= &type_ctype
;
2359 /* These can not exist as stand-alone expressions */
2360 case EXPR_INITIALIZER
:
2361 case EXPR_IDENTIFIER
:
2364 sparse_error(expr
->pos
, "internal front-end error: initializer in expression");
2367 sparse_error(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2373 static void check_duplicates(struct symbol
*sym
)
2376 struct symbol
*next
= sym
;
2378 while ((next
= next
->same_symbol
) != NULL
) {
2379 const char *typediff
;
2380 evaluate_symbol(next
);
2382 typediff
= type_difference(sym
, next
, 0, 0);
2384 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2385 show_ident(sym
->ident
),
2386 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
2391 unsigned long mod
= sym
->ctype
.modifiers
;
2392 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
2394 if (!(mod
& MOD_TOPLEVEL
))
2396 if (sym
->ident
== &main_ident
)
2398 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
2402 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
2404 struct symbol
*base_type
;
2412 sym
= examine_symbol_type(sym
);
2413 base_type
= get_base_type(sym
);
2417 /* Evaluate the initializers */
2418 if (sym
->initializer
)
2419 evaluate_initializer(sym
, &sym
->initializer
);
2421 /* And finally, evaluate the body of the symbol too */
2422 if (base_type
->type
== SYM_FN
) {
2423 struct symbol
*curr
= current_fn
;
2425 current_fn
= base_type
;
2427 examine_fn_arguments(base_type
);
2428 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2430 if (base_type
->stmt
)
2431 evaluate_statement(base_type
->stmt
);
2439 void evaluate_symbol_list(struct symbol_list
*list
)
2443 FOR_EACH_PTR(list
, sym
) {
2444 check_duplicates(sym
);
2445 evaluate_symbol(sym
);
2446 } END_FOR_EACH_PTR(sym
);
2449 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2451 struct expression
*expr
= stmt
->expression
;
2452 struct symbol
*ctype
, *fntype
;
2454 evaluate_expression(expr
);
2455 ctype
= degenerate(expr
);
2456 fntype
= current_fn
->ctype
.base_type
;
2457 if (!fntype
|| fntype
== &void_ctype
) {
2458 if (expr
&& ctype
!= &void_ctype
)
2459 sparse_error(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2464 sparse_error(stmt
->pos
, "return with no return value");
2469 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2473 static void evaluate_if_statement(struct statement
*stmt
)
2475 if (!stmt
->if_conditional
)
2478 evaluate_conditional(stmt
->if_conditional
, 0);
2479 evaluate_statement(stmt
->if_true
);
2480 evaluate_statement(stmt
->if_false
);
2483 static void evaluate_iterator(struct statement
*stmt
)
2485 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2486 evaluate_conditional(stmt
->iterator_post_condition
,1);
2487 evaluate_statement(stmt
->iterator_pre_statement
);
2488 evaluate_statement(stmt
->iterator_statement
);
2489 evaluate_statement(stmt
->iterator_post_statement
);
2492 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
2494 switch (*constraint
) {
2495 case '=': /* Assignment */
2496 case '+': /* Update */
2499 sparse_error(expr
->pos
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
2503 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
2505 switch (*constraint
) {
2506 case '=': /* Assignment */
2507 case '+': /* Update */
2508 sparse_error(expr
->pos
, "input constraint with assignment (\"%s\")", constraint
);
2512 static void evaluate_asm_statement(struct statement
*stmt
)
2514 struct expression
*expr
;
2517 expr
= stmt
->asm_string
;
2518 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2519 sparse_error(stmt
->pos
, "need constant string for inline asm");
2524 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
2525 struct ident
*ident
;
2528 case 0: /* Identifier */
2530 ident
= (struct ident
*)expr
;
2533 case 1: /* Constraint */
2535 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2536 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
2537 *THIS_ADDRESS(expr
) = NULL
;
2540 verify_output_constraint(expr
, expr
->string
->data
);
2543 case 2: /* Expression */
2545 if (!evaluate_expression(expr
))
2547 if (!lvalue_expression(expr
))
2548 warning(expr
->pos
, "asm output is not an lvalue");
2549 evaluate_assign_to(expr
, expr
->ctype
);
2552 } END_FOR_EACH_PTR(expr
);
2555 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
2556 struct ident
*ident
;
2559 case 0: /* Identifier */
2561 ident
= (struct ident
*)expr
;
2564 case 1: /* Constraint */
2566 if (!expr
|| expr
->type
!= EXPR_STRING
) {
2567 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
2568 *THIS_ADDRESS(expr
) = NULL
;
2571 verify_input_constraint(expr
, expr
->string
->data
);
2574 case 2: /* Expression */
2576 if (!evaluate_expression(expr
))
2580 } END_FOR_EACH_PTR(expr
);
2582 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
2584 sparse_error(stmt
->pos
, "bad asm output");
2587 if (expr
->type
== EXPR_STRING
)
2589 sparse_error(expr
->pos
, "asm clobber is not a string");
2590 } END_FOR_EACH_PTR(expr
);
2593 static void evaluate_case_statement(struct statement
*stmt
)
2595 evaluate_expression(stmt
->case_expression
);
2596 evaluate_expression(stmt
->case_to
);
2597 evaluate_statement(stmt
->case_statement
);
2600 static void check_case_type(struct expression
*switch_expr
, struct expression
*case_expr
)
2602 struct symbol
*switch_type
, *case_type
;
2605 switch_type
= switch_expr
->ctype
;
2606 case_type
= evaluate_expression(case_expr
);
2608 if (case_type
&& switch_type
) {
2609 /* Both integer types? */
2610 if (is_int_type(switch_type
) && is_int_type(case_type
))
2612 if (compatible_restricted_binop(SPECIAL_EQUAL
, &switch_expr
, &case_expr
))
2616 sparse_error(case_expr
->pos
, "incompatible types for 'case' statement");
2619 static void evaluate_switch_statement(struct statement
*stmt
)
2623 evaluate_expression(stmt
->switch_expression
);
2624 evaluate_statement(stmt
->switch_statement
);
2625 if (!stmt
->switch_expression
)
2627 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
2628 struct statement
*case_stmt
= sym
->stmt
;
2629 check_case_type(stmt
->switch_expression
, case_stmt
->case_expression
);
2630 check_case_type(stmt
->switch_expression
, case_stmt
->case_to
);
2631 } END_FOR_EACH_PTR(sym
);
2634 struct symbol
*evaluate_statement(struct statement
*stmt
)
2639 switch (stmt
->type
) {
2641 return evaluate_return_expression(stmt
);
2643 case STMT_EXPRESSION
:
2644 if (!evaluate_expression(stmt
->expression
))
2646 return degenerate(stmt
->expression
);
2648 case STMT_COMPOUND
: {
2649 struct statement
*s
;
2650 struct symbol
*type
= NULL
;
2653 /* Evaluate each symbol in the compound statement */
2654 FOR_EACH_PTR(stmt
->syms
, sym
) {
2655 evaluate_symbol(sym
);
2656 } END_FOR_EACH_PTR(sym
);
2657 evaluate_symbol(stmt
->ret
);
2660 * Then, evaluate each statement, making the type of the
2661 * compound statement be the type of the last statement
2664 FOR_EACH_PTR(stmt
->stmts
, s
) {
2665 type
= evaluate_statement(s
);
2666 } END_FOR_EACH_PTR(s
);
2672 evaluate_if_statement(stmt
);
2675 evaluate_iterator(stmt
);
2678 evaluate_switch_statement(stmt
);
2681 evaluate_case_statement(stmt
);
2684 return evaluate_statement(stmt
->label_statement
);
2686 evaluate_expression(stmt
->goto_expression
);
2691 evaluate_asm_statement(stmt
);
2694 evaluate_expression(stmt
->expression
);
2697 evaluate_expression(stmt
->range_expression
);
2698 evaluate_expression(stmt
->range_low
);
2699 evaluate_expression(stmt
->range_high
);