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_ARRAY
);
77 int length
= expr
->string
->length
;
79 sym
->array_size
= length
;
80 sym
->bit_size
= BITS_IN_CHAR
* length
;
81 sym
->ctype
.alignment
= 1;
82 sym
->ctype
.modifiers
= 0;
83 sym
->ctype
.base_type
= &char_ctype
;
88 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
90 unsigned long lmod
, rmod
, mod
;
95 if (left
->bit_size
> right
->bit_size
)
98 if (right
->bit_size
> left
->bit_size
)
101 /* Same size integers - promote to unsigned, promote to long */
102 lmod
= left
->ctype
.modifiers
;
103 rmod
= right
->ctype
.modifiers
;
109 return ctype_integer(mod
);
112 static struct symbol
* cast_value(struct expression
*expr
, struct symbol
*newtype
,
113 struct expression
*old
, struct symbol
*oldtype
)
115 int old_size
= oldtype
->bit_size
;
116 int new_size
= newtype
->bit_size
;
117 long long value
, mask
, ormask
, andmask
;
120 // FIXME! We don't handle FP casts of constant values yet
121 if (newtype
->ctype
.base_type
== &fp_type
)
123 if (oldtype
->ctype
.base_type
== &fp_type
)
126 // For pointers and integers, we can just move the value around
127 expr
->type
= EXPR_VALUE
;
128 if (old_size
== new_size
) {
129 expr
->value
= old
->value
;
133 // expand it to the full "long long" value
134 is_signed
= !(oldtype
->ctype
.modifiers
& MOD_UNSIGNED
);
135 mask
= 1ULL << (old_size
-1);
139 andmask
= mask
| (mask
-1);
143 value
= (value
& andmask
) | ormask
;
145 // Truncate it to the new size
146 mask
= 1ULL << (new_size
-1);
147 mask
= mask
| (mask
-1);
148 expr
->value
= value
& mask
;
152 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
154 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
156 expr
->cast_type
= type
;
157 expr
->cast_expression
= old
;
158 if (old
->type
== EXPR_VALUE
)
159 cast_value(expr
, type
, old
, old
->ctype
);
163 static int is_ptr_type(struct symbol
*type
)
165 if (type
->type
== SYM_NODE
)
166 type
= type
->ctype
.base_type
;
167 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
170 static int is_int_type(struct symbol
*type
)
172 if (type
->type
== SYM_NODE
)
173 type
= type
->ctype
.base_type
;
174 return type
->ctype
.base_type
== &int_type
;
177 static struct symbol
*bad_expr_type(struct expression
*expr
)
179 warn(expr
->pos
, "incompatible types for operation");
183 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
185 struct expression
*left
= *lp
, *right
= *rp
;
186 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
188 if (ltype
->type
== SYM_NODE
)
189 ltype
= ltype
->ctype
.base_type
;
190 if (rtype
->type
== SYM_NODE
)
191 rtype
= rtype
->ctype
.base_type
;
192 /* Integer promotion? */
193 if (ltype
->type
== SYM_ENUM
)
195 if (rtype
->type
== SYM_ENUM
)
197 if (is_int_type(ltype
) && is_int_type(rtype
)) {
198 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
200 /* Don't bother promoting same-size entities, it only adds clutter */
201 if (ltype
->bit_size
!= ctype
->bit_size
)
202 *lp
= cast_to(left
, ctype
);
203 if (rtype
->bit_size
!= ctype
->bit_size
)
204 *rp
= cast_to(right
, ctype
);
210 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
212 if (count
>= ctype
->bit_size
) {
213 warn(expr
->pos
, "shift too big for type");
214 count
&= ctype
->bit_size
-1;
220 * CAREFUL! We need to get the size and sign of the
223 static void simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
225 struct expression
*left
= expr
->left
, *right
= expr
->right
;
226 unsigned long long v
, l
, r
, mask
;
227 signed long long s
, sl
, sr
;
228 int is_signed
, shift
;
230 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
232 l
= left
->value
; r
= right
->value
;
233 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
234 mask
= 1ULL << (ctype
->bit_size
-1);
236 if (is_signed
&& (sl
& mask
))
238 if (is_signed
&& (sr
& mask
))
242 case '+': v
= l
+ r
; s
= v
; break;
243 case '-': v
= l
- r
; s
= v
; break;
244 case '&': v
= l
& r
; s
= v
; break;
245 case '|': v
= l
| r
; s
= v
; break;
246 case '^': v
= l
^ r
; s
= v
; break;
247 case '*': v
= l
* r
; s
= sl
* sr
; break;
248 case '/': if (!r
) return; v
= l
/ r
; s
= sl
/ sr
; break;
249 case '%': if (!r
) return; v
= l
% r
; s
= sl
% sr
; break;
250 case SPECIAL_LEFTSHIFT
: shift
= check_shift_count(expr
, ctype
, r
); v
= l
<< shift
; s
= v
; break;
251 case SPECIAL_RIGHTSHIFT
:shift
= check_shift_count(expr
, ctype
, r
); v
= l
>> shift
; s
= sl
>> shift
; break;
252 case '<': v
= l
< r
; s
= sl
< sr
; break;
253 case '>': v
= l
> r
; s
= sl
> sr
; break;
254 case SPECIAL_LTE
: v
= l
<= r
; s
= sl
<= sr
; break;
255 case SPECIAL_GTE
: v
= l
>= r
; s
= sl
>= sr
; break;
256 case SPECIAL_EQUAL
: v
= l
== r
; s
= v
; break;
257 case SPECIAL_NOTEQUAL
: v
= l
!= r
; s
= v
; break;
262 mask
= mask
| (mask
-1);
263 expr
->value
= v
& mask
;
264 expr
->type
= EXPR_VALUE
;
267 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
269 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
272 simplify_int_binop(expr
, ctype
);
275 return bad_expr_type(expr
);
278 static inline int lvalue_expression(struct expression
*expr
)
280 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
283 /* Arrays degenerate into pointers on pointer arithmetic */
284 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
286 if (ctype
->type
== SYM_ARRAY
) {
287 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
288 struct expression
*ptr
;
290 sym
->ctype
= ctype
->ctype
;
291 sym
->bit_size
= BITS_IN_POINTER
;
292 sym
->ctype
.alignment
= POINTER_ALIGNMENT
;
295 if (expr
->type
!= EXPR_STRING
) {
300 * This all really assumes that we got the degenerate
301 * array as an lvalue (ie a dereference). If that
302 * is not the case, then holler - because we've screwed
305 if (!lvalue_expression(ptr
))
306 warn(ptr
->pos
, "internal error: strange degenerate array case");
312 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
314 struct symbol
*ctype
;
315 struct symbol
*ptr_type
= ptr
->ctype
;
316 struct symbol
*i_type
= i
->ctype
;
319 if (i_type
->type
== SYM_NODE
)
320 i_type
= i_type
->ctype
.base_type
;
321 if (ptr_type
->type
== SYM_NODE
)
322 ptr_type
= ptr_type
->ctype
.base_type
;
324 if (i_type
->type
== SYM_ENUM
)
326 if (!is_int_type(i_type
))
327 return bad_expr_type(expr
);
330 examine_symbol_type(ctype
);
332 ctype
= degenerate(expr
, ctype
, &ptr
);
333 bit_size
= ctype
->bit_size
;
335 /* Special case: adding zero commonly happens as a result of 'array[0]' */
336 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
338 } else if (bit_size
> BITS_IN_CHAR
) {
339 struct expression
*add
= expr
;
340 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
341 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
343 val
->ctype
= size_t_ctype
;
344 val
->value
= bit_size
>> 3;
347 mul
->ctype
= size_t_ctype
;
350 simplify_int_binop(mul
, size_t_ctype
);
352 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
355 simplify_int_binop(add
, ctype
);
362 static struct symbol
*evaluate_add(struct expression
*expr
)
364 struct expression
*left
= expr
->left
, *right
= expr
->right
;
365 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
367 if (is_ptr_type(ltype
))
368 return evaluate_ptr_add(expr
, left
, right
);
370 if (is_ptr_type(rtype
))
371 return evaluate_ptr_add(expr
, right
, left
);
373 // FIXME! FP promotion
374 return evaluate_int_binop(expr
);
377 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
379 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
380 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
383 unsigned long mod1
, mod2
, diff
;
384 unsigned long as1
, as2
;
386 if (target
== source
)
388 if (!target
|| !source
)
389 return "different types";
391 * Peel of per-node information.
392 * FIXME! Check alignment, address space, and context too here!
394 mod1
= target
->ctype
.modifiers
;
395 as1
= target
->ctype
.as
;
396 mod2
= source
->ctype
.modifiers
;
397 as2
= source
->ctype
.as
;
398 if (target
->type
== SYM_NODE
) {
399 target
= target
->ctype
.base_type
;
400 mod1
|= target
->ctype
.modifiers
;
401 as1
|= target
->ctype
.as
;
403 if (source
->type
== SYM_NODE
) {
404 source
= source
->ctype
.base_type
;
405 mod2
|= source
->ctype
.modifiers
;
406 as2
|= source
->ctype
.as
;
409 if (target
->type
!= source
->type
) {
410 int type1
= target
->type
;
411 int type2
= source
->type
;
413 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
414 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
415 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
417 return "different base types";
420 /* Must be same address space to be comparable */
422 return "different address spaces";
424 /* Ignore differences in storage types, sign, or addressability */
425 diff
= (mod1
^ mod2
) & ~(MOD_STORAGE
| MOD_ADDRESSABLE
| MOD_SIGNED
| MOD_UNSIGNED
);
427 mod1
&= diff
& ~target_mod_ignore
;
428 mod2
&= diff
& ~source_mod_ignore
;
430 if ((mod1
| mod2
) & MOD_SIZE
)
431 return "different type sizes";
432 return "different modifiers";
436 target
= target
->ctype
.base_type
;
437 source
= source
->ctype
.base_type
;
442 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
444 /* NULL expression? Just return the type of the "other side" */
445 if (r
->type
== EXPR_VALUE
&& !r
->value
)
447 if (l
->type
== EXPR_VALUE
&& !l
->value
)
453 * Ignore differences in "volatile" and "const"ness when
454 * subtracting pointers
456 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
458 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
460 const char *typediff
;
461 struct symbol
*ctype
;
462 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
465 * If it is an integer subtract: the ptr add case will do the
468 if (!is_ptr_type(rtype
))
469 return evaluate_ptr_add(expr
, l
, r
);
472 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
474 ctype
= common_ptr_type(l
, r
);
476 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
480 examine_symbol_type(ctype
);
482 /* Figure out the base type we point to */
483 if (ctype
->type
== SYM_NODE
)
484 ctype
= ctype
->ctype
.base_type
;
485 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
486 warn(expr
->pos
, "subtraction of functions? Share your drugs");
489 ctype
= ctype
->ctype
.base_type
;
491 expr
->ctype
= ssize_t_ctype
;
492 if (ctype
->bit_size
> BITS_IN_CHAR
) {
493 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
494 struct expression
*div
= expr
;
495 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
497 val
->ctype
= size_t_ctype
;
498 val
->value
= ctype
->bit_size
>> 3;
501 sub
->ctype
= ssize_t_ctype
;
510 return ssize_t_ctype
;
513 static struct symbol
*evaluate_sub(struct expression
*expr
)
515 struct expression
*left
= expr
->left
, *right
= expr
->right
;
516 struct symbol
*ltype
= left
->ctype
;
518 if (is_ptr_type(ltype
))
519 return evaluate_ptr_sub(expr
, left
, right
);
521 // FIXME! FP promotion
522 return evaluate_int_binop(expr
);
525 static struct symbol
*evaluate_logical(struct expression
*expr
)
527 struct expression
*left
= expr
->left
;
528 struct expression
*right
;
530 if (!evaluate_expression(left
))
533 /* Do immediate short-circuiting ... */
534 if (left
->type
== EXPR_VALUE
) {
535 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
537 expr
->type
= EXPR_VALUE
;
539 expr
->ctype
= &bool_ctype
;
544 expr
->type
= EXPR_VALUE
;
546 expr
->ctype
= &bool_ctype
;
553 if (!evaluate_expression(right
))
555 expr
->ctype
= &bool_ctype
;
556 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
558 * We know the left value doesn't matter, since
559 * otherwise we would have short-circuited it..
561 expr
->type
= EXPR_VALUE
;
562 expr
->value
= right
->value
;
568 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
570 // FIXME! Floating-point promotion!
571 return evaluate_int_binop(expr
);
574 static struct symbol
*evaluate_binop(struct expression
*expr
)
577 // addition can take ptr+int, fp and int
579 return evaluate_add(expr
);
581 // subtraction can take ptr-ptr, fp and int
583 return evaluate_sub(expr
);
585 // Arithmetic operations can take fp and int
586 case '*': case '/': case '%':
587 return evaluate_arithmetic(expr
);
589 // The rest are integer operations (bitops)
590 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
593 return evaluate_int_binop(expr
);
597 static struct symbol
*evaluate_comma(struct expression
*expr
)
599 expr
->ctype
= expr
->right
->ctype
;
603 static struct symbol
*evaluate_compare(struct expression
*expr
)
605 struct expression
*left
= expr
->left
, *right
= expr
->right
;
606 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
607 struct symbol
*ctype
;
610 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
611 expr
->ctype
= &bool_ctype
;
612 // FIXME! Check the types for compatibility
616 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
618 simplify_int_binop(expr
, ctype
);
619 expr
->ctype
= &bool_ctype
;
623 return bad_expr_type(expr
);
626 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
628 /* Integer promotion? */
629 if (ltype
->type
== SYM_NODE
)
630 ltype
= ltype
->ctype
.base_type
;
631 if (rtype
->type
== SYM_NODE
)
632 rtype
= rtype
->ctype
.base_type
;
633 if (ltype
->type
== SYM_ENUM
)
635 if (rtype
->type
== SYM_ENUM
)
637 return (is_int_type(ltype
) && is_int_type(rtype
));
640 static int is_void_ptr(struct expression
*expr
)
642 return (expr
->type
== EXPR_VALUE
&&
647 * FIXME!! This shoul ddo casts, array degeneration etc..
649 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
651 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
653 if (ltype
->type
== SYM_PTR
) {
654 if (is_void_ptr(right
))
658 if (rtype
->type
== SYM_PTR
) {
659 if (is_void_ptr(left
))
665 static struct symbol
* evaluate_conditional(struct expression
*expr
)
667 struct expression
*cond
, *true, *false;
668 struct symbol
*ctype
, *ltype
, *rtype
;
669 const char * typediff
;
671 cond
= expr
->conditional
;
672 true = expr
->cond_true
? : cond
;
673 false = expr
->cond_false
;
676 rtype
= false->ctype
;
679 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
681 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
683 ctype
= compatible_ptr_type(true, expr
->cond_false
);
685 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
691 /* Simplify conditional expression.. */
692 if (cond
->type
== EXPR_VALUE
) {
701 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
702 struct expression
**rp
, struct symbol
*source
, const char *where
)
704 const char *typediff
;
708 /* It's ok if the target is more volatile or const than the source */
709 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
713 if (compatible_integer_types(target
, source
)) {
714 if (target
->bit_size
!= source
->bit_size
)
715 *rp
= cast_to(*rp
, target
);
719 /* Pointer destination? */
721 target_as
= t
->ctype
.as
;
722 if (t
->type
== SYM_NODE
) {
723 t
= t
->ctype
.base_type
;
724 target_as
|= t
->ctype
.as
;
726 if (t
->type
== SYM_PTR
) {
727 struct expression
*right
= *rp
;
728 struct symbol
*s
= source
;
731 // NULL pointer is always ok
732 if (right
->type
== EXPR_VALUE
&& !right
->value
)
735 /* "void *" matches anything as long as the address space is ok */
736 source_as
= s
->ctype
.as
;
737 if (s
->type
== SYM_NODE
) {
738 s
= s
->ctype
.base_type
;
739 source_as
|= s
->ctype
.as
;
741 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
742 s
= s
->ctype
.base_type
;
743 t
= t
->ctype
.base_type
;
744 if (s
== &void_ctype
|| t
== &void_ctype
)
748 if (s
->type
== SYM_FN
) {
749 typediff
= type_difference(t
->ctype
.base_type
, s
, 0, 0);
755 warn(expr
->pos
, "different types in %s (%s)", where
, typediff
);
760 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
761 warn(expr
->pos
, " expected %s", show_typename(target
));
762 warn(expr
->pos
, " got %s", show_typename(source
));
767 * FIXME!! This is wrong from a double evaluation standpoint. We can't
768 * just expand the expression twice, that would make any side effects
771 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
774 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
775 static const int op_trans
[] = {
776 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
777 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
778 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
779 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
780 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
781 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
782 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
783 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
784 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '&',
785 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
788 subexpr
->left
= left
;
789 subexpr
->right
= right
;
790 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
792 expr
->right
= subexpr
;
793 return evaluate_binop(subexpr
);
796 static struct symbol
*evaluate_assignment(struct expression
*expr
)
798 struct expression
*left
= expr
->left
, *right
= expr
->right
;
799 struct symbol
*ltype
, *rtype
;
802 rtype
= right
->ctype
;
803 if (expr
->op
!= '=') {
804 rtype
= evaluate_binop_assignment(expr
, left
, right
);
810 if (!lvalue_expression(left
)) {
811 warn(expr
->pos
, "not an lvalue");
815 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
818 expr
->ctype
= expr
->left
->ctype
;
822 static struct symbol
*evaluate_addressof(struct expression
*expr
)
824 struct symbol
*ctype
, *symbol
;
825 struct expression
*op
= expr
->unop
;
827 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
828 warn(expr
->pos
, "not addressable");
832 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
833 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
834 symbol
->bit_size
= BITS_IN_POINTER
;
837 if (ctype
->type
== SYM_NODE
) {
838 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
839 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
840 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
841 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
843 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
844 symbol
->ctype
.as
= ctype
->ctype
.as
;
845 symbol
->ctype
.context
= ctype
->ctype
.context
;
846 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
847 ctype
= ctype
->ctype
.base_type
;
850 symbol
->ctype
.base_type
= ctype
;
852 expr
->ctype
= symbol
;
857 static struct symbol
*evaluate_dereference(struct expression
*expr
)
859 struct expression
*op
= expr
->unop
;
860 struct symbol
*ctype
= op
->ctype
, *sym
;
862 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
863 sym
->ctype
= ctype
->ctype
;
864 if (ctype
->type
== SYM_NODE
) {
865 ctype
= ctype
->ctype
.base_type
;
866 sym
->ctype
.modifiers
|= ctype
->ctype
.modifiers
;
867 sym
->ctype
.as
|= ctype
->ctype
.as
;
869 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
870 warn(expr
->pos
, "cannot derefence this type");
874 ctype
= ctype
->ctype
.base_type
;
876 warn(expr
->pos
, "undefined type");
879 examine_symbol_type(ctype
);
880 sym
->ctype
.base_type
= ctype
;
882 sym
->bit_size
= ctype
->bit_size
;
883 sym
->array_size
= ctype
->array_size
;
885 /* Simplify: *&(expr) => (expr) */
886 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
894 static void simplify_preop(struct expression
*expr
)
896 struct expression
*op
= expr
->unop
;
897 unsigned long long v
, mask
;
899 if (op
->type
!= EXPR_VALUE
)
904 case '-': v
= -v
; break;
905 case '!': v
= !v
; break;
906 case '~': v
= ~v
; break;
909 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
910 mask
= mask
| (mask
-1);
911 expr
->value
= v
& mask
;
912 expr
->type
= EXPR_VALUE
;
916 * Unary post-ops: x++ and x--
918 static struct symbol
*evaluate_postop(struct expression
*expr
)
920 struct expression
*op
= expr
->unop
;
921 struct symbol
*ctype
= op
->ctype
;
923 if (!lvalue_expression(expr
->unop
)) {
924 warn(expr
->pos
, "need lvalue expression for ++/--");
931 static struct symbol
*evaluate_preop(struct expression
*expr
)
933 struct symbol
*ctype
= expr
->unop
->ctype
;
942 return evaluate_dereference(expr
);
945 return evaluate_addressof(expr
);
948 expr
->ctype
= &bool_ctype
;
949 simplify_preop(expr
);
952 case SPECIAL_INCREMENT
:
953 case SPECIAL_DECREMENT
:
955 * From a type evaluation standpoint the pre-ops are
956 * the same as the postops
958 return evaluate_postop(expr
);
962 simplify_preop(expr
);
967 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
969 struct ptr_list
*head
= (struct ptr_list
*)_list
;
970 struct ptr_list
*list
= head
;
976 for (i
= 0; i
< list
->nr
; i
++) {
977 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
979 if (sym
->ident
!= ident
)
981 *offset
= sym
->offset
;
984 struct symbol
*ctype
= sym
->ctype
.base_type
;
988 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
990 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
993 *offset
+= sym
->offset
;
997 } while ((list
= list
->next
) != head
);
1001 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1003 struct expression
*add
;
1008 /* Create a new add-expression */
1009 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1011 add
->ctype
= &ptr_ctype
;
1013 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1014 add
->right
->ctype
= &int_ctype
;
1015 add
->right
->value
= offset
;
1017 simplify_int_binop(add
, &ptr_ctype
);
1021 /* structure/union dereference */
1022 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1025 struct symbol
*ctype
, *member
, *sym
;
1026 struct expression
*deref
= expr
->deref
, *add
;
1027 struct ident
*ident
= expr
->member
;
1031 if (!evaluate_expression(deref
))
1034 warn(expr
->pos
, "bad member name");
1038 ctype
= deref
->ctype
;
1039 mod
= ctype
->ctype
.modifiers
;
1040 address_space
= ctype
->ctype
.as
;
1041 if (ctype
->type
== SYM_NODE
) {
1042 ctype
= ctype
->ctype
.base_type
;
1043 mod
|= ctype
->ctype
.modifiers
;
1045 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1046 /* Arrays will degenerate into pointers for '->' */
1047 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1048 warn(expr
->pos
, "expected a pointer to a struct/union");
1051 address_space
= ctype
->ctype
.as
;
1052 ctype
= ctype
->ctype
.base_type
;
1053 mod
|= ctype
->ctype
.modifiers
;
1054 if (ctype
->type
== SYM_NODE
) {
1055 ctype
= ctype
->ctype
.base_type
;
1056 mod
|= ctype
->ctype
.modifiers
;
1059 if (!lvalue_expression(deref
)) {
1060 warn(deref
->pos
, "expected lvalue for member dereference");
1063 deref
= deref
->unop
;
1064 expr
->deref
= deref
;
1066 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1067 warn(expr
->pos
, "expected structure or union");
1071 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1073 warn(expr
->pos
, "no such struct/union member");
1077 add
= evaluate_offset(deref
, offset
);
1079 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1080 sym
->bit_size
= member
->bit_size
;
1081 sym
->array_size
= member
->array_size
;
1082 sym
->ctype
= member
->ctype
;
1083 sym
->ctype
.modifiers
= mod
;
1084 sym
->ctype
.as
= address_space
;
1085 ctype
= member
->ctype
.base_type
;
1086 if (ctype
->type
== SYM_BITFIELD
) {
1087 ctype
= ctype
->ctype
.base_type
;
1088 expr
->type
= EXPR_BITFIELD
;
1089 expr
->bitpos
= member
->bit_offset
;
1090 expr
->nrbits
= member
->fieldwidth
;
1091 expr
->address
= add
;
1093 expr
->type
= EXPR_PREOP
;
1102 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1106 if (expr
->cast_type
) {
1107 examine_symbol_type(expr
->cast_type
);
1108 size
= expr
->cast_type
->bit_size
;
1110 if (!evaluate_expression(expr
->cast_expression
))
1112 size
= expr
->cast_expression
->ctype
->bit_size
;
1115 warn(expr
->pos
, "cannot size expression");
1118 expr
->type
= EXPR_VALUE
;
1119 expr
->value
= size
>> 3;
1120 expr
->ctype
= size_t_ctype
;
1121 return size_t_ctype
;
1124 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
1126 struct expression
*expr
;
1127 struct symbol_list
*argument_types
= fn
->arguments
;
1128 struct symbol
*argtype
;
1130 PREPARE_PTR_LIST(argument_types
, argtype
);
1131 FOR_EACH_PTR (head
, expr
) {
1132 struct expression
**p
= THIS_ADDRESS(expr
);
1133 struct symbol
*ctype
, *target
;
1134 ctype
= evaluate_expression(expr
);
1139 if (ctype
->type
== SYM_ARRAY
) {
1140 ctype
= degenerate(expr
, ctype
, p
);
1141 expr
->ctype
= ctype
;
1145 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1146 target
= &int_ctype
;
1148 examine_symbol_type(target
);
1149 compatible_assignment_types(expr
, target
, p
, ctype
, "argument passing");
1152 NEXT_PTR_LIST(argument_types
, argtype
);
1159 * FIXME! This is bogus: we need to take array index
1160 * entries into account when calculating the size of
1163 static int count_array_initializer(struct expression
*expr
)
1165 struct expression_list
*list
;
1167 if (expr
->type
!= EXPR_INITIALIZER
)
1169 list
= expr
->expr_list
;
1170 return expression_list_size(list
);
1173 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1175 return count_array_initializer(expr
);
1178 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1180 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1185 * Initializers are kind of like assignments. Except
1186 * they can be a hell of a lot more complex.
1188 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1190 struct expression
*expr
= *ep
;
1193 * Simple non-structure/array initializers are the simple
1194 * case, and look (and parse) largely like assignments.
1196 if (expr
->type
!= EXPR_INITIALIZER
) {
1198 struct symbol
*rtype
= evaluate_expression(expr
);
1200 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1201 /* strings are special: char arrays */
1202 if (rtype
->type
== SYM_ARRAY
)
1203 size
= rtype
->array_size
;
1208 expr
->ctype
= ctype
;
1209 switch (ctype
->type
) {
1212 return evaluate_array_initializer(ctype
, expr
);
1214 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1216 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1220 warn(expr
->pos
, "unexpected compound initializer");
1224 static struct symbol
*evaluate_cast(struct expression
*expr
)
1226 struct expression
*target
= expr
->cast_expression
;
1227 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1229 expr
->ctype
= ctype
;
1230 expr
->cast_type
= ctype
;
1233 * Special case: a cast can be followed by an
1234 * initializer, in which case we need to pass
1235 * the type value down to that initializer rather
1236 * than trying to evaluate it as an expression
1238 if (target
->type
== EXPR_INITIALIZER
) {
1239 evaluate_initializer(ctype
, &expr
->cast_expression
);
1243 evaluate_expression(target
);
1245 /* Simplify normal integer casts.. */
1246 if (target
->type
== EXPR_VALUE
)
1247 cast_value(expr
, ctype
, target
, target
->ctype
);
1251 static struct symbol
*evaluate_call(struct expression
*expr
)
1254 struct symbol
*ctype
;
1255 struct expression
*fn
= expr
->fn
;
1256 struct expression_list
*arglist
= expr
->args
;
1258 if (!evaluate_expression(fn
))
1261 if (ctype
->type
== SYM_NODE
) {
1263 * FIXME!! We should really expand the inline functions.
1264 * For now we just mark them accessed so that they show
1265 * up on the list of used symbols.
1267 if (ctype
->ctype
.modifiers
& MOD_INLINE
)
1268 access_symbol(ctype
);
1269 ctype
= ctype
->ctype
.base_type
;
1271 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1272 ctype
= ctype
->ctype
.base_type
;
1273 if (ctype
->type
!= SYM_FN
) {
1274 warn(expr
->pos
, "not a function");
1277 if (!evaluate_arguments(ctype
, arglist
))
1279 args
= expression_list_size(expr
->args
);
1280 fnargs
= symbol_list_size(ctype
->arguments
);
1282 warn(expr
->pos
, "not enough arguments for function");
1283 if (args
> fnargs
&& !ctype
->variadic
)
1284 warn(expr
->pos
, "too many arguments for function");
1285 expr
->ctype
= ctype
->ctype
.base_type
;
1289 struct symbol
*evaluate_expression(struct expression
*expr
)
1296 switch (expr
->type
) {
1298 warn(expr
->pos
, "value expression without a type");
1301 return evaluate_string(expr
);
1303 return evaluate_symbol_expression(expr
);
1305 if (!evaluate_expression(expr
->left
))
1307 if (!evaluate_expression(expr
->right
))
1309 return evaluate_binop(expr
);
1311 return evaluate_logical(expr
);
1313 if (!evaluate_expression(expr
->left
))
1315 if (!evaluate_expression(expr
->right
))
1317 return evaluate_comma(expr
);
1319 if (!evaluate_expression(expr
->left
))
1321 if (!evaluate_expression(expr
->right
))
1323 return evaluate_compare(expr
);
1324 case EXPR_ASSIGNMENT
:
1325 if (!evaluate_expression(expr
->left
))
1327 if (!evaluate_expression(expr
->right
))
1329 return evaluate_assignment(expr
);
1331 if (!evaluate_expression(expr
->unop
))
1333 return evaluate_preop(expr
);
1335 if (!evaluate_expression(expr
->unop
))
1337 return evaluate_postop(expr
);
1339 return evaluate_cast(expr
);
1341 return evaluate_sizeof(expr
);
1343 return evaluate_member_dereference(expr
);
1345 return evaluate_call(expr
);
1347 warn(expr
->pos
, "bitfield generated by parser");
1349 case EXPR_CONDITIONAL
:
1350 if (!evaluate_expression(expr
->conditional
))
1352 if (!evaluate_expression(expr
->cond_false
))
1354 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1356 return evaluate_conditional(expr
);
1357 case EXPR_STATEMENT
:
1358 expr
->ctype
= evaluate_statement(expr
->statement
);
1361 /* These can not exist as stand-alone expressions */
1362 case EXPR_INITIALIZER
:
1363 case EXPR_IDENTIFIER
:
1365 warn(expr
->pos
, "internal front-end error: initializer in expression");
1371 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1373 struct symbol
**last
= _last
;
1374 struct symbol
*type
= evaluate_statement(stmt
);
1376 if (flags
& ITERATE_LAST
)
1380 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1382 evaluate_symbol(sym
);
1385 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1387 struct symbol
*base_type
;
1389 examine_symbol_type(sym
);
1390 base_type
= sym
->ctype
.base_type
;
1393 sym
->ctype
.base_type
= base_type
;
1395 /* Evaluate the initializers */
1396 if (sym
->initializer
) {
1397 int count
= evaluate_initializer(base_type
, &sym
->initializer
);
1398 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1399 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1400 base_type
->array_size
= count
;
1401 base_type
->bit_size
= bit_size
;
1402 sym
->array_size
= count
;
1403 sym
->bit_size
= bit_size
;
1407 /* And finally, evaluate the body of the symbol too */
1408 if (base_type
->type
== SYM_FN
) {
1409 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1410 if (base_type
->stmt
) {
1411 current_fn
= base_type
;
1412 current_contextmask
= sym
->ctype
.contextmask
;
1413 current_context
= sym
->ctype
.context
;
1414 evaluate_statement(base_type
->stmt
);
1421 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1423 struct expression
*expr
= stmt
->expression
;
1424 struct symbol
*ctype
, *fntype
;
1426 fntype
= current_fn
->ctype
.base_type
;
1427 if (fntype
== &void_ctype
) {
1429 warn(expr
->pos
, "return expression in void function");
1434 warn(stmt
->pos
, "return with no return value");
1437 ctype
= evaluate_expression(expr
);
1440 ctype
= degenerate(expr
, ctype
, &expr
);
1441 expr
->ctype
= ctype
;
1442 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1443 stmt
->expression
= expr
;
1447 struct symbol
*evaluate_statement(struct statement
*stmt
)
1452 switch (stmt
->type
) {
1454 return evaluate_return_expression(stmt
);
1456 case STMT_EXPRESSION
:
1457 return evaluate_expression(stmt
->expression
);
1459 case STMT_COMPOUND
: {
1460 struct symbol
*type
= NULL
;
1461 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1462 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1466 evaluate_expression(stmt
->if_conditional
);
1467 evaluate_statement(stmt
->if_true
);
1468 evaluate_statement(stmt
->if_false
);
1471 evaluate_expression(stmt
->iterator_pre_condition
);
1472 evaluate_expression(stmt
->iterator_post_condition
);
1473 evaluate_statement(stmt
->iterator_pre_statement
);
1474 evaluate_statement(stmt
->iterator_statement
);
1475 evaluate_statement(stmt
->iterator_post_statement
);
1478 evaluate_expression(stmt
->switch_expression
);
1479 evaluate_statement(stmt
->switch_statement
);
1482 evaluate_expression(stmt
->case_expression
);
1483 evaluate_expression(stmt
->case_to
);
1484 evaluate_statement(stmt
->case_statement
);
1487 evaluate_statement(stmt
->label_statement
);
1490 evaluate_expression(stmt
->goto_expression
);
1495 /* FIXME! Do the asm parameter evaluation! */
1501 long long get_expression_value(struct expression
*expr
)
1503 long long value
, mask
;
1504 struct symbol
*ctype
;
1506 ctype
= evaluate_expression(expr
);
1507 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1508 warn(expr
->pos
, "bad constant expression");
1512 value
= expr
->value
;
1513 mask
= 1ULL << (ctype
->bit_size
-1);
1516 while (ctype
->type
!= SYM_BASETYPE
)
1517 ctype
= ctype
->ctype
.base_type
;
1518 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1519 value
= value
| mask
| ~(mask
-1);