4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Evaluate constant expressions.
23 #include "expression.h"
25 static struct symbol
*current_fn
;
26 static int current_context
, current_contextmask
;
28 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
30 struct symbol
*sym
= expr
->symbol
;
31 struct symbol
*base_type
;
35 warn(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
36 expr
->type
= EXPR_VALUE
;
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 (base_type
->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
;
74 static struct symbol
*evaluate_string(struct expression
*expr
)
76 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
77 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
78 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
79 int length
= expr
->string
->length
;
81 sym
->array_size
= length
;
82 sym
->bit_size
= BITS_IN_CHAR
* length
;
83 sym
->ctype
.alignment
= 1;
84 sym
->ctype
.modifiers
= MOD_STATIC
| MOD_CONST
;
85 sym
->ctype
.base_type
= array
;
87 array
->array_size
= length
;
88 array
->bit_size
= BITS_IN_CHAR
* length
;
89 array
->ctype
.alignment
= 1;
90 array
->ctype
.modifiers
= MOD_STATIC
;
91 array
->ctype
.base_type
= &char_ctype
;
94 addr
->ctype
= &ptr_ctype
;
96 expr
->type
= EXPR_PREOP
;
103 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
105 unsigned long lmod
, rmod
, mod
;
110 if (left
->bit_size
> right
->bit_size
)
113 if (right
->bit_size
> left
->bit_size
)
116 /* Same size integers - promote to unsigned, promote to long */
117 lmod
= left
->ctype
.modifiers
;
118 rmod
= right
->ctype
.modifiers
;
124 return ctype_integer(mod
);
127 static struct symbol
* cast_value(struct expression
*expr
, struct symbol
*newtype
,
128 struct expression
*old
, struct symbol
*oldtype
)
130 int old_size
= oldtype
->bit_size
;
131 int new_size
= newtype
->bit_size
;
132 long long value
, mask
, ormask
, andmask
;
135 // FIXME! We don't handle FP casts of constant values yet
136 if (newtype
->ctype
.base_type
== &fp_type
)
138 if (oldtype
->ctype
.base_type
== &fp_type
)
141 // For pointers and integers, we can just move the value around
142 expr
->type
= EXPR_VALUE
;
143 if (old_size
== new_size
) {
144 expr
->value
= old
->value
;
148 // expand it to the full "long long" value
149 is_signed
= !(oldtype
->ctype
.modifiers
& MOD_UNSIGNED
);
150 mask
= 1ULL << (old_size
-1);
154 andmask
= mask
| (mask
-1);
158 value
= (value
& andmask
) | ormask
;
160 // Truncate it to the new size
161 mask
= 1ULL << (new_size
-1);
162 mask
= mask
| (mask
-1);
163 expr
->value
= value
& mask
;
167 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
169 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
171 expr
->cast_type
= type
;
172 expr
->cast_expression
= old
;
173 if (old
->type
== EXPR_VALUE
)
174 cast_value(expr
, type
, old
, old
->ctype
);
178 static int is_ptr_type(struct symbol
*type
)
180 if (type
->type
== SYM_NODE
)
181 type
= type
->ctype
.base_type
;
182 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
185 static int is_int_type(struct symbol
*type
)
187 if (type
->type
== SYM_NODE
)
188 type
= type
->ctype
.base_type
;
189 return type
->ctype
.base_type
== &int_type
;
192 static struct symbol
*bad_expr_type(struct expression
*expr
)
194 warn(expr
->pos
, "incompatible types for operation");
198 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
200 struct expression
*left
= *lp
, *right
= *rp
;
201 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
203 if (ltype
->type
== SYM_NODE
)
204 ltype
= ltype
->ctype
.base_type
;
205 if (rtype
->type
== SYM_NODE
)
206 rtype
= rtype
->ctype
.base_type
;
207 /* Integer promotion? */
208 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
210 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
212 if (is_int_type(ltype
) && is_int_type(rtype
)) {
213 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
215 /* Don't bother promoting same-size entities, it only adds clutter */
216 if (ltype
->bit_size
!= ctype
->bit_size
)
217 *lp
= cast_to(left
, ctype
);
218 if (rtype
->bit_size
!= ctype
->bit_size
)
219 *rp
= cast_to(right
, ctype
);
225 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
227 if (count
>= ctype
->bit_size
) {
228 warn(expr
->pos
, "shift too big for type");
229 count
&= ctype
->bit_size
-1;
235 * CAREFUL! We need to get the size and sign of the
238 static void simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
240 struct expression
*left
= expr
->left
, *right
= expr
->right
;
241 unsigned long long v
, l
, r
, mask
;
242 signed long long s
, sl
, sr
;
243 int is_signed
, shift
;
245 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
247 l
= left
->value
; r
= right
->value
;
248 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
249 mask
= 1ULL << (ctype
->bit_size
-1);
251 if (is_signed
&& (sl
& mask
))
253 if (is_signed
&& (sr
& mask
))
257 case '+': v
= l
+ r
; s
= v
; break;
258 case '-': v
= l
- r
; s
= v
; break;
259 case '&': v
= l
& r
; s
= v
; break;
260 case '|': v
= l
| r
; s
= v
; break;
261 case '^': v
= l
^ r
; s
= v
; break;
262 case '*': v
= l
* r
; s
= sl
* sr
; break;
263 case '/': if (!r
) return; v
= l
/ r
; s
= sl
/ sr
; break;
264 case '%': if (!r
) return; v
= l
% r
; s
= sl
% sr
; break;
265 case SPECIAL_LEFTSHIFT
: shift
= check_shift_count(expr
, ctype
, r
); v
= l
<< shift
; s
= v
; break;
266 case SPECIAL_RIGHTSHIFT
:shift
= check_shift_count(expr
, ctype
, r
); v
= l
>> shift
; s
= sl
>> shift
; break;
267 case '<': v
= l
< r
; s
= sl
< sr
; break;
268 case '>': v
= l
> r
; s
= sl
> sr
; break;
269 case SPECIAL_LTE
: v
= l
<= r
; s
= sl
<= sr
; break;
270 case SPECIAL_GTE
: v
= l
>= r
; s
= sl
>= sr
; break;
271 case SPECIAL_EQUAL
: v
= l
== r
; s
= v
; break;
272 case SPECIAL_NOTEQUAL
: v
= l
!= r
; s
= v
; break;
277 mask
= mask
| (mask
-1);
278 expr
->value
= v
& mask
;
279 expr
->type
= EXPR_VALUE
;
282 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
284 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
287 simplify_int_binop(expr
, ctype
);
290 return bad_expr_type(expr
);
293 static inline int lvalue_expression(struct expression
*expr
)
295 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
298 /* Arrays degenerate into pointers on pointer arithmetic */
299 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
301 struct symbol
*base
= ctype
;
303 if (ctype
->type
== SYM_NODE
)
304 base
= ctype
->ctype
.base_type
;
305 if (base
->type
== SYM_ARRAY
) {
306 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
307 struct expression
*ptr
;
309 merge_type(sym
, base
);
310 sym
->bit_size
= BITS_IN_POINTER
;
317 * This all really assumes that we got the degenerate
318 * array as an lvalue (ie a dereference). If that
319 * is not the case, then holler - because we've screwed
322 if (!lvalue_expression(ptr
))
323 warn(ptr
->pos
, "internal error: strange degenerate array case");
329 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
331 struct symbol
*ctype
;
332 struct symbol
*ptr_type
= ptr
->ctype
;
333 struct symbol
*i_type
= i
->ctype
;
336 if (i_type
->type
== SYM_NODE
)
337 i_type
= i_type
->ctype
.base_type
;
338 if (ptr_type
->type
== SYM_NODE
)
339 ptr_type
= ptr_type
->ctype
.base_type
;
341 if (i_type
->type
== SYM_ENUM
)
343 if (!is_int_type(i_type
))
344 return bad_expr_type(expr
);
347 examine_symbol_type(ctype
);
349 ctype
= degenerate(expr
, ctype
, &ptr
);
350 bit_size
= ctype
->bit_size
;
352 /* Special case: adding zero commonly happens as a result of 'array[0]' */
353 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
355 } else if (bit_size
> BITS_IN_CHAR
) {
356 struct expression
*add
= expr
;
357 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
358 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
360 val
->ctype
= size_t_ctype
;
361 val
->value
= bit_size
>> 3;
364 mul
->ctype
= size_t_ctype
;
367 simplify_int_binop(mul
, size_t_ctype
);
369 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
372 simplify_int_binop(add
, ctype
);
379 static struct symbol
*evaluate_add(struct expression
*expr
)
381 struct expression
*left
= expr
->left
, *right
= expr
->right
;
382 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
384 if (is_ptr_type(ltype
))
385 return evaluate_ptr_add(expr
, left
, right
);
387 if (is_ptr_type(rtype
))
388 return evaluate_ptr_add(expr
, right
, left
);
390 // FIXME! FP promotion
391 return evaluate_int_binop(expr
);
394 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
395 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
397 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
398 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
401 unsigned long mod1
, mod2
, diff
;
402 unsigned long as1
, as2
;
404 if (target
== source
)
406 if (!target
|| !source
)
407 return "different types";
409 * Peel of per-node information.
410 * FIXME! Check alignment, address space, and context too here!
412 if (target
->type
== SYM_NODE
)
413 target
= target
->ctype
.base_type
;
414 if (source
->type
== SYM_NODE
)
415 source
= source
->ctype
.base_type
;
416 mod1
= target
->ctype
.modifiers
;
417 as1
= target
->ctype
.as
;
418 mod2
= source
->ctype
.modifiers
;
419 as2
= source
->ctype
.as
;
421 if (target
->type
!= source
->type
) {
422 int type1
= target
->type
;
423 int type2
= source
->type
;
425 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
426 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
427 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
429 return "different base types";
432 /* Must be same address space to be comparable */
434 return "different address spaces";
436 /* Ignore differences in storage types, sign, or addressability */
437 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
439 mod1
&= diff
& ~target_mod_ignore
;
440 mod2
&= diff
& ~source_mod_ignore
;
442 if ((mod1
| mod2
) & MOD_SIZE
)
443 return "different type sizes";
444 return "different modifiers";
448 target
= target
->ctype
.base_type
;
449 source
= source
->ctype
.base_type
;
454 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
456 /* NULL expression? Just return the type of the "other side" */
457 if (r
->type
== EXPR_VALUE
&& !r
->value
)
459 if (l
->type
== EXPR_VALUE
&& !l
->value
)
465 * Ignore differences in "volatile" and "const"ness when
466 * subtracting pointers
468 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
470 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
472 const char *typediff
;
473 struct symbol
*ctype
;
474 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
477 * If it is an integer subtract: the ptr add case will do the
480 if (!is_ptr_type(rtype
))
481 return evaluate_ptr_add(expr
, l
, r
);
484 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
486 ctype
= common_ptr_type(l
, r
);
488 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
492 examine_symbol_type(ctype
);
494 /* Figure out the base type we point to */
495 if (ctype
->type
== SYM_NODE
)
496 ctype
= ctype
->ctype
.base_type
;
497 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
498 warn(expr
->pos
, "subtraction of functions? Share your drugs");
501 ctype
= ctype
->ctype
.base_type
;
503 expr
->ctype
= ssize_t_ctype
;
504 if (ctype
->bit_size
> BITS_IN_CHAR
) {
505 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
506 struct expression
*div
= expr
;
507 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
509 val
->ctype
= size_t_ctype
;
510 val
->value
= ctype
->bit_size
>> 3;
513 sub
->ctype
= ssize_t_ctype
;
522 return ssize_t_ctype
;
525 static struct symbol
*evaluate_sub(struct expression
*expr
)
527 struct expression
*left
= expr
->left
, *right
= expr
->right
;
528 struct symbol
*ltype
= left
->ctype
;
530 if (is_ptr_type(ltype
))
531 return evaluate_ptr_sub(expr
, left
, right
);
533 // FIXME! FP promotion
534 return evaluate_int_binop(expr
);
537 static struct symbol
*evaluate_logical(struct expression
*expr
)
539 struct expression
*left
= expr
->left
;
540 struct expression
*right
;
542 if (!evaluate_expression(left
))
545 /* Do immediate short-circuiting ... */
546 if (left
->type
== EXPR_VALUE
) {
547 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
549 expr
->type
= EXPR_VALUE
;
551 expr
->ctype
= &bool_ctype
;
556 expr
->type
= EXPR_VALUE
;
558 expr
->ctype
= &bool_ctype
;
565 if (!evaluate_expression(right
))
567 expr
->ctype
= &bool_ctype
;
568 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
570 * We know the left value doesn't matter, since
571 * otherwise we would have short-circuited it..
573 expr
->type
= EXPR_VALUE
;
574 expr
->value
= right
->value
;
580 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
582 // FIXME! Floating-point promotion!
583 return evaluate_int_binop(expr
);
586 static struct symbol
*evaluate_binop(struct expression
*expr
)
589 // addition can take ptr+int, fp and int
591 return evaluate_add(expr
);
593 // subtraction can take ptr-ptr, fp and int
595 return evaluate_sub(expr
);
597 // Arithmetic operations can take fp and int
598 case '*': case '/': case '%':
599 return evaluate_arithmetic(expr
);
601 // The rest are integer operations (bitops)
602 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
605 return evaluate_int_binop(expr
);
609 static struct symbol
*evaluate_comma(struct expression
*expr
)
611 expr
->ctype
= expr
->right
->ctype
;
615 static struct symbol
*evaluate_compare(struct expression
*expr
)
617 struct expression
*left
= expr
->left
, *right
= expr
->right
;
618 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
619 struct symbol
*ctype
;
622 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
623 expr
->ctype
= &bool_ctype
;
624 // FIXME! Check the types for compatibility
628 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
630 simplify_int_binop(expr
, ctype
);
631 expr
->ctype
= &bool_ctype
;
635 return bad_expr_type(expr
);
638 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
640 /* Integer promotion? */
641 if (ltype
->type
== SYM_NODE
)
642 ltype
= ltype
->ctype
.base_type
;
643 if (rtype
->type
== SYM_NODE
)
644 rtype
= rtype
->ctype
.base_type
;
645 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
647 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
649 return (is_int_type(ltype
) && is_int_type(rtype
));
652 static int is_void_ptr(struct expression
*expr
)
654 return (expr
->type
== EXPR_VALUE
&&
659 * FIXME!! This shoul ddo casts, array degeneration etc..
661 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
663 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
665 if (ltype
->type
== SYM_PTR
) {
666 if (is_void_ptr(right
))
670 if (rtype
->type
== SYM_PTR
) {
671 if (is_void_ptr(left
))
677 static struct symbol
*do_degenerate(struct expression
**ep
)
679 struct expression
*expr
= *ep
;
680 return degenerate(expr
, expr
->ctype
, ep
);
683 static struct symbol
* evaluate_conditional(struct expression
*expr
)
685 struct expression
*cond
, *true, *false;
686 struct symbol
*ctype
, *ltype
, *rtype
;
687 const char * typediff
;
689 ctype
= do_degenerate(&expr
->conditional
);
690 cond
= expr
->conditional
;
694 if (expr
->cond_true
) {
695 ltype
= do_degenerate(&expr
->cond_true
);
696 true = expr
->cond_true
;
699 rtype
= do_degenerate(&expr
->cond_false
);
700 false = expr
->cond_false
;
703 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
705 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
707 ctype
= compatible_ptr_type(true, expr
->cond_false
);
709 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
715 /* Simplify conditional expression.. */
716 if (cond
->type
== EXPR_VALUE
) {
725 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
726 struct expression
**rp
, struct symbol
*source
, const char *where
)
728 const char *typediff
;
732 /* It's ok if the target is more volatile or const than the source */
733 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
737 if (compatible_integer_types(target
, source
)) {
738 if (target
->bit_size
!= source
->bit_size
)
739 *rp
= cast_to(*rp
, target
);
743 /* Pointer destination? */
745 target_as
= t
->ctype
.as
;
746 if (t
->type
== SYM_NODE
) {
747 t
= t
->ctype
.base_type
;
748 target_as
|= t
->ctype
.as
;
750 if (t
->type
== SYM_PTR
) {
751 struct expression
*right
= *rp
;
752 struct symbol
*s
= source
;
755 // NULL pointer is always ok
756 if (right
->type
== EXPR_VALUE
&& !right
->value
)
759 /* "void *" matches anything as long as the address space is ok */
760 source_as
= s
->ctype
.as
;
761 if (s
->type
== SYM_NODE
) {
762 s
= s
->ctype
.base_type
;
763 source_as
|= s
->ctype
.as
;
765 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
766 s
= s
->ctype
.base_type
;
767 t
= t
->ctype
.base_type
;
768 if (s
== &void_ctype
|| t
== &void_ctype
)
772 if (s
->type
== SYM_FN
) {
773 typediff
= type_difference(t
->ctype
.base_type
, s
, 0, 0);
780 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
781 warn(expr
->pos
, " expected %s", show_typename(target
));
782 warn(expr
->pos
, " got %s", show_typename(source
));
787 * FIXME!! This is wrong from a double evaluation standpoint. We can't
788 * just expand the expression twice, that would make any side effects
791 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
794 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
795 static const int op_trans
[] = {
796 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
797 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
798 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
799 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
800 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
801 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
802 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
803 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
804 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '&',
805 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
808 subexpr
->left
= left
;
809 subexpr
->right
= right
;
810 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
812 expr
->right
= subexpr
;
813 return evaluate_binop(subexpr
);
816 static struct symbol
*evaluate_assignment(struct expression
*expr
)
818 struct expression
*left
= expr
->left
, *right
= expr
->right
;
819 struct symbol
*ltype
, *rtype
;
822 rtype
= right
->ctype
;
823 if (expr
->op
!= '=') {
824 rtype
= evaluate_binop_assignment(expr
, left
, right
);
830 if (!lvalue_expression(left
)) {
831 warn(expr
->pos
, "not an lvalue");
835 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
838 expr
->ctype
= expr
->left
->ctype
;
842 static struct symbol
*evaluate_addressof(struct expression
*expr
)
844 struct symbol
*ctype
, *symbol
;
845 struct expression
*op
= expr
->unop
;
847 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
848 warn(expr
->pos
, "not addressable");
852 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
853 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
854 symbol
->bit_size
= BITS_IN_POINTER
;
857 if (ctype
->type
== SYM_NODE
) {
858 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
859 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
860 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
861 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
863 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
864 symbol
->ctype
.as
= ctype
->ctype
.as
;
865 symbol
->ctype
.context
= ctype
->ctype
.context
;
866 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
867 ctype
= ctype
->ctype
.base_type
;
870 symbol
->ctype
.base_type
= ctype
;
872 expr
->ctype
= symbol
;
877 static struct symbol
*evaluate_dereference(struct expression
*expr
)
879 struct expression
*op
= expr
->unop
;
880 struct symbol
*ctype
= op
->ctype
, *sym
;
882 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
883 sym
->ctype
= ctype
->ctype
;
884 if (ctype
->type
== SYM_NODE
) {
885 ctype
= ctype
->ctype
.base_type
;
886 merge_type(sym
, ctype
);
888 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
889 warn(expr
->pos
, "cannot derefence this type");
893 ctype
= ctype
->ctype
.base_type
;
894 examine_symbol_type(ctype
);
896 warn(expr
->pos
, "undefined type");
900 sym
->bit_size
= ctype
->bit_size
;
901 sym
->array_size
= ctype
->array_size
;
903 /* Simplify: *&(expr) => (expr) */
904 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
912 static void simplify_preop(struct expression
*expr
)
914 struct expression
*op
= expr
->unop
;
915 unsigned long long v
, mask
;
917 if (op
->type
!= EXPR_VALUE
)
922 case '-': v
= -v
; break;
923 case '!': v
= !v
; break;
924 case '~': v
= ~v
; break;
927 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
928 mask
= mask
| (mask
-1);
929 expr
->value
= v
& mask
;
930 expr
->type
= EXPR_VALUE
;
934 * Unary post-ops: x++ and x--
936 static struct symbol
*evaluate_postop(struct expression
*expr
)
938 struct expression
*op
= expr
->unop
;
939 struct symbol
*ctype
= op
->ctype
;
941 if (!lvalue_expression(expr
->unop
)) {
942 warn(expr
->pos
, "need lvalue expression for ++/--");
949 static struct symbol
*evaluate_preop(struct expression
*expr
)
951 struct symbol
*ctype
= expr
->unop
->ctype
;
960 return evaluate_dereference(expr
);
963 return evaluate_addressof(expr
);
965 case SPECIAL_INCREMENT
:
966 case SPECIAL_DECREMENT
:
968 * From a type evaluation standpoint the pre-ops are
969 * the same as the postops
971 return evaluate_postop(expr
);
981 simplify_preop(expr
);
985 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
987 struct ptr_list
*head
= (struct ptr_list
*)_list
;
988 struct ptr_list
*list
= head
;
994 for (i
= 0; i
< list
->nr
; i
++) {
995 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
997 if (sym
->ident
!= ident
)
999 *offset
= sym
->offset
;
1002 struct symbol
*ctype
= sym
->ctype
.base_type
;
1006 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1008 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1011 *offset
+= sym
->offset
;
1015 } while ((list
= list
->next
) != head
);
1019 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1021 struct expression
*add
;
1026 /* Create a new add-expression */
1027 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1029 add
->ctype
= &ptr_ctype
;
1031 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1032 add
->right
->ctype
= &int_ctype
;
1033 add
->right
->value
= offset
;
1035 simplify_int_binop(add
, &ptr_ctype
);
1039 /* structure/union dereference */
1040 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1043 struct symbol
*ctype
, *member
, *sym
;
1044 struct expression
*deref
= expr
->deref
, *add
;
1045 struct ident
*ident
= expr
->member
;
1049 if (!evaluate_expression(deref
))
1052 warn(expr
->pos
, "bad member name");
1056 ctype
= deref
->ctype
;
1057 address_space
= ctype
->ctype
.as
;
1058 mod
= ctype
->ctype
.modifiers
;
1059 if (ctype
->type
== SYM_NODE
) {
1060 ctype
= ctype
->ctype
.base_type
;
1061 address_space
|= ctype
->ctype
.as
;
1062 mod
|= ctype
->ctype
.modifiers
;
1064 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1065 /* Arrays will degenerate into pointers for '->' */
1066 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1067 warn(expr
->pos
, "expected a pointer to a struct/union");
1070 mod
= ctype
->ctype
.modifiers
;
1071 address_space
= ctype
->ctype
.as
;
1072 ctype
= ctype
->ctype
.base_type
;
1073 if (ctype
->type
== SYM_NODE
) {
1074 mod
|= ctype
->ctype
.modifiers
;
1075 address_space
|= ctype
->ctype
.as
;
1076 ctype
= ctype
->ctype
.base_type
;
1079 if (!lvalue_expression(deref
)) {
1080 warn(deref
->pos
, "expected lvalue for member dereference");
1083 deref
= deref
->unop
;
1084 expr
->deref
= deref
;
1086 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1087 warn(expr
->pos
, "expected structure or union");
1091 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1093 warn(expr
->pos
, "no such struct/union member");
1097 add
= evaluate_offset(deref
, offset
);
1099 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1100 sym
->bit_size
= member
->bit_size
;
1101 sym
->array_size
= member
->array_size
;
1102 sym
->ctype
= member
->ctype
;
1103 sym
->ctype
.modifiers
= mod
;
1104 sym
->ctype
.as
= address_space
;
1105 ctype
= member
->ctype
.base_type
;
1106 if (ctype
->type
== SYM_BITFIELD
) {
1107 ctype
= ctype
->ctype
.base_type
;
1108 expr
->type
= EXPR_BITFIELD
;
1109 expr
->bitpos
= member
->bit_offset
;
1110 expr
->nrbits
= member
->fieldwidth
;
1111 expr
->address
= add
;
1113 expr
->type
= EXPR_PREOP
;
1122 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1126 if (expr
->cast_type
) {
1127 examine_symbol_type(expr
->cast_type
);
1128 size
= expr
->cast_type
->bit_size
;
1130 if (!evaluate_expression(expr
->cast_expression
))
1132 size
= expr
->cast_expression
->ctype
->bit_size
;
1135 warn(expr
->pos
, "cannot size expression");
1138 expr
->type
= EXPR_VALUE
;
1139 expr
->value
= size
>> 3;
1140 expr
->ctype
= size_t_ctype
;
1141 return size_t_ctype
;
1144 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
1146 struct expression
*expr
;
1147 struct symbol_list
*argument_types
= fn
->arguments
;
1148 struct symbol
*argtype
;
1151 PREPARE_PTR_LIST(argument_types
, argtype
);
1152 FOR_EACH_PTR (head
, expr
) {
1153 struct expression
**p
= THIS_ADDRESS(expr
);
1154 struct symbol
*ctype
, *target
;
1155 ctype
= evaluate_expression(expr
);
1160 ctype
= degenerate(expr
, ctype
, p
);
1163 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1164 target
= &int_ctype
;
1166 static char where
[30];
1167 examine_symbol_type(target
);
1168 sprintf(where
, "argument %d", i
);
1169 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1173 NEXT_PTR_LIST(argument_types
, argtype
);
1180 * FIXME! This is bogus: we need to take array index
1181 * entries into account when calculating the size of
1184 static int count_array_initializer(struct expression
*expr
)
1186 struct expression_list
*list
;
1188 if (expr
->type
!= EXPR_INITIALIZER
)
1190 list
= expr
->expr_list
;
1191 return expression_list_size(list
);
1194 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1196 return count_array_initializer(expr
);
1199 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1201 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1206 * Initializers are kind of like assignments. Except
1207 * they can be a hell of a lot more complex.
1209 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1211 struct expression
*expr
= *ep
;
1214 * Simple non-structure/array initializers are the simple
1215 * case, and look (and parse) largely like assignments.
1217 if (expr
->type
!= EXPR_INITIALIZER
) {
1219 struct symbol
*rtype
= evaluate_expression(expr
);
1221 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1222 /* strings are special: char arrays */
1223 if (rtype
->type
== SYM_ARRAY
)
1224 size
= rtype
->array_size
;
1229 expr
->ctype
= ctype
;
1230 switch (ctype
->type
) {
1233 return evaluate_array_initializer(ctype
, expr
);
1235 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1237 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1241 warn(expr
->pos
, "unexpected compound initializer");
1245 static struct symbol
*evaluate_cast(struct expression
*expr
)
1247 struct expression
*target
= expr
->cast_expression
;
1248 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1250 expr
->ctype
= ctype
;
1251 expr
->cast_type
= ctype
;
1254 * Special case: a cast can be followed by an
1255 * initializer, in which case we need to pass
1256 * the type value down to that initializer rather
1257 * than trying to evaluate it as an expression
1259 if (target
->type
== EXPR_INITIALIZER
) {
1260 evaluate_initializer(ctype
, &expr
->cast_expression
);
1264 evaluate_expression(target
);
1266 /* Simplify normal integer casts.. */
1267 if (target
->type
== EXPR_VALUE
)
1268 cast_value(expr
, ctype
, target
, target
->ctype
);
1272 static struct symbol
*evaluate_call(struct expression
*expr
)
1275 struct symbol
*ctype
;
1276 struct expression
*fn
= expr
->fn
;
1277 struct expression_list
*arglist
= expr
->args
;
1279 if (!evaluate_expression(fn
))
1282 if (ctype
->type
== SYM_NODE
) {
1284 * FIXME!! We should really expand the inline functions.
1285 * For now we just mark them accessed so that they show
1286 * up on the list of used symbols.
1288 if (ctype
->ctype
.modifiers
& MOD_INLINE
)
1289 access_symbol(ctype
);
1290 ctype
= ctype
->ctype
.base_type
;
1292 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1293 ctype
= ctype
->ctype
.base_type
;
1294 if (ctype
->type
!= SYM_FN
) {
1295 warn(expr
->pos
, "not a function");
1298 if (!evaluate_arguments(ctype
, arglist
))
1300 args
= expression_list_size(expr
->args
);
1301 fnargs
= symbol_list_size(ctype
->arguments
);
1303 warn(expr
->pos
, "not enough arguments for function");
1304 if (args
> fnargs
&& !ctype
->variadic
)
1305 warn(expr
->pos
, "too many arguments for function");
1306 expr
->ctype
= ctype
->ctype
.base_type
;
1310 struct symbol
*evaluate_expression(struct expression
*expr
)
1317 switch (expr
->type
) {
1319 warn(expr
->pos
, "value expression without a type");
1322 return evaluate_string(expr
);
1324 return evaluate_symbol_expression(expr
);
1326 if (!evaluate_expression(expr
->left
))
1328 if (!evaluate_expression(expr
->right
))
1330 return evaluate_binop(expr
);
1332 return evaluate_logical(expr
);
1334 if (!evaluate_expression(expr
->left
))
1336 if (!evaluate_expression(expr
->right
))
1338 return evaluate_comma(expr
);
1340 if (!evaluate_expression(expr
->left
))
1342 if (!evaluate_expression(expr
->right
))
1344 return evaluate_compare(expr
);
1345 case EXPR_ASSIGNMENT
:
1346 if (!evaluate_expression(expr
->left
))
1348 if (!evaluate_expression(expr
->right
))
1350 return evaluate_assignment(expr
);
1352 if (!evaluate_expression(expr
->unop
))
1354 return evaluate_preop(expr
);
1356 if (!evaluate_expression(expr
->unop
))
1358 return evaluate_postop(expr
);
1360 return evaluate_cast(expr
);
1362 return evaluate_sizeof(expr
);
1364 return evaluate_member_dereference(expr
);
1366 return evaluate_call(expr
);
1368 warn(expr
->pos
, "bitfield generated by parser");
1370 case EXPR_CONDITIONAL
:
1371 if (!evaluate_expression(expr
->conditional
))
1373 if (!evaluate_expression(expr
->cond_false
))
1375 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1377 return evaluate_conditional(expr
);
1378 case EXPR_STATEMENT
:
1379 expr
->ctype
= evaluate_statement(expr
->statement
);
1382 /* These can not exist as stand-alone expressions */
1383 case EXPR_INITIALIZER
:
1384 case EXPR_IDENTIFIER
:
1386 warn(expr
->pos
, "internal front-end error: initializer in expression");
1392 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1394 struct symbol
**last
= _last
;
1395 struct symbol
*type
= evaluate_statement(stmt
);
1397 if (flags
& ITERATE_LAST
)
1401 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1403 evaluate_symbol(sym
);
1406 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1408 struct symbol
*base_type
;
1410 sym
= examine_symbol_type(sym
);
1411 base_type
= sym
->ctype
.base_type
;
1415 /* Evaluate the initializers */
1416 if (sym
->initializer
) {
1417 int count
= evaluate_initializer(base_type
, &sym
->initializer
);
1418 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1419 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1420 base_type
->array_size
= count
;
1421 base_type
->bit_size
= bit_size
;
1422 sym
->array_size
= count
;
1423 sym
->bit_size
= bit_size
;
1427 /* And finally, evaluate the body of the symbol too */
1428 if (base_type
->type
== SYM_FN
) {
1429 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1430 if (base_type
->stmt
) {
1431 current_fn
= base_type
;
1432 current_contextmask
= sym
->ctype
.contextmask
;
1433 current_context
= sym
->ctype
.context
;
1434 evaluate_statement(base_type
->stmt
);
1441 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1443 struct expression
*expr
= stmt
->expression
;
1444 struct symbol
*ctype
, *fntype
;
1446 fntype
= current_fn
->ctype
.base_type
;
1447 if (fntype
== &void_ctype
) {
1449 warn(expr
->pos
, "return expression in void function");
1454 warn(stmt
->pos
, "return with no return value");
1457 ctype
= evaluate_expression(expr
);
1460 ctype
= degenerate(expr
, ctype
, &expr
);
1461 expr
->ctype
= ctype
;
1462 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1463 stmt
->expression
= expr
;
1467 struct symbol
*evaluate_statement(struct statement
*stmt
)
1472 switch (stmt
->type
) {
1474 return evaluate_return_expression(stmt
);
1476 case STMT_EXPRESSION
:
1477 return evaluate_expression(stmt
->expression
);
1479 case STMT_COMPOUND
: {
1480 struct symbol
*type
= NULL
;
1481 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1482 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1486 evaluate_expression(stmt
->if_conditional
);
1487 evaluate_statement(stmt
->if_true
);
1488 evaluate_statement(stmt
->if_false
);
1491 evaluate_expression(stmt
->iterator_pre_condition
);
1492 evaluate_expression(stmt
->iterator_post_condition
);
1493 evaluate_statement(stmt
->iterator_pre_statement
);
1494 evaluate_statement(stmt
->iterator_statement
);
1495 evaluate_statement(stmt
->iterator_post_statement
);
1498 evaluate_expression(stmt
->switch_expression
);
1499 evaluate_statement(stmt
->switch_statement
);
1502 evaluate_expression(stmt
->case_expression
);
1503 evaluate_expression(stmt
->case_to
);
1504 evaluate_statement(stmt
->case_statement
);
1507 evaluate_statement(stmt
->label_statement
);
1510 evaluate_expression(stmt
->goto_expression
);
1515 /* FIXME! Do the asm parameter evaluation! */
1521 long long get_expression_value(struct expression
*expr
)
1523 long long value
, mask
;
1524 struct symbol
*ctype
;
1526 ctype
= evaluate_expression(expr
);
1527 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1528 warn(expr
->pos
, "bad constant expression");
1532 value
= expr
->value
;
1533 mask
= 1ULL << (ctype
->bit_size
-1);
1536 while (ctype
->type
!= SYM_BASETYPE
)
1537 ctype
= ctype
->ctype
.base_type
;
1538 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1539 value
= value
| mask
| ~(mask
-1);