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_ptr_type(struct symbol
*type
)
144 if (type
->type
== SYM_NODE
)
145 type
= type
->ctype
.base_type
;
146 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
149 static int is_int_type(struct symbol
*type
)
151 if (type
->type
== SYM_NODE
)
152 type
= type
->ctype
.base_type
;
153 return type
->ctype
.base_type
== &int_type
;
156 static struct symbol
*bad_expr_type(struct expression
*expr
)
158 warn(expr
->pos
, "incompatible types for operation");
162 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
164 struct expression
*left
= *lp
, *right
= *rp
;
165 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
167 if (ltype
->type
== SYM_NODE
)
168 ltype
= ltype
->ctype
.base_type
;
169 if (rtype
->type
== SYM_NODE
)
170 rtype
= rtype
->ctype
.base_type
;
171 /* Integer promotion? */
172 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
174 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
176 if (is_int_type(ltype
) && is_int_type(rtype
)) {
177 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
179 /* Don't bother promoting same-size entities, it only adds clutter */
180 if (ltype
->bit_size
!= ctype
->bit_size
)
181 *lp
= cast_to(left
, ctype
);
182 if (rtype
->bit_size
!= ctype
->bit_size
)
183 *rp
= cast_to(right
, ctype
);
189 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
191 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
196 return bad_expr_type(expr
);
199 static inline int lvalue_expression(struct expression
*expr
)
201 while (expr
->type
== EXPR_CAST
)
202 expr
= expr
->cast_expression
;
203 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
206 /* Arrays degenerate into pointers on pointer arithmetic */
207 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
209 struct symbol
*base
= ctype
;
211 if (ctype
->type
== SYM_NODE
)
212 base
= ctype
->ctype
.base_type
;
213 if (base
->type
== SYM_ARRAY
|| base
->type
== SYM_FN
) {
214 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
215 struct expression
*n
= alloc_expression(expr
->pos
, 0);
216 struct expression
*ptr
;
218 merge_type(sym
, ctype
);
219 if (base
->type
== SYM_FN
)
221 merge_type(sym
, base
);
222 sym
->bit_size
= BITS_IN_POINTER
;
231 * This all really assumes that we got the degenerate
232 * array as an lvalue (ie a dereference). If that
233 * is not the case, then holler - because we've screwed
236 if (!lvalue_expression(ptr
))
237 warn(ptr
->pos
, "internal error: strange degenerate array case");
242 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
244 struct symbol
*ctype
;
245 struct symbol
*ptr_type
= ptr
->ctype
;
246 struct symbol
*i_type
= i
->ctype
;
249 if (i_type
->type
== SYM_NODE
)
250 i_type
= i_type
->ctype
.base_type
;
251 if (ptr_type
->type
== SYM_NODE
)
252 ptr_type
= ptr_type
->ctype
.base_type
;
254 if (i_type
->type
== SYM_ENUM
)
256 if (!is_int_type(i_type
))
257 return bad_expr_type(expr
);
260 examine_symbol_type(ctype
);
262 ctype
= degenerate(expr
, ctype
, &ptr
);
263 bit_size
= ctype
->bit_size
;
265 /* Special case: adding zero commonly happens as a result of 'array[0]' */
266 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
268 } else if (bit_size
> BITS_IN_CHAR
) {
269 struct expression
*add
= expr
;
270 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
271 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
273 val
->ctype
= size_t_ctype
;
274 val
->value
= bit_size
>> 3;
277 mul
->ctype
= size_t_ctype
;
281 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
290 static struct symbol
*evaluate_add(struct expression
*expr
)
292 struct expression
*left
= expr
->left
, *right
= expr
->right
;
293 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
295 if (is_ptr_type(ltype
))
296 return evaluate_ptr_add(expr
, left
, right
);
298 if (is_ptr_type(rtype
))
299 return evaluate_ptr_add(expr
, right
, left
);
301 // FIXME! FP promotion
302 return evaluate_int_binop(expr
);
305 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
306 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED)
308 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
309 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
312 unsigned long mod1
, mod2
, diff
;
313 unsigned long as1
, as2
;
315 if (target
== source
)
317 if (!target
|| !source
)
318 return "different types";
320 * Peel of per-node information.
321 * FIXME! Check alignment, address space, and context too here!
323 if (target
->type
== SYM_NODE
)
324 target
= target
->ctype
.base_type
;
325 if (source
->type
== SYM_NODE
)
326 source
= source
->ctype
.base_type
;
328 if (target
== source
)
330 if (!target
|| !source
)
331 return "different types";
333 mod1
= target
->ctype
.modifiers
;
334 as1
= target
->ctype
.as
;
335 mod2
= source
->ctype
.modifiers
;
336 as2
= source
->ctype
.as
;
338 if (target
->type
!= source
->type
) {
339 int type1
= target
->type
;
340 int type2
= source
->type
;
342 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
343 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
344 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
346 if ((type1
== SYM_PTR
) && (target
->ctype
.base_type
->type
== SYM_FN
)) {
347 target
= target
->ctype
.base_type
;
351 if ((type2
== SYM_PTR
) && (source
->ctype
.base_type
->type
== SYM_FN
)) {
352 source
= source
->ctype
.base_type
;
357 return "different base types";
360 /* Must be same address space to be comparable */
362 return "different address spaces";
364 /* Ignore differences in storage types, sign, or addressability */
365 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
367 mod1
&= diff
& ~target_mod_ignore
;
368 mod2
&= diff
& ~source_mod_ignore
;
370 if ((mod1
| mod2
) & MOD_SIZE
)
371 return "different type sizes";
372 return "different modifiers";
376 if (target
->type
== SYM_FN
) {
378 struct symbol
*arg1
, *arg2
;
379 if (target
->variadic
!= source
->variadic
)
380 return "incompatible variadic arguments";
381 PREPARE_PTR_LIST(target
->arguments
, arg1
);
382 PREPARE_PTR_LIST(source
->arguments
, arg2
);
386 diff
= type_difference(arg1
, arg2
, 0, 0);
388 static char argdiff
[80];
389 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
398 FINISH_PTR_LIST(arg2
);
399 FINISH_PTR_LIST(arg1
);
402 target
= target
->ctype
.base_type
;
403 source
= source
->ctype
.base_type
;
408 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
410 /* NULL expression? Just return the type of the "other side" */
411 if (r
->type
== EXPR_VALUE
&& !r
->value
)
413 if (l
->type
== EXPR_VALUE
&& !l
->value
)
419 * Ignore differences in "volatile" and "const"ness when
420 * subtracting pointers
422 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
424 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
426 const char *typediff
;
427 struct symbol
*ctype
;
428 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
431 * If it is an integer subtract: the ptr add case will do the
434 if (!is_ptr_type(rtype
))
435 return evaluate_ptr_add(expr
, l
, r
);
438 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
440 ctype
= common_ptr_type(l
, r
);
442 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
446 examine_symbol_type(ctype
);
448 /* Figure out the base type we point to */
449 if (ctype
->type
== SYM_NODE
)
450 ctype
= ctype
->ctype
.base_type
;
451 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
452 warn(expr
->pos
, "subtraction of functions? Share your drugs");
455 ctype
= ctype
->ctype
.base_type
;
457 expr
->ctype
= ssize_t_ctype
;
458 if (ctype
->bit_size
> BITS_IN_CHAR
) {
459 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
460 struct expression
*div
= expr
;
461 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
463 val
->ctype
= size_t_ctype
;
464 val
->value
= ctype
->bit_size
>> 3;
467 sub
->ctype
= ssize_t_ctype
;
476 return ssize_t_ctype
;
479 static struct symbol
*evaluate_sub(struct expression
*expr
)
481 struct expression
*left
= expr
->left
, *right
= expr
->right
;
482 struct symbol
*ltype
= left
->ctype
;
484 if (is_ptr_type(ltype
))
485 return evaluate_ptr_sub(expr
, left
, right
);
487 // FIXME! FP promotion
488 return evaluate_int_binop(expr
);
491 static struct symbol
*evaluate_logical(struct expression
*expr
)
493 if (!evaluate_expression(expr
->left
))
495 if (!evaluate_expression(expr
->right
))
497 expr
->ctype
= &bool_ctype
;
501 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
503 // FIXME! Floating-point promotion!
504 return evaluate_int_binop(expr
);
507 static struct symbol
*evaluate_binop(struct expression
*expr
)
510 // addition can take ptr+int, fp and int
512 return evaluate_add(expr
);
514 // subtraction can take ptr-ptr, fp and int
516 return evaluate_sub(expr
);
518 // Arithmetic operations can take fp and int
519 case '*': case '/': case '%':
520 return evaluate_arithmetic(expr
);
522 // The rest are integer operations (bitops)
523 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
526 return evaluate_int_binop(expr
);
530 static struct symbol
*evaluate_comma(struct expression
*expr
)
532 expr
->ctype
= expr
->right
->ctype
;
536 static struct symbol
*evaluate_compare(struct expression
*expr
)
538 struct expression
*left
= expr
->left
, *right
= expr
->right
;
539 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
540 struct symbol
*ctype
;
543 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
544 expr
->ctype
= &bool_ctype
;
545 // FIXME! Check the types for compatibility
549 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
551 expr
->ctype
= &bool_ctype
;
555 return bad_expr_type(expr
);
558 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
560 /* Integer promotion? */
561 if (ltype
->type
== SYM_NODE
)
562 ltype
= ltype
->ctype
.base_type
;
563 if (rtype
->type
== SYM_NODE
)
564 rtype
= rtype
->ctype
.base_type
;
565 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
567 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
569 return (is_int_type(ltype
) && is_int_type(rtype
));
572 static int is_null_ptr(struct expression
*expr
)
574 return (expr
->type
== EXPR_VALUE
&&
579 * FIXME!! This should do casts, array degeneration etc..
581 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
583 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
585 if (ltype
->type
== SYM_NODE
)
586 ltype
= ltype
->ctype
.base_type
;
588 if (ltype
->type
== SYM_PTR
) {
589 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
593 if (rtype
->type
== SYM_NODE
)
594 rtype
= rtype
->ctype
.base_type
;
596 if (rtype
->type
== SYM_PTR
) {
597 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
603 static struct symbol
*do_degenerate(struct expression
**ep
)
605 struct expression
*expr
= *ep
;
606 return degenerate(expr
, expr
->ctype
, ep
);
609 static struct symbol
* evaluate_conditional(struct expression
*expr
)
611 struct expression
*cond
, *true, *false;
612 struct symbol
*ctype
, *ltype
, *rtype
;
613 const char * typediff
;
615 ctype
= do_degenerate(&expr
->conditional
);
616 cond
= expr
->conditional
;
620 if (expr
->cond_true
) {
621 ltype
= do_degenerate(&expr
->cond_true
);
622 true = expr
->cond_true
;
625 rtype
= do_degenerate(&expr
->cond_false
);
626 false = expr
->cond_false
;
629 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
631 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
633 ctype
= compatible_ptr_type(true, expr
->cond_false
);
635 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
645 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
646 struct expression
**rp
, struct symbol
*source
, const char *where
)
648 const char *typediff
;
652 /* It's ok if the target is more volatile or const than the source */
653 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
657 if (compatible_integer_types(target
, source
)) {
658 if (target
->bit_size
!= source
->bit_size
)
659 *rp
= cast_to(*rp
, target
);
663 /* Pointer destination? */
665 target_as
= t
->ctype
.as
;
666 if (t
->type
== SYM_NODE
) {
667 t
= t
->ctype
.base_type
;
668 target_as
|= t
->ctype
.as
;
670 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
) {
671 struct expression
*right
= *rp
;
672 struct symbol
*s
= source
;
675 // NULL pointer is always ok
676 if (right
->type
== EXPR_VALUE
&& !right
->value
)
679 /* "void *" matches anything as long as the address space is ok */
680 source_as
= s
->ctype
.as
;
681 if (s
->type
== SYM_NODE
) {
682 s
= s
->ctype
.base_type
;
683 source_as
|= s
->ctype
.as
;
685 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
686 s
= s
->ctype
.base_type
;
687 t
= t
->ctype
.base_type
;
688 if (s
== &void_ctype
|| t
== &void_ctype
)
694 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
695 warn(expr
->pos
, " expected %s", show_typename(target
));
696 warn(expr
->pos
, " got %s", show_typename(source
));
701 * FIXME!! This is wrong from a double evaluation standpoint. We can't
702 * just expand the expression twice, that would make any side effects
705 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
708 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
709 static const int op_trans
[] = {
710 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
711 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
712 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
713 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
714 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
715 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
716 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
717 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
718 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
719 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
722 subexpr
->left
= left
;
723 subexpr
->right
= right
;
724 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
726 expr
->right
= subexpr
;
727 return evaluate_binop(subexpr
);
730 static struct symbol
*evaluate_assignment(struct expression
*expr
)
732 struct expression
*left
= expr
->left
, *right
= expr
->right
;
733 struct symbol
*ltype
, *rtype
;
736 rtype
= right
->ctype
;
737 if (expr
->op
!= '=') {
738 rtype
= evaluate_binop_assignment(expr
, left
, right
);
744 if (!lvalue_expression(left
)) {
745 warn(expr
->pos
, "not an lvalue");
749 rtype
= degenerate(right
, rtype
, &expr
->right
);
751 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
754 if (ltype
->type
== SYM_NODE
)
755 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
761 static struct symbol
*evaluate_addressof(struct expression
*expr
)
763 struct symbol
*ctype
, *symbol
;
764 struct expression
*op
= expr
->unop
;
766 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
767 warn(expr
->pos
, "not addressable");
771 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
772 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
773 symbol
->bit_size
= BITS_IN_POINTER
;
776 if (ctype
->type
== SYM_NODE
) {
777 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
778 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
779 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
780 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
782 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
783 symbol
->ctype
.as
= ctype
->ctype
.as
;
784 symbol
->ctype
.context
= ctype
->ctype
.context
;
785 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
786 ctype
= ctype
->ctype
.base_type
;
789 symbol
->ctype
.base_type
= ctype
;
791 expr
->ctype
= symbol
;
796 static struct symbol
*evaluate_dereference(struct expression
*expr
)
798 struct expression
*op
= expr
->unop
;
799 struct symbol
*ctype
= op
->ctype
, *sym
;
801 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
802 sym
->ctype
= ctype
->ctype
;
803 if (ctype
->type
== SYM_NODE
) {
804 ctype
= ctype
->ctype
.base_type
;
805 merge_type(sym
, ctype
);
807 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
808 warn(expr
->pos
, "cannot derefence this type");
812 ctype
= ctype
->ctype
.base_type
;
813 examine_symbol_type(ctype
);
815 warn(expr
->pos
, "undefined type");
819 sym
->bit_size
= ctype
->bit_size
;
820 sym
->array_size
= ctype
->array_size
;
822 /* Simplify: *&(expr) => (expr) */
823 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
832 * Unary post-ops: x++ and x--
834 static struct symbol
*evaluate_postop(struct expression
*expr
)
836 struct expression
*op
= expr
->unop
;
837 struct symbol
*ctype
= op
->ctype
;
839 if (!lvalue_expression(expr
->unop
)) {
840 warn(expr
->pos
, "need lvalue expression for ++/--");
847 static struct symbol
*evaluate_preop(struct expression
*expr
)
849 struct symbol
*ctype
= expr
->unop
->ctype
;
858 return evaluate_dereference(expr
);
861 return evaluate_addressof(expr
);
863 case SPECIAL_INCREMENT
:
864 case SPECIAL_DECREMENT
:
866 * From a type evaluation standpoint the pre-ops are
867 * the same as the postops
869 return evaluate_postop(expr
);
882 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
884 struct ptr_list
*head
= (struct ptr_list
*)_list
;
885 struct ptr_list
*list
= head
;
891 for (i
= 0; i
< list
->nr
; i
++) {
892 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
894 if (sym
->ident
!= ident
)
896 *offset
= sym
->offset
;
899 struct symbol
*ctype
= sym
->ctype
.base_type
;
903 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
905 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
908 *offset
+= sym
->offset
;
912 } while ((list
= list
->next
) != head
);
916 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
918 struct expression
*add
;
923 /* Create a new add-expression */
924 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
926 add
->ctype
= &ptr_ctype
;
928 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
929 add
->right
->ctype
= &int_ctype
;
930 add
->right
->value
= offset
;
935 /* structure/union dereference */
936 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
939 struct symbol
*ctype
, *member
, *sym
;
940 struct expression
*deref
= expr
->deref
, *add
;
941 struct ident
*ident
= expr
->member
;
945 if (!evaluate_expression(deref
))
948 warn(expr
->pos
, "bad member name");
952 ctype
= deref
->ctype
;
953 address_space
= ctype
->ctype
.as
;
954 mod
= ctype
->ctype
.modifiers
;
955 if (ctype
->type
== SYM_NODE
) {
956 ctype
= ctype
->ctype
.base_type
;
957 address_space
|= ctype
->ctype
.as
;
958 mod
|= ctype
->ctype
.modifiers
;
960 if (expr
->op
== SPECIAL_DEREFERENCE
) {
961 /* Arrays will degenerate into pointers for '->' */
962 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
963 warn(expr
->pos
, "expected a pointer to a struct/union");
966 mod
= ctype
->ctype
.modifiers
;
967 address_space
= ctype
->ctype
.as
;
968 ctype
= ctype
->ctype
.base_type
;
969 if (ctype
->type
== SYM_NODE
) {
970 mod
|= ctype
->ctype
.modifiers
;
971 address_space
|= ctype
->ctype
.as
;
972 ctype
= ctype
->ctype
.base_type
;
975 if (!lvalue_expression(deref
)) {
976 warn(deref
->pos
, "expected lvalue for member dereference");
982 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
983 warn(expr
->pos
, "expected structure or union");
987 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
989 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
990 const char *name
= "<unnamed>";
993 name
= ctype
->ident
->name
;
994 namelen
= ctype
->ident
->len
;
996 warn(expr
->pos
, "no member '%s' in %s %.*s",
997 show_ident(ident
), type
, namelen
, name
);
1001 add
= evaluate_offset(deref
, offset
);
1003 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1004 sym
->bit_size
= member
->bit_size
;
1005 sym
->array_size
= member
->array_size
;
1006 sym
->ctype
= member
->ctype
;
1007 sym
->ctype
.modifiers
= mod
;
1008 sym
->ctype
.as
= address_space
;
1009 ctype
= member
->ctype
.base_type
;
1010 if (ctype
->type
== SYM_BITFIELD
) {
1011 ctype
= ctype
->ctype
.base_type
;
1012 expr
->type
= EXPR_BITFIELD
;
1013 expr
->bitpos
= member
->bit_offset
;
1014 expr
->nrbits
= member
->fieldwidth
;
1015 expr
->address
= add
;
1017 expr
->type
= EXPR_PREOP
;
1026 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1030 if (expr
->cast_type
) {
1031 examine_symbol_type(expr
->cast_type
);
1032 size
= expr
->cast_type
->bit_size
;
1034 if (!evaluate_expression(expr
->cast_expression
))
1036 size
= expr
->cast_expression
->ctype
->bit_size
;
1039 warn(expr
->pos
, "cannot size expression");
1042 expr
->type
= EXPR_VALUE
;
1043 expr
->value
= size
>> 3;
1044 expr
->ctype
= size_t_ctype
;
1045 return size_t_ctype
;
1048 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1050 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1051 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1055 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1057 struct expression
*expr
;
1058 struct symbol_list
*argument_types
= fn
->arguments
;
1059 struct symbol
*argtype
;
1062 PREPARE_PTR_LIST(argument_types
, argtype
);
1063 FOR_EACH_PTR (head
, expr
) {
1064 struct expression
**p
= THIS_ADDRESS(expr
);
1065 struct symbol
*ctype
, *target
;
1066 ctype
= evaluate_expression(expr
);
1071 if (context_clash(f
, ctype
))
1072 warn(expr
->pos
, "argument %d used in wrong context", i
);
1074 ctype
= degenerate(expr
, ctype
, p
);
1077 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1078 target
= &int_ctype
;
1080 static char where
[30];
1081 examine_symbol_type(target
);
1082 sprintf(where
, "argument %d", i
);
1083 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1087 NEXT_PTR_LIST(argtype
);
1089 FINISH_PTR_LIST(argtype
);
1093 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1094 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1096 struct expression
*entry
;
1100 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1101 struct expression
**p
= THIS_ADDRESS(entry
);
1103 if (entry
->type
== EXPR_INDEX
) {
1104 current
= entry
->idx_to
;
1107 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1115 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1117 struct expression
*entry
;
1120 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1121 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1122 struct expression
**p
= THIS_ADDRESS(entry
);
1124 if (entry
->type
== EXPR_IDENTIFIER
) {
1125 struct ident
*ident
= entry
->expr_ident
;
1126 /* We special-case the "already right place" case */
1127 if (sym
&& sym
->ident
== ident
)
1129 RESET_PTR_LIST(sym
);
1132 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1135 if (sym
->ident
== ident
)
1143 warn(expr
->pos
, "too many initializers for struct/union");
1147 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1151 FINISH_PTR_LIST(sym
);
1157 * Initializers are kind of like assignments. Except
1158 * they can be a hell of a lot more complex.
1160 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1162 struct expression
*expr
= *ep
;
1165 * Simple non-structure/array initializers are the simple
1166 * case, and look (and parse) largely like assignments.
1168 if (expr
->type
!= EXPR_INITIALIZER
) {
1170 struct symbol
*rtype
= evaluate_expression(expr
);
1172 struct expression
*pos
;
1174 // FIXME! char array[] = "string" special case
1175 // should _not_ degenerate.
1176 rtype
= degenerate(expr
, rtype
, ep
);
1178 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1179 /* strings are special: char arrays */
1180 if (rtype
->type
== SYM_ARRAY
)
1181 size
= get_expression_value(rtype
->array_size
);
1183 * Don't bother creating a position expression for
1184 * the simple initializer cases that don't need it.
1186 * We need a position if the initializer has a byte
1187 * offset, _or_ if we're initializing a bitfield.
1189 if (offset
|| ctype
->fieldwidth
) {
1190 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1191 pos
->init_offset
= offset
;
1192 pos
->init_sym
= ctype
;
1193 pos
->init_expr
= *ep
;
1194 pos
->ctype
= expr
->ctype
;
1201 expr
->ctype
= ctype
;
1202 if (ctype
->type
== SYM_NODE
)
1203 ctype
= ctype
->ctype
.base_type
;
1205 switch (ctype
->type
) {
1208 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1210 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1212 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1216 warn(expr
->pos
, "unexpected compound initializer");
1220 static struct symbol
*evaluate_cast(struct expression
*expr
)
1222 struct expression
*target
= expr
->cast_expression
;
1223 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1225 expr
->ctype
= ctype
;
1226 expr
->cast_type
= ctype
;
1229 * Special case: a cast can be followed by an
1230 * initializer, in which case we need to pass
1231 * the type value down to that initializer rather
1232 * than trying to evaluate it as an expression
1234 * A more complex case is when the initializer is
1235 * dereferenced as part of a post-fix expression.
1236 * We need to produce an expression that can be dereferenced.
1238 if (target
->type
== EXPR_INITIALIZER
) {
1239 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1240 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1242 sym
->ctype
.base_type
= ctype
;
1243 sym
->initializer
= expr
->cast_expression
;
1244 evaluate_symbol(sym
);
1246 addr
->ctype
= &ptr_ctype
;
1249 expr
->type
= EXPR_PREOP
;
1252 expr
->ctype
= ctype
;
1256 evaluate_expression(target
);
1259 * Casts of constant values are special: they
1260 * can be NULL, and thus need to be simplified
1263 if (target
->type
== EXPR_VALUE
)
1264 cast_value(expr
, ctype
, target
, target
->ctype
);
1270 * Evaluate a call expression with a symbol. This
1271 * should expand inline functions, and evaluate
1274 static int evaluate_symbol_call(struct expression
*expr
)
1276 struct expression
*fn
= expr
->fn
;
1277 struct symbol
*ctype
= fn
->ctype
;
1279 if (fn
->type
!= EXPR_PREOP
)
1282 if (ctype
->op
&& ctype
->op
->evaluate
)
1283 return ctype
->op
->evaluate(expr
);
1285 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1287 struct symbol
*curr
= current_fn
;
1288 unsigned long context
= current_context
;
1289 unsigned long mask
= current_contextmask
;
1291 current_context
|= ctype
->ctype
.context
;
1292 current_contextmask
|= ctype
->ctype
.contextmask
;
1293 current_fn
= ctype
->ctype
.base_type
;
1294 ret
= inline_function(expr
, ctype
);
1296 /* restore the old function context */
1298 current_context
= context
;
1299 current_contextmask
= mask
;
1306 static struct symbol
*evaluate_call(struct expression
*expr
)
1309 struct symbol
*ctype
, *sym
;
1310 struct expression
*fn
= expr
->fn
;
1311 struct expression_list
*arglist
= expr
->args
;
1313 if (!evaluate_expression(fn
))
1315 sym
= ctype
= fn
->ctype
;
1316 if (ctype
->type
== SYM_NODE
)
1317 ctype
= ctype
->ctype
.base_type
;
1318 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1319 ctype
= ctype
->ctype
.base_type
;
1320 if (!evaluate_arguments(sym
, ctype
, arglist
))
1322 if (sym
->type
== SYM_NODE
) {
1323 if (evaluate_symbol_call(expr
))
1326 if (sym
->type
== SYM_NODE
) {
1327 if (evaluate_symbol_call(expr
))
1330 if (ctype
->type
!= SYM_FN
) {
1331 warn(expr
->pos
, "not a function %.*s",
1332 sym
->ident
->len
, sym
->ident
->name
);
1335 args
= expression_list_size(expr
->args
);
1336 fnargs
= symbol_list_size(ctype
->arguments
);
1338 warn(expr
->pos
, "not enough arguments for function %.*s",
1339 sym
->ident
->len
, sym
->ident
->name
);
1340 if (args
> fnargs
&& !ctype
->variadic
)
1341 warn(expr
->pos
, "too many arguments for function %.*s",
1342 sym
->ident
->len
, sym
->ident
->name
);
1343 expr
->ctype
= ctype
->ctype
.base_type
;
1347 struct symbol
*evaluate_expression(struct expression
*expr
)
1354 switch (expr
->type
) {
1356 warn(expr
->pos
, "value expression without a type");
1359 return evaluate_string(expr
);
1361 return evaluate_symbol_expression(expr
);
1363 if (!evaluate_expression(expr
->left
))
1365 if (!evaluate_expression(expr
->right
))
1367 return evaluate_binop(expr
);
1369 return evaluate_logical(expr
);
1371 if (!evaluate_expression(expr
->left
))
1373 if (!evaluate_expression(expr
->right
))
1375 return evaluate_comma(expr
);
1377 if (!evaluate_expression(expr
->left
))
1379 if (!evaluate_expression(expr
->right
))
1381 return evaluate_compare(expr
);
1382 case EXPR_ASSIGNMENT
:
1383 if (!evaluate_expression(expr
->left
))
1385 if (!evaluate_expression(expr
->right
))
1387 return evaluate_assignment(expr
);
1389 if (!evaluate_expression(expr
->unop
))
1391 return evaluate_preop(expr
);
1393 if (!evaluate_expression(expr
->unop
))
1395 return evaluate_postop(expr
);
1397 return evaluate_cast(expr
);
1399 return evaluate_sizeof(expr
);
1401 return evaluate_member_dereference(expr
);
1403 return evaluate_call(expr
);
1405 warn(expr
->pos
, "bitfield generated by parser");
1407 case EXPR_CONDITIONAL
:
1408 if (!evaluate_expression(expr
->conditional
))
1410 if (!evaluate_expression(expr
->cond_false
))
1412 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1414 return evaluate_conditional(expr
);
1415 case EXPR_STATEMENT
:
1416 expr
->ctype
= evaluate_statement(expr
->statement
);
1420 expr
->ctype
= &ptr_ctype
;
1423 /* These can not exist as stand-alone expressions */
1424 case EXPR_INITIALIZER
:
1425 case EXPR_IDENTIFIER
:
1428 warn(expr
->pos
, "internal front-end error: initializer in expression");
1434 void check_duplicates(struct symbol
*sym
)
1436 struct symbol
*next
= sym
;
1438 while ((next
= next
->same_symbol
) != NULL
) {
1439 const char *typediff
;
1440 evaluate_symbol(next
);
1441 typediff
= type_difference(sym
, next
, 0, 0);
1443 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1444 show_ident(sym
->ident
),
1445 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1451 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1453 struct symbol
*base_type
;
1458 sym
= examine_symbol_type(sym
);
1459 base_type
= sym
->ctype
.base_type
;
1463 /* Evaluate the initializers */
1464 if (sym
->initializer
) {
1465 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1466 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1467 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1468 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1469 base_type
->bit_size
= bit_size
;
1470 sym
->array_size
= base_type
->array_size
;
1471 sym
->bit_size
= bit_size
;
1475 /* And finally, evaluate the body of the symbol too */
1476 if (base_type
->type
== SYM_FN
) {
1479 FOR_EACH_PTR(base_type
->arguments
, s
) {
1483 if (base_type
->stmt
) {
1484 current_fn
= base_type
;
1485 current_contextmask
= sym
->ctype
.contextmask
;
1486 current_context
= sym
->ctype
.context
;
1487 evaluate_statement(base_type
->stmt
);
1494 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1496 struct expression
*expr
= stmt
->expression
;
1497 struct symbol
*ctype
, *fntype
;
1499 fntype
= current_fn
->ctype
.base_type
;
1500 if (!fntype
|| fntype
== &void_ctype
) {
1502 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1507 warn(stmt
->pos
, "return with no return value");
1510 ctype
= evaluate_expression(expr
);
1513 ctype
= degenerate(expr
, ctype
, &expr
);
1514 expr
->ctype
= ctype
;
1515 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1516 stmt
->expression
= expr
;
1520 static void evaluate_if_statement(struct statement
*stmt
)
1522 struct expression
*expr
= stmt
->if_conditional
;
1523 struct symbol
*ctype
;
1527 if (expr
->type
== EXPR_ASSIGNMENT
)
1528 warn(expr
->pos
, "assignment expression in conditional");
1530 ctype
= evaluate_expression(expr
);
1534 evaluate_statement(stmt
->if_true
);
1535 evaluate_statement(stmt
->if_false
);
1538 struct symbol
*evaluate_statement(struct statement
*stmt
)
1543 switch (stmt
->type
) {
1545 return evaluate_return_expression(stmt
);
1547 case STMT_EXPRESSION
:
1548 return evaluate_expression(stmt
->expression
);
1550 case STMT_COMPOUND
: {
1551 struct statement
*s
;
1552 struct symbol
*type
= NULL
;
1555 /* Evaluate each symbol in the compound statement */
1556 FOR_EACH_PTR(stmt
->syms
, sym
) {
1557 evaluate_symbol(sym
);
1559 evaluate_symbol(stmt
->ret
);
1562 * Then, evaluate each statement, making the type of the
1563 * compound statement be the type of the last statement
1566 FOR_EACH_PTR(stmt
->stmts
, s
) {
1567 type
= evaluate_statement(s
);
1572 evaluate_if_statement(stmt
);
1575 evaluate_expression(stmt
->iterator_pre_condition
);
1576 evaluate_expression(stmt
->iterator_post_condition
);
1577 evaluate_statement(stmt
->iterator_pre_statement
);
1578 evaluate_statement(stmt
->iterator_statement
);
1579 evaluate_statement(stmt
->iterator_post_statement
);
1582 evaluate_expression(stmt
->switch_expression
);
1583 evaluate_statement(stmt
->switch_statement
);
1586 evaluate_expression(stmt
->case_expression
);
1587 evaluate_expression(stmt
->case_to
);
1588 evaluate_statement(stmt
->case_statement
);
1591 evaluate_statement(stmt
->label_statement
);
1594 evaluate_expression(stmt
->goto_expression
);
1599 /* FIXME! Do the asm parameter evaluation! */
1602 /* These should not exist at evaluation time */
1604 case STMT_CONDFALSE
: