4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
8 * Evaluate constant expressions.
25 #include "expression.h"
27 static struct symbol
*current_fn
;
28 static int current_context
, current_contextmask
;
30 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
32 struct symbol
*sym
= expr
->symbol
;
33 struct symbol
*base_type
;
37 warn(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
38 expr
->type
= EXPR_VALUE
;
40 expr
->ctype
= &int_ctype
;
43 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
47 examine_symbol_type(sym
);
48 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
49 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
51 base_type
= sym
->ctype
.base_type
;
53 warn(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
57 /* The type of a symbol is the symbol itself! */
60 /* Const symbol with a constant initializer? */
61 if (sym
->ctype
.modifiers
& MOD_CONST
) {
62 struct expression
*value
= sym
->initializer
;
63 if (value
&& value
->type
== EXPR_VALUE
) {
64 expr
->type
= EXPR_VALUE
;
65 expr
->value
= value
->value
;
70 /* enum's can be turned into plain values */
71 if (sym
->type
!= SYM_ENUM
) {
72 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
74 addr
->symbol_name
= expr
->symbol_name
;
75 addr
->ctype
= &ptr_ctype
;
76 expr
->type
= EXPR_PREOP
;
81 expr
->type
= EXPR_VALUE
;
82 expr
->value
= sym
->value
;
83 expr
->ctype
= base_type
;
87 static struct symbol
*evaluate_string(struct expression
*expr
)
89 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
90 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
91 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
92 int length
= expr
->string
->length
;
94 sym
->array_size
= length
;
95 sym
->bit_size
= BITS_IN_CHAR
* length
;
96 sym
->ctype
.alignment
= 1;
97 sym
->ctype
.modifiers
= MOD_STATIC
;
98 sym
->ctype
.base_type
= array
;
100 array
->array_size
= length
;
101 array
->bit_size
= BITS_IN_CHAR
* length
;
102 array
->ctype
.alignment
= 1;
103 array
->ctype
.modifiers
= MOD_STATIC
;
104 array
->ctype
.base_type
= &char_ctype
;
107 addr
->ctype
= &ptr_ctype
;
109 expr
->type
= EXPR_PREOP
;
116 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
118 unsigned long lmod
, rmod
, mod
;
123 if (left
->bit_size
> right
->bit_size
)
126 if (right
->bit_size
> left
->bit_size
)
129 /* Same size integers - promote to unsigned, promote to long */
130 lmod
= left
->ctype
.modifiers
;
131 rmod
= right
->ctype
.modifiers
;
137 return ctype_integer(mod
);
140 static struct symbol
* cast_value(struct expression
*expr
, struct symbol
*newtype
,
141 struct expression
*old
, struct symbol
*oldtype
)
143 int old_size
= oldtype
->bit_size
;
144 int new_size
= newtype
->bit_size
;
145 long long value
, mask
, ormask
, andmask
;
148 // FIXME! We don't handle FP casts of constant values yet
149 if (newtype
->ctype
.base_type
== &fp_type
)
151 if (oldtype
->ctype
.base_type
== &fp_type
)
154 // For pointers and integers, we can just move the value around
155 expr
->type
= EXPR_VALUE
;
156 if (old_size
== new_size
) {
157 expr
->value
= old
->value
;
161 // expand it to the full "long long" value
162 is_signed
= !(oldtype
->ctype
.modifiers
& MOD_UNSIGNED
);
163 mask
= 1ULL << (old_size
-1);
167 andmask
= mask
| (mask
-1);
171 value
= (value
& andmask
) | ormask
;
173 // Truncate it to the new size
174 mask
= 1ULL << (new_size
-1);
175 mask
= mask
| (mask
-1);
176 expr
->value
= value
& mask
;
180 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
182 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
184 expr
->cast_type
= type
;
185 expr
->cast_expression
= old
;
186 if (old
->type
== EXPR_VALUE
)
187 cast_value(expr
, type
, old
, old
->ctype
);
191 static int is_ptr_type(struct symbol
*type
)
193 if (type
->type
== SYM_NODE
)
194 type
= type
->ctype
.base_type
;
195 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
198 static int is_int_type(struct symbol
*type
)
200 if (type
->type
== SYM_NODE
)
201 type
= type
->ctype
.base_type
;
202 return type
->ctype
.base_type
== &int_type
;
205 static struct symbol
*bad_expr_type(struct expression
*expr
)
207 warn(expr
->pos
, "incompatible types for operation");
211 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
213 struct expression
*left
= *lp
, *right
= *rp
;
214 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
216 if (ltype
->type
== SYM_NODE
)
217 ltype
= ltype
->ctype
.base_type
;
218 if (rtype
->type
== SYM_NODE
)
219 rtype
= rtype
->ctype
.base_type
;
220 /* Integer promotion? */
221 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
223 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
225 if (is_int_type(ltype
) && is_int_type(rtype
)) {
226 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
228 /* Don't bother promoting same-size entities, it only adds clutter */
229 if (ltype
->bit_size
!= ctype
->bit_size
)
230 *lp
= cast_to(left
, ctype
);
231 if (rtype
->bit_size
!= ctype
->bit_size
)
232 *rp
= cast_to(right
, ctype
);
238 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
240 if (count
>= ctype
->bit_size
) {
241 warn(expr
->pos
, "shift too big for type");
242 count
&= ctype
->bit_size
-1;
248 * CAREFUL! We need to get the size and sign of the
251 static void simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
253 struct expression
*left
= expr
->left
, *right
= expr
->right
;
254 unsigned long long v
, l
, r
, mask
;
255 signed long long s
, sl
, sr
;
256 int is_signed
, shift
;
258 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
260 l
= left
->value
; r
= right
->value
;
261 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
262 mask
= 1ULL << (ctype
->bit_size
-1);
264 if (is_signed
&& (sl
& mask
))
266 if (is_signed
&& (sr
& mask
))
270 case '+': v
= l
+ r
; s
= v
; break;
271 case '-': v
= l
- r
; s
= v
; break;
272 case '&': v
= l
& r
; s
= v
; break;
273 case '|': v
= l
| r
; s
= v
; break;
274 case '^': v
= l
^ r
; s
= v
; break;
275 case '*': v
= l
* r
; s
= sl
* sr
; break;
276 case '/': if (!r
) return; v
= l
/ r
; s
= sl
/ sr
; break;
277 case '%': if (!r
) return; v
= l
% r
; s
= sl
% sr
; break;
278 case SPECIAL_LEFTSHIFT
: shift
= check_shift_count(expr
, ctype
, r
); v
= l
<< shift
; s
= v
; break;
279 case SPECIAL_RIGHTSHIFT
:shift
= check_shift_count(expr
, ctype
, r
); v
= l
>> shift
; s
= sl
>> shift
; break;
280 case '<': v
= l
< r
; s
= sl
< sr
; break;
281 case '>': v
= l
> r
; s
= sl
> sr
; break;
282 case SPECIAL_LTE
: v
= l
<= r
; s
= sl
<= sr
; break;
283 case SPECIAL_GTE
: v
= l
>= r
; s
= sl
>= sr
; break;
284 case SPECIAL_EQUAL
: v
= l
== r
; s
= v
; break;
285 case SPECIAL_NOTEQUAL
: v
= l
!= r
; s
= v
; break;
290 mask
= mask
| (mask
-1);
291 expr
->value
= v
& mask
;
292 expr
->type
= EXPR_VALUE
;
295 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
297 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
300 simplify_int_binop(expr
, ctype
);
303 return bad_expr_type(expr
);
306 static inline int lvalue_expression(struct expression
*expr
)
308 while (expr
->type
== EXPR_CAST
)
309 expr
= expr
->cast_expression
;
310 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
313 /* Arrays degenerate into pointers on pointer arithmetic */
314 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
316 struct symbol
*base
= ctype
;
318 if (ctype
->type
== SYM_NODE
)
319 base
= ctype
->ctype
.base_type
;
320 if (base
->type
== SYM_ARRAY
|| base
->type
== SYM_FN
) {
321 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
322 struct expression
*ptr
;
324 merge_type(sym
, ctype
);
325 if (base
->type
== SYM_FN
)
327 merge_type(sym
, base
);
328 sym
->bit_size
= BITS_IN_POINTER
;
335 * This all really assumes that we got the degenerate
336 * array as an lvalue (ie a dereference). If that
337 * is not the case, then holler - because we've screwed
340 if (!lvalue_expression(ptr
))
341 warn(ptr
->pos
, "internal error: strange degenerate array case");
347 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
349 struct symbol
*ctype
;
350 struct symbol
*ptr_type
= ptr
->ctype
;
351 struct symbol
*i_type
= i
->ctype
;
354 if (i_type
->type
== SYM_NODE
)
355 i_type
= i_type
->ctype
.base_type
;
356 if (ptr_type
->type
== SYM_NODE
)
357 ptr_type
= ptr_type
->ctype
.base_type
;
359 if (i_type
->type
== SYM_ENUM
)
361 if (!is_int_type(i_type
))
362 return bad_expr_type(expr
);
365 examine_symbol_type(ctype
);
367 ctype
= degenerate(expr
, ctype
, &ptr
);
368 bit_size
= ctype
->bit_size
;
370 /* Special case: adding zero commonly happens as a result of 'array[0]' */
371 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
373 } else if (bit_size
> BITS_IN_CHAR
) {
374 struct expression
*add
= expr
;
375 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
376 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
378 val
->ctype
= size_t_ctype
;
379 val
->value
= bit_size
>> 3;
382 mul
->ctype
= size_t_ctype
;
385 simplify_int_binop(mul
, size_t_ctype
);
387 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
390 simplify_int_binop(add
, ctype
);
397 static struct symbol
*evaluate_add(struct expression
*expr
)
399 struct expression
*left
= expr
->left
, *right
= expr
->right
;
400 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
402 if (is_ptr_type(ltype
))
403 return evaluate_ptr_add(expr
, left
, right
);
405 if (is_ptr_type(rtype
))
406 return evaluate_ptr_add(expr
, right
, left
);
408 // FIXME! FP promotion
409 return evaluate_int_binop(expr
);
412 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
413 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
415 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
416 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
419 unsigned long mod1
, mod2
, diff
;
420 unsigned long as1
, as2
;
422 if (target
== source
)
424 if (!target
|| !source
)
425 return "different types";
427 * Peel of per-node information.
428 * FIXME! Check alignment, address space, and context too here!
430 if (target
->type
== SYM_NODE
)
431 target
= target
->ctype
.base_type
;
432 if (source
->type
== SYM_NODE
)
433 source
= source
->ctype
.base_type
;
434 mod1
= target
->ctype
.modifiers
;
435 as1
= target
->ctype
.as
;
436 mod2
= source
->ctype
.modifiers
;
437 as2
= source
->ctype
.as
;
439 if (target
->type
!= source
->type
) {
440 int type1
= target
->type
;
441 int type2
= source
->type
;
443 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
444 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
445 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
447 if ((type1
== SYM_PTR
) && (target
->ctype
.base_type
->type
== SYM_FN
)) {
448 target
= target
->ctype
.base_type
;
452 if ((type2
== SYM_PTR
) && (source
->ctype
.base_type
->type
== SYM_FN
)) {
453 source
= source
->ctype
.base_type
;
458 return "different base types";
461 /* Must be same address space to be comparable */
463 return "different address spaces";
465 /* Ignore differences in storage types, sign, or addressability */
466 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
468 mod1
&= diff
& ~target_mod_ignore
;
469 mod2
&= diff
& ~source_mod_ignore
;
471 if ((mod1
| mod2
) & MOD_SIZE
)
472 return "different type sizes";
473 return "different modifiers";
477 if (target
->type
== SYM_FN
) {
479 struct symbol
*arg1
, *arg2
;
480 if (target
->variadic
!= source
->variadic
)
481 return "incompatible variadic arguments";
482 PREPARE_PTR_LIST(target
->arguments
, arg1
);
483 PREPARE_PTR_LIST(source
->arguments
, arg2
);
487 diff
= type_difference(arg1
, arg2
, 0, 0);
489 static char argdiff
[80];
490 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
499 FINISH_PTR_LIST(arg2
);
500 FINISH_PTR_LIST(arg1
);
503 target
= target
->ctype
.base_type
;
504 source
= source
->ctype
.base_type
;
509 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
511 /* NULL expression? Just return the type of the "other side" */
512 if (r
->type
== EXPR_VALUE
&& !r
->value
)
514 if (l
->type
== EXPR_VALUE
&& !l
->value
)
520 * Ignore differences in "volatile" and "const"ness when
521 * subtracting pointers
523 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
525 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
527 const char *typediff
;
528 struct symbol
*ctype
;
529 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
532 * If it is an integer subtract: the ptr add case will do the
535 if (!is_ptr_type(rtype
))
536 return evaluate_ptr_add(expr
, l
, r
);
539 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
541 ctype
= common_ptr_type(l
, r
);
543 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
547 examine_symbol_type(ctype
);
549 /* Figure out the base type we point to */
550 if (ctype
->type
== SYM_NODE
)
551 ctype
= ctype
->ctype
.base_type
;
552 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
553 warn(expr
->pos
, "subtraction of functions? Share your drugs");
556 ctype
= ctype
->ctype
.base_type
;
558 expr
->ctype
= ssize_t_ctype
;
559 if (ctype
->bit_size
> BITS_IN_CHAR
) {
560 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
561 struct expression
*div
= expr
;
562 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
564 val
->ctype
= size_t_ctype
;
565 val
->value
= ctype
->bit_size
>> 3;
568 sub
->ctype
= ssize_t_ctype
;
577 return ssize_t_ctype
;
580 static struct symbol
*evaluate_sub(struct expression
*expr
)
582 struct expression
*left
= expr
->left
, *right
= expr
->right
;
583 struct symbol
*ltype
= left
->ctype
;
585 if (is_ptr_type(ltype
))
586 return evaluate_ptr_sub(expr
, left
, right
);
588 // FIXME! FP promotion
589 return evaluate_int_binop(expr
);
592 static struct symbol
*evaluate_logical(struct expression
*expr
)
594 struct expression
*left
= expr
->left
;
595 struct expression
*right
;
597 if (!evaluate_expression(left
))
600 /* Do immediate short-circuiting ... */
601 if (left
->type
== EXPR_VALUE
) {
602 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
604 expr
->type
= EXPR_VALUE
;
606 expr
->ctype
= &bool_ctype
;
611 expr
->type
= EXPR_VALUE
;
613 expr
->ctype
= &bool_ctype
;
620 if (!evaluate_expression(right
))
622 expr
->ctype
= &bool_ctype
;
623 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
625 * We know the left value doesn't matter, since
626 * otherwise we would have short-circuited it..
628 expr
->type
= EXPR_VALUE
;
629 expr
->value
= right
->value
;
635 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
637 // FIXME! Floating-point promotion!
638 return evaluate_int_binop(expr
);
641 static struct symbol
*evaluate_binop(struct expression
*expr
)
644 // addition can take ptr+int, fp and int
646 return evaluate_add(expr
);
648 // subtraction can take ptr-ptr, fp and int
650 return evaluate_sub(expr
);
652 // Arithmetic operations can take fp and int
653 case '*': case '/': case '%':
654 return evaluate_arithmetic(expr
);
656 // The rest are integer operations (bitops)
657 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
660 return evaluate_int_binop(expr
);
664 static struct symbol
*evaluate_comma(struct expression
*expr
)
666 expr
->ctype
= expr
->right
->ctype
;
670 static struct symbol
*evaluate_compare(struct expression
*expr
)
672 struct expression
*left
= expr
->left
, *right
= expr
->right
;
673 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
674 struct symbol
*ctype
;
677 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
678 expr
->ctype
= &bool_ctype
;
679 // FIXME! Check the types for compatibility
683 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
685 simplify_int_binop(expr
, ctype
);
686 expr
->ctype
= &bool_ctype
;
690 return bad_expr_type(expr
);
693 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
695 /* Integer promotion? */
696 if (ltype
->type
== SYM_NODE
)
697 ltype
= ltype
->ctype
.base_type
;
698 if (rtype
->type
== SYM_NODE
)
699 rtype
= rtype
->ctype
.base_type
;
700 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
702 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
704 return (is_int_type(ltype
) && is_int_type(rtype
));
707 static int is_null_ptr(struct expression
*expr
)
709 return (expr
->type
== EXPR_VALUE
&&
714 * FIXME!! This shoul ddo casts, array degeneration etc..
716 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
718 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
720 if (ltype
->type
== SYM_PTR
) {
721 if (is_null_ptr(right
))
725 if (rtype
->type
== SYM_PTR
) {
726 if (is_null_ptr(left
))
732 static struct symbol
*do_degenerate(struct expression
**ep
)
734 struct expression
*expr
= *ep
;
735 return degenerate(expr
, expr
->ctype
, ep
);
738 static struct symbol
* evaluate_conditional(struct expression
*expr
)
740 struct expression
*cond
, *true, *false;
741 struct symbol
*ctype
, *ltype
, *rtype
;
742 const char * typediff
;
744 ctype
= do_degenerate(&expr
->conditional
);
745 cond
= expr
->conditional
;
749 if (expr
->cond_true
) {
750 ltype
= do_degenerate(&expr
->cond_true
);
751 true = expr
->cond_true
;
754 rtype
= do_degenerate(&expr
->cond_false
);
755 false = expr
->cond_false
;
758 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
760 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
762 ctype
= compatible_ptr_type(true, expr
->cond_false
);
764 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
770 /* Simplify conditional expression.. */
771 if (cond
->type
== EXPR_VALUE
) {
780 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
781 struct expression
**rp
, struct symbol
*source
, const char *where
)
783 const char *typediff
;
787 /* It's ok if the target is more volatile or const than the source */
788 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
792 if (compatible_integer_types(target
, source
)) {
793 if (target
->bit_size
!= source
->bit_size
)
794 *rp
= cast_to(*rp
, target
);
798 /* Pointer destination? */
800 target_as
= t
->ctype
.as
;
801 if (t
->type
== SYM_NODE
) {
802 t
= t
->ctype
.base_type
;
803 target_as
|= t
->ctype
.as
;
805 if (t
->type
== SYM_PTR
) {
806 struct expression
*right
= *rp
;
807 struct symbol
*s
= source
;
810 // NULL pointer is always ok
811 if (right
->type
== EXPR_VALUE
&& !right
->value
)
814 /* "void *" matches anything as long as the address space is ok */
815 source_as
= s
->ctype
.as
;
816 if (s
->type
== SYM_NODE
) {
817 s
= s
->ctype
.base_type
;
818 source_as
|= s
->ctype
.as
;
820 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
821 s
= s
->ctype
.base_type
;
822 t
= t
->ctype
.base_type
;
823 if (s
== &void_ctype
|| t
== &void_ctype
)
829 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
830 warn(expr
->pos
, " expected %s", show_typename(target
));
831 warn(expr
->pos
, " got %s", show_typename(source
));
836 * FIXME!! This is wrong from a double evaluation standpoint. We can't
837 * just expand the expression twice, that would make any side effects
840 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
843 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
844 static const int op_trans
[] = {
845 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
846 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
847 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
848 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
849 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
850 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
851 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
852 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
853 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
854 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
857 subexpr
->left
= left
;
858 subexpr
->right
= right
;
859 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
861 expr
->right
= subexpr
;
862 return evaluate_binop(subexpr
);
865 static struct symbol
*evaluate_assignment(struct expression
*expr
)
867 struct expression
*left
= expr
->left
, *right
= expr
->right
;
868 struct symbol
*ltype
, *rtype
;
871 rtype
= right
->ctype
;
872 if (expr
->op
!= '=') {
873 rtype
= evaluate_binop_assignment(expr
, left
, right
);
879 if (!lvalue_expression(left
)) {
880 warn(expr
->pos
, "not an lvalue");
884 rtype
= degenerate(right
, rtype
, &expr
->right
);
886 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
889 expr
->ctype
= expr
->left
->ctype
;
893 static struct symbol
*evaluate_addressof(struct expression
*expr
)
895 struct symbol
*ctype
, *symbol
;
896 struct expression
*op
= expr
->unop
;
898 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
899 warn(expr
->pos
, "not addressable");
903 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
904 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
905 symbol
->bit_size
= BITS_IN_POINTER
;
908 if (ctype
->type
== SYM_NODE
) {
909 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
910 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
911 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
912 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
914 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
915 symbol
->ctype
.as
= ctype
->ctype
.as
;
916 symbol
->ctype
.context
= ctype
->ctype
.context
;
917 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
918 ctype
= ctype
->ctype
.base_type
;
921 symbol
->ctype
.base_type
= ctype
;
923 expr
->ctype
= symbol
;
928 static struct symbol
*evaluate_dereference(struct expression
*expr
)
930 struct expression
*op
= expr
->unop
;
931 struct symbol
*ctype
= op
->ctype
, *sym
;
933 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
934 if (ctype
->type
== SYM_NODE
) {
935 ctype
= ctype
->ctype
.base_type
;
936 merge_type(sym
, ctype
);
938 sym
->ctype
= ctype
->ctype
;
939 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
940 warn(expr
->pos
, "cannot derefence this type");
944 ctype
= ctype
->ctype
.base_type
;
945 examine_symbol_type(ctype
);
947 warn(expr
->pos
, "undefined type");
951 sym
->bit_size
= ctype
->bit_size
;
952 sym
->array_size
= ctype
->array_size
;
954 /* Simplify: *&(expr) => (expr) */
955 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
963 static void simplify_preop(struct expression
*expr
)
965 struct expression
*op
= expr
->unop
;
966 unsigned long long v
, mask
;
968 if (op
->type
!= EXPR_VALUE
)
973 case '-': v
= -v
; break;
974 case '!': v
= !v
; break;
975 case '~': v
= ~v
; break;
978 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
979 mask
= mask
| (mask
-1);
980 expr
->value
= v
& mask
;
981 expr
->type
= EXPR_VALUE
;
985 * Unary post-ops: x++ and x--
987 static struct symbol
*evaluate_postop(struct expression
*expr
)
989 struct expression
*op
= expr
->unop
;
990 struct symbol
*ctype
= op
->ctype
;
992 if (!lvalue_expression(expr
->unop
)) {
993 warn(expr
->pos
, "need lvalue expression for ++/--");
1000 static struct symbol
*evaluate_preop(struct expression
*expr
)
1002 struct symbol
*ctype
= expr
->unop
->ctype
;
1007 *expr
= *expr
->unop
;
1011 return evaluate_dereference(expr
);
1014 return evaluate_addressof(expr
);
1016 case SPECIAL_INCREMENT
:
1017 case SPECIAL_DECREMENT
:
1019 * From a type evaluation standpoint the pre-ops are
1020 * the same as the postops
1022 return evaluate_postop(expr
);
1025 ctype
= &bool_ctype
;
1031 expr
->ctype
= ctype
;
1032 simplify_preop(expr
);
1036 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1038 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1039 struct ptr_list
*list
= head
;
1045 for (i
= 0; i
< list
->nr
; i
++) {
1046 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1048 if (sym
->ident
!= ident
)
1050 *offset
= sym
->offset
;
1053 struct symbol
*ctype
= sym
->ctype
.base_type
;
1057 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1059 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1062 *offset
+= sym
->offset
;
1066 } while ((list
= list
->next
) != head
);
1070 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1072 struct expression
*add
;
1077 /* Create a new add-expression */
1078 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1080 add
->ctype
= &ptr_ctype
;
1082 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1083 add
->right
->ctype
= &int_ctype
;
1084 add
->right
->value
= offset
;
1086 simplify_int_binop(add
, &ptr_ctype
);
1090 /* structure/union dereference */
1091 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1094 struct symbol
*ctype
, *member
, *sym
;
1095 struct expression
*deref
= expr
->deref
, *add
;
1096 struct ident
*ident
= expr
->member
;
1100 if (!evaluate_expression(deref
))
1103 warn(expr
->pos
, "bad member name");
1107 ctype
= deref
->ctype
;
1108 address_space
= ctype
->ctype
.as
;
1109 mod
= ctype
->ctype
.modifiers
;
1110 if (ctype
->type
== SYM_NODE
) {
1111 ctype
= ctype
->ctype
.base_type
;
1112 address_space
|= ctype
->ctype
.as
;
1113 mod
|= ctype
->ctype
.modifiers
;
1115 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1116 /* Arrays will degenerate into pointers for '->' */
1117 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1118 warn(expr
->pos
, "expected a pointer to a struct/union");
1121 mod
= ctype
->ctype
.modifiers
;
1122 address_space
= ctype
->ctype
.as
;
1123 ctype
= ctype
->ctype
.base_type
;
1124 if (ctype
->type
== SYM_NODE
) {
1125 mod
|= ctype
->ctype
.modifiers
;
1126 address_space
|= ctype
->ctype
.as
;
1127 ctype
= ctype
->ctype
.base_type
;
1130 if (!lvalue_expression(deref
)) {
1131 warn(deref
->pos
, "expected lvalue for member dereference");
1134 deref
= deref
->unop
;
1135 expr
->deref
= deref
;
1137 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1138 warn(expr
->pos
, "expected structure or union");
1142 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1144 warn(expr
->pos
, "no such struct/union member");
1148 add
= evaluate_offset(deref
, offset
);
1150 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1151 sym
->bit_size
= member
->bit_size
;
1152 sym
->array_size
= member
->array_size
;
1153 sym
->ctype
= member
->ctype
;
1154 sym
->ctype
.modifiers
= mod
;
1155 sym
->ctype
.as
= address_space
;
1156 ctype
= member
->ctype
.base_type
;
1157 if (ctype
->type
== SYM_BITFIELD
) {
1158 ctype
= ctype
->ctype
.base_type
;
1159 expr
->type
= EXPR_BITFIELD
;
1160 expr
->bitpos
= member
->bit_offset
;
1161 expr
->nrbits
= member
->fieldwidth
;
1162 expr
->address
= add
;
1164 expr
->type
= EXPR_PREOP
;
1173 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1177 if (expr
->cast_type
) {
1178 examine_symbol_type(expr
->cast_type
);
1179 size
= expr
->cast_type
->bit_size
;
1181 if (!evaluate_expression(expr
->cast_expression
))
1183 size
= expr
->cast_expression
->ctype
->bit_size
;
1186 warn(expr
->pos
, "cannot size expression");
1189 expr
->type
= EXPR_VALUE
;
1190 expr
->value
= size
>> 3;
1191 expr
->ctype
= size_t_ctype
;
1192 return size_t_ctype
;
1195 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1197 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1198 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1202 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1204 struct expression
*expr
;
1205 struct symbol_list
*argument_types
= fn
->arguments
;
1206 struct symbol
*argtype
;
1209 PREPARE_PTR_LIST(argument_types
, argtype
);
1210 FOR_EACH_PTR (head
, expr
) {
1211 struct expression
**p
= THIS_ADDRESS(expr
);
1212 struct symbol
*ctype
, *target
;
1213 ctype
= evaluate_expression(expr
);
1218 if (context_clash(f
, ctype
))
1219 warn(expr
->pos
, "argument %d used in wrong context", i
);
1221 ctype
= degenerate(expr
, ctype
, p
);
1224 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1225 target
= &int_ctype
;
1227 static char where
[30];
1228 examine_symbol_type(target
);
1229 sprintf(where
, "argument %d", i
);
1230 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1234 NEXT_PTR_LIST(argtype
);
1236 FINISH_PTR_LIST(argtype
);
1240 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1241 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1243 struct expression
*entry
;
1247 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1248 struct expression
**p
= THIS_ADDRESS(entry
);
1250 if (entry
->type
== EXPR_INDEX
) {
1251 current
= entry
->idx_to
;
1254 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1262 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1264 struct expression
*entry
;
1267 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1268 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1269 struct expression
**p
= THIS_ADDRESS(entry
);
1271 if (entry
->type
== EXPR_IDENTIFIER
) {
1272 struct ident
*ident
= entry
->expr_ident
;
1273 /* We special-case the "already right place" case */
1274 if (sym
&& sym
->ident
== ident
)
1276 RESET_PTR_LIST(sym
);
1279 warn(entry
->pos
, "unknown named initializer");
1282 if (sym
->ident
== ident
)
1290 warn(expr
->pos
, "too many initializers for struct/union");
1294 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1298 FINISH_PTR_LIST(sym
);
1304 * Initializers are kind of like assignments. Except
1305 * they can be a hell of a lot more complex.
1307 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1309 struct expression
*expr
= *ep
;
1312 * Simple non-structure/array initializers are the simple
1313 * case, and look (and parse) largely like assignments.
1315 if (expr
->type
!= EXPR_INITIALIZER
) {
1317 struct symbol
*rtype
= evaluate_expression(expr
);
1319 struct expression
*pos
;
1321 // FIXME! char array[] = "string" special case
1322 // should _not_ degenerate.
1323 rtype
= degenerate(expr
, rtype
, ep
);
1325 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1326 /* strings are special: char arrays */
1327 if (rtype
->type
== SYM_ARRAY
)
1328 size
= rtype
->array_size
;
1330 * Don't bother creating a position expression for
1331 * the simple initializer cases that don't need it.
1333 * We need a position if the initializer has a byte
1334 * offset, _or_ if we're initializing a bitfield.
1336 if (offset
|| ctype
->fieldwidth
) {
1337 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1338 pos
->init_offset
= offset
;
1339 pos
->init_sym
= ctype
;
1340 pos
->init_expr
= *ep
;
1341 pos
->ctype
= expr
->ctype
;
1348 expr
->ctype
= ctype
;
1349 if (ctype
->type
== SYM_NODE
)
1350 ctype
= ctype
->ctype
.base_type
;
1352 switch (ctype
->type
) {
1355 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1357 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1359 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1363 warn(expr
->pos
, "unexpected compound initializer");
1367 static struct symbol
*evaluate_cast(struct expression
*expr
)
1369 struct expression
*target
= expr
->cast_expression
;
1370 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1372 expr
->ctype
= ctype
;
1373 expr
->cast_type
= ctype
;
1376 * Special case: a cast can be followed by an
1377 * initializer, in which case we need to pass
1378 * the type value down to that initializer rather
1379 * than trying to evaluate it as an expression
1381 if (target
->type
== EXPR_INITIALIZER
) {
1382 evaluate_initializer(ctype
, &expr
->cast_expression
, 0);
1386 evaluate_expression(target
);
1388 /* Simplify normal integer casts.. */
1389 if (target
->type
== EXPR_VALUE
)
1390 cast_value(expr
, ctype
, target
, target
->ctype
);
1395 * Evaluate a call expression with a symbol. This
1396 * should expand inline functions, and evaluate
1399 static int evaluate_symbol_call(struct expression
*expr
)
1401 struct expression
*fn
= expr
->fn
;
1402 struct symbol
*ctype
= fn
->ctype
;
1404 if (fn
->type
!= EXPR_PREOP
)
1407 if (ctype
->evaluate
)
1408 return ctype
->evaluate(expr
);
1411 * FIXME!! We should really expand the inline functions.
1412 * For now we just mark them accessed so that they show
1413 * up on the list of used symbols.
1415 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1417 struct symbol
*curr
= current_fn
;
1418 current_fn
= ctype
->ctype
.base_type
;
1419 ret
= inline_function(expr
, ctype
);
1427 static struct symbol
*evaluate_call(struct expression
*expr
)
1430 struct symbol
*ctype
, *sym
;
1431 struct expression
*fn
= expr
->fn
;
1432 struct expression_list
*arglist
= expr
->args
;
1434 if (!evaluate_expression(fn
))
1436 sym
= ctype
= fn
->ctype
;
1437 if (ctype
->type
== SYM_NODE
)
1438 ctype
= ctype
->ctype
.base_type
;
1439 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1440 ctype
= ctype
->ctype
.base_type
;
1441 if (!evaluate_arguments(sym
, ctype
, arglist
))
1443 if (sym
->type
== SYM_NODE
) {
1444 if (evaluate_symbol_call(expr
))
1447 if (ctype
->type
!= SYM_FN
) {
1448 warn(expr
->pos
, "not a function");
1451 args
= expression_list_size(expr
->args
);
1452 fnargs
= symbol_list_size(ctype
->arguments
);
1454 warn(expr
->pos
, "not enough arguments for function");
1455 if (args
> fnargs
&& !ctype
->variadic
)
1456 warn(expr
->pos
, "too many arguments for function");
1457 expr
->ctype
= ctype
->ctype
.base_type
;
1461 struct symbol
*evaluate_expression(struct expression
*expr
)
1468 switch (expr
->type
) {
1470 warn(expr
->pos
, "value expression without a type");
1473 return evaluate_string(expr
);
1475 return evaluate_symbol_expression(expr
);
1477 if (!evaluate_expression(expr
->left
))
1479 if (!evaluate_expression(expr
->right
))
1481 return evaluate_binop(expr
);
1483 return evaluate_logical(expr
);
1485 if (!evaluate_expression(expr
->left
))
1487 if (!evaluate_expression(expr
->right
))
1489 return evaluate_comma(expr
);
1491 if (!evaluate_expression(expr
->left
))
1493 if (!evaluate_expression(expr
->right
))
1495 return evaluate_compare(expr
);
1496 case EXPR_ASSIGNMENT
:
1497 if (!evaluate_expression(expr
->left
))
1499 if (!evaluate_expression(expr
->right
))
1501 return evaluate_assignment(expr
);
1503 if (!evaluate_expression(expr
->unop
))
1505 return evaluate_preop(expr
);
1507 if (!evaluate_expression(expr
->unop
))
1509 return evaluate_postop(expr
);
1511 return evaluate_cast(expr
);
1513 return evaluate_sizeof(expr
);
1515 return evaluate_member_dereference(expr
);
1517 return evaluate_call(expr
);
1519 warn(expr
->pos
, "bitfield generated by parser");
1521 case EXPR_CONDITIONAL
:
1522 if (!evaluate_expression(expr
->conditional
))
1524 if (!evaluate_expression(expr
->cond_false
))
1526 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1528 return evaluate_conditional(expr
);
1529 case EXPR_STATEMENT
:
1530 expr
->ctype
= evaluate_statement(expr
->statement
);
1534 expr
->ctype
= &ptr_ctype
;
1537 /* These can not exist as stand-alone expressions */
1538 case EXPR_INITIALIZER
:
1539 case EXPR_IDENTIFIER
:
1542 warn(expr
->pos
, "internal front-end error: initializer in expression");
1548 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1550 struct symbol
**last
= _last
;
1551 struct symbol
*type
= evaluate_statement(stmt
);
1553 if (flags
& ITERATE_LAST
)
1557 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1559 evaluate_symbol(sym
);
1562 void check_duplicates(struct symbol
*sym
)
1564 struct symbol
*next
= sym
;
1566 while ((next
= next
->same_symbol
) != NULL
) {
1567 const char *typediff
;
1568 evaluate_symbol(next
);
1569 typediff
= type_difference(sym
, next
, 0, 0);
1571 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1572 show_ident(sym
->ident
),
1573 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1579 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1581 struct symbol
*base_type
;
1583 sym
= examine_symbol_type(sym
);
1584 base_type
= sym
->ctype
.base_type
;
1588 /* Evaluate the initializers */
1589 if (sym
->initializer
) {
1590 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1591 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1592 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1593 base_type
->array_size
= count
;
1594 base_type
->bit_size
= bit_size
;
1595 sym
->array_size
= count
;
1596 sym
->bit_size
= bit_size
;
1600 /* And finally, evaluate the body of the symbol too */
1601 if (base_type
->type
== SYM_FN
) {
1602 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1603 if (base_type
->stmt
) {
1604 current_fn
= base_type
;
1605 current_contextmask
= sym
->ctype
.contextmask
;
1606 current_context
= sym
->ctype
.context
;
1607 evaluate_statement(base_type
->stmt
);
1614 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1616 struct expression
*expr
= stmt
->expression
;
1617 struct symbol
*ctype
, *fntype
;
1619 fntype
= current_fn
->ctype
.base_type
;
1620 if (fntype
== &void_ctype
) {
1622 warn(expr
->pos
, "return expression in void function");
1627 warn(stmt
->pos
, "return with no return value");
1630 ctype
= evaluate_expression(expr
);
1633 ctype
= degenerate(expr
, ctype
, &expr
);
1634 expr
->ctype
= ctype
;
1635 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1636 stmt
->expression
= expr
;
1640 static void evaluate_if_statement(struct statement
*stmt
)
1642 struct expression
*expr
= stmt
->if_conditional
;
1643 struct symbol
*ctype
;
1647 if (expr
->type
== EXPR_ASSIGNMENT
)
1648 warn(expr
->pos
, "assignment expression in conditional");
1650 ctype
= evaluate_expression(expr
);
1654 /* Simplify constant conditionals without even evaluating the false side */
1655 if (expr
->type
== EXPR_VALUE
) {
1656 struct statement
*simple
;
1657 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1661 stmt
->type
= STMT_NONE
;
1664 evaluate_statement(simple
);
1668 evaluate_statement(stmt
->if_true
);
1669 evaluate_statement(stmt
->if_false
);
1672 struct symbol
*evaluate_statement(struct statement
*stmt
)
1677 switch (stmt
->type
) {
1679 return evaluate_return_expression(stmt
);
1681 case STMT_EXPRESSION
:
1682 return evaluate_expression(stmt
->expression
);
1684 case STMT_COMPOUND
: {
1685 struct symbol
*type
= NULL
;
1686 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1687 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1691 evaluate_if_statement(stmt
);
1694 evaluate_expression(stmt
->iterator_pre_condition
);
1695 evaluate_expression(stmt
->iterator_post_condition
);
1696 evaluate_statement(stmt
->iterator_pre_statement
);
1697 evaluate_statement(stmt
->iterator_statement
);
1698 evaluate_statement(stmt
->iterator_post_statement
);
1701 evaluate_expression(stmt
->switch_expression
);
1702 evaluate_statement(stmt
->switch_statement
);
1705 evaluate_expression(stmt
->case_expression
);
1706 evaluate_expression(stmt
->case_to
);
1707 evaluate_statement(stmt
->case_statement
);
1710 evaluate_statement(stmt
->label_statement
);
1713 evaluate_expression(stmt
->goto_expression
);
1718 /* FIXME! Do the asm parameter evaluation! */
1724 long long get_expression_value(struct expression
*expr
)
1726 long long value
, mask
;
1727 struct symbol
*ctype
;
1729 ctype
= evaluate_expression(expr
);
1730 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1731 warn(expr
->pos
, "bad constant expression");
1735 value
= expr
->value
;
1736 mask
= 1ULL << (ctype
->bit_size
-1);
1739 while (ctype
->type
!= SYM_BASETYPE
)
1740 ctype
= ctype
->ctype
.base_type
;
1741 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1742 value
= value
| mask
| ~(mask
-1);