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 /* enum's can be turned into plain values */
61 if (base_type
->type
!= SYM_ENUM
) {
62 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
64 addr
->symbol_name
= expr
->symbol_name
;
65 addr
->ctype
= &ptr_ctype
;
66 expr
->type
= EXPR_PREOP
;
71 expr
->type
= EXPR_VALUE
;
72 expr
->value
= sym
->value
;
76 static struct symbol
*evaluate_string(struct expression
*expr
)
78 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
79 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
80 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
81 int length
= expr
->string
->length
;
83 sym
->array_size
= length
;
84 sym
->bit_size
= BITS_IN_CHAR
* length
;
85 sym
->ctype
.alignment
= 1;
86 sym
->ctype
.modifiers
= MOD_STATIC
;
87 sym
->ctype
.base_type
= array
;
89 array
->array_size
= length
;
90 array
->bit_size
= BITS_IN_CHAR
* length
;
91 array
->ctype
.alignment
= 1;
92 array
->ctype
.modifiers
= MOD_STATIC
;
93 array
->ctype
.base_type
= &char_ctype
;
96 addr
->ctype
= &ptr_ctype
;
98 expr
->type
= EXPR_PREOP
;
105 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
107 unsigned long lmod
, rmod
, mod
;
112 if (left
->bit_size
> right
->bit_size
)
115 if (right
->bit_size
> left
->bit_size
)
118 /* Same size integers - promote to unsigned, promote to long */
119 lmod
= left
->ctype
.modifiers
;
120 rmod
= right
->ctype
.modifiers
;
126 return ctype_integer(mod
);
129 static struct symbol
* cast_value(struct expression
*expr
, struct symbol
*newtype
,
130 struct expression
*old
, struct symbol
*oldtype
)
132 int old_size
= oldtype
->bit_size
;
133 int new_size
= newtype
->bit_size
;
134 long long value
, mask
, ormask
, andmask
;
137 // FIXME! We don't handle FP casts of constant values yet
138 if (newtype
->ctype
.base_type
== &fp_type
)
140 if (oldtype
->ctype
.base_type
== &fp_type
)
143 // For pointers and integers, we can just move the value around
144 expr
->type
= EXPR_VALUE
;
145 if (old_size
== new_size
) {
146 expr
->value
= old
->value
;
150 // expand it to the full "long long" value
151 is_signed
= !(oldtype
->ctype
.modifiers
& MOD_UNSIGNED
);
152 mask
= 1ULL << (old_size
-1);
156 andmask
= mask
| (mask
-1);
160 value
= (value
& andmask
) | ormask
;
162 // Truncate it to the new size
163 mask
= 1ULL << (new_size
-1);
164 mask
= mask
| (mask
-1);
165 expr
->value
= value
& mask
;
169 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
171 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
173 expr
->cast_type
= type
;
174 expr
->cast_expression
= old
;
175 if (old
->type
== EXPR_VALUE
)
176 cast_value(expr
, type
, old
, old
->ctype
);
180 static int is_ptr_type(struct symbol
*type
)
182 if (type
->type
== SYM_NODE
)
183 type
= type
->ctype
.base_type
;
184 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
187 static int is_int_type(struct symbol
*type
)
189 if (type
->type
== SYM_NODE
)
190 type
= type
->ctype
.base_type
;
191 return type
->ctype
.base_type
== &int_type
;
194 static struct symbol
*bad_expr_type(struct expression
*expr
)
196 warn(expr
->pos
, "incompatible types for operation");
200 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
202 struct expression
*left
= *lp
, *right
= *rp
;
203 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
205 if (ltype
->type
== SYM_NODE
)
206 ltype
= ltype
->ctype
.base_type
;
207 if (rtype
->type
== SYM_NODE
)
208 rtype
= rtype
->ctype
.base_type
;
209 /* Integer promotion? */
210 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
212 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
214 if (is_int_type(ltype
) && is_int_type(rtype
)) {
215 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
217 /* Don't bother promoting same-size entities, it only adds clutter */
218 if (ltype
->bit_size
!= ctype
->bit_size
)
219 *lp
= cast_to(left
, ctype
);
220 if (rtype
->bit_size
!= ctype
->bit_size
)
221 *rp
= cast_to(right
, ctype
);
227 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
229 if (count
>= ctype
->bit_size
) {
230 warn(expr
->pos
, "shift too big for type");
231 count
&= ctype
->bit_size
-1;
237 * CAREFUL! We need to get the size and sign of the
240 static void simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
242 struct expression
*left
= expr
->left
, *right
= expr
->right
;
243 unsigned long long v
, l
, r
, mask
;
244 signed long long s
, sl
, sr
;
245 int is_signed
, shift
;
247 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
249 l
= left
->value
; r
= right
->value
;
250 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
251 mask
= 1ULL << (ctype
->bit_size
-1);
253 if (is_signed
&& (sl
& mask
))
255 if (is_signed
&& (sr
& mask
))
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
= v
; break;
263 case '^': v
= l
^ r
; s
= v
; break;
264 case '*': v
= l
* r
; s
= sl
* sr
; break;
265 case '/': if (!r
) return; v
= l
/ r
; s
= sl
/ sr
; break;
266 case '%': if (!r
) return; v
= l
% r
; s
= sl
% sr
; break;
267 case SPECIAL_LEFTSHIFT
: shift
= check_shift_count(expr
, ctype
, r
); v
= l
<< shift
; s
= v
; break;
268 case SPECIAL_RIGHTSHIFT
:shift
= check_shift_count(expr
, ctype
, r
); v
= l
>> shift
; s
= sl
>> shift
; break;
269 case '<': v
= l
< r
; s
= sl
< sr
; break;
270 case '>': v
= l
> r
; s
= sl
> sr
; break;
271 case SPECIAL_LTE
: v
= l
<= r
; s
= sl
<= sr
; break;
272 case SPECIAL_GTE
: v
= l
>= r
; s
= sl
>= sr
; break;
273 case SPECIAL_EQUAL
: v
= l
== r
; s
= v
; break;
274 case SPECIAL_NOTEQUAL
: v
= l
!= r
; s
= v
; break;
279 mask
= mask
| (mask
-1);
280 expr
->value
= v
& mask
;
281 expr
->type
= EXPR_VALUE
;
284 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
286 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
289 simplify_int_binop(expr
, ctype
);
292 return bad_expr_type(expr
);
295 static inline int lvalue_expression(struct expression
*expr
)
297 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
300 /* Arrays degenerate into pointers on pointer arithmetic */
301 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
303 struct symbol
*base
= ctype
;
305 if (ctype
->type
== SYM_NODE
)
306 base
= ctype
->ctype
.base_type
;
307 if (base
->type
== SYM_ARRAY
|| base
->type
== SYM_FN
) {
308 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
309 struct expression
*ptr
;
311 merge_type(sym
, ctype
);
312 if (base
->type
== SYM_FN
)
314 merge_type(sym
, base
);
315 sym
->bit_size
= BITS_IN_POINTER
;
322 * This all really assumes that we got the degenerate
323 * array as an lvalue (ie a dereference). If that
324 * is not the case, then holler - because we've screwed
327 if (!lvalue_expression(ptr
))
328 warn(ptr
->pos
, "internal error: strange degenerate array case");
334 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
336 struct symbol
*ctype
;
337 struct symbol
*ptr_type
= ptr
->ctype
;
338 struct symbol
*i_type
= i
->ctype
;
341 if (i_type
->type
== SYM_NODE
)
342 i_type
= i_type
->ctype
.base_type
;
343 if (ptr_type
->type
== SYM_NODE
)
344 ptr_type
= ptr_type
->ctype
.base_type
;
346 if (i_type
->type
== SYM_ENUM
)
348 if (!is_int_type(i_type
))
349 return bad_expr_type(expr
);
352 examine_symbol_type(ctype
);
354 ctype
= degenerate(expr
, ctype
, &ptr
);
355 bit_size
= ctype
->bit_size
;
357 /* Special case: adding zero commonly happens as a result of 'array[0]' */
358 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
360 } else if (bit_size
> BITS_IN_CHAR
) {
361 struct expression
*add
= expr
;
362 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
363 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
365 val
->ctype
= size_t_ctype
;
366 val
->value
= bit_size
>> 3;
369 mul
->ctype
= size_t_ctype
;
372 simplify_int_binop(mul
, size_t_ctype
);
374 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
377 simplify_int_binop(add
, ctype
);
384 static struct symbol
*evaluate_add(struct expression
*expr
)
386 struct expression
*left
= expr
->left
, *right
= expr
->right
;
387 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
389 if (is_ptr_type(ltype
))
390 return evaluate_ptr_add(expr
, left
, right
);
392 if (is_ptr_type(rtype
))
393 return evaluate_ptr_add(expr
, right
, left
);
395 // FIXME! FP promotion
396 return evaluate_int_binop(expr
);
399 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
400 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
402 static const char * type_difference(struct symbol
*target
, struct symbol
*source
,
403 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
406 unsigned long mod1
, mod2
, diff
;
407 unsigned long as1
, as2
;
409 if (target
== source
)
411 if (!target
|| !source
)
412 return "different types";
414 * Peel of per-node information.
415 * FIXME! Check alignment, address space, and context too here!
417 if (target
->type
== SYM_NODE
)
418 target
= target
->ctype
.base_type
;
419 if (source
->type
== SYM_NODE
)
420 source
= source
->ctype
.base_type
;
421 mod1
= target
->ctype
.modifiers
;
422 as1
= target
->ctype
.as
;
423 mod2
= source
->ctype
.modifiers
;
424 as2
= source
->ctype
.as
;
426 if (target
->type
!= source
->type
) {
427 int type1
= target
->type
;
428 int type2
= source
->type
;
430 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
431 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
432 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
434 return "different base types";
437 /* Must be same address space to be comparable */
439 return "different address spaces";
441 /* Ignore differences in storage types, sign, or addressability */
442 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
444 mod1
&= diff
& ~target_mod_ignore
;
445 mod2
&= diff
& ~source_mod_ignore
;
447 if ((mod1
| mod2
) & MOD_SIZE
)
448 return "different type sizes";
449 return "different modifiers";
453 if (target
->type
== SYM_FN
) {
455 struct symbol
*arg1
, *arg2
;
456 if (target
->variadic
!= source
->variadic
)
457 return "incompatible variadic arguments";
458 PREPARE_PTR_LIST(target
->arguments
, arg1
);
459 PREPARE_PTR_LIST(source
->arguments
, arg2
);
462 if (type_difference(arg1
, arg2
, 0, 0)) {
463 static char argdiff
[30];
464 sprintf(argdiff
, "incompatible argument %d", i
);
473 FINISH_PTR_LIST(arg2
);
474 FINISH_PTR_LIST(arg1
);
477 target
= target
->ctype
.base_type
;
478 source
= source
->ctype
.base_type
;
483 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
485 /* NULL expression? Just return the type of the "other side" */
486 if (r
->type
== EXPR_VALUE
&& !r
->value
)
488 if (l
->type
== EXPR_VALUE
&& !l
->value
)
494 * Ignore differences in "volatile" and "const"ness when
495 * subtracting pointers
497 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
499 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
501 const char *typediff
;
502 struct symbol
*ctype
;
503 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
506 * If it is an integer subtract: the ptr add case will do the
509 if (!is_ptr_type(rtype
))
510 return evaluate_ptr_add(expr
, l
, r
);
513 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
515 ctype
= common_ptr_type(l
, r
);
517 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
521 examine_symbol_type(ctype
);
523 /* Figure out the base type we point to */
524 if (ctype
->type
== SYM_NODE
)
525 ctype
= ctype
->ctype
.base_type
;
526 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
527 warn(expr
->pos
, "subtraction of functions? Share your drugs");
530 ctype
= ctype
->ctype
.base_type
;
532 expr
->ctype
= ssize_t_ctype
;
533 if (ctype
->bit_size
> BITS_IN_CHAR
) {
534 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
535 struct expression
*div
= expr
;
536 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
538 val
->ctype
= size_t_ctype
;
539 val
->value
= ctype
->bit_size
>> 3;
542 sub
->ctype
= ssize_t_ctype
;
551 return ssize_t_ctype
;
554 static struct symbol
*evaluate_sub(struct expression
*expr
)
556 struct expression
*left
= expr
->left
, *right
= expr
->right
;
557 struct symbol
*ltype
= left
->ctype
;
559 if (is_ptr_type(ltype
))
560 return evaluate_ptr_sub(expr
, left
, right
);
562 // FIXME! FP promotion
563 return evaluate_int_binop(expr
);
566 static struct symbol
*evaluate_logical(struct expression
*expr
)
568 struct expression
*left
= expr
->left
;
569 struct expression
*right
;
571 if (!evaluate_expression(left
))
574 /* Do immediate short-circuiting ... */
575 if (left
->type
== EXPR_VALUE
) {
576 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
578 expr
->type
= EXPR_VALUE
;
580 expr
->ctype
= &bool_ctype
;
585 expr
->type
= EXPR_VALUE
;
587 expr
->ctype
= &bool_ctype
;
594 if (!evaluate_expression(right
))
596 expr
->ctype
= &bool_ctype
;
597 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
599 * We know the left value doesn't matter, since
600 * otherwise we would have short-circuited it..
602 expr
->type
= EXPR_VALUE
;
603 expr
->value
= right
->value
;
609 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
611 // FIXME! Floating-point promotion!
612 return evaluate_int_binop(expr
);
615 static struct symbol
*evaluate_binop(struct expression
*expr
)
618 // addition can take ptr+int, fp and int
620 return evaluate_add(expr
);
622 // subtraction can take ptr-ptr, fp and int
624 return evaluate_sub(expr
);
626 // Arithmetic operations can take fp and int
627 case '*': case '/': case '%':
628 return evaluate_arithmetic(expr
);
630 // The rest are integer operations (bitops)
631 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
634 return evaluate_int_binop(expr
);
638 static struct symbol
*evaluate_comma(struct expression
*expr
)
640 expr
->ctype
= expr
->right
->ctype
;
644 static struct symbol
*evaluate_compare(struct expression
*expr
)
646 struct expression
*left
= expr
->left
, *right
= expr
->right
;
647 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
648 struct symbol
*ctype
;
651 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
652 expr
->ctype
= &bool_ctype
;
653 // FIXME! Check the types for compatibility
657 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
659 simplify_int_binop(expr
, ctype
);
660 expr
->ctype
= &bool_ctype
;
664 return bad_expr_type(expr
);
667 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
669 /* Integer promotion? */
670 if (ltype
->type
== SYM_NODE
)
671 ltype
= ltype
->ctype
.base_type
;
672 if (rtype
->type
== SYM_NODE
)
673 rtype
= rtype
->ctype
.base_type
;
674 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
676 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
678 return (is_int_type(ltype
) && is_int_type(rtype
));
681 static int is_void_ptr(struct expression
*expr
)
683 return (expr
->type
== EXPR_VALUE
&&
688 * FIXME!! This shoul ddo casts, array degeneration etc..
690 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
692 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
694 if (ltype
->type
== SYM_PTR
) {
695 if (is_void_ptr(right
))
699 if (rtype
->type
== SYM_PTR
) {
700 if (is_void_ptr(left
))
706 static struct symbol
*do_degenerate(struct expression
**ep
)
708 struct expression
*expr
= *ep
;
709 return degenerate(expr
, expr
->ctype
, ep
);
712 static struct symbol
* evaluate_conditional(struct expression
*expr
)
714 struct expression
*cond
, *true, *false;
715 struct symbol
*ctype
, *ltype
, *rtype
;
716 const char * typediff
;
718 ctype
= do_degenerate(&expr
->conditional
);
719 cond
= expr
->conditional
;
723 if (expr
->cond_true
) {
724 ltype
= do_degenerate(&expr
->cond_true
);
725 true = expr
->cond_true
;
728 rtype
= do_degenerate(&expr
->cond_false
);
729 false = expr
->cond_false
;
732 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
734 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
736 ctype
= compatible_ptr_type(true, expr
->cond_false
);
738 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
744 /* Simplify conditional expression.. */
745 if (cond
->type
== EXPR_VALUE
) {
754 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
755 struct expression
**rp
, struct symbol
*source
, const char *where
)
757 const char *typediff
;
761 /* It's ok if the target is more volatile or const than the source */
762 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
766 if (compatible_integer_types(target
, source
)) {
767 if (target
->bit_size
!= source
->bit_size
)
768 *rp
= cast_to(*rp
, target
);
772 /* Pointer destination? */
774 target_as
= t
->ctype
.as
;
775 if (t
->type
== SYM_NODE
) {
776 t
= t
->ctype
.base_type
;
777 target_as
|= t
->ctype
.as
;
779 if (t
->type
== SYM_PTR
) {
780 struct expression
*right
= *rp
;
781 struct symbol
*s
= source
;
784 // NULL pointer is always ok
785 if (right
->type
== EXPR_VALUE
&& !right
->value
)
788 /* "void *" matches anything as long as the address space is ok */
789 source_as
= s
->ctype
.as
;
790 if (s
->type
== SYM_NODE
) {
791 s
= s
->ctype
.base_type
;
792 source_as
|= s
->ctype
.as
;
794 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
795 s
= s
->ctype
.base_type
;
796 t
= t
->ctype
.base_type
;
797 if (s
== &void_ctype
|| t
== &void_ctype
)
803 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
804 warn(expr
->pos
, " expected %s", show_typename(target
));
805 warn(expr
->pos
, " got %s", show_typename(source
));
810 * FIXME!! This is wrong from a double evaluation standpoint. We can't
811 * just expand the expression twice, that would make any side effects
814 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
817 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
818 static const int op_trans
[] = {
819 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
820 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
821 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
822 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
823 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
824 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
825 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
826 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
827 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '&',
828 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
831 subexpr
->left
= left
;
832 subexpr
->right
= right
;
833 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
835 expr
->right
= subexpr
;
836 return evaluate_binop(subexpr
);
839 static struct symbol
*evaluate_assignment(struct expression
*expr
)
841 struct expression
*left
= expr
->left
, *right
= expr
->right
;
842 struct symbol
*ltype
, *rtype
;
845 rtype
= right
->ctype
;
846 if (expr
->op
!= '=') {
847 rtype
= evaluate_binop_assignment(expr
, left
, right
);
853 if (!lvalue_expression(left
)) {
854 warn(expr
->pos
, "not an lvalue");
858 rtype
= degenerate(right
, rtype
, &expr
->right
);
860 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
863 expr
->ctype
= expr
->left
->ctype
;
867 static struct symbol
*evaluate_addressof(struct expression
*expr
)
869 struct symbol
*ctype
, *symbol
;
870 struct expression
*op
= expr
->unop
;
872 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
873 warn(expr
->pos
, "not addressable");
877 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
878 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
879 symbol
->bit_size
= BITS_IN_POINTER
;
882 if (ctype
->type
== SYM_NODE
) {
883 ctype
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
884 if (ctype
->ctype
.modifiers
& MOD_REGISTER
) {
885 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(ctype
->ident
));
886 ctype
->ctype
.modifiers
&= ~MOD_REGISTER
;
888 symbol
->ctype
.modifiers
= ctype
->ctype
.modifiers
;
889 symbol
->ctype
.as
= ctype
->ctype
.as
;
890 symbol
->ctype
.context
= ctype
->ctype
.context
;
891 symbol
->ctype
.contextmask
= ctype
->ctype
.contextmask
;
892 ctype
= ctype
->ctype
.base_type
;
895 symbol
->ctype
.base_type
= ctype
;
897 expr
->ctype
= symbol
;
902 static struct symbol
*evaluate_dereference(struct expression
*expr
)
904 struct expression
*op
= expr
->unop
;
905 struct symbol
*ctype
= op
->ctype
, *sym
;
907 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
908 if (ctype
->type
== SYM_NODE
) {
909 ctype
= ctype
->ctype
.base_type
;
910 merge_type(sym
, ctype
);
912 sym
->ctype
= ctype
->ctype
;
913 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
914 warn(expr
->pos
, "cannot derefence this type");
918 ctype
= ctype
->ctype
.base_type
;
919 examine_symbol_type(ctype
);
921 warn(expr
->pos
, "undefined type");
925 sym
->bit_size
= ctype
->bit_size
;
926 sym
->array_size
= ctype
->array_size
;
928 /* Simplify: *&(expr) => (expr) */
929 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
937 static void simplify_preop(struct expression
*expr
)
939 struct expression
*op
= expr
->unop
;
940 unsigned long long v
, mask
;
942 if (op
->type
!= EXPR_VALUE
)
947 case '-': v
= -v
; break;
948 case '!': v
= !v
; break;
949 case '~': v
= ~v
; break;
952 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
953 mask
= mask
| (mask
-1);
954 expr
->value
= v
& mask
;
955 expr
->type
= EXPR_VALUE
;
959 * Unary post-ops: x++ and x--
961 static struct symbol
*evaluate_postop(struct expression
*expr
)
963 struct expression
*op
= expr
->unop
;
964 struct symbol
*ctype
= op
->ctype
;
966 if (!lvalue_expression(expr
->unop
)) {
967 warn(expr
->pos
, "need lvalue expression for ++/--");
974 static struct symbol
*evaluate_preop(struct expression
*expr
)
976 struct symbol
*ctype
= expr
->unop
->ctype
;
985 return evaluate_dereference(expr
);
988 return evaluate_addressof(expr
);
990 case SPECIAL_INCREMENT
:
991 case SPECIAL_DECREMENT
:
993 * From a type evaluation standpoint the pre-ops are
994 * the same as the postops
996 return evaluate_postop(expr
);
1005 expr
->ctype
= ctype
;
1006 simplify_preop(expr
);
1010 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1012 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1013 struct ptr_list
*list
= head
;
1019 for (i
= 0; i
< list
->nr
; i
++) {
1020 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1022 if (sym
->ident
!= ident
)
1024 *offset
= sym
->offset
;
1027 struct symbol
*ctype
= sym
->ctype
.base_type
;
1031 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1033 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1036 *offset
+= sym
->offset
;
1040 } while ((list
= list
->next
) != head
);
1044 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1046 struct expression
*add
;
1051 /* Create a new add-expression */
1052 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1054 add
->ctype
= &ptr_ctype
;
1056 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1057 add
->right
->ctype
= &int_ctype
;
1058 add
->right
->value
= offset
;
1060 simplify_int_binop(add
, &ptr_ctype
);
1064 /* structure/union dereference */
1065 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1068 struct symbol
*ctype
, *member
, *sym
;
1069 struct expression
*deref
= expr
->deref
, *add
;
1070 struct ident
*ident
= expr
->member
;
1074 if (!evaluate_expression(deref
))
1077 warn(expr
->pos
, "bad member name");
1081 ctype
= deref
->ctype
;
1082 address_space
= ctype
->ctype
.as
;
1083 mod
= ctype
->ctype
.modifiers
;
1084 if (ctype
->type
== SYM_NODE
) {
1085 ctype
= ctype
->ctype
.base_type
;
1086 address_space
|= ctype
->ctype
.as
;
1087 mod
|= ctype
->ctype
.modifiers
;
1089 if (expr
->op
== SPECIAL_DEREFERENCE
) {
1090 /* Arrays will degenerate into pointers for '->' */
1091 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
1092 warn(expr
->pos
, "expected a pointer to a struct/union");
1095 mod
= ctype
->ctype
.modifiers
;
1096 address_space
= ctype
->ctype
.as
;
1097 ctype
= ctype
->ctype
.base_type
;
1098 if (ctype
->type
== SYM_NODE
) {
1099 mod
|= ctype
->ctype
.modifiers
;
1100 address_space
|= ctype
->ctype
.as
;
1101 ctype
= ctype
->ctype
.base_type
;
1104 if (!lvalue_expression(deref
)) {
1105 warn(deref
->pos
, "expected lvalue for member dereference");
1108 deref
= deref
->unop
;
1109 expr
->deref
= deref
;
1111 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1112 warn(expr
->pos
, "expected structure or union");
1116 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1118 warn(expr
->pos
, "no such struct/union member");
1122 add
= evaluate_offset(deref
, offset
);
1124 sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1125 sym
->bit_size
= member
->bit_size
;
1126 sym
->array_size
= member
->array_size
;
1127 sym
->ctype
= member
->ctype
;
1128 sym
->ctype
.modifiers
= mod
;
1129 sym
->ctype
.as
= address_space
;
1130 ctype
= member
->ctype
.base_type
;
1131 if (ctype
->type
== SYM_BITFIELD
) {
1132 ctype
= ctype
->ctype
.base_type
;
1133 expr
->type
= EXPR_BITFIELD
;
1134 expr
->bitpos
= member
->bit_offset
;
1135 expr
->nrbits
= member
->fieldwidth
;
1136 expr
->address
= add
;
1138 expr
->type
= EXPR_PREOP
;
1147 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1151 if (expr
->cast_type
) {
1152 examine_symbol_type(expr
->cast_type
);
1153 size
= expr
->cast_type
->bit_size
;
1155 if (!evaluate_expression(expr
->cast_expression
))
1157 size
= expr
->cast_expression
->ctype
->bit_size
;
1160 warn(expr
->pos
, "cannot size expression");
1163 expr
->type
= EXPR_VALUE
;
1164 expr
->value
= size
>> 3;
1165 expr
->ctype
= size_t_ctype
;
1166 return size_t_ctype
;
1169 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
1171 struct expression
*expr
;
1172 struct symbol_list
*argument_types
= fn
->arguments
;
1173 struct symbol
*argtype
;
1176 PREPARE_PTR_LIST(argument_types
, argtype
);
1177 FOR_EACH_PTR (head
, expr
) {
1178 struct expression
**p
= THIS_ADDRESS(expr
);
1179 struct symbol
*ctype
, *target
;
1180 ctype
= evaluate_expression(expr
);
1185 ctype
= degenerate(expr
, ctype
, p
);
1188 if (!target
&& ctype
->bit_size
< BITS_IN_INT
)
1189 target
= &int_ctype
;
1191 static char where
[30];
1192 examine_symbol_type(target
);
1193 sprintf(where
, "argument %d", i
);
1194 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1198 NEXT_PTR_LIST(argtype
);
1200 FINISH_PTR_LIST(argtype
);
1204 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1205 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1207 struct expression
*entry
;
1211 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1212 struct expression
**p
= THIS_ADDRESS(entry
);
1214 if (entry
->type
== EXPR_INDEX
) {
1215 current
= entry
->idx_to
;
1218 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1226 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1228 struct expression
*entry
;
1231 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1232 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1233 struct expression
**p
= THIS_ADDRESS(entry
);
1235 if (entry
->type
== EXPR_IDENTIFIER
) {
1236 struct ident
*ident
= entry
->expr_ident
;
1237 /* We special-case the "already right place" case */
1238 if (sym
&& sym
->ident
== ident
)
1240 RESET_PTR_LIST(sym
);
1243 warn(entry
->pos
, "unknown named initializer");
1246 if (sym
->ident
== ident
)
1254 warn(expr
->pos
, "too many initializers for struct/union");
1258 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1262 FINISH_PTR_LIST(sym
);
1268 * Initializers are kind of like assignments. Except
1269 * they can be a hell of a lot more complex.
1271 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1273 struct expression
*expr
= *ep
;
1276 * Simple non-structure/array initializers are the simple
1277 * case, and look (and parse) largely like assignments.
1279 if (expr
->type
!= EXPR_INITIALIZER
) {
1281 struct symbol
*rtype
= evaluate_expression(expr
);
1283 struct expression
*pos
;
1285 // FIXME! char array[] = "string" special case
1286 // should _not_ degenerate.
1287 rtype
= degenerate(expr
, rtype
, ep
);
1289 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1290 /* strings are special: char arrays */
1291 if (rtype
->type
== SYM_ARRAY
)
1292 size
= rtype
->array_size
;
1294 * Don't bother creating a position expression for
1295 * the simple initializer cases that don't need it.
1297 * We need a position if the initializer has a byte
1298 * offset, _or_ if we're initializing a bitfield.
1300 if (offset
|| ctype
->fieldwidth
) {
1301 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1302 pos
->init_offset
= offset
;
1303 pos
->init_sym
= ctype
;
1304 pos
->init_expr
= *ep
;
1305 pos
->ctype
= expr
->ctype
;
1312 expr
->ctype
= ctype
;
1313 if (ctype
->type
== SYM_NODE
)
1314 ctype
= ctype
->ctype
.base_type
;
1316 switch (ctype
->type
) {
1319 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1321 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1323 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1327 warn(expr
->pos
, "unexpected compound initializer");
1331 static struct symbol
*evaluate_cast(struct expression
*expr
)
1333 struct expression
*target
= expr
->cast_expression
;
1334 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1336 expr
->ctype
= ctype
;
1337 expr
->cast_type
= ctype
;
1340 * Special case: a cast can be followed by an
1341 * initializer, in which case we need to pass
1342 * the type value down to that initializer rather
1343 * than trying to evaluate it as an expression
1345 if (target
->type
== EXPR_INITIALIZER
) {
1346 evaluate_initializer(ctype
, &expr
->cast_expression
, 0);
1350 evaluate_expression(target
);
1352 /* Simplify normal integer casts.. */
1353 if (target
->type
== EXPR_VALUE
)
1354 cast_value(expr
, ctype
, target
, target
->ctype
);
1358 static struct symbol
*evaluate_call(struct expression
*expr
)
1361 struct symbol
*ctype
;
1362 struct expression
*fn
= expr
->fn
;
1363 struct expression_list
*arglist
= expr
->args
;
1365 if (!evaluate_expression(fn
))
1368 if (ctype
->type
== SYM_NODE
) {
1370 * FIXME!! We should really expand the inline functions.
1371 * For now we just mark them accessed so that they show
1372 * up on the list of used symbols.
1374 if (ctype
->ctype
.modifiers
& MOD_INLINE
)
1375 access_symbol(ctype
);
1376 ctype
= ctype
->ctype
.base_type
;
1378 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1379 ctype
= ctype
->ctype
.base_type
;
1380 if (ctype
->type
!= SYM_FN
) {
1381 warn(expr
->pos
, "not a function");
1384 if (!evaluate_arguments(ctype
, arglist
))
1386 args
= expression_list_size(expr
->args
);
1387 fnargs
= symbol_list_size(ctype
->arguments
);
1389 warn(expr
->pos
, "not enough arguments for function");
1390 if (args
> fnargs
&& !ctype
->variadic
)
1391 warn(expr
->pos
, "too many arguments for function");
1392 expr
->ctype
= ctype
->ctype
.base_type
;
1396 struct symbol
*evaluate_expression(struct expression
*expr
)
1403 switch (expr
->type
) {
1405 warn(expr
->pos
, "value expression without a type");
1408 return evaluate_string(expr
);
1410 return evaluate_symbol_expression(expr
);
1412 if (!evaluate_expression(expr
->left
))
1414 if (!evaluate_expression(expr
->right
))
1416 return evaluate_binop(expr
);
1418 return evaluate_logical(expr
);
1420 if (!evaluate_expression(expr
->left
))
1422 if (!evaluate_expression(expr
->right
))
1424 return evaluate_comma(expr
);
1426 if (!evaluate_expression(expr
->left
))
1428 if (!evaluate_expression(expr
->right
))
1430 return evaluate_compare(expr
);
1431 case EXPR_ASSIGNMENT
:
1432 if (!evaluate_expression(expr
->left
))
1434 if (!evaluate_expression(expr
->right
))
1436 return evaluate_assignment(expr
);
1438 if (!evaluate_expression(expr
->unop
))
1440 return evaluate_preop(expr
);
1442 if (!evaluate_expression(expr
->unop
))
1444 return evaluate_postop(expr
);
1446 return evaluate_cast(expr
);
1448 return evaluate_sizeof(expr
);
1450 return evaluate_member_dereference(expr
);
1452 return evaluate_call(expr
);
1454 warn(expr
->pos
, "bitfield generated by parser");
1456 case EXPR_CONDITIONAL
:
1457 if (!evaluate_expression(expr
->conditional
))
1459 if (!evaluate_expression(expr
->cond_false
))
1461 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1463 return evaluate_conditional(expr
);
1464 case EXPR_STATEMENT
:
1465 expr
->ctype
= evaluate_statement(expr
->statement
);
1468 /* These can not exist as stand-alone expressions */
1469 case EXPR_INITIALIZER
:
1470 case EXPR_IDENTIFIER
:
1473 warn(expr
->pos
, "internal front-end error: initializer in expression");
1479 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1481 struct symbol
**last
= _last
;
1482 struct symbol
*type
= evaluate_statement(stmt
);
1484 if (flags
& ITERATE_LAST
)
1488 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1490 evaluate_symbol(sym
);
1493 static void check_duplicates(struct symbol
*sym
)
1495 struct symbol
*next
= sym
;
1497 while ((next
= next
->same_symbol
) != NULL
) {
1498 const char *typediff
;
1499 evaluate_symbol(next
);
1500 typediff
= type_difference(sym
, next
, 0, 0);
1502 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1503 show_ident(sym
->ident
),
1504 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1510 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1512 struct symbol
*base_type
;
1514 sym
= examine_symbol_type(sym
);
1515 base_type
= sym
->ctype
.base_type
;
1519 check_duplicates(sym
);
1521 /* Evaluate the initializers */
1522 if (sym
->initializer
) {
1523 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1524 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1525 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1526 base_type
->array_size
= count
;
1527 base_type
->bit_size
= bit_size
;
1528 sym
->array_size
= count
;
1529 sym
->bit_size
= bit_size
;
1533 /* And finally, evaluate the body of the symbol too */
1534 if (base_type
->type
== SYM_FN
) {
1535 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1536 if (base_type
->stmt
) {
1537 current_fn
= base_type
;
1538 current_contextmask
= sym
->ctype
.contextmask
;
1539 current_context
= sym
->ctype
.context
;
1540 evaluate_statement(base_type
->stmt
);
1547 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1549 struct expression
*expr
= stmt
->expression
;
1550 struct symbol
*ctype
, *fntype
;
1552 fntype
= current_fn
->ctype
.base_type
;
1553 if (fntype
== &void_ctype
) {
1555 warn(expr
->pos
, "return expression in void function");
1560 warn(stmt
->pos
, "return with no return value");
1563 ctype
= evaluate_expression(expr
);
1566 ctype
= degenerate(expr
, ctype
, &expr
);
1567 expr
->ctype
= ctype
;
1568 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1569 stmt
->expression
= expr
;
1573 struct symbol
*evaluate_statement(struct statement
*stmt
)
1578 switch (stmt
->type
) {
1580 return evaluate_return_expression(stmt
);
1582 case STMT_EXPRESSION
:
1583 return evaluate_expression(stmt
->expression
);
1585 case STMT_COMPOUND
: {
1586 struct symbol
*type
= NULL
;
1587 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1588 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1592 evaluate_expression(stmt
->if_conditional
);
1593 evaluate_statement(stmt
->if_true
);
1594 evaluate_statement(stmt
->if_false
);
1597 evaluate_expression(stmt
->iterator_pre_condition
);
1598 evaluate_expression(stmt
->iterator_post_condition
);
1599 evaluate_statement(stmt
->iterator_pre_statement
);
1600 evaluate_statement(stmt
->iterator_statement
);
1601 evaluate_statement(stmt
->iterator_post_statement
);
1604 evaluate_expression(stmt
->switch_expression
);
1605 evaluate_statement(stmt
->switch_statement
);
1608 evaluate_expression(stmt
->case_expression
);
1609 evaluate_expression(stmt
->case_to
);
1610 evaluate_statement(stmt
->case_statement
);
1613 evaluate_statement(stmt
->label_statement
);
1616 evaluate_expression(stmt
->goto_expression
);
1621 /* FIXME! Do the asm parameter evaluation! */
1627 long long get_expression_value(struct expression
*expr
)
1629 long long value
, mask
;
1630 struct symbol
*ctype
;
1632 ctype
= evaluate_expression(expr
);
1633 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1634 warn(expr
->pos
, "bad constant expression");
1638 value
= expr
->value
;
1639 mask
= 1ULL << (ctype
->bit_size
-1);
1642 while (ctype
->type
!= SYM_BASETYPE
)
1643 ctype
= ctype
->ctype
.base_type
;
1644 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1645 value
= value
| mask
| ~(mask
-1);