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
= NULL
; /* 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
;
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_int_type(struct symbol
*type
)
199 if (type
->type
== SYM_NODE
)
200 type
= type
->ctype
.base_type
;
201 return (type
->type
== SYM_ENUM
) ||
202 (type
->type
== SYM_BITFIELD
) ||
203 type
->ctype
.base_type
== &int_type
;
206 static inline int is_float_type(struct symbol
*type
)
208 if (type
->type
== SYM_NODE
)
209 type
= type
->ctype
.base_type
;
210 return type
->ctype
.base_type
== &fp_type
;
213 static struct symbol
*bad_expr_type(struct expression
*expr
)
215 warn(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
216 switch (expr
->type
) {
219 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
220 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
224 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
233 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
235 struct expression
*left
= *lp
, *right
= *rp
;
236 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
238 if (ltype
->type
== SYM_NODE
)
239 ltype
= ltype
->ctype
.base_type
;
240 if (rtype
->type
== SYM_NODE
)
241 rtype
= rtype
->ctype
.base_type
;
242 if (is_float_type(ltype
)) {
243 if (is_int_type(rtype
))
245 if (is_float_type(rtype
)) {
246 unsigned long lmod
= ltype
->ctype
.modifiers
;
247 unsigned long rmod
= rtype
->ctype
.modifiers
;
248 lmod
&= MOD_LONG
| MOD_LONGLONG
;
249 rmod
&= MOD_LONG
| MOD_LONGLONG
;
259 if (!is_float_type(rtype
) || !is_int_type(ltype
))
262 *lp
= cast_to(left
, rtype
);
265 *rp
= cast_to(right
, ltype
);
269 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
271 struct expression
*left
= *lp
, *right
= *rp
;
272 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
274 if (ltype
->type
== SYM_NODE
)
275 ltype
= ltype
->ctype
.base_type
;
276 if (rtype
->type
== SYM_NODE
)
277 rtype
= rtype
->ctype
.base_type
;
278 if (is_int_type(ltype
) && is_int_type(rtype
)) {
279 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
281 /* Don't bother promoting same-size entities, it only adds clutter */
282 if (ltype
->bit_size
!= ctype
->bit_size
)
283 *lp
= cast_to(left
, ctype
);
284 if (rtype
->bit_size
!= ctype
->bit_size
)
285 *rp
= cast_to(right
, ctype
);
291 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
293 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
294 if (!ctype
&& float_ok
)
295 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
300 return bad_expr_type(expr
);
303 static inline int lvalue_expression(struct expression
*expr
)
305 while (expr
->type
== EXPR_CAST
)
306 expr
= expr
->cast_expression
;
307 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
310 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
312 struct symbol
*ctype
;
313 struct symbol
*ptr_type
= ptr
->ctype
;
316 if (ptr_type
->type
== SYM_NODE
)
317 ptr_type
= ptr_type
->ctype
.base_type
;
319 if (!is_int_type(i
->ctype
))
320 return bad_expr_type(expr
);
323 examine_symbol_type(ctype
);
325 ctype
= degenerate(ptr
);
326 if (!ctype
->ctype
.base_type
) {
327 warn(expr
->pos
, "missing type information");
331 /* Get the size of whatever the pointer points to */
333 if (ptr_type
->type
== SYM_NODE
)
334 ptr_type
= ptr_type
->ctype
.base_type
;
335 if (ptr_type
->type
== SYM_PTR
)
336 ptr_type
= ptr_type
->ctype
.base_type
;
337 bit_size
= ptr_type
->bit_size
;
339 /* Special case: adding zero commonly happens as a result of 'array[0]' */
340 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
342 } else if (bit_size
> bits_in_char
) {
343 struct expression
*add
= expr
;
344 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
345 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
347 val
->ctype
= size_t_ctype
;
348 val
->value
= bit_size
>> 3;
351 mul
->ctype
= size_t_ctype
;
355 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
364 static struct symbol
*evaluate_add(struct expression
*expr
)
366 struct expression
*left
= expr
->left
, *right
= expr
->right
;
367 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
369 if (is_ptr_type(ltype
))
370 return evaluate_ptr_add(expr
, left
, right
);
372 if (is_ptr_type(rtype
))
373 return evaluate_ptr_add(expr
, right
, left
);
375 return evaluate_arith(expr
, 1);
378 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
379 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
381 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
382 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
385 unsigned long mod1
, mod2
, diff
;
386 unsigned long as1
, as2
;
388 struct symbol
*base1
, *base2
;
390 if (target
== source
)
392 if (!target
|| !source
)
393 return "different types";
395 * Peel of per-node information.
396 * FIXME! Check alignment and context too here!
398 mod1
= target
->ctype
.modifiers
;
399 as1
= target
->ctype
.as
;
400 mod2
= source
->ctype
.modifiers
;
401 as2
= source
->ctype
.as
;
402 if (target
->type
== SYM_NODE
) {
403 target
= target
->ctype
.base_type
;
406 if (target
->type
== SYM_PTR
) {
410 mod1
|= target
->ctype
.modifiers
;
411 as1
|= target
->ctype
.as
;
413 if (source
->type
== SYM_NODE
) {
414 source
= source
->ctype
.base_type
;
417 if (source
->type
== SYM_PTR
) {
421 mod2
|= source
->ctype
.modifiers
;
422 as2
|= source
->ctype
.as
;
425 if (target
== source
)
427 if (!target
|| !source
)
428 return "different types";
430 type1
= target
->type
;
431 base1
= target
->ctype
.base_type
;
433 type2
= source
->type
;
434 base2
= source
->ctype
.base_type
;
437 * Pointers to functions compare as the function itself
439 if (type1
== SYM_PTR
&& base1
) {
440 switch (base1
->type
) {
444 base1
= base1
->ctype
.base_type
;
449 if (type2
== SYM_PTR
&& base2
) {
450 switch (base2
->type
) {
454 base2
= base2
->ctype
.base_type
;
460 /* Arrays degenerate to pointers for type comparisons */
461 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
462 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
465 return "different base types";
467 /* Must be same address space to be comparable */
469 return "different address spaces";
471 /* Ignore differences in storage types, sign, or addressability */
472 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
474 mod1
&= diff
& ~target_mod_ignore
;
475 mod2
&= diff
& ~source_mod_ignore
;
477 if ((mod1
| mod2
) & MOD_SIZE
)
478 return "different type sizes";
479 return "different modifiers";
483 if (type1
== SYM_FN
) {
485 struct symbol
*arg1
, *arg2
;
486 if (base1
->variadic
!= base2
->variadic
)
487 return "incompatible variadic arguments";
488 PREPARE_PTR_LIST(target
->arguments
, arg1
);
489 PREPARE_PTR_LIST(source
->arguments
, arg2
);
493 diff
= type_difference(arg1
, arg2
, 0, 0);
495 static char argdiff
[80];
496 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
505 FINISH_PTR_LIST(arg2
);
506 FINISH_PTR_LIST(arg1
);
515 static int is_null_ptr(struct expression
*expr
)
517 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
519 if (!is_ptr_type(expr
->ctype
))
520 warn(expr
->pos
, "Using plain integer as NULL pointer");
524 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
526 /* NULL expression? Just return the type of the "other side" */
535 * Ignore differences in "volatile" and "const"ness when
536 * subtracting pointers
538 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
540 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
542 const char *typediff
;
543 struct symbol
*ctype
;
544 struct symbol
*ltype
, *rtype
;
546 ltype
= degenerate(l
);
547 rtype
= degenerate(r
);
550 * If it is an integer subtract: the ptr add case will do the
553 if (!is_ptr_type(rtype
))
554 return evaluate_ptr_add(expr
, l
, r
);
557 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
559 ctype
= common_ptr_type(l
, r
);
561 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
565 examine_symbol_type(ctype
);
567 /* Figure out the base type we point to */
568 if (ctype
->type
== SYM_NODE
)
569 ctype
= ctype
->ctype
.base_type
;
570 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
571 warn(expr
->pos
, "subtraction of functions? Share your drugs");
574 ctype
= ctype
->ctype
.base_type
;
576 expr
->ctype
= ssize_t_ctype
;
577 if (ctype
->bit_size
> bits_in_char
) {
578 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
579 struct expression
*div
= expr
;
580 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
582 val
->ctype
= size_t_ctype
;
583 val
->value
= ctype
->bit_size
>> 3;
586 sub
->ctype
= ssize_t_ctype
;
595 return ssize_t_ctype
;
598 static struct symbol
*evaluate_sub(struct expression
*expr
)
600 struct expression
*left
= expr
->left
, *right
= expr
->right
;
601 struct symbol
*ltype
= left
->ctype
;
603 if (is_ptr_type(ltype
))
604 return evaluate_ptr_sub(expr
, left
, right
);
606 return evaluate_arith(expr
, 1);
609 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
611 static struct symbol
*evaluate_conditional(struct expression
**p
)
613 struct symbol
*ctype
;
614 struct expression
*expr
= *p
;
619 if (expr
->type
== EXPR_ASSIGNMENT
)
620 warn(expr
->pos
, "assignment expression in conditional");
622 ctype
= evaluate_expression(expr
);
624 if (is_safe_type(ctype
))
625 warn(expr
->pos
, "testing a 'safe expression'");
626 if (is_float_type(ctype
)) {
627 struct expression
*comp
;
629 * It's easier to handle here, rather than deal with
630 * FP all over the place. Floating point in boolean
631 * context is rare enough (and very often wrong),
632 * so price of explicit comparison with appropriate
633 * FP zero is not too high. And it simplifies things
636 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
637 comp
->op
= SPECIAL_NOTEQUAL
;
639 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
640 comp
->right
->ctype
= comp
->left
->ctype
;
641 comp
->right
->fvalue
= 0;
642 ctype
= comp
->ctype
= &bool_ctype
;
650 static struct symbol
*evaluate_logical(struct expression
*expr
)
652 if (!evaluate_conditional(&expr
->left
))
654 if (!evaluate_conditional(&expr
->right
))
657 expr
->ctype
= &bool_ctype
;
661 static struct symbol
*evaluate_shift(struct expression
*expr
)
663 struct expression
*left
= expr
->left
, *right
= expr
->right
;
664 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
666 if (ltype
->type
== SYM_NODE
)
667 ltype
= ltype
->ctype
.base_type
;
668 if (rtype
->type
== SYM_NODE
)
669 rtype
= rtype
->ctype
.base_type
;
670 if (is_int_type(ltype
) && is_int_type(rtype
)) {
671 struct symbol
*ctype
= integer_promotion(ltype
);
672 if (ltype
->bit_size
!= ctype
->bit_size
)
673 expr
->left
= cast_to(expr
->left
, ctype
);
675 ctype
= integer_promotion(rtype
);
676 if (rtype
->bit_size
!= ctype
->bit_size
)
677 expr
->right
= cast_to(expr
->right
, ctype
);
680 return bad_expr_type(expr
);
683 static struct symbol
*evaluate_binop(struct expression
*expr
)
686 // addition can take ptr+int, fp and int
688 return evaluate_add(expr
);
690 // subtraction can take ptr-ptr, fp and int
692 return evaluate_sub(expr
);
694 // Arithmetic operations can take fp and int
696 return evaluate_arith(expr
, 1);
698 // shifts do integer promotions, but that's it.
699 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
700 return evaluate_shift(expr
);
702 // The rest are integer operations
703 // '%', '&', '^', '|'
705 return evaluate_arith(expr
, 0);
709 static struct symbol
*evaluate_comma(struct expression
*expr
)
711 expr
->ctype
= expr
->right
->ctype
;
715 static int modify_for_unsigned(int op
)
718 op
= SPECIAL_UNSIGNED_LT
;
720 op
= SPECIAL_UNSIGNED_GT
;
721 else if (op
== SPECIAL_LTE
)
722 op
= SPECIAL_UNSIGNED_LTE
;
723 else if (op
== SPECIAL_GTE
)
724 op
= SPECIAL_UNSIGNED_GTE
;
728 static struct symbol
*evaluate_compare(struct expression
*expr
)
730 struct expression
*left
= expr
->left
, *right
= expr
->right
;
731 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
732 struct symbol
*ctype
;
735 if (is_type_type(ltype
) && is_type_type(rtype
)) {
736 expr
->ctype
= &bool_ctype
;
740 if (is_safe_type(ltype
) || is_safe_type(rtype
))
741 warn(expr
->pos
, "testing a 'safe expression'");
744 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
745 expr
->ctype
= &bool_ctype
;
746 // FIXME! Check the types for compatibility
750 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
752 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
753 expr
->op
= modify_for_unsigned(expr
->op
);
754 expr
->ctype
= &bool_ctype
;
757 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
759 expr
->ctype
= &bool_ctype
;
763 return bad_expr_type(expr
);
767 * FIXME!! This should do casts, array degeneration etc..
769 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
771 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
773 if (ltype
->type
== SYM_NODE
)
774 ltype
= ltype
->ctype
.base_type
;
776 if (rtype
->type
== SYM_NODE
)
777 rtype
= rtype
->ctype
.base_type
;
779 if (ltype
->type
== SYM_PTR
) {
780 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
784 if (rtype
->type
== SYM_PTR
) {
785 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
791 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
793 struct expression
*cond
, *true, *false;
794 struct symbol
*ctype
, *ltype
, *rtype
;
795 const char * typediff
;
797 ctype
= degenerate(expr
->conditional
);
798 cond
= expr
->conditional
;
802 if (expr
->cond_true
) {
803 ltype
= degenerate(expr
->cond_true
);
804 true = expr
->cond_true
;
807 rtype
= degenerate(expr
->cond_false
);
808 false = expr
->cond_false
;
811 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
815 ctype
= compatible_integer_binop(&true, &expr
->cond_false
);
818 ctype
= compatible_ptr_type(true, expr
->cond_false
);
821 ctype
= compatible_float_binop(&true, &expr
->cond_false
);
824 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
832 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
833 struct expression
**rp
, struct symbol
*source
, const char *where
)
835 const char *typediff
;
839 /* It's ok if the target is more volatile or const than the source */
840 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
844 if (is_int_type(target
)) {
845 if (is_int_type(source
)) {
846 if (target
->bit_size
!= source
->bit_size
)
850 if (is_float_type(source
))
852 } else if (is_float_type(target
)) {
853 if (is_int_type(source
))
855 if (is_float_type(source
)) {
856 if (target
->bit_size
!= source
->bit_size
)
862 /* Pointer destination? */
864 target_as
= t
->ctype
.as
;
865 if (t
->type
== SYM_NODE
) {
866 t
= t
->ctype
.base_type
;
867 target_as
|= t
->ctype
.as
;
869 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
870 struct expression
*right
= *rp
;
871 struct symbol
*s
= source
;
874 // NULL pointer is always ok
875 if (is_null_ptr(right
))
878 /* "void *" matches anything as long as the address space is ok */
879 source_as
= s
->ctype
.as
;
880 if (s
->type
== SYM_NODE
) {
881 s
= s
->ctype
.base_type
;
882 source_as
|= s
->ctype
.as
;
884 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
885 s
= s
->ctype
.base_type
;
886 t
= t
->ctype
.base_type
;
887 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
));
898 *rp
= cast_to(*rp
, target
);
903 * FIXME!! This is wrong from a double evaluation standpoint. We can't
904 * just expand the expression twice, that would make any side effects
907 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
910 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
911 static const int op_trans
[] = {
912 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
913 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
914 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
915 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
916 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
917 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
918 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
919 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
920 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
921 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
924 subexpr
->left
= left
;
925 subexpr
->right
= right
;
926 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
928 expr
->right
= subexpr
;
929 return evaluate_binop(subexpr
);
932 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
934 if (type
->ctype
.modifiers
& MOD_CONST
)
935 warn(left
->pos
, "assignment to const expression");
936 if (type
->type
== SYM_NODE
)
937 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
940 static struct symbol
*evaluate_assignment(struct expression
*expr
)
942 struct expression
*left
= expr
->left
, *right
= expr
->right
;
943 struct symbol
*ltype
, *rtype
;
946 rtype
= right
->ctype
;
947 if (expr
->op
!= '=') {
948 rtype
= evaluate_binop_assignment(expr
, left
, right
);
954 if (!lvalue_expression(left
)) {
955 warn(expr
->pos
, "not an lvalue");
959 rtype
= degenerate(right
);
961 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
964 evaluate_assign_to(left
, ltype
);
970 static void examine_fn_arguments(struct symbol
*fn
)
974 FOR_EACH_PTR(fn
->arguments
, s
) {
975 struct symbol
*arg
= evaluate_symbol(s
);
976 /* Array/function arguments silently degenerate into pointers */
982 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
983 if (arg
->type
== SYM_ARRAY
)
984 ptr
->ctype
= arg
->ctype
;
986 ptr
->ctype
.base_type
= arg
;
987 ptr
->ctype
.as
|= s
->ctype
.as
;
988 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
990 s
->ctype
.base_type
= ptr
;
992 s
->ctype
.modifiers
= 0;
993 examine_symbol_type(s
);
1003 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1005 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1006 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1008 newsym
->ctype
.as
= as
;
1009 newsym
->ctype
.modifiers
= mod
;
1015 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1017 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1018 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1020 node
->ctype
.base_type
= ptr
;
1021 ptr
->bit_size
= bits_in_pointer
;
1022 ptr
->ctype
.alignment
= pointer_alignment
;
1024 node
->bit_size
= bits_in_pointer
;
1025 node
->ctype
.alignment
= pointer_alignment
;
1027 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1028 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1029 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1030 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1032 if (sym
->type
== SYM_NODE
) {
1033 ptr
->ctype
.as
|= sym
->ctype
.as
;
1034 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1035 sym
= sym
->ctype
.base_type
;
1037 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1038 ptr
->ctype
.as
|= sym
->ctype
.as
;
1039 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1040 sym
= sym
->ctype
.base_type
;
1042 ptr
->ctype
.base_type
= sym
;
1047 /* Arrays degenerate into pointers on pointer arithmetic */
1048 static struct symbol
*degenerate(struct expression
*expr
)
1050 struct symbol
*ctype
, *base
;
1054 ctype
= expr
->ctype
;
1058 if (ctype
->type
== SYM_NODE
)
1059 base
= ctype
->ctype
.base_type
;
1061 * Arrays degenerate into pointers to the entries, while
1062 * functions degenerate into pointers to themselves
1064 switch (base
->type
) {
1067 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1068 warn(expr
->pos
, "strange non-value function or array");
1071 *expr
= *expr
->unop
;
1072 ctype
= create_pointer(expr
, ctype
, 1);
1073 expr
->ctype
= ctype
;
1080 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1082 struct expression
*op
= expr
->unop
;
1083 struct symbol
*ctype
;
1085 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1086 warn(expr
->pos
, "not addressable");
1093 * symbol expression evaluation is lazy about the type
1094 * of the sub-expression, so we may have to generate
1095 * the type here if so..
1098 ctype
= create_pointer(expr
, ctype
, 0);
1099 expr
->ctype
= ctype
;
1105 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1107 struct expression
*op
= expr
->unop
;
1108 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1110 /* Simplify: *&(expr) => (expr) */
1111 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1116 /* Dereferencing a node drops all the node information. */
1117 if (ctype
->type
== SYM_NODE
)
1118 ctype
= ctype
->ctype
.base_type
;
1120 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1121 target
= ctype
->ctype
.base_type
;
1123 switch (ctype
->type
) {
1125 warn(expr
->pos
, "cannot derefence this type");
1128 merge_type(node
, ctype
);
1129 if (ctype
->type
!= SYM_ARRAY
)
1132 * Dereferencing a pointer to an array results in a
1133 * degenerate dereference: the expression becomes
1134 * just a pointer to the entry, and the derefence
1139 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1140 target
->bit_size
= bits_in_pointer
;
1141 target
->ctype
.alignment
= pointer_alignment
;
1142 merge_type(target
, ctype
->ctype
.base_type
);
1147 * When an array is dereferenced, we need to pick
1148 * up the attributes of the original node too..
1150 merge_type(node
, op
->ctype
);
1151 merge_type(node
, ctype
);
1155 node
->bit_size
= target
->bit_size
;
1156 node
->array_size
= target
->array_size
;
1163 * Unary post-ops: x++ and x--
1165 static struct symbol
*evaluate_postop(struct expression
*expr
)
1167 struct expression
*op
= expr
->unop
;
1168 struct symbol
*ctype
= op
->ctype
;
1170 if (!lvalue_expression(expr
->unop
)) {
1171 warn(expr
->pos
, "need lvalue expression for ++/--");
1175 evaluate_assign_to(op
, ctype
);
1177 expr
->ctype
= ctype
;
1181 static struct symbol
*evaluate_sign(struct expression
*expr
)
1183 struct symbol
*ctype
= expr
->unop
->ctype
;
1184 if (is_int_type(ctype
)) {
1185 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1186 if (rtype
->bit_size
!= ctype
->bit_size
)
1187 expr
->unop
= cast_to(expr
->unop
, rtype
);
1189 } else if (is_float_type(ctype
) && expr
->op
!= '%') {
1190 /* no conversions needed */
1192 return bad_expr_type(expr
);
1194 if (expr
->op
== '+')
1195 *expr
= *expr
->unop
;
1196 expr
->ctype
= ctype
;
1200 static struct symbol
*evaluate_preop(struct expression
*expr
)
1202 struct symbol
*ctype
= expr
->unop
->ctype
;
1206 *expr
= *expr
->unop
;
1212 return evaluate_sign(expr
);
1215 return evaluate_dereference(expr
);
1218 return evaluate_addressof(expr
);
1220 case SPECIAL_INCREMENT
:
1221 case SPECIAL_DECREMENT
:
1223 * From a type evaluation standpoint the pre-ops are
1224 * the same as the postops
1226 return evaluate_postop(expr
);
1229 if (is_safe_type(ctype
))
1230 warn(expr
->pos
, "testing a 'safe expression'");
1231 if (is_float_type(ctype
)) {
1232 struct expression
*arg
= expr
->unop
;
1233 expr
->type
= EXPR_BINOP
;
1234 expr
->op
= SPECIAL_EQUAL
;
1236 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1237 expr
->right
->ctype
= ctype
;
1238 expr
->right
->fvalue
= 0;
1240 ctype
= &bool_ctype
;
1246 expr
->ctype
= ctype
;
1250 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1252 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1253 struct ptr_list
*list
= head
;
1259 for (i
= 0; i
< list
->nr
; i
++) {
1260 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1262 if (sym
->ident
!= ident
)
1264 *offset
= sym
->offset
;
1267 struct symbol
*ctype
= sym
->ctype
.base_type
;
1271 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1273 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1276 *offset
+= sym
->offset
;
1280 } while ((list
= list
->next
) != head
);
1284 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1286 struct expression
*add
;
1289 * Create a new add-expression
1291 * NOTE! Even if we just add zero, we need a new node
1292 * for the member pointer, since it has a different
1293 * type than the original pointer. We could make that
1294 * be just a cast, but the fact is, a node is a node,
1295 * so we might as well just do the "add zero" here.
1297 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1300 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1301 add
->right
->ctype
= &int_ctype
;
1302 add
->right
->value
= offset
;
1305 * The ctype of the pointer will be lazily evaluated if
1306 * we ever take the address of this member dereference..
1312 /* structure/union dereference */
1313 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1316 struct symbol
*ctype
, *member
;
1317 struct expression
*deref
= expr
->deref
, *add
;
1318 struct ident
*ident
= expr
->member
;
1322 if (!evaluate_expression(deref
))
1325 warn(expr
->pos
, "bad member name");
1329 ctype
= deref
->ctype
;
1330 address_space
= ctype
->ctype
.as
;
1331 mod
= ctype
->ctype
.modifiers
;
1332 if (ctype
->type
== SYM_NODE
) {
1333 ctype
= ctype
->ctype
.base_type
;
1334 address_space
|= ctype
->ctype
.as
;
1335 mod
|= ctype
->ctype
.modifiers
;
1337 if (!lvalue_expression(deref
)) {
1338 warn(deref
->pos
, "expected lvalue for member dereference");
1341 deref
= deref
->unop
;
1342 expr
->deref
= deref
;
1343 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1344 warn(expr
->pos
, "expected structure or union");
1348 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1350 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1351 const char *name
= "<unnamed>";
1354 name
= ctype
->ident
->name
;
1355 namelen
= ctype
->ident
->len
;
1357 warn(expr
->pos
, "no member '%s' in %s %.*s",
1358 show_ident(ident
), type
, namelen
, name
);
1363 * The member needs to take on the address space and modifiers of
1364 * the "parent" type.
1366 member
= convert_to_as_mod(member
, address_space
, mod
);
1367 add
= evaluate_offset(deref
, offset
);
1369 ctype
= member
->ctype
.base_type
;
1370 if (ctype
->type
== SYM_BITFIELD
) {
1371 expr
->type
= EXPR_BITFIELD
;
1372 expr
->bitpos
= member
->bit_offset
;
1373 expr
->nrbits
= member
->fieldwidth
;
1374 expr
->address
= add
;
1376 expr
->type
= EXPR_PREOP
;
1381 expr
->ctype
= member
;
1385 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1389 if (expr
->cast_type
) {
1390 examine_symbol_type(expr
->cast_type
);
1391 size
= expr
->cast_type
->bit_size
;
1393 if (!evaluate_expression(expr
->cast_expression
))
1395 size
= expr
->cast_expression
->ctype
->bit_size
;
1398 warn(expr
->pos
, "cannot size expression");
1401 expr
->type
= EXPR_VALUE
;
1402 expr
->value
= size
>> 3;
1403 expr
->ctype
= size_t_ctype
;
1404 return size_t_ctype
;
1407 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1409 struct symbol
*type
= expr
->cast_type
;
1412 type
= evaluate_expression(expr
->cast_expression
);
1416 examine_symbol_type(type
);
1417 expr
->type
= EXPR_VALUE
;
1418 expr
->value
= type
->ctype
.alignment
;
1419 expr
->ctype
= size_t_ctype
;
1420 return size_t_ctype
;
1423 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1425 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1426 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1430 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1432 struct expression
*expr
;
1433 struct symbol_list
*argument_types
= fn
->arguments
;
1434 struct symbol
*argtype
;
1437 PREPARE_PTR_LIST(argument_types
, argtype
);
1438 FOR_EACH_PTR (head
, expr
) {
1439 struct expression
**p
= THIS_ADDRESS(expr
);
1440 struct symbol
*ctype
, *target
;
1441 ctype
= evaluate_expression(expr
);
1446 if (context_clash(f
, ctype
))
1447 warn(expr
->pos
, "argument %d used in wrong context", i
);
1449 ctype
= degenerate(expr
);
1452 if (!target
&& ctype
->bit_size
< bits_in_int
)
1453 target
= &int_ctype
;
1455 static char where
[30];
1456 examine_symbol_type(target
);
1457 sprintf(where
, "argument %d", i
);
1458 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1462 NEXT_PTR_LIST(argtype
);
1464 FINISH_PTR_LIST(argtype
);
1468 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1469 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1471 struct expression
*entry
;
1475 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1476 struct expression
**p
= THIS_ADDRESS(entry
);
1478 if (entry
->type
== EXPR_INDEX
) {
1479 current
= entry
->idx_to
;
1482 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1490 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1491 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1493 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1494 warn(expr
->pos
, "unexpected compound initializer");
1497 return evaluate_array_initializer(ctype
, expr
, 0);
1500 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1502 struct expression
*entry
;
1505 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1506 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1507 struct expression
**p
= THIS_ADDRESS(entry
);
1509 if (entry
->type
== EXPR_IDENTIFIER
) {
1510 struct ident
*ident
= entry
->expr_ident
;
1511 /* We special-case the "already right place" case */
1512 if (sym
&& sym
->ident
== ident
)
1514 RESET_PTR_LIST(sym
);
1517 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1520 if (sym
->ident
== ident
)
1528 warn(expr
->pos
, "too many initializers for struct/union");
1532 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1536 FINISH_PTR_LIST(sym
);
1542 * Initializers are kind of like assignments. Except
1543 * they can be a hell of a lot more complex.
1545 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1547 struct expression
*expr
= *ep
;
1550 * Simple non-structure/array initializers are the simple
1551 * case, and look (and parse) largely like assignments.
1553 if (expr
->type
!= EXPR_INITIALIZER
) {
1555 struct symbol
*rtype
= evaluate_expression(expr
);
1557 struct expression
*pos
;
1559 // FIXME! char array[] = "string" special case
1560 // should _not_ degenerate.
1561 rtype
= degenerate(expr
);
1562 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1563 /* strings are special: char arrays */
1564 if (rtype
->type
== SYM_ARRAY
)
1565 size
= get_expression_value(rtype
->array_size
);
1567 * Don't bother creating a position expression for
1568 * the simple initializer cases that don't need it.
1570 * We need a position if the initializer has a byte
1571 * offset, _or_ if we're initializing a bitfield.
1573 if (offset
|| ctype
->fieldwidth
) {
1574 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1575 pos
->init_offset
= offset
;
1576 pos
->init_sym
= ctype
;
1577 pos
->init_expr
= *ep
;
1578 pos
->ctype
= expr
->ctype
;
1585 expr
->ctype
= ctype
;
1586 if (ctype
->type
== SYM_NODE
)
1587 ctype
= ctype
->ctype
.base_type
;
1589 switch (ctype
->type
) {
1592 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1594 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1596 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1598 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1602 static int get_as(struct symbol
*sym
)
1610 mod
= sym
->ctype
.modifiers
;
1611 if (sym
->type
== SYM_NODE
) {
1612 sym
= sym
->ctype
.base_type
;
1613 as
|= sym
->ctype
.as
;
1614 mod
|= sym
->ctype
.modifiers
;
1617 * You can always throw a value away by casting to
1618 * "void" - that's an implicit "force". Note that
1619 * the same is _not_ true of "void *".
1621 if (sym
== &void_ctype
)
1625 * At least for now, allow casting to a "unsigned long".
1626 * That's how we do things like pointer arithmetic and
1627 * store pointers to registers.
1629 if (sym
== &ulong_ctype
)
1632 if (sym
&& sym
->type
== SYM_PTR
) {
1633 sym
= sym
->ctype
.base_type
;
1634 as
|= sym
->ctype
.as
;
1635 mod
|= sym
->ctype
.modifiers
;
1637 if (mod
& MOD_FORCE
)
1642 static struct symbol
*evaluate_cast(struct expression
*expr
)
1644 struct expression
*target
= expr
->cast_expression
;
1645 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1647 expr
->ctype
= ctype
;
1648 expr
->cast_type
= ctype
;
1651 * Special case: a cast can be followed by an
1652 * initializer, in which case we need to pass
1653 * the type value down to that initializer rather
1654 * than trying to evaluate it as an expression
1656 * A more complex case is when the initializer is
1657 * dereferenced as part of a post-fix expression.
1658 * We need to produce an expression that can be dereferenced.
1660 if (target
->type
== EXPR_INITIALIZER
) {
1661 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1662 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1664 sym
->ctype
.base_type
= ctype
;
1665 sym
->initializer
= expr
->cast_expression
;
1666 evaluate_symbol(sym
);
1668 addr
->ctype
= NULL
; /* Lazy eval */
1671 expr
->type
= EXPR_PREOP
;
1674 expr
->ctype
= ctype
;
1678 evaluate_expression(target
);
1681 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1682 warn(expr
->pos
, "cast removes address space of expression");
1685 * Casts of constant values are special: they
1686 * can be NULL, and thus need to be simplified
1689 if (target
->type
== EXPR_VALUE
)
1690 cast_value(expr
, ctype
, target
, target
->ctype
);
1696 * Evaluate a call expression with a symbol. This
1697 * should expand inline functions, and evaluate
1700 static int evaluate_symbol_call(struct expression
*expr
)
1702 struct expression
*fn
= expr
->fn
;
1703 struct symbol
*ctype
= fn
->ctype
;
1705 if (fn
->type
!= EXPR_PREOP
)
1708 if (ctype
->op
&& ctype
->op
->evaluate
)
1709 return ctype
->op
->evaluate(expr
);
1711 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1713 struct symbol
*curr
= current_fn
;
1714 unsigned long context
= current_context
;
1715 unsigned long mask
= current_contextmask
;
1717 current_context
|= ctype
->ctype
.context
;
1718 current_contextmask
|= ctype
->ctype
.contextmask
;
1719 current_fn
= ctype
->ctype
.base_type
;
1720 examine_fn_arguments(current_fn
);
1722 ret
= inline_function(expr
, ctype
);
1724 /* restore the old function context */
1726 current_context
= context
;
1727 current_contextmask
= mask
;
1734 static struct symbol
*evaluate_call(struct expression
*expr
)
1737 struct symbol
*ctype
, *sym
;
1738 struct expression
*fn
= expr
->fn
;
1739 struct expression_list
*arglist
= expr
->args
;
1741 if (!evaluate_expression(fn
))
1743 sym
= ctype
= fn
->ctype
;
1744 if (ctype
->type
== SYM_NODE
)
1745 ctype
= ctype
->ctype
.base_type
;
1746 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1747 ctype
= ctype
->ctype
.base_type
;
1748 if (!evaluate_arguments(sym
, ctype
, arglist
))
1750 if (ctype
->type
!= SYM_FN
) {
1751 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1754 args
= expression_list_size(expr
->args
);
1755 fnargs
= symbol_list_size(ctype
->arguments
);
1757 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1758 if (args
> fnargs
&& !ctype
->variadic
)
1759 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1760 if (sym
->type
== SYM_NODE
) {
1761 if (evaluate_symbol_call(expr
))
1764 expr
->ctype
= ctype
->ctype
.base_type
;
1768 struct symbol
*evaluate_expression(struct expression
*expr
)
1775 switch (expr
->type
) {
1778 warn(expr
->pos
, "value expression without a type");
1781 return evaluate_string(expr
);
1783 return evaluate_symbol_expression(expr
);
1785 if (!evaluate_expression(expr
->left
))
1787 if (!evaluate_expression(expr
->right
))
1789 return evaluate_binop(expr
);
1791 return evaluate_logical(expr
);
1793 if (!evaluate_expression(expr
->left
))
1795 if (!evaluate_expression(expr
->right
))
1797 return evaluate_comma(expr
);
1799 if (!evaluate_expression(expr
->left
))
1801 if (!evaluate_expression(expr
->right
))
1803 return evaluate_compare(expr
);
1804 case EXPR_ASSIGNMENT
:
1805 if (!evaluate_expression(expr
->left
))
1807 if (!evaluate_expression(expr
->right
))
1809 return evaluate_assignment(expr
);
1811 if (!evaluate_expression(expr
->unop
))
1813 return evaluate_preop(expr
);
1815 if (!evaluate_expression(expr
->unop
))
1817 return evaluate_postop(expr
);
1819 return evaluate_cast(expr
);
1821 return evaluate_sizeof(expr
);
1823 return evaluate_alignof(expr
);
1825 return evaluate_member_dereference(expr
);
1827 return evaluate_call(expr
);
1829 warn(expr
->pos
, "bitfield generated by parser");
1831 case EXPR_CONDITIONAL
:
1832 if (!evaluate_conditional(&expr
->conditional
))
1834 if (!evaluate_expression(expr
->cond_false
))
1836 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1838 return evaluate_conditional_expression(expr
);
1839 case EXPR_STATEMENT
:
1840 expr
->ctype
= evaluate_statement(expr
->statement
);
1844 expr
->ctype
= &ptr_ctype
;
1848 /* Evaluate the type of the symbol .. */
1849 evaluate_symbol(expr
->symbol
);
1850 /* .. but the type of the _expression_ is a "type" */
1851 expr
->ctype
= &type_ctype
;
1854 /* These can not exist as stand-alone expressions */
1855 case EXPR_INITIALIZER
:
1856 case EXPR_IDENTIFIER
:
1859 warn(expr
->pos
, "internal front-end error: initializer in expression");
1865 void check_duplicates(struct symbol
*sym
)
1867 struct symbol
*next
= sym
;
1869 while ((next
= next
->same_symbol
) != NULL
) {
1870 const char *typediff
;
1871 evaluate_symbol(next
);
1872 typediff
= type_difference(sym
, next
, 0, 0);
1874 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1875 show_ident(sym
->ident
),
1876 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1882 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1884 struct symbol
*base_type
;
1889 sym
= examine_symbol_type(sym
);
1890 base_type
= sym
->ctype
.base_type
;
1894 /* Evaluate the initializers */
1895 if (sym
->initializer
) {
1896 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1897 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1898 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1899 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1900 base_type
->bit_size
= bit_size
;
1901 sym
->array_size
= base_type
->array_size
;
1902 sym
->bit_size
= bit_size
;
1906 /* And finally, evaluate the body of the symbol too */
1907 if (base_type
->type
== SYM_FN
) {
1908 examine_fn_arguments(base_type
);
1909 if (base_type
->stmt
) {
1910 current_fn
= base_type
;
1911 current_contextmask
= sym
->ctype
.contextmask
;
1912 current_context
= sym
->ctype
.context
;
1913 evaluate_statement(base_type
->stmt
);
1920 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1922 struct expression
*expr
= stmt
->expression
;
1923 struct symbol
*ctype
, *fntype
;
1925 evaluate_expression(expr
);
1926 ctype
= degenerate(expr
);
1927 fntype
= current_fn
->ctype
.base_type
;
1928 if (!fntype
|| fntype
== &void_ctype
) {
1929 if (expr
&& ctype
!= &void_ctype
)
1930 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1935 warn(stmt
->pos
, "return with no return value");
1940 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
1944 static void evaluate_if_statement(struct statement
*stmt
)
1946 struct symbol
*ctype
;
1948 if (!stmt
->if_conditional
)
1951 ctype
= evaluate_conditional(&stmt
->if_conditional
);
1955 evaluate_statement(stmt
->if_true
);
1956 evaluate_statement(stmt
->if_false
);
1959 struct symbol
*evaluate_statement(struct statement
*stmt
)
1964 switch (stmt
->type
) {
1966 return evaluate_return_expression(stmt
);
1968 case STMT_EXPRESSION
:
1969 evaluate_expression(stmt
->expression
);
1970 return degenerate(stmt
->expression
);
1972 case STMT_COMPOUND
: {
1973 struct statement
*s
;
1974 struct symbol
*type
= NULL
;
1977 /* Evaluate each symbol in the compound statement */
1978 FOR_EACH_PTR(stmt
->syms
, sym
) {
1979 evaluate_symbol(sym
);
1981 evaluate_symbol(stmt
->ret
);
1984 * Then, evaluate each statement, making the type of the
1985 * compound statement be the type of the last statement
1988 FOR_EACH_PTR(stmt
->stmts
, s
) {
1989 type
= evaluate_statement(s
);
1996 evaluate_if_statement(stmt
);
1999 evaluate_conditional(&stmt
->iterator_pre_condition
);
2000 evaluate_conditional(&stmt
->iterator_post_condition
);
2001 evaluate_statement(stmt
->iterator_pre_statement
);
2002 evaluate_statement(stmt
->iterator_statement
);
2003 evaluate_statement(stmt
->iterator_post_statement
);
2006 evaluate_expression(stmt
->switch_expression
);
2007 evaluate_statement(stmt
->switch_statement
);
2010 evaluate_expression(stmt
->case_expression
);
2011 evaluate_expression(stmt
->case_to
);
2012 evaluate_statement(stmt
->case_statement
);
2015 return evaluate_statement(stmt
->label_statement
);
2017 evaluate_expression(stmt
->goto_expression
);
2022 /* FIXME! Do the asm parameter evaluation! */