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
,
98 struct expression
*old
, struct symbol
*oldtype
)
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
;
116 // expand it to the full "long long" value
117 value
= get_longlong(old
);
120 // _Bool requires a zero test rather than truncation.
121 if (is_bool_type(newtype
)) {
122 expr
->value
= !!value
;
123 if (!conservative
&& value
!= 0 && value
!= 1)
124 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
128 // Truncate it to the new size
129 signmask
= 1ULL << (new_size
-1);
130 mask
= signmask
| (signmask
-1);
131 expr
->value
= value
& mask
;
133 // Stop here unless checking for truncation
134 if (!Wcast_truncate
|| conservative
)
137 // Check if we dropped any bits..
138 oldsignmask
= 1ULL << (old_size
-1);
139 oldmask
= oldsignmask
| (oldsignmask
-1);
140 dropped
= oldmask
& ~mask
;
142 // OK if the bits were (and still are) purely sign bits
143 if (value
& dropped
) {
144 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
145 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
152 if (!is_float_type(newtype
)) {
153 value
= (long long)old
->fvalue
;
154 expr
->type
= EXPR_VALUE
;
159 if (!is_float_type(oldtype
))
160 expr
->fvalue
= (long double)get_longlong(old
);
162 expr
->fvalue
= old
->fvalue
;
164 if (newtype
->rank
<= 0) {
165 if (newtype
->rank
== 0)
166 expr
->fvalue
= (double)expr
->fvalue
;
168 expr
->fvalue
= (float)expr
->fvalue
;
170 expr
->type
= EXPR_FVALUE
;
173 static void warn_shift_count(struct expression
*expr
, struct symbol
*ctype
, long long count
)
176 if (!Wshift_count_negative
)
178 warning(expr
->pos
, "shift count is negative (%lld)", count
);
181 if (ctype
->type
== SYM_NODE
)
182 ctype
= ctype
->ctype
.base_type
;
184 if (!Wshift_count_overflow
)
186 warning(expr
->pos
, "shift too big (%llu) for type %s", count
, show_typename(ctype
));
189 /* Return true if constant shift size is valid */
190 static bool check_shift_count(struct expression
*expr
, struct expression
*right
)
192 struct symbol
*ctype
= expr
->ctype
;
193 long long count
= get_longlong(right
);
195 if (count
>= 0 && count
< ctype
->bit_size
)
198 warn_shift_count(expr
, ctype
, count
);
203 * CAREFUL! We need to get the size and sign of the
206 #define CONVERT(op,s) (((op)<<1)+(s))
207 #define SIGNED(op) CONVERT(op, 1)
208 #define UNSIGNED(op) CONVERT(op, 0)
209 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
211 struct expression
*left
= expr
->left
, *right
= expr
->right
;
212 unsigned long long v
, l
, r
, mask
;
213 signed long long sl
, sr
;
216 if (right
->type
!= EXPR_VALUE
)
219 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
220 if (!check_shift_count(expr
, right
))
223 if (left
->type
!= EXPR_VALUE
)
225 l
= left
->value
; r
= right
->value
;
226 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
227 mask
= 1ULL << (ctype
->bit_size
-1);
229 if (is_signed
&& (sl
& mask
))
231 if (is_signed
&& (sr
& mask
))
234 switch (CONVERT(expr
->op
,is_signed
)) {
271 if (l
== mask
&& sr
== -1)
284 if (l
== mask
&& sr
== -1)
294 case SIGNED(SPECIAL_LEFTSHIFT
):
295 case UNSIGNED(SPECIAL_LEFTSHIFT
):
299 case SIGNED(SPECIAL_RIGHTSHIFT
):
303 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
310 mask
= mask
| (mask
-1);
311 expr
->value
= v
& mask
;
312 expr
->type
= EXPR_VALUE
;
313 expr
->taint
= left
->taint
| right
->taint
;
317 warning(expr
->pos
, "division by zero");
321 warning(expr
->pos
, "constant integer operation overflow");
325 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
327 struct expression
*left
= expr
->left
, *right
= expr
->right
;
328 unsigned long long l
, r
, mask
;
329 signed long long sl
, sr
;
331 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
333 l
= left
->value
; r
= right
->value
;
334 mask
= 1ULL << (ctype
->bit_size
-1);
341 case '<': expr
->value
= sl
< sr
; break;
342 case '>': expr
->value
= sl
> sr
; break;
343 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
344 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
345 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
346 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
347 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
348 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
349 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
350 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
352 expr
->type
= EXPR_VALUE
;
353 expr
->taint
= left
->taint
| right
->taint
;
357 static int simplify_float_binop(struct expression
*expr
)
359 struct expression
*left
= expr
->left
, *right
= expr
->right
;
360 int rank
= expr
->ctype
->rank
;
361 long double l
, r
, res
;
363 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
371 case '+': res
= l
+ r
; break;
372 case '-': res
= l
- r
; break;
373 case '*': res
= l
* r
; break;
374 case '/': if (!r
) goto Div
;
378 } else if (rank
== 0) {
380 case '+': res
= (double) l
+ (double) r
; break;
381 case '-': res
= (double) l
- (double) r
; break;
382 case '*': res
= (double) l
* (double) r
; break;
383 case '/': if (!r
) goto Div
;
384 res
= (double) l
/ (double) r
; break;
389 case '+': res
= (float)l
+ (float)r
; break;
390 case '-': res
= (float)l
- (float)r
; break;
391 case '*': res
= (float)l
* (float)r
; break;
392 case '/': if (!r
) goto Div
;
393 res
= (float)l
/ (float)r
; break;
397 expr
->type
= EXPR_FVALUE
;
402 warning(expr
->pos
, "division by zero");
406 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
408 struct expression
*left
= expr
->left
, *right
= expr
->right
;
411 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
417 case '<': expr
->value
= l
< r
; break;
418 case '>': expr
->value
= l
> r
; break;
419 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
420 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
421 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
422 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
424 expr
->type
= EXPR_VALUE
;
429 static int expand_binop(struct expression
*expr
)
433 cost
= expand_expression(expr
->left
);
434 cost
+= expand_expression(expr
->right
);
435 if (simplify_int_binop(expr
, expr
->ctype
))
437 if (simplify_float_binop(expr
))
442 static int expand_logical(struct expression
*expr
)
444 struct expression
*left
= expr
->left
;
445 struct expression
*right
;
448 /* Do immediate short-circuiting ... */
449 cost
= expand_expression(left
);
450 if (left
->type
== EXPR_VALUE
) {
451 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
453 expr
->type
= EXPR_VALUE
;
455 expr
->taint
= left
->taint
;
460 expr
->type
= EXPR_VALUE
;
462 expr
->taint
= left
->taint
;
469 rcost
= expand_expression(right
);
470 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
472 * We know the left value doesn't matter, since
473 * otherwise we would have short-circuited it..
475 expr
->type
= EXPR_VALUE
;
476 expr
->value
= right
->value
!= 0;
477 expr
->taint
= left
->taint
| right
->taint
;
482 * If the right side is safe and cheaper than a branch,
483 * just avoid the branch and turn it into a regular binop
486 if (rcost
< BRANCH_COST
) {
487 expr
->type
= EXPR_BINOP
;
488 rcost
-= BRANCH_COST
- 1;
491 return cost
+ BRANCH_COST
+ rcost
;
494 static int expand_comma(struct expression
*expr
)
498 cost
= expand_expression(expr
->left
);
499 cost
+= expand_expression(expr
->right
);
500 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
501 unsigned flags
= expr
->flags
;
503 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
504 *expr
= *expr
->right
;
506 if (expr
->type
== EXPR_VALUE
)
507 expr
->taint
|= Taint_comma
| taint
;
512 #define MOD_IGN (MOD_QUALIFIER)
514 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
516 struct ctype c1
= {.base_type
= left
};
517 struct ctype c2
= {.base_type
= right
};
520 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
521 case SPECIAL_NOTEQUAL
:
522 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
524 return left
->bit_size
< right
->bit_size
;
526 return left
->bit_size
> right
->bit_size
;
528 return left
->bit_size
<= right
->bit_size
;
530 return left
->bit_size
>= right
->bit_size
;
535 static int expand_compare(struct expression
*expr
)
537 struct expression
*left
= expr
->left
, *right
= expr
->right
;
540 cost
= expand_expression(left
);
541 cost
+= expand_expression(right
);
544 /* Type comparison? */
545 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
547 expr
->type
= EXPR_VALUE
;
548 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
552 if (simplify_cmp_binop(expr
, left
->ctype
))
554 if (simplify_float_cmp(expr
, left
->ctype
))
560 static int expand_conditional(struct expression
*expr
)
562 struct expression
*cond
= expr
->conditional
;
563 struct expression
*valt
= expr
->cond_true
;
564 struct expression
*valf
= expr
->cond_false
;
567 cond_cost
= expand_expression(cond
);
568 if (cond
->type
== EXPR_VALUE
) {
569 unsigned flags
= expr
->flags
;
574 cost
= expand_expression(valt
);
577 if (expr
->type
== EXPR_VALUE
)
578 expr
->taint
|= cond
->taint
;
582 cost
= expand_expression(valt
);
583 cost
+= expand_expression(valf
);
585 if (cost
< SELECT_COST
) {
586 expr
->type
= EXPR_SELECT
;
587 cost
-= BRANCH_COST
- 1;
590 return cost
+ cond_cost
+ BRANCH_COST
;
593 static void check_assignment(struct expression
*expr
)
595 struct expression
*right
;
598 case SPECIAL_SHL_ASSIGN
:
599 case SPECIAL_SHR_ASSIGN
:
601 if (right
->type
!= EXPR_VALUE
)
603 check_shift_count(expr
, right
);
609 static int expand_assignment(struct expression
*expr
)
611 expand_expression(expr
->left
);
612 expand_expression(expr
->right
);
615 check_assignment(expr
);
619 static int expand_addressof(struct expression
*expr
)
621 return expand_expression(expr
->unop
);
625 // lookup the type of a struct's memeber at the requested offset
626 static struct symbol
*find_member(struct symbol
*sym
, int offset
)
628 struct ptr_list
*head
, *list
;
630 head
= (struct ptr_list
*) sym
->symbol_list
;
637 for (i
= 0; i
< nr
; i
++) {
638 struct symbol
*ent
= (struct symbol
*) list
->list
[i
];
639 int curr
= ent
->offset
;
645 } while ((list
= list
->next
) != head
);
650 // lookup a suitable default initializer value at the requested offset
651 static struct expression
*default_initializer(struct symbol
*sym
, int offset
)
653 static struct expression value
;
659 sym
= sym
->ctype
.base_type
;
662 type
= find_member(sym
, offset
);
667 type
= sym
->ctype
.base_type
;
673 if (is_integral_type(type
))
674 value
.type
= EXPR_VALUE
;
675 else if (is_float_type(type
))
676 value
.type
= EXPR_FVALUE
;
685 * Look up a trustable initializer value at the requested offset.
687 * Return NULL if no such value can be found or statically trusted.
689 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
691 struct expression
*value
;
693 if (sym
->ctype
.modifiers
& MOD_ACCESS
)
695 value
= sym
->initializer
;
698 if (value
->type
== EXPR_INITIALIZER
) {
699 struct expression
*entry
;
700 FOR_EACH_PTR(value
->expr_list
, entry
) {
701 if (entry
->type
!= EXPR_POS
) {
706 if (entry
->init_offset
< offset
)
708 if (entry
->init_offset
> offset
)
710 return entry
->init_expr
;
711 } END_FOR_EACH_PTR(entry
);
713 value
= default_initializer(sym
, offset
);
718 static int expand_dereference(struct expression
*expr
)
720 struct expression
*unop
= expr
->unop
;
723 expand_expression(unop
);
726 * NOTE! We get a bogus warning right now for some special
727 * cases: apparently I've screwed up the optimization of
728 * a zero-offset dereference, and the ctype is wrong.
730 * Leave the warning in anyway, since this is also a good
731 * test for me to get the type evaluation right..
733 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
734 warning(unop
->pos
, "dereference of noderef expression");
737 * Is it "symbol" or "symbol + offset"?
740 while (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
741 struct expression
*right
= unop
->right
;
742 if (right
->type
!= EXPR_VALUE
)
744 offset
+= right
->value
;
748 if (unop
->type
== EXPR_SYMBOL
) {
749 struct symbol
*sym
= unop
->symbol
;
750 struct symbol
*ctype
= expr
->ctype
;
751 struct expression
*value
= constant_symbol_value(sym
, offset
);
753 /* Const symbol with a constant initializer? */
754 if (value
&& value
->ctype
) {
755 if (ctype
->bit_size
!= value
->ctype
->bit_size
)
757 if (value
->type
== EXPR_VALUE
) {
758 if (!is_integral_type(ctype
))
760 if (is_bitfield_type(value
->ctype
))
762 expr
->type
= EXPR_VALUE
;
763 expr
->value
= value
->value
;
766 } else if (value
->type
== EXPR_FVALUE
) {
767 if (!is_float_type(ctype
))
769 expr
->type
= EXPR_FVALUE
;
770 expr
->fvalue
= value
->fvalue
;
775 /* Direct symbol dereference? Cheap and safe */
776 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
782 static int simplify_preop(struct expression
*expr
)
784 struct expression
*op
= expr
->unop
;
785 unsigned long long v
, mask
;
787 if (op
->type
!= EXPR_VALUE
)
790 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
795 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
799 case '!': v
= !v
; break;
800 case '~': v
= ~v
; break;
803 mask
= mask
| (mask
-1);
804 expr
->value
= v
& mask
;
805 expr
->type
= EXPR_VALUE
;
806 expr
->taint
= op
->taint
;
811 warning(expr
->pos
, "constant integer operation overflow");
815 static int simplify_float_preop(struct expression
*expr
)
817 struct expression
*op
= expr
->unop
;
820 if (op
->type
!= EXPR_FVALUE
)
825 case '-': v
= -v
; break;
829 expr
->type
= EXPR_FVALUE
;
834 * Unary post-ops: x++ and x--
836 static int expand_postop(struct expression
*expr
)
838 expand_expression(expr
->unop
);
842 static int expand_preop(struct expression
*expr
)
848 return expand_dereference(expr
);
851 return expand_addressof(expr
);
853 case SPECIAL_INCREMENT
:
854 case SPECIAL_DECREMENT
:
856 * From a type evaluation standpoint the preops are
857 * the same as the postops
859 return expand_postop(expr
);
864 cost
= expand_expression(expr
->unop
);
866 if (simplify_preop(expr
))
868 if (simplify_float_preop(expr
))
873 static int expand_arguments(struct expression_list
*head
)
876 struct expression
*expr
;
878 FOR_EACH_PTR (head
, expr
) {
879 cost
+= expand_expression(expr
);
880 } END_FOR_EACH_PTR(expr
);
884 static int expand_cast(struct expression
*expr
)
887 struct expression
*target
= expr
->cast_expression
;
889 cost
= expand_expression(target
);
891 /* Simplify normal integer casts.. */
892 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
893 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
900 * expand a call expression with a symbol. This
901 * should expand builtins.
903 static int expand_symbol_call(struct expression
*expr
, int cost
)
905 struct expression
*fn
= expr
->fn
;
906 struct symbol
*ctype
= fn
->ctype
;
908 expand_expression(fn
);
910 if (fn
->type
!= EXPR_PREOP
)
913 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
916 def
= ctype
->definition
? ctype
->definition
: ctype
;
917 if (inline_function(expr
, def
)) {
918 struct symbol
*fn
= def
->ctype
.base_type
;
919 struct symbol
*curr
= current_fn
;
922 evaluate_statement(expr
->statement
);
926 cost
= expand_expression(expr
);
932 if (ctype
->op
&& ctype
->op
->expand
)
933 return ctype
->op
->expand(expr
, cost
);
935 if (ctype
->ctype
.modifiers
& MOD_PURE
)
941 static int expand_call(struct expression
*expr
)
945 struct expression
*fn
= expr
->fn
;
947 cost
= expand_arguments(expr
->args
);
950 expression_error(expr
, "function has no type");
953 if (sym
->type
== SYM_NODE
)
954 return expand_symbol_call(expr
, cost
);
959 static int expand_expression_list(struct expression_list
*list
)
962 struct expression
*expr
;
964 FOR_EACH_PTR(list
, expr
) {
965 cost
+= expand_expression(expr
);
966 } END_FOR_EACH_PTR(expr
);
971 * We can simplify nested position expressions if
972 * this is a simple (single) positional expression.
974 static int expand_pos_expression(struct expression
*expr
)
976 struct expression
*nested
= expr
->init_expr
;
977 unsigned long offset
= expr
->init_offset
;
978 int nr
= expr
->init_nr
;
981 switch (nested
->type
) {
983 offset
+= nested
->init_offset
;
985 expr
->init_offset
= offset
;
989 case EXPR_INITIALIZER
: {
990 struct expression
*reuse
= nested
, *entry
;
992 FOR_EACH_PTR(expr
->expr_list
, entry
) {
993 if (entry
->type
== EXPR_POS
) {
994 entry
->init_offset
+= offset
;
998 * This happens rarely, but it can happen
999 * with bitfields that are all at offset
1002 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
1004 reuse
->type
= EXPR_POS
;
1005 reuse
->ctype
= entry
->ctype
;
1006 reuse
->init_offset
= offset
;
1008 reuse
->init_expr
= entry
;
1009 REPLACE_CURRENT_PTR(entry
, reuse
);
1012 } END_FOR_EACH_PTR(entry
);
1021 return expand_expression(nested
);
1024 static unsigned long bit_offset(const struct expression
*expr
)
1026 unsigned long offset
= 0;
1027 while (expr
->type
== EXPR_POS
) {
1028 offset
+= bytes_to_bits(expr
->init_offset
);
1029 expr
= expr
->init_expr
;
1031 if (expr
&& expr
->ctype
)
1032 offset
+= expr
->ctype
->bit_offset
;
1036 static unsigned long bit_range(const struct expression
*expr
)
1038 unsigned long range
= 0;
1039 unsigned long size
= 0;
1040 while (expr
->type
== EXPR_POS
) {
1041 unsigned long nr
= expr
->init_nr
;
1042 size
= expr
->ctype
->bit_size
;
1043 range
+= (nr
- 1) * size
;
1044 expr
= expr
->init_expr
;
1050 static int compare_expressions(const void *_a
, const void *_b
)
1052 const struct expression
*a
= _a
;
1053 const struct expression
*b
= _b
;
1054 unsigned long a_pos
= bit_offset(a
);
1055 unsigned long b_pos
= bit_offset(b
);
1057 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
1060 static void sort_expression_list(struct expression_list
**list
)
1062 sort_list((struct ptr_list
**)list
, compare_expressions
);
1065 static void verify_nonoverlapping(struct expression_list
**list
, struct expression
*expr
)
1067 struct expression
*a
= NULL
;
1068 unsigned long max
= 0;
1069 unsigned long whole
= expr
->ctype
->bit_size
;
1070 struct expression
*b
;
1072 if (!Woverride_init
)
1075 FOR_EACH_PTR(*list
, b
) {
1076 unsigned long off
, end
;
1077 if (!b
->ctype
|| !b
->ctype
->bit_size
)
1079 off
= bit_offset(b
);
1080 if (a
&& off
< max
) {
1081 warning(a
->pos
, "Initializer entry defined twice");
1082 info(b
->pos
, " also defined here");
1083 if (!Woverride_init_all
)
1086 end
= off
+ bit_range(b
);
1087 if (!a
&& !Woverride_init_whole_range
) {
1088 // If first entry is the whole range, do not let
1089 // any warning about it (this allow to initialize
1090 // an array with some default value and then override
1091 // some specific entries).
1092 if (off
== 0 && end
== whole
)
1099 } END_FOR_EACH_PTR(b
);
1102 static int expand_expression(struct expression
*expr
)
1106 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1109 switch (expr
->type
) {
1116 return expand_symbol_expression(expr
);
1118 return expand_binop(expr
);
1121 return expand_logical(expr
);
1124 return expand_comma(expr
);
1127 return expand_compare(expr
);
1129 case EXPR_ASSIGNMENT
:
1130 return expand_assignment(expr
);
1133 return expand_preop(expr
);
1136 return expand_postop(expr
);
1139 case EXPR_FORCE_CAST
:
1140 case EXPR_IMPLIED_CAST
:
1141 return expand_cast(expr
);
1144 return expand_call(expr
);
1147 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
1151 case EXPR_CONDITIONAL
:
1152 return expand_conditional(expr
);
1154 case EXPR_STATEMENT
: {
1155 struct statement
*stmt
= expr
->statement
;
1156 int cost
= expand_statement(stmt
);
1158 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
1159 *expr
= *stmt
->expression
;
1166 case EXPR_INITIALIZER
:
1167 sort_expression_list(&expr
->expr_list
);
1168 verify_nonoverlapping(&expr
->expr_list
, expr
);
1169 return expand_expression_list(expr
->expr_list
);
1171 case EXPR_IDENTIFIER
:
1178 return expand_expression(expr
->base
) + 1;
1181 return expand_pos_expression(expr
);
1185 case EXPR_PTRSIZEOF
:
1188 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1191 return SIDE_EFFECTS
;
1194 static void expand_const_expression(struct expression
*expr
, const char *where
)
1197 expand_expression(expr
);
1198 if (expr
->type
!= EXPR_VALUE
)
1199 expression_error(expr
, "Expected constant expression in %s", where
);
1203 int expand_symbol(struct symbol
*sym
)
1206 struct symbol
*base_type
;
1210 base_type
= sym
->ctype
.base_type
;
1214 retval
= expand_expression(sym
->initializer
);
1215 /* expand the body of the symbol */
1216 if (base_type
->type
== SYM_FN
) {
1217 if (base_type
->stmt
)
1218 expand_statement(base_type
->stmt
);
1223 static void expand_return_expression(struct statement
*stmt
)
1225 expand_expression(stmt
->expression
);
1228 static int expand_if_statement(struct statement
*stmt
)
1230 struct expression
*expr
= stmt
->if_conditional
;
1232 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1235 expand_expression(expr
);
1237 /* This is only valid if nobody jumps into the "dead" side */
1239 /* Simplify constant conditionals without even evaluating the false side */
1240 if (expr
->type
== EXPR_VALUE
) {
1241 struct statement
*simple
;
1242 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1246 stmt
->type
= STMT_NONE
;
1249 expand_statement(simple
);
1251 return SIDE_EFFECTS
;
1254 expand_statement(stmt
->if_true
);
1255 expand_statement(stmt
->if_false
);
1256 return SIDE_EFFECTS
;
1259 static int expand_asm_statement(struct statement
*stmt
)
1261 struct asm_operand
*op
;
1264 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
1265 cost
+= expand_expression(op
->expr
);
1266 } END_FOR_EACH_PTR(op
);
1268 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
1269 cost
+= expand_expression(op
->expr
);
1270 } END_FOR_EACH_PTR(op
);
1276 * Expanding a compound statement is really just
1277 * about adding up the costs of each individual
1280 * We also collapse a simple compound statement:
1281 * this would trigger for simple inline functions,
1282 * except we would have to check the "return"
1283 * symbol usage. Next time.
1285 static int expand_compound(struct statement
*stmt
)
1287 struct statement
*s
, *last
;
1288 int cost
, statements
;
1291 expand_symbol(stmt
->ret
);
1294 cost
= expand_statement(last
);
1295 statements
= last
!= NULL
;
1296 FOR_EACH_PTR(stmt
->stmts
, s
) {
1299 cost
+= expand_statement(s
);
1300 } END_FOR_EACH_PTR(s
);
1302 if (statements
== 1 && !stmt
->ret
)
1308 static int expand_statement(struct statement
*stmt
)
1313 switch (stmt
->type
) {
1314 case STMT_DECLARATION
: {
1316 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1318 } END_FOR_EACH_PTR(sym
);
1319 return SIDE_EFFECTS
;
1323 expand_return_expression(stmt
);
1324 return SIDE_EFFECTS
;
1326 case STMT_EXPRESSION
:
1327 return expand_expression(stmt
->expression
);
1330 return expand_compound(stmt
);
1333 return expand_if_statement(stmt
);
1336 expand_expression(stmt
->iterator_pre_condition
);
1337 expand_expression(stmt
->iterator_post_condition
);
1338 expand_statement(stmt
->iterator_pre_statement
);
1339 expand_statement(stmt
->iterator_statement
);
1340 expand_statement(stmt
->iterator_post_statement
);
1341 return SIDE_EFFECTS
;
1344 expand_expression(stmt
->switch_expression
);
1345 expand_statement(stmt
->switch_statement
);
1346 return SIDE_EFFECTS
;
1349 expand_const_expression(stmt
->case_expression
, "case statement");
1350 expand_const_expression(stmt
->case_to
, "case statement");
1351 expand_statement(stmt
->case_statement
);
1352 return SIDE_EFFECTS
;
1355 expand_statement(stmt
->label_statement
);
1356 return SIDE_EFFECTS
;
1359 expand_expression(stmt
->goto_expression
);
1360 return SIDE_EFFECTS
;
1365 expand_asm_statement(stmt
);
1368 expand_expression(stmt
->expression
);
1371 expand_expression(stmt
->range_expression
);
1372 expand_expression(stmt
->range_low
);
1373 expand_expression(stmt
->range_high
);
1376 return SIDE_EFFECTS
;
1379 static inline int bad_integer_constant_expression(struct expression
*expr
)
1381 if (!(expr
->flags
& CEF_ICE
))
1383 if (expr
->taint
& Taint_comma
)
1388 static long long __get_expression_value(struct expression
*expr
, int strict
)
1390 long long value
, mask
;
1391 struct symbol
*ctype
;
1395 ctype
= evaluate_expression(expr
);
1397 expression_error(expr
, "bad constant expression type");
1400 expand_expression(expr
);
1401 if (expr
->type
!= EXPR_VALUE
) {
1403 expression_error(expr
, "bad constant expression");
1406 #if 0 // This complains about "1 ? 1 :__bits_per()" which the kernel use
1407 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1408 expression_error(expr
, "bad integer constant expression");
1413 value
= expr
->value
;
1414 mask
= 1ULL << (ctype
->bit_size
-1);
1417 while (ctype
->type
!= SYM_BASETYPE
)
1418 ctype
= ctype
->ctype
.base_type
;
1419 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1420 value
= value
| mask
| ~(mask
-1);
1425 long long get_expression_value(struct expression
*expr
)
1427 return __get_expression_value(expr
, 0);
1430 long long const_expression_value(struct expression
*expr
)
1432 return __get_expression_value(expr
, 1);
1435 long long get_expression_value_silent(struct expression
*expr
)
1438 return __get_expression_value(expr
, 2);
1441 int expr_truth_value(struct expression
*expr
)
1443 const int saved
= conservative
;
1444 struct symbol
*ctype
;
1449 ctype
= evaluate_expression(expr
);
1454 expand_expression(expr
);
1455 conservative
= saved
;
1458 switch (expr
->type
) {
1463 return expr
->value
!= 0;
1465 return expr
->fvalue
!= 0;
1471 int is_zero_constant(struct expression
*expr
)
1473 const int saved
= conservative
;
1475 expand_expression(expr
);
1476 conservative
= saved
;
1477 return expr
->type
== EXPR_VALUE
&& !expr
->value
;