Remove totally bogus phi-source liveness thing.
[smatch.git] / evaluate.c
blob24acefe49a74e5eee3b5049decb73ac91eb4a7a3
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 sparse_error(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 sparse_error(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->ctype.modifiers = MOD_STATIC;
78 sym->ctype.base_type = array;
79 sym->initializer = initstr;
81 initstr->ctype = sym;
82 initstr->string = expr->string;
84 array->array_size = sym->array_size;
85 array->bit_size = bits_in_char * length;
86 array->ctype.alignment = 1;
87 array->ctype.modifiers = MOD_STATIC;
88 array->ctype.base_type = &char_ctype;
90 addr->symbol = sym;
91 addr->ctype = &lazy_ptr_ctype;
93 expr->type = EXPR_PREOP;
94 expr->op = '*';
95 expr->unop = addr;
96 expr->ctype = sym;
97 return sym;
100 static inline struct symbol *integer_promotion(struct symbol *type)
102 struct symbol *orig_type = type;
103 unsigned long mod = type->ctype.modifiers;
104 int width;
106 if (type->type == SYM_NODE)
107 type = type->ctype.base_type;
108 if (type->type == SYM_ENUM)
109 type = type->ctype.base_type;
110 width = type->bit_size;
113 * Bitfields always promote to the base type,
114 * even if the bitfield might be bigger than
115 * an "int".
117 if (type->type == SYM_BITFIELD) {
118 type = type->ctype.base_type;
119 orig_type = type;
121 mod = type->ctype.modifiers;
122 if (width < bits_in_int)
123 return &int_ctype;
125 /* If char/short has as many bits as int, it still gets "promoted" */
126 if (mod & (MOD_CHAR | MOD_SHORT)) {
127 if (mod & MOD_UNSIGNED)
128 return &uint_ctype;
129 return &int_ctype;
131 return orig_type;
135 * integer part of usual arithmetic conversions:
136 * integer promotions are applied
137 * if left and right are identical, we are done
138 * if signedness is the same, convert one with lower rank
139 * unless unsigned argument has rank lower than signed one, convert the
140 * signed one.
141 * if signed argument is bigger than unsigned one, convert the unsigned.
142 * otherwise, convert signed.
144 * Leaving aside the integer promotions, that is equivalent to
145 * if identical, don't convert
146 * if left is bigger than right, convert right
147 * if right is bigger than left, convert right
148 * otherwise, if signedness is the same, convert one with lower rank
149 * otherwise convert the signed one.
151 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
153 unsigned long lmod, rmod;
155 left = integer_promotion(left);
156 right = integer_promotion(right);
158 if (left == right)
159 goto left;
161 if (left->bit_size > right->bit_size)
162 goto left;
164 if (right->bit_size > left->bit_size)
165 goto right;
167 lmod = left->ctype.modifiers;
168 rmod = right->ctype.modifiers;
169 if ((lmod ^ rmod) & MOD_UNSIGNED) {
170 if (lmod & MOD_UNSIGNED)
171 goto left;
172 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
173 goto left;
174 right:
175 left = right;
176 left:
177 return left;
180 static int same_cast_type(struct symbol *orig, struct symbol *new)
182 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
185 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
187 unsigned long mod, as;
189 mod = 0; as = 0;
190 while (node) {
191 mod |= node->ctype.modifiers;
192 as |= node->ctype.as;
193 if (node->type == SYM_NODE) {
194 node = node->ctype.base_type;
195 continue;
197 break;
199 *modp = mod & ~MOD_IGNORE;
200 *asp = as;
201 return node;
204 static int is_same_type(struct expression *expr, struct symbol *new)
206 struct symbol *old = expr->ctype;
207 unsigned long oldmod, newmod, oldas, newas;
209 old = base_type(old, &oldmod, &oldas);
210 new = base_type(new, &newmod, &newas);
212 /* Same base type, same address space? */
213 if (old == new && oldas == newas) {
214 unsigned long difmod;
216 /* Check the modifier bits. */
217 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
219 /* Exact same type? */
220 if (!difmod)
221 return 1;
224 * Not the same type, but differs only in "const".
225 * Don't warn about MOD_NOCAST.
227 if (difmod == MOD_CONST)
228 return 0;
230 if ((oldmod | newmod) & MOD_NOCAST) {
231 const char *tofrom = "to/from";
232 if (!(newmod & MOD_NOCAST))
233 tofrom = "from";
234 if (!(oldmod & MOD_NOCAST))
235 tofrom = "to";
236 warning(expr->pos, "implicit cast %s nocast type", tofrom);
238 return 0;
242 * This gets called for implicit casts in assignments and
243 * integer promotion. We often want to try to move the
244 * cast down, because the ops involved may have been
245 * implicitly cast up, and we can get rid of the casts
246 * early.
248 static struct expression * cast_to(struct expression *old, struct symbol *type)
250 struct expression *expr;
252 if (is_same_type(old, type))
253 return old;
256 * See if we can simplify the op. Move the cast down.
258 switch (old->type) {
259 case EXPR_PREOP:
260 if (old->op == '~') {
261 old->ctype = type;
262 old->unop = cast_to(old->unop, type);
263 return old;
265 break;
267 case EXPR_IMPLIED_CAST:
268 if (old->ctype->bit_size >= type->bit_size) {
269 struct expression *orig = old->cast_expression;
270 if (same_cast_type(orig->ctype, type))
271 return orig;
272 if (old->ctype->bit_offset == type->bit_offset) {
273 old->ctype = type;
274 old->cast_type = type;
275 return old;
278 break;
280 default:
281 /* nothing */;
284 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
285 expr->ctype = type;
286 expr->cast_type = type;
287 expr->cast_expression = old;
288 return expr;
291 static int is_type_type(struct symbol *type)
293 return (type->ctype.modifiers & MOD_TYPE) != 0;
296 int is_ptr_type(struct symbol *type)
298 if (type->type == SYM_NODE)
299 type = type->ctype.base_type;
300 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
303 static inline int is_float_type(struct symbol *type)
305 if (type->type == SYM_NODE)
306 type = type->ctype.base_type;
307 return type->ctype.base_type == &fp_type;
310 static inline int is_byte_type(struct symbol *type)
312 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
315 static inline int is_string_type(struct symbol *type)
317 if (type->type == SYM_NODE)
318 type = type->ctype.base_type;
319 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
322 static struct symbol *bad_expr_type(struct expression *expr)
324 sparse_error(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
325 switch (expr->type) {
326 case EXPR_BINOP:
327 case EXPR_COMPARE:
328 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
329 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
330 break;
331 case EXPR_PREOP:
332 case EXPR_POSTOP:
333 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
334 break;
335 default:
336 break;
339 return NULL;
342 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
344 struct expression *left = *lp, *right = *rp;
345 struct symbol *ltype = left->ctype, *rtype = right->ctype;
347 if (ltype->type == SYM_NODE)
348 ltype = ltype->ctype.base_type;
349 if (rtype->type == SYM_NODE)
350 rtype = rtype->ctype.base_type;
351 if (is_float_type(ltype)) {
352 if (is_int_type(rtype))
353 goto Left;
354 if (is_float_type(rtype)) {
355 unsigned long lmod = ltype->ctype.modifiers;
356 unsigned long rmod = rtype->ctype.modifiers;
357 lmod &= MOD_LONG | MOD_LONGLONG;
358 rmod &= MOD_LONG | MOD_LONGLONG;
359 if (lmod == rmod)
360 return ltype;
361 if (lmod & ~rmod)
362 goto Left;
363 else
364 goto Right;
366 return NULL;
368 if (!is_float_type(rtype) || !is_int_type(ltype))
369 return NULL;
370 Right:
371 *lp = cast_to(left, rtype);
372 return rtype;
373 Left:
374 *rp = cast_to(right, ltype);
375 return ltype;
378 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
380 struct expression *left = *lp, *right = *rp;
381 struct symbol *ltype = left->ctype, *rtype = right->ctype;
383 if (ltype->type == SYM_NODE)
384 ltype = ltype->ctype.base_type;
385 if (rtype->type == SYM_NODE)
386 rtype = rtype->ctype.base_type;
387 if (is_int_type(ltype) && is_int_type(rtype)) {
388 struct symbol *ctype = bigger_int_type(ltype, rtype);
390 *lp = cast_to(left, ctype);
391 *rp = cast_to(right, ctype);
392 return ctype;
394 return NULL;
397 static int restricted_value(struct expression *v, struct symbol *type)
399 if (v->type != EXPR_VALUE)
400 return 1;
401 if (v->value != 0)
402 return 1;
403 return 0;
406 static int restricted_binop(int op, struct symbol *type)
408 switch (op) {
409 case '&':
410 case '|':
411 case '^':
412 case '?':
413 case '=':
414 case SPECIAL_EQUAL:
415 case SPECIAL_NOTEQUAL:
416 case SPECIAL_AND_ASSIGN:
417 case SPECIAL_OR_ASSIGN:
418 case SPECIAL_XOR_ASSIGN:
419 return 0;
420 default:
421 return 1;
425 static int restricted_unop(int op, struct symbol *type)
427 if (op == '~' && type->bit_size >= bits_in_int)
428 return 0;
429 if (op == '+')
430 return 0;
431 return 1;
434 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
436 struct expression *left = *lp, *right = *rp;
437 struct symbol *ltype = left->ctype, *rtype = right->ctype;
438 struct symbol *type = NULL;
440 if (ltype->type == SYM_NODE)
441 ltype = ltype->ctype.base_type;
442 if (ltype->type == SYM_ENUM)
443 ltype = ltype->ctype.base_type;
444 if (rtype->type == SYM_NODE)
445 rtype = rtype->ctype.base_type;
446 if (rtype->type == SYM_ENUM)
447 rtype = rtype->ctype.base_type;
448 if (is_restricted_type(ltype)) {
449 if (is_restricted_type(rtype)) {
450 if (ltype == rtype)
451 type = ltype;
452 } else {
453 if (!restricted_value(right, ltype))
454 type = ltype;
456 } else if (is_restricted_type(rtype)) {
457 if (!restricted_value(left, rtype))
458 type = rtype;
460 if (!type)
461 return NULL;
462 if (restricted_binop(op, type))
463 return NULL;
464 return type;
467 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
469 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
470 if (!ctype && float_ok)
471 ctype = compatible_float_binop(&expr->left, &expr->right);
472 if (!ctype)
473 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
474 if (ctype) {
475 expr->ctype = ctype;
476 return ctype;
478 return bad_expr_type(expr);
481 static inline int lvalue_expression(struct expression *expr)
483 return expr->type == EXPR_PREOP && expr->op == '*';
486 static int ptr_object_size(struct symbol *ptr_type)
488 if (ptr_type->type == SYM_NODE)
489 ptr_type = ptr_type->ctype.base_type;
490 if (ptr_type->type == SYM_PTR)
491 ptr_type = get_base_type(ptr_type);
492 return ptr_type->bit_size;
495 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
497 struct expression *i = *ip;
498 struct symbol *ptr_type = ctype;
499 int bit_size;
501 if (ptr_type->type == SYM_NODE)
502 ptr_type = ptr_type->ctype.base_type;
504 if (!is_int_type(i->ctype))
505 return bad_expr_type(expr);
507 examine_symbol_type(ctype);
509 if (!ctype->ctype.base_type) {
510 sparse_error(expr->pos, "missing type information");
511 return NULL;
514 /* Get the size of whatever the pointer points to */
515 bit_size = ptr_object_size(ctype);
517 if (bit_size > bits_in_char) {
518 int multiply = bit_size >> 3;
519 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
521 if (i->type == EXPR_VALUE) {
522 val->value = i->value * multiply;
523 val->ctype = size_t_ctype;
524 *ip = val;
525 } else {
526 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
528 val->ctype = size_t_ctype;
529 val->value = bit_size >> 3;
531 mul->op = '*';
532 mul->ctype = size_t_ctype;
533 mul->left = i;
534 mul->right = val;
536 *ip = mul;
540 expr->ctype = ctype;
541 return ctype;
544 static struct symbol *evaluate_add(struct expression *expr)
546 struct expression *left = expr->left, *right = expr->right;
547 struct symbol *ltype = left->ctype, *rtype = right->ctype;
549 if (is_ptr_type(ltype))
550 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
552 if (is_ptr_type(rtype))
553 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
555 return evaluate_arith(expr, 1);
558 const char * type_difference(struct symbol *target, struct symbol *source,
559 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
561 for (;;) {
562 unsigned long mod1, mod2, diff;
563 unsigned long as1, as2;
564 int type1, type2;
565 struct symbol *base1, *base2;
567 if (target == source)
568 break;
569 if (!target || !source)
570 return "different types";
572 * Peel of per-node information.
573 * FIXME! Check alignment and context too here!
575 mod1 = target->ctype.modifiers;
576 as1 = target->ctype.as;
577 mod2 = source->ctype.modifiers;
578 as2 = source->ctype.as;
579 if (target->type == SYM_NODE) {
580 target = target->ctype.base_type;
581 if (!target)
582 return "bad types";
583 if (target->type == SYM_PTR) {
584 mod1 = 0;
585 as1 = 0;
587 mod1 |= target->ctype.modifiers;
588 as1 |= target->ctype.as;
590 if (source->type == SYM_NODE) {
591 source = source->ctype.base_type;
592 if (!source)
593 return "bad types";
594 if (source->type == SYM_PTR) {
595 mod2 = 0;
596 as2 = 0;
598 mod2 |= source->ctype.modifiers;
599 as2 |= source->ctype.as;
601 if (target->type == SYM_ENUM) {
602 target = target->ctype.base_type;
603 if (!target)
604 return "bad types";
606 if (source->type == SYM_ENUM) {
607 source = source->ctype.base_type;
608 if (!source)
609 return "bad types";
612 if (target == source)
613 break;
614 if (!target || !source)
615 return "different types";
617 type1 = target->type;
618 base1 = target->ctype.base_type;
620 type2 = source->type;
621 base2 = source->ctype.base_type;
624 * Pointers to functions compare as the function itself
626 if (type1 == SYM_PTR && base1) {
627 base1 = examine_symbol_type(base1);
628 switch (base1->type) {
629 case SYM_FN:
630 type1 = SYM_FN;
631 target = base1;
632 base1 = base1->ctype.base_type;
633 default:
634 /* nothing */;
637 if (type2 == SYM_PTR && base2) {
638 base2 = examine_symbol_type(base2);
639 switch (base2->type) {
640 case SYM_FN:
641 type2 = SYM_FN;
642 source = base2;
643 base2 = base2->ctype.base_type;
644 default:
645 /* nothing */;
649 /* Arrays degenerate to pointers for type comparisons */
650 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
651 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
653 if (type1 != type2 || type1 == SYM_RESTRICT)
654 return "different base types";
656 /* Must be same address space to be comparable */
657 if (as1 != as2)
658 return "different address spaces";
660 /* Ignore differences in storage types or addressability */
661 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
662 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
663 if (diff) {
664 if (diff & MOD_SIZE)
665 return "different type sizes";
666 if (diff & ~MOD_SIGNEDNESS)
667 return "different modifiers";
669 /* Differs in signedness only.. */
670 if (Wtypesign) {
672 * Warn if both are explicitly signed ("unsigned" is obvously
673 * always explicit, and since we know one of them has to be
674 * unsigned, we check if the signed one was explicit).
676 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
677 return "different explicit signedness";
680 * "char" matches both "unsigned char" and "signed char",
681 * so if the explicit test didn't trigger, then we should
682 * not warn about a char.
684 if (!(mod1 & MOD_CHAR))
685 return "different signedness";
689 if (type1 == SYM_FN) {
690 int i;
691 struct symbol *arg1, *arg2;
692 if (base1->variadic != base2->variadic)
693 return "incompatible variadic arguments";
694 PREPARE_PTR_LIST(target->arguments, arg1);
695 PREPARE_PTR_LIST(source->arguments, arg2);
696 i = 1;
697 for (;;) {
698 const char *diff;
699 diff = type_difference(arg1, arg2, 0, 0);
700 if (diff) {
701 static char argdiff[80];
702 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
703 return argdiff;
705 if (!arg1)
706 break;
707 NEXT_PTR_LIST(arg1);
708 NEXT_PTR_LIST(arg2);
709 i++;
711 FINISH_PTR_LIST(arg2);
712 FINISH_PTR_LIST(arg1);
715 target = base1;
716 source = base2;
718 return NULL;
721 static int is_null_ptr(struct expression *expr)
723 if (expr->type != EXPR_VALUE || expr->value)
724 return 0;
725 if (!is_ptr_type(expr->ctype))
726 warning(expr->pos, "Using plain integer as NULL pointer");
727 return 1;
730 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
732 /* NULL expression? Just return the type of the "other side" */
733 if (is_null_ptr(r))
734 return l->ctype;
735 if (is_null_ptr(l))
736 return r->ctype;
737 return NULL;
741 * Ignore differences in "volatile" and "const"ness when
742 * subtracting pointers
744 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
746 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
748 const char *typediff;
749 struct symbol *ctype;
750 struct symbol *ltype, *rtype;
751 struct expression *r = *rp;
753 ltype = degenerate(l);
754 rtype = degenerate(r);
757 * If it is an integer subtract: the ptr add case will do the
758 * right thing.
760 if (!is_ptr_type(rtype))
761 return evaluate_ptr_add(expr, degenerate(l), rp);
763 ctype = ltype;
764 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
765 if (typediff) {
766 ctype = common_ptr_type(l, r);
767 if (!ctype) {
768 sparse_error(expr->pos, "subtraction of different types can't work (%s)", typediff);
769 return NULL;
772 examine_symbol_type(ctype);
774 /* Figure out the base type we point to */
775 if (ctype->type == SYM_NODE)
776 ctype = ctype->ctype.base_type;
777 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
778 sparse_error(expr->pos, "subtraction of functions? Share your drugs");
779 return NULL;
781 ctype = get_base_type(ctype);
783 expr->ctype = ssize_t_ctype;
784 if (ctype->bit_size > bits_in_char) {
785 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
786 struct expression *div = expr;
787 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
788 unsigned long value = ctype->bit_size >> 3;
790 val->ctype = size_t_ctype;
791 val->value = value;
793 if (value & (value-1)) {
794 if (Wptr_subtraction_blows)
795 warning(expr->pos, "potentially expensive pointer subtraction");
798 sub->op = '-';
799 sub->ctype = ssize_t_ctype;
800 sub->left = l;
801 sub->right = r;
803 div->op = '/';
804 div->left = sub;
805 div->right = val;
808 return ssize_t_ctype;
811 static struct symbol *evaluate_sub(struct expression *expr)
813 struct expression *left = expr->left;
814 struct symbol *ltype = left->ctype;
816 if (is_ptr_type(ltype))
817 return evaluate_ptr_sub(expr, left, &expr->right);
819 return evaluate_arith(expr, 1);
822 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
824 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
826 struct symbol *ctype;
828 if (!expr)
829 return NULL;
831 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
832 warning(expr->pos, "assignment expression in conditional");
834 ctype = evaluate_expression(expr);
835 if (ctype) {
836 if (is_safe_type(ctype))
837 warning(expr->pos, "testing a 'safe expression'");
840 return ctype;
843 static struct symbol *evaluate_logical(struct expression *expr)
845 if (!evaluate_conditional(expr->left, 0))
846 return NULL;
847 if (!evaluate_conditional(expr->right, 0))
848 return NULL;
850 expr->ctype = &bool_ctype;
851 return &bool_ctype;
854 static struct symbol *evaluate_shift(struct expression *expr)
856 struct expression *left = expr->left, *right = expr->right;
857 struct symbol *ltype = left->ctype, *rtype = right->ctype;
859 if (ltype->type == SYM_NODE)
860 ltype = ltype->ctype.base_type;
861 if (rtype->type == SYM_NODE)
862 rtype = rtype->ctype.base_type;
863 if (is_int_type(ltype) && is_int_type(rtype)) {
864 struct symbol *ctype = integer_promotion(ltype);
865 expr->left = cast_to(expr->left, ctype);
866 expr->ctype = ctype;
867 ctype = integer_promotion(rtype);
868 expr->right = cast_to(expr->right, ctype);
869 return expr->ctype;
871 return bad_expr_type(expr);
874 static struct symbol *evaluate_binop(struct expression *expr)
876 switch (expr->op) {
877 // addition can take ptr+int, fp and int
878 case '+':
879 return evaluate_add(expr);
881 // subtraction can take ptr-ptr, fp and int
882 case '-':
883 return evaluate_sub(expr);
885 // Arithmetic operations can take fp and int
886 case '*': case '/':
887 return evaluate_arith(expr, 1);
889 // shifts do integer promotions, but that's it.
890 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
891 return evaluate_shift(expr);
893 // The rest are integer operations
894 // '%', '&', '^', '|'
895 default:
896 return evaluate_arith(expr, 0);
900 static struct symbol *evaluate_comma(struct expression *expr)
902 expr->ctype = expr->right->ctype;
903 return expr->ctype;
906 static int modify_for_unsigned(int op)
908 if (op == '<')
909 op = SPECIAL_UNSIGNED_LT;
910 else if (op == '>')
911 op = SPECIAL_UNSIGNED_GT;
912 else if (op == SPECIAL_LTE)
913 op = SPECIAL_UNSIGNED_LTE;
914 else if (op == SPECIAL_GTE)
915 op = SPECIAL_UNSIGNED_GTE;
916 return op;
919 static struct symbol *evaluate_compare(struct expression *expr)
921 struct expression *left = expr->left, *right = expr->right;
922 struct symbol *ltype = left->ctype, *rtype = right->ctype;
923 struct symbol *ctype;
925 /* Type types? */
926 if (is_type_type(ltype) && is_type_type(rtype))
927 goto OK;
929 if (is_safe_type(ltype) || is_safe_type(rtype))
930 warning(expr->pos, "testing a 'safe expression'");
932 /* Pointer types? */
933 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
934 // FIXME! Check the types for compatibility
935 expr->op = modify_for_unsigned(expr->op);
936 goto OK;
939 ctype = compatible_integer_binop(&expr->left, &expr->right);
940 if (ctype) {
941 if (ctype->ctype.modifiers & MOD_UNSIGNED)
942 expr->op = modify_for_unsigned(expr->op);
943 goto OK;
946 ctype = compatible_float_binop(&expr->left, &expr->right);
947 if (ctype)
948 goto OK;
950 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
951 if (ctype) {
952 if (ctype->ctype.modifiers & MOD_UNSIGNED)
953 expr->op = modify_for_unsigned(expr->op);
954 goto OK;
957 bad_expr_type(expr);
960 expr->ctype = &bool_ctype;
961 return &bool_ctype;
965 * FIXME!! This should do casts, array degeneration etc..
967 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
969 struct symbol *ltype = left->ctype, *rtype = right->ctype;
971 if (ltype->type == SYM_NODE)
972 ltype = ltype->ctype.base_type;
974 if (rtype->type == SYM_NODE)
975 rtype = rtype->ctype.base_type;
977 if (ltype->type == SYM_PTR) {
978 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
979 return ltype;
982 if (rtype->type == SYM_PTR) {
983 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
984 return rtype;
986 return NULL;
990 * NOTE! The degenerate case of "x ? : y", where we don't
991 * have a true case, this will possibly promote "x" to the
992 * same type as "y", and thus _change_ the conditional
993 * test in the expression. But since promotion is "safe"
994 * for testing, that's ok.
996 static struct symbol *evaluate_conditional_expression(struct expression *expr)
998 struct expression **true;
999 struct symbol *ctype, *ltype, *rtype;
1000 const char * typediff;
1002 if (!evaluate_conditional(expr->conditional, 0))
1003 return NULL;
1004 if (!evaluate_expression(expr->cond_false))
1005 return NULL;
1007 ctype = degenerate(expr->conditional);
1008 rtype = degenerate(expr->cond_false);
1010 true = &expr->conditional;
1011 ltype = ctype;
1012 if (expr->cond_true) {
1013 if (!evaluate_expression(expr->cond_true))
1014 return NULL;
1015 ltype = degenerate(expr->cond_true);
1016 true = &expr->cond_true;
1019 ctype = compatible_integer_binop(true, &expr->cond_false);
1020 if (ctype)
1021 goto out;
1022 ctype = compatible_ptr_type(*true, expr->cond_false);
1023 if (ctype)
1024 goto out;
1025 ctype = compatible_float_binop(true, &expr->cond_false);
1026 if (ctype)
1027 goto out;
1028 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1029 if (ctype)
1030 goto out;
1031 ctype = ltype;
1032 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1033 if (!typediff)
1034 goto out;
1035 sparse_error(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1036 return NULL;
1038 out:
1039 expr->ctype = ctype;
1040 return ctype;
1043 /* FP assignments can not do modulo or bit operations */
1044 static int compatible_float_op(int op)
1046 return op == '=' ||
1047 op == SPECIAL_ADD_ASSIGN ||
1048 op == SPECIAL_SUB_ASSIGN ||
1049 op == SPECIAL_MUL_ASSIGN ||
1050 op == SPECIAL_DIV_ASSIGN;
1053 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1054 struct expression **rp, struct symbol *source, const char *where, int op)
1056 const char *typediff;
1057 struct symbol *t;
1058 int target_as;
1060 if (is_int_type(target)) {
1061 if (is_int_type(source))
1062 goto Cast;
1063 if (is_float_type(source))
1064 goto Cast;
1065 } else if (is_float_type(target)) {
1066 if (!compatible_float_op(op)) {
1067 sparse_error(expr->pos, "invalid assignment");
1068 return 0;
1070 if (is_int_type(source))
1071 goto Cast;
1072 if (is_float_type(source))
1073 goto Cast;
1074 } else if (is_restricted_type(target)) {
1075 if (restricted_binop(op, target)) {
1076 sparse_error(expr->pos, "bad restricted assignment");
1077 return 0;
1079 if (!restricted_value(*rp, target))
1080 return 1;
1081 } else if (is_ptr_type(target)) {
1082 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1083 evaluate_ptr_add(expr, target, rp);
1084 return 1;
1086 if (op != '=') {
1087 sparse_error(expr->pos, "invalid pointer assignment");
1088 return 0;
1090 } else if (op != '=') {
1091 sparse_error(expr->pos, "invalid assignment");
1092 return 0;
1095 /* It's ok if the target is more volatile or const than the source */
1096 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1097 if (!typediff)
1098 return 1;
1100 /* Pointer destination? */
1101 t = target;
1102 target_as = t->ctype.as;
1103 if (t->type == SYM_NODE) {
1104 t = t->ctype.base_type;
1105 target_as |= t->ctype.as;
1107 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1108 struct expression *right = *rp;
1109 struct symbol *s = source;
1110 int source_as;
1112 // NULL pointer is always ok
1113 if (is_null_ptr(right))
1114 goto Cast;
1116 /* "void *" matches anything as long as the address space is ok */
1117 source_as = s->ctype.as;
1118 if (s->type == SYM_NODE) {
1119 s = s->ctype.base_type;
1120 source_as |= s->ctype.as;
1122 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1123 s = get_base_type(s);
1124 t = get_base_type(t);
1125 if (s == &void_ctype || t == &void_ctype)
1126 goto Cast;
1130 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1131 info(expr->pos, " expected %s", show_typename(target));
1132 info(expr->pos, " got %s", show_typename(source));
1133 *rp = cast_to(*rp, target);
1134 return 0;
1135 Cast:
1136 *rp = cast_to(*rp, target);
1137 return 1;
1140 static void mark_assigned(struct expression *expr)
1142 struct symbol *sym;
1144 if (!expr)
1145 return;
1146 switch (expr->type) {
1147 case EXPR_SYMBOL:
1148 sym = expr->symbol;
1149 if (!sym)
1150 return;
1151 if (sym->type != SYM_NODE)
1152 return;
1153 sym->ctype.modifiers |= MOD_ASSIGNED;
1154 return;
1156 case EXPR_BINOP:
1157 mark_assigned(expr->left);
1158 mark_assigned(expr->right);
1159 return;
1160 case EXPR_CAST:
1161 mark_assigned(expr->cast_expression);
1162 return;
1163 case EXPR_SLICE:
1164 mark_assigned(expr->base);
1165 return;
1166 default:
1167 /* Hmm? */
1168 return;
1172 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1174 if (type->ctype.modifiers & MOD_CONST)
1175 sparse_error(left->pos, "assignment to const expression");
1177 /* We know left is an lvalue, so it's a "preop-*" */
1178 mark_assigned(left->unop);
1181 static struct symbol *evaluate_assignment(struct expression *expr)
1183 struct expression *left = expr->left, *right = expr->right;
1184 struct expression *where = expr;
1185 struct symbol *ltype, *rtype;
1187 if (!lvalue_expression(left)) {
1188 sparse_error(expr->pos, "not an lvalue");
1189 return NULL;
1192 ltype = left->ctype;
1194 rtype = degenerate(right);
1196 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1197 return NULL;
1199 evaluate_assign_to(left, ltype);
1201 expr->ctype = ltype;
1202 return ltype;
1205 static void examine_fn_arguments(struct symbol *fn)
1207 struct symbol *s;
1209 FOR_EACH_PTR(fn->arguments, s) {
1210 struct symbol *arg = evaluate_symbol(s);
1211 /* Array/function arguments silently degenerate into pointers */
1212 if (arg) {
1213 struct symbol *ptr;
1214 switch(arg->type) {
1215 case SYM_ARRAY:
1216 case SYM_FN:
1217 ptr = alloc_symbol(s->pos, SYM_PTR);
1218 if (arg->type == SYM_ARRAY)
1219 ptr->ctype = arg->ctype;
1220 else
1221 ptr->ctype.base_type = arg;
1222 ptr->ctype.as |= s->ctype.as;
1223 ptr->ctype.modifiers |= s->ctype.modifiers;
1225 s->ctype.base_type = ptr;
1226 s->ctype.as = 0;
1227 s->ctype.modifiers = 0;
1228 s->bit_size = 0;
1229 s->examined = 0;
1230 examine_symbol_type(s);
1231 break;
1232 default:
1233 /* nothing */
1234 break;
1237 } END_FOR_EACH_PTR(s);
1240 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1242 /* Take the modifiers of the pointer, and apply them to the member */
1243 mod |= sym->ctype.modifiers;
1244 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1245 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1246 *newsym = *sym;
1247 newsym->ctype.as = as;
1248 newsym->ctype.modifiers = mod;
1249 sym = newsym;
1251 return sym;
1254 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1256 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1257 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1259 node->ctype.base_type = ptr;
1260 ptr->bit_size = bits_in_pointer;
1261 ptr->ctype.alignment = pointer_alignment;
1263 node->bit_size = bits_in_pointer;
1264 node->ctype.alignment = pointer_alignment;
1266 access_symbol(sym);
1267 if (sym->ctype.modifiers & MOD_REGISTER) {
1268 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1269 sym->ctype.modifiers &= ~MOD_REGISTER;
1271 if (sym->type == SYM_NODE) {
1272 ptr->ctype.as |= sym->ctype.as;
1273 ptr->ctype.modifiers |= sym->ctype.modifiers;
1274 sym = sym->ctype.base_type;
1276 if (degenerate && sym->type == SYM_ARRAY) {
1277 ptr->ctype.as |= sym->ctype.as;
1278 ptr->ctype.modifiers |= sym->ctype.modifiers;
1279 sym = sym->ctype.base_type;
1281 ptr->ctype.base_type = sym;
1283 return node;
1286 /* Arrays degenerate into pointers on pointer arithmetic */
1287 static struct symbol *degenerate(struct expression *expr)
1289 struct symbol *ctype, *base;
1291 if (!expr)
1292 return NULL;
1293 ctype = expr->ctype;
1294 if (!ctype)
1295 return NULL;
1296 base = examine_symbol_type(ctype);
1297 if (ctype->type == SYM_NODE)
1298 base = ctype->ctype.base_type;
1300 * Arrays degenerate into pointers to the entries, while
1301 * functions degenerate into pointers to themselves.
1302 * If array was part of non-lvalue compound, we create a copy
1303 * of that compound first and then act as if we were dealing with
1304 * the corresponding field in there.
1306 switch (base->type) {
1307 case SYM_ARRAY:
1308 if (expr->type == EXPR_SLICE) {
1309 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1310 struct expression *e0, *e1, *e2, *e3, *e4;
1312 a->ctype.base_type = expr->base->ctype;
1313 a->bit_size = expr->base->ctype->bit_size;
1314 a->array_size = expr->base->ctype->array_size;
1316 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1317 e0->symbol = a;
1318 e0->ctype = &lazy_ptr_ctype;
1320 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1321 e1->unop = e0;
1322 e1->op = '*';
1323 e1->ctype = expr->base->ctype; /* XXX */
1325 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1326 e2->left = e1;
1327 e2->right = expr->base;
1328 e2->op = '=';
1329 e2->ctype = expr->base->ctype;
1331 if (expr->r_bitpos) {
1332 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1333 e3->op = '+';
1334 e3->left = e0;
1335 e3->right = alloc_const_expression(expr->pos,
1336 expr->r_bitpos >> 3);
1337 e3->ctype = &lazy_ptr_ctype;
1338 } else {
1339 e3 = e0;
1342 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1343 e4->left = e2;
1344 e4->right = e3;
1345 e4->ctype = &lazy_ptr_ctype;
1347 expr->unop = e4;
1348 expr->type = EXPR_PREOP;
1349 expr->op = '*';
1351 case SYM_FN:
1352 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1353 sparse_error(expr->pos, "strange non-value function or array");
1354 return &bad_ctype;
1356 *expr = *expr->unop;
1357 ctype = create_pointer(expr, ctype, 1);
1358 expr->ctype = ctype;
1359 default:
1360 /* nothing */;
1362 return ctype;
1365 static struct symbol *evaluate_addressof(struct expression *expr)
1367 struct expression *op = expr->unop;
1368 struct symbol *ctype;
1370 if (op->op != '*' || op->type != EXPR_PREOP) {
1371 sparse_error(expr->pos, "not addressable");
1372 return NULL;
1374 ctype = op->ctype;
1375 *expr = *op->unop;
1377 if (expr->type == EXPR_SYMBOL) {
1378 struct symbol *sym = expr->symbol;
1379 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1383 * symbol expression evaluation is lazy about the type
1384 * of the sub-expression, so we may have to generate
1385 * the type here if so..
1387 if (expr->ctype == &lazy_ptr_ctype) {
1388 ctype = create_pointer(expr, ctype, 0);
1389 expr->ctype = ctype;
1391 return expr->ctype;
1395 static struct symbol *evaluate_dereference(struct expression *expr)
1397 struct expression *op = expr->unop;
1398 struct symbol *ctype = op->ctype, *node, *target;
1400 /* Simplify: *&(expr) => (expr) */
1401 if (op->type == EXPR_PREOP && op->op == '&') {
1402 *expr = *op->unop;
1403 return expr->ctype;
1406 /* Dereferencing a node drops all the node information. */
1407 if (ctype->type == SYM_NODE)
1408 ctype = ctype->ctype.base_type;
1410 node = alloc_symbol(expr->pos, SYM_NODE);
1411 target = ctype->ctype.base_type;
1413 switch (ctype->type) {
1414 default:
1415 sparse_error(expr->pos, "cannot derefence this type");
1416 return NULL;
1417 case SYM_PTR:
1418 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1419 merge_type(node, ctype);
1420 break;
1422 case SYM_ARRAY:
1423 if (!lvalue_expression(op)) {
1424 sparse_error(op->pos, "non-lvalue array??");
1425 return NULL;
1428 /* Do the implied "addressof" on the array */
1429 *op = *op->unop;
1432 * When an array is dereferenced, we need to pick
1433 * up the attributes of the original node too..
1435 merge_type(node, op->ctype);
1436 merge_type(node, ctype);
1437 break;
1440 node->bit_size = target->bit_size;
1441 node->array_size = target->array_size;
1443 expr->ctype = node;
1444 return node;
1448 * Unary post-ops: x++ and x--
1450 static struct symbol *evaluate_postop(struct expression *expr)
1452 struct expression *op = expr->unop;
1453 struct symbol *ctype = op->ctype;
1455 if (!lvalue_expression(expr->unop)) {
1456 sparse_error(expr->pos, "need lvalue expression for ++/--");
1457 return NULL;
1459 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1460 sparse_error(expr->pos, "bad operation on restricted");
1461 return NULL;
1464 evaluate_assign_to(op, ctype);
1466 expr->ctype = ctype;
1467 expr->op_value = 1;
1468 if (is_ptr_type(ctype))
1469 expr->op_value = ptr_object_size(ctype) >> 3;
1471 return ctype;
1474 static struct symbol *evaluate_sign(struct expression *expr)
1476 struct symbol *ctype = expr->unop->ctype;
1477 if (is_int_type(ctype)) {
1478 struct symbol *rtype = rtype = integer_promotion(ctype);
1479 expr->unop = cast_to(expr->unop, rtype);
1480 ctype = rtype;
1481 } else if (is_float_type(ctype) && expr->op != '~') {
1482 /* no conversions needed */
1483 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1484 /* no conversions needed */
1485 } else {
1486 return bad_expr_type(expr);
1488 if (expr->op == '+')
1489 *expr = *expr->unop;
1490 expr->ctype = ctype;
1491 return ctype;
1494 static struct symbol *evaluate_preop(struct expression *expr)
1496 struct symbol *ctype = expr->unop->ctype;
1498 switch (expr->op) {
1499 case '(':
1500 *expr = *expr->unop;
1501 return ctype;
1503 case '+':
1504 case '-':
1505 case '~':
1506 return evaluate_sign(expr);
1508 case '*':
1509 return evaluate_dereference(expr);
1511 case '&':
1512 return evaluate_addressof(expr);
1514 case SPECIAL_INCREMENT:
1515 case SPECIAL_DECREMENT:
1517 * From a type evaluation standpoint the pre-ops are
1518 * the same as the postops
1520 return evaluate_postop(expr);
1522 case '!':
1523 if (is_safe_type(ctype))
1524 warning(expr->pos, "testing a 'safe expression'");
1525 if (is_float_type(ctype)) {
1526 struct expression *arg = expr->unop;
1527 expr->type = EXPR_BINOP;
1528 expr->op = SPECIAL_EQUAL;
1529 expr->left = arg;
1530 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1531 expr->right->ctype = ctype;
1532 expr->right->fvalue = 0;
1534 ctype = &bool_ctype;
1535 break;
1537 default:
1538 break;
1540 expr->ctype = ctype;
1541 return &bool_ctype;
1544 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1546 struct ptr_list *head = (struct ptr_list *)_list;
1547 struct ptr_list *list = head;
1549 if (!head)
1550 return NULL;
1551 do {
1552 int i;
1553 for (i = 0; i < list->nr; i++) {
1554 struct symbol *sym = (struct symbol *) list->list[i];
1555 if (sym->ident) {
1556 if (sym->ident != ident)
1557 continue;
1558 *offset = sym->offset;
1559 return sym;
1560 } else {
1561 struct symbol *ctype = sym->ctype.base_type;
1562 struct symbol *sub;
1563 if (!ctype)
1564 continue;
1565 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1566 continue;
1567 sub = find_identifier(ident, ctype->symbol_list, offset);
1568 if (!sub)
1569 continue;
1570 *offset += sym->offset;
1571 return sub;
1574 } while ((list = list->next) != head);
1575 return NULL;
1578 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1580 struct expression *add;
1583 * Create a new add-expression
1585 * NOTE! Even if we just add zero, we need a new node
1586 * for the member pointer, since it has a different
1587 * type than the original pointer. We could make that
1588 * be just a cast, but the fact is, a node is a node,
1589 * so we might as well just do the "add zero" here.
1591 add = alloc_expression(expr->pos, EXPR_BINOP);
1592 add->op = '+';
1593 add->left = expr;
1594 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1595 add->right->ctype = &int_ctype;
1596 add->right->value = offset;
1599 * The ctype of the pointer will be lazily evaluated if
1600 * we ever take the address of this member dereference..
1602 add->ctype = &lazy_ptr_ctype;
1603 return add;
1606 /* structure/union dereference */
1607 static struct symbol *evaluate_member_dereference(struct expression *expr)
1609 int offset;
1610 struct symbol *ctype, *member;
1611 struct expression *deref = expr->deref, *add;
1612 struct ident *ident = expr->member;
1613 unsigned int mod;
1614 int address_space;
1616 if (!evaluate_expression(deref))
1617 return NULL;
1618 if (!ident) {
1619 sparse_error(expr->pos, "bad member name");
1620 return NULL;
1623 ctype = deref->ctype;
1624 address_space = ctype->ctype.as;
1625 mod = ctype->ctype.modifiers;
1626 if (ctype->type == SYM_NODE) {
1627 ctype = ctype->ctype.base_type;
1628 address_space |= ctype->ctype.as;
1629 mod |= ctype->ctype.modifiers;
1631 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1632 sparse_error(expr->pos, "expected structure or union");
1633 return NULL;
1635 offset = 0;
1636 member = find_identifier(ident, ctype->symbol_list, &offset);
1637 if (!member) {
1638 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1639 const char *name = "<unnamed>";
1640 int namelen = 9;
1641 if (ctype->ident) {
1642 name = ctype->ident->name;
1643 namelen = ctype->ident->len;
1645 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1646 show_ident(ident), type, namelen, name);
1647 return NULL;
1651 * The member needs to take on the address space and modifiers of
1652 * the "parent" type.
1654 member = convert_to_as_mod(member, address_space, mod);
1655 ctype = get_base_type(member);
1657 if (!lvalue_expression(deref)) {
1658 if (deref->type != EXPR_SLICE) {
1659 expr->base = deref;
1660 expr->r_bitpos = 0;
1661 } else {
1662 expr->base = deref->base;
1663 expr->r_bitpos = deref->r_bitpos;
1665 expr->r_bitpos += offset << 3;
1666 expr->type = EXPR_SLICE;
1667 expr->r_nrbits = member->bit_size;
1668 expr->r_bitpos += member->bit_offset;
1669 expr->ctype = member;
1670 return member;
1673 deref = deref->unop;
1674 expr->deref = deref;
1676 add = evaluate_offset(deref, offset);
1677 expr->type = EXPR_PREOP;
1678 expr->op = '*';
1679 expr->unop = add;
1681 expr->ctype = member;
1682 return member;
1685 static int is_promoted(struct expression *expr)
1687 while (1) {
1688 switch (expr->type) {
1689 case EXPR_BINOP:
1690 case EXPR_SELECT:
1691 case EXPR_CONDITIONAL:
1692 return 1;
1693 case EXPR_COMMA:
1694 expr = expr->right;
1695 continue;
1696 case EXPR_PREOP:
1697 switch (expr->op) {
1698 case '(':
1699 expr = expr->unop;
1700 continue;
1701 case '+':
1702 case '-':
1703 case '~':
1704 return 1;
1705 default:
1706 return 0;
1708 default:
1709 return 0;
1715 static struct symbol *evaluate_cast(struct expression *);
1717 static struct symbol *evaluate_type_information(struct expression *expr)
1719 struct symbol *sym = expr->cast_type;
1720 if (!sym) {
1721 sym = evaluate_expression(expr->cast_expression);
1722 if (!sym)
1723 return NULL;
1725 * Expressions of restricted types will possibly get
1726 * promoted - check that here
1728 if (is_restricted_type(sym)) {
1729 if (sym->bit_size < bits_in_int && is_promoted(expr))
1730 sym = &int_ctype;
1733 examine_symbol_type(sym);
1734 if (is_bitfield_type(sym)) {
1735 sparse_error(expr->pos, "trying to examine bitfield type");
1736 return NULL;
1738 return sym;
1741 static struct symbol *evaluate_sizeof(struct expression *expr)
1743 struct symbol *type;
1744 int size;
1746 type = evaluate_type_information(expr);
1747 if (!type)
1748 return NULL;
1750 size = type->bit_size;
1751 if ((size < 0) || (size & 7))
1752 sparse_error(expr->pos, "cannot size expression");
1753 expr->type = EXPR_VALUE;
1754 expr->value = size >> 3;
1755 expr->ctype = size_t_ctype;
1756 return size_t_ctype;
1759 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1761 struct symbol *type;
1762 int size;
1764 type = evaluate_type_information(expr);
1765 if (!type)
1766 return NULL;
1768 if (type->type == SYM_NODE)
1769 type = type->ctype.base_type;
1770 if (!type)
1771 return NULL;
1772 switch (type->type) {
1773 case SYM_ARRAY:
1774 break;
1775 case SYM_PTR:
1776 type = get_base_type(type);
1777 if (type)
1778 break;
1779 default:
1780 sparse_error(expr->pos, "expected pointer expression");
1781 return NULL;
1783 size = type->bit_size;
1784 if (size & 7)
1785 size = 0;
1786 expr->type = EXPR_VALUE;
1787 expr->value = size >> 3;
1788 expr->ctype = size_t_ctype;
1789 return size_t_ctype;
1792 static struct symbol *evaluate_alignof(struct expression *expr)
1794 struct symbol *type;
1796 type = evaluate_type_information(expr);
1797 if (!type)
1798 return NULL;
1800 expr->type = EXPR_VALUE;
1801 expr->value = type->ctype.alignment;
1802 expr->ctype = size_t_ctype;
1803 return size_t_ctype;
1806 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1808 struct expression *expr;
1809 struct symbol_list *argument_types = fn->arguments;
1810 struct symbol *argtype;
1811 int i = 1;
1813 PREPARE_PTR_LIST(argument_types, argtype);
1814 FOR_EACH_PTR (head, expr) {
1815 struct expression **p = THIS_ADDRESS(expr);
1816 struct symbol *ctype, *target;
1817 ctype = evaluate_expression(expr);
1819 if (!ctype)
1820 return 0;
1822 ctype = degenerate(expr);
1824 target = argtype;
1825 if (!target && ctype->bit_size < bits_in_int)
1826 target = &int_ctype;
1827 if (target) {
1828 static char where[30];
1829 examine_symbol_type(target);
1830 sprintf(where, "argument %d", i);
1831 compatible_assignment_types(expr, target, p, ctype, where, '=');
1834 i++;
1835 NEXT_PTR_LIST(argtype);
1836 } END_FOR_EACH_PTR(expr);
1837 FINISH_PTR_LIST(argtype);
1838 return 1;
1841 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1843 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1845 struct expression *entry = *ep;
1846 struct expression **parent, *reuse = NULL;
1847 unsigned long offset;
1848 struct symbol *sym;
1849 unsigned long from, to;
1850 int accept_string = is_byte_type(ctype);
1852 from = current;
1853 to = from+1;
1854 parent = ep;
1855 if (entry->type == EXPR_INDEX) {
1856 from = entry->idx_from;
1857 to = entry->idx_to+1;
1858 parent = &entry->idx_expression;
1859 reuse = entry;
1860 entry = entry->idx_expression;
1863 offset = from * (ctype->bit_size>>3);
1864 if (offset) {
1865 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1866 reuse->type = EXPR_POS;
1867 reuse->ctype = ctype;
1868 reuse->init_offset = offset;
1869 reuse->init_nr = to - from;
1870 reuse->init_expr = entry;
1871 parent = &reuse->init_expr;
1872 entry = reuse;
1874 *ep = entry;
1876 if (accept_string && entry->type == EXPR_STRING) {
1877 sym = evaluate_expression(entry);
1878 to = from + get_expression_value(sym->array_size);
1879 } else {
1880 evaluate_initializer(ctype, parent);
1882 return to;
1885 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1887 struct expression *entry;
1888 int current = 0;
1890 FOR_EACH_PTR(expr->expr_list, entry) {
1891 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1892 } END_FOR_EACH_PTR(entry);
1895 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1896 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1898 if (expression_list_size(expr->expr_list) != 1) {
1899 sparse_error(expr->pos, "unexpected compound initializer");
1900 return;
1902 evaluate_array_initializer(ctype, expr);
1903 return;
1906 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1908 struct symbol *sym;
1910 FOR_EACH_PTR(ctype->symbol_list, sym) {
1911 if (sym->ident == ident)
1912 return sym;
1913 } END_FOR_EACH_PTR(sym);
1914 return NULL;
1917 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1919 struct expression *entry = *ep;
1920 struct expression **parent;
1921 struct expression *reuse = NULL;
1922 unsigned long offset;
1924 if (!sym) {
1925 sparse_error(entry->pos, "unknown named initializer");
1926 return -1;
1929 if (entry->type == EXPR_IDENTIFIER) {
1930 reuse = entry;
1931 entry = entry->ident_expression;
1934 parent = ep;
1935 offset = sym->offset;
1936 if (offset) {
1937 if (!reuse)
1938 reuse = alloc_expression(entry->pos, EXPR_POS);
1939 reuse->type = EXPR_POS;
1940 reuse->ctype = sym;
1941 reuse->init_offset = offset;
1942 reuse->init_nr = 1;
1943 reuse->init_expr = entry;
1944 parent = &reuse->init_expr;
1945 entry = reuse;
1947 *ep = entry;
1948 evaluate_initializer(sym, parent);
1949 return 0;
1952 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1954 struct expression *entry;
1955 struct symbol *sym;
1957 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1958 FOR_EACH_PTR(expr->expr_list, entry) {
1959 if (entry->type == EXPR_IDENTIFIER) {
1960 struct ident *ident = entry->expr_ident;
1961 /* We special-case the "already right place" case */
1962 if (!sym || sym->ident != ident) {
1963 RESET_PTR_LIST(sym);
1964 for (;;) {
1965 if (!sym)
1966 break;
1967 if (sym->ident == ident)
1968 break;
1969 NEXT_PTR_LIST(sym);
1973 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1974 return;
1975 NEXT_PTR_LIST(sym);
1976 } END_FOR_EACH_PTR(entry);
1977 FINISH_PTR_LIST(sym);
1981 * Initializers are kind of like assignments. Except
1982 * they can be a hell of a lot more complex.
1984 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1986 struct expression *expr = *ep;
1989 * Simple non-structure/array initializers are the simple
1990 * case, and look (and parse) largely like assignments.
1992 switch (expr->type) {
1993 default: {
1994 int is_string = expr->type == EXPR_STRING;
1995 struct symbol *rtype = evaluate_expression(expr);
1996 if (rtype) {
1998 * Special case:
1999 * char array[] = "string"
2000 * should _not_ degenerate.
2002 if (!is_string || !is_string_type(ctype))
2003 rtype = degenerate(expr);
2004 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2006 return;
2009 case EXPR_INITIALIZER:
2010 expr->ctype = ctype;
2011 if (ctype->type == SYM_NODE)
2012 ctype = ctype->ctype.base_type;
2014 switch (ctype->type) {
2015 case SYM_ARRAY:
2016 case SYM_PTR:
2017 evaluate_array_initializer(get_base_type(ctype), expr);
2018 return;
2019 case SYM_UNION:
2020 evaluate_struct_or_union_initializer(ctype, expr, 0);
2021 return;
2022 case SYM_STRUCT:
2023 evaluate_struct_or_union_initializer(ctype, expr, 1);
2024 return;
2025 default:
2026 evaluate_scalar_initializer(ctype, expr);
2027 return;
2030 case EXPR_IDENTIFIER:
2031 if (ctype->type == SYM_NODE)
2032 ctype = ctype->ctype.base_type;
2033 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2034 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2035 show_symbol(ctype);
2036 return;
2038 evaluate_one_struct_initializer(ctype, ep,
2039 find_struct_ident(ctype, expr->expr_ident));
2040 return;
2042 case EXPR_INDEX:
2043 if (ctype->type == SYM_NODE)
2044 ctype = ctype->ctype.base_type;
2045 if (ctype->type != SYM_ARRAY) {
2046 sparse_error(expr->pos, "expected array");
2047 return;
2049 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2050 return;
2052 case EXPR_POS:
2054 * An EXPR_POS expression has already been evaluated, and we don't
2055 * need to do anything more
2057 return;
2061 static int get_as(struct symbol *sym)
2063 int as;
2064 unsigned long mod;
2066 if (!sym)
2067 return 0;
2068 as = sym->ctype.as;
2069 mod = sym->ctype.modifiers;
2070 if (sym->type == SYM_NODE) {
2071 sym = sym->ctype.base_type;
2072 as |= sym->ctype.as;
2073 mod |= sym->ctype.modifiers;
2077 * At least for now, allow casting to a "unsigned long".
2078 * That's how we do things like pointer arithmetic and
2079 * store pointers to registers.
2081 if (sym == &ulong_ctype)
2082 return -1;
2084 if (sym && sym->type == SYM_PTR) {
2085 sym = get_base_type(sym);
2086 as |= sym->ctype.as;
2087 mod |= sym->ctype.modifiers;
2089 if (mod & MOD_FORCE)
2090 return -1;
2091 return as;
2094 static void cast_to_as(struct expression *e, int as)
2096 struct expression *v = e->cast_expression;
2098 if (!Wcast_to_address_space)
2099 return;
2101 /* cast from constant 0 to pointer is OK */
2102 if (v->type == EXPR_VALUE && is_int_type(v->ctype) && !v->value)
2103 return;
2105 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2108 static struct symbol *evaluate_cast(struct expression *expr)
2110 struct expression *target = expr->cast_expression;
2111 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2112 struct symbol *t1, *t2;
2113 enum type type1, type2;
2114 int as1, as2;
2116 if (!target)
2117 return NULL;
2119 expr->ctype = ctype;
2120 expr->cast_type = ctype;
2123 * Special case: a cast can be followed by an
2124 * initializer, in which case we need to pass
2125 * the type value down to that initializer rather
2126 * than trying to evaluate it as an expression
2128 * A more complex case is when the initializer is
2129 * dereferenced as part of a post-fix expression.
2130 * We need to produce an expression that can be dereferenced.
2132 if (target->type == EXPR_INITIALIZER) {
2133 struct symbol *sym = expr->cast_type;
2134 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2136 sym->initializer = expr->cast_expression;
2137 evaluate_symbol(sym);
2139 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2140 addr->symbol = sym;
2142 expr->type = EXPR_PREOP;
2143 expr->op = '*';
2144 expr->unop = addr;
2145 expr->ctype = sym;
2147 return sym;
2150 evaluate_expression(target);
2151 degenerate(target);
2153 t1 = ctype;
2154 if (t1->type == SYM_NODE)
2155 t1 = t1->ctype.base_type;
2156 if (t1->type == SYM_ENUM)
2157 t1 = t1->ctype.base_type;
2160 * You can always throw a value away by casting to
2161 * "void" - that's an implicit "force". Note that
2162 * the same is _not_ true of "void *".
2164 if (t1 == &void_ctype)
2165 goto out;
2167 type1 = t1->type;
2168 if (type1 == SYM_ARRAY || type1 == SYM_UNION || type1 == SYM_STRUCT)
2169 warning(expr->pos, "cast to non-scalar");
2171 t2 = target->ctype;
2172 if (!t2) {
2173 sparse_error(expr->pos, "cast from unknown type");
2174 goto out;
2176 if (t2->type == SYM_NODE)
2177 t2 = t2->ctype.base_type;
2178 if (t2->type == SYM_ENUM)
2179 t2 = t2->ctype.base_type;
2181 type2 = t2->type;
2182 if (type2 == SYM_ARRAY || type2 == SYM_UNION || type2 == SYM_STRUCT)
2183 warning(expr->pos, "cast from non-scalar");
2185 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2186 if (t1->type == SYM_RESTRICT)
2187 warning(expr->pos, "cast to restricted type");
2188 if (t2->type == SYM_RESTRICT)
2189 warning(expr->pos, "cast from restricted type");
2192 as1 = get_as(ctype);
2193 as2 = get_as(target->ctype);
2194 if (!as1 && as2 > 0)
2195 warning(expr->pos, "cast removes address space of expression");
2196 if (as1 > 0 && as2 > 0 && as1 != as2)
2197 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2198 if (as1 > 0 && !as2)
2199 cast_to_as(expr, as1);
2202 * Casts of constant values are special: they
2203 * can be NULL, and thus need to be simplified
2204 * early.
2206 if (target->type == EXPR_VALUE)
2207 cast_value(expr, ctype, target, target->ctype);
2209 out:
2210 return ctype;
2214 * Evaluate a call expression with a symbol. This
2215 * should expand inline functions, and evaluate
2216 * builtins.
2218 static int evaluate_symbol_call(struct expression *expr)
2220 struct expression *fn = expr->fn;
2221 struct symbol *ctype = fn->ctype;
2223 if (fn->type != EXPR_PREOP)
2224 return 0;
2226 if (ctype->op && ctype->op->evaluate)
2227 return ctype->op->evaluate(expr);
2229 if (ctype->ctype.modifiers & MOD_INLINE) {
2230 int ret;
2231 struct symbol *curr = current_fn;
2232 current_fn = ctype->ctype.base_type;
2233 examine_fn_arguments(current_fn);
2235 ret = inline_function(expr, ctype);
2237 /* restore the old function */
2238 current_fn = curr;
2239 return ret;
2242 return 0;
2245 static struct symbol *evaluate_call(struct expression *expr)
2247 int args, fnargs;
2248 struct symbol *ctype, *sym;
2249 struct expression *fn = expr->fn;
2250 struct expression_list *arglist = expr->args;
2252 if (!evaluate_expression(fn))
2253 return NULL;
2254 sym = ctype = fn->ctype;
2255 if (ctype->type == SYM_NODE)
2256 ctype = ctype->ctype.base_type;
2257 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2258 ctype = get_base_type(ctype);
2259 if (!evaluate_arguments(sym, ctype, arglist))
2260 return NULL;
2261 if (ctype->type != SYM_FN) {
2262 sparse_error(expr->pos, "not a function %s", show_ident(sym->ident));
2263 return NULL;
2265 args = expression_list_size(expr->args);
2266 fnargs = symbol_list_size(ctype->arguments);
2267 if (args < fnargs)
2268 sparse_error(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2269 if (args > fnargs && !ctype->variadic)
2270 sparse_error(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2271 if (sym->type == SYM_NODE) {
2272 if (evaluate_symbol_call(expr))
2273 return expr->ctype;
2275 expr->ctype = ctype->ctype.base_type;
2276 return expr->ctype;
2279 struct symbol *evaluate_expression(struct expression *expr)
2281 if (!expr)
2282 return NULL;
2283 if (expr->ctype)
2284 return expr->ctype;
2286 switch (expr->type) {
2287 case EXPR_VALUE:
2288 case EXPR_FVALUE:
2289 sparse_error(expr->pos, "value expression without a type");
2290 return NULL;
2291 case EXPR_STRING:
2292 return evaluate_string(expr);
2293 case EXPR_SYMBOL:
2294 return evaluate_symbol_expression(expr);
2295 case EXPR_BINOP:
2296 if (!evaluate_expression(expr->left))
2297 return NULL;
2298 if (!evaluate_expression(expr->right))
2299 return NULL;
2300 return evaluate_binop(expr);
2301 case EXPR_LOGICAL:
2302 return evaluate_logical(expr);
2303 case EXPR_COMMA:
2304 evaluate_expression(expr->left);
2305 if (!evaluate_expression(expr->right))
2306 return NULL;
2307 return evaluate_comma(expr);
2308 case EXPR_COMPARE:
2309 if (!evaluate_expression(expr->left))
2310 return NULL;
2311 if (!evaluate_expression(expr->right))
2312 return NULL;
2313 return evaluate_compare(expr);
2314 case EXPR_ASSIGNMENT:
2315 if (!evaluate_expression(expr->left))
2316 return NULL;
2317 if (!evaluate_expression(expr->right))
2318 return NULL;
2319 return evaluate_assignment(expr);
2320 case EXPR_PREOP:
2321 if (!evaluate_expression(expr->unop))
2322 return NULL;
2323 return evaluate_preop(expr);
2324 case EXPR_POSTOP:
2325 if (!evaluate_expression(expr->unop))
2326 return NULL;
2327 return evaluate_postop(expr);
2328 case EXPR_CAST:
2329 case EXPR_IMPLIED_CAST:
2330 return evaluate_cast(expr);
2331 case EXPR_SIZEOF:
2332 return evaluate_sizeof(expr);
2333 case EXPR_PTRSIZEOF:
2334 return evaluate_ptrsizeof(expr);
2335 case EXPR_ALIGNOF:
2336 return evaluate_alignof(expr);
2337 case EXPR_DEREF:
2338 return evaluate_member_dereference(expr);
2339 case EXPR_CALL:
2340 return evaluate_call(expr);
2341 case EXPR_SELECT:
2342 case EXPR_CONDITIONAL:
2343 return evaluate_conditional_expression(expr);
2344 case EXPR_STATEMENT:
2345 expr->ctype = evaluate_statement(expr->statement);
2346 return expr->ctype;
2348 case EXPR_LABEL:
2349 expr->ctype = &ptr_ctype;
2350 return &ptr_ctype;
2352 case EXPR_TYPE:
2353 /* Evaluate the type of the symbol .. */
2354 evaluate_symbol(expr->symbol);
2355 /* .. but the type of the _expression_ is a "type" */
2356 expr->ctype = &type_ctype;
2357 return &type_ctype;
2359 /* These can not exist as stand-alone expressions */
2360 case EXPR_INITIALIZER:
2361 case EXPR_IDENTIFIER:
2362 case EXPR_INDEX:
2363 case EXPR_POS:
2364 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2365 return NULL;
2366 case EXPR_SLICE:
2367 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2368 return NULL;
2370 return NULL;
2373 static void check_duplicates(struct symbol *sym)
2375 int declared = 0;
2376 struct symbol *next = sym;
2378 while ((next = next->same_symbol) != NULL) {
2379 const char *typediff;
2380 evaluate_symbol(next);
2381 declared++;
2382 typediff = type_difference(sym, next, 0, 0);
2383 if (typediff) {
2384 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2385 show_ident(sym->ident),
2386 stream_name(next->pos.stream), next->pos.line, typediff);
2387 return;
2390 if (!declared) {
2391 unsigned long mod = sym->ctype.modifiers;
2392 if (mod & (MOD_STATIC | MOD_REGISTER))
2393 return;
2394 if (!(mod & MOD_TOPLEVEL))
2395 return;
2396 if (sym->ident == &main_ident)
2397 return;
2398 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2402 static struct symbol *evaluate_symbol(struct symbol *sym)
2404 struct symbol *base_type;
2406 if (!sym)
2407 return sym;
2408 if (sym->evaluated)
2409 return sym;
2410 sym->evaluated = 1;
2412 sym = examine_symbol_type(sym);
2413 base_type = get_base_type(sym);
2414 if (!base_type)
2415 return NULL;
2417 /* Evaluate the initializers */
2418 if (sym->initializer)
2419 evaluate_initializer(sym, &sym->initializer);
2421 /* And finally, evaluate the body of the symbol too */
2422 if (base_type->type == SYM_FN) {
2423 struct symbol *curr = current_fn;
2425 current_fn = base_type;
2427 examine_fn_arguments(base_type);
2428 if (!base_type->stmt && base_type->inline_stmt)
2429 uninline(sym);
2430 if (base_type->stmt)
2431 evaluate_statement(base_type->stmt);
2433 current_fn = curr;
2436 return base_type;
2439 void evaluate_symbol_list(struct symbol_list *list)
2441 struct symbol *sym;
2443 FOR_EACH_PTR(list, sym) {
2444 check_duplicates(sym);
2445 evaluate_symbol(sym);
2446 } END_FOR_EACH_PTR(sym);
2449 static struct symbol *evaluate_return_expression(struct statement *stmt)
2451 struct expression *expr = stmt->expression;
2452 struct symbol *ctype, *fntype;
2454 evaluate_expression(expr);
2455 ctype = degenerate(expr);
2456 fntype = current_fn->ctype.base_type;
2457 if (!fntype || fntype == &void_ctype) {
2458 if (expr && ctype != &void_ctype)
2459 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2460 return NULL;
2463 if (!expr) {
2464 sparse_error(stmt->pos, "return with no return value");
2465 return NULL;
2467 if (!ctype)
2468 return NULL;
2469 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2470 return NULL;
2473 static void evaluate_if_statement(struct statement *stmt)
2475 if (!stmt->if_conditional)
2476 return;
2478 evaluate_conditional(stmt->if_conditional, 0);
2479 evaluate_statement(stmt->if_true);
2480 evaluate_statement(stmt->if_false);
2483 static void evaluate_iterator(struct statement *stmt)
2485 evaluate_conditional(stmt->iterator_pre_condition, 1);
2486 evaluate_conditional(stmt->iterator_post_condition,1);
2487 evaluate_statement(stmt->iterator_pre_statement);
2488 evaluate_statement(stmt->iterator_statement);
2489 evaluate_statement(stmt->iterator_post_statement);
2492 static void verify_output_constraint(struct expression *expr, const char *constraint)
2494 switch (*constraint) {
2495 case '=': /* Assignment */
2496 case '+': /* Update */
2497 break;
2498 default:
2499 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2503 static void verify_input_constraint(struct expression *expr, const char *constraint)
2505 switch (*constraint) {
2506 case '=': /* Assignment */
2507 case '+': /* Update */
2508 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2512 static void evaluate_asm_statement(struct statement *stmt)
2514 struct expression *expr;
2515 int state;
2517 expr = stmt->asm_string;
2518 if (!expr || expr->type != EXPR_STRING) {
2519 sparse_error(stmt->pos, "need constant string for inline asm");
2520 return;
2523 state = 0;
2524 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2525 struct ident *ident;
2527 switch (state) {
2528 case 0: /* Identifier */
2529 state = 1;
2530 ident = (struct ident *)expr;
2531 continue;
2533 case 1: /* Constraint */
2534 state = 2;
2535 if (!expr || expr->type != EXPR_STRING) {
2536 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2537 *THIS_ADDRESS(expr) = NULL;
2538 continue;
2540 verify_output_constraint(expr, expr->string->data);
2541 continue;
2543 case 2: /* Expression */
2544 state = 0;
2545 if (!evaluate_expression(expr))
2546 return;
2547 if (!lvalue_expression(expr))
2548 warning(expr->pos, "asm output is not an lvalue");
2549 evaluate_assign_to(expr, expr->ctype);
2550 continue;
2552 } END_FOR_EACH_PTR(expr);
2554 state = 0;
2555 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2556 struct ident *ident;
2558 switch (state) {
2559 case 0: /* Identifier */
2560 state = 1;
2561 ident = (struct ident *)expr;
2562 continue;
2564 case 1: /* Constraint */
2565 state = 2;
2566 if (!expr || expr->type != EXPR_STRING) {
2567 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2568 *THIS_ADDRESS(expr) = NULL;
2569 continue;
2571 verify_input_constraint(expr, expr->string->data);
2572 continue;
2574 case 2: /* Expression */
2575 state = 0;
2576 if (!evaluate_expression(expr))
2577 return;
2578 continue;
2580 } END_FOR_EACH_PTR(expr);
2582 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2583 if (!expr) {
2584 sparse_error(stmt->pos, "bad asm output");
2585 return;
2587 if (expr->type == EXPR_STRING)
2588 continue;
2589 sparse_error(expr->pos, "asm clobber is not a string");
2590 } END_FOR_EACH_PTR(expr);
2593 static void evaluate_case_statement(struct statement *stmt)
2595 evaluate_expression(stmt->case_expression);
2596 evaluate_expression(stmt->case_to);
2597 evaluate_statement(stmt->case_statement);
2600 static void check_case_type(struct expression *switch_expr, struct expression *case_expr)
2602 struct symbol *switch_type, *case_type;
2603 if (!case_expr)
2604 return;
2605 switch_type = switch_expr->ctype;
2606 case_type = evaluate_expression(case_expr);
2608 if (case_type && switch_type) {
2609 /* Both integer types? */
2610 if (is_int_type(switch_type) && is_int_type(case_type))
2611 return;
2612 if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
2613 return;
2616 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2619 static void evaluate_switch_statement(struct statement *stmt)
2621 struct symbol *sym;
2623 evaluate_expression(stmt->switch_expression);
2624 evaluate_statement(stmt->switch_statement);
2625 if (!stmt->switch_expression)
2626 return;
2627 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2628 struct statement *case_stmt = sym->stmt;
2629 check_case_type(stmt->switch_expression, case_stmt->case_expression);
2630 check_case_type(stmt->switch_expression, case_stmt->case_to);
2631 } END_FOR_EACH_PTR(sym);
2634 struct symbol *evaluate_statement(struct statement *stmt)
2636 if (!stmt)
2637 return NULL;
2639 switch (stmt->type) {
2640 case STMT_RETURN:
2641 return evaluate_return_expression(stmt);
2643 case STMT_EXPRESSION:
2644 if (!evaluate_expression(stmt->expression))
2645 return NULL;
2646 return degenerate(stmt->expression);
2648 case STMT_COMPOUND: {
2649 struct statement *s;
2650 struct symbol *type = NULL;
2651 struct symbol *sym;
2653 /* Evaluate each symbol in the compound statement */
2654 FOR_EACH_PTR(stmt->syms, sym) {
2655 evaluate_symbol(sym);
2656 } END_FOR_EACH_PTR(sym);
2657 evaluate_symbol(stmt->ret);
2660 * Then, evaluate each statement, making the type of the
2661 * compound statement be the type of the last statement
2663 type = NULL;
2664 FOR_EACH_PTR(stmt->stmts, s) {
2665 type = evaluate_statement(s);
2666 } END_FOR_EACH_PTR(s);
2667 if (!type)
2668 type = &void_ctype;
2669 return type;
2671 case STMT_IF:
2672 evaluate_if_statement(stmt);
2673 return NULL;
2674 case STMT_ITERATOR:
2675 evaluate_iterator(stmt);
2676 return NULL;
2677 case STMT_SWITCH:
2678 evaluate_switch_statement(stmt);
2679 return NULL;
2680 case STMT_CASE:
2681 evaluate_case_statement(stmt);
2682 return NULL;
2683 case STMT_LABEL:
2684 return evaluate_statement(stmt->label_statement);
2685 case STMT_GOTO:
2686 evaluate_expression(stmt->goto_expression);
2687 return NULL;
2688 case STMT_NONE:
2689 break;
2690 case STMT_ASM:
2691 evaluate_asm_statement(stmt);
2692 return NULL;
2693 case STMT_CONTEXT:
2694 evaluate_expression(stmt->expression);
2695 return NULL;
2696 case STMT_RANGE:
2697 evaluate_expression(stmt->range_expression);
2698 evaluate_expression(stmt->range_low);
2699 evaluate_expression(stmt->range_high);
2700 return NULL;
2702 return NULL;