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
->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 if (target
== source
)
322 if (!target
|| !source
)
323 return "different types";
325 * Peel of per-node information.
326 * FIXME! Check alignment, address space, and context too here!
328 if (target
->type
== SYM_NODE
)
329 target
= target
->ctype
.base_type
;
330 if (source
->type
== SYM_NODE
)
331 source
= source
->ctype
.base_type
;
333 if (target
== source
)
335 if (!target
|| !source
)
336 return "different types";
338 mod1
= target
->ctype
.modifiers
;
339 as1
= target
->ctype
.as
;
340 mod2
= source
->ctype
.modifiers
;
341 as2
= source
->ctype
.as
;
343 if (target
->type
!= source
->type
) {
344 int type1
= target
->type
;
345 int type2
= source
->type
;
347 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
348 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
349 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
351 if ((type1
== SYM_PTR
) && (target
->ctype
.base_type
->type
== SYM_FN
)) {
352 target
= target
->ctype
.base_type
;
356 if ((type2
== SYM_PTR
) && (source
->ctype
.base_type
->type
== SYM_FN
)) {
357 source
= source
->ctype
.base_type
;
362 return "different base types";
365 /* Must be same address space to be comparable */
367 return "different address spaces";
369 /* Ignore differences in storage types, sign, or addressability */
370 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
372 mod1
&= diff
& ~target_mod_ignore
;
373 mod2
&= diff
& ~source_mod_ignore
;
375 if ((mod1
| mod2
) & MOD_SIZE
)
376 return "different type sizes";
377 return "different modifiers";
381 if (target
->type
== SYM_FN
) {
383 struct symbol
*arg1
, *arg2
;
384 if (target
->variadic
!= source
->variadic
)
385 return "incompatible variadic arguments";
386 PREPARE_PTR_LIST(target
->arguments
, arg1
);
387 PREPARE_PTR_LIST(source
->arguments
, arg2
);
391 diff
= type_difference(arg1
, arg2
, 0, 0);
393 static char argdiff
[80];
394 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
403 FINISH_PTR_LIST(arg2
);
404 FINISH_PTR_LIST(arg1
);
407 target
= target
->ctype
.base_type
;
408 source
= source
->ctype
.base_type
;
413 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
415 /* NULL expression? Just return the type of the "other side" */
416 if (r
->type
== EXPR_VALUE
&& !r
->value
)
418 if (l
->type
== EXPR_VALUE
&& !l
->value
)
424 * Ignore differences in "volatile" and "const"ness when
425 * subtracting pointers
427 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
429 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
431 const char *typediff
;
432 struct symbol
*ctype
;
433 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
436 * If it is an integer subtract: the ptr add case will do the
439 if (!is_ptr_type(rtype
))
440 return evaluate_ptr_add(expr
, l
, r
);
443 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
445 ctype
= common_ptr_type(l
, r
);
447 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
451 examine_symbol_type(ctype
);
453 /* Figure out the base type we point to */
454 if (ctype
->type
== SYM_NODE
)
455 ctype
= ctype
->ctype
.base_type
;
456 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
457 warn(expr
->pos
, "subtraction of functions? Share your drugs");
460 ctype
= ctype
->ctype
.base_type
;
462 expr
->ctype
= ssize_t_ctype
;
463 if (ctype
->bit_size
> BITS_IN_CHAR
) {
464 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
465 struct expression
*div
= expr
;
466 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
468 val
->ctype
= size_t_ctype
;
469 val
->value
= ctype
->bit_size
>> 3;
472 sub
->ctype
= ssize_t_ctype
;
481 return ssize_t_ctype
;
484 static struct symbol
*evaluate_sub(struct expression
*expr
)
486 struct expression
*left
= expr
->left
, *right
= expr
->right
;
487 struct symbol
*ltype
= left
->ctype
;
489 if (is_ptr_type(ltype
))
490 return evaluate_ptr_sub(expr
, left
, right
);
492 // FIXME! FP promotion
493 return evaluate_int_binop(expr
);
496 static struct symbol
*evaluate_logical(struct expression
*expr
)
498 if (!evaluate_expression(expr
->left
))
500 if (!evaluate_expression(expr
->right
))
502 expr
->ctype
= &bool_ctype
;
506 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
508 // FIXME! Floating-point promotion!
509 return evaluate_int_binop(expr
);
512 static struct symbol
*evaluate_binop(struct expression
*expr
)
515 // addition can take ptr+int, fp and int
517 return evaluate_add(expr
);
519 // subtraction can take ptr-ptr, fp and int
521 return evaluate_sub(expr
);
523 // Arithmetic operations can take fp and int
524 case '*': case '/': case '%':
525 return evaluate_arithmetic(expr
);
527 // The rest are integer operations (bitops)
528 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
531 return evaluate_int_binop(expr
);
535 static struct symbol
*evaluate_comma(struct expression
*expr
)
537 expr
->ctype
= expr
->right
->ctype
;
541 static struct symbol
*evaluate_compare(struct expression
*expr
)
543 struct expression
*left
= expr
->left
, *right
= expr
->right
;
544 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
545 struct symbol
*ctype
;
548 if (is_type_type(ltype
) && is_type_type(rtype
)) {
549 expr
->ctype
= &bool_ctype
;
554 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
555 expr
->ctype
= &bool_ctype
;
556 // FIXME! Check the types for compatibility
560 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
562 expr
->ctype
= &bool_ctype
;
566 return bad_expr_type(expr
);
569 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
571 /* Integer promotion? */
572 if (ltype
->type
== SYM_NODE
)
573 ltype
= ltype
->ctype
.base_type
;
574 if (rtype
->type
== SYM_NODE
)
575 rtype
= rtype
->ctype
.base_type
;
576 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
578 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
580 return (is_int_type(ltype
) && is_int_type(rtype
));
583 static int is_null_ptr(struct expression
*expr
)
585 return (expr
->type
== EXPR_VALUE
&&
590 * FIXME!! This should do casts, array degeneration etc..
592 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
594 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
596 if (ltype
->type
== SYM_NODE
)
597 ltype
= ltype
->ctype
.base_type
;
599 if (ltype
->type
== SYM_PTR
) {
600 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
604 if (rtype
->type
== SYM_NODE
)
605 rtype
= rtype
->ctype
.base_type
;
607 if (rtype
->type
== SYM_PTR
) {
608 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
614 static struct symbol
*do_degenerate(struct expression
**ep
)
616 struct expression
*expr
= *ep
;
617 return degenerate(expr
, expr
->ctype
, ep
);
620 static struct symbol
* evaluate_conditional(struct expression
*expr
)
622 struct expression
*cond
, *true, *false;
623 struct symbol
*ctype
, *ltype
, *rtype
;
624 const char * typediff
;
626 ctype
= do_degenerate(&expr
->conditional
);
627 cond
= expr
->conditional
;
631 if (expr
->cond_true
) {
632 ltype
= do_degenerate(&expr
->cond_true
);
633 true = expr
->cond_true
;
636 rtype
= do_degenerate(&expr
->cond_false
);
637 false = expr
->cond_false
;
640 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
642 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
644 ctype
= compatible_ptr_type(true, expr
->cond_false
);
646 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
656 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
657 struct expression
**rp
, struct symbol
*source
, const char *where
)
659 const char *typediff
;
663 /* It's ok if the target is more volatile or const than the source */
664 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
668 if (compatible_integer_types(target
, source
)) {
669 if (target
->bit_size
!= source
->bit_size
)
670 *rp
= cast_to(*rp
, target
);
674 /* Pointer destination? */
676 target_as
= t
->ctype
.as
;
677 if (t
->type
== SYM_NODE
) {
678 t
= t
->ctype
.base_type
;
679 target_as
|= t
->ctype
.as
;
681 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
) {
682 struct expression
*right
= *rp
;
683 struct symbol
*s
= source
;
686 // NULL pointer is always ok
687 if (right
->type
== EXPR_VALUE
&& !right
->value
)
690 /* "void *" matches anything as long as the address space is ok */
691 source_as
= s
->ctype
.as
;
692 if (s
->type
== SYM_NODE
) {
693 s
= s
->ctype
.base_type
;
694 source_as
|= s
->ctype
.as
;
696 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
697 s
= s
->ctype
.base_type
;
698 t
= t
->ctype
.base_type
;
699 if (s
== &void_ctype
|| t
== &void_ctype
)
705 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
706 warn(expr
->pos
, " expected %s", show_typename(target
));
707 warn(expr
->pos
, " got %s", show_typename(source
));
712 * FIXME!! This is wrong from a double evaluation standpoint. We can't
713 * just expand the expression twice, that would make any side effects
716 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
719 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
720 static const int op_trans
[] = {
721 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
722 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
723 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
724 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
725 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
726 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
727 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
728 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
729 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
730 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
733 subexpr
->left
= left
;
734 subexpr
->right
= right
;
735 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
737 expr
->right
= subexpr
;
738 return evaluate_binop(subexpr
);
741 static struct symbol
*evaluate_assignment(struct expression
*expr
)
743 struct expression
*left
= expr
->left
, *right
= expr
->right
;
744 struct symbol
*ltype
, *rtype
;
747 rtype
= right
->ctype
;
748 if (expr
->op
!= '=') {
749 rtype
= evaluate_binop_assignment(expr
, left
, right
);
755 if (!lvalue_expression(left
)) {
756 warn(expr
->pos
, "not an lvalue");
760 rtype
= degenerate(right
, rtype
, &expr
->right
);
762 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
765 if (ltype
->type
== SYM_NODE
)
766 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
772 static struct symbol
*evaluate_addressof(struct expression
*expr
)
774 struct symbol
*ctype
, *symbol
;
775 struct expression
*op
= expr
->unop
;
777 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
778 warn(expr
->pos
, "not addressable");
782 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
783 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
784 symbol
->bit_size
= BITS_IN_POINTER
;
787 if (ctype
->type
== SYM_NODE
) {
788 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
789 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
790 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
791 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
793 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
794 symbol
->ctype
.as
= ctype
->ctype
.as
;
795 symbol
->ctype
.context
= ctype
->ctype
.context
;
796 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
797 ctype
= ctype
->ctype
.base_type
;
800 symbol
->ctype
.base_type
= ctype
;
802 expr
->ctype
= symbol
;
807 static struct symbol
*evaluate_dereference(struct expression
*expr
)
809 struct expression
*op
= expr
->unop
;
810 struct symbol
*ctype
= op
->ctype
, *sym
;
812 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
813 sym
->ctype
= ctype
->ctype
;
814 if (ctype
->type
== SYM_NODE
) {
815 ctype
= ctype
->ctype
.base_type
;
816 merge_type(sym
, ctype
);
818 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
819 warn(expr
->pos
, "cannot derefence this type");
823 ctype
= ctype
->ctype
.base_type
;
824 examine_symbol_type(ctype
);
826 warn(expr
->pos
, "undefined type");
830 sym
->bit_size
= ctype
->bit_size
;
831 sym
->array_size
= ctype
->array_size
;
833 /* Simplify: *&(expr) => (expr) */
834 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
843 * Unary post-ops: x++ and x--
845 static struct symbol
*evaluate_postop(struct expression
*expr
)
847 struct expression
*op
= expr
->unop
;
848 struct symbol
*ctype
= op
->ctype
;
850 if (!lvalue_expression(expr
->unop
)) {
851 warn(expr
->pos
, "need lvalue expression for ++/--");
858 static struct symbol
*evaluate_preop(struct expression
*expr
)
860 struct symbol
*ctype
= expr
->unop
->ctype
;
869 return evaluate_dereference(expr
);
872 return evaluate_addressof(expr
);
874 case SPECIAL_INCREMENT
:
875 case SPECIAL_DECREMENT
:
877 * From a type evaluation standpoint the pre-ops are
878 * the same as the postops
880 return evaluate_postop(expr
);
893 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
895 struct ptr_list
*head
= (struct ptr_list
*)_list
;
896 struct ptr_list
*list
= head
;
902 for (i
= 0; i
< list
->nr
; i
++) {
903 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
905 if (sym
->ident
!= ident
)
907 *offset
= sym
->offset
;
910 struct symbol
*ctype
= sym
->ctype
.base_type
;
914 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
916 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
919 *offset
+= sym
->offset
;
923 } while ((list
= list
->next
) != head
);
927 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
929 struct expression
*add
;
934 /* Create a new add-expression */
935 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
937 add
->ctype
= &ptr_ctype
;
939 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
940 add
->right
->ctype
= &int_ctype
;
941 add
->right
->value
= offset
;
946 /* structure/union dereference */
947 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
950 struct symbol
*ctype
, *member
, *sym
;
951 struct expression
*deref
= expr
->deref
, *add
;
952 struct ident
*ident
= expr
->member
;
956 if (!evaluate_expression(deref
))
959 warn(expr
->pos
, "bad member name");
963 ctype
= deref
->ctype
;
964 address_space
= ctype
->ctype
.as
;
965 mod
= ctype
->ctype
.modifiers
;
966 if (ctype
->type
== SYM_NODE
) {
967 ctype
= ctype
->ctype
.base_type
;
968 address_space
|= ctype
->ctype
.as
;
969 mod
|= ctype
->ctype
.modifiers
;
971 if (expr
->op
== SPECIAL_DEREFERENCE
) {
972 /* Arrays will degenerate into pointers for '->' */
973 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
974 warn(expr
->pos
, "expected a pointer to a struct/union");
977 mod
= ctype
->ctype
.modifiers
;
978 address_space
= ctype
->ctype
.as
;
979 ctype
= ctype
->ctype
.base_type
;
980 if (ctype
->type
== SYM_NODE
) {
981 mod
|= ctype
->ctype
.modifiers
;
982 address_space
|= ctype
->ctype
.as
;
983 ctype
= ctype
->ctype
.base_type
;
986 if (!lvalue_expression(deref
)) {
987 warn(deref
->pos
, "expected lvalue for member dereference");
993 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
994 warn(expr
->pos
, "expected structure or union");
998 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1000 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1001 const char *name
= "<unnamed>";
1004 name
= ctype
->ident
->name
;
1005 namelen
= ctype
->ident
->len
;
1007 warn(expr
->pos
, "no member '%s' in %s %.*s",
1008 show_ident(ident
), type
, namelen
, name
);
1012 add
= evaluate_offset(deref
, offset
);
1014 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1015 sym
->bit_size
= member
->bit_size
;
1016 sym
->array_size
= member
->array_size
;
1017 sym
->ctype
= member
->ctype
;
1018 sym
->ctype
.modifiers
= mod
;
1019 sym
->ctype
.as
= address_space
;
1020 ctype
= member
->ctype
.base_type
;
1021 if (ctype
->type
== SYM_BITFIELD
) {
1022 ctype
= ctype
->ctype
.base_type
;
1023 expr
->type
= EXPR_BITFIELD
;
1024 expr
->bitpos
= member
->bit_offset
;
1025 expr
->nrbits
= member
->fieldwidth
;
1026 expr
->address
= add
;
1028 expr
->type
= EXPR_PREOP
;
1037 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1041 if (expr
->cast_type
) {
1042 examine_symbol_type(expr
->cast_type
);
1043 size
= expr
->cast_type
->bit_size
;
1045 if (!evaluate_expression(expr
->cast_expression
))
1047 size
= expr
->cast_expression
->ctype
->bit_size
;
1050 warn(expr
->pos
, "cannot size expression");
1053 expr
->type
= EXPR_VALUE
;
1054 expr
->value
= size
>> 3;
1055 expr
->ctype
= size_t_ctype
;
1056 return size_t_ctype
;
1059 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1061 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1062 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1066 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1068 struct expression
*expr
;
1069 struct symbol_list
*argument_types
= fn
->arguments
;
1070 struct symbol
*argtype
;
1073 PREPARE_PTR_LIST(argument_types
, argtype
);
1074 FOR_EACH_PTR (head
, expr
) {
1075 struct expression
**p
= THIS_ADDRESS(expr
);
1076 struct symbol
*ctype
, *target
;
1077 ctype
= evaluate_expression(expr
);
1082 if (context_clash(f
, ctype
))
1083 warn(expr
->pos
, "argument %d used in wrong context", i
);
1085 ctype
= degenerate(expr
, ctype
, p
);
1088 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1089 target
= &int_ctype
;
1091 static char where
[30];
1092 examine_symbol_type(target
);
1093 sprintf(where
, "argument %d", i
);
1094 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1098 NEXT_PTR_LIST(argtype
);
1100 FINISH_PTR_LIST(argtype
);
1104 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1105 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1107 struct expression
*entry
;
1111 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1112 struct expression
**p
= THIS_ADDRESS(entry
);
1114 if (entry
->type
== EXPR_INDEX
) {
1115 current
= entry
->idx_to
;
1118 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1126 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1128 struct expression
*entry
;
1131 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1132 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1133 struct expression
**p
= THIS_ADDRESS(entry
);
1135 if (entry
->type
== EXPR_IDENTIFIER
) {
1136 struct ident
*ident
= entry
->expr_ident
;
1137 /* We special-case the "already right place" case */
1138 if (sym
&& sym
->ident
== ident
)
1140 RESET_PTR_LIST(sym
);
1143 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1146 if (sym
->ident
== ident
)
1154 warn(expr
->pos
, "too many initializers for struct/union");
1158 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1162 FINISH_PTR_LIST(sym
);
1168 * Initializers are kind of like assignments. Except
1169 * they can be a hell of a lot more complex.
1171 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1173 struct expression
*expr
= *ep
;
1176 * Simple non-structure/array initializers are the simple
1177 * case, and look (and parse) largely like assignments.
1179 if (expr
->type
!= EXPR_INITIALIZER
) {
1181 struct symbol
*rtype
= evaluate_expression(expr
);
1183 struct expression
*pos
;
1185 // FIXME! char array[] = "string" special case
1186 // should _not_ degenerate.
1187 rtype
= degenerate(expr
, rtype
, ep
);
1189 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1190 /* strings are special: char arrays */
1191 if (rtype
->type
== SYM_ARRAY
)
1192 size
= get_expression_value(rtype
->array_size
);
1194 * Don't bother creating a position expression for
1195 * the simple initializer cases that don't need it.
1197 * We need a position if the initializer has a byte
1198 * offset, _or_ if we're initializing a bitfield.
1200 if (offset
|| ctype
->fieldwidth
) {
1201 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1202 pos
->init_offset
= offset
;
1203 pos
->init_sym
= ctype
;
1204 pos
->init_expr
= *ep
;
1205 pos
->ctype
= expr
->ctype
;
1212 expr
->ctype
= ctype
;
1213 if (ctype
->type
== SYM_NODE
)
1214 ctype
= ctype
->ctype
.base_type
;
1216 switch (ctype
->type
) {
1219 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1221 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1223 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1227 warn(expr
->pos
, "unexpected compound initializer");
1231 static struct symbol
*evaluate_cast(struct expression
*expr
)
1233 struct expression
*target
= expr
->cast_expression
;
1234 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1236 expr
->ctype
= ctype
;
1237 expr
->cast_type
= ctype
;
1240 * Special case: a cast can be followed by an
1241 * initializer, in which case we need to pass
1242 * the type value down to that initializer rather
1243 * than trying to evaluate it as an expression
1245 * A more complex case is when the initializer is
1246 * dereferenced as part of a post-fix expression.
1247 * We need to produce an expression that can be dereferenced.
1249 if (target
->type
== EXPR_INITIALIZER
) {
1250 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1251 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1253 sym
->ctype
.base_type
= ctype
;
1254 sym
->initializer
= expr
->cast_expression
;
1255 evaluate_symbol(sym
);
1257 addr
->ctype
= &ptr_ctype
;
1260 expr
->type
= EXPR_PREOP
;
1263 expr
->ctype
= ctype
;
1267 evaluate_expression(target
);
1270 * Casts of constant values are special: they
1271 * can be NULL, and thus need to be simplified
1274 if (target
->type
== EXPR_VALUE
)
1275 cast_value(expr
, ctype
, target
, target
->ctype
);
1281 * Evaluate a call expression with a symbol. This
1282 * should expand inline functions, and evaluate
1285 static int evaluate_symbol_call(struct expression
*expr
)
1287 struct expression
*fn
= expr
->fn
;
1288 struct symbol
*ctype
= fn
->ctype
;
1290 if (fn
->type
!= EXPR_PREOP
)
1293 if (ctype
->op
&& ctype
->op
->evaluate
)
1294 return ctype
->op
->evaluate(expr
);
1296 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1298 struct symbol
*curr
= current_fn
;
1299 unsigned long context
= current_context
;
1300 unsigned long mask
= current_contextmask
;
1302 current_context
|= ctype
->ctype
.context
;
1303 current_contextmask
|= ctype
->ctype
.contextmask
;
1304 current_fn
= ctype
->ctype
.base_type
;
1305 ret
= inline_function(expr
, ctype
);
1307 /* restore the old function context */
1309 current_context
= context
;
1310 current_contextmask
= mask
;
1317 static struct symbol
*evaluate_call(struct expression
*expr
)
1320 struct symbol
*ctype
, *sym
;
1321 struct expression
*fn
= expr
->fn
;
1322 struct expression_list
*arglist
= expr
->args
;
1324 if (!evaluate_expression(fn
))
1326 sym
= ctype
= fn
->ctype
;
1327 if (ctype
->type
== SYM_NODE
)
1328 ctype
= ctype
->ctype
.base_type
;
1329 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1330 ctype
= ctype
->ctype
.base_type
;
1331 if (!evaluate_arguments(sym
, ctype
, arglist
))
1333 if (sym
->type
== SYM_NODE
) {
1334 if (evaluate_symbol_call(expr
))
1337 if (sym
->type
== SYM_NODE
) {
1338 if (evaluate_symbol_call(expr
))
1341 if (ctype
->type
!= SYM_FN
) {
1342 warn(expr
->pos
, "not a function %.*s",
1343 sym
->ident
->len
, sym
->ident
->name
);
1346 args
= expression_list_size(expr
->args
);
1347 fnargs
= symbol_list_size(ctype
->arguments
);
1349 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1350 if (args
> fnargs
&& !ctype
->variadic
)
1351 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1352 expr
->ctype
= ctype
->ctype
.base_type
;
1356 struct symbol
*evaluate_expression(struct expression
*expr
)
1363 switch (expr
->type
) {
1365 warn(expr
->pos
, "value expression without a type");
1368 return evaluate_string(expr
);
1370 return evaluate_symbol_expression(expr
);
1372 if (!evaluate_expression(expr
->left
))
1374 if (!evaluate_expression(expr
->right
))
1376 return evaluate_binop(expr
);
1378 return evaluate_logical(expr
);
1380 if (!evaluate_expression(expr
->left
))
1382 if (!evaluate_expression(expr
->right
))
1384 return evaluate_comma(expr
);
1386 if (!evaluate_expression(expr
->left
))
1388 if (!evaluate_expression(expr
->right
))
1390 return evaluate_compare(expr
);
1391 case EXPR_ASSIGNMENT
:
1392 if (!evaluate_expression(expr
->left
))
1394 if (!evaluate_expression(expr
->right
))
1396 return evaluate_assignment(expr
);
1398 if (!evaluate_expression(expr
->unop
))
1400 return evaluate_preop(expr
);
1402 if (!evaluate_expression(expr
->unop
))
1404 return evaluate_postop(expr
);
1406 return evaluate_cast(expr
);
1408 return evaluate_sizeof(expr
);
1410 return evaluate_member_dereference(expr
);
1412 return evaluate_call(expr
);
1414 warn(expr
->pos
, "bitfield generated by parser");
1416 case EXPR_CONDITIONAL
:
1417 if (!evaluate_expression(expr
->conditional
))
1419 if (!evaluate_expression(expr
->cond_false
))
1421 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1423 return evaluate_conditional(expr
);
1424 case EXPR_STATEMENT
:
1425 expr
->ctype
= evaluate_statement(expr
->statement
);
1429 expr
->ctype
= &ptr_ctype
;
1433 /* Evaluate the type of the symbol .. */
1434 evaluate_symbol(expr
->symbol
);
1435 /* .. but the type of the _expression_ is a "type" */
1436 expr
->ctype
= &type_ctype
;
1439 /* These can not exist as stand-alone expressions */
1440 case EXPR_INITIALIZER
:
1441 case EXPR_IDENTIFIER
:
1444 warn(expr
->pos
, "internal front-end error: initializer in expression");
1450 void check_duplicates(struct symbol
*sym
)
1452 struct symbol
*next
= sym
;
1454 while ((next
= next
->same_symbol
) != NULL
) {
1455 const char *typediff
;
1456 evaluate_symbol(next
);
1457 typediff
= type_difference(sym
, next
, 0, 0);
1459 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1460 show_ident(sym
->ident
),
1461 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1467 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1469 struct symbol
*base_type
;
1474 sym
= examine_symbol_type(sym
);
1475 base_type
= sym
->ctype
.base_type
;
1479 /* Evaluate the initializers */
1480 if (sym
->initializer
) {
1481 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1482 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1483 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1484 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1485 base_type
->bit_size
= bit_size
;
1486 sym
->array_size
= base_type
->array_size
;
1487 sym
->bit_size
= bit_size
;
1491 /* And finally, evaluate the body of the symbol too */
1492 if (base_type
->type
== SYM_FN
) {
1495 FOR_EACH_PTR(base_type
->arguments
, s
) {
1499 if (base_type
->stmt
) {
1500 current_fn
= base_type
;
1501 current_contextmask
= sym
->ctype
.contextmask
;
1502 current_context
= sym
->ctype
.context
;
1503 evaluate_statement(base_type
->stmt
);
1510 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1512 struct expression
*expr
= stmt
->expression
;
1513 struct symbol
*ctype
, *fntype
;
1515 fntype
= current_fn
->ctype
.base_type
;
1516 if (!fntype
|| fntype
== &void_ctype
) {
1518 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1523 warn(stmt
->pos
, "return with no return value");
1526 ctype
= evaluate_expression(expr
);
1529 ctype
= degenerate(expr
, ctype
, &expr
);
1530 expr
->ctype
= ctype
;
1531 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1532 stmt
->expression
= expr
;
1536 static void evaluate_if_statement(struct statement
*stmt
)
1538 struct expression
*expr
= stmt
->if_conditional
;
1539 struct symbol
*ctype
;
1543 if (expr
->type
== EXPR_ASSIGNMENT
)
1544 warn(expr
->pos
, "assignment expression in conditional");
1546 ctype
= evaluate_expression(expr
);
1550 evaluate_statement(stmt
->if_true
);
1551 evaluate_statement(stmt
->if_false
);
1554 struct symbol
*evaluate_statement(struct statement
*stmt
)
1559 switch (stmt
->type
) {
1561 return evaluate_return_expression(stmt
);
1563 case STMT_EXPRESSION
:
1564 return evaluate_expression(stmt
->expression
);
1566 case STMT_COMPOUND
: {
1567 struct statement
*s
;
1568 struct symbol
*type
= NULL
;
1571 /* Evaluate each symbol in the compound statement */
1572 FOR_EACH_PTR(stmt
->syms
, sym
) {
1573 evaluate_symbol(sym
);
1575 evaluate_symbol(stmt
->ret
);
1578 * Then, evaluate each statement, making the type of the
1579 * compound statement be the type of the last statement
1582 FOR_EACH_PTR(stmt
->stmts
, s
) {
1583 type
= evaluate_statement(s
);
1588 evaluate_if_statement(stmt
);
1591 evaluate_expression(stmt
->iterator_pre_condition
);
1592 evaluate_expression(stmt
->iterator_post_condition
);
1593 evaluate_statement(stmt
->iterator_pre_statement
);
1594 evaluate_statement(stmt
->iterator_statement
);
1595 evaluate_statement(stmt
->iterator_post_statement
);
1598 evaluate_expression(stmt
->switch_expression
);
1599 evaluate_statement(stmt
->switch_statement
);
1602 evaluate_expression(stmt
->case_expression
);
1603 evaluate_expression(stmt
->case_to
);
1604 evaluate_statement(stmt
->case_statement
);
1607 evaluate_statement(stmt
->label_statement
);
1610 evaluate_expression(stmt
->goto_expression
);
1615 /* FIXME! Do the asm parameter evaluation! */