4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * expand constant expressions.
27 #include "expression.h"
29 /* Random cost numbers */
30 #define SIDE_EFFECTS 10000 /* The expression has side effects */
31 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
32 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
33 #define BRANCH_COST 10 /* Cost of a conditional branch */
35 static int expand_expression(struct expression
*);
36 static int expand_statement(struct statement
*);
37 static int conservative
;
39 static int expand_symbol_expression(struct expression
*expr
)
41 struct symbol
*sym
= expr
->symbol
;
43 if (sym
== &zero_int
) {
45 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
46 expr
->type
= EXPR_VALUE
;
51 /* The cost of a symbol expression is lower for on-stack symbols */
52 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
55 static long long get_longlong(struct expression
*expr
)
57 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
58 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
59 long long value
= expr
->value
;
60 long long ormask
, andmask
;
64 andmask
= mask
| (mask
-1);
68 return (value
& andmask
) | ormask
;
71 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
72 struct expression
*old
, struct symbol
*oldtype
)
74 int old_size
= oldtype
->bit_size
;
75 int new_size
= newtype
->bit_size
;
76 long long value
, mask
, signmask
;
77 long long oldmask
, oldsignmask
, dropped
;
79 if (newtype
->ctype
.base_type
== &fp_type
||
80 oldtype
->ctype
.base_type
== &fp_type
)
83 // For pointers and integers, we can just move the value around
84 expr
->type
= EXPR_VALUE
;
85 expr
->taint
= old
->taint
;
86 if (old_size
== new_size
) {
87 expr
->value
= old
->value
;
91 // expand it to the full "long long" value
92 value
= get_longlong(old
);
95 // _Bool requires a zero test rather than truncation.
96 if (is_bool_type(newtype
)) {
97 expr
->value
= !!value
;
98 if (!conservative
&& value
!= 0 && value
!= 1)
99 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
103 // Truncate it to the new size
104 signmask
= 1ULL << (new_size
-1);
105 mask
= signmask
| (signmask
-1);
106 expr
->value
= value
& mask
;
108 // Stop here unless checking for truncation
109 if (!Wcast_truncate
|| conservative
)
112 // Check if we dropped any bits..
113 oldsignmask
= 1ULL << (old_size
-1);
114 oldmask
= oldsignmask
| (oldsignmask
-1);
115 dropped
= oldmask
& ~mask
;
117 // OK if the bits were (and still are) purely sign bits
118 if (value
& dropped
) {
119 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
120 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
127 if (!is_float_type(newtype
)) {
128 value
= (long long)old
->fvalue
;
129 expr
->type
= EXPR_VALUE
;
134 if (!is_float_type(oldtype
))
135 expr
->fvalue
= (long double)get_longlong(old
);
137 expr
->fvalue
= old
->fvalue
;
139 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
140 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
141 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
142 expr
->fvalue
= (double)expr
->fvalue
;
144 expr
->fvalue
= (float)expr
->fvalue
;
146 expr
->type
= EXPR_FVALUE
;
149 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
151 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
152 count
&= ctype
->bit_size
-1;
157 * CAREFUL! We need to get the size and sign of the
160 #define CONVERT(op,s) (((op)<<1)+(s))
161 #define SIGNED(op) CONVERT(op, 1)
162 #define UNSIGNED(op) CONVERT(op, 0)
163 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
165 struct expression
*left
= expr
->left
, *right
= expr
->right
;
166 unsigned long long v
, l
, r
, mask
;
167 signed long long sl
, sr
;
170 if (right
->type
!= EXPR_VALUE
)
173 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
174 if (r
>= ctype
->bit_size
) {
177 r
= check_shift_count(expr
, ctype
, r
);
181 if (left
->type
!= EXPR_VALUE
)
183 l
= left
->value
; r
= right
->value
;
184 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
185 mask
= 1ULL << (ctype
->bit_size
-1);
187 if (is_signed
&& (sl
& mask
))
189 if (is_signed
&& (sr
& mask
))
192 switch (CONVERT(expr
->op
,is_signed
)) {
229 if (l
== mask
&& sr
== -1)
242 if (l
== mask
&& sr
== -1)
252 case SIGNED(SPECIAL_LEFTSHIFT
):
253 case UNSIGNED(SPECIAL_LEFTSHIFT
):
257 case SIGNED(SPECIAL_RIGHTSHIFT
):
261 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
268 mask
= mask
| (mask
-1);
269 expr
->value
= v
& mask
;
270 expr
->type
= EXPR_VALUE
;
271 expr
->taint
= left
->taint
| right
->taint
;
275 warning(expr
->pos
, "division by zero");
279 warning(expr
->pos
, "constant integer operation overflow");
283 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
285 struct expression
*left
= expr
->left
, *right
= expr
->right
;
286 unsigned long long l
, r
, mask
;
287 signed long long sl
, sr
;
289 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
291 l
= left
->value
; r
= right
->value
;
292 mask
= 1ULL << (ctype
->bit_size
-1);
299 case '<': expr
->value
= sl
< sr
; break;
300 case '>': expr
->value
= sl
> sr
; break;
301 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
302 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
303 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
304 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
305 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
306 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
307 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
308 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
310 expr
->type
= EXPR_VALUE
;
311 expr
->taint
= left
->taint
| right
->taint
;
315 static int simplify_float_binop(struct expression
*expr
)
317 struct expression
*left
= expr
->left
, *right
= expr
->right
;
318 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
319 long double l
, r
, res
;
321 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
327 if (mod
& MOD_LONGLONG
) {
329 case '+': res
= l
+ r
; break;
330 case '-': res
= l
- r
; break;
331 case '*': res
= l
* r
; break;
332 case '/': if (!r
) goto Div
;
336 } else if (mod
& MOD_LONG
) {
338 case '+': res
= (double) l
+ (double) r
; break;
339 case '-': res
= (double) l
- (double) r
; break;
340 case '*': res
= (double) l
* (double) r
; break;
341 case '/': if (!r
) goto Div
;
342 res
= (double) l
/ (double) r
; break;
347 case '+': res
= (float)l
+ (float)r
; break;
348 case '-': res
= (float)l
- (float)r
; break;
349 case '*': res
= (float)l
* (float)r
; break;
350 case '/': if (!r
) goto Div
;
351 res
= (float)l
/ (float)r
; break;
355 expr
->type
= EXPR_FVALUE
;
360 warning(expr
->pos
, "division by zero");
364 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
366 struct expression
*left
= expr
->left
, *right
= expr
->right
;
369 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
375 case '<': expr
->value
= l
< r
; break;
376 case '>': expr
->value
= l
> r
; break;
377 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
378 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
379 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
380 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
382 expr
->type
= EXPR_VALUE
;
387 static int expand_binop(struct expression
*expr
)
391 cost
= expand_expression(expr
->left
);
392 cost
+= expand_expression(expr
->right
);
393 if (simplify_int_binop(expr
, expr
->ctype
))
395 if (simplify_float_binop(expr
))
400 static int expand_logical(struct expression
*expr
)
402 struct expression
*left
= expr
->left
;
403 struct expression
*right
;
406 /* Do immediate short-circuiting ... */
407 cost
= expand_expression(left
);
408 if (left
->type
== EXPR_VALUE
) {
409 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
411 expr
->type
= EXPR_VALUE
;
413 expr
->taint
= left
->taint
;
418 expr
->type
= EXPR_VALUE
;
420 expr
->taint
= left
->taint
;
427 rcost
= expand_expression(right
);
428 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
430 * We know the left value doesn't matter, since
431 * otherwise we would have short-circuited it..
433 expr
->type
= EXPR_VALUE
;
434 expr
->value
= right
->value
!= 0;
435 expr
->taint
= left
->taint
| right
->taint
;
440 * If the right side is safe and cheaper than a branch,
441 * just avoid the branch and turn it into a regular binop
444 if (rcost
< BRANCH_COST
) {
445 expr
->type
= EXPR_BINOP
;
446 rcost
-= BRANCH_COST
- 1;
449 return cost
+ BRANCH_COST
+ rcost
;
452 static int expand_comma(struct expression
*expr
)
456 cost
= expand_expression(expr
->left
);
457 cost
+= expand_expression(expr
->right
);
458 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
459 unsigned flags
= expr
->flags
;
461 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
462 *expr
= *expr
->right
;
464 if (expr
->type
== EXPR_VALUE
)
465 expr
->taint
|= Taint_comma
| taint
;
470 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
472 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
474 struct ctype c1
= {.base_type
= left
};
475 struct ctype c2
= {.base_type
= right
};
478 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
479 case SPECIAL_NOTEQUAL
:
480 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
482 return left
->bit_size
< right
->bit_size
;
484 return left
->bit_size
> right
->bit_size
;
486 return left
->bit_size
<= right
->bit_size
;
488 return left
->bit_size
>= right
->bit_size
;
493 static int expand_compare(struct expression
*expr
)
495 struct expression
*left
= expr
->left
, *right
= expr
->right
;
498 cost
= expand_expression(left
);
499 cost
+= expand_expression(right
);
502 /* Type comparison? */
503 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
505 expr
->type
= EXPR_VALUE
;
506 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
510 if (simplify_cmp_binop(expr
, left
->ctype
))
512 if (simplify_float_cmp(expr
, left
->ctype
))
518 static int expand_conditional(struct expression
*expr
)
520 struct expression
*cond
= expr
->conditional
;
521 struct expression
*true = expr
->cond_true
;
522 struct expression
*false = expr
->cond_false
;
525 cond_cost
= expand_expression(cond
);
526 if (cond
->type
== EXPR_VALUE
) {
527 unsigned flags
= expr
->flags
;
532 cost
= expand_expression(true);
535 if (expr
->type
== EXPR_VALUE
)
536 expr
->taint
|= cond
->taint
;
540 cost
= expand_expression(true);
541 cost
+= expand_expression(false);
543 if (cost
< SELECT_COST
) {
544 expr
->type
= EXPR_SELECT
;
545 cost
-= BRANCH_COST
- 1;
548 return cost
+ cond_cost
+ BRANCH_COST
;
551 static int expand_assignment(struct expression
*expr
)
553 expand_expression(expr
->left
);
554 expand_expression(expr
->right
);
558 static int expand_addressof(struct expression
*expr
)
560 return expand_expression(expr
->unop
);
564 * Look up a trustable initializer value at the requested offset.
566 * Return NULL if no such value can be found or statically trusted.
568 * FIXME!! We should check that the size is right!
570 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
572 struct expression
*value
;
574 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
576 value
= sym
->initializer
;
579 if (value
->type
== EXPR_INITIALIZER
) {
580 struct expression
*entry
;
581 FOR_EACH_PTR(value
->expr_list
, entry
) {
582 if (entry
->type
!= EXPR_POS
) {
587 if (entry
->init_offset
< offset
)
589 if (entry
->init_offset
> offset
)
591 return entry
->init_expr
;
592 } END_FOR_EACH_PTR(entry
);
598 static int expand_dereference(struct expression
*expr
)
600 struct expression
*unop
= expr
->unop
;
603 expand_expression(unop
);
606 * NOTE! We get a bogus warning right now for some special
607 * cases: apparently I've screwed up the optimization of
608 * a zero-offset dereference, and the ctype is wrong.
610 * Leave the warning in anyway, since this is also a good
611 * test for me to get the type evaluation right..
613 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
614 warning(unop
->pos
, "dereference of noderef expression");
617 * Is it "symbol" or "symbol + offset"?
620 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
621 struct expression
*right
= unop
->right
;
622 if (right
->type
== EXPR_VALUE
) {
623 offset
= right
->value
;
628 if (unop
->type
== EXPR_SYMBOL
) {
629 struct symbol
*sym
= unop
->symbol
;
630 struct expression
*value
= constant_symbol_value(sym
, offset
);
632 /* Const symbol with a constant initializer? */
634 /* FIXME! We should check that the size is right! */
635 if (value
->type
== EXPR_VALUE
) {
636 expr
->type
= EXPR_VALUE
;
637 expr
->value
= value
->value
;
640 } else if (value
->type
== EXPR_FVALUE
) {
641 expr
->type
= EXPR_FVALUE
;
642 expr
->fvalue
= value
->fvalue
;
647 /* Direct symbol dereference? Cheap and safe */
648 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
654 static int simplify_preop(struct expression
*expr
)
656 struct expression
*op
= expr
->unop
;
657 unsigned long long v
, mask
;
659 if (op
->type
!= EXPR_VALUE
)
662 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
667 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
671 case '!': v
= !v
; break;
672 case '~': v
= ~v
; break;
675 mask
= mask
| (mask
-1);
676 expr
->value
= v
& mask
;
677 expr
->type
= EXPR_VALUE
;
678 expr
->taint
= op
->taint
;
683 warning(expr
->pos
, "constant integer operation overflow");
687 static int simplify_float_preop(struct expression
*expr
)
689 struct expression
*op
= expr
->unop
;
692 if (op
->type
!= EXPR_FVALUE
)
697 case '-': v
= -v
; break;
701 expr
->type
= EXPR_FVALUE
;
706 * Unary post-ops: x++ and x--
708 static int expand_postop(struct expression
*expr
)
710 expand_expression(expr
->unop
);
714 static int expand_preop(struct expression
*expr
)
720 return expand_dereference(expr
);
723 return expand_addressof(expr
);
725 case SPECIAL_INCREMENT
:
726 case SPECIAL_DECREMENT
:
728 * From a type evaluation standpoint the preops are
729 * the same as the postops
731 return expand_postop(expr
);
736 cost
= expand_expression(expr
->unop
);
738 if (simplify_preop(expr
))
740 if (simplify_float_preop(expr
))
745 static int expand_arguments(struct expression_list
*head
)
748 struct expression
*expr
;
750 FOR_EACH_PTR (head
, expr
) {
751 cost
+= expand_expression(expr
);
752 } END_FOR_EACH_PTR(expr
);
756 static int expand_cast(struct expression
*expr
)
759 struct expression
*target
= expr
->cast_expression
;
761 cost
= expand_expression(target
);
763 /* Simplify normal integer casts.. */
764 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
765 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
771 /* The arguments are constant if the cost of all of them is zero */
772 int expand_constant_p(struct expression
*expr
, int cost
)
774 expr
->type
= EXPR_VALUE
;
780 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
781 int expand_safe_p(struct expression
*expr
, int cost
)
783 expr
->type
= EXPR_VALUE
;
784 expr
->value
= (cost
< SIDE_EFFECTS
);
790 * expand a call expression with a symbol. This
791 * should expand builtins.
793 static int expand_symbol_call(struct expression
*expr
, int cost
)
795 struct expression
*fn
= expr
->fn
;
796 struct symbol
*ctype
= fn
->ctype
;
798 if (fn
->type
!= EXPR_PREOP
)
801 if (ctype
->op
&& ctype
->op
->expand
)
802 return ctype
->op
->expand(expr
, cost
);
804 if (ctype
->ctype
.modifiers
& MOD_PURE
)
810 static int expand_call(struct expression
*expr
)
814 struct expression
*fn
= expr
->fn
;
816 cost
= expand_arguments(expr
->args
);
819 expression_error(expr
, "function has no type");
822 if (sym
->type
== SYM_NODE
)
823 return expand_symbol_call(expr
, cost
);
828 static int expand_expression_list(struct expression_list
*list
)
831 struct expression
*expr
;
833 FOR_EACH_PTR(list
, expr
) {
834 cost
+= expand_expression(expr
);
835 } END_FOR_EACH_PTR(expr
);
840 * We can simplify nested position expressions if
841 * this is a simple (single) positional expression.
843 static int expand_pos_expression(struct expression
*expr
)
845 struct expression
*nested
= expr
->init_expr
;
846 unsigned long offset
= expr
->init_offset
;
847 int nr
= expr
->init_nr
;
850 switch (nested
->type
) {
852 offset
+= nested
->init_offset
;
854 expr
->init_offset
= offset
;
858 case EXPR_INITIALIZER
: {
859 struct expression
*reuse
= nested
, *entry
;
861 FOR_EACH_PTR(expr
->expr_list
, entry
) {
862 if (entry
->type
== EXPR_POS
) {
863 entry
->init_offset
+= offset
;
867 * This happens rarely, but it can happen
868 * with bitfields that are all at offset
871 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
873 reuse
->type
= EXPR_POS
;
874 reuse
->ctype
= entry
->ctype
;
875 reuse
->init_offset
= offset
;
877 reuse
->init_expr
= entry
;
878 REPLACE_CURRENT_PTR(entry
, reuse
);
881 } END_FOR_EACH_PTR(entry
);
890 return expand_expression(nested
);
893 static unsigned long bit_offset(const struct expression
*expr
)
895 unsigned long offset
= 0;
896 while (expr
->type
== EXPR_POS
) {
897 offset
+= bytes_to_bits(expr
->init_offset
);
898 expr
= expr
->init_expr
;
900 if (expr
&& expr
->ctype
)
901 offset
+= expr
->ctype
->bit_offset
;
905 static int compare_expressions(const void *_a
, const void *_b
)
907 const struct expression
*a
= _a
;
908 const struct expression
*b
= _b
;
909 unsigned long a_pos
= bit_offset(a
);
910 unsigned long b_pos
= bit_offset(b
);
912 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
915 static void sort_expression_list(struct expression_list
**list
)
917 sort_list((struct ptr_list
**)list
, compare_expressions
);
920 static void verify_nonoverlapping(struct expression_list
**list
)
922 struct expression
*a
= NULL
;
923 struct expression
*b
;
925 FOR_EACH_PTR(*list
, b
) {
926 if (!b
->ctype
|| !b
->ctype
->bit_size
)
928 if (a
&& bit_offset(a
) == bit_offset(b
)) {
929 warning(a
->pos
, "Initializer entry defined twice");
930 info(b
->pos
, " also defined here");
934 } END_FOR_EACH_PTR(b
);
937 static int expand_expression(struct expression
*expr
)
941 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
944 switch (expr
->type
) {
951 return expand_symbol_expression(expr
);
953 return expand_binop(expr
);
956 return expand_logical(expr
);
959 return expand_comma(expr
);
962 return expand_compare(expr
);
964 case EXPR_ASSIGNMENT
:
965 return expand_assignment(expr
);
968 return expand_preop(expr
);
971 return expand_postop(expr
);
974 case EXPR_FORCE_CAST
:
975 case EXPR_IMPLIED_CAST
:
976 return expand_cast(expr
);
979 return expand_call(expr
);
982 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
986 case EXPR_CONDITIONAL
:
987 return expand_conditional(expr
);
989 case EXPR_STATEMENT
: {
990 struct statement
*stmt
= expr
->statement
;
991 int cost
= expand_statement(stmt
);
993 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
994 *expr
= *stmt
->expression
;
1001 case EXPR_INITIALIZER
:
1002 sort_expression_list(&expr
->expr_list
);
1003 verify_nonoverlapping(&expr
->expr_list
);
1004 return expand_expression_list(expr
->expr_list
);
1006 case EXPR_IDENTIFIER
:
1013 return expand_expression(expr
->base
) + 1;
1016 return expand_pos_expression(expr
);
1019 case EXPR_PTRSIZEOF
:
1022 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1025 return SIDE_EFFECTS
;
1028 static void expand_const_expression(struct expression
*expr
, const char *where
)
1031 expand_expression(expr
);
1032 if (expr
->type
!= EXPR_VALUE
)
1033 expression_error(expr
, "Expected constant expression in %s", where
);
1037 int expand_symbol(struct symbol
*sym
)
1040 struct symbol
*base_type
;
1044 base_type
= sym
->ctype
.base_type
;
1048 retval
= expand_expression(sym
->initializer
);
1049 /* expand the body of the symbol */
1050 if (base_type
->type
== SYM_FN
) {
1051 if (base_type
->stmt
)
1052 expand_statement(base_type
->stmt
);
1057 static void expand_return_expression(struct statement
*stmt
)
1059 expand_expression(stmt
->expression
);
1062 static int expand_if_statement(struct statement
*stmt
)
1064 struct expression
*expr
= stmt
->if_conditional
;
1066 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1069 expand_expression(expr
);
1071 /* This is only valid if nobody jumps into the "dead" side */
1073 /* Simplify constant conditionals without even evaluating the false side */
1074 if (expr
->type
== EXPR_VALUE
) {
1075 struct statement
*simple
;
1076 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1080 stmt
->type
= STMT_NONE
;
1083 expand_statement(simple
);
1085 return SIDE_EFFECTS
;
1088 expand_statement(stmt
->if_true
);
1089 expand_statement(stmt
->if_false
);
1090 return SIDE_EFFECTS
;
1094 * Expanding a compound statement is really just
1095 * about adding up the costs of each individual
1098 * We also collapse a simple compound statement:
1099 * this would trigger for simple inline functions,
1100 * except we would have to check the "return"
1101 * symbol usage. Next time.
1103 static int expand_compound(struct statement
*stmt
)
1105 struct statement
*s
, *last
;
1106 int cost
, statements
;
1109 expand_symbol(stmt
->ret
);
1112 cost
= expand_statement(last
);
1113 statements
= last
!= NULL
;
1114 FOR_EACH_PTR(stmt
->stmts
, s
) {
1117 cost
+= expand_statement(s
);
1118 } END_FOR_EACH_PTR(s
);
1120 if (statements
== 1 && !stmt
->ret
)
1126 static int expand_statement(struct statement
*stmt
)
1131 switch (stmt
->type
) {
1132 case STMT_DECLARATION
: {
1134 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1136 } END_FOR_EACH_PTR(sym
);
1137 return SIDE_EFFECTS
;
1141 expand_return_expression(stmt
);
1142 return SIDE_EFFECTS
;
1144 case STMT_EXPRESSION
:
1145 return expand_expression(stmt
->expression
);
1148 return expand_compound(stmt
);
1151 return expand_if_statement(stmt
);
1154 expand_expression(stmt
->iterator_pre_condition
);
1155 expand_expression(stmt
->iterator_post_condition
);
1156 expand_statement(stmt
->iterator_pre_statement
);
1157 expand_statement(stmt
->iterator_statement
);
1158 expand_statement(stmt
->iterator_post_statement
);
1159 return SIDE_EFFECTS
;
1162 expand_expression(stmt
->switch_expression
);
1163 expand_statement(stmt
->switch_statement
);
1164 return SIDE_EFFECTS
;
1167 expand_const_expression(stmt
->case_expression
, "case statement");
1168 expand_const_expression(stmt
->case_to
, "case statement");
1169 expand_statement(stmt
->case_statement
);
1170 return SIDE_EFFECTS
;
1173 expand_statement(stmt
->label_statement
);
1174 return SIDE_EFFECTS
;
1177 expand_expression(stmt
->goto_expression
);
1178 return SIDE_EFFECTS
;
1183 /* FIXME! Do the asm parameter evaluation! */
1186 expand_expression(stmt
->expression
);
1189 expand_expression(stmt
->range_expression
);
1190 expand_expression(stmt
->range_low
);
1191 expand_expression(stmt
->range_high
);
1194 return SIDE_EFFECTS
;
1197 static inline int bad_integer_constant_expression(struct expression
*expr
)
1199 if (!(expr
->flags
& Int_const_expr
))
1201 if (expr
->taint
& Taint_comma
)
1206 static long long __get_expression_value(struct expression
*expr
, int strict
)
1208 long long value
, mask
;
1209 struct symbol
*ctype
;
1213 ctype
= evaluate_expression(expr
);
1215 expression_error(expr
, "bad constant expression type");
1218 expand_expression(expr
);
1219 if (expr
->type
!= EXPR_VALUE
) {
1221 expression_error(expr
, "bad constant expression");
1224 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1225 expression_error(expr
, "bad integer constant expression");
1229 value
= expr
->value
;
1230 mask
= 1ULL << (ctype
->bit_size
-1);
1233 while (ctype
->type
!= SYM_BASETYPE
)
1234 ctype
= ctype
->ctype
.base_type
;
1235 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1236 value
= value
| mask
| ~(mask
-1);
1241 long long get_expression_value(struct expression
*expr
)
1243 return __get_expression_value(expr
, 0);
1246 long long const_expression_value(struct expression
*expr
)
1248 return __get_expression_value(expr
, 1);
1251 long long get_expression_value_silent(struct expression
*expr
)
1254 return __get_expression_value(expr
, 2);
1257 int is_zero_constant(struct expression
*expr
)
1259 const int saved
= conservative
;
1261 expand_expression(expr
);
1262 conservative
= saved
;
1263 return expr
->type
== EXPR_VALUE
&& !expr
->value
;