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 /* enum's 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
;
71 expr
->type
= EXPR_VALUE
;
72 expr
->value
= sym
->value
;
73 expr
->ctype
= base_type
;
77 static struct symbol
*evaluate_string(struct expression
*expr
)
79 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
80 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
81 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
82 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
83 unsigned int length
= expr
->string
->length
;
85 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
86 sym
->bit_size
= bits_in_char
* length
;
87 sym
->ctype
.alignment
= 1;
88 sym
->ctype
.modifiers
= MOD_STATIC
;
89 sym
->ctype
.base_type
= array
;
90 sym
->initializer
= initstr
;
93 initstr
->string
= expr
->string
;
95 array
->array_size
= sym
->array_size
;
96 array
->bit_size
= bits_in_char
* length
;
97 array
->ctype
.alignment
= 1;
98 array
->ctype
.modifiers
= MOD_STATIC
;
99 array
->ctype
.base_type
= &char_ctype
;
102 addr
->ctype
= &lazy_ptr_ctype
;
104 expr
->type
= EXPR_PREOP
;
111 static inline struct symbol
*integer_promotion(struct symbol
*type
)
113 unsigned long mod
= type
->ctype
.modifiers
;
116 if (type
->type
== SYM_ENUM
)
118 else if (type
->type
== SYM_BITFIELD
) {
119 mod
= type
->ctype
.base_type
->ctype
.modifiers
;
120 width
= type
->fieldwidth
;
121 } else if (mod
& (MOD_CHAR
| MOD_SHORT
))
122 width
= type
->bit_size
;
125 if (mod
& MOD_UNSIGNED
&& width
== bits_in_int
)
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
149 unsigned long lmod
, rmod
;
151 left
= integer_promotion(left
);
152 right
= integer_promotion(right
);
157 if (left
->bit_size
> right
->bit_size
)
160 if (right
->bit_size
> left
->bit_size
)
163 lmod
= left
->ctype
.modifiers
;
164 rmod
= right
->ctype
.modifiers
;
165 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
166 if (lmod
& MOD_UNSIGNED
)
168 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
176 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
178 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
180 expr
->cast_type
= type
;
181 expr
->cast_expression
= old
;
185 static int is_type_type(struct symbol
*type
)
187 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
190 static int is_ptr_type(struct symbol
*type
)
192 if (type
->type
== SYM_NODE
)
193 type
= type
->ctype
.base_type
;
194 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
197 static inline int is_float_type(struct symbol
*type
)
199 if (type
->type
== SYM_NODE
)
200 type
= type
->ctype
.base_type
;
201 return type
->ctype
.base_type
== &fp_type
;
204 static inline int is_byte_type(struct symbol
*type
)
206 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
209 static inline int is_string_type(struct symbol
*type
)
211 if (type
->type
== SYM_NODE
)
212 type
= type
->ctype
.base_type
;
213 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
216 static struct symbol
*bad_expr_type(struct expression
*expr
)
218 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
219 switch (expr
->type
) {
222 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
223 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
227 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
236 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
238 struct expression
*left
= *lp
, *right
= *rp
;
239 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
241 if (ltype
->type
== SYM_NODE
)
242 ltype
= ltype
->ctype
.base_type
;
243 if (rtype
->type
== SYM_NODE
)
244 rtype
= rtype
->ctype
.base_type
;
245 if (is_float_type(ltype
)) {
246 if (is_int_type(rtype
))
248 if (is_float_type(rtype
)) {
249 unsigned long lmod
= ltype
->ctype
.modifiers
;
250 unsigned long rmod
= rtype
->ctype
.modifiers
;
251 lmod
&= MOD_LONG
| MOD_LONGLONG
;
252 rmod
&= MOD_LONG
| MOD_LONGLONG
;
262 if (!is_float_type(rtype
) || !is_int_type(ltype
))
265 *lp
= cast_to(left
, rtype
);
268 *rp
= cast_to(right
, ltype
);
272 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
274 struct expression
*left
= *lp
, *right
= *rp
;
275 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
277 if (ltype
->type
== SYM_NODE
)
278 ltype
= ltype
->ctype
.base_type
;
279 if (rtype
->type
== SYM_NODE
)
280 rtype
= rtype
->ctype
.base_type
;
281 if (is_int_type(ltype
) && is_int_type(rtype
)) {
282 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
284 /* Don't bother promoting same-size entities, it only adds clutter */
285 if (ltype
->bit_size
!= ctype
->bit_size
)
286 *lp
= cast_to(left
, ctype
);
287 if (rtype
->bit_size
!= ctype
->bit_size
)
288 *rp
= cast_to(right
, ctype
);
294 static int restricted_value(struct expression
*v
, struct symbol
*type
)
296 if (v
->type
!= EXPR_VALUE
)
303 static int restricted_binop(int op
, struct symbol
*type
)
311 case SPECIAL_NOTEQUAL
:
318 static int restricted_unop(int op
, struct symbol
*type
)
320 if (op
== '~' && type
->bit_size
>= bits_in_int
)
325 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
327 struct expression
*left
= *lp
, *right
= *rp
;
328 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
329 struct symbol
*type
= NULL
;
331 if (ltype
->type
== SYM_NODE
)
332 ltype
= ltype
->ctype
.base_type
;
333 if (rtype
->type
== SYM_NODE
)
334 rtype
= rtype
->ctype
.base_type
;
335 if (is_restricted_type(ltype
)) {
336 if (is_restricted_type(rtype
)) {
340 if (!restricted_value(right
, ltype
))
343 } else if (is_restricted_type(rtype
)) {
344 if (!restricted_value(left
, rtype
))
349 if (restricted_binop(op
, type
))
354 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
356 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
357 if (!ctype
&& float_ok
)
358 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
360 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
365 return bad_expr_type(expr
);
368 static inline int lvalue_expression(struct expression
*expr
)
370 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
373 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
375 struct symbol
*ctype
;
376 struct symbol
*ptr_type
= ptr
->ctype
;
379 if (ptr_type
->type
== SYM_NODE
)
380 ptr_type
= ptr_type
->ctype
.base_type
;
382 if (!is_int_type(i
->ctype
))
383 return bad_expr_type(expr
);
386 examine_symbol_type(ctype
);
388 ctype
= degenerate(ptr
);
389 if (!ctype
->ctype
.base_type
) {
390 warning(expr
->pos
, "missing type information");
394 /* Get the size of whatever the pointer points to */
396 if (ptr_type
->type
== SYM_NODE
)
397 ptr_type
= ptr_type
->ctype
.base_type
;
398 if (ptr_type
->type
== SYM_PTR
)
399 ptr_type
= ptr_type
->ctype
.base_type
;
400 bit_size
= ptr_type
->bit_size
;
402 /* Special case: adding zero commonly happens as a result of 'array[0]' */
403 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
405 } else if (bit_size
> bits_in_char
) {
406 struct expression
*add
= expr
;
407 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
408 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
410 val
->ctype
= size_t_ctype
;
411 val
->value
= bit_size
>> 3;
414 mul
->ctype
= size_t_ctype
;
418 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
427 static struct symbol
*evaluate_add(struct expression
*expr
)
429 struct expression
*left
= expr
->left
, *right
= expr
->right
;
430 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
432 if (is_ptr_type(ltype
))
433 return evaluate_ptr_add(expr
, left
, right
);
435 if (is_ptr_type(rtype
))
436 return evaluate_ptr_add(expr
, right
, left
);
438 return evaluate_arith(expr
, 1);
441 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
442 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
443 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
444 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
446 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
447 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
450 unsigned long mod1
, mod2
, diff
;
451 unsigned long as1
, as2
;
453 struct symbol
*base1
, *base2
;
455 if (target
== source
)
457 if (!target
|| !source
)
458 return "different types";
460 * Peel of per-node information.
461 * FIXME! Check alignment and context too here!
463 mod1
= target
->ctype
.modifiers
;
464 as1
= target
->ctype
.as
;
465 mod2
= source
->ctype
.modifiers
;
466 as2
= source
->ctype
.as
;
467 if (target
->type
== SYM_NODE
) {
468 target
= target
->ctype
.base_type
;
471 if (target
->type
== SYM_PTR
) {
475 mod1
|= target
->ctype
.modifiers
;
476 as1
|= target
->ctype
.as
;
478 if (source
->type
== SYM_NODE
) {
479 source
= source
->ctype
.base_type
;
482 if (source
->type
== SYM_PTR
) {
486 mod2
|= source
->ctype
.modifiers
;
487 as2
|= source
->ctype
.as
;
490 if (target
== source
)
492 if (!target
|| !source
)
493 return "different types";
495 type1
= target
->type
;
496 base1
= target
->ctype
.base_type
;
498 type2
= source
->type
;
499 base2
= source
->ctype
.base_type
;
502 * Pointers to functions compare as the function itself
504 if (type1
== SYM_PTR
&& base1
) {
505 switch (base1
->type
) {
509 base1
= base1
->ctype
.base_type
;
514 if (type2
== SYM_PTR
&& base2
) {
515 switch (base2
->type
) {
519 base2
= base2
->ctype
.base_type
;
525 /* Arrays degenerate to pointers for type comparisons */
526 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
527 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
529 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
530 return "different base types";
532 /* Must be same address space to be comparable */
534 return "different address spaces";
536 /* Ignore differences in storage types or addressability */
537 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
538 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
541 return "different type sizes";
542 if (diff
& ~MOD_SIGNEDNESS
)
543 return "different modifiers";
545 /* Differs in signedness only.. */
548 * Warn if both are explicitly signed ("unsigned" is obvously
549 * always explicit, and since we know one of them has to be
550 * unsigned, we check if the signed one was explicit).
552 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
553 return "different explicit signedness";
556 * "char" matches both "unsigned char" and "signed char",
557 * so if the explicit test didn't trigger, then we should
558 * not warn about a char.
560 if (!(mod1
& MOD_CHAR
))
561 return "different signedness";
565 if (type1
== SYM_FN
) {
567 struct symbol
*arg1
, *arg2
;
568 if (base1
->variadic
!= base2
->variadic
)
569 return "incompatible variadic arguments";
570 PREPARE_PTR_LIST(target
->arguments
, arg1
);
571 PREPARE_PTR_LIST(source
->arguments
, arg2
);
575 diff
= type_difference(arg1
, arg2
, 0, 0);
577 static char argdiff
[80];
578 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
587 FINISH_PTR_LIST(arg2
);
588 FINISH_PTR_LIST(arg1
);
597 static int is_null_ptr(struct expression
*expr
)
599 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
601 if (!is_ptr_type(expr
->ctype
))
602 warning(expr
->pos
, "Using plain integer as NULL pointer");
606 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
608 /* NULL expression? Just return the type of the "other side" */
617 * Ignore differences in "volatile" and "const"ness when
618 * subtracting pointers
620 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
622 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
624 const char *typediff
;
625 struct symbol
*ctype
;
626 struct symbol
*ltype
, *rtype
;
628 ltype
= degenerate(l
);
629 rtype
= degenerate(r
);
632 * If it is an integer subtract: the ptr add case will do the
635 if (!is_ptr_type(rtype
))
636 return evaluate_ptr_add(expr
, l
, r
);
639 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
641 ctype
= common_ptr_type(l
, r
);
643 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
647 examine_symbol_type(ctype
);
649 /* Figure out the base type we point to */
650 if (ctype
->type
== SYM_NODE
)
651 ctype
= ctype
->ctype
.base_type
;
652 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
653 warning(expr
->pos
, "subtraction of functions? Share your drugs");
656 ctype
= ctype
->ctype
.base_type
;
658 expr
->ctype
= ssize_t_ctype
;
659 if (ctype
->bit_size
> bits_in_char
) {
660 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
661 struct expression
*div
= expr
;
662 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
664 val
->ctype
= size_t_ctype
;
665 val
->value
= ctype
->bit_size
>> 3;
668 sub
->ctype
= ssize_t_ctype
;
677 return ssize_t_ctype
;
680 static struct symbol
*evaluate_sub(struct expression
*expr
)
682 struct expression
*left
= expr
->left
, *right
= expr
->right
;
683 struct symbol
*ltype
= left
->ctype
;
685 if (is_ptr_type(ltype
))
686 return evaluate_ptr_sub(expr
, left
, right
);
688 return evaluate_arith(expr
, 1);
691 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
693 static struct symbol
*evaluate_conditional(struct expression
**p
)
695 struct symbol
*ctype
;
696 struct expression
*expr
= *p
;
701 if (expr
->type
== EXPR_ASSIGNMENT
)
702 warning(expr
->pos
, "assignment expression in conditional");
704 ctype
= evaluate_expression(expr
);
706 if (is_safe_type(ctype
))
707 warning(expr
->pos
, "testing a 'safe expression'");
708 if (is_float_type(ctype
)) {
709 struct expression
*comp
;
711 * It's easier to handle here, rather than deal with
712 * FP all over the place. Floating point in boolean
713 * context is rare enough (and very often wrong),
714 * so price of explicit comparison with appropriate
715 * FP zero is not too high. And it simplifies things
718 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
719 comp
->op
= SPECIAL_NOTEQUAL
;
721 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
722 comp
->right
->ctype
= comp
->left
->ctype
;
723 comp
->right
->fvalue
= 0;
724 ctype
= comp
->ctype
= &bool_ctype
;
732 static struct symbol
*evaluate_logical(struct expression
*expr
)
734 if (!evaluate_conditional(&expr
->left
))
736 if (!evaluate_conditional(&expr
->right
))
739 expr
->ctype
= &bool_ctype
;
743 static struct symbol
*evaluate_shift(struct expression
*expr
)
745 struct expression
*left
= expr
->left
, *right
= expr
->right
;
746 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
748 if (ltype
->type
== SYM_NODE
)
749 ltype
= ltype
->ctype
.base_type
;
750 if (rtype
->type
== SYM_NODE
)
751 rtype
= rtype
->ctype
.base_type
;
752 if (is_int_type(ltype
) && is_int_type(rtype
)) {
753 struct symbol
*ctype
= integer_promotion(ltype
);
754 if (ltype
->bit_size
!= ctype
->bit_size
)
755 expr
->left
= cast_to(expr
->left
, ctype
);
757 ctype
= integer_promotion(rtype
);
758 if (rtype
->bit_size
!= ctype
->bit_size
)
759 expr
->right
= cast_to(expr
->right
, ctype
);
762 return bad_expr_type(expr
);
765 static struct symbol
*evaluate_binop(struct expression
*expr
)
768 // addition can take ptr+int, fp and int
770 return evaluate_add(expr
);
772 // subtraction can take ptr-ptr, fp and int
774 return evaluate_sub(expr
);
776 // Arithmetic operations can take fp and int
778 return evaluate_arith(expr
, 1);
780 // shifts do integer promotions, but that's it.
781 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
782 return evaluate_shift(expr
);
784 // The rest are integer operations
785 // '%', '&', '^', '|'
787 return evaluate_arith(expr
, 0);
791 static struct symbol
*evaluate_comma(struct expression
*expr
)
793 expr
->ctype
= expr
->right
->ctype
;
797 static int modify_for_unsigned(int op
)
800 op
= SPECIAL_UNSIGNED_LT
;
802 op
= SPECIAL_UNSIGNED_GT
;
803 else if (op
== SPECIAL_LTE
)
804 op
= SPECIAL_UNSIGNED_LTE
;
805 else if (op
== SPECIAL_GTE
)
806 op
= SPECIAL_UNSIGNED_GTE
;
810 static struct symbol
*evaluate_compare(struct expression
*expr
)
812 struct expression
*left
= expr
->left
, *right
= expr
->right
;
813 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
814 struct symbol
*ctype
;
817 if (is_type_type(ltype
) && is_type_type(rtype
))
820 if (is_safe_type(ltype
) || is_safe_type(rtype
))
821 warning(expr
->pos
, "testing a 'safe expression'");
824 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
825 // FIXME! Check the types for compatibility
829 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
831 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
832 expr
->op
= modify_for_unsigned(expr
->op
);
836 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
840 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
847 expr
->ctype
= &bool_ctype
;
852 * FIXME!! This should do casts, array degeneration etc..
854 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
856 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
858 if (ltype
->type
== SYM_NODE
)
859 ltype
= ltype
->ctype
.base_type
;
861 if (rtype
->type
== SYM_NODE
)
862 rtype
= rtype
->ctype
.base_type
;
864 if (ltype
->type
== SYM_PTR
) {
865 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
869 if (rtype
->type
== SYM_PTR
) {
870 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
876 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
878 struct expression
*cond
, *false;
879 struct expression
**true_p
;
880 struct symbol
*ctype
, *ltype
, *rtype
;
881 const char * typediff
;
883 ctype
= degenerate(expr
->conditional
);
884 cond
= expr
->conditional
;
887 true_p
= &expr
->conditional
;
888 if (expr
->cond_true
) {
889 ltype
= degenerate(expr
->cond_true
);
890 true_p
= &expr
->cond_true
;
893 rtype
= degenerate(expr
->cond_false
);
894 false = expr
->cond_false
;
897 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
901 ctype
= compatible_integer_binop(true_p
, &expr
->cond_false
);
904 ctype
= compatible_ptr_type(*true_p
, expr
->cond_false
);
907 ctype
= compatible_float_binop(true_p
, &expr
->cond_false
);
910 ctype
= compatible_restricted_binop('?', true_p
, &expr
->cond_false
);
913 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
921 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
922 struct expression
**rp
, struct symbol
*source
, const char *where
)
924 const char *typediff
;
928 /* It's ok if the target is more volatile or const than the source */
929 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
933 if (is_int_type(target
)) {
934 if (is_int_type(source
)) {
935 if (target
->bit_size
!= source
->bit_size
)
939 if (is_float_type(source
))
941 } else if (is_float_type(target
)) {
942 if (is_int_type(source
))
944 if (is_float_type(source
)) {
945 if (target
->bit_size
!= source
->bit_size
)
951 if (is_restricted_type(target
) && !restricted_value(*rp
, target
))
954 /* Pointer destination? */
956 target_as
= t
->ctype
.as
;
957 if (t
->type
== SYM_NODE
) {
958 t
= t
->ctype
.base_type
;
959 target_as
|= t
->ctype
.as
;
961 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
962 struct expression
*right
= *rp
;
963 struct symbol
*s
= source
;
966 // NULL pointer is always ok
967 if (is_null_ptr(right
))
970 /* "void *" matches anything as long as the address space is ok */
971 source_as
= s
->ctype
.as
;
972 if (s
->type
== SYM_NODE
) {
973 s
= s
->ctype
.base_type
;
974 source_as
|= s
->ctype
.as
;
976 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
977 s
= s
->ctype
.base_type
;
978 t
= t
->ctype
.base_type
;
979 if (s
== &void_ctype
|| t
== &void_ctype
)
984 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
985 info(expr
->pos
, " expected %s", show_typename(target
));
986 info(expr
->pos
, " got %s", show_typename(source
));
987 *rp
= cast_to(*rp
, target
);
990 *rp
= cast_to(*rp
, target
);
995 * FIXME!! This is wrong from a double evaluation standpoint. We can't
996 * just expand the expression twice, that would make any side effects
999 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
1002 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1003 static const int op_trans
[] = {
1004 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
1005 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
1006 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
1007 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
1008 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
1009 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
1010 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
1011 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
1012 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
1013 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
1015 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
, *e5
;
1016 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1017 struct symbol
*ltype
= left
->ctype
;
1018 struct expression
*addr
;
1019 struct symbol
*lptype
;
1021 if (left
->type
== EXPR_BITFIELD
)
1022 addr
= left
->address
;
1026 lptype
= addr
->ctype
;
1028 a
->ctype
.base_type
= lptype
;
1029 a
->bit_size
= lptype
->bit_size
;
1030 a
->array_size
= lptype
->array_size
;
1032 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1034 e0
->ctype
= &lazy_ptr_ctype
;
1036 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1041 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1047 /* we can't cannibalize left, unfortunately */
1048 e3
= alloc_expression(expr
->pos
, left
->type
);
1050 if (e3
->type
== EXPR_BITFIELD
)
1055 e4
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1056 e4
->op
= subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
1059 /* will calculate type later */
1061 e5
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1062 e5
->left
= e3
; /* we can share that one */
1067 expr
->type
= EXPR_COMMA
;
1070 expr
->ctype
= ltype
;
1072 return evaluate_binop(e4
);
1075 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1077 if (type
->ctype
.modifiers
& MOD_CONST
)
1078 warning(left
->pos
, "assignment to const expression");
1079 if (type
->type
== SYM_NODE
)
1080 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1083 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1085 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1086 struct expression
*where
= expr
;
1087 struct symbol
*ltype
, *rtype
;
1089 if (!lvalue_expression(left
)) {
1090 warning(expr
->pos
, "not an lvalue");
1094 ltype
= left
->ctype
;
1096 if (expr
->op
!= '=') {
1097 if (!evaluate_binop_assignment(expr
, left
, right
))
1099 where
= expr
->right
; /* expr is EXPR_COMMA now */
1101 right
= where
->right
;
1104 rtype
= degenerate(right
);
1106 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1109 evaluate_assign_to(left
, ltype
);
1111 expr
->ctype
= ltype
;
1115 static void examine_fn_arguments(struct symbol
*fn
)
1119 FOR_EACH_PTR(fn
->arguments
, s
) {
1120 struct symbol
*arg
= evaluate_symbol(s
);
1121 /* Array/function arguments silently degenerate into pointers */
1127 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1128 if (arg
->type
== SYM_ARRAY
)
1129 ptr
->ctype
= arg
->ctype
;
1131 ptr
->ctype
.base_type
= arg
;
1132 ptr
->ctype
.as
|= s
->ctype
.as
;
1133 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1135 s
->ctype
.base_type
= ptr
;
1137 s
->ctype
.modifiers
= 0;
1138 examine_symbol_type(s
);
1145 } END_FOR_EACH_PTR(s
);
1148 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1150 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1151 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1153 newsym
->ctype
.as
= as
;
1154 newsym
->ctype
.modifiers
= mod
;
1160 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1162 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1163 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1165 node
->ctype
.base_type
= ptr
;
1166 ptr
->bit_size
= bits_in_pointer
;
1167 ptr
->ctype
.alignment
= pointer_alignment
;
1169 node
->bit_size
= bits_in_pointer
;
1170 node
->ctype
.alignment
= pointer_alignment
;
1173 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1174 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1175 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1176 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1178 if (sym
->type
== SYM_NODE
) {
1179 ptr
->ctype
.as
|= sym
->ctype
.as
;
1180 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1181 sym
= sym
->ctype
.base_type
;
1183 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1184 ptr
->ctype
.as
|= sym
->ctype
.as
;
1185 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1186 sym
= sym
->ctype
.base_type
;
1188 ptr
->ctype
.base_type
= sym
;
1193 /* Arrays degenerate into pointers on pointer arithmetic */
1194 static struct symbol
*degenerate(struct expression
*expr
)
1196 struct symbol
*ctype
, *base
;
1200 ctype
= expr
->ctype
;
1204 if (ctype
->type
== SYM_NODE
)
1205 base
= ctype
->ctype
.base_type
;
1207 * Arrays degenerate into pointers to the entries, while
1208 * functions degenerate into pointers to themselves.
1209 * If array was part of non-lvalue compound, we create a copy
1210 * of that compound first and then act as if we were dealing with
1211 * the corresponding field in there.
1213 switch (base
->type
) {
1215 if (expr
->type
== EXPR_SLICE
) {
1216 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1217 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1219 a
->ctype
.base_type
= expr
->base
->ctype
;
1220 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1221 a
->array_size
= expr
->base
->ctype
->array_size
;
1223 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1225 e0
->ctype
= &lazy_ptr_ctype
;
1227 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1230 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1232 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1234 e2
->right
= expr
->base
;
1236 e2
->ctype
= expr
->base
->ctype
;
1238 if (expr
->r_bitpos
) {
1239 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1242 e3
->right
= alloc_const_expression(expr
->pos
,
1243 expr
->r_bitpos
>> 3);
1244 e3
->ctype
= &lazy_ptr_ctype
;
1249 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1252 e4
->ctype
= &lazy_ptr_ctype
;
1255 expr
->type
= EXPR_PREOP
;
1259 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1260 warning(expr
->pos
, "strange non-value function or array");
1263 *expr
= *expr
->unop
;
1264 ctype
= create_pointer(expr
, ctype
, 1);
1265 expr
->ctype
= ctype
;
1272 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1274 struct expression
*op
= expr
->unop
;
1275 struct symbol
*ctype
;
1277 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1278 warning(expr
->pos
, "not addressable");
1285 * symbol expression evaluation is lazy about the type
1286 * of the sub-expression, so we may have to generate
1287 * the type here if so..
1289 if (expr
->ctype
== &lazy_ptr_ctype
) {
1290 ctype
= create_pointer(expr
, ctype
, 0);
1291 expr
->ctype
= ctype
;
1297 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1299 struct expression
*op
= expr
->unop
;
1300 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1302 /* Simplify: *&(expr) => (expr) */
1303 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1308 /* Dereferencing a node drops all the node information. */
1309 if (ctype
->type
== SYM_NODE
)
1310 ctype
= ctype
->ctype
.base_type
;
1312 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1313 target
= ctype
->ctype
.base_type
;
1315 switch (ctype
->type
) {
1317 warning(expr
->pos
, "cannot derefence this type");
1320 merge_type(node
, ctype
);
1321 if (ctype
->type
!= SYM_ARRAY
)
1324 * Dereferencing a pointer to an array results in a
1325 * degenerate dereference: the expression becomes
1326 * just a pointer to the entry, and the derefence
1331 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1332 target
->bit_size
= bits_in_pointer
;
1333 target
->ctype
.alignment
= pointer_alignment
;
1334 merge_type(target
, ctype
->ctype
.base_type
);
1338 if (!lvalue_expression(op
)) {
1339 warning(op
->pos
, "non-lvalue array??");
1343 /* Do the implied "addressof" on the array */
1347 * When an array is dereferenced, we need to pick
1348 * up the attributes of the original node too..
1350 merge_type(node
, op
->ctype
);
1351 merge_type(node
, ctype
);
1355 node
->bit_size
= target
->bit_size
;
1356 node
->array_size
= target
->array_size
;
1363 * Unary post-ops: x++ and x--
1365 static struct symbol
*evaluate_postop(struct expression
*expr
)
1367 struct expression
*op
= expr
->unop
;
1368 struct symbol
*ctype
= op
->ctype
;
1370 if (!lvalue_expression(expr
->unop
)) {
1371 warning(expr
->pos
, "need lvalue expression for ++/--");
1374 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1375 warning(expr
->pos
, "bad operation on restricted");
1379 evaluate_assign_to(op
, ctype
);
1381 expr
->ctype
= ctype
;
1385 static struct symbol
*evaluate_sign(struct expression
*expr
)
1387 struct symbol
*ctype
= expr
->unop
->ctype
;
1388 if (is_int_type(ctype
)) {
1389 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1390 if (rtype
->bit_size
!= ctype
->bit_size
)
1391 expr
->unop
= cast_to(expr
->unop
, rtype
);
1393 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1394 /* no conversions needed */
1395 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1396 /* no conversions needed */
1398 return bad_expr_type(expr
);
1400 if (expr
->op
== '+')
1401 *expr
= *expr
->unop
;
1402 expr
->ctype
= ctype
;
1406 static struct symbol
*evaluate_preop(struct expression
*expr
)
1408 struct symbol
*ctype
= expr
->unop
->ctype
;
1412 *expr
= *expr
->unop
;
1418 return evaluate_sign(expr
);
1421 return evaluate_dereference(expr
);
1424 return evaluate_addressof(expr
);
1426 case SPECIAL_INCREMENT
:
1427 case SPECIAL_DECREMENT
:
1429 * From a type evaluation standpoint the pre-ops are
1430 * the same as the postops
1432 return evaluate_postop(expr
);
1435 if (is_safe_type(ctype
))
1436 warning(expr
->pos
, "testing a 'safe expression'");
1437 if (is_float_type(ctype
)) {
1438 struct expression
*arg
= expr
->unop
;
1439 expr
->type
= EXPR_BINOP
;
1440 expr
->op
= SPECIAL_EQUAL
;
1442 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1443 expr
->right
->ctype
= ctype
;
1444 expr
->right
->fvalue
= 0;
1446 ctype
= &bool_ctype
;
1452 expr
->ctype
= ctype
;
1456 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1458 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1459 struct ptr_list
*list
= head
;
1465 for (i
= 0; i
< list
->nr
; i
++) {
1466 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1468 if (sym
->ident
!= ident
)
1470 *offset
= sym
->offset
;
1473 struct symbol
*ctype
= sym
->ctype
.base_type
;
1477 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1479 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1482 *offset
+= sym
->offset
;
1486 } while ((list
= list
->next
) != head
);
1490 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1492 struct expression
*add
;
1495 * Create a new add-expression
1497 * NOTE! Even if we just add zero, we need a new node
1498 * for the member pointer, since it has a different
1499 * type than the original pointer. We could make that
1500 * be just a cast, but the fact is, a node is a node,
1501 * so we might as well just do the "add zero" here.
1503 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1506 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1507 add
->right
->ctype
= &int_ctype
;
1508 add
->right
->value
= offset
;
1511 * The ctype of the pointer will be lazily evaluated if
1512 * we ever take the address of this member dereference..
1514 add
->ctype
= &lazy_ptr_ctype
;
1518 /* structure/union dereference */
1519 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1522 struct symbol
*ctype
, *member
;
1523 struct expression
*deref
= expr
->deref
, *add
;
1524 struct ident
*ident
= expr
->member
;
1528 if (!evaluate_expression(deref
))
1531 warning(expr
->pos
, "bad member name");
1535 ctype
= deref
->ctype
;
1536 address_space
= ctype
->ctype
.as
;
1537 mod
= ctype
->ctype
.modifiers
;
1538 if (ctype
->type
== SYM_NODE
) {
1539 ctype
= ctype
->ctype
.base_type
;
1540 address_space
|= ctype
->ctype
.as
;
1541 mod
|= ctype
->ctype
.modifiers
;
1543 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1544 warning(expr
->pos
, "expected structure or union");
1548 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1550 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1551 const char *name
= "<unnamed>";
1554 name
= ctype
->ident
->name
;
1555 namelen
= ctype
->ident
->len
;
1557 warning(expr
->pos
, "no member '%s' in %s %.*s",
1558 show_ident(ident
), type
, namelen
, name
);
1563 * The member needs to take on the address space and modifiers of
1564 * the "parent" type.
1566 member
= convert_to_as_mod(member
, address_space
, mod
);
1567 ctype
= member
->ctype
.base_type
;
1569 if (!lvalue_expression(deref
)) {
1570 if (deref
->type
!= EXPR_SLICE
) {
1574 expr
->base
= deref
->base
;
1575 expr
->r_bitpos
= deref
->r_bitpos
;
1577 expr
->r_bitpos
+= offset
<< 3;
1578 expr
->type
= EXPR_SLICE
;
1579 if (ctype
->type
== SYM_BITFIELD
) {
1580 expr
->r_bitpos
+= member
->bit_offset
;
1581 expr
->r_nrbits
= member
->fieldwidth
;
1583 expr
->r_nrbits
= member
->bit_size
;
1585 expr
->ctype
= member
;
1589 deref
= deref
->unop
;
1590 expr
->deref
= deref
;
1592 add
= evaluate_offset(deref
, offset
);
1593 if (ctype
->type
== SYM_BITFIELD
) {
1594 expr
->type
= EXPR_BITFIELD
;
1595 expr
->bitpos
= member
->bit_offset
;
1596 expr
->nrbits
= member
->fieldwidth
;
1597 expr
->address
= add
;
1599 expr
->type
= EXPR_PREOP
;
1604 expr
->ctype
= member
;
1608 static int is_promoted(struct expression
*expr
)
1611 switch (expr
->type
) {
1614 case EXPR_CONDITIONAL
:
1638 static struct symbol
*evaluate_cast(struct expression
*);
1640 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1642 struct expression
*what
= expr
->cast_expression
;
1645 if (expr
->cast_type
) {
1647 struct symbol
*sym
= evaluate_cast(expr
);
1648 size
= sym
->bit_size
;
1650 examine_symbol_type(expr
->cast_type
);
1651 size
= expr
->cast_type
->bit_size
;
1654 if (!evaluate_expression(what
))
1656 size
= what
->ctype
->bit_size
;
1657 if (is_restricted_type(what
->ctype
)) {
1658 if (size
< bits_in_int
&& is_promoted(what
))
1661 if (is_bitfield_type(what
->ctype
))
1662 warning(expr
->pos
, "sizeof applied to bitfield type");
1665 warning(expr
->pos
, "cannot size expression");
1666 expr
->type
= EXPR_VALUE
;
1667 expr
->value
= size
>> 3;
1668 expr
->ctype
= size_t_ctype
;
1669 return size_t_ctype
;
1672 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1674 struct symbol
*type
= expr
->cast_type
;
1677 type
= evaluate_expression(expr
->cast_expression
);
1681 if (is_bitfield_type(type
))
1682 warning(expr
->pos
, "alignof applied to bitfield type");
1683 examine_symbol_type(type
);
1684 expr
->type
= EXPR_VALUE
;
1685 expr
->value
= type
->ctype
.alignment
;
1686 expr
->ctype
= size_t_ctype
;
1687 return size_t_ctype
;
1690 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1692 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1693 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1697 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1699 struct expression
*expr
;
1700 struct symbol_list
*argument_types
= fn
->arguments
;
1701 struct symbol
*argtype
;
1704 PREPARE_PTR_LIST(argument_types
, argtype
);
1705 FOR_EACH_PTR (head
, expr
) {
1706 struct expression
**p
= THIS_ADDRESS(expr
);
1707 struct symbol
*ctype
, *target
;
1708 ctype
= evaluate_expression(expr
);
1713 if (context_clash(f
, ctype
))
1714 warning(expr
->pos
, "argument %d used in wrong context", i
);
1716 ctype
= degenerate(expr
);
1719 if (!target
&& ctype
->bit_size
< bits_in_int
)
1720 target
= &int_ctype
;
1722 static char where
[30];
1723 examine_symbol_type(target
);
1724 sprintf(where
, "argument %d", i
);
1725 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1729 NEXT_PTR_LIST(argtype
);
1730 } END_FOR_EACH_PTR(expr
);
1731 FINISH_PTR_LIST(argtype
);
1735 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1737 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1739 struct expression
*entry
= *ep
;
1740 struct expression
**parent
, *reuse
= NULL
;
1741 unsigned long offset
;
1743 unsigned long from
, to
;
1744 int accept_string
= is_byte_type(ctype
);
1749 if (entry
->type
== EXPR_INDEX
) {
1750 from
= entry
->idx_from
;
1752 parent
= &entry
->idx_expression
;
1754 entry
= entry
->idx_expression
;
1757 offset
= from
* (ctype
->bit_size
>>3);
1759 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1760 reuse
->type
= EXPR_POS
;
1761 reuse
->init_offset
= offset
;
1762 reuse
->init_nr
= to
- from
;
1763 reuse
->init_expr
= entry
;
1764 parent
= &reuse
->init_expr
;
1769 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1770 sym
= evaluate_expression(entry
);
1771 to
= from
+ get_expression_value(sym
->array_size
);
1773 evaluate_initializer(ctype
, parent
);
1778 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1780 struct expression
*entry
;
1784 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1785 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1788 } END_FOR_EACH_PTR(entry
);
1792 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1793 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1795 if (expression_list_size(expr
->expr_list
) != 1) {
1796 warning(expr
->pos
, "unexpected compound initializer");
1799 return evaluate_array_initializer(ctype
, expr
);
1802 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1806 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1807 if (sym
->ident
== ident
)
1809 } END_FOR_EACH_PTR(sym
);
1813 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1815 struct expression
*entry
= *ep
;
1816 struct expression
**parent
;
1817 struct expression
*reuse
= NULL
;
1818 unsigned long offset
;
1821 error(entry
->pos
, "unknown named initializer");
1825 if (entry
->type
== EXPR_IDENTIFIER
) {
1827 entry
= entry
->ident_expression
;
1831 offset
= sym
->offset
;
1834 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1835 reuse
->type
= EXPR_POS
;
1836 reuse
->init_offset
= offset
;
1838 reuse
->init_expr
= entry
;
1839 parent
= &reuse
->init_expr
;
1843 evaluate_initializer(sym
, parent
);
1847 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1849 struct expression
*entry
;
1852 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1853 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1854 if (entry
->type
== EXPR_IDENTIFIER
) {
1855 struct ident
*ident
= entry
->expr_ident
;
1856 /* We special-case the "already right place" case */
1857 if (!sym
|| sym
->ident
!= ident
) {
1858 RESET_PTR_LIST(sym
);
1862 if (sym
->ident
== ident
)
1868 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1871 } END_FOR_EACH_PTR(entry
);
1872 FINISH_PTR_LIST(sym
);
1878 * Initializers are kind of like assignments. Except
1879 * they can be a hell of a lot more complex.
1881 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1883 struct expression
*expr
= *ep
;
1886 * Simple non-structure/array initializers are the simple
1887 * case, and look (and parse) largely like assignments.
1889 switch (expr
->type
) {
1891 int size
= 0, is_string
= expr
->type
== EXPR_STRING
;
1892 struct symbol
*rtype
= evaluate_expression(expr
);
1896 * char array[] = "string"
1897 * should _not_ degenerate.
1899 if (is_string
&& is_string_type(ctype
)) {
1900 struct expression
*array_size
= ctype
->array_size
;
1902 array_size
= ctype
->array_size
= rtype
->array_size
;
1903 size
= get_expression_value(array_size
);
1905 rtype
= degenerate(expr
);
1908 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1913 case EXPR_INITIALIZER
:
1914 expr
->ctype
= ctype
;
1915 if (ctype
->type
== SYM_NODE
)
1916 ctype
= ctype
->ctype
.base_type
;
1918 switch (ctype
->type
) {
1921 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1923 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1925 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1927 return 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 return evaluate_one_struct_initializer(ctype
, ep
,
1939 find_struct_ident(ctype
, expr
->expr_ident
));
1942 if (ctype
->type
== SYM_NODE
)
1943 ctype
= ctype
->ctype
.base_type
;
1944 if (ctype
->type
!= SYM_ARRAY
) {
1945 error(expr
->pos
, "expected array");
1948 return evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
1952 static int get_as(struct symbol
*sym
)
1960 mod
= sym
->ctype
.modifiers
;
1961 if (sym
->type
== SYM_NODE
) {
1962 sym
= sym
->ctype
.base_type
;
1963 as
|= sym
->ctype
.as
;
1964 mod
|= sym
->ctype
.modifiers
;
1968 * At least for now, allow casting to a "unsigned long".
1969 * That's how we do things like pointer arithmetic and
1970 * store pointers to registers.
1972 if (sym
== &ulong_ctype
)
1975 if (sym
&& sym
->type
== SYM_PTR
) {
1976 sym
= sym
->ctype
.base_type
;
1977 as
|= sym
->ctype
.as
;
1978 mod
|= sym
->ctype
.modifiers
;
1980 if (mod
& MOD_FORCE
)
1985 static struct symbol
*evaluate_cast(struct expression
*expr
)
1987 struct expression
*target
= expr
->cast_expression
;
1988 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1991 expr
->ctype
= ctype
;
1992 expr
->cast_type
= ctype
;
1995 * Special case: a cast can be followed by an
1996 * initializer, in which case we need to pass
1997 * the type value down to that initializer rather
1998 * than trying to evaluate it as an expression
2000 * A more complex case is when the initializer is
2001 * dereferenced as part of a post-fix expression.
2002 * We need to produce an expression that can be dereferenced.
2004 if (target
->type
== EXPR_INITIALIZER
) {
2005 struct symbol
*sym
= expr
->cast_type
;
2006 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2008 sym
->initializer
= expr
->cast_expression
;
2009 evaluate_symbol(sym
);
2011 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2014 expr
->type
= EXPR_PREOP
;
2022 evaluate_expression(target
);
2026 * You can always throw a value away by casting to
2027 * "void" - that's an implicit "force". Note that
2028 * the same is _not_ true of "void *".
2030 if (ctype
== &void_ctype
)
2034 if (type
== SYM_NODE
) {
2035 type
= ctype
->ctype
.base_type
->type
;
2036 if (ctype
->ctype
.base_type
== &void_ctype
)
2039 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2040 warning(expr
->pos
, "cast to non-scalar");
2042 if (!target
->ctype
) {
2043 warning(expr
->pos
, "cast from unknown type");
2047 type
= target
->ctype
->type
;
2048 if (type
== SYM_NODE
)
2049 type
= target
->ctype
->ctype
.base_type
->type
;
2050 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2051 warning(expr
->pos
, "cast from non-scalar");
2053 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2054 warning(expr
->pos
, "cast removes address space of expression");
2056 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2057 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2058 if (t1
->type
== SYM_NODE
)
2059 t1
= t1
->ctype
.base_type
;
2060 if (t2
->type
== SYM_NODE
)
2061 t2
= t2
->ctype
.base_type
;
2063 if (t1
->type
== SYM_RESTRICT
)
2064 warning(expr
->pos
, "cast to restricted type");
2065 if (t2
->type
== SYM_RESTRICT
)
2066 warning(expr
->pos
, "cast from restricted type");
2071 * Casts of constant values are special: they
2072 * can be NULL, and thus need to be simplified
2075 if (target
->type
== EXPR_VALUE
)
2076 cast_value(expr
, ctype
, target
, target
->ctype
);
2083 * Evaluate a call expression with a symbol. This
2084 * should expand inline functions, and evaluate
2087 static int evaluate_symbol_call(struct expression
*expr
)
2089 struct expression
*fn
= expr
->fn
;
2090 struct symbol
*ctype
= fn
->ctype
;
2092 if (fn
->type
!= EXPR_PREOP
)
2095 if (ctype
->op
&& ctype
->op
->evaluate
)
2096 return ctype
->op
->evaluate(expr
);
2098 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2100 struct symbol
*curr
= current_fn
;
2101 unsigned long context
= current_context
;
2102 unsigned long mask
= current_contextmask
;
2104 current_context
|= ctype
->ctype
.context
;
2105 current_contextmask
|= ctype
->ctype
.contextmask
;
2106 current_fn
= ctype
->ctype
.base_type
;
2107 examine_fn_arguments(current_fn
);
2109 ret
= inline_function(expr
, ctype
);
2111 /* restore the old function context */
2113 current_context
= context
;
2114 current_contextmask
= mask
;
2121 static struct symbol
*evaluate_call(struct expression
*expr
)
2124 struct symbol
*ctype
, *sym
;
2125 struct expression
*fn
= expr
->fn
;
2126 struct expression_list
*arglist
= expr
->args
;
2128 if (!evaluate_expression(fn
))
2130 sym
= ctype
= fn
->ctype
;
2131 if (ctype
->type
== SYM_NODE
)
2132 ctype
= ctype
->ctype
.base_type
;
2133 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2134 ctype
= ctype
->ctype
.base_type
;
2135 if (!evaluate_arguments(sym
, ctype
, arglist
))
2137 if (ctype
->type
!= SYM_FN
) {
2138 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2141 args
= expression_list_size(expr
->args
);
2142 fnargs
= symbol_list_size(ctype
->arguments
);
2144 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2145 if (args
> fnargs
&& !ctype
->variadic
)
2146 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2147 if (sym
->type
== SYM_NODE
) {
2148 if (evaluate_symbol_call(expr
))
2151 expr
->ctype
= ctype
->ctype
.base_type
;
2155 struct symbol
*evaluate_expression(struct expression
*expr
)
2162 switch (expr
->type
) {
2165 warning(expr
->pos
, "value expression without a type");
2168 return evaluate_string(expr
);
2170 return evaluate_symbol_expression(expr
);
2172 if (!evaluate_expression(expr
->left
))
2174 if (!evaluate_expression(expr
->right
))
2176 return evaluate_binop(expr
);
2178 return evaluate_logical(expr
);
2180 evaluate_expression(expr
->left
);
2181 if (!evaluate_expression(expr
->right
))
2183 return evaluate_comma(expr
);
2185 if (!evaluate_expression(expr
->left
))
2187 if (!evaluate_expression(expr
->right
))
2189 return evaluate_compare(expr
);
2190 case EXPR_ASSIGNMENT
:
2191 if (!evaluate_expression(expr
->left
))
2193 if (!evaluate_expression(expr
->right
))
2195 return evaluate_assignment(expr
);
2197 if (!evaluate_expression(expr
->unop
))
2199 return evaluate_preop(expr
);
2201 if (!evaluate_expression(expr
->unop
))
2203 return evaluate_postop(expr
);
2205 return evaluate_cast(expr
);
2207 return evaluate_sizeof(expr
);
2209 return evaluate_alignof(expr
);
2211 return evaluate_member_dereference(expr
);
2213 return evaluate_call(expr
);
2215 warning(expr
->pos
, "bitfield generated by parser");
2218 case EXPR_CONDITIONAL
:
2219 if (!evaluate_conditional(&expr
->conditional
))
2221 if (!evaluate_expression(expr
->cond_false
))
2223 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
2225 return evaluate_conditional_expression(expr
);
2226 case EXPR_STATEMENT
:
2227 expr
->ctype
= evaluate_statement(expr
->statement
);
2231 expr
->ctype
= &ptr_ctype
;
2235 /* Evaluate the type of the symbol .. */
2236 evaluate_symbol(expr
->symbol
);
2237 /* .. but the type of the _expression_ is a "type" */
2238 expr
->ctype
= &type_ctype
;
2241 /* These can not exist as stand-alone expressions */
2242 case EXPR_INITIALIZER
:
2243 case EXPR_IDENTIFIER
:
2246 warning(expr
->pos
, "internal front-end error: initializer in expression");
2249 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2255 void check_duplicates(struct symbol
*sym
)
2257 struct symbol
*next
= sym
;
2259 while ((next
= next
->same_symbol
) != NULL
) {
2260 const char *typediff
;
2261 evaluate_symbol(next
);
2262 typediff
= type_difference(sym
, next
, 0, 0);
2264 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2265 show_ident(sym
->ident
),
2266 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2272 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2274 struct symbol
*base_type
;
2279 sym
= examine_symbol_type(sym
);
2280 base_type
= sym
->ctype
.base_type
;
2284 /* Evaluate the initializers */
2285 if (sym
->initializer
) {
2286 int count
= evaluate_initializer(sym
, &sym
->initializer
);
2287 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
2288 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
2289 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
2290 base_type
->bit_size
= bit_size
;
2291 sym
->array_size
= base_type
->array_size
;
2292 sym
->bit_size
= bit_size
;
2296 /* And finally, evaluate the body of the symbol too */
2297 if (base_type
->type
== SYM_FN
) {
2298 struct symbol
*curr
= current_fn
;
2299 unsigned long context
= current_context
;
2300 unsigned long mask
= current_contextmask
;
2302 current_fn
= base_type
;
2303 current_contextmask
= sym
->ctype
.contextmask
;
2304 current_context
= sym
->ctype
.context
;
2306 examine_fn_arguments(base_type
);
2307 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2309 if (base_type
->stmt
)
2310 evaluate_statement(base_type
->stmt
);
2313 current_contextmask
= mask
;
2314 current_context
= context
;
2320 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2322 struct expression
*expr
= stmt
->expression
;
2323 struct symbol
*ctype
, *fntype
;
2325 evaluate_expression(expr
);
2326 ctype
= degenerate(expr
);
2327 fntype
= current_fn
->ctype
.base_type
;
2328 if (!fntype
|| fntype
== &void_ctype
) {
2329 if (expr
&& ctype
!= &void_ctype
)
2330 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2335 warning(stmt
->pos
, "return with no return value");
2340 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2344 static void evaluate_if_statement(struct statement
*stmt
)
2346 if (!stmt
->if_conditional
)
2349 evaluate_conditional(&stmt
->if_conditional
);
2350 evaluate_statement(stmt
->if_true
);
2351 evaluate_statement(stmt
->if_false
);
2354 static void evaluate_iterator(struct statement
*stmt
)
2356 struct expression
**pre
= &stmt
->iterator_pre_condition
;
2357 struct expression
**post
= &stmt
->iterator_post_condition
;
2358 if (*pre
== *post
) {
2359 evaluate_conditional(pre
);
2362 evaluate_conditional(pre
);
2363 evaluate_conditional(post
);
2365 evaluate_statement(stmt
->iterator_pre_statement
);
2366 evaluate_statement(stmt
->iterator_statement
);
2367 evaluate_statement(stmt
->iterator_post_statement
);
2370 struct symbol
*evaluate_statement(struct statement
*stmt
)
2375 switch (stmt
->type
) {
2377 return evaluate_return_expression(stmt
);
2379 case STMT_EXPRESSION
:
2380 if (!evaluate_expression(stmt
->expression
))
2382 return degenerate(stmt
->expression
);
2384 case STMT_COMPOUND
: {
2385 struct statement
*s
;
2386 struct symbol
*type
= NULL
;
2389 /* Evaluate each symbol in the compound statement */
2390 FOR_EACH_PTR(stmt
->syms
, sym
) {
2391 evaluate_symbol(sym
);
2392 } END_FOR_EACH_PTR(sym
);
2393 evaluate_symbol(stmt
->ret
);
2396 * Then, evaluate each statement, making the type of the
2397 * compound statement be the type of the last statement
2400 FOR_EACH_PTR(stmt
->stmts
, s
) {
2401 type
= evaluate_statement(s
);
2402 } END_FOR_EACH_PTR(s
);
2408 evaluate_if_statement(stmt
);
2411 evaluate_iterator(stmt
);
2414 evaluate_expression(stmt
->switch_expression
);
2415 evaluate_statement(stmt
->switch_statement
);
2418 evaluate_expression(stmt
->case_expression
);
2419 evaluate_expression(stmt
->case_to
);
2420 evaluate_statement(stmt
->case_statement
);
2423 return evaluate_statement(stmt
->label_statement
);
2425 evaluate_expression(stmt
->goto_expression
);
2430 /* FIXME! Do the asm parameter evaluation! */