4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * expand constant expressions.
26 #include "expression.h"
28 /* Random cost numbers */
29 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
30 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
31 #define BRANCH_COST 10 /* Cost of a conditional branch */
33 static int expand_expression(struct expression
*);
34 static int expand_statement(struct statement
*);
36 static int expand_symbol_expression(struct expression
*expr
)
38 struct symbol
*sym
= expr
->symbol
;
41 * The preprocessor can cause unknown symbols to be generated
44 warn(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
45 expr
->type
= EXPR_VALUE
;
50 /* The cost of a symbol expression is lower for on-stack symbols */
51 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
54 static long long get_longlong(struct expression
*expr
)
56 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
57 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
58 long long value
= expr
->value
;
59 long long ormask
, andmask
;
63 andmask
= mask
| (mask
-1);
67 return (value
& andmask
) | ormask
;
70 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
71 struct expression
*old
, struct symbol
*oldtype
)
73 int old_size
= oldtype
->bit_size
;
74 int new_size
= newtype
->bit_size
;
75 long long value
, mask
;
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 mask
= 1ULL << (new_size
-1);
94 mask
= mask
| (mask
-1);
95 expr
->value
= value
& mask
;
99 if (newtype
->ctype
.base_type
!= &fp_type
) {
100 value
= (long long)old
->fvalue
;
101 expr
->type
= EXPR_VALUE
;
105 if (oldtype
->ctype
.base_type
!= &fp_type
)
106 expr
->fvalue
= (long double)get_longlong(old
);
108 expr
->fvalue
= old
->value
;
110 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
)) {
111 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
112 expr
->fvalue
= (double)expr
->fvalue
;
114 expr
->fvalue
= (float)expr
->fvalue
;
116 expr
->type
= EXPR_FVALUE
;
119 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
121 if (count
>= ctype
->bit_size
) {
122 warn(expr
->pos
, "shift too big for type (%x)", ctype
->ctype
.modifiers
);
123 count
&= ctype
->bit_size
-1;
129 * CAREFUL! We need to get the size and sign of the
132 #define CONVERT(op,s) (((op)<<1)+(s))
133 #define SIGNED(op) CONVERT(op, 1)
134 #define UNSIGNED(op) CONVERT(op, 0)
135 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
137 struct expression
*left
= expr
->left
, *right
= expr
->right
;
138 unsigned long long v
, l
, r
, mask
;
139 signed long long sl
, sr
;
140 int is_signed
, shift
;
142 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
144 l
= left
->value
; r
= right
->value
;
145 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
146 mask
= 1ULL << (ctype
->bit_size
-1);
148 if (is_signed
&& (sl
& mask
))
150 if (is_signed
&& (sr
& mask
))
153 switch (CONVERT(expr
->op
,is_signed
)) {
190 if (l
== mask
&& sr
== -1)
211 case SIGNED(SPECIAL_LEFTSHIFT
):
212 case UNSIGNED(SPECIAL_LEFTSHIFT
):
213 shift
= check_shift_count(expr
, ctype
, r
);
217 case SIGNED(SPECIAL_RIGHTSHIFT
):
218 shift
= check_shift_count(expr
, ctype
, r
);
222 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
223 shift
= check_shift_count(expr
, ctype
, r
);
230 mask
= mask
| (mask
-1);
231 expr
->value
= v
& mask
;
232 expr
->type
= EXPR_VALUE
;
235 warn(expr
->pos
, "division by zero");
238 warn(expr
->pos
, "constant integer operation overflow");
242 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
244 struct expression
*left
= expr
->left
, *right
= expr
->right
;
245 unsigned long long l
, r
, mask
;
246 signed long long sl
, sr
;
248 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
250 l
= left
->value
; r
= right
->value
;
251 mask
= 1ULL << (ctype
->bit_size
-1);
258 case '<': expr
->value
= sl
< sr
; break;
259 case '>': expr
->value
= sl
> sr
; break;
260 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
261 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
262 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
263 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
264 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
265 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
266 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
267 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
269 expr
->type
= EXPR_VALUE
;
273 static int simplify_float_binop(struct expression
*expr
)
275 struct expression
*left
= expr
->left
, *right
= expr
->right
;
276 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
277 long double l
= left
->fvalue
;
278 long double r
= right
->fvalue
;
281 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
283 if (mod
& MOD_LONGLONG
) {
285 case '+': res
= l
+ r
; break;
286 case '-': res
= l
- r
; break;
287 case '*': res
= l
* r
; break;
288 case '/': if (!r
) goto Div
;
292 } else if (mod
& MOD_LONG
) {
294 case '+': res
= (double) l
+ (double) r
; break;
295 case '-': res
= (double) l
- (double) r
; break;
296 case '*': res
= (double) l
* (double) r
; break;
297 case '/': if (!r
) goto Div
;
298 res
= (double) l
/ (double) r
; break;
303 case '+': res
= (float)l
+ (float)r
; break;
304 case '-': res
= (float)l
- (float)r
; break;
305 case '*': res
= (float)l
* (float)r
; break;
306 case '/': if (!r
) goto Div
;
307 res
= (float)l
/ (float)r
; break;
311 expr
->type
= EXPR_FVALUE
;
315 warn(expr
->pos
, "division by zero");
319 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
321 struct expression
*left
= expr
->left
, *right
= expr
->right
;
324 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
330 case '<': expr
->value
= l
< r
; break;
331 case '>': expr
->value
= l
> r
; break;
332 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
333 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
334 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
335 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
337 expr
->type
= EXPR_VALUE
;
341 static int expand_binop(struct expression
*expr
)
345 cost
= expand_expression(expr
->left
);
346 cost
+= expand_expression(expr
->right
);
347 if (simplify_int_binop(expr
, expr
->ctype
))
349 if (simplify_float_binop(expr
))
354 static int expand_logical(struct expression
*expr
)
356 struct expression
*left
= expr
->left
;
357 struct expression
*right
;
360 /* Do immediate short-circuiting ... */
361 cost
= expand_expression(left
);
362 if (left
->type
== EXPR_VALUE
) {
363 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
365 expr
->type
= EXPR_VALUE
;
371 expr
->type
= EXPR_VALUE
;
379 rcost
= expand_expression(right
);
380 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
382 * We know the left value doesn't matter, since
383 * otherwise we would have short-circuited it..
385 expr
->type
= EXPR_VALUE
;
386 expr
->value
= right
->value
!= 0;
391 * If the right side is safe and cheaper than a branch,
392 * just avoid the branch and turn it into a regular binop
395 if (rcost
< BRANCH_COST
) {
396 expr
->type
= EXPR_BINOP
;
397 rcost
-= BRANCH_COST
- 1;
400 return cost
+ BRANCH_COST
+ rcost
;
403 static int expand_comma(struct expression
*expr
)
407 cost
= expand_expression(expr
->left
);
408 cost
+= expand_expression(expr
->right
);
409 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
)
410 *expr
= *expr
->right
;
414 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
416 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
420 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
421 case SPECIAL_NOTEQUAL
:
422 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
424 return left
->bit_size
< right
->bit_size
;
426 return left
->bit_size
> right
->bit_size
;
428 return left
->bit_size
<= right
->bit_size
;
430 return left
->bit_size
>= right
->bit_size
;
435 static int expand_compare(struct expression
*expr
)
437 struct expression
*left
= expr
->left
, *right
= expr
->right
;
440 cost
= expand_expression(left
);
441 cost
+= expand_expression(right
);
443 /* Type comparison? */
444 if (left
&& right
&& left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
446 expr
->type
= EXPR_VALUE
;
447 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
450 if (simplify_cmp_binop(expr
, left
->ctype
))
452 if (simplify_float_cmp(expr
, left
->ctype
))
457 static int expand_conditional(struct expression
*expr
)
459 struct expression
*cond
= expr
->conditional
;
460 struct expression
*true = expr
->cond_true
;
461 struct expression
*false = expr
->cond_false
;
464 cond_cost
= expand_expression(cond
);
465 if (cond
->type
== EXPR_VALUE
) {
466 true = true ? : cond
;
470 return expand_expression(expr
);
473 cost
= expand_expression(true);
474 cost
+= expand_expression(false);
476 if (cost
< SELECT_COST
) {
477 expr
->type
= EXPR_SELECT
;
478 cost
-= BRANCH_COST
- 1;
481 return cost
+ cond_cost
+ BRANCH_COST
;
484 static int expand_assignment(struct expression
*expr
)
486 expand_expression(expr
->left
);
487 expand_expression(expr
->right
);
491 static int expand_addressof(struct expression
*expr
)
493 return expand_expression(expr
->unop
);
496 static int expand_dereference(struct expression
*expr
)
498 struct expression
*unop
= expr
->unop
;
500 expand_expression(unop
);
503 * NOTE! We get a bogus warning right now for some special
504 * cases: apparently I've screwed up the optimization of
505 * a zero-offset derefence, and the ctype is wrong.
507 * Leave the warning in anyway, since this is also a good
508 * test for me to get the type evaluation right..
510 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
511 warn(unop
->pos
, "dereference of noderef expression");
513 if (unop
->type
== EXPR_SYMBOL
) {
514 struct symbol
*sym
= unop
->symbol
;
516 /* Const symbol with a constant initializer? */
517 if (!(sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))) {
518 struct expression
*value
= sym
->initializer
;
520 if (value
->type
== EXPR_VALUE
) {
521 expr
->type
= EXPR_VALUE
;
522 expr
->value
= value
->value
;
524 } else if (value
->type
== EXPR_FVALUE
) {
525 expr
->type
= EXPR_FVALUE
;
526 expr
->fvalue
= value
->fvalue
;
532 /* Direct symbol dereference? Cheap and safe */
533 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
538 static int simplify_preop(struct expression
*expr
)
540 struct expression
*op
= expr
->unop
;
541 unsigned long long v
, mask
;
543 if (op
->type
!= EXPR_VALUE
)
548 case '-': v
= -v
; break;
549 case '!': v
= !v
; break;
550 case '~': v
= ~v
; break;
553 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
554 mask
= mask
| (mask
-1);
555 expr
->value
= v
& mask
;
556 expr
->type
= EXPR_VALUE
;
560 static int simplify_float_preop(struct expression
*expr
)
562 struct expression
*op
= expr
->unop
;
565 if (op
->type
!= EXPR_FVALUE
)
570 case '-': v
= -v
; break;
574 expr
->type
= EXPR_FVALUE
;
579 * Unary post-ops: x++ and x--
581 static int expand_postop(struct expression
*expr
)
583 expand_expression(expr
->unop
);
587 static int expand_preop(struct expression
*expr
)
593 return expand_dereference(expr
);
596 return expand_addressof(expr
);
598 case SPECIAL_INCREMENT
:
599 case SPECIAL_DECREMENT
:
601 * From a type evaluation standpoint the pre-ops are
602 * the same as the postops
604 return expand_postop(expr
);
609 cost
= expand_expression(expr
->unop
);
611 if (simplify_preop(expr
))
613 if (simplify_float_preop(expr
))
618 static int expand_arguments(struct expression_list
*head
)
621 struct expression
*expr
;
623 FOR_EACH_PTR (head
, expr
) {
624 cost
+= expand_expression(expr
);
629 static int expand_cast(struct expression
*expr
)
632 struct expression
*target
= expr
->cast_expression
;
634 cost
= expand_expression(target
);
636 /* Simplify normal integer casts.. */
637 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
638 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
645 * expand a call expression with a symbol. This
646 * should expand builtins.
648 static int expand_symbol_call(struct expression
*expr
)
650 struct expression
*fn
= expr
->fn
;
651 struct symbol
*ctype
= fn
->ctype
;
653 if (fn
->type
!= EXPR_PREOP
)
656 if (ctype
->op
&& ctype
->op
->expand
)
657 return ctype
->op
->expand(expr
);
662 static int expand_call(struct expression
*expr
)
666 struct expression
*fn
= expr
->fn
;
668 cost
= expand_arguments(expr
->args
);
670 if (sym
->type
== SYM_NODE
)
671 return expand_symbol_call(expr
);
676 static int expand_expression_list(struct expression_list
*list
)
679 struct expression
*expr
;
681 FOR_EACH_PTR(list
, expr
) {
682 cost
+= expand_expression(expr
);
687 static int expand_expression(struct expression
*expr
)
694 switch (expr
->type
) {
701 return expand_symbol_expression(expr
);
703 return expand_binop(expr
);
706 return expand_logical(expr
);
709 return expand_comma(expr
);
712 return expand_compare(expr
);
714 case EXPR_ASSIGNMENT
:
715 return expand_assignment(expr
);
718 return expand_preop(expr
);
721 return expand_postop(expr
);
724 return expand_cast(expr
);
727 return expand_call(expr
);
730 warn(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
734 return expand_expression(expr
->address
);
737 case EXPR_CONDITIONAL
:
738 return expand_conditional(expr
);
741 return expand_statement(expr
->statement
);
746 case EXPR_INITIALIZER
:
747 return expand_expression_list(expr
->expr_list
);
749 case EXPR_IDENTIFIER
:
756 return expand_expression(expr
->init_expr
);
760 warn(expr
->pos
, "internal front-end error: sizeof in expansion?");
766 static void expand_const_expression(struct expression
*expr
, const char *where
)
769 expand_expression(expr
);
770 if (expr
->type
!= EXPR_VALUE
)
771 warn(expr
->pos
, "Expected constant expression in %s", where
);
775 void expand_symbol(struct symbol
*sym
)
777 struct symbol
*base_type
;
781 base_type
= sym
->ctype
.base_type
;
785 expand_expression(sym
->initializer
);
786 /* expand the body of the symbol */
787 if (base_type
->type
== SYM_FN
) {
789 expand_statement(base_type
->stmt
);
793 static void expand_return_expression(struct statement
*stmt
)
795 expand_expression(stmt
->expression
);
798 static int expand_if_statement(struct statement
*stmt
)
800 struct expression
*expr
= stmt
->if_conditional
;
802 if (!expr
|| !expr
->ctype
)
805 expand_expression(expr
);
807 /* This is only valid if nobody jumps into the "dead" side */
809 /* Simplify constant conditionals without even evaluating the false side */
810 if (expr
->type
== EXPR_VALUE
) {
811 struct statement
*simple
;
812 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
816 stmt
->type
= STMT_NONE
;
819 expand_statement(simple
);
824 expand_statement(stmt
->if_true
);
825 expand_statement(stmt
->if_false
);
829 static int expand_statement(struct statement
*stmt
)
834 switch (stmt
->type
) {
836 expand_return_expression(stmt
);
839 case STMT_EXPRESSION
:
840 return expand_expression(stmt
->expression
);
842 case STMT_COMPOUND
: {
847 FOR_EACH_PTR(stmt
->syms
, sym
) {
850 expand_symbol(stmt
->ret
);
853 FOR_EACH_PTR(stmt
->stmts
, s
) {
854 cost
+= expand_statement(s
);
860 return expand_if_statement(stmt
);
863 expand_expression(stmt
->iterator_pre_condition
);
864 expand_expression(stmt
->iterator_post_condition
);
865 expand_statement(stmt
->iterator_pre_statement
);
866 expand_statement(stmt
->iterator_statement
);
867 expand_statement(stmt
->iterator_post_statement
);
871 expand_expression(stmt
->switch_expression
);
872 expand_statement(stmt
->switch_statement
);
876 expand_const_expression(stmt
->case_expression
, "case statement");
877 expand_const_expression(stmt
->case_to
, "case statement");
878 expand_statement(stmt
->case_statement
);
882 expand_statement(stmt
->label_statement
);
886 expand_expression(stmt
->goto_expression
);
892 /* FIXME! Do the asm parameter evaluation! */
898 long long get_expression_value(struct expression
*expr
)
900 long long value
, mask
;
901 struct symbol
*ctype
;
905 ctype
= evaluate_expression(expr
);
907 warn(expr
->pos
, "bad constant expression type");
910 expand_expression(expr
);
911 if (expr
->type
!= EXPR_VALUE
) {
912 warn(expr
->pos
, "bad constant expression");
917 mask
= 1ULL << (ctype
->bit_size
-1);
920 while (ctype
->type
!= SYM_BASETYPE
)
921 ctype
= ctype
->ctype
.base_type
;
922 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
923 value
= value
| mask
| ~(mask
-1);