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 static struct symbol
*current_fn
;
29 static int current_context
, current_contextmask
;
31 static struct symbol
*degenerate(struct expression
*expr
);
33 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
35 struct symbol
*sym
= expr
->symbol
;
36 struct symbol
*base_type
;
40 expr
->ctype
= &int_ctype
;
43 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
47 examine_symbol_type(sym
);
48 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
49 warning(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
51 base_type
= sym
->ctype
.base_type
;
53 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
57 /* The type of a symbol is the symbol itself! */
60 /* enums can be turned into plain values */
61 if (sym
->type
!= SYM_ENUM
) {
62 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
64 addr
->symbol_name
= expr
->symbol_name
;
65 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr
->type
= EXPR_PREOP
;
70 } else if (base_type
->bit_size
< bits_in_int
) {
71 /* ugly - we need to force sizeof for these guys */
72 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
73 e
->value
= sym
->value
;
75 expr
->type
= EXPR_PREOP
;
79 expr
->type
= EXPR_VALUE
;
80 expr
->value
= sym
->value
;
82 expr
->ctype
= base_type
;
86 static struct symbol
*evaluate_string(struct expression
*expr
)
88 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
89 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
90 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
91 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
92 unsigned int length
= expr
->string
->length
;
94 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
95 sym
->bit_size
= bits_in_char
* length
;
96 sym
->ctype
.alignment
= 1;
97 sym
->ctype
.modifiers
= MOD_STATIC
;
98 sym
->ctype
.base_type
= array
;
99 sym
->initializer
= initstr
;
101 initstr
->ctype
= sym
;
102 initstr
->string
= expr
->string
;
104 array
->array_size
= sym
->array_size
;
105 array
->bit_size
= bits_in_char
* length
;
106 array
->ctype
.alignment
= 1;
107 array
->ctype
.modifiers
= MOD_STATIC
;
108 array
->ctype
.base_type
= &char_ctype
;
111 addr
->ctype
= &lazy_ptr_ctype
;
113 expr
->type
= EXPR_PREOP
;
120 static inline struct symbol
*integer_promotion(struct symbol
*type
)
122 unsigned long mod
= type
->ctype
.modifiers
;
125 if (type
->type
== SYM_ENUM
) {
126 type
= type
->ctype
.base_type
;
127 mod
= type
->ctype
.modifiers
;
129 if (type
->type
== SYM_BITFIELD
) {
130 mod
= type
->ctype
.base_type
->ctype
.modifiers
;
131 width
= type
->fieldwidth
;
132 } else if (mod
& (MOD_CHAR
| MOD_SHORT
))
133 width
= type
->bit_size
;
136 if (mod
& MOD_UNSIGNED
&& width
== bits_in_int
)
142 * integer part of usual arithmetic conversions:
143 * integer promotions are applied
144 * if left and right are identical, we are done
145 * if signedness is the same, convert one with lower rank
146 * unless unsigned argument has rank lower than signed one, convert the
148 * if signed argument is bigger than unsigned one, convert the unsigned.
149 * otherwise, convert signed.
151 * Leaving aside the integer promotions, that is equivalent to
152 * if identical, don't convert
153 * if left is bigger than right, convert right
154 * if right is bigger than left, convert right
155 * otherwise, if signedness is the same, convert one with lower rank
156 * otherwise convert the signed one.
158 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
160 unsigned long lmod
, rmod
;
162 left
= integer_promotion(left
);
163 right
= integer_promotion(right
);
168 if (left
->bit_size
> right
->bit_size
)
171 if (right
->bit_size
> left
->bit_size
)
174 lmod
= left
->ctype
.modifiers
;
175 rmod
= right
->ctype
.modifiers
;
176 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
177 if (lmod
& MOD_UNSIGNED
)
179 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
187 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
189 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
191 expr
->cast_type
= type
;
192 expr
->cast_expression
= old
;
196 static int is_type_type(struct symbol
*type
)
198 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
201 static int is_ptr_type(struct symbol
*type
)
203 if (type
->type
== SYM_NODE
)
204 type
= type
->ctype
.base_type
;
205 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
208 static inline int is_float_type(struct symbol
*type
)
210 if (type
->type
== SYM_NODE
)
211 type
= type
->ctype
.base_type
;
212 return type
->ctype
.base_type
== &fp_type
;
215 static inline int is_byte_type(struct symbol
*type
)
217 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
220 static inline int is_string_type(struct symbol
*type
)
222 if (type
->type
== SYM_NODE
)
223 type
= type
->ctype
.base_type
;
224 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
227 static struct symbol
*bad_expr_type(struct expression
*expr
)
229 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
230 switch (expr
->type
) {
233 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
234 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
238 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
247 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
249 struct expression
*left
= *lp
, *right
= *rp
;
250 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
252 if (ltype
->type
== SYM_NODE
)
253 ltype
= ltype
->ctype
.base_type
;
254 if (rtype
->type
== SYM_NODE
)
255 rtype
= rtype
->ctype
.base_type
;
256 if (is_float_type(ltype
)) {
257 if (is_int_type(rtype
))
259 if (is_float_type(rtype
)) {
260 unsigned long lmod
= ltype
->ctype
.modifiers
;
261 unsigned long rmod
= rtype
->ctype
.modifiers
;
262 lmod
&= MOD_LONG
| MOD_LONGLONG
;
263 rmod
&= MOD_LONG
| MOD_LONGLONG
;
273 if (!is_float_type(rtype
) || !is_int_type(ltype
))
276 *lp
= cast_to(left
, rtype
);
279 *rp
= cast_to(right
, ltype
);
283 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
285 struct expression
*left
= *lp
, *right
= *rp
;
286 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
288 if (ltype
->type
== SYM_NODE
)
289 ltype
= ltype
->ctype
.base_type
;
290 if (rtype
->type
== SYM_NODE
)
291 rtype
= rtype
->ctype
.base_type
;
292 if (is_int_type(ltype
) && is_int_type(rtype
)) {
293 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
295 /* Don't bother promoting same-size entities, it only adds clutter */
296 if (ltype
->bit_size
!= ctype
->bit_size
)
297 *lp
= cast_to(left
, ctype
);
298 if (rtype
->bit_size
!= ctype
->bit_size
)
299 *rp
= cast_to(right
, ctype
);
305 static int restricted_value(struct expression
*v
, struct symbol
*type
)
307 if (v
->type
!= EXPR_VALUE
)
314 static int restricted_binop(int op
, struct symbol
*type
)
322 case SPECIAL_NOTEQUAL
:
329 static int restricted_unop(int op
, struct symbol
*type
)
331 if (op
== '~' && type
->bit_size
>= bits_in_int
)
338 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
340 struct expression
*left
= *lp
, *right
= *rp
;
341 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
342 struct symbol
*type
= NULL
;
344 if (ltype
->type
== SYM_NODE
)
345 ltype
= ltype
->ctype
.base_type
;
346 if (ltype
->type
== SYM_ENUM
)
347 ltype
= ltype
->ctype
.base_type
;
348 if (rtype
->type
== SYM_NODE
)
349 rtype
= rtype
->ctype
.base_type
;
350 if (rtype
->type
== SYM_ENUM
)
351 rtype
= rtype
->ctype
.base_type
;
352 if (is_restricted_type(ltype
)) {
353 if (is_restricted_type(rtype
)) {
357 if (!restricted_value(right
, ltype
))
360 } else if (is_restricted_type(rtype
)) {
361 if (!restricted_value(left
, rtype
))
366 if (restricted_binop(op
, type
))
371 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
373 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
374 if (!ctype
&& float_ok
)
375 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
377 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
382 return bad_expr_type(expr
);
385 static inline int lvalue_expression(struct expression
*expr
)
387 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
390 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
392 struct symbol
*ctype
;
393 struct symbol
*ptr_type
= ptr
->ctype
;
396 if (ptr_type
->type
== SYM_NODE
)
397 ptr_type
= ptr_type
->ctype
.base_type
;
399 if (!is_int_type(i
->ctype
))
400 return bad_expr_type(expr
);
403 examine_symbol_type(ctype
);
405 ctype
= degenerate(ptr
);
406 if (!ctype
->ctype
.base_type
) {
407 warning(expr
->pos
, "missing type information");
411 /* Get the size of whatever the pointer points to */
413 if (ptr_type
->type
== SYM_NODE
)
414 ptr_type
= ptr_type
->ctype
.base_type
;
415 if (ptr_type
->type
== SYM_PTR
)
416 ptr_type
= ptr_type
->ctype
.base_type
;
417 bit_size
= ptr_type
->bit_size
;
419 /* Special case: adding zero commonly happens as a result of 'array[0]' */
420 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
422 } else if (bit_size
> bits_in_char
) {
423 struct expression
*add
= expr
;
424 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
425 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
427 val
->ctype
= size_t_ctype
;
428 val
->value
= bit_size
>> 3;
431 mul
->ctype
= size_t_ctype
;
435 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
444 static struct symbol
*evaluate_add(struct expression
*expr
)
446 struct expression
*left
= expr
->left
, *right
= expr
->right
;
447 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
449 if (is_ptr_type(ltype
))
450 return evaluate_ptr_add(expr
, left
, right
);
452 if (is_ptr_type(rtype
))
453 return evaluate_ptr_add(expr
, right
, left
);
455 return evaluate_arith(expr
, 1);
458 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
459 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
460 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
461 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
463 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
464 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
467 unsigned long mod1
, mod2
, diff
;
468 unsigned long as1
, as2
;
470 struct symbol
*base1
, *base2
;
472 if (target
== source
)
474 if (!target
|| !source
)
475 return "different types";
477 * Peel of per-node information.
478 * FIXME! Check alignment and context too here!
480 mod1
= target
->ctype
.modifiers
;
481 as1
= target
->ctype
.as
;
482 mod2
= source
->ctype
.modifiers
;
483 as2
= source
->ctype
.as
;
484 if (target
->type
== SYM_NODE
) {
485 target
= target
->ctype
.base_type
;
488 if (target
->type
== SYM_PTR
) {
492 mod1
|= target
->ctype
.modifiers
;
493 as1
|= target
->ctype
.as
;
495 if (source
->type
== SYM_NODE
) {
496 source
= source
->ctype
.base_type
;
499 if (source
->type
== SYM_PTR
) {
503 mod2
|= source
->ctype
.modifiers
;
504 as2
|= source
->ctype
.as
;
506 if (target
->type
== SYM_ENUM
) {
507 target
= target
->ctype
.base_type
;
511 if (source
->type
== SYM_ENUM
) {
512 source
= source
->ctype
.base_type
;
517 if (target
== source
)
519 if (!target
|| !source
)
520 return "different types";
522 type1
= target
->type
;
523 base1
= target
->ctype
.base_type
;
525 type2
= source
->type
;
526 base2
= source
->ctype
.base_type
;
529 * Pointers to functions compare as the function itself
531 if (type1
== SYM_PTR
&& base1
) {
532 switch (base1
->type
) {
536 base1
= base1
->ctype
.base_type
;
541 if (type2
== SYM_PTR
&& base2
) {
542 switch (base2
->type
) {
546 base2
= base2
->ctype
.base_type
;
552 /* Arrays degenerate to pointers for type comparisons */
553 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
554 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
556 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
557 return "different base types";
559 /* Must be same address space to be comparable */
561 return "different address spaces";
563 /* Ignore differences in storage types or addressability */
564 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
565 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
568 return "different type sizes";
569 if (diff
& ~MOD_SIGNEDNESS
)
570 return "different modifiers";
572 /* Differs in signedness only.. */
575 * Warn if both are explicitly signed ("unsigned" is obvously
576 * always explicit, and since we know one of them has to be
577 * unsigned, we check if the signed one was explicit).
579 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
580 return "different explicit signedness";
583 * "char" matches both "unsigned char" and "signed char",
584 * so if the explicit test didn't trigger, then we should
585 * not warn about a char.
587 if (!(mod1
& MOD_CHAR
))
588 return "different signedness";
592 if (type1
== SYM_FN
) {
594 struct symbol
*arg1
, *arg2
;
595 if (base1
->variadic
!= base2
->variadic
)
596 return "incompatible variadic arguments";
597 PREPARE_PTR_LIST(target
->arguments
, arg1
);
598 PREPARE_PTR_LIST(source
->arguments
, arg2
);
602 diff
= type_difference(arg1
, arg2
, 0, 0);
604 static char argdiff
[80];
605 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
614 FINISH_PTR_LIST(arg2
);
615 FINISH_PTR_LIST(arg1
);
624 static int is_null_ptr(struct expression
*expr
)
626 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
628 if (!is_ptr_type(expr
->ctype
))
629 warning(expr
->pos
, "Using plain integer as NULL pointer");
633 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
635 /* NULL expression? Just return the type of the "other side" */
644 * Ignore differences in "volatile" and "const"ness when
645 * subtracting pointers
647 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
649 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
651 const char *typediff
;
652 struct symbol
*ctype
;
653 struct symbol
*ltype
, *rtype
;
655 ltype
= degenerate(l
);
656 rtype
= degenerate(r
);
659 * If it is an integer subtract: the ptr add case will do the
662 if (!is_ptr_type(rtype
))
663 return evaluate_ptr_add(expr
, l
, r
);
666 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
668 ctype
= common_ptr_type(l
, r
);
670 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
674 examine_symbol_type(ctype
);
676 /* Figure out the base type we point to */
677 if (ctype
->type
== SYM_NODE
)
678 ctype
= ctype
->ctype
.base_type
;
679 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
680 warning(expr
->pos
, "subtraction of functions? Share your drugs");
683 ctype
= ctype
->ctype
.base_type
;
685 expr
->ctype
= ssize_t_ctype
;
686 if (ctype
->bit_size
> bits_in_char
) {
687 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
688 struct expression
*div
= expr
;
689 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
691 val
->ctype
= size_t_ctype
;
692 val
->value
= ctype
->bit_size
>> 3;
695 sub
->ctype
= ssize_t_ctype
;
704 return ssize_t_ctype
;
707 static struct symbol
*evaluate_sub(struct expression
*expr
)
709 struct expression
*left
= expr
->left
, *right
= expr
->right
;
710 struct symbol
*ltype
= left
->ctype
;
712 if (is_ptr_type(ltype
))
713 return evaluate_ptr_sub(expr
, left
, right
);
715 return evaluate_arith(expr
, 1);
718 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
720 static struct symbol
*evaluate_conditional(struct expression
**p
)
722 struct symbol
*ctype
;
723 struct expression
*expr
= *p
;
728 if (expr
->type
== EXPR_ASSIGNMENT
)
729 warning(expr
->pos
, "assignment expression in conditional");
731 ctype
= evaluate_expression(expr
);
733 if (is_safe_type(ctype
))
734 warning(expr
->pos
, "testing a 'safe expression'");
735 if (is_float_type(ctype
)) {
736 struct expression
*comp
;
738 * It's easier to handle here, rather than deal with
739 * FP all over the place. Floating point in boolean
740 * context is rare enough (and very often wrong),
741 * so price of explicit comparison with appropriate
742 * FP zero is not too high. And it simplifies things
745 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
746 comp
->op
= SPECIAL_NOTEQUAL
;
748 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
749 comp
->right
->ctype
= comp
->left
->ctype
;
750 comp
->right
->fvalue
= 0;
751 ctype
= comp
->ctype
= &bool_ctype
;
759 static struct symbol
*evaluate_logical(struct expression
*expr
)
761 if (!evaluate_conditional(&expr
->left
))
763 if (!evaluate_conditional(&expr
->right
))
766 expr
->ctype
= &bool_ctype
;
770 static struct symbol
*evaluate_shift(struct expression
*expr
)
772 struct expression
*left
= expr
->left
, *right
= expr
->right
;
773 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
775 if (ltype
->type
== SYM_NODE
)
776 ltype
= ltype
->ctype
.base_type
;
777 if (rtype
->type
== SYM_NODE
)
778 rtype
= rtype
->ctype
.base_type
;
779 if (is_int_type(ltype
) && is_int_type(rtype
)) {
780 struct symbol
*ctype
= integer_promotion(ltype
);
781 if (ltype
->bit_size
!= ctype
->bit_size
)
782 expr
->left
= cast_to(expr
->left
, ctype
);
784 ctype
= integer_promotion(rtype
);
785 if (rtype
->bit_size
!= ctype
->bit_size
)
786 expr
->right
= cast_to(expr
->right
, ctype
);
789 return bad_expr_type(expr
);
792 static struct symbol
*evaluate_binop(struct expression
*expr
)
795 // addition can take ptr+int, fp and int
797 return evaluate_add(expr
);
799 // subtraction can take ptr-ptr, fp and int
801 return evaluate_sub(expr
);
803 // Arithmetic operations can take fp and int
805 return evaluate_arith(expr
, 1);
807 // shifts do integer promotions, but that's it.
808 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
809 return evaluate_shift(expr
);
811 // The rest are integer operations
812 // '%', '&', '^', '|'
814 return evaluate_arith(expr
, 0);
818 static struct symbol
*evaluate_comma(struct expression
*expr
)
820 expr
->ctype
= expr
->right
->ctype
;
824 static int modify_for_unsigned(int op
)
827 op
= SPECIAL_UNSIGNED_LT
;
829 op
= SPECIAL_UNSIGNED_GT
;
830 else if (op
== SPECIAL_LTE
)
831 op
= SPECIAL_UNSIGNED_LTE
;
832 else if (op
== SPECIAL_GTE
)
833 op
= SPECIAL_UNSIGNED_GTE
;
837 static struct symbol
*evaluate_compare(struct expression
*expr
)
839 struct expression
*left
= expr
->left
, *right
= expr
->right
;
840 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
841 struct symbol
*ctype
;
844 if (is_type_type(ltype
) && is_type_type(rtype
))
847 if (is_safe_type(ltype
) || is_safe_type(rtype
))
848 warning(expr
->pos
, "testing a 'safe expression'");
851 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
852 // FIXME! Check the types for compatibility
856 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
858 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
859 expr
->op
= modify_for_unsigned(expr
->op
);
863 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
867 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
874 expr
->ctype
= &bool_ctype
;
879 * FIXME!! This should do casts, array degeneration etc..
881 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
883 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
885 if (ltype
->type
== SYM_NODE
)
886 ltype
= ltype
->ctype
.base_type
;
888 if (rtype
->type
== SYM_NODE
)
889 rtype
= rtype
->ctype
.base_type
;
891 if (ltype
->type
== SYM_PTR
) {
892 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
896 if (rtype
->type
== SYM_PTR
) {
897 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
903 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
905 struct expression
*cond
, *false;
906 struct expression
**true_p
;
907 struct symbol
*ctype
, *ltype
, *rtype
;
908 const char * typediff
;
910 ctype
= degenerate(expr
->conditional
);
911 cond
= expr
->conditional
;
914 true_p
= &expr
->conditional
;
915 if (expr
->cond_true
) {
916 ltype
= degenerate(expr
->cond_true
);
917 true_p
= &expr
->cond_true
;
920 rtype
= degenerate(expr
->cond_false
);
921 false = expr
->cond_false
;
924 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
928 ctype
= compatible_integer_binop(true_p
, &expr
->cond_false
);
931 ctype
= compatible_ptr_type(*true_p
, expr
->cond_false
);
934 ctype
= compatible_float_binop(true_p
, &expr
->cond_false
);
937 ctype
= compatible_restricted_binop('?', true_p
, &expr
->cond_false
);
940 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
948 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
949 struct expression
**rp
, struct symbol
*source
, const char *where
)
951 const char *typediff
;
955 /* It's ok if the target is more volatile or const than the source */
956 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
960 if (is_int_type(target
)) {
961 if (is_int_type(source
)) {
962 if (target
->bit_size
!= source
->bit_size
)
966 if (is_float_type(source
))
968 } else if (is_float_type(target
)) {
969 if (is_int_type(source
))
971 if (is_float_type(source
)) {
972 if (target
->bit_size
!= source
->bit_size
)
978 if (is_restricted_type(target
) && !restricted_value(*rp
, target
))
981 /* Pointer destination? */
983 target_as
= t
->ctype
.as
;
984 if (t
->type
== SYM_NODE
) {
985 t
= t
->ctype
.base_type
;
986 target_as
|= t
->ctype
.as
;
988 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
989 struct expression
*right
= *rp
;
990 struct symbol
*s
= source
;
993 // NULL pointer is always ok
994 if (is_null_ptr(right
))
997 /* "void *" matches anything as long as the address space is ok */
998 source_as
= s
->ctype
.as
;
999 if (s
->type
== SYM_NODE
) {
1000 s
= s
->ctype
.base_type
;
1001 source_as
|= s
->ctype
.as
;
1003 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1004 s
= s
->ctype
.base_type
;
1005 t
= t
->ctype
.base_type
;
1006 if (s
== &void_ctype
|| t
== &void_ctype
)
1011 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1012 info(expr
->pos
, " expected %s", show_typename(target
));
1013 info(expr
->pos
, " got %s", show_typename(source
));
1014 *rp
= cast_to(*rp
, target
);
1017 *rp
= cast_to(*rp
, target
);
1022 * FIXME!! This is wrong from a double evaluation standpoint. We can't
1023 * just expand the expression twice, that would make any side effects
1026 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
1029 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1030 static const int op_trans
[] = {
1031 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
1032 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
1033 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
1034 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
1035 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
1036 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
1037 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
1038 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
1039 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
1040 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
1042 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
, *e5
;
1043 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1044 struct symbol
*ltype
= left
->ctype
;
1045 struct expression
*addr
;
1046 struct symbol
*lptype
;
1048 if (left
->type
== EXPR_BITFIELD
)
1049 addr
= left
->address
;
1053 lptype
= addr
->ctype
;
1055 a
->ctype
.base_type
= lptype
;
1056 a
->bit_size
= lptype
->bit_size
;
1057 a
->array_size
= lptype
->array_size
;
1059 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1061 e0
->ctype
= &lazy_ptr_ctype
;
1063 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1068 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1074 /* we can't cannibalize left, unfortunately */
1075 e3
= alloc_expression(expr
->pos
, left
->type
);
1077 if (e3
->type
== EXPR_BITFIELD
)
1082 e4
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1083 e4
->op
= subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
1086 /* will calculate type later */
1088 e5
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1089 e5
->left
= e3
; /* we can share that one */
1094 expr
->type
= EXPR_COMMA
;
1097 expr
->ctype
= ltype
;
1099 return evaluate_binop(e4
);
1102 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1104 if (type
->ctype
.modifiers
& MOD_CONST
)
1105 warning(left
->pos
, "assignment to const expression");
1106 if (type
->type
== SYM_NODE
)
1107 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1110 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1112 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1113 struct expression
*where
= expr
;
1114 struct symbol
*ltype
, *rtype
;
1116 if (!lvalue_expression(left
)) {
1117 warning(expr
->pos
, "not an lvalue");
1121 ltype
= left
->ctype
;
1123 if (expr
->op
!= '=') {
1124 if (!evaluate_binop_assignment(expr
, left
, right
))
1126 where
= expr
->right
; /* expr is EXPR_COMMA now */
1128 right
= where
->right
;
1131 rtype
= degenerate(right
);
1133 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1136 evaluate_assign_to(left
, ltype
);
1138 expr
->ctype
= ltype
;
1142 static void examine_fn_arguments(struct symbol
*fn
)
1146 FOR_EACH_PTR(fn
->arguments
, s
) {
1147 struct symbol
*arg
= evaluate_symbol(s
);
1148 /* Array/function arguments silently degenerate into pointers */
1154 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1155 if (arg
->type
== SYM_ARRAY
)
1156 ptr
->ctype
= arg
->ctype
;
1158 ptr
->ctype
.base_type
= arg
;
1159 ptr
->ctype
.as
|= s
->ctype
.as
;
1160 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1162 s
->ctype
.base_type
= ptr
;
1164 s
->ctype
.modifiers
= 0;
1165 examine_symbol_type(s
);
1172 } END_FOR_EACH_PTR(s
);
1175 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1177 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1178 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1180 newsym
->ctype
.as
= as
;
1181 newsym
->ctype
.modifiers
= mod
;
1187 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1189 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1190 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1192 node
->ctype
.base_type
= ptr
;
1193 ptr
->bit_size
= bits_in_pointer
;
1194 ptr
->ctype
.alignment
= pointer_alignment
;
1196 node
->bit_size
= bits_in_pointer
;
1197 node
->ctype
.alignment
= pointer_alignment
;
1200 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1201 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1202 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1203 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1205 if (sym
->type
== SYM_NODE
) {
1206 ptr
->ctype
.as
|= sym
->ctype
.as
;
1207 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1208 sym
= sym
->ctype
.base_type
;
1210 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1211 ptr
->ctype
.as
|= sym
->ctype
.as
;
1212 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1213 sym
= sym
->ctype
.base_type
;
1215 ptr
->ctype
.base_type
= sym
;
1220 /* Arrays degenerate into pointers on pointer arithmetic */
1221 static struct symbol
*degenerate(struct expression
*expr
)
1223 struct symbol
*ctype
, *base
;
1227 ctype
= expr
->ctype
;
1231 if (ctype
->type
== SYM_NODE
)
1232 base
= ctype
->ctype
.base_type
;
1234 * Arrays degenerate into pointers to the entries, while
1235 * functions degenerate into pointers to themselves.
1236 * If array was part of non-lvalue compound, we create a copy
1237 * of that compound first and then act as if we were dealing with
1238 * the corresponding field in there.
1240 switch (base
->type
) {
1242 if (expr
->type
== EXPR_SLICE
) {
1243 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1244 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1246 a
->ctype
.base_type
= expr
->base
->ctype
;
1247 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1248 a
->array_size
= expr
->base
->ctype
->array_size
;
1250 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1252 e0
->ctype
= &lazy_ptr_ctype
;
1254 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1257 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1259 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1261 e2
->right
= expr
->base
;
1263 e2
->ctype
= expr
->base
->ctype
;
1265 if (expr
->r_bitpos
) {
1266 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1269 e3
->right
= alloc_const_expression(expr
->pos
,
1270 expr
->r_bitpos
>> 3);
1271 e3
->ctype
= &lazy_ptr_ctype
;
1276 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1279 e4
->ctype
= &lazy_ptr_ctype
;
1282 expr
->type
= EXPR_PREOP
;
1286 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1287 warning(expr
->pos
, "strange non-value function or array");
1290 *expr
= *expr
->unop
;
1291 ctype
= create_pointer(expr
, ctype
, 1);
1292 expr
->ctype
= ctype
;
1299 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1301 struct expression
*op
= expr
->unop
;
1302 struct symbol
*ctype
;
1304 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1305 warning(expr
->pos
, "not addressable");
1312 * symbol expression evaluation is lazy about the type
1313 * of the sub-expression, so we may have to generate
1314 * the type here if so..
1316 if (expr
->ctype
== &lazy_ptr_ctype
) {
1317 ctype
= create_pointer(expr
, ctype
, 0);
1318 expr
->ctype
= ctype
;
1324 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1326 struct expression
*op
= expr
->unop
;
1327 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1329 /* Simplify: *&(expr) => (expr) */
1330 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1335 /* Dereferencing a node drops all the node information. */
1336 if (ctype
->type
== SYM_NODE
)
1337 ctype
= ctype
->ctype
.base_type
;
1339 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1340 target
= ctype
->ctype
.base_type
;
1342 switch (ctype
->type
) {
1344 warning(expr
->pos
, "cannot derefence this type");
1347 merge_type(node
, ctype
);
1348 if (ctype
->type
!= SYM_ARRAY
)
1351 * Dereferencing a pointer to an array results in a
1352 * degenerate dereference: the expression becomes
1353 * just a pointer to the entry, and the derefence
1358 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1359 target
->bit_size
= bits_in_pointer
;
1360 target
->ctype
.alignment
= pointer_alignment
;
1361 merge_type(target
, ctype
->ctype
.base_type
);
1365 if (!lvalue_expression(op
)) {
1366 warning(op
->pos
, "non-lvalue array??");
1370 /* Do the implied "addressof" on the array */
1374 * When an array is dereferenced, we need to pick
1375 * up the attributes of the original node too..
1377 merge_type(node
, op
->ctype
);
1378 merge_type(node
, ctype
);
1382 node
->bit_size
= target
->bit_size
;
1383 node
->array_size
= target
->array_size
;
1390 * Unary post-ops: x++ and x--
1392 static struct symbol
*evaluate_postop(struct expression
*expr
)
1394 struct expression
*op
= expr
->unop
;
1395 struct symbol
*ctype
= op
->ctype
;
1397 if (!lvalue_expression(expr
->unop
)) {
1398 warning(expr
->pos
, "need lvalue expression for ++/--");
1401 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1402 warning(expr
->pos
, "bad operation on restricted");
1406 evaluate_assign_to(op
, ctype
);
1408 expr
->ctype
= ctype
;
1412 static struct symbol
*evaluate_sign(struct expression
*expr
)
1414 struct symbol
*ctype
= expr
->unop
->ctype
;
1415 if (is_int_type(ctype
)) {
1416 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1417 if (rtype
->bit_size
!= ctype
->bit_size
)
1418 expr
->unop
= cast_to(expr
->unop
, rtype
);
1420 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1421 /* no conversions needed */
1422 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1423 /* no conversions needed */
1425 return bad_expr_type(expr
);
1427 if (expr
->op
== '+')
1428 *expr
= *expr
->unop
;
1429 expr
->ctype
= ctype
;
1433 static struct symbol
*evaluate_preop(struct expression
*expr
)
1435 struct symbol
*ctype
= expr
->unop
->ctype
;
1439 *expr
= *expr
->unop
;
1445 return evaluate_sign(expr
);
1448 return evaluate_dereference(expr
);
1451 return evaluate_addressof(expr
);
1453 case SPECIAL_INCREMENT
:
1454 case SPECIAL_DECREMENT
:
1456 * From a type evaluation standpoint the pre-ops are
1457 * the same as the postops
1459 return evaluate_postop(expr
);
1462 if (is_safe_type(ctype
))
1463 warning(expr
->pos
, "testing a 'safe expression'");
1464 if (is_float_type(ctype
)) {
1465 struct expression
*arg
= expr
->unop
;
1466 expr
->type
= EXPR_BINOP
;
1467 expr
->op
= SPECIAL_EQUAL
;
1469 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1470 expr
->right
->ctype
= ctype
;
1471 expr
->right
->fvalue
= 0;
1473 ctype
= &bool_ctype
;
1479 expr
->ctype
= ctype
;
1483 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1485 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1486 struct ptr_list
*list
= head
;
1492 for (i
= 0; i
< list
->nr
; i
++) {
1493 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1495 if (sym
->ident
!= ident
)
1497 *offset
= sym
->offset
;
1500 struct symbol
*ctype
= sym
->ctype
.base_type
;
1504 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1506 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1509 *offset
+= sym
->offset
;
1513 } while ((list
= list
->next
) != head
);
1517 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1519 struct expression
*add
;
1522 * Create a new add-expression
1524 * NOTE! Even if we just add zero, we need a new node
1525 * for the member pointer, since it has a different
1526 * type than the original pointer. We could make that
1527 * be just a cast, but the fact is, a node is a node,
1528 * so we might as well just do the "add zero" here.
1530 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1533 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1534 add
->right
->ctype
= &int_ctype
;
1535 add
->right
->value
= offset
;
1538 * The ctype of the pointer will be lazily evaluated if
1539 * we ever take the address of this member dereference..
1541 add
->ctype
= &lazy_ptr_ctype
;
1545 /* structure/union dereference */
1546 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1549 struct symbol
*ctype
, *member
;
1550 struct expression
*deref
= expr
->deref
, *add
;
1551 struct ident
*ident
= expr
->member
;
1555 if (!evaluate_expression(deref
))
1558 warning(expr
->pos
, "bad member name");
1562 ctype
= deref
->ctype
;
1563 address_space
= ctype
->ctype
.as
;
1564 mod
= ctype
->ctype
.modifiers
;
1565 if (ctype
->type
== SYM_NODE
) {
1566 ctype
= ctype
->ctype
.base_type
;
1567 address_space
|= ctype
->ctype
.as
;
1568 mod
|= ctype
->ctype
.modifiers
;
1570 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1571 warning(expr
->pos
, "expected structure or union");
1575 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1577 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1578 const char *name
= "<unnamed>";
1581 name
= ctype
->ident
->name
;
1582 namelen
= ctype
->ident
->len
;
1584 warning(expr
->pos
, "no member '%s' in %s %.*s",
1585 show_ident(ident
), type
, namelen
, name
);
1590 * The member needs to take on the address space and modifiers of
1591 * the "parent" type.
1593 member
= convert_to_as_mod(member
, address_space
, mod
);
1594 ctype
= member
->ctype
.base_type
;
1596 if (!lvalue_expression(deref
)) {
1597 if (deref
->type
!= EXPR_SLICE
) {
1601 expr
->base
= deref
->base
;
1602 expr
->r_bitpos
= deref
->r_bitpos
;
1604 expr
->r_bitpos
+= offset
<< 3;
1605 expr
->type
= EXPR_SLICE
;
1606 if (ctype
->type
== SYM_BITFIELD
) {
1607 expr
->r_bitpos
+= member
->bit_offset
;
1608 expr
->r_nrbits
= member
->fieldwidth
;
1610 expr
->r_nrbits
= member
->bit_size
;
1612 expr
->ctype
= member
;
1616 deref
= deref
->unop
;
1617 expr
->deref
= deref
;
1619 add
= evaluate_offset(deref
, offset
);
1620 if (ctype
->type
== SYM_BITFIELD
) {
1621 expr
->type
= EXPR_BITFIELD
;
1622 expr
->bitpos
= member
->bit_offset
;
1623 expr
->nrbits
= member
->fieldwidth
;
1624 expr
->address
= add
;
1626 expr
->type
= EXPR_PREOP
;
1631 expr
->ctype
= member
;
1635 static int is_promoted(struct expression
*expr
)
1638 switch (expr
->type
) {
1641 case EXPR_CONDITIONAL
:
1665 static struct symbol
*evaluate_cast(struct expression
*);
1667 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1669 struct expression
*what
= expr
->cast_expression
;
1672 if (expr
->cast_type
) {
1674 struct symbol
*sym
= evaluate_cast(expr
);
1675 size
= sym
->bit_size
;
1677 examine_symbol_type(expr
->cast_type
);
1678 size
= expr
->cast_type
->bit_size
;
1681 if (!evaluate_expression(what
))
1683 size
= what
->ctype
->bit_size
;
1684 if (is_restricted_type(what
->ctype
)) {
1685 if (size
< bits_in_int
&& is_promoted(what
))
1688 if (is_bitfield_type(what
->ctype
))
1689 warning(expr
->pos
, "sizeof applied to bitfield type");
1692 warning(expr
->pos
, "cannot size expression");
1693 expr
->type
= EXPR_VALUE
;
1694 expr
->value
= size
>> 3;
1695 expr
->ctype
= size_t_ctype
;
1696 return size_t_ctype
;
1699 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1701 struct symbol
*type
= expr
->cast_type
;
1704 type
= evaluate_expression(expr
->cast_expression
);
1708 if (is_bitfield_type(type
))
1709 warning(expr
->pos
, "alignof applied to bitfield type");
1710 examine_symbol_type(type
);
1711 expr
->type
= EXPR_VALUE
;
1712 expr
->value
= type
->ctype
.alignment
;
1713 expr
->ctype
= size_t_ctype
;
1714 return size_t_ctype
;
1717 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1719 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1720 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1724 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1726 struct expression
*expr
;
1727 struct symbol_list
*argument_types
= fn
->arguments
;
1728 struct symbol
*argtype
;
1731 PREPARE_PTR_LIST(argument_types
, argtype
);
1732 FOR_EACH_PTR (head
, expr
) {
1733 struct expression
**p
= THIS_ADDRESS(expr
);
1734 struct symbol
*ctype
, *target
;
1735 ctype
= evaluate_expression(expr
);
1740 if (context_clash(f
, ctype
))
1741 warning(expr
->pos
, "argument %d used in wrong context", i
);
1743 ctype
= degenerate(expr
);
1746 if (!target
&& ctype
->bit_size
< bits_in_int
)
1747 target
= &int_ctype
;
1749 static char where
[30];
1750 examine_symbol_type(target
);
1751 sprintf(where
, "argument %d", i
);
1752 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1756 NEXT_PTR_LIST(argtype
);
1757 } END_FOR_EACH_PTR(expr
);
1758 FINISH_PTR_LIST(argtype
);
1762 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1764 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1766 struct expression
*entry
= *ep
;
1767 struct expression
**parent
, *reuse
= NULL
;
1768 unsigned long offset
;
1770 unsigned long from
, to
;
1771 int accept_string
= is_byte_type(ctype
);
1776 if (entry
->type
== EXPR_INDEX
) {
1777 from
= entry
->idx_from
;
1778 to
= entry
->idx_to
+1;
1779 parent
= &entry
->idx_expression
;
1781 entry
= entry
->idx_expression
;
1784 offset
= from
* (ctype
->bit_size
>>3);
1786 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1787 reuse
->type
= EXPR_POS
;
1788 reuse
->ctype
= ctype
;
1789 reuse
->init_offset
= offset
;
1790 reuse
->init_nr
= to
- from
;
1791 reuse
->init_expr
= entry
;
1792 parent
= &reuse
->init_expr
;
1797 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1798 sym
= evaluate_expression(entry
);
1799 to
= from
+ get_expression_value(sym
->array_size
);
1801 evaluate_initializer(ctype
, parent
);
1806 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1808 struct expression
*entry
;
1812 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1813 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1816 } END_FOR_EACH_PTR(entry
);
1820 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1821 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1823 if (expression_list_size(expr
->expr_list
) != 1) {
1824 warning(expr
->pos
, "unexpected compound initializer");
1827 return evaluate_array_initializer(ctype
, expr
);
1830 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1834 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1835 if (sym
->ident
== ident
)
1837 } END_FOR_EACH_PTR(sym
);
1841 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1843 struct expression
*entry
= *ep
;
1844 struct expression
**parent
;
1845 struct expression
*reuse
= NULL
;
1846 unsigned long offset
;
1849 error(entry
->pos
, "unknown named initializer");
1853 if (entry
->type
== EXPR_IDENTIFIER
) {
1855 entry
= entry
->ident_expression
;
1859 offset
= sym
->offset
;
1862 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1863 reuse
->type
= EXPR_POS
;
1864 reuse
->ctype
= ctype
;
1865 reuse
->init_offset
= offset
;
1867 reuse
->init_expr
= entry
;
1868 parent
= &reuse
->init_expr
;
1872 evaluate_initializer(sym
, parent
);
1876 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1878 struct expression
*entry
;
1881 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1882 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1883 if (entry
->type
== EXPR_IDENTIFIER
) {
1884 struct ident
*ident
= entry
->expr_ident
;
1885 /* We special-case the "already right place" case */
1886 if (!sym
|| sym
->ident
!= ident
) {
1887 RESET_PTR_LIST(sym
);
1891 if (sym
->ident
== ident
)
1897 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1900 } END_FOR_EACH_PTR(entry
);
1901 FINISH_PTR_LIST(sym
);
1907 * Initializers are kind of like assignments. Except
1908 * they can be a hell of a lot more complex.
1910 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1912 struct expression
*expr
= *ep
;
1915 * Simple non-structure/array initializers are the simple
1916 * case, and look (and parse) largely like assignments.
1918 switch (expr
->type
) {
1920 int size
= 0, is_string
= expr
->type
== EXPR_STRING
;
1921 struct symbol
*rtype
= evaluate_expression(expr
);
1925 * char array[] = "string"
1926 * should _not_ degenerate.
1928 if (is_string
&& is_string_type(ctype
)) {
1929 struct expression
*array_size
= ctype
->array_size
;
1931 array_size
= ctype
->array_size
= rtype
->array_size
;
1932 size
= get_expression_value(array_size
);
1934 rtype
= degenerate(expr
);
1937 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1942 case EXPR_INITIALIZER
:
1943 expr
->ctype
= ctype
;
1944 if (ctype
->type
== SYM_NODE
)
1945 ctype
= ctype
->ctype
.base_type
;
1947 switch (ctype
->type
) {
1950 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1952 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1954 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1956 return evaluate_scalar_initializer(ctype
, expr
);
1959 case EXPR_IDENTIFIER
:
1960 if (ctype
->type
== SYM_NODE
)
1961 ctype
= ctype
->ctype
.base_type
;
1962 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1963 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
1967 return evaluate_one_struct_initializer(ctype
, ep
,
1968 find_struct_ident(ctype
, expr
->expr_ident
));
1971 if (ctype
->type
== SYM_NODE
)
1972 ctype
= ctype
->ctype
.base_type
;
1973 if (ctype
->type
!= SYM_ARRAY
) {
1974 error(expr
->pos
, "expected array");
1977 return evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1981 * An EXPR_POS expression has already been evaluated, and we don't
1982 * need to do anything more
1988 static int get_as(struct symbol
*sym
)
1996 mod
= sym
->ctype
.modifiers
;
1997 if (sym
->type
== SYM_NODE
) {
1998 sym
= sym
->ctype
.base_type
;
1999 as
|= sym
->ctype
.as
;
2000 mod
|= sym
->ctype
.modifiers
;
2004 * At least for now, allow casting to a "unsigned long".
2005 * That's how we do things like pointer arithmetic and
2006 * store pointers to registers.
2008 if (sym
== &ulong_ctype
)
2011 if (sym
&& sym
->type
== SYM_PTR
) {
2012 sym
= sym
->ctype
.base_type
;
2013 as
|= sym
->ctype
.as
;
2014 mod
|= sym
->ctype
.modifiers
;
2016 if (mod
& MOD_FORCE
)
2021 static struct symbol
*evaluate_cast(struct expression
*expr
)
2023 struct expression
*target
= expr
->cast_expression
;
2024 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2030 expr
->ctype
= ctype
;
2031 expr
->cast_type
= ctype
;
2034 * Special case: a cast can be followed by an
2035 * initializer, in which case we need to pass
2036 * the type value down to that initializer rather
2037 * than trying to evaluate it as an expression
2039 * A more complex case is when the initializer is
2040 * dereferenced as part of a post-fix expression.
2041 * We need to produce an expression that can be dereferenced.
2043 if (target
->type
== EXPR_INITIALIZER
) {
2044 struct symbol
*sym
= expr
->cast_type
;
2045 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2047 sym
->initializer
= expr
->cast_expression
;
2048 evaluate_symbol(sym
);
2050 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2053 expr
->type
= EXPR_PREOP
;
2061 evaluate_expression(target
);
2065 * You can always throw a value away by casting to
2066 * "void" - that's an implicit "force". Note that
2067 * the same is _not_ true of "void *".
2069 if (ctype
== &void_ctype
)
2073 if (type
== SYM_NODE
) {
2074 type
= ctype
->ctype
.base_type
->type
;
2075 if (ctype
->ctype
.base_type
== &void_ctype
)
2078 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2079 warning(expr
->pos
, "cast to non-scalar");
2081 if (!target
->ctype
) {
2082 warning(expr
->pos
, "cast from unknown type");
2086 type
= target
->ctype
->type
;
2087 if (type
== SYM_NODE
)
2088 type
= target
->ctype
->ctype
.base_type
->type
;
2089 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2090 warning(expr
->pos
, "cast from non-scalar");
2092 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2093 warning(expr
->pos
, "cast removes address space of expression");
2095 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2096 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2097 if (t1
->type
== SYM_NODE
)
2098 t1
= t1
->ctype
.base_type
;
2099 if (t2
->type
== SYM_NODE
)
2100 t2
= t2
->ctype
.base_type
;
2102 if (t1
->type
== SYM_RESTRICT
)
2103 warning(expr
->pos
, "cast to restricted type");
2104 if (t2
->type
== SYM_RESTRICT
)
2105 warning(expr
->pos
, "cast from restricted type");
2110 * Casts of constant values are special: they
2111 * can be NULL, and thus need to be simplified
2114 if (target
->type
== EXPR_VALUE
)
2115 cast_value(expr
, ctype
, target
, target
->ctype
);
2122 * Evaluate a call expression with a symbol. This
2123 * should expand inline functions, and evaluate
2126 static int evaluate_symbol_call(struct expression
*expr
)
2128 struct expression
*fn
= expr
->fn
;
2129 struct symbol
*ctype
= fn
->ctype
;
2131 if (fn
->type
!= EXPR_PREOP
)
2134 if (ctype
->op
&& ctype
->op
->evaluate
)
2135 return ctype
->op
->evaluate(expr
);
2137 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2139 struct symbol
*curr
= current_fn
;
2140 unsigned long context
= current_context
;
2141 unsigned long mask
= current_contextmask
;
2143 current_context
|= ctype
->ctype
.context
;
2144 current_contextmask
|= ctype
->ctype
.contextmask
;
2145 current_fn
= ctype
->ctype
.base_type
;
2146 examine_fn_arguments(current_fn
);
2148 ret
= inline_function(expr
, ctype
);
2150 /* restore the old function context */
2152 current_context
= context
;
2153 current_contextmask
= mask
;
2160 static struct symbol
*evaluate_call(struct expression
*expr
)
2163 struct symbol
*ctype
, *sym
;
2164 struct expression
*fn
= expr
->fn
;
2165 struct expression_list
*arglist
= expr
->args
;
2167 if (!evaluate_expression(fn
))
2169 sym
= ctype
= fn
->ctype
;
2170 if (ctype
->type
== SYM_NODE
)
2171 ctype
= ctype
->ctype
.base_type
;
2172 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2173 ctype
= ctype
->ctype
.base_type
;
2174 if (!evaluate_arguments(sym
, ctype
, arglist
))
2176 if (ctype
->type
!= SYM_FN
) {
2177 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2180 args
= expression_list_size(expr
->args
);
2181 fnargs
= symbol_list_size(ctype
->arguments
);
2183 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2184 if (args
> fnargs
&& !ctype
->variadic
)
2185 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2186 if (sym
->type
== SYM_NODE
) {
2187 if (evaluate_symbol_call(expr
))
2190 expr
->ctype
= ctype
->ctype
.base_type
;
2194 struct symbol
*evaluate_expression(struct expression
*expr
)
2201 switch (expr
->type
) {
2204 warning(expr
->pos
, "value expression without a type");
2207 return evaluate_string(expr
);
2209 return evaluate_symbol_expression(expr
);
2211 if (!evaluate_expression(expr
->left
))
2213 if (!evaluate_expression(expr
->right
))
2215 return evaluate_binop(expr
);
2217 return evaluate_logical(expr
);
2219 evaluate_expression(expr
->left
);
2220 if (!evaluate_expression(expr
->right
))
2222 return evaluate_comma(expr
);
2224 if (!evaluate_expression(expr
->left
))
2226 if (!evaluate_expression(expr
->right
))
2228 return evaluate_compare(expr
);
2229 case EXPR_ASSIGNMENT
:
2230 if (!evaluate_expression(expr
->left
))
2232 if (!evaluate_expression(expr
->right
))
2234 return evaluate_assignment(expr
);
2236 if (!evaluate_expression(expr
->unop
))
2238 return evaluate_preop(expr
);
2240 if (!evaluate_expression(expr
->unop
))
2242 return evaluate_postop(expr
);
2244 return evaluate_cast(expr
);
2246 return evaluate_sizeof(expr
);
2248 return evaluate_alignof(expr
);
2250 return evaluate_member_dereference(expr
);
2252 return evaluate_call(expr
);
2254 warning(expr
->pos
, "bitfield generated by parser");
2257 case EXPR_CONDITIONAL
:
2258 if (!evaluate_conditional(&expr
->conditional
))
2260 if (!evaluate_expression(expr
->cond_false
))
2262 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
2264 return evaluate_conditional_expression(expr
);
2265 case EXPR_STATEMENT
:
2266 expr
->ctype
= evaluate_statement(expr
->statement
);
2270 expr
->ctype
= &ptr_ctype
;
2274 /* Evaluate the type of the symbol .. */
2275 evaluate_symbol(expr
->symbol
);
2276 /* .. but the type of the _expression_ is a "type" */
2277 expr
->ctype
= &type_ctype
;
2280 /* These can not exist as stand-alone expressions */
2281 case EXPR_INITIALIZER
:
2282 case EXPR_IDENTIFIER
:
2285 warning(expr
->pos
, "internal front-end error: initializer in expression");
2288 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2294 void check_duplicates(struct symbol
*sym
)
2296 struct symbol
*next
= sym
;
2298 while ((next
= next
->same_symbol
) != NULL
) {
2299 const char *typediff
;
2300 evaluate_symbol(next
);
2301 typediff
= type_difference(sym
, next
, 0, 0);
2303 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2304 show_ident(sym
->ident
),
2305 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2311 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2313 struct symbol
*base_type
;
2318 sym
= examine_symbol_type(sym
);
2319 base_type
= sym
->ctype
.base_type
;
2323 /* Evaluate the initializers */
2324 if (sym
->initializer
) {
2325 int count
= evaluate_initializer(sym
, &sym
->initializer
);
2326 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
2327 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
2328 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
2329 base_type
->bit_size
= bit_size
;
2330 sym
->array_size
= base_type
->array_size
;
2331 sym
->bit_size
= bit_size
;
2335 /* And finally, evaluate the body of the symbol too */
2336 if (base_type
->type
== SYM_FN
) {
2337 struct symbol
*curr
= current_fn
;
2338 unsigned long context
= current_context
;
2339 unsigned long mask
= current_contextmask
;
2341 current_fn
= base_type
;
2342 current_contextmask
= sym
->ctype
.contextmask
;
2343 current_context
= sym
->ctype
.context
;
2345 examine_fn_arguments(base_type
);
2346 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2348 if (base_type
->stmt
)
2349 evaluate_statement(base_type
->stmt
);
2352 current_contextmask
= mask
;
2353 current_context
= context
;
2359 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2361 struct expression
*expr
= stmt
->expression
;
2362 struct symbol
*ctype
, *fntype
;
2364 evaluate_expression(expr
);
2365 ctype
= degenerate(expr
);
2366 fntype
= current_fn
->ctype
.base_type
;
2367 if (!fntype
|| fntype
== &void_ctype
) {
2368 if (expr
&& ctype
!= &void_ctype
)
2369 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2374 warning(stmt
->pos
, "return with no return value");
2379 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2383 static void evaluate_if_statement(struct statement
*stmt
)
2385 if (!stmt
->if_conditional
)
2388 evaluate_conditional(&stmt
->if_conditional
);
2389 evaluate_statement(stmt
->if_true
);
2390 evaluate_statement(stmt
->if_false
);
2393 static void evaluate_iterator(struct statement
*stmt
)
2395 struct expression
**pre
= &stmt
->iterator_pre_condition
;
2396 struct expression
**post
= &stmt
->iterator_post_condition
;
2397 if (*pre
== *post
) {
2398 evaluate_conditional(pre
);
2401 evaluate_conditional(pre
);
2402 evaluate_conditional(post
);
2404 evaluate_statement(stmt
->iterator_pre_statement
);
2405 evaluate_statement(stmt
->iterator_statement
);
2406 evaluate_statement(stmt
->iterator_post_statement
);
2409 struct symbol
*evaluate_statement(struct statement
*stmt
)
2414 switch (stmt
->type
) {
2416 return evaluate_return_expression(stmt
);
2418 case STMT_EXPRESSION
:
2419 if (!evaluate_expression(stmt
->expression
))
2421 return degenerate(stmt
->expression
);
2423 case STMT_COMPOUND
: {
2424 struct statement
*s
;
2425 struct symbol
*type
= NULL
;
2428 /* Evaluate each symbol in the compound statement */
2429 FOR_EACH_PTR(stmt
->syms
, sym
) {
2430 evaluate_symbol(sym
);
2431 } END_FOR_EACH_PTR(sym
);
2432 evaluate_symbol(stmt
->ret
);
2435 * Then, evaluate each statement, making the type of the
2436 * compound statement be the type of the last statement
2439 FOR_EACH_PTR(stmt
->stmts
, s
) {
2440 type
= evaluate_statement(s
);
2441 } END_FOR_EACH_PTR(s
);
2447 evaluate_if_statement(stmt
);
2450 evaluate_iterator(stmt
);
2453 evaluate_expression(stmt
->switch_expression
);
2454 evaluate_statement(stmt
->switch_statement
);
2457 evaluate_expression(stmt
->case_expression
);
2458 evaluate_expression(stmt
->case_to
);
2459 evaluate_statement(stmt
->case_statement
);
2462 return evaluate_statement(stmt
->label_statement
);
2464 evaluate_expression(stmt
->goto_expression
);
2469 /* FIXME! Do the asm parameter evaluation! */