4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * expand constant expressions.
27 #include "expression.h"
29 /* Random cost numbers */
30 #define SIDE_EFFECTS 10000 /* The expression has side effects */
31 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
32 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
33 #define BRANCH_COST 10 /* Cost of a conditional branch */
35 static int expand_expression(struct expression
*);
36 static int expand_statement(struct statement
*);
38 static int expand_symbol_expression(struct expression
*expr
)
40 struct symbol
*sym
= expr
->symbol
;
42 if (sym
== &zero_int
) {
43 if (Wundefined_preprocessor
)
44 warning(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
45 expr
->type
= EXPR_VALUE
;
49 /* The cost of a symbol expression is lower for on-stack symbols */
50 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
53 static long long get_longlong(struct expression
*expr
)
55 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
56 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
57 long long value
= expr
->value
;
58 long long ormask
, andmask
;
62 andmask
= mask
| (mask
-1);
66 return (value
& andmask
) | ormask
;
69 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
70 struct expression
*old
, struct symbol
*oldtype
)
72 int old_size
= oldtype
->bit_size
;
73 int new_size
= newtype
->bit_size
;
74 long long value
, mask
, signmask
;
75 long long oldmask
, oldsignmask
, dropped
;
77 if (newtype
->ctype
.base_type
== &fp_type
||
78 oldtype
->ctype
.base_type
== &fp_type
)
81 // For pointers and integers, we can just move the value around
82 expr
->type
= EXPR_VALUE
;
83 if (old_size
== new_size
) {
84 expr
->value
= old
->value
;
88 // expand it to the full "long long" value
89 value
= get_longlong(old
);
92 // Truncate it to the new size
93 signmask
= 1ULL << (new_size
-1);
94 mask
= signmask
| (signmask
-1);
95 expr
->value
= value
& mask
;
97 // Stop here unless checking for truncation
101 // Check if we dropped any bits..
102 oldsignmask
= 1ULL << (old_size
-1);
103 oldmask
= oldsignmask
| (oldsignmask
-1);
104 dropped
= oldmask
& ~mask
;
106 // OK if the bits were (and still are) purely sign bits
107 if (value
& dropped
) {
108 if (!(value
& oldsignmask
) || !(value
& signmask
) || (value
& dropped
) != dropped
)
109 warning(old
->pos
, "cast truncates bits from constant value (%llx becomes %llx)",
116 if (newtype
->ctype
.base_type
!= &fp_type
) {
117 value
= (long long)old
->fvalue
;
118 expr
->type
= EXPR_VALUE
;
122 if (oldtype
->ctype
.base_type
!= &fp_type
)
123 expr
->fvalue
= (long double)get_longlong(old
);
125 expr
->fvalue
= old
->value
;
127 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
)) {
128 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
129 expr
->fvalue
= (double)expr
->fvalue
;
131 expr
->fvalue
= (float)expr
->fvalue
;
133 expr
->type
= EXPR_FVALUE
;
136 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
138 if (count
>= ctype
->bit_size
) {
139 warning(expr
->pos
, "shift too big (%u) for type %s", count
, show_typename(ctype
));
140 count
&= ctype
->bit_size
-1;
146 * CAREFUL! We need to get the size and sign of the
149 #define CONVERT(op,s) (((op)<<1)+(s))
150 #define SIGNED(op) CONVERT(op, 1)
151 #define UNSIGNED(op) CONVERT(op, 0)
152 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
154 struct expression
*left
= expr
->left
, *right
= expr
->right
;
155 unsigned long long v
, l
, r
, mask
;
156 signed long long sl
, sr
;
159 if (right
->type
!= EXPR_VALUE
)
162 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
163 r
= check_shift_count(expr
, ctype
, r
);
166 if (left
->type
!= EXPR_VALUE
)
168 l
= left
->value
; r
= right
->value
;
169 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
170 mask
= 1ULL << (ctype
->bit_size
-1);
172 if (is_signed
&& (sl
& mask
))
174 if (is_signed
&& (sr
& mask
))
177 switch (CONVERT(expr
->op
,is_signed
)) {
214 if (l
== mask
&& sr
== -1)
235 case SIGNED(SPECIAL_LEFTSHIFT
):
236 case UNSIGNED(SPECIAL_LEFTSHIFT
):
240 case SIGNED(SPECIAL_RIGHTSHIFT
):
244 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
251 mask
= mask
| (mask
-1);
252 expr
->value
= v
& mask
;
253 expr
->type
= EXPR_VALUE
;
256 warning(expr
->pos
, "division by zero");
259 warning(expr
->pos
, "constant integer operation overflow");
263 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
265 struct expression
*left
= expr
->left
, *right
= expr
->right
;
266 unsigned long long l
, r
, mask
;
267 signed long long sl
, sr
;
269 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
271 l
= left
->value
; r
= right
->value
;
272 mask
= 1ULL << (ctype
->bit_size
-1);
279 case '<': expr
->value
= sl
< sr
; break;
280 case '>': expr
->value
= sl
> sr
; break;
281 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
282 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
283 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
284 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
285 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
286 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
287 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
288 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
290 expr
->type
= EXPR_VALUE
;
294 static int simplify_float_binop(struct expression
*expr
)
296 struct expression
*left
= expr
->left
, *right
= expr
->right
;
297 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
298 long double l
, r
, res
;
300 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
306 if (mod
& MOD_LONGLONG
) {
308 case '+': res
= l
+ r
; break;
309 case '-': res
= l
- r
; break;
310 case '*': res
= l
* r
; break;
311 case '/': if (!r
) goto Div
;
315 } else if (mod
& MOD_LONG
) {
317 case '+': res
= (double) l
+ (double) r
; break;
318 case '-': res
= (double) l
- (double) r
; break;
319 case '*': res
= (double) l
* (double) r
; break;
320 case '/': if (!r
) goto Div
;
321 res
= (double) l
/ (double) r
; break;
326 case '+': res
= (float)l
+ (float)r
; break;
327 case '-': res
= (float)l
- (float)r
; break;
328 case '*': res
= (float)l
* (float)r
; break;
329 case '/': if (!r
) goto Div
;
330 res
= (float)l
/ (float)r
; break;
334 expr
->type
= EXPR_FVALUE
;
338 warning(expr
->pos
, "division by zero");
342 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
344 struct expression
*left
= expr
->left
, *right
= expr
->right
;
347 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
353 case '<': expr
->value
= l
< r
; break;
354 case '>': expr
->value
= l
> r
; break;
355 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
356 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
357 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
358 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
360 expr
->type
= EXPR_VALUE
;
364 static int expand_binop(struct expression
*expr
)
368 cost
= expand_expression(expr
->left
);
369 cost
+= expand_expression(expr
->right
);
370 if (simplify_int_binop(expr
, expr
->ctype
))
372 if (simplify_float_binop(expr
))
377 static int expand_logical(struct expression
*expr
)
379 struct expression
*left
= expr
->left
;
380 struct expression
*right
;
383 /* Do immediate short-circuiting ... */
384 cost
= expand_expression(left
);
385 if (left
->type
== EXPR_VALUE
) {
386 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
388 expr
->type
= EXPR_VALUE
;
394 expr
->type
= EXPR_VALUE
;
402 rcost
= expand_expression(right
);
403 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
405 * We know the left value doesn't matter, since
406 * otherwise we would have short-circuited it..
408 expr
->type
= EXPR_VALUE
;
409 expr
->value
= right
->value
!= 0;
414 * If the right side is safe and cheaper than a branch,
415 * just avoid the branch and turn it into a regular binop
418 if (rcost
< BRANCH_COST
) {
419 expr
->type
= EXPR_BINOP
;
420 rcost
-= BRANCH_COST
- 1;
423 return cost
+ BRANCH_COST
+ rcost
;
426 static int expand_comma(struct expression
*expr
)
430 cost
= expand_expression(expr
->left
);
431 cost
+= expand_expression(expr
->right
);
432 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
)
433 *expr
= *expr
->right
;
437 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
439 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
443 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
444 case SPECIAL_NOTEQUAL
:
445 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
447 return left
->bit_size
< right
->bit_size
;
449 return left
->bit_size
> right
->bit_size
;
451 return left
->bit_size
<= right
->bit_size
;
453 return left
->bit_size
>= right
->bit_size
;
458 static int expand_compare(struct expression
*expr
)
460 struct expression
*left
= expr
->left
, *right
= expr
->right
;
463 cost
= expand_expression(left
);
464 cost
+= expand_expression(right
);
467 /* Type comparison? */
468 if (left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
470 expr
->type
= EXPR_VALUE
;
471 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
474 if (simplify_cmp_binop(expr
, left
->ctype
))
476 if (simplify_float_cmp(expr
, left
->ctype
))
482 static int expand_conditional(struct expression
*expr
)
484 struct expression
*cond
= expr
->conditional
;
485 struct expression
*true = expr
->cond_true
;
486 struct expression
*false = expr
->cond_false
;
489 cond_cost
= expand_expression(cond
);
490 if (cond
->type
== EXPR_VALUE
) {
496 return expand_expression(expr
);
499 cost
= expand_expression(true);
500 cost
+= expand_expression(false);
502 if (cost
< SELECT_COST
) {
503 expr
->type
= EXPR_SELECT
;
504 cost
-= BRANCH_COST
- 1;
507 return cost
+ cond_cost
+ BRANCH_COST
;
510 static int expand_assignment(struct expression
*expr
)
512 expand_expression(expr
->left
);
513 expand_expression(expr
->right
);
517 static int expand_addressof(struct expression
*expr
)
519 return expand_expression(expr
->unop
);
523 * Look up a trustable initializer value at the requested offset.
525 * Return NULL if no such value can be found or statically trusted.
527 * FIXME!! We should check that the size is right!
529 static struct expression
*constant_symbol_value(struct symbol
*sym
, int offset
)
531 struct expression
*value
;
533 if (sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))
535 value
= sym
->initializer
;
538 if (value
->type
== EXPR_INITIALIZER
) {
539 struct expression
*entry
;
540 FOR_EACH_PTR(value
->expr_list
, entry
) {
541 if (entry
->type
!= EXPR_POS
) {
546 if (entry
->init_offset
< offset
)
548 if (entry
->init_offset
> offset
)
550 return entry
->init_expr
;
551 } END_FOR_EACH_PTR(entry
);
557 static int expand_dereference(struct expression
*expr
)
559 struct expression
*unop
= expr
->unop
;
562 expand_expression(unop
);
565 * NOTE! We get a bogus warning right now for some special
566 * cases: apparently I've screwed up the optimization of
567 * a zero-offset dereference, and the ctype is wrong.
569 * Leave the warning in anyway, since this is also a good
570 * test for me to get the type evaluation right..
572 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
573 warning(unop
->pos
, "dereference of noderef expression");
576 * Is it "symbol" or "symbol + offset"?
579 if (unop
->type
== EXPR_BINOP
&& unop
->op
== '+') {
580 struct expression
*right
= unop
->right
;
581 if (right
->type
== EXPR_VALUE
) {
582 offset
= right
->value
;
587 if (unop
->type
== EXPR_SYMBOL
) {
588 struct symbol
*sym
= unop
->symbol
;
589 struct expression
*value
= constant_symbol_value(sym
, offset
);
591 /* Const symbol with a constant initializer? */
593 /* FIXME! We should check that the size is right! */
594 if (value
->type
== EXPR_VALUE
) {
595 expr
->type
= EXPR_VALUE
;
596 expr
->value
= value
->value
;
598 } else if (value
->type
== EXPR_FVALUE
) {
599 expr
->type
= EXPR_FVALUE
;
600 expr
->fvalue
= value
->fvalue
;
605 /* Direct symbol dereference? Cheap and safe */
606 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
612 static int simplify_preop(struct expression
*expr
)
614 struct expression
*op
= expr
->unop
;
615 unsigned long long v
, mask
;
617 if (op
->type
!= EXPR_VALUE
)
620 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
625 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
629 case '!': v
= !v
; break;
630 case '~': v
= ~v
; break;
633 mask
= mask
| (mask
-1);
634 expr
->value
= v
& mask
;
635 expr
->type
= EXPR_VALUE
;
639 warning(expr
->pos
, "constant integer operation overflow");
643 static int simplify_float_preop(struct expression
*expr
)
645 struct expression
*op
= expr
->unop
;
648 if (op
->type
!= EXPR_FVALUE
)
653 case '-': v
= -v
; break;
657 expr
->type
= EXPR_FVALUE
;
662 * Unary post-ops: x++ and x--
664 static int expand_postop(struct expression
*expr
)
666 expand_expression(expr
->unop
);
670 static int expand_preop(struct expression
*expr
)
676 return expand_dereference(expr
);
679 return expand_addressof(expr
);
681 case SPECIAL_INCREMENT
:
682 case SPECIAL_DECREMENT
:
684 * From a type evaluation standpoint the preops are
685 * the same as the postops
687 return expand_postop(expr
);
692 cost
= expand_expression(expr
->unop
);
694 if (simplify_preop(expr
))
696 if (simplify_float_preop(expr
))
701 static int expand_arguments(struct expression_list
*head
)
704 struct expression
*expr
;
706 FOR_EACH_PTR (head
, expr
) {
707 cost
+= expand_expression(expr
);
708 } END_FOR_EACH_PTR(expr
);
712 static int expand_cast(struct expression
*expr
)
715 struct expression
*target
= expr
->cast_expression
;
717 cost
= expand_expression(target
);
719 /* Simplify normal integer casts.. */
720 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
721 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
727 /* The arguments are constant if the cost of all of them is zero */
728 int expand_constant_p(struct expression
*expr
, int cost
)
730 expr
->type
= EXPR_VALUE
;
735 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
736 int expand_safe_p(struct expression
*expr
, int cost
)
738 expr
->type
= EXPR_VALUE
;
739 expr
->value
= (cost
< SIDE_EFFECTS
);
744 * expand a call expression with a symbol. This
745 * should expand builtins.
747 static int expand_symbol_call(struct expression
*expr
, int cost
)
749 struct expression
*fn
= expr
->fn
;
750 struct symbol
*ctype
= fn
->ctype
;
752 if (fn
->type
!= EXPR_PREOP
)
755 if (ctype
->op
&& ctype
->op
->expand
)
756 return ctype
->op
->expand(expr
, cost
);
761 static int expand_call(struct expression
*expr
)
765 struct expression
*fn
= expr
->fn
;
767 cost
= expand_arguments(expr
->args
);
770 expression_error(expr
, "function has no type");
773 if (sym
->type
== SYM_NODE
)
774 return expand_symbol_call(expr
, cost
);
779 static int expand_expression_list(struct expression_list
*list
)
782 struct expression
*expr
;
784 FOR_EACH_PTR(list
, expr
) {
785 cost
+= expand_expression(expr
);
786 } END_FOR_EACH_PTR(expr
);
791 * We can simplify nested position expressions if
792 * this is a simple (single) positional expression.
794 static int expand_pos_expression(struct expression
*expr
)
796 struct expression
*nested
= expr
->init_expr
;
797 unsigned long offset
= expr
->init_offset
;
798 int nr
= expr
->init_nr
;
801 switch (nested
->type
) {
803 offset
+= nested
->init_offset
;
805 expr
->init_offset
= offset
;
809 case EXPR_INITIALIZER
: {
810 struct expression
*reuse
= nested
, *entry
;
812 FOR_EACH_PTR(expr
->expr_list
, entry
) {
813 if (entry
->type
== EXPR_POS
) {
814 entry
->init_offset
+= offset
;
818 * This happens rarely, but it can happen
819 * with bitfields that are all at offset
822 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
824 reuse
->type
= EXPR_POS
;
825 reuse
->ctype
= entry
->ctype
;
826 reuse
->init_offset
= offset
;
828 reuse
->init_expr
= entry
;
829 REPLACE_CURRENT_PTR(entry
, reuse
);
832 } END_FOR_EACH_PTR(entry
);
841 return expand_expression(nested
);
844 static unsigned long bit_offset(const struct expression
*expr
)
846 unsigned long offset
= 0;
847 while (expr
->type
== EXPR_POS
) {
848 offset
+= expr
->init_offset
<< 3;
849 expr
= expr
->init_expr
;
851 if (expr
&& expr
->ctype
)
852 offset
+= expr
->ctype
->bit_offset
;
856 static int compare_expressions(const void *_a
, const void *_b
)
858 const struct expression
*a
= _a
;
859 const struct expression
*b
= _b
;
860 unsigned long a_pos
= bit_offset(a
);
861 unsigned long b_pos
= bit_offset(b
);
863 return (a_pos
< b_pos
) ? -1 : (a_pos
== b_pos
) ? 0 : 1;
866 static void sort_expression_list(struct expression_list
**list
)
868 sort_list((struct ptr_list
**)list
, compare_expressions
);
871 static void verify_nonoverlapping(struct expression_list
**list
)
873 struct expression
*a
= NULL
;
874 struct expression
*b
;
876 FOR_EACH_PTR(*list
, b
) {
877 if (!b
->ctype
|| !b
->ctype
->bit_size
)
879 if (a
&& bit_offset(a
) == bit_offset(b
)) {
880 sparse_error(a
->pos
, "Initializer entry defined twice");
881 info(b
->pos
, " also defined here");
885 } END_FOR_EACH_PTR(b
);
888 static int expand_expression(struct expression
*expr
)
892 if (!expr
->ctype
|| expr
->ctype
== &bad_ctype
)
895 switch (expr
->type
) {
902 return expand_symbol_expression(expr
);
904 return expand_binop(expr
);
907 return expand_logical(expr
);
910 return expand_comma(expr
);
913 return expand_compare(expr
);
915 case EXPR_ASSIGNMENT
:
916 return expand_assignment(expr
);
919 return expand_preop(expr
);
922 return expand_postop(expr
);
925 case EXPR_IMPLIED_CAST
:
926 return expand_cast(expr
);
929 return expand_call(expr
);
932 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
936 case EXPR_CONDITIONAL
:
937 return expand_conditional(expr
);
939 case EXPR_STATEMENT
: {
940 struct statement
*stmt
= expr
->statement
;
941 int cost
= expand_statement(stmt
);
943 if (stmt
->type
== STMT_EXPRESSION
&& stmt
->expression
)
944 *expr
= *stmt
->expression
;
951 case EXPR_INITIALIZER
:
952 sort_expression_list(&expr
->expr_list
);
953 verify_nonoverlapping(&expr
->expr_list
);
954 return expand_expression_list(expr
->expr_list
);
956 case EXPR_IDENTIFIER
:
963 return expand_expression(expr
->base
) + 1;
966 return expand_pos_expression(expr
);
971 expression_error(expr
, "internal front-end error: sizeof in expansion?");
977 static void expand_const_expression(struct expression
*expr
, const char *where
)
980 expand_expression(expr
);
981 if (expr
->type
!= EXPR_VALUE
)
982 expression_error(expr
, "Expected constant expression in %s", where
);
986 int expand_symbol(struct symbol
*sym
)
989 struct symbol
*base_type
;
993 base_type
= sym
->ctype
.base_type
;
997 retval
= expand_expression(sym
->initializer
);
998 /* expand the body of the symbol */
999 if (base_type
->type
== SYM_FN
) {
1000 if (base_type
->stmt
)
1001 expand_statement(base_type
->stmt
);
1006 static void expand_return_expression(struct statement
*stmt
)
1008 expand_expression(stmt
->expression
);
1011 static int expand_if_statement(struct statement
*stmt
)
1013 struct expression
*expr
= stmt
->if_conditional
;
1015 if (!expr
|| !expr
->ctype
|| expr
->ctype
== &bad_ctype
)
1018 expand_expression(expr
);
1020 /* This is only valid if nobody jumps into the "dead" side */
1022 /* Simplify constant conditionals without even evaluating the false side */
1023 if (expr
->type
== EXPR_VALUE
) {
1024 struct statement
*simple
;
1025 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
1029 stmt
->type
= STMT_NONE
;
1032 expand_statement(simple
);
1034 return SIDE_EFFECTS
;
1037 expand_statement(stmt
->if_true
);
1038 expand_statement(stmt
->if_false
);
1039 return SIDE_EFFECTS
;
1043 * Expanding a compound statement is really just
1044 * about adding up the costs of each individual
1047 * We also collapse a simple compound statement:
1048 * this would trigger for simple inline functions,
1049 * except we would have to check the "return"
1050 * symbol usage. Next time.
1052 static int expand_compound(struct statement
*stmt
)
1054 struct statement
*s
, *last
;
1055 int cost
, statements
;
1058 expand_symbol(stmt
->ret
);
1061 cost
= expand_statement(last
);
1062 statements
= last
!= NULL
;
1063 FOR_EACH_PTR(stmt
->stmts
, s
) {
1066 cost
+= expand_statement(s
);
1067 } END_FOR_EACH_PTR(s
);
1069 if (statements
== 1 && !stmt
->ret
)
1075 static int expand_statement(struct statement
*stmt
)
1080 switch (stmt
->type
) {
1081 case STMT_DECLARATION
: {
1083 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1085 } END_FOR_EACH_PTR(sym
);
1086 return SIDE_EFFECTS
;
1090 expand_return_expression(stmt
);
1091 return SIDE_EFFECTS
;
1093 case STMT_EXPRESSION
:
1094 return expand_expression(stmt
->expression
);
1097 return expand_compound(stmt
);
1100 return expand_if_statement(stmt
);
1103 expand_expression(stmt
->iterator_pre_condition
);
1104 expand_expression(stmt
->iterator_post_condition
);
1105 expand_statement(stmt
->iterator_pre_statement
);
1106 expand_statement(stmt
->iterator_statement
);
1107 expand_statement(stmt
->iterator_post_statement
);
1108 return SIDE_EFFECTS
;
1111 expand_expression(stmt
->switch_expression
);
1112 expand_statement(stmt
->switch_statement
);
1113 return SIDE_EFFECTS
;
1116 expand_const_expression(stmt
->case_expression
, "case statement");
1117 expand_const_expression(stmt
->case_to
, "case statement");
1118 expand_statement(stmt
->case_statement
);
1119 return SIDE_EFFECTS
;
1122 expand_statement(stmt
->label_statement
);
1123 return SIDE_EFFECTS
;
1126 expand_expression(stmt
->goto_expression
);
1127 return SIDE_EFFECTS
;
1132 /* FIXME! Do the asm parameter evaluation! */
1135 expand_expression(stmt
->expression
);
1138 expand_expression(stmt
->range_expression
);
1139 expand_expression(stmt
->range_low
);
1140 expand_expression(stmt
->range_high
);
1143 return SIDE_EFFECTS
;
1146 long long get_expression_value(struct expression
*expr
)
1148 long long value
, mask
;
1149 struct symbol
*ctype
;
1153 ctype
= evaluate_expression(expr
);
1155 expression_error(expr
, "bad constant expression type");
1158 expand_expression(expr
);
1159 if (expr
->type
!= EXPR_VALUE
) {
1160 expression_error(expr
, "bad constant expression");
1164 value
= expr
->value
;
1165 mask
= 1ULL << (ctype
->bit_size
-1);
1168 while (ctype
->type
!= SYM_BASETYPE
)
1169 ctype
= ctype
->ctype
.base_type
;
1170 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1171 value
= value
| mask
| ~(mask
-1);