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"
45 /* Random cost numbers */
46 #define SIDE_EFFECTS 10000 /* The expression has side effects */
47 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
48 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
49 #define BRANCH_COST 10 /* Cost of a conditional branch */
51 static int expand_expression(struct expression
*);
52 static int expand_statement(struct statement
*);
53 static int conservative
;
55 static int expand_symbol_expression(struct expression
*expr
)
57 struct symbol
*sym
= expr
->symbol
;
59 if (sym
== &zero_int
) {
61 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
62 expr
->type
= EXPR_VALUE
;
67 /* The cost of a symbol expression is lower for on-stack symbols */
68 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
71 static long long get_longlong(struct expression
*expr
)
73 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
74 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
75 long long value
= expr
->value
;
76 long long ormask
, andmask
;
80 andmask
= mask
| (mask
-1);
84 return (value
& andmask
) | ormask
;
87 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
88 struct expression
*old
, struct symbol
*oldtype
)
90 int old_size
= oldtype
->bit_size
;
91 int new_size
= newtype
->bit_size
;
92 long long value
, mask
, signmask
;
93 long long oldmask
, oldsignmask
, dropped
;
95 if (newtype
->ctype
.base_type
== &fp_type
||
96 oldtype
->ctype
.base_type
== &fp_type
)
99 // For pointers and integers, we can just move the value around
100 expr
->type
= EXPR_VALUE
;
101 expr
->taint
= old
->taint
;
102 if (old_size
== new_size
) {
103 expr
->value
= old
->value
;
107 // expand it to the full "long long" value
108 value
= get_longlong(old
);
111 // _Bool requires a zero test rather than truncation.
112 if (is_bool_type(newtype
)) {
113 expr
->value
= !!value
;
114 if (!conservative
&& value
!= 0 && value
!= 1)
115 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
119 // Truncate it to the new size
120 signmask
= 1ULL << (new_size
-1);
121 mask
= signmask
| (signmask
-1);
122 expr
->value
= value
& mask
;
124 // Stop here unless checking for truncation
125 if (!Wcast_truncate
|| conservative
)
128 // Check if we dropped any bits..
129 oldsignmask
= 1ULL << (old_size
-1);
130 oldmask
= oldsignmask
| (oldsignmask
-1);
131 dropped
= oldmask
& ~mask
;
133 // OK if the bits were (and still are) purely sign bits
134 if (value
& dropped
) {
135 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
136 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
143 if (!is_float_type(newtype
)) {
144 value
= (long long)old
->fvalue
;
145 expr
->type
= EXPR_VALUE
;
150 if (!is_float_type(oldtype
))
151 expr
->fvalue
= (long double)get_longlong(old
);
153 expr
->fvalue
= old
->fvalue
;
155 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
156 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
157 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
158 expr
->fvalue
= (double)expr
->fvalue
;
160 expr
->fvalue
= (float)expr
->fvalue
;
162 expr
->type
= EXPR_FVALUE
;
165 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
167 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
168 count
&= ctype
->bit_size
-1;
173 * CAREFUL! We need to get the size and sign of the
176 #define CONVERT(op,s) (((op)<<1)+(s))
177 #define SIGNED(op) CONVERT(op, 1)
178 #define UNSIGNED(op) CONVERT(op, 0)
179 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
181 struct expression
*left
= expr
->left
, *right
= expr
->right
;
182 unsigned long long v
, l
, r
, mask
;
183 signed long long sl
, sr
;
186 if (right
->type
!= EXPR_VALUE
)
189 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
190 if (r
>= ctype
->bit_size
) {
193 r
= check_shift_count(expr
, ctype
, r
);
197 if (left
->type
!= EXPR_VALUE
)
199 l
= left
->value
; r
= right
->value
;
200 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
201 mask
= 1ULL << (ctype
->bit_size
-1);
203 if (is_signed
&& (sl
& mask
))
205 if (is_signed
&& (sr
& mask
))
208 switch (CONVERT(expr
->op
,is_signed
)) {
245 if (l
== mask
&& sr
== -1)
258 if (l
== mask
&& sr
== -1)
268 case SIGNED(SPECIAL_LEFTSHIFT
):
269 case UNSIGNED(SPECIAL_LEFTSHIFT
):
273 case SIGNED(SPECIAL_RIGHTSHIFT
):
277 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
284 mask
= mask
| (mask
-1);
285 expr
->value
= v
& mask
;
286 expr
->type
= EXPR_VALUE
;
287 expr
->taint
= left
->taint
| right
->taint
;
291 warning(expr
->pos
, "division by zero");
295 warning(expr
->pos
, "constant integer operation overflow");
299 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
301 struct expression
*left
= expr
->left
, *right
= expr
->right
;
302 unsigned long long l
, r
, mask
;
303 signed long long sl
, sr
;
305 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
307 l
= left
->value
; r
= right
->value
;
308 mask
= 1ULL << (ctype
->bit_size
-1);
315 case '<': expr
->value
= sl
< sr
; break;
316 case '>': expr
->value
= sl
> sr
; break;
317 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
318 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
319 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
320 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
321 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
322 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
323 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
324 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
326 expr
->type
= EXPR_VALUE
;
327 expr
->taint
= left
->taint
| right
->taint
;
331 static int simplify_float_binop(struct expression
*expr
)
333 struct expression
*left
= expr
->left
, *right
= expr
->right
;
334 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
335 long double l
, r
, res
;
337 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
343 if (mod
& MOD_LONGLONG
) {
345 case '+': res
= l
+ r
; break;
346 case '-': res
= l
- r
; break;
347 case '*': res
= l
* r
; break;
348 case '/': if (!r
) goto Div
;
352 } else if (mod
& MOD_LONG
) {
354 case '+': res
= (double) l
+ (double) r
; break;
355 case '-': res
= (double) l
- (double) r
; break;
356 case '*': res
= (double) l
* (double) r
; break;
357 case '/': if (!r
) goto Div
;
358 res
= (double) l
/ (double) r
; break;
363 case '+': res
= (float)l
+ (float)r
; break;
364 case '-': res
= (float)l
- (float)r
; break;
365 case '*': res
= (float)l
* (float)r
; break;
366 case '/': if (!r
) goto Div
;
367 res
= (float)l
/ (float)r
; break;
371 expr
->type
= EXPR_FVALUE
;
376 warning(expr
->pos
, "division by zero");
380 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
382 struct expression
*left
= expr
->left
, *right
= expr
->right
;
385 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
391 case '<': expr
->value
= l
< r
; break;
392 case '>': expr
->value
= l
> r
; break;
393 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
394 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
395 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
396 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
398 expr
->type
= EXPR_VALUE
;
403 static int expand_binop(struct expression
*expr
)
407 cost
= expand_expression(expr
->left
);
408 cost
+= expand_expression(expr
->right
);
409 if (simplify_int_binop(expr
, expr
->ctype
))
411 if (simplify_float_binop(expr
))
416 static int expand_logical(struct expression
*expr
)
418 struct expression
*left
= expr
->left
;
419 struct expression
*right
;
422 /* Do immediate short-circuiting ... */
423 cost
= expand_expression(left
);
424 if (left
->type
== EXPR_VALUE
) {
425 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
427 expr
->type
= EXPR_VALUE
;
429 expr
->taint
= left
->taint
;
434 expr
->type
= EXPR_VALUE
;
436 expr
->taint
= left
->taint
;
443 rcost
= expand_expression(right
);
444 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
446 * We know the left value doesn't matter, since
447 * otherwise we would have short-circuited it..
449 expr
->type
= EXPR_VALUE
;
450 expr
->value
= right
->value
!= 0;
451 expr
->taint
= left
->taint
| right
->taint
;
456 * If the right side is safe and cheaper than a branch,
457 * just avoid the branch and turn it into a regular binop
460 if (rcost
< BRANCH_COST
) {
461 expr
->type
= EXPR_BINOP
;
462 rcost
-= BRANCH_COST
- 1;
465 return cost
+ BRANCH_COST
+ rcost
;
468 static int expand_comma(struct expression
*expr
)
472 cost
= expand_expression(expr
->left
);
473 cost
+= expand_expression(expr
->right
);
474 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
475 unsigned flags
= expr
->flags
;
477 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
478 *expr
= *expr
->right
;
480 if (expr
->type
== EXPR_VALUE
)
481 expr
->taint
|= Taint_comma
| taint
;
486 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
488 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
490 struct ctype c1
= {.base_type
= left
, .attribute
= &null_attr
};
491 struct ctype c2
= {.base_type
= right
, .attribute
= &null_attr
};
494 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
495 case SPECIAL_NOTEQUAL
:
496 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
498 return left
->bit_size
< right
->bit_size
;
500 return left
->bit_size
> right
->bit_size
;
502 return left
->bit_size
<= right
->bit_size
;
504 return left
->bit_size
>= right
->bit_size
;
509 static int expand_compare(struct expression
*expr
)
511 struct expression
*left
= expr
->left
, *right
= expr
->right
;
514 cost
= expand_expression(left
);
515 cost
+= expand_expression(right
);
518 /* Type comparison? */
519 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
521 expr
->type
= EXPR_VALUE
;
522 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
526 if (simplify_cmp_binop(expr
, left
->ctype
))
528 if (simplify_float_cmp(expr
, left
->ctype
))
534 static int expand_conditional(struct expression
*expr
)
536 struct expression
*cond
= expr
->conditional
;
537 struct expression
*true = expr
->cond_true
;
538 struct expression
*false = expr
->cond_false
;
541 cond_cost
= expand_expression(cond
);
542 if (cond
->type
== EXPR_VALUE
) {
543 unsigned flags
= expr
->flags
;
548 cost
= expand_expression(true);
551 if (expr
->type
== EXPR_VALUE
)
552 expr
->taint
|= cond
->taint
;
556 cost
= expand_expression(true);
557 cost
+= expand_expression(false);
559 if (cost
< SELECT_COST
) {
560 expr
->type
= EXPR_SELECT
;
561 cost
-= BRANCH_COST
- 1;
564 return cost
+ cond_cost
+ BRANCH_COST
;
567 static int expand_assignment(struct expression
*expr
)
569 expand_expression(expr
->left
);
570 expand_expression(expr
->right
);
574 static int expand_addressof(struct expression
*expr
)
576 return expand_expression(expr
->unop
);
580 * Look up a trustable initializer value at the requested offset.
582 * Return NULL if no such value can be found or statically trusted.
584 * FIXME!! We should check that the size is right!
586 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
588 struct expression
*value
;
590 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
592 value
= sym
->initializer
;
595 if (value
->type
== EXPR_INITIALIZER
) {
596 struct expression
*entry
;
597 FOR_EACH_PTR(value
->expr_list
, entry
) {
598 if (entry
->type
!= EXPR_POS
) {
603 if (entry
->init_offset
< offset
)
605 if (entry
->init_offset
> offset
)
607 return entry
->init_expr
;
608 } END_FOR_EACH_PTR(entry
);
614 static int expand_dereference(struct expression
*expr
)
616 struct expression
*unop
= expr
->unop
;
619 expand_expression(unop
);
622 * NOTE! We get a bogus warning right now for some special
623 * cases: apparently I've screwed up the optimization of
624 * a zero-offset dereference, and the ctype is wrong.
626 * Leave the warning in anyway, since this is also a good
627 * test for me to get the type evaluation right..
629 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
630 warning(unop
->pos
, "dereference of noderef expression");
633 * Is it "symbol" or "symbol + offset"?
636 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
637 struct expression
*right
= unop
->right
;
638 if (right
->type
== EXPR_VALUE
) {
639 offset
= right
->value
;
644 if (unop
->type
== EXPR_SYMBOL
) {
645 struct symbol
*sym
= unop
->symbol
;
646 struct expression
*value
= constant_symbol_value(sym
, offset
);
648 /* Const symbol with a constant initializer? */
650 /* FIXME! We should check that the size is right! */
651 if (value
->type
== EXPR_VALUE
) {
652 expr
->type
= EXPR_VALUE
;
653 expr
->value
= value
->value
;
656 } else if (value
->type
== EXPR_FVALUE
) {
657 expr
->type
= EXPR_FVALUE
;
658 expr
->fvalue
= value
->fvalue
;
663 /* Direct symbol dereference? Cheap and safe */
664 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
670 static int simplify_preop(struct expression
*expr
)
672 struct expression
*op
= expr
->unop
;
673 unsigned long long v
, mask
;
675 if (op
->type
!= EXPR_VALUE
)
678 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
683 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
687 case '!': v
= !v
; break;
688 case '~': v
= ~v
; break;
691 mask
= mask
| (mask
-1);
692 expr
->value
= v
& mask
;
693 expr
->type
= EXPR_VALUE
;
694 expr
->taint
= op
->taint
;
699 warning(expr
->pos
, "constant integer operation overflow");
703 static int simplify_float_preop(struct expression
*expr
)
705 struct expression
*op
= expr
->unop
;
708 if (op
->type
!= EXPR_FVALUE
)
713 case '-': v
= -v
; break;
717 expr
->type
= EXPR_FVALUE
;
722 * Unary post-ops: x++ and x--
724 static int expand_postop(struct expression
*expr
)
726 expand_expression(expr
->unop
);
730 static int expand_preop(struct expression
*expr
)
736 return expand_dereference(expr
);
739 return expand_addressof(expr
);
741 case SPECIAL_INCREMENT
:
742 case SPECIAL_DECREMENT
:
744 * From a type evaluation standpoint the preops are
745 * the same as the postops
747 return expand_postop(expr
);
752 cost
= expand_expression(expr
->unop
);
754 if (simplify_preop(expr
))
756 if (simplify_float_preop(expr
))
761 static int expand_arguments(struct expression_list
*head
)
764 struct expression
*expr
;
766 FOR_EACH_PTR (head
, expr
) {
767 cost
+= expand_expression(expr
);
768 } END_FOR_EACH_PTR(expr
);
772 static int expand_cast(struct expression
*expr
)
775 struct expression
*target
= expr
->cast_expression
;
777 cost
= expand_expression(target
);
779 /* Simplify normal integer casts.. */
780 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
781 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
787 /* The arguments are constant if the cost of all of them is zero */
788 int expand_constant_p(struct expression
*expr
, int cost
)
790 expr
->type
= EXPR_VALUE
;
796 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
797 int expand_safe_p(struct expression
*expr
, int cost
)
799 expr
->type
= EXPR_VALUE
;
800 expr
->value
= (cost
< SIDE_EFFECTS
);
806 * expand a call expression with a symbol. This
807 * should expand builtins.
809 static int expand_symbol_call(struct expression
*expr
, int cost
)
811 struct expression
*fn
= expr
->fn
;
812 struct symbol
*ctype
= fn
->ctype
;
814 if (fn
->type
!= EXPR_PREOP
)
817 if (ctype
->op
&& ctype
->op
->expand
)
818 return ctype
->op
->expand(expr
, cost
);
820 if (ctype
->ctype
.modifiers
& MOD_PURE
)
826 static int expand_call(struct expression
*expr
)
830 struct expression
*fn
= expr
->fn
;
832 cost
= expand_arguments(expr
->args
);
835 expression_error(expr
, "function has no type");
838 if (sym
->type
== SYM_NODE
)
839 return expand_symbol_call(expr
, cost
);
844 static int expand_expression_list(struct expression_list
*list
)
847 struct expression
*expr
;
849 FOR_EACH_PTR(list
, expr
) {
850 cost
+= expand_expression(expr
);
851 } END_FOR_EACH_PTR(expr
);
856 * We can simplify nested position expressions if
857 * this is a simple (single) positional expression.
859 static int expand_pos_expression(struct expression
*expr
)
861 struct expression
*nested
= expr
->init_expr
;
862 unsigned long offset
= expr
->init_offset
;
863 int nr
= expr
->init_nr
;
866 switch (nested
->type
) {
868 offset
+= nested
->init_offset
;
870 expr
->init_offset
= offset
;
874 case EXPR_INITIALIZER
: {
875 struct expression
*reuse
= nested
, *entry
;
877 FOR_EACH_PTR(expr
->expr_list
, entry
) {
878 if (entry
->type
== EXPR_POS
) {
879 entry
->init_offset
+= offset
;
883 * This happens rarely, but it can happen
884 * with bitfields that are all at offset
887 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
889 reuse
->type
= EXPR_POS
;
890 reuse
->ctype
= entry
->ctype
;
891 reuse
->init_offset
= offset
;
893 reuse
->init_expr
= entry
;
894 REPLACE_CURRENT_PTR(entry
, reuse
);
897 } END_FOR_EACH_PTR(entry
);
906 return expand_expression(nested
);
909 static unsigned long bit_offset(const struct expression
*expr
)
911 unsigned long offset
= 0;
912 while (expr
->type
== EXPR_POS
) {
913 offset
+= bytes_to_bits(expr
->init_offset
);
914 expr
= expr
->init_expr
;
916 if (expr
&& expr
->ctype
)
917 offset
+= expr
->ctype
->bit_offset
;
921 static int compare_expressions(const void *_a
, const void *_b
)
923 const struct expression
*a
= _a
;
924 const struct expression
*b
= _b
;
925 unsigned long a_pos
= bit_offset(a
);
926 unsigned long b_pos
= bit_offset(b
);
928 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
931 static void sort_expression_list(struct expression_list
**list
)
933 sort_list((struct ptr_list
**)list
, compare_expressions
);
936 static void verify_nonoverlapping(struct expression_list
**list
)
938 struct expression
*a
= NULL
;
939 struct expression
*b
;
941 FOR_EACH_PTR(*list
, b
) {
942 if (!b
->ctype
|| !b
->ctype
->bit_size
)
944 if (a
&& bit_offset(a
) == bit_offset(b
)) {
945 warning(a
->pos
, "Initializer entry defined twice");
946 info(b
->pos
, " also defined here");
950 } END_FOR_EACH_PTR(b
);
953 static int expand_expression(struct expression
*expr
)
957 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
960 switch (expr
->type
) {
967 return expand_symbol_expression(expr
);
969 return expand_binop(expr
);
972 return expand_logical(expr
);
975 return expand_comma(expr
);
978 return expand_compare(expr
);
980 case EXPR_ASSIGNMENT
:
981 return expand_assignment(expr
);
984 return expand_preop(expr
);
987 return expand_postop(expr
);
990 case EXPR_FORCE_CAST
:
991 case EXPR_IMPLIED_CAST
:
992 return expand_cast(expr
);
995 return expand_call(expr
);
998 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
1002 case EXPR_CONDITIONAL
:
1003 return expand_conditional(expr
);
1005 case EXPR_STATEMENT
: {
1006 struct statement
*stmt
= expr
->statement
;
1007 int cost
= expand_statement(stmt
);
1009 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
1010 *expr
= *stmt
->expression
;
1017 case EXPR_INITIALIZER
:
1018 sort_expression_list(&expr
->expr_list
);
1019 verify_nonoverlapping(&expr
->expr_list
);
1020 return expand_expression_list(expr
->expr_list
);
1022 case EXPR_IDENTIFIER
:
1029 return expand_expression(expr
->base
) + 1;
1032 return expand_pos_expression(expr
);
1035 case EXPR_PTRSIZEOF
:
1038 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1041 return SIDE_EFFECTS
;
1044 static void expand_const_expression(struct expression
*expr
, const char *where
)
1047 expand_expression(expr
);
1048 if (expr
->type
!= EXPR_VALUE
)
1049 expression_error(expr
, "Expected constant expression in %s", where
);
1053 int expand_symbol(struct symbol
*sym
)
1056 struct symbol
*base_type
;
1060 base_type
= sym
->ctype
.base_type
;
1064 retval
= expand_expression(sym
->initializer
);
1065 /* expand the body of the symbol */
1066 if (base_type
->type
== SYM_FN
) {
1067 if (base_type
->stmt
)
1068 expand_statement(base_type
->stmt
);
1073 static void expand_return_expression(struct statement
*stmt
)
1075 expand_expression(stmt
->expression
);
1078 static int expand_if_statement(struct statement
*stmt
)
1080 struct expression
*expr
= stmt
->if_conditional
;
1082 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1085 expand_expression(expr
);
1087 /* This is only valid if nobody jumps into the "dead" side */
1089 /* Simplify constant conditionals without even evaluating the false side */
1090 if (expr
->type
== EXPR_VALUE
) {
1091 struct statement
*simple
;
1092 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1096 stmt
->type
= STMT_NONE
;
1099 expand_statement(simple
);
1101 return SIDE_EFFECTS
;
1104 expand_statement(stmt
->if_true
);
1105 expand_statement(stmt
->if_false
);
1106 return SIDE_EFFECTS
;
1110 * Expanding a compound statement is really just
1111 * about adding up the costs of each individual
1114 * We also collapse a simple compound statement:
1115 * this would trigger for simple inline functions,
1116 * except we would have to check the "return"
1117 * symbol usage. Next time.
1119 static int expand_compound(struct statement
*stmt
)
1121 struct statement
*s
, *last
;
1122 int cost
, statements
;
1125 expand_symbol(stmt
->ret
);
1128 cost
= expand_statement(last
);
1129 statements
= last
!= NULL
;
1130 FOR_EACH_PTR(stmt
->stmts
, s
) {
1133 cost
+= expand_statement(s
);
1134 } END_FOR_EACH_PTR(s
);
1136 if (statements
== 1 && !stmt
->ret
)
1142 static int expand_statement(struct statement
*stmt
)
1147 switch (stmt
->type
) {
1148 case STMT_DECLARATION
: {
1150 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1152 } END_FOR_EACH_PTR(sym
);
1153 return SIDE_EFFECTS
;
1157 expand_return_expression(stmt
);
1158 return SIDE_EFFECTS
;
1160 case STMT_EXPRESSION
:
1161 return expand_expression(stmt
->expression
);
1164 return expand_compound(stmt
);
1167 return expand_if_statement(stmt
);
1170 expand_expression(stmt
->iterator_pre_condition
);
1171 expand_expression(stmt
->iterator_post_condition
);
1172 expand_statement(stmt
->iterator_pre_statement
);
1173 expand_statement(stmt
->iterator_statement
);
1174 expand_statement(stmt
->iterator_post_statement
);
1175 return SIDE_EFFECTS
;
1178 expand_expression(stmt
->switch_expression
);
1179 expand_statement(stmt
->switch_statement
);
1180 return SIDE_EFFECTS
;
1183 expand_const_expression(stmt
->case_expression
, "case statement");
1184 expand_const_expression(stmt
->case_to
, "case statement");
1185 expand_statement(stmt
->case_statement
);
1186 return SIDE_EFFECTS
;
1189 expand_statement(stmt
->label_statement
);
1190 return SIDE_EFFECTS
;
1193 expand_expression(stmt
->goto_expression
);
1194 return SIDE_EFFECTS
;
1199 /* FIXME! Do the asm parameter evaluation! */
1202 expand_expression(stmt
->expression
);
1205 expand_expression(stmt
->range_expression
);
1206 expand_expression(stmt
->range_low
);
1207 expand_expression(stmt
->range_high
);
1210 return SIDE_EFFECTS
;
1213 static inline int bad_integer_constant_expression(struct expression
*expr
)
1215 if (!(expr
->flags
& Int_const_expr
))
1217 if (expr
->taint
& Taint_comma
)
1222 static long long __get_expression_value(struct expression
*expr
, int strict
)
1224 long long value
, mask
;
1225 struct symbol
*ctype
;
1229 ctype
= evaluate_expression(expr
);
1231 expression_error(expr
, "bad constant expression type");
1234 expand_expression(expr
);
1235 if (expr
->type
!= EXPR_VALUE
) {
1237 expression_error(expr
, "bad constant expression");
1240 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1241 expression_error(expr
, "bad integer constant expression");
1245 value
= expr
->value
;
1246 mask
= 1ULL << (ctype
->bit_size
-1);
1249 while (ctype
->type
!= SYM_BASETYPE
)
1250 ctype
= ctype
->ctype
.base_type
;
1251 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1252 value
= value
| mask
| ~(mask
-1);
1257 long long get_expression_value(struct expression
*expr
)
1259 return __get_expression_value(expr
, 0);
1262 long long const_expression_value(struct expression
*expr
)
1264 return __get_expression_value(expr
, 1);
1267 long long get_expression_value_silent(struct expression
*expr
)
1270 return __get_expression_value(expr
, 2);
1273 int is_zero_constant(struct expression
*expr
)
1275 const int saved
= conservative
;
1277 expand_expression(expr
);
1278 conservative
= saved
;
1279 return expr
->type
== EXPR_VALUE
&& !expr
->value
;