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 SIDE_EFFECTS 10000 /* The expression has side effects */
30 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
31 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
32 #define BRANCH_COST 10 /* Cost of a conditional branch */
34 static int expand_expression(struct expression
*);
35 static int expand_statement(struct statement
*);
37 static int expand_symbol_expression(struct expression
*expr
)
39 struct symbol
*sym
= expr
->symbol
;
42 * The preprocessor can cause unknown symbols to be generated
45 warn(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
46 expr
->type
= EXPR_VALUE
;
51 /* The cost of a symbol expression is lower for on-stack symbols */
52 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
55 static long long get_longlong(struct expression
*expr
)
57 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
58 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
59 long long value
= expr
->value
;
60 long long ormask
, andmask
;
64 andmask
= mask
| (mask
-1);
68 return (value
& andmask
) | ormask
;
71 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
72 struct expression
*old
, struct symbol
*oldtype
)
74 int old_size
= oldtype
->bit_size
;
75 int new_size
= newtype
->bit_size
;
76 long long value
, mask
;
78 if (newtype
->ctype
.base_type
== &fp_type
||
79 oldtype
->ctype
.base_type
== &fp_type
)
82 // For pointers and integers, we can just move the value around
83 expr
->type
= EXPR_VALUE
;
84 if (old_size
== new_size
) {
85 expr
->value
= old
->value
;
89 // expand it to the full "long long" value
90 value
= get_longlong(old
);
93 // Truncate it to the new size
94 mask
= 1ULL << (new_size
-1);
95 mask
= mask
| (mask
-1);
96 expr
->value
= value
& mask
;
100 if (newtype
->ctype
.base_type
!= &fp_type
) {
101 value
= (long long)old
->fvalue
;
102 expr
->type
= EXPR_VALUE
;
106 if (oldtype
->ctype
.base_type
!= &fp_type
)
107 expr
->fvalue
= (long double)get_longlong(old
);
109 expr
->fvalue
= old
->value
;
111 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
)) {
112 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
113 expr
->fvalue
= (double)expr
->fvalue
;
115 expr
->fvalue
= (float)expr
->fvalue
;
117 expr
->type
= EXPR_FVALUE
;
120 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
122 if (count
>= ctype
->bit_size
) {
123 warn(expr
->pos
, "shift too big for type (%x)", ctype
->ctype
.modifiers
);
124 count
&= ctype
->bit_size
-1;
130 * CAREFUL! We need to get the size and sign of the
133 #define CONVERT(op,s) (((op)<<1)+(s))
134 #define SIGNED(op) CONVERT(op, 1)
135 #define UNSIGNED(op) CONVERT(op, 0)
136 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
138 struct expression
*left
= expr
->left
, *right
= expr
->right
;
139 unsigned long long v
, l
, r
, mask
;
140 signed long long sl
, sr
;
141 int is_signed
, shift
;
143 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
145 l
= left
->value
; r
= right
->value
;
146 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
147 mask
= 1ULL << (ctype
->bit_size
-1);
149 if (is_signed
&& (sl
& mask
))
151 if (is_signed
&& (sr
& mask
))
154 switch (CONVERT(expr
->op
,is_signed
)) {
191 if (l
== mask
&& sr
== -1)
212 case SIGNED(SPECIAL_LEFTSHIFT
):
213 case UNSIGNED(SPECIAL_LEFTSHIFT
):
214 shift
= check_shift_count(expr
, ctype
, r
);
218 case SIGNED(SPECIAL_RIGHTSHIFT
):
219 shift
= check_shift_count(expr
, ctype
, r
);
223 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
224 shift
= check_shift_count(expr
, ctype
, r
);
231 mask
= mask
| (mask
-1);
232 expr
->value
= v
& mask
;
233 expr
->type
= EXPR_VALUE
;
236 warn(expr
->pos
, "division by zero");
239 warn(expr
->pos
, "constant integer operation overflow");
243 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
245 struct expression
*left
= expr
->left
, *right
= expr
->right
;
246 unsigned long long l
, r
, mask
;
247 signed long long sl
, sr
;
249 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
251 l
= left
->value
; r
= right
->value
;
252 mask
= 1ULL << (ctype
->bit_size
-1);
259 case '<': expr
->value
= sl
< sr
; break;
260 case '>': expr
->value
= sl
> sr
; break;
261 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
262 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
263 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
264 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
265 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
266 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
267 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
268 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
270 expr
->type
= EXPR_VALUE
;
274 static int simplify_float_binop(struct expression
*expr
)
276 struct expression
*left
= expr
->left
, *right
= expr
->right
;
277 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
278 long double l
, r
, res
;
280 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
286 if (mod
& MOD_LONGLONG
) {
288 case '+': res
= l
+ r
; break;
289 case '-': res
= l
- r
; break;
290 case '*': res
= l
* r
; break;
291 case '/': if (!r
) goto Div
;
295 } else if (mod
& MOD_LONG
) {
297 case '+': res
= (double) l
+ (double) r
; break;
298 case '-': res
= (double) l
- (double) r
; break;
299 case '*': res
= (double) l
* (double) r
; break;
300 case '/': if (!r
) goto Div
;
301 res
= (double) l
/ (double) r
; break;
306 case '+': res
= (float)l
+ (float)r
; break;
307 case '-': res
= (float)l
- (float)r
; break;
308 case '*': res
= (float)l
* (float)r
; break;
309 case '/': if (!r
) goto Div
;
310 res
= (float)l
/ (float)r
; break;
314 expr
->type
= EXPR_FVALUE
;
318 warn(expr
->pos
, "division by zero");
322 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
324 struct expression
*left
= expr
->left
, *right
= expr
->right
;
327 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
333 case '<': expr
->value
= l
< r
; break;
334 case '>': expr
->value
= l
> r
; break;
335 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
336 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
337 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
338 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
340 expr
->type
= EXPR_VALUE
;
344 static int expand_binop(struct expression
*expr
)
348 cost
= expand_expression(expr
->left
);
349 cost
+= expand_expression(expr
->right
);
350 if (simplify_int_binop(expr
, expr
->ctype
))
352 if (simplify_float_binop(expr
))
357 static int expand_logical(struct expression
*expr
)
359 struct expression
*left
= expr
->left
;
360 struct expression
*right
;
363 /* Do immediate short-circuiting ... */
364 cost
= expand_expression(left
);
365 if (left
->type
== EXPR_VALUE
) {
366 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
368 expr
->type
= EXPR_VALUE
;
374 expr
->type
= EXPR_VALUE
;
382 rcost
= expand_expression(right
);
383 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
385 * We know the left value doesn't matter, since
386 * otherwise we would have short-circuited it..
388 expr
->type
= EXPR_VALUE
;
389 expr
->value
= right
->value
!= 0;
394 * If the right side is safe and cheaper than a branch,
395 * just avoid the branch and turn it into a regular binop
398 if (rcost
< BRANCH_COST
) {
399 expr
->type
= EXPR_BINOP
;
400 rcost
-= BRANCH_COST
- 1;
403 return cost
+ BRANCH_COST
+ rcost
;
406 static int expand_comma(struct expression
*expr
)
410 cost
= expand_expression(expr
->left
);
411 cost
+= expand_expression(expr
->right
);
412 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
)
413 *expr
= *expr
->right
;
417 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
419 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
423 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
424 case SPECIAL_NOTEQUAL
:
425 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
427 return left
->bit_size
< right
->bit_size
;
429 return left
->bit_size
> right
->bit_size
;
431 return left
->bit_size
<= right
->bit_size
;
433 return left
->bit_size
>= right
->bit_size
;
438 static int expand_compare(struct expression
*expr
)
440 struct expression
*left
= expr
->left
, *right
= expr
->right
;
443 cost
= expand_expression(left
);
444 cost
+= expand_expression(right
);
446 /* Type comparison? */
447 if (left
&& right
&& left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
449 expr
->type
= EXPR_VALUE
;
450 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
453 if (simplify_cmp_binop(expr
, left
->ctype
))
455 if (simplify_float_cmp(expr
, left
->ctype
))
460 static int expand_conditional(struct expression
*expr
)
462 struct expression
*cond
= expr
->conditional
;
463 struct expression
*true = expr
->cond_true
;
464 struct expression
*false = expr
->cond_false
;
467 cond_cost
= expand_expression(cond
);
468 if (cond
->type
== EXPR_VALUE
) {
469 true = true ? : cond
;
473 return expand_expression(expr
);
476 cost
= expand_expression(true);
477 cost
+= expand_expression(false);
479 if (cost
< SELECT_COST
) {
480 expr
->type
= EXPR_SELECT
;
481 cost
-= BRANCH_COST
- 1;
484 return cost
+ cond_cost
+ BRANCH_COST
;
487 static int expand_assignment(struct expression
*expr
)
489 expand_expression(expr
->left
);
490 expand_expression(expr
->right
);
494 static int expand_addressof(struct expression
*expr
)
496 return expand_expression(expr
->unop
);
499 static int expand_dereference(struct expression
*expr
)
501 struct expression
*unop
= expr
->unop
;
503 expand_expression(unop
);
506 * NOTE! We get a bogus warning right now for some special
507 * cases: apparently I've screwed up the optimization of
508 * a zero-offset derefence, and the ctype is wrong.
510 * Leave the warning in anyway, since this is also a good
511 * test for me to get the type evaluation right..
513 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
514 warn(unop
->pos
, "dereference of noderef expression");
516 if (unop
->type
== EXPR_SYMBOL
) {
517 struct symbol
*sym
= unop
->symbol
;
519 /* Const symbol with a constant initializer? */
520 if (!(sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))) {
521 struct expression
*value
= sym
->initializer
;
523 if (value
->type
== EXPR_VALUE
) {
524 expr
->type
= EXPR_VALUE
;
525 expr
->value
= value
->value
;
527 } else if (value
->type
== EXPR_FVALUE
) {
528 expr
->type
= EXPR_FVALUE
;
529 expr
->fvalue
= value
->fvalue
;
535 /* Direct symbol dereference? Cheap and safe */
536 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
541 static int simplify_preop(struct expression
*expr
)
543 struct expression
*op
= expr
->unop
;
544 unsigned long long v
, mask
;
546 if (op
->type
!= EXPR_VALUE
)
549 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
554 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
558 case '!': v
= !v
; break;
559 case '~': v
= ~v
; break;
562 mask
= mask
| (mask
-1);
563 expr
->value
= v
& mask
;
564 expr
->type
= EXPR_VALUE
;
568 warn(expr
->pos
, "constant integer operation overflow");
572 static int simplify_float_preop(struct expression
*expr
)
574 struct expression
*op
= expr
->unop
;
577 if (op
->type
!= EXPR_FVALUE
)
582 case '-': v
= -v
; break;
586 expr
->type
= EXPR_FVALUE
;
591 * Unary post-ops: x++ and x--
593 static int expand_postop(struct expression
*expr
)
595 expand_expression(expr
->unop
);
599 static int expand_preop(struct expression
*expr
)
605 return expand_dereference(expr
);
608 return expand_addressof(expr
);
610 case SPECIAL_INCREMENT
:
611 case SPECIAL_DECREMENT
:
613 * From a type evaluation standpoint the pre-ops are
614 * the same as the postops
616 return expand_postop(expr
);
621 cost
= expand_expression(expr
->unop
);
623 if (simplify_preop(expr
))
625 if (simplify_float_preop(expr
))
630 static int expand_arguments(struct expression_list
*head
)
633 struct expression
*expr
;
635 FOR_EACH_PTR (head
, expr
) {
636 cost
+= expand_expression(expr
);
641 static int expand_cast(struct expression
*expr
)
644 struct expression
*target
= expr
->cast_expression
;
646 cost
= expand_expression(target
);
648 /* Simplify normal integer casts.. */
649 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
) {
650 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
657 * expand a call expression with a symbol. This
658 * should expand builtins.
660 static int expand_symbol_call(struct expression
*expr
)
662 struct expression
*fn
= expr
->fn
;
663 struct symbol
*ctype
= fn
->ctype
;
665 if (fn
->type
!= EXPR_PREOP
)
668 if (ctype
->op
&& ctype
->op
->expand
)
669 return ctype
->op
->expand(expr
);
674 static int expand_call(struct expression
*expr
)
678 struct expression
*fn
= expr
->fn
;
680 cost
= expand_arguments(expr
->args
);
682 if (sym
->type
== SYM_NODE
)
683 return expand_symbol_call(expr
);
688 static int expand_expression_list(struct expression_list
*list
)
691 struct expression
*expr
;
693 FOR_EACH_PTR(list
, expr
) {
694 cost
+= expand_expression(expr
);
699 static int expand_expression(struct expression
*expr
)
706 switch (expr
->type
) {
713 return expand_symbol_expression(expr
);
715 return expand_binop(expr
);
718 return expand_logical(expr
);
721 return expand_comma(expr
);
724 return expand_compare(expr
);
726 case EXPR_ASSIGNMENT
:
727 return expand_assignment(expr
);
730 return expand_preop(expr
);
733 return expand_postop(expr
);
736 return expand_cast(expr
);
739 return expand_call(expr
);
742 warn(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
746 return expand_expression(expr
->address
);
749 case EXPR_CONDITIONAL
:
750 return expand_conditional(expr
);
753 return expand_statement(expr
->statement
);
758 case EXPR_INITIALIZER
:
759 return expand_expression_list(expr
->expr_list
);
761 case EXPR_IDENTIFIER
:
768 return expand_expression(expr
->base
) + 1;
771 return expand_expression(expr
->init_expr
);
775 warn(expr
->pos
, "internal front-end error: sizeof in expansion?");
781 static void expand_const_expression(struct expression
*expr
, const char *where
)
784 expand_expression(expr
);
785 if (expr
->type
!= EXPR_VALUE
)
786 warn(expr
->pos
, "Expected constant expression in %s", where
);
790 void expand_symbol(struct symbol
*sym
)
792 struct symbol
*base_type
;
796 base_type
= sym
->ctype
.base_type
;
800 expand_expression(sym
->initializer
);
801 /* expand the body of the symbol */
802 if (base_type
->type
== SYM_FN
) {
804 expand_statement(base_type
->stmt
);
808 static void expand_return_expression(struct statement
*stmt
)
810 expand_expression(stmt
->expression
);
813 static int expand_if_statement(struct statement
*stmt
)
815 struct expression
*expr
= stmt
->if_conditional
;
817 if (!expr
|| !expr
->ctype
)
820 expand_expression(expr
);
822 /* This is only valid if nobody jumps into the "dead" side */
824 /* Simplify constant conditionals without even evaluating the false side */
825 if (expr
->type
== EXPR_VALUE
) {
826 struct statement
*simple
;
827 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
831 stmt
->type
= STMT_NONE
;
834 expand_statement(simple
);
839 expand_statement(stmt
->if_true
);
840 expand_statement(stmt
->if_false
);
844 static int expand_statement(struct statement
*stmt
)
849 switch (stmt
->type
) {
851 expand_return_expression(stmt
);
854 case STMT_EXPRESSION
:
855 return expand_expression(stmt
->expression
);
857 case STMT_COMPOUND
: {
862 FOR_EACH_PTR(stmt
->syms
, sym
) {
865 expand_symbol(stmt
->ret
);
868 FOR_EACH_PTR(stmt
->stmts
, s
) {
869 cost
+= expand_statement(s
);
875 return expand_if_statement(stmt
);
878 expand_expression(stmt
->iterator_pre_condition
);
879 expand_expression(stmt
->iterator_post_condition
);
880 expand_statement(stmt
->iterator_pre_statement
);
881 expand_statement(stmt
->iterator_statement
);
882 expand_statement(stmt
->iterator_post_statement
);
886 expand_expression(stmt
->switch_expression
);
887 expand_statement(stmt
->switch_statement
);
891 expand_const_expression(stmt
->case_expression
, "case statement");
892 expand_const_expression(stmt
->case_to
, "case statement");
893 expand_statement(stmt
->case_statement
);
897 expand_statement(stmt
->label_statement
);
901 expand_expression(stmt
->goto_expression
);
907 /* FIXME! Do the asm parameter evaluation! */
913 long long get_expression_value(struct expression
*expr
)
915 long long value
, mask
;
916 struct symbol
*ctype
;
920 ctype
= evaluate_expression(expr
);
922 warn(expr
->pos
, "bad constant expression type");
925 expand_expression(expr
);
926 if (expr
->type
!= EXPR_VALUE
) {
927 warn(expr
->pos
, "bad constant expression");
932 mask
= 1ULL << (ctype
->bit_size
-1);
935 while (ctype
->type
!= SYM_BASETYPE
)
936 ctype
= ctype
->ctype
.base_type
;
937 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
938 value
= value
| mask
| ~(mask
-1);