[PATCH] Fix address space ordering problem
[smatch.git] / evaluate.c
blob5f28a8c77ba0d7965bcef69d3295e7954f0907be
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 warning(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 warning(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 unsigned long mod = type->ctype.modifiers;
103 int width;
105 if (type->type == SYM_NODE)
106 type = type->ctype.base_type;
107 if (type->type == SYM_ENUM)
108 type = type->ctype.base_type;
109 width = type->bit_size;
110 if (type->type == SYM_BITFIELD)
111 type = type->ctype.base_type;
112 mod = type->ctype.modifiers;
113 if (width < bits_in_int)
114 return &int_ctype;
116 /* If char/short has as many bits as int, it still gets "promoted" */
117 if (mod & (MOD_CHAR | MOD_SHORT)) {
118 type = &int_ctype;
119 if (mod & MOD_UNSIGNED)
120 type = &uint_ctype;
122 return type;
126 * integer part of usual arithmetic conversions:
127 * integer promotions are applied
128 * if left and right are identical, we are done
129 * if signedness is the same, convert one with lower rank
130 * unless unsigned argument has rank lower than signed one, convert the
131 * signed one.
132 * if signed argument is bigger than unsigned one, convert the unsigned.
133 * otherwise, convert signed.
135 * Leaving aside the integer promotions, that is equivalent to
136 * if identical, don't convert
137 * if left is bigger than right, convert right
138 * if right is bigger than left, convert right
139 * otherwise, if signedness is the same, convert one with lower rank
140 * otherwise convert the signed one.
142 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
144 unsigned long lmod, rmod;
146 left = integer_promotion(left);
147 right = integer_promotion(right);
149 if (left == right)
150 goto left;
152 if (left->bit_size > right->bit_size)
153 goto left;
155 if (right->bit_size > left->bit_size)
156 goto right;
158 lmod = left->ctype.modifiers;
159 rmod = right->ctype.modifiers;
160 if ((lmod ^ rmod) & MOD_UNSIGNED) {
161 if (lmod & MOD_UNSIGNED)
162 goto left;
163 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
164 goto left;
165 right:
166 left = right;
167 left:
168 return left;
171 static int same_cast_type(struct symbol *orig, struct symbol *new)
173 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
176 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
178 unsigned long mod, as;
180 mod = 0; as = 0;
181 while (node) {
182 mod |= node->ctype.modifiers;
183 as |= node->ctype.as;
184 if (node->type == SYM_NODE) {
185 node = node->ctype.base_type;
186 continue;
188 break;
190 *modp = mod & ~MOD_IGNORE;
191 *asp = as;
192 return node;
195 static int is_same_type(struct expression *expr, struct symbol *new)
197 struct symbol *old = expr->ctype;
198 unsigned long oldmod, newmod, oldas, newas;
200 old = base_type(old, &oldmod, &oldas);
201 new = base_type(new, &newmod, &newas);
203 /* Same base type, same address space? */
204 if (old == new && oldas == newas) {
205 unsigned long difmod;
207 /* Check the modifier bits. */
208 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
210 /* Exact same type? */
211 if (!difmod)
212 return 1;
215 * Not the same type, but differs only in "const".
216 * Don't warn about MOD_NOCAST.
218 if (difmod == MOD_CONST)
219 return 0;
221 if ((oldmod | newmod) & MOD_NOCAST) {
222 const char *tofrom = "to/from";
223 if (!(newmod & MOD_NOCAST))
224 tofrom = "from";
225 if (!(oldmod & MOD_NOCAST))
226 tofrom = "to";
227 warning(expr->pos, "implicit cast %s nocast type", tofrom);
229 return 0;
233 * This gets called for implicit casts in assignments and
234 * integer promotion. We often want to try to move the
235 * cast down, because the ops involved may have been
236 * implicitly cast up, and we can get rid of the casts
237 * early.
239 static struct expression * cast_to(struct expression *old, struct symbol *type)
241 struct expression *expr;
243 if (is_same_type(old, type))
244 return old;
247 * See if we can simplify the op. Move the cast down.
249 switch (old->type) {
250 case EXPR_PREOP:
251 if (old->op == '~') {
252 old->ctype = type;
253 old->unop = cast_to(old->unop, type);
254 return old;
256 break;
258 case EXPR_IMPLIED_CAST:
259 if (old->ctype->bit_size >= type->bit_size) {
260 struct expression *orig = old->cast_expression;
261 if (same_cast_type(orig->ctype, type))
262 return orig;
263 if (old->ctype->bit_offset == type->bit_offset) {
264 old->ctype = type;
265 old->cast_type = type;
266 return old;
269 break;
271 default:
272 /* nothing */;
275 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
276 expr->ctype = type;
277 expr->cast_type = type;
278 expr->cast_expression = old;
279 return expr;
282 static int is_type_type(struct symbol *type)
284 return (type->ctype.modifiers & MOD_TYPE) != 0;
287 int is_ptr_type(struct symbol *type)
289 if (type->type == SYM_NODE)
290 type = type->ctype.base_type;
291 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
294 static inline int is_float_type(struct symbol *type)
296 if (type->type == SYM_NODE)
297 type = type->ctype.base_type;
298 return type->ctype.base_type == &fp_type;
301 static inline int is_byte_type(struct symbol *type)
303 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
306 static inline int is_string_type(struct symbol *type)
308 if (type->type == SYM_NODE)
309 type = type->ctype.base_type;
310 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
313 static struct symbol *bad_expr_type(struct expression *expr)
315 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
316 switch (expr->type) {
317 case EXPR_BINOP:
318 case EXPR_COMPARE:
319 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
320 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
321 break;
322 case EXPR_PREOP:
323 case EXPR_POSTOP:
324 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
325 break;
326 default:
327 break;
330 return NULL;
333 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
335 struct expression *left = *lp, *right = *rp;
336 struct symbol *ltype = left->ctype, *rtype = right->ctype;
338 if (ltype->type == SYM_NODE)
339 ltype = ltype->ctype.base_type;
340 if (rtype->type == SYM_NODE)
341 rtype = rtype->ctype.base_type;
342 if (is_float_type(ltype)) {
343 if (is_int_type(rtype))
344 goto Left;
345 if (is_float_type(rtype)) {
346 unsigned long lmod = ltype->ctype.modifiers;
347 unsigned long rmod = rtype->ctype.modifiers;
348 lmod &= MOD_LONG | MOD_LONGLONG;
349 rmod &= MOD_LONG | MOD_LONGLONG;
350 if (lmod == rmod)
351 return ltype;
352 if (lmod & ~rmod)
353 goto Left;
354 else
355 goto Right;
357 return NULL;
359 if (!is_float_type(rtype) || !is_int_type(ltype))
360 return NULL;
361 Right:
362 *lp = cast_to(left, rtype);
363 return rtype;
364 Left:
365 *rp = cast_to(right, ltype);
366 return ltype;
369 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
371 struct expression *left = *lp, *right = *rp;
372 struct symbol *ltype = left->ctype, *rtype = right->ctype;
374 if (ltype->type == SYM_NODE)
375 ltype = ltype->ctype.base_type;
376 if (rtype->type == SYM_NODE)
377 rtype = rtype->ctype.base_type;
378 if (is_int_type(ltype) && is_int_type(rtype)) {
379 struct symbol *ctype = bigger_int_type(ltype, rtype);
381 *lp = cast_to(left, ctype);
382 *rp = cast_to(right, ctype);
383 return ctype;
385 return NULL;
388 static int restricted_value(struct expression *v, struct symbol *type)
390 if (v->type != EXPR_VALUE)
391 return 1;
392 if (v->value != 0)
393 return 1;
394 return 0;
397 static int restricted_binop(int op, struct symbol *type)
399 switch (op) {
400 case '&':
401 case '|':
402 case '^':
403 case '?':
404 case '=':
405 case SPECIAL_EQUAL:
406 case SPECIAL_NOTEQUAL:
407 case SPECIAL_AND_ASSIGN:
408 case SPECIAL_OR_ASSIGN:
409 case SPECIAL_XOR_ASSIGN:
410 return 0;
411 default:
412 return 1;
416 static int restricted_unop(int op, struct symbol *type)
418 if (op == '~' && type->bit_size >= bits_in_int)
419 return 0;
420 if (op == '+')
421 return 0;
422 return 1;
425 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
427 struct expression *left = *lp, *right = *rp;
428 struct symbol *ltype = left->ctype, *rtype = right->ctype;
429 struct symbol *type = NULL;
431 if (ltype->type == SYM_NODE)
432 ltype = ltype->ctype.base_type;
433 if (ltype->type == SYM_ENUM)
434 ltype = ltype->ctype.base_type;
435 if (rtype->type == SYM_NODE)
436 rtype = rtype->ctype.base_type;
437 if (rtype->type == SYM_ENUM)
438 rtype = rtype->ctype.base_type;
439 if (is_restricted_type(ltype)) {
440 if (is_restricted_type(rtype)) {
441 if (ltype == rtype)
442 type = ltype;
443 } else {
444 if (!restricted_value(right, ltype))
445 type = ltype;
447 } else if (is_restricted_type(rtype)) {
448 if (!restricted_value(left, rtype))
449 type = rtype;
451 if (!type)
452 return NULL;
453 if (restricted_binop(op, type))
454 return NULL;
455 return type;
458 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
460 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
461 if (!ctype && float_ok)
462 ctype = compatible_float_binop(&expr->left, &expr->right);
463 if (!ctype)
464 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
465 if (ctype) {
466 expr->ctype = ctype;
467 return ctype;
469 return bad_expr_type(expr);
472 static inline int lvalue_expression(struct expression *expr)
474 return expr->type == EXPR_PREOP && expr->op == '*';
477 static int ptr_object_size(struct symbol *ptr_type)
479 if (ptr_type->type == SYM_NODE)
480 ptr_type = ptr_type->ctype.base_type;
481 if (ptr_type->type == SYM_PTR)
482 ptr_type = get_base_type(ptr_type);
483 return ptr_type->bit_size;
486 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
488 struct expression *i = *ip;
489 struct symbol *ptr_type = ctype;
490 int bit_size;
492 if (ptr_type->type == SYM_NODE)
493 ptr_type = ptr_type->ctype.base_type;
495 if (!is_int_type(i->ctype))
496 return bad_expr_type(expr);
498 examine_symbol_type(ctype);
500 if (!ctype->ctype.base_type) {
501 warning(expr->pos, "missing type information");
502 return NULL;
505 /* Get the size of whatever the pointer points to */
506 bit_size = ptr_object_size(ctype);
508 if (bit_size > bits_in_char) {
509 int multiply = bit_size >> 3;
510 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
512 if (i->type == EXPR_VALUE) {
513 val->value = i->value * multiply;
514 val->ctype = size_t_ctype;
515 *ip = val;
516 } else {
517 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
519 val->ctype = size_t_ctype;
520 val->value = bit_size >> 3;
522 mul->op = '*';
523 mul->ctype = size_t_ctype;
524 mul->left = i;
525 mul->right = val;
527 *ip = mul;
531 expr->ctype = ctype;
532 return ctype;
535 static struct symbol *evaluate_add(struct expression *expr)
537 struct expression *left = expr->left, *right = expr->right;
538 struct symbol *ltype = left->ctype, *rtype = right->ctype;
540 if (is_ptr_type(ltype))
541 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
543 if (is_ptr_type(rtype))
544 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
546 return evaluate_arith(expr, 1);
549 const char * type_difference(struct symbol *target, struct symbol *source,
550 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
552 for (;;) {
553 unsigned long mod1, mod2, diff;
554 unsigned long as1, as2;
555 int type1, type2;
556 struct symbol *base1, *base2;
558 if (target == source)
559 break;
560 if (!target || !source)
561 return "different types";
563 * Peel of per-node information.
564 * FIXME! Check alignment and context too here!
566 mod1 = target->ctype.modifiers;
567 as1 = target->ctype.as;
568 mod2 = source->ctype.modifiers;
569 as2 = source->ctype.as;
570 if (target->type == SYM_NODE) {
571 target = target->ctype.base_type;
572 if (!target)
573 return "bad types";
574 if (target->type == SYM_PTR) {
575 mod1 = 0;
576 as1 = 0;
578 mod1 |= target->ctype.modifiers;
579 as1 |= target->ctype.as;
581 if (source->type == SYM_NODE) {
582 source = source->ctype.base_type;
583 if (!source)
584 return "bad types";
585 if (source->type == SYM_PTR) {
586 mod2 = 0;
587 as2 = 0;
589 mod2 |= source->ctype.modifiers;
590 as2 |= source->ctype.as;
592 if (target->type == SYM_ENUM) {
593 target = target->ctype.base_type;
594 if (!target)
595 return "bad types";
597 if (source->type == SYM_ENUM) {
598 source = source->ctype.base_type;
599 if (!source)
600 return "bad types";
603 if (target == source)
604 break;
605 if (!target || !source)
606 return "different types";
608 type1 = target->type;
609 base1 = target->ctype.base_type;
611 type2 = source->type;
612 base2 = source->ctype.base_type;
615 * Pointers to functions compare as the function itself
617 if (type1 == SYM_PTR && base1) {
618 base1 = examine_symbol_type(base1);
619 switch (base1->type) {
620 case SYM_FN:
621 type1 = SYM_FN;
622 target = base1;
623 base1 = base1->ctype.base_type;
624 default:
625 /* nothing */;
628 if (type2 == SYM_PTR && base2) {
629 base2 = examine_symbol_type(base2);
630 switch (base2->type) {
631 case SYM_FN:
632 type2 = SYM_FN;
633 source = base2;
634 base2 = base2->ctype.base_type;
635 default:
636 /* nothing */;
640 /* Arrays degenerate to pointers for type comparisons */
641 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
642 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
644 if (type1 != type2 || type1 == SYM_RESTRICT)
645 return "different base types";
647 /* Must be same address space to be comparable */
648 if (as1 != as2)
649 return "different address spaces";
651 /* Ignore differences in storage types or addressability */
652 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
653 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
654 if (diff) {
655 if (diff & MOD_SIZE)
656 return "different type sizes";
657 if (diff & ~MOD_SIGNEDNESS)
658 return "different modifiers";
660 /* Differs in signedness only.. */
661 if (Wtypesign) {
663 * Warn if both are explicitly signed ("unsigned" is obvously
664 * always explicit, and since we know one of them has to be
665 * unsigned, we check if the signed one was explicit).
667 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
668 return "different explicit signedness";
671 * "char" matches both "unsigned char" and "signed char",
672 * so if the explicit test didn't trigger, then we should
673 * not warn about a char.
675 if (!(mod1 & MOD_CHAR))
676 return "different signedness";
680 if (type1 == SYM_FN) {
681 int i;
682 struct symbol *arg1, *arg2;
683 if (base1->variadic != base2->variadic)
684 return "incompatible variadic arguments";
685 PREPARE_PTR_LIST(target->arguments, arg1);
686 PREPARE_PTR_LIST(source->arguments, arg2);
687 i = 1;
688 for (;;) {
689 const char *diff;
690 diff = type_difference(arg1, arg2, 0, 0);
691 if (diff) {
692 static char argdiff[80];
693 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
694 return argdiff;
696 if (!arg1)
697 break;
698 NEXT_PTR_LIST(arg1);
699 NEXT_PTR_LIST(arg2);
700 i++;
702 FINISH_PTR_LIST(arg2);
703 FINISH_PTR_LIST(arg1);
706 target = base1;
707 source = base2;
709 return NULL;
712 static int is_null_ptr(struct expression *expr)
714 if (expr->type != EXPR_VALUE || expr->value)
715 return 0;
716 if (!is_ptr_type(expr->ctype))
717 warning(expr->pos, "Using plain integer as NULL pointer");
718 return 1;
721 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
723 /* NULL expression? Just return the type of the "other side" */
724 if (is_null_ptr(r))
725 return l->ctype;
726 if (is_null_ptr(l))
727 return r->ctype;
728 return NULL;
732 * Ignore differences in "volatile" and "const"ness when
733 * subtracting pointers
735 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
737 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
739 const char *typediff;
740 struct symbol *ctype;
741 struct symbol *ltype, *rtype;
742 struct expression *r = *rp;
744 ltype = degenerate(l);
745 rtype = degenerate(r);
748 * If it is an integer subtract: the ptr add case will do the
749 * right thing.
751 if (!is_ptr_type(rtype))
752 return evaluate_ptr_add(expr, degenerate(l), rp);
754 ctype = ltype;
755 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
756 if (typediff) {
757 ctype = common_ptr_type(l, r);
758 if (!ctype) {
759 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
760 return NULL;
763 examine_symbol_type(ctype);
765 /* Figure out the base type we point to */
766 if (ctype->type == SYM_NODE)
767 ctype = ctype->ctype.base_type;
768 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
769 warning(expr->pos, "subtraction of functions? Share your drugs");
770 return NULL;
772 ctype = get_base_type(ctype);
774 expr->ctype = ssize_t_ctype;
775 if (ctype->bit_size > bits_in_char) {
776 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
777 struct expression *div = expr;
778 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
779 unsigned long value = ctype->bit_size >> 3;
781 val->ctype = size_t_ctype;
782 val->value = value;
784 if (value & (value-1)) {
785 if (Wptr_subtraction_blows)
786 warning(expr->pos, "potentially expensive pointer subtraction");
789 sub->op = '-';
790 sub->ctype = ssize_t_ctype;
791 sub->left = l;
792 sub->right = r;
794 div->op = '/';
795 div->left = sub;
796 div->right = val;
799 return ssize_t_ctype;
802 static struct symbol *evaluate_sub(struct expression *expr)
804 struct expression *left = expr->left;
805 struct symbol *ltype = left->ctype;
807 if (is_ptr_type(ltype))
808 return evaluate_ptr_sub(expr, left, &expr->right);
810 return evaluate_arith(expr, 1);
813 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
815 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
817 struct symbol *ctype;
819 if (!expr)
820 return NULL;
822 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
823 warning(expr->pos, "assignment expression in conditional");
825 ctype = evaluate_expression(expr);
826 if (ctype) {
827 if (is_safe_type(ctype))
828 warning(expr->pos, "testing a 'safe expression'");
831 return ctype;
834 static struct symbol *evaluate_logical(struct expression *expr)
836 if (!evaluate_conditional(expr->left, 0))
837 return NULL;
838 if (!evaluate_conditional(expr->right, 0))
839 return NULL;
841 expr->ctype = &bool_ctype;
842 return &bool_ctype;
845 static struct symbol *evaluate_shift(struct expression *expr)
847 struct expression *left = expr->left, *right = expr->right;
848 struct symbol *ltype = left->ctype, *rtype = right->ctype;
850 if (ltype->type == SYM_NODE)
851 ltype = ltype->ctype.base_type;
852 if (rtype->type == SYM_NODE)
853 rtype = rtype->ctype.base_type;
854 if (is_int_type(ltype) && is_int_type(rtype)) {
855 struct symbol *ctype = integer_promotion(ltype);
856 expr->left = cast_to(expr->left, ctype);
857 expr->ctype = ctype;
858 ctype = integer_promotion(rtype);
859 expr->right = cast_to(expr->right, ctype);
860 return expr->ctype;
862 return bad_expr_type(expr);
865 static struct symbol *evaluate_binop(struct expression *expr)
867 switch (expr->op) {
868 // addition can take ptr+int, fp and int
869 case '+':
870 return evaluate_add(expr);
872 // subtraction can take ptr-ptr, fp and int
873 case '-':
874 return evaluate_sub(expr);
876 // Arithmetic operations can take fp and int
877 case '*': case '/':
878 return evaluate_arith(expr, 1);
880 // shifts do integer promotions, but that's it.
881 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
882 return evaluate_shift(expr);
884 // The rest are integer operations
885 // '%', '&', '^', '|'
886 default:
887 return evaluate_arith(expr, 0);
891 static struct symbol *evaluate_comma(struct expression *expr)
893 expr->ctype = expr->right->ctype;
894 return expr->ctype;
897 static int modify_for_unsigned(int op)
899 if (op == '<')
900 op = SPECIAL_UNSIGNED_LT;
901 else if (op == '>')
902 op = SPECIAL_UNSIGNED_GT;
903 else if (op == SPECIAL_LTE)
904 op = SPECIAL_UNSIGNED_LTE;
905 else if (op == SPECIAL_GTE)
906 op = SPECIAL_UNSIGNED_GTE;
907 return op;
910 static struct symbol *evaluate_compare(struct expression *expr)
912 struct expression *left = expr->left, *right = expr->right;
913 struct symbol *ltype = left->ctype, *rtype = right->ctype;
914 struct symbol *ctype;
916 /* Type types? */
917 if (is_type_type(ltype) && is_type_type(rtype))
918 goto OK;
920 if (is_safe_type(ltype) || is_safe_type(rtype))
921 warning(expr->pos, "testing a 'safe expression'");
923 /* Pointer types? */
924 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
925 // FIXME! Check the types for compatibility
926 expr->op = modify_for_unsigned(expr->op);
927 goto OK;
930 ctype = compatible_integer_binop(&expr->left, &expr->right);
931 if (ctype) {
932 if (ctype->ctype.modifiers & MOD_UNSIGNED)
933 expr->op = modify_for_unsigned(expr->op);
934 goto OK;
937 ctype = compatible_float_binop(&expr->left, &expr->right);
938 if (ctype)
939 goto OK;
941 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
942 if (ctype) {
943 if (ctype->ctype.modifiers & MOD_UNSIGNED)
944 expr->op = modify_for_unsigned(expr->op);
945 goto OK;
948 bad_expr_type(expr);
951 expr->ctype = &bool_ctype;
952 return &bool_ctype;
956 * FIXME!! This should do casts, array degeneration etc..
958 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
960 struct symbol *ltype = left->ctype, *rtype = right->ctype;
962 if (ltype->type == SYM_NODE)
963 ltype = ltype->ctype.base_type;
965 if (rtype->type == SYM_NODE)
966 rtype = rtype->ctype.base_type;
968 if (ltype->type == SYM_PTR) {
969 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
970 return ltype;
973 if (rtype->type == SYM_PTR) {
974 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
975 return rtype;
977 return NULL;
981 * NOTE! The degenerate case of "x ? : y", where we don't
982 * have a true case, this will possibly promote "x" to the
983 * same type as "y", and thus _change_ the conditional
984 * test in the expression. But since promotion is "safe"
985 * for testing, that's ok.
987 static struct symbol *evaluate_conditional_expression(struct expression *expr)
989 struct expression **true;
990 struct symbol *ctype, *ltype, *rtype;
991 const char * typediff;
993 if (!evaluate_conditional(expr->conditional, 0))
994 return NULL;
995 if (!evaluate_expression(expr->cond_false))
996 return NULL;
998 ctype = degenerate(expr->conditional);
999 rtype = degenerate(expr->cond_false);
1001 true = &expr->conditional;
1002 ltype = ctype;
1003 if (expr->cond_true) {
1004 if (!evaluate_expression(expr->cond_true))
1005 return NULL;
1006 ltype = degenerate(expr->cond_true);
1007 true = &expr->cond_true;
1010 ctype = compatible_integer_binop(true, &expr->cond_false);
1011 if (ctype)
1012 goto out;
1013 ctype = compatible_ptr_type(*true, expr->cond_false);
1014 if (ctype)
1015 goto out;
1016 ctype = compatible_float_binop(true, &expr->cond_false);
1017 if (ctype)
1018 goto out;
1019 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1020 if (ctype)
1021 goto out;
1022 ctype = ltype;
1023 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1024 if (!typediff)
1025 goto out;
1026 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1027 return NULL;
1029 out:
1030 expr->ctype = ctype;
1031 return ctype;
1034 /* FP assignments can not do modulo or bit operations */
1035 static int compatible_float_op(int op)
1037 return op == '=' ||
1038 op == SPECIAL_ADD_ASSIGN ||
1039 op == SPECIAL_SUB_ASSIGN ||
1040 op == SPECIAL_MUL_ASSIGN ||
1041 op == SPECIAL_DIV_ASSIGN;
1044 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1045 struct expression **rp, struct symbol *source, const char *where, int op)
1047 const char *typediff;
1048 struct symbol *t;
1049 int target_as;
1051 if (is_int_type(target)) {
1052 if (is_int_type(source))
1053 goto Cast;
1054 if (is_float_type(source))
1055 goto Cast;
1056 } else if (is_float_type(target)) {
1057 if (!compatible_float_op(op)) {
1058 warning(expr->pos, "invalid assignment");
1059 return 0;
1061 if (is_int_type(source))
1062 goto Cast;
1063 if (is_float_type(source))
1064 goto Cast;
1065 } else if (is_restricted_type(target)) {
1066 if (restricted_binop(op, target)) {
1067 warning(expr->pos, "bad restricted assignment");
1068 return 0;
1070 if (!restricted_value(*rp, target))
1071 return 1;
1072 } else if (is_ptr_type(target)) {
1073 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1074 evaluate_ptr_add(expr, target, rp);
1075 return 1;
1077 if (op != '=') {
1078 warning(expr->pos, "invalid pointer assignment");
1079 return 0;
1081 } else if (op != '=') {
1082 warning(expr->pos, "invalid assignment");
1083 return 0;
1086 /* It's ok if the target is more volatile or const than the source */
1087 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1088 if (!typediff)
1089 return 1;
1091 /* Pointer destination? */
1092 t = target;
1093 target_as = t->ctype.as;
1094 if (t->type == SYM_NODE) {
1095 t = t->ctype.base_type;
1096 target_as |= t->ctype.as;
1098 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1099 struct expression *right = *rp;
1100 struct symbol *s = source;
1101 int source_as;
1103 // NULL pointer is always ok
1104 if (is_null_ptr(right))
1105 goto Cast;
1107 /* "void *" matches anything as long as the address space is ok */
1108 source_as = s->ctype.as;
1109 if (s->type == SYM_NODE) {
1110 s = s->ctype.base_type;
1111 source_as |= s->ctype.as;
1113 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1114 s = get_base_type(s);
1115 t = get_base_type(t);
1116 if (s == &void_ctype || t == &void_ctype)
1117 goto Cast;
1121 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1122 info(expr->pos, " expected %s", show_typename(target));
1123 info(expr->pos, " got %s", show_typename(source));
1124 *rp = cast_to(*rp, target);
1125 return 0;
1126 Cast:
1127 *rp = cast_to(*rp, target);
1128 return 1;
1131 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1133 if (type->ctype.modifiers & MOD_CONST)
1134 warning(left->pos, "assignment to const expression");
1135 if (type->type == SYM_NODE)
1136 type->ctype.modifiers |= MOD_ASSIGNED;
1139 static struct symbol *evaluate_assignment(struct expression *expr)
1141 struct expression *left = expr->left, *right = expr->right;
1142 struct expression *where = expr;
1143 struct symbol *ltype, *rtype;
1145 if (!lvalue_expression(left)) {
1146 warning(expr->pos, "not an lvalue");
1147 return NULL;
1150 ltype = left->ctype;
1152 rtype = degenerate(right);
1154 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1155 return NULL;
1157 evaluate_assign_to(left, ltype);
1159 expr->ctype = ltype;
1160 return ltype;
1163 static void examine_fn_arguments(struct symbol *fn)
1165 struct symbol *s;
1167 FOR_EACH_PTR(fn->arguments, s) {
1168 struct symbol *arg = evaluate_symbol(s);
1169 /* Array/function arguments silently degenerate into pointers */
1170 if (arg) {
1171 struct symbol *ptr;
1172 switch(arg->type) {
1173 case SYM_ARRAY:
1174 case SYM_FN:
1175 ptr = alloc_symbol(s->pos, SYM_PTR);
1176 if (arg->type == SYM_ARRAY)
1177 ptr->ctype = arg->ctype;
1178 else
1179 ptr->ctype.base_type = arg;
1180 ptr->ctype.as |= s->ctype.as;
1181 ptr->ctype.modifiers |= s->ctype.modifiers;
1183 s->ctype.base_type = ptr;
1184 s->ctype.as = 0;
1185 s->ctype.modifiers = 0;
1186 s->bit_size = 0;
1187 s->examined = 0;
1188 examine_symbol_type(s);
1189 break;
1190 default:
1191 /* nothing */
1192 break;
1195 } END_FOR_EACH_PTR(s);
1198 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1200 /* Take the modifiers of the pointer, and apply them to the member */
1201 mod |= sym->ctype.modifiers;
1202 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1203 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1204 *newsym = *sym;
1205 newsym->ctype.as = as;
1206 newsym->ctype.modifiers = mod;
1207 sym = newsym;
1209 return sym;
1212 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1214 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1215 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1217 node->ctype.base_type = ptr;
1218 ptr->bit_size = bits_in_pointer;
1219 ptr->ctype.alignment = pointer_alignment;
1221 node->bit_size = bits_in_pointer;
1222 node->ctype.alignment = pointer_alignment;
1224 access_symbol(sym);
1225 if (sym->ctype.modifiers & MOD_REGISTER) {
1226 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1227 sym->ctype.modifiers &= ~MOD_REGISTER;
1229 if (sym->type == SYM_NODE) {
1230 ptr->ctype.as |= sym->ctype.as;
1231 ptr->ctype.modifiers |= sym->ctype.modifiers;
1232 sym = sym->ctype.base_type;
1234 if (degenerate && sym->type == SYM_ARRAY) {
1235 ptr->ctype.as |= sym->ctype.as;
1236 ptr->ctype.modifiers |= sym->ctype.modifiers;
1237 sym = sym->ctype.base_type;
1239 ptr->ctype.base_type = sym;
1241 return node;
1244 /* Arrays degenerate into pointers on pointer arithmetic */
1245 static struct symbol *degenerate(struct expression *expr)
1247 struct symbol *ctype, *base;
1249 if (!expr)
1250 return NULL;
1251 ctype = expr->ctype;
1252 if (!ctype)
1253 return NULL;
1254 base = examine_symbol_type(ctype);
1255 if (ctype->type == SYM_NODE)
1256 base = ctype->ctype.base_type;
1258 * Arrays degenerate into pointers to the entries, while
1259 * functions degenerate into pointers to themselves.
1260 * If array was part of non-lvalue compound, we create a copy
1261 * of that compound first and then act as if we were dealing with
1262 * the corresponding field in there.
1264 switch (base->type) {
1265 case SYM_ARRAY:
1266 if (expr->type == EXPR_SLICE) {
1267 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1268 struct expression *e0, *e1, *e2, *e3, *e4;
1270 a->ctype.base_type = expr->base->ctype;
1271 a->bit_size = expr->base->ctype->bit_size;
1272 a->array_size = expr->base->ctype->array_size;
1274 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1275 e0->symbol = a;
1276 e0->ctype = &lazy_ptr_ctype;
1278 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1279 e1->unop = e0;
1280 e1->op = '*';
1281 e1->ctype = expr->base->ctype; /* XXX */
1283 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1284 e2->left = e1;
1285 e2->right = expr->base;
1286 e2->op = '=';
1287 e2->ctype = expr->base->ctype;
1289 if (expr->r_bitpos) {
1290 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1291 e3->op = '+';
1292 e3->left = e0;
1293 e3->right = alloc_const_expression(expr->pos,
1294 expr->r_bitpos >> 3);
1295 e3->ctype = &lazy_ptr_ctype;
1296 } else {
1297 e3 = e0;
1300 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1301 e4->left = e2;
1302 e4->right = e3;
1303 e4->ctype = &lazy_ptr_ctype;
1305 expr->unop = e4;
1306 expr->type = EXPR_PREOP;
1307 expr->op = '*';
1309 case SYM_FN:
1310 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1311 warning(expr->pos, "strange non-value function or array");
1312 return &bad_ctype;
1314 *expr = *expr->unop;
1315 ctype = create_pointer(expr, ctype, 1);
1316 expr->ctype = ctype;
1317 default:
1318 /* nothing */;
1320 return ctype;
1323 static struct symbol *evaluate_addressof(struct expression *expr)
1325 struct expression *op = expr->unop;
1326 struct symbol *ctype;
1328 if (op->op != '*' || op->type != EXPR_PREOP) {
1329 warning(expr->pos, "not addressable");
1330 return NULL;
1332 ctype = op->ctype;
1333 *expr = *op->unop;
1335 if (expr->type == EXPR_SYMBOL) {
1336 struct symbol *sym = expr->symbol;
1337 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1341 * symbol expression evaluation is lazy about the type
1342 * of the sub-expression, so we may have to generate
1343 * the type here if so..
1345 if (expr->ctype == &lazy_ptr_ctype) {
1346 ctype = create_pointer(expr, ctype, 0);
1347 expr->ctype = ctype;
1349 return expr->ctype;
1353 static struct symbol *evaluate_dereference(struct expression *expr)
1355 struct expression *op = expr->unop;
1356 struct symbol *ctype = op->ctype, *node, *target;
1358 /* Simplify: *&(expr) => (expr) */
1359 if (op->type == EXPR_PREOP && op->op == '&') {
1360 *expr = *op->unop;
1361 return expr->ctype;
1364 /* Dereferencing a node drops all the node information. */
1365 if (ctype->type == SYM_NODE)
1366 ctype = ctype->ctype.base_type;
1368 node = alloc_symbol(expr->pos, SYM_NODE);
1369 target = ctype->ctype.base_type;
1371 switch (ctype->type) {
1372 default:
1373 warning(expr->pos, "cannot derefence this type");
1374 return NULL;
1375 case SYM_PTR:
1376 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1377 merge_type(node, ctype);
1378 break;
1380 case SYM_ARRAY:
1381 if (!lvalue_expression(op)) {
1382 warning(op->pos, "non-lvalue array??");
1383 return NULL;
1386 /* Do the implied "addressof" on the array */
1387 *op = *op->unop;
1390 * When an array is dereferenced, we need to pick
1391 * up the attributes of the original node too..
1393 merge_type(node, op->ctype);
1394 merge_type(node, ctype);
1395 break;
1398 node->bit_size = target->bit_size;
1399 node->array_size = target->array_size;
1401 expr->ctype = node;
1402 return node;
1406 * Unary post-ops: x++ and x--
1408 static struct symbol *evaluate_postop(struct expression *expr)
1410 struct expression *op = expr->unop;
1411 struct symbol *ctype = op->ctype;
1413 if (!lvalue_expression(expr->unop)) {
1414 warning(expr->pos, "need lvalue expression for ++/--");
1415 return NULL;
1417 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1418 warning(expr->pos, "bad operation on restricted");
1419 return NULL;
1422 evaluate_assign_to(op, ctype);
1424 expr->ctype = ctype;
1425 expr->op_value = 1;
1426 if (is_ptr_type(ctype))
1427 expr->op_value = ptr_object_size(ctype) >> 3;
1429 return ctype;
1432 static struct symbol *evaluate_sign(struct expression *expr)
1434 struct symbol *ctype = expr->unop->ctype;
1435 if (is_int_type(ctype)) {
1436 struct symbol *rtype = rtype = integer_promotion(ctype);
1437 expr->unop = cast_to(expr->unop, rtype);
1438 ctype = rtype;
1439 } else if (is_float_type(ctype) && expr->op != '~') {
1440 /* no conversions needed */
1441 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1442 /* no conversions needed */
1443 } else {
1444 return bad_expr_type(expr);
1446 if (expr->op == '+')
1447 *expr = *expr->unop;
1448 expr->ctype = ctype;
1449 return ctype;
1452 static struct symbol *evaluate_preop(struct expression *expr)
1454 struct symbol *ctype = expr->unop->ctype;
1456 switch (expr->op) {
1457 case '(':
1458 *expr = *expr->unop;
1459 return ctype;
1461 case '+':
1462 case '-':
1463 case '~':
1464 return evaluate_sign(expr);
1466 case '*':
1467 return evaluate_dereference(expr);
1469 case '&':
1470 return evaluate_addressof(expr);
1472 case SPECIAL_INCREMENT:
1473 case SPECIAL_DECREMENT:
1475 * From a type evaluation standpoint the pre-ops are
1476 * the same as the postops
1478 return evaluate_postop(expr);
1480 case '!':
1481 if (is_safe_type(ctype))
1482 warning(expr->pos, "testing a 'safe expression'");
1483 if (is_float_type(ctype)) {
1484 struct expression *arg = expr->unop;
1485 expr->type = EXPR_BINOP;
1486 expr->op = SPECIAL_EQUAL;
1487 expr->left = arg;
1488 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1489 expr->right->ctype = ctype;
1490 expr->right->fvalue = 0;
1492 ctype = &bool_ctype;
1493 break;
1495 default:
1496 break;
1498 expr->ctype = ctype;
1499 return &bool_ctype;
1502 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1504 struct ptr_list *head = (struct ptr_list *)_list;
1505 struct ptr_list *list = head;
1507 if (!head)
1508 return NULL;
1509 do {
1510 int i;
1511 for (i = 0; i < list->nr; i++) {
1512 struct symbol *sym = (struct symbol *) list->list[i];
1513 if (sym->ident) {
1514 if (sym->ident != ident)
1515 continue;
1516 *offset = sym->offset;
1517 return sym;
1518 } else {
1519 struct symbol *ctype = sym->ctype.base_type;
1520 struct symbol *sub;
1521 if (!ctype)
1522 continue;
1523 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1524 continue;
1525 sub = find_identifier(ident, ctype->symbol_list, offset);
1526 if (!sub)
1527 continue;
1528 *offset += sym->offset;
1529 return sub;
1532 } while ((list = list->next) != head);
1533 return NULL;
1536 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1538 struct expression *add;
1541 * Create a new add-expression
1543 * NOTE! Even if we just add zero, we need a new node
1544 * for the member pointer, since it has a different
1545 * type than the original pointer. We could make that
1546 * be just a cast, but the fact is, a node is a node,
1547 * so we might as well just do the "add zero" here.
1549 add = alloc_expression(expr->pos, EXPR_BINOP);
1550 add->op = '+';
1551 add->left = expr;
1552 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1553 add->right->ctype = &int_ctype;
1554 add->right->value = offset;
1557 * The ctype of the pointer will be lazily evaluated if
1558 * we ever take the address of this member dereference..
1560 add->ctype = &lazy_ptr_ctype;
1561 return add;
1564 /* structure/union dereference */
1565 static struct symbol *evaluate_member_dereference(struct expression *expr)
1567 int offset;
1568 struct symbol *ctype, *member;
1569 struct expression *deref = expr->deref, *add;
1570 struct ident *ident = expr->member;
1571 unsigned int mod;
1572 int address_space;
1574 if (!evaluate_expression(deref))
1575 return NULL;
1576 if (!ident) {
1577 warning(expr->pos, "bad member name");
1578 return NULL;
1581 ctype = deref->ctype;
1582 address_space = ctype->ctype.as;
1583 mod = ctype->ctype.modifiers;
1584 if (ctype->type == SYM_NODE) {
1585 ctype = ctype->ctype.base_type;
1586 address_space |= ctype->ctype.as;
1587 mod |= ctype->ctype.modifiers;
1589 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1590 warning(expr->pos, "expected structure or union");
1591 return NULL;
1593 offset = 0;
1594 member = find_identifier(ident, ctype->symbol_list, &offset);
1595 if (!member) {
1596 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1597 const char *name = "<unnamed>";
1598 int namelen = 9;
1599 if (ctype->ident) {
1600 name = ctype->ident->name;
1601 namelen = ctype->ident->len;
1603 warning(expr->pos, "no member '%s' in %s %.*s",
1604 show_ident(ident), type, namelen, name);
1605 return NULL;
1609 * The member needs to take on the address space and modifiers of
1610 * the "parent" type.
1612 member = convert_to_as_mod(member, address_space, mod);
1613 ctype = get_base_type(member);
1615 if (!lvalue_expression(deref)) {
1616 if (deref->type != EXPR_SLICE) {
1617 expr->base = deref;
1618 expr->r_bitpos = 0;
1619 } else {
1620 expr->base = deref->base;
1621 expr->r_bitpos = deref->r_bitpos;
1623 expr->r_bitpos += offset << 3;
1624 expr->type = EXPR_SLICE;
1625 expr->r_nrbits = member->bit_size;
1626 expr->r_bitpos += member->bit_offset;
1627 expr->ctype = member;
1628 return member;
1631 deref = deref->unop;
1632 expr->deref = deref;
1634 add = evaluate_offset(deref, offset);
1635 expr->type = EXPR_PREOP;
1636 expr->op = '*';
1637 expr->unop = add;
1639 expr->ctype = member;
1640 return member;
1643 static int is_promoted(struct expression *expr)
1645 while (1) {
1646 switch (expr->type) {
1647 case EXPR_BINOP:
1648 case EXPR_SELECT:
1649 case EXPR_CONDITIONAL:
1650 return 1;
1651 case EXPR_COMMA:
1652 expr = expr->right;
1653 continue;
1654 case EXPR_PREOP:
1655 switch (expr->op) {
1656 case '(':
1657 expr = expr->unop;
1658 continue;
1659 case '+':
1660 case '-':
1661 case '~':
1662 return 1;
1663 default:
1664 return 0;
1666 default:
1667 return 0;
1673 static struct symbol *evaluate_cast(struct expression *);
1675 static struct symbol *evaluate_type_information(struct expression *expr)
1677 struct symbol *sym = expr->cast_type;
1678 if (!sym) {
1679 sym = evaluate_expression(expr->cast_expression);
1680 if (!sym)
1681 return NULL;
1683 * Expressions of restricted types will possibly get
1684 * promoted - check that here
1686 if (is_restricted_type(sym)) {
1687 if (sym->bit_size < bits_in_int && is_promoted(expr))
1688 sym = &int_ctype;
1691 examine_symbol_type(sym);
1692 if (is_bitfield_type(sym)) {
1693 warning(expr->pos, "trying to examine bitfield type");
1694 return NULL;
1696 return sym;
1699 static struct symbol *evaluate_sizeof(struct expression *expr)
1701 struct symbol *type;
1702 int size;
1704 type = evaluate_type_information(expr);
1705 if (!type)
1706 return NULL;
1708 size = type->bit_size;
1709 if ((size < 0) || (size & 7))
1710 warning(expr->pos, "cannot size expression");
1711 expr->type = EXPR_VALUE;
1712 expr->value = size >> 3;
1713 expr->ctype = size_t_ctype;
1714 return size_t_ctype;
1717 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1719 struct symbol *type;
1720 int size;
1722 type = evaluate_type_information(expr);
1723 if (!type)
1724 return NULL;
1726 if (type->type == SYM_NODE)
1727 type = type->ctype.base_type;
1728 if (!type)
1729 return NULL;
1730 switch (type->type) {
1731 case SYM_ARRAY:
1732 break;
1733 case SYM_PTR:
1734 type = get_base_type(type);
1735 if (type)
1736 break;
1737 default:
1738 warning(expr->pos, "expected pointer expression");
1739 return NULL;
1741 size = type->bit_size;
1742 if (size & 7)
1743 size = 0;
1744 expr->type = EXPR_VALUE;
1745 expr->value = size >> 3;
1746 expr->ctype = size_t_ctype;
1747 return size_t_ctype;
1750 static struct symbol *evaluate_alignof(struct expression *expr)
1752 struct symbol *type;
1754 type = evaluate_type_information(expr);
1755 if (!type)
1756 return NULL;
1758 expr->type = EXPR_VALUE;
1759 expr->value = type->ctype.alignment;
1760 expr->ctype = size_t_ctype;
1761 return size_t_ctype;
1764 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1766 struct expression *expr;
1767 struct symbol_list *argument_types = fn->arguments;
1768 struct symbol *argtype;
1769 int i = 1;
1771 PREPARE_PTR_LIST(argument_types, argtype);
1772 FOR_EACH_PTR (head, expr) {
1773 struct expression **p = THIS_ADDRESS(expr);
1774 struct symbol *ctype, *target;
1775 ctype = evaluate_expression(expr);
1777 if (!ctype)
1778 return 0;
1780 ctype = degenerate(expr);
1782 target = argtype;
1783 if (!target && ctype->bit_size < bits_in_int)
1784 target = &int_ctype;
1785 if (target) {
1786 static char where[30];
1787 examine_symbol_type(target);
1788 sprintf(where, "argument %d", i);
1789 compatible_assignment_types(expr, target, p, ctype, where, '=');
1792 i++;
1793 NEXT_PTR_LIST(argtype);
1794 } END_FOR_EACH_PTR(expr);
1795 FINISH_PTR_LIST(argtype);
1796 return 1;
1799 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1801 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1803 struct expression *entry = *ep;
1804 struct expression **parent, *reuse = NULL;
1805 unsigned long offset;
1806 struct symbol *sym;
1807 unsigned long from, to;
1808 int accept_string = is_byte_type(ctype);
1810 from = current;
1811 to = from+1;
1812 parent = ep;
1813 if (entry->type == EXPR_INDEX) {
1814 from = entry->idx_from;
1815 to = entry->idx_to+1;
1816 parent = &entry->idx_expression;
1817 reuse = entry;
1818 entry = entry->idx_expression;
1821 offset = from * (ctype->bit_size>>3);
1822 if (offset) {
1823 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1824 reuse->type = EXPR_POS;
1825 reuse->ctype = ctype;
1826 reuse->init_offset = offset;
1827 reuse->init_nr = to - from;
1828 reuse->init_expr = entry;
1829 parent = &reuse->init_expr;
1830 entry = reuse;
1832 *ep = entry;
1834 if (accept_string && entry->type == EXPR_STRING) {
1835 sym = evaluate_expression(entry);
1836 to = from + get_expression_value(sym->array_size);
1837 } else {
1838 evaluate_initializer(ctype, parent);
1840 return to;
1843 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1845 struct expression *entry;
1846 int current = 0;
1848 FOR_EACH_PTR(expr->expr_list, entry) {
1849 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1850 } END_FOR_EACH_PTR(entry);
1853 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1854 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1856 if (expression_list_size(expr->expr_list) != 1) {
1857 warning(expr->pos, "unexpected compound initializer");
1858 return;
1860 evaluate_array_initializer(ctype, expr);
1861 return;
1864 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1866 struct symbol *sym;
1868 FOR_EACH_PTR(ctype->symbol_list, sym) {
1869 if (sym->ident == ident)
1870 return sym;
1871 } END_FOR_EACH_PTR(sym);
1872 return NULL;
1875 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1877 struct expression *entry = *ep;
1878 struct expression **parent;
1879 struct expression *reuse = NULL;
1880 unsigned long offset;
1882 if (!sym) {
1883 error(entry->pos, "unknown named initializer");
1884 return -1;
1887 if (entry->type == EXPR_IDENTIFIER) {
1888 reuse = entry;
1889 entry = entry->ident_expression;
1892 parent = ep;
1893 offset = sym->offset;
1894 if (offset) {
1895 if (!reuse)
1896 reuse = alloc_expression(entry->pos, EXPR_POS);
1897 reuse->type = EXPR_POS;
1898 reuse->ctype = sym;
1899 reuse->init_offset = offset;
1900 reuse->init_nr = 1;
1901 reuse->init_expr = entry;
1902 parent = &reuse->init_expr;
1903 entry = reuse;
1905 *ep = entry;
1906 evaluate_initializer(sym, parent);
1907 return 0;
1910 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1912 struct expression *entry;
1913 struct symbol *sym;
1915 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1916 FOR_EACH_PTR(expr->expr_list, entry) {
1917 if (entry->type == EXPR_IDENTIFIER) {
1918 struct ident *ident = entry->expr_ident;
1919 /* We special-case the "already right place" case */
1920 if (!sym || sym->ident != ident) {
1921 RESET_PTR_LIST(sym);
1922 for (;;) {
1923 if (!sym)
1924 break;
1925 if (sym->ident == ident)
1926 break;
1927 NEXT_PTR_LIST(sym);
1931 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1932 return;
1933 NEXT_PTR_LIST(sym);
1934 } END_FOR_EACH_PTR(entry);
1935 FINISH_PTR_LIST(sym);
1939 * Initializers are kind of like assignments. Except
1940 * they can be a hell of a lot more complex.
1942 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1944 struct expression *expr = *ep;
1947 * Simple non-structure/array initializers are the simple
1948 * case, and look (and parse) largely like assignments.
1950 switch (expr->type) {
1951 default: {
1952 int is_string = expr->type == EXPR_STRING;
1953 struct symbol *rtype = evaluate_expression(expr);
1954 if (rtype) {
1956 * Special case:
1957 * char array[] = "string"
1958 * should _not_ degenerate.
1960 if (!is_string || !is_string_type(ctype))
1961 rtype = degenerate(expr);
1962 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1964 return;
1967 case EXPR_INITIALIZER:
1968 expr->ctype = ctype;
1969 if (ctype->type == SYM_NODE)
1970 ctype = ctype->ctype.base_type;
1972 switch (ctype->type) {
1973 case SYM_ARRAY:
1974 case SYM_PTR:
1975 evaluate_array_initializer(get_base_type(ctype), expr);
1976 return;
1977 case SYM_UNION:
1978 evaluate_struct_or_union_initializer(ctype, expr, 0);
1979 return;
1980 case SYM_STRUCT:
1981 evaluate_struct_or_union_initializer(ctype, expr, 1);
1982 return;
1983 default:
1984 evaluate_scalar_initializer(ctype, expr);
1985 return;
1988 case EXPR_IDENTIFIER:
1989 if (ctype->type == SYM_NODE)
1990 ctype = ctype->ctype.base_type;
1991 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1992 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1993 show_symbol(ctype);
1994 return;
1996 evaluate_one_struct_initializer(ctype, ep,
1997 find_struct_ident(ctype, expr->expr_ident));
1998 return;
2000 case EXPR_INDEX:
2001 if (ctype->type == SYM_NODE)
2002 ctype = ctype->ctype.base_type;
2003 if (ctype->type != SYM_ARRAY) {
2004 error(expr->pos, "expected array");
2005 return;
2007 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
2008 return;
2010 case EXPR_POS:
2012 * An EXPR_POS expression has already been evaluated, and we don't
2013 * need to do anything more
2015 return;
2019 static int get_as(struct symbol *sym)
2021 int as;
2022 unsigned long mod;
2024 if (!sym)
2025 return 0;
2026 as = sym->ctype.as;
2027 mod = sym->ctype.modifiers;
2028 if (sym->type == SYM_NODE) {
2029 sym = sym->ctype.base_type;
2030 as |= sym->ctype.as;
2031 mod |= sym->ctype.modifiers;
2035 * At least for now, allow casting to a "unsigned long".
2036 * That's how we do things like pointer arithmetic and
2037 * store pointers to registers.
2039 if (sym == &ulong_ctype)
2040 return -1;
2042 if (sym && sym->type == SYM_PTR) {
2043 sym = get_base_type(sym);
2044 as |= sym->ctype.as;
2045 mod |= sym->ctype.modifiers;
2047 if (mod & MOD_FORCE)
2048 return -1;
2049 return as;
2052 static struct symbol *evaluate_cast(struct expression *expr)
2054 struct expression *target = expr->cast_expression;
2055 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2056 enum type type;
2058 if (!target)
2059 return NULL;
2061 expr->ctype = ctype;
2062 expr->cast_type = ctype;
2065 * Special case: a cast can be followed by an
2066 * initializer, in which case we need to pass
2067 * the type value down to that initializer rather
2068 * than trying to evaluate it as an expression
2070 * A more complex case is when the initializer is
2071 * dereferenced as part of a post-fix expression.
2072 * We need to produce an expression that can be dereferenced.
2074 if (target->type == EXPR_INITIALIZER) {
2075 struct symbol *sym = expr->cast_type;
2076 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2078 sym->initializer = expr->cast_expression;
2079 evaluate_symbol(sym);
2081 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2082 addr->symbol = sym;
2084 expr->type = EXPR_PREOP;
2085 expr->op = '*';
2086 expr->unop = addr;
2087 expr->ctype = sym;
2089 return sym;
2092 evaluate_expression(target);
2093 degenerate(target);
2096 * You can always throw a value away by casting to
2097 * "void" - that's an implicit "force". Note that
2098 * the same is _not_ true of "void *".
2100 if (ctype == &void_ctype)
2101 goto out;
2103 type = ctype->type;
2104 if (type == SYM_NODE) {
2105 type = ctype->ctype.base_type->type;
2106 if (ctype->ctype.base_type == &void_ctype)
2107 goto out;
2109 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2110 warning(expr->pos, "cast to non-scalar");
2112 if (!target->ctype) {
2113 warning(expr->pos, "cast from unknown type");
2114 goto out;
2117 type = target->ctype->type;
2118 if (type == SYM_NODE)
2119 type = target->ctype->ctype.base_type->type;
2120 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2121 warning(expr->pos, "cast from non-scalar");
2123 if (!get_as(ctype) && get_as(target->ctype) > 0)
2124 warning(expr->pos, "cast removes address space of expression");
2126 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2127 struct symbol *t1 = ctype, *t2 = target->ctype;
2128 if (t1->type == SYM_NODE)
2129 t1 = t1->ctype.base_type;
2130 if (t2->type == SYM_NODE)
2131 t2 = t2->ctype.base_type;
2132 if (t1 != t2) {
2133 if (t1->type == SYM_RESTRICT)
2134 warning(expr->pos, "cast to restricted type");
2135 if (t2->type == SYM_RESTRICT)
2136 warning(expr->pos, "cast from restricted type");
2141 * Casts of constant values are special: they
2142 * can be NULL, and thus need to be simplified
2143 * early.
2145 if (target->type == EXPR_VALUE)
2146 cast_value(expr, ctype, target, target->ctype);
2148 out:
2149 return ctype;
2153 * Evaluate a call expression with a symbol. This
2154 * should expand inline functions, and evaluate
2155 * builtins.
2157 static int evaluate_symbol_call(struct expression *expr)
2159 struct expression *fn = expr->fn;
2160 struct symbol *ctype = fn->ctype;
2162 if (fn->type != EXPR_PREOP)
2163 return 0;
2165 if (ctype->op && ctype->op->evaluate)
2166 return ctype->op->evaluate(expr);
2168 if (ctype->ctype.modifiers & MOD_INLINE) {
2169 int ret;
2170 struct symbol *curr = current_fn;
2171 current_fn = ctype->ctype.base_type;
2172 examine_fn_arguments(current_fn);
2174 ret = inline_function(expr, ctype);
2176 /* restore the old function */
2177 current_fn = curr;
2178 return ret;
2181 return 0;
2184 static struct symbol *evaluate_call(struct expression *expr)
2186 int args, fnargs;
2187 struct symbol *ctype, *sym;
2188 struct expression *fn = expr->fn;
2189 struct expression_list *arglist = expr->args;
2191 if (!evaluate_expression(fn))
2192 return NULL;
2193 sym = ctype = fn->ctype;
2194 if (ctype->type == SYM_NODE)
2195 ctype = ctype->ctype.base_type;
2196 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2197 ctype = get_base_type(ctype);
2198 if (!evaluate_arguments(sym, ctype, arglist))
2199 return NULL;
2200 if (ctype->type != SYM_FN) {
2201 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2202 return NULL;
2204 args = expression_list_size(expr->args);
2205 fnargs = symbol_list_size(ctype->arguments);
2206 if (args < fnargs)
2207 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2208 if (args > fnargs && !ctype->variadic)
2209 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2210 if (sym->type == SYM_NODE) {
2211 if (evaluate_symbol_call(expr))
2212 return expr->ctype;
2214 expr->ctype = ctype->ctype.base_type;
2215 return expr->ctype;
2218 struct symbol *evaluate_expression(struct expression *expr)
2220 if (!expr)
2221 return NULL;
2222 if (expr->ctype)
2223 return expr->ctype;
2225 switch (expr->type) {
2226 case EXPR_VALUE:
2227 case EXPR_FVALUE:
2228 warning(expr->pos, "value expression without a type");
2229 return NULL;
2230 case EXPR_STRING:
2231 return evaluate_string(expr);
2232 case EXPR_SYMBOL:
2233 return evaluate_symbol_expression(expr);
2234 case EXPR_BINOP:
2235 if (!evaluate_expression(expr->left))
2236 return NULL;
2237 if (!evaluate_expression(expr->right))
2238 return NULL;
2239 return evaluate_binop(expr);
2240 case EXPR_LOGICAL:
2241 return evaluate_logical(expr);
2242 case EXPR_COMMA:
2243 evaluate_expression(expr->left);
2244 if (!evaluate_expression(expr->right))
2245 return NULL;
2246 return evaluate_comma(expr);
2247 case EXPR_COMPARE:
2248 if (!evaluate_expression(expr->left))
2249 return NULL;
2250 if (!evaluate_expression(expr->right))
2251 return NULL;
2252 return evaluate_compare(expr);
2253 case EXPR_ASSIGNMENT:
2254 if (!evaluate_expression(expr->left))
2255 return NULL;
2256 if (!evaluate_expression(expr->right))
2257 return NULL;
2258 return evaluate_assignment(expr);
2259 case EXPR_PREOP:
2260 if (!evaluate_expression(expr->unop))
2261 return NULL;
2262 return evaluate_preop(expr);
2263 case EXPR_POSTOP:
2264 if (!evaluate_expression(expr->unop))
2265 return NULL;
2266 return evaluate_postop(expr);
2267 case EXPR_CAST:
2268 case EXPR_IMPLIED_CAST:
2269 return evaluate_cast(expr);
2270 case EXPR_SIZEOF:
2271 return evaluate_sizeof(expr);
2272 case EXPR_PTRSIZEOF:
2273 return evaluate_ptrsizeof(expr);
2274 case EXPR_ALIGNOF:
2275 return evaluate_alignof(expr);
2276 case EXPR_DEREF:
2277 return evaluate_member_dereference(expr);
2278 case EXPR_CALL:
2279 return evaluate_call(expr);
2280 case EXPR_SELECT:
2281 case EXPR_CONDITIONAL:
2282 return evaluate_conditional_expression(expr);
2283 case EXPR_STATEMENT:
2284 expr->ctype = evaluate_statement(expr->statement);
2285 return expr->ctype;
2287 case EXPR_LABEL:
2288 expr->ctype = &ptr_ctype;
2289 return &ptr_ctype;
2291 case EXPR_TYPE:
2292 /* Evaluate the type of the symbol .. */
2293 evaluate_symbol(expr->symbol);
2294 /* .. but the type of the _expression_ is a "type" */
2295 expr->ctype = &type_ctype;
2296 return &type_ctype;
2298 /* These can not exist as stand-alone expressions */
2299 case EXPR_INITIALIZER:
2300 case EXPR_IDENTIFIER:
2301 case EXPR_INDEX:
2302 case EXPR_POS:
2303 warning(expr->pos, "internal front-end error: initializer in expression");
2304 return NULL;
2305 case EXPR_SLICE:
2306 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2307 return NULL;
2309 return NULL;
2312 static void check_duplicates(struct symbol *sym)
2314 int declared = 0;
2315 struct symbol *next = sym;
2317 while ((next = next->same_symbol) != NULL) {
2318 const char *typediff;
2319 evaluate_symbol(next);
2320 declared++;
2321 typediff = type_difference(sym, next, 0, 0);
2322 if (typediff) {
2323 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2324 show_ident(sym->ident),
2325 stream_name(next->pos.stream), next->pos.line, typediff);
2326 return;
2329 if (!declared) {
2330 unsigned long mod = sym->ctype.modifiers;
2331 if (mod & (MOD_STATIC | MOD_REGISTER))
2332 return;
2333 if (!(mod & MOD_TOPLEVEL))
2334 return;
2335 if (sym->ident == &main_ident)
2336 return;
2337 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2341 static struct symbol *evaluate_symbol(struct symbol *sym)
2343 struct symbol *base_type;
2345 if (!sym)
2346 return sym;
2348 sym = examine_symbol_type(sym);
2349 base_type = get_base_type(sym);
2350 if (!base_type)
2351 return NULL;
2353 /* Evaluate the initializers */
2354 if (sym->initializer)
2355 evaluate_initializer(sym, &sym->initializer);
2357 /* And finally, evaluate the body of the symbol too */
2358 if (base_type->type == SYM_FN) {
2359 struct symbol *curr = current_fn;
2361 current_fn = base_type;
2363 examine_fn_arguments(base_type);
2364 if (!base_type->stmt && base_type->inline_stmt)
2365 uninline(sym);
2366 if (base_type->stmt)
2367 evaluate_statement(base_type->stmt);
2369 current_fn = curr;
2372 return base_type;
2375 void evaluate_symbol_list(struct symbol_list *list)
2377 struct symbol *sym;
2379 FOR_EACH_PTR(list, sym) {
2380 check_duplicates(sym);
2381 evaluate_symbol(sym);
2382 } END_FOR_EACH_PTR(sym);
2385 static struct symbol *evaluate_return_expression(struct statement *stmt)
2387 struct expression *expr = stmt->expression;
2388 struct symbol *ctype, *fntype;
2390 evaluate_expression(expr);
2391 ctype = degenerate(expr);
2392 fntype = current_fn->ctype.base_type;
2393 if (!fntype || fntype == &void_ctype) {
2394 if (expr && ctype != &void_ctype)
2395 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2396 return NULL;
2399 if (!expr) {
2400 warning(stmt->pos, "return with no return value");
2401 return NULL;
2403 if (!ctype)
2404 return NULL;
2405 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2406 return NULL;
2409 static void evaluate_if_statement(struct statement *stmt)
2411 if (!stmt->if_conditional)
2412 return;
2414 evaluate_conditional(stmt->if_conditional, 0);
2415 evaluate_statement(stmt->if_true);
2416 evaluate_statement(stmt->if_false);
2419 static void evaluate_iterator(struct statement *stmt)
2421 evaluate_conditional(stmt->iterator_pre_condition, 1);
2422 evaluate_conditional(stmt->iterator_post_condition,1);
2423 evaluate_statement(stmt->iterator_pre_statement);
2424 evaluate_statement(stmt->iterator_statement);
2425 evaluate_statement(stmt->iterator_post_statement);
2428 static void verify_output_constraint(struct expression *expr, const char *constraint)
2430 switch (*constraint) {
2431 case '=': /* Assignment */
2432 case '+': /* Update */
2433 break;
2434 default:
2435 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2439 static void verify_input_constraint(struct expression *expr, const char *constraint)
2441 switch (*constraint) {
2442 case '=': /* Assignment */
2443 case '+': /* Update */
2444 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2448 static void evaluate_asm_statement(struct statement *stmt)
2450 struct expression *expr;
2451 int state;
2453 expr = stmt->asm_string;
2454 if (!expr || expr->type != EXPR_STRING) {
2455 warning(stmt->pos, "need constant string for inline asm");
2456 return;
2459 state = 0;
2460 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2461 struct ident *ident;
2463 switch (state) {
2464 case 0: /* Identifier */
2465 state = 1;
2466 ident = (struct ident *)expr;
2467 continue;
2469 case 1: /* Constraint */
2470 state = 2;
2471 if (!expr || expr->type != EXPR_STRING) {
2472 warning(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2473 *THIS_ADDRESS(expr) = NULL;
2474 continue;
2476 verify_output_constraint(expr, expr->string->data);
2477 continue;
2479 case 2: /* Expression */
2480 state = 0;
2481 if (!evaluate_expression(expr))
2482 return;
2483 if (!lvalue_expression(expr))
2484 warning(expr->pos, "asm output is not an lvalue");
2485 evaluate_assign_to(expr, expr->ctype);
2486 continue;
2488 } END_FOR_EACH_PTR(expr);
2490 state = 0;
2491 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2492 struct ident *ident;
2494 switch (state) {
2495 case 0: /* Identifier */
2496 state = 1;
2497 ident = (struct ident *)expr;
2498 continue;
2500 case 1: /* Constraint */
2501 state = 2;
2502 if (!expr || expr->type != EXPR_STRING) {
2503 warning(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2504 *THIS_ADDRESS(expr) = NULL;
2505 continue;
2507 verify_input_constraint(expr, expr->string->data);
2508 continue;
2510 case 2: /* Expression */
2511 state = 0;
2512 if (!evaluate_expression(expr))
2513 return;
2514 continue;
2516 } END_FOR_EACH_PTR(expr);
2518 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2519 if (!expr) {
2520 warning(stmt->pos, "bad asm output");
2521 return;
2523 if (expr->type == EXPR_STRING)
2524 continue;
2525 warning(expr->pos, "asm clobber is not a string");
2526 } END_FOR_EACH_PTR(expr);
2529 struct symbol *evaluate_statement(struct statement *stmt)
2531 if (!stmt)
2532 return NULL;
2534 switch (stmt->type) {
2535 case STMT_RETURN:
2536 return evaluate_return_expression(stmt);
2538 case STMT_EXPRESSION:
2539 if (!evaluate_expression(stmt->expression))
2540 return NULL;
2541 return degenerate(stmt->expression);
2543 case STMT_COMPOUND: {
2544 struct statement *s;
2545 struct symbol *type = NULL;
2546 struct symbol *sym;
2548 /* Evaluate each symbol in the compound statement */
2549 FOR_EACH_PTR(stmt->syms, sym) {
2550 evaluate_symbol(sym);
2551 } END_FOR_EACH_PTR(sym);
2552 evaluate_symbol(stmt->ret);
2555 * Then, evaluate each statement, making the type of the
2556 * compound statement be the type of the last statement
2558 type = NULL;
2559 FOR_EACH_PTR(stmt->stmts, s) {
2560 type = evaluate_statement(s);
2561 } END_FOR_EACH_PTR(s);
2562 if (!type)
2563 type = &void_ctype;
2564 return type;
2566 case STMT_IF:
2567 evaluate_if_statement(stmt);
2568 return NULL;
2569 case STMT_ITERATOR:
2570 evaluate_iterator(stmt);
2571 return NULL;
2572 case STMT_SWITCH:
2573 evaluate_expression(stmt->switch_expression);
2574 evaluate_statement(stmt->switch_statement);
2575 return NULL;
2576 case STMT_CASE:
2577 evaluate_expression(stmt->case_expression);
2578 evaluate_expression(stmt->case_to);
2579 evaluate_statement(stmt->case_statement);
2580 return NULL;
2581 case STMT_LABEL:
2582 return evaluate_statement(stmt->label_statement);
2583 case STMT_GOTO:
2584 evaluate_expression(stmt->goto_expression);
2585 return NULL;
2586 case STMT_NONE:
2587 break;
2588 case STMT_ASM:
2589 evaluate_asm_statement(stmt);
2590 return NULL;
2591 case STMT_CONTEXT:
2592 evaluate_expression(stmt->expression);
2593 return NULL;
2594 case STMT_RANGE:
2595 evaluate_expression(stmt->range_expression);
2596 evaluate_expression(stmt->range_low);
2597 evaluate_expression(stmt->range_high);
2598 return NULL;
2600 return NULL;