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
, r
, res
;
279 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
285 if (mod
& MOD_LONGLONG
) {
287 case '+': res
= l
+ r
; break;
288 case '-': res
= l
- r
; break;
289 case '*': res
= l
* r
; break;
290 case '/': if (!r
) goto Div
;
294 } else if (mod
& MOD_LONG
) {
296 case '+': res
= (double) l
+ (double) r
; break;
297 case '-': res
= (double) l
- (double) r
; break;
298 case '*': res
= (double) l
* (double) r
; break;
299 case '/': if (!r
) goto Div
;
300 res
= (double) l
/ (double) r
; break;
305 case '+': res
= (float)l
+ (float)r
; break;
306 case '-': res
= (float)l
- (float)r
; break;
307 case '*': res
= (float)l
* (float)r
; break;
308 case '/': if (!r
) goto Div
;
309 res
= (float)l
/ (float)r
; break;
313 expr
->type
= EXPR_FVALUE
;
317 warn(expr
->pos
, "division by zero");
321 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
323 struct expression
*left
= expr
->left
, *right
= expr
->right
;
326 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
332 case '<': expr
->value
= l
< r
; break;
333 case '>': expr
->value
= l
> r
; break;
334 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
335 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
336 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
337 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
339 expr
->type
= EXPR_VALUE
;
343 static int expand_binop(struct expression
*expr
)
347 cost
= expand_expression(expr
->left
);
348 cost
+= expand_expression(expr
->right
);
349 if (simplify_int_binop(expr
, expr
->ctype
))
351 if (simplify_float_binop(expr
))
356 static int expand_logical(struct expression
*expr
)
358 struct expression
*left
= expr
->left
;
359 struct expression
*right
;
362 /* Do immediate short-circuiting ... */
363 cost
= expand_expression(left
);
364 if (left
->type
== EXPR_VALUE
) {
365 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
367 expr
->type
= EXPR_VALUE
;
373 expr
->type
= EXPR_VALUE
;
381 rcost
= expand_expression(right
);
382 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
384 * We know the left value doesn't matter, since
385 * otherwise we would have short-circuited it..
387 expr
->type
= EXPR_VALUE
;
388 expr
->value
= right
->value
!= 0;
393 * If the right side is safe and cheaper than a branch,
394 * just avoid the branch and turn it into a regular binop
397 if (rcost
< BRANCH_COST
) {
398 expr
->type
= EXPR_BINOP
;
399 rcost
-= BRANCH_COST
- 1;
402 return cost
+ BRANCH_COST
+ rcost
;
405 static int expand_comma(struct expression
*expr
)
409 cost
= expand_expression(expr
->left
);
410 cost
+= expand_expression(expr
->right
);
411 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
)
412 *expr
= *expr
->right
;
416 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
418 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
422 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
423 case SPECIAL_NOTEQUAL
:
424 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
426 return left
->bit_size
< right
->bit_size
;
428 return left
->bit_size
> right
->bit_size
;
430 return left
->bit_size
<= right
->bit_size
;
432 return left
->bit_size
>= right
->bit_size
;
437 static int expand_compare(struct expression
*expr
)
439 struct expression
*left
= expr
->left
, *right
= expr
->right
;
442 cost
= expand_expression(left
);
443 cost
+= expand_expression(right
);
445 /* Type comparison? */
446 if (left
&& right
&& left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
448 expr
->type
= EXPR_VALUE
;
449 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
452 if (simplify_cmp_binop(expr
, left
->ctype
))
454 if (simplify_float_cmp(expr
, left
->ctype
))
459 static int expand_conditional(struct expression
*expr
)
461 struct expression
*cond
= expr
->conditional
;
462 struct expression
*true = expr
->cond_true
;
463 struct expression
*false = expr
->cond_false
;
466 cond_cost
= expand_expression(cond
);
467 if (cond
->type
== EXPR_VALUE
) {
468 true = true ? : cond
;
472 return expand_expression(expr
);
475 cost
= expand_expression(true);
476 cost
+= expand_expression(false);
478 if (cost
< SELECT_COST
) {
479 expr
->type
= EXPR_SELECT
;
480 cost
-= BRANCH_COST
- 1;
483 return cost
+ cond_cost
+ BRANCH_COST
;
486 static int expand_assignment(struct expression
*expr
)
488 expand_expression(expr
->left
);
489 expand_expression(expr
->right
);
493 static int expand_addressof(struct expression
*expr
)
495 return expand_expression(expr
->unop
);
498 static int expand_dereference(struct expression
*expr
)
500 struct expression
*unop
= expr
->unop
;
502 expand_expression(unop
);
505 * NOTE! We get a bogus warning right now for some special
506 * cases: apparently I've screwed up the optimization of
507 * a zero-offset derefence, and the ctype is wrong.
509 * Leave the warning in anyway, since this is also a good
510 * test for me to get the type evaluation right..
512 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
513 warn(unop
->pos
, "dereference of noderef expression");
515 if (unop
->type
== EXPR_SYMBOL
) {
516 struct symbol
*sym
= unop
->symbol
;
518 /* Const symbol with a constant initializer? */
519 if (!(sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))) {
520 struct expression
*value
= sym
->initializer
;
522 if (value
->type
== EXPR_VALUE
) {
523 expr
->type
= EXPR_VALUE
;
524 expr
->value
= value
->value
;
526 } else if (value
->type
== EXPR_FVALUE
) {
527 expr
->type
= EXPR_FVALUE
;
528 expr
->fvalue
= value
->fvalue
;
534 /* Direct symbol dereference? Cheap and safe */
535 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
540 static int simplify_preop(struct expression
*expr
)
542 struct expression
*op
= expr
->unop
;
543 unsigned long long v
, mask
;
545 if (op
->type
!= EXPR_VALUE
)
548 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
553 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
557 case '!': v
= !v
; break;
558 case '~': v
= ~v
; break;
561 mask
= mask
| (mask
-1);
562 expr
->value
= v
& mask
;
563 expr
->type
= EXPR_VALUE
;
567 warn(expr
->pos
, "constant integer operation overflow");
571 static int simplify_float_preop(struct expression
*expr
)
573 struct expression
*op
= expr
->unop
;
576 if (op
->type
!= EXPR_FVALUE
)
581 case '-': v
= -v
; break;
585 expr
->type
= EXPR_FVALUE
;
590 * Unary post-ops: x++ and x--
592 static int expand_postop(struct expression
*expr
)
594 expand_expression(expr
->unop
);
598 static int expand_preop(struct expression
*expr
)
604 return expand_dereference(expr
);
607 return expand_addressof(expr
);
609 case SPECIAL_INCREMENT
:
610 case SPECIAL_DECREMENT
:
612 * From a type evaluation standpoint the pre-ops are
613 * the same as the postops
615 return expand_postop(expr
);
620 cost
= expand_expression(expr
->unop
);
622 if (simplify_preop(expr
))
624 if (simplify_float_preop(expr
))
629 static int expand_arguments(struct expression_list
*head
)
632 struct expression
*expr
;
634 FOR_EACH_PTR (head
, expr
) {
635 cost
+= expand_expression(expr
);
640 static int expand_cast(struct expression
*expr
)
643 struct expression
*target
= expr
->cast_expression
;
645 cost
= expand_expression(target
);
647 /* Simplify normal integer casts.. */
648 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
649 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
656 * expand a call expression with a symbol. This
657 * should expand builtins.
659 static int expand_symbol_call(struct expression
*expr
)
661 struct expression
*fn
= expr
->fn
;
662 struct symbol
*ctype
= fn
->ctype
;
664 if (fn
->type
!= EXPR_PREOP
)
667 if (ctype
->op
&& ctype
->op
->expand
)
668 return ctype
->op
->expand(expr
);
673 static int expand_call(struct expression
*expr
)
677 struct expression
*fn
= expr
->fn
;
679 cost
= expand_arguments(expr
->args
);
681 if (sym
->type
== SYM_NODE
)
682 return expand_symbol_call(expr
);
687 static int expand_expression_list(struct expression_list
*list
)
690 struct expression
*expr
;
692 FOR_EACH_PTR(list
, expr
) {
693 cost
+= expand_expression(expr
);
698 static int expand_expression(struct expression
*expr
)
705 switch (expr
->type
) {
712 return expand_symbol_expression(expr
);
714 return expand_binop(expr
);
717 return expand_logical(expr
);
720 return expand_comma(expr
);
723 return expand_compare(expr
);
725 case EXPR_ASSIGNMENT
:
726 return expand_assignment(expr
);
729 return expand_preop(expr
);
732 return expand_postop(expr
);
735 return expand_cast(expr
);
738 return expand_call(expr
);
741 warn(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
745 return expand_expression(expr
->address
);
748 case EXPR_CONDITIONAL
:
749 return expand_conditional(expr
);
752 return expand_statement(expr
->statement
);
757 case EXPR_INITIALIZER
:
758 return expand_expression_list(expr
->expr_list
);
760 case EXPR_IDENTIFIER
:
767 return expand_expression(expr
->base
) + 1;
770 return expand_expression(expr
->init_expr
);
774 warn(expr
->pos
, "internal front-end error: sizeof in expansion?");
780 static void expand_const_expression(struct expression
*expr
, const char *where
)
783 expand_expression(expr
);
784 if (expr
->type
!= EXPR_VALUE
)
785 warn(expr
->pos
, "Expected constant expression in %s", where
);
789 void expand_symbol(struct symbol
*sym
)
791 struct symbol
*base_type
;
795 base_type
= sym
->ctype
.base_type
;
799 expand_expression(sym
->initializer
);
800 /* expand the body of the symbol */
801 if (base_type
->type
== SYM_FN
) {
803 expand_statement(base_type
->stmt
);
807 static void expand_return_expression(struct statement
*stmt
)
809 expand_expression(stmt
->expression
);
812 static int expand_if_statement(struct statement
*stmt
)
814 struct expression
*expr
= stmt
->if_conditional
;
816 if (!expr
|| !expr
->ctype
)
819 expand_expression(expr
);
821 /* This is only valid if nobody jumps into the "dead" side */
823 /* Simplify constant conditionals without even evaluating the false side */
824 if (expr
->type
== EXPR_VALUE
) {
825 struct statement
*simple
;
826 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
830 stmt
->type
= STMT_NONE
;
833 expand_statement(simple
);
838 expand_statement(stmt
->if_true
);
839 expand_statement(stmt
->if_false
);
843 static int expand_statement(struct statement
*stmt
)
848 switch (stmt
->type
) {
850 expand_return_expression(stmt
);
853 case STMT_EXPRESSION
:
854 return expand_expression(stmt
->expression
);
856 case STMT_COMPOUND
: {
861 FOR_EACH_PTR(stmt
->syms
, sym
) {
864 expand_symbol(stmt
->ret
);
867 FOR_EACH_PTR(stmt
->stmts
, s
) {
868 cost
+= expand_statement(s
);
874 return expand_if_statement(stmt
);
877 expand_expression(stmt
->iterator_pre_condition
);
878 expand_expression(stmt
->iterator_post_condition
);
879 expand_statement(stmt
->iterator_pre_statement
);
880 expand_statement(stmt
->iterator_statement
);
881 expand_statement(stmt
->iterator_post_statement
);
885 expand_expression(stmt
->switch_expression
);
886 expand_statement(stmt
->switch_statement
);
890 expand_const_expression(stmt
->case_expression
, "case statement");
891 expand_const_expression(stmt
->case_to
, "case statement");
892 expand_statement(stmt
->case_statement
);
896 expand_statement(stmt
->label_statement
);
900 expand_expression(stmt
->goto_expression
);
906 /* FIXME! Do the asm parameter evaluation! */
912 long long get_expression_value(struct expression
*expr
)
914 long long value
, mask
;
915 struct symbol
*ctype
;
919 ctype
= evaluate_expression(expr
);
921 warn(expr
->pos
, "bad constant expression type");
924 expand_expression(expr
);
925 if (expr
->type
!= EXPR_VALUE
) {
926 warn(expr
->pos
, "bad constant expression");
931 mask
= 1ULL << (ctype
->bit_size
-1);
934 while (ctype
->type
!= SYM_BASETYPE
)
935 ctype
= ctype
->ctype
.base_type
;
936 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
937 value
= value
| mask
| ~(mask
-1);