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
) {
44 if (Wundefined_preprocessor
)
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 // Truncate it to the new size
96 signmask
= 1ULL << (new_size
-1);
97 mask
= signmask
| (signmask
-1);
98 expr
->value
= value
& mask
;
100 // Stop here unless checking for truncation
101 if (!Wcast_truncate
|| conservative
)
104 // Check if we dropped any bits..
105 oldsignmask
= 1ULL << (old_size
-1);
106 oldmask
= oldsignmask
| (oldsignmask
-1);
107 dropped
= oldmask
& ~mask
;
109 // OK if the bits were (and still are) purely sign bits
110 if (value
& dropped
) {
111 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
112 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
119 if (newtype
->ctype
.base_type
!= &fp_type
) {
120 value
= (long long)old
->fvalue
;
121 expr
->type
= EXPR_VALUE
;
126 if (oldtype
->ctype
.base_type
!= &fp_type
)
127 expr
->fvalue
= (long double)get_longlong(old
);
129 expr
->fvalue
= old
->value
;
131 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
)) {
132 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
133 expr
->fvalue
= (double)expr
->fvalue
;
135 expr
->fvalue
= (float)expr
->fvalue
;
137 expr
->type
= EXPR_FVALUE
;
140 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
142 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
143 count
&= ctype
->bit_size
-1;
148 * CAREFUL! We need to get the size and sign of the
151 #define CONVERT(op,s) (((op)<<1)+(s))
152 #define SIGNED(op) CONVERT(op, 1)
153 #define UNSIGNED(op) CONVERT(op, 0)
154 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
156 struct expression
*left
= expr
->left
, *right
= expr
->right
;
157 unsigned long long v
, l
, r
, mask
;
158 signed long long sl
, sr
;
161 if (right
->type
!= EXPR_VALUE
)
164 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
165 if (r
>= ctype
->bit_size
) {
168 r
= check_shift_count(expr
, ctype
, r
);
172 if (left
->type
!= EXPR_VALUE
)
174 l
= left
->value
; r
= right
->value
;
175 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
176 mask
= 1ULL << (ctype
->bit_size
-1);
178 if (is_signed
&& (sl
& mask
))
180 if (is_signed
&& (sr
& mask
))
183 switch (CONVERT(expr
->op
,is_signed
)) {
220 if (l
== mask
&& sr
== -1)
241 case SIGNED(SPECIAL_LEFTSHIFT
):
242 case UNSIGNED(SPECIAL_LEFTSHIFT
):
246 case SIGNED(SPECIAL_RIGHTSHIFT
):
250 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
257 mask
= mask
| (mask
-1);
258 expr
->value
= v
& mask
;
259 expr
->type
= EXPR_VALUE
;
260 expr
->taint
= left
->taint
| right
->taint
;
264 warning(expr
->pos
, "division by zero");
268 warning(expr
->pos
, "constant integer operation overflow");
272 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
274 struct expression
*left
= expr
->left
, *right
= expr
->right
;
275 unsigned long long l
, r
, mask
;
276 signed long long sl
, sr
;
278 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
280 l
= left
->value
; r
= right
->value
;
281 mask
= 1ULL << (ctype
->bit_size
-1);
288 case '<': expr
->value
= sl
< sr
; break;
289 case '>': expr
->value
= sl
> sr
; break;
290 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
291 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
292 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
293 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
294 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
295 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
296 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
297 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
299 expr
->type
= EXPR_VALUE
;
300 expr
->taint
= left
->taint
| right
->taint
;
304 static int simplify_float_binop(struct expression
*expr
)
306 struct expression
*left
= expr
->left
, *right
= expr
->right
;
307 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
308 long double l
, r
, res
;
310 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
316 if (mod
& MOD_LONGLONG
) {
318 case '+': res
= l
+ r
; break;
319 case '-': res
= l
- r
; break;
320 case '*': res
= l
* r
; break;
321 case '/': if (!r
) goto Div
;
325 } else if (mod
& MOD_LONG
) {
327 case '+': res
= (double) l
+ (double) r
; break;
328 case '-': res
= (double) l
- (double) r
; break;
329 case '*': res
= (double) l
* (double) r
; break;
330 case '/': if (!r
) goto Div
;
331 res
= (double) l
/ (double) r
; break;
336 case '+': res
= (float)l
+ (float)r
; break;
337 case '-': res
= (float)l
- (float)r
; break;
338 case '*': res
= (float)l
* (float)r
; break;
339 case '/': if (!r
) goto Div
;
340 res
= (float)l
/ (float)r
; break;
344 expr
->type
= EXPR_FVALUE
;
349 warning(expr
->pos
, "division by zero");
353 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
355 struct expression
*left
= expr
->left
, *right
= expr
->right
;
358 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
364 case '<': expr
->value
= l
< r
; break;
365 case '>': expr
->value
= l
> r
; break;
366 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
367 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
368 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
369 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
371 expr
->type
= EXPR_VALUE
;
376 static int expand_binop(struct expression
*expr
)
380 cost
= expand_expression(expr
->left
);
381 cost
+= expand_expression(expr
->right
);
382 if (simplify_int_binop(expr
, expr
->ctype
))
384 if (simplify_float_binop(expr
))
389 static int expand_logical(struct expression
*expr
)
391 struct expression
*left
= expr
->left
;
392 struct expression
*right
;
395 /* Do immediate short-circuiting ... */
396 cost
= expand_expression(left
);
397 if (left
->type
== EXPR_VALUE
) {
398 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
400 expr
->type
= EXPR_VALUE
;
402 expr
->taint
= left
->taint
;
407 expr
->type
= EXPR_VALUE
;
409 expr
->taint
= left
->taint
;
416 rcost
= expand_expression(right
);
417 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
419 * We know the left value doesn't matter, since
420 * otherwise we would have short-circuited it..
422 expr
->type
= EXPR_VALUE
;
423 expr
->value
= right
->value
!= 0;
424 expr
->taint
= left
->taint
| right
->taint
;
429 * If the right side is safe and cheaper than a branch,
430 * just avoid the branch and turn it into a regular binop
433 if (rcost
< BRANCH_COST
) {
434 expr
->type
= EXPR_BINOP
;
435 rcost
-= BRANCH_COST
- 1;
438 return cost
+ BRANCH_COST
+ rcost
;
441 static int expand_comma(struct expression
*expr
)
445 cost
= expand_expression(expr
->left
);
446 cost
+= expand_expression(expr
->right
);
447 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
448 unsigned flags
= expr
->flags
;
450 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
451 *expr
= *expr
->right
;
453 if (expr
->type
== EXPR_VALUE
)
454 expr
->taint
|= Taint_comma
| taint
;
459 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
461 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
465 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
466 case SPECIAL_NOTEQUAL
:
467 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
469 return left
->bit_size
< right
->bit_size
;
471 return left
->bit_size
> right
->bit_size
;
473 return left
->bit_size
<= right
->bit_size
;
475 return left
->bit_size
>= right
->bit_size
;
480 static int expand_compare(struct expression
*expr
)
482 struct expression
*left
= expr
->left
, *right
= expr
->right
;
485 cost
= expand_expression(left
);
486 cost
+= expand_expression(right
);
489 /* Type comparison? */
490 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
492 expr
->type
= EXPR_VALUE
;
493 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
497 if (simplify_cmp_binop(expr
, left
->ctype
))
499 if (simplify_float_cmp(expr
, left
->ctype
))
505 static int expand_conditional(struct expression
*expr
)
507 struct expression
*cond
= expr
->conditional
;
508 struct expression
*true = expr
->cond_true
;
509 struct expression
*false = expr
->cond_false
;
512 cond_cost
= expand_expression(cond
);
513 if (cond
->type
== EXPR_VALUE
) {
514 unsigned flags
= expr
->flags
;
519 cost
= expand_expression(true);
522 if (expr
->type
== EXPR_VALUE
)
523 expr
->taint
|= cond
->taint
;
527 cost
= expand_expression(true);
528 cost
+= expand_expression(false);
530 if (cost
< SELECT_COST
) {
531 expr
->type
= EXPR_SELECT
;
532 cost
-= BRANCH_COST
- 1;
535 return cost
+ cond_cost
+ BRANCH_COST
;
538 static int expand_assignment(struct expression
*expr
)
540 expand_expression(expr
->left
);
541 expand_expression(expr
->right
);
545 static int expand_addressof(struct expression
*expr
)
547 return expand_expression(expr
->unop
);
551 * Look up a trustable initializer value at the requested offset.
553 * Return NULL if no such value can be found or statically trusted.
555 * FIXME!! We should check that the size is right!
557 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
559 struct expression
*value
;
561 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
563 value
= sym
->initializer
;
566 if (value
->type
== EXPR_INITIALIZER
) {
567 struct expression
*entry
;
568 FOR_EACH_PTR(value
->expr_list
, entry
) {
569 if (entry
->type
!= EXPR_POS
) {
574 if (entry
->init_offset
< offset
)
576 if (entry
->init_offset
> offset
)
578 return entry
->init_expr
;
579 } END_FOR_EACH_PTR(entry
);
585 static int expand_dereference(struct expression
*expr
)
587 struct expression
*unop
= expr
->unop
;
590 expand_expression(unop
);
593 * NOTE! We get a bogus warning right now for some special
594 * cases: apparently I've screwed up the optimization of
595 * a zero-offset dereference, and the ctype is wrong.
597 * Leave the warning in anyway, since this is also a good
598 * test for me to get the type evaluation right..
600 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
601 warning(unop
->pos
, "dereference of noderef expression");
604 * Is it "symbol" or "symbol + offset"?
607 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
608 struct expression
*right
= unop
->right
;
609 if (right
->type
== EXPR_VALUE
) {
610 offset
= right
->value
;
615 if (unop
->type
== EXPR_SYMBOL
) {
616 struct symbol
*sym
= unop
->symbol
;
617 struct expression
*value
= constant_symbol_value(sym
, offset
);
619 /* Const symbol with a constant initializer? */
621 /* FIXME! We should check that the size is right! */
622 if (value
->type
== EXPR_VALUE
) {
623 expr
->type
= EXPR_VALUE
;
624 expr
->value
= value
->value
;
627 } else if (value
->type
== EXPR_FVALUE
) {
628 expr
->type
= EXPR_FVALUE
;
629 expr
->fvalue
= value
->fvalue
;
634 /* Direct symbol dereference? Cheap and safe */
635 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
641 static int simplify_preop(struct expression
*expr
)
643 struct expression
*op
= expr
->unop
;
644 unsigned long long v
, mask
;
646 if (op
->type
!= EXPR_VALUE
)
649 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
654 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
658 case '!': v
= !v
; break;
659 case '~': v
= ~v
; break;
662 mask
= mask
| (mask
-1);
663 expr
->value
= v
& mask
;
664 expr
->type
= EXPR_VALUE
;
665 expr
->taint
= op
->taint
;
670 warning(expr
->pos
, "constant integer operation overflow");
674 static int simplify_float_preop(struct expression
*expr
)
676 struct expression
*op
= expr
->unop
;
679 if (op
->type
!= EXPR_FVALUE
)
684 case '-': v
= -v
; break;
688 expr
->type
= EXPR_FVALUE
;
693 * Unary post-ops: x++ and x--
695 static int expand_postop(struct expression
*expr
)
697 expand_expression(expr
->unop
);
701 static int expand_preop(struct expression
*expr
)
707 return expand_dereference(expr
);
710 return expand_addressof(expr
);
712 case SPECIAL_INCREMENT
:
713 case SPECIAL_DECREMENT
:
715 * From a type evaluation standpoint the preops are
716 * the same as the postops
718 return expand_postop(expr
);
723 cost
= expand_expression(expr
->unop
);
725 if (simplify_preop(expr
))
727 if (simplify_float_preop(expr
))
732 static int expand_arguments(struct expression_list
*head
)
735 struct expression
*expr
;
737 FOR_EACH_PTR (head
, expr
) {
738 cost
+= expand_expression(expr
);
739 } END_FOR_EACH_PTR(expr
);
743 static int expand_cast(struct expression
*expr
)
746 struct expression
*target
= expr
->cast_expression
;
748 cost
= expand_expression(target
);
750 /* Simplify normal integer casts.. */
751 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
752 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
758 /* The arguments are constant if the cost of all of them is zero */
759 int expand_constant_p(struct expression
*expr
, int cost
)
761 expr
->type
= EXPR_VALUE
;
767 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
768 int expand_safe_p(struct expression
*expr
, int cost
)
770 expr
->type
= EXPR_VALUE
;
771 expr
->value
= (cost
< SIDE_EFFECTS
);
777 * expand a call expression with a symbol. This
778 * should expand builtins.
780 static int expand_symbol_call(struct expression
*expr
, int cost
)
782 struct expression
*fn
= expr
->fn
;
783 struct symbol
*ctype
= fn
->ctype
;
785 if (fn
->type
!= EXPR_PREOP
)
788 if (ctype
->op
&& ctype
->op
->expand
)
789 return ctype
->op
->expand(expr
, cost
);
794 static int expand_call(struct expression
*expr
)
798 struct expression
*fn
= expr
->fn
;
800 cost
= expand_arguments(expr
->args
);
803 expression_error(expr
, "function has no type");
806 if (sym
->type
== SYM_NODE
)
807 return expand_symbol_call(expr
, cost
);
812 static int expand_expression_list(struct expression_list
*list
)
815 struct expression
*expr
;
817 FOR_EACH_PTR(list
, expr
) {
818 cost
+= expand_expression(expr
);
819 } END_FOR_EACH_PTR(expr
);
824 * We can simplify nested position expressions if
825 * this is a simple (single) positional expression.
827 static int expand_pos_expression(struct expression
*expr
)
829 struct expression
*nested
= expr
->init_expr
;
830 unsigned long offset
= expr
->init_offset
;
831 int nr
= expr
->init_nr
;
834 switch (nested
->type
) {
836 offset
+= nested
->init_offset
;
838 expr
->init_offset
= offset
;
842 case EXPR_INITIALIZER
: {
843 struct expression
*reuse
= nested
, *entry
;
845 FOR_EACH_PTR(expr
->expr_list
, entry
) {
846 if (entry
->type
== EXPR_POS
) {
847 entry
->init_offset
+= offset
;
851 * This happens rarely, but it can happen
852 * with bitfields that are all at offset
855 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
857 reuse
->type
= EXPR_POS
;
858 reuse
->ctype
= entry
->ctype
;
859 reuse
->init_offset
= offset
;
861 reuse
->init_expr
= entry
;
862 REPLACE_CURRENT_PTR(entry
, reuse
);
865 } END_FOR_EACH_PTR(entry
);
874 return expand_expression(nested
);
877 static unsigned long bit_offset(const struct expression
*expr
)
879 unsigned long offset
= 0;
880 while (expr
->type
== EXPR_POS
) {
881 offset
+= expr
->init_offset
<< 3;
882 expr
= expr
->init_expr
;
884 if (expr
&& expr
->ctype
)
885 offset
+= expr
->ctype
->bit_offset
;
889 static int compare_expressions(const void *_a
, const void *_b
)
891 const struct expression
*a
= _a
;
892 const struct expression
*b
= _b
;
893 unsigned long a_pos
= bit_offset(a
);
894 unsigned long b_pos
= bit_offset(b
);
896 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
899 static void sort_expression_list(struct expression_list
**list
)
901 sort_list((struct ptr_list
**)list
, compare_expressions
);
904 static void verify_nonoverlapping(struct expression_list
**list
)
906 struct expression
*a
= NULL
;
907 struct expression
*b
;
909 FOR_EACH_PTR(*list
, b
) {
910 if (!b
->ctype
|| !b
->ctype
->bit_size
)
912 if (a
&& bit_offset(a
) == bit_offset(b
)) {
913 sparse_error(a
->pos
, "Initializer entry defined twice");
914 info(b
->pos
, " also defined here");
918 } END_FOR_EACH_PTR(b
);
921 static int expand_expression(struct expression
*expr
)
925 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
928 switch (expr
->type
) {
935 return expand_symbol_expression(expr
);
937 return expand_binop(expr
);
940 return expand_logical(expr
);
943 return expand_comma(expr
);
946 return expand_compare(expr
);
948 case EXPR_ASSIGNMENT
:
949 return expand_assignment(expr
);
952 return expand_preop(expr
);
955 return expand_postop(expr
);
958 case EXPR_FORCE_CAST
:
959 case EXPR_IMPLIED_CAST
:
960 return expand_cast(expr
);
963 return expand_call(expr
);
966 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
970 case EXPR_CONDITIONAL
:
971 return expand_conditional(expr
);
973 case EXPR_STATEMENT
: {
974 struct statement
*stmt
= expr
->statement
;
975 int cost
= expand_statement(stmt
);
977 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
978 *expr
= *stmt
->expression
;
985 case EXPR_INITIALIZER
:
986 sort_expression_list(&expr
->expr_list
);
987 verify_nonoverlapping(&expr
->expr_list
);
988 return expand_expression_list(expr
->expr_list
);
990 case EXPR_IDENTIFIER
:
997 return expand_expression(expr
->base
) + 1;
1000 return expand_pos_expression(expr
);
1003 case EXPR_PTRSIZEOF
:
1006 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1009 return SIDE_EFFECTS
;
1012 static void expand_const_expression(struct expression
*expr
, const char *where
)
1015 expand_expression(expr
);
1016 if (expr
->type
!= EXPR_VALUE
)
1017 expression_error(expr
, "Expected constant expression in %s", where
);
1021 int expand_symbol(struct symbol
*sym
)
1024 struct symbol
*base_type
;
1028 base_type
= sym
->ctype
.base_type
;
1032 retval
= expand_expression(sym
->initializer
);
1033 /* expand the body of the symbol */
1034 if (base_type
->type
== SYM_FN
) {
1035 if (base_type
->stmt
)
1036 expand_statement(base_type
->stmt
);
1041 static void expand_return_expression(struct statement
*stmt
)
1043 expand_expression(stmt
->expression
);
1046 static int expand_if_statement(struct statement
*stmt
)
1048 struct expression
*expr
= stmt
->if_conditional
;
1050 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1053 expand_expression(expr
);
1055 /* This is only valid if nobody jumps into the "dead" side */
1057 /* Simplify constant conditionals without even evaluating the false side */
1058 if (expr
->type
== EXPR_VALUE
) {
1059 struct statement
*simple
;
1060 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1064 stmt
->type
= STMT_NONE
;
1067 expand_statement(simple
);
1069 return SIDE_EFFECTS
;
1072 expand_statement(stmt
->if_true
);
1073 expand_statement(stmt
->if_false
);
1074 return SIDE_EFFECTS
;
1078 * Expanding a compound statement is really just
1079 * about adding up the costs of each individual
1082 * We also collapse a simple compound statement:
1083 * this would trigger for simple inline functions,
1084 * except we would have to check the "return"
1085 * symbol usage. Next time.
1087 static int expand_compound(struct statement
*stmt
)
1089 struct statement
*s
, *last
;
1090 int cost
, statements
;
1093 expand_symbol(stmt
->ret
);
1096 cost
= expand_statement(last
);
1097 statements
= last
!= NULL
;
1098 FOR_EACH_PTR(stmt
->stmts
, s
) {
1101 cost
+= expand_statement(s
);
1102 } END_FOR_EACH_PTR(s
);
1104 if (statements
== 1 && !stmt
->ret
)
1110 static int expand_statement(struct statement
*stmt
)
1115 switch (stmt
->type
) {
1116 case STMT_DECLARATION
: {
1118 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1120 } END_FOR_EACH_PTR(sym
);
1121 return SIDE_EFFECTS
;
1125 expand_return_expression(stmt
);
1126 return SIDE_EFFECTS
;
1128 case STMT_EXPRESSION
:
1129 return expand_expression(stmt
->expression
);
1132 return expand_compound(stmt
);
1135 return expand_if_statement(stmt
);
1138 expand_expression(stmt
->iterator_pre_condition
);
1139 expand_expression(stmt
->iterator_post_condition
);
1140 expand_statement(stmt
->iterator_pre_statement
);
1141 expand_statement(stmt
->iterator_statement
);
1142 expand_statement(stmt
->iterator_post_statement
);
1143 return SIDE_EFFECTS
;
1146 expand_expression(stmt
->switch_expression
);
1147 expand_statement(stmt
->switch_statement
);
1148 return SIDE_EFFECTS
;
1151 expand_const_expression(stmt
->case_expression
, "case statement");
1152 expand_const_expression(stmt
->case_to
, "case statement");
1153 expand_statement(stmt
->case_statement
);
1154 return SIDE_EFFECTS
;
1157 expand_statement(stmt
->label_statement
);
1158 return SIDE_EFFECTS
;
1161 expand_expression(stmt
->goto_expression
);
1162 return SIDE_EFFECTS
;
1167 /* FIXME! Do the asm parameter evaluation! */
1170 expand_expression(stmt
->expression
);
1173 expand_expression(stmt
->range_expression
);
1174 expand_expression(stmt
->range_low
);
1175 expand_expression(stmt
->range_high
);
1178 return SIDE_EFFECTS
;
1181 static inline int bad_integer_constant_expression(struct expression
*expr
)
1183 if (!(expr
->flags
& Int_const_expr
))
1185 if (expr
->taint
& Taint_comma
)
1190 static long long __get_expression_value(struct expression
*expr
, int strict
)
1192 long long value
, mask
;
1193 struct symbol
*ctype
;
1197 ctype
= evaluate_expression(expr
);
1199 expression_error(expr
, "bad constant expression type");
1202 expand_expression(expr
);
1203 if (expr
->type
!= EXPR_VALUE
) {
1204 expression_error(expr
, "bad constant expression");
1207 if (strict
&& bad_integer_constant_expression(expr
)) {
1208 expression_error(expr
, "bad integer constant expression");
1212 value
= expr
->value
;
1213 mask
= 1ULL << (ctype
->bit_size
-1);
1216 while (ctype
->type
!= SYM_BASETYPE
)
1217 ctype
= ctype
->ctype
.base_type
;
1218 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1219 value
= value
| mask
| ~(mask
-1);
1224 long long get_expression_value(struct expression
*expr
)
1226 return __get_expression_value(expr
, 0);
1229 long long const_expression_value(struct expression
*expr
)
1231 return __get_expression_value(expr
, 1);
1234 int is_zero_constant(struct expression
*expr
)
1236 int saved
= conservative
;
1238 expand_expression(expr
);
1239 conservative
= saved
;
1240 return expr
->type
== EXPR_VALUE
&& !expr
->value
;