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
;
39 warning(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
43 examine_symbol_type(sym
);
44 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
45 warning(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
47 base_type
= sym
->ctype
.base_type
;
49 warning(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 /* The type of a symbol is the symbol itself! */
56 /* enums can be turned into plain values */
57 if (sym
->type
!= SYM_ENUM
) {
58 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
60 addr
->symbol_name
= expr
->symbol_name
;
61 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
62 expr
->type
= EXPR_PREOP
;
66 } else if (base_type
->bit_size
< bits_in_int
) {
67 /* ugly - we need to force sizeof for these guys */
68 struct expression
*e
= alloc_expression(expr
->pos
, EXPR_VALUE
);
69 e
->value
= sym
->value
;
71 expr
->type
= EXPR_PREOP
;
75 expr
->type
= EXPR_VALUE
;
76 expr
->value
= sym
->value
;
78 expr
->ctype
= base_type
;
82 static struct symbol
*evaluate_string(struct expression
*expr
)
84 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
85 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
86 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
87 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
88 unsigned int length
= expr
->string
->length
;
90 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
91 sym
->bit_size
= bits_in_char
* length
;
92 sym
->ctype
.alignment
= 1;
93 sym
->ctype
.modifiers
= MOD_STATIC
;
94 sym
->ctype
.base_type
= array
;
95 sym
->initializer
= initstr
;
98 initstr
->string
= expr
->string
;
100 array
->array_size
= sym
->array_size
;
101 array
->bit_size
= bits_in_char
* length
;
102 array
->ctype
.alignment
= 1;
103 array
->ctype
.modifiers
= MOD_STATIC
;
104 array
->ctype
.base_type
= &char_ctype
;
107 addr
->ctype
= &lazy_ptr_ctype
;
109 expr
->type
= EXPR_PREOP
;
116 static inline struct symbol
*integer_promotion(struct symbol
*type
)
118 unsigned long mod
= type
->ctype
.modifiers
;
121 if (type
->type
== SYM_ENUM
) {
122 type
= type
->ctype
.base_type
;
123 mod
= type
->ctype
.modifiers
;
125 if (type
->type
== SYM_BITFIELD
) {
126 mod
= type
->ctype
.base_type
->ctype
.modifiers
;
127 width
= type
->fieldwidth
;
128 } else if (mod
& (MOD_CHAR
| MOD_SHORT
))
129 width
= type
->bit_size
;
132 if (mod
& MOD_UNSIGNED
&& width
== bits_in_int
)
138 * integer part of usual arithmetic conversions:
139 * integer promotions are applied
140 * if left and right are identical, we are done
141 * if signedness is the same, convert one with lower rank
142 * unless unsigned argument has rank lower than signed one, convert the
144 * if signed argument is bigger than unsigned one, convert the unsigned.
145 * otherwise, convert signed.
147 * Leaving aside the integer promotions, that is equivalent to
148 * if identical, don't convert
149 * if left is bigger than right, convert right
150 * if right is bigger than left, convert right
151 * otherwise, if signedness is the same, convert one with lower rank
152 * otherwise convert the signed one.
154 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
156 unsigned long lmod
, rmod
;
158 left
= integer_promotion(left
);
159 right
= integer_promotion(right
);
164 if (left
->bit_size
> right
->bit_size
)
167 if (right
->bit_size
> left
->bit_size
)
170 lmod
= left
->ctype
.modifiers
;
171 rmod
= right
->ctype
.modifiers
;
172 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
173 if (lmod
& MOD_UNSIGNED
)
175 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
183 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
185 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
187 expr
->cast_type
= type
;
188 expr
->cast_expression
= old
;
192 static int is_type_type(struct symbol
*type
)
194 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
197 static int is_ptr_type(struct symbol
*type
)
199 if (type
->type
== SYM_NODE
)
200 type
= type
->ctype
.base_type
;
201 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
204 static inline int is_float_type(struct symbol
*type
)
206 if (type
->type
== SYM_NODE
)
207 type
= type
->ctype
.base_type
;
208 return type
->ctype
.base_type
== &fp_type
;
211 static inline int is_byte_type(struct symbol
*type
)
213 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
216 static inline int is_string_type(struct symbol
*type
)
218 if (type
->type
== SYM_NODE
)
219 type
= type
->ctype
.base_type
;
220 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
223 static struct symbol
*bad_expr_type(struct expression
*expr
)
225 warning(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
226 switch (expr
->type
) {
229 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
230 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
234 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
243 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
245 struct expression
*left
= *lp
, *right
= *rp
;
246 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
248 if (ltype
->type
== SYM_NODE
)
249 ltype
= ltype
->ctype
.base_type
;
250 if (rtype
->type
== SYM_NODE
)
251 rtype
= rtype
->ctype
.base_type
;
252 if (is_float_type(ltype
)) {
253 if (is_int_type(rtype
))
255 if (is_float_type(rtype
)) {
256 unsigned long lmod
= ltype
->ctype
.modifiers
;
257 unsigned long rmod
= rtype
->ctype
.modifiers
;
258 lmod
&= MOD_LONG
| MOD_LONGLONG
;
259 rmod
&= MOD_LONG
| MOD_LONGLONG
;
269 if (!is_float_type(rtype
) || !is_int_type(ltype
))
272 *lp
= cast_to(left
, rtype
);
275 *rp
= cast_to(right
, ltype
);
279 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
281 struct expression
*left
= *lp
, *right
= *rp
;
282 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
284 if (ltype
->type
== SYM_NODE
)
285 ltype
= ltype
->ctype
.base_type
;
286 if (rtype
->type
== SYM_NODE
)
287 rtype
= rtype
->ctype
.base_type
;
288 if (is_int_type(ltype
) && is_int_type(rtype
)) {
289 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
291 /* Don't bother promoting same-size entities, it only adds clutter */
292 if (ltype
->bit_size
!= ctype
->bit_size
)
293 *lp
= cast_to(left
, ctype
);
294 if (rtype
->bit_size
!= ctype
->bit_size
)
295 *rp
= cast_to(right
, ctype
);
301 static int restricted_value(struct expression
*v
, struct symbol
*type
)
303 if (v
->type
!= EXPR_VALUE
)
310 static int restricted_binop(int op
, struct symbol
*type
)
318 case SPECIAL_NOTEQUAL
:
325 static int restricted_unop(int op
, struct symbol
*type
)
327 if (op
== '~' && type
->bit_size
>= bits_in_int
)
334 static struct symbol
*compatible_restricted_binop(int op
, struct expression
**lp
, struct expression
**rp
)
336 struct expression
*left
= *lp
, *right
= *rp
;
337 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
338 struct symbol
*type
= NULL
;
340 if (ltype
->type
== SYM_NODE
)
341 ltype
= ltype
->ctype
.base_type
;
342 if (ltype
->type
== SYM_ENUM
)
343 ltype
= ltype
->ctype
.base_type
;
344 if (rtype
->type
== SYM_NODE
)
345 rtype
= rtype
->ctype
.base_type
;
346 if (rtype
->type
== SYM_ENUM
)
347 rtype
= rtype
->ctype
.base_type
;
348 if (is_restricted_type(ltype
)) {
349 if (is_restricted_type(rtype
)) {
353 if (!restricted_value(right
, ltype
))
356 } else if (is_restricted_type(rtype
)) {
357 if (!restricted_value(left
, rtype
))
362 if (restricted_binop(op
, type
))
367 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
369 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
370 if (!ctype
&& float_ok
)
371 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
373 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
378 return bad_expr_type(expr
);
381 static inline int lvalue_expression(struct expression
*expr
)
383 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
386 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
388 struct symbol
*ctype
;
389 struct symbol
*ptr_type
= ptr
->ctype
;
392 if (ptr_type
->type
== SYM_NODE
)
393 ptr_type
= ptr_type
->ctype
.base_type
;
395 if (!is_int_type(i
->ctype
))
396 return bad_expr_type(expr
);
399 examine_symbol_type(ctype
);
401 ctype
= degenerate(ptr
);
402 if (!ctype
->ctype
.base_type
) {
403 warning(expr
->pos
, "missing type information");
407 /* Get the size of whatever the pointer points to */
409 if (ptr_type
->type
== SYM_NODE
)
410 ptr_type
= ptr_type
->ctype
.base_type
;
411 if (ptr_type
->type
== SYM_PTR
)
412 ptr_type
= ptr_type
->ctype
.base_type
;
413 bit_size
= ptr_type
->bit_size
;
415 /* Special case: adding zero commonly happens as a result of 'array[0]' */
416 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
418 } else if (bit_size
> bits_in_char
) {
419 struct expression
*add
= expr
;
420 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
421 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
423 val
->ctype
= size_t_ctype
;
424 val
->value
= bit_size
>> 3;
427 mul
->ctype
= size_t_ctype
;
431 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
440 static struct symbol
*evaluate_add(struct expression
*expr
)
442 struct expression
*left
= expr
->left
, *right
= expr
->right
;
443 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
445 if (is_ptr_type(ltype
))
446 return evaluate_ptr_add(expr
, left
, right
);
448 if (is_ptr_type(rtype
))
449 return evaluate_ptr_add(expr
, right
, left
);
451 return evaluate_arith(expr
, 1);
454 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
455 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
456 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
457 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
459 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
460 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
463 unsigned long mod1
, mod2
, diff
;
464 unsigned long as1
, as2
;
466 struct symbol
*base1
, *base2
;
468 if (target
== source
)
470 if (!target
|| !source
)
471 return "different types";
473 * Peel of per-node information.
474 * FIXME! Check alignment and context too here!
476 mod1
= target
->ctype
.modifiers
;
477 as1
= target
->ctype
.as
;
478 mod2
= source
->ctype
.modifiers
;
479 as2
= source
->ctype
.as
;
480 if (target
->type
== SYM_NODE
) {
481 target
= target
->ctype
.base_type
;
484 if (target
->type
== SYM_PTR
) {
488 mod1
|= target
->ctype
.modifiers
;
489 as1
|= target
->ctype
.as
;
491 if (source
->type
== SYM_NODE
) {
492 source
= source
->ctype
.base_type
;
495 if (source
->type
== SYM_PTR
) {
499 mod2
|= source
->ctype
.modifiers
;
500 as2
|= source
->ctype
.as
;
502 if (target
->type
== SYM_ENUM
) {
503 target
= target
->ctype
.base_type
;
507 if (source
->type
== SYM_ENUM
) {
508 source
= source
->ctype
.base_type
;
513 if (target
== source
)
515 if (!target
|| !source
)
516 return "different types";
518 type1
= target
->type
;
519 base1
= target
->ctype
.base_type
;
521 type2
= source
->type
;
522 base2
= source
->ctype
.base_type
;
525 * Pointers to functions compare as the function itself
527 if (type1
== SYM_PTR
&& base1
) {
528 switch (base1
->type
) {
532 base1
= base1
->ctype
.base_type
;
537 if (type2
== SYM_PTR
&& base2
) {
538 switch (base2
->type
) {
542 base2
= base2
->ctype
.base_type
;
548 /* Arrays degenerate to pointers for type comparisons */
549 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
550 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
552 if (type1
!= type2
|| type1
== SYM_RESTRICT
)
553 return "different base types";
555 /* Must be same address space to be comparable */
557 return "different address spaces";
559 /* Ignore differences in storage types or addressability */
560 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
561 diff
&= (mod1
& ~target_mod_ignore
) | (mod2
& ~source_mod_ignore
);
564 return "different type sizes";
565 if (diff
& ~MOD_SIGNEDNESS
)
566 return "different modifiers";
568 /* Differs in signedness only.. */
571 * Warn if both are explicitly signed ("unsigned" is obvously
572 * always explicit, and since we know one of them has to be
573 * unsigned, we check if the signed one was explicit).
575 if ((mod1
| mod2
) & MOD_EXPLICITLY_SIGNED
)
576 return "different explicit signedness";
579 * "char" matches both "unsigned char" and "signed char",
580 * so if the explicit test didn't trigger, then we should
581 * not warn about a char.
583 if (!(mod1
& MOD_CHAR
))
584 return "different signedness";
588 if (type1
== SYM_FN
) {
590 struct symbol
*arg1
, *arg2
;
591 if (base1
->variadic
!= base2
->variadic
)
592 return "incompatible variadic arguments";
593 PREPARE_PTR_LIST(target
->arguments
, arg1
);
594 PREPARE_PTR_LIST(source
->arguments
, arg2
);
598 diff
= type_difference(arg1
, arg2
, 0, 0);
600 static char argdiff
[80];
601 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
610 FINISH_PTR_LIST(arg2
);
611 FINISH_PTR_LIST(arg1
);
620 static int is_null_ptr(struct expression
*expr
)
622 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
624 if (!is_ptr_type(expr
->ctype
))
625 warning(expr
->pos
, "Using plain integer as NULL pointer");
629 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
631 /* NULL expression? Just return the type of the "other side" */
640 * Ignore differences in "volatile" and "const"ness when
641 * subtracting pointers
643 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
645 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
647 const char *typediff
;
648 struct symbol
*ctype
;
649 struct symbol
*ltype
, *rtype
;
651 ltype
= degenerate(l
);
652 rtype
= degenerate(r
);
655 * If it is an integer subtract: the ptr add case will do the
658 if (!is_ptr_type(rtype
))
659 return evaluate_ptr_add(expr
, l
, r
);
662 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
664 ctype
= common_ptr_type(l
, r
);
666 warning(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
670 examine_symbol_type(ctype
);
672 /* Figure out the base type we point to */
673 if (ctype
->type
== SYM_NODE
)
674 ctype
= ctype
->ctype
.base_type
;
675 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
676 warning(expr
->pos
, "subtraction of functions? Share your drugs");
679 ctype
= ctype
->ctype
.base_type
;
681 expr
->ctype
= ssize_t_ctype
;
682 if (ctype
->bit_size
> bits_in_char
) {
683 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
684 struct expression
*div
= expr
;
685 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
687 val
->ctype
= size_t_ctype
;
688 val
->value
= ctype
->bit_size
>> 3;
691 sub
->ctype
= ssize_t_ctype
;
700 return ssize_t_ctype
;
703 static struct symbol
*evaluate_sub(struct expression
*expr
)
705 struct expression
*left
= expr
->left
, *right
= expr
->right
;
706 struct symbol
*ltype
= left
->ctype
;
708 if (is_ptr_type(ltype
))
709 return evaluate_ptr_sub(expr
, left
, right
);
711 return evaluate_arith(expr
, 1);
714 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
716 static struct symbol
*evaluate_conditional(struct expression
**p
)
718 struct symbol
*ctype
;
719 struct expression
*expr
= *p
;
724 if (expr
->type
== EXPR_ASSIGNMENT
)
725 warning(expr
->pos
, "assignment expression in conditional");
727 ctype
= evaluate_expression(expr
);
729 if (is_safe_type(ctype
))
730 warning(expr
->pos
, "testing a 'safe expression'");
731 if (is_float_type(ctype
)) {
732 struct expression
*comp
;
734 * It's easier to handle here, rather than deal with
735 * FP all over the place. Floating point in boolean
736 * context is rare enough (and very often wrong),
737 * so price of explicit comparison with appropriate
738 * FP zero is not too high. And it simplifies things
741 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
742 comp
->op
= SPECIAL_NOTEQUAL
;
744 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
745 comp
->right
->ctype
= comp
->left
->ctype
;
746 comp
->right
->fvalue
= 0;
747 ctype
= comp
->ctype
= &bool_ctype
;
755 static struct symbol
*evaluate_logical(struct expression
*expr
)
757 if (!evaluate_conditional(&expr
->left
))
759 if (!evaluate_conditional(&expr
->right
))
762 expr
->ctype
= &bool_ctype
;
766 static struct symbol
*evaluate_shift(struct expression
*expr
)
768 struct expression
*left
= expr
->left
, *right
= expr
->right
;
769 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
771 if (ltype
->type
== SYM_NODE
)
772 ltype
= ltype
->ctype
.base_type
;
773 if (rtype
->type
== SYM_NODE
)
774 rtype
= rtype
->ctype
.base_type
;
775 if (is_int_type(ltype
) && is_int_type(rtype
)) {
776 struct symbol
*ctype
= integer_promotion(ltype
);
777 if (ltype
->bit_size
!= ctype
->bit_size
)
778 expr
->left
= cast_to(expr
->left
, ctype
);
780 ctype
= integer_promotion(rtype
);
781 if (rtype
->bit_size
!= ctype
->bit_size
)
782 expr
->right
= cast_to(expr
->right
, ctype
);
785 return bad_expr_type(expr
);
788 static struct symbol
*evaluate_binop(struct expression
*expr
)
791 // addition can take ptr+int, fp and int
793 return evaluate_add(expr
);
795 // subtraction can take ptr-ptr, fp and int
797 return evaluate_sub(expr
);
799 // Arithmetic operations can take fp and int
801 return evaluate_arith(expr
, 1);
803 // shifts do integer promotions, but that's it.
804 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
805 return evaluate_shift(expr
);
807 // The rest are integer operations
808 // '%', '&', '^', '|'
810 return evaluate_arith(expr
, 0);
814 static struct symbol
*evaluate_comma(struct expression
*expr
)
816 expr
->ctype
= expr
->right
->ctype
;
820 static int modify_for_unsigned(int op
)
823 op
= SPECIAL_UNSIGNED_LT
;
825 op
= SPECIAL_UNSIGNED_GT
;
826 else if (op
== SPECIAL_LTE
)
827 op
= SPECIAL_UNSIGNED_LTE
;
828 else if (op
== SPECIAL_GTE
)
829 op
= SPECIAL_UNSIGNED_GTE
;
833 static struct symbol
*evaluate_compare(struct expression
*expr
)
835 struct expression
*left
= expr
->left
, *right
= expr
->right
;
836 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
837 struct symbol
*ctype
;
840 if (is_type_type(ltype
) && is_type_type(rtype
))
843 if (is_safe_type(ltype
) || is_safe_type(rtype
))
844 warning(expr
->pos
, "testing a 'safe expression'");
847 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
848 // FIXME! Check the types for compatibility
852 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
854 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
855 expr
->op
= modify_for_unsigned(expr
->op
);
859 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
863 ctype
= compatible_restricted_binop(expr
->op
, &expr
->left
, &expr
->right
);
870 expr
->ctype
= &bool_ctype
;
875 * FIXME!! This should do casts, array degeneration etc..
877 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
879 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
881 if (ltype
->type
== SYM_NODE
)
882 ltype
= ltype
->ctype
.base_type
;
884 if (rtype
->type
== SYM_NODE
)
885 rtype
= rtype
->ctype
.base_type
;
887 if (ltype
->type
== SYM_PTR
) {
888 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
892 if (rtype
->type
== SYM_PTR
) {
893 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
899 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
901 struct symbol
*ctype
, *ltype
, *rtype
;
902 const char * typediff
;
904 if (!evaluate_conditional(&expr
->conditional
))
906 if (!evaluate_expression(expr
->cond_false
))
908 if (!evaluate_expression(expr
->cond_true
))
911 ctype
= degenerate(expr
->conditional
);
913 ltype
= degenerate(expr
->cond_true
);
914 rtype
= degenerate(expr
->cond_false
);
917 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
921 ctype
= compatible_integer_binop(&expr
->cond_true
, &expr
->cond_false
);
924 ctype
= compatible_ptr_type(expr
->cond_true
, expr
->cond_false
);
927 ctype
= compatible_float_binop(&expr
->cond_true
, &expr
->cond_false
);
930 ctype
= compatible_restricted_binop('?', &expr
->cond_true
, &expr
->cond_false
);
933 warning(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
941 static struct symbol
*evaluate_short_conditional(struct expression
*expr
)
943 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
944 struct expression
*e0
, *e1
, *e2
, *e3
;
945 struct symbol
*ctype
;
947 if (!evaluate_expression(expr
->conditional
))
950 ctype
= degenerate(expr
->conditional
);
952 a
->ctype
.base_type
= ctype
;
953 a
->bit_size
= ctype
->bit_size
;
954 a
->array_size
= ctype
->array_size
;
956 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
958 e0
->ctype
= &lazy_ptr_ctype
;
960 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
965 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
967 e2
->right
= expr
->conditional
;
971 e3
= alloc_expression(expr
->pos
, EXPR_CONDITIONAL
);
972 e3
->conditional
= e1
;
974 e3
->cond_false
= expr
->cond_false
;
975 e3
->ctype
= evaluate_conditional_expression(e3
);
977 expr
->type
= EXPR_COMMA
;
980 return expr
->ctype
= e3
->ctype
;
983 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
984 struct expression
**rp
, struct symbol
*source
, const char *where
)
986 const char *typediff
;
990 /* It's ok if the target is more volatile or const than the source */
991 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
995 if (is_int_type(target
)) {
996 if (is_int_type(source
)) {
997 if (target
->bit_size
!= source
->bit_size
)
1001 if (is_float_type(source
))
1003 } else if (is_float_type(target
)) {
1004 if (is_int_type(source
))
1006 if (is_float_type(source
)) {
1007 if (target
->bit_size
!= source
->bit_size
)
1013 if (is_restricted_type(target
) && !restricted_value(*rp
, target
))
1016 /* Pointer destination? */
1018 target_as
= t
->ctype
.as
;
1019 if (t
->type
== SYM_NODE
) {
1020 t
= t
->ctype
.base_type
;
1021 target_as
|= t
->ctype
.as
;
1023 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
1024 struct expression
*right
= *rp
;
1025 struct symbol
*s
= source
;
1028 // NULL pointer is always ok
1029 if (is_null_ptr(right
))
1032 /* "void *" matches anything as long as the address space is ok */
1033 source_as
= s
->ctype
.as
;
1034 if (s
->type
== SYM_NODE
) {
1035 s
= s
->ctype
.base_type
;
1036 source_as
|= s
->ctype
.as
;
1038 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
1039 s
= s
->ctype
.base_type
;
1040 t
= t
->ctype
.base_type
;
1041 if (s
== &void_ctype
|| t
== &void_ctype
)
1046 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1047 info(expr
->pos
, " expected %s", show_typename(target
));
1048 info(expr
->pos
, " got %s", show_typename(source
));
1049 *rp
= cast_to(*rp
, target
);
1052 *rp
= cast_to(*rp
, target
);
1057 * FIXME!! This is wrong from a double evaluation standpoint. We can't
1058 * just expand the expression twice, that would make any side effects
1061 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
1064 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1065 static const int op_trans
[] = {
1066 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
1067 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
1068 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
1069 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
1070 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
1071 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
1072 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
1073 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
1074 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
1075 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
1077 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
, *e5
;
1078 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1079 struct symbol
*ltype
= left
->ctype
;
1080 struct expression
*addr
;
1081 struct symbol
*lptype
;
1083 if (left
->type
== EXPR_BITFIELD
)
1084 addr
= left
->address
;
1088 lptype
= addr
->ctype
;
1090 a
->ctype
.base_type
= lptype
;
1091 a
->bit_size
= lptype
->bit_size
;
1092 a
->array_size
= lptype
->array_size
;
1094 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1096 e0
->ctype
= &lazy_ptr_ctype
;
1098 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1103 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1109 /* we can't cannibalize left, unfortunately */
1110 e3
= alloc_expression(expr
->pos
, left
->type
);
1112 if (e3
->type
== EXPR_BITFIELD
)
1117 e4
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1118 e4
->op
= subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
1121 /* will calculate type later */
1123 e5
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1124 e5
->left
= e3
; /* we can share that one */
1129 expr
->type
= EXPR_COMMA
;
1132 expr
->ctype
= ltype
;
1134 return evaluate_binop(e4
);
1137 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1139 if (type
->ctype
.modifiers
& MOD_CONST
)
1140 warning(left
->pos
, "assignment to const expression");
1141 if (type
->type
== SYM_NODE
)
1142 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
1145 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1147 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1148 struct expression
*where
= expr
;
1149 struct symbol
*ltype
, *rtype
;
1151 if (!lvalue_expression(left
)) {
1152 warning(expr
->pos
, "not an lvalue");
1156 ltype
= left
->ctype
;
1158 if (expr
->op
!= '=') {
1159 if (!evaluate_binop_assignment(expr
, left
, right
))
1161 where
= expr
->right
; /* expr is EXPR_COMMA now */
1163 right
= where
->right
;
1166 rtype
= degenerate(right
);
1168 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1171 evaluate_assign_to(left
, ltype
);
1173 expr
->ctype
= ltype
;
1177 static void examine_fn_arguments(struct symbol
*fn
)
1181 FOR_EACH_PTR(fn
->arguments
, s
) {
1182 struct symbol
*arg
= evaluate_symbol(s
);
1183 /* Array/function arguments silently degenerate into pointers */
1189 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1190 if (arg
->type
== SYM_ARRAY
)
1191 ptr
->ctype
= arg
->ctype
;
1193 ptr
->ctype
.base_type
= arg
;
1194 ptr
->ctype
.as
|= s
->ctype
.as
;
1195 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1197 s
->ctype
.base_type
= ptr
;
1199 s
->ctype
.modifiers
= 0;
1200 examine_symbol_type(s
);
1207 } END_FOR_EACH_PTR(s
);
1210 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1212 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1213 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1215 newsym
->ctype
.as
= as
;
1216 newsym
->ctype
.modifiers
= mod
;
1222 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1224 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1225 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1227 node
->ctype
.base_type
= ptr
;
1228 ptr
->bit_size
= bits_in_pointer
;
1229 ptr
->ctype
.alignment
= pointer_alignment
;
1231 node
->bit_size
= bits_in_pointer
;
1232 node
->ctype
.alignment
= pointer_alignment
;
1235 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1236 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1237 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1238 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1240 if (sym
->type
== SYM_NODE
) {
1241 ptr
->ctype
.as
|= sym
->ctype
.as
;
1242 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1243 sym
= sym
->ctype
.base_type
;
1245 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1246 ptr
->ctype
.as
|= sym
->ctype
.as
;
1247 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1248 sym
= sym
->ctype
.base_type
;
1250 ptr
->ctype
.base_type
= sym
;
1255 /* Arrays degenerate into pointers on pointer arithmetic */
1256 static struct symbol
*degenerate(struct expression
*expr
)
1258 struct symbol
*ctype
, *base
;
1262 ctype
= expr
->ctype
;
1266 if (ctype
->type
== SYM_NODE
)
1267 base
= ctype
->ctype
.base_type
;
1269 * Arrays degenerate into pointers to the entries, while
1270 * functions degenerate into pointers to themselves.
1271 * If array was part of non-lvalue compound, we create a copy
1272 * of that compound first and then act as if we were dealing with
1273 * the corresponding field in there.
1275 switch (base
->type
) {
1277 if (expr
->type
== EXPR_SLICE
) {
1278 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1279 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1281 a
->ctype
.base_type
= expr
->base
->ctype
;
1282 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1283 a
->array_size
= expr
->base
->ctype
->array_size
;
1285 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1287 e0
->ctype
= &lazy_ptr_ctype
;
1289 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1292 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1294 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1296 e2
->right
= expr
->base
;
1298 e2
->ctype
= expr
->base
->ctype
;
1300 if (expr
->r_bitpos
) {
1301 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1304 e3
->right
= alloc_const_expression(expr
->pos
,
1305 expr
->r_bitpos
>> 3);
1306 e3
->ctype
= &lazy_ptr_ctype
;
1311 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1314 e4
->ctype
= &lazy_ptr_ctype
;
1317 expr
->type
= EXPR_PREOP
;
1321 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1322 warning(expr
->pos
, "strange non-value function or array");
1325 *expr
= *expr
->unop
;
1326 ctype
= create_pointer(expr
, ctype
, 1);
1327 expr
->ctype
= ctype
;
1334 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1336 struct expression
*op
= expr
->unop
;
1337 struct symbol
*ctype
;
1339 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1340 warning(expr
->pos
, "not addressable");
1347 * symbol expression evaluation is lazy about the type
1348 * of the sub-expression, so we may have to generate
1349 * the type here if so..
1351 if (expr
->ctype
== &lazy_ptr_ctype
) {
1352 ctype
= create_pointer(expr
, ctype
, 0);
1353 expr
->ctype
= ctype
;
1359 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1361 struct expression
*op
= expr
->unop
;
1362 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1364 /* Simplify: *&(expr) => (expr) */
1365 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1370 /* Dereferencing a node drops all the node information. */
1371 if (ctype
->type
== SYM_NODE
)
1372 ctype
= ctype
->ctype
.base_type
;
1374 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1375 target
= ctype
->ctype
.base_type
;
1377 switch (ctype
->type
) {
1379 warning(expr
->pos
, "cannot derefence this type");
1382 merge_type(node
, ctype
);
1383 if (ctype
->type
!= SYM_ARRAY
)
1386 * Dereferencing a pointer to an array results in a
1387 * degenerate dereference: the expression becomes
1388 * just a pointer to the entry, and the derefence
1393 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1394 target
->bit_size
= bits_in_pointer
;
1395 target
->ctype
.alignment
= pointer_alignment
;
1396 merge_type(target
, ctype
->ctype
.base_type
);
1400 if (!lvalue_expression(op
)) {
1401 warning(op
->pos
, "non-lvalue array??");
1405 /* Do the implied "addressof" on the array */
1409 * When an array is dereferenced, we need to pick
1410 * up the attributes of the original node too..
1412 merge_type(node
, op
->ctype
);
1413 merge_type(node
, ctype
);
1417 node
->bit_size
= target
->bit_size
;
1418 node
->array_size
= target
->array_size
;
1425 * Unary post-ops: x++ and x--
1427 static struct symbol
*evaluate_postop(struct expression
*expr
)
1429 struct expression
*op
= expr
->unop
;
1430 struct symbol
*ctype
= op
->ctype
;
1432 if (!lvalue_expression(expr
->unop
)) {
1433 warning(expr
->pos
, "need lvalue expression for ++/--");
1436 if (is_restricted_type(ctype
) && restricted_unop(expr
->op
, ctype
)) {
1437 warning(expr
->pos
, "bad operation on restricted");
1441 evaluate_assign_to(op
, ctype
);
1443 expr
->ctype
= ctype
;
1447 static struct symbol
*evaluate_sign(struct expression
*expr
)
1449 struct symbol
*ctype
= expr
->unop
->ctype
;
1450 if (is_int_type(ctype
)) {
1451 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1452 if (rtype
->bit_size
!= ctype
->bit_size
)
1453 expr
->unop
= cast_to(expr
->unop
, rtype
);
1455 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1456 /* no conversions needed */
1457 } else if (is_restricted_type(ctype
) && !restricted_unop(expr
->op
, ctype
)) {
1458 /* no conversions needed */
1460 return bad_expr_type(expr
);
1462 if (expr
->op
== '+')
1463 *expr
= *expr
->unop
;
1464 expr
->ctype
= ctype
;
1468 static struct symbol
*evaluate_preop(struct expression
*expr
)
1470 struct symbol
*ctype
= expr
->unop
->ctype
;
1474 *expr
= *expr
->unop
;
1480 return evaluate_sign(expr
);
1483 return evaluate_dereference(expr
);
1486 return evaluate_addressof(expr
);
1488 case SPECIAL_INCREMENT
:
1489 case SPECIAL_DECREMENT
:
1491 * From a type evaluation standpoint the pre-ops are
1492 * the same as the postops
1494 return evaluate_postop(expr
);
1497 if (is_safe_type(ctype
))
1498 warning(expr
->pos
, "testing a 'safe expression'");
1499 if (is_float_type(ctype
)) {
1500 struct expression
*arg
= expr
->unop
;
1501 expr
->type
= EXPR_BINOP
;
1502 expr
->op
= SPECIAL_EQUAL
;
1504 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1505 expr
->right
->ctype
= ctype
;
1506 expr
->right
->fvalue
= 0;
1508 ctype
= &bool_ctype
;
1514 expr
->ctype
= ctype
;
1518 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1520 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1521 struct ptr_list
*list
= head
;
1527 for (i
= 0; i
< list
->nr
; i
++) {
1528 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1530 if (sym
->ident
!= ident
)
1532 *offset
= sym
->offset
;
1535 struct symbol
*ctype
= sym
->ctype
.base_type
;
1539 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1541 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1544 *offset
+= sym
->offset
;
1548 } while ((list
= list
->next
) != head
);
1552 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1554 struct expression
*add
;
1557 * Create a new add-expression
1559 * NOTE! Even if we just add zero, we need a new node
1560 * for the member pointer, since it has a different
1561 * type than the original pointer. We could make that
1562 * be just a cast, but the fact is, a node is a node,
1563 * so we might as well just do the "add zero" here.
1565 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1568 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1569 add
->right
->ctype
= &int_ctype
;
1570 add
->right
->value
= offset
;
1573 * The ctype of the pointer will be lazily evaluated if
1574 * we ever take the address of this member dereference..
1576 add
->ctype
= &lazy_ptr_ctype
;
1580 /* structure/union dereference */
1581 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1584 struct symbol
*ctype
, *member
;
1585 struct expression
*deref
= expr
->deref
, *add
;
1586 struct ident
*ident
= expr
->member
;
1590 if (!evaluate_expression(deref
))
1593 warning(expr
->pos
, "bad member name");
1597 ctype
= deref
->ctype
;
1598 address_space
= ctype
->ctype
.as
;
1599 mod
= ctype
->ctype
.modifiers
;
1600 if (ctype
->type
== SYM_NODE
) {
1601 ctype
= ctype
->ctype
.base_type
;
1602 address_space
|= ctype
->ctype
.as
;
1603 mod
|= ctype
->ctype
.modifiers
;
1605 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1606 warning(expr
->pos
, "expected structure or union");
1610 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1612 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1613 const char *name
= "<unnamed>";
1616 name
= ctype
->ident
->name
;
1617 namelen
= ctype
->ident
->len
;
1619 warning(expr
->pos
, "no member '%s' in %s %.*s",
1620 show_ident(ident
), type
, namelen
, name
);
1625 * The member needs to take on the address space and modifiers of
1626 * the "parent" type.
1628 member
= convert_to_as_mod(member
, address_space
, mod
);
1629 ctype
= member
->ctype
.base_type
;
1631 if (!lvalue_expression(deref
)) {
1632 if (deref
->type
!= EXPR_SLICE
) {
1636 expr
->base
= deref
->base
;
1637 expr
->r_bitpos
= deref
->r_bitpos
;
1639 expr
->r_bitpos
+= offset
<< 3;
1640 expr
->type
= EXPR_SLICE
;
1641 if (ctype
->type
== SYM_BITFIELD
) {
1642 expr
->r_bitpos
+= member
->bit_offset
;
1643 expr
->r_nrbits
= member
->fieldwidth
;
1645 expr
->r_nrbits
= member
->bit_size
;
1647 expr
->ctype
= member
;
1651 deref
= deref
->unop
;
1652 expr
->deref
= deref
;
1654 add
= evaluate_offset(deref
, offset
);
1655 if (ctype
->type
== SYM_BITFIELD
) {
1656 expr
->type
= EXPR_BITFIELD
;
1657 expr
->bitpos
= member
->bit_offset
;
1658 expr
->nrbits
= member
->fieldwidth
;
1659 expr
->address
= add
;
1661 expr
->type
= EXPR_PREOP
;
1666 expr
->ctype
= member
;
1670 static int is_promoted(struct expression
*expr
)
1673 switch (expr
->type
) {
1676 case EXPR_CONDITIONAL
:
1700 static struct symbol
*evaluate_cast(struct expression
*);
1702 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1704 struct expression
*what
= expr
->cast_expression
;
1707 if (expr
->cast_type
) {
1709 struct symbol
*sym
= evaluate_cast(expr
);
1710 size
= sym
->bit_size
;
1712 examine_symbol_type(expr
->cast_type
);
1713 size
= expr
->cast_type
->bit_size
;
1716 if (!evaluate_expression(what
))
1718 size
= what
->ctype
->bit_size
;
1719 if (is_restricted_type(what
->ctype
)) {
1720 if (size
< bits_in_int
&& is_promoted(what
))
1723 if (is_bitfield_type(what
->ctype
))
1724 warning(expr
->pos
, "sizeof applied to bitfield type");
1727 warning(expr
->pos
, "cannot size expression");
1728 expr
->type
= EXPR_VALUE
;
1729 expr
->value
= size
>> 3;
1730 expr
->ctype
= size_t_ctype
;
1731 return size_t_ctype
;
1734 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1736 struct symbol
*type
= expr
->cast_type
;
1739 type
= evaluate_expression(expr
->cast_expression
);
1743 if (is_bitfield_type(type
))
1744 warning(expr
->pos
, "alignof applied to bitfield type");
1745 examine_symbol_type(type
);
1746 expr
->type
= EXPR_VALUE
;
1747 expr
->value
= type
->ctype
.alignment
;
1748 expr
->ctype
= size_t_ctype
;
1749 return size_t_ctype
;
1752 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1754 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1755 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1759 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1761 struct expression
*expr
;
1762 struct symbol_list
*argument_types
= fn
->arguments
;
1763 struct symbol
*argtype
;
1766 PREPARE_PTR_LIST(argument_types
, argtype
);
1767 FOR_EACH_PTR (head
, expr
) {
1768 struct expression
**p
= THIS_ADDRESS(expr
);
1769 struct symbol
*ctype
, *target
;
1770 ctype
= evaluate_expression(expr
);
1775 if (context_clash(f
, ctype
))
1776 warning(expr
->pos
, "argument %d used in wrong context", i
);
1778 ctype
= degenerate(expr
);
1781 if (!target
&& ctype
->bit_size
< bits_in_int
)
1782 target
= &int_ctype
;
1784 static char where
[30];
1785 examine_symbol_type(target
);
1786 sprintf(where
, "argument %d", i
);
1787 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1791 NEXT_PTR_LIST(argtype
);
1792 } END_FOR_EACH_PTR(expr
);
1793 FINISH_PTR_LIST(argtype
);
1797 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
);
1799 static int evaluate_one_array_initializer(struct symbol
*ctype
, struct expression
**ep
, int current
)
1801 struct expression
*entry
= *ep
;
1802 struct expression
**parent
, *reuse
= NULL
;
1803 unsigned long offset
;
1805 unsigned long from
, to
;
1806 int accept_string
= is_byte_type(ctype
);
1811 if (entry
->type
== EXPR_INDEX
) {
1812 from
= entry
->idx_from
;
1813 to
= entry
->idx_to
+1;
1814 parent
= &entry
->idx_expression
;
1816 entry
= entry
->idx_expression
;
1819 offset
= from
* (ctype
->bit_size
>>3);
1821 if (!reuse
) reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1822 reuse
->type
= EXPR_POS
;
1823 reuse
->ctype
= ctype
;
1824 reuse
->init_offset
= offset
;
1825 reuse
->init_nr
= to
- from
;
1826 reuse
->init_expr
= entry
;
1827 parent
= &reuse
->init_expr
;
1832 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1833 sym
= evaluate_expression(entry
);
1834 to
= from
+ get_expression_value(sym
->array_size
);
1836 evaluate_initializer(ctype
, parent
);
1841 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1843 struct expression
*entry
;
1847 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1848 current
= evaluate_one_array_initializer(ctype
, THIS_ADDRESS(entry
), current
);
1851 } END_FOR_EACH_PTR(entry
);
1855 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1856 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
)
1858 if (expression_list_size(expr
->expr_list
) != 1) {
1859 warning(expr
->pos
, "unexpected compound initializer");
1862 return evaluate_array_initializer(ctype
, expr
);
1865 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
1869 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
1870 if (sym
->ident
== ident
)
1872 } END_FOR_EACH_PTR(sym
);
1876 static int evaluate_one_struct_initializer(struct symbol
*ctype
, struct expression
**ep
, struct symbol
*sym
)
1878 struct expression
*entry
= *ep
;
1879 struct expression
**parent
;
1880 struct expression
*reuse
= NULL
;
1881 unsigned long offset
;
1884 error(entry
->pos
, "unknown named initializer");
1888 if (entry
->type
== EXPR_IDENTIFIER
) {
1890 entry
= entry
->ident_expression
;
1894 offset
= sym
->offset
;
1897 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1898 reuse
->type
= EXPR_POS
;
1899 reuse
->ctype
= ctype
;
1900 reuse
->init_offset
= offset
;
1902 reuse
->init_expr
= entry
;
1903 parent
= &reuse
->init_expr
;
1907 evaluate_initializer(sym
, parent
);
1911 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1913 struct expression
*entry
;
1916 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1917 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1918 if (entry
->type
== EXPR_IDENTIFIER
) {
1919 struct ident
*ident
= entry
->expr_ident
;
1920 /* We special-case the "already right place" case */
1921 if (!sym
|| sym
->ident
!= ident
) {
1922 RESET_PTR_LIST(sym
);
1926 if (sym
->ident
== ident
)
1932 if (evaluate_one_struct_initializer(ctype
, THIS_ADDRESS(entry
), sym
))
1935 } END_FOR_EACH_PTR(entry
);
1936 FINISH_PTR_LIST(sym
);
1942 * Initializers are kind of like assignments. Except
1943 * they can be a hell of a lot more complex.
1945 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1947 struct expression
*expr
= *ep
;
1950 * Simple non-structure/array initializers are the simple
1951 * case, and look (and parse) largely like assignments.
1953 switch (expr
->type
) {
1955 int size
= 0, is_string
= expr
->type
== EXPR_STRING
;
1956 struct symbol
*rtype
= evaluate_expression(expr
);
1960 * char array[] = "string"
1961 * should _not_ degenerate.
1963 if (is_string
&& is_string_type(ctype
)) {
1964 struct expression
*array_size
= ctype
->array_size
;
1966 array_size
= ctype
->array_size
= rtype
->array_size
;
1967 size
= get_expression_value(array_size
);
1969 rtype
= degenerate(expr
);
1972 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1977 case EXPR_INITIALIZER
:
1978 expr
->ctype
= ctype
;
1979 if (ctype
->type
== SYM_NODE
)
1980 ctype
= ctype
->ctype
.base_type
;
1982 switch (ctype
->type
) {
1985 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
);
1987 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1989 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1991 return evaluate_scalar_initializer(ctype
, expr
);
1994 case EXPR_IDENTIFIER
:
1995 if (ctype
->type
== SYM_NODE
)
1996 ctype
= ctype
->ctype
.base_type
;
1997 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
1998 error(expr
->pos
, "expected structure or union for '%s' dereference", show_ident(expr
->expr_ident
));
2002 return evaluate_one_struct_initializer(ctype
, ep
,
2003 find_struct_ident(ctype
, expr
->expr_ident
));
2006 if (ctype
->type
== SYM_NODE
)
2007 ctype
= ctype
->ctype
.base_type
;
2008 if (ctype
->type
!= SYM_ARRAY
) {
2009 error(expr
->pos
, "expected array");
2012 return evaluate_one_array_initializer(ctype
->ctype
.base_type
, ep
, 0);
2016 * An EXPR_POS expression has already been evaluated, and we don't
2017 * need to do anything more
2023 static int get_as(struct symbol
*sym
)
2031 mod
= sym
->ctype
.modifiers
;
2032 if (sym
->type
== SYM_NODE
) {
2033 sym
= sym
->ctype
.base_type
;
2034 as
|= sym
->ctype
.as
;
2035 mod
|= sym
->ctype
.modifiers
;
2039 * At least for now, allow casting to a "unsigned long".
2040 * That's how we do things like pointer arithmetic and
2041 * store pointers to registers.
2043 if (sym
== &ulong_ctype
)
2046 if (sym
&& sym
->type
== SYM_PTR
) {
2047 sym
= sym
->ctype
.base_type
;
2048 as
|= sym
->ctype
.as
;
2049 mod
|= sym
->ctype
.modifiers
;
2051 if (mod
& MOD_FORCE
)
2056 static struct symbol
*evaluate_cast(struct expression
*expr
)
2058 struct expression
*target
= expr
->cast_expression
;
2059 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
2065 expr
->ctype
= ctype
;
2066 expr
->cast_type
= ctype
;
2069 * Special case: a cast can be followed by an
2070 * initializer, in which case we need to pass
2071 * the type value down to that initializer rather
2072 * than trying to evaluate it as an expression
2074 * A more complex case is when the initializer is
2075 * dereferenced as part of a post-fix expression.
2076 * We need to produce an expression that can be dereferenced.
2078 if (target
->type
== EXPR_INITIALIZER
) {
2079 struct symbol
*sym
= expr
->cast_type
;
2080 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2082 sym
->initializer
= expr
->cast_expression
;
2083 evaluate_symbol(sym
);
2085 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2088 expr
->type
= EXPR_PREOP
;
2096 evaluate_expression(target
);
2100 * You can always throw a value away by casting to
2101 * "void" - that's an implicit "force". Note that
2102 * the same is _not_ true of "void *".
2104 if (ctype
== &void_ctype
)
2108 if (type
== SYM_NODE
) {
2109 type
= ctype
->ctype
.base_type
->type
;
2110 if (ctype
->ctype
.base_type
== &void_ctype
)
2113 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2114 warning(expr
->pos
, "cast to non-scalar");
2116 if (!target
->ctype
) {
2117 warning(expr
->pos
, "cast from unknown type");
2121 type
= target
->ctype
->type
;
2122 if (type
== SYM_NODE
)
2123 type
= target
->ctype
->ctype
.base_type
->type
;
2124 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
2125 warning(expr
->pos
, "cast from non-scalar");
2127 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
2128 warning(expr
->pos
, "cast removes address space of expression");
2130 if (!(ctype
->ctype
.modifiers
& MOD_FORCE
)) {
2131 struct symbol
*t1
= ctype
, *t2
= target
->ctype
;
2132 if (t1
->type
== SYM_NODE
)
2133 t1
= t1
->ctype
.base_type
;
2134 if (t2
->type
== SYM_NODE
)
2135 t2
= t2
->ctype
.base_type
;
2137 if (t1
->type
== SYM_RESTRICT
)
2138 warning(expr
->pos
, "cast to restricted type");
2139 if (t2
->type
== SYM_RESTRICT
)
2140 warning(expr
->pos
, "cast from restricted type");
2145 * Casts of constant values are special: they
2146 * can be NULL, and thus need to be simplified
2149 if (target
->type
== EXPR_VALUE
)
2150 cast_value(expr
, ctype
, target
, target
->ctype
);
2157 * Evaluate a call expression with a symbol. This
2158 * should expand inline functions, and evaluate
2161 static int evaluate_symbol_call(struct expression
*expr
)
2163 struct expression
*fn
= expr
->fn
;
2164 struct symbol
*ctype
= fn
->ctype
;
2166 if (fn
->type
!= EXPR_PREOP
)
2169 if (ctype
->op
&& ctype
->op
->evaluate
)
2170 return ctype
->op
->evaluate(expr
);
2172 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2174 struct symbol
*curr
= current_fn
;
2175 unsigned long context
= current_context
;
2176 unsigned long mask
= current_contextmask
;
2178 current_context
|= ctype
->ctype
.context
;
2179 current_contextmask
|= ctype
->ctype
.contextmask
;
2180 current_fn
= ctype
->ctype
.base_type
;
2181 examine_fn_arguments(current_fn
);
2183 ret
= inline_function(expr
, ctype
);
2185 /* restore the old function context */
2187 current_context
= context
;
2188 current_contextmask
= mask
;
2195 static struct symbol
*evaluate_call(struct expression
*expr
)
2198 struct symbol
*ctype
, *sym
;
2199 struct expression
*fn
= expr
->fn
;
2200 struct expression_list
*arglist
= expr
->args
;
2202 if (!evaluate_expression(fn
))
2204 sym
= ctype
= fn
->ctype
;
2205 if (ctype
->type
== SYM_NODE
)
2206 ctype
= ctype
->ctype
.base_type
;
2207 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
2208 ctype
= ctype
->ctype
.base_type
;
2209 if (!evaluate_arguments(sym
, ctype
, arglist
))
2211 if (ctype
->type
!= SYM_FN
) {
2212 warning(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
2215 args
= expression_list_size(expr
->args
);
2216 fnargs
= symbol_list_size(ctype
->arguments
);
2218 warning(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
2219 if (args
> fnargs
&& !ctype
->variadic
)
2220 warning(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
2221 if (sym
->type
== SYM_NODE
) {
2222 if (evaluate_symbol_call(expr
))
2225 expr
->ctype
= ctype
->ctype
.base_type
;
2229 struct symbol
*evaluate_expression(struct expression
*expr
)
2236 switch (expr
->type
) {
2239 warning(expr
->pos
, "value expression without a type");
2242 return evaluate_string(expr
);
2244 return evaluate_symbol_expression(expr
);
2246 if (!evaluate_expression(expr
->left
))
2248 if (!evaluate_expression(expr
->right
))
2250 return evaluate_binop(expr
);
2252 return evaluate_logical(expr
);
2254 evaluate_expression(expr
->left
);
2255 if (!evaluate_expression(expr
->right
))
2257 return evaluate_comma(expr
);
2259 if (!evaluate_expression(expr
->left
))
2261 if (!evaluate_expression(expr
->right
))
2263 return evaluate_compare(expr
);
2264 case EXPR_ASSIGNMENT
:
2265 if (!evaluate_expression(expr
->left
))
2267 if (!evaluate_expression(expr
->right
))
2269 return evaluate_assignment(expr
);
2271 if (!evaluate_expression(expr
->unop
))
2273 return evaluate_preop(expr
);
2275 if (!evaluate_expression(expr
->unop
))
2277 return evaluate_postop(expr
);
2279 return evaluate_cast(expr
);
2281 return evaluate_sizeof(expr
);
2283 return evaluate_alignof(expr
);
2285 return evaluate_member_dereference(expr
);
2287 return evaluate_call(expr
);
2289 warning(expr
->pos
, "bitfield generated by parser");
2292 case EXPR_CONDITIONAL
:
2293 if (expr
->cond_true
)
2294 return evaluate_conditional_expression(expr
);
2296 return evaluate_short_conditional(expr
);
2297 case EXPR_STATEMENT
:
2298 expr
->ctype
= evaluate_statement(expr
->statement
);
2302 expr
->ctype
= &ptr_ctype
;
2306 /* Evaluate the type of the symbol .. */
2307 evaluate_symbol(expr
->symbol
);
2308 /* .. but the type of the _expression_ is a "type" */
2309 expr
->ctype
= &type_ctype
;
2312 /* These can not exist as stand-alone expressions */
2313 case EXPR_INITIALIZER
:
2314 case EXPR_IDENTIFIER
:
2317 warning(expr
->pos
, "internal front-end error: initializer in expression");
2320 warning(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2326 void check_duplicates(struct symbol
*sym
)
2328 struct symbol
*next
= sym
;
2330 while ((next
= next
->same_symbol
) != NULL
) {
2331 const char *typediff
;
2332 evaluate_symbol(next
);
2333 typediff
= type_difference(sym
, next
, 0, 0);
2335 warning(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2336 show_ident(sym
->ident
),
2337 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2343 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2345 struct symbol
*base_type
;
2350 sym
= examine_symbol_type(sym
);
2351 base_type
= sym
->ctype
.base_type
;
2355 /* Evaluate the initializers */
2356 if (sym
->initializer
) {
2357 int count
= evaluate_initializer(sym
, &sym
->initializer
);
2358 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
2359 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
2360 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
2361 base_type
->bit_size
= bit_size
;
2362 sym
->array_size
= base_type
->array_size
;
2363 sym
->bit_size
= bit_size
;
2367 /* And finally, evaluate the body of the symbol too */
2368 if (base_type
->type
== SYM_FN
) {
2369 struct symbol
*curr
= current_fn
;
2370 unsigned long context
= current_context
;
2371 unsigned long mask
= current_contextmask
;
2373 current_fn
= base_type
;
2374 current_contextmask
= sym
->ctype
.contextmask
;
2375 current_context
= sym
->ctype
.context
;
2377 examine_fn_arguments(base_type
);
2378 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2380 if (base_type
->stmt
)
2381 evaluate_statement(base_type
->stmt
);
2384 current_contextmask
= mask
;
2385 current_context
= context
;
2391 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2393 struct expression
*expr
= stmt
->expression
;
2394 struct symbol
*ctype
, *fntype
;
2396 evaluate_expression(expr
);
2397 ctype
= degenerate(expr
);
2398 fntype
= current_fn
->ctype
.base_type
;
2399 if (!fntype
|| fntype
== &void_ctype
) {
2400 if (expr
&& ctype
!= &void_ctype
)
2401 warning(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2406 warning(stmt
->pos
, "return with no return value");
2411 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2415 static void evaluate_if_statement(struct statement
*stmt
)
2417 if (!stmt
->if_conditional
)
2420 evaluate_conditional(&stmt
->if_conditional
);
2421 evaluate_statement(stmt
->if_true
);
2422 evaluate_statement(stmt
->if_false
);
2425 static void evaluate_iterator(struct statement
*stmt
)
2427 struct expression
**pre
= &stmt
->iterator_pre_condition
;
2428 struct expression
**post
= &stmt
->iterator_post_condition
;
2429 if (*pre
== *post
) {
2430 evaluate_conditional(pre
);
2433 evaluate_conditional(pre
);
2434 evaluate_conditional(post
);
2436 evaluate_statement(stmt
->iterator_pre_statement
);
2437 evaluate_statement(stmt
->iterator_statement
);
2438 evaluate_statement(stmt
->iterator_post_statement
);
2441 struct symbol
*evaluate_statement(struct statement
*stmt
)
2446 switch (stmt
->type
) {
2448 return evaluate_return_expression(stmt
);
2450 case STMT_EXPRESSION
:
2451 if (!evaluate_expression(stmt
->expression
))
2453 return degenerate(stmt
->expression
);
2455 case STMT_COMPOUND
: {
2456 struct statement
*s
;
2457 struct symbol
*type
= NULL
;
2460 /* Evaluate each symbol in the compound statement */
2461 FOR_EACH_PTR(stmt
->syms
, sym
) {
2462 evaluate_symbol(sym
);
2463 } END_FOR_EACH_PTR(sym
);
2464 evaluate_symbol(stmt
->ret
);
2467 * Then, evaluate each statement, making the type of the
2468 * compound statement be the type of the last statement
2471 FOR_EACH_PTR(stmt
->stmts
, s
) {
2472 type
= evaluate_statement(s
);
2473 } END_FOR_EACH_PTR(s
);
2479 evaluate_if_statement(stmt
);
2482 evaluate_iterator(stmt
);
2485 evaluate_expression(stmt
->switch_expression
);
2486 evaluate_statement(stmt
->switch_statement
);
2489 evaluate_expression(stmt
->case_expression
);
2490 evaluate_expression(stmt
->case_to
);
2491 evaluate_statement(stmt
->case_statement
);
2494 return evaluate_statement(stmt
->label_statement
);
2496 evaluate_expression(stmt
->goto_expression
);
2501 /* FIXME! Do the asm parameter evaluation! */