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)
250 case SIGNED(SPECIAL_LEFTSHIFT
):
251 case UNSIGNED(SPECIAL_LEFTSHIFT
):
255 case SIGNED(SPECIAL_RIGHTSHIFT
):
259 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
266 mask
= mask
| (mask
-1);
267 expr
->value
= v
& mask
;
268 expr
->type
= EXPR_VALUE
;
269 expr
->taint
= left
->taint
| right
->taint
;
273 warning(expr
->pos
, "division by zero");
277 warning(expr
->pos
, "constant integer operation overflow");
281 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
283 struct expression
*left
= expr
->left
, *right
= expr
->right
;
284 unsigned long long l
, r
, mask
;
285 signed long long sl
, sr
;
287 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
289 l
= left
->value
; r
= right
->value
;
290 mask
= 1ULL << (ctype
->bit_size
-1);
297 case '<': expr
->value
= sl
< sr
; break;
298 case '>': expr
->value
= sl
> sr
; break;
299 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
300 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
301 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
302 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
303 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
304 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
305 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
306 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
308 expr
->type
= EXPR_VALUE
;
309 expr
->taint
= left
->taint
| right
->taint
;
313 static int simplify_float_binop(struct expression
*expr
)
315 struct expression
*left
= expr
->left
, *right
= expr
->right
;
316 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
317 long double l
, r
, res
;
319 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
325 if (mod
& MOD_LONGLONG
) {
327 case '+': res
= l
+ r
; break;
328 case '-': res
= l
- r
; break;
329 case '*': res
= l
* r
; break;
330 case '/': if (!r
) goto Div
;
334 } else if (mod
& MOD_LONG
) {
336 case '+': res
= (double) l
+ (double) r
; break;
337 case '-': res
= (double) l
- (double) r
; break;
338 case '*': res
= (double) l
* (double) r
; break;
339 case '/': if (!r
) goto Div
;
340 res
= (double) l
/ (double) r
; break;
345 case '+': res
= (float)l
+ (float)r
; break;
346 case '-': res
= (float)l
- (float)r
; break;
347 case '*': res
= (float)l
* (float)r
; break;
348 case '/': if (!r
) goto Div
;
349 res
= (float)l
/ (float)r
; break;
353 expr
->type
= EXPR_FVALUE
;
358 warning(expr
->pos
, "division by zero");
362 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
364 struct expression
*left
= expr
->left
, *right
= expr
->right
;
367 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
373 case '<': expr
->value
= l
< r
; break;
374 case '>': expr
->value
= l
> r
; break;
375 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
376 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
377 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
378 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
380 expr
->type
= EXPR_VALUE
;
385 static int expand_binop(struct expression
*expr
)
389 cost
= expand_expression(expr
->left
);
390 cost
+= expand_expression(expr
->right
);
391 if (simplify_int_binop(expr
, expr
->ctype
))
393 if (simplify_float_binop(expr
))
398 static int expand_logical(struct expression
*expr
)
400 struct expression
*left
= expr
->left
;
401 struct expression
*right
;
404 /* Do immediate short-circuiting ... */
405 cost
= expand_expression(left
);
406 if (left
->type
== EXPR_VALUE
) {
407 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
409 expr
->type
= EXPR_VALUE
;
411 expr
->taint
= left
->taint
;
416 expr
->type
= EXPR_VALUE
;
418 expr
->taint
= left
->taint
;
425 rcost
= expand_expression(right
);
426 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
428 * We know the left value doesn't matter, since
429 * otherwise we would have short-circuited it..
431 expr
->type
= EXPR_VALUE
;
432 expr
->value
= right
->value
!= 0;
433 expr
->taint
= left
->taint
| right
->taint
;
438 * If the right side is safe and cheaper than a branch,
439 * just avoid the branch and turn it into a regular binop
442 if (rcost
< BRANCH_COST
) {
443 expr
->type
= EXPR_BINOP
;
444 rcost
-= BRANCH_COST
- 1;
447 return cost
+ BRANCH_COST
+ rcost
;
450 static int expand_comma(struct expression
*expr
)
454 cost
= expand_expression(expr
->left
);
455 cost
+= expand_expression(expr
->right
);
456 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
457 unsigned flags
= expr
->flags
;
459 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
460 *expr
= *expr
->right
;
462 if (expr
->type
== EXPR_VALUE
)
463 expr
->taint
|= Taint_comma
| taint
;
468 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
470 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
472 struct ctype c1
= {.base_type
= left
};
473 struct ctype c2
= {.base_type
= right
};
476 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
477 case SPECIAL_NOTEQUAL
:
478 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
480 return left
->bit_size
< right
->bit_size
;
482 return left
->bit_size
> right
->bit_size
;
484 return left
->bit_size
<= right
->bit_size
;
486 return left
->bit_size
>= right
->bit_size
;
491 static int expand_compare(struct expression
*expr
)
493 struct expression
*left
= expr
->left
, *right
= expr
->right
;
496 cost
= expand_expression(left
);
497 cost
+= expand_expression(right
);
500 /* Type comparison? */
501 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
503 expr
->type
= EXPR_VALUE
;
504 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
508 if (simplify_cmp_binop(expr
, left
->ctype
))
510 if (simplify_float_cmp(expr
, left
->ctype
))
516 static int expand_conditional(struct expression
*expr
)
518 struct expression
*cond
= expr
->conditional
;
519 struct expression
*true = expr
->cond_true
;
520 struct expression
*false = expr
->cond_false
;
523 cond_cost
= expand_expression(cond
);
524 if (cond
->type
== EXPR_VALUE
) {
525 unsigned flags
= expr
->flags
;
530 cost
= expand_expression(true);
533 if (expr
->type
== EXPR_VALUE
)
534 expr
->taint
|= cond
->taint
;
538 cost
= expand_expression(true);
539 cost
+= expand_expression(false);
541 if (cost
< SELECT_COST
) {
542 expr
->type
= EXPR_SELECT
;
543 cost
-= BRANCH_COST
- 1;
546 return cost
+ cond_cost
+ BRANCH_COST
;
549 static int expand_assignment(struct expression
*expr
)
551 expand_expression(expr
->left
);
552 expand_expression(expr
->right
);
556 static int expand_addressof(struct expression
*expr
)
558 return expand_expression(expr
->unop
);
562 * Look up a trustable initializer value at the requested offset.
564 * Return NULL if no such value can be found or statically trusted.
566 * FIXME!! We should check that the size is right!
568 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
570 struct expression
*value
;
572 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
574 value
= sym
->initializer
;
577 if (value
->type
== EXPR_INITIALIZER
) {
578 struct expression
*entry
;
579 FOR_EACH_PTR(value
->expr_list
, entry
) {
580 if (entry
->type
!= EXPR_POS
) {
585 if (entry
->init_offset
< offset
)
587 if (entry
->init_offset
> offset
)
589 return entry
->init_expr
;
590 } END_FOR_EACH_PTR(entry
);
596 static int expand_dereference(struct expression
*expr
)
598 struct expression
*unop
= expr
->unop
;
601 expand_expression(unop
);
604 * NOTE! We get a bogus warning right now for some special
605 * cases: apparently I've screwed up the optimization of
606 * a zero-offset dereference, and the ctype is wrong.
608 * Leave the warning in anyway, since this is also a good
609 * test for me to get the type evaluation right..
611 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
612 warning(unop
->pos
, "dereference of noderef expression");
615 * Is it "symbol" or "symbol + offset"?
618 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
619 struct expression
*right
= unop
->right
;
620 if (right
->type
== EXPR_VALUE
) {
621 offset
= right
->value
;
626 if (unop
->type
== EXPR_SYMBOL
) {
627 struct symbol
*sym
= unop
->symbol
;
628 struct expression
*value
= constant_symbol_value(sym
, offset
);
630 /* Const symbol with a constant initializer? */
632 /* FIXME! We should check that the size is right! */
633 if (value
->type
== EXPR_VALUE
) {
634 expr
->type
= EXPR_VALUE
;
635 expr
->value
= value
->value
;
638 } else if (value
->type
== EXPR_FVALUE
) {
639 expr
->type
= EXPR_FVALUE
;
640 expr
->fvalue
= value
->fvalue
;
645 /* Direct symbol dereference? Cheap and safe */
646 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
652 static int simplify_preop(struct expression
*expr
)
654 struct expression
*op
= expr
->unop
;
655 unsigned long long v
, mask
;
657 if (op
->type
!= EXPR_VALUE
)
660 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
665 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
669 case '!': v
= !v
; break;
670 case '~': v
= ~v
; break;
673 mask
= mask
| (mask
-1);
674 expr
->value
= v
& mask
;
675 expr
->type
= EXPR_VALUE
;
676 expr
->taint
= op
->taint
;
681 warning(expr
->pos
, "constant integer operation overflow");
685 static int simplify_float_preop(struct expression
*expr
)
687 struct expression
*op
= expr
->unop
;
690 if (op
->type
!= EXPR_FVALUE
)
695 case '-': v
= -v
; break;
699 expr
->type
= EXPR_FVALUE
;
704 * Unary post-ops: x++ and x--
706 static int expand_postop(struct expression
*expr
)
708 expand_expression(expr
->unop
);
712 static int expand_preop(struct expression
*expr
)
718 return expand_dereference(expr
);
721 return expand_addressof(expr
);
723 case SPECIAL_INCREMENT
:
724 case SPECIAL_DECREMENT
:
726 * From a type evaluation standpoint the preops are
727 * the same as the postops
729 return expand_postop(expr
);
734 cost
= expand_expression(expr
->unop
);
736 if (simplify_preop(expr
))
738 if (simplify_float_preop(expr
))
743 static int expand_arguments(struct expression_list
*head
)
746 struct expression
*expr
;
748 FOR_EACH_PTR (head
, expr
) {
749 cost
+= expand_expression(expr
);
750 } END_FOR_EACH_PTR(expr
);
754 static int expand_cast(struct expression
*expr
)
757 struct expression
*target
= expr
->cast_expression
;
759 cost
= expand_expression(target
);
761 /* Simplify normal integer casts.. */
762 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
763 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
769 /* The arguments are constant if the cost of all of them is zero */
770 int expand_constant_p(struct expression
*expr
, int cost
)
772 expr
->type
= EXPR_VALUE
;
778 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
779 int expand_safe_p(struct expression
*expr
, int cost
)
781 expr
->type
= EXPR_VALUE
;
782 expr
->value
= (cost
< SIDE_EFFECTS
);
788 * expand a call expression with a symbol. This
789 * should expand builtins.
791 static int expand_symbol_call(struct expression
*expr
, int cost
)
793 struct expression
*fn
= expr
->fn
;
794 struct symbol
*ctype
= fn
->ctype
;
796 if (fn
->type
!= EXPR_PREOP
)
799 if (ctype
->op
&& ctype
->op
->expand
)
800 return ctype
->op
->expand(expr
, cost
);
802 if (ctype
->ctype
.modifiers
& MOD_PURE
)
808 static int expand_call(struct expression
*expr
)
812 struct expression
*fn
= expr
->fn
;
814 cost
= expand_arguments(expr
->args
);
817 expression_error(expr
, "function has no type");
820 if (sym
->type
== SYM_NODE
)
821 return expand_symbol_call(expr
, cost
);
826 static int expand_expression_list(struct expression_list
*list
)
829 struct expression
*expr
;
831 FOR_EACH_PTR(list
, expr
) {
832 cost
+= expand_expression(expr
);
833 } END_FOR_EACH_PTR(expr
);
838 * We can simplify nested position expressions if
839 * this is a simple (single) positional expression.
841 static int expand_pos_expression(struct expression
*expr
)
843 struct expression
*nested
= expr
->init_expr
;
844 unsigned long offset
= expr
->init_offset
;
845 int nr
= expr
->init_nr
;
848 switch (nested
->type
) {
850 offset
+= nested
->init_offset
;
852 expr
->init_offset
= offset
;
856 case EXPR_INITIALIZER
: {
857 struct expression
*reuse
= nested
, *entry
;
859 FOR_EACH_PTR(expr
->expr_list
, entry
) {
860 if (entry
->type
== EXPR_POS
) {
861 entry
->init_offset
+= offset
;
865 * This happens rarely, but it can happen
866 * with bitfields that are all at offset
869 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
871 reuse
->type
= EXPR_POS
;
872 reuse
->ctype
= entry
->ctype
;
873 reuse
->init_offset
= offset
;
875 reuse
->init_expr
= entry
;
876 REPLACE_CURRENT_PTR(entry
, reuse
);
879 } END_FOR_EACH_PTR(entry
);
888 return expand_expression(nested
);
891 static unsigned long bit_offset(const struct expression
*expr
)
893 unsigned long offset
= 0;
894 while (expr
->type
== EXPR_POS
) {
895 offset
+= bytes_to_bits(expr
->init_offset
);
896 expr
= expr
->init_expr
;
898 if (expr
&& expr
->ctype
)
899 offset
+= expr
->ctype
->bit_offset
;
903 static int compare_expressions(const void *_a
, const void *_b
)
905 const struct expression
*a
= _a
;
906 const struct expression
*b
= _b
;
907 unsigned long a_pos
= bit_offset(a
);
908 unsigned long b_pos
= bit_offset(b
);
910 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
913 static void sort_expression_list(struct expression_list
**list
)
915 sort_list((struct ptr_list
**)list
, compare_expressions
);
918 static void verify_nonoverlapping(struct expression_list
**list
)
920 struct expression
*a
= NULL
;
921 struct expression
*b
;
923 FOR_EACH_PTR(*list
, b
) {
924 if (!b
->ctype
|| !b
->ctype
->bit_size
)
926 if (a
&& bit_offset(a
) == bit_offset(b
)) {
927 warning(a
->pos
, "Initializer entry defined twice");
928 info(b
->pos
, " also defined here");
932 } END_FOR_EACH_PTR(b
);
935 static int expand_expression(struct expression
*expr
)
939 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
942 switch (expr
->type
) {
949 return expand_symbol_expression(expr
);
951 return expand_binop(expr
);
954 return expand_logical(expr
);
957 return expand_comma(expr
);
960 return expand_compare(expr
);
962 case EXPR_ASSIGNMENT
:
963 return expand_assignment(expr
);
966 return expand_preop(expr
);
969 return expand_postop(expr
);
972 case EXPR_FORCE_CAST
:
973 case EXPR_IMPLIED_CAST
:
974 return expand_cast(expr
);
977 return expand_call(expr
);
980 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
984 case EXPR_CONDITIONAL
:
985 return expand_conditional(expr
);
987 case EXPR_STATEMENT
: {
988 struct statement
*stmt
= expr
->statement
;
989 int cost
= expand_statement(stmt
);
991 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
992 *expr
= *stmt
->expression
;
999 case EXPR_INITIALIZER
:
1000 sort_expression_list(&expr
->expr_list
);
1001 verify_nonoverlapping(&expr
->expr_list
);
1002 return expand_expression_list(expr
->expr_list
);
1004 case EXPR_IDENTIFIER
:
1011 return expand_expression(expr
->base
) + 1;
1014 return expand_pos_expression(expr
);
1017 case EXPR_PTRSIZEOF
:
1020 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1023 return SIDE_EFFECTS
;
1026 static void expand_const_expression(struct expression
*expr
, const char *where
)
1029 expand_expression(expr
);
1030 if (expr
->type
!= EXPR_VALUE
)
1031 expression_error(expr
, "Expected constant expression in %s", where
);
1035 int expand_symbol(struct symbol
*sym
)
1038 struct symbol
*base_type
;
1042 base_type
= sym
->ctype
.base_type
;
1046 retval
= expand_expression(sym
->initializer
);
1047 /* expand the body of the symbol */
1048 if (base_type
->type
== SYM_FN
) {
1049 if (base_type
->stmt
)
1050 expand_statement(base_type
->stmt
);
1055 static void expand_return_expression(struct statement
*stmt
)
1057 expand_expression(stmt
->expression
);
1060 static int expand_if_statement(struct statement
*stmt
)
1062 struct expression
*expr
= stmt
->if_conditional
;
1064 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1067 expand_expression(expr
);
1069 /* This is only valid if nobody jumps into the "dead" side */
1071 /* Simplify constant conditionals without even evaluating the false side */
1072 if (expr
->type
== EXPR_VALUE
) {
1073 struct statement
*simple
;
1074 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1078 stmt
->type
= STMT_NONE
;
1081 expand_statement(simple
);
1083 return SIDE_EFFECTS
;
1086 expand_statement(stmt
->if_true
);
1087 expand_statement(stmt
->if_false
);
1088 return SIDE_EFFECTS
;
1092 * Expanding a compound statement is really just
1093 * about adding up the costs of each individual
1096 * We also collapse a simple compound statement:
1097 * this would trigger for simple inline functions,
1098 * except we would have to check the "return"
1099 * symbol usage. Next time.
1101 static int expand_compound(struct statement
*stmt
)
1103 struct statement
*s
, *last
;
1104 int cost
, statements
;
1107 expand_symbol(stmt
->ret
);
1110 cost
= expand_statement(last
);
1111 statements
= last
!= NULL
;
1112 FOR_EACH_PTR(stmt
->stmts
, s
) {
1115 cost
+= expand_statement(s
);
1116 } END_FOR_EACH_PTR(s
);
1118 if (statements
== 1 && !stmt
->ret
)
1124 static int expand_statement(struct statement
*stmt
)
1129 switch (stmt
->type
) {
1130 case STMT_DECLARATION
: {
1132 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1134 } END_FOR_EACH_PTR(sym
);
1135 return SIDE_EFFECTS
;
1139 expand_return_expression(stmt
);
1140 return SIDE_EFFECTS
;
1142 case STMT_EXPRESSION
:
1143 return expand_expression(stmt
->expression
);
1146 return expand_compound(stmt
);
1149 return expand_if_statement(stmt
);
1152 expand_expression(stmt
->iterator_pre_condition
);
1153 expand_expression(stmt
->iterator_post_condition
);
1154 expand_statement(stmt
->iterator_pre_statement
);
1155 expand_statement(stmt
->iterator_statement
);
1156 expand_statement(stmt
->iterator_post_statement
);
1157 return SIDE_EFFECTS
;
1160 expand_expression(stmt
->switch_expression
);
1161 expand_statement(stmt
->switch_statement
);
1162 return SIDE_EFFECTS
;
1165 expand_const_expression(stmt
->case_expression
, "case statement");
1166 expand_const_expression(stmt
->case_to
, "case statement");
1167 expand_statement(stmt
->case_statement
);
1168 return SIDE_EFFECTS
;
1171 expand_statement(stmt
->label_statement
);
1172 return SIDE_EFFECTS
;
1175 expand_expression(stmt
->goto_expression
);
1176 return SIDE_EFFECTS
;
1181 /* FIXME! Do the asm parameter evaluation! */
1184 expand_expression(stmt
->expression
);
1187 expand_expression(stmt
->range_expression
);
1188 expand_expression(stmt
->range_low
);
1189 expand_expression(stmt
->range_high
);
1192 return SIDE_EFFECTS
;
1195 static inline int bad_integer_constant_expression(struct expression
*expr
)
1197 if (!(expr
->flags
& Int_const_expr
))
1199 if (expr
->taint
& Taint_comma
)
1204 static long long __get_expression_value(struct expression
*expr
, int strict
)
1206 long long value
, mask
;
1207 struct symbol
*ctype
;
1211 ctype
= evaluate_expression(expr
);
1213 expression_error(expr
, "bad constant expression type");
1216 expand_expression(expr
);
1217 if (expr
->type
!= EXPR_VALUE
) {
1219 expression_error(expr
, "bad constant expression");
1222 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1223 expression_error(expr
, "bad integer constant expression");
1227 value
= expr
->value
;
1228 mask
= 1ULL << (ctype
->bit_size
-1);
1231 while (ctype
->type
!= SYM_BASETYPE
)
1232 ctype
= ctype
->ctype
.base_type
;
1233 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1234 value
= value
| mask
| ~(mask
-1);
1239 long long get_expression_value(struct expression
*expr
)
1241 return __get_expression_value(expr
, 0);
1244 long long const_expression_value(struct expression
*expr
)
1246 return __get_expression_value(expr
, 1);
1249 long long get_expression_value_silent(struct expression
*expr
)
1252 return __get_expression_value(expr
, 2);
1255 int is_zero_constant(struct expression
*expr
)
1257 const int saved
= conservative
;
1259 expand_expression(expr
);
1260 conservative
= saved
;
1261 return expr
->type
== EXPR_VALUE
&& !expr
->value
;