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"
48 static int expand_expression(struct expression
*);
49 static int expand_statement(struct statement
*);
51 // If set, don't issue a warning on divide-by-0, invalid shift, ...
52 // and don't mark the expression as erroneous but leave it as-is.
53 // This allows testing some characteristics of the expression
54 // without creating any side-effects (e.g.: is_zero_constant()).
55 static int conservative
;
57 static int expand_symbol_expression(struct expression
*expr
)
59 struct symbol
*sym
= expr
->symbol
;
61 if (sym
== &zero_int
) {
63 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
64 expr
->type
= EXPR_VALUE
;
69 /* The cost of a symbol expression is lower for on-stack symbols */
70 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
73 static long long get_longlong(struct expression
*expr
)
75 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
76 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
77 long long value
= expr
->value
;
78 long long ormask
, andmask
;
82 andmask
= mask
| (mask
-1);
86 return (value
& andmask
) | ormask
;
89 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
90 struct expression
*old
, struct symbol
*oldtype
)
92 int old_size
= oldtype
->bit_size
;
93 int new_size
= newtype
->bit_size
;
94 long long value
, mask
, signmask
;
95 long long oldmask
, oldsignmask
, dropped
;
97 if (is_float_type(newtype
) || is_float_type(oldtype
))
100 // For pointers and integers, we can just move the value around
101 expr
->type
= EXPR_VALUE
;
102 expr
->taint
= old
->taint
;
103 if (old_size
== new_size
) {
104 expr
->value
= old
->value
;
108 // expand it to the full "long long" value
109 value
= get_longlong(old
);
112 // _Bool requires a zero test rather than truncation.
113 if (is_bool_type(newtype
)) {
114 expr
->value
= !!value
;
115 if (!conservative
&& value
!= 0 && value
!= 1)
116 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
120 // Truncate it to the new size
121 signmask
= 1ULL << (new_size
-1);
122 mask
= signmask
| (signmask
-1);
123 expr
->value
= value
& mask
;
125 // Stop here unless checking for truncation
126 if (!Wcast_truncate
|| conservative
)
129 // Check if we dropped any bits..
130 oldsignmask
= 1ULL << (old_size
-1);
131 oldmask
= oldsignmask
| (oldsignmask
-1);
132 dropped
= oldmask
& ~mask
;
134 // OK if the bits were (and still are) purely sign bits
135 if (value
& dropped
) {
136 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
137 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
144 if (!is_float_type(newtype
)) {
145 value
= (long long)old
->fvalue
;
146 expr
->type
= EXPR_VALUE
;
151 if (!is_float_type(oldtype
))
152 expr
->fvalue
= (long double)get_longlong(old
);
154 expr
->fvalue
= old
->fvalue
;
156 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
157 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
158 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
159 expr
->fvalue
= (double)expr
->fvalue
;
161 expr
->fvalue
= (float)expr
->fvalue
;
163 expr
->type
= EXPR_FVALUE
;
166 static void warn_shift_count(struct expression
*expr
, struct symbol
*ctype
, long long count
)
169 if (!Wshift_count_negative
)
171 warning(expr
->pos
, "shift count is negative (%lld)", count
);
174 if (ctype
->type
== SYM_NODE
)
175 ctype
= ctype
->ctype
.base_type
;
177 if (!Wshift_count_overflow
)
179 warning(expr
->pos
, "shift too big (%llu) for type %s", count
, show_typename(ctype
));
182 /* Return true if constant shift size is valid */
183 static bool check_shift_count(struct expression
*expr
, struct expression
*right
)
185 struct symbol
*ctype
= expr
->ctype
;
186 long long count
= get_longlong(right
);
188 if (count
>= 0 && count
< ctype
->bit_size
)
191 warn_shift_count(expr
, ctype
, count
);
196 * CAREFUL! We need to get the size and sign of the
199 #define CONVERT(op,s) (((op)<<1)+(s))
200 #define SIGNED(op) CONVERT(op, 1)
201 #define UNSIGNED(op) CONVERT(op, 0)
202 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
204 struct expression
*left
= expr
->left
, *right
= expr
->right
;
205 unsigned long long v
, l
, r
, mask
;
206 signed long long sl
, sr
;
209 if (right
->type
!= EXPR_VALUE
)
212 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
213 if (!check_shift_count(expr
, right
))
216 if (left
->type
!= EXPR_VALUE
)
218 l
= left
->value
; r
= right
->value
;
219 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
220 mask
= 1ULL << (ctype
->bit_size
-1);
222 if (is_signed
&& (sl
& mask
))
224 if (is_signed
&& (sr
& mask
))
227 switch (CONVERT(expr
->op
,is_signed
)) {
264 if (l
== mask
&& sr
== -1)
277 if (l
== mask
&& sr
== -1)
287 case SIGNED(SPECIAL_LEFTSHIFT
):
288 case UNSIGNED(SPECIAL_LEFTSHIFT
):
292 case SIGNED(SPECIAL_RIGHTSHIFT
):
296 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
303 mask
= mask
| (mask
-1);
304 expr
->value
= v
& mask
;
305 expr
->type
= EXPR_VALUE
;
306 expr
->taint
= left
->taint
| right
->taint
;
310 warning(expr
->pos
, "division by zero");
314 warning(expr
->pos
, "constant integer operation overflow");
318 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
320 struct expression
*left
= expr
->left
, *right
= expr
->right
;
321 unsigned long long l
, r
, mask
;
322 signed long long sl
, sr
;
324 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
326 l
= left
->value
; r
= right
->value
;
327 mask
= 1ULL << (ctype
->bit_size
-1);
334 case '<': expr
->value
= sl
< sr
; break;
335 case '>': expr
->value
= sl
> sr
; break;
336 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
337 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
338 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
339 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
340 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
341 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
342 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
343 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
345 expr
->type
= EXPR_VALUE
;
346 expr
->taint
= left
->taint
| right
->taint
;
350 static int simplify_float_binop(struct expression
*expr
)
352 struct expression
*left
= expr
->left
, *right
= expr
->right
;
353 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
354 long double l
, r
, res
;
356 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
362 if (mod
& MOD_LONGLONG
) {
364 case '+': res
= l
+ r
; break;
365 case '-': res
= l
- r
; break;
366 case '*': res
= l
* r
; break;
367 case '/': if (!r
) goto Div
;
371 } else if (mod
& MOD_LONG
) {
373 case '+': res
= (double) l
+ (double) r
; break;
374 case '-': res
= (double) l
- (double) r
; break;
375 case '*': res
= (double) l
* (double) r
; break;
376 case '/': if (!r
) goto Div
;
377 res
= (double) l
/ (double) r
; break;
382 case '+': res
= (float)l
+ (float)r
; break;
383 case '-': res
= (float)l
- (float)r
; break;
384 case '*': res
= (float)l
* (float)r
; break;
385 case '/': if (!r
) goto Div
;
386 res
= (float)l
/ (float)r
; break;
390 expr
->type
= EXPR_FVALUE
;
395 warning(expr
->pos
, "division by zero");
399 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
401 struct expression
*left
= expr
->left
, *right
= expr
->right
;
404 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
410 case '<': expr
->value
= l
< r
; break;
411 case '>': expr
->value
= l
> r
; break;
412 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
413 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
414 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
415 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
417 expr
->type
= EXPR_VALUE
;
422 static int expand_binop(struct expression
*expr
)
426 cost
= expand_expression(expr
->left
);
427 cost
+= expand_expression(expr
->right
);
428 if (simplify_int_binop(expr
, expr
->ctype
))
430 if (simplify_float_binop(expr
))
435 static int expand_logical(struct expression
*expr
)
437 struct expression
*left
= expr
->left
;
438 struct expression
*right
;
441 /* Do immediate short-circuiting ... */
442 cost
= expand_expression(left
);
443 if (left
->type
== EXPR_VALUE
) {
444 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
446 expr
->type
= EXPR_VALUE
;
448 expr
->taint
= left
->taint
;
453 expr
->type
= EXPR_VALUE
;
455 expr
->taint
= left
->taint
;
462 rcost
= expand_expression(right
);
463 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
465 * We know the left value doesn't matter, since
466 * otherwise we would have short-circuited it..
468 expr
->type
= EXPR_VALUE
;
469 expr
->value
= right
->value
!= 0;
470 expr
->taint
= left
->taint
| right
->taint
;
475 * If the right side is safe and cheaper than a branch,
476 * just avoid the branch and turn it into a regular binop
479 if (rcost
< BRANCH_COST
) {
480 expr
->type
= EXPR_BINOP
;
481 rcost
-= BRANCH_COST
- 1;
484 return cost
+ BRANCH_COST
+ rcost
;
487 static int expand_comma(struct expression
*expr
)
491 cost
= expand_expression(expr
->left
);
492 cost
+= expand_expression(expr
->right
);
493 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
494 unsigned flags
= expr
->flags
;
496 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
497 *expr
= *expr
->right
;
499 if (expr
->type
== EXPR_VALUE
)
500 expr
->taint
|= Taint_comma
| taint
;
505 #define MOD_IGN (MOD_QUALIFIER)
507 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
509 struct ctype c1
= {.base_type
= left
};
510 struct ctype c2
= {.base_type
= right
};
513 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
514 case SPECIAL_NOTEQUAL
:
515 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
517 return left
->bit_size
< right
->bit_size
;
519 return left
->bit_size
> right
->bit_size
;
521 return left
->bit_size
<= right
->bit_size
;
523 return left
->bit_size
>= right
->bit_size
;
528 static int expand_compare(struct expression
*expr
)
530 struct expression
*left
= expr
->left
, *right
= expr
->right
;
533 cost
= expand_expression(left
);
534 cost
+= expand_expression(right
);
537 /* Type comparison? */
538 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
540 expr
->type
= EXPR_VALUE
;
541 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
545 if (simplify_cmp_binop(expr
, left
->ctype
))
547 if (simplify_float_cmp(expr
, left
->ctype
))
553 static int expand_conditional(struct expression
*expr
)
555 struct expression
*cond
= expr
->conditional
;
556 struct expression
*valt
= expr
->cond_true
;
557 struct expression
*valf
= expr
->cond_false
;
560 cond_cost
= expand_expression(cond
);
561 if (cond
->type
== EXPR_VALUE
) {
562 unsigned flags
= expr
->flags
;
567 cost
= expand_expression(valt
);
570 if (expr
->type
== EXPR_VALUE
)
571 expr
->taint
|= cond
->taint
;
575 cost
= expand_expression(valt
);
576 cost
+= expand_expression(valf
);
578 if (cost
< SELECT_COST
) {
579 expr
->type
= EXPR_SELECT
;
580 cost
-= BRANCH_COST
- 1;
583 return cost
+ cond_cost
+ BRANCH_COST
;
586 static void check_assignment(struct expression
*expr
)
588 struct expression
*right
;
591 case SPECIAL_SHL_ASSIGN
:
592 case SPECIAL_SHR_ASSIGN
:
594 if (right
->type
!= EXPR_VALUE
)
596 check_shift_count(expr
, right
);
602 static int expand_assignment(struct expression
*expr
)
604 expand_expression(expr
->left
);
605 expand_expression(expr
->right
);
608 check_assignment(expr
);
612 static int expand_addressof(struct expression
*expr
)
614 return expand_expression(expr
->unop
);
618 * Look up a trustable initializer value at the requested offset.
620 * Return NULL if no such value can be found or statically trusted.
622 * FIXME!! We should check that the size is right!
624 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
626 struct expression
*value
;
628 if (sym
->ctype
.modifiers
& MOD_ACCESS
)
630 value
= sym
->initializer
;
633 if (value
->type
== EXPR_INITIALIZER
) {
634 struct expression
*entry
;
635 FOR_EACH_PTR(value
->expr_list
, entry
) {
636 if (entry
->type
!= EXPR_POS
) {
641 if (entry
->init_offset
< offset
)
643 if (entry
->init_offset
> offset
)
645 return entry
->init_expr
;
646 } END_FOR_EACH_PTR(entry
);
652 static int expand_dereference(struct expression
*expr
)
654 struct expression
*unop
= expr
->unop
;
657 expand_expression(unop
);
660 * NOTE! We get a bogus warning right now for some special
661 * cases: apparently I've screwed up the optimization of
662 * a zero-offset dereference, and the ctype is wrong.
664 * Leave the warning in anyway, since this is also a good
665 * test for me to get the type evaluation right..
667 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
668 warning(unop
->pos
, "dereference of noderef expression");
671 * Is it "symbol" or "symbol + offset"?
674 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
675 struct expression
*right
= unop
->right
;
676 if (right
->type
== EXPR_VALUE
) {
677 offset
= right
->value
;
682 if (unop
->type
== EXPR_SYMBOL
) {
683 struct symbol
*sym
= unop
->symbol
;
684 struct expression
*value
= constant_symbol_value(sym
, offset
);
686 /* Const symbol with a constant initializer? */
688 /* FIXME! We should check that the size is right! */
689 if (value
->type
== EXPR_VALUE
) {
690 if (is_bitfield_type(value
->ctype
))
692 expr
->type
= EXPR_VALUE
;
693 expr
->value
= value
->value
;
696 } else if (value
->type
== EXPR_FVALUE
) {
697 expr
->type
= EXPR_FVALUE
;
698 expr
->fvalue
= value
->fvalue
;
703 /* Direct symbol dereference? Cheap and safe */
704 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
710 static int simplify_preop(struct expression
*expr
)
712 struct expression
*op
= expr
->unop
;
713 unsigned long long v
, mask
;
715 if (op
->type
!= EXPR_VALUE
)
718 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
723 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
727 case '!': v
= !v
; break;
728 case '~': v
= ~v
; break;
731 mask
= mask
| (mask
-1);
732 expr
->value
= v
& mask
;
733 expr
->type
= EXPR_VALUE
;
734 expr
->taint
= op
->taint
;
739 warning(expr
->pos
, "constant integer operation overflow");
743 static int simplify_float_preop(struct expression
*expr
)
745 struct expression
*op
= expr
->unop
;
748 if (op
->type
!= EXPR_FVALUE
)
753 case '-': v
= -v
; break;
757 expr
->type
= EXPR_FVALUE
;
762 * Unary post-ops: x++ and x--
764 static int expand_postop(struct expression
*expr
)
766 expand_expression(expr
->unop
);
770 static int expand_preop(struct expression
*expr
)
776 return expand_dereference(expr
);
779 return expand_addressof(expr
);
781 case SPECIAL_INCREMENT
:
782 case SPECIAL_DECREMENT
:
784 * From a type evaluation standpoint the preops are
785 * the same as the postops
787 return expand_postop(expr
);
792 cost
= expand_expression(expr
->unop
);
794 if (simplify_preop(expr
))
796 if (simplify_float_preop(expr
))
801 static int expand_arguments(struct expression_list
*head
)
804 struct expression
*expr
;
806 FOR_EACH_PTR (head
, expr
) {
807 cost
+= expand_expression(expr
);
808 } END_FOR_EACH_PTR(expr
);
812 static int expand_cast(struct expression
*expr
)
815 struct expression
*target
= expr
->cast_expression
;
817 cost
= expand_expression(target
);
819 /* Simplify normal integer casts.. */
820 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
821 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
828 * expand a call expression with a symbol. This
829 * should expand builtins.
831 static int expand_symbol_call(struct expression
*expr
, int cost
)
833 struct expression
*fn
= expr
->fn
;
834 struct symbol
*ctype
= fn
->ctype
;
836 expand_expression(fn
);
838 if (fn
->type
!= EXPR_PREOP
)
841 if (ctype
->op
&& ctype
->op
->expand
)
842 return ctype
->op
->expand(expr
, cost
);
844 if (ctype
->ctype
.modifiers
& MOD_PURE
)
850 static int expand_call(struct expression
*expr
)
854 struct expression
*fn
= expr
->fn
;
856 cost
= expand_arguments(expr
->args
);
859 expression_error(expr
, "function has no type");
862 if (sym
->type
== SYM_NODE
)
863 return expand_symbol_call(expr
, cost
);
868 static int expand_expression_list(struct expression_list
*list
)
871 struct expression
*expr
;
873 FOR_EACH_PTR(list
, expr
) {
874 cost
+= expand_expression(expr
);
875 } END_FOR_EACH_PTR(expr
);
880 * We can simplify nested position expressions if
881 * this is a simple (single) positional expression.
883 static int expand_pos_expression(struct expression
*expr
)
885 struct expression
*nested
= expr
->init_expr
;
886 unsigned long offset
= expr
->init_offset
;
887 int nr
= expr
->init_nr
;
890 switch (nested
->type
) {
892 offset
+= nested
->init_offset
;
894 expr
->init_offset
= offset
;
898 case EXPR_INITIALIZER
: {
899 struct expression
*reuse
= nested
, *entry
;
901 FOR_EACH_PTR(expr
->expr_list
, entry
) {
902 if (entry
->type
== EXPR_POS
) {
903 entry
->init_offset
+= offset
;
907 * This happens rarely, but it can happen
908 * with bitfields that are all at offset
911 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
913 reuse
->type
= EXPR_POS
;
914 reuse
->ctype
= entry
->ctype
;
915 reuse
->init_offset
= offset
;
917 reuse
->init_expr
= entry
;
918 REPLACE_CURRENT_PTR(entry
, reuse
);
921 } END_FOR_EACH_PTR(entry
);
930 return expand_expression(nested
);
933 static unsigned long bit_offset(const struct expression
*expr
)
935 unsigned long offset
= 0;
936 while (expr
->type
== EXPR_POS
) {
937 offset
+= bytes_to_bits(expr
->init_offset
);
938 expr
= expr
->init_expr
;
940 if (expr
&& expr
->ctype
)
941 offset
+= expr
->ctype
->bit_offset
;
945 static unsigned long bit_range(const struct expression
*expr
)
947 unsigned long range
= 0;
948 unsigned long size
= 0;
949 while (expr
->type
== EXPR_POS
) {
950 unsigned long nr
= expr
->init_nr
;
951 size
= expr
->ctype
->bit_size
;
952 range
+= (nr
- 1) * size
;
953 expr
= expr
->init_expr
;
959 static int compare_expressions(const void *_a
, const void *_b
)
961 const struct expression
*a
= _a
;
962 const struct expression
*b
= _b
;
963 unsigned long a_pos
= bit_offset(a
);
964 unsigned long b_pos
= bit_offset(b
);
966 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
969 static void sort_expression_list(struct expression_list
**list
)
971 sort_list((struct ptr_list
**)list
, compare_expressions
);
974 static void verify_nonoverlapping(struct expression_list
**list
, struct expression
*expr
)
976 struct expression
*a
= NULL
;
977 unsigned long max
= 0;
978 unsigned long whole
= expr
->ctype
->bit_size
;
979 struct expression
*b
;
984 FOR_EACH_PTR(*list
, b
) {
985 unsigned long off
, end
;
986 if (!b
->ctype
|| !b
->ctype
->bit_size
)
989 if (a
&& off
< max
) {
990 warning(a
->pos
, "Initializer entry defined twice");
991 info(b
->pos
, " also defined here");
992 if (!Woverride_init_all
)
995 end
= off
+ bit_range(b
);
996 if (!a
&& !Woverride_init_whole_range
) {
997 // If first entry is the whole range, do not let
998 // any warning about it (this allow to initialize
999 // an array with some default value and then override
1000 // some specific entries).
1001 if (off
== 0 && end
== whole
)
1008 } END_FOR_EACH_PTR(b
);
1011 static int expand_expression(struct expression
*expr
)
1015 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1018 switch (expr
->type
) {
1025 return expand_symbol_expression(expr
);
1027 return expand_binop(expr
);
1030 return expand_logical(expr
);
1033 return expand_comma(expr
);
1036 return expand_compare(expr
);
1038 case EXPR_ASSIGNMENT
:
1039 return expand_assignment(expr
);
1042 return expand_preop(expr
);
1045 return expand_postop(expr
);
1048 case EXPR_FORCE_CAST
:
1049 case EXPR_IMPLIED_CAST
:
1050 return expand_cast(expr
);
1053 return expand_call(expr
);
1056 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
1060 case EXPR_CONDITIONAL
:
1061 return expand_conditional(expr
);
1063 case EXPR_STATEMENT
: {
1064 struct statement
*stmt
= expr
->statement
;
1065 int cost
= expand_statement(stmt
);
1067 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
1068 *expr
= *stmt
->expression
;
1075 case EXPR_INITIALIZER
:
1076 sort_expression_list(&expr
->expr_list
);
1077 verify_nonoverlapping(&expr
->expr_list
, expr
);
1078 return expand_expression_list(expr
->expr_list
);
1080 case EXPR_IDENTIFIER
:
1087 return expand_expression(expr
->base
) + 1;
1090 return expand_pos_expression(expr
);
1093 case EXPR_PTRSIZEOF
:
1096 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1098 case EXPR_ASM_OPERAND
:
1099 expression_error(expr
, "internal front-end error: ASM_OPERAND in expansion?");
1102 return SIDE_EFFECTS
;
1105 static void expand_const_expression(struct expression
*expr
, const char *where
)
1108 expand_expression(expr
);
1109 if (expr
->type
!= EXPR_VALUE
)
1110 expression_error(expr
, "Expected constant expression in %s", where
);
1114 int expand_symbol(struct symbol
*sym
)
1117 struct symbol
*base_type
;
1121 base_type
= sym
->ctype
.base_type
;
1125 retval
= expand_expression(sym
->initializer
);
1126 /* expand the body of the symbol */
1127 if (base_type
->type
== SYM_FN
) {
1128 if (base_type
->stmt
)
1129 expand_statement(base_type
->stmt
);
1134 static void expand_return_expression(struct statement
*stmt
)
1136 expand_expression(stmt
->expression
);
1139 static int expand_if_statement(struct statement
*stmt
)
1141 struct expression
*expr
= stmt
->if_conditional
;
1143 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1146 expand_expression(expr
);
1148 /* This is only valid if nobody jumps into the "dead" side */
1150 /* Simplify constant conditionals without even evaluating the false side */
1151 if (expr
->type
== EXPR_VALUE
) {
1152 struct statement
*simple
;
1153 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1157 stmt
->type
= STMT_NONE
;
1160 expand_statement(simple
);
1162 return SIDE_EFFECTS
;
1165 expand_statement(stmt
->if_true
);
1166 expand_statement(stmt
->if_false
);
1167 return SIDE_EFFECTS
;
1171 * Expanding a compound statement is really just
1172 * about adding up the costs of each individual
1175 * We also collapse a simple compound statement:
1176 * this would trigger for simple inline functions,
1177 * except we would have to check the "return"
1178 * symbol usage. Next time.
1180 static int expand_compound(struct statement
*stmt
)
1182 struct statement
*s
, *last
;
1183 int cost
, statements
;
1186 expand_symbol(stmt
->ret
);
1189 cost
= expand_statement(last
);
1190 statements
= last
!= NULL
;
1191 FOR_EACH_PTR(stmt
->stmts
, s
) {
1194 cost
+= expand_statement(s
);
1195 } END_FOR_EACH_PTR(s
);
1197 if (statements
== 1 && !stmt
->ret
)
1203 static int expand_statement(struct statement
*stmt
)
1208 switch (stmt
->type
) {
1209 case STMT_DECLARATION
: {
1211 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1213 } END_FOR_EACH_PTR(sym
);
1214 return SIDE_EFFECTS
;
1218 expand_return_expression(stmt
);
1219 return SIDE_EFFECTS
;
1221 case STMT_EXPRESSION
:
1222 return expand_expression(stmt
->expression
);
1225 return expand_compound(stmt
);
1228 return expand_if_statement(stmt
);
1231 expand_expression(stmt
->iterator_pre_condition
);
1232 expand_expression(stmt
->iterator_post_condition
);
1233 expand_statement(stmt
->iterator_pre_statement
);
1234 expand_statement(stmt
->iterator_statement
);
1235 expand_statement(stmt
->iterator_post_statement
);
1236 return SIDE_EFFECTS
;
1239 expand_expression(stmt
->switch_expression
);
1240 expand_statement(stmt
->switch_statement
);
1241 return SIDE_EFFECTS
;
1244 expand_const_expression(stmt
->case_expression
, "case statement");
1245 expand_const_expression(stmt
->case_to
, "case statement");
1246 expand_statement(stmt
->case_statement
);
1247 return SIDE_EFFECTS
;
1250 expand_statement(stmt
->label_statement
);
1251 return SIDE_EFFECTS
;
1254 expand_expression(stmt
->goto_expression
);
1255 return SIDE_EFFECTS
;
1260 /* FIXME! Do the asm parameter evaluation! */
1263 expand_expression(stmt
->expression
);
1266 expand_expression(stmt
->range_expression
);
1267 expand_expression(stmt
->range_low
);
1268 expand_expression(stmt
->range_high
);
1271 return SIDE_EFFECTS
;
1274 static inline int bad_integer_constant_expression(struct expression
*expr
)
1276 if (!(expr
->flags
& CEF_ICE
))
1278 if (expr
->taint
& Taint_comma
)
1283 static long long __get_expression_value(struct expression
*expr
, int strict
)
1285 long long value
, mask
;
1286 struct symbol
*ctype
;
1290 ctype
= evaluate_expression(expr
);
1292 expression_error(expr
, "bad constant expression type");
1295 expand_expression(expr
);
1296 if (expr
->type
!= EXPR_VALUE
) {
1298 expression_error(expr
, "bad constant expression");
1301 #if 0 // This complains about "1 ? 1 :__bits_per()" which the kernel use
1302 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1303 expression_error(expr
, "bad integer constant expression");
1308 value
= expr
->value
;
1309 mask
= 1ULL << (ctype
->bit_size
-1);
1312 while (ctype
->type
!= SYM_BASETYPE
)
1313 ctype
= ctype
->ctype
.base_type
;
1314 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1315 value
= value
| mask
| ~(mask
-1);
1320 long long get_expression_value(struct expression
*expr
)
1322 return __get_expression_value(expr
, 0);
1325 long long const_expression_value(struct expression
*expr
)
1327 return __get_expression_value(expr
, 1);
1330 long long get_expression_value_silent(struct expression
*expr
)
1333 return __get_expression_value(expr
, 2);
1336 int expr_truth_value(struct expression
*expr
)
1338 const int saved
= conservative
;
1339 struct symbol
*ctype
;
1344 ctype
= evaluate_expression(expr
);
1349 expand_expression(expr
);
1350 conservative
= saved
;
1353 switch (expr
->type
) {
1358 return expr
->value
!= 0;
1360 return expr
->fvalue
!= 0;
1366 int is_zero_constant(struct expression
*expr
)
1368 const int saved
= conservative
;
1370 expand_expression(expr
);
1371 conservative
= saved
;
1372 return expr
->type
== EXPR_VALUE
&& !expr
->value
;