4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Evaluate constant expressions.
22 #include "expression.h"
24 static int current_context
, current_contextmask
;
26 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
28 struct symbol
*sym
= expr
->symbol
;
29 struct symbol
*base_type
;
32 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
36 examine_symbol_type(sym
);
37 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
38 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
40 base_type
= sym
->ctype
.base_type
;
42 warn(sym
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
46 /* The type of a symbol is the symbol itself! */
49 /* enum's can be turned into plain values */
50 if (base_type
->type
!= SYM_ENUM
) {
51 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
53 addr
->symbol_name
= expr
->symbol_name
;
54 addr
->ctype
= &ptr_ctype
;
55 expr
->type
= EXPR_PREOP
;
60 expr
->type
= EXPR_VALUE
;
61 expr
->value
= sym
->value
;
65 static struct symbol
*evaluate_string(struct expression
*expr
)
67 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
68 int length
= expr
->string
->length
;
70 sym
->array_size
= length
;
71 sym
->bit_size
= BITS_IN_CHAR
* length
;
72 sym
->ctype
.alignment
= 1;
73 sym
->ctype
.modifiers
= MOD_CONST
;
74 sym
->ctype
.base_type
= &char_ctype
;
79 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
81 unsigned long lmod
, rmod
, mod
;
86 if (left
->bit_size
> right
->bit_size
)
89 if (right
->bit_size
> left
->bit_size
)
92 /* Same size integers - promote to unsigned, promote to long */
93 lmod
= left
->ctype
.modifiers
;
94 rmod
= right
->ctype
.modifiers
;
100 return ctype_integer(mod
);
103 static struct symbol
* cast_value(struct expression
*expr
, struct symbol
*newtype
,
104 struct expression
*old
, struct symbol
*oldtype
)
106 int old_size
= oldtype
->bit_size
;
107 int new_size
= newtype
->bit_size
;
108 long long value
, mask
, ormask
, andmask
;
111 // FIXME! We don't handle FP casts of constant values yet
112 if (newtype
->ctype
.base_type
== &fp_type
)
114 if (oldtype
->ctype
.base_type
== &fp_type
)
117 // For pointers and integers, we can just move the value around
118 expr
->type
= EXPR_VALUE
;
119 if (old_size
== new_size
) {
120 expr
->value
= old
->value
;
124 // expand it to the full "long long" value
125 is_signed
= !(oldtype
->ctype
.modifiers
& MOD_UNSIGNED
);
126 mask
= 1ULL << (old_size
-1);
130 andmask
= mask
| (mask
-1);
134 value
= (value
& andmask
) | ormask
;
136 // Truncate it to the new size
137 mask
= 1ULL << (new_size
-1);
138 mask
= mask
| (mask
-1);
139 expr
->value
= value
& mask
;
143 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
145 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
147 expr
->cast_type
= type
;
148 expr
->cast_expression
= old
;
149 if (old
->type
== EXPR_VALUE
)
150 cast_value(expr
, type
, old
, old
->ctype
);
154 static int is_ptr_type(struct symbol
*type
)
156 if (type
->type
== SYM_NODE
)
157 type
= type
->ctype
.base_type
;
158 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
161 static int is_int_type(struct symbol
*type
)
163 if (type
->type
== SYM_NODE
)
164 type
= type
->ctype
.base_type
;
165 return type
->ctype
.base_type
== &int_type
;
168 static struct symbol
*bad_expr_type(struct expression
*expr
)
170 warn(expr
->pos
, "incompatible types for operation");
174 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
176 struct expression
*left
= *lp
, *right
= *rp
;
177 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
179 if (ltype
->type
== SYM_NODE
)
180 ltype
= ltype
->ctype
.base_type
;
181 if (rtype
->type
== SYM_NODE
)
182 rtype
= rtype
->ctype
.base_type
;
183 /* Integer promotion? */
184 if (ltype
->type
== SYM_ENUM
)
186 if (rtype
->type
== SYM_ENUM
)
188 if (is_int_type(ltype
) && is_int_type(rtype
)) {
189 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
191 /* Don't bother promoting same-size entities, it only adds clutter */
192 if (ltype
->bit_size
!= ctype
->bit_size
)
193 *lp
= cast_to(left
, ctype
);
194 if (rtype
->bit_size
!= ctype
->bit_size
)
195 *rp
= cast_to(right
, ctype
);
201 static int check_shift_count(struct expression
*expr
, struct symbol
*ctype
, unsigned int count
)
203 if (count
>= ctype
->bit_size
) {
204 warn(expr
->pos
, "shift too big for type");
205 count
&= ctype
->bit_size
-1;
211 * CAREFUL! We need to get the size and sign of the
214 static void simplify_int_binop(struct expression
*expr
, struct symbol
*ctype
)
216 struct expression
*left
= expr
->left
, *right
= expr
->right
;
217 unsigned long long v
, l
, r
, mask
;
218 signed long long s
, sl
, sr
;
219 int is_signed
, shift
;
221 if (left
->type
!= EXPR_VALUE
|| right
->type
!= EXPR_VALUE
)
223 l
= left
->value
; r
= right
->value
;
224 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
225 mask
= 1ULL << (ctype
->bit_size
-1);
227 if (is_signed
&& (sl
& mask
))
229 if (is_signed
&& (sr
& mask
))
233 case '+': v
= l
+ r
; s
= v
; break;
234 case '-': v
= l
- r
; s
= v
; break;
235 case '&': v
= l
& r
; s
= v
; break;
236 case '|': v
= l
| r
; s
= v
; break;
237 case '^': v
= l
^ r
; s
= v
; break;
238 case '*': v
= l
* r
; s
= sl
* sr
; break;
239 case '/': if (!r
) return; v
= l
/ r
; s
= sl
/ sr
; break;
240 case '%': if (!r
) return; v
= l
% r
; s
= sl
% sr
; break;
241 case SPECIAL_LEFTSHIFT
: shift
= check_shift_count(expr
, ctype
, r
); v
= l
<< shift
; s
= v
; break;
242 case SPECIAL_RIGHTSHIFT
:shift
= check_shift_count(expr
, ctype
, r
); v
= l
>> shift
; s
= sl
>> shift
; break;
243 case '<': v
= l
< r
; s
= sl
< sr
; break;
244 case '>': v
= l
> r
; s
= sl
> sr
; break;
245 case SPECIAL_LTE
: v
= l
<= r
; s
= sl
<= sr
; break;
246 case SPECIAL_GTE
: v
= l
>= r
; s
= sl
>= sr
; break;
247 case SPECIAL_EQUAL
: v
= l
== r
; s
= v
; break;
248 case SPECIAL_NOTEQUAL
: v
= l
!= r
; s
= v
; break;
253 mask
= mask
| (mask
-1);
254 expr
->value
= v
& mask
;
255 expr
->type
= EXPR_VALUE
;
258 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
260 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
263 simplify_int_binop(expr
, ctype
);
266 return bad_expr_type(expr
);
269 static inline int lvalue_expression(struct expression
*expr
)
271 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
275 /* Arrays degenerate into pointers on pointer arithmetic */
276 static struct symbol
*degenerate(struct expression
*expr
, struct symbol
*ctype
, struct expression
**ptr_p
)
278 if (ctype
->type
== SYM_ARRAY
) {
279 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
280 struct expression
*ptr
;
282 sym
->ctype
= ctype
->ctype
;
283 sym
->bit_size
= BITS_IN_POINTER
;
284 sym
->ctype
.alignment
= POINTER_ALIGNMENT
;
290 * This all really assumes that we got the degenerate
291 * array as an lvalue (ie a dereference). If that
292 * is not the case, then holler - because we've screwed
295 if (!lvalue_expression(ptr
))
296 warn(ptr
->pos
, "internal error: strange degenerate array case");
301 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
303 struct symbol
*ctype
;
304 struct symbol
*ptr_type
= ptr
->ctype
;
305 struct symbol
*i_type
= i
->ctype
;
308 if (i_type
->type
== SYM_NODE
)
309 i_type
= i_type
->ctype
.base_type
;
310 if (ptr_type
->type
== SYM_NODE
)
311 ptr_type
= ptr_type
->ctype
.base_type
;
313 if (i_type
->type
== SYM_ENUM
)
315 if (!is_int_type(i_type
))
316 return bad_expr_type(expr
);
318 ctype
= ptr_type
->ctype
.base_type
;
319 examine_symbol_type(ctype
);
321 bit_size
= ctype
->bit_size
;
322 ctype
= degenerate(expr
, ptr_type
, &ptr
);
324 /* Special case: adding zero commonly happens as a result of 'array[0]' */
325 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
327 } else if (bit_size
> BITS_IN_CHAR
) {
328 struct expression
*add
= expr
;
329 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
330 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
332 val
->ctype
= size_t_ctype
;
333 val
->value
= bit_size
>> 3;
336 mul
->ctype
= size_t_ctype
;
339 simplify_int_binop(mul
, size_t_ctype
);
341 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
344 simplify_int_binop(add
, add
->ctype
);
351 static struct symbol
*evaluate_add(struct expression
*expr
)
353 struct expression
*left
= expr
->left
, *right
= expr
->right
;
354 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
356 if (is_ptr_type(ltype
))
357 return evaluate_ptr_add(expr
, left
, right
);
359 if (is_ptr_type(rtype
))
360 return evaluate_ptr_add(expr
, right
, left
);
362 // FIXME! FP promotion
363 return evaluate_int_binop(expr
);
366 static int same_type(struct symbol
*target
, struct symbol
*source
)
368 int dropped_modifiers
= 0;
370 unsigned long mod1
, mod2
;
371 unsigned long as1
, as2
;
373 if (target
== source
)
375 if (!target
|| !source
)
378 * Peel of per-node information.
379 * FIXME! Check alignment, address space, and context too here!
381 mod1
= target
->ctype
.modifiers
;
382 as1
= target
->ctype
.as
;
383 mod2
= source
->ctype
.modifiers
;
384 as2
= source
->ctype
.as
;
385 if (target
->type
== SYM_NODE
) {
386 target
= target
->ctype
.base_type
;
387 mod1
|= target
->ctype
.modifiers
;
388 as1
|= target
->ctype
.as
;
390 if (source
->type
== SYM_NODE
) {
391 source
= source
->ctype
.base_type
;
392 mod2
|= source
->ctype
.modifiers
;
393 as2
|= source
->ctype
.as
;
396 /* Ignore differences in storage types */
397 if ((mod1
^ mod2
) & ~MOD_STORAGE
)
400 /* Must be same address space to be comparable */
404 if (target
->type
!= source
->type
) {
405 int type1
= target
->type
;
406 int type2
= source
->type
;
408 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
409 type1
= type1
== SYM_ARRAY
? SYM_PTR
: type1
;
410 type2
= type2
== SYM_ARRAY
? SYM_PTR
: type2
;
415 target
= target
->ctype
.base_type
;
416 source
= source
->ctype
.base_type
;
418 if (dropped_modifiers
)
419 warn(target
->pos
, "assignment drops modifiers");
423 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
425 /* NULL expression? Just return the type of the "other side" */
426 if (r
->type
== EXPR_VALUE
&& !r
->value
)
428 if (l
->type
== EXPR_VALUE
&& !l
->value
)
433 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
435 struct symbol
*ctype
;
436 struct symbol
*ltype
= l
->ctype
, *rtype
= r
->ctype
;
439 * If it is an integer subtract: the ptr add case will do the
442 if (!is_ptr_type(rtype
))
443 return evaluate_ptr_add(expr
, l
, r
);
446 if (!same_type(ltype
, rtype
)) {
447 ctype
= common_ptr_type(l
, r
);
449 warn(expr
->pos
, "subtraction of different types can't work");
453 examine_symbol_type(ctype
);
455 /* Figure out the base type we point to */
456 if (ctype
->type
== SYM_NODE
)
457 ctype
= ctype
->ctype
.base_type
;
458 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
459 warn(expr
->pos
, "subtraction of functions? Share your drugs");
462 ctype
= ctype
->ctype
.base_type
;
464 expr
->ctype
= ssize_t_ctype
;
465 if (ctype
->bit_size
> BITS_IN_CHAR
) {
466 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
467 struct expression
*div
= expr
;
468 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
470 val
->ctype
= size_t_ctype
;
471 val
->value
= ctype
->bit_size
>> 3;
474 sub
->ctype
= ssize_t_ctype
;
483 return ssize_t_ctype
;
486 static struct symbol
*evaluate_sub(struct expression
*expr
)
488 struct expression
*left
= expr
->left
, *right
= expr
->right
;
489 struct symbol
*ltype
= left
->ctype
;
491 if (is_ptr_type(ltype
))
492 return evaluate_ptr_sub(expr
, left
, right
);
494 // FIXME! FP promotion
495 return evaluate_int_binop(expr
);
498 static struct symbol
*evaluate_logical(struct expression
*expr
)
500 struct expression
*left
= expr
->left
;
501 struct expression
*right
;
503 if (!evaluate_expression(left
))
506 /* Do immediate short-circuiting ... */
507 if (left
->type
== EXPR_VALUE
) {
508 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
510 expr
->type
= EXPR_VALUE
;
512 expr
->ctype
= &bool_ctype
;
517 expr
->type
= EXPR_VALUE
;
519 expr
->ctype
= &bool_ctype
;
526 if (!evaluate_expression(right
))
528 expr
->ctype
= &bool_ctype
;
529 if (left
->type
== EXPR_VALUE
&& right
->type
== EXPR_VALUE
) {
531 * We know the left value doesn't matter, since
532 * otherwise we would have short-circuited it..
534 expr
->type
= EXPR_VALUE
;
535 expr
->value
= right
->value
;
541 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
543 // FIXME! Floating-point promotion!
544 return evaluate_int_binop(expr
);
547 static struct symbol
*evaluate_binop(struct expression
*expr
)
550 // addition can take ptr+int, fp and int
552 return evaluate_add(expr
);
554 // subtraction can take ptr-ptr, fp and int
556 return evaluate_sub(expr
);
558 // Arithmetic operations can take fp and int
559 case '*': case '/': case '%':
560 return evaluate_arithmetic(expr
);
562 // The rest are integer operations (bitops)
563 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
566 return evaluate_int_binop(expr
);
570 static struct symbol
*evaluate_comma(struct expression
*expr
)
572 expr
->ctype
= expr
->right
->ctype
;
576 static struct symbol
*evaluate_compare(struct expression
*expr
)
578 struct expression
*left
= expr
->left
, *right
= expr
->right
;
579 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
580 struct symbol
*ctype
;
583 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
584 expr
->ctype
= &bool_ctype
;
585 // FIXME! Check the types for compatibility
589 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
591 simplify_int_binop(expr
, ctype
);
592 expr
->ctype
= &bool_ctype
;
596 return bad_expr_type(expr
);
599 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
601 /* Integer promotion? */
602 if (ltype
->type
== SYM_ENUM
)
604 if (rtype
->type
== SYM_ENUM
)
606 return (is_int_type(ltype
) && is_int_type(rtype
));
609 static int is_void_ptr(struct expression
*expr
)
611 return (expr
->type
== EXPR_VALUE
&&
616 * FIXME!! This shoul ddo casts, array degeneration etc..
618 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
620 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
622 if (ltype
->type
== SYM_PTR
) {
623 if (is_void_ptr(right
))
627 if (rtype
->type
== SYM_PTR
) {
628 if (is_void_ptr(left
))
634 static struct symbol
* evaluate_conditional(struct expression
*expr
)
636 struct expression
*cond
, *true, *false;
637 struct symbol
*ctype
, *ltype
, *rtype
;
639 cond
= expr
->conditional
;
640 true = expr
->cond_true
? : cond
;
641 false = expr
->cond_false
;
644 rtype
= false->ctype
;
647 if (!same_type(ltype
, rtype
)) {
648 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
650 ctype
= compatible_ptr_type(true, expr
->cond_false
);
652 warn(expr
->pos
, "incompatible types in conditional expression");
658 /* Simplify conditional expression.. */
659 if (cond
->type
== EXPR_VALUE
) {
668 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
669 struct expression
**rp
, struct symbol
*source
)
671 if (same_type(target
, source
))
674 if (compatible_integer_types(target
, source
)) {
675 if (target
->bit_size
!= source
->bit_size
)
676 *rp
= cast_to(*rp
, target
);
680 /* Pointer destination? */
681 if (target
->type
== SYM_NODE
)
682 target
= target
->ctype
.base_type
;
683 if (source
->type
== SYM_NODE
)
684 source
= source
->ctype
.base_type
;
685 if (target
->type
== SYM_PTR
) {
686 struct expression
*right
= *rp
;
687 struct symbol
*source_base
= source
->ctype
.base_type
;
688 struct symbol
*target_base
= target
->ctype
.base_type
;
690 if (source
->type
== SYM_NODE
) {
691 source
= source_base
;
692 source_base
= source
->ctype
.base_type
;
694 if (target
->type
== SYM_NODE
) {
695 target
= target_base
;
696 target_base
= target
->ctype
.base_type
;
698 if (source
->type
== SYM_ARRAY
&& same_type(target_base
, source_base
))
700 if (source
->type
== SYM_FN
&& same_type(target_base
, source
))
704 if (right
->type
== EXPR_VALUE
&& !right
->value
)
708 if (target
->type
== SYM_PTR
) {
709 struct symbol
*source_base
= source
->ctype
.base_type
;
710 struct symbol
*target_base
= target
->ctype
.base_type
;
711 if (source_base
== &void_ctype
|| target_base
== &void_ctype
)
713 warn(expr
->pos
, "assignment from incompatible pointer types");
718 warn(expr
->pos
, "assignment from different types");
723 warn(expr
->pos
, "assignment from bad type");
728 * FIXME!! This is wrong from a double evaluation standpoint. We can't
729 * just expand the expression twice, that would make any side effects
732 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
735 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
736 static const int op_trans
[] = {
737 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
738 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
739 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
740 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
741 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
742 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
743 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
744 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
745 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '&',
746 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
749 subexpr
->left
= left
;
750 subexpr
->right
= right
;
751 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
753 expr
->right
= subexpr
;
754 return evaluate_binop(subexpr
);
757 static struct symbol
*evaluate_assignment(struct expression
*expr
)
759 struct expression
*left
= expr
->left
, *right
= expr
->right
;
760 struct symbol
*ltype
, *rtype
;
763 rtype
= right
->ctype
;
764 if (expr
->op
!= '=') {
765 rtype
= evaluate_binop_assignment(expr
, left
, right
);
771 if (!lvalue_expression(left
)) {
772 warn(expr
->pos
, "not an lvalue");
776 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
))
779 expr
->ctype
= expr
->left
->ctype
;
783 static struct symbol
*evaluate_addressof(struct expression
*expr
)
785 struct symbol
*ctype
, *symbol
;
786 struct expression
*op
= expr
->unop
;
788 if (!lvalue_expression(op
)) {
789 warn(expr
->pos
, "not an lvalue");
793 symbol
= alloc_symbol(expr
->pos
, SYM_PTR
);
794 symbol
->ctype
.base_type
= ctype
;
795 symbol
->ctype
.alignment
= POINTER_ALIGNMENT
;
796 symbol
->bit_size
= BITS_IN_POINTER
;
799 expr
->ctype
= symbol
;
804 static struct symbol
*evaluate_dereference(struct expression
*expr
)
806 struct expression
*op
= expr
->unop
;
807 struct symbol
*ctype
= op
->ctype
;
810 mod
= ctype
->ctype
.modifiers
;
811 if (ctype
->type
== SYM_NODE
) {
812 ctype
= ctype
->ctype
.base_type
;
813 mod
|= ctype
->ctype
.modifiers
;
815 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
816 warn(expr
->pos
, "cannot derefence this type");
820 if (mod
& MOD_NODEREF
)
821 warn(expr
->pos
, "bad dereference");
822 ctype
= ctype
->ctype
.base_type
;
824 warn(expr
->pos
, "undefined type");
828 examine_symbol_type(ctype
);
830 /* Simplify: *&(expr) => (expr) */
831 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
839 static void simplify_preop(struct expression
*expr
)
841 struct expression
*op
= expr
->unop
;
842 unsigned long long v
, mask
;
844 if (op
->type
!= EXPR_VALUE
)
849 case '-': v
= -v
; break;
850 case '!': v
= !v
; break;
851 case '~': v
= ~v
; break;
854 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
855 mask
= mask
| (mask
-1);
856 expr
->value
= v
& mask
;
857 expr
->type
= EXPR_VALUE
;
861 * Unary post-ops: x++ and x--
863 static struct symbol
*evaluate_postop(struct expression
*expr
)
865 struct expression
*op
= expr
->unop
;
866 struct symbol
*ctype
= op
->ctype
;
868 if (!lvalue_expression(expr
->unop
)) {
869 warn(expr
->pos
, "need lvalue expression for ++/--");
876 static struct symbol
*evaluate_preop(struct expression
*expr
)
878 struct symbol
*ctype
= expr
->unop
->ctype
;
886 return evaluate_dereference(expr
);
889 return evaluate_addressof(expr
);
892 expr
->ctype
= &bool_ctype
;
893 simplify_preop(expr
);
896 case SPECIAL_INCREMENT
:
897 case SPECIAL_DECREMENT
:
899 * From a type evaluation standpoint the pre-ops are
900 * the same as the postops
902 return evaluate_postop(expr
);
906 simplify_preop(expr
);
911 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
913 struct ptr_list
*head
= (struct ptr_list
*)_list
;
914 struct ptr_list
*list
= head
;
920 for (i
= 0; i
< list
->nr
; i
++) {
921 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
923 if (sym
->ident
!= ident
)
925 *offset
= sym
->offset
;
928 struct symbol
*ctype
= sym
->ctype
.base_type
;
932 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
934 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
937 *offset
+= sym
->offset
;
941 } while ((list
= list
->next
) != head
);
945 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
947 struct expression
*add
;
952 /* Create a new add-expression */
953 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
955 add
->ctype
= &ptr_ctype
;
957 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
958 add
->right
->ctype
= &int_ctype
;
959 add
->right
->value
= offset
;
961 simplify_int_binop(add
, &ptr_ctype
);
965 /* structure/union dereference */
966 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
969 struct symbol
*ctype
, *member
;
970 struct expression
*deref
= expr
->deref
, *add
;
971 struct ident
*ident
= expr
->member
;
974 if (!evaluate_expression(deref
))
977 warn(expr
->pos
, "bad member name");
981 ctype
= deref
->ctype
;
982 mod
= ctype
->ctype
.modifiers
;
983 if (ctype
->type
== SYM_NODE
) {
984 ctype
= ctype
->ctype
.base_type
;
985 mod
|= ctype
->ctype
.modifiers
;
987 if (expr
->op
== SPECIAL_DEREFERENCE
) {
988 /* Arrays will degenerate into pointers for '->' */
989 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
990 warn(expr
->pos
, "expected a pointer to a struct/union");
993 ctype
= ctype
->ctype
.base_type
;
994 mod
|= ctype
->ctype
.modifiers
;
995 if (ctype
->type
== SYM_NODE
) {
996 ctype
= ctype
->ctype
.base_type
;
997 mod
|= ctype
->ctype
.modifiers
;
1000 if (!lvalue_expression(deref
)) {
1001 warn(deref
->pos
, "expected lvalue for member dereference");
1004 deref
= deref
->unop
;
1005 expr
->deref
= deref
;
1007 if (mod
& MOD_NODEREF
)
1008 warn(expr
->pos
, "bad dereference");
1009 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1010 warn(expr
->pos
, "expected structure or union");
1014 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1016 warn(expr
->pos
, "no such struct/union member");
1020 add
= evaluate_offset(deref
, offset
);
1022 ctype
= member
->ctype
.base_type
;
1023 if (ctype
->type
== SYM_BITFIELD
) {
1024 ctype
= ctype
->ctype
.base_type
;
1025 expr
->type
= EXPR_BITFIELD
;
1026 expr
->bitpos
= member
->bit_offset
;
1027 expr
->nrbits
= member
->fieldwidth
;
1028 expr
->address
= add
;
1030 expr
->type
= EXPR_PREOP
;
1035 expr
->ctype
= ctype
;
1039 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1043 if (expr
->cast_type
) {
1044 examine_symbol_type(expr
->cast_type
);
1045 size
= expr
->cast_type
->bit_size
;
1047 if (!evaluate_expression(expr
->cast_expression
))
1049 size
= expr
->cast_expression
->ctype
->bit_size
;
1052 warn(expr
->pos
, "cannot size expression");
1055 expr
->type
= EXPR_VALUE
;
1056 expr
->value
= size
>> 3;
1057 expr
->ctype
= size_t_ctype
;
1058 return size_t_ctype
;
1061 static int evaluate_expression_list(struct expression_list
*head
)
1064 struct ptr_list
*list
= (struct ptr_list
*)head
;
1067 for (i
= 0; i
< list
->nr
; i
++) {
1068 struct expression
*expr
= (struct expression
*)list
->list
[i
];
1069 evaluate_expression(expr
);
1071 } while ((list
= list
->next
) != (struct ptr_list
*)head
);
1078 * FIXME! This is bogus: we need to take array index
1079 * entries into account when calculating the size of
1082 static int count_array_initializer(struct expression
*expr
)
1084 struct expression_list
*list
;
1086 if (expr
->type
!= EXPR_INITIALIZER
)
1088 list
= expr
->expr_list
;
1089 return expression_list_size(list
);
1092 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
)
1094 return count_array_initializer(expr
);
1097 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
)
1099 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1104 * Initializers are kind of like assignments. Except
1105 * they can be a hell of a lot more complex.
1107 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
1109 struct expression
*expr
= *ep
;
1112 * Simple non-structure/array initializers are the simple
1113 * case, and look (and parse) largely like assignments.
1115 if (expr
->type
!= EXPR_INITIALIZER
) {
1117 struct symbol
*rtype
= evaluate_expression(expr
);
1119 compatible_assignment_types(expr
, ctype
, ep
, rtype
);
1120 /* strings are special: char arrays */
1121 if (rtype
->type
== SYM_ARRAY
)
1122 size
= rtype
->array_size
;
1127 expr
->ctype
= ctype
;
1128 switch (ctype
->type
) {
1131 return evaluate_array_initializer(ctype
, expr
);
1133 return evaluate_struct_or_union_initializer(ctype
, expr
, 0);
1135 return evaluate_struct_or_union_initializer(ctype
, expr
, 1);
1139 warn(expr
->pos
, "unexpected compound initializer");
1143 static struct symbol
*evaluate_cast(struct expression
*expr
)
1145 struct expression
*target
= expr
->cast_expression
;
1146 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1148 expr
->ctype
= ctype
;
1149 expr
->cast_type
= ctype
;
1152 * Special case: a cast can be followed by an
1153 * initializer, in which case we need to pass
1154 * the type value down to that initializer rather
1155 * than trying to evaluate it as an expression
1157 if (target
->type
== EXPR_INITIALIZER
) {
1158 evaluate_initializer(ctype
, &expr
->cast_expression
);
1162 evaluate_expression(target
);
1164 /* Simplify normal integer casts.. */
1165 if (target
->type
== EXPR_VALUE
)
1166 cast_value(expr
, ctype
, target
, target
->ctype
);
1170 static struct symbol
*evaluate_call(struct expression
*expr
)
1173 struct symbol
*ctype
;
1174 struct expression
*fn
= expr
->fn
;
1175 struct expression_list
*arglist
= expr
->args
;
1177 if (!evaluate_expression(fn
))
1179 if (!evaluate_expression_list(arglist
))
1182 if (ctype
->type
== SYM_NODE
)
1183 ctype
= ctype
->ctype
.base_type
;
1184 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1185 ctype
= ctype
->ctype
.base_type
;
1186 if (ctype
->type
!= SYM_FN
) {
1187 warn(expr
->pos
, "not a function");
1190 args
= expression_list_size(expr
->args
);
1191 fnargs
= symbol_list_size(ctype
->arguments
);
1193 warn(expr
->pos
, "not enough arguments for function");
1194 if (args
> fnargs
&& !ctype
->variadic
)
1195 warn(expr
->pos
, "too many arguments for function");
1196 expr
->ctype
= ctype
->ctype
.base_type
;
1200 struct symbol
*evaluate_expression(struct expression
*expr
)
1207 switch (expr
->type
) {
1209 warn(expr
->pos
, "value expression without a type");
1212 return evaluate_string(expr
);
1214 return evaluate_symbol_expression(expr
);
1216 if (!evaluate_expression(expr
->left
))
1218 if (!evaluate_expression(expr
->right
))
1220 return evaluate_binop(expr
);
1222 return evaluate_logical(expr
);
1224 if (!evaluate_expression(expr
->left
))
1226 if (!evaluate_expression(expr
->right
))
1228 return evaluate_comma(expr
);
1230 if (!evaluate_expression(expr
->left
))
1232 if (!evaluate_expression(expr
->right
))
1234 return evaluate_compare(expr
);
1235 case EXPR_ASSIGNMENT
:
1236 if (!evaluate_expression(expr
->left
))
1238 if (!evaluate_expression(expr
->right
))
1240 return evaluate_assignment(expr
);
1242 if (!evaluate_expression(expr
->unop
))
1244 return evaluate_preop(expr
);
1246 if (!evaluate_expression(expr
->unop
))
1248 return evaluate_postop(expr
);
1250 return evaluate_cast(expr
);
1252 return evaluate_sizeof(expr
);
1254 return evaluate_member_dereference(expr
);
1256 return evaluate_call(expr
);
1258 warn(expr
->pos
, "bitfield generated by parser");
1260 case EXPR_CONDITIONAL
:
1261 if (!evaluate_expression(expr
->conditional
))
1263 if (!evaluate_expression(expr
->cond_false
))
1265 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1267 return evaluate_conditional(expr
);
1268 case EXPR_STATEMENT
:
1269 expr
->ctype
= evaluate_statement(expr
->statement
);
1272 /* These can not exist as stand-alone expressions */
1273 case EXPR_INITIALIZER
:
1274 case EXPR_IDENTIFIER
:
1276 warn(expr
->pos
, "internal front-end error: initializer in expression");
1282 static void evaluate_one_statement(struct statement
*stmt
, void *_last
, int flags
)
1284 struct symbol
**last
= _last
;
1285 struct symbol
*type
= evaluate_statement(stmt
);
1287 if (flags
& ITERATE_LAST
)
1291 static void evaluate_one_symbol(struct symbol
*sym
, void *unused
, int flags
)
1293 evaluate_symbol(sym
);
1296 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1298 struct symbol
*base_type
;
1300 examine_symbol_type(sym
);
1301 base_type
= sym
->ctype
.base_type
;
1304 sym
->ctype
.base_type
= base_type
;
1306 /* Evaluate the initializers */
1307 if (sym
->initializer
) {
1308 int count
= evaluate_initializer(base_type
, &sym
->initializer
);
1309 if (base_type
->type
== SYM_ARRAY
&& base_type
->array_size
< 0) {
1310 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1311 base_type
->array_size
= count
;
1312 base_type
->bit_size
= bit_size
;
1313 sym
->array_size
= count
;
1314 sym
->bit_size
= bit_size
;
1318 /* And finally, evaluate the body of the symbol too */
1319 if (base_type
->type
== SYM_FN
) {
1320 symbol_iterate(base_type
->arguments
, evaluate_one_symbol
, NULL
);
1321 if (base_type
->stmt
) {
1322 current_contextmask
= sym
->ctype
.contextmask
;
1323 current_context
= sym
->ctype
.context
;
1324 evaluate_statement(base_type
->stmt
);
1331 struct symbol
*evaluate_statement(struct statement
*stmt
)
1336 switch (stmt
->type
) {
1338 case STMT_EXPRESSION
:
1339 return evaluate_expression(stmt
->expression
);
1341 case STMT_COMPOUND
: {
1342 struct symbol
*type
= NULL
;
1343 symbol_iterate(stmt
->syms
, evaluate_one_symbol
, NULL
);
1344 statement_iterate(stmt
->stmts
, evaluate_one_statement
, &type
);
1348 evaluate_expression(stmt
->if_conditional
);
1349 evaluate_statement(stmt
->if_true
);
1350 evaluate_statement(stmt
->if_false
);
1353 evaluate_expression(stmt
->iterator_pre_condition
);
1354 evaluate_expression(stmt
->iterator_post_condition
);
1355 evaluate_statement(stmt
->iterator_pre_statement
);
1356 evaluate_statement(stmt
->iterator_statement
);
1357 evaluate_statement(stmt
->iterator_post_statement
);
1360 evaluate_expression(stmt
->switch_expression
);
1361 evaluate_statement(stmt
->switch_statement
);
1364 evaluate_expression(stmt
->case_expression
);
1365 evaluate_expression(stmt
->case_to
);
1366 evaluate_statement(stmt
->case_statement
);
1369 evaluate_statement(stmt
->label_statement
);
1372 evaluate_expression(stmt
->goto_expression
);
1379 /* FIXME! Do the asm parameter evaluation! */
1385 long long get_expression_value(struct expression
*expr
)
1387 long long value
, mask
;
1388 struct symbol
*ctype
;
1390 ctype
= evaluate_expression(expr
);
1391 if (!ctype
|| expr
->type
!= EXPR_VALUE
) {
1392 warn(expr
->pos
, "bad constant expression");
1393 show_expression(expr
);
1394 printf(": badness\n");
1398 value
= expr
->value
;
1399 mask
= 1ULL << (ctype
->bit_size
-1);
1402 while (ctype
->type
!= SYM_BASETYPE
)
1403 ctype
= ctype
->ctype
.base_type
;
1404 if (!(ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
1405 value
= value
| mask
| ~(mask
-1);