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
;
70 // expand compound literals (C99 & C11 6.5.2.5)
71 // FIXME: is this the correct way to identify them?
72 // All compound literals are anonymous but is
74 if (sym
->initializer
&& !expr
->symbol_name
)
75 return expand_expression(sym
->initializer
);
77 /* The cost of a symbol expression is lower for on-stack symbols */
78 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
81 static long long get_longlong(struct expression
*expr
)
83 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
84 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
85 long long value
= expr
->value
;
86 long long ormask
, andmask
;
90 andmask
= mask
| (mask
-1);
94 return (value
& andmask
) | ormask
;
97 void cast_value(struct expression
*expr
, struct symbol
*newtype
, struct expression
*old
)
99 struct symbol
*oldtype
= old
->ctype
;
100 int old_size
= oldtype
->bit_size
;
101 int new_size
= newtype
->bit_size
;
102 long long value
, mask
, signmask
;
103 long long oldmask
, oldsignmask
, dropped
;
105 if (is_float_type(newtype
) || is_float_type(oldtype
))
108 // For pointers and integers, we can just move the value around
109 expr
->type
= EXPR_VALUE
;
110 expr
->taint
= old
->taint
;
111 if (old_size
== new_size
) {
112 expr
->value
= old
->value
;
113 expr
->ctype
= newtype
;
117 // expand it to the full "long long" value
118 value
= get_longlong(old
);
119 expr
->ctype
= newtype
;
122 // _Bool requires a zero test rather than truncation.
123 if (is_bool_type(newtype
)) {
124 expr
->value
= !!value
;
125 if (!conservative
&& value
!= 0 && value
!= 1)
126 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
130 // Truncate it to the new size
131 signmask
= 1ULL << (new_size
-1);
132 mask
= signmask
| (signmask
-1);
133 expr
->value
= value
& mask
;
135 // Stop here unless checking for truncation
136 if (!Wcast_truncate
|| conservative
)
139 // Check if we dropped any bits..
140 oldsignmask
= 1ULL << (old_size
-1);
141 oldmask
= oldsignmask
| (oldsignmask
-1);
142 dropped
= oldmask
& ~mask
;
144 // OK if the bits were (and still are) purely sign bits
145 if (value
& dropped
) {
146 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
147 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
154 if (!is_float_type(newtype
)) {
155 value
= (long long)old
->fvalue
;
156 expr
->type
= EXPR_VALUE
;
158 expr
->ctype
= newtype
;
162 if (!is_float_type(oldtype
))
163 expr
->fvalue
= (long double)get_longlong(old
);
165 expr
->fvalue
= old
->fvalue
;
167 if (newtype
->rank
<= 0) {
168 if (newtype
->rank
== 0)
169 expr
->fvalue
= (double)expr
->fvalue
;
171 expr
->fvalue
= (float)expr
->fvalue
;
173 expr
->type
= EXPR_FVALUE
;
174 expr
->ctype
= newtype
;
177 /* Return true if constant shift size is valid */
178 static bool check_shift_count(struct expression
*expr
, struct expression
*right
)
180 struct symbol
*ctype
= expr
->ctype
;
181 long long count
= get_longlong(right
);
183 if (count
>= 0 && count
< ctype
->bit_size
)
189 * CAREFUL! We need to get the size and sign of the
192 #define CONVERT(op,s) (((op)<<1)+(s))
193 #define SIGNED(op) CONVERT(op, 1)
194 #define UNSIGNED(op) CONVERT(op, 0)
195 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
197 struct expression
*left
= expr
->left
, *right
= expr
->right
;
198 unsigned long long v
, l
, r
, mask
;
199 signed long long sl
, sr
;
202 if (right
->type
!= EXPR_VALUE
)
205 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
206 if (!check_shift_count(expr
, right
))
209 if (left
->type
!= EXPR_VALUE
)
211 l
= left
->value
; r
= right
->value
;
212 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
213 mask
= 1ULL << (ctype
->bit_size
-1);
215 if (is_signed
&& (sl
& mask
))
217 if (is_signed
&& (sr
& mask
))
220 switch (CONVERT(expr
->op
,is_signed
)) {
257 if (l
== mask
&& sr
== -1)
270 if (l
== mask
&& sr
== -1)
280 case SIGNED(SPECIAL_LEFTSHIFT
):
281 case UNSIGNED(SPECIAL_LEFTSHIFT
):
285 case SIGNED(SPECIAL_RIGHTSHIFT
):
289 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
296 mask
= mask
| (mask
-1);
297 expr
->value
= v
& mask
;
298 expr
->type
= EXPR_VALUE
;
299 expr
->taint
= left
->taint
| right
->taint
;
303 warning(expr
->pos
, "division by zero");
307 warning(expr
->pos
, "constant integer operation overflow");
311 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
313 struct expression
*left
= expr
->left
, *right
= expr
->right
;
314 unsigned long long l
, r
, mask
;
315 signed long long sl
, sr
;
317 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
319 l
= left
->value
; r
= right
->value
;
320 mask
= 1ULL << (ctype
->bit_size
-1);
327 case '<': expr
->value
= sl
< sr
; break;
328 case '>': expr
->value
= sl
> sr
; break;
329 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
330 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
331 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
332 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
333 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
334 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
335 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
336 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
338 expr
->type
= EXPR_VALUE
;
339 expr
->taint
= left
->taint
| right
->taint
;
343 static int simplify_float_binop(struct expression
*expr
)
345 struct expression
*left
= expr
->left
, *right
= expr
->right
;
346 int rank
= expr
->ctype
->rank
;
347 long double l
, r
, res
;
349 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
357 case '+': res
= l
+ r
; break;
358 case '-': res
= l
- r
; break;
359 case '*': res
= l
* r
; break;
360 case '/': if (!r
) goto Div
;
364 } else if (rank
== 0) {
366 case '+': res
= (double) l
+ (double) r
; break;
367 case '-': res
= (double) l
- (double) r
; break;
368 case '*': res
= (double) l
* (double) r
; break;
369 case '/': if (!r
) goto Div
;
370 res
= (double) l
/ (double) r
; break;
375 case '+': res
= (float)l
+ (float)r
; break;
376 case '-': res
= (float)l
- (float)r
; break;
377 case '*': res
= (float)l
* (float)r
; break;
378 case '/': if (!r
) goto Div
;
379 res
= (float)l
/ (float)r
; break;
383 expr
->type
= EXPR_FVALUE
;
388 warning(expr
->pos
, "division by zero");
392 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
394 struct expression
*left
= expr
->left
, *right
= expr
->right
;
397 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
403 case '<': expr
->value
= l
< r
; break;
404 case '>': expr
->value
= l
> r
; break;
405 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
406 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
407 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
408 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
410 expr
->type
= EXPR_VALUE
;
415 static int expand_binop(struct expression
*expr
)
419 cost
= expand_expression(expr
->left
);
420 cost
+= expand_expression(expr
->right
);
421 if (simplify_int_binop(expr
, expr
->ctype
))
423 if (simplify_float_binop(expr
))
428 static int expand_logical(struct expression
*expr
)
430 struct expression
*left
= expr
->left
;
431 struct expression
*right
;
434 /* Do immediate short-circuiting ... */
435 cost
= expand_expression(left
);
436 if (left
->type
== EXPR_VALUE
) {
437 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
439 expr
->type
= EXPR_VALUE
;
441 expr
->taint
= left
->taint
;
446 expr
->type
= EXPR_VALUE
;
448 expr
->taint
= left
->taint
;
455 rcost
= expand_expression(right
);
456 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
458 * We know the left value doesn't matter, since
459 * otherwise we would have short-circuited it..
461 expr
->type
= EXPR_VALUE
;
462 expr
->value
= right
->value
!= 0;
463 expr
->taint
= left
->taint
| right
->taint
;
468 * If the right side is safe and cheaper than a branch,
469 * just avoid the branch and turn it into a regular binop
472 if (rcost
< BRANCH_COST
) {
473 expr
->type
= EXPR_BINOP
;
474 rcost
-= BRANCH_COST
- 1;
477 return cost
+ BRANCH_COST
+ rcost
;
480 static int expand_comma(struct expression
*expr
)
484 cost
= expand_expression(expr
->left
);
485 cost
+= expand_expression(expr
->right
);
486 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
487 unsigned flags
= expr
->flags
;
489 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
490 *expr
= *expr
->right
;
492 if (expr
->type
== EXPR_VALUE
)
493 expr
->taint
|= Taint_comma
| taint
;
498 #define MOD_IGN (MOD_QUALIFIER)
500 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
502 struct ctype c1
= {.base_type
= left
};
503 struct ctype c2
= {.base_type
= right
};
506 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
507 case SPECIAL_NOTEQUAL
:
508 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
510 return left
->bit_size
< right
->bit_size
;
512 return left
->bit_size
> right
->bit_size
;
514 return left
->bit_size
<= right
->bit_size
;
516 return left
->bit_size
>= right
->bit_size
;
521 static int expand_compare(struct expression
*expr
)
523 struct expression
*left
= expr
->left
, *right
= expr
->right
;
526 cost
= expand_expression(left
);
527 cost
+= expand_expression(right
);
530 /* Type comparison? */
531 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
533 expr
->type
= EXPR_VALUE
;
534 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
538 if (simplify_cmp_binop(expr
, left
->ctype
))
540 if (simplify_float_cmp(expr
, left
->ctype
))
546 static int expand_conditional(struct expression
*expr
)
548 struct expression
*cond
= expr
->conditional
;
549 struct expression
*valt
= expr
->cond_true
;
550 struct expression
*valf
= expr
->cond_false
;
553 cond_cost
= expand_expression(cond
);
554 if (cond
->type
== EXPR_VALUE
) {
555 unsigned flags
= expr
->flags
;
560 cost
= expand_expression(valt
);
563 if (expr
->type
== EXPR_VALUE
)
564 expr
->taint
|= cond
->taint
;
568 cost
= expand_expression(valt
);
569 cost
+= expand_expression(valf
);
571 if (cost
< SELECT_COST
) {
572 expr
->type
= EXPR_SELECT
;
573 cost
-= BRANCH_COST
- 1;
576 return cost
+ cond_cost
+ BRANCH_COST
;
579 static void check_assignment(struct expression
*expr
)
581 struct expression
*right
;
584 case SPECIAL_SHL_ASSIGN
:
585 case SPECIAL_SHR_ASSIGN
:
587 if (right
->type
!= EXPR_VALUE
)
589 check_shift_count(expr
, right
);
595 static int expand_assignment(struct expression
*expr
)
597 expand_expression(expr
->left
);
598 expand_expression(expr
->right
);
601 check_assignment(expr
);
605 static int expand_addressof(struct expression
*expr
)
607 return expand_expression(expr
->unop
);
611 // lookup the type of a struct's memeber at the requested offset
612 static struct symbol
*find_member(struct symbol
*sym
, int offset
)
614 struct ptr_list
*head
, *list
;
616 head
= (struct ptr_list
*) sym
->symbol_list
;
623 for (i
= 0; i
< nr
; i
++) {
624 struct symbol
*ent
= (struct symbol
*) list
->list
[i
];
625 int curr
= ent
->offset
;
631 } while ((list
= list
->next
) != head
);
636 // lookup a suitable default initializer value at the requested offset
637 static struct expression
*default_initializer(struct symbol
*sym
, int offset
)
639 static struct expression value
;
645 sym
= sym
->ctype
.base_type
;
648 type
= find_member(sym
, offset
);
653 type
= sym
->ctype
.base_type
;
659 if (is_integral_type(type
))
660 value
.type
= EXPR_VALUE
;
661 else if (is_float_type(type
))
662 value
.type
= EXPR_FVALUE
;
671 * Look up a trustable initializer value at the requested offset.
673 * Return NULL if no such value can be found or statically trusted.
675 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
677 struct expression
*value
;
679 if (sym
->ctype
.modifiers
& MOD_ACCESS
)
681 value
= sym
->initializer
;
684 if (value
->type
== EXPR_INITIALIZER
) {
685 struct expression
*entry
;
686 FOR_EACH_PTR(value
->expr_list
, entry
) {
687 if (entry
->type
!= EXPR_POS
) {
692 if (entry
->init_offset
< offset
)
694 if (entry
->init_offset
> offset
)
696 return entry
->init_expr
;
697 } END_FOR_EACH_PTR(entry
);
699 value
= default_initializer(sym
, offset
);
704 static int expand_dereference(struct expression
*expr
)
706 struct expression
*unop
= expr
->unop
;
709 expand_expression(unop
);
712 * NOTE! We get a bogus warning right now for some special
713 * cases: apparently I've screwed up the optimization of
714 * a zero-offset dereference, and the ctype is wrong.
716 * Leave the warning in anyway, since this is also a good
717 * test for me to get the type evaluation right..
719 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
720 warning(unop
->pos
, "dereference of noderef expression");
723 * Is it "symbol" or "symbol + offset"?
726 while (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
727 struct expression
*right
= unop
->right
;
728 if (right
->type
!= EXPR_VALUE
)
730 offset
+= right
->value
;
734 if (unop
->type
== EXPR_SYMBOL
) {
735 struct symbol
*sym
= unop
->symbol
;
736 struct symbol
*ctype
= expr
->ctype
;
737 struct expression
*value
= constant_symbol_value(sym
, offset
);
739 /* Const symbol with a constant initializer? */
740 if (value
&& value
->ctype
) {
741 if (ctype
->bit_size
!= value
->ctype
->bit_size
)
743 if (value
->type
== EXPR_VALUE
) {
744 if (!is_integral_type(ctype
))
746 if (is_bitfield_type(value
->ctype
))
748 expr
->type
= EXPR_VALUE
;
749 expr
->value
= value
->value
;
752 } else if (value
->type
== EXPR_FVALUE
) {
753 if (!is_float_type(ctype
))
755 expr
->type
= EXPR_FVALUE
;
756 expr
->fvalue
= value
->fvalue
;
761 /* Direct symbol dereference? Cheap and safe */
762 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
768 static int simplify_preop(struct expression
*expr
)
770 struct expression
*op
= expr
->unop
;
771 unsigned long long v
, mask
;
773 if (op
->type
!= EXPR_VALUE
)
776 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
781 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
785 case '!': v
= !v
; break;
786 case '~': v
= ~v
; break;
789 mask
= mask
| (mask
-1);
790 expr
->value
= v
& mask
;
791 expr
->type
= EXPR_VALUE
;
792 expr
->taint
= op
->taint
;
797 warning(expr
->pos
, "constant integer operation overflow");
801 static int simplify_float_preop(struct expression
*expr
)
803 struct expression
*op
= expr
->unop
;
806 if (op
->type
!= EXPR_FVALUE
)
811 case '-': v
= -v
; break;
815 expr
->type
= EXPR_FVALUE
;
820 * Unary post-ops: x++ and x--
822 static int expand_postop(struct expression
*expr
)
824 expand_expression(expr
->unop
);
828 static int expand_preop(struct expression
*expr
)
834 return expand_dereference(expr
);
837 return expand_addressof(expr
);
839 case SPECIAL_INCREMENT
:
840 case SPECIAL_DECREMENT
:
842 * From a type evaluation standpoint the preops are
843 * the same as the postops
845 return expand_postop(expr
);
850 cost
= expand_expression(expr
->unop
);
852 if (simplify_preop(expr
))
854 if (simplify_float_preop(expr
))
859 static int expand_arguments(struct expression_list
*head
)
862 struct expression
*expr
;
864 FOR_EACH_PTR (head
, expr
) {
865 cost
+= expand_expression(expr
);
866 } END_FOR_EACH_PTR(expr
);
870 static int expand_cast(struct expression
*expr
)
873 struct expression
*target
= expr
->cast_expression
;
875 cost
= expand_expression(target
);
877 /* Simplify normal integer casts.. */
878 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
879 cast_value(expr
, expr
->ctype
, target
);
886 * expand a call expression with a symbol. This
887 * should expand builtins.
889 static int expand_symbol_call(struct expression
*expr
, int cost
)
891 struct expression
*fn
= expr
->fn
;
892 struct symbol
*ctype
= fn
->ctype
;
894 expand_expression(fn
);
896 if (fn
->type
!= EXPR_PREOP
)
899 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
902 def
= ctype
->definition
? ctype
->definition
: ctype
;
903 if (inline_function(expr
, def
)) {
904 struct symbol
*fn
= def
->ctype
.base_type
;
905 struct symbol
*curr
= current_fn
;
908 evaluate_statement(expr
->statement
);
912 cost
= expand_expression(expr
);
918 if (ctype
->op
&& ctype
->op
->expand
)
919 return ctype
->op
->expand(expr
, cost
);
921 if (ctype
->ctype
.modifiers
& MOD_PURE
)
927 static int expand_call(struct expression
*expr
)
931 struct expression
*fn
= expr
->fn
;
933 cost
= expand_arguments(expr
->args
);
936 expression_error(expr
, "function has no type");
939 if (sym
->type
== SYM_NODE
)
940 return expand_symbol_call(expr
, cost
);
945 static int expand_expression_list(struct expression_list
*list
)
948 struct expression
*expr
;
950 FOR_EACH_PTR(list
, expr
) {
951 cost
+= expand_expression(expr
);
952 } END_FOR_EACH_PTR(expr
);
957 * We can simplify nested position expressions if
958 * this is a simple (single) positional expression.
960 static int expand_pos_expression(struct expression
*expr
)
962 struct expression
*nested
= expr
->init_expr
;
963 unsigned long offset
= expr
->init_offset
;
964 int nr
= expr
->init_nr
;
967 switch (nested
->type
) {
969 offset
+= nested
->init_offset
;
971 expr
->init_offset
= offset
;
975 case EXPR_INITIALIZER
: {
976 struct expression
*reuse
= nested
, *entry
;
978 FOR_EACH_PTR(expr
->expr_list
, entry
) {
979 if (entry
->type
== EXPR_POS
) {
980 entry
->init_offset
+= offset
;
984 * This happens rarely, but it can happen
985 * with bitfields that are all at offset
988 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
990 reuse
->type
= EXPR_POS
;
991 reuse
->ctype
= entry
->ctype
;
992 reuse
->init_offset
= offset
;
994 reuse
->init_expr
= entry
;
995 REPLACE_CURRENT_PTR(entry
, reuse
);
998 } END_FOR_EACH_PTR(entry
);
1007 return expand_expression(nested
);
1010 static unsigned long bit_offset(const struct expression
*expr
)
1012 unsigned long offset
= 0;
1013 while (expr
->type
== EXPR_POS
) {
1014 offset
+= bytes_to_bits(expr
->init_offset
);
1015 expr
= expr
->init_expr
;
1017 if (expr
&& expr
->ctype
)
1018 offset
+= expr
->ctype
->bit_offset
;
1022 static unsigned long bit_range(const struct expression
*expr
)
1024 unsigned long range
= 0;
1025 unsigned long size
= 0;
1026 while (expr
->type
== EXPR_POS
) {
1027 unsigned long nr
= expr
->init_nr
;
1028 size
= expr
->ctype
->bit_size
;
1029 range
+= (nr
- 1) * size
;
1030 expr
= expr
->init_expr
;
1036 static int compare_expressions(const void *_a
, const void *_b
)
1038 const struct expression
*a
= _a
;
1039 const struct expression
*b
= _b
;
1040 unsigned long a_pos
= bit_offset(a
);
1041 unsigned long b_pos
= bit_offset(b
);
1043 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
1046 static void sort_expression_list(struct expression_list
**list
)
1048 sort_list((struct ptr_list
**)list
, compare_expressions
);
1051 static void verify_nonoverlapping(struct expression_list
**list
, struct expression
*expr
)
1053 struct expression
*a
= NULL
;
1054 unsigned long max
= 0;
1055 unsigned long whole
= expr
->ctype
->bit_size
;
1056 struct expression
*b
;
1058 if (!Woverride_init
)
1061 FOR_EACH_PTR(*list
, b
) {
1062 unsigned long off
, end
;
1063 if (!b
->ctype
|| !b
->ctype
->bit_size
)
1065 off
= bit_offset(b
);
1066 if (a
&& off
< max
) {
1067 warning(a
->pos
, "Initializer entry defined twice");
1068 info(b
->pos
, " also defined here");
1069 if (!Woverride_init_all
)
1072 end
= off
+ bit_range(b
);
1073 if (!a
&& !Woverride_init_whole_range
) {
1074 // If first entry is the whole range, do not let
1075 // any warning about it (this allow to initialize
1076 // an array with some default value and then override
1077 // some specific entries).
1078 if (off
== 0 && end
== whole
)
1085 } END_FOR_EACH_PTR(b
);
1088 static int expand_expression(struct expression
*expr
)
1092 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1095 switch (expr
->type
) {
1102 return expand_symbol_expression(expr
);
1104 return expand_binop(expr
);
1107 return expand_logical(expr
);
1110 return expand_comma(expr
);
1113 return expand_compare(expr
);
1115 case EXPR_ASSIGNMENT
:
1116 return expand_assignment(expr
);
1119 return expand_preop(expr
);
1122 return expand_postop(expr
);
1125 case EXPR_FORCE_CAST
:
1126 case EXPR_IMPLIED_CAST
:
1127 return expand_cast(expr
);
1130 return expand_call(expr
);
1133 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
1137 case EXPR_CONDITIONAL
:
1138 return expand_conditional(expr
);
1140 case EXPR_STATEMENT
: {
1141 struct statement
*stmt
= expr
->statement
;
1142 int cost
= expand_statement(stmt
);
1144 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
1145 *expr
= *stmt
->expression
;
1152 case EXPR_INITIALIZER
:
1153 sort_expression_list(&expr
->expr_list
);
1154 verify_nonoverlapping(&expr
->expr_list
, expr
);
1155 return expand_expression_list(expr
->expr_list
);
1157 case EXPR_IDENTIFIER
:
1164 return expand_expression(expr
->base
) + 1;
1167 return expand_pos_expression(expr
);
1171 case EXPR_PTRSIZEOF
:
1174 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1177 return SIDE_EFFECTS
;
1180 static void expand_const_expression(struct expression
*expr
, const char *where
)
1183 expand_expression(expr
);
1184 if (expr
->type
!= EXPR_VALUE
) {
1185 expression_error(expr
, "Expected constant expression in %s", where
);
1186 expr
->ctype
= &int_ctype
;
1187 expr
->type
= EXPR_VALUE
;
1193 int expand_symbol(struct symbol
*sym
)
1196 struct symbol
*base_type
;
1200 base_type
= sym
->ctype
.base_type
;
1204 retval
= expand_expression(sym
->initializer
);
1205 /* expand the body of the symbol */
1206 if (base_type
->type
== SYM_FN
) {
1207 if (base_type
->stmt
)
1208 expand_statement(base_type
->stmt
);
1213 static void expand_return_expression(struct statement
*stmt
)
1215 expand_expression(stmt
->expression
);
1218 static int expand_if_statement(struct statement
*stmt
)
1220 struct expression
*expr
= stmt
->if_conditional
;
1222 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1225 expand_expression(expr
);
1227 /* This is only valid if nobody jumps into the "dead" side */
1229 /* Simplify constant conditionals without even evaluating the false side */
1230 if (expr
->type
== EXPR_VALUE
) {
1231 struct statement
*simple
;
1232 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1236 stmt
->type
= STMT_NONE
;
1239 expand_statement(simple
);
1241 return SIDE_EFFECTS
;
1244 expand_statement(stmt
->if_true
);
1245 expand_statement(stmt
->if_false
);
1246 return SIDE_EFFECTS
;
1249 static int expand_asm_statement(struct statement
*stmt
)
1251 struct asm_operand
*op
;
1254 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1255 cost
+= expand_expression(op
->expr
);
1256 } END_FOR_EACH_PTR(op
);
1258 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
1259 cost
+= expand_expression(op
->expr
);
1260 } END_FOR_EACH_PTR(op
);
1266 * Expanding a compound statement is really just
1267 * about adding up the costs of each individual
1270 * We also collapse a simple compound statement:
1271 * this would trigger for simple inline functions,
1272 * except we would have to check the "return"
1273 * symbol usage. Next time.
1275 static int expand_compound(struct statement
*stmt
)
1277 struct statement
*s
, *last
;
1278 int cost
, statements
;
1281 expand_symbol(stmt
->ret
);
1284 cost
= expand_statement(last
);
1285 statements
= last
!= NULL
;
1286 FOR_EACH_PTR(stmt
->stmts
, s
) {
1289 cost
+= expand_statement(s
);
1290 } END_FOR_EACH_PTR(s
);
1292 if (statements
== 1 && !stmt
->ret
)
1298 static int expand_statement(struct statement
*stmt
)
1303 switch (stmt
->type
) {
1304 case STMT_DECLARATION
: {
1306 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1308 } END_FOR_EACH_PTR(sym
);
1309 return SIDE_EFFECTS
;
1313 expand_return_expression(stmt
);
1314 return SIDE_EFFECTS
;
1316 case STMT_EXPRESSION
:
1317 return expand_expression(stmt
->expression
);
1320 return expand_compound(stmt
);
1323 return expand_if_statement(stmt
);
1326 expand_expression(stmt
->iterator_pre_condition
);
1327 expand_expression(stmt
->iterator_post_condition
);
1328 expand_statement(stmt
->iterator_pre_statement
);
1329 expand_statement(stmt
->iterator_statement
);
1330 expand_statement(stmt
->iterator_post_statement
);
1331 return SIDE_EFFECTS
;
1334 expand_expression(stmt
->switch_expression
);
1335 expand_statement(stmt
->switch_statement
);
1336 return SIDE_EFFECTS
;
1339 expand_const_expression(stmt
->case_expression
, "case statement");
1340 expand_const_expression(stmt
->case_to
, "case statement");
1341 expand_statement(stmt
->case_statement
);
1342 return SIDE_EFFECTS
;
1345 expand_statement(stmt
->label_statement
);
1346 return SIDE_EFFECTS
;
1349 expand_expression(stmt
->goto_expression
);
1350 return SIDE_EFFECTS
;
1355 expand_asm_statement(stmt
);
1358 expand_expression(stmt
->expression
);
1361 expand_expression(stmt
->range_expression
);
1362 expand_expression(stmt
->range_low
);
1363 expand_expression(stmt
->range_high
);
1366 return SIDE_EFFECTS
;
1369 static inline int bad_integer_constant_expression(struct expression
*expr
)
1371 if (!(expr
->flags
& CEF_ICE
))
1373 if (expr
->taint
& Taint_comma
)
1378 static long long __get_expression_value(struct expression
*expr
, int strict
)
1380 long long value
, mask
;
1381 struct symbol
*ctype
;
1385 ctype
= evaluate_expression(expr
);
1387 expression_error(expr
, "bad constant expression type");
1390 expand_expression(expr
);
1391 if (expr
->type
!= EXPR_VALUE
) {
1393 expression_error(expr
, "bad constant expression");
1396 #if 0 // This complains about "1 ? 1 :__bits_per()" which the kernel use
1397 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1398 expression_error(expr
, "bad integer constant expression");
1403 value
= expr
->value
;
1404 mask
= 1ULL << (ctype
->bit_size
-1);
1407 while (ctype
->type
!= SYM_BASETYPE
)
1408 ctype
= ctype
->ctype
.base_type
;
1409 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1410 value
= value
| mask
| ~(mask
-1);
1415 long long get_expression_value(struct expression
*expr
)
1417 return __get_expression_value(expr
, 0);
1420 long long const_expression_value(struct expression
*expr
)
1422 return __get_expression_value(expr
, 1);
1425 long long get_expression_value_silent(struct expression
*expr
)
1428 return __get_expression_value(expr
, 2);
1431 int expr_truth_value(struct expression
*expr
)
1433 const int saved
= conservative
;
1434 struct symbol
*ctype
;
1439 ctype
= evaluate_expression(expr
);
1444 expand_expression(expr
);
1445 conservative
= saved
;
1448 switch (expr
->type
) {
1453 return expr
->value
!= 0;
1455 return expr
->fvalue
!= 0;
1461 int is_zero_constant(struct expression
*expr
)
1463 const int saved
= conservative
;
1465 expand_expression(expr
);
1466 conservative
= saved
;
1467 return expr
->type
== EXPR_VALUE
&& !expr
->value
;