4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * expand constant expressions.
43 #include "expression.h"
47 static int expand_expression(struct expression
*);
48 static int expand_statement(struct statement
*);
49 static int conservative
;
51 static int expand_symbol_expression(struct expression
*expr
)
53 struct symbol
*sym
= expr
->symbol
;
55 if (sym
== &zero_int
) {
57 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
58 expr
->type
= EXPR_VALUE
;
63 /* The cost of a symbol expression is lower for on-stack symbols */
64 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
67 static long long get_longlong(struct expression
*expr
)
69 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
70 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
71 long long value
= expr
->value
;
72 long long ormask
, andmask
;
76 andmask
= mask
| (mask
-1);
80 return (value
& andmask
) | ormask
;
83 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
84 struct expression
*old
, struct symbol
*oldtype
)
86 int old_size
= oldtype
->bit_size
;
87 int new_size
= newtype
->bit_size
;
88 long long value
, mask
, signmask
;
89 long long oldmask
, oldsignmask
, dropped
;
91 if (is_float_type(newtype
) || is_float_type(oldtype
))
94 // For pointers and integers, we can just move the value around
95 expr
->type
= EXPR_VALUE
;
96 expr
->taint
= old
->taint
;
97 if (old_size
== new_size
) {
98 expr
->value
= old
->value
;
102 // expand it to the full "long long" value
103 value
= get_longlong(old
);
106 // _Bool requires a zero test rather than truncation.
107 if (is_bool_type(newtype
)) {
108 expr
->value
= !!value
;
109 if (!conservative
&& value
!= 0 && value
!= 1)
110 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
114 // Truncate it to the new size
115 signmask
= 1ULL << (new_size
-1);
116 mask
= signmask
| (signmask
-1);
117 expr
->value
= value
& mask
;
119 // Stop here unless checking for truncation
120 if (!Wcast_truncate
|| conservative
)
123 // Check if we dropped any bits..
124 oldsignmask
= 1ULL << (old_size
-1);
125 oldmask
= oldsignmask
| (oldsignmask
-1);
126 dropped
= oldmask
& ~mask
;
128 // OK if the bits were (and still are) purely sign bits
129 if (value
& dropped
) {
130 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
131 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
138 if (!is_float_type(newtype
)) {
139 value
= (long long)old
->fvalue
;
140 expr
->type
= EXPR_VALUE
;
145 if (!is_float_type(oldtype
))
146 expr
->fvalue
= (long double)get_longlong(old
);
148 expr
->fvalue
= old
->fvalue
;
150 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
151 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
152 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
153 expr
->fvalue
= (double)expr
->fvalue
;
155 expr
->fvalue
= (float)expr
->fvalue
;
157 expr
->type
= EXPR_FVALUE
;
160 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
162 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
163 count
&= ctype
->bit_size
-1;
168 * CAREFUL! We need to get the size and sign of the
171 #define CONVERT(op,s) (((op)<<1)+(s))
172 #define SIGNED(op) CONVERT(op, 1)
173 #define UNSIGNED(op) CONVERT(op, 0)
174 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
176 struct expression
*left
= expr
->left
, *right
= expr
->right
;
177 unsigned long long v
, l
, r
, mask
;
178 signed long long sl
, sr
;
181 if (right
->type
!= EXPR_VALUE
)
184 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
185 if (r
>= ctype
->bit_size
) {
188 r
= check_shift_count(expr
, ctype
, r
);
192 if (left
->type
!= EXPR_VALUE
)
194 l
= left
->value
; r
= right
->value
;
195 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
196 mask
= 1ULL << (ctype
->bit_size
-1);
198 if (is_signed
&& (sl
& mask
))
200 if (is_signed
&& (sr
& mask
))
203 switch (CONVERT(expr
->op
,is_signed
)) {
240 if (l
== mask
&& sr
== -1)
253 if (l
== mask
&& sr
== -1)
263 case SIGNED(SPECIAL_LEFTSHIFT
):
264 case UNSIGNED(SPECIAL_LEFTSHIFT
):
268 case SIGNED(SPECIAL_RIGHTSHIFT
):
272 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
279 mask
= mask
| (mask
-1);
280 expr
->value
= v
& mask
;
281 expr
->type
= EXPR_VALUE
;
282 expr
->taint
= left
->taint
| right
->taint
;
286 warning(expr
->pos
, "division by zero");
290 warning(expr
->pos
, "constant integer operation overflow");
294 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
296 struct expression
*left
= expr
->left
, *right
= expr
->right
;
297 unsigned long long l
, r
, mask
;
298 signed long long sl
, sr
;
300 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
302 l
= left
->value
; r
= right
->value
;
303 mask
= 1ULL << (ctype
->bit_size
-1);
310 case '<': expr
->value
= sl
< sr
; break;
311 case '>': expr
->value
= sl
> sr
; break;
312 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
313 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
314 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
315 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
316 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
317 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
318 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
319 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
321 expr
->type
= EXPR_VALUE
;
322 expr
->taint
= left
->taint
| right
->taint
;
326 static int simplify_float_binop(struct expression
*expr
)
328 struct expression
*left
= expr
->left
, *right
= expr
->right
;
329 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
330 long double l
, r
, res
;
332 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
338 if (mod
& MOD_LONGLONG
) {
340 case '+': res
= l
+ r
; break;
341 case '-': res
= l
- r
; break;
342 case '*': res
= l
* r
; break;
343 case '/': if (!r
) goto Div
;
347 } else if (mod
& MOD_LONG
) {
349 case '+': res
= (double) l
+ (double) r
; break;
350 case '-': res
= (double) l
- (double) r
; break;
351 case '*': res
= (double) l
* (double) r
; break;
352 case '/': if (!r
) goto Div
;
353 res
= (double) l
/ (double) r
; break;
358 case '+': res
= (float)l
+ (float)r
; break;
359 case '-': res
= (float)l
- (float)r
; break;
360 case '*': res
= (float)l
* (float)r
; break;
361 case '/': if (!r
) goto Div
;
362 res
= (float)l
/ (float)r
; break;
366 expr
->type
= EXPR_FVALUE
;
371 warning(expr
->pos
, "division by zero");
375 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
377 struct expression
*left
= expr
->left
, *right
= expr
->right
;
380 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
386 case '<': expr
->value
= l
< r
; break;
387 case '>': expr
->value
= l
> r
; break;
388 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
389 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
390 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
391 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
393 expr
->type
= EXPR_VALUE
;
398 static int expand_binop(struct expression
*expr
)
402 cost
= expand_expression(expr
->left
);
403 cost
+= expand_expression(expr
->right
);
404 if (simplify_int_binop(expr
, expr
->ctype
))
406 if (simplify_float_binop(expr
))
411 static int expand_logical(struct expression
*expr
)
413 struct expression
*left
= expr
->left
;
414 struct expression
*right
;
417 /* Do immediate short-circuiting ... */
418 cost
= expand_expression(left
);
419 if (left
->type
== EXPR_VALUE
) {
420 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
422 expr
->type
= EXPR_VALUE
;
424 expr
->taint
= left
->taint
;
429 expr
->type
= EXPR_VALUE
;
431 expr
->taint
= left
->taint
;
438 rcost
= expand_expression(right
);
439 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
441 * We know the left value doesn't matter, since
442 * otherwise we would have short-circuited it..
444 expr
->type
= EXPR_VALUE
;
445 expr
->value
= right
->value
!= 0;
446 expr
->taint
= left
->taint
| right
->taint
;
451 * If the right side is safe and cheaper than a branch,
452 * just avoid the branch and turn it into a regular binop
455 if (rcost
< BRANCH_COST
) {
456 expr
->type
= EXPR_BINOP
;
457 rcost
-= BRANCH_COST
- 1;
460 return cost
+ BRANCH_COST
+ rcost
;
463 static int expand_comma(struct expression
*expr
)
467 cost
= expand_expression(expr
->left
);
468 cost
+= expand_expression(expr
->right
);
469 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
470 unsigned flags
= expr
->flags
;
472 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
473 *expr
= *expr
->right
;
475 if (expr
->type
== EXPR_VALUE
)
476 expr
->taint
|= Taint_comma
| taint
;
481 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
483 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
485 struct ctype c1
= {.base_type
= left
};
486 struct ctype c2
= {.base_type
= right
};
489 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
490 case SPECIAL_NOTEQUAL
:
491 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
493 return left
->bit_size
< right
->bit_size
;
495 return left
->bit_size
> right
->bit_size
;
497 return left
->bit_size
<= right
->bit_size
;
499 return left
->bit_size
>= right
->bit_size
;
504 static int expand_compare(struct expression
*expr
)
506 struct expression
*left
= expr
->left
, *right
= expr
->right
;
509 cost
= expand_expression(left
);
510 cost
+= expand_expression(right
);
513 /* Type comparison? */
514 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
516 expr
->type
= EXPR_VALUE
;
517 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
521 if (simplify_cmp_binop(expr
, left
->ctype
))
523 if (simplify_float_cmp(expr
, left
->ctype
))
529 static int expand_conditional(struct expression
*expr
)
531 struct expression
*cond
= expr
->conditional
;
532 struct expression
*true = expr
->cond_true
;
533 struct expression
*false = expr
->cond_false
;
536 cond_cost
= expand_expression(cond
);
537 if (cond
->type
== EXPR_VALUE
) {
538 unsigned flags
= expr
->flags
;
543 cost
= expand_expression(true);
546 if (expr
->type
== EXPR_VALUE
)
547 expr
->taint
|= cond
->taint
;
551 cost
= expand_expression(true);
552 cost
+= expand_expression(false);
554 if (cost
< SELECT_COST
) {
555 expr
->type
= EXPR_SELECT
;
556 cost
-= BRANCH_COST
- 1;
559 return cost
+ cond_cost
+ BRANCH_COST
;
562 static int expand_assignment(struct expression
*expr
)
564 expand_expression(expr
->left
);
565 expand_expression(expr
->right
);
569 static int expand_addressof(struct expression
*expr
)
571 return expand_expression(expr
->unop
);
575 * Look up a trustable initializer value at the requested offset.
577 * Return NULL if no such value can be found or statically trusted.
579 * FIXME!! We should check that the size is right!
581 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
583 struct expression
*value
;
585 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
587 value
= sym
->initializer
;
590 if (value
->type
== EXPR_INITIALIZER
) {
591 struct expression
*entry
;
592 FOR_EACH_PTR(value
->expr_list
, entry
) {
593 if (entry
->type
!= EXPR_POS
) {
598 if (entry
->init_offset
< offset
)
600 if (entry
->init_offset
> offset
)
602 return entry
->init_expr
;
603 } END_FOR_EACH_PTR(entry
);
609 static int expand_dereference(struct expression
*expr
)
611 struct expression
*unop
= expr
->unop
;
614 expand_expression(unop
);
617 * NOTE! We get a bogus warning right now for some special
618 * cases: apparently I've screwed up the optimization of
619 * a zero-offset dereference, and the ctype is wrong.
621 * Leave the warning in anyway, since this is also a good
622 * test for me to get the type evaluation right..
624 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
625 warning(unop
->pos
, "dereference of noderef expression");
628 * Is it "symbol" or "symbol + offset"?
631 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
632 struct expression
*right
= unop
->right
;
633 if (right
->type
== EXPR_VALUE
) {
634 offset
= right
->value
;
639 if (unop
->type
== EXPR_SYMBOL
) {
640 struct symbol
*sym
= unop
->symbol
;
641 struct expression
*value
= constant_symbol_value(sym
, offset
);
643 /* Const symbol with a constant initializer? */
645 /* FIXME! We should check that the size is right! */
646 if (value
->type
== EXPR_VALUE
) {
647 expr
->type
= EXPR_VALUE
;
648 expr
->value
= value
->value
;
651 } else if (value
->type
== EXPR_FVALUE
) {
652 expr
->type
= EXPR_FVALUE
;
653 expr
->fvalue
= value
->fvalue
;
658 /* Direct symbol dereference? Cheap and safe */
659 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
665 static int simplify_preop(struct expression
*expr
)
667 struct expression
*op
= expr
->unop
;
668 unsigned long long v
, mask
;
670 if (op
->type
!= EXPR_VALUE
)
673 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
678 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
682 case '!': v
= !v
; break;
683 case '~': v
= ~v
; break;
686 mask
= mask
| (mask
-1);
687 expr
->value
= v
& mask
;
688 expr
->type
= EXPR_VALUE
;
689 expr
->taint
= op
->taint
;
694 warning(expr
->pos
, "constant integer operation overflow");
698 static int simplify_float_preop(struct expression
*expr
)
700 struct expression
*op
= expr
->unop
;
703 if (op
->type
!= EXPR_FVALUE
)
708 case '-': v
= -v
; break;
712 expr
->type
= EXPR_FVALUE
;
717 * Unary post-ops: x++ and x--
719 static int expand_postop(struct expression
*expr
)
721 expand_expression(expr
->unop
);
725 static int expand_preop(struct expression
*expr
)
731 return expand_dereference(expr
);
734 return expand_addressof(expr
);
736 case SPECIAL_INCREMENT
:
737 case SPECIAL_DECREMENT
:
739 * From a type evaluation standpoint the preops are
740 * the same as the postops
742 return expand_postop(expr
);
747 cost
= expand_expression(expr
->unop
);
749 if (simplify_preop(expr
))
751 if (simplify_float_preop(expr
))
756 static int expand_arguments(struct expression_list
*head
)
759 struct expression
*expr
;
761 FOR_EACH_PTR (head
, expr
) {
762 cost
+= expand_expression(expr
);
763 } END_FOR_EACH_PTR(expr
);
767 static int expand_cast(struct expression
*expr
)
770 struct expression
*target
= expr
->cast_expression
;
772 cost
= expand_expression(target
);
774 /* Simplify normal integer casts.. */
775 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
776 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
783 * expand a call expression with a symbol. This
784 * should expand builtins.
786 static int expand_symbol_call(struct expression
*expr
, int cost
)
788 struct expression
*fn
= expr
->fn
;
789 struct symbol
*ctype
= fn
->ctype
;
791 if (fn
->type
!= EXPR_PREOP
)
794 if (ctype
->op
&& ctype
->op
->expand
)
795 return ctype
->op
->expand(expr
, cost
);
797 if (ctype
->ctype
.modifiers
& MOD_PURE
)
803 static int expand_call(struct expression
*expr
)
807 struct expression
*fn
= expr
->fn
;
809 cost
= expand_arguments(expr
->args
);
812 expression_error(expr
, "function has no type");
815 if (sym
->type
== SYM_NODE
)
816 return expand_symbol_call(expr
, cost
);
821 static int expand_expression_list(struct expression_list
*list
)
824 struct expression
*expr
;
826 FOR_EACH_PTR(list
, expr
) {
827 cost
+= expand_expression(expr
);
828 } END_FOR_EACH_PTR(expr
);
833 * We can simplify nested position expressions if
834 * this is a simple (single) positional expression.
836 static int expand_pos_expression(struct expression
*expr
)
838 struct expression
*nested
= expr
->init_expr
;
839 unsigned long offset
= expr
->init_offset
;
840 int nr
= expr
->init_nr
;
843 switch (nested
->type
) {
845 offset
+= nested
->init_offset
;
847 expr
->init_offset
= offset
;
851 case EXPR_INITIALIZER
: {
852 struct expression
*reuse
= nested
, *entry
;
854 FOR_EACH_PTR(expr
->expr_list
, entry
) {
855 if (entry
->type
== EXPR_POS
) {
856 entry
->init_offset
+= offset
;
860 * This happens rarely, but it can happen
861 * with bitfields that are all at offset
864 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
866 reuse
->type
= EXPR_POS
;
867 reuse
->ctype
= entry
->ctype
;
868 reuse
->init_offset
= offset
;
870 reuse
->init_expr
= entry
;
871 REPLACE_CURRENT_PTR(entry
, reuse
);
874 } END_FOR_EACH_PTR(entry
);
883 return expand_expression(nested
);
886 static unsigned long bit_offset(const struct expression
*expr
)
888 unsigned long offset
= 0;
889 while (expr
->type
== EXPR_POS
) {
890 offset
+= bytes_to_bits(expr
->init_offset
);
891 expr
= expr
->init_expr
;
893 if (expr
&& expr
->ctype
)
894 offset
+= expr
->ctype
->bit_offset
;
898 static unsigned long bit_range(const struct expression
*expr
)
900 unsigned long range
= 0;
901 unsigned long size
= 0;
902 while (expr
->type
== EXPR_POS
) {
903 unsigned long nr
= expr
->init_nr
;
904 size
= expr
->ctype
->bit_size
;
905 range
+= (nr
- 1) * size
;
906 expr
= expr
->init_expr
;
912 static int compare_expressions(const void *_a
, const void *_b
)
914 const struct expression
*a
= _a
;
915 const struct expression
*b
= _b
;
916 unsigned long a_pos
= bit_offset(a
);
917 unsigned long b_pos
= bit_offset(b
);
919 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
922 static void sort_expression_list(struct expression_list
**list
)
924 sort_list((struct ptr_list
**)list
, compare_expressions
);
927 static void verify_nonoverlapping(struct expression_list
**list
, struct expression
*expr
)
929 struct expression
*a
= NULL
;
930 unsigned long max
= 0;
931 unsigned long whole
= expr
->ctype
->bit_size
;
932 struct expression
*b
;
937 FOR_EACH_PTR(*list
, b
) {
938 unsigned long off
, end
;
939 if (!b
->ctype
|| !b
->ctype
->bit_size
)
942 if (a
&& off
< max
) {
943 warning(a
->pos
, "Initializer entry defined twice");
944 info(b
->pos
, " also defined here");
945 if (!Woverride_init_all
)
948 end
= off
+ bit_range(b
);
949 if (!a
&& !Woverride_init_whole_range
) {
950 // If first entry is the whole range, do not let
951 // any warning about it (this allow to initialize
952 // an array with some default value and then override
953 // some specific entries).
954 if (off
== 0 && end
== whole
)
961 } END_FOR_EACH_PTR(b
);
964 static int expand_expression(struct expression
*expr
)
968 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
971 switch (expr
->type
) {
978 return expand_symbol_expression(expr
);
980 return expand_binop(expr
);
983 return expand_logical(expr
);
986 return expand_comma(expr
);
989 return expand_compare(expr
);
991 case EXPR_ASSIGNMENT
:
992 return expand_assignment(expr
);
995 return expand_preop(expr
);
998 return expand_postop(expr
);
1001 case EXPR_FORCE_CAST
:
1002 case EXPR_IMPLIED_CAST
:
1003 return expand_cast(expr
);
1006 return expand_call(expr
);
1009 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
1013 case EXPR_CONDITIONAL
:
1014 return expand_conditional(expr
);
1016 case EXPR_STATEMENT
: {
1017 struct statement
*stmt
= expr
->statement
;
1018 int cost
= expand_statement(stmt
);
1020 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
1021 *expr
= *stmt
->expression
;
1028 case EXPR_INITIALIZER
:
1029 sort_expression_list(&expr
->expr_list
);
1030 verify_nonoverlapping(&expr
->expr_list
, expr
);
1031 return expand_expression_list(expr
->expr_list
);
1033 case EXPR_IDENTIFIER
:
1040 return expand_expression(expr
->base
) + 1;
1043 return expand_pos_expression(expr
);
1046 case EXPR_PTRSIZEOF
:
1049 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1052 return SIDE_EFFECTS
;
1055 static void expand_const_expression(struct expression
*expr
, const char *where
)
1058 expand_expression(expr
);
1059 if (expr
->type
!= EXPR_VALUE
)
1060 expression_error(expr
, "Expected constant expression in %s", where
);
1064 int expand_symbol(struct symbol
*sym
)
1067 struct symbol
*base_type
;
1071 base_type
= sym
->ctype
.base_type
;
1075 retval
= expand_expression(sym
->initializer
);
1076 /* expand the body of the symbol */
1077 if (base_type
->type
== SYM_FN
) {
1078 if (base_type
->stmt
)
1079 expand_statement(base_type
->stmt
);
1084 static void expand_return_expression(struct statement
*stmt
)
1086 expand_expression(stmt
->expression
);
1089 static int expand_if_statement(struct statement
*stmt
)
1091 struct expression
*expr
= stmt
->if_conditional
;
1093 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1096 expand_expression(expr
);
1098 /* This is only valid if nobody jumps into the "dead" side */
1100 /* Simplify constant conditionals without even evaluating the false side */
1101 if (expr
->type
== EXPR_VALUE
) {
1102 struct statement
*simple
;
1103 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1107 stmt
->type
= STMT_NONE
;
1110 expand_statement(simple
);
1112 return SIDE_EFFECTS
;
1115 expand_statement(stmt
->if_true
);
1116 expand_statement(stmt
->if_false
);
1117 return SIDE_EFFECTS
;
1121 * Expanding a compound statement is really just
1122 * about adding up the costs of each individual
1125 * We also collapse a simple compound statement:
1126 * this would trigger for simple inline functions,
1127 * except we would have to check the "return"
1128 * symbol usage. Next time.
1130 static int expand_compound(struct statement
*stmt
)
1132 struct statement
*s
, *last
;
1133 int cost
, statements
;
1136 expand_symbol(stmt
->ret
);
1139 cost
= expand_statement(last
);
1140 statements
= last
!= NULL
;
1141 FOR_EACH_PTR(stmt
->stmts
, s
) {
1144 cost
+= expand_statement(s
);
1145 } END_FOR_EACH_PTR(s
);
1147 if (statements
== 1 && !stmt
->ret
)
1153 static int expand_statement(struct statement
*stmt
)
1158 switch (stmt
->type
) {
1159 case STMT_DECLARATION
: {
1161 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1163 } END_FOR_EACH_PTR(sym
);
1164 return SIDE_EFFECTS
;
1168 expand_return_expression(stmt
);
1169 return SIDE_EFFECTS
;
1171 case STMT_EXPRESSION
:
1172 return expand_expression(stmt
->expression
);
1175 return expand_compound(stmt
);
1178 return expand_if_statement(stmt
);
1181 expand_expression(stmt
->iterator_pre_condition
);
1182 expand_expression(stmt
->iterator_post_condition
);
1183 expand_statement(stmt
->iterator_pre_statement
);
1184 expand_statement(stmt
->iterator_statement
);
1185 expand_statement(stmt
->iterator_post_statement
);
1186 return SIDE_EFFECTS
;
1189 expand_expression(stmt
->switch_expression
);
1190 expand_statement(stmt
->switch_statement
);
1191 return SIDE_EFFECTS
;
1194 expand_const_expression(stmt
->case_expression
, "case statement");
1195 expand_const_expression(stmt
->case_to
, "case statement");
1196 expand_statement(stmt
->case_statement
);
1197 return SIDE_EFFECTS
;
1200 expand_statement(stmt
->label_statement
);
1201 return SIDE_EFFECTS
;
1204 expand_expression(stmt
->goto_expression
);
1205 return SIDE_EFFECTS
;
1210 /* FIXME! Do the asm parameter evaluation! */
1213 expand_expression(stmt
->expression
);
1216 expand_expression(stmt
->range_expression
);
1217 expand_expression(stmt
->range_low
);
1218 expand_expression(stmt
->range_high
);
1221 return SIDE_EFFECTS
;
1224 static inline int bad_integer_constant_expression(struct expression
*expr
)
1226 if (!(expr
->flags
& CEF_ICE
))
1228 if (expr
->taint
& Taint_comma
)
1233 static long long __get_expression_value(struct expression
*expr
, int strict
)
1235 long long value
, mask
;
1236 struct symbol
*ctype
;
1240 ctype
= evaluate_expression(expr
);
1242 expression_error(expr
, "bad constant expression type");
1245 expand_expression(expr
);
1246 if (expr
->type
!= EXPR_VALUE
) {
1248 expression_error(expr
, "bad constant expression");
1251 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1252 expression_error(expr
, "bad integer constant expression");
1256 value
= expr
->value
;
1257 mask
= 1ULL << (ctype
->bit_size
-1);
1260 while (ctype
->type
!= SYM_BASETYPE
)
1261 ctype
= ctype
->ctype
.base_type
;
1262 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1263 value
= value
| mask
| ~(mask
-1);
1268 long long get_expression_value(struct expression
*expr
)
1270 return __get_expression_value(expr
, 0);
1273 long long const_expression_value(struct expression
*expr
)
1275 return __get_expression_value(expr
, 1);
1278 long long get_expression_value_silent(struct expression
*expr
)
1281 return __get_expression_value(expr
, 2);
1284 int expr_truth_value(struct expression
*expr
)
1286 const int saved
= conservative
;
1287 struct symbol
*ctype
;
1292 ctype
= evaluate_expression(expr
);
1297 expand_expression(expr
);
1298 conservative
= saved
;
1301 switch (expr
->type
) {
1306 return expr
->value
!= 0;
1308 return expr
->fvalue
!= 0;
1314 int is_zero_constant(struct expression
*expr
)
1316 const int saved
= conservative
;
1318 expand_expression(expr
);
1319 conservative
= saved
;
1320 return expr
->type
== EXPR_VALUE
&& !expr
->value
;