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(sym
->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_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
= 0;
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
)
210 if (rtype
->type
== SYM_ENUM
)
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 if (ctype
->type
== SYM_ARRAY
) {
302 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
303 struct expression
*ptr
;
305 sym
->ctype
= ctype
->ctype
;
306 sym
->bit_size
= BITS_IN_POINTER
;
307 sym
->ctype
.alignment
= POINTER_ALIGNMENT
;
310 if (expr
->type
!= EXPR_STRING
) {
315 * This all really assumes that we got the degenerate
316 * array as an lvalue (ie a dereference). If that
317 * is not the case, then holler - because we've screwed
320 if (!lvalue_expression(ptr
))
321 warn(ptr
->pos
, "internal error: strange degenerate array case");
327 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
329 struct symbol
*ctype
;
330 struct symbol
*ptr_type
= ptr
->ctype
;
331 struct symbol
*i_type
= i
->ctype
;
334 if (i_type
->type
== SYM_NODE
)
335 i_type
= i_type
->ctype
.base_type
;
336 if (ptr_type
->type
== SYM_NODE
)
337 ptr_type
= ptr_type
->ctype
.base_type
;
339 if (i_type
->type
== SYM_ENUM
)
341 if (!is_int_type(i_type
))
342 return bad_expr_type(expr
);
345 examine_symbol_type(ctype
);
347 ctype
= degenerate(expr
, ctype
, &ptr
);
348 bit_size
= ctype
->bit_size
;
350 /* Special case: adding zero commonly happens as a result of 'array[0]' */
351 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
353 } else if (bit_size
> BITS_IN_CHAR
) {
354 struct expression
*add
= expr
;
355 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
356 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
358 val
->ctype
= size_t_ctype
;
359 val
->value
= bit_size
>> 3;
362 mul
->ctype
= size_t_ctype
;
365 simplify_int_binop(mul
, size_t_ctype
);
367 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
370 simplify_int_binop(add
, ctype
);
377 static struct symbol
*evaluate_add(struct expression
*expr
)
379 struct expression
*left
= expr
->left
, *right
= expr
->right
;
380 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
382 if (is_ptr_type(ltype
))
383 return evaluate_ptr_add(expr
, left
, right
);
385 if (is_ptr_type(rtype
))
386 return evaluate_ptr_add(expr
, right
, left
);
388 // FIXME! FP promotion
389 return evaluate_int_binop(expr
);
392 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
394 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
395 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
398 unsigned long mod1
, mod2
, diff
;
399 unsigned long as1
, as2
;
401 if (target
== source
)
403 if (!target
|| !source
)
404 return "different types";
406 * Peel of per-node information.
407 * FIXME! Check alignment, address space, and context too here!
409 if (target
->type
== SYM_NODE
)
410 target
= target
->ctype
.base_type
;
411 if (source
->type
== SYM_NODE
)
412 source
= source
->ctype
.base_type
;
413 mod1
= target
->ctype
.modifiers
;
414 as1
= target
->ctype
.as
;
415 mod2
= source
->ctype
.modifiers
;
416 as2
= source
->ctype
.as
;
418 if (target
->type
!= source
->type
) {
419 int type1
= target
->type
;
420 int type2
= source
->type
;
422 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
423 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
424 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
426 return "different base types";
429 /* Must be same address space to be comparable */
431 return "different address spaces";
433 /* Ignore differences in storage types, sign, or addressability */
434 diff
= (mod1
^ mod2
) & ~(MOD_STORAGE
| MOD_ADDRESSABLE
| MOD_SIGNED
| MOD_UNSIGNED
);
436 mod1
&= diff
& ~target_mod_ignore
;
437 mod2
&= diff
& ~source_mod_ignore
;
439 if ((mod1
| mod2
) & MOD_SIZE
)
440 return "different type sizes";
441 return "different modifiers";
445 target
= target
->ctype
.base_type
;
446 source
= source
->ctype
.base_type
;
451 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
453 /* NULL expression? Just return the type of the "other side" */
454 if (r
->type
== EXPR_VALUE
&& !r
->value
)
456 if (l
->type
== EXPR_VALUE
&& !l
->value
)
462 * Ignore differences in "volatile" and "const"ness when
463 * subtracting pointers
465 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
467 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
469 const char *typediff
;
470 struct symbol
*ctype
;
471 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
474 * If it is an integer subtract: the ptr add case will do the
477 if (!is_ptr_type(rtype
))
478 return evaluate_ptr_add(expr
, l
, r
);
481 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
483 ctype
= common_ptr_type(l
, r
);
485 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
489 examine_symbol_type(ctype
);
491 /* Figure out the base type we point to */
492 if (ctype
->type
== SYM_NODE
)
493 ctype
= ctype
->ctype
.base_type
;
494 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
495 warn(expr
->pos
, "subtraction of functions? Share your drugs");
498 ctype
= ctype
->ctype
.base_type
;
500 expr
->ctype
= ssize_t_ctype
;
501 if (ctype
->bit_size
> BITS_IN_CHAR
) {
502 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
503 struct expression
*div
= expr
;
504 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
506 val
->ctype
= size_t_ctype
;
507 val
->value
= ctype
->bit_size
>> 3;
510 sub
->ctype
= ssize_t_ctype
;
519 return ssize_t_ctype
;
522 static struct symbol
*evaluate_sub(struct expression
*expr
)
524 struct expression
*left
= expr
->left
, *right
= expr
->right
;
525 struct symbol
*ltype
= left
->ctype
;
527 if (is_ptr_type(ltype
))
528 return evaluate_ptr_sub(expr
, left
, right
);
530 // FIXME! FP promotion
531 return evaluate_int_binop(expr
);
534 static struct symbol
*evaluate_logical(struct expression
*expr
)
536 struct expression
*left
= expr
->left
;
537 struct expression
*right
;
539 if (!evaluate_expression(left
))
542 /* Do immediate short-circuiting ... */
543 if (left
->type
== EXPR_VALUE
) {
544 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
546 expr
->type
= EXPR_VALUE
;
548 expr
->ctype
= &bool_ctype
;
553 expr
->type
= EXPR_VALUE
;
555 expr
->ctype
= &bool_ctype
;
562 if (!evaluate_expression(right
))
564 expr
->ctype
= &bool_ctype
;
565 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
567 * We know the left value doesn't matter, since
568 * otherwise we would have short-circuited it..
570 expr
->type
= EXPR_VALUE
;
571 expr
->value
= right
->value
;
577 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
579 // FIXME! Floating-point promotion!
580 return evaluate_int_binop(expr
);
583 static struct symbol
*evaluate_binop(struct expression
*expr
)
586 // addition can take ptr+int, fp and int
588 return evaluate_add(expr
);
590 // subtraction can take ptr-ptr, fp and int
592 return evaluate_sub(expr
);
594 // Arithmetic operations can take fp and int
595 case '*': case '/': case '%':
596 return evaluate_arithmetic(expr
);
598 // The rest are integer operations (bitops)
599 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
602 return evaluate_int_binop(expr
);
606 static struct symbol
*evaluate_comma(struct expression
*expr
)
608 expr
->ctype
= expr
->right
->ctype
;
612 static struct symbol
*evaluate_compare(struct expression
*expr
)
614 struct expression
*left
= expr
->left
, *right
= expr
->right
;
615 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
616 struct symbol
*ctype
;
619 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
620 expr
->ctype
= &bool_ctype
;
621 // FIXME! Check the types for compatibility
625 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
627 simplify_int_binop(expr
, ctype
);
628 expr
->ctype
= &bool_ctype
;
632 return bad_expr_type(expr
);
635 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
637 /* Integer promotion? */
638 if (ltype
->type
== SYM_NODE
)
639 ltype
= ltype
->ctype
.base_type
;
640 if (rtype
->type
== SYM_NODE
)
641 rtype
= rtype
->ctype
.base_type
;
642 if (ltype
->type
== SYM_ENUM
)
644 if (rtype
->type
== SYM_ENUM
)
646 return (is_int_type(ltype
) && is_int_type(rtype
));
649 static int is_void_ptr(struct expression
*expr
)
651 return (expr
->type
== EXPR_VALUE
&&
656 * FIXME!! This shoul ddo casts, array degeneration etc..
658 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
660 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
662 if (ltype
->type
== SYM_PTR
) {
663 if (is_void_ptr(right
))
667 if (rtype
->type
== SYM_PTR
) {
668 if (is_void_ptr(left
))
674 static struct symbol
* evaluate_conditional(struct expression
*expr
)
676 struct expression
*cond
, *true, *false;
677 struct symbol
*ctype
, *ltype
, *rtype
;
678 const char * typediff
;
680 cond
= expr
->conditional
;
681 true = expr
->cond_true
? : cond
;
682 false = expr
->cond_false
;
685 rtype
= false->ctype
;
688 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
690 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
692 ctype
= compatible_ptr_type(true, expr
->cond_false
);
694 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
700 /* Simplify conditional expression.. */
701 if (cond
->type
== EXPR_VALUE
) {
710 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
711 struct expression
**rp
, struct symbol
*source
, const char *where
)
713 const char *typediff
;
717 /* It's ok if the target is more volatile or const than the source */
718 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
722 if (compatible_integer_types(target
, source
)) {
723 if (target
->bit_size
!= source
->bit_size
)
724 *rp
= cast_to(*rp
, target
);
728 /* Pointer destination? */
730 target_as
= t
->ctype
.as
;
731 if (t
->type
== SYM_NODE
) {
732 t
= t
->ctype
.base_type
;
733 target_as
|= t
->ctype
.as
;
735 if (t
->type
== SYM_PTR
) {
736 struct expression
*right
= *rp
;
737 struct symbol
*s
= source
;
740 // NULL pointer is always ok
741 if (right
->type
== EXPR_VALUE
&& !right
->value
)
744 /* "void *" matches anything as long as the address space is ok */
745 source_as
= s
->ctype
.as
;
746 if (s
->type
== SYM_NODE
) {
747 s
= s
->ctype
.base_type
;
748 source_as
|= s
->ctype
.as
;
750 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
751 s
= s
->ctype
.base_type
;
752 t
= t
->ctype
.base_type
;
753 if (s
== &void_ctype
|| t
== &void_ctype
)
757 if (s
->type
== SYM_FN
) {
758 typediff
= type_difference(t
->ctype
.base_type
, s
, 0, 0);
764 warn(expr
->pos
, "different types in %s (%s)", where
, typediff
);
769 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
770 warn(expr
->pos
, " expected %s", show_typename(target
));
771 warn(expr
->pos
, " got %s", show_typename(source
));
776 * FIXME!! This is wrong from a double evaluation standpoint. We can't
777 * just expand the expression twice, that would make any side effects
780 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
783 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
784 static const int op_trans
[] = {
785 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
786 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
787 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
788 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
789 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
790 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
791 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
792 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
793 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '&',
794 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
797 subexpr
->left
= left
;
798 subexpr
->right
= right
;
799 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
801 expr
->right
= subexpr
;
802 return evaluate_binop(subexpr
);
805 static struct symbol
*evaluate_assignment(struct expression
*expr
)
807 struct expression
*left
= expr
->left
, *right
= expr
->right
;
808 struct symbol
*ltype
, *rtype
;
811 rtype
= right
->ctype
;
812 if (expr
->op
!= '=') {
813 rtype
= evaluate_binop_assignment(expr
, left
, right
);
819 if (!lvalue_expression(left
)) {
820 warn(expr
->pos
, "not an lvalue");
824 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
827 expr
->ctype
= expr
->left
->ctype
;
831 static struct symbol
*evaluate_addressof(struct expression
*expr
)
833 struct symbol
*ctype
, *symbol
;
834 struct expression
*op
= expr
->unop
;
836 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
837 warn(expr
->pos
, "not addressable");
841 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
842 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
843 symbol
->bit_size
= BITS_IN_POINTER
;
846 if (ctype
->type
== SYM_NODE
) {
847 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
848 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
849 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
850 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
852 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
853 symbol
->ctype
.as
= ctype
->ctype
.as
;
854 symbol
->ctype
.context
= ctype
->ctype
.context
;
855 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
856 ctype
= ctype
->ctype
.base_type
;
859 symbol
->ctype
.base_type
= ctype
;
861 expr
->ctype
= symbol
;
866 static struct symbol
*evaluate_dereference(struct expression
*expr
)
868 struct expression
*op
= expr
->unop
;
869 struct symbol
*ctype
= op
->ctype
, *sym
;
871 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
872 sym
->ctype
= ctype
->ctype
;
873 if (ctype
->type
== SYM_NODE
) {
874 ctype
= ctype
->ctype
.base_type
;
875 sym
->ctype
.modifiers
|= ctype
->ctype
.modifiers
;
876 sym
->ctype
.as
|= ctype
->ctype
.as
;
878 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
879 warn(expr
->pos
, "cannot derefence this type");
883 ctype
= ctype
->ctype
.base_type
;
885 warn(expr
->pos
, "undefined type");
888 examine_symbol_type(ctype
);
889 sym
->ctype
.base_type
= ctype
;
891 sym
->bit_size
= ctype
->bit_size
;
892 sym
->array_size
= ctype
->array_size
;
894 /* Simplify: *&(expr) => (expr) */
895 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
903 static void simplify_preop(struct expression
*expr
)
905 struct expression
*op
= expr
->unop
;
906 unsigned long long v
, mask
;
908 if (op
->type
!= EXPR_VALUE
)
913 case '-': v
= -v
; break;
914 case '!': v
= !v
; break;
915 case '~': v
= ~v
; break;
918 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
919 mask
= mask
| (mask
-1);
920 expr
->value
= v
& mask
;
921 expr
->type
= EXPR_VALUE
;
925 * Unary post-ops: x++ and x--
927 static struct symbol
*evaluate_postop(struct expression
*expr
)
929 struct expression
*op
= expr
->unop
;
930 struct symbol
*ctype
= op
->ctype
;
932 if (!lvalue_expression(expr
->unop
)) {
933 warn(expr
->pos
, "need lvalue expression for ++/--");
940 static struct symbol
*evaluate_preop(struct expression
*expr
)
942 struct symbol
*ctype
= expr
->unop
->ctype
;
951 return evaluate_dereference(expr
);
954 return evaluate_addressof(expr
);
957 expr
->ctype
= &bool_ctype
;
958 simplify_preop(expr
);
961 case SPECIAL_INCREMENT
:
962 case SPECIAL_DECREMENT
:
964 * From a type evaluation standpoint the pre-ops are
965 * the same as the postops
967 return evaluate_postop(expr
);
971 simplify_preop(expr
);
976 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
978 struct ptr_list
*head
= (struct ptr_list
*)_list
;
979 struct ptr_list
*list
= head
;
985 for (i
= 0; i
< list
->nr
; i
++) {
986 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
988 if (sym
->ident
!= ident
)
990 *offset
= sym
->offset
;
993 struct symbol
*ctype
= sym
->ctype
.base_type
;
997 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
999 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1002 *offset
+= sym
->offset
;
1006 } while ((list
= list
->next
) != head
);
1010 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1012 struct expression
*add
;
1017 /* Create a new add-expression */
1018 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1020 add
->ctype
= &ptr_ctype
;
1022 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1023 add
->right
->ctype
= &int_ctype
;
1024 add
->right
->value
= offset
;
1026 simplify_int_binop(add
, &ptr_ctype
);
1030 /* structure/union dereference */
1031 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1034 struct symbol
*ctype
, *member
, *sym
;
1035 struct expression
*deref
= expr
->deref
, *add
;
1036 struct ident
*ident
= expr
->member
;
1040 if (!evaluate_expression(deref
))
1043 warn(expr
->pos
, "bad member name");
1047 ctype
= deref
->ctype
;
1048 if (ctype
->type
== SYM_NODE
)
1049 ctype
= ctype
->ctype
.base_type
;
1050 mod
= ctype
->ctype
.modifiers
;
1051 address_space
= ctype
->ctype
.as
;
1052 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1053 /* Arrays will degenerate into pointers for '->' */
1054 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1055 warn(expr
->pos
, "expected a pointer to a struct/union");
1058 address_space
= ctype
->ctype
.as
;
1059 ctype
= ctype
->ctype
.base_type
;
1060 mod
|= ctype
->ctype
.modifiers
;
1061 if (ctype
->type
== SYM_NODE
) {
1062 ctype
= ctype
->ctype
.base_type
;
1063 mod
|= ctype
->ctype
.modifiers
;
1066 if (!lvalue_expression(deref
)) {
1067 warn(deref
->pos
, "expected lvalue for member dereference");
1070 deref
= deref
->unop
;
1071 expr
->deref
= deref
;
1073 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1074 warn(expr
->pos
, "expected structure or union");
1078 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1080 warn(expr
->pos
, "no such struct/union member");
1084 add
= evaluate_offset(deref
, offset
);
1086 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1087 sym
->bit_size
= member
->bit_size
;
1088 sym
->array_size
= member
->array_size
;
1089 sym
->ctype
= member
->ctype
;
1090 sym
->ctype
.modifiers
= mod
;
1091 sym
->ctype
.as
= address_space
;
1092 ctype
= member
->ctype
.base_type
;
1093 if (ctype
->type
== SYM_BITFIELD
) {
1094 ctype
= ctype
->ctype
.base_type
;
1095 expr
->type
= EXPR_BITFIELD
;
1096 expr
->bitpos
= member
->bit_offset
;
1097 expr
->nrbits
= member
->fieldwidth
;
1098 expr
->address
= add
;
1100 expr
->type
= EXPR_PREOP
;
1109 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1113 if (expr
->cast_type
) {
1114 examine_symbol_type(expr
->cast_type
);
1115 size
= expr
->cast_type
->bit_size
;
1117 if (!evaluate_expression(expr
->cast_expression
))
1119 size
= expr
->cast_expression
->ctype
->bit_size
;
1122 warn(expr
->pos
, "cannot size expression");
1125 expr
->type
= EXPR_VALUE
;
1126 expr
->value
= size
>> 3;
1127 expr
->ctype
= size_t_ctype
;
1128 return size_t_ctype
;
1131 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
1133 struct expression
*expr
;
1134 struct symbol_list
*argument_types
= fn
->arguments
;
1135 struct symbol
*argtype
;
1138 PREPARE_PTR_LIST(argument_types
, argtype
);
1139 FOR_EACH_PTR (head
, expr
) {
1140 struct expression
**p
= THIS_ADDRESS(expr
);
1141 struct symbol
*ctype
, *target
;
1142 ctype
= evaluate_expression(expr
);
1147 if (ctype
->type
== SYM_ARRAY
) {
1148 ctype
= degenerate(expr
, ctype
, p
);
1149 expr
->ctype
= ctype
;
1153 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1154 target
= &int_ctype
;
1156 static char where
[30];
1157 examine_symbol_type(target
);
1158 sprintf(where
, "argument %d", i
);
1159 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1163 NEXT_PTR_LIST(argument_types
, argtype
);
1170 * FIXME! This is bogus: we need to take array index
1171 * entries into account when calculating the size of
1174 static int count_array_initializer(struct expression
*expr
)
1176 struct expression_list
*list
;
1178 if (expr
->type
!= EXPR_INITIALIZER
)
1180 list
= expr
->expr_list
;
1181 return expression_list_size(list
);
1184 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1186 return count_array_initializer(expr
);
1189 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1191 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1196 * Initializers are kind of like assignments. Except
1197 * they can be a hell of a lot more complex.
1199 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1201 struct expression
*expr
= *ep
;
1204 * Simple non-structure/array initializers are the simple
1205 * case, and look (and parse) largely like assignments.
1207 if (expr
->type
!= EXPR_INITIALIZER
) {
1209 struct symbol
*rtype
= evaluate_expression(expr
);
1211 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1212 /* strings are special: char arrays */
1213 if (rtype
->type
== SYM_ARRAY
)
1214 size
= rtype
->array_size
;
1219 expr
->ctype
= ctype
;
1220 switch (ctype
->type
) {
1223 return evaluate_array_initializer(ctype
, expr
);
1225 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1227 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1231 warn(expr
->pos
, "unexpected compound initializer");
1235 static struct symbol
*evaluate_cast(struct expression
*expr
)
1237 struct expression
*target
= expr
->cast_expression
;
1238 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1240 expr
->ctype
= ctype
;
1241 expr
->cast_type
= ctype
;
1244 * Special case: a cast can be followed by an
1245 * initializer, in which case we need to pass
1246 * the type value down to that initializer rather
1247 * than trying to evaluate it as an expression
1249 if (target
->type
== EXPR_INITIALIZER
) {
1250 evaluate_initializer(ctype
, &expr
->cast_expression
);
1254 evaluate_expression(target
);
1256 /* Simplify normal integer casts.. */
1257 if (target
->type
== EXPR_VALUE
)
1258 cast_value(expr
, ctype
, target
, target
->ctype
);
1262 static struct symbol
*evaluate_call(struct expression
*expr
)
1265 struct symbol
*ctype
;
1266 struct expression
*fn
= expr
->fn
;
1267 struct expression_list
*arglist
= expr
->args
;
1269 if (!evaluate_expression(fn
))
1272 if (ctype
->type
== SYM_NODE
) {
1274 * FIXME!! We should really expand the inline functions.
1275 * For now we just mark them accessed so that they show
1276 * up on the list of used symbols.
1278 if (ctype
->ctype
.modifiers
& MOD_INLINE
)
1279 access_symbol(ctype
);
1280 ctype
= ctype
->ctype
.base_type
;
1282 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1283 ctype
= ctype
->ctype
.base_type
;
1284 if (ctype
->type
!= SYM_FN
) {
1285 warn(expr
->pos
, "not a function");
1288 if (!evaluate_arguments(ctype
, arglist
))
1290 args
= expression_list_size(expr
->args
);
1291 fnargs
= symbol_list_size(ctype
->arguments
);
1293 warn(expr
->pos
, "not enough arguments for function");
1294 if (args
> fnargs
&& !ctype
->variadic
)
1295 warn(expr
->pos
, "too many arguments for function");
1296 expr
->ctype
= ctype
->ctype
.base_type
;
1300 struct symbol
*evaluate_expression(struct expression
*expr
)
1307 switch (expr
->type
) {
1309 warn(expr
->pos
, "value expression without a type");
1312 return evaluate_string(expr
);
1314 return evaluate_symbol_expression(expr
);
1316 if (!evaluate_expression(expr
->left
))
1318 if (!evaluate_expression(expr
->right
))
1320 return evaluate_binop(expr
);
1322 return evaluate_logical(expr
);
1324 if (!evaluate_expression(expr
->left
))
1326 if (!evaluate_expression(expr
->right
))
1328 return evaluate_comma(expr
);
1330 if (!evaluate_expression(expr
->left
))
1332 if (!evaluate_expression(expr
->right
))
1334 return evaluate_compare(expr
);
1335 case EXPR_ASSIGNMENT
:
1336 if (!evaluate_expression(expr
->left
))
1338 if (!evaluate_expression(expr
->right
))
1340 return evaluate_assignment(expr
);
1342 if (!evaluate_expression(expr
->unop
))
1344 return evaluate_preop(expr
);
1346 if (!evaluate_expression(expr
->unop
))
1348 return evaluate_postop(expr
);
1350 return evaluate_cast(expr
);
1352 return evaluate_sizeof(expr
);
1354 return evaluate_member_dereference(expr
);
1356 return evaluate_call(expr
);
1358 warn(expr
->pos
, "bitfield generated by parser");
1360 case EXPR_CONDITIONAL
:
1361 if (!evaluate_expression(expr
->conditional
))
1363 if (!evaluate_expression(expr
->cond_false
))
1365 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1367 return evaluate_conditional(expr
);
1368 case EXPR_STATEMENT
:
1369 expr
->ctype
= evaluate_statement(expr
->statement
);
1372 /* These can not exist as stand-alone expressions */
1373 case EXPR_INITIALIZER
:
1374 case EXPR_IDENTIFIER
:
1376 warn(expr
->pos
, "internal front-end error: initializer in expression");
1382 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1384 struct symbol
**last
= _last
;
1385 struct symbol
*type
= evaluate_statement(stmt
);
1387 if (flags
& ITERATE_LAST
)
1391 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1393 evaluate_symbol(sym
);
1396 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1398 struct symbol
*base_type
;
1400 examine_symbol_type(sym
);
1401 base_type
= sym
->ctype
.base_type
;
1404 sym
->ctype
.base_type
= base_type
;
1406 /* Evaluate the initializers */
1407 if (sym
->initializer
) {
1408 int count
= evaluate_initializer(base_type
, &sym
->initializer
);
1409 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1410 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1411 base_type
->array_size
= count
;
1412 base_type
->bit_size
= bit_size
;
1413 sym
->array_size
= count
;
1414 sym
->bit_size
= bit_size
;
1418 /* And finally, evaluate the body of the symbol too */
1419 if (base_type
->type
== SYM_FN
) {
1420 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1421 if (base_type
->stmt
) {
1422 current_fn
= base_type
;
1423 current_contextmask
= sym
->ctype
.contextmask
;
1424 current_context
= sym
->ctype
.context
;
1425 evaluate_statement(base_type
->stmt
);
1432 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1434 struct expression
*expr
= stmt
->expression
;
1435 struct symbol
*ctype
, *fntype
;
1437 fntype
= current_fn
->ctype
.base_type
;
1438 if (fntype
== &void_ctype
) {
1440 warn(expr
->pos
, "return expression in void function");
1445 warn(stmt
->pos
, "return with no return value");
1448 ctype
= evaluate_expression(expr
);
1451 ctype
= degenerate(expr
, ctype
, &expr
);
1452 expr
->ctype
= ctype
;
1453 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1454 stmt
->expression
= expr
;
1458 struct symbol
*evaluate_statement(struct statement
*stmt
)
1463 switch (stmt
->type
) {
1465 return evaluate_return_expression(stmt
);
1467 case STMT_EXPRESSION
:
1468 return evaluate_expression(stmt
->expression
);
1470 case STMT_COMPOUND
: {
1471 struct symbol
*type
= NULL
;
1472 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1473 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1477 evaluate_expression(stmt
->if_conditional
);
1478 evaluate_statement(stmt
->if_true
);
1479 evaluate_statement(stmt
->if_false
);
1482 evaluate_expression(stmt
->iterator_pre_condition
);
1483 evaluate_expression(stmt
->iterator_post_condition
);
1484 evaluate_statement(stmt
->iterator_pre_statement
);
1485 evaluate_statement(stmt
->iterator_statement
);
1486 evaluate_statement(stmt
->iterator_post_statement
);
1489 evaluate_expression(stmt
->switch_expression
);
1490 evaluate_statement(stmt
->switch_statement
);
1493 evaluate_expression(stmt
->case_expression
);
1494 evaluate_expression(stmt
->case_to
);
1495 evaluate_statement(stmt
->case_statement
);
1498 evaluate_statement(stmt
->label_statement
);
1501 evaluate_expression(stmt
->goto_expression
);
1506 /* FIXME! Do the asm parameter evaluation! */
1512 long long get_expression_value(struct expression
*expr
)
1514 long long value
, mask
;
1515 struct symbol
*ctype
;
1517 ctype
= evaluate_expression(expr
);
1518 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1519 warn(expr
->pos
, "bad constant expression");
1523 value
= expr
->value
;
1524 mask
= 1ULL << (ctype
->bit_size
-1);
1527 while (ctype
->type
!= SYM_BASETYPE
)
1528 ctype
= ctype
->ctype
.base_type
;
1529 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1530 value
= value
| mask
| ~(mask
-1);