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 // 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
)
463 struct ctype c1
= {.base_type
= left
};
464 struct ctype c2
= {.base_type
= right
};
467 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
468 case SPECIAL_NOTEQUAL
:
469 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
471 return left
->bit_size
< right
->bit_size
;
473 return left
->bit_size
> right
->bit_size
;
475 return left
->bit_size
<= right
->bit_size
;
477 return left
->bit_size
>= right
->bit_size
;
482 static int expand_compare(struct expression
*expr
)
484 struct expression
*left
= expr
->left
, *right
= expr
->right
;
487 cost
= expand_expression(left
);
488 cost
+= expand_expression(right
);
491 /* Type comparison? */
492 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
494 expr
->type
= EXPR_VALUE
;
495 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
499 if (simplify_cmp_binop(expr
, left
->ctype
))
501 if (simplify_float_cmp(expr
, left
->ctype
))
507 static int expand_conditional(struct expression
*expr
)
509 struct expression
*cond
= expr
->conditional
;
510 struct expression
*true = expr
->cond_true
;
511 struct expression
*false = expr
->cond_false
;
514 cond_cost
= expand_expression(cond
);
515 if (cond
->type
== EXPR_VALUE
) {
516 unsigned flags
= expr
->flags
;
521 cost
= expand_expression(true);
524 if (expr
->type
== EXPR_VALUE
)
525 expr
->taint
|= cond
->taint
;
529 cost
= expand_expression(true);
530 cost
+= expand_expression(false);
532 if (cost
< SELECT_COST
) {
533 expr
->type
= EXPR_SELECT
;
534 cost
-= BRANCH_COST
- 1;
537 return cost
+ cond_cost
+ BRANCH_COST
;
540 static int expand_assignment(struct expression
*expr
)
542 expand_expression(expr
->left
);
543 expand_expression(expr
->right
);
547 static int expand_addressof(struct expression
*expr
)
549 return expand_expression(expr
->unop
);
553 * Look up a trustable initializer value at the requested offset.
555 * Return NULL if no such value can be found or statically trusted.
557 * FIXME!! We should check that the size is right!
559 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
561 struct expression
*value
;
563 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
565 value
= sym
->initializer
;
568 if (value
->type
== EXPR_INITIALIZER
) {
569 struct expression
*entry
;
570 FOR_EACH_PTR(value
->expr_list
, entry
) {
571 if (entry
->type
!= EXPR_POS
) {
576 if (entry
->init_offset
< offset
)
578 if (entry
->init_offset
> offset
)
580 return entry
->init_expr
;
581 } END_FOR_EACH_PTR(entry
);
587 static int expand_dereference(struct expression
*expr
)
589 struct expression
*unop
= expr
->unop
;
592 expand_expression(unop
);
595 * NOTE! We get a bogus warning right now for some special
596 * cases: apparently I've screwed up the optimization of
597 * a zero-offset dereference, and the ctype is wrong.
599 * Leave the warning in anyway, since this is also a good
600 * test for me to get the type evaluation right..
602 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
603 warning(unop
->pos
, "dereference of noderef expression");
606 * Is it "symbol" or "symbol + offset"?
609 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
610 struct expression
*right
= unop
->right
;
611 if (right
->type
== EXPR_VALUE
) {
612 offset
= right
->value
;
617 if (unop
->type
== EXPR_SYMBOL
) {
618 struct symbol
*sym
= unop
->symbol
;
619 struct expression
*value
= constant_symbol_value(sym
, offset
);
621 /* Const symbol with a constant initializer? */
623 /* FIXME! We should check that the size is right! */
624 if (value
->type
== EXPR_VALUE
) {
625 expr
->type
= EXPR_VALUE
;
626 expr
->value
= value
->value
;
629 } else if (value
->type
== EXPR_FVALUE
) {
630 expr
->type
= EXPR_FVALUE
;
631 expr
->fvalue
= value
->fvalue
;
636 /* Direct symbol dereference? Cheap and safe */
637 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
643 static int simplify_preop(struct expression
*expr
)
645 struct expression
*op
= expr
->unop
;
646 unsigned long long v
, mask
;
648 if (op
->type
!= EXPR_VALUE
)
651 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
656 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
660 case '!': v
= !v
; break;
661 case '~': v
= ~v
; break;
664 mask
= mask
| (mask
-1);
665 expr
->value
= v
& mask
;
666 expr
->type
= EXPR_VALUE
;
667 expr
->taint
= op
->taint
;
672 warning(expr
->pos
, "constant integer operation overflow");
676 static int simplify_float_preop(struct expression
*expr
)
678 struct expression
*op
= expr
->unop
;
681 if (op
->type
!= EXPR_FVALUE
)
686 case '-': v
= -v
; break;
690 expr
->type
= EXPR_FVALUE
;
695 * Unary post-ops: x++ and x--
697 static int expand_postop(struct expression
*expr
)
699 expand_expression(expr
->unop
);
703 static int expand_preop(struct expression
*expr
)
709 return expand_dereference(expr
);
712 return expand_addressof(expr
);
714 case SPECIAL_INCREMENT
:
715 case SPECIAL_DECREMENT
:
717 * From a type evaluation standpoint the preops are
718 * the same as the postops
720 return expand_postop(expr
);
725 cost
= expand_expression(expr
->unop
);
727 if (simplify_preop(expr
))
729 if (simplify_float_preop(expr
))
734 static int expand_arguments(struct expression_list
*head
)
737 struct expression
*expr
;
739 FOR_EACH_PTR (head
, expr
) {
740 cost
+= expand_expression(expr
);
741 } END_FOR_EACH_PTR(expr
);
745 static int expand_cast(struct expression
*expr
)
748 struct expression
*target
= expr
->cast_expression
;
750 cost
= expand_expression(target
);
752 /* Simplify normal integer casts.. */
753 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
754 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
760 /* The arguments are constant if the cost of all of them is zero */
761 int expand_constant_p(struct expression
*expr
, int cost
)
763 expr
->type
= EXPR_VALUE
;
769 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
770 int expand_safe_p(struct expression
*expr
, int cost
)
772 expr
->type
= EXPR_VALUE
;
773 expr
->value
= (cost
< SIDE_EFFECTS
);
779 * expand a call expression with a symbol. This
780 * should expand builtins.
782 static int expand_symbol_call(struct expression
*expr
, int cost
)
784 struct expression
*fn
= expr
->fn
;
785 struct symbol
*ctype
= fn
->ctype
;
787 if (fn
->type
!= EXPR_PREOP
)
790 if (ctype
->op
&& ctype
->op
->expand
)
791 return ctype
->op
->expand(expr
, cost
);
796 static int expand_call(struct expression
*expr
)
800 struct expression
*fn
= expr
->fn
;
802 cost
= expand_arguments(expr
->args
);
805 expression_error(expr
, "function has no type");
808 if (sym
->type
== SYM_NODE
)
809 return expand_symbol_call(expr
, cost
);
814 static int expand_expression_list(struct expression_list
*list
)
817 struct expression
*expr
;
819 FOR_EACH_PTR(list
, expr
) {
820 cost
+= expand_expression(expr
);
821 } END_FOR_EACH_PTR(expr
);
826 * We can simplify nested position expressions if
827 * this is a simple (single) positional expression.
829 static int expand_pos_expression(struct expression
*expr
)
831 struct expression
*nested
= expr
->init_expr
;
832 unsigned long offset
= expr
->init_offset
;
833 int nr
= expr
->init_nr
;
836 switch (nested
->type
) {
838 offset
+= nested
->init_offset
;
840 expr
->init_offset
= offset
;
844 case EXPR_INITIALIZER
: {
845 struct expression
*reuse
= nested
, *entry
;
847 FOR_EACH_PTR(expr
->expr_list
, entry
) {
848 if (entry
->type
== EXPR_POS
) {
849 entry
->init_offset
+= offset
;
853 * This happens rarely, but it can happen
854 * with bitfields that are all at offset
857 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
859 reuse
->type
= EXPR_POS
;
860 reuse
->ctype
= entry
->ctype
;
861 reuse
->init_offset
= offset
;
863 reuse
->init_expr
= entry
;
864 REPLACE_CURRENT_PTR(entry
, reuse
);
867 } END_FOR_EACH_PTR(entry
);
876 return expand_expression(nested
);
879 static unsigned long bit_offset(const struct expression
*expr
)
881 unsigned long offset
= 0;
882 while (expr
->type
== EXPR_POS
) {
883 offset
+= expr
->init_offset
<< 3;
884 expr
= expr
->init_expr
;
886 if (expr
&& expr
->ctype
)
887 offset
+= expr
->ctype
->bit_offset
;
891 static int compare_expressions(const void *_a
, const void *_b
)
893 const struct expression
*a
= _a
;
894 const struct expression
*b
= _b
;
895 unsigned long a_pos
= bit_offset(a
);
896 unsigned long b_pos
= bit_offset(b
);
898 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
901 static void sort_expression_list(struct expression_list
**list
)
903 sort_list((struct ptr_list
**)list
, compare_expressions
);
906 static void verify_nonoverlapping(struct expression_list
**list
)
908 struct expression
*a
= NULL
;
909 struct expression
*b
;
911 FOR_EACH_PTR(*list
, b
) {
912 if (!b
->ctype
|| !b
->ctype
->bit_size
)
914 if (a
&& bit_offset(a
) == bit_offset(b
)) {
915 warning(a
->pos
, "Initializer entry defined twice");
916 info(b
->pos
, " also defined here");
920 } END_FOR_EACH_PTR(b
);
923 static int expand_expression(struct expression
*expr
)
927 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
930 switch (expr
->type
) {
937 return expand_symbol_expression(expr
);
939 return expand_binop(expr
);
942 return expand_logical(expr
);
945 return expand_comma(expr
);
948 return expand_compare(expr
);
950 case EXPR_ASSIGNMENT
:
951 return expand_assignment(expr
);
954 return expand_preop(expr
);
957 return expand_postop(expr
);
960 case EXPR_FORCE_CAST
:
961 case EXPR_IMPLIED_CAST
:
962 return expand_cast(expr
);
965 return expand_call(expr
);
968 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
972 case EXPR_CONDITIONAL
:
973 return expand_conditional(expr
);
975 case EXPR_STATEMENT
: {
976 struct statement
*stmt
= expr
->statement
;
977 int cost
= expand_statement(stmt
);
979 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
980 *expr
= *stmt
->expression
;
987 case EXPR_INITIALIZER
:
988 sort_expression_list(&expr
->expr_list
);
989 verify_nonoverlapping(&expr
->expr_list
);
990 return expand_expression_list(expr
->expr_list
);
992 case EXPR_IDENTIFIER
:
999 return expand_expression(expr
->base
) + 1;
1002 return expand_pos_expression(expr
);
1005 case EXPR_PTRSIZEOF
:
1008 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1011 return SIDE_EFFECTS
;
1014 static void expand_const_expression(struct expression
*expr
, const char *where
)
1017 expand_expression(expr
);
1018 if (expr
->type
!= EXPR_VALUE
)
1019 expression_error(expr
, "Expected constant expression in %s", where
);
1023 int expand_symbol(struct symbol
*sym
)
1026 struct symbol
*base_type
;
1030 base_type
= sym
->ctype
.base_type
;
1034 retval
= expand_expression(sym
->initializer
);
1035 /* expand the body of the symbol */
1036 if (base_type
->type
== SYM_FN
) {
1037 if (base_type
->stmt
)
1038 expand_statement(base_type
->stmt
);
1043 static void expand_return_expression(struct statement
*stmt
)
1045 expand_expression(stmt
->expression
);
1048 static int expand_if_statement(struct statement
*stmt
)
1050 struct expression
*expr
= stmt
->if_conditional
;
1052 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1055 expand_expression(expr
);
1057 /* This is only valid if nobody jumps into the "dead" side */
1059 /* Simplify constant conditionals without even evaluating the false side */
1060 if (expr
->type
== EXPR_VALUE
) {
1061 struct statement
*simple
;
1062 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1066 stmt
->type
= STMT_NONE
;
1069 expand_statement(simple
);
1071 return SIDE_EFFECTS
;
1074 expand_statement(stmt
->if_true
);
1075 expand_statement(stmt
->if_false
);
1076 return SIDE_EFFECTS
;
1080 * Expanding a compound statement is really just
1081 * about adding up the costs of each individual
1084 * We also collapse a simple compound statement:
1085 * this would trigger for simple inline functions,
1086 * except we would have to check the "return"
1087 * symbol usage. Next time.
1089 static int expand_compound(struct statement
*stmt
)
1091 struct statement
*s
, *last
;
1092 int cost
, statements
;
1095 expand_symbol(stmt
->ret
);
1098 cost
= expand_statement(last
);
1099 statements
= last
!= NULL
;
1100 FOR_EACH_PTR(stmt
->stmts
, s
) {
1103 cost
+= expand_statement(s
);
1104 } END_FOR_EACH_PTR(s
);
1106 if (statements
== 1 && !stmt
->ret
)
1112 static int expand_statement(struct statement
*stmt
)
1117 switch (stmt
->type
) {
1118 case STMT_DECLARATION
: {
1120 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1122 } END_FOR_EACH_PTR(sym
);
1123 return SIDE_EFFECTS
;
1127 expand_return_expression(stmt
);
1128 return SIDE_EFFECTS
;
1130 case STMT_EXPRESSION
:
1131 return expand_expression(stmt
->expression
);
1134 return expand_compound(stmt
);
1137 return expand_if_statement(stmt
);
1140 expand_expression(stmt
->iterator_pre_condition
);
1141 expand_expression(stmt
->iterator_post_condition
);
1142 expand_statement(stmt
->iterator_pre_statement
);
1143 expand_statement(stmt
->iterator_statement
);
1144 expand_statement(stmt
->iterator_post_statement
);
1145 return SIDE_EFFECTS
;
1148 expand_expression(stmt
->switch_expression
);
1149 expand_statement(stmt
->switch_statement
);
1150 return SIDE_EFFECTS
;
1153 expand_const_expression(stmt
->case_expression
, "case statement");
1154 expand_const_expression(stmt
->case_to
, "case statement");
1155 expand_statement(stmt
->case_statement
);
1156 return SIDE_EFFECTS
;
1159 expand_statement(stmt
->label_statement
);
1160 return SIDE_EFFECTS
;
1163 expand_expression(stmt
->goto_expression
);
1164 return SIDE_EFFECTS
;
1169 /* FIXME! Do the asm parameter evaluation! */
1172 expand_expression(stmt
->expression
);
1175 expand_expression(stmt
->range_expression
);
1176 expand_expression(stmt
->range_low
);
1177 expand_expression(stmt
->range_high
);
1180 return SIDE_EFFECTS
;
1183 static inline int bad_integer_constant_expression(struct expression
*expr
)
1185 if (!(expr
->flags
& Int_const_expr
))
1187 if (expr
->taint
& Taint_comma
)
1192 static long long __get_expression_value(struct expression
*expr
, int strict
)
1194 long long value
, mask
;
1195 struct symbol
*ctype
;
1199 ctype
= evaluate_expression(expr
);
1201 expression_error(expr
, "bad constant expression type");
1204 expand_expression(expr
);
1205 if (expr
->type
!= EXPR_VALUE
) {
1206 expression_error(expr
, "bad constant expression");
1209 if (strict
&& bad_integer_constant_expression(expr
)) {
1210 expression_error(expr
, "bad integer constant expression");
1214 value
= expr
->value
;
1215 mask
= 1ULL << (ctype
->bit_size
-1);
1218 while (ctype
->type
!= SYM_BASETYPE
)
1219 ctype
= ctype
->ctype
.base_type
;
1220 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1221 value
= value
| mask
| ~(mask
-1);
1226 long long get_expression_value(struct expression
*expr
)
1228 return __get_expression_value(expr
, 0);
1231 long long const_expression_value(struct expression
*expr
)
1233 return __get_expression_value(expr
, 1);
1236 int is_zero_constant(struct expression
*expr
)
1238 const int saved
= conservative
;
1240 expand_expression(expr
);
1241 conservative
= saved
;
1242 return expr
->type
== EXPR_VALUE
&& !expr
->value
;