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 static void expand_expression(struct expression
*);
29 static void expand_statement(struct statement
*);
31 static void expand_symbol_expression(struct expression
*expr
)
33 struct symbol
*sym
= expr
->symbol
;
36 * The preprocessor can cause unknown symbols to be generated
39 warn(expr
->pos
, "undefined preprocessor identifier '%s'", show_ident(expr
->symbol_name
));
40 expr
->type
= EXPR_VALUE
;
46 static long long get_longlong(struct expression
*expr
)
48 int no_expand
= expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
;
49 long long mask
= 1ULL << (expr
->ctype
->bit_size
- 1);
50 long long value
= expr
->value
;
51 long long ormask
, andmask
;
55 andmask
= mask
| (mask
-1);
59 return (value
& andmask
) | ormask
;
62 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
63 struct expression
*old
, struct symbol
*oldtype
)
65 int old_size
= oldtype
->bit_size
;
66 int new_size
= newtype
->bit_size
;
67 long long value
, mask
;
69 if (newtype
->ctype
.base_type
== &fp_type
||
70 oldtype
->ctype
.base_type
== &fp_type
)
73 // For pointers and integers, we can just move the value around
74 expr
->type
= EXPR_VALUE
;
75 if (old_size
== new_size
) {
76 expr
->value
= old
->value
;
80 // expand it to the full "long long" value
81 value
= get_longlong(old
);
84 // Truncate it to the new size
85 mask
= 1ULL << (new_size
-1);
86 mask
= mask
| (mask
-1);
87 expr
->value
= value
& mask
;
91 if (newtype
->ctype
.base_type
!= &fp_type
) {
92 value
= (long long)old
->fvalue
;
93 expr
->type
= EXPR_VALUE
;
97 if (oldtype
->ctype
.base_type
!= &fp_type
)
98 expr
->fvalue
= (long double)get_longlong(old
);
100 expr
->fvalue
= old
->value
;
102 if (!(newtype
->ctype
.modifiers
& MOD_LONGLONG
)) {
103 if ((newtype
->ctype
.modifiers
& MOD_LONG
))
104 expr
->fvalue
= (double)expr
->fvalue
;
106 expr
->fvalue
= (float)expr
->fvalue
;
108 expr
->type
= EXPR_FVALUE
;
111 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
113 if (count
>= ctype
->bit_size
) {
114 warn(expr
->pos
, "shift too big for type (%x)", ctype
->ctype
.modifiers
);
115 count
&= ctype
->bit_size
-1;
121 * CAREFUL! We need to get the size and sign of the
124 #define CONVERT(op,s) (((op)<<1)+(s))
125 #define SIGNED(op) CONVERT(op, 1)
126 #define UNSIGNED(op) CONVERT(op, 0)
127 static int simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
129 struct expression
*left
= expr
->left
, *right
= expr
->right
;
130 unsigned long long v
, l
, r
, mask
;
131 signed long long sl
, sr
;
132 int is_signed
, shift
;
134 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
136 l
= left
->value
; r
= right
->value
;
137 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
138 mask
= 1ULL << (ctype
->bit_size
-1);
140 if (is_signed
&& (sl
& mask
))
142 if (is_signed
&& (sr
& mask
))
145 switch (CONVERT(expr
->op
,is_signed
)) {
182 if (l
== mask
&& sr
== -1)
203 case SIGNED(SPECIAL_LEFTSHIFT
):
204 case UNSIGNED(SPECIAL_LEFTSHIFT
):
205 shift
= check_shift_count(expr
, ctype
, r
);
209 case SIGNED(SPECIAL_RIGHTSHIFT
):
210 shift
= check_shift_count(expr
, ctype
, r
);
214 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
215 shift
= check_shift_count(expr
, ctype
, r
);
222 mask
= mask
| (mask
-1);
223 expr
->value
= v
& mask
;
224 expr
->type
= EXPR_VALUE
;
227 warn(expr
->pos
, "division by zero");
230 warn(expr
->pos
, "constant integer operation overflow");
234 static int simplify_cmp_binop(struct expression
*expr
, struct symbol
*ctype
)
236 struct expression
*left
= expr
->left
, *right
= expr
->right
;
237 unsigned long long l
, r
, mask
;
238 signed long long sl
, sr
;
240 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
242 l
= left
->value
; r
= right
->value
;
243 mask
= 1ULL << (ctype
->bit_size
-1);
250 case '<': expr
->value
= sl
< sr
; break;
251 case '>': expr
->value
= sl
> sr
; break;
252 case SPECIAL_LTE
: expr
->value
= sl
<= sr
; break;
253 case SPECIAL_GTE
: expr
->value
= sl
>= sr
; break;
254 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
255 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
256 case SPECIAL_UNSIGNED_LT
:expr
->value
= l
< r
; break;
257 case SPECIAL_UNSIGNED_GT
:expr
->value
= l
> r
; break;
258 case SPECIAL_UNSIGNED_LTE
:expr
->value
= l
<= r
; break;
259 case SPECIAL_UNSIGNED_GTE
:expr
->value
= l
>= r
; break;
261 expr
->type
= EXPR_VALUE
;
265 static int simplify_float_binop(struct expression
*expr
)
267 struct expression
*left
= expr
->left
, *right
= expr
->right
;
268 unsigned long mod
= expr
->ctype
->ctype
.modifiers
;
269 long double l
= left
->fvalue
;
270 long double r
= right
->fvalue
;
273 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
275 if (mod
& MOD_LONGLONG
) {
277 case '+': res
= l
+ r
; break;
278 case '-': res
= l
- r
; break;
279 case '*': res
= l
* r
; break;
280 case '/': if (!r
) goto Div
;
284 } else if (mod
& MOD_LONG
) {
286 case '+': res
= (double) l
+ (double) r
; break;
287 case '-': res
= (double) l
- (double) r
; break;
288 case '*': res
= (double) l
* (double) r
; break;
289 case '/': if (!r
) goto Div
;
290 res
= (double) l
/ (double) r
; break;
295 case '+': res
= (float)l
+ (float)r
; break;
296 case '-': res
= (float)l
- (float)r
; break;
297 case '*': res
= (float)l
* (float)r
; break;
298 case '/': if (!r
) goto Div
;
299 res
= (float)l
/ (float)r
; break;
303 expr
->type
= EXPR_FVALUE
;
307 warn(expr
->pos
, "division by zero");
311 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
313 struct expression
*left
= expr
->left
, *right
= expr
->right
;
316 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
322 case '<': expr
->value
= l
< r
; break;
323 case '>': expr
->value
= l
> r
; break;
324 case SPECIAL_LTE
: expr
->value
= l
<= r
; break;
325 case SPECIAL_GTE
: expr
->value
= l
>= r
; break;
326 case SPECIAL_EQUAL
: expr
->value
= l
== r
; break;
327 case SPECIAL_NOTEQUAL
: expr
->value
= l
!= r
; break;
329 expr
->type
= EXPR_VALUE
;
333 static void expand_binop(struct expression
*expr
)
335 if (simplify_int_binop(expr
, expr
->ctype
))
337 simplify_float_binop(expr
);
340 static void expand_logical(struct expression
*expr
)
342 struct expression
*left
= expr
->left
;
343 struct expression
*right
;
345 /* Do immediate short-circuiting ... */
346 expand_expression(left
);
347 if (left
->type
== EXPR_VALUE
) {
348 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
350 expr
->type
= EXPR_VALUE
;
356 expr
->type
= EXPR_VALUE
;
364 expand_expression(right
);
365 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
367 * We know the left value doesn't matter, since
368 * otherwise we would have short-circuited it..
370 expr
->type
= EXPR_VALUE
;
371 expr
->value
= right
->value
!= 0;
375 static void expand_comma(struct expression
*expr
)
377 if (expr
->left
->type
== EXPR_VALUE
|| expr
->left
->type
== EXPR_FVALUE
)
378 *expr
= *expr
->right
;
381 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
383 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
387 return !type_difference(left
, right
, MOD_IGN
, MOD_IGN
);
388 case SPECIAL_NOTEQUAL
:
389 return type_difference(left
, right
, MOD_IGN
, MOD_IGN
) != NULL
;
391 return left
->bit_size
< right
->bit_size
;
393 return left
->bit_size
> right
->bit_size
;
395 return left
->bit_size
<= right
->bit_size
;
397 return left
->bit_size
>= right
->bit_size
;
402 static void expand_compare(struct expression
*expr
)
404 struct expression
*left
= expr
->left
, *right
= expr
->right
;
406 /* Type comparison? */
407 if (left
&& right
&& left
->type
== EXPR_TYPE
&& right
->type
== EXPR_TYPE
) {
409 expr
->type
= EXPR_VALUE
;
410 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
413 if (simplify_cmp_binop(expr
, left
->ctype
))
415 simplify_float_cmp(expr
, left
->ctype
);
418 static void expand_conditional(struct expression
*expr
)
420 struct expression
*cond
= expr
->conditional
;
422 if (cond
->type
== EXPR_VALUE
) {
423 struct expression
*true, *false;
425 true = expr
->cond_true
? : cond
;
426 false = expr
->cond_false
;
434 static void expand_assignment(struct expression
*expr
)
438 static void expand_addressof(struct expression
*expr
)
442 static void expand_dereference(struct expression
*expr
)
444 struct expression
*unop
= expr
->unop
;
447 * NOTE! We get a bogus warning right now for some special
448 * cases: apparently I've screwed up the optimization of
449 * a zero-offset derefence, and the ctype is wrong.
451 * Leave the warning in anyway, since this is also a good
452 * test for me to get the type evaluation right..
454 if (expr
->ctype
->ctype
.modifiers
& MOD_NODEREF
)
455 warn(unop
->pos
, "dereference of noderef expression");
457 if (unop
->type
== EXPR_SYMBOL
) {
458 struct symbol
*sym
= unop
->symbol
;
460 /* Const symbol with a constant initializer? */
461 if (!(sym
->ctype
.modifiers
& (MOD_ASSIGNED
| MOD_ADDRESSABLE
))) {
462 struct expression
*value
= sym
->initializer
;
464 if (value
->type
== EXPR_VALUE
) {
465 expr
->type
= EXPR_VALUE
;
466 expr
->value
= value
->value
;
467 } else if (value
->type
== EXPR_FVALUE
) {
468 expr
->type
= EXPR_FVALUE
;
469 expr
->fvalue
= value
->fvalue
;
476 static int simplify_preop(struct expression
*expr
)
478 struct expression
*op
= expr
->unop
;
479 unsigned long long v
, mask
;
481 if (op
->type
!= EXPR_VALUE
)
486 case '-': v
= -v
; break;
487 case '!': v
= !v
; break;
488 case '~': v
= ~v
; break;
491 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
492 mask
= mask
| (mask
-1);
493 expr
->value
= v
& mask
;
494 expr
->type
= EXPR_VALUE
;
498 static int simplify_float_preop(struct expression
*expr
)
500 struct expression
*op
= expr
->unop
;
503 if (op
->type
!= EXPR_FVALUE
)
508 case '-': v
= -v
; break;
512 expr
->type
= EXPR_FVALUE
;
517 * Unary post-ops: x++ and x--
519 static void expand_postop(struct expression
*expr
)
523 static void expand_preop(struct expression
*expr
)
527 expand_dereference(expr
);
531 expand_addressof(expr
);
534 case SPECIAL_INCREMENT
:
535 case SPECIAL_DECREMENT
:
537 * From a type evaluation standpoint the pre-ops are
538 * the same as the postops
546 if (simplify_preop(expr
))
548 simplify_float_preop(expr
);
551 static void expand_arguments(struct expression_list
*head
)
553 struct expression
*expr
;
555 FOR_EACH_PTR (head
, expr
) {
556 expand_expression(expr
);
560 static void expand_cast(struct expression
*expr
)
562 struct expression
*target
= expr
->cast_expression
;
564 expand_expression(target
);
566 /* Simplify normal integer casts.. */
567 if (target
->type
== EXPR_VALUE
|| target
->type
== EXPR_FVALUE
)
568 cast_value(expr
, expr
->ctype
, target
, target
->ctype
);
572 * expand a call expression with a symbol. This
573 * should expand inline functions, and expand
576 static void expand_symbol_call(struct expression
*expr
)
578 struct expression
*fn
= expr
->fn
;
579 struct symbol
*ctype
= fn
->ctype
;
581 if (fn
->type
!= EXPR_PREOP
)
584 if (ctype
->op
&& ctype
->op
->expand
) {
585 ctype
->op
->expand(expr
);
590 static void expand_call(struct expression
*expr
)
593 struct expression
*fn
= expr
->fn
;
596 expand_arguments(expr
->args
);
597 if (sym
->type
== SYM_NODE
)
598 expand_symbol_call(expr
);
601 static void expand_expression_list(struct expression_list
*list
)
603 struct expression
*expr
;
605 FOR_EACH_PTR(list
, expr
) {
606 expand_expression(expr
);
610 static void expand_expression(struct expression
*expr
)
617 switch (expr
->type
) {
624 expand_symbol_expression(expr
);
627 expand_expression(expr
->left
);
628 expand_expression(expr
->right
);
633 expand_logical(expr
);
637 expand_expression(expr
->left
);
638 expand_expression(expr
->right
);
643 expand_expression(expr
->left
);
644 expand_expression(expr
->right
);
645 expand_compare(expr
);
648 case EXPR_ASSIGNMENT
:
649 expand_expression(expr
->left
);
650 expand_expression(expr
->right
);
651 expand_assignment(expr
);
655 expand_expression(expr
->unop
);
660 expand_expression(expr
->unop
);
676 expand_expression(expr
->address
);
679 case EXPR_CONDITIONAL
:
680 expand_expression(expr
->conditional
);
681 expand_expression(expr
->cond_false
);
682 expand_expression(expr
->cond_true
);
683 expand_conditional(expr
);
687 expand_statement(expr
->statement
);
693 case EXPR_INITIALIZER
:
694 expand_expression_list(expr
->expr_list
);
697 case EXPR_IDENTIFIER
:
704 expand_expression(expr
->init_expr
);
709 warn(expr
->pos
, "internal front-end error: sizeof in expansion?");
715 static void expand_const_expression(struct expression
*expr
, const char *where
)
718 expand_expression(expr
);
719 if (expr
->type
!= EXPR_VALUE
)
720 warn(expr
->pos
, "Expected constant expression in %s", where
);
724 void expand_symbol(struct symbol
*sym
)
726 struct symbol
*base_type
;
730 base_type
= sym
->ctype
.base_type
;
734 expand_expression(sym
->initializer
);
735 /* expand the body of the symbol */
736 if (base_type
->type
== SYM_FN
) {
738 expand_statement(base_type
->stmt
);
742 static void expand_return_expression(struct statement
*stmt
)
744 expand_expression(stmt
->expression
);
747 static void expand_if_statement(struct statement
*stmt
)
749 struct expression
*expr
= stmt
->if_conditional
;
751 if (!expr
|| !expr
->ctype
)
754 expand_expression(expr
);
756 /* This is only valid if nobody jumps into the "dead" side */
758 /* Simplify constant conditionals without even evaluating the false side */
759 if (expr
->type
== EXPR_VALUE
) {
760 struct statement
*simple
;
761 simple
= expr
->value
? stmt
->if_true
: stmt
->if_false
;
765 stmt
->type
= STMT_NONE
;
768 expand_statement(simple
);
773 expand_statement(stmt
->if_true
);
774 expand_statement(stmt
->if_false
);
777 static void expand_statement(struct statement
*stmt
)
782 switch (stmt
->type
) {
784 expand_return_expression(stmt
);
787 case STMT_EXPRESSION
:
788 expand_expression(stmt
->expression
);
791 case STMT_COMPOUND
: {
795 FOR_EACH_PTR(stmt
->syms
, sym
) {
798 expand_symbol(stmt
->ret
);
800 FOR_EACH_PTR(stmt
->stmts
, s
) {
807 expand_if_statement(stmt
);
811 expand_expression(stmt
->iterator_pre_condition
);
812 expand_expression(stmt
->iterator_post_condition
);
813 expand_statement(stmt
->iterator_pre_statement
);
814 expand_statement(stmt
->iterator_statement
);
815 expand_statement(stmt
->iterator_post_statement
);
819 expand_expression(stmt
->switch_expression
);
820 expand_statement(stmt
->switch_statement
);
824 expand_const_expression(stmt
->case_expression
, "case statement");
825 expand_const_expression(stmt
->case_to
, "case statement");
826 expand_statement(stmt
->case_statement
);
830 expand_statement(stmt
->label_statement
);
834 expand_expression(stmt
->goto_expression
);
840 /* FIXME! Do the asm parameter evaluation! */
845 long long get_expression_value(struct expression
*expr
)
847 long long value
, mask
;
848 struct symbol
*ctype
;
852 ctype
= evaluate_expression(expr
);
854 warn(expr
->pos
, "bad constant expression type");
857 expand_expression(expr
);
858 if (expr
->type
!= EXPR_VALUE
) {
859 warn(expr
->pos
, "bad constant expression");
864 mask
= 1ULL << (ctype
->bit_size
-1);
867 while (ctype
->type
!= SYM_BASETYPE
)
868 ctype
= ctype
->ctype
.base_type
;
869 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
870 value
= value
| mask
| ~(mask
-1);