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 | \
381 MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED | \
382 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED)
384 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
385 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
388 unsigned long mod1
, mod2
, diff
;
389 unsigned long as1
, as2
;
391 struct symbol
*base1
, *base2
;
393 if (target
== source
)
395 if (!target
|| !source
)
396 return "different types";
398 * Peel of per-node information.
399 * FIXME! Check alignment and context too here!
401 mod1
= target
->ctype
.modifiers
;
402 as1
= target
->ctype
.as
;
403 mod2
= source
->ctype
.modifiers
;
404 as2
= source
->ctype
.as
;
405 if (target
->type
== SYM_NODE
) {
406 target
= target
->ctype
.base_type
;
409 if (target
->type
== SYM_PTR
) {
413 mod1
|= target
->ctype
.modifiers
;
414 as1
|= target
->ctype
.as
;
416 if (source
->type
== SYM_NODE
) {
417 source
= source
->ctype
.base_type
;
420 if (source
->type
== SYM_PTR
) {
424 mod2
|= source
->ctype
.modifiers
;
425 as2
|= source
->ctype
.as
;
428 if (target
== source
)
430 if (!target
|| !source
)
431 return "different types";
433 type1
= target
->type
;
434 base1
= target
->ctype
.base_type
;
436 type2
= source
->type
;
437 base2
= source
->ctype
.base_type
;
440 * Pointers to functions compare as the function itself
442 if (type1
== SYM_PTR
&& base1
) {
443 switch (base1
->type
) {
447 base1
= base1
->ctype
.base_type
;
452 if (type2
== SYM_PTR
&& base2
) {
453 switch (base2
->type
) {
457 base2
= base2
->ctype
.base_type
;
463 /* Arrays degenerate to pointers for type comparisons */
464 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
465 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
468 return "different base types";
470 /* Must be same address space to be comparable */
472 return "different address spaces";
474 /* Ignore differences in storage types, sign, or addressability */
475 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
477 mod1
&= diff
& ~target_mod_ignore
;
478 mod2
&= diff
& ~source_mod_ignore
;
480 if ((mod1
| mod2
) & MOD_SIZE
)
481 return "different type sizes";
482 return "different modifiers";
486 if (type1
== SYM_FN
) {
488 struct symbol
*arg1
, *arg2
;
489 if (base1
->variadic
!= base2
->variadic
)
490 return "incompatible variadic arguments";
491 PREPARE_PTR_LIST(target
->arguments
, arg1
);
492 PREPARE_PTR_LIST(source
->arguments
, arg2
);
496 diff
= type_difference(arg1
, arg2
, 0, 0);
498 static char argdiff
[80];
499 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
508 FINISH_PTR_LIST(arg2
);
509 FINISH_PTR_LIST(arg1
);
518 static int is_null_ptr(struct expression
*expr
)
520 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
522 if (!is_ptr_type(expr
->ctype
))
523 warn(expr
->pos
, "Using plain integer as NULL pointer");
527 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
529 /* NULL expression? Just return the type of the "other side" */
538 * Ignore differences in "volatile" and "const"ness when
539 * subtracting pointers
541 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
543 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
545 const char *typediff
;
546 struct symbol
*ctype
;
547 struct symbol
*ltype
, *rtype
;
549 ltype
= degenerate(l
);
550 rtype
= degenerate(r
);
553 * If it is an integer subtract: the ptr add case will do the
556 if (!is_ptr_type(rtype
))
557 return evaluate_ptr_add(expr
, l
, r
);
560 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
562 ctype
= common_ptr_type(l
, r
);
564 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
568 examine_symbol_type(ctype
);
570 /* Figure out the base type we point to */
571 if (ctype
->type
== SYM_NODE
)
572 ctype
= ctype
->ctype
.base_type
;
573 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
574 warn(expr
->pos
, "subtraction of functions? Share your drugs");
577 ctype
= ctype
->ctype
.base_type
;
579 expr
->ctype
= ssize_t_ctype
;
580 if (ctype
->bit_size
> bits_in_char
) {
581 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
582 struct expression
*div
= expr
;
583 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
585 val
->ctype
= size_t_ctype
;
586 val
->value
= ctype
->bit_size
>> 3;
589 sub
->ctype
= ssize_t_ctype
;
598 return ssize_t_ctype
;
601 static struct symbol
*evaluate_sub(struct expression
*expr
)
603 struct expression
*left
= expr
->left
, *right
= expr
->right
;
604 struct symbol
*ltype
= left
->ctype
;
606 if (is_ptr_type(ltype
))
607 return evaluate_ptr_sub(expr
, left
, right
);
609 return evaluate_arith(expr
, 1);
612 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
614 static struct symbol
*evaluate_conditional(struct expression
**p
)
616 struct symbol
*ctype
;
617 struct expression
*expr
= *p
;
622 if (expr
->type
== EXPR_ASSIGNMENT
)
623 warn(expr
->pos
, "assignment expression in conditional");
625 ctype
= evaluate_expression(expr
);
627 if (is_safe_type(ctype
))
628 warn(expr
->pos
, "testing a 'safe expression'");
629 if (is_float_type(ctype
)) {
630 struct expression
*comp
;
632 * It's easier to handle here, rather than deal with
633 * FP all over the place. Floating point in boolean
634 * context is rare enough (and very often wrong),
635 * so price of explicit comparison with appropriate
636 * FP zero is not too high. And it simplifies things
639 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
640 comp
->op
= SPECIAL_NOTEQUAL
;
642 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
643 comp
->right
->ctype
= comp
->left
->ctype
;
644 comp
->right
->fvalue
= 0;
645 ctype
= comp
->ctype
= &bool_ctype
;
653 static struct symbol
*evaluate_logical(struct expression
*expr
)
655 if (!evaluate_conditional(&expr
->left
))
657 if (!evaluate_conditional(&expr
->right
))
660 expr
->ctype
= &bool_ctype
;
664 static struct symbol
*evaluate_shift(struct expression
*expr
)
666 struct expression
*left
= expr
->left
, *right
= expr
->right
;
667 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
669 if (ltype
->type
== SYM_NODE
)
670 ltype
= ltype
->ctype
.base_type
;
671 if (rtype
->type
== SYM_NODE
)
672 rtype
= rtype
->ctype
.base_type
;
673 if (is_int_type(ltype
) && is_int_type(rtype
)) {
674 struct symbol
*ctype
= integer_promotion(ltype
);
675 if (ltype
->bit_size
!= ctype
->bit_size
)
676 expr
->left
= cast_to(expr
->left
, ctype
);
678 ctype
= integer_promotion(rtype
);
679 if (rtype
->bit_size
!= ctype
->bit_size
)
680 expr
->right
= cast_to(expr
->right
, ctype
);
683 return bad_expr_type(expr
);
686 static struct symbol
*evaluate_binop(struct expression
*expr
)
689 // addition can take ptr+int, fp and int
691 return evaluate_add(expr
);
693 // subtraction can take ptr-ptr, fp and int
695 return evaluate_sub(expr
);
697 // Arithmetic operations can take fp and int
699 return evaluate_arith(expr
, 1);
701 // shifts do integer promotions, but that's it.
702 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
703 return evaluate_shift(expr
);
705 // The rest are integer operations
706 // '%', '&', '^', '|'
708 return evaluate_arith(expr
, 0);
712 static struct symbol
*evaluate_comma(struct expression
*expr
)
714 expr
->ctype
= expr
->right
->ctype
;
718 static int modify_for_unsigned(int op
)
721 op
= SPECIAL_UNSIGNED_LT
;
723 op
= SPECIAL_UNSIGNED_GT
;
724 else if (op
== SPECIAL_LTE
)
725 op
= SPECIAL_UNSIGNED_LTE
;
726 else if (op
== SPECIAL_GTE
)
727 op
= SPECIAL_UNSIGNED_GTE
;
731 static struct symbol
*evaluate_compare(struct expression
*expr
)
733 struct expression
*left
= expr
->left
, *right
= expr
->right
;
734 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
735 struct symbol
*ctype
;
738 if (is_type_type(ltype
) && is_type_type(rtype
)) {
739 expr
->ctype
= &bool_ctype
;
743 if (is_safe_type(ltype
) || is_safe_type(rtype
))
744 warn(expr
->pos
, "testing a 'safe expression'");
747 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
748 expr
->ctype
= &bool_ctype
;
749 // FIXME! Check the types for compatibility
753 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
755 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
756 expr
->op
= modify_for_unsigned(expr
->op
);
757 expr
->ctype
= &bool_ctype
;
760 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
762 expr
->ctype
= &bool_ctype
;
766 return bad_expr_type(expr
);
770 * FIXME!! This should do casts, array degeneration etc..
772 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
774 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
776 if (ltype
->type
== SYM_NODE
)
777 ltype
= ltype
->ctype
.base_type
;
779 if (rtype
->type
== SYM_NODE
)
780 rtype
= rtype
->ctype
.base_type
;
782 if (ltype
->type
== SYM_PTR
) {
783 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
787 if (rtype
->type
== SYM_PTR
) {
788 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
794 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
796 struct expression
*cond
, *true, *false;
797 struct symbol
*ctype
, *ltype
, *rtype
;
798 const char * typediff
;
800 ctype
= degenerate(expr
->conditional
);
801 cond
= expr
->conditional
;
805 if (expr
->cond_true
) {
806 ltype
= degenerate(expr
->cond_true
);
807 true = expr
->cond_true
;
810 rtype
= degenerate(expr
->cond_false
);
811 false = expr
->cond_false
;
814 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
818 ctype
= compatible_integer_binop(&true, &expr
->cond_false
);
821 ctype
= compatible_ptr_type(true, expr
->cond_false
);
824 ctype
= compatible_float_binop(&true, &expr
->cond_false
);
827 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
835 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
836 struct expression
**rp
, struct symbol
*source
, const char *where
)
838 const char *typediff
;
842 /* It's ok if the target is more volatile or const than the source */
843 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
847 if (is_int_type(target
)) {
848 if (is_int_type(source
)) {
849 if (target
->bit_size
!= source
->bit_size
)
853 if (is_float_type(source
))
855 } else if (is_float_type(target
)) {
856 if (is_int_type(source
))
858 if (is_float_type(source
)) {
859 if (target
->bit_size
!= source
->bit_size
)
865 /* Pointer destination? */
867 target_as
= t
->ctype
.as
;
868 if (t
->type
== SYM_NODE
) {
869 t
= t
->ctype
.base_type
;
870 target_as
|= t
->ctype
.as
;
872 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
873 struct expression
*right
= *rp
;
874 struct symbol
*s
= source
;
877 // NULL pointer is always ok
878 if (is_null_ptr(right
))
881 /* "void *" matches anything as long as the address space is ok */
882 source_as
= s
->ctype
.as
;
883 if (s
->type
== SYM_NODE
) {
884 s
= s
->ctype
.base_type
;
885 source_as
|= s
->ctype
.as
;
887 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
888 s
= s
->ctype
.base_type
;
889 t
= t
->ctype
.base_type
;
890 if (s
== &void_ctype
|| t
== &void_ctype
)
895 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
896 info(expr
->pos
, " expected %s", show_typename(target
));
897 info(expr
->pos
, " got %s", show_typename(source
));
898 *rp
= cast_to(*rp
, target
);
901 *rp
= cast_to(*rp
, target
);
906 * FIXME!! This is wrong from a double evaluation standpoint. We can't
907 * just expand the expression twice, that would make any side effects
910 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
913 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
914 static const int op_trans
[] = {
915 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
916 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
917 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
918 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
919 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
920 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
921 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
922 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
923 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
924 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
926 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
, *e5
;
927 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
928 struct symbol
*ltype
= left
->ctype
;
929 struct expression
*addr
;
930 struct symbol
*lptype
;
932 if (left
->type
== EXPR_BITFIELD
)
933 addr
= left
->address
;
937 lptype
= addr
->ctype
;
939 a
->ctype
.base_type
= lptype
;
940 a
->bit_size
= lptype
->bit_size
;
941 a
->array_size
= lptype
->array_size
;
943 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
945 e0
->ctype
= &lazy_ptr_ctype
;
947 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
952 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
958 /* we can't cannibalize left, unfortunately */
959 e3
= alloc_expression(expr
->pos
, left
->type
);
961 if (e3
->type
== EXPR_BITFIELD
)
966 e4
= alloc_expression(expr
->pos
, EXPR_BINOP
);
967 e4
->op
= subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
970 /* will calculate type later */
972 e5
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
973 e5
->left
= e3
; /* we can share that one */
978 expr
->type
= EXPR_COMMA
;
983 return evaluate_binop(e4
);
986 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
988 if (type
->ctype
.modifiers
& MOD_CONST
)
989 warn(left
->pos
, "assignment to const expression");
990 if (type
->type
== SYM_NODE
)
991 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
994 static struct symbol
*evaluate_assignment(struct expression
*expr
)
996 struct expression
*left
= expr
->left
, *right
= expr
->right
;
997 struct expression
*where
= expr
;
998 struct symbol
*ltype
, *rtype
;
1000 if (!lvalue_expression(left
)) {
1001 warn(expr
->pos
, "not an lvalue");
1005 ltype
= left
->ctype
;
1007 if (expr
->op
!= '=') {
1008 if (!evaluate_binop_assignment(expr
, left
, right
))
1010 where
= expr
->right
; /* expr is EXPR_COMMA now */
1012 right
= where
->right
;
1015 rtype
= degenerate(right
);
1017 if (!compatible_assignment_types(where
, ltype
, &where
->right
, rtype
, "assignment"))
1020 evaluate_assign_to(left
, ltype
);
1022 expr
->ctype
= ltype
;
1026 static void examine_fn_arguments(struct symbol
*fn
)
1030 FOR_EACH_PTR(fn
->arguments
, s
) {
1031 struct symbol
*arg
= evaluate_symbol(s
);
1032 /* Array/function arguments silently degenerate into pointers */
1038 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1039 if (arg
->type
== SYM_ARRAY
)
1040 ptr
->ctype
= arg
->ctype
;
1042 ptr
->ctype
.base_type
= arg
;
1043 ptr
->ctype
.as
|= s
->ctype
.as
;
1044 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
1046 s
->ctype
.base_type
= ptr
;
1048 s
->ctype
.modifiers
= 0;
1049 examine_symbol_type(s
);
1059 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1061 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1062 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1064 newsym
->ctype
.as
= as
;
1065 newsym
->ctype
.modifiers
= mod
;
1071 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1073 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1074 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1076 node
->ctype
.base_type
= ptr
;
1077 ptr
->bit_size
= bits_in_pointer
;
1078 ptr
->ctype
.alignment
= pointer_alignment
;
1080 node
->bit_size
= bits_in_pointer
;
1081 node
->ctype
.alignment
= pointer_alignment
;
1084 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1085 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1086 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1087 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1089 if (sym
->type
== SYM_NODE
) {
1090 ptr
->ctype
.as
|= sym
->ctype
.as
;
1091 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1092 sym
= sym
->ctype
.base_type
;
1094 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1095 ptr
->ctype
.as
|= sym
->ctype
.as
;
1096 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1097 sym
= sym
->ctype
.base_type
;
1099 ptr
->ctype
.base_type
= sym
;
1104 /* Arrays degenerate into pointers on pointer arithmetic */
1105 static struct symbol
*degenerate(struct expression
*expr
)
1107 struct symbol
*ctype
, *base
;
1111 ctype
= expr
->ctype
;
1115 if (ctype
->type
== SYM_NODE
)
1116 base
= ctype
->ctype
.base_type
;
1118 * Arrays degenerate into pointers to the entries, while
1119 * functions degenerate into pointers to themselves.
1120 * If array was part of non-lvalue compound, we create a copy
1121 * of that compound first and then act as if we were dealing with
1122 * the corresponding field in there.
1124 switch (base
->type
) {
1126 if (expr
->type
== EXPR_SLICE
) {
1127 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1128 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1130 a
->ctype
.base_type
= expr
->base
->ctype
;
1131 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1132 a
->array_size
= expr
->base
->ctype
->array_size
;
1134 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1136 e0
->ctype
= &lazy_ptr_ctype
;
1138 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1141 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1143 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1145 e2
->right
= expr
->base
;
1147 e2
->ctype
= expr
->base
->ctype
;
1149 if (expr
->r_bitpos
) {
1150 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1153 e3
->right
= alloc_const_expression(expr
->pos
,
1154 expr
->r_bitpos
>> 3);
1155 e3
->ctype
= &lazy_ptr_ctype
;
1160 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1163 e4
->ctype
= &lazy_ptr_ctype
;
1166 expr
->type
= EXPR_PREOP
;
1170 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1171 warn(expr
->pos
, "strange non-value function or array");
1174 *expr
= *expr
->unop
;
1175 ctype
= create_pointer(expr
, ctype
, 1);
1176 expr
->ctype
= ctype
;
1183 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1185 struct expression
*op
= expr
->unop
;
1186 struct symbol
*ctype
;
1188 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1189 warn(expr
->pos
, "not addressable");
1196 * symbol expression evaluation is lazy about the type
1197 * of the sub-expression, so we may have to generate
1198 * the type here if so..
1200 if (expr
->ctype
== &lazy_ptr_ctype
) {
1201 ctype
= create_pointer(expr
, ctype
, 0);
1202 expr
->ctype
= ctype
;
1208 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1210 struct expression
*op
= expr
->unop
;
1211 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1213 /* Simplify: *&(expr) => (expr) */
1214 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1219 /* Dereferencing a node drops all the node information. */
1220 if (ctype
->type
== SYM_NODE
)
1221 ctype
= ctype
->ctype
.base_type
;
1223 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1224 target
= ctype
->ctype
.base_type
;
1226 switch (ctype
->type
) {
1228 warn(expr
->pos
, "cannot derefence this type");
1231 merge_type(node
, ctype
);
1232 if (ctype
->type
!= SYM_ARRAY
)
1235 * Dereferencing a pointer to an array results in a
1236 * degenerate dereference: the expression becomes
1237 * just a pointer to the entry, and the derefence
1242 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1243 target
->bit_size
= bits_in_pointer
;
1244 target
->ctype
.alignment
= pointer_alignment
;
1245 merge_type(target
, ctype
->ctype
.base_type
);
1249 if (!lvalue_expression(op
)) {
1250 warn(op
->pos
, "non-lvalue array??");
1254 /* Do the implied "addressof" on the array */
1258 * When an array is dereferenced, we need to pick
1259 * up the attributes of the original node too..
1261 merge_type(node
, op
->ctype
);
1262 merge_type(node
, ctype
);
1266 node
->bit_size
= target
->bit_size
;
1267 node
->array_size
= target
->array_size
;
1274 * Unary post-ops: x++ and x--
1276 static struct symbol
*evaluate_postop(struct expression
*expr
)
1278 struct expression
*op
= expr
->unop
;
1279 struct symbol
*ctype
= op
->ctype
;
1281 if (!lvalue_expression(expr
->unop
)) {
1282 warn(expr
->pos
, "need lvalue expression for ++/--");
1286 evaluate_assign_to(op
, ctype
);
1288 expr
->ctype
= ctype
;
1292 static struct symbol
*evaluate_sign(struct expression
*expr
)
1294 struct symbol
*ctype
= expr
->unop
->ctype
;
1295 if (is_int_type(ctype
)) {
1296 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1297 if (rtype
->bit_size
!= ctype
->bit_size
)
1298 expr
->unop
= cast_to(expr
->unop
, rtype
);
1300 } else if (is_float_type(ctype
) && expr
->op
!= '~') {
1301 /* no conversions needed */
1303 return bad_expr_type(expr
);
1305 if (expr
->op
== '+')
1306 *expr
= *expr
->unop
;
1307 expr
->ctype
= ctype
;
1311 static struct symbol
*evaluate_preop(struct expression
*expr
)
1313 struct symbol
*ctype
= expr
->unop
->ctype
;
1317 *expr
= *expr
->unop
;
1323 return evaluate_sign(expr
);
1326 return evaluate_dereference(expr
);
1329 return evaluate_addressof(expr
);
1331 case SPECIAL_INCREMENT
:
1332 case SPECIAL_DECREMENT
:
1334 * From a type evaluation standpoint the pre-ops are
1335 * the same as the postops
1337 return evaluate_postop(expr
);
1340 if (is_safe_type(ctype
))
1341 warn(expr
->pos
, "testing a 'safe expression'");
1342 if (is_float_type(ctype
)) {
1343 struct expression
*arg
= expr
->unop
;
1344 expr
->type
= EXPR_BINOP
;
1345 expr
->op
= SPECIAL_EQUAL
;
1347 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1348 expr
->right
->ctype
= ctype
;
1349 expr
->right
->fvalue
= 0;
1351 ctype
= &bool_ctype
;
1357 expr
->ctype
= ctype
;
1361 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1363 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1364 struct ptr_list
*list
= head
;
1370 for (i
= 0; i
< list
->nr
; i
++) {
1371 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1373 if (sym
->ident
!= ident
)
1375 *offset
= sym
->offset
;
1378 struct symbol
*ctype
= sym
->ctype
.base_type
;
1382 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1384 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1387 *offset
+= sym
->offset
;
1391 } while ((list
= list
->next
) != head
);
1395 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1397 struct expression
*add
;
1400 * Create a new add-expression
1402 * NOTE! Even if we just add zero, we need a new node
1403 * for the member pointer, since it has a different
1404 * type than the original pointer. We could make that
1405 * be just a cast, but the fact is, a node is a node,
1406 * so we might as well just do the "add zero" here.
1408 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1411 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1412 add
->right
->ctype
= &int_ctype
;
1413 add
->right
->value
= offset
;
1416 * The ctype of the pointer will be lazily evaluated if
1417 * we ever take the address of this member dereference..
1419 add
->ctype
= &lazy_ptr_ctype
;
1423 /* structure/union dereference */
1424 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1427 struct symbol
*ctype
, *member
;
1428 struct expression
*deref
= expr
->deref
, *add
;
1429 struct ident
*ident
= expr
->member
;
1433 if (!evaluate_expression(deref
))
1436 warn(expr
->pos
, "bad member name");
1440 ctype
= deref
->ctype
;
1441 address_space
= ctype
->ctype
.as
;
1442 mod
= ctype
->ctype
.modifiers
;
1443 if (ctype
->type
== SYM_NODE
) {
1444 ctype
= ctype
->ctype
.base_type
;
1445 address_space
|= ctype
->ctype
.as
;
1446 mod
|= ctype
->ctype
.modifiers
;
1448 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1449 warn(expr
->pos
, "expected structure or union");
1453 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1455 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1456 const char *name
= "<unnamed>";
1459 name
= ctype
->ident
->name
;
1460 namelen
= ctype
->ident
->len
;
1462 warn(expr
->pos
, "no member '%s' in %s %.*s",
1463 show_ident(ident
), type
, namelen
, name
);
1468 * The member needs to take on the address space and modifiers of
1469 * the "parent" type.
1471 member
= convert_to_as_mod(member
, address_space
, mod
);
1472 ctype
= member
->ctype
.base_type
;
1474 if (!lvalue_expression(deref
)) {
1475 if (deref
->type
!= EXPR_SLICE
) {
1479 expr
->base
= deref
->base
;
1480 expr
->r_bitpos
= deref
->r_bitpos
;
1482 expr
->r_bitpos
+= offset
<< 3;
1483 expr
->type
= EXPR_SLICE
;
1484 if (ctype
->type
== SYM_BITFIELD
) {
1485 expr
->r_bitpos
+= member
->bit_offset
;
1486 expr
->r_nrbits
= member
->fieldwidth
;
1488 expr
->r_nrbits
= member
->bit_size
;
1490 expr
->ctype
= member
;
1494 deref
= deref
->unop
;
1495 expr
->deref
= deref
;
1497 add
= evaluate_offset(deref
, offset
);
1498 if (ctype
->type
== SYM_BITFIELD
) {
1499 expr
->type
= EXPR_BITFIELD
;
1500 expr
->bitpos
= member
->bit_offset
;
1501 expr
->nrbits
= member
->fieldwidth
;
1502 expr
->address
= add
;
1504 expr
->type
= EXPR_PREOP
;
1509 expr
->ctype
= member
;
1513 static struct symbol
*evaluate_cast(struct expression
*);
1515 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1519 if (expr
->cast_type
) {
1520 if (expr
->cast_expression
) {
1521 struct symbol
*sym
= evaluate_cast(expr
);
1522 size
= sym
->bit_size
;
1524 examine_symbol_type(expr
->cast_type
);
1525 size
= expr
->cast_type
->bit_size
;
1528 if (!evaluate_expression(expr
->cast_expression
))
1530 size
= expr
->cast_expression
->ctype
->bit_size
;
1531 if (is_bitfield_type (expr
->cast_expression
->ctype
))
1532 warn(expr
->pos
, "sizeof applied to bitfield type");
1535 warn(expr
->pos
, "cannot size expression");
1536 expr
->type
= EXPR_VALUE
;
1537 expr
->value
= size
>> 3;
1538 expr
->ctype
= size_t_ctype
;
1539 return size_t_ctype
;
1542 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1544 struct symbol
*type
= expr
->cast_type
;
1547 type
= evaluate_expression(expr
->cast_expression
);
1551 if (is_bitfield_type(type
))
1552 warn(expr
->pos
, "alignof applied to bitfield type");
1553 examine_symbol_type(type
);
1554 expr
->type
= EXPR_VALUE
;
1555 expr
->value
= type
->ctype
.alignment
;
1556 expr
->ctype
= size_t_ctype
;
1557 return size_t_ctype
;
1560 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1562 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1563 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1567 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1569 struct expression
*expr
;
1570 struct symbol_list
*argument_types
= fn
->arguments
;
1571 struct symbol
*argtype
;
1574 PREPARE_PTR_LIST(argument_types
, argtype
);
1575 FOR_EACH_PTR (head
, expr
) {
1576 struct expression
**p
= THIS_ADDRESS(expr
);
1577 struct symbol
*ctype
, *target
;
1578 ctype
= evaluate_expression(expr
);
1583 if (context_clash(f
, ctype
))
1584 warn(expr
->pos
, "argument %d used in wrong context", i
);
1586 ctype
= degenerate(expr
);
1589 if (!target
&& ctype
->bit_size
< bits_in_int
)
1590 target
= &int_ctype
;
1592 static char where
[30];
1593 examine_symbol_type(target
);
1594 sprintf(where
, "argument %d", i
);
1595 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1599 NEXT_PTR_LIST(argtype
);
1601 FINISH_PTR_LIST(argtype
);
1605 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1606 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1608 struct expression
*entry
;
1611 int accept_string
= is_byte_type(ctype
);
1613 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1614 struct expression
**p
= THIS_ADDRESS(entry
);
1618 if (entry
->type
== EXPR_INDEX
) {
1619 current
= entry
->idx_to
;
1622 if (accept_string
&& entry
->type
== EXPR_STRING
) {
1623 sym
= evaluate_expression(entry
);
1624 entries
= get_expression_value(sym
->array_size
);
1626 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1636 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1637 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1639 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1640 warn(expr
->pos
, "unexpected compound initializer");
1643 return evaluate_array_initializer(ctype
, expr
, 0);
1646 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1648 struct expression
*entry
;
1651 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1652 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1653 struct expression
**p
= THIS_ADDRESS(entry
);
1655 if (entry
->type
== EXPR_IDENTIFIER
) {
1656 struct ident
*ident
= entry
->expr_ident
;
1657 /* We special-case the "already right place" case */
1658 if (sym
&& sym
->ident
== ident
)
1660 RESET_PTR_LIST(sym
);
1663 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1666 if (sym
->ident
== ident
)
1674 warn(expr
->pos
, "too many initializers for struct/union");
1678 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1682 FINISH_PTR_LIST(sym
);
1688 * Initializers are kind of like assignments. Except
1689 * they can be a hell of a lot more complex.
1691 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1693 struct expression
*expr
= *ep
;
1696 * Simple non-structure/array initializers are the simple
1697 * case, and look (and parse) largely like assignments.
1699 if (expr
->type
!= EXPR_INITIALIZER
) {
1700 int size
= 0, is_string
= expr
->type
== EXPR_STRING
;
1701 struct symbol
*rtype
= evaluate_expression(expr
);
1703 struct expression
*pos
;
1707 * char array[] = "string"
1708 * should _not_ degenerate.
1710 if (is_string
&& is_string_type(ctype
)) {
1711 struct expression
*array_size
= ctype
->array_size
;
1713 array_size
= ctype
->array_size
= rtype
->array_size
;
1714 size
= get_expression_value(array_size
);
1716 rtype
= degenerate(expr
);
1719 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1722 * Don't bother creating a position expression for
1723 * the simple initializer cases that don't need it.
1725 * We need a position if the initializer has a byte
1726 * offset, _or_ if we're initializing a bitfield.
1728 if (offset
|| ctype
->fieldwidth
) {
1729 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1730 pos
->init_offset
= offset
;
1731 pos
->init_sym
= ctype
;
1732 pos
->init_expr
= *ep
;
1733 pos
->ctype
= expr
->ctype
;
1740 expr
->ctype
= ctype
;
1741 if (ctype
->type
== SYM_NODE
)
1742 ctype
= ctype
->ctype
.base_type
;
1744 switch (ctype
->type
) {
1747 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1749 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1751 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1753 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1757 static int get_as(struct symbol
*sym
)
1765 mod
= sym
->ctype
.modifiers
;
1766 if (sym
->type
== SYM_NODE
) {
1767 sym
= sym
->ctype
.base_type
;
1768 as
|= sym
->ctype
.as
;
1769 mod
|= sym
->ctype
.modifiers
;
1773 * At least for now, allow casting to a "unsigned long".
1774 * That's how we do things like pointer arithmetic and
1775 * store pointers to registers.
1777 if (sym
== &ulong_ctype
)
1780 if (sym
&& sym
->type
== SYM_PTR
) {
1781 sym
= sym
->ctype
.base_type
;
1782 as
|= sym
->ctype
.as
;
1783 mod
|= sym
->ctype
.modifiers
;
1785 if (mod
& MOD_FORCE
)
1790 static struct symbol
*evaluate_cast(struct expression
*expr
)
1792 struct expression
*target
= expr
->cast_expression
;
1793 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1796 expr
->ctype
= ctype
;
1797 expr
->cast_type
= ctype
;
1800 * Special case: a cast can be followed by an
1801 * initializer, in which case we need to pass
1802 * the type value down to that initializer rather
1803 * than trying to evaluate it as an expression
1805 * A more complex case is when the initializer is
1806 * dereferenced as part of a post-fix expression.
1807 * We need to produce an expression that can be dereferenced.
1809 if (target
->type
== EXPR_INITIALIZER
) {
1810 struct symbol
*sym
= expr
->cast_type
;
1811 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1813 sym
->initializer
= expr
->cast_expression
;
1814 evaluate_symbol(sym
);
1816 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
1819 expr
->type
= EXPR_PREOP
;
1827 evaluate_expression(target
);
1831 * You can always throw a value away by casting to
1832 * "void" - that's an implicit "force". Note that
1833 * the same is _not_ true of "void *".
1835 if (ctype
== &void_ctype
)
1839 if (type
== SYM_NODE
) {
1840 type
= ctype
->ctype
.base_type
->type
;
1841 if (ctype
->ctype
.base_type
== &void_ctype
)
1844 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
1845 warn(expr
->pos
, "cast to non-scalar");
1847 if (!target
->ctype
) {
1848 warn(expr
->pos
, "cast from unknown type");
1852 type
= target
->ctype
->type
;
1853 if (type
== SYM_NODE
)
1854 type
= target
->ctype
->ctype
.base_type
->type
;
1855 if (type
== SYM_ARRAY
|| type
== SYM_UNION
|| type
== SYM_STRUCT
)
1856 warn(expr
->pos
, "cast from non-scalar");
1858 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1859 warn(expr
->pos
, "cast removes address space of expression");
1862 * Casts of constant values are special: they
1863 * can be NULL, and thus need to be simplified
1866 if (target
->type
== EXPR_VALUE
)
1867 cast_value(expr
, ctype
, target
, target
->ctype
);
1874 * Evaluate a call expression with a symbol. This
1875 * should expand inline functions, and evaluate
1878 static int evaluate_symbol_call(struct expression
*expr
)
1880 struct expression
*fn
= expr
->fn
;
1881 struct symbol
*ctype
= fn
->ctype
;
1883 if (fn
->type
!= EXPR_PREOP
)
1886 if (ctype
->op
&& ctype
->op
->evaluate
)
1887 return ctype
->op
->evaluate(expr
);
1889 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1891 struct symbol
*curr
= current_fn
;
1892 unsigned long context
= current_context
;
1893 unsigned long mask
= current_contextmask
;
1895 current_context
|= ctype
->ctype
.context
;
1896 current_contextmask
|= ctype
->ctype
.contextmask
;
1897 current_fn
= ctype
->ctype
.base_type
;
1898 examine_fn_arguments(current_fn
);
1900 ret
= inline_function(expr
, ctype
);
1902 /* restore the old function context */
1904 current_context
= context
;
1905 current_contextmask
= mask
;
1912 static struct symbol
*evaluate_call(struct expression
*expr
)
1915 struct symbol
*ctype
, *sym
;
1916 struct expression
*fn
= expr
->fn
;
1917 struct expression_list
*arglist
= expr
->args
;
1919 if (!evaluate_expression(fn
))
1921 sym
= ctype
= fn
->ctype
;
1922 if (ctype
->type
== SYM_NODE
)
1923 ctype
= ctype
->ctype
.base_type
;
1924 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1925 ctype
= ctype
->ctype
.base_type
;
1926 if (!evaluate_arguments(sym
, ctype
, arglist
))
1928 if (ctype
->type
!= SYM_FN
) {
1929 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1932 args
= expression_list_size(expr
->args
);
1933 fnargs
= symbol_list_size(ctype
->arguments
);
1935 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1936 if (args
> fnargs
&& !ctype
->variadic
)
1937 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1938 if (sym
->type
== SYM_NODE
) {
1939 if (evaluate_symbol_call(expr
))
1942 expr
->ctype
= ctype
->ctype
.base_type
;
1946 struct symbol
*evaluate_expression(struct expression
*expr
)
1953 switch (expr
->type
) {
1956 warn(expr
->pos
, "value expression without a type");
1959 return evaluate_string(expr
);
1961 return evaluate_symbol_expression(expr
);
1963 if (!evaluate_expression(expr
->left
))
1965 if (!evaluate_expression(expr
->right
))
1967 return evaluate_binop(expr
);
1969 return evaluate_logical(expr
);
1971 evaluate_expression(expr
->left
);
1972 if (!evaluate_expression(expr
->right
))
1974 return evaluate_comma(expr
);
1976 if (!evaluate_expression(expr
->left
))
1978 if (!evaluate_expression(expr
->right
))
1980 return evaluate_compare(expr
);
1981 case EXPR_ASSIGNMENT
:
1982 if (!evaluate_expression(expr
->left
))
1984 if (!evaluate_expression(expr
->right
))
1986 return evaluate_assignment(expr
);
1988 if (!evaluate_expression(expr
->unop
))
1990 return evaluate_preop(expr
);
1992 if (!evaluate_expression(expr
->unop
))
1994 return evaluate_postop(expr
);
1996 return evaluate_cast(expr
);
1998 return evaluate_sizeof(expr
);
2000 return evaluate_alignof(expr
);
2002 return evaluate_member_dereference(expr
);
2004 return evaluate_call(expr
);
2006 warn(expr
->pos
, "bitfield generated by parser");
2009 case EXPR_CONDITIONAL
:
2010 if (!evaluate_conditional(&expr
->conditional
))
2012 if (!evaluate_expression(expr
->cond_false
))
2014 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
2016 return evaluate_conditional_expression(expr
);
2017 case EXPR_STATEMENT
:
2018 expr
->ctype
= evaluate_statement(expr
->statement
);
2022 expr
->ctype
= &ptr_ctype
;
2026 /* Evaluate the type of the symbol .. */
2027 evaluate_symbol(expr
->symbol
);
2028 /* .. but the type of the _expression_ is a "type" */
2029 expr
->ctype
= &type_ctype
;
2032 /* These can not exist as stand-alone expressions */
2033 case EXPR_INITIALIZER
:
2034 case EXPR_IDENTIFIER
:
2037 warn(expr
->pos
, "internal front-end error: initializer in expression");
2040 warn(expr
->pos
, "internal front-end error: SLICE re-evaluated");
2046 void check_duplicates(struct symbol
*sym
)
2048 struct symbol
*next
= sym
;
2050 while ((next
= next
->same_symbol
) != NULL
) {
2051 const char *typediff
;
2052 evaluate_symbol(next
);
2053 typediff
= type_difference(sym
, next
, 0, 0);
2055 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2056 show_ident(sym
->ident
),
2057 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
2063 struct symbol
*evaluate_symbol(struct symbol
*sym
)
2065 struct symbol
*base_type
;
2070 sym
= examine_symbol_type(sym
);
2071 base_type
= sym
->ctype
.base_type
;
2075 /* Evaluate the initializers */
2076 if (sym
->initializer
) {
2077 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
2078 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
2079 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
2080 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
2081 base_type
->bit_size
= bit_size
;
2082 sym
->array_size
= base_type
->array_size
;
2083 sym
->bit_size
= bit_size
;
2087 /* And finally, evaluate the body of the symbol too */
2088 if (base_type
->type
== SYM_FN
) {
2089 struct symbol
*curr
= current_fn
;
2090 unsigned long context
= current_context
;
2091 unsigned long mask
= current_contextmask
;
2093 current_fn
= base_type
;
2094 current_contextmask
= sym
->ctype
.contextmask
;
2095 current_context
= sym
->ctype
.context
;
2097 examine_fn_arguments(base_type
);
2098 if (!base_type
->stmt
&& base_type
->inline_stmt
)
2100 if (base_type
->stmt
)
2101 evaluate_statement(base_type
->stmt
);
2104 current_contextmask
= mask
;
2105 current_context
= context
;
2111 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
2113 struct expression
*expr
= stmt
->expression
;
2114 struct symbol
*ctype
, *fntype
;
2116 evaluate_expression(expr
);
2117 ctype
= degenerate(expr
);
2118 fntype
= current_fn
->ctype
.base_type
;
2119 if (!fntype
|| fntype
== &void_ctype
) {
2120 if (expr
&& ctype
!= &void_ctype
)
2121 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
2126 warn(stmt
->pos
, "return with no return value");
2131 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
2135 static void evaluate_if_statement(struct statement
*stmt
)
2137 if (!stmt
->if_conditional
)
2140 evaluate_conditional(&stmt
->if_conditional
);
2141 evaluate_statement(stmt
->if_true
);
2142 evaluate_statement(stmt
->if_false
);
2145 static void evaluate_iterator(struct statement
*stmt
)
2147 struct expression
**pre
= &stmt
->iterator_pre_condition
;
2148 struct expression
**post
= &stmt
->iterator_post_condition
;
2149 if (*pre
== *post
) {
2150 evaluate_conditional(pre
);
2153 evaluate_conditional(pre
);
2154 evaluate_conditional(post
);
2156 evaluate_statement(stmt
->iterator_pre_statement
);
2157 evaluate_statement(stmt
->iterator_statement
);
2158 evaluate_statement(stmt
->iterator_post_statement
);
2161 struct symbol
*evaluate_statement(struct statement
*stmt
)
2166 switch (stmt
->type
) {
2168 return evaluate_return_expression(stmt
);
2170 case STMT_EXPRESSION
:
2171 if (!evaluate_expression(stmt
->expression
))
2173 return degenerate(stmt
->expression
);
2175 case STMT_COMPOUND
: {
2176 struct statement
*s
;
2177 struct symbol
*type
= NULL
;
2180 /* Evaluate each symbol in the compound statement */
2181 FOR_EACH_PTR(stmt
->syms
, sym
) {
2182 evaluate_symbol(sym
);
2184 evaluate_symbol(stmt
->ret
);
2187 * Then, evaluate each statement, making the type of the
2188 * compound statement be the type of the last statement
2191 FOR_EACH_PTR(stmt
->stmts
, s
) {
2192 type
= evaluate_statement(s
);
2199 evaluate_if_statement(stmt
);
2202 evaluate_iterator(stmt
);
2205 evaluate_expression(stmt
->switch_expression
);
2206 evaluate_statement(stmt
->switch_statement
);
2209 evaluate_expression(stmt
->case_expression
);
2210 evaluate_expression(stmt
->case_to
);
2211 evaluate_statement(stmt
->case_statement
);
2214 return evaluate_statement(stmt
->label_statement
);
2216 evaluate_expression(stmt
->goto_expression
);
2221 /* FIXME! Do the asm parameter evaluation! */