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
*evaluate_symbol_expression(struct expression
*expr
)
33 struct symbol
*sym
= expr
->symbol
;
34 struct symbol
*base_type
;
38 expr
->ctype
= &int_ctype
;
41 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
46 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
47 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
49 base_type
= sym
->ctype
.base_type
;
51 warn(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
55 /* The type of a symbol is the symbol itself! */
58 /* enum's can be turned into plain values */
59 if (sym
->type
!= SYM_ENUM
) {
60 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
62 addr
->symbol_name
= expr
->symbol_name
;
63 addr
->ctype
= &ptr_ctype
;
64 expr
->type
= EXPR_PREOP
;
69 expr
->type
= EXPR_VALUE
;
70 expr
->value
= sym
->value
;
71 expr
->ctype
= base_type
;
75 static struct symbol
*evaluate_string(struct expression
*expr
)
77 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
78 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
79 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
80 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
81 unsigned int length
= expr
->string
->length
;
83 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
84 sym
->bit_size
= BITS_IN_CHAR
* length
;
85 sym
->ctype
.alignment
= 1;
86 sym
->ctype
.modifiers
= MOD_STATIC
;
87 sym
->ctype
.base_type
= array
;
88 sym
->initializer
= initstr
;
91 initstr
->string
= expr
->string
;
93 array
->array_size
= sym
->array_size
;
94 array
->bit_size
= BITS_IN_CHAR
* length
;
95 array
->ctype
.alignment
= 1;
96 array
->ctype
.modifiers
= MOD_STATIC
;
97 array
->ctype
.base_type
= &char_ctype
;
100 addr
->ctype
= &ptr_ctype
;
102 expr
->type
= EXPR_PREOP
;
109 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
111 unsigned long lmod
, rmod
, mod
;
116 if (left
->bit_size
> right
->bit_size
)
119 if (right
->bit_size
> left
->bit_size
)
122 /* Same size integers - promote to unsigned, promote to long */
123 lmod
= left
->ctype
.modifiers
;
124 rmod
= right
->ctype
.modifiers
;
130 return ctype_integer(mod
);
133 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
135 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
137 expr
->cast_type
= type
;
138 expr
->cast_expression
= old
;
142 static int is_type_type(struct symbol
*type
)
144 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
147 static int is_ptr_type(struct symbol
*type
)
149 if (type
->type
== SYM_NODE
)
150 type
= type
->ctype
.base_type
;
151 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
154 static int is_int_type(struct symbol
*type
)
156 if (type
->type
== SYM_NODE
)
157 type
= type
->ctype
.base_type
;
158 return type
->ctype
.base_type
== &int_type
;
161 static struct symbol
*bad_expr_type(struct expression
*expr
)
163 warn(expr
->pos
, "incompatible types for operation");
167 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
169 struct expression
*left
= *lp
, *right
= *rp
;
170 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
172 if (ltype
->type
== SYM_NODE
)
173 ltype
= ltype
->ctype
.base_type
;
174 if (rtype
->type
== SYM_NODE
)
175 rtype
= rtype
->ctype
.base_type
;
176 /* Integer promotion? */
177 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
179 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
181 if (is_int_type(ltype
) && is_int_type(rtype
)) {
182 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
184 /* Don't bother promoting same-size entities, it only adds clutter */
185 if (ltype
->bit_size
!= ctype
->bit_size
)
186 *lp
= cast_to(left
, ctype
);
187 if (rtype
->bit_size
!= ctype
->bit_size
)
188 *rp
= cast_to(right
, ctype
);
194 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
196 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
201 return bad_expr_type(expr
);
204 static inline int lvalue_expression(struct expression
*expr
)
206 while (expr
->type
== EXPR_CAST
)
207 expr
= expr
->cast_expression
;
208 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
211 /* Arrays degenerate into pointers on pointer arithmetic */
212 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
214 struct symbol
*base
= ctype
;
216 if (ctype
->type
== SYM_NODE
)
217 base
= ctype
->ctype
.base_type
;
218 if (base
->type
== SYM_ARRAY
|| base
->type
== SYM_FN
) {
219 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
220 struct expression
*n
= alloc_expression(expr
->pos
, 0);
221 struct expression
*ptr
;
223 merge_type(sym
, ctype
);
224 if (base
->type
== SYM_FN
)
226 merge_type(sym
, base
);
227 sym
->bit_size
= BITS_IN_POINTER
;
236 * This all really assumes that we got the degenerate
237 * array as an lvalue (ie a dereference). If that
238 * is not the case, then holler - because we've screwed
241 if (!lvalue_expression(ptr
))
242 warn(ptr
->pos
, "internal error: strange degenerate array case");
247 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
249 struct symbol
*ctype
;
250 struct symbol
*ptr_type
= ptr
->ctype
;
251 struct symbol
*i_type
= i
->ctype
;
254 if (i_type
->type
== SYM_NODE
)
255 i_type
= i_type
->ctype
.base_type
;
256 if (ptr_type
->type
== SYM_NODE
)
257 ptr_type
= ptr_type
->ctype
.base_type
;
259 if (i_type
->type
== SYM_ENUM
)
261 if (!is_int_type(i_type
))
262 return bad_expr_type(expr
);
265 examine_symbol_type(ctype
);
267 ctype
= degenerate(expr
, ctype
, &ptr
);
268 bit_size
= ctype
->ctype
.base_type
->bit_size
;
270 /* Special case: adding zero commonly happens as a result of 'array[0]' */
271 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
273 } else if (bit_size
> BITS_IN_CHAR
) {
274 struct expression
*add
= expr
;
275 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
276 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
278 val
->ctype
= size_t_ctype
;
279 val
->value
= bit_size
>> 3;
282 mul
->ctype
= size_t_ctype
;
286 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
295 static struct symbol
*evaluate_add(struct expression
*expr
)
297 struct expression
*left
= expr
->left
, *right
= expr
->right
;
298 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
300 if (is_ptr_type(ltype
))
301 return evaluate_ptr_add(expr
, left
, right
);
303 if (is_ptr_type(rtype
))
304 return evaluate_ptr_add(expr
, right
, left
);
306 // FIXME! FP promotion
307 return evaluate_int_binop(expr
);
310 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
311 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
313 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
314 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
317 unsigned long mod1
, mod2
, diff
;
318 unsigned long as1
, as2
;
320 struct symbol
*base1
, *base2
;
322 if (target
== source
)
324 if (!target
|| !source
)
325 return "different types";
327 * Peel of per-node information.
328 * FIXME! Check alignment, address space, and context too here!
330 if (target
->type
== SYM_NODE
)
331 target
= target
->ctype
.base_type
;
332 if (source
->type
== SYM_NODE
)
333 source
= source
->ctype
.base_type
;
335 if (target
== source
)
337 if (!target
|| !source
)
338 return "different types";
340 mod1
= target
->ctype
.modifiers
;
341 as1
= target
->ctype
.as
;
342 mod2
= source
->ctype
.modifiers
;
343 as2
= source
->ctype
.as
;
345 type1
= target
->type
;
346 base1
= target
->ctype
.base_type
;
348 type2
= source
->type
;
349 base2
= source
->ctype
.base_type
;
352 * Pointers to arrays degenerate to pointers to the array entry.
353 * Pointers to functions compare as the function itself
355 if (type1
== SYM_PTR
&& base1
) {
356 switch (base1
->type
) {
362 base1
= base1
->ctype
.base_type
;
367 if (type2
== SYM_PTR
&& base2
) {
368 switch (base2
->type
) {
374 base2
= base2
->ctype
.base_type
;
380 /* Arrays degenerate to pointers for type comparisons */
381 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
382 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
385 return "different base types";
387 /* Must be same address space to be comparable */
389 return "different address spaces";
391 /* Ignore differences in storage types, sign, or addressability */
392 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
394 mod1
&= diff
& ~target_mod_ignore
;
395 mod2
&= diff
& ~source_mod_ignore
;
397 if ((mod1
| mod2
) & MOD_SIZE
)
398 return "different type sizes";
399 return "different modifiers";
403 if (type1
== SYM_FN
) {
405 struct symbol
*arg1
, *arg2
;
406 if (base1
->variadic
!= base2
->variadic
)
407 return "incompatible variadic arguments";
408 PREPARE_PTR_LIST(target
->arguments
, arg1
);
409 PREPARE_PTR_LIST(source
->arguments
, arg2
);
413 diff
= type_difference(arg1
, arg2
, 0, 0);
415 static char argdiff
[80];
416 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
425 FINISH_PTR_LIST(arg2
);
426 FINISH_PTR_LIST(arg1
);
435 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
437 /* NULL expression? Just return the type of the "other side" */
438 if (r
->type
== EXPR_VALUE
&& !r
->value
)
440 if (l
->type
== EXPR_VALUE
&& !l
->value
)
446 * Ignore differences in "volatile" and "const"ness when
447 * subtracting pointers
449 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
451 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
453 const char *typediff
;
454 struct symbol
*ctype
;
455 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
458 * If it is an integer subtract: the ptr add case will do the
461 if (!is_ptr_type(rtype
))
462 return evaluate_ptr_add(expr
, l
, r
);
465 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
467 ctype
= common_ptr_type(l
, r
);
469 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
473 examine_symbol_type(ctype
);
475 /* Figure out the base type we point to */
476 if (ctype
->type
== SYM_NODE
)
477 ctype
= ctype
->ctype
.base_type
;
478 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
479 warn(expr
->pos
, "subtraction of functions? Share your drugs");
482 ctype
= ctype
->ctype
.base_type
;
484 expr
->ctype
= ssize_t_ctype
;
485 if (ctype
->bit_size
> BITS_IN_CHAR
) {
486 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
487 struct expression
*div
= expr
;
488 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
490 val
->ctype
= size_t_ctype
;
491 val
->value
= ctype
->bit_size
>> 3;
494 sub
->ctype
= ssize_t_ctype
;
503 return ssize_t_ctype
;
506 static struct symbol
*evaluate_sub(struct expression
*expr
)
508 struct expression
*left
= expr
->left
, *right
= expr
->right
;
509 struct symbol
*ltype
= left
->ctype
;
511 if (is_ptr_type(ltype
))
512 return evaluate_ptr_sub(expr
, left
, right
);
514 // FIXME! FP promotion
515 return evaluate_int_binop(expr
);
518 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
520 static struct symbol
*evaluate_conditional(struct expression
*expr
)
522 struct symbol
*ctype
;
527 if (expr
->type
== EXPR_ASSIGNMENT
)
528 warn(expr
->pos
, "assignment expression in conditional");
530 ctype
= evaluate_expression(expr
);
531 if (ctype
&& is_safe_type(ctype
))
532 warn(expr
->pos
, "testing a 'safe expression'");
537 static struct symbol
*evaluate_logical(struct expression
*expr
)
539 if (!evaluate_conditional(expr
->left
))
541 if (!evaluate_conditional(expr
->right
))
544 expr
->ctype
= &bool_ctype
;
548 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
550 // FIXME! Floating-point promotion!
551 return evaluate_int_binop(expr
);
554 static struct symbol
*evaluate_binop(struct expression
*expr
)
557 // addition can take ptr+int, fp and int
559 return evaluate_add(expr
);
561 // subtraction can take ptr-ptr, fp and int
563 return evaluate_sub(expr
);
565 // Arithmetic operations can take fp and int
566 case '*': case '/': case '%':
567 return evaluate_arithmetic(expr
);
569 // The rest are integer operations (bitops)
570 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
573 return evaluate_int_binop(expr
);
577 static struct symbol
*evaluate_comma(struct expression
*expr
)
579 expr
->ctype
= expr
->right
->ctype
;
583 static struct symbol
*evaluate_compare(struct expression
*expr
)
585 struct expression
*left
= expr
->left
, *right
= expr
->right
;
586 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
587 struct symbol
*ctype
;
590 if (is_type_type(ltype
) && is_type_type(rtype
)) {
591 expr
->ctype
= &bool_ctype
;
595 if (is_safe_type(ltype
) || is_safe_type(rtype
))
596 warn(expr
->pos
, "testing a 'safe expression'");
599 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
600 expr
->ctype
= &bool_ctype
;
601 // FIXME! Check the types for compatibility
605 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
607 expr
->ctype
= &bool_ctype
;
611 return bad_expr_type(expr
);
614 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
616 /* Integer promotion? */
617 if (ltype
->type
== SYM_NODE
)
618 ltype
= ltype
->ctype
.base_type
;
619 if (rtype
->type
== SYM_NODE
)
620 rtype
= rtype
->ctype
.base_type
;
621 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
623 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
625 return (is_int_type(ltype
) && is_int_type(rtype
));
628 static int is_null_ptr(struct expression
*expr
)
630 return (expr
->type
== EXPR_VALUE
&&
635 * FIXME!! This should do casts, array degeneration etc..
637 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
639 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
641 if (ltype
->type
== SYM_NODE
)
642 ltype
= ltype
->ctype
.base_type
;
644 if (ltype
->type
== SYM_PTR
) {
645 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
649 if (rtype
->type
== SYM_NODE
)
650 rtype
= rtype
->ctype
.base_type
;
652 if (rtype
->type
== SYM_PTR
) {
653 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
659 static struct symbol
*do_degenerate(struct expression
**ep
)
661 struct expression
*expr
= *ep
;
662 return degenerate(expr
, expr
->ctype
, ep
);
665 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
667 struct expression
*cond
, *true, *false;
668 struct symbol
*ctype
, *ltype
, *rtype
;
669 const char * typediff
;
671 ctype
= do_degenerate(&expr
->conditional
);
672 cond
= expr
->conditional
;
676 if (expr
->cond_true
) {
677 ltype
= do_degenerate(&expr
->cond_true
);
678 true = expr
->cond_true
;
681 rtype
= do_degenerate(&expr
->cond_false
);
682 false = expr
->cond_false
;
685 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
687 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
689 ctype
= compatible_ptr_type(true, expr
->cond_false
);
691 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
701 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
702 struct expression
**rp
, struct symbol
*source
, const char *where
)
704 const char *typediff
;
708 /* It's ok if the target is more volatile or const than the source */
709 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
713 if (compatible_integer_types(target
, source
)) {
714 if (target
->bit_size
!= source
->bit_size
)
715 *rp
= cast_to(*rp
, target
);
719 /* Pointer destination? */
721 target_as
= t
->ctype
.as
;
722 if (t
->type
== SYM_NODE
) {
723 t
= t
->ctype
.base_type
;
724 target_as
|= t
->ctype
.as
;
726 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
) {
727 struct expression
*right
= *rp
;
728 struct symbol
*s
= source
;
731 // NULL pointer is always ok
732 if (right
->type
== EXPR_VALUE
&& !right
->value
)
735 /* "void *" matches anything as long as the address space is ok */
736 source_as
= s
->ctype
.as
;
737 if (s
->type
== SYM_NODE
) {
738 s
= s
->ctype
.base_type
;
739 source_as
|= s
->ctype
.as
;
741 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
742 s
= s
->ctype
.base_type
;
743 t
= t
->ctype
.base_type
;
744 if (s
== &void_ctype
|| t
== &void_ctype
)
750 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
751 warn(expr
->pos
, " expected %s", show_typename(target
));
752 warn(expr
->pos
, " got %s", show_typename(source
));
757 * FIXME!! This is wrong from a double evaluation standpoint. We can't
758 * just expand the expression twice, that would make any side effects
761 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
764 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
765 static const int op_trans
[] = {
766 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
767 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
768 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
769 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
770 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
771 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
772 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
773 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
774 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
775 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
778 subexpr
->left
= left
;
779 subexpr
->right
= right
;
780 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
782 expr
->right
= subexpr
;
783 return evaluate_binop(subexpr
);
786 static struct symbol
*evaluate_assignment(struct expression
*expr
)
788 struct expression
*left
= expr
->left
, *right
= expr
->right
;
789 struct symbol
*ltype
, *rtype
;
792 rtype
= right
->ctype
;
793 if (expr
->op
!= '=') {
794 rtype
= evaluate_binop_assignment(expr
, left
, right
);
800 if (!lvalue_expression(left
)) {
801 warn(expr
->pos
, "not an lvalue");
805 rtype
= degenerate(right
, rtype
, &expr
->right
);
807 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
810 if (ltype
->type
== SYM_NODE
)
811 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
817 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
)
819 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
821 ptr
->ctype
.base_type
= sym
;
822 ptr
->bit_size
= BITS_IN_POINTER
;
824 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
825 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
826 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
827 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
829 if (sym
->type
== SYM_NODE
)
830 sym
= sym
->ctype
.base_type
;
831 if (sym
->type
== SYM_ARRAY
)
832 sym
= sym
->ctype
.base_type
;
833 ptr
->ctype
.base_type
= sym
;
838 static struct symbol
*evaluate_addressof(struct expression
*expr
)
840 struct expression
*op
= expr
->unop
;
841 struct symbol
*ctype
;
843 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
844 warn(expr
->pos
, "not addressable");
848 ctype
= create_pointer(expr
, op
->ctype
);
857 static struct symbol
*evaluate_dereference(struct expression
*expr
)
859 struct expression
*op
= expr
->unop
;
860 struct symbol
*ctype
= op
->ctype
, *sym
;
862 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
863 sym
->ctype
= ctype
->ctype
;
864 if (ctype
->type
== SYM_NODE
) {
865 ctype
= ctype
->ctype
.base_type
;
866 merge_type(sym
, ctype
);
868 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
869 warn(expr
->pos
, "cannot derefence this type");
873 ctype
= ctype
->ctype
.base_type
;
874 examine_symbol_type(ctype
);
876 warn(expr
->pos
, "undefined type");
880 sym
->bit_size
= ctype
->bit_size
;
881 sym
->array_size
= ctype
->array_size
;
883 /* Simplify: *&(expr) => (expr) */
884 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
893 * Unary post-ops: x++ and x--
895 static struct symbol
*evaluate_postop(struct expression
*expr
)
897 struct expression
*op
= expr
->unop
;
898 struct symbol
*ctype
= op
->ctype
;
900 if (!lvalue_expression(expr
->unop
)) {
901 warn(expr
->pos
, "need lvalue expression for ++/--");
905 if (ctype
->type
== SYM_NODE
)
906 ctype
->ctype
.modifiers
|= MOD_ASSIGNED
;
912 static struct symbol
*evaluate_preop(struct expression
*expr
)
914 struct symbol
*ctype
= expr
->unop
->ctype
;
923 return evaluate_dereference(expr
);
926 return evaluate_addressof(expr
);
928 case SPECIAL_INCREMENT
:
929 case SPECIAL_DECREMENT
:
931 * From a type evaluation standpoint the pre-ops are
932 * the same as the postops
934 return evaluate_postop(expr
);
937 if (is_safe_type(ctype
))
938 warn(expr
->pos
, "testing a 'safe expression'");
949 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
951 struct ptr_list
*head
= (struct ptr_list
*)_list
;
952 struct ptr_list
*list
= head
;
958 for (i
= 0; i
< list
->nr
; i
++) {
959 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
961 if (sym
->ident
!= ident
)
963 *offset
= sym
->offset
;
966 struct symbol
*ctype
= sym
->ctype
.base_type
;
970 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
972 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
975 *offset
+= sym
->offset
;
979 } while ((list
= list
->next
) != head
);
983 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
, struct symbol
*ctype
)
985 struct expression
*add
;
989 /* Create a new add-expression */
990 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
993 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
994 add
->right
->ctype
= &int_ctype
;
995 add
->right
->value
= offset
;
1002 /* structure/union dereference */
1003 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1006 struct symbol
*ctype
, *member
;
1007 struct expression
*deref
= expr
->deref
, *add
;
1008 struct ident
*ident
= expr
->member
;
1012 if (!evaluate_expression(deref
))
1015 warn(expr
->pos
, "bad member name");
1019 ctype
= deref
->ctype
;
1020 address_space
= ctype
->ctype
.as
;
1021 mod
= ctype
->ctype
.modifiers
;
1022 if (ctype
->type
== SYM_NODE
) {
1023 ctype
= ctype
->ctype
.base_type
;
1024 address_space
|= ctype
->ctype
.as
;
1025 mod
|= ctype
->ctype
.modifiers
;
1027 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1028 /* Arrays will degenerate into pointers for '->' */
1029 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1030 warn(expr
->pos
, "expected a pointer to a struct/union");
1033 mod
= ctype
->ctype
.modifiers
;
1034 address_space
= ctype
->ctype
.as
;
1035 ctype
= ctype
->ctype
.base_type
;
1036 if (ctype
->type
== SYM_NODE
) {
1037 mod
|= ctype
->ctype
.modifiers
;
1038 address_space
|= ctype
->ctype
.as
;
1039 ctype
= ctype
->ctype
.base_type
;
1042 if (!lvalue_expression(deref
)) {
1043 warn(deref
->pos
, "expected lvalue for member dereference");
1046 deref
= deref
->unop
;
1047 expr
->deref
= deref
;
1049 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1050 warn(expr
->pos
, "expected structure or union");
1054 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1056 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1057 const char *name
= "<unnamed>";
1060 name
= ctype
->ident
->name
;
1061 namelen
= ctype
->ident
->len
;
1063 warn(expr
->pos
, "no member '%s' in %s %.*s",
1064 show_ident(ident
), type
, namelen
, name
);
1068 ctype
= create_pointer(deref
, member
);
1069 ctype
->ctype
.modifiers
= mod
;
1070 ctype
->ctype
.as
= address_space
;
1071 add
= evaluate_offset(deref
, offset
, ctype
);
1073 ctype
= member
->ctype
.base_type
;
1074 if (ctype
->type
== SYM_BITFIELD
) {
1075 ctype
= ctype
->ctype
.base_type
;
1076 expr
->type
= EXPR_BITFIELD
;
1077 expr
->bitpos
= member
->bit_offset
;
1078 expr
->nrbits
= member
->fieldwidth
;
1079 expr
->address
= add
;
1081 expr
->type
= EXPR_PREOP
;
1086 expr
->ctype
= member
;
1090 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1094 if (expr
->cast_type
) {
1095 examine_symbol_type(expr
->cast_type
);
1096 size
= expr
->cast_type
->bit_size
;
1098 if (!evaluate_expression(expr
->cast_expression
))
1100 size
= expr
->cast_expression
->ctype
->bit_size
;
1103 warn(expr
->pos
, "cannot size expression");
1106 expr
->type
= EXPR_VALUE
;
1107 expr
->value
= size
>> 3;
1108 expr
->ctype
= size_t_ctype
;
1109 return size_t_ctype
;
1112 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1114 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1115 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1119 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1121 struct expression
*expr
;
1122 struct symbol_list
*argument_types
= fn
->arguments
;
1123 struct symbol
*argtype
;
1126 PREPARE_PTR_LIST(argument_types
, argtype
);
1127 FOR_EACH_PTR (head
, expr
) {
1128 struct expression
**p
= THIS_ADDRESS(expr
);
1129 struct symbol
*ctype
, *target
;
1130 ctype
= evaluate_expression(expr
);
1135 if (context_clash(f
, ctype
))
1136 warn(expr
->pos
, "argument %d used in wrong context", i
);
1138 ctype
= degenerate(expr
, ctype
, p
);
1141 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1142 target
= &int_ctype
;
1144 static char where
[30];
1145 examine_symbol_type(target
);
1146 sprintf(where
, "argument %d", i
);
1147 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1151 NEXT_PTR_LIST(argtype
);
1153 FINISH_PTR_LIST(argtype
);
1157 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1158 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1160 struct expression
*entry
;
1164 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1165 struct expression
**p
= THIS_ADDRESS(entry
);
1167 if (entry
->type
== EXPR_INDEX
) {
1168 current
= entry
->idx_to
;
1171 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1179 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1181 struct expression
*entry
;
1184 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1185 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1186 struct expression
**p
= THIS_ADDRESS(entry
);
1188 if (entry
->type
== EXPR_IDENTIFIER
) {
1189 struct ident
*ident
= entry
->expr_ident
;
1190 /* We special-case the "already right place" case */
1191 if (sym
&& sym
->ident
== ident
)
1193 RESET_PTR_LIST(sym
);
1196 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1199 if (sym
->ident
== ident
)
1207 warn(expr
->pos
, "too many initializers for struct/union");
1211 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1215 FINISH_PTR_LIST(sym
);
1221 * Initializers are kind of like assignments. Except
1222 * they can be a hell of a lot more complex.
1224 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1226 struct expression
*expr
= *ep
;
1229 * Simple non-structure/array initializers are the simple
1230 * case, and look (and parse) largely like assignments.
1232 if (expr
->type
!= EXPR_INITIALIZER
) {
1234 struct symbol
*rtype
= evaluate_expression(expr
);
1236 struct expression
*pos
;
1238 // FIXME! char array[] = "string" special case
1239 // should _not_ degenerate.
1240 rtype
= degenerate(expr
, rtype
, ep
);
1242 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1243 /* strings are special: char arrays */
1244 if (rtype
->type
== SYM_ARRAY
)
1245 size
= get_expression_value(rtype
->array_size
);
1247 * Don't bother creating a position expression for
1248 * the simple initializer cases that don't need it.
1250 * We need a position if the initializer has a byte
1251 * offset, _or_ if we're initializing a bitfield.
1253 if (offset
|| ctype
->fieldwidth
) {
1254 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1255 pos
->init_offset
= offset
;
1256 pos
->init_sym
= ctype
;
1257 pos
->init_expr
= *ep
;
1258 pos
->ctype
= expr
->ctype
;
1265 expr
->ctype
= ctype
;
1266 if (ctype
->type
== SYM_NODE
)
1267 ctype
= ctype
->ctype
.base_type
;
1269 switch (ctype
->type
) {
1272 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1274 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1276 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1280 warn(expr
->pos
, "unexpected compound initializer");
1284 static struct symbol
*evaluate_cast(struct expression
*expr
)
1286 struct expression
*target
= expr
->cast_expression
;
1287 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1289 expr
->ctype
= ctype
;
1290 expr
->cast_type
= ctype
;
1293 * Special case: a cast can be followed by an
1294 * initializer, in which case we need to pass
1295 * the type value down to that initializer rather
1296 * than trying to evaluate it as an expression
1298 * A more complex case is when the initializer is
1299 * dereferenced as part of a post-fix expression.
1300 * We need to produce an expression that can be dereferenced.
1302 if (target
->type
== EXPR_INITIALIZER
) {
1303 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1304 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1306 sym
->ctype
.base_type
= ctype
;
1307 sym
->initializer
= expr
->cast_expression
;
1308 evaluate_symbol(sym
);
1310 addr
->ctype
= &ptr_ctype
;
1313 expr
->type
= EXPR_PREOP
;
1316 expr
->ctype
= ctype
;
1320 evaluate_expression(target
);
1323 * Casts of constant values are special: they
1324 * can be NULL, and thus need to be simplified
1327 if (target
->type
== EXPR_VALUE
)
1328 cast_value(expr
, ctype
, target
, target
->ctype
);
1334 * Evaluate a call expression with a symbol. This
1335 * should expand inline functions, and evaluate
1338 static int evaluate_symbol_call(struct expression
*expr
)
1340 struct expression
*fn
= expr
->fn
;
1341 struct symbol
*ctype
= fn
->ctype
;
1343 if (fn
->type
!= EXPR_PREOP
)
1346 if (ctype
->op
&& ctype
->op
->evaluate
)
1347 return ctype
->op
->evaluate(expr
);
1349 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1351 struct symbol
*curr
= current_fn
;
1352 unsigned long context
= current_context
;
1353 unsigned long mask
= current_contextmask
;
1355 current_context
|= ctype
->ctype
.context
;
1356 current_contextmask
|= ctype
->ctype
.contextmask
;
1357 current_fn
= ctype
->ctype
.base_type
;
1358 ret
= inline_function(expr
, ctype
);
1360 /* restore the old function context */
1362 current_context
= context
;
1363 current_contextmask
= mask
;
1370 static struct symbol
*evaluate_call(struct expression
*expr
)
1373 struct symbol
*ctype
, *sym
;
1374 struct expression
*fn
= expr
->fn
;
1375 struct expression_list
*arglist
= expr
->args
;
1377 if (!evaluate_expression(fn
))
1379 sym
= ctype
= fn
->ctype
;
1380 if (ctype
->type
== SYM_NODE
)
1381 ctype
= ctype
->ctype
.base_type
;
1382 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1383 ctype
= ctype
->ctype
.base_type
;
1384 if (!evaluate_arguments(sym
, ctype
, arglist
))
1386 if (sym
->type
== SYM_NODE
) {
1387 if (evaluate_symbol_call(expr
))
1390 if (sym
->type
== SYM_NODE
) {
1391 if (evaluate_symbol_call(expr
))
1394 if (ctype
->type
!= SYM_FN
) {
1395 warn(expr
->pos
, "not a function %.*s",
1396 sym
->ident
->len
, sym
->ident
->name
);
1399 args
= expression_list_size(expr
->args
);
1400 fnargs
= symbol_list_size(ctype
->arguments
);
1402 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1403 if (args
> fnargs
&& !ctype
->variadic
)
1404 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1405 expr
->ctype
= ctype
->ctype
.base_type
;
1409 struct symbol
*evaluate_expression(struct expression
*expr
)
1416 switch (expr
->type
) {
1418 warn(expr
->pos
, "value expression without a type");
1421 return evaluate_string(expr
);
1423 return evaluate_symbol_expression(expr
);
1425 if (!evaluate_expression(expr
->left
))
1427 if (!evaluate_expression(expr
->right
))
1429 return evaluate_binop(expr
);
1431 return evaluate_logical(expr
);
1433 if (!evaluate_expression(expr
->left
))
1435 if (!evaluate_expression(expr
->right
))
1437 return evaluate_comma(expr
);
1439 if (!evaluate_expression(expr
->left
))
1441 if (!evaluate_expression(expr
->right
))
1443 return evaluate_compare(expr
);
1444 case EXPR_ASSIGNMENT
:
1445 if (!evaluate_expression(expr
->left
))
1447 if (!evaluate_expression(expr
->right
))
1449 return evaluate_assignment(expr
);
1451 if (!evaluate_expression(expr
->unop
))
1453 return evaluate_preop(expr
);
1455 if (!evaluate_expression(expr
->unop
))
1457 return evaluate_postop(expr
);
1459 return evaluate_cast(expr
);
1461 return evaluate_sizeof(expr
);
1463 return evaluate_member_dereference(expr
);
1465 return evaluate_call(expr
);
1467 warn(expr
->pos
, "bitfield generated by parser");
1469 case EXPR_CONDITIONAL
:
1470 if (!evaluate_conditional(expr
->conditional
))
1472 if (!evaluate_expression(expr
->cond_false
))
1474 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1476 return evaluate_conditional_expression(expr
);
1477 case EXPR_STATEMENT
:
1478 expr
->ctype
= evaluate_statement(expr
->statement
);
1482 expr
->ctype
= &ptr_ctype
;
1486 /* Evaluate the type of the symbol .. */
1487 evaluate_symbol(expr
->symbol
);
1488 /* .. but the type of the _expression_ is a "type" */
1489 expr
->ctype
= &type_ctype
;
1492 /* These can not exist as stand-alone expressions */
1493 case EXPR_INITIALIZER
:
1494 case EXPR_IDENTIFIER
:
1497 warn(expr
->pos
, "internal front-end error: initializer in expression");
1503 void check_duplicates(struct symbol
*sym
)
1505 struct symbol
*next
= sym
;
1507 while ((next
= next
->same_symbol
) != NULL
) {
1508 const char *typediff
;
1509 evaluate_symbol(next
);
1510 typediff
= type_difference(sym
, next
, 0, 0);
1512 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1513 show_ident(sym
->ident
),
1514 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1520 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1522 struct symbol
*base_type
;
1527 sym
= examine_symbol_type(sym
);
1528 base_type
= sym
->ctype
.base_type
;
1532 /* Evaluate the initializers */
1533 if (sym
->initializer
) {
1534 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1535 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1536 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1537 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1538 base_type
->bit_size
= bit_size
;
1539 sym
->array_size
= base_type
->array_size
;
1540 sym
->bit_size
= bit_size
;
1544 /* And finally, evaluate the body of the symbol too */
1545 if (base_type
->type
== SYM_FN
) {
1548 FOR_EACH_PTR(base_type
->arguments
, s
) {
1552 if (base_type
->stmt
) {
1553 current_fn
= base_type
;
1554 current_contextmask
= sym
->ctype
.contextmask
;
1555 current_context
= sym
->ctype
.context
;
1556 evaluate_statement(base_type
->stmt
);
1563 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1565 struct expression
*expr
= stmt
->expression
;
1566 struct symbol
*ctype
, *fntype
;
1568 fntype
= current_fn
->ctype
.base_type
;
1569 if (!fntype
|| fntype
== &void_ctype
) {
1571 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1576 warn(stmt
->pos
, "return with no return value");
1579 ctype
= evaluate_expression(expr
);
1582 ctype
= degenerate(expr
, ctype
, &expr
);
1583 expr
->ctype
= ctype
;
1584 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1585 stmt
->expression
= expr
;
1589 static void evaluate_if_statement(struct statement
*stmt
)
1591 struct expression
*expr
= stmt
->if_conditional
;
1592 struct symbol
*ctype
;
1597 ctype
= evaluate_conditional(expr
);
1601 evaluate_statement(stmt
->if_true
);
1602 evaluate_statement(stmt
->if_false
);
1605 struct symbol
*evaluate_statement(struct statement
*stmt
)
1610 switch (stmt
->type
) {
1612 return evaluate_return_expression(stmt
);
1614 case STMT_EXPRESSION
:
1615 return evaluate_expression(stmt
->expression
);
1617 case STMT_COMPOUND
: {
1618 struct statement
*s
;
1619 struct symbol
*type
= NULL
;
1622 /* Evaluate each symbol in the compound statement */
1623 FOR_EACH_PTR(stmt
->syms
, sym
) {
1624 evaluate_symbol(sym
);
1626 evaluate_symbol(stmt
->ret
);
1629 * Then, evaluate each statement, making the type of the
1630 * compound statement be the type of the last statement
1633 FOR_EACH_PTR(stmt
->stmts
, s
) {
1634 type
= evaluate_statement(s
);
1639 evaluate_if_statement(stmt
);
1642 evaluate_conditional(stmt
->iterator_pre_condition
);
1643 evaluate_conditional(stmt
->iterator_post_condition
);
1644 evaluate_statement(stmt
->iterator_pre_statement
);
1645 evaluate_statement(stmt
->iterator_statement
);
1646 evaluate_statement(stmt
->iterator_post_statement
);
1649 evaluate_expression(stmt
->switch_expression
);
1650 evaluate_statement(stmt
->switch_statement
);
1653 evaluate_expression(stmt
->case_expression
);
1654 evaluate_expression(stmt
->case_to
);
1655 evaluate_statement(stmt
->case_statement
);
1658 evaluate_statement(stmt
->label_statement
);
1661 evaluate_expression(stmt
->goto_expression
);
1666 /* FIXME! Do the asm parameter evaluation! */