4 * Copyright (C) 2003 Transmeta Corp.
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
);
32 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
34 struct symbol
*sym
= expr
->symbol
;
35 struct symbol
*base_type
;
38 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
42 examine_symbol_type(sym
);
44 base_type
= sym
->ctype
.base_type
;
46 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
50 /* The type of a symbol is the symbol itself! */
53 /* enums can be turned into plain values */
54 if (sym
->type
!= SYM_ENUM
) {
55 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
57 addr
->symbol_name
= expr
->symbol_name
;
58 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
59 expr
->type
= EXPR_PREOP
;
63 } else if (base_type
->bit_size
< bits_in_int
) {
64 /* ugly - we need to force sizeof for these guys */
65 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
66 e
->value
= sym
->value
;
68 expr
->type
= EXPR_PREOP
;
72 expr
->type
= EXPR_VALUE
;
73 expr
->value
= sym
->value
;
75 expr
->ctype
= base_type
;
79 static struct symbol
*evaluate_string(struct expression
*expr
)
81 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
82 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
83 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
84 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
85 unsigned int length
= expr
->string
->length
;
87 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
88 sym
->bit_size
= bits_in_char
* length
;
89 sym
->ctype
.alignment
= 1;
90 sym
->ctype
.modifiers
= MOD_STATIC
;
91 sym
->ctype
.base_type
= array
;
92 sym
->initializer
= initstr
;
95 initstr
->string
= expr
->string
;
97 array
->array_size
= sym
->array_size
;
98 array
->bit_size
= bits_in_char
* length
;
99 array
->ctype
.alignment
= 1;
100 array
->ctype
.modifiers
= MOD_STATIC
;
101 array
->ctype
.base_type
= &char_ctype
;
104 addr
->ctype
= &lazy_ptr_ctype
;
106 expr
->type
= EXPR_PREOP
;
113 static inline struct symbol
*integer_promotion(struct symbol
*type
)
115 unsigned long mod
= type
->ctype
.modifiers
;
118 if (type
->type
== SYM_NODE
)
119 type
= type
->ctype
.base_type
;
120 if (type
->type
== SYM_ENUM
)
121 type
= type
->ctype
.base_type
;
122 width
= type
->bit_size
;
123 if (type
->type
== SYM_BITFIELD
)
124 type
= type
->ctype
.base_type
;
125 mod
= type
->ctype
.modifiers
;
126 if (width
< bits_in_int
)
129 /* If char/short has as many bits as int, it still gets "promoted" */
130 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
132 if (mod
& MOD_UNSIGNED
)
139 * integer part of usual arithmetic conversions:
140 * integer promotions are applied
141 * if left and right are identical, we are done
142 * if signedness is the same, convert one with lower rank
143 * unless unsigned argument has rank lower than signed one, convert the
145 * if signed argument is bigger than unsigned one, convert the unsigned.
146 * otherwise, convert signed.
148 * Leaving aside the integer promotions, that is equivalent to
149 * if identical, don't convert
150 * if left is bigger than right, convert right
151 * if right is bigger than left, convert right
152 * otherwise, if signedness is the same, convert one with lower rank
153 * otherwise convert the signed one.
155 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
157 unsigned long lmod
, rmod
;
159 left
= integer_promotion(left
);
160 right
= integer_promotion(right
);
165 if (left
->bit_size
> right
->bit_size
)
168 if (right
->bit_size
> left
->bit_size
)
171 lmod
= left
->ctype
.modifiers
;
172 rmod
= right
->ctype
.modifiers
;
173 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
174 if (lmod
& MOD_UNSIGNED
)
176 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
184 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
186 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== orig
->bit_offset
;
190 * This gets called for implicit casts in assignments and
191 * integer promotion. We often want to try to move the
192 * cast down, because the ops involved may have been
193 * implicitly cast up, and we can get rid of the casts
196 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
198 struct expression
*expr
;
201 * See if we can simplify the op. Move the cast down.
205 if (old
->op
== '~') {
207 old
->unop
= cast_to(old
->unop
, type
);
212 case EXPR_IMPLIED_CAST
:
213 if (old
->ctype
->bit_size
>= type
->bit_size
) {
214 struct expression
*orig
= old
->cast_expression
;
215 if (same_cast_type(orig
->ctype
, type
))
217 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
219 old
->cast_type
= type
;
229 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
231 expr
->cast_type
= type
;
232 expr
->cast_expression
= old
;
236 static int is_type_type(struct symbol
*type
)
238 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
241 static int is_ptr_type(struct symbol
*type
)
243 if (type
->type
== SYM_NODE
)
244 type
= type
->ctype
.base_type
;
245 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
248 static inline int is_float_type(struct symbol
*type
)
250 if (type
->type
== SYM_NODE
)
251 type
= type
->ctype
.base_type
;
252 return type
->ctype
.base_type
== &fp_type
;
255 static inline int is_byte_type(struct symbol
*type
)
257 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
260 static inline int is_string_type(struct symbol
*type
)
262 if (type
->type
== SYM_NODE
)
263 type
= type
->ctype
.base_type
;
264 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
267 static struct symbol
*bad_expr_type(struct expression
*expr
)
269 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
270 switch (expr
->type
) {
273 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
274 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
278 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
287 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
289 struct expression
*left
= *lp
, *right
= *rp
;
290 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
292 if (ltype
->type
== SYM_NODE
)
293 ltype
= ltype
->ctype
.base_type
;
294 if (rtype
->type
== SYM_NODE
)
295 rtype
= rtype
->ctype
.base_type
;
296 if (is_float_type(ltype
)) {
297 if (is_int_type(rtype
))
299 if (is_float_type(rtype
)) {
300 unsigned long lmod
= ltype
->ctype
.modifiers
;
301 unsigned long rmod
= rtype
->ctype
.modifiers
;
302 lmod
&= MOD_LONG
| MOD_LONGLONG
;
303 rmod
&= MOD_LONG
| MOD_LONGLONG
;
313 if (!is_float_type(rtype
) || !is_int_type(ltype
))
316 *lp
= cast_to(left
, rtype
);
319 *rp
= cast_to(right
, ltype
);
323 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
325 struct expression
*left
= *lp
, *right
= *rp
;
326 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
328 if (ltype
->type
== SYM_NODE
)
329 ltype
= ltype
->ctype
.base_type
;
330 if (rtype
->type
== SYM_NODE
)
331 rtype
= rtype
->ctype
.base_type
;
332 if (is_int_type(ltype
) && is_int_type(rtype
)) {
333 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
335 /* Don't bother promoting same-size entities, it only adds clutter */
336 if (ltype
->bit_size
!= ctype
->bit_size
)
337 *lp
= cast_to(left
, ctype
);
338 if (rtype
->bit_size
!= ctype
->bit_size
)
339 *rp
= cast_to(right
, ctype
);
345 static int restricted_value(struct expression
*v
, struct symbol
*type
)
347 if (v
->type
!= EXPR_VALUE
)
354 static int restricted_binop(int op
, struct symbol
*type
)
362 case SPECIAL_NOTEQUAL
:
369 static int restricted_unop(int op
, struct symbol
*type
)
371 if (op
== '~' && type
->bit_size
>= bits_in_int
)
378 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
380 struct expression
*left
= *lp
, *right
= *rp
;
381 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
382 struct symbol
*type
= NULL
;
384 if (ltype
->type
== SYM_NODE
)
385 ltype
= ltype
->ctype
.base_type
;
386 if (ltype
->type
== SYM_ENUM
)
387 ltype
= ltype
->ctype
.base_type
;
388 if (rtype
->type
== SYM_NODE
)
389 rtype
= rtype
->ctype
.base_type
;
390 if (rtype
->type
== SYM_ENUM
)
391 rtype
= rtype
->ctype
.base_type
;
392 if (is_restricted_type(ltype
)) {
393 if (is_restricted_type(rtype
)) {
397 if (!restricted_value(right
, ltype
))
400 } else if (is_restricted_type(rtype
)) {
401 if (!restricted_value(left
, rtype
))
406 if (restricted_binop(op
, type
))
411 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
413 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
414 if (!ctype
&& float_ok
)
415 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
417 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
422 return bad_expr_type(expr
);
425 static inline int lvalue_expression(struct expression
*expr
)
427 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
430 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ip
)
432 struct expression
*i
= *ip
;
433 struct symbol
*ptr_type
= ctype
;
436 if (ptr_type
->type
== SYM_NODE
)
437 ptr_type
= ptr_type
->ctype
.base_type
;
439 if (!is_int_type(i
->ctype
))
440 return bad_expr_type(expr
);
442 examine_symbol_type(ctype
);
444 if (!ctype
->ctype
.base_type
) {
445 warning(expr
->pos
, "missing type information");
449 /* Get the size of whatever the pointer points to */
451 if (ptr_type
->type
== SYM_NODE
)
452 ptr_type
= ptr_type
->ctype
.base_type
;
453 if (ptr_type
->type
== SYM_PTR
)
454 ptr_type
= ptr_type
->ctype
.base_type
;
455 bit_size
= ptr_type
->bit_size
;
457 if (i
->type
== EXPR_VALUE
) {
458 i
->value
*= bit_size
>> 3;
459 } else if (bit_size
> bits_in_char
) {
460 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
461 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
463 val
->ctype
= size_t_ctype
;
464 val
->value
= bit_size
>> 3;
467 mul
->ctype
= size_t_ctype
;
478 static struct symbol
*evaluate_add(struct expression
*expr
)
480 struct expression
*left
= expr
->left
, *right
= expr
->right
;
481 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
483 if (is_ptr_type(ltype
))
484 return evaluate_ptr_add(expr
, degenerate(left
), &expr
->right
);
486 if (is_ptr_type(rtype
))
487 return evaluate_ptr_add(expr
, degenerate(right
), &expr
->left
);
489 return evaluate_arith(expr
, 1);
492 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
493 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
494 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
496 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
497 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
500 unsigned long mod1
, mod2
, diff
;
501 unsigned long as1
, as2
;
503 struct symbol
*base1
, *base2
;
505 if (target
== source
)
507 if (!target
|| !source
)
508 return "different types";
510 * Peel of per-node information.
511 * FIXME! Check alignment and context too here!
513 mod1
= target
->ctype
.modifiers
;
514 as1
= target
->ctype
.as
;
515 mod2
= source
->ctype
.modifiers
;
516 as2
= source
->ctype
.as
;
517 if (target
->type
== SYM_NODE
) {
518 target
= target
->ctype
.base_type
;
521 if (target
->type
== SYM_PTR
) {
525 mod1
|= target
->ctype
.modifiers
;
526 as1
|= target
->ctype
.as
;
528 if (source
->type
== SYM_NODE
) {
529 source
= source
->ctype
.base_type
;
532 if (source
->type
== SYM_PTR
) {
536 mod2
|= source
->ctype
.modifiers
;
537 as2
|= source
->ctype
.as
;
539 if (target
->type
== SYM_ENUM
) {
540 target
= target
->ctype
.base_type
;
544 if (source
->type
== SYM_ENUM
) {
545 source
= source
->ctype
.base_type
;
550 if (target
== source
)
552 if (!target
|| !source
)
553 return "different types";
555 type1
= target
->type
;
556 base1
= target
->ctype
.base_type
;
558 type2
= source
->type
;
559 base2
= source
->ctype
.base_type
;
562 * Pointers to functions compare as the function itself
564 if (type1
== SYM_PTR
&& base1
) {
565 switch (base1
->type
) {
569 base1
= base1
->ctype
.base_type
;
574 if (type2
== SYM_PTR
&& base2
) {
575 switch (base2
->type
) {
579 base2
= base2
->ctype
.base_type
;
585 /* Arrays degenerate to pointers for type comparisons */
586 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
587 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
589 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
590 return "different base types";
592 /* Must be same address space to be comparable */
594 return "different address spaces";
596 /* Ignore differences in storage types or addressability */
597 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
598 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
601 return "different type sizes";
602 if (diff
& ~MOD_SIGNEDNESS
)
603 return "different modifiers";
605 /* Differs in signedness only.. */
608 * Warn if both are explicitly signed ("unsigned" is obvously
609 * always explicit, and since we know one of them has to be
610 * unsigned, we check if the signed one was explicit).
612 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
613 return "different explicit signedness";
616 * "char" matches both "unsigned char" and "signed char",
617 * so if the explicit test didn't trigger, then we should
618 * not warn about a char.
620 if (!(mod1
& MOD_CHAR
))
621 return "different signedness";
625 if (type1
== SYM_FN
) {
627 struct symbol
*arg1
, *arg2
;
628 if (base1
->variadic
!= base2
->variadic
)
629 return "incompatible variadic arguments";
630 PREPARE_PTR_LIST(target
->arguments
, arg1
);
631 PREPARE_PTR_LIST(source
->arguments
, arg2
);
635 diff
= type_difference(arg1
, arg2
, 0, 0);
637 static char argdiff
[80];
638 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
647 FINISH_PTR_LIST(arg2
);
648 FINISH_PTR_LIST(arg1
);
657 static int is_null_ptr(struct expression
*expr
)
659 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
661 if (!is_ptr_type(expr
->ctype
))
662 warning(expr
->pos
, "Using plain integer as NULL pointer");
666 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
668 /* NULL expression? Just return the type of the "other side" */
677 * Ignore differences in "volatile" and "const"ness when
678 * subtracting pointers
680 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
682 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
**rp
)
684 const char *typediff
;
685 struct symbol
*ctype
;
686 struct symbol
*ltype
, *rtype
;
687 struct expression
*r
= *rp
;
689 ltype
= degenerate(l
);
690 rtype
= degenerate(r
);
693 * If it is an integer subtract: the ptr add case will do the
696 if (!is_ptr_type(rtype
))
697 return evaluate_ptr_add(expr
, degenerate(l
), rp
);
700 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
702 ctype
= common_ptr_type(l
, r
);
704 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
708 examine_symbol_type(ctype
);
710 /* Figure out the base type we point to */
711 if (ctype
->type
== SYM_NODE
)
712 ctype
= ctype
->ctype
.base_type
;
713 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
714 warning(expr
->pos
, "subtraction of functions? Share your drugs");
717 ctype
= ctype
->ctype
.base_type
;
719 expr
->ctype
= ssize_t_ctype
;
720 if (ctype
->bit_size
> bits_in_char
) {
721 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
722 struct expression
*div
= expr
;
723 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
725 val
->ctype
= size_t_ctype
;
726 val
->value
= ctype
->bit_size
>> 3;
729 sub
->ctype
= ssize_t_ctype
;
738 return ssize_t_ctype
;
741 static struct symbol
*evaluate_sub(struct expression
*expr
)
743 struct expression
*left
= expr
->left
;
744 struct symbol
*ltype
= left
->ctype
;
746 if (is_ptr_type(ltype
))
747 return evaluate_ptr_sub(expr
, left
, &expr
->right
);
749 return evaluate_arith(expr
, 1);
752 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
754 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
756 struct symbol
*ctype
;
761 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
762 warning(expr
->pos
, "assignment expression in conditional");
764 ctype
= evaluate_expression(expr
);
766 if (is_safe_type(ctype
))
767 warning(expr
->pos
, "testing a 'safe expression'");
773 static struct symbol
*evaluate_logical(struct expression
*expr
)
775 if (!evaluate_conditional(expr
->left
, 0))
777 if (!evaluate_conditional(expr
->right
, 0))
780 expr
->ctype
= &bool_ctype
;
784 static struct symbol
*evaluate_shift(struct expression
*expr
)
786 struct expression
*left
= expr
->left
, *right
= expr
->right
;
787 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
789 if (ltype
->type
== SYM_NODE
)
790 ltype
= ltype
->ctype
.base_type
;
791 if (rtype
->type
== SYM_NODE
)
792 rtype
= rtype
->ctype
.base_type
;
793 if (is_int_type(ltype
) && is_int_type(rtype
)) {
794 struct symbol
*ctype
= integer_promotion(ltype
);
795 if (ltype
->bit_size
!= ctype
->bit_size
)
796 expr
->left
= cast_to(expr
->left
, ctype
);
798 ctype
= integer_promotion(rtype
);
799 if (rtype
->bit_size
!= ctype
->bit_size
)
800 expr
->right
= cast_to(expr
->right
, ctype
);
803 return bad_expr_type(expr
);
806 static struct symbol
*evaluate_binop(struct expression
*expr
)
809 // addition can take ptr+int, fp and int
811 return evaluate_add(expr
);
813 // subtraction can take ptr-ptr, fp and int
815 return evaluate_sub(expr
);
817 // Arithmetic operations can take fp and int
819 return evaluate_arith(expr
, 1);
821 // shifts do integer promotions, but that's it.
822 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
823 return evaluate_shift(expr
);
825 // The rest are integer operations
826 // '%', '&', '^', '|'
828 return evaluate_arith(expr
, 0);
832 static struct symbol
*evaluate_comma(struct expression
*expr
)
834 expr
->ctype
= expr
->right
->ctype
;
838 static int modify_for_unsigned(int op
)
841 op
= SPECIAL_UNSIGNED_LT
;
843 op
= SPECIAL_UNSIGNED_GT
;
844 else if (op
== SPECIAL_LTE
)
845 op
= SPECIAL_UNSIGNED_LTE
;
846 else if (op
== SPECIAL_GTE
)
847 op
= SPECIAL_UNSIGNED_GTE
;
851 static struct symbol
*evaluate_compare(struct expression
*expr
)
853 struct expression
*left
= expr
->left
, *right
= expr
->right
;
854 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
855 struct symbol
*ctype
;
858 if (is_type_type(ltype
) && is_type_type(rtype
))
861 if (is_safe_type(ltype
) || is_safe_type(rtype
))
862 warning(expr
->pos
, "testing a 'safe expression'");
865 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
866 // FIXME! Check the types for compatibility
870 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
872 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
873 expr
->op
= modify_for_unsigned(expr
->op
);
877 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
881 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
888 expr
->ctype
= &bool_ctype
;
893 * FIXME!! This should do casts, array degeneration etc..
895 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
897 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
899 if (ltype
->type
== SYM_NODE
)
900 ltype
= ltype
->ctype
.base_type
;
902 if (rtype
->type
== SYM_NODE
)
903 rtype
= rtype
->ctype
.base_type
;
905 if (ltype
->type
== SYM_PTR
) {
906 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
910 if (rtype
->type
== SYM_PTR
) {
911 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
918 * NOTE! The degenerate case of "x ? : y", where we don't
919 * have a true case, this will possibly promote "x" to the
920 * same type as "y", and thus _change_ the conditional
921 * test in the expression. But since promotion is "safe"
922 * for testing, that's ok.
924 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
926 struct expression
**true;
927 struct symbol
*ctype
, *ltype
, *rtype
;
928 const char * typediff
;
930 if (!evaluate_conditional(expr
->conditional
, 0))
932 if (!evaluate_expression(expr
->cond_false
))
935 ctype
= degenerate(expr
->conditional
);
936 rtype
= degenerate(expr
->cond_false
);
938 true = &expr
->conditional
;
940 if (expr
->cond_true
) {
941 if (!evaluate_expression(expr
->cond_true
))
943 ltype
= degenerate(expr
->cond_true
);
944 true = &expr
->cond_true
;
947 ctype
= compatible_integer_binop(true, &expr
->cond_false
);
950 ctype
= compatible_ptr_type(*true, expr
->cond_false
);
953 ctype
= compatible_float_binop(true, &expr
->cond_false
);
956 ctype
= compatible_restricted_binop('?', true, &expr
->cond_false
);
960 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
963 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
971 /* FP assignments can not do modulo or bit operations */
972 static int compatible_float_op(int op
)
975 op
== SPECIAL_ADD_ASSIGN
||
976 op
== SPECIAL_SUB_ASSIGN
||
977 op
== SPECIAL_MUL_ASSIGN
||
978 op
== SPECIAL_DIV_ASSIGN
;
981 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
982 struct expression
**rp
, struct symbol
*source
, const char *where
, int op
)
984 const char *typediff
;
988 if (is_int_type(target
)) {
989 if (is_int_type(source
)) {
990 if (target
->bit_size
!= source
->bit_size
)
992 if (target
->bit_offset
!= source
->bit_offset
)
996 if (is_float_type(source
))
998 } else if (is_float_type(target
)) {
999 if (!compatible_float_op(op
)) {
1000 warning(expr
->pos
, "invalid assignment");
1003 if (is_int_type(source
))
1005 if (is_float_type(source
)) {
1006 if (target
->bit_size
!= source
->bit_size
)
1010 } else if (is_ptr_type(target
)) {
1011 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1012 evaluate_ptr_add(expr
, target
, rp
);
1018 warning(expr
->pos
, "invalid assignment");
1022 /* It's ok if the target is more volatile or const than the source */
1023 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
1027 if (is_restricted_type(target
) && !restricted_value(*rp
, target
))
1030 /* Pointer destination? */
1032 target_as
= t
->ctype
.as
;
1033 if (t
->type
== SYM_NODE
) {
1034 t
= t
->ctype
.base_type
;
1035 target_as
|= t
->ctype
.as
;
1037 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1038 struct expression
*right
= *rp
;
1039 struct symbol
*s
= source
;
1042 // NULL pointer is always ok
1043 if (is_null_ptr(right
))
1046 /* "void *" matches anything as long as the address space is ok */
1047 source_as
= s
->ctype
.as
;
1048 if (s
->type
== SYM_NODE
) {
1049 s
= s
->ctype
.base_type
;
1050 source_as
|= s
->ctype
.as
;
1052 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1053 s
= s
->ctype
.base_type
;
1054 t
= t
->ctype
.base_type
;
1055 if (s
== &void_ctype
|| t
== &void_ctype
)
1060 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1061 info(expr
->pos
, " expected %s", show_typename(target
));
1062 info(expr
->pos
, " got %s", show_typename(source
));
1063 *rp
= cast_to(*rp
, target
);
1066 *rp
= cast_to(*rp
, target
);
1070 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1072 if (type
->ctype
.modifiers
& MOD_CONST
)
1073 warning(left
->pos
, "assignment to const expression");
1074 if (type
->type
== SYM_NODE
)
1075 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1078 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1080 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1081 struct expression
*where
= expr
;
1082 struct symbol
*ltype
, *rtype
;
1084 if (!lvalue_expression(left
)) {
1085 warning(expr
->pos
, "not an lvalue");
1089 ltype
= left
->ctype
;
1091 rtype
= degenerate(right
);
1093 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment", expr
->op
))
1096 evaluate_assign_to(left
, ltype
);
1098 expr
->ctype
= ltype
;
1102 static void examine_fn_arguments(struct symbol
*fn
)
1106 FOR_EACH_PTR(fn
->arguments
, s
) {
1107 struct symbol
*arg
= evaluate_symbol(s
);
1108 /* Array/function arguments silently degenerate into pointers */
1114 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1115 if (arg
->type
== SYM_ARRAY
)
1116 ptr
->ctype
= arg
->ctype
;
1118 ptr
->ctype
.base_type
= arg
;
1119 ptr
->ctype
.as
|= s
->ctype
.as
;
1120 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1122 s
->ctype
.base_type
= ptr
;
1124 s
->ctype
.modifiers
= 0;
1126 examine_symbol_type(s
);
1133 } END_FOR_EACH_PTR(s
);
1136 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1138 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1139 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1141 newsym
->ctype
.as
= as
;
1142 newsym
->ctype
.modifiers
= mod
;
1148 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1150 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1151 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1153 node
->ctype
.base_type
= ptr
;
1154 ptr
->bit_size
= bits_in_pointer
;
1155 ptr
->ctype
.alignment
= pointer_alignment
;
1157 node
->bit_size
= bits_in_pointer
;
1158 node
->ctype
.alignment
= pointer_alignment
;
1161 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1162 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1163 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1164 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1166 if (sym
->type
== SYM_NODE
) {
1167 ptr
->ctype
.as
|= sym
->ctype
.as
;
1168 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1169 sym
= sym
->ctype
.base_type
;
1171 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1172 ptr
->ctype
.as
|= sym
->ctype
.as
;
1173 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1174 sym
= sym
->ctype
.base_type
;
1176 ptr
->ctype
.base_type
= sym
;
1181 /* Arrays degenerate into pointers on pointer arithmetic */
1182 static struct symbol
*degenerate(struct expression
*expr
)
1184 struct symbol
*ctype
, *base
;
1188 ctype
= expr
->ctype
;
1192 if (ctype
->type
== SYM_NODE
)
1193 base
= ctype
->ctype
.base_type
;
1195 * Arrays degenerate into pointers to the entries, while
1196 * functions degenerate into pointers to themselves.
1197 * If array was part of non-lvalue compound, we create a copy
1198 * of that compound first and then act as if we were dealing with
1199 * the corresponding field in there.
1201 switch (base
->type
) {
1203 if (expr
->type
== EXPR_SLICE
) {
1204 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1205 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1207 a
->ctype
.base_type
= expr
->base
->ctype
;
1208 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1209 a
->array_size
= expr
->base
->ctype
->array_size
;
1211 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1213 e0
->ctype
= &lazy_ptr_ctype
;
1215 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1218 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1220 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1222 e2
->right
= expr
->base
;
1224 e2
->ctype
= expr
->base
->ctype
;
1226 if (expr
->r_bitpos
) {
1227 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1230 e3
->right
= alloc_const_expression(expr
->pos
,
1231 expr
->r_bitpos
>> 3);
1232 e3
->ctype
= &lazy_ptr_ctype
;
1237 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1240 e4
->ctype
= &lazy_ptr_ctype
;
1243 expr
->type
= EXPR_PREOP
;
1247 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1248 warning(expr
->pos
, "strange non-value function or array");
1251 *expr
= *expr
->unop
;
1252 ctype
= create_pointer(expr
, ctype
, 1);
1253 expr
->ctype
= ctype
;
1260 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1262 struct expression
*op
= expr
->unop
;
1263 struct symbol
*ctype
;
1265 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1266 warning(expr
->pos
, "not addressable");
1273 * symbol expression evaluation is lazy about the type
1274 * of the sub-expression, so we may have to generate
1275 * the type here if so..
1277 if (expr
->ctype
== &lazy_ptr_ctype
) {
1278 ctype
= create_pointer(expr
, ctype
, 0);
1279 expr
->ctype
= ctype
;
1285 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1287 struct expression
*op
= expr
->unop
;
1288 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1290 /* Simplify: *&(expr) => (expr) */
1291 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1296 /* Dereferencing a node drops all the node information. */
1297 if (ctype
->type
== SYM_NODE
)
1298 ctype
= ctype
->ctype
.base_type
;
1300 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1301 target
= ctype
->ctype
.base_type
;
1303 switch (ctype
->type
) {
1305 warning(expr
->pos
, "cannot derefence this type");
1308 merge_type(node
, ctype
);
1309 if (ctype
->type
!= SYM_ARRAY
)
1312 * Dereferencing a pointer to an array results in a
1313 * degenerate dereference: the expression becomes
1314 * just a pointer to the entry, and the derefence
1319 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1320 target
->bit_size
= bits_in_pointer
;
1321 target
->ctype
.alignment
= pointer_alignment
;
1322 merge_type(target
, ctype
->ctype
.base_type
);
1326 if (!lvalue_expression(op
)) {
1327 warning(op
->pos
, "non-lvalue array??");
1331 /* Do the implied "addressof" on the array */
1335 * When an array is dereferenced, we need to pick
1336 * up the attributes of the original node too..
1338 merge_type(node
, op
->ctype
);
1339 merge_type(node
, ctype
);
1343 node
->bit_size
= target
->bit_size
;
1344 node
->array_size
= target
->array_size
;
1351 * Unary post-ops: x++ and x--
1353 static struct symbol
*evaluate_postop(struct expression
*expr
)
1355 struct expression
*op
= expr
->unop
;
1356 struct symbol
*ctype
= op
->ctype
;
1358 if (!lvalue_expression(expr
->unop
)) {
1359 warning(expr
->pos
, "need lvalue expression for ++/--");
1362 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1363 warning(expr
->pos
, "bad operation on restricted");
1367 evaluate_assign_to(op
, ctype
);
1369 expr
->ctype
= ctype
;
1373 static struct symbol
*evaluate_sign(struct expression
*expr
)
1375 struct symbol
*ctype
= expr
->unop
->ctype
;
1376 if (is_int_type(ctype
)) {
1377 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1378 if (rtype
->bit_size
!= ctype
->bit_size
)
1379 expr
->unop
= cast_to(expr
->unop
, rtype
);
1381 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1382 /* no conversions needed */
1383 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1384 /* no conversions needed */
1386 return bad_expr_type(expr
);
1388 if (expr
->op
== '+')
1389 *expr
= *expr
->unop
;
1390 expr
->ctype
= ctype
;
1394 static struct symbol
*evaluate_preop(struct expression
*expr
)
1396 struct symbol
*ctype
= expr
->unop
->ctype
;
1400 *expr
= *expr
->unop
;
1406 return evaluate_sign(expr
);
1409 return evaluate_dereference(expr
);
1412 return evaluate_addressof(expr
);
1414 case SPECIAL_INCREMENT
:
1415 case SPECIAL_DECREMENT
:
1417 * From a type evaluation standpoint the pre-ops are
1418 * the same as the postops
1420 return evaluate_postop(expr
);
1423 if (is_safe_type(ctype
))
1424 warning(expr
->pos
, "testing a 'safe expression'");
1425 if (is_float_type(ctype
)) {
1426 struct expression
*arg
= expr
->unop
;
1427 expr
->type
= EXPR_BINOP
;
1428 expr
->op
= SPECIAL_EQUAL
;
1430 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1431 expr
->right
->ctype
= ctype
;
1432 expr
->right
->fvalue
= 0;
1434 ctype
= &bool_ctype
;
1440 expr
->ctype
= ctype
;
1444 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1446 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1447 struct ptr_list
*list
= head
;
1453 for (i
= 0; i
< list
->nr
; i
++) {
1454 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1456 if (sym
->ident
!= ident
)
1458 *offset
= sym
->offset
;
1461 struct symbol
*ctype
= sym
->ctype
.base_type
;
1465 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1467 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1470 *offset
+= sym
->offset
;
1474 } while ((list
= list
->next
) != head
);
1478 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1480 struct expression
*add
;
1483 * Create a new add-expression
1485 * NOTE! Even if we just add zero, we need a new node
1486 * for the member pointer, since it has a different
1487 * type than the original pointer. We could make that
1488 * be just a cast, but the fact is, a node is a node,
1489 * so we might as well just do the "add zero" here.
1491 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1494 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1495 add
->right
->ctype
= &int_ctype
;
1496 add
->right
->value
= offset
;
1499 * The ctype of the pointer will be lazily evaluated if
1500 * we ever take the address of this member dereference..
1502 add
->ctype
= &lazy_ptr_ctype
;
1506 /* structure/union dereference */
1507 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1510 struct symbol
*ctype
, *member
;
1511 struct expression
*deref
= expr
->deref
, *add
;
1512 struct ident
*ident
= expr
->member
;
1516 if (!evaluate_expression(deref
))
1519 warning(expr
->pos
, "bad member name");
1523 ctype
= deref
->ctype
;
1524 address_space
= ctype
->ctype
.as
;
1525 mod
= ctype
->ctype
.modifiers
;
1526 if (ctype
->type
== SYM_NODE
) {
1527 ctype
= ctype
->ctype
.base_type
;
1528 address_space
|= ctype
->ctype
.as
;
1529 mod
|= ctype
->ctype
.modifiers
;
1531 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1532 warning(expr
->pos
, "expected structure or union");
1536 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1538 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1539 const char *name
= "<unnamed>";
1542 name
= ctype
->ident
->name
;
1543 namelen
= ctype
->ident
->len
;
1545 warning(expr
->pos
, "no member '%s' in %s %.*s",
1546 show_ident(ident
), type
, namelen
, name
);
1551 * The member needs to take on the address space and modifiers of
1552 * the "parent" type.
1554 member
= convert_to_as_mod(member
, address_space
, mod
);
1555 ctype
= member
->ctype
.base_type
;
1557 if (!lvalue_expression(deref
)) {
1558 if (deref
->type
!= EXPR_SLICE
) {
1562 expr
->base
= deref
->base
;
1563 expr
->r_bitpos
= deref
->r_bitpos
;
1565 expr
->r_bitpos
+= offset
<< 3;
1566 expr
->type
= EXPR_SLICE
;
1567 expr
->r_nrbits
= member
->bit_size
;
1568 expr
->r_bitpos
+= member
->bit_offset
;
1569 expr
->ctype
= member
;
1573 deref
= deref
->unop
;
1574 expr
->deref
= deref
;
1576 add
= evaluate_offset(deref
, offset
);
1577 expr
->type
= EXPR_PREOP
;
1581 expr
->ctype
= member
;
1585 static int is_promoted(struct expression
*expr
)
1588 switch (expr
->type
) {
1591 case EXPR_CONDITIONAL
:
1615 static struct symbol
*evaluate_cast(struct expression
*);
1617 static struct symbol
*evaluate_type_information(struct expression
*expr
)
1619 struct symbol
*sym
= expr
->cast_type
;
1621 sym
= evaluate_expression(expr
->cast_expression
);
1625 * Expressions of restricted types will possibly get
1626 * promoted - check that here
1628 if (is_restricted_type(sym
)) {
1629 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
1633 examine_symbol_type(sym
);
1634 if (is_bitfield_type(sym
)) {
1635 warning(expr
->pos
, "trying to examine bitfield type");
1641 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1643 struct symbol
*type
;
1646 type
= evaluate_type_information(expr
);
1650 size
= type
->bit_size
;
1652 warning(expr
->pos
, "cannot size expression");
1653 expr
->type
= EXPR_VALUE
;
1654 expr
->value
= size
>> 3;
1655 expr
->ctype
= size_t_ctype
;
1656 return size_t_ctype
;
1659 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
1661 struct symbol
*type
;
1664 type
= evaluate_type_information(expr
);
1668 if (type
->type
== SYM_NODE
)
1669 type
= type
->ctype
.base_type
;
1672 switch (type
->type
) {
1676 type
= type
->ctype
.base_type
;
1680 warning(expr
->pos
, "expected pointer expression");
1683 size
= type
->bit_size
;
1686 expr
->type
= EXPR_VALUE
;
1687 expr
->value
= size
>> 3;
1688 expr
->ctype
= size_t_ctype
;
1689 return size_t_ctype
;
1692 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1694 struct symbol
*type
;
1696 type
= evaluate_type_information(expr
);
1700 expr
->type
= EXPR_VALUE
;
1701 expr
->value
= type
->ctype
.alignment
;
1702 expr
->ctype
= size_t_ctype
;
1703 return size_t_ctype
;
1706 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1708 struct expression
*expr
;
1709 struct symbol_list
*argument_types
= fn
->arguments
;
1710 struct symbol
*argtype
;
1713 PREPARE_PTR_LIST(argument_types
, argtype
);
1714 FOR_EACH_PTR (head
, expr
) {
1715 struct expression
**p
= THIS_ADDRESS(expr
);
1716 struct symbol
*ctype
, *target
;
1717 ctype
= evaluate_expression(expr
);
1722 ctype
= degenerate(expr
);
1725 if (!target
&& ctype
->bit_size
< bits_in_int
)
1726 target
= &int_ctype
;
1728 static char where
[30];
1729 examine_symbol_type(target
);
1730 sprintf(where
, "argument %d", i
);
1731 compatible_assignment_types(expr
, target
, p
, ctype
, where
, '=');
1735 NEXT_PTR_LIST(argtype
);
1736 } END_FOR_EACH_PTR(expr
);
1737 FINISH_PTR_LIST(argtype
);
1741 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1743 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1745 struct expression
*entry
= *ep
;
1746 struct expression
**parent
, *reuse
= NULL
;
1747 unsigned long offset
;
1749 unsigned long from
, to
;
1750 int accept_string
= is_byte_type(ctype
);
1755 if (entry
->type
== EXPR_INDEX
) {
1756 from
= entry
->idx_from
;
1757 to
= entry
->idx_to
+1;
1758 parent
= &entry
->idx_expression
;
1760 entry
= entry
->idx_expression
;
1763 offset
= from
* (ctype
->bit_size
>>3);
1765 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1766 reuse
->type
= EXPR_POS
;
1767 reuse
->ctype
= ctype
;
1768 reuse
->init_offset
= offset
;
1769 reuse
->init_nr
= to
- from
;
1770 reuse
->init_expr
= entry
;
1771 parent
= &reuse
->init_expr
;
1776 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1777 sym
= evaluate_expression(entry
);
1778 to
= from
+ get_expression_value(sym
->array_size
);
1780 evaluate_initializer(ctype
, parent
);
1785 static void evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1787 struct expression
*entry
;
1790 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1791 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1792 } END_FOR_EACH_PTR(entry
);
1795 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1796 static void evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1798 if (expression_list_size(expr
->expr_list
) != 1) {
1799 warning(expr
->pos
, "unexpected compound initializer");
1802 evaluate_array_initializer(ctype
, expr
);
1806 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1810 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1811 if (sym
->ident
== ident
)
1813 } END_FOR_EACH_PTR(sym
);
1817 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1819 struct expression
*entry
= *ep
;
1820 struct expression
**parent
;
1821 struct expression
*reuse
= NULL
;
1822 unsigned long offset
;
1825 error(entry
->pos
, "unknown named initializer");
1829 if (entry
->type
== EXPR_IDENTIFIER
) {
1831 entry
= entry
->ident_expression
;
1835 offset
= sym
->offset
;
1838 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1839 reuse
->type
= EXPR_POS
;
1841 reuse
->init_offset
= offset
;
1843 reuse
->init_expr
= entry
;
1844 parent
= &reuse
->init_expr
;
1848 evaluate_initializer(sym
, parent
);
1852 static void evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1854 struct expression
*entry
;
1857 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1858 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1859 if (entry
->type
== EXPR_IDENTIFIER
) {
1860 struct ident
*ident
= entry
->expr_ident
;
1861 /* We special-case the "already right place" case */
1862 if (!sym
|| sym
->ident
!= ident
) {
1863 RESET_PTR_LIST(sym
);
1867 if (sym
->ident
== ident
)
1873 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1876 } END_FOR_EACH_PTR(entry
);
1877 FINISH_PTR_LIST(sym
);
1881 * Initializers are kind of like assignments. Except
1882 * they can be a hell of a lot more complex.
1884 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1886 struct expression
*expr
= *ep
;
1889 * Simple non-structure/array initializers are the simple
1890 * case, and look (and parse) largely like assignments.
1892 switch (expr
->type
) {
1894 int is_string
= expr
->type
== EXPR_STRING
;
1895 struct symbol
*rtype
= evaluate_expression(expr
);
1899 * char array[] = "string"
1900 * should _not_ degenerate.
1902 if (!is_string
|| !is_string_type(ctype
))
1903 rtype
= degenerate(expr
);
1904 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer", '=');
1909 case EXPR_INITIALIZER
:
1910 expr
->ctype
= ctype
;
1911 if (ctype
->type
== SYM_NODE
)
1912 ctype
= ctype
->ctype
.base_type
;
1914 switch (ctype
->type
) {
1917 evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1920 evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1923 evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1926 evaluate_scalar_initializer(ctype
, expr
);
1930 case EXPR_IDENTIFIER
:
1931 if (ctype
->type
== SYM_NODE
)
1932 ctype
= ctype
->ctype
.base_type
;
1933 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1934 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1938 evaluate_one_struct_initializer(ctype
, ep
,
1939 find_struct_ident(ctype
, expr
->expr_ident
));
1943 if (ctype
->type
== SYM_NODE
)
1944 ctype
= ctype
->ctype
.base_type
;
1945 if (ctype
->type
!= SYM_ARRAY
) {
1946 error(expr
->pos
, "expected array");
1949 evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1954 * An EXPR_POS expression has already been evaluated, and we don't
1955 * need to do anything more
1961 static int get_as(struct symbol
*sym
)
1969 mod
= sym
->ctype
.modifiers
;
1970 if (sym
->type
== SYM_NODE
) {
1971 sym
= sym
->ctype
.base_type
;
1972 as
|= sym
->ctype
.as
;
1973 mod
|= sym
->ctype
.modifiers
;
1977 * At least for now, allow casting to a "unsigned long".
1978 * That's how we do things like pointer arithmetic and
1979 * store pointers to registers.
1981 if (sym
== &ulong_ctype
)
1984 if (sym
&& sym
->type
== SYM_PTR
) {
1985 sym
= sym
->ctype
.base_type
;
1986 as
|= sym
->ctype
.as
;
1987 mod
|= sym
->ctype
.modifiers
;
1989 if (mod
& MOD_FORCE
)
1994 static struct symbol
*evaluate_cast(struct expression
*expr
)
1996 struct expression
*target
= expr
->cast_expression
;
1997 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2003 expr
->ctype
= ctype
;
2004 expr
->cast_type
= ctype
;
2007 * Special case: a cast can be followed by an
2008 * initializer, in which case we need to pass
2009 * the type value down to that initializer rather
2010 * than trying to evaluate it as an expression
2012 * A more complex case is when the initializer is
2013 * dereferenced as part of a post-fix expression.
2014 * We need to produce an expression that can be dereferenced.
2016 if (target
->type
== EXPR_INITIALIZER
) {
2017 struct symbol
*sym
= expr
->cast_type
;
2018 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2020 sym
->initializer
= expr
->cast_expression
;
2021 evaluate_symbol(sym
);
2023 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2026 expr
->type
= EXPR_PREOP
;
2034 evaluate_expression(target
);
2038 * You can always throw a value away by casting to
2039 * "void" - that's an implicit "force". Note that
2040 * the same is _not_ true of "void *".
2042 if (ctype
== &void_ctype
)
2046 if (type
== SYM_NODE
) {
2047 type
= ctype
->ctype
.base_type
->type
;
2048 if (ctype
->ctype
.base_type
== &void_ctype
)
2051 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2052 warning(expr
->pos
, "cast to non-scalar");
2054 if (!target
->ctype
) {
2055 warning(expr
->pos
, "cast from unknown type");
2059 type
= target
->ctype
->type
;
2060 if (type
== SYM_NODE
)
2061 type
= target
->ctype
->ctype
.base_type
->type
;
2062 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2063 warning(expr
->pos
, "cast from non-scalar");
2065 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2066 warning(expr
->pos
, "cast removes address space of expression");
2068 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2069 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2070 if (t1
->type
== SYM_NODE
)
2071 t1
= t1
->ctype
.base_type
;
2072 if (t2
->type
== SYM_NODE
)
2073 t2
= t2
->ctype
.base_type
;
2075 if (t1
->type
== SYM_RESTRICT
)
2076 warning(expr
->pos
, "cast to restricted type");
2077 if (t2
->type
== SYM_RESTRICT
)
2078 warning(expr
->pos
, "cast from restricted type");
2083 * Casts of constant values are special: they
2084 * can be NULL, and thus need to be simplified
2087 if (target
->type
== EXPR_VALUE
)
2088 cast_value(expr
, ctype
, target
, target
->ctype
);
2095 * Evaluate a call expression with a symbol. This
2096 * should expand inline functions, and evaluate
2099 static int evaluate_symbol_call(struct expression
*expr
)
2101 struct expression
*fn
= expr
->fn
;
2102 struct symbol
*ctype
= fn
->ctype
;
2104 if (fn
->type
!= EXPR_PREOP
)
2107 if (ctype
->op
&& ctype
->op
->evaluate
)
2108 return ctype
->op
->evaluate(expr
);
2110 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2112 struct symbol
*curr
= current_fn
;
2113 current_fn
= ctype
->ctype
.base_type
;
2114 examine_fn_arguments(current_fn
);
2116 ret
= inline_function(expr
, ctype
);
2118 /* restore the old function */
2126 static struct symbol
*evaluate_call(struct expression
*expr
)
2129 struct symbol
*ctype
, *sym
;
2130 struct expression
*fn
= expr
->fn
;
2131 struct expression_list
*arglist
= expr
->args
;
2133 if (!evaluate_expression(fn
))
2135 sym
= ctype
= fn
->ctype
;
2136 if (ctype
->type
== SYM_NODE
)
2137 ctype
= ctype
->ctype
.base_type
;
2138 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2139 ctype
= ctype
->ctype
.base_type
;
2140 if (!evaluate_arguments(sym
, ctype
, arglist
))
2142 if (ctype
->type
!= SYM_FN
) {
2143 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2146 args
= expression_list_size(expr
->args
);
2147 fnargs
= symbol_list_size(ctype
->arguments
);
2149 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2150 if (args
> fnargs
&& !ctype
->variadic
)
2151 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2152 if (sym
->type
== SYM_NODE
) {
2153 if (evaluate_symbol_call(expr
))
2156 expr
->ctype
= ctype
->ctype
.base_type
;
2160 struct symbol
*evaluate_expression(struct expression
*expr
)
2167 switch (expr
->type
) {
2170 warning(expr
->pos
, "value expression without a type");
2173 return evaluate_string(expr
);
2175 return evaluate_symbol_expression(expr
);
2177 if (!evaluate_expression(expr
->left
))
2179 if (!evaluate_expression(expr
->right
))
2181 return evaluate_binop(expr
);
2183 return evaluate_logical(expr
);
2185 evaluate_expression(expr
->left
);
2186 if (!evaluate_expression(expr
->right
))
2188 return evaluate_comma(expr
);
2190 if (!evaluate_expression(expr
->left
))
2192 if (!evaluate_expression(expr
->right
))
2194 return evaluate_compare(expr
);
2195 case EXPR_ASSIGNMENT
:
2196 if (!evaluate_expression(expr
->left
))
2198 if (!evaluate_expression(expr
->right
))
2200 return evaluate_assignment(expr
);
2202 if (!evaluate_expression(expr
->unop
))
2204 return evaluate_preop(expr
);
2206 if (!evaluate_expression(expr
->unop
))
2208 return evaluate_postop(expr
);
2210 case EXPR_IMPLIED_CAST
:
2211 return evaluate_cast(expr
);
2213 return evaluate_sizeof(expr
);
2214 case EXPR_PTRSIZEOF
:
2215 return evaluate_ptrsizeof(expr
);
2217 return evaluate_alignof(expr
);
2219 return evaluate_member_dereference(expr
);
2221 return evaluate_call(expr
);
2223 case EXPR_CONDITIONAL
:
2224 return evaluate_conditional_expression(expr
);
2225 case EXPR_STATEMENT
:
2226 expr
->ctype
= evaluate_statement(expr
->statement
);
2230 expr
->ctype
= &ptr_ctype
;
2234 /* Evaluate the type of the symbol .. */
2235 evaluate_symbol(expr
->symbol
);
2236 /* .. but the type of the _expression_ is a "type" */
2237 expr
->ctype
= &type_ctype
;
2240 /* These can not exist as stand-alone expressions */
2241 case EXPR_INITIALIZER
:
2242 case EXPR_IDENTIFIER
:
2245 warning(expr
->pos
, "internal front-end error: initializer in expression");
2248 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2254 void check_duplicates(struct symbol
*sym
)
2256 struct symbol
*next
= sym
;
2258 while ((next
= next
->same_symbol
) != NULL
) {
2259 const char *typediff
;
2260 evaluate_symbol(next
);
2261 typediff
= type_difference(sym
, next
, 0, 0);
2263 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2264 show_ident(sym
->ident
),
2265 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2271 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2273 struct symbol
*base_type
;
2278 sym
= examine_symbol_type(sym
);
2279 base_type
= sym
->ctype
.base_type
;
2283 /* Evaluate the initializers */
2284 if (sym
->initializer
)
2285 evaluate_initializer(sym
, &sym
->initializer
);
2287 /* And finally, evaluate the body of the symbol too */
2288 if (base_type
->type
== SYM_FN
) {
2289 struct symbol
*curr
= current_fn
;
2291 current_fn
= base_type
;
2293 examine_fn_arguments(base_type
);
2294 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2296 if (base_type
->stmt
)
2297 evaluate_statement(base_type
->stmt
);
2305 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2307 struct expression
*expr
= stmt
->expression
;
2308 struct symbol
*ctype
, *fntype
;
2310 evaluate_expression(expr
);
2311 ctype
= degenerate(expr
);
2312 fntype
= current_fn
->ctype
.base_type
;
2313 if (!fntype
|| fntype
== &void_ctype
) {
2314 if (expr
&& ctype
!= &void_ctype
)
2315 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2320 warning(stmt
->pos
, "return with no return value");
2325 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression", '=');
2329 static void evaluate_if_statement(struct statement
*stmt
)
2331 if (!stmt
->if_conditional
)
2334 evaluate_conditional(stmt
->if_conditional
, 0);
2335 evaluate_statement(stmt
->if_true
);
2336 evaluate_statement(stmt
->if_false
);
2339 static void evaluate_iterator(struct statement
*stmt
)
2341 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
2342 evaluate_conditional(stmt
->iterator_post_condition
,1);
2343 evaluate_statement(stmt
->iterator_pre_statement
);
2344 evaluate_statement(stmt
->iterator_statement
);
2345 evaluate_statement(stmt
->iterator_post_statement
);
2348 struct symbol
*evaluate_statement(struct statement
*stmt
)
2353 switch (stmt
->type
) {
2355 return evaluate_return_expression(stmt
);
2357 case STMT_EXPRESSION
:
2358 if (!evaluate_expression(stmt
->expression
))
2360 return degenerate(stmt
->expression
);
2362 case STMT_COMPOUND
: {
2363 struct statement
*s
;
2364 struct symbol
*type
= NULL
;
2367 /* Evaluate each symbol in the compound statement */
2368 FOR_EACH_PTR(stmt
->syms
, sym
) {
2369 evaluate_symbol(sym
);
2370 } END_FOR_EACH_PTR(sym
);
2371 evaluate_symbol(stmt
->ret
);
2374 * Then, evaluate each statement, making the type of the
2375 * compound statement be the type of the last statement
2378 FOR_EACH_PTR(stmt
->stmts
, s
) {
2379 type
= evaluate_statement(s
);
2380 } END_FOR_EACH_PTR(s
);
2386 evaluate_if_statement(stmt
);
2389 evaluate_iterator(stmt
);
2392 evaluate_expression(stmt
->switch_expression
);
2393 evaluate_statement(stmt
->switch_statement
);
2396 evaluate_expression(stmt
->case_expression
);
2397 evaluate_expression(stmt
->case_to
);
2398 evaluate_statement(stmt
->case_statement
);
2401 return evaluate_statement(stmt
->label_statement
);
2403 evaluate_expression(stmt
->goto_expression
);
2408 /* FIXME! Do the asm parameter evaluation! */
2411 evaluate_expression(stmt
->expression
);