Make local declarations be statements of their own
[smatch.git] / evaluate.c
blobc75ab37c2002b521e3b7a79f08736d89785497ef
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 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
1256 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1258 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1259 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1261 node->ctype.base_type = ptr;
1262 ptr->bit_size = bits_in_pointer;
1263 ptr->ctype.alignment = pointer_alignment;
1265 node->bit_size = bits_in_pointer;
1266 node->ctype.alignment = pointer_alignment;
1268 access_symbol(sym);
1269 if (sym->ctype.modifiers & MOD_REGISTER) {
1270 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1271 sym->ctype.modifiers &= ~MOD_REGISTER;
1273 if (sym->type == SYM_NODE) {
1274 ptr->ctype.as |= sym->ctype.as;
1275 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1276 sym = sym->ctype.base_type;
1278 if (degenerate && sym->type == SYM_ARRAY) {
1279 ptr->ctype.as |= sym->ctype.as;
1280 ptr->ctype.modifiers |= sym->ctype.modifiers & MOD_PTRINHERIT;
1281 sym = sym->ctype.base_type;
1283 ptr->ctype.base_type = sym;
1285 return node;
1288 /* Arrays degenerate into pointers on pointer arithmetic */
1289 static struct symbol *degenerate(struct expression *expr)
1291 struct symbol *ctype, *base;
1293 if (!expr)
1294 return NULL;
1295 ctype = expr->ctype;
1296 if (!ctype)
1297 return NULL;
1298 base = examine_symbol_type(ctype);
1299 if (ctype->type == SYM_NODE)
1300 base = ctype->ctype.base_type;
1302 * Arrays degenerate into pointers to the entries, while
1303 * functions degenerate into pointers to themselves.
1304 * If array was part of non-lvalue compound, we create a copy
1305 * of that compound first and then act as if we were dealing with
1306 * the corresponding field in there.
1308 switch (base->type) {
1309 case SYM_ARRAY:
1310 if (expr->type == EXPR_SLICE) {
1311 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1312 struct expression *e0, *e1, *e2, *e3, *e4;
1314 a->ctype.base_type = expr->base->ctype;
1315 a->bit_size = expr->base->ctype->bit_size;
1316 a->array_size = expr->base->ctype->array_size;
1318 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1319 e0->symbol = a;
1320 e0->ctype = &lazy_ptr_ctype;
1322 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1323 e1->unop = e0;
1324 e1->op = '*';
1325 e1->ctype = expr->base->ctype; /* XXX */
1327 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1328 e2->left = e1;
1329 e2->right = expr->base;
1330 e2->op = '=';
1331 e2->ctype = expr->base->ctype;
1333 if (expr->r_bitpos) {
1334 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1335 e3->op = '+';
1336 e3->left = e0;
1337 e3->right = alloc_const_expression(expr->pos,
1338 expr->r_bitpos >> 3);
1339 e3->ctype = &lazy_ptr_ctype;
1340 } else {
1341 e3 = e0;
1344 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1345 e4->left = e2;
1346 e4->right = e3;
1347 e4->ctype = &lazy_ptr_ctype;
1349 expr->unop = e4;
1350 expr->type = EXPR_PREOP;
1351 expr->op = '*';
1353 case SYM_FN:
1354 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1355 sparse_error(expr->pos, "strange non-value function or array");
1356 return &bad_ctype;
1358 *expr = *expr->unop;
1359 ctype = create_pointer(expr, ctype, 1);
1360 expr->ctype = ctype;
1361 default:
1362 /* nothing */;
1364 return ctype;
1367 static struct symbol *evaluate_addressof(struct expression *expr)
1369 struct expression *op = expr->unop;
1370 struct symbol *ctype;
1372 if (op->op != '*' || op->type != EXPR_PREOP) {
1373 sparse_error(expr->pos, "not addressable");
1374 return NULL;
1376 ctype = op->ctype;
1377 *expr = *op->unop;
1379 if (expr->type == EXPR_SYMBOL) {
1380 struct symbol *sym = expr->symbol;
1381 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1385 * symbol expression evaluation is lazy about the type
1386 * of the sub-expression, so we may have to generate
1387 * the type here if so..
1389 if (expr->ctype == &lazy_ptr_ctype) {
1390 ctype = create_pointer(expr, ctype, 0);
1391 expr->ctype = ctype;
1393 return expr->ctype;
1397 static struct symbol *evaluate_dereference(struct expression *expr)
1399 struct expression *op = expr->unop;
1400 struct symbol *ctype = op->ctype, *node, *target;
1402 /* Simplify: *&(expr) => (expr) */
1403 if (op->type == EXPR_PREOP && op->op == '&') {
1404 *expr = *op->unop;
1405 return expr->ctype;
1408 /* Dereferencing a node drops all the node information. */
1409 if (ctype->type == SYM_NODE)
1410 ctype = ctype->ctype.base_type;
1412 node = alloc_symbol(expr->pos, SYM_NODE);
1413 target = ctype->ctype.base_type;
1415 switch (ctype->type) {
1416 default:
1417 sparse_error(expr->pos, "cannot derefence this type");
1418 return NULL;
1419 case SYM_PTR:
1420 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1421 merge_type(node, ctype);
1422 break;
1424 case SYM_ARRAY:
1425 if (!lvalue_expression(op)) {
1426 sparse_error(op->pos, "non-lvalue array??");
1427 return NULL;
1430 /* Do the implied "addressof" on the array */
1431 *op = *op->unop;
1434 * When an array is dereferenced, we need to pick
1435 * up the attributes of the original node too..
1437 merge_type(node, op->ctype);
1438 merge_type(node, ctype);
1439 break;
1442 node->bit_size = target->bit_size;
1443 node->array_size = target->array_size;
1445 expr->ctype = node;
1446 return node;
1450 * Unary post-ops: x++ and x--
1452 static struct symbol *evaluate_postop(struct expression *expr)
1454 struct expression *op = expr->unop;
1455 struct symbol *ctype = op->ctype;
1457 if (!lvalue_expression(expr->unop)) {
1458 sparse_error(expr->pos, "need lvalue expression for ++/--");
1459 return NULL;
1461 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1462 sparse_error(expr->pos, "bad operation on restricted");
1463 return NULL;
1466 evaluate_assign_to(op, ctype);
1468 expr->ctype = ctype;
1469 expr->op_value = 1;
1470 if (is_ptr_type(ctype))
1471 expr->op_value = ptr_object_size(ctype) >> 3;
1473 return ctype;
1476 static struct symbol *evaluate_sign(struct expression *expr)
1478 struct symbol *ctype = expr->unop->ctype;
1479 if (is_int_type(ctype)) {
1480 struct symbol *rtype = rtype = integer_promotion(ctype);
1481 expr->unop = cast_to(expr->unop, rtype);
1482 ctype = rtype;
1483 } else if (is_float_type(ctype) && expr->op != '~') {
1484 /* no conversions needed */
1485 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1486 /* no conversions needed */
1487 } else {
1488 return bad_expr_type(expr);
1490 if (expr->op == '+')
1491 *expr = *expr->unop;
1492 expr->ctype = ctype;
1493 return ctype;
1496 static struct symbol *evaluate_preop(struct expression *expr)
1498 struct symbol *ctype = expr->unop->ctype;
1500 switch (expr->op) {
1501 case '(':
1502 *expr = *expr->unop;
1503 return ctype;
1505 case '+':
1506 case '-':
1507 case '~':
1508 return evaluate_sign(expr);
1510 case '*':
1511 return evaluate_dereference(expr);
1513 case '&':
1514 return evaluate_addressof(expr);
1516 case SPECIAL_INCREMENT:
1517 case SPECIAL_DECREMENT:
1519 * From a type evaluation standpoint the pre-ops are
1520 * the same as the postops
1522 return evaluate_postop(expr);
1524 case '!':
1525 if (is_safe_type(ctype))
1526 warning(expr->pos, "testing a 'safe expression'");
1527 if (is_float_type(ctype)) {
1528 struct expression *arg = expr->unop;
1529 expr->type = EXPR_BINOP;
1530 expr->op = SPECIAL_EQUAL;
1531 expr->left = arg;
1532 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1533 expr->right->ctype = ctype;
1534 expr->right->fvalue = 0;
1536 ctype = &bool_ctype;
1537 break;
1539 default:
1540 break;
1542 expr->ctype = ctype;
1543 return &bool_ctype;
1546 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1548 struct ptr_list *head = (struct ptr_list *)_list;
1549 struct ptr_list *list = head;
1551 if (!head)
1552 return NULL;
1553 do {
1554 int i;
1555 for (i = 0; i < list->nr; i++) {
1556 struct symbol *sym = (struct symbol *) list->list[i];
1557 if (sym->ident) {
1558 if (sym->ident != ident)
1559 continue;
1560 *offset = sym->offset;
1561 return sym;
1562 } else {
1563 struct symbol *ctype = sym->ctype.base_type;
1564 struct symbol *sub;
1565 if (!ctype)
1566 continue;
1567 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1568 continue;
1569 sub = find_identifier(ident, ctype->symbol_list, offset);
1570 if (!sub)
1571 continue;
1572 *offset += sym->offset;
1573 return sub;
1576 } while ((list = list->next) != head);
1577 return NULL;
1580 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1582 struct expression *add;
1585 * Create a new add-expression
1587 * NOTE! Even if we just add zero, we need a new node
1588 * for the member pointer, since it has a different
1589 * type than the original pointer. We could make that
1590 * be just a cast, but the fact is, a node is a node,
1591 * so we might as well just do the "add zero" here.
1593 add = alloc_expression(expr->pos, EXPR_BINOP);
1594 add->op = '+';
1595 add->left = expr;
1596 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1597 add->right->ctype = &int_ctype;
1598 add->right->value = offset;
1601 * The ctype of the pointer will be lazily evaluated if
1602 * we ever take the address of this member dereference..
1604 add->ctype = &lazy_ptr_ctype;
1605 return add;
1608 /* structure/union dereference */
1609 static struct symbol *evaluate_member_dereference(struct expression *expr)
1611 int offset;
1612 struct symbol *ctype, *member;
1613 struct expression *deref = expr->deref, *add;
1614 struct ident *ident = expr->member;
1615 unsigned int mod;
1616 int address_space;
1618 if (!evaluate_expression(deref))
1619 return NULL;
1620 if (!ident) {
1621 sparse_error(expr->pos, "bad member name");
1622 return NULL;
1625 ctype = deref->ctype;
1626 address_space = ctype->ctype.as;
1627 mod = ctype->ctype.modifiers;
1628 if (ctype->type == SYM_NODE) {
1629 ctype = ctype->ctype.base_type;
1630 address_space |= ctype->ctype.as;
1631 mod |= ctype->ctype.modifiers;
1633 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1634 sparse_error(expr->pos, "expected structure or union");
1635 return NULL;
1637 examine_symbol_type(ctype);
1638 offset = 0;
1639 member = find_identifier(ident, ctype->symbol_list, &offset);
1640 if (!member) {
1641 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1642 const char *name = "<unnamed>";
1643 int namelen = 9;
1644 if (ctype->ident) {
1645 name = ctype->ident->name;
1646 namelen = ctype->ident->len;
1648 sparse_error(expr->pos, "no member '%s' in %s %.*s",
1649 show_ident(ident), type, namelen, name);
1650 return NULL;
1654 * The member needs to take on the address space and modifiers of
1655 * the "parent" type.
1657 member = convert_to_as_mod(member, address_space, mod);
1658 ctype = get_base_type(member);
1660 if (!lvalue_expression(deref)) {
1661 if (deref->type != EXPR_SLICE) {
1662 expr->base = deref;
1663 expr->r_bitpos = 0;
1664 } else {
1665 expr->base = deref->base;
1666 expr->r_bitpos = deref->r_bitpos;
1668 expr->r_bitpos += offset << 3;
1669 expr->type = EXPR_SLICE;
1670 expr->r_nrbits = member->bit_size;
1671 expr->r_bitpos += member->bit_offset;
1672 expr->ctype = member;
1673 return member;
1676 deref = deref->unop;
1677 expr->deref = deref;
1679 add = evaluate_offset(deref, offset);
1680 expr->type = EXPR_PREOP;
1681 expr->op = '*';
1682 expr->unop = add;
1684 expr->ctype = member;
1685 return member;
1688 static int is_promoted(struct expression *expr)
1690 while (1) {
1691 switch (expr->type) {
1692 case EXPR_BINOP:
1693 case EXPR_SELECT:
1694 case EXPR_CONDITIONAL:
1695 return 1;
1696 case EXPR_COMMA:
1697 expr = expr->right;
1698 continue;
1699 case EXPR_PREOP:
1700 switch (expr->op) {
1701 case '(':
1702 expr = expr->unop;
1703 continue;
1704 case '+':
1705 case '-':
1706 case '~':
1707 return 1;
1708 default:
1709 return 0;
1711 default:
1712 return 0;
1718 static struct symbol *evaluate_cast(struct expression *);
1720 static struct symbol *evaluate_type_information(struct expression *expr)
1722 struct symbol *sym = expr->cast_type;
1723 if (!sym) {
1724 sym = evaluate_expression(expr->cast_expression);
1725 if (!sym)
1726 return NULL;
1728 * Expressions of restricted types will possibly get
1729 * promoted - check that here
1731 if (is_restricted_type(sym)) {
1732 if (sym->bit_size < bits_in_int && is_promoted(expr))
1733 sym = &int_ctype;
1736 examine_symbol_type(sym);
1737 if (is_bitfield_type(sym)) {
1738 sparse_error(expr->pos, "trying to examine bitfield type");
1739 return NULL;
1741 return sym;
1744 static struct symbol *evaluate_sizeof(struct expression *expr)
1746 struct symbol *type;
1747 int size;
1749 type = evaluate_type_information(expr);
1750 if (!type)
1751 return NULL;
1753 size = type->bit_size;
1754 if ((size < 0) || (size & 7))
1755 sparse_error(expr->pos, "cannot size expression");
1756 expr->type = EXPR_VALUE;
1757 expr->value = size >> 3;
1758 expr->ctype = size_t_ctype;
1759 return size_t_ctype;
1762 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1764 struct symbol *type;
1765 int size;
1767 type = evaluate_type_information(expr);
1768 if (!type)
1769 return NULL;
1771 if (type->type == SYM_NODE)
1772 type = type->ctype.base_type;
1773 if (!type)
1774 return NULL;
1775 switch (type->type) {
1776 case SYM_ARRAY:
1777 break;
1778 case SYM_PTR:
1779 type = get_base_type(type);
1780 if (type)
1781 break;
1782 default:
1783 sparse_error(expr->pos, "expected pointer expression");
1784 return NULL;
1786 size = type->bit_size;
1787 if (size & 7)
1788 size = 0;
1789 expr->type = EXPR_VALUE;
1790 expr->value = size >> 3;
1791 expr->ctype = size_t_ctype;
1792 return size_t_ctype;
1795 static struct symbol *evaluate_alignof(struct expression *expr)
1797 struct symbol *type;
1799 type = evaluate_type_information(expr);
1800 if (!type)
1801 return NULL;
1803 expr->type = EXPR_VALUE;
1804 expr->value = type->ctype.alignment;
1805 expr->ctype = size_t_ctype;
1806 return size_t_ctype;
1809 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1811 struct expression *expr;
1812 struct symbol_list *argument_types = fn->arguments;
1813 struct symbol *argtype;
1814 int i = 1;
1816 PREPARE_PTR_LIST(argument_types, argtype);
1817 FOR_EACH_PTR (head, expr) {
1818 struct expression **p = THIS_ADDRESS(expr);
1819 struct symbol *ctype, *target;
1820 ctype = evaluate_expression(expr);
1822 if (!ctype)
1823 return 0;
1825 ctype = degenerate(expr);
1827 target = argtype;
1828 if (!target && ctype->bit_size < bits_in_int)
1829 target = &int_ctype;
1830 if (target) {
1831 static char where[30];
1832 examine_symbol_type(target);
1833 sprintf(where, "argument %d", i);
1834 compatible_assignment_types(expr, target, p, ctype, where, '=');
1837 i++;
1838 NEXT_PTR_LIST(argtype);
1839 } END_FOR_EACH_PTR(expr);
1840 FINISH_PTR_LIST(argtype);
1841 return 1;
1844 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1846 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1848 struct expression *entry = *ep;
1849 struct expression **parent, *reuse = NULL;
1850 unsigned long offset;
1851 struct symbol *sym;
1852 unsigned long from, to;
1853 int accept_string = is_byte_type(ctype);
1855 from = current;
1856 to = from+1;
1857 parent = ep;
1858 if (entry->type == EXPR_INDEX) {
1859 from = entry->idx_from;
1860 to = entry->idx_to+1;
1861 parent = &entry->idx_expression;
1862 reuse = entry;
1863 entry = entry->idx_expression;
1866 offset = from * (ctype->bit_size>>3);
1867 if (offset) {
1868 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1869 reuse->type = EXPR_POS;
1870 reuse->ctype = ctype;
1871 reuse->init_offset = offset;
1872 reuse->init_nr = to - from;
1873 reuse->init_expr = entry;
1874 parent = &reuse->init_expr;
1875 entry = reuse;
1877 *ep = entry;
1879 if (accept_string && entry->type == EXPR_STRING) {
1880 sym = evaluate_expression(entry);
1881 to = from + get_expression_value(sym->array_size);
1882 } else {
1883 evaluate_initializer(ctype, parent);
1885 return to;
1888 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1890 struct expression *entry;
1891 int current = 0;
1893 FOR_EACH_PTR(expr->expr_list, entry) {
1894 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1895 } END_FOR_EACH_PTR(entry);
1898 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1899 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1901 if (expression_list_size(expr->expr_list) != 1) {
1902 sparse_error(expr->pos, "unexpected compound initializer");
1903 return;
1905 evaluate_array_initializer(ctype, expr);
1906 return;
1909 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1911 struct symbol *sym;
1913 FOR_EACH_PTR(ctype->symbol_list, sym) {
1914 if (sym->ident == ident)
1915 return sym;
1916 } END_FOR_EACH_PTR(sym);
1917 return NULL;
1920 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1922 struct expression *entry = *ep;
1923 struct expression **parent;
1924 struct expression *reuse = NULL;
1925 unsigned long offset;
1927 if (!sym) {
1928 sparse_error(entry->pos, "unknown named initializer");
1929 return -1;
1932 if (entry->type == EXPR_IDENTIFIER) {
1933 reuse = entry;
1934 entry = entry->ident_expression;
1937 parent = ep;
1938 offset = sym->offset;
1939 if (offset) {
1940 if (!reuse)
1941 reuse = alloc_expression(entry->pos, EXPR_POS);
1942 reuse->type = EXPR_POS;
1943 reuse->ctype = sym;
1944 reuse->init_offset = offset;
1945 reuse->init_nr = 1;
1946 reuse->init_expr = entry;
1947 parent = &reuse->init_expr;
1948 entry = reuse;
1950 *ep = entry;
1951 evaluate_initializer(sym, parent);
1952 return 0;
1955 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1957 struct expression *entry;
1958 struct symbol *sym;
1960 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1961 FOR_EACH_PTR(expr->expr_list, entry) {
1962 if (entry->type == EXPR_IDENTIFIER) {
1963 struct ident *ident = entry->expr_ident;
1964 /* We special-case the "already right place" case */
1965 if (!sym || sym->ident != ident) {
1966 RESET_PTR_LIST(sym);
1967 for (;;) {
1968 if (!sym)
1969 break;
1970 if (sym->ident == ident)
1971 break;
1972 NEXT_PTR_LIST(sym);
1976 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1977 return;
1978 NEXT_PTR_LIST(sym);
1979 } END_FOR_EACH_PTR(entry);
1980 FINISH_PTR_LIST(sym);
1984 * Initializers are kind of like assignments. Except
1985 * they can be a hell of a lot more complex.
1987 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1989 struct expression *expr = *ep;
1992 * Simple non-structure/array initializers are the simple
1993 * case, and look (and parse) largely like assignments.
1995 switch (expr->type) {
1996 default: {
1997 int is_string = expr->type == EXPR_STRING;
1998 struct symbol *rtype = evaluate_expression(expr);
1999 if (rtype) {
2001 * Special case:
2002 * char array[] = "string"
2003 * should _not_ degenerate.
2005 if (!is_string || !is_string_type(ctype))
2006 rtype = degenerate(expr);
2007 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
2009 return;
2012 case EXPR_INITIALIZER:
2013 expr->ctype = ctype;
2014 if (ctype->type == SYM_NODE)
2015 ctype = ctype->ctype.base_type;
2017 switch (ctype->type) {
2018 case SYM_ARRAY:
2019 case SYM_PTR:
2020 evaluate_array_initializer(get_base_type(ctype), expr);
2021 return;
2022 case SYM_UNION:
2023 evaluate_struct_or_union_initializer(ctype, expr, 0);
2024 return;
2025 case SYM_STRUCT:
2026 evaluate_struct_or_union_initializer(ctype, expr, 1);
2027 return;
2028 default:
2029 evaluate_scalar_initializer(ctype, expr);
2030 return;
2033 case EXPR_IDENTIFIER:
2034 if (ctype->type == SYM_NODE)
2035 ctype = ctype->ctype.base_type;
2036 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
2037 sparse_error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
2038 show_symbol(ctype);
2039 return;
2041 evaluate_one_struct_initializer(ctype, ep,
2042 find_struct_ident(ctype, expr->expr_ident));
2043 return;
2045 case EXPR_INDEX:
2046 if (ctype->type == SYM_NODE)
2047 ctype = ctype->ctype.base_type;
2048 if (ctype->type != SYM_ARRAY) {
2049 sparse_error(expr->pos, "expected array");
2050 return;
2052 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2053 return;
2055 case EXPR_POS:
2057 * An EXPR_POS expression has already been evaluated, and we don't
2058 * need to do anything more
2060 return;
2064 static int get_as(struct symbol *sym)
2066 int as;
2067 unsigned long mod;
2069 if (!sym)
2070 return 0;
2071 as = sym->ctype.as;
2072 mod = sym->ctype.modifiers;
2073 if (sym->type == SYM_NODE) {
2074 sym = sym->ctype.base_type;
2075 as |= sym->ctype.as;
2076 mod |= sym->ctype.modifiers;
2080 * At least for now, allow casting to a "unsigned long".
2081 * That's how we do things like pointer arithmetic and
2082 * store pointers to registers.
2084 if (sym == &ulong_ctype)
2085 return -1;
2087 if (sym && sym->type == SYM_PTR) {
2088 sym = get_base_type(sym);
2089 as |= sym->ctype.as;
2090 mod |= sym->ctype.modifiers;
2092 if (mod & MOD_FORCE)
2093 return -1;
2094 return as;
2097 static void cast_to_as(struct expression *e, int as)
2099 struct expression *v = e->cast_expression;
2101 if (!Wcast_to_address_space)
2102 return;
2104 /* cast from constant 0 to pointer is OK */
2105 if (v->type == EXPR_VALUE && is_int_type(v->ctype) && !v->value)
2106 return;
2108 warning(e->pos, "cast adds address space to expression (<asn:%d>)", as);
2111 static struct symbol *evaluate_cast(struct expression *expr)
2113 struct expression *target = expr->cast_expression;
2114 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2115 struct symbol *t1, *t2;
2116 enum type type1, type2;
2117 int as1, as2;
2119 if (!target)
2120 return NULL;
2122 expr->ctype = ctype;
2123 expr->cast_type = ctype;
2126 * Special case: a cast can be followed by an
2127 * initializer, in which case we need to pass
2128 * the type value down to that initializer rather
2129 * than trying to evaluate it as an expression
2131 * A more complex case is when the initializer is
2132 * dereferenced as part of a post-fix expression.
2133 * We need to produce an expression that can be dereferenced.
2135 if (target->type == EXPR_INITIALIZER) {
2136 struct symbol *sym = expr->cast_type;
2137 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2139 sym->initializer = expr->cast_expression;
2140 evaluate_symbol(sym);
2142 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2143 addr->symbol = sym;
2145 expr->type = EXPR_PREOP;
2146 expr->op = '*';
2147 expr->unop = addr;
2148 expr->ctype = sym;
2150 return sym;
2153 evaluate_expression(target);
2154 degenerate(target);
2156 t1 = ctype;
2157 if (t1->type == SYM_NODE)
2158 t1 = t1->ctype.base_type;
2159 if (t1->type == SYM_ENUM)
2160 t1 = t1->ctype.base_type;
2163 * You can always throw a value away by casting to
2164 * "void" - that's an implicit "force". Note that
2165 * the same is _not_ true of "void *".
2167 if (t1 == &void_ctype)
2168 goto out;
2170 type1 = t1->type;
2171 if (type1 == SYM_ARRAY || type1 == SYM_UNION || type1 == SYM_STRUCT)
2172 warning(expr->pos, "cast to non-scalar");
2174 t2 = target->ctype;
2175 if (!t2) {
2176 sparse_error(expr->pos, "cast from unknown type");
2177 goto out;
2179 if (t2->type == SYM_NODE)
2180 t2 = t2->ctype.base_type;
2181 if (t2->type == SYM_ENUM)
2182 t2 = t2->ctype.base_type;
2184 type2 = t2->type;
2185 if (type2 == SYM_ARRAY || type2 == SYM_UNION || type2 == SYM_STRUCT)
2186 warning(expr->pos, "cast from non-scalar");
2188 if (!(ctype->ctype.modifiers & MOD_FORCE) && t1 != t2) {
2189 if (t1->type == SYM_RESTRICT)
2190 warning(expr->pos, "cast to restricted type");
2191 if (t2->type == SYM_RESTRICT)
2192 warning(expr->pos, "cast from restricted type");
2195 as1 = get_as(ctype);
2196 as2 = get_as(target->ctype);
2197 if (!as1 && as2 > 0)
2198 warning(expr->pos, "cast removes address space of expression");
2199 if (as1 > 0 && as2 > 0 && as1 != as2)
2200 warning(expr->pos, "cast between address spaces (<asn:%d>-><asn:%d>)", as2, as1);
2201 if (as1 > 0 && !as2)
2202 cast_to_as(expr, as1);
2205 * Casts of constant values are special: they
2206 * can be NULL, and thus need to be simplified
2207 * early.
2209 if (target->type == EXPR_VALUE)
2210 cast_value(expr, ctype, target, target->ctype);
2212 out:
2213 return ctype;
2217 * Evaluate a call expression with a symbol. This
2218 * should expand inline functions, and evaluate
2219 * builtins.
2221 static int evaluate_symbol_call(struct expression *expr)
2223 struct expression *fn = expr->fn;
2224 struct symbol *ctype = fn->ctype;
2226 if (fn->type != EXPR_PREOP)
2227 return 0;
2229 if (ctype->op && ctype->op->evaluate)
2230 return ctype->op->evaluate(expr);
2232 if (ctype->ctype.modifiers & MOD_INLINE) {
2233 int ret;
2234 struct symbol *curr = current_fn;
2235 current_fn = ctype->ctype.base_type;
2236 examine_fn_arguments(current_fn);
2238 ret = inline_function(expr, ctype);
2240 /* restore the old function */
2241 current_fn = curr;
2242 return ret;
2245 return 0;
2248 static struct symbol *evaluate_call(struct expression *expr)
2250 int args, fnargs;
2251 struct symbol *ctype, *sym;
2252 struct expression *fn = expr->fn;
2253 struct expression_list *arglist = expr->args;
2255 if (!evaluate_expression(fn))
2256 return NULL;
2257 sym = ctype = fn->ctype;
2258 if (ctype->type == SYM_NODE)
2259 ctype = ctype->ctype.base_type;
2260 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2261 ctype = get_base_type(ctype);
2262 if (!evaluate_arguments(sym, ctype, arglist))
2263 return NULL;
2264 if (ctype->type != SYM_FN) {
2265 sparse_error(expr->pos, "not a function %s", show_ident(sym->ident));
2266 return NULL;
2268 args = expression_list_size(expr->args);
2269 fnargs = symbol_list_size(ctype->arguments);
2270 if (args < fnargs)
2271 sparse_error(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2272 if (args > fnargs && !ctype->variadic)
2273 sparse_error(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2274 if (sym->type == SYM_NODE) {
2275 if (evaluate_symbol_call(expr))
2276 return expr->ctype;
2278 expr->ctype = ctype->ctype.base_type;
2279 return expr->ctype;
2282 struct symbol *evaluate_expression(struct expression *expr)
2284 if (!expr)
2285 return NULL;
2286 if (expr->ctype)
2287 return expr->ctype;
2289 switch (expr->type) {
2290 case EXPR_VALUE:
2291 case EXPR_FVALUE:
2292 sparse_error(expr->pos, "value expression without a type");
2293 return NULL;
2294 case EXPR_STRING:
2295 return evaluate_string(expr);
2296 case EXPR_SYMBOL:
2297 return evaluate_symbol_expression(expr);
2298 case EXPR_BINOP:
2299 if (!evaluate_expression(expr->left))
2300 return NULL;
2301 if (!evaluate_expression(expr->right))
2302 return NULL;
2303 return evaluate_binop(expr);
2304 case EXPR_LOGICAL:
2305 return evaluate_logical(expr);
2306 case EXPR_COMMA:
2307 evaluate_expression(expr->left);
2308 if (!evaluate_expression(expr->right))
2309 return NULL;
2310 return evaluate_comma(expr);
2311 case EXPR_COMPARE:
2312 if (!evaluate_expression(expr->left))
2313 return NULL;
2314 if (!evaluate_expression(expr->right))
2315 return NULL;
2316 return evaluate_compare(expr);
2317 case EXPR_ASSIGNMENT:
2318 if (!evaluate_expression(expr->left))
2319 return NULL;
2320 if (!evaluate_expression(expr->right))
2321 return NULL;
2322 return evaluate_assignment(expr);
2323 case EXPR_PREOP:
2324 if (!evaluate_expression(expr->unop))
2325 return NULL;
2326 return evaluate_preop(expr);
2327 case EXPR_POSTOP:
2328 if (!evaluate_expression(expr->unop))
2329 return NULL;
2330 return evaluate_postop(expr);
2331 case EXPR_CAST:
2332 case EXPR_IMPLIED_CAST:
2333 return evaluate_cast(expr);
2334 case EXPR_SIZEOF:
2335 return evaluate_sizeof(expr);
2336 case EXPR_PTRSIZEOF:
2337 return evaluate_ptrsizeof(expr);
2338 case EXPR_ALIGNOF:
2339 return evaluate_alignof(expr);
2340 case EXPR_DEREF:
2341 return evaluate_member_dereference(expr);
2342 case EXPR_CALL:
2343 return evaluate_call(expr);
2344 case EXPR_SELECT:
2345 case EXPR_CONDITIONAL:
2346 return evaluate_conditional_expression(expr);
2347 case EXPR_STATEMENT:
2348 expr->ctype = evaluate_statement(expr->statement);
2349 return expr->ctype;
2351 case EXPR_LABEL:
2352 expr->ctype = &ptr_ctype;
2353 return &ptr_ctype;
2355 case EXPR_TYPE:
2356 /* Evaluate the type of the symbol .. */
2357 evaluate_symbol(expr->symbol);
2358 /* .. but the type of the _expression_ is a "type" */
2359 expr->ctype = &type_ctype;
2360 return &type_ctype;
2362 /* These can not exist as stand-alone expressions */
2363 case EXPR_INITIALIZER:
2364 case EXPR_IDENTIFIER:
2365 case EXPR_INDEX:
2366 case EXPR_POS:
2367 sparse_error(expr->pos, "internal front-end error: initializer in expression");
2368 return NULL;
2369 case EXPR_SLICE:
2370 sparse_error(expr->pos, "internal front-end error: SLICE re-evaluated");
2371 return NULL;
2373 return NULL;
2376 static void check_duplicates(struct symbol *sym)
2378 int declared = 0;
2379 struct symbol *next = sym;
2381 while ((next = next->same_symbol) != NULL) {
2382 const char *typediff;
2383 evaluate_symbol(next);
2384 declared++;
2385 typediff = type_difference(sym, next, 0, 0);
2386 if (typediff) {
2387 sparse_error(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2388 show_ident(sym->ident),
2389 stream_name(next->pos.stream), next->pos.line, typediff);
2390 return;
2393 if (!declared) {
2394 unsigned long mod = sym->ctype.modifiers;
2395 if (mod & (MOD_STATIC | MOD_REGISTER))
2396 return;
2397 if (!(mod & MOD_TOPLEVEL))
2398 return;
2399 if (!Wdecl)
2400 return;
2401 if (sym->ident == &main_ident)
2402 return;
2403 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2407 static struct symbol *evaluate_symbol(struct symbol *sym)
2409 struct symbol *base_type;
2411 if (!sym)
2412 return sym;
2413 if (sym->evaluated)
2414 return sym;
2415 sym->evaluated = 1;
2417 sym = examine_symbol_type(sym);
2418 base_type = get_base_type(sym);
2419 if (!base_type)
2420 return NULL;
2422 /* Evaluate the initializers */
2423 if (sym->initializer)
2424 evaluate_initializer(sym, &sym->initializer);
2426 /* And finally, evaluate the body of the symbol too */
2427 if (base_type->type == SYM_FN) {
2428 struct symbol *curr = current_fn;
2430 current_fn = base_type;
2432 examine_fn_arguments(base_type);
2433 if (!base_type->stmt && base_type->inline_stmt)
2434 uninline(sym);
2435 if (base_type->stmt)
2436 evaluate_statement(base_type->stmt);
2438 current_fn = curr;
2441 return base_type;
2444 void evaluate_symbol_list(struct symbol_list *list)
2446 struct symbol *sym;
2448 FOR_EACH_PTR(list, sym) {
2449 check_duplicates(sym);
2450 evaluate_symbol(sym);
2451 } END_FOR_EACH_PTR(sym);
2454 static struct symbol *evaluate_return_expression(struct statement *stmt)
2456 struct expression *expr = stmt->expression;
2457 struct symbol *ctype, *fntype;
2459 evaluate_expression(expr);
2460 ctype = degenerate(expr);
2461 fntype = current_fn->ctype.base_type;
2462 if (!fntype || fntype == &void_ctype) {
2463 if (expr && ctype != &void_ctype)
2464 sparse_error(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2465 return NULL;
2468 if (!expr) {
2469 sparse_error(stmt->pos, "return with no return value");
2470 return NULL;
2472 if (!ctype)
2473 return NULL;
2474 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2475 return NULL;
2478 static void evaluate_if_statement(struct statement *stmt)
2480 if (!stmt->if_conditional)
2481 return;
2483 evaluate_conditional(stmt->if_conditional, 0);
2484 evaluate_statement(stmt->if_true);
2485 evaluate_statement(stmt->if_false);
2488 static void evaluate_iterator(struct statement *stmt)
2490 evaluate_conditional(stmt->iterator_pre_condition, 1);
2491 evaluate_conditional(stmt->iterator_post_condition,1);
2492 evaluate_statement(stmt->iterator_pre_statement);
2493 evaluate_statement(stmt->iterator_statement);
2494 evaluate_statement(stmt->iterator_post_statement);
2497 static void verify_output_constraint(struct expression *expr, const char *constraint)
2499 switch (*constraint) {
2500 case '=': /* Assignment */
2501 case '+': /* Update */
2502 break;
2503 default:
2504 sparse_error(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2508 static void verify_input_constraint(struct expression *expr, const char *constraint)
2510 switch (*constraint) {
2511 case '=': /* Assignment */
2512 case '+': /* Update */
2513 sparse_error(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2517 static void evaluate_asm_statement(struct statement *stmt)
2519 struct expression *expr;
2520 int state;
2522 expr = stmt->asm_string;
2523 if (!expr || expr->type != EXPR_STRING) {
2524 sparse_error(stmt->pos, "need constant string for inline asm");
2525 return;
2528 state = 0;
2529 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2530 struct ident *ident;
2532 switch (state) {
2533 case 0: /* Identifier */
2534 state = 1;
2535 ident = (struct ident *)expr;
2536 continue;
2538 case 1: /* Constraint */
2539 state = 2;
2540 if (!expr || expr->type != EXPR_STRING) {
2541 sparse_error(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2542 *THIS_ADDRESS(expr) = NULL;
2543 continue;
2545 verify_output_constraint(expr, expr->string->data);
2546 continue;
2548 case 2: /* Expression */
2549 state = 0;
2550 if (!evaluate_expression(expr))
2551 return;
2552 if (!lvalue_expression(expr))
2553 warning(expr->pos, "asm output is not an lvalue");
2554 evaluate_assign_to(expr, expr->ctype);
2555 continue;
2557 } END_FOR_EACH_PTR(expr);
2559 state = 0;
2560 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2561 struct ident *ident;
2563 switch (state) {
2564 case 0: /* Identifier */
2565 state = 1;
2566 ident = (struct ident *)expr;
2567 continue;
2569 case 1: /* Constraint */
2570 state = 2;
2571 if (!expr || expr->type != EXPR_STRING) {
2572 sparse_error(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2573 *THIS_ADDRESS(expr) = NULL;
2574 continue;
2576 verify_input_constraint(expr, expr->string->data);
2577 continue;
2579 case 2: /* Expression */
2580 state = 0;
2581 if (!evaluate_expression(expr))
2582 return;
2583 continue;
2585 } END_FOR_EACH_PTR(expr);
2587 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2588 if (!expr) {
2589 sparse_error(stmt->pos, "bad asm output");
2590 return;
2592 if (expr->type == EXPR_STRING)
2593 continue;
2594 sparse_error(expr->pos, "asm clobber is not a string");
2595 } END_FOR_EACH_PTR(expr);
2598 static void evaluate_case_statement(struct statement *stmt)
2600 evaluate_expression(stmt->case_expression);
2601 evaluate_expression(stmt->case_to);
2602 evaluate_statement(stmt->case_statement);
2605 static void check_case_type(struct expression *switch_expr, struct expression *case_expr)
2607 struct symbol *switch_type, *case_type;
2608 if (!case_expr)
2609 return;
2610 switch_type = switch_expr->ctype;
2611 case_type = evaluate_expression(case_expr);
2613 if (case_type && switch_type) {
2614 /* Both integer types? */
2615 if (is_int_type(switch_type) && is_int_type(case_type))
2616 return;
2617 if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
2618 return;
2621 sparse_error(case_expr->pos, "incompatible types for 'case' statement");
2624 static void evaluate_switch_statement(struct statement *stmt)
2626 struct symbol *sym;
2628 evaluate_expression(stmt->switch_expression);
2629 evaluate_statement(stmt->switch_statement);
2630 if (!stmt->switch_expression)
2631 return;
2632 FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
2633 struct statement *case_stmt = sym->stmt;
2634 check_case_type(stmt->switch_expression, case_stmt->case_expression);
2635 check_case_type(stmt->switch_expression, case_stmt->case_to);
2636 } END_FOR_EACH_PTR(sym);
2639 struct symbol *evaluate_statement(struct statement *stmt)
2641 if (!stmt)
2642 return NULL;
2644 switch (stmt->type) {
2645 case STMT_DECLARATION: {
2646 struct symbol *s;
2647 FOR_EACH_PTR(stmt->declaration, s) {
2648 evaluate_symbol(s);
2649 } END_FOR_EACH_PTR(s);
2650 return NULL;
2653 case STMT_RETURN:
2654 return evaluate_return_expression(stmt);
2656 case STMT_EXPRESSION:
2657 if (!evaluate_expression(stmt->expression))
2658 return NULL;
2659 return degenerate(stmt->expression);
2661 case STMT_COMPOUND: {
2662 struct statement *s;
2663 struct symbol *type = NULL;
2665 /* Evaluate the return symbol in the compound statement */
2666 evaluate_symbol(stmt->ret);
2669 * Then, evaluate each statement, making the type of the
2670 * compound statement be the type of the last statement
2672 type = NULL;
2673 FOR_EACH_PTR(stmt->stmts, s) {
2674 type = evaluate_statement(s);
2675 } END_FOR_EACH_PTR(s);
2676 if (!type)
2677 type = &void_ctype;
2678 return type;
2680 case STMT_IF:
2681 evaluate_if_statement(stmt);
2682 return NULL;
2683 case STMT_ITERATOR:
2684 evaluate_iterator(stmt);
2685 return NULL;
2686 case STMT_SWITCH:
2687 evaluate_switch_statement(stmt);
2688 return NULL;
2689 case STMT_CASE:
2690 evaluate_case_statement(stmt);
2691 return NULL;
2692 case STMT_LABEL:
2693 return evaluate_statement(stmt->label_statement);
2694 case STMT_GOTO:
2695 evaluate_expression(stmt->goto_expression);
2696 return NULL;
2697 case STMT_NONE:
2698 break;
2699 case STMT_ASM:
2700 evaluate_asm_statement(stmt);
2701 return NULL;
2702 case STMT_CONTEXT:
2703 evaluate_expression(stmt->expression);
2704 return NULL;
2705 case STMT_RANGE:
2706 evaluate_expression(stmt->range_expression);
2707 evaluate_expression(stmt->range_low);
2708 evaluate_expression(stmt->range_high);
2709 return NULL;
2711 return NULL;