4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
26 #include "expression.h"
28 static struct symbol
*current_fn
;
29 static int current_context
, current_contextmask
;
31 static struct symbol
*degenerate(struct expression
*expr
);
33 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
35 struct symbol
*sym
= expr
->symbol
;
36 struct symbol
*base_type
;
40 expr
->ctype
= &int_ctype
;
43 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
47 examine_symbol_type(sym
);
48 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
49 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
51 base_type
= sym
->ctype
.base_type
;
53 warn(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
57 /* The type of a symbol is the symbol itself! */
60 /* enum's can be turned into plain values */
61 if (sym
->type
!= SYM_ENUM
) {
62 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
64 addr
->symbol_name
= expr
->symbol_name
;
65 addr
->ctype
= NULL
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr
->type
= EXPR_PREOP
;
71 expr
->type
= EXPR_VALUE
;
72 expr
->value
= sym
->value
;
73 expr
->ctype
= base_type
;
77 static struct symbol
*evaluate_string(struct expression
*expr
)
79 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
80 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
81 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
82 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
83 unsigned int length
= expr
->string
->length
;
85 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
86 sym
->bit_size
= bits_in_char
* length
;
87 sym
->ctype
.alignment
= 1;
88 sym
->ctype
.modifiers
= MOD_STATIC
;
89 sym
->ctype
.base_type
= array
;
90 sym
->initializer
= initstr
;
93 initstr
->string
= expr
->string
;
95 array
->array_size
= sym
->array_size
;
96 array
->bit_size
= bits_in_char
* length
;
97 array
->ctype
.alignment
= 1;
98 array
->ctype
.modifiers
= MOD_STATIC
;
99 array
->ctype
.base_type
= &char_ctype
;
104 expr
->type
= EXPR_PREOP
;
111 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
113 unsigned long lmod
, rmod
, mod
;
118 if (left
->bit_size
> right
->bit_size
)
121 if (right
->bit_size
> left
->bit_size
)
124 /* Same size integers - promote to unsigned, promote to long */
125 lmod
= left
->ctype
.modifiers
;
126 rmod
= right
->ctype
.modifiers
;
132 return ctype_integer(mod
);
137 if (left
->bit_size
< bits_in_int
)
142 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
144 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
146 expr
->cast_type
= type
;
147 expr
->cast_expression
= old
;
151 static int is_type_type(struct symbol
*type
)
153 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
156 static int is_ptr_type(struct symbol
*type
)
158 if (type
->type
== SYM_NODE
)
159 type
= type
->ctype
.base_type
;
160 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
163 static int is_int_type(struct symbol
*type
)
165 if (type
->type
== SYM_NODE
)
166 type
= type
->ctype
.base_type
;
167 return (type
->type
== SYM_BITFIELD
) || type
->ctype
.base_type
== &int_type
;
170 static struct symbol
*bad_expr_type(struct expression
*expr
)
172 warn(expr
->pos
, "incompatible types for operation");
176 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
178 struct expression
*left
= *lp
, *right
= *rp
;
179 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
181 if (ltype
->type
== SYM_NODE
)
182 ltype
= ltype
->ctype
.base_type
;
183 if (rtype
->type
== SYM_NODE
)
184 rtype
= rtype
->ctype
.base_type
;
185 /* Integer promotion? */
186 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
188 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
190 if (is_int_type(ltype
) && is_int_type(rtype
)) {
191 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
193 /* Don't bother promoting same-size entities, it only adds clutter */
194 if (ltype
->bit_size
!= ctype
->bit_size
)
195 *lp
= cast_to(left
, ctype
);
196 if (rtype
->bit_size
!= ctype
->bit_size
)
197 *rp
= cast_to(right
, ctype
);
203 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
205 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
210 return bad_expr_type(expr
);
213 static inline int lvalue_expression(struct expression
*expr
)
215 while (expr
->type
== EXPR_CAST
)
216 expr
= expr
->cast_expression
;
217 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
220 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
222 struct symbol
*ctype
;
223 struct symbol
*ptr_type
= ptr
->ctype
;
224 struct symbol
*i_type
= i
->ctype
;
227 if (i_type
->type
== SYM_NODE
)
228 i_type
= i_type
->ctype
.base_type
;
229 if (ptr_type
->type
== SYM_NODE
)
230 ptr_type
= ptr_type
->ctype
.base_type
;
232 if (i_type
->type
== SYM_ENUM
)
234 if (!is_int_type(i_type
))
235 return bad_expr_type(expr
);
238 examine_symbol_type(ctype
);
240 ctype
= degenerate(ptr
);
241 if (!ctype
->ctype
.base_type
) {
242 warn(expr
->pos
, "missing type information");
246 /* Get the size of whatever the pointer points to */
248 if (ptr_type
->type
== SYM_NODE
)
249 ptr_type
= ptr_type
->ctype
.base_type
;
250 if (ptr_type
->type
== SYM_PTR
)
251 ptr_type
= ptr_type
->ctype
.base_type
;
252 bit_size
= ptr_type
->bit_size
;
254 /* Special case: adding zero commonly happens as a result of 'array[0]' */
255 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
257 } else if (bit_size
> bits_in_char
) {
258 struct expression
*add
= expr
;
259 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
260 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
262 val
->ctype
= size_t_ctype
;
263 val
->value
= bit_size
>> 3;
266 mul
->ctype
= size_t_ctype
;
270 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
279 static struct symbol
*evaluate_add(struct expression
*expr
)
281 struct expression
*left
= expr
->left
, *right
= expr
->right
;
282 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
284 if (is_ptr_type(ltype
))
285 return evaluate_ptr_add(expr
, left
, right
);
287 if (is_ptr_type(rtype
))
288 return evaluate_ptr_add(expr
, right
, left
);
290 // FIXME! FP promotion
291 return evaluate_int_binop(expr
);
294 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
295 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
297 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
298 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
301 unsigned long mod1
, mod2
, diff
;
302 unsigned long as1
, as2
;
304 struct symbol
*base1
, *base2
;
306 if (target
== source
)
308 if (!target
|| !source
)
309 return "different types";
311 * Peel of per-node information.
312 * FIXME! Check alignment, address space, and context too here!
314 if (target
->type
== SYM_NODE
)
315 target
= target
->ctype
.base_type
;
316 if (source
->type
== SYM_NODE
)
317 source
= source
->ctype
.base_type
;
319 if (target
== source
)
321 if (!target
|| !source
)
322 return "different types";
324 mod1
= target
->ctype
.modifiers
;
325 as1
= target
->ctype
.as
;
326 mod2
= source
->ctype
.modifiers
;
327 as2
= source
->ctype
.as
;
329 type1
= target
->type
;
330 base1
= target
->ctype
.base_type
;
332 type2
= source
->type
;
333 base2
= source
->ctype
.base_type
;
336 * Pointers to functions compare as the function itself
338 if (type1
== SYM_PTR
&& base1
) {
339 switch (base1
->type
) {
343 base1
= base1
->ctype
.base_type
;
348 if (type2
== SYM_PTR
&& base2
) {
349 switch (base2
->type
) {
353 base2
= base2
->ctype
.base_type
;
359 /* Arrays degenerate to pointers for type comparisons */
360 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
361 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
364 return "different base types";
366 /* Must be same address space to be comparable */
368 return "different address spaces";
370 /* Ignore differences in storage types, sign, or addressability */
371 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
373 mod1
&= diff
& ~target_mod_ignore
;
374 mod2
&= diff
& ~source_mod_ignore
;
376 if ((mod1
| mod2
) & MOD_SIZE
)
377 return "different type sizes";
378 return "different modifiers";
382 if (type1
== SYM_FN
) {
384 struct symbol
*arg1
, *arg2
;
385 if (base1
->variadic
!= base2
->variadic
)
386 return "incompatible variadic arguments";
387 PREPARE_PTR_LIST(target
->arguments
, arg1
);
388 PREPARE_PTR_LIST(source
->arguments
, arg2
);
392 diff
= type_difference(arg1
, arg2
, 0, 0);
394 static char argdiff
[80];
395 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
404 FINISH_PTR_LIST(arg2
);
405 FINISH_PTR_LIST(arg1
);
414 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
416 /* NULL expression? Just return the type of the "other side" */
417 if (r
->type
== EXPR_VALUE
&& !r
->value
)
419 if (l
->type
== EXPR_VALUE
&& !l
->value
)
425 * Ignore differences in "volatile" and "const"ness when
426 * subtracting pointers
428 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
430 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
432 const char *typediff
;
433 struct symbol
*ctype
;
434 struct symbol
*ltype
, *rtype
;
436 ltype
= degenerate(l
);
437 rtype
= degenerate(r
);
440 * If it is an integer subtract: the ptr add case will do the
443 if (!is_ptr_type(rtype
))
444 return evaluate_ptr_add(expr
, l
, r
);
447 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
449 ctype
= common_ptr_type(l
, r
);
451 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
455 examine_symbol_type(ctype
);
457 /* Figure out the base type we point to */
458 if (ctype
->type
== SYM_NODE
)
459 ctype
= ctype
->ctype
.base_type
;
460 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
461 warn(expr
->pos
, "subtraction of functions? Share your drugs");
464 ctype
= ctype
->ctype
.base_type
;
466 expr
->ctype
= ssize_t_ctype
;
467 if (ctype
->bit_size
> bits_in_char
) {
468 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
469 struct expression
*div
= expr
;
470 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
472 val
->ctype
= size_t_ctype
;
473 val
->value
= ctype
->bit_size
>> 3;
476 sub
->ctype
= ssize_t_ctype
;
485 return ssize_t_ctype
;
488 static struct symbol
*evaluate_sub(struct expression
*expr
)
490 struct expression
*left
= expr
->left
, *right
= expr
->right
;
491 struct symbol
*ltype
= left
->ctype
;
493 if (is_ptr_type(ltype
))
494 return evaluate_ptr_sub(expr
, left
, right
);
496 // FIXME! FP promotion
497 return evaluate_int_binop(expr
);
500 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
502 static struct symbol
*evaluate_conditional(struct expression
*expr
)
504 struct symbol
*ctype
;
509 if (expr
->type
== EXPR_ASSIGNMENT
)
510 warn(expr
->pos
, "assignment expression in conditional");
512 ctype
= evaluate_expression(expr
);
513 if (ctype
&& is_safe_type(ctype
))
514 warn(expr
->pos
, "testing a 'safe expression'");
519 static struct symbol
*evaluate_logical(struct expression
*expr
)
521 if (!evaluate_conditional(expr
->left
))
523 if (!evaluate_conditional(expr
->right
))
526 expr
->ctype
= &bool_ctype
;
530 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
532 // FIXME! Floating-point promotion!
533 return evaluate_int_binop(expr
);
536 static struct symbol
*evaluate_binop(struct expression
*expr
)
539 // addition can take ptr+int, fp and int
541 return evaluate_add(expr
);
543 // subtraction can take ptr-ptr, fp and int
545 return evaluate_sub(expr
);
547 // Arithmetic operations can take fp and int
548 case '*': case '/': case '%':
549 return evaluate_arithmetic(expr
);
551 // The rest are integer operations (bitops)
552 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
555 return evaluate_int_binop(expr
);
559 static struct symbol
*evaluate_comma(struct expression
*expr
)
561 expr
->ctype
= expr
->right
->ctype
;
565 static struct symbol
*evaluate_compare(struct expression
*expr
)
567 struct expression
*left
= expr
->left
, *right
= expr
->right
;
568 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
569 struct symbol
*ctype
;
572 if (is_type_type(ltype
) && is_type_type(rtype
)) {
573 expr
->ctype
= &bool_ctype
;
577 if (is_safe_type(ltype
) || is_safe_type(rtype
))
578 warn(expr
->pos
, "testing a 'safe expression'");
581 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
582 expr
->ctype
= &bool_ctype
;
583 // FIXME! Check the types for compatibility
587 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
589 expr
->ctype
= &bool_ctype
;
593 return bad_expr_type(expr
);
596 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
598 /* Integer promotion? */
599 if (ltype
->type
== SYM_NODE
)
600 ltype
= ltype
->ctype
.base_type
;
601 if (rtype
->type
== SYM_NODE
)
602 rtype
= rtype
->ctype
.base_type
;
603 if (ltype
->type
== SYM_ENUM
|| ltype
->type
== SYM_BITFIELD
)
605 if (rtype
->type
== SYM_ENUM
|| rtype
->type
== SYM_BITFIELD
)
607 return (is_int_type(ltype
) && is_int_type(rtype
));
610 static int is_null_ptr(struct expression
*expr
)
612 return (expr
->type
== EXPR_VALUE
&&
617 * FIXME!! This should do casts, array degeneration etc..
619 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
621 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
623 if (ltype
->type
== SYM_NODE
)
624 ltype
= ltype
->ctype
.base_type
;
626 if (rtype
->type
== SYM_NODE
)
627 rtype
= rtype
->ctype
.base_type
;
629 if (ltype
->type
== SYM_PTR
) {
630 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
634 if (rtype
->type
== SYM_PTR
) {
635 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
641 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
643 struct expression
*cond
, *true, *false;
644 struct symbol
*ctype
, *ltype
, *rtype
;
645 const char * typediff
;
647 ctype
= degenerate(expr
->conditional
);
648 cond
= expr
->conditional
;
652 if (expr
->cond_true
) {
653 ltype
= degenerate(expr
->cond_true
);
654 true = expr
->cond_true
;
657 rtype
= degenerate(expr
->cond_false
);
658 false = expr
->cond_false
;
661 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
663 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
665 ctype
= compatible_ptr_type(true, expr
->cond_false
);
667 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
677 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
678 struct expression
**rp
, struct symbol
*source
, const char *where
)
680 const char *typediff
;
684 /* It's ok if the target is more volatile or const than the source */
685 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
689 if (compatible_integer_types(target
, source
)) {
690 if (target
->bit_size
!= source
->bit_size
)
691 *rp
= cast_to(*rp
, target
);
695 /* Pointer destination? */
697 target_as
= t
->ctype
.as
;
698 if (t
->type
== SYM_NODE
) {
699 t
= t
->ctype
.base_type
;
700 target_as
|= t
->ctype
.as
;
702 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
703 struct expression
*right
= *rp
;
704 struct symbol
*s
= source
;
707 // NULL pointer is always ok
708 if (right
->type
== EXPR_VALUE
&& !right
->value
)
711 /* "void *" matches anything as long as the address space is ok */
712 source_as
= s
->ctype
.as
;
713 if (s
->type
== SYM_NODE
) {
714 s
= s
->ctype
.base_type
;
715 source_as
|= s
->ctype
.as
;
717 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
718 s
= s
->ctype
.base_type
;
719 t
= t
->ctype
.base_type
;
720 if (s
== &void_ctype
|| t
== &void_ctype
)
726 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
727 info(expr
->pos
, " expected %s", show_typename(target
));
728 info(expr
->pos
, " got %s", show_typename(source
));
733 * FIXME!! This is wrong from a double evaluation standpoint. We can't
734 * just expand the expression twice, that would make any side effects
737 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
740 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
741 static const int op_trans
[] = {
742 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
743 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
744 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
745 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
746 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
747 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
748 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
749 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
750 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
751 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
754 subexpr
->left
= left
;
755 subexpr
->right
= right
;
756 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
758 expr
->right
= subexpr
;
759 return evaluate_binop(subexpr
);
762 static struct symbol
*evaluate_assignment(struct expression
*expr
)
764 struct expression
*left
= expr
->left
, *right
= expr
->right
;
765 struct symbol
*ltype
, *rtype
;
768 rtype
= right
->ctype
;
769 if (expr
->op
!= '=') {
770 rtype
= evaluate_binop_assignment(expr
, left
, right
);
776 if (!lvalue_expression(left
)) {
777 warn(expr
->pos
, "not an lvalue");
781 rtype
= degenerate(right
);
783 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
786 if (ltype
->type
== SYM_NODE
)
787 ltype
->ctype
.modifiers
|= MOD_ASSIGNED
;
793 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
795 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
796 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
798 newsym
->ctype
.as
= as
;
799 newsym
->ctype
.modifiers
= mod
;
805 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
807 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
808 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
810 node
->ctype
.base_type
= ptr
;
811 ptr
->bit_size
= bits_in_pointer
;
812 ptr
->ctype
.alignment
= pointer_alignment
;
814 node
->bit_size
= bits_in_pointer
;
815 node
->ctype
.alignment
= pointer_alignment
;
817 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
818 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
819 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
820 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
822 if (sym
->type
== SYM_NODE
) {
823 ptr
->ctype
.as
|= sym
->ctype
.as
;
824 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
825 sym
= sym
->ctype
.base_type
;
827 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
828 ptr
->ctype
.as
|= sym
->ctype
.as
;
829 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
830 sym
= sym
->ctype
.base_type
;
832 ptr
->ctype
.base_type
= sym
;
837 /* Arrays degenerate into pointers on pointer arithmetic */
838 static struct symbol
*degenerate(struct expression
*expr
)
840 struct symbol
*ctype
, *base
;
848 if (ctype
->type
== SYM_NODE
)
849 base
= ctype
->ctype
.base_type
;
851 * Arrays degenerate into pointers to the entries, while
852 * functions degenerate into pointers to themselves
854 switch (base
->type
) {
857 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
858 warn(expr
->pos
, "strange non-value function or array");
862 ctype
= create_pointer(expr
, ctype
, 1);
870 static struct symbol
*evaluate_addressof(struct expression
*expr
)
872 struct expression
*op
= expr
->unop
;
873 struct symbol
*ctype
;
875 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
876 warn(expr
->pos
, "not addressable");
883 * symbol expression evaluation is lazy about the type
884 * of the sub-expression, so we may have to generate
885 * the type here if so..
888 ctype
= create_pointer(expr
, ctype
, 0);
895 static struct symbol
*evaluate_dereference(struct expression
*expr
)
897 struct expression
*op
= expr
->unop
;
898 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
900 /* Simplify: *&(expr) => (expr) */
901 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
906 /* Dereferencing a node drops all the node information. */
907 if (ctype
->type
== SYM_NODE
)
908 ctype
= ctype
->ctype
.base_type
;
910 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
911 target
= ctype
->ctype
.base_type
;
913 switch (ctype
->type
) {
915 warn(expr
->pos
, "cannot derefence this type");
918 merge_type(node
, ctype
);
919 if (ctype
->type
!= SYM_ARRAY
)
922 * Dereferencing a pointer to an array results in a
923 * degenerate dereference: the expression becomes
924 * just a pointer to the entry, and the derefence
929 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
930 target
->bit_size
= bits_in_pointer
;
931 target
->ctype
.alignment
= pointer_alignment
;
932 merge_type(target
, ctype
->ctype
.base_type
);
937 * When an array is dereferenced, we need to pick
938 * up the attributes of the original node too..
940 merge_type(node
, op
->ctype
);
941 merge_type(node
, ctype
);
945 node
->bit_size
= target
->bit_size
;
946 node
->array_size
= target
->array_size
;
953 * Unary post-ops: x++ and x--
955 static struct symbol
*evaluate_postop(struct expression
*expr
)
957 struct expression
*op
= expr
->unop
;
958 struct symbol
*ctype
= op
->ctype
;
960 if (!lvalue_expression(expr
->unop
)) {
961 warn(expr
->pos
, "need lvalue expression for ++/--");
965 if (ctype
->type
== SYM_NODE
)
966 ctype
->ctype
.modifiers
|= MOD_ASSIGNED
;
972 static struct symbol
*evaluate_preop(struct expression
*expr
)
974 struct symbol
*ctype
= expr
->unop
->ctype
;
983 return evaluate_dereference(expr
);
986 return evaluate_addressof(expr
);
988 case SPECIAL_INCREMENT
:
989 case SPECIAL_DECREMENT
:
991 * From a type evaluation standpoint the pre-ops are
992 * the same as the postops
994 return evaluate_postop(expr
);
997 if (is_safe_type(ctype
))
998 warn(expr
->pos
, "testing a 'safe expression'");
1005 expr
->ctype
= ctype
;
1009 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1011 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1012 struct ptr_list
*list
= head
;
1018 for (i
= 0; i
< list
->nr
; i
++) {
1019 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1021 if (sym
->ident
!= ident
)
1023 *offset
= sym
->offset
;
1026 struct symbol
*ctype
= sym
->ctype
.base_type
;
1030 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1032 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1035 *offset
+= sym
->offset
;
1039 } while ((list
= list
->next
) != head
);
1043 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1045 struct expression
*add
;
1049 /* Create a new add-expression */
1050 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1053 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1054 add
->right
->ctype
= &int_ctype
;
1055 add
->right
->value
= offset
;
1059 * The ctype of the pointer will be lazily evaluated if
1060 * we ever take the address of this member dereference..
1066 /* structure/union dereference */
1067 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1070 struct symbol
*ctype
, *member
;
1071 struct expression
*deref
= expr
->deref
, *add
;
1072 struct ident
*ident
= expr
->member
;
1076 if (!evaluate_expression(deref
))
1079 warn(expr
->pos
, "bad member name");
1083 ctype
= deref
->ctype
;
1084 address_space
= ctype
->ctype
.as
;
1085 mod
= ctype
->ctype
.modifiers
;
1086 if (ctype
->type
== SYM_NODE
) {
1087 ctype
= ctype
->ctype
.base_type
;
1088 address_space
|= ctype
->ctype
.as
;
1089 mod
|= ctype
->ctype
.modifiers
;
1091 if (!lvalue_expression(deref
)) {
1092 warn(deref
->pos
, "expected lvalue for member dereference");
1095 deref
= deref
->unop
;
1096 expr
->deref
= deref
;
1097 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1098 warn(expr
->pos
, "expected structure or union");
1102 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1104 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1105 const char *name
= "<unnamed>";
1108 name
= ctype
->ident
->name
;
1109 namelen
= ctype
->ident
->len
;
1111 warn(expr
->pos
, "no member '%s' in %s %.*s",
1112 show_ident(ident
), type
, namelen
, name
);
1117 * The member needs to take on the address space and modifiers of
1118 * the "parent" type.
1120 member
= convert_to_as_mod(member
, address_space
, mod
);
1121 add
= evaluate_offset(deref
, offset
);
1123 ctype
= member
->ctype
.base_type
;
1124 if (ctype
->type
== SYM_BITFIELD
) {
1125 expr
->type
= EXPR_BITFIELD
;
1126 expr
->bitpos
= member
->bit_offset
;
1127 expr
->nrbits
= member
->fieldwidth
;
1128 expr
->address
= add
;
1130 expr
->type
= EXPR_PREOP
;
1135 expr
->ctype
= member
;
1139 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1143 if (expr
->cast_type
) {
1144 examine_symbol_type(expr
->cast_type
);
1145 size
= expr
->cast_type
->bit_size
;
1147 if (!evaluate_expression(expr
->cast_expression
))
1149 size
= expr
->cast_expression
->ctype
->bit_size
;
1152 warn(expr
->pos
, "cannot size expression");
1155 expr
->type
= EXPR_VALUE
;
1156 expr
->value
= size
>> 3;
1157 expr
->ctype
= size_t_ctype
;
1158 return size_t_ctype
;
1161 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1163 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1164 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1168 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1170 struct expression
*expr
;
1171 struct symbol_list
*argument_types
= fn
->arguments
;
1172 struct symbol
*argtype
;
1175 PREPARE_PTR_LIST(argument_types
, argtype
);
1176 FOR_EACH_PTR (head
, expr
) {
1177 struct expression
**p
= THIS_ADDRESS(expr
);
1178 struct symbol
*ctype
, *target
;
1179 ctype
= evaluate_expression(expr
);
1184 if (context_clash(f
, ctype
))
1185 warn(expr
->pos
, "argument %d used in wrong context", i
);
1187 ctype
= degenerate(expr
);
1190 if (!target
&& ctype
->bit_size
< bits_in_int
)
1191 target
= &int_ctype
;
1193 static char where
[30];
1194 examine_symbol_type(target
);
1195 sprintf(where
, "argument %d", i
);
1196 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1200 NEXT_PTR_LIST(argtype
);
1202 FINISH_PTR_LIST(argtype
);
1206 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1207 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1209 struct expression
*entry
;
1213 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1214 struct expression
**p
= THIS_ADDRESS(entry
);
1216 if (entry
->type
== EXPR_INDEX
) {
1217 current
= entry
->idx_to
;
1220 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1228 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1230 struct expression
*entry
;
1233 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1234 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1235 struct expression
**p
= THIS_ADDRESS(entry
);
1237 if (entry
->type
== EXPR_IDENTIFIER
) {
1238 struct ident
*ident
= entry
->expr_ident
;
1239 /* We special-case the "already right place" case */
1240 if (sym
&& sym
->ident
== ident
)
1242 RESET_PTR_LIST(sym
);
1245 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1248 if (sym
->ident
== ident
)
1256 warn(expr
->pos
, "too many initializers for struct/union");
1260 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1264 FINISH_PTR_LIST(sym
);
1270 * Initializers are kind of like assignments. Except
1271 * they can be a hell of a lot more complex.
1273 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1275 struct expression
*expr
= *ep
;
1278 * Simple non-structure/array initializers are the simple
1279 * case, and look (and parse) largely like assignments.
1281 if (expr
->type
!= EXPR_INITIALIZER
) {
1283 struct symbol
*rtype
= evaluate_expression(expr
);
1285 struct expression
*pos
;
1287 // FIXME! char array[] = "string" special case
1288 // should _not_ degenerate.
1289 rtype
= degenerate(expr
);
1290 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1291 /* strings are special: char arrays */
1292 if (rtype
->type
== SYM_ARRAY
)
1293 size
= get_expression_value(rtype
->array_size
);
1295 * Don't bother creating a position expression for
1296 * the simple initializer cases that don't need it.
1298 * We need a position if the initializer has a byte
1299 * offset, _or_ if we're initializing a bitfield.
1301 if (offset
|| ctype
->fieldwidth
) {
1302 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1303 pos
->init_offset
= offset
;
1304 pos
->init_sym
= ctype
;
1305 pos
->init_expr
= *ep
;
1306 pos
->ctype
= expr
->ctype
;
1313 expr
->ctype
= ctype
;
1314 if (ctype
->type
== SYM_NODE
)
1315 ctype
= ctype
->ctype
.base_type
;
1317 switch (ctype
->type
) {
1320 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1322 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1324 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1328 warn(expr
->pos
, "unexpected compound initializer");
1332 static int get_as(struct symbol
*sym
)
1340 mod
= sym
->ctype
.modifiers
;
1341 if (sym
->type
== SYM_NODE
) {
1342 sym
= sym
->ctype
.base_type
;
1343 as
|= sym
->ctype
.as
;
1344 mod
|= sym
->ctype
.modifiers
;
1347 * You can always throw a value away by casting to
1348 * "void" - that's an implicit "force". Note that
1349 * the same is _not_ true of "void *".
1351 if (sym
== &void_ctype
)
1355 * At least for now, allow casting to a "unsigned long".
1356 * That's how we do things like pointer arithmetic and
1357 * store pointers to registers.
1359 if (sym
== &ulong_ctype
)
1362 if (sym
&& sym
->type
== SYM_PTR
) {
1363 sym
= sym
->ctype
.base_type
;
1364 as
|= sym
->ctype
.as
;
1365 mod
|= sym
->ctype
.modifiers
;
1367 if (mod
& MOD_FORCE
)
1372 static struct symbol
*evaluate_cast(struct expression
*expr
)
1374 struct expression
*target
= expr
->cast_expression
;
1375 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1377 expr
->ctype
= ctype
;
1378 expr
->cast_type
= ctype
;
1381 * Special case: a cast can be followed by an
1382 * initializer, in which case we need to pass
1383 * the type value down to that initializer rather
1384 * than trying to evaluate it as an expression
1386 * A more complex case is when the initializer is
1387 * dereferenced as part of a post-fix expression.
1388 * We need to produce an expression that can be dereferenced.
1390 if (target
->type
== EXPR_INITIALIZER
) {
1391 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1392 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1394 sym
->ctype
.base_type
= ctype
;
1395 sym
->initializer
= expr
->cast_expression
;
1396 evaluate_symbol(sym
);
1398 addr
->ctype
= NULL
; /* Lazy eval */
1401 expr
->type
= EXPR_PREOP
;
1404 expr
->ctype
= ctype
;
1408 evaluate_expression(target
);
1411 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1412 warn(expr
->pos
, "cast removes address space of expression");
1415 * Casts of constant values are special: they
1416 * can be NULL, and thus need to be simplified
1419 if (target
->type
== EXPR_VALUE
)
1420 cast_value(expr
, ctype
, target
, target
->ctype
);
1426 * Evaluate a call expression with a symbol. This
1427 * should expand inline functions, and evaluate
1430 static int evaluate_symbol_call(struct expression
*expr
)
1432 struct expression
*fn
= expr
->fn
;
1433 struct symbol
*ctype
= fn
->ctype
;
1435 if (fn
->type
!= EXPR_PREOP
)
1438 if (ctype
->op
&& ctype
->op
->evaluate
)
1439 return ctype
->op
->evaluate(expr
);
1441 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1443 struct symbol
*curr
= current_fn
;
1444 unsigned long context
= current_context
;
1445 unsigned long mask
= current_contextmask
;
1447 current_context
|= ctype
->ctype
.context
;
1448 current_contextmask
|= ctype
->ctype
.contextmask
;
1449 current_fn
= ctype
->ctype
.base_type
;
1450 ret
= inline_function(expr
, ctype
);
1452 /* restore the old function context */
1454 current_context
= context
;
1455 current_contextmask
= mask
;
1462 static struct symbol
*evaluate_call(struct expression
*expr
)
1465 struct symbol
*ctype
, *sym
;
1466 struct expression
*fn
= expr
->fn
;
1467 struct expression_list
*arglist
= expr
->args
;
1469 if (!evaluate_expression(fn
))
1471 sym
= ctype
= fn
->ctype
;
1472 if (ctype
->type
== SYM_NODE
)
1473 ctype
= ctype
->ctype
.base_type
;
1474 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1475 ctype
= ctype
->ctype
.base_type
;
1476 if (!evaluate_arguments(sym
, ctype
, arglist
))
1478 if (ctype
->type
!= SYM_FN
) {
1479 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1482 args
= expression_list_size(expr
->args
);
1483 fnargs
= symbol_list_size(ctype
->arguments
);
1485 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1486 if (args
> fnargs
&& !ctype
->variadic
)
1487 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1488 if (sym
->type
== SYM_NODE
) {
1489 if (evaluate_symbol_call(expr
))
1492 expr
->ctype
= ctype
->ctype
.base_type
;
1496 struct symbol
*evaluate_expression(struct expression
*expr
)
1503 switch (expr
->type
) {
1505 warn(expr
->pos
, "value expression without a type");
1508 return evaluate_string(expr
);
1510 return evaluate_symbol_expression(expr
);
1512 if (!evaluate_expression(expr
->left
))
1514 if (!evaluate_expression(expr
->right
))
1516 return evaluate_binop(expr
);
1518 return evaluate_logical(expr
);
1520 if (!evaluate_expression(expr
->left
))
1522 if (!evaluate_expression(expr
->right
))
1524 return evaluate_comma(expr
);
1526 if (!evaluate_expression(expr
->left
))
1528 if (!evaluate_expression(expr
->right
))
1530 return evaluate_compare(expr
);
1531 case EXPR_ASSIGNMENT
:
1532 if (!evaluate_expression(expr
->left
))
1534 if (!evaluate_expression(expr
->right
))
1536 return evaluate_assignment(expr
);
1538 if (!evaluate_expression(expr
->unop
))
1540 return evaluate_preop(expr
);
1542 if (!evaluate_expression(expr
->unop
))
1544 return evaluate_postop(expr
);
1546 return evaluate_cast(expr
);
1548 return evaluate_sizeof(expr
);
1550 return evaluate_member_dereference(expr
);
1552 return evaluate_call(expr
);
1554 warn(expr
->pos
, "bitfield generated by parser");
1556 case EXPR_CONDITIONAL
:
1557 if (!evaluate_conditional(expr
->conditional
))
1559 if (!evaluate_expression(expr
->cond_false
))
1561 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1563 return evaluate_conditional_expression(expr
);
1564 case EXPR_STATEMENT
:
1565 expr
->ctype
= evaluate_statement(expr
->statement
);
1569 expr
->ctype
= &ptr_ctype
;
1573 /* Evaluate the type of the symbol .. */
1574 evaluate_symbol(expr
->symbol
);
1575 /* .. but the type of the _expression_ is a "type" */
1576 expr
->ctype
= &type_ctype
;
1579 /* These can not exist as stand-alone expressions */
1580 case EXPR_INITIALIZER
:
1581 case EXPR_IDENTIFIER
:
1584 warn(expr
->pos
, "internal front-end error: initializer in expression");
1590 void check_duplicates(struct symbol
*sym
)
1592 struct symbol
*next
= sym
;
1594 while ((next
= next
->same_symbol
) != NULL
) {
1595 const char *typediff
;
1596 evaluate_symbol(next
);
1597 typediff
= type_difference(sym
, next
, 0, 0);
1599 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1600 show_ident(sym
->ident
),
1601 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1607 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1609 struct symbol
*base_type
;
1614 sym
= examine_symbol_type(sym
);
1615 base_type
= sym
->ctype
.base_type
;
1619 /* Evaluate the initializers */
1620 if (sym
->initializer
) {
1621 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1622 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1623 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1624 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1625 base_type
->bit_size
= bit_size
;
1626 sym
->array_size
= base_type
->array_size
;
1627 sym
->bit_size
= bit_size
;
1631 /* And finally, evaluate the body of the symbol too */
1632 if (base_type
->type
== SYM_FN
) {
1635 FOR_EACH_PTR(base_type
->arguments
, s
) {
1639 if (base_type
->stmt
) {
1640 current_fn
= base_type
;
1641 current_contextmask
= sym
->ctype
.contextmask
;
1642 current_context
= sym
->ctype
.context
;
1643 evaluate_statement(base_type
->stmt
);
1650 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1652 struct expression
*expr
= stmt
->expression
;
1653 struct symbol
*ctype
, *fntype
;
1655 fntype
= current_fn
->ctype
.base_type
;
1656 if (!fntype
|| fntype
== &void_ctype
) {
1658 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1663 warn(stmt
->pos
, "return with no return value");
1666 ctype
= evaluate_expression(expr
);
1669 ctype
= degenerate(expr
);
1670 expr
->ctype
= ctype
;
1671 compatible_assignment_types(expr
, fntype
, &expr
, ctype
, "return expression");
1672 stmt
->expression
= expr
;
1676 static void evaluate_if_statement(struct statement
*stmt
)
1678 struct expression
*expr
= stmt
->if_conditional
;
1679 struct symbol
*ctype
;
1684 ctype
= evaluate_conditional(expr
);
1688 evaluate_statement(stmt
->if_true
);
1689 evaluate_statement(stmt
->if_false
);
1692 struct symbol
*evaluate_statement(struct statement
*stmt
)
1697 switch (stmt
->type
) {
1699 return evaluate_return_expression(stmt
);
1701 case STMT_EXPRESSION
:
1702 evaluate_expression(stmt
->expression
);
1703 return degenerate(stmt
->expression
);
1705 case STMT_COMPOUND
: {
1706 struct statement
*s
;
1707 struct symbol
*type
= NULL
;
1710 /* Evaluate each symbol in the compound statement */
1711 FOR_EACH_PTR(stmt
->syms
, sym
) {
1712 evaluate_symbol(sym
);
1714 evaluate_symbol(stmt
->ret
);
1717 * Then, evaluate each statement, making the type of the
1718 * compound statement be the type of the last statement
1721 FOR_EACH_PTR(stmt
->stmts
, s
) {
1722 type
= evaluate_statement(s
);
1727 evaluate_if_statement(stmt
);
1730 evaluate_conditional(stmt
->iterator_pre_condition
);
1731 evaluate_conditional(stmt
->iterator_post_condition
);
1732 evaluate_statement(stmt
->iterator_pre_statement
);
1733 evaluate_statement(stmt
->iterator_statement
);
1734 evaluate_statement(stmt
->iterator_post_statement
);
1737 evaluate_expression(stmt
->switch_expression
);
1738 evaluate_statement(stmt
->switch_statement
);
1741 evaluate_expression(stmt
->case_expression
);
1742 evaluate_expression(stmt
->case_to
);
1743 evaluate_statement(stmt
->case_statement
);
1746 evaluate_statement(stmt
->label_statement
);
1749 evaluate_expression(stmt
->goto_expression
);
1754 /* FIXME! Do the asm parameter evaluation! */