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 warn(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 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
51 base_type
= sym
->ctype
.base_type
;
53 warn(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 warn(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 struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
296 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
297 if (!ctype
&& float_ok
)
298 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
303 return bad_expr_type(expr
);
306 static inline int lvalue_expression(struct expression
*expr
)
308 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
311 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
313 struct symbol
*ctype
;
314 struct symbol
*ptr_type
= ptr
->ctype
;
317 if (ptr_type
->type
== SYM_NODE
)
318 ptr_type
= ptr_type
->ctype
.base_type
;
320 if (!is_int_type(i
->ctype
))
321 return bad_expr_type(expr
);
324 examine_symbol_type(ctype
);
326 ctype
= degenerate(ptr
);
327 if (!ctype
->ctype
.base_type
) {
328 warn(expr
->pos
, "missing type information");
332 /* Get the size of whatever the pointer points to */
334 if (ptr_type
->type
== SYM_NODE
)
335 ptr_type
= ptr_type
->ctype
.base_type
;
336 if (ptr_type
->type
== SYM_PTR
)
337 ptr_type
= ptr_type
->ctype
.base_type
;
338 bit_size
= ptr_type
->bit_size
;
340 /* Special case: adding zero commonly happens as a result of 'array[0]' */
341 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
343 } else if (bit_size
> bits_in_char
) {
344 struct expression
*add
= expr
;
345 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
346 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
348 val
->ctype
= size_t_ctype
;
349 val
->value
= bit_size
>> 3;
352 mul
->ctype
= size_t_ctype
;
356 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
365 static struct symbol
*evaluate_add(struct expression
*expr
)
367 struct expression
*left
= expr
->left
, *right
= expr
->right
;
368 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
370 if (is_ptr_type(ltype
))
371 return evaluate_ptr_add(expr
, left
, right
);
373 if (is_ptr_type(rtype
))
374 return evaluate_ptr_add(expr
, right
, left
);
376 return evaluate_arith(expr
, 1);
379 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
380 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED)
382 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
383 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
386 unsigned long mod1
, mod2
, diff
;
387 unsigned long as1
, as2
;
389 struct symbol
*base1
, *base2
;
391 if (target
== source
)
393 if (!target
|| !source
)
394 return "different types";
396 * Peel of per-node information.
397 * FIXME! Check alignment and context too here!
399 mod1
= target
->ctype
.modifiers
;
400 as1
= target
->ctype
.as
;
401 mod2
= source
->ctype
.modifiers
;
402 as2
= source
->ctype
.as
;
403 if (target
->type
== SYM_NODE
) {
404 target
= target
->ctype
.base_type
;
407 if (target
->type
== SYM_PTR
) {
411 mod1
|= target
->ctype
.modifiers
;
412 as1
|= target
->ctype
.as
;
414 if (source
->type
== SYM_NODE
) {
415 source
= source
->ctype
.base_type
;
418 if (source
->type
== SYM_PTR
) {
422 mod2
|= source
->ctype
.modifiers
;
423 as2
|= source
->ctype
.as
;
426 if (target
== source
)
428 if (!target
|| !source
)
429 return "different types";
431 type1
= target
->type
;
432 base1
= target
->ctype
.base_type
;
434 type2
= source
->type
;
435 base2
= source
->ctype
.base_type
;
438 * Pointers to functions compare as the function itself
440 if (type1
== SYM_PTR
&& base1
) {
441 switch (base1
->type
) {
445 base1
= base1
->ctype
.base_type
;
450 if (type2
== SYM_PTR
&& base2
) {
451 switch (base2
->type
) {
455 base2
= base2
->ctype
.base_type
;
461 /* Arrays degenerate to pointers for type comparisons */
462 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
463 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
466 return "different base types";
468 /* Must be same address space to be comparable */
470 return "different address spaces";
472 /* Ignore differences in storage types, sign, or addressability */
473 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
475 mod1
&= diff
& ~target_mod_ignore
;
476 mod2
&= diff
& ~source_mod_ignore
;
478 if ((mod1
| mod2
) & MOD_SIZE
)
479 return "different type sizes";
480 return "different modifiers";
484 if (type1
== SYM_FN
) {
486 struct symbol
*arg1
, *arg2
;
487 if (base1
->variadic
!= base2
->variadic
)
488 return "incompatible variadic arguments";
489 PREPARE_PTR_LIST(target
->arguments
, arg1
);
490 PREPARE_PTR_LIST(source
->arguments
, arg2
);
494 diff
= type_difference(arg1
, arg2
, 0, 0);
496 static char argdiff
[80];
497 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
506 FINISH_PTR_LIST(arg2
);
507 FINISH_PTR_LIST(arg1
);
516 static int is_null_ptr(struct expression
*expr
)
518 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
520 if (!is_ptr_type(expr
->ctype
))
521 warn(expr
->pos
, "Using plain integer as NULL pointer");
525 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
527 /* NULL expression? Just return the type of the "other side" */
536 * Ignore differences in "volatile" and "const"ness when
537 * subtracting pointers
539 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
541 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
543 const char *typediff
;
544 struct symbol
*ctype
;
545 struct symbol
*ltype
, *rtype
;
547 ltype
= degenerate(l
);
548 rtype
= degenerate(r
);
551 * If it is an integer subtract: the ptr add case will do the
554 if (!is_ptr_type(rtype
))
555 return evaluate_ptr_add(expr
, l
, r
);
558 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
560 ctype
= common_ptr_type(l
, r
);
562 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
566 examine_symbol_type(ctype
);
568 /* Figure out the base type we point to */
569 if (ctype
->type
== SYM_NODE
)
570 ctype
= ctype
->ctype
.base_type
;
571 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
572 warn(expr
->pos
, "subtraction of functions? Share your drugs");
575 ctype
= ctype
->ctype
.base_type
;
577 expr
->ctype
= ssize_t_ctype
;
578 if (ctype
->bit_size
> bits_in_char
) {
579 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
580 struct expression
*div
= expr
;
581 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
583 val
->ctype
= size_t_ctype
;
584 val
->value
= ctype
->bit_size
>> 3;
587 sub
->ctype
= ssize_t_ctype
;
596 return ssize_t_ctype
;
599 static struct symbol
*evaluate_sub(struct expression
*expr
)
601 struct expression
*left
= expr
->left
, *right
= expr
->right
;
602 struct symbol
*ltype
= left
->ctype
;
604 if (is_ptr_type(ltype
))
605 return evaluate_ptr_sub(expr
, left
, right
);
607 return evaluate_arith(expr
, 1);
610 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
612 static struct symbol
*evaluate_conditional(struct expression
**p
)
614 struct symbol
*ctype
;
615 struct expression
*expr
= *p
;
620 if (expr
->type
== EXPR_ASSIGNMENT
)
621 warn(expr
->pos
, "assignment expression in conditional");
623 ctype
= evaluate_expression(expr
);
625 if (is_safe_type(ctype
))
626 warn(expr
->pos
, "testing a 'safe expression'");
627 if (is_float_type(ctype
)) {
628 struct expression
*comp
;
630 * It's easier to handle here, rather than deal with
631 * FP all over the place. Floating point in boolean
632 * context is rare enough (and very often wrong),
633 * so price of explicit comparison with appropriate
634 * FP zero is not too high. And it simplifies things
637 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
638 comp
->op
= SPECIAL_NOTEQUAL
;
640 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
641 comp
->right
->ctype
= comp
->left
->ctype
;
642 comp
->right
->fvalue
= 0;
643 ctype
= comp
->ctype
= &bool_ctype
;
651 static struct symbol
*evaluate_logical(struct expression
*expr
)
653 if (!evaluate_conditional(&expr
->left
))
655 if (!evaluate_conditional(&expr
->right
))
658 expr
->ctype
= &bool_ctype
;
662 static struct symbol
*evaluate_shift(struct expression
*expr
)
664 struct expression
*left
= expr
->left
, *right
= expr
->right
;
665 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
667 if (ltype
->type
== SYM_NODE
)
668 ltype
= ltype
->ctype
.base_type
;
669 if (rtype
->type
== SYM_NODE
)
670 rtype
= rtype
->ctype
.base_type
;
671 if (is_int_type(ltype
) && is_int_type(rtype
)) {
672 struct symbol
*ctype
= integer_promotion(ltype
);
673 if (ltype
->bit_size
!= ctype
->bit_size
)
674 expr
->left
= cast_to(expr
->left
, ctype
);
676 ctype
= integer_promotion(rtype
);
677 if (rtype
->bit_size
!= ctype
->bit_size
)
678 expr
->right
= cast_to(expr
->right
, ctype
);
681 return bad_expr_type(expr
);
684 static struct symbol
*evaluate_binop(struct expression
*expr
)
687 // addition can take ptr+int, fp and int
689 return evaluate_add(expr
);
691 // subtraction can take ptr-ptr, fp and int
693 return evaluate_sub(expr
);
695 // Arithmetic operations can take fp and int
697 return evaluate_arith(expr
, 1);
699 // shifts do integer promotions, but that's it.
700 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
701 return evaluate_shift(expr
);
703 // The rest are integer operations
704 // '%', '&', '^', '|'
706 return evaluate_arith(expr
, 0);
710 static struct symbol
*evaluate_comma(struct expression
*expr
)
712 expr
->ctype
= expr
->right
->ctype
;
716 static int modify_for_unsigned(int op
)
719 op
= SPECIAL_UNSIGNED_LT
;
721 op
= SPECIAL_UNSIGNED_GT
;
722 else if (op
== SPECIAL_LTE
)
723 op
= SPECIAL_UNSIGNED_LTE
;
724 else if (op
== SPECIAL_GTE
)
725 op
= SPECIAL_UNSIGNED_GTE
;
729 static struct symbol
*evaluate_compare(struct expression
*expr
)
731 struct expression
*left
= expr
->left
, *right
= expr
->right
;
732 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
733 struct symbol
*ctype
;
736 if (is_type_type(ltype
) && is_type_type(rtype
)) {
737 expr
->ctype
= &bool_ctype
;
741 if (is_safe_type(ltype
) || is_safe_type(rtype
))
742 warn(expr
->pos
, "testing a 'safe expression'");
745 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
746 expr
->ctype
= &bool_ctype
;
747 // FIXME! Check the types for compatibility
751 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
753 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
754 expr
->op
= modify_for_unsigned(expr
->op
);
755 expr
->ctype
= &bool_ctype
;
758 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
760 expr
->ctype
= &bool_ctype
;
764 return bad_expr_type(expr
);
768 * FIXME!! This should do casts, array degeneration etc..
770 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
772 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
774 if (ltype
->type
== SYM_NODE
)
775 ltype
= ltype
->ctype
.base_type
;
777 if (rtype
->type
== SYM_NODE
)
778 rtype
= rtype
->ctype
.base_type
;
780 if (ltype
->type
== SYM_PTR
) {
781 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
785 if (rtype
->type
== SYM_PTR
) {
786 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
792 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
794 struct expression
*cond
, *true, *false;
795 struct symbol
*ctype
, *ltype
, *rtype
;
796 const char * typediff
;
798 ctype
= degenerate(expr
->conditional
);
799 cond
= expr
->conditional
;
803 if (expr
->cond_true
) {
804 ltype
= degenerate(expr
->cond_true
);
805 true = expr
->cond_true
;
808 rtype
= degenerate(expr
->cond_false
);
809 false = expr
->cond_false
;
812 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
816 ctype
= compatible_integer_binop(&true, &expr
->cond_false
);
819 ctype
= compatible_ptr_type(true, expr
->cond_false
);
822 ctype
= compatible_float_binop(&true, &expr
->cond_false
);
825 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
833 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
834 struct expression
**rp
, struct symbol
*source
, const char *where
)
836 const char *typediff
;
840 /* It's ok if the target is more volatile or const than the source */
841 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
845 if (is_int_type(target
)) {
846 if (is_int_type(source
)) {
847 if (target
->bit_size
!= source
->bit_size
)
851 if (is_float_type(source
))
853 } else if (is_float_type(target
)) {
854 if (is_int_type(source
))
856 if (is_float_type(source
)) {
857 if (target
->bit_size
!= source
->bit_size
)
863 /* Pointer destination? */
865 target_as
= t
->ctype
.as
;
866 if (t
->type
== SYM_NODE
) {
867 t
= t
->ctype
.base_type
;
868 target_as
|= t
->ctype
.as
;
870 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
871 struct expression
*right
= *rp
;
872 struct symbol
*s
= source
;
875 // NULL pointer is always ok
876 if (is_null_ptr(right
))
879 /* "void *" matches anything as long as the address space is ok */
880 source_as
= s
->ctype
.as
;
881 if (s
->type
== SYM_NODE
) {
882 s
= s
->ctype
.base_type
;
883 source_as
|= s
->ctype
.as
;
885 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
886 s
= s
->ctype
.base_type
;
887 t
= t
->ctype
.base_type
;
888 if (s
== &void_ctype
|| t
== &void_ctype
)
893 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
894 info(expr
->pos
, " expected %s", show_typename(target
));
895 info(expr
->pos
, " got %s", show_typename(source
));
896 *rp
= cast_to(*rp
, target
);
899 *rp
= cast_to(*rp
, target
);
904 * FIXME!! This is wrong from a double evaluation standpoint. We can't
905 * just expand the expression twice, that would make any side effects
908 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
911 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
912 static const int op_trans
[] = {
913 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
914 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
915 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
916 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
917 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
918 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
919 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
920 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
921 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
922 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
924 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
, *e5
;
925 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
926 struct symbol
*ltype
= left
->ctype
;
927 struct expression
*addr
;
928 struct symbol
*lptype
;
930 if (left
->type
== EXPR_BITFIELD
)
931 addr
= left
->address
;
935 lptype
= addr
->ctype
;
937 a
->ctype
.base_type
= lptype
;
938 a
->bit_size
= lptype
->bit_size
;
939 a
->array_size
= lptype
->array_size
;
941 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
943 e0
->ctype
= &lazy_ptr_ctype
;
945 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
950 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
956 /* we can't cannibalize left, unfortunately */
957 e3
= alloc_expression(expr
->pos
, left
->type
);
959 if (e3
->type
== EXPR_BITFIELD
)
964 e4
= alloc_expression(expr
->pos
, EXPR_BINOP
);
965 e4
->op
= subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
968 /* will calculate type later */
970 e5
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
971 e5
->left
= e3
; /* we can share that one */
976 expr
->type
= EXPR_COMMA
;
981 return evaluate_binop(e4
);
984 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
986 if (type
->ctype
.modifiers
& MOD_CONST
)
987 warn(left
->pos
, "assignment to const expression");
988 if (type
->type
== SYM_NODE
)
989 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
992 static struct symbol
*evaluate_assignment(struct expression
*expr
)
994 struct expression
*left
= expr
->left
, *right
= expr
->right
;
995 struct expression
*where
= expr
;
996 struct symbol
*ltype
, *rtype
;
998 if (!lvalue_expression(left
)) {
999 warn(expr
->pos
, "not an lvalue");
1003 ltype
= left
->ctype
;
1005 if (expr
->op
!= '=') {
1006 if (!evaluate_binop_assignment(expr
, left
, right
))
1008 where
= expr
->right
; /* expr is EXPR_COMMA now */
1010 right
= where
->right
;
1013 rtype
= degenerate(right
);
1015 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1018 evaluate_assign_to(left
, ltype
);
1020 expr
->ctype
= ltype
;
1024 static void examine_fn_arguments(struct symbol
*fn
)
1028 FOR_EACH_PTR(fn
->arguments
, s
) {
1029 struct symbol
*arg
= evaluate_symbol(s
);
1030 /* Array/function arguments silently degenerate into pointers */
1036 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1037 if (arg
->type
== SYM_ARRAY
)
1038 ptr
->ctype
= arg
->ctype
;
1040 ptr
->ctype
.base_type
= arg
;
1041 ptr
->ctype
.as
|= s
->ctype
.as
;
1042 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1044 s
->ctype
.base_type
= ptr
;
1046 s
->ctype
.modifiers
= 0;
1047 examine_symbol_type(s
);
1057 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1059 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1060 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1062 newsym
->ctype
.as
= as
;
1063 newsym
->ctype
.modifiers
= mod
;
1069 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1071 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1072 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1074 node
->ctype
.base_type
= ptr
;
1075 ptr
->bit_size
= bits_in_pointer
;
1076 ptr
->ctype
.alignment
= pointer_alignment
;
1078 node
->bit_size
= bits_in_pointer
;
1079 node
->ctype
.alignment
= pointer_alignment
;
1082 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1083 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1084 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1085 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1087 if (sym
->type
== SYM_NODE
) {
1088 ptr
->ctype
.as
|= sym
->ctype
.as
;
1089 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1090 sym
= sym
->ctype
.base_type
;
1092 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1093 ptr
->ctype
.as
|= sym
->ctype
.as
;
1094 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1095 sym
= sym
->ctype
.base_type
;
1097 ptr
->ctype
.base_type
= sym
;
1102 /* Arrays degenerate into pointers on pointer arithmetic */
1103 static struct symbol
*degenerate(struct expression
*expr
)
1105 struct symbol
*ctype
, *base
;
1109 ctype
= expr
->ctype
;
1113 if (ctype
->type
== SYM_NODE
)
1114 base
= ctype
->ctype
.base_type
;
1116 * Arrays degenerate into pointers to the entries, while
1117 * functions degenerate into pointers to themselves.
1118 * If array was part of non-lvalue compound, we create a copy
1119 * of that compound first and then act as if we were dealing with
1120 * the corresponding field in there.
1122 switch (base
->type
) {
1124 if (expr
->type
== EXPR_SLICE
) {
1125 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1126 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1128 a
->ctype
.base_type
= expr
->base
->ctype
;
1129 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1130 a
->array_size
= expr
->base
->ctype
->array_size
;
1132 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1134 e0
->ctype
= &lazy_ptr_ctype
;
1136 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1139 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1141 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1143 e2
->right
= expr
->base
;
1145 e2
->ctype
= expr
->base
->ctype
;
1147 if (expr
->r_bitpos
) {
1148 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1151 e3
->right
= alloc_const_expression(expr
->pos
,
1152 expr
->r_bitpos
>> 3);
1153 e3
->ctype
= &lazy_ptr_ctype
;
1158 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1161 e4
->ctype
= &lazy_ptr_ctype
;
1164 expr
->type
= EXPR_PREOP
;
1168 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1169 warn(expr
->pos
, "strange non-value function or array");
1172 *expr
= *expr
->unop
;
1173 ctype
= create_pointer(expr
, ctype
, 1);
1174 expr
->ctype
= ctype
;
1181 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1183 struct expression
*op
= expr
->unop
;
1184 struct symbol
*ctype
;
1186 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1187 warn(expr
->pos
, "not addressable");
1194 * symbol expression evaluation is lazy about the type
1195 * of the sub-expression, so we may have to generate
1196 * the type here if so..
1198 if (expr
->ctype
== &lazy_ptr_ctype
) {
1199 ctype
= create_pointer(expr
, ctype
, 0);
1200 expr
->ctype
= ctype
;
1206 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1208 struct expression
*op
= expr
->unop
;
1209 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1211 /* Simplify: *&(expr) => (expr) */
1212 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1217 /* Dereferencing a node drops all the node information. */
1218 if (ctype
->type
== SYM_NODE
)
1219 ctype
= ctype
->ctype
.base_type
;
1221 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1222 target
= ctype
->ctype
.base_type
;
1224 switch (ctype
->type
) {
1226 warn(expr
->pos
, "cannot derefence this type");
1229 merge_type(node
, ctype
);
1230 if (ctype
->type
!= SYM_ARRAY
)
1233 * Dereferencing a pointer to an array results in a
1234 * degenerate dereference: the expression becomes
1235 * just a pointer to the entry, and the derefence
1240 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1241 target
->bit_size
= bits_in_pointer
;
1242 target
->ctype
.alignment
= pointer_alignment
;
1243 merge_type(target
, ctype
->ctype
.base_type
);
1248 * When an array is dereferenced, we need to pick
1249 * up the attributes of the original node too..
1251 merge_type(node
, op
->ctype
);
1252 merge_type(node
, ctype
);
1256 node
->bit_size
= target
->bit_size
;
1257 node
->array_size
= target
->array_size
;
1264 * Unary post-ops: x++ and x--
1266 static struct symbol
*evaluate_postop(struct expression
*expr
)
1268 struct expression
*op
= expr
->unop
;
1269 struct symbol
*ctype
= op
->ctype
;
1271 if (!lvalue_expression(expr
->unop
)) {
1272 warn(expr
->pos
, "need lvalue expression for ++/--");
1276 evaluate_assign_to(op
, ctype
);
1278 expr
->ctype
= ctype
;
1282 static struct symbol
*evaluate_sign(struct expression
*expr
)
1284 struct symbol
*ctype
= expr
->unop
->ctype
;
1285 if (is_int_type(ctype
)) {
1286 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1287 if (rtype
->bit_size
!= ctype
->bit_size
)
1288 expr
->unop
= cast_to(expr
->unop
, rtype
);
1290 } else if (is_float_type(ctype
) && expr
->op
!= '%') {
1291 /* no conversions needed */
1293 return bad_expr_type(expr
);
1295 if (expr
->op
== '+')
1296 *expr
= *expr
->unop
;
1297 expr
->ctype
= ctype
;
1301 static struct symbol
*evaluate_preop(struct expression
*expr
)
1303 struct symbol
*ctype
= expr
->unop
->ctype
;
1307 *expr
= *expr
->unop
;
1313 return evaluate_sign(expr
);
1316 return evaluate_dereference(expr
);
1319 return evaluate_addressof(expr
);
1321 case SPECIAL_INCREMENT
:
1322 case SPECIAL_DECREMENT
:
1324 * From a type evaluation standpoint the pre-ops are
1325 * the same as the postops
1327 return evaluate_postop(expr
);
1330 if (is_safe_type(ctype
))
1331 warn(expr
->pos
, "testing a 'safe expression'");
1332 if (is_float_type(ctype
)) {
1333 struct expression
*arg
= expr
->unop
;
1334 expr
->type
= EXPR_BINOP
;
1335 expr
->op
= SPECIAL_EQUAL
;
1337 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1338 expr
->right
->ctype
= ctype
;
1339 expr
->right
->fvalue
= 0;
1341 ctype
= &bool_ctype
;
1347 expr
->ctype
= ctype
;
1351 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1353 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1354 struct ptr_list
*list
= head
;
1360 for (i
= 0; i
< list
->nr
; i
++) {
1361 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1363 if (sym
->ident
!= ident
)
1365 *offset
= sym
->offset
;
1368 struct symbol
*ctype
= sym
->ctype
.base_type
;
1372 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1374 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1377 *offset
+= sym
->offset
;
1381 } while ((list
= list
->next
) != head
);
1385 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1387 struct expression
*add
;
1390 * Create a new add-expression
1392 * NOTE! Even if we just add zero, we need a new node
1393 * for the member pointer, since it has a different
1394 * type than the original pointer. We could make that
1395 * be just a cast, but the fact is, a node is a node,
1396 * so we might as well just do the "add zero" here.
1398 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1401 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1402 add
->right
->ctype
= &int_ctype
;
1403 add
->right
->value
= offset
;
1406 * The ctype of the pointer will be lazily evaluated if
1407 * we ever take the address of this member dereference..
1409 add
->ctype
= &lazy_ptr_ctype
;
1413 /* structure/union dereference */
1414 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1417 struct symbol
*ctype
, *member
;
1418 struct expression
*deref
= expr
->deref
, *add
;
1419 struct ident
*ident
= expr
->member
;
1423 if (!evaluate_expression(deref
))
1426 warn(expr
->pos
, "bad member name");
1430 ctype
= deref
->ctype
;
1431 address_space
= ctype
->ctype
.as
;
1432 mod
= ctype
->ctype
.modifiers
;
1433 if (ctype
->type
== SYM_NODE
) {
1434 ctype
= ctype
->ctype
.base_type
;
1435 address_space
|= ctype
->ctype
.as
;
1436 mod
|= ctype
->ctype
.modifiers
;
1438 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1439 warn(expr
->pos
, "expected structure or union");
1443 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1445 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1446 const char *name
= "<unnamed>";
1449 name
= ctype
->ident
->name
;
1450 namelen
= ctype
->ident
->len
;
1452 warn(expr
->pos
, "no member '%s' in %s %.*s",
1453 show_ident(ident
), type
, namelen
, name
);
1458 * The member needs to take on the address space and modifiers of
1459 * the "parent" type.
1461 member
= convert_to_as_mod(member
, address_space
, mod
);
1462 ctype
= member
->ctype
.base_type
;
1464 if (!lvalue_expression(deref
)) {
1465 if (deref
->type
!= EXPR_SLICE
) {
1469 expr
->base
= deref
->base
;
1470 expr
->r_bitpos
= deref
->r_bitpos
;
1472 expr
->r_bitpos
+= offset
<< 3;
1473 expr
->type
= EXPR_SLICE
;
1474 if (ctype
->type
== SYM_BITFIELD
) {
1475 expr
->r_bitpos
+= member
->bit_offset
;
1476 expr
->r_nrbits
= member
->fieldwidth
;
1478 expr
->r_nrbits
= member
->bit_size
;
1480 expr
->ctype
= member
;
1484 deref
= deref
->unop
;
1485 expr
->deref
= deref
;
1487 add
= evaluate_offset(deref
, offset
);
1488 if (ctype
->type
== SYM_BITFIELD
) {
1489 expr
->type
= EXPR_BITFIELD
;
1490 expr
->bitpos
= member
->bit_offset
;
1491 expr
->nrbits
= member
->fieldwidth
;
1492 expr
->address
= add
;
1494 expr
->type
= EXPR_PREOP
;
1499 expr
->ctype
= member
;
1503 static struct symbol
*evaluate_cast(struct expression
*);
1505 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1509 if (expr
->cast_type
) {
1510 if (expr
->cast_expression
) {
1511 struct symbol
*sym
= evaluate_cast(expr
);
1512 size
= sym
->bit_size
;
1514 examine_symbol_type(expr
->cast_type
);
1515 size
= expr
->cast_type
->bit_size
;
1518 if (!evaluate_expression(expr
->cast_expression
))
1520 size
= expr
->cast_expression
->ctype
->bit_size
;
1523 warn(expr
->pos
, "cannot size expression");
1524 expr
->type
= EXPR_VALUE
;
1525 expr
->value
= size
>> 3;
1526 expr
->ctype
= size_t_ctype
;
1527 return size_t_ctype
;
1530 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1532 struct symbol
*type
= expr
->cast_type
;
1535 type
= evaluate_expression(expr
->cast_expression
);
1539 examine_symbol_type(type
);
1540 expr
->type
= EXPR_VALUE
;
1541 expr
->value
= type
->ctype
.alignment
;
1542 expr
->ctype
= size_t_ctype
;
1543 return size_t_ctype
;
1546 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1548 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1549 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1553 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1555 struct expression
*expr
;
1556 struct symbol_list
*argument_types
= fn
->arguments
;
1557 struct symbol
*argtype
;
1560 PREPARE_PTR_LIST(argument_types
, argtype
);
1561 FOR_EACH_PTR (head
, expr
) {
1562 struct expression
**p
= THIS_ADDRESS(expr
);
1563 struct symbol
*ctype
, *target
;
1564 ctype
= evaluate_expression(expr
);
1569 if (context_clash(f
, ctype
))
1570 warn(expr
->pos
, "argument %d used in wrong context", i
);
1572 ctype
= degenerate(expr
);
1575 if (!target
&& ctype
->bit_size
< bits_in_int
)
1576 target
= &int_ctype
;
1578 static char where
[30];
1579 examine_symbol_type(target
);
1580 sprintf(where
, "argument %d", i
);
1581 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1585 NEXT_PTR_LIST(argtype
);
1587 FINISH_PTR_LIST(argtype
);
1591 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1592 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1594 struct expression
*entry
;
1597 int accept_string
= is_byte_type(ctype
);
1599 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1600 struct expression
**p
= THIS_ADDRESS(entry
);
1604 if (entry
->type
== EXPR_INDEX
) {
1605 current
= entry
->idx_to
;
1608 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1609 sym
= evaluate_expression(entry
);
1610 entries
= get_expression_value(sym
->array_size
);
1612 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1622 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1623 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1625 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1626 warn(expr
->pos
, "unexpected compound initializer");
1629 return evaluate_array_initializer(ctype
, expr
, 0);
1632 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1634 struct expression
*entry
;
1637 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1638 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1639 struct expression
**p
= THIS_ADDRESS(entry
);
1641 if (entry
->type
== EXPR_IDENTIFIER
) {
1642 struct ident
*ident
= entry
->expr_ident
;
1643 /* We special-case the "already right place" case */
1644 if (sym
&& sym
->ident
== ident
)
1646 RESET_PTR_LIST(sym
);
1649 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1652 if (sym
->ident
== ident
)
1660 warn(expr
->pos
, "too many initializers for struct/union");
1664 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1668 FINISH_PTR_LIST(sym
);
1674 * Initializers are kind of like assignments. Except
1675 * they can be a hell of a lot more complex.
1677 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1679 struct expression
*expr
= *ep
;
1682 * Simple non-structure/array initializers are the simple
1683 * case, and look (and parse) largely like assignments.
1685 if (expr
->type
!= EXPR_INITIALIZER
) {
1686 int size
= 0, is_string
= expr
->type
== EXPR_STRING
;
1687 struct symbol
*rtype
= evaluate_expression(expr
);
1689 struct expression
*pos
;
1693 * char array[] = "string"
1694 * should _not_ degenerate.
1696 if (is_string
&& is_string_type(ctype
)) {
1697 struct expression
*array_size
= ctype
->array_size
;
1699 array_size
= ctype
->array_size
= rtype
->array_size
;
1700 size
= get_expression_value(array_size
);
1702 rtype
= degenerate(expr
);
1705 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1708 * Don't bother creating a position expression for
1709 * the simple initializer cases that don't need it.
1711 * We need a position if the initializer has a byte
1712 * offset, _or_ if we're initializing a bitfield.
1714 if (offset
|| ctype
->fieldwidth
) {
1715 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1716 pos
->init_offset
= offset
;
1717 pos
->init_sym
= ctype
;
1718 pos
->init_expr
= *ep
;
1719 pos
->ctype
= expr
->ctype
;
1726 expr
->ctype
= ctype
;
1727 if (ctype
->type
== SYM_NODE
)
1728 ctype
= ctype
->ctype
.base_type
;
1730 switch (ctype
->type
) {
1733 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1735 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1737 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1739 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1743 static int get_as(struct symbol
*sym
)
1751 mod
= sym
->ctype
.modifiers
;
1752 if (sym
->type
== SYM_NODE
) {
1753 sym
= sym
->ctype
.base_type
;
1754 as
|= sym
->ctype
.as
;
1755 mod
|= sym
->ctype
.modifiers
;
1759 * At least for now, allow casting to a "unsigned long".
1760 * That's how we do things like pointer arithmetic and
1761 * store pointers to registers.
1763 if (sym
== &ulong_ctype
)
1766 if (sym
&& sym
->type
== SYM_PTR
) {
1767 sym
= sym
->ctype
.base_type
;
1768 as
|= sym
->ctype
.as
;
1769 mod
|= sym
->ctype
.modifiers
;
1771 if (mod
& MOD_FORCE
)
1776 static struct symbol
*evaluate_cast(struct expression
*expr
)
1778 struct expression
*target
= expr
->cast_expression
;
1779 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1782 expr
->ctype
= ctype
;
1783 expr
->cast_type
= ctype
;
1786 * Special case: a cast can be followed by an
1787 * initializer, in which case we need to pass
1788 * the type value down to that initializer rather
1789 * than trying to evaluate it as an expression
1791 * A more complex case is when the initializer is
1792 * dereferenced as part of a post-fix expression.
1793 * We need to produce an expression that can be dereferenced.
1795 if (target
->type
== EXPR_INITIALIZER
) {
1796 struct symbol
*sym
= expr
->cast_type
;
1797 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1799 sym
->initializer
= expr
->cast_expression
;
1800 evaluate_symbol(sym
);
1802 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
1805 expr
->type
= EXPR_PREOP
;
1813 evaluate_expression(target
);
1817 * You can always throw a value away by casting to
1818 * "void" - that's an implicit "force". Note that
1819 * the same is _not_ true of "void *".
1821 if (ctype
== &void_ctype
)
1825 if (type
== SYM_NODE
) {
1826 type
= ctype
->ctype
.base_type
->type
;
1827 if (ctype
->ctype
.base_type
== &void_ctype
)
1830 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
1831 warn(expr
->pos
, "cast to non-scalar");
1833 if (!target
->ctype
) {
1834 warn(expr
->pos
, "cast from unknown type");
1838 type
= target
->ctype
->type
;
1839 if (type
== SYM_NODE
)
1840 type
= target
->ctype
->ctype
.base_type
->type
;
1841 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
1842 warn(expr
->pos
, "cast from non-scalar");
1844 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1845 warn(expr
->pos
, "cast removes address space of expression");
1848 * Casts of constant values are special: they
1849 * can be NULL, and thus need to be simplified
1852 if (target
->type
== EXPR_VALUE
)
1853 cast_value(expr
, ctype
, target
, target
->ctype
);
1860 * Evaluate a call expression with a symbol. This
1861 * should expand inline functions, and evaluate
1864 static int evaluate_symbol_call(struct expression
*expr
)
1866 struct expression
*fn
= expr
->fn
;
1867 struct symbol
*ctype
= fn
->ctype
;
1869 if (fn
->type
!= EXPR_PREOP
)
1872 if (ctype
->op
&& ctype
->op
->evaluate
)
1873 return ctype
->op
->evaluate(expr
);
1875 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1877 struct symbol
*curr
= current_fn
;
1878 unsigned long context
= current_context
;
1879 unsigned long mask
= current_contextmask
;
1881 current_context
|= ctype
->ctype
.context
;
1882 current_contextmask
|= ctype
->ctype
.contextmask
;
1883 current_fn
= ctype
->ctype
.base_type
;
1884 examine_fn_arguments(current_fn
);
1886 ret
= inline_function(expr
, ctype
);
1888 /* restore the old function context */
1890 current_context
= context
;
1891 current_contextmask
= mask
;
1898 static struct symbol
*evaluate_call(struct expression
*expr
)
1901 struct symbol
*ctype
, *sym
;
1902 struct expression
*fn
= expr
->fn
;
1903 struct expression_list
*arglist
= expr
->args
;
1905 if (!evaluate_expression(fn
))
1907 sym
= ctype
= fn
->ctype
;
1908 if (ctype
->type
== SYM_NODE
)
1909 ctype
= ctype
->ctype
.base_type
;
1910 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1911 ctype
= ctype
->ctype
.base_type
;
1912 if (!evaluate_arguments(sym
, ctype
, arglist
))
1914 if (ctype
->type
!= SYM_FN
) {
1915 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1918 args
= expression_list_size(expr
->args
);
1919 fnargs
= symbol_list_size(ctype
->arguments
);
1921 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1922 if (args
> fnargs
&& !ctype
->variadic
)
1923 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1924 if (sym
->type
== SYM_NODE
) {
1925 if (evaluate_symbol_call(expr
))
1928 expr
->ctype
= ctype
->ctype
.base_type
;
1932 struct symbol
*evaluate_expression(struct expression
*expr
)
1939 switch (expr
->type
) {
1942 warn(expr
->pos
, "value expression without a type");
1945 return evaluate_string(expr
);
1947 return evaluate_symbol_expression(expr
);
1949 if (!evaluate_expression(expr
->left
))
1951 if (!evaluate_expression(expr
->right
))
1953 return evaluate_binop(expr
);
1955 return evaluate_logical(expr
);
1957 if (!evaluate_expression(expr
->left
))
1959 if (!evaluate_expression(expr
->right
))
1961 return evaluate_comma(expr
);
1963 if (!evaluate_expression(expr
->left
))
1965 if (!evaluate_expression(expr
->right
))
1967 return evaluate_compare(expr
);
1968 case EXPR_ASSIGNMENT
:
1969 if (!evaluate_expression(expr
->left
))
1971 if (!evaluate_expression(expr
->right
))
1973 return evaluate_assignment(expr
);
1975 if (!evaluate_expression(expr
->unop
))
1977 return evaluate_preop(expr
);
1979 if (!evaluate_expression(expr
->unop
))
1981 return evaluate_postop(expr
);
1983 return evaluate_cast(expr
);
1985 return evaluate_sizeof(expr
);
1987 return evaluate_alignof(expr
);
1989 return evaluate_member_dereference(expr
);
1991 return evaluate_call(expr
);
1993 warn(expr
->pos
, "bitfield generated by parser");
1996 case EXPR_CONDITIONAL
:
1997 if (!evaluate_conditional(&expr
->conditional
))
1999 if (!evaluate_expression(expr
->cond_false
))
2001 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
2003 return evaluate_conditional_expression(expr
);
2004 case EXPR_STATEMENT
:
2005 expr
->ctype
= evaluate_statement(expr
->statement
);
2009 expr
->ctype
= &ptr_ctype
;
2013 /* Evaluate the type of the symbol .. */
2014 evaluate_symbol(expr
->symbol
);
2015 /* .. but the type of the _expression_ is a "type" */
2016 expr
->ctype
= &type_ctype
;
2019 /* These can not exist as stand-alone expressions */
2020 case EXPR_INITIALIZER
:
2021 case EXPR_IDENTIFIER
:
2024 warn(expr
->pos
, "internal front-end error: initializer in expression");
2027 warn(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2033 void check_duplicates(struct symbol
*sym
)
2035 struct symbol
*next
= sym
;
2037 while ((next
= next
->same_symbol
) != NULL
) {
2038 const char *typediff
;
2039 evaluate_symbol(next
);
2040 typediff
= type_difference(sym
, next
, 0, 0);
2042 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2043 show_ident(sym
->ident
),
2044 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2050 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2052 struct symbol
*base_type
;
2057 sym
= examine_symbol_type(sym
);
2058 base_type
= sym
->ctype
.base_type
;
2062 /* Evaluate the initializers */
2063 if (sym
->initializer
) {
2064 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
2065 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
2066 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
2067 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
2068 base_type
->bit_size
= bit_size
;
2069 sym
->array_size
= base_type
->array_size
;
2070 sym
->bit_size
= bit_size
;
2074 /* And finally, evaluate the body of the symbol too */
2075 if (base_type
->type
== SYM_FN
) {
2076 examine_fn_arguments(base_type
);
2077 if (base_type
->stmt
) {
2078 current_fn
= base_type
;
2079 current_contextmask
= sym
->ctype
.contextmask
;
2080 current_context
= sym
->ctype
.context
;
2081 evaluate_statement(base_type
->stmt
);
2088 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2090 struct expression
*expr
= stmt
->expression
;
2091 struct symbol
*ctype
, *fntype
;
2093 evaluate_expression(expr
);
2094 ctype
= degenerate(expr
);
2095 fntype
= current_fn
->ctype
.base_type
;
2096 if (!fntype
|| fntype
== &void_ctype
) {
2097 if (expr
&& ctype
!= &void_ctype
)
2098 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2103 warn(stmt
->pos
, "return with no return value");
2108 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2112 static void evaluate_if_statement(struct statement
*stmt
)
2114 struct symbol
*ctype
;
2116 if (!stmt
->if_conditional
)
2119 evaluate_conditional(&stmt
->if_conditional
);
2120 evaluate_statement(stmt
->if_true
);
2121 evaluate_statement(stmt
->if_false
);
2124 static void evaluate_iterator(struct statement
*stmt
)
2126 struct expression
**pre
= &stmt
->iterator_pre_condition
;
2127 struct expression
**post
= &stmt
->iterator_post_condition
;
2128 if (*pre
== *post
) {
2129 evaluate_conditional(pre
);
2132 evaluate_conditional(pre
);
2133 evaluate_conditional(post
);
2135 evaluate_statement(stmt
->iterator_pre_statement
);
2136 evaluate_statement(stmt
->iterator_statement
);
2137 evaluate_statement(stmt
->iterator_post_statement
);
2140 struct symbol
*evaluate_statement(struct statement
*stmt
)
2145 switch (stmt
->type
) {
2147 return evaluate_return_expression(stmt
);
2149 case STMT_EXPRESSION
:
2150 if (!evaluate_expression(stmt
->expression
))
2152 return degenerate(stmt
->expression
);
2154 case STMT_COMPOUND
: {
2155 struct statement
*s
;
2156 struct symbol
*type
= NULL
;
2159 /* Evaluate each symbol in the compound statement */
2160 FOR_EACH_PTR(stmt
->syms
, sym
) {
2161 evaluate_symbol(sym
);
2163 evaluate_symbol(stmt
->ret
);
2166 * Then, evaluate each statement, making the type of the
2167 * compound statement be the type of the last statement
2170 FOR_EACH_PTR(stmt
->stmts
, s
) {
2171 type
= evaluate_statement(s
);
2178 evaluate_if_statement(stmt
);
2181 evaluate_iterator(stmt
);
2184 evaluate_expression(stmt
->switch_expression
);
2185 evaluate_statement(stmt
->switch_statement
);
2188 evaluate_expression(stmt
->case_expression
);
2189 evaluate_expression(stmt
->case_to
);
2190 evaluate_statement(stmt
->case_statement
);
2193 return evaluate_statement(stmt
->label_statement
);
2195 evaluate_expression(stmt
->goto_expression
);
2200 /* FIXME! Do the asm parameter evaluation! */