4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * expand constant expressions.
43 #include "expression.h"
47 static int expand_expression(struct expression
*);
48 static int expand_statement(struct statement
*);
49 static int conservative
;
51 static int expand_symbol_expression(struct expression
*expr
)
53 struct symbol
*sym
= expr
->symbol
;
55 if (sym
== &zero_int
) {
57 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
58 expr
->type
= EXPR_VALUE
;
63 /* The cost of a symbol expression is lower for on-stack symbols */
64 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
67 static long long get_longlong(struct expression
*expr
)
69 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
70 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
71 long long value
= expr
->value
;
72 long long ormask
, andmask
;
76 andmask
= mask
| (mask
-1);
80 return (value
& andmask
) | ormask
;
83 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
84 struct expression
*old
, struct symbol
*oldtype
)
86 int old_size
= oldtype
->bit_size
;
87 int new_size
= newtype
->bit_size
;
88 long long value
, mask
, signmask
;
89 long long oldmask
, oldsignmask
, dropped
;
91 if (newtype
->ctype
.base_type
== &fp_type
||
92 oldtype
->ctype
.base_type
== &fp_type
)
95 // For pointers and integers, we can just move the value around
96 expr
->type
= EXPR_VALUE
;
97 expr
->taint
= old
->taint
;
98 if (old_size
== new_size
) {
99 expr
->value
= old
->value
;
103 // expand it to the full "long long" value
104 value
= get_longlong(old
);
107 // _Bool requires a zero test rather than truncation.
108 if (is_bool_type(newtype
)) {
109 expr
->value
= !!value
;
110 if (!conservative
&& value
!= 0 && value
!= 1)
111 warning(old
->pos
, "odd constant _Bool cast (%llx becomes 1)", value
);
115 // Truncate it to the new size
116 signmask
= 1ULL << (new_size
-1);
117 mask
= signmask
| (signmask
-1);
118 expr
->value
= value
& mask
;
120 // Stop here unless checking for truncation
121 if (!Wcast_truncate
|| conservative
)
124 // Check if we dropped any bits..
125 oldsignmask
= 1ULL << (old_size
-1);
126 oldmask
= oldsignmask
| (oldsignmask
-1);
127 dropped
= oldmask
& ~mask
;
129 // OK if the bits were (and still are) purely sign bits
130 if (value
& dropped
) {
131 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
132 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
139 if (!is_float_type(newtype
)) {
140 value
= (long long)old
->fvalue
;
141 expr
->type
= EXPR_VALUE
;
146 if (!is_float_type(oldtype
))
147 expr
->fvalue
= (long double)get_longlong(old
);
149 expr
->fvalue
= old
->fvalue
;
151 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
) && \
152 !(newtype
->ctype
.modifiers
& MOD_LONGLONGLONG
)) {
153 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
154 expr
->fvalue
= (double)expr
->fvalue
;
156 expr
->fvalue
= (float)expr
->fvalue
;
158 expr
->type
= EXPR_FVALUE
;
161 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
163 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
164 count
&= ctype
->bit_size
-1;
169 * CAREFUL! We need to get the size and sign of the
172 #define CONVERT(op,s) (((op)<<1)+(s))
173 #define SIGNED(op) CONVERT(op, 1)
174 #define UNSIGNED(op) CONVERT(op, 0)
175 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
177 struct expression
*left
= expr
->left
, *right
= expr
->right
;
178 unsigned long long v
, l
, r
, mask
;
179 signed long long sl
, sr
;
182 if (right
->type
!= EXPR_VALUE
)
185 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
186 if (r
>= ctype
->bit_size
) {
189 r
= check_shift_count(expr
, ctype
, r
);
193 if (left
->type
!= EXPR_VALUE
)
195 l
= left
->value
; r
= right
->value
;
196 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
197 mask
= 1ULL << (ctype
->bit_size
-1);
199 if (is_signed
&& (sl
& mask
))
201 if (is_signed
&& (sr
& mask
))
204 switch (CONVERT(expr
->op
,is_signed
)) {
241 if (l
== mask
&& sr
== -1)
254 if (l
== mask
&& sr
== -1)
264 case SIGNED(SPECIAL_LEFTSHIFT
):
265 case UNSIGNED(SPECIAL_LEFTSHIFT
):
269 case SIGNED(SPECIAL_RIGHTSHIFT
):
273 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
280 mask
= mask
| (mask
-1);
281 expr
->value
= v
& mask
;
282 expr
->type
= EXPR_VALUE
;
283 expr
->taint
= left
->taint
| right
->taint
;
287 warning(expr
->pos
, "division by zero");
291 warning(expr
->pos
, "constant integer operation overflow");
295 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
297 struct expression
*left
= expr
->left
, *right
= expr
->right
;
298 unsigned long long l
, r
, mask
;
299 signed long long sl
, sr
;
301 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
303 l
= left
->value
; r
= right
->value
;
304 mask
= 1ULL << (ctype
->bit_size
-1);
311 case '<': expr
->value
= sl
< sr
; break;
312 case '>': expr
->value
= sl
> sr
; break;
313 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
314 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
315 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
316 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
317 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
318 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
319 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
320 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
322 expr
->type
= EXPR_VALUE
;
323 expr
->taint
= left
->taint
| right
->taint
;
327 static int simplify_float_binop(struct expression
*expr
)
329 struct expression
*left
= expr
->left
, *right
= expr
->right
;
330 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
331 long double l
, r
, res
;
333 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
339 if (mod
& MOD_LONGLONG
) {
341 case '+': res
= l
+ r
; break;
342 case '-': res
= l
- r
; break;
343 case '*': res
= l
* r
; break;
344 case '/': if (!r
) goto Div
;
348 } else if (mod
& MOD_LONG
) {
350 case '+': res
= (double) l
+ (double) r
; break;
351 case '-': res
= (double) l
- (double) r
; break;
352 case '*': res
= (double) l
* (double) r
; break;
353 case '/': if (!r
) goto Div
;
354 res
= (double) l
/ (double) r
; break;
359 case '+': res
= (float)l
+ (float)r
; break;
360 case '-': res
= (float)l
- (float)r
; break;
361 case '*': res
= (float)l
* (float)r
; break;
362 case '/': if (!r
) goto Div
;
363 res
= (float)l
/ (float)r
; break;
367 expr
->type
= EXPR_FVALUE
;
372 warning(expr
->pos
, "division by zero");
376 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
378 struct expression
*left
= expr
->left
, *right
= expr
->right
;
381 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
387 case '<': expr
->value
= l
< r
; break;
388 case '>': expr
->value
= l
> r
; break;
389 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
390 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
391 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
392 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
394 expr
->type
= EXPR_VALUE
;
399 static int expand_binop(struct expression
*expr
)
403 cost
= expand_expression(expr
->left
);
404 cost
+= expand_expression(expr
->right
);
405 if (simplify_int_binop(expr
, expr
->ctype
))
407 if (simplify_float_binop(expr
))
412 static int expand_logical(struct expression
*expr
)
414 struct expression
*left
= expr
->left
;
415 struct expression
*right
;
418 /* Do immediate short-circuiting ... */
419 cost
= expand_expression(left
);
420 if (left
->type
== EXPR_VALUE
) {
421 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
423 expr
->type
= EXPR_VALUE
;
425 expr
->taint
= left
->taint
;
430 expr
->type
= EXPR_VALUE
;
432 expr
->taint
= left
->taint
;
439 rcost
= expand_expression(right
);
440 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
442 * We know the left value doesn't matter, since
443 * otherwise we would have short-circuited it..
445 expr
->type
= EXPR_VALUE
;
446 expr
->value
= right
->value
!= 0;
447 expr
->taint
= left
->taint
| right
->taint
;
452 * If the right side is safe and cheaper than a branch,
453 * just avoid the branch and turn it into a regular binop
456 if (rcost
< BRANCH_COST
) {
457 expr
->type
= EXPR_BINOP
;
458 rcost
-= BRANCH_COST
- 1;
461 return cost
+ BRANCH_COST
+ rcost
;
464 static int expand_comma(struct expression
*expr
)
468 cost
= expand_expression(expr
->left
);
469 cost
+= expand_expression(expr
->right
);
470 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
) {
471 unsigned flags
= expr
->flags
;
473 taint
= expr
->left
->type
== EXPR_VALUE
? expr
->left
->taint
: 0;
474 *expr
= *expr
->right
;
476 if (expr
->type
== EXPR_VALUE
)
477 expr
->taint
|= Taint_comma
| taint
;
482 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
484 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
486 struct ctype c1
= {.base_type
= left
};
487 struct ctype c2
= {.base_type
= right
};
490 return !type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
);
491 case SPECIAL_NOTEQUAL
:
492 return type_difference(&c1
, &c2
, MOD_IGN
, MOD_IGN
) != NULL
;
494 return left
->bit_size
< right
->bit_size
;
496 return left
->bit_size
> right
->bit_size
;
498 return left
->bit_size
<= right
->bit_size
;
500 return left
->bit_size
>= right
->bit_size
;
505 static int expand_compare(struct expression
*expr
)
507 struct expression
*left
= expr
->left
, *right
= expr
->right
;
510 cost
= expand_expression(left
);
511 cost
+= expand_expression(right
);
514 /* Type comparison? */
515 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
517 expr
->type
= EXPR_VALUE
;
518 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
522 if (simplify_cmp_binop(expr
, left
->ctype
))
524 if (simplify_float_cmp(expr
, left
->ctype
))
530 static int expand_conditional(struct expression
*expr
)
532 struct expression
*cond
= expr
->conditional
;
533 struct expression
*true = expr
->cond_true
;
534 struct expression
*false = expr
->cond_false
;
537 cond_cost
= expand_expression(cond
);
538 if (cond
->type
== EXPR_VALUE
) {
539 unsigned flags
= expr
->flags
;
544 cost
= expand_expression(true);
547 if (expr
->type
== EXPR_VALUE
)
548 expr
->taint
|= cond
->taint
;
552 cost
= expand_expression(true);
553 cost
+= expand_expression(false);
555 if (cost
< SELECT_COST
) {
556 expr
->type
= EXPR_SELECT
;
557 cost
-= BRANCH_COST
- 1;
560 return cost
+ cond_cost
+ BRANCH_COST
;
563 static int expand_assignment(struct expression
*expr
)
565 expand_expression(expr
->left
);
566 expand_expression(expr
->right
);
570 static int expand_addressof(struct expression
*expr
)
572 return expand_expression(expr
->unop
);
576 * Look up a trustable initializer value at the requested offset.
578 * Return NULL if no such value can be found or statically trusted.
580 * FIXME!! We should check that the size is right!
582 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
584 struct expression
*value
;
586 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
588 value
= sym
->initializer
;
591 if (value
->type
== EXPR_INITIALIZER
) {
592 struct expression
*entry
;
593 FOR_EACH_PTR(value
->expr_list
, entry
) {
594 if (entry
->type
!= EXPR_POS
) {
599 if (entry
->init_offset
< offset
)
601 if (entry
->init_offset
> offset
)
603 return entry
->init_expr
;
604 } END_FOR_EACH_PTR(entry
);
610 static int expand_dereference(struct expression
*expr
)
612 struct expression
*unop
= expr
->unop
;
615 expand_expression(unop
);
618 * NOTE! We get a bogus warning right now for some special
619 * cases: apparently I've screwed up the optimization of
620 * a zero-offset dereference, and the ctype is wrong.
622 * Leave the warning in anyway, since this is also a good
623 * test for me to get the type evaluation right..
625 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
626 warning(unop
->pos
, "dereference of noderef expression");
629 * Is it "symbol" or "symbol + offset"?
632 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
633 struct expression
*right
= unop
->right
;
634 if (right
->type
== EXPR_VALUE
) {
635 offset
= right
->value
;
640 if (unop
->type
== EXPR_SYMBOL
) {
641 struct symbol
*sym
= unop
->symbol
;
642 struct expression
*value
= constant_symbol_value(sym
, offset
);
644 /* Const symbol with a constant initializer? */
646 /* FIXME! We should check that the size is right! */
647 if (value
->type
== EXPR_VALUE
) {
648 expr
->type
= EXPR_VALUE
;
649 expr
->value
= value
->value
;
652 } else if (value
->type
== EXPR_FVALUE
) {
653 expr
->type
= EXPR_FVALUE
;
654 expr
->fvalue
= value
->fvalue
;
659 /* Direct symbol dereference? Cheap and safe */
660 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
666 static int simplify_preop(struct expression
*expr
)
668 struct expression
*op
= expr
->unop
;
669 unsigned long long v
, mask
;
671 if (op
->type
!= EXPR_VALUE
)
674 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
679 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
683 case '!': v
= !v
; break;
684 case '~': v
= ~v
; break;
687 mask
= mask
| (mask
-1);
688 expr
->value
= v
& mask
;
689 expr
->type
= EXPR_VALUE
;
690 expr
->taint
= op
->taint
;
695 warning(expr
->pos
, "constant integer operation overflow");
699 static int simplify_float_preop(struct expression
*expr
)
701 struct expression
*op
= expr
->unop
;
704 if (op
->type
!= EXPR_FVALUE
)
709 case '-': v
= -v
; break;
713 expr
->type
= EXPR_FVALUE
;
718 * Unary post-ops: x++ and x--
720 static int expand_postop(struct expression
*expr
)
722 expand_expression(expr
->unop
);
726 static int expand_preop(struct expression
*expr
)
732 return expand_dereference(expr
);
735 return expand_addressof(expr
);
737 case SPECIAL_INCREMENT
:
738 case SPECIAL_DECREMENT
:
740 * From a type evaluation standpoint the preops are
741 * the same as the postops
743 return expand_postop(expr
);
748 cost
= expand_expression(expr
->unop
);
750 if (simplify_preop(expr
))
752 if (simplify_float_preop(expr
))
757 static int expand_arguments(struct expression_list
*head
)
760 struct expression
*expr
;
762 FOR_EACH_PTR (head
, expr
) {
763 cost
+= expand_expression(expr
);
764 } END_FOR_EACH_PTR(expr
);
768 static int expand_cast(struct expression
*expr
)
771 struct expression
*target
= expr
->cast_expression
;
773 cost
= expand_expression(target
);
775 /* Simplify normal integer casts.. */
776 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
777 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
784 * expand a call expression with a symbol. This
785 * should expand builtins.
787 static int expand_symbol_call(struct expression
*expr
, int cost
)
789 struct expression
*fn
= expr
->fn
;
790 struct symbol
*ctype
= fn
->ctype
;
792 if (fn
->type
!= EXPR_PREOP
)
795 if (ctype
->op
&& ctype
->op
->expand
)
796 return ctype
->op
->expand(expr
, cost
);
798 if (ctype
->ctype
.modifiers
& MOD_PURE
)
804 static int expand_call(struct expression
*expr
)
808 struct expression
*fn
= expr
->fn
;
810 cost
= expand_arguments(expr
->args
);
813 expression_error(expr
, "function has no type");
816 if (sym
->type
== SYM_NODE
)
817 return expand_symbol_call(expr
, cost
);
822 static int expand_expression_list(struct expression_list
*list
)
825 struct expression
*expr
;
827 FOR_EACH_PTR(list
, expr
) {
828 cost
+= expand_expression(expr
);
829 } END_FOR_EACH_PTR(expr
);
834 * We can simplify nested position expressions if
835 * this is a simple (single) positional expression.
837 static int expand_pos_expression(struct expression
*expr
)
839 struct expression
*nested
= expr
->init_expr
;
840 unsigned long offset
= expr
->init_offset
;
841 int nr
= expr
->init_nr
;
844 switch (nested
->type
) {
846 offset
+= nested
->init_offset
;
848 expr
->init_offset
= offset
;
852 case EXPR_INITIALIZER
: {
853 struct expression
*reuse
= nested
, *entry
;
855 FOR_EACH_PTR(expr
->expr_list
, entry
) {
856 if (entry
->type
== EXPR_POS
) {
857 entry
->init_offset
+= offset
;
861 * This happens rarely, but it can happen
862 * with bitfields that are all at offset
865 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
867 reuse
->type
= EXPR_POS
;
868 reuse
->ctype
= entry
->ctype
;
869 reuse
->init_offset
= offset
;
871 reuse
->init_expr
= entry
;
872 REPLACE_CURRENT_PTR(entry
, reuse
);
875 } END_FOR_EACH_PTR(entry
);
884 return expand_expression(nested
);
887 static unsigned long bit_offset(const struct expression
*expr
)
889 unsigned long offset
= 0;
890 while (expr
->type
== EXPR_POS
) {
891 offset
+= bytes_to_bits(expr
->init_offset
);
892 expr
= expr
->init_expr
;
894 if (expr
&& expr
->ctype
)
895 offset
+= expr
->ctype
->bit_offset
;
899 static int compare_expressions(const void *_a
, const void *_b
)
901 const struct expression
*a
= _a
;
902 const struct expression
*b
= _b
;
903 unsigned long a_pos
= bit_offset(a
);
904 unsigned long b_pos
= bit_offset(b
);
906 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
909 static void sort_expression_list(struct expression_list
**list
)
911 sort_list((struct ptr_list
**)list
, compare_expressions
);
914 static void verify_nonoverlapping(struct expression_list
**list
)
916 struct expression
*a
= NULL
;
917 struct expression
*b
;
919 FOR_EACH_PTR(*list
, b
) {
920 if (!b
->ctype
|| !b
->ctype
->bit_size
)
922 if (a
&& bit_offset(a
) == bit_offset(b
)) {
923 warning(a
->pos
, "Initializer entry defined twice");
924 info(b
->pos
, " also defined here");
928 } END_FOR_EACH_PTR(b
);
931 static int expand_expression(struct expression
*expr
)
935 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
938 switch (expr
->type
) {
945 return expand_symbol_expression(expr
);
947 return expand_binop(expr
);
950 return expand_logical(expr
);
953 return expand_comma(expr
);
956 return expand_compare(expr
);
958 case EXPR_ASSIGNMENT
:
959 return expand_assignment(expr
);
962 return expand_preop(expr
);
965 return expand_postop(expr
);
968 case EXPR_FORCE_CAST
:
969 case EXPR_IMPLIED_CAST
:
970 return expand_cast(expr
);
973 return expand_call(expr
);
976 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
980 case EXPR_CONDITIONAL
:
981 return expand_conditional(expr
);
983 case EXPR_STATEMENT
: {
984 struct statement
*stmt
= expr
->statement
;
985 int cost
= expand_statement(stmt
);
987 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
988 *expr
= *stmt
->expression
;
995 case EXPR_INITIALIZER
:
996 sort_expression_list(&expr
->expr_list
);
997 verify_nonoverlapping(&expr
->expr_list
);
998 return expand_expression_list(expr
->expr_list
);
1000 case EXPR_IDENTIFIER
:
1007 return expand_expression(expr
->base
) + 1;
1010 return expand_pos_expression(expr
);
1013 case EXPR_PTRSIZEOF
:
1016 expression_error(expr
, "internal front-end error: sizeof in expansion?");
1019 return SIDE_EFFECTS
;
1022 static void expand_const_expression(struct expression
*expr
, const char *where
)
1025 expand_expression(expr
);
1026 if (expr
->type
!= EXPR_VALUE
)
1027 expression_error(expr
, "Expected constant expression in %s", where
);
1031 int expand_symbol(struct symbol
*sym
)
1034 struct symbol
*base_type
;
1038 base_type
= sym
->ctype
.base_type
;
1042 retval
= expand_expression(sym
->initializer
);
1043 /* expand the body of the symbol */
1044 if (base_type
->type
== SYM_FN
) {
1045 if (base_type
->stmt
)
1046 expand_statement(base_type
->stmt
);
1051 static void expand_return_expression(struct statement
*stmt
)
1053 expand_expression(stmt
->expression
);
1056 static int expand_if_statement(struct statement
*stmt
)
1058 struct expression
*expr
= stmt
->if_conditional
;
1060 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1063 expand_expression(expr
);
1065 /* This is only valid if nobody jumps into the "dead" side */
1067 /* Simplify constant conditionals without even evaluating the false side */
1068 if (expr
->type
== EXPR_VALUE
) {
1069 struct statement
*simple
;
1070 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1074 stmt
->type
= STMT_NONE
;
1077 expand_statement(simple
);
1079 return SIDE_EFFECTS
;
1082 expand_statement(stmt
->if_true
);
1083 expand_statement(stmt
->if_false
);
1084 return SIDE_EFFECTS
;
1088 * Expanding a compound statement is really just
1089 * about adding up the costs of each individual
1092 * We also collapse a simple compound statement:
1093 * this would trigger for simple inline functions,
1094 * except we would have to check the "return"
1095 * symbol usage. Next time.
1097 static int expand_compound(struct statement
*stmt
)
1099 struct statement
*s
, *last
;
1100 int cost
, statements
;
1103 expand_symbol(stmt
->ret
);
1106 cost
= expand_statement(last
);
1107 statements
= last
!= NULL
;
1108 FOR_EACH_PTR(stmt
->stmts
, s
) {
1111 cost
+= expand_statement(s
);
1112 } END_FOR_EACH_PTR(s
);
1114 if (statements
== 1 && !stmt
->ret
)
1120 static int expand_statement(struct statement
*stmt
)
1125 switch (stmt
->type
) {
1126 case STMT_DECLARATION
: {
1128 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1130 } END_FOR_EACH_PTR(sym
);
1131 return SIDE_EFFECTS
;
1135 expand_return_expression(stmt
);
1136 return SIDE_EFFECTS
;
1138 case STMT_EXPRESSION
:
1139 return expand_expression(stmt
->expression
);
1142 return expand_compound(stmt
);
1145 return expand_if_statement(stmt
);
1148 expand_expression(stmt
->iterator_pre_condition
);
1149 expand_expression(stmt
->iterator_post_condition
);
1150 expand_statement(stmt
->iterator_pre_statement
);
1151 expand_statement(stmt
->iterator_statement
);
1152 expand_statement(stmt
->iterator_post_statement
);
1153 return SIDE_EFFECTS
;
1156 expand_expression(stmt
->switch_expression
);
1157 expand_statement(stmt
->switch_statement
);
1158 return SIDE_EFFECTS
;
1161 expand_const_expression(stmt
->case_expression
, "case statement");
1162 expand_const_expression(stmt
->case_to
, "case statement");
1163 expand_statement(stmt
->case_statement
);
1164 return SIDE_EFFECTS
;
1167 expand_statement(stmt
->label_statement
);
1168 return SIDE_EFFECTS
;
1171 expand_expression(stmt
->goto_expression
);
1172 return SIDE_EFFECTS
;
1177 /* FIXME! Do the asm parameter evaluation! */
1180 expand_expression(stmt
->expression
);
1183 expand_expression(stmt
->range_expression
);
1184 expand_expression(stmt
->range_low
);
1185 expand_expression(stmt
->range_high
);
1188 return SIDE_EFFECTS
;
1191 static inline int bad_integer_constant_expression(struct expression
*expr
)
1193 if (!(expr
->flags
& Int_const_expr
))
1195 if (expr
->taint
& Taint_comma
)
1200 static long long __get_expression_value(struct expression
*expr
, int strict
)
1202 long long value
, mask
;
1203 struct symbol
*ctype
;
1207 ctype
= evaluate_expression(expr
);
1209 expression_error(expr
, "bad constant expression type");
1212 expand_expression(expr
);
1213 if (expr
->type
!= EXPR_VALUE
) {
1215 expression_error(expr
, "bad constant expression");
1218 if ((strict
== 1) && bad_integer_constant_expression(expr
)) {
1219 expression_error(expr
, "bad integer constant expression");
1223 value
= expr
->value
;
1224 mask
= 1ULL << (ctype
->bit_size
-1);
1227 while (ctype
->type
!= SYM_BASETYPE
)
1228 ctype
= ctype
->ctype
.base_type
;
1229 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1230 value
= value
| mask
| ~(mask
-1);
1235 long long get_expression_value(struct expression
*expr
)
1237 return __get_expression_value(expr
, 0);
1240 long long const_expression_value(struct expression
*expr
)
1242 return __get_expression_value(expr
, 1);
1245 long long get_expression_value_silent(struct expression
*expr
)
1248 return __get_expression_value(expr
, 2);
1251 int is_zero_constant(struct expression
*expr
)
1253 const int saved
= conservative
;
1255 expand_expression(expr
);
1256 conservative
= saved
;
1257 return expr
->type
== EXPR_VALUE
&& !expr
->value
;