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 (!is_float_type(newtype
)) {
120 value
= (long long)old
->fvalue
;
121 expr
->type
= EXPR_VALUE
;
126 if (!is_float_type(oldtype
))
127 expr
->fvalue
= (long double)get_longlong(old
);
129 expr
->fvalue
= old
->fvalue
;
131 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
132 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
133 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
134 expr
->fvalue
= (double)expr
->fvalue
;
136 expr
->fvalue
= (float)expr
->fvalue
;
138 expr
->type
= EXPR_FVALUE
;
141 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
143 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
144 count
&= ctype
->bit_size
-1;
149 * CAREFUL! We need to get the size and sign of the
152 #define CONVERT(op,s) (((op)<<1)+(s))
153 #define SIGNED(op) CONVERT(op, 1)
154 #define UNSIGNED(op) CONVERT(op, 0)
155 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
157 struct expression
*left
= expr
->left
, *right
= expr
->right
;
158 unsigned long long v
, l
, r
, mask
;
159 signed long long sl
, sr
;
162 if (right
->type
!= EXPR_VALUE
)
165 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
166 if (r
>= ctype
->bit_size
) {
169 r
= check_shift_count(expr
, ctype
, r
);
173 if (left
->type
!= EXPR_VALUE
)
175 l
= left
->value
; r
= right
->value
;
176 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
177 mask
= 1ULL << (ctype
->bit_size
-1);
179 if (is_signed
&& (sl
& mask
))
181 if (is_signed
&& (sr
& mask
))
184 switch (CONVERT(expr
->op
,is_signed
)) {
221 if (l
== mask
&& sr
== -1)
242 case SIGNED(SPECIAL_LEFTSHIFT
):
243 case UNSIGNED(SPECIAL_LEFTSHIFT
):
247 case SIGNED(SPECIAL_RIGHTSHIFT
):
251 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
258 mask
= mask
| (mask
-1);
259 expr
->value
= v
& mask
;
260 expr
->type
= EXPR_VALUE
;
261 expr
->taint
= left
->taint
| right
->taint
;
265 warning(expr
->pos
, "division by zero");
269 warning(expr
->pos
, "constant integer operation overflow");
273 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
275 struct expression
*left
= expr
->left
, *right
= expr
->right
;
276 unsigned long long l
, r
, mask
;
277 signed long long sl
, sr
;
279 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
281 l
= left
->value
; r
= right
->value
;
282 mask
= 1ULL << (ctype
->bit_size
-1);
289 case '<': expr
->value
= sl
< sr
; break;
290 case '>': expr
->value
= sl
> sr
; break;
291 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
292 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
293 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
294 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
295 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
296 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
297 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
298 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
300 expr
->type
= EXPR_VALUE
;
301 expr
->taint
= left
->taint
| right
->taint
;
305 static int simplify_float_binop(struct expression
*expr
)
307 struct expression
*left
= expr
->left
, *right
= expr
->right
;
308 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
309 long double l
, r
, res
;
311 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
317 if (mod
& MOD_LONGLONG
) {
319 case '+': res
= l
+ r
; break;
320 case '-': res
= l
- r
; break;
321 case '*': res
= l
* r
; break;
322 case '/': if (!r
) goto Div
;
326 } else if (mod
& MOD_LONG
) {
328 case '+': res
= (double) l
+ (double) r
; break;
329 case '-': res
= (double) l
- (double) r
; break;
330 case '*': res
= (double) l
* (double) r
; break;
331 case '/': if (!r
) goto Div
;
332 res
= (double) l
/ (double) r
; break;
337 case '+': res
= (float)l
+ (float)r
; break;
338 case '-': res
= (float)l
- (float)r
; break;
339 case '*': res
= (float)l
* (float)r
; break;
340 case '/': if (!r
) goto Div
;
341 res
= (float)l
/ (float)r
; break;
345 expr
->type
= EXPR_FVALUE
;
350 warning(expr
->pos
, "division by zero");
354 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
356 struct expression
*left
= expr
->left
, *right
= expr
->right
;
359 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
365 case '<': expr
->value
= l
< r
; break;
366 case '>': expr
->value
= l
> r
; break;
367 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
368 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
369 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
370 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
372 expr
->type
= EXPR_VALUE
;
377 static int expand_binop(struct expression
*expr
)
381 cost
= expand_expression(expr
->left
);
382 cost
+= expand_expression(expr
->right
);
383 if (simplify_int_binop(expr
, expr
->ctype
))
385 if (simplify_float_binop(expr
))
390 static int expand_logical(struct expression
*expr
)
392 struct expression
*left
= expr
->left
;
393 struct expression
*right
;
396 /* Do immediate short-circuiting ... */
397 cost
= expand_expression(left
);
398 if (left
->type
== EXPR_VALUE
) {
399 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
401 expr
->type
= EXPR_VALUE
;
403 expr
->taint
= left
->taint
;
408 expr
->type
= EXPR_VALUE
;
410 expr
->taint
= left
->taint
;
417 rcost
= expand_expression(right
);
418 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
420 * We know the left value doesn't matter, since
421 * otherwise we would have short-circuited it..
423 expr
->type
= EXPR_VALUE
;
424 expr
->value
= right
->value
!= 0;
425 expr
->taint
= left
->taint
| right
->taint
;
430 * If the right side is safe and cheaper than a branch,
431 * just avoid the branch and turn it into a regular binop
434 if (rcost
< BRANCH_COST
) {
435 expr
->type
= EXPR_BINOP
;
436 rcost
-= BRANCH_COST
- 1;
439 return cost
+ BRANCH_COST
+ rcost
;
442 static int expand_comma(struct expression
*expr
)
446 cost
= expand_expression(expr
->left
);
447 cost
+= expand_expression(expr
->right
);
448 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
449 unsigned flags
= expr
->flags
;
451 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
452 *expr
= *expr
->right
;
454 if (expr
->type
== EXPR_VALUE
)
455 expr
->taint
|= Taint_comma
| taint
;
460 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
462 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
464 struct ctype c1
= {.base_type
= left
};
465 struct ctype c2
= {.base_type
= right
};
468 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
469 case SPECIAL_NOTEQUAL
:
470 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
472 return left
->bit_size
< right
->bit_size
;
474 return left
->bit_size
> right
->bit_size
;
476 return left
->bit_size
<= right
->bit_size
;
478 return left
->bit_size
>= right
->bit_size
;
483 static int expand_compare(struct expression
*expr
)
485 struct expression
*left
= expr
->left
, *right
= expr
->right
;
488 cost
= expand_expression(left
);
489 cost
+= expand_expression(right
);
492 /* Type comparison? */
493 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
495 expr
->type
= EXPR_VALUE
;
496 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
500 if (simplify_cmp_binop(expr
, left
->ctype
))
502 if (simplify_float_cmp(expr
, left
->ctype
))
508 static int expand_conditional(struct expression
*expr
)
510 struct expression
*cond
= expr
->conditional
;
511 struct expression
*true = expr
->cond_true
;
512 struct expression
*false = expr
->cond_false
;
515 cond_cost
= expand_expression(cond
);
516 if (cond
->type
== EXPR_VALUE
) {
517 unsigned flags
= expr
->flags
;
522 cost
= expand_expression(true);
525 if (expr
->type
== EXPR_VALUE
)
526 expr
->taint
|= cond
->taint
;
530 cost
= expand_expression(true);
531 cost
+= expand_expression(false);
533 if (cost
< SELECT_COST
) {
534 expr
->type
= EXPR_SELECT
;
535 cost
-= BRANCH_COST
- 1;
538 return cost
+ cond_cost
+ BRANCH_COST
;
541 static int expand_assignment(struct expression
*expr
)
543 expand_expression(expr
->left
);
544 expand_expression(expr
->right
);
548 static int expand_addressof(struct expression
*expr
)
550 return expand_expression(expr
->unop
);
554 * Look up a trustable initializer value at the requested offset.
556 * Return NULL if no such value can be found or statically trusted.
558 * FIXME!! We should check that the size is right!
560 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
562 struct expression
*value
;
564 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
566 value
= sym
->initializer
;
569 if (value
->type
== EXPR_INITIALIZER
) {
570 struct expression
*entry
;
571 FOR_EACH_PTR(value
->expr_list
, entry
) {
572 if (entry
->type
!= EXPR_POS
) {
577 if (entry
->init_offset
< offset
)
579 if (entry
->init_offset
> offset
)
581 return entry
->init_expr
;
582 } END_FOR_EACH_PTR(entry
);
588 static int expand_dereference(struct expression
*expr
)
590 struct expression
*unop
= expr
->unop
;
593 expand_expression(unop
);
596 * NOTE! We get a bogus warning right now for some special
597 * cases: apparently I've screwed up the optimization of
598 * a zero-offset dereference, and the ctype is wrong.
600 * Leave the warning in anyway, since this is also a good
601 * test for me to get the type evaluation right..
603 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
604 warning(unop
->pos
, "dereference of noderef expression");
607 * Is it "symbol" or "symbol + offset"?
610 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
611 struct expression
*right
= unop
->right
;
612 if (right
->type
== EXPR_VALUE
) {
613 offset
= right
->value
;
618 if (unop
->type
== EXPR_SYMBOL
) {
619 struct symbol
*sym
= unop
->symbol
;
620 struct expression
*value
= constant_symbol_value(sym
, offset
);
622 /* Const symbol with a constant initializer? */
624 /* FIXME! We should check that the size is right! */
625 if (value
->type
== EXPR_VALUE
) {
626 expr
->type
= EXPR_VALUE
;
627 expr
->value
= value
->value
;
630 } else if (value
->type
== EXPR_FVALUE
) {
631 expr
->type
= EXPR_FVALUE
;
632 expr
->fvalue
= value
->fvalue
;
637 /* Direct symbol dereference? Cheap and safe */
638 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
644 static int simplify_preop(struct expression
*expr
)
646 struct expression
*op
= expr
->unop
;
647 unsigned long long v
, mask
;
649 if (op
->type
!= EXPR_VALUE
)
652 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
657 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
661 case '!': v
= !v
; break;
662 case '~': v
= ~v
; break;
665 mask
= mask
| (mask
-1);
666 expr
->value
= v
& mask
;
667 expr
->type
= EXPR_VALUE
;
668 expr
->taint
= op
->taint
;
673 warning(expr
->pos
, "constant integer operation overflow");
677 static int simplify_float_preop(struct expression
*expr
)
679 struct expression
*op
= expr
->unop
;
682 if (op
->type
!= EXPR_FVALUE
)
687 case '-': v
= -v
; break;
691 expr
->type
= EXPR_FVALUE
;
696 * Unary post-ops: x++ and x--
698 static int expand_postop(struct expression
*expr
)
700 expand_expression(expr
->unop
);
704 static int expand_preop(struct expression
*expr
)
710 return expand_dereference(expr
);
713 return expand_addressof(expr
);
715 case SPECIAL_INCREMENT
:
716 case SPECIAL_DECREMENT
:
718 * From a type evaluation standpoint the preops are
719 * the same as the postops
721 return expand_postop(expr
);
726 cost
= expand_expression(expr
->unop
);
728 if (simplify_preop(expr
))
730 if (simplify_float_preop(expr
))
735 static int expand_arguments(struct expression_list
*head
)
738 struct expression
*expr
;
740 FOR_EACH_PTR (head
, expr
) {
741 cost
+= expand_expression(expr
);
742 } END_FOR_EACH_PTR(expr
);
746 static int expand_cast(struct expression
*expr
)
749 struct expression
*target
= expr
->cast_expression
;
751 cost
= expand_expression(target
);
753 /* Simplify normal integer casts.. */
754 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
755 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
761 /* The arguments are constant if the cost of all of them is zero */
762 int expand_constant_p(struct expression
*expr
, int cost
)
764 expr
->type
= EXPR_VALUE
;
770 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
771 int expand_safe_p(struct expression
*expr
, int cost
)
773 expr
->type
= EXPR_VALUE
;
774 expr
->value
= (cost
< SIDE_EFFECTS
);
780 * expand a call expression with a symbol. This
781 * should expand builtins.
783 static int expand_symbol_call(struct expression
*expr
, int cost
)
785 struct expression
*fn
= expr
->fn
;
786 struct symbol
*ctype
= fn
->ctype
;
788 if (fn
->type
!= EXPR_PREOP
)
791 if (ctype
->op
&& ctype
->op
->expand
)
792 return ctype
->op
->expand(expr
, cost
);
797 static int expand_call(struct expression
*expr
)
801 struct expression
*fn
= expr
->fn
;
803 cost
= expand_arguments(expr
->args
);
806 expression_error(expr
, "function has no type");
809 if (sym
->type
== SYM_NODE
)
810 return expand_symbol_call(expr
, cost
);
815 static int expand_expression_list(struct expression_list
*list
)
818 struct expression
*expr
;
820 FOR_EACH_PTR(list
, expr
) {
821 cost
+= expand_expression(expr
);
822 } END_FOR_EACH_PTR(expr
);
827 * We can simplify nested position expressions if
828 * this is a simple (single) positional expression.
830 static int expand_pos_expression(struct expression
*expr
)
832 struct expression
*nested
= expr
->init_expr
;
833 unsigned long offset
= expr
->init_offset
;
834 int nr
= expr
->init_nr
;
837 switch (nested
->type
) {
839 offset
+= nested
->init_offset
;
841 expr
->init_offset
= offset
;
845 case EXPR_INITIALIZER
: {
846 struct expression
*reuse
= nested
, *entry
;
848 FOR_EACH_PTR(expr
->expr_list
, entry
) {
849 if (entry
->type
== EXPR_POS
) {
850 entry
->init_offset
+= offset
;
854 * This happens rarely, but it can happen
855 * with bitfields that are all at offset
858 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
860 reuse
->type
= EXPR_POS
;
861 reuse
->ctype
= entry
->ctype
;
862 reuse
->init_offset
= offset
;
864 reuse
->init_expr
= entry
;
865 REPLACE_CURRENT_PTR(entry
, reuse
);
868 } END_FOR_EACH_PTR(entry
);
877 return expand_expression(nested
);
880 static unsigned long bit_offset(const struct expression
*expr
)
882 unsigned long offset
= 0;
883 while (expr
->type
== EXPR_POS
) {
884 offset
+= bytes_to_bits(expr
->init_offset
);
885 expr
= expr
->init_expr
;
887 if (expr
&& expr
->ctype
)
888 offset
+= expr
->ctype
->bit_offset
;
892 static int compare_expressions(const void *_a
, const void *_b
)
894 const struct expression
*a
= _a
;
895 const struct expression
*b
= _b
;
896 unsigned long a_pos
= bit_offset(a
);
897 unsigned long b_pos
= bit_offset(b
);
899 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
902 static void sort_expression_list(struct expression_list
**list
)
904 sort_list((struct ptr_list
**)list
, compare_expressions
);
907 static void verify_nonoverlapping(struct expression_list
**list
)
909 struct expression
*a
= NULL
;
910 struct expression
*b
;
912 FOR_EACH_PTR(*list
, b
) {
913 if (!b
->ctype
|| !b
->ctype
->bit_size
)
915 if (a
&& bit_offset(a
) == bit_offset(b
)) {
916 warning(a
->pos
, "Initializer entry defined twice");
917 info(b
->pos
, " also defined here");
921 } END_FOR_EACH_PTR(b
);
924 static int expand_expression(struct expression
*expr
)
928 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
931 switch (expr
->type
) {
938 return expand_symbol_expression(expr
);
940 return expand_binop(expr
);
943 return expand_logical(expr
);
946 return expand_comma(expr
);
949 return expand_compare(expr
);
951 case EXPR_ASSIGNMENT
:
952 return expand_assignment(expr
);
955 return expand_preop(expr
);
958 return expand_postop(expr
);
961 case EXPR_FORCE_CAST
:
962 case EXPR_IMPLIED_CAST
:
963 return expand_cast(expr
);
966 return expand_call(expr
);
969 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
973 case EXPR_CONDITIONAL
:
974 return expand_conditional(expr
);
976 case EXPR_STATEMENT
: {
977 struct statement
*stmt
= expr
->statement
;
978 int cost
= expand_statement(stmt
);
980 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
981 *expr
= *stmt
->expression
;
988 case EXPR_INITIALIZER
:
989 sort_expression_list(&expr
->expr_list
);
990 verify_nonoverlapping(&expr
->expr_list
);
991 return expand_expression_list(expr
->expr_list
);
993 case EXPR_IDENTIFIER
:
1000 return expand_expression(expr
->base
) + 1;
1003 return expand_pos_expression(expr
);
1006 case EXPR_PTRSIZEOF
:
1009 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1012 return SIDE_EFFECTS
;
1015 static void expand_const_expression(struct expression
*expr
, const char *where
)
1018 expand_expression(expr
);
1019 if (expr
->type
!= EXPR_VALUE
)
1020 expression_error(expr
, "Expected constant expression in %s", where
);
1024 int expand_symbol(struct symbol
*sym
)
1027 struct symbol
*base_type
;
1031 base_type
= sym
->ctype
.base_type
;
1035 retval
= expand_expression(sym
->initializer
);
1036 /* expand the body of the symbol */
1037 if (base_type
->type
== SYM_FN
) {
1038 if (base_type
->stmt
)
1039 expand_statement(base_type
->stmt
);
1044 static void expand_return_expression(struct statement
*stmt
)
1046 expand_expression(stmt
->expression
);
1049 static int expand_if_statement(struct statement
*stmt
)
1051 struct expression
*expr
= stmt
->if_conditional
;
1053 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1056 expand_expression(expr
);
1058 /* This is only valid if nobody jumps into the "dead" side */
1060 /* Simplify constant conditionals without even evaluating the false side */
1061 if (expr
->type
== EXPR_VALUE
) {
1062 struct statement
*simple
;
1063 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1067 stmt
->type
= STMT_NONE
;
1070 expand_statement(simple
);
1072 return SIDE_EFFECTS
;
1075 expand_statement(stmt
->if_true
);
1076 expand_statement(stmt
->if_false
);
1077 return SIDE_EFFECTS
;
1081 * Expanding a compound statement is really just
1082 * about adding up the costs of each individual
1085 * We also collapse a simple compound statement:
1086 * this would trigger for simple inline functions,
1087 * except we would have to check the "return"
1088 * symbol usage. Next time.
1090 static int expand_compound(struct statement
*stmt
)
1092 struct statement
*s
, *last
;
1093 int cost
, statements
;
1096 expand_symbol(stmt
->ret
);
1099 cost
= expand_statement(last
);
1100 statements
= last
!= NULL
;
1101 FOR_EACH_PTR(stmt
->stmts
, s
) {
1104 cost
+= expand_statement(s
);
1105 } END_FOR_EACH_PTR(s
);
1107 if (statements
== 1 && !stmt
->ret
)
1113 static int expand_statement(struct statement
*stmt
)
1118 switch (stmt
->type
) {
1119 case STMT_DECLARATION
: {
1121 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1123 } END_FOR_EACH_PTR(sym
);
1124 return SIDE_EFFECTS
;
1128 expand_return_expression(stmt
);
1129 return SIDE_EFFECTS
;
1131 case STMT_EXPRESSION
:
1132 return expand_expression(stmt
->expression
);
1135 return expand_compound(stmt
);
1138 return expand_if_statement(stmt
);
1141 expand_expression(stmt
->iterator_pre_condition
);
1142 expand_expression(stmt
->iterator_post_condition
);
1143 expand_statement(stmt
->iterator_pre_statement
);
1144 expand_statement(stmt
->iterator_statement
);
1145 expand_statement(stmt
->iterator_post_statement
);
1146 return SIDE_EFFECTS
;
1149 expand_expression(stmt
->switch_expression
);
1150 expand_statement(stmt
->switch_statement
);
1151 return SIDE_EFFECTS
;
1154 expand_const_expression(stmt
->case_expression
, "case statement");
1155 expand_const_expression(stmt
->case_to
, "case statement");
1156 expand_statement(stmt
->case_statement
);
1157 return SIDE_EFFECTS
;
1160 expand_statement(stmt
->label_statement
);
1161 return SIDE_EFFECTS
;
1164 expand_expression(stmt
->goto_expression
);
1165 return SIDE_EFFECTS
;
1170 /* FIXME! Do the asm parameter evaluation! */
1173 expand_expression(stmt
->expression
);
1176 expand_expression(stmt
->range_expression
);
1177 expand_expression(stmt
->range_low
);
1178 expand_expression(stmt
->range_high
);
1181 return SIDE_EFFECTS
;
1184 static inline int bad_integer_constant_expression(struct expression
*expr
)
1186 if (!(expr
->flags
& Int_const_expr
))
1188 if (expr
->taint
& Taint_comma
)
1193 static long long __get_expression_value(struct expression
*expr
, int strict
)
1195 long long value
, mask
;
1196 struct symbol
*ctype
;
1200 ctype
= evaluate_expression(expr
);
1202 expression_error(expr
, "bad constant expression type");
1205 expand_expression(expr
);
1206 if (expr
->type
!= EXPR_VALUE
) {
1207 expression_error(expr
, "bad constant expression");
1210 if (strict
&& bad_integer_constant_expression(expr
)) {
1211 expression_error(expr
, "bad integer constant expression");
1215 value
= expr
->value
;
1216 mask
= 1ULL << (ctype
->bit_size
-1);
1219 while (ctype
->type
!= SYM_BASETYPE
)
1220 ctype
= ctype
->ctype
.base_type
;
1221 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1222 value
= value
| mask
| ~(mask
-1);
1227 long long get_expression_value_nomod(struct expression
*expr
)
1229 long long value
, mask
;
1230 struct symbol
*ctype
;
1231 struct expression copy
;
1235 memcpy(©
, expr
, sizeof(copy
));
1236 ctype
= evaluate_expression(©
);
1238 expression_error(©
, "bad constant expression type");
1241 expand_expression(©
);
1242 if (copy
.type
!= EXPR_VALUE
) {
1243 expression_error(©
, "bad constant expression");
1248 mask
= 1ULL << (ctype
->bit_size
-1);
1251 while (ctype
->type
!= SYM_BASETYPE
)
1252 ctype
= ctype
->ctype
.base_type
;
1253 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1254 value
= value
| mask
| ~(mask
-1);
1259 long long get_expression_value(struct expression
*expr
)
1261 return __get_expression_value(expr
, 0);
1264 long long const_expression_value(struct expression
*expr
)
1266 return __get_expression_value(expr
, 1);
1269 int is_zero_constant(struct expression
*expr
)
1271 const int saved
= conservative
;
1273 expand_expression(expr
);
1274 conservative
= saved
;
1275 return expr
->type
== EXPR_VALUE
&& !expr
->value
;