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 int length
= expr
->string
->length
;
82 sym
->array_size
= length
;
83 sym
->bit_size
= BITS_IN_CHAR
* length
;
84 sym
->ctype
.alignment
= 1;
85 sym
->ctype
.modifiers
= MOD_STATIC
;
86 sym
->ctype
.base_type
= array
;
88 array
->array_size
= length
;
89 array
->bit_size
= BITS_IN_CHAR
* length
;
90 array
->ctype
.alignment
= 1;
91 array
->ctype
.modifiers
= MOD_STATIC
;
92 array
->ctype
.base_type
= &char_ctype
;
95 addr
->ctype
= &ptr_ctype
;
97 expr
->type
= EXPR_PREOP
;
104 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
106 unsigned long lmod
, rmod
, mod
;
111 if (left
->bit_size
> right
->bit_size
)
114 if (right
->bit_size
> left
->bit_size
)
117 /* Same size integers - promote to unsigned, promote to long */
118 lmod
= left
->ctype
.modifiers
;
119 rmod
= right
->ctype
.modifiers
;
125 return ctype_integer(mod
);
128 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
130 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
132 expr
->cast_type
= type
;
133 expr
->cast_expression
= old
;
137 static int is_ptr_type(struct symbol
*type
)
139 if (type
->type
== SYM_NODE
)
140 type
= type
->ctype
.base_type
;
141 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
144 static int is_int_type(struct symbol
*type
)
146 if (type
->type
== SYM_NODE
)
147 type
= type
->ctype
.base_type
;
148 return type
->ctype
.base_type
== &int_type
;
151 static struct symbol
*bad_expr_type(struct expression
*expr
)
153 warn(expr
->pos
, "incompatible types for operation");
157 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
159 struct expression
*left
= *lp
, *right
= *rp
;
160 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
162 if (ltype
->type
== SYM_NODE
)
163 ltype
= ltype
->ctype
.base_type
;
164 if (rtype
->type
== SYM_NODE
)
165 rtype
= rtype
->ctype
.base_type
;
166 /* Integer promotion? */
167 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
169 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
171 if (is_int_type(ltype
) && is_int_type(rtype
)) {
172 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
174 /* Don't bother promoting same-size entities, it only adds clutter */
175 if (ltype
->bit_size
!= ctype
->bit_size
)
176 *lp
= cast_to(left
, ctype
);
177 if (rtype
->bit_size
!= ctype
->bit_size
)
178 *rp
= cast_to(right
, ctype
);
184 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
186 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
191 return bad_expr_type(expr
);
194 static inline int lvalue_expression(struct expression
*expr
)
196 while (expr
->type
== EXPR_CAST
)
197 expr
= expr
->cast_expression
;
198 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
201 /* Arrays degenerate into pointers on pointer arithmetic */
202 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
204 struct symbol
*base
= ctype
;
206 if (ctype
->type
== SYM_NODE
)
207 base
= ctype
->ctype
.base_type
;
208 if (base
->type
== SYM_ARRAY
|| base
->type
== SYM_FN
) {
209 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
210 struct expression
*n
= alloc_expression(expr
->pos
, 0);
211 struct expression
*ptr
;
213 merge_type(sym
, ctype
);
214 if (base
->type
== SYM_FN
)
216 merge_type(sym
, base
);
217 sym
->bit_size
= BITS_IN_POINTER
;
226 * This all really assumes that we got the degenerate
227 * array as an lvalue (ie a dereference). If that
228 * is not the case, then holler - because we've screwed
231 if (!lvalue_expression(ptr
))
232 warn(ptr
->pos
, "internal error: strange degenerate array case");
237 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
239 struct symbol
*ctype
;
240 struct symbol
*ptr_type
= ptr
->ctype
;
241 struct symbol
*i_type
= i
->ctype
;
244 if (i_type
->type
== SYM_NODE
)
245 i_type
= i_type
->ctype
.base_type
;
246 if (ptr_type
->type
== SYM_NODE
)
247 ptr_type
= ptr_type
->ctype
.base_type
;
249 if (i_type
->type
== SYM_ENUM
)
251 if (!is_int_type(i_type
))
252 return bad_expr_type(expr
);
255 examine_symbol_type(ctype
);
257 ctype
= degenerate(expr
, ctype
, &ptr
);
258 bit_size
= ctype
->bit_size
;
260 /* Special case: adding zero commonly happens as a result of 'array[0]' */
261 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
263 } else if (bit_size
> BITS_IN_CHAR
) {
264 struct expression
*add
= expr
;
265 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
266 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
268 val
->ctype
= size_t_ctype
;
269 val
->value
= bit_size
>> 3;
272 mul
->ctype
= size_t_ctype
;
276 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
285 static struct symbol
*evaluate_add(struct expression
*expr
)
287 struct expression
*left
= expr
->left
, *right
= expr
->right
;
288 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
290 if (is_ptr_type(ltype
))
291 return evaluate_ptr_add(expr
, left
, right
);
293 if (is_ptr_type(rtype
))
294 return evaluate_ptr_add(expr
, right
, left
);
296 // FIXME! FP promotion
297 return evaluate_int_binop(expr
);
300 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
301 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
303 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
304 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
307 unsigned long mod1
, mod2
, diff
;
308 unsigned long as1
, as2
;
310 if (target
== source
)
312 if (!target
|| !source
)
313 return "different types";
315 * Peel of per-node information.
316 * FIXME! Check alignment, address space, and context too here!
318 if (target
->type
== SYM_NODE
)
319 target
= target
->ctype
.base_type
;
320 if (source
->type
== SYM_NODE
)
321 source
= source
->ctype
.base_type
;
322 mod1
= target
->ctype
.modifiers
;
323 as1
= target
->ctype
.as
;
324 mod2
= source
->ctype
.modifiers
;
325 as2
= source
->ctype
.as
;
327 if (target
->type
!= source
->type
) {
328 int type1
= target
->type
;
329 int type2
= source
->type
;
331 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
332 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
333 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
335 if ((type1
== SYM_PTR
) && (target
->ctype
.base_type
->type
== SYM_FN
)) {
336 target
= target
->ctype
.base_type
;
340 if ((type2
== SYM_PTR
) && (source
->ctype
.base_type
->type
== SYM_FN
)) {
341 source
= source
->ctype
.base_type
;
346 return "different base types";
349 /* Must be same address space to be comparable */
351 return "different address spaces";
353 /* Ignore differences in storage types, sign, or addressability */
354 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
356 mod1
&= diff
& ~target_mod_ignore
;
357 mod2
&= diff
& ~source_mod_ignore
;
359 if ((mod1
| mod2
) & MOD_SIZE
)
360 return "different type sizes";
361 return "different modifiers";
365 if (target
->type
== SYM_FN
) {
367 struct symbol
*arg1
, *arg2
;
368 if (target
->variadic
!= source
->variadic
)
369 return "incompatible variadic arguments";
370 PREPARE_PTR_LIST(target
->arguments
, arg1
);
371 PREPARE_PTR_LIST(source
->arguments
, arg2
);
375 diff
= type_difference(arg1
, arg2
, 0, 0);
377 static char argdiff
[80];
378 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
387 FINISH_PTR_LIST(arg2
);
388 FINISH_PTR_LIST(arg1
);
391 target
= target
->ctype
.base_type
;
392 source
= source
->ctype
.base_type
;
397 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
399 /* NULL expression? Just return the type of the "other side" */
400 if (r
->type
== EXPR_VALUE
&& !r
->value
)
402 if (l
->type
== EXPR_VALUE
&& !l
->value
)
408 * Ignore differences in "volatile" and "const"ness when
409 * subtracting pointers
411 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
413 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
415 const char *typediff
;
416 struct symbol
*ctype
;
417 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
420 * If it is an integer subtract: the ptr add case will do the
423 if (!is_ptr_type(rtype
))
424 return evaluate_ptr_add(expr
, l
, r
);
427 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
429 ctype
= common_ptr_type(l
, r
);
431 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
435 examine_symbol_type(ctype
);
437 /* Figure out the base type we point to */
438 if (ctype
->type
== SYM_NODE
)
439 ctype
= ctype
->ctype
.base_type
;
440 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
441 warn(expr
->pos
, "subtraction of functions? Share your drugs");
444 ctype
= ctype
->ctype
.base_type
;
446 expr
->ctype
= ssize_t_ctype
;
447 if (ctype
->bit_size
> BITS_IN_CHAR
) {
448 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
449 struct expression
*div
= expr
;
450 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
452 val
->ctype
= size_t_ctype
;
453 val
->value
= ctype
->bit_size
>> 3;
456 sub
->ctype
= ssize_t_ctype
;
465 return ssize_t_ctype
;
468 static struct symbol
*evaluate_sub(struct expression
*expr
)
470 struct expression
*left
= expr
->left
, *right
= expr
->right
;
471 struct symbol
*ltype
= left
->ctype
;
473 if (is_ptr_type(ltype
))
474 return evaluate_ptr_sub(expr
, left
, right
);
476 // FIXME! FP promotion
477 return evaluate_int_binop(expr
);
480 static struct symbol
*evaluate_logical(struct expression
*expr
)
482 if (!evaluate_expression(expr
->left
))
484 if (!evaluate_expression(expr
->right
))
486 expr
->ctype
= &bool_ctype
;
490 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
492 // FIXME! Floating-point promotion!
493 return evaluate_int_binop(expr
);
496 static struct symbol
*evaluate_binop(struct expression
*expr
)
499 // addition can take ptr+int, fp and int
501 return evaluate_add(expr
);
503 // subtraction can take ptr-ptr, fp and int
505 return evaluate_sub(expr
);
507 // Arithmetic operations can take fp and int
508 case '*': case '/': case '%':
509 return evaluate_arithmetic(expr
);
511 // The rest are integer operations (bitops)
512 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
515 return evaluate_int_binop(expr
);
519 static struct symbol
*evaluate_comma(struct expression
*expr
)
521 expr
->ctype
= expr
->right
->ctype
;
525 static struct symbol
*evaluate_compare(struct expression
*expr
)
527 struct expression
*left
= expr
->left
, *right
= expr
->right
;
528 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
529 struct symbol
*ctype
;
532 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
533 expr
->ctype
= &bool_ctype
;
534 // FIXME! Check the types for compatibility
538 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
540 expr
->ctype
= &bool_ctype
;
544 return bad_expr_type(expr
);
547 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
549 /* Integer promotion? */
550 if (ltype
->type
== SYM_NODE
)
551 ltype
= ltype
->ctype
.base_type
;
552 if (rtype
->type
== SYM_NODE
)
553 rtype
= rtype
->ctype
.base_type
;
554 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
556 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
558 return (is_int_type(ltype
) && is_int_type(rtype
));
561 static int is_null_ptr(struct expression
*expr
)
563 return (expr
->type
== EXPR_VALUE
&&
568 * FIXME!! This should do casts, array degeneration etc..
570 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
572 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
574 if (ltype
->type
== SYM_NODE
)
575 ltype
= ltype
->ctype
.base_type
;
577 if (ltype
->type
== SYM_PTR
) {
578 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
582 if (rtype
->type
== SYM_NODE
)
583 rtype
= rtype
->ctype
.base_type
;
585 if (rtype
->type
== SYM_PTR
) {
586 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
592 static struct symbol
*do_degenerate(struct expression
**ep
)
594 struct expression
*expr
= *ep
;
595 return degenerate(expr
, expr
->ctype
, ep
);
598 static struct symbol
* evaluate_conditional(struct expression
*expr
)
600 struct expression
*cond
, *true, *false;
601 struct symbol
*ctype
, *ltype
, *rtype
;
602 const char * typediff
;
604 ctype
= do_degenerate(&expr
->conditional
);
605 cond
= expr
->conditional
;
609 if (expr
->cond_true
) {
610 ltype
= do_degenerate(&expr
->cond_true
);
611 true = expr
->cond_true
;
614 rtype
= do_degenerate(&expr
->cond_false
);
615 false = expr
->cond_false
;
618 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
620 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
622 ctype
= compatible_ptr_type(true, expr
->cond_false
);
624 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
634 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
635 struct expression
**rp
, struct symbol
*source
, const char *where
)
637 const char *typediff
;
641 /* It's ok if the target is more volatile or const than the source */
642 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
646 if (compatible_integer_types(target
, source
)) {
647 if (target
->bit_size
!= source
->bit_size
)
648 *rp
= cast_to(*rp
, target
);
652 /* Pointer destination? */
654 target_as
= t
->ctype
.as
;
655 if (t
->type
== SYM_NODE
) {
656 t
= t
->ctype
.base_type
;
657 target_as
|= t
->ctype
.as
;
659 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
) {
660 struct expression
*right
= *rp
;
661 struct symbol
*s
= source
;
664 // NULL pointer is always ok
665 if (right
->type
== EXPR_VALUE
&& !right
->value
)
668 /* "void *" matches anything as long as the address space is ok */
669 source_as
= s
->ctype
.as
;
670 if (s
->type
== SYM_NODE
) {
671 s
= s
->ctype
.base_type
;
672 source_as
|= s
->ctype
.as
;
674 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
675 s
= s
->ctype
.base_type
;
676 t
= t
->ctype
.base_type
;
677 if (s
== &void_ctype
|| t
== &void_ctype
)
683 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
684 warn(expr
->pos
, " expected %s", show_typename(target
));
685 warn(expr
->pos
, " got %s", show_typename(source
));
690 * FIXME!! This is wrong from a double evaluation standpoint. We can't
691 * just expand the expression twice, that would make any side effects
694 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
697 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
698 static const int op_trans
[] = {
699 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
700 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
701 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
702 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
703 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
704 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
705 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
706 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
707 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
708 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
711 subexpr
->left
= left
;
712 subexpr
->right
= right
;
713 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
715 expr
->right
= subexpr
;
716 return evaluate_binop(subexpr
);
719 static struct symbol
*evaluate_assignment(struct expression
*expr
)
721 struct expression
*left
= expr
->left
, *right
= expr
->right
;
722 struct symbol
*ltype
, *rtype
;
725 rtype
= right
->ctype
;
726 if (expr
->op
!= '=') {
727 rtype
= evaluate_binop_assignment(expr
, left
, right
);
733 if (!lvalue_expression(left
)) {
734 warn(expr
->pos
, "not an lvalue");
738 rtype
= degenerate(right
, rtype
, &expr
->right
);
740 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
743 if (ltype
->type
== SYM_NODE
)
744 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
750 static struct symbol
*evaluate_addressof(struct expression
*expr
)
752 struct symbol
*ctype
, *symbol
;
753 struct expression
*op
= expr
->unop
;
755 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
756 warn(expr
->pos
, "not addressable");
760 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
761 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
762 symbol
->bit_size
= BITS_IN_POINTER
;
765 if (ctype
->type
== SYM_NODE
) {
766 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
767 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
768 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
769 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
771 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
772 symbol
->ctype
.as
= ctype
->ctype
.as
;
773 symbol
->ctype
.context
= ctype
->ctype
.context
;
774 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
775 ctype
= ctype
->ctype
.base_type
;
778 symbol
->ctype
.base_type
= ctype
;
780 expr
->ctype
= symbol
;
785 static struct symbol
*evaluate_dereference(struct expression
*expr
)
787 struct expression
*op
= expr
->unop
;
788 struct symbol
*ctype
= op
->ctype
, *sym
;
790 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
791 if (ctype
->type
== SYM_NODE
) {
792 ctype
= ctype
->ctype
.base_type
;
793 merge_type(sym
, ctype
);
795 sym
->ctype
= ctype
->ctype
;
796 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
797 warn(expr
->pos
, "cannot derefence this type");
801 ctype
= ctype
->ctype
.base_type
;
802 examine_symbol_type(ctype
);
804 warn(expr
->pos
, "undefined type");
808 sym
->bit_size
= ctype
->bit_size
;
809 sym
->array_size
= ctype
->array_size
;
811 /* Simplify: *&(expr) => (expr) */
812 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
821 * Unary post-ops: x++ and x--
823 static struct symbol
*evaluate_postop(struct expression
*expr
)
825 struct expression
*op
= expr
->unop
;
826 struct symbol
*ctype
= op
->ctype
;
828 if (!lvalue_expression(expr
->unop
)) {
829 warn(expr
->pos
, "need lvalue expression for ++/--");
836 static struct symbol
*evaluate_preop(struct expression
*expr
)
838 struct symbol
*ctype
= expr
->unop
->ctype
;
847 return evaluate_dereference(expr
);
850 return evaluate_addressof(expr
);
852 case SPECIAL_INCREMENT
:
853 case SPECIAL_DECREMENT
:
855 * From a type evaluation standpoint the pre-ops are
856 * the same as the postops
858 return evaluate_postop(expr
);
871 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
873 struct ptr_list
*head
= (struct ptr_list
*)_list
;
874 struct ptr_list
*list
= head
;
880 for (i
= 0; i
< list
->nr
; i
++) {
881 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
883 if (sym
->ident
!= ident
)
885 *offset
= sym
->offset
;
888 struct symbol
*ctype
= sym
->ctype
.base_type
;
892 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
894 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
897 *offset
+= sym
->offset
;
901 } while ((list
= list
->next
) != head
);
905 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
907 struct expression
*add
;
912 /* Create a new add-expression */
913 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
915 add
->ctype
= &ptr_ctype
;
917 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
918 add
->right
->ctype
= &int_ctype
;
919 add
->right
->value
= offset
;
924 /* structure/union dereference */
925 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
928 struct symbol
*ctype
, *member
, *sym
;
929 struct expression
*deref
= expr
->deref
, *add
;
930 struct ident
*ident
= expr
->member
;
934 if (!evaluate_expression(deref
))
937 warn(expr
->pos
, "bad member name");
941 ctype
= deref
->ctype
;
942 address_space
= ctype
->ctype
.as
;
943 mod
= ctype
->ctype
.modifiers
;
944 if (ctype
->type
== SYM_NODE
) {
945 ctype
= ctype
->ctype
.base_type
;
946 address_space
|= ctype
->ctype
.as
;
947 mod
|= ctype
->ctype
.modifiers
;
949 if (expr
->op
== SPECIAL_DEREFERENCE
) {
950 /* Arrays will degenerate into pointers for '->' */
951 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
952 warn(expr
->pos
, "expected a pointer to a struct/union");
955 mod
= ctype
->ctype
.modifiers
;
956 address_space
= ctype
->ctype
.as
;
957 ctype
= ctype
->ctype
.base_type
;
958 if (ctype
->type
== SYM_NODE
) {
959 mod
|= ctype
->ctype
.modifiers
;
960 address_space
|= ctype
->ctype
.as
;
961 ctype
= ctype
->ctype
.base_type
;
964 if (!lvalue_expression(deref
)) {
965 warn(deref
->pos
, "expected lvalue for member dereference");
971 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
972 warn(expr
->pos
, "expected structure or union");
976 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
978 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
979 const char *name
= "<unnamed>";
982 name
= ctype
->ident
->name
;
983 namelen
= ctype
->ident
->len
;
985 warn(expr
->pos
, "no member '%s' in %s %.*s",
986 show_ident(ident
), type
, namelen
, name
);
990 add
= evaluate_offset(deref
, offset
);
992 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
993 sym
->bit_size
= member
->bit_size
;
994 sym
->array_size
= member
->array_size
;
995 sym
->ctype
= member
->ctype
;
996 sym
->ctype
.modifiers
= mod
;
997 sym
->ctype
.as
= address_space
;
998 ctype
= member
->ctype
.base_type
;
999 if (ctype
->type
== SYM_BITFIELD
) {
1000 ctype
= ctype
->ctype
.base_type
;
1001 expr
->type
= EXPR_BITFIELD
;
1002 expr
->bitpos
= member
->bit_offset
;
1003 expr
->nrbits
= member
->fieldwidth
;
1004 expr
->address
= add
;
1006 expr
->type
= EXPR_PREOP
;
1015 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1019 if (expr
->cast_type
) {
1020 examine_symbol_type(expr
->cast_type
);
1021 size
= expr
->cast_type
->bit_size
;
1023 if (!evaluate_expression(expr
->cast_expression
))
1025 size
= expr
->cast_expression
->ctype
->bit_size
;
1028 warn(expr
->pos
, "cannot size expression");
1031 expr
->type
= EXPR_VALUE
;
1032 expr
->value
= size
>> 3;
1033 expr
->ctype
= size_t_ctype
;
1034 return size_t_ctype
;
1037 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1039 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1040 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1044 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1046 struct expression
*expr
;
1047 struct symbol_list
*argument_types
= fn
->arguments
;
1048 struct symbol
*argtype
;
1051 PREPARE_PTR_LIST(argument_types
, argtype
);
1052 FOR_EACH_PTR (head
, expr
) {
1053 struct expression
**p
= THIS_ADDRESS(expr
);
1054 struct symbol
*ctype
, *target
;
1055 ctype
= evaluate_expression(expr
);
1060 if (context_clash(f
, ctype
))
1061 warn(expr
->pos
, "argument %d used in wrong context", i
);
1063 ctype
= degenerate(expr
, ctype
, p
);
1066 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1067 target
= &int_ctype
;
1069 static char where
[30];
1070 examine_symbol_type(target
);
1071 sprintf(where
, "argument %d", i
);
1072 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1076 NEXT_PTR_LIST(argtype
);
1078 FINISH_PTR_LIST(argtype
);
1082 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1083 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1085 struct expression
*entry
;
1089 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1090 struct expression
**p
= THIS_ADDRESS(entry
);
1092 if (entry
->type
== EXPR_INDEX
) {
1093 current
= entry
->idx_to
;
1096 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1104 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1106 struct expression
*entry
;
1109 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1110 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1111 struct expression
**p
= THIS_ADDRESS(entry
);
1113 if (entry
->type
== EXPR_IDENTIFIER
) {
1114 struct ident
*ident
= entry
->expr_ident
;
1115 /* We special-case the "already right place" case */
1116 if (sym
&& sym
->ident
== ident
)
1118 RESET_PTR_LIST(sym
);
1121 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1124 if (sym
->ident
== ident
)
1132 warn(expr
->pos
, "too many initializers for struct/union");
1136 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1140 FINISH_PTR_LIST(sym
);
1146 * Initializers are kind of like assignments. Except
1147 * they can be a hell of a lot more complex.
1149 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1151 struct expression
*expr
= *ep
;
1154 * Simple non-structure/array initializers are the simple
1155 * case, and look (and parse) largely like assignments.
1157 if (expr
->type
!= EXPR_INITIALIZER
) {
1159 struct symbol
*rtype
= evaluate_expression(expr
);
1161 struct expression
*pos
;
1163 // FIXME! char array[] = "string" special case
1164 // should _not_ degenerate.
1165 rtype
= degenerate(expr
, rtype
, ep
);
1167 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1168 /* strings are special: char arrays */
1169 if (rtype
->type
== SYM_ARRAY
)
1170 size
= rtype
->array_size
;
1172 * Don't bother creating a position expression for
1173 * the simple initializer cases that don't need it.
1175 * We need a position if the initializer has a byte
1176 * offset, _or_ if we're initializing a bitfield.
1178 if (offset
|| ctype
->fieldwidth
) {
1179 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1180 pos
->init_offset
= offset
;
1181 pos
->init_sym
= ctype
;
1182 pos
->init_expr
= *ep
;
1183 pos
->ctype
= expr
->ctype
;
1190 expr
->ctype
= ctype
;
1191 if (ctype
->type
== SYM_NODE
)
1192 ctype
= ctype
->ctype
.base_type
;
1194 switch (ctype
->type
) {
1197 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1199 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1201 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1205 warn(expr
->pos
, "unexpected compound initializer");
1209 static struct symbol
*evaluate_cast(struct expression
*expr
)
1211 struct expression
*target
= expr
->cast_expression
;
1212 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1214 expr
->ctype
= ctype
;
1215 expr
->cast_type
= ctype
;
1218 * Special case: a cast can be followed by an
1219 * initializer, in which case we need to pass
1220 * the type value down to that initializer rather
1221 * than trying to evaluate it as an expression
1223 if (target
->type
== EXPR_INITIALIZER
) {
1224 evaluate_initializer(ctype
, &expr
->cast_expression
, 0);
1228 evaluate_expression(target
);
1231 * Casts of constant values are special: they
1232 * can be NULL, and thus need to be simplified
1235 if (target
->type
== EXPR_VALUE
)
1236 cast_value(expr
, ctype
, target
, target
->ctype
);
1242 * Evaluate a call expression with a symbol. This
1243 * should expand inline functions, and evaluate
1246 static int evaluate_symbol_call(struct expression
*expr
)
1248 struct expression
*fn
= expr
->fn
;
1249 struct symbol
*ctype
= fn
->ctype
;
1251 if (fn
->type
!= EXPR_PREOP
)
1254 if (ctype
->op
&& ctype
->op
->evaluate
)
1255 return ctype
->op
->evaluate(expr
);
1257 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1259 struct symbol
*curr
= current_fn
;
1260 unsigned long context
= current_context
;
1261 unsigned long mask
= current_contextmask
;
1263 current_context
|= ctype
->ctype
.context
;
1264 current_contextmask
|= ctype
->ctype
.contextmask
;
1265 current_fn
= ctype
->ctype
.base_type
;
1266 ret
= inline_function(expr
, ctype
);
1268 /* restore the old function context */
1270 current_context
= context
;
1271 current_contextmask
= mask
;
1278 static struct symbol
*evaluate_call(struct expression
*expr
)
1281 struct symbol
*ctype
, *sym
;
1282 struct expression
*fn
= expr
->fn
;
1283 struct expression_list
*arglist
= expr
->args
;
1285 if (!evaluate_expression(fn
))
1287 sym
= ctype
= fn
->ctype
;
1288 if (ctype
->type
== SYM_NODE
)
1289 ctype
= ctype
->ctype
.base_type
;
1290 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1291 ctype
= ctype
->ctype
.base_type
;
1292 if (!evaluate_arguments(sym
, ctype
, arglist
))
1294 if (sym
->type
== SYM_NODE
) {
1295 if (evaluate_symbol_call(expr
))
1298 if (sym
->type
== SYM_NODE
) {
1299 if (evaluate_symbol_call(expr
))
1302 if (ctype
->type
!= SYM_FN
) {
1303 warn(expr
->pos
, "not a function");
1306 args
= expression_list_size(expr
->args
);
1307 fnargs
= symbol_list_size(ctype
->arguments
);
1309 warn(expr
->pos
, "not enough arguments for function");
1310 if (args
> fnargs
&& !ctype
->variadic
)
1311 warn(expr
->pos
, "too many arguments for function");
1312 expr
->ctype
= ctype
->ctype
.base_type
;
1316 struct symbol
*evaluate_expression(struct expression
*expr
)
1323 switch (expr
->type
) {
1325 warn(expr
->pos
, "value expression without a type");
1328 return evaluate_string(expr
);
1330 return evaluate_symbol_expression(expr
);
1332 if (!evaluate_expression(expr
->left
))
1334 if (!evaluate_expression(expr
->right
))
1336 return evaluate_binop(expr
);
1338 return evaluate_logical(expr
);
1340 if (!evaluate_expression(expr
->left
))
1342 if (!evaluate_expression(expr
->right
))
1344 return evaluate_comma(expr
);
1346 if (!evaluate_expression(expr
->left
))
1348 if (!evaluate_expression(expr
->right
))
1350 return evaluate_compare(expr
);
1351 case EXPR_ASSIGNMENT
:
1352 if (!evaluate_expression(expr
->left
))
1354 if (!evaluate_expression(expr
->right
))
1356 return evaluate_assignment(expr
);
1358 if (!evaluate_expression(expr
->unop
))
1360 return evaluate_preop(expr
);
1362 if (!evaluate_expression(expr
->unop
))
1364 return evaluate_postop(expr
);
1366 return evaluate_cast(expr
);
1368 return evaluate_sizeof(expr
);
1370 return evaluate_member_dereference(expr
);
1372 return evaluate_call(expr
);
1374 warn(expr
->pos
, "bitfield generated by parser");
1376 case EXPR_CONDITIONAL
:
1377 if (!evaluate_expression(expr
->conditional
))
1379 if (!evaluate_expression(expr
->cond_false
))
1381 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1383 return evaluate_conditional(expr
);
1384 case EXPR_STATEMENT
:
1385 expr
->ctype
= evaluate_statement(expr
->statement
);
1389 expr
->ctype
= &ptr_ctype
;
1392 /* These can not exist as stand-alone expressions */
1393 case EXPR_INITIALIZER
:
1394 case EXPR_IDENTIFIER
:
1397 warn(expr
->pos
, "internal front-end error: initializer in expression");
1403 void check_duplicates(struct symbol
*sym
)
1405 struct symbol
*next
= sym
;
1407 while ((next
= next
->same_symbol
) != NULL
) {
1408 const char *typediff
;
1409 evaluate_symbol(next
);
1410 typediff
= type_difference(sym
, next
, 0, 0);
1412 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1413 show_ident(sym
->ident
),
1414 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1420 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1422 struct symbol
*base_type
;
1427 sym
= examine_symbol_type(sym
);
1428 base_type
= sym
->ctype
.base_type
;
1432 /* Evaluate the initializers */
1433 if (sym
->initializer
) {
1434 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1435 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1436 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1437 base_type
->array_size
= count
;
1438 base_type
->bit_size
= bit_size
;
1439 sym
->array_size
= count
;
1440 sym
->bit_size
= bit_size
;
1444 /* And finally, evaluate the body of the symbol too */
1445 if (base_type
->type
== SYM_FN
) {
1448 FOR_EACH_PTR(base_type
->arguments
, s
) {
1452 if (base_type
->stmt
) {
1453 current_fn
= base_type
;
1454 current_contextmask
= sym
->ctype
.contextmask
;
1455 current_context
= sym
->ctype
.context
;
1456 evaluate_statement(base_type
->stmt
);
1463 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1465 struct expression
*expr
= stmt
->expression
;
1466 struct symbol
*ctype
, *fntype
;
1468 fntype
= current_fn
->ctype
.base_type
;
1469 if (!fntype
|| fntype
== &void_ctype
) {
1471 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1476 warn(stmt
->pos
, "return with no return value");
1479 ctype
= evaluate_expression(expr
);
1482 ctype
= degenerate(expr
, ctype
, &expr
);
1483 expr
->ctype
= ctype
;
1484 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1485 stmt
->expression
= expr
;
1489 static void evaluate_if_statement(struct statement
*stmt
)
1491 struct expression
*expr
= stmt
->if_conditional
;
1492 struct symbol
*ctype
;
1496 if (expr
->type
== EXPR_ASSIGNMENT
)
1497 warn(expr
->pos
, "assignment expression in conditional");
1499 ctype
= evaluate_expression(expr
);
1503 evaluate_statement(stmt
->if_true
);
1504 evaluate_statement(stmt
->if_false
);
1507 struct symbol
*evaluate_statement(struct statement
*stmt
)
1512 switch (stmt
->type
) {
1514 return evaluate_return_expression(stmt
);
1516 case STMT_EXPRESSION
:
1517 return evaluate_expression(stmt
->expression
);
1519 case STMT_COMPOUND
: {
1520 struct statement
*s
;
1521 struct symbol
*type
= NULL
;
1524 /* Evaluate each symbol in the compound statement */
1525 FOR_EACH_PTR(stmt
->syms
, sym
) {
1526 evaluate_symbol(sym
);
1528 evaluate_symbol(stmt
->ret
);
1531 * Then, evaluate each statement, making the type of the
1532 * compound statement be the type of the last statement
1535 FOR_EACH_PTR(stmt
->stmts
, s
) {
1536 type
= evaluate_statement(s
);
1541 evaluate_if_statement(stmt
);
1544 evaluate_expression(stmt
->iterator_pre_condition
);
1545 evaluate_expression(stmt
->iterator_post_condition
);
1546 evaluate_statement(stmt
->iterator_pre_statement
);
1547 evaluate_statement(stmt
->iterator_statement
);
1548 evaluate_statement(stmt
->iterator_post_statement
);
1551 evaluate_expression(stmt
->switch_expression
);
1552 evaluate_statement(stmt
->switch_statement
);
1555 evaluate_expression(stmt
->case_expression
);
1556 evaluate_expression(stmt
->case_to
);
1557 evaluate_statement(stmt
->case_statement
);
1560 evaluate_statement(stmt
->label_statement
);
1563 evaluate_expression(stmt
->goto_expression
);
1568 /* FIXME! Do the asm parameter evaluation! */