Give function name in non-ANSI declaration warning.
[smatch.git] / evaluate.c
blob89a1269709fab6416977c23d7b3622421a0d74a9
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 = sym->ctype.base_type;
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, difmod, oldas, newas;
200 old = base_type(old, &oldmod, &oldas);
201 new = base_type(new, &newmod, &newas);
202 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
203 if (old == new && oldas == newas && !difmod)
204 return 1;
205 if ((oldmod | newmod) & MOD_NOCAST) {
206 const char *tofrom = "to/from";
207 if (!(newmod & MOD_NOCAST))
208 tofrom = "from";
209 if (!(oldmod & MOD_NOCAST))
210 tofrom = "to";
211 warning(expr->pos, "implicit cast %s nocast type", tofrom);
213 return 0;
217 * This gets called for implicit casts in assignments and
218 * integer promotion. We often want to try to move the
219 * cast down, because the ops involved may have been
220 * implicitly cast up, and we can get rid of the casts
221 * early.
223 static struct expression * cast_to(struct expression *old, struct symbol *type)
225 struct expression *expr;
227 if (is_same_type(old, type))
228 return old;
231 * See if we can simplify the op. Move the cast down.
233 switch (old->type) {
234 case EXPR_PREOP:
235 if (old->op == '~') {
236 old->ctype = type;
237 old->unop = cast_to(old->unop, type);
238 return old;
240 break;
242 case EXPR_IMPLIED_CAST:
243 if (old->ctype->bit_size >= type->bit_size) {
244 struct expression *orig = old->cast_expression;
245 if (same_cast_type(orig->ctype, type))
246 return orig;
247 if (old->ctype->bit_offset == type->bit_offset) {
248 old->ctype = type;
249 old->cast_type = type;
250 return old;
253 break;
255 default:
256 /* nothing */;
259 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
260 expr->ctype = type;
261 expr->cast_type = type;
262 expr->cast_expression = old;
263 return expr;
266 static int is_type_type(struct symbol *type)
268 return (type->ctype.modifiers & MOD_TYPE) != 0;
271 int is_ptr_type(struct symbol *type)
273 if (type->type == SYM_NODE)
274 type = type->ctype.base_type;
275 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
278 static inline int is_float_type(struct symbol *type)
280 if (type->type == SYM_NODE)
281 type = type->ctype.base_type;
282 return type->ctype.base_type == &fp_type;
285 static inline int is_byte_type(struct symbol *type)
287 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
290 static inline int is_string_type(struct symbol *type)
292 if (type->type == SYM_NODE)
293 type = type->ctype.base_type;
294 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
297 static struct symbol *bad_expr_type(struct expression *expr)
299 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
300 switch (expr->type) {
301 case EXPR_BINOP:
302 case EXPR_COMPARE:
303 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
304 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
305 break;
306 case EXPR_PREOP:
307 case EXPR_POSTOP:
308 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
309 break;
310 default:
311 break;
314 return NULL;
317 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
319 struct expression *left = *lp, *right = *rp;
320 struct symbol *ltype = left->ctype, *rtype = right->ctype;
322 if (ltype->type == SYM_NODE)
323 ltype = ltype->ctype.base_type;
324 if (rtype->type == SYM_NODE)
325 rtype = rtype->ctype.base_type;
326 if (is_float_type(ltype)) {
327 if (is_int_type(rtype))
328 goto Left;
329 if (is_float_type(rtype)) {
330 unsigned long lmod = ltype->ctype.modifiers;
331 unsigned long rmod = rtype->ctype.modifiers;
332 lmod &= MOD_LONG | MOD_LONGLONG;
333 rmod &= MOD_LONG | MOD_LONGLONG;
334 if (lmod == rmod)
335 return ltype;
336 if (lmod & ~rmod)
337 goto Left;
338 else
339 goto Right;
341 return NULL;
343 if (!is_float_type(rtype) || !is_int_type(ltype))
344 return NULL;
345 Right:
346 *lp = cast_to(left, rtype);
347 return rtype;
348 Left:
349 *rp = cast_to(right, ltype);
350 return ltype;
353 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
355 struct expression *left = *lp, *right = *rp;
356 struct symbol *ltype = left->ctype, *rtype = right->ctype;
358 if (ltype->type == SYM_NODE)
359 ltype = ltype->ctype.base_type;
360 if (rtype->type == SYM_NODE)
361 rtype = rtype->ctype.base_type;
362 if (is_int_type(ltype) && is_int_type(rtype)) {
363 struct symbol *ctype = bigger_int_type(ltype, rtype);
365 *lp = cast_to(left, ctype);
366 *rp = cast_to(right, ctype);
367 return ctype;
369 return NULL;
372 static int restricted_value(struct expression *v, struct symbol *type)
374 if (v->type != EXPR_VALUE)
375 return 1;
376 if (v->value != 0)
377 return 1;
378 return 0;
381 static int restricted_binop(int op, struct symbol *type)
383 switch (op) {
384 case '&':
385 case '|':
386 case '^':
387 case '?':
388 case '=':
389 case SPECIAL_EQUAL:
390 case SPECIAL_NOTEQUAL:
391 case SPECIAL_AND_ASSIGN:
392 case SPECIAL_OR_ASSIGN:
393 case SPECIAL_XOR_ASSIGN:
394 return 0;
395 default:
396 return 1;
400 static int restricted_unop(int op, struct symbol *type)
402 if (op == '~' && type->bit_size >= bits_in_int)
403 return 0;
404 if (op == '+')
405 return 0;
406 return 1;
409 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
411 struct expression *left = *lp, *right = *rp;
412 struct symbol *ltype = left->ctype, *rtype = right->ctype;
413 struct symbol *type = NULL;
415 if (ltype->type == SYM_NODE)
416 ltype = ltype->ctype.base_type;
417 if (ltype->type == SYM_ENUM)
418 ltype = ltype->ctype.base_type;
419 if (rtype->type == SYM_NODE)
420 rtype = rtype->ctype.base_type;
421 if (rtype->type == SYM_ENUM)
422 rtype = rtype->ctype.base_type;
423 if (is_restricted_type(ltype)) {
424 if (is_restricted_type(rtype)) {
425 if (ltype == rtype)
426 type = ltype;
427 } else {
428 if (!restricted_value(right, ltype))
429 type = ltype;
431 } else if (is_restricted_type(rtype)) {
432 if (!restricted_value(left, rtype))
433 type = rtype;
435 if (!type)
436 return NULL;
437 if (restricted_binop(op, type))
438 return NULL;
439 return type;
442 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
444 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
445 if (!ctype && float_ok)
446 ctype = compatible_float_binop(&expr->left, &expr->right);
447 if (!ctype)
448 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
449 if (ctype) {
450 expr->ctype = ctype;
451 return ctype;
453 return bad_expr_type(expr);
456 static inline int lvalue_expression(struct expression *expr)
458 return expr->type == EXPR_PREOP && expr->op == '*';
461 static int ptr_object_size(struct symbol *ptr_type)
463 if (ptr_type->type == SYM_NODE)
464 ptr_type = ptr_type->ctype.base_type;
465 if (ptr_type->type == SYM_PTR)
466 ptr_type = ptr_type->ctype.base_type;
467 return ptr_type->bit_size;
470 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
472 struct expression *i = *ip;
473 struct symbol *ptr_type = ctype;
474 int bit_size;
476 if (ptr_type->type == SYM_NODE)
477 ptr_type = ptr_type->ctype.base_type;
479 if (!is_int_type(i->ctype))
480 return bad_expr_type(expr);
482 examine_symbol_type(ctype);
484 if (!ctype->ctype.base_type) {
485 warning(expr->pos, "missing type information");
486 return NULL;
489 /* Get the size of whatever the pointer points to */
490 bit_size = ptr_object_size(ctype);
492 if (bit_size > bits_in_char) {
493 int multiply = bit_size >> 3;
494 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
496 if (i->type == EXPR_VALUE) {
497 val->value = i->value * multiply;
498 val->ctype = size_t_ctype;
499 *ip = val;
500 } else {
501 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
503 val->ctype = size_t_ctype;
504 val->value = bit_size >> 3;
506 mul->op = '*';
507 mul->ctype = size_t_ctype;
508 mul->left = i;
509 mul->right = val;
511 *ip = mul;
515 expr->ctype = ctype;
516 return ctype;
519 static struct symbol *evaluate_add(struct expression *expr)
521 struct expression *left = expr->left, *right = expr->right;
522 struct symbol *ltype = left->ctype, *rtype = right->ctype;
524 if (is_ptr_type(ltype))
525 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
527 if (is_ptr_type(rtype))
528 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
530 return evaluate_arith(expr, 1);
533 const char * type_difference(struct symbol *target, struct symbol *source,
534 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
536 for (;;) {
537 unsigned long mod1, mod2, diff;
538 unsigned long as1, as2;
539 int type1, type2;
540 struct symbol *base1, *base2;
542 if (target == source)
543 break;
544 if (!target || !source)
545 return "different types";
547 * Peel of per-node information.
548 * FIXME! Check alignment and context too here!
550 mod1 = target->ctype.modifiers;
551 as1 = target->ctype.as;
552 mod2 = source->ctype.modifiers;
553 as2 = source->ctype.as;
554 if (target->type == SYM_NODE) {
555 target = target->ctype.base_type;
556 if (!target)
557 return "bad types";
558 if (target->type == SYM_PTR) {
559 mod1 = 0;
560 as1 = 0;
562 mod1 |= target->ctype.modifiers;
563 as1 |= target->ctype.as;
565 if (source->type == SYM_NODE) {
566 source = source->ctype.base_type;
567 if (!source)
568 return "bad types";
569 if (source->type == SYM_PTR) {
570 mod2 = 0;
571 as2 = 0;
573 mod2 |= source->ctype.modifiers;
574 as2 |= source->ctype.as;
576 if (target->type == SYM_ENUM) {
577 target = target->ctype.base_type;
578 if (!target)
579 return "bad types";
581 if (source->type == SYM_ENUM) {
582 source = source->ctype.base_type;
583 if (!source)
584 return "bad types";
587 if (target == source)
588 break;
589 if (!target || !source)
590 return "different types";
592 type1 = target->type;
593 base1 = target->ctype.base_type;
595 type2 = source->type;
596 base2 = source->ctype.base_type;
599 * Pointers to functions compare as the function itself
601 if (type1 == SYM_PTR && base1) {
602 switch (base1->type) {
603 case SYM_FN:
604 type1 = SYM_FN;
605 target = base1;
606 base1 = base1->ctype.base_type;
607 default:
608 /* nothing */;
611 if (type2 == SYM_PTR && base2) {
612 switch (base2->type) {
613 case SYM_FN:
614 type2 = SYM_FN;
615 source = base2;
616 base2 = base2->ctype.base_type;
617 default:
618 /* nothing */;
622 /* Arrays degenerate to pointers for type comparisons */
623 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
624 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
626 if (type1 != type2 || type1 == SYM_RESTRICT)
627 return "different base types";
629 /* Must be same address space to be comparable */
630 if (as1 != as2)
631 return "different address spaces";
633 /* Ignore differences in storage types or addressability */
634 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
635 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
636 if (diff) {
637 if (diff & MOD_SIZE)
638 return "different type sizes";
639 if (diff & ~MOD_SIGNEDNESS)
640 return "different modifiers";
642 /* Differs in signedness only.. */
643 if (Wtypesign) {
645 * Warn if both are explicitly signed ("unsigned" is obvously
646 * always explicit, and since we know one of them has to be
647 * unsigned, we check if the signed one was explicit).
649 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
650 return "different explicit signedness";
653 * "char" matches both "unsigned char" and "signed char",
654 * so if the explicit test didn't trigger, then we should
655 * not warn about a char.
657 if (!(mod1 & MOD_CHAR))
658 return "different signedness";
662 if (type1 == SYM_FN) {
663 int i;
664 struct symbol *arg1, *arg2;
665 if (base1->variadic != base2->variadic)
666 return "incompatible variadic arguments";
667 PREPARE_PTR_LIST(target->arguments, arg1);
668 PREPARE_PTR_LIST(source->arguments, arg2);
669 i = 1;
670 for (;;) {
671 const char *diff;
672 diff = type_difference(arg1, arg2, 0, 0);
673 if (diff) {
674 static char argdiff[80];
675 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
676 return argdiff;
678 if (!arg1)
679 break;
680 NEXT_PTR_LIST(arg1);
681 NEXT_PTR_LIST(arg2);
682 i++;
684 FINISH_PTR_LIST(arg2);
685 FINISH_PTR_LIST(arg1);
688 target = base1;
689 source = base2;
691 return NULL;
694 static int is_null_ptr(struct expression *expr)
696 if (expr->type != EXPR_VALUE || expr->value)
697 return 0;
698 if (!is_ptr_type(expr->ctype))
699 warning(expr->pos, "Using plain integer as NULL pointer");
700 return 1;
703 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
705 /* NULL expression? Just return the type of the "other side" */
706 if (is_null_ptr(r))
707 return l->ctype;
708 if (is_null_ptr(l))
709 return r->ctype;
710 return NULL;
714 * Ignore differences in "volatile" and "const"ness when
715 * subtracting pointers
717 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
719 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
721 const char *typediff;
722 struct symbol *ctype;
723 struct symbol *ltype, *rtype;
724 struct expression *r = *rp;
726 ltype = degenerate(l);
727 rtype = degenerate(r);
730 * If it is an integer subtract: the ptr add case will do the
731 * right thing.
733 if (!is_ptr_type(rtype))
734 return evaluate_ptr_add(expr, degenerate(l), rp);
736 ctype = ltype;
737 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
738 if (typediff) {
739 ctype = common_ptr_type(l, r);
740 if (!ctype) {
741 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
742 return NULL;
745 examine_symbol_type(ctype);
747 /* Figure out the base type we point to */
748 if (ctype->type == SYM_NODE)
749 ctype = ctype->ctype.base_type;
750 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
751 warning(expr->pos, "subtraction of functions? Share your drugs");
752 return NULL;
754 ctype = ctype->ctype.base_type;
756 expr->ctype = ssize_t_ctype;
757 if (ctype->bit_size > bits_in_char) {
758 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
759 struct expression *div = expr;
760 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
761 unsigned long value = ctype->bit_size >> 3;
763 val->ctype = size_t_ctype;
764 val->value = value;
766 if (value & (value-1)) {
767 if (Wptr_subtraction_blows)
768 warning(expr->pos, "potentially expensive pointer subtraction");
771 sub->op = '-';
772 sub->ctype = ssize_t_ctype;
773 sub->left = l;
774 sub->right = r;
776 div->op = '/';
777 div->left = sub;
778 div->right = val;
781 return ssize_t_ctype;
784 static struct symbol *evaluate_sub(struct expression *expr)
786 struct expression *left = expr->left;
787 struct symbol *ltype = left->ctype;
789 if (is_ptr_type(ltype))
790 return evaluate_ptr_sub(expr, left, &expr->right);
792 return evaluate_arith(expr, 1);
795 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
797 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
799 struct symbol *ctype;
801 if (!expr)
802 return NULL;
804 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
805 warning(expr->pos, "assignment expression in conditional");
807 ctype = evaluate_expression(expr);
808 if (ctype) {
809 if (is_safe_type(ctype))
810 warning(expr->pos, "testing a 'safe expression'");
813 return ctype;
816 static struct symbol *evaluate_logical(struct expression *expr)
818 if (!evaluate_conditional(expr->left, 0))
819 return NULL;
820 if (!evaluate_conditional(expr->right, 0))
821 return NULL;
823 expr->ctype = &bool_ctype;
824 return &bool_ctype;
827 static struct symbol *evaluate_shift(struct expression *expr)
829 struct expression *left = expr->left, *right = expr->right;
830 struct symbol *ltype = left->ctype, *rtype = right->ctype;
832 if (ltype->type == SYM_NODE)
833 ltype = ltype->ctype.base_type;
834 if (rtype->type == SYM_NODE)
835 rtype = rtype->ctype.base_type;
836 if (is_int_type(ltype) && is_int_type(rtype)) {
837 struct symbol *ctype = integer_promotion(ltype);
838 expr->left = cast_to(expr->left, ctype);
839 expr->ctype = ctype;
840 ctype = integer_promotion(rtype);
841 expr->right = cast_to(expr->right, ctype);
842 return expr->ctype;
844 return bad_expr_type(expr);
847 static struct symbol *evaluate_binop(struct expression *expr)
849 switch (expr->op) {
850 // addition can take ptr+int, fp and int
851 case '+':
852 return evaluate_add(expr);
854 // subtraction can take ptr-ptr, fp and int
855 case '-':
856 return evaluate_sub(expr);
858 // Arithmetic operations can take fp and int
859 case '*': case '/':
860 return evaluate_arith(expr, 1);
862 // shifts do integer promotions, but that's it.
863 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
864 return evaluate_shift(expr);
866 // The rest are integer operations
867 // '%', '&', '^', '|'
868 default:
869 return evaluate_arith(expr, 0);
873 static struct symbol *evaluate_comma(struct expression *expr)
875 expr->ctype = expr->right->ctype;
876 return expr->ctype;
879 static int modify_for_unsigned(int op)
881 if (op == '<')
882 op = SPECIAL_UNSIGNED_LT;
883 else if (op == '>')
884 op = SPECIAL_UNSIGNED_GT;
885 else if (op == SPECIAL_LTE)
886 op = SPECIAL_UNSIGNED_LTE;
887 else if (op == SPECIAL_GTE)
888 op = SPECIAL_UNSIGNED_GTE;
889 return op;
892 static struct symbol *evaluate_compare(struct expression *expr)
894 struct expression *left = expr->left, *right = expr->right;
895 struct symbol *ltype = left->ctype, *rtype = right->ctype;
896 struct symbol *ctype;
898 /* Type types? */
899 if (is_type_type(ltype) && is_type_type(rtype))
900 goto OK;
902 if (is_safe_type(ltype) || is_safe_type(rtype))
903 warning(expr->pos, "testing a 'safe expression'");
905 /* Pointer types? */
906 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
907 // FIXME! Check the types for compatibility
908 expr->op = modify_for_unsigned(expr->op);
909 goto OK;
912 ctype = compatible_integer_binop(&expr->left, &expr->right);
913 if (ctype) {
914 if (ctype->ctype.modifiers & MOD_UNSIGNED)
915 expr->op = modify_for_unsigned(expr->op);
916 goto OK;
919 ctype = compatible_float_binop(&expr->left, &expr->right);
920 if (ctype)
921 goto OK;
923 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
924 if (ctype) {
925 if (ctype->ctype.modifiers & MOD_UNSIGNED)
926 expr->op = modify_for_unsigned(expr->op);
927 goto OK;
930 bad_expr_type(expr);
933 expr->ctype = &bool_ctype;
934 return &bool_ctype;
938 * FIXME!! This should do casts, array degeneration etc..
940 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
942 struct symbol *ltype = left->ctype, *rtype = right->ctype;
944 if (ltype->type == SYM_NODE)
945 ltype = ltype->ctype.base_type;
947 if (rtype->type == SYM_NODE)
948 rtype = rtype->ctype.base_type;
950 if (ltype->type == SYM_PTR) {
951 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
952 return ltype;
955 if (rtype->type == SYM_PTR) {
956 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
957 return rtype;
959 return NULL;
963 * NOTE! The degenerate case of "x ? : y", where we don't
964 * have a true case, this will possibly promote "x" to the
965 * same type as "y", and thus _change_ the conditional
966 * test in the expression. But since promotion is "safe"
967 * for testing, that's ok.
969 static struct symbol *evaluate_conditional_expression(struct expression *expr)
971 struct expression **true;
972 struct symbol *ctype, *ltype, *rtype;
973 const char * typediff;
975 if (!evaluate_conditional(expr->conditional, 0))
976 return NULL;
977 if (!evaluate_expression(expr->cond_false))
978 return NULL;
980 ctype = degenerate(expr->conditional);
981 rtype = degenerate(expr->cond_false);
983 true = &expr->conditional;
984 ltype = ctype;
985 if (expr->cond_true) {
986 if (!evaluate_expression(expr->cond_true))
987 return NULL;
988 ltype = degenerate(expr->cond_true);
989 true = &expr->cond_true;
992 ctype = compatible_integer_binop(true, &expr->cond_false);
993 if (ctype)
994 goto out;
995 ctype = compatible_ptr_type(*true, expr->cond_false);
996 if (ctype)
997 goto out;
998 ctype = compatible_float_binop(true, &expr->cond_false);
999 if (ctype)
1000 goto out;
1001 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1002 if (ctype)
1003 goto out;
1004 ctype = ltype;
1005 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1006 if (!typediff)
1007 goto out;
1008 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1009 return NULL;
1011 out:
1012 expr->ctype = ctype;
1013 return ctype;
1016 /* FP assignments can not do modulo or bit operations */
1017 static int compatible_float_op(int op)
1019 return op == '=' ||
1020 op == SPECIAL_ADD_ASSIGN ||
1021 op == SPECIAL_SUB_ASSIGN ||
1022 op == SPECIAL_MUL_ASSIGN ||
1023 op == SPECIAL_DIV_ASSIGN;
1026 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1027 struct expression **rp, struct symbol *source, const char *where, int op)
1029 const char *typediff;
1030 struct symbol *t;
1031 int target_as;
1033 if (is_int_type(target)) {
1034 if (is_int_type(source))
1035 goto Cast;
1036 if (is_float_type(source))
1037 goto Cast;
1038 } else if (is_float_type(target)) {
1039 if (!compatible_float_op(op)) {
1040 warning(expr->pos, "invalid assignment");
1041 return 0;
1043 if (is_int_type(source))
1044 goto Cast;
1045 if (is_float_type(source))
1046 goto Cast;
1047 } else if (is_restricted_type(target)) {
1048 if (restricted_binop(op, target)) {
1049 warning(expr->pos, "bad restricted assignment");
1050 return 0;
1052 if (!restricted_value(*rp, target))
1053 return 1;
1054 } else if (is_ptr_type(target)) {
1055 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1056 evaluate_ptr_add(expr, target, rp);
1057 return 1;
1059 if (op != '=') {
1060 warning(expr->pos, "invalid pointer assignment");
1061 return 0;
1063 } else if (op != '=') {
1064 warning(expr->pos, "invalid assignment");
1065 return 0;
1068 /* It's ok if the target is more volatile or const than the source */
1069 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1070 if (!typediff)
1071 return 1;
1073 /* Pointer destination? */
1074 t = target;
1075 target_as = t->ctype.as;
1076 if (t->type == SYM_NODE) {
1077 t = t->ctype.base_type;
1078 target_as |= t->ctype.as;
1080 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1081 struct expression *right = *rp;
1082 struct symbol *s = source;
1083 int source_as;
1085 // NULL pointer is always ok
1086 if (is_null_ptr(right))
1087 goto Cast;
1089 /* "void *" matches anything as long as the address space is ok */
1090 source_as = s->ctype.as;
1091 if (s->type == SYM_NODE) {
1092 s = s->ctype.base_type;
1093 source_as |= s->ctype.as;
1095 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1096 s = s->ctype.base_type;
1097 t = t->ctype.base_type;
1098 if (s == &void_ctype || t == &void_ctype)
1099 goto Cast;
1103 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1104 info(expr->pos, " expected %s", show_typename(target));
1105 info(expr->pos, " got %s", show_typename(source));
1106 *rp = cast_to(*rp, target);
1107 return 0;
1108 Cast:
1109 *rp = cast_to(*rp, target);
1110 return 1;
1113 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1115 if (type->ctype.modifiers & MOD_CONST)
1116 warning(left->pos, "assignment to const expression");
1117 if (type->type == SYM_NODE)
1118 type->ctype.modifiers |= MOD_ASSIGNED;
1121 static struct symbol *evaluate_assignment(struct expression *expr)
1123 struct expression *left = expr->left, *right = expr->right;
1124 struct expression *where = expr;
1125 struct symbol *ltype, *rtype;
1127 if (!lvalue_expression(left)) {
1128 warning(expr->pos, "not an lvalue");
1129 return NULL;
1132 ltype = left->ctype;
1134 rtype = degenerate(right);
1136 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1137 return NULL;
1139 evaluate_assign_to(left, ltype);
1141 expr->ctype = ltype;
1142 return ltype;
1145 static void examine_fn_arguments(struct symbol *fn)
1147 struct symbol *s;
1149 FOR_EACH_PTR(fn->arguments, s) {
1150 struct symbol *arg = evaluate_symbol(s);
1151 /* Array/function arguments silently degenerate into pointers */
1152 if (arg) {
1153 struct symbol *ptr;
1154 switch(arg->type) {
1155 case SYM_ARRAY:
1156 case SYM_FN:
1157 ptr = alloc_symbol(s->pos, SYM_PTR);
1158 if (arg->type == SYM_ARRAY)
1159 ptr->ctype = arg->ctype;
1160 else
1161 ptr->ctype.base_type = arg;
1162 ptr->ctype.as |= s->ctype.as;
1163 ptr->ctype.modifiers |= s->ctype.modifiers;
1165 s->ctype.base_type = ptr;
1166 s->ctype.as = 0;
1167 s->ctype.modifiers = 0;
1168 s->bit_size = 0;
1169 s->examined = 0;
1170 examine_symbol_type(s);
1171 break;
1172 default:
1173 /* nothing */
1174 break;
1177 } END_FOR_EACH_PTR(s);
1180 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1182 /* Take the modifiers of the pointer, and apply them to the member */
1183 mod |= sym->ctype.modifiers;
1184 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1185 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1186 *newsym = *sym;
1187 newsym->ctype.as = as;
1188 newsym->ctype.modifiers = mod;
1189 sym = newsym;
1191 return sym;
1194 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1196 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1197 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1199 node->ctype.base_type = ptr;
1200 ptr->bit_size = bits_in_pointer;
1201 ptr->ctype.alignment = pointer_alignment;
1203 node->bit_size = bits_in_pointer;
1204 node->ctype.alignment = pointer_alignment;
1206 access_symbol(sym);
1207 if (sym->ctype.modifiers & MOD_REGISTER) {
1208 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1209 sym->ctype.modifiers &= ~MOD_REGISTER;
1211 if (sym->type == SYM_NODE) {
1212 ptr->ctype.as |= sym->ctype.as;
1213 ptr->ctype.modifiers |= sym->ctype.modifiers;
1214 sym = sym->ctype.base_type;
1216 if (degenerate && sym->type == SYM_ARRAY) {
1217 ptr->ctype.as |= sym->ctype.as;
1218 ptr->ctype.modifiers |= sym->ctype.modifiers;
1219 sym = sym->ctype.base_type;
1221 ptr->ctype.base_type = sym;
1223 return node;
1226 /* Arrays degenerate into pointers on pointer arithmetic */
1227 static struct symbol *degenerate(struct expression *expr)
1229 struct symbol *ctype, *base;
1231 if (!expr)
1232 return NULL;
1233 ctype = expr->ctype;
1234 if (!ctype)
1235 return NULL;
1236 base = ctype;
1237 if (ctype->type == SYM_NODE)
1238 base = ctype->ctype.base_type;
1240 * Arrays degenerate into pointers to the entries, while
1241 * functions degenerate into pointers to themselves.
1242 * If array was part of non-lvalue compound, we create a copy
1243 * of that compound first and then act as if we were dealing with
1244 * the corresponding field in there.
1246 switch (base->type) {
1247 case SYM_ARRAY:
1248 if (expr->type == EXPR_SLICE) {
1249 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1250 struct expression *e0, *e1, *e2, *e3, *e4;
1252 a->ctype.base_type = expr->base->ctype;
1253 a->bit_size = expr->base->ctype->bit_size;
1254 a->array_size = expr->base->ctype->array_size;
1256 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1257 e0->symbol = a;
1258 e0->ctype = &lazy_ptr_ctype;
1260 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1261 e1->unop = e0;
1262 e1->op = '*';
1263 e1->ctype = expr->base->ctype; /* XXX */
1265 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1266 e2->left = e1;
1267 e2->right = expr->base;
1268 e2->op = '=';
1269 e2->ctype = expr->base->ctype;
1271 if (expr->r_bitpos) {
1272 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1273 e3->op = '+';
1274 e3->left = e0;
1275 e3->right = alloc_const_expression(expr->pos,
1276 expr->r_bitpos >> 3);
1277 e3->ctype = &lazy_ptr_ctype;
1278 } else {
1279 e3 = e0;
1282 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1283 e4->left = e2;
1284 e4->right = e3;
1285 e4->ctype = &lazy_ptr_ctype;
1287 expr->unop = e4;
1288 expr->type = EXPR_PREOP;
1289 expr->op = '*';
1291 case SYM_FN:
1292 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1293 warning(expr->pos, "strange non-value function or array");
1294 return &bad_ctype;
1296 *expr = *expr->unop;
1297 ctype = create_pointer(expr, ctype, 1);
1298 expr->ctype = ctype;
1299 default:
1300 /* nothing */;
1302 return ctype;
1305 static struct symbol *evaluate_addressof(struct expression *expr)
1307 struct expression *op = expr->unop;
1308 struct symbol *ctype;
1310 if (op->op != '*' || op->type != EXPR_PREOP) {
1311 warning(expr->pos, "not addressable");
1312 return NULL;
1314 ctype = op->ctype;
1315 *expr = *op->unop;
1317 if (expr->type == EXPR_SYMBOL) {
1318 struct symbol *sym = expr->symbol;
1319 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1323 * symbol expression evaluation is lazy about the type
1324 * of the sub-expression, so we may have to generate
1325 * the type here if so..
1327 if (expr->ctype == &lazy_ptr_ctype) {
1328 ctype = create_pointer(expr, ctype, 0);
1329 expr->ctype = ctype;
1331 return expr->ctype;
1335 static struct symbol *evaluate_dereference(struct expression *expr)
1337 struct expression *op = expr->unop;
1338 struct symbol *ctype = op->ctype, *node, *target;
1340 /* Simplify: *&(expr) => (expr) */
1341 if (op->type == EXPR_PREOP && op->op == '&') {
1342 *expr = *op->unop;
1343 return expr->ctype;
1346 /* Dereferencing a node drops all the node information. */
1347 if (ctype->type == SYM_NODE)
1348 ctype = ctype->ctype.base_type;
1350 node = alloc_symbol(expr->pos, SYM_NODE);
1351 target = ctype->ctype.base_type;
1353 switch (ctype->type) {
1354 default:
1355 warning(expr->pos, "cannot derefence this type");
1356 return NULL;
1357 case SYM_PTR:
1358 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1359 merge_type(node, ctype);
1360 break;
1362 case SYM_ARRAY:
1363 if (!lvalue_expression(op)) {
1364 warning(op->pos, "non-lvalue array??");
1365 return NULL;
1368 /* Do the implied "addressof" on the array */
1369 *op = *op->unop;
1372 * When an array is dereferenced, we need to pick
1373 * up the attributes of the original node too..
1375 merge_type(node, op->ctype);
1376 merge_type(node, ctype);
1377 break;
1380 node->bit_size = target->bit_size;
1381 node->array_size = target->array_size;
1383 expr->ctype = node;
1384 return node;
1388 * Unary post-ops: x++ and x--
1390 static struct symbol *evaluate_postop(struct expression *expr)
1392 struct expression *op = expr->unop;
1393 struct symbol *ctype = op->ctype;
1395 if (!lvalue_expression(expr->unop)) {
1396 warning(expr->pos, "need lvalue expression for ++/--");
1397 return NULL;
1399 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1400 warning(expr->pos, "bad operation on restricted");
1401 return NULL;
1404 evaluate_assign_to(op, ctype);
1406 expr->ctype = ctype;
1407 expr->op_value = 1;
1408 if (is_ptr_type(ctype))
1409 expr->op_value = ptr_object_size(ctype) >> 3;
1411 return ctype;
1414 static struct symbol *evaluate_sign(struct expression *expr)
1416 struct symbol *ctype = expr->unop->ctype;
1417 if (is_int_type(ctype)) {
1418 struct symbol *rtype = rtype = integer_promotion(ctype);
1419 expr->unop = cast_to(expr->unop, rtype);
1420 ctype = rtype;
1421 } else if (is_float_type(ctype) && expr->op != '~') {
1422 /* no conversions needed */
1423 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1424 /* no conversions needed */
1425 } else {
1426 return bad_expr_type(expr);
1428 if (expr->op == '+')
1429 *expr = *expr->unop;
1430 expr->ctype = ctype;
1431 return ctype;
1434 static struct symbol *evaluate_preop(struct expression *expr)
1436 struct symbol *ctype = expr->unop->ctype;
1438 switch (expr->op) {
1439 case '(':
1440 *expr = *expr->unop;
1441 return ctype;
1443 case '+':
1444 case '-':
1445 case '~':
1446 return evaluate_sign(expr);
1448 case '*':
1449 return evaluate_dereference(expr);
1451 case '&':
1452 return evaluate_addressof(expr);
1454 case SPECIAL_INCREMENT:
1455 case SPECIAL_DECREMENT:
1457 * From a type evaluation standpoint the pre-ops are
1458 * the same as the postops
1460 return evaluate_postop(expr);
1462 case '!':
1463 if (is_safe_type(ctype))
1464 warning(expr->pos, "testing a 'safe expression'");
1465 if (is_float_type(ctype)) {
1466 struct expression *arg = expr->unop;
1467 expr->type = EXPR_BINOP;
1468 expr->op = SPECIAL_EQUAL;
1469 expr->left = arg;
1470 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1471 expr->right->ctype = ctype;
1472 expr->right->fvalue = 0;
1474 ctype = &bool_ctype;
1475 break;
1477 default:
1478 break;
1480 expr->ctype = ctype;
1481 return &bool_ctype;
1484 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1486 struct ptr_list *head = (struct ptr_list *)_list;
1487 struct ptr_list *list = head;
1489 if (!head)
1490 return NULL;
1491 do {
1492 int i;
1493 for (i = 0; i < list->nr; i++) {
1494 struct symbol *sym = (struct symbol *) list->list[i];
1495 if (sym->ident) {
1496 if (sym->ident != ident)
1497 continue;
1498 *offset = sym->offset;
1499 return sym;
1500 } else {
1501 struct symbol *ctype = sym->ctype.base_type;
1502 struct symbol *sub;
1503 if (!ctype)
1504 continue;
1505 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1506 continue;
1507 sub = find_identifier(ident, ctype->symbol_list, offset);
1508 if (!sub)
1509 continue;
1510 *offset += sym->offset;
1511 return sub;
1514 } while ((list = list->next) != head);
1515 return NULL;
1518 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1520 struct expression *add;
1523 * Create a new add-expression
1525 * NOTE! Even if we just add zero, we need a new node
1526 * for the member pointer, since it has a different
1527 * type than the original pointer. We could make that
1528 * be just a cast, but the fact is, a node is a node,
1529 * so we might as well just do the "add zero" here.
1531 add = alloc_expression(expr->pos, EXPR_BINOP);
1532 add->op = '+';
1533 add->left = expr;
1534 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1535 add->right->ctype = &int_ctype;
1536 add->right->value = offset;
1539 * The ctype of the pointer will be lazily evaluated if
1540 * we ever take the address of this member dereference..
1542 add->ctype = &lazy_ptr_ctype;
1543 return add;
1546 /* structure/union dereference */
1547 static struct symbol *evaluate_member_dereference(struct expression *expr)
1549 int offset;
1550 struct symbol *ctype, *member;
1551 struct expression *deref = expr->deref, *add;
1552 struct ident *ident = expr->member;
1553 unsigned int mod;
1554 int address_space;
1556 if (!evaluate_expression(deref))
1557 return NULL;
1558 if (!ident) {
1559 warning(expr->pos, "bad member name");
1560 return NULL;
1563 ctype = deref->ctype;
1564 address_space = ctype->ctype.as;
1565 mod = ctype->ctype.modifiers;
1566 if (ctype->type == SYM_NODE) {
1567 ctype = ctype->ctype.base_type;
1568 address_space |= ctype->ctype.as;
1569 mod |= ctype->ctype.modifiers;
1571 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1572 warning(expr->pos, "expected structure or union");
1573 return NULL;
1575 offset = 0;
1576 member = find_identifier(ident, ctype->symbol_list, &offset);
1577 if (!member) {
1578 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1579 const char *name = "<unnamed>";
1580 int namelen = 9;
1581 if (ctype->ident) {
1582 name = ctype->ident->name;
1583 namelen = ctype->ident->len;
1585 warning(expr->pos, "no member '%s' in %s %.*s",
1586 show_ident(ident), type, namelen, name);
1587 return NULL;
1591 * The member needs to take on the address space and modifiers of
1592 * the "parent" type.
1594 member = convert_to_as_mod(member, address_space, mod);
1595 ctype = member->ctype.base_type;
1597 if (!lvalue_expression(deref)) {
1598 if (deref->type != EXPR_SLICE) {
1599 expr->base = deref;
1600 expr->r_bitpos = 0;
1601 } else {
1602 expr->base = deref->base;
1603 expr->r_bitpos = deref->r_bitpos;
1605 expr->r_bitpos += offset << 3;
1606 expr->type = EXPR_SLICE;
1607 expr->r_nrbits = member->bit_size;
1608 expr->r_bitpos += member->bit_offset;
1609 expr->ctype = member;
1610 return member;
1613 deref = deref->unop;
1614 expr->deref = deref;
1616 add = evaluate_offset(deref, offset);
1617 expr->type = EXPR_PREOP;
1618 expr->op = '*';
1619 expr->unop = add;
1621 expr->ctype = member;
1622 return member;
1625 static int is_promoted(struct expression *expr)
1627 while (1) {
1628 switch (expr->type) {
1629 case EXPR_BINOP:
1630 case EXPR_SELECT:
1631 case EXPR_CONDITIONAL:
1632 return 1;
1633 case EXPR_COMMA:
1634 expr = expr->right;
1635 continue;
1636 case EXPR_PREOP:
1637 switch (expr->op) {
1638 case '(':
1639 expr = expr->unop;
1640 continue;
1641 case '+':
1642 case '-':
1643 case '~':
1644 return 1;
1645 default:
1646 return 0;
1648 default:
1649 return 0;
1655 static struct symbol *evaluate_cast(struct expression *);
1657 static struct symbol *evaluate_type_information(struct expression *expr)
1659 struct symbol *sym = expr->cast_type;
1660 if (!sym) {
1661 sym = evaluate_expression(expr->cast_expression);
1662 if (!sym)
1663 return NULL;
1665 * Expressions of restricted types will possibly get
1666 * promoted - check that here
1668 if (is_restricted_type(sym)) {
1669 if (sym->bit_size < bits_in_int && is_promoted(expr))
1670 sym = &int_ctype;
1673 examine_symbol_type(sym);
1674 if (is_bitfield_type(sym)) {
1675 warning(expr->pos, "trying to examine bitfield type");
1676 return NULL;
1678 return sym;
1681 static struct symbol *evaluate_sizeof(struct expression *expr)
1683 struct symbol *type;
1684 int size;
1686 type = evaluate_type_information(expr);
1687 if (!type)
1688 return NULL;
1690 size = type->bit_size;
1691 if ((size < 0) || (size & 7))
1692 warning(expr->pos, "cannot size expression");
1693 expr->type = EXPR_VALUE;
1694 expr->value = size >> 3;
1695 expr->ctype = size_t_ctype;
1696 return size_t_ctype;
1699 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1701 struct symbol *type;
1702 int size;
1704 type = evaluate_type_information(expr);
1705 if (!type)
1706 return NULL;
1708 if (type->type == SYM_NODE)
1709 type = type->ctype.base_type;
1710 if (!type)
1711 return NULL;
1712 switch (type->type) {
1713 case SYM_ARRAY:
1714 break;
1715 case SYM_PTR:
1716 type = type->ctype.base_type;
1717 if (type)
1718 break;
1719 default:
1720 warning(expr->pos, "expected pointer expression");
1721 return NULL;
1723 size = type->bit_size;
1724 if (size & 7)
1725 size = 0;
1726 expr->type = EXPR_VALUE;
1727 expr->value = size >> 3;
1728 expr->ctype = size_t_ctype;
1729 return size_t_ctype;
1732 static struct symbol *evaluate_alignof(struct expression *expr)
1734 struct symbol *type;
1736 type = evaluate_type_information(expr);
1737 if (!type)
1738 return NULL;
1740 expr->type = EXPR_VALUE;
1741 expr->value = type->ctype.alignment;
1742 expr->ctype = size_t_ctype;
1743 return size_t_ctype;
1746 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1748 struct expression *expr;
1749 struct symbol_list *argument_types = fn->arguments;
1750 struct symbol *argtype;
1751 int i = 1;
1753 PREPARE_PTR_LIST(argument_types, argtype);
1754 FOR_EACH_PTR (head, expr) {
1755 struct expression **p = THIS_ADDRESS(expr);
1756 struct symbol *ctype, *target;
1757 ctype = evaluate_expression(expr);
1759 if (!ctype)
1760 return 0;
1762 ctype = degenerate(expr);
1764 target = argtype;
1765 if (!target && ctype->bit_size < bits_in_int)
1766 target = &int_ctype;
1767 if (target) {
1768 static char where[30];
1769 examine_symbol_type(target);
1770 sprintf(where, "argument %d", i);
1771 compatible_assignment_types(expr, target, p, ctype, where, '=');
1774 i++;
1775 NEXT_PTR_LIST(argtype);
1776 } END_FOR_EACH_PTR(expr);
1777 FINISH_PTR_LIST(argtype);
1778 return 1;
1781 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1783 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1785 struct expression *entry = *ep;
1786 struct expression **parent, *reuse = NULL;
1787 unsigned long offset;
1788 struct symbol *sym;
1789 unsigned long from, to;
1790 int accept_string = is_byte_type(ctype);
1792 from = current;
1793 to = from+1;
1794 parent = ep;
1795 if (entry->type == EXPR_INDEX) {
1796 from = entry->idx_from;
1797 to = entry->idx_to+1;
1798 parent = &entry->idx_expression;
1799 reuse = entry;
1800 entry = entry->idx_expression;
1803 offset = from * (ctype->bit_size>>3);
1804 if (offset) {
1805 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1806 reuse->type = EXPR_POS;
1807 reuse->ctype = ctype;
1808 reuse->init_offset = offset;
1809 reuse->init_nr = to - from;
1810 reuse->init_expr = entry;
1811 parent = &reuse->init_expr;
1812 entry = reuse;
1814 *ep = entry;
1816 if (accept_string && entry->type == EXPR_STRING) {
1817 sym = evaluate_expression(entry);
1818 to = from + get_expression_value(sym->array_size);
1819 } else {
1820 evaluate_initializer(ctype, parent);
1822 return to;
1825 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1827 struct expression *entry;
1828 int current = 0;
1830 FOR_EACH_PTR(expr->expr_list, entry) {
1831 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1832 } END_FOR_EACH_PTR(entry);
1835 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1836 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1838 if (expression_list_size(expr->expr_list) != 1) {
1839 warning(expr->pos, "unexpected compound initializer");
1840 return;
1842 evaluate_array_initializer(ctype, expr);
1843 return;
1846 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1848 struct symbol *sym;
1850 FOR_EACH_PTR(ctype->symbol_list, sym) {
1851 if (sym->ident == ident)
1852 return sym;
1853 } END_FOR_EACH_PTR(sym);
1854 return NULL;
1857 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1859 struct expression *entry = *ep;
1860 struct expression **parent;
1861 struct expression *reuse = NULL;
1862 unsigned long offset;
1864 if (!sym) {
1865 error(entry->pos, "unknown named initializer");
1866 return -1;
1869 if (entry->type == EXPR_IDENTIFIER) {
1870 reuse = entry;
1871 entry = entry->ident_expression;
1874 parent = ep;
1875 offset = sym->offset;
1876 if (offset) {
1877 if (!reuse)
1878 reuse = alloc_expression(entry->pos, EXPR_POS);
1879 reuse->type = EXPR_POS;
1880 reuse->ctype = sym;
1881 reuse->init_offset = offset;
1882 reuse->init_nr = 1;
1883 reuse->init_expr = entry;
1884 parent = &reuse->init_expr;
1885 entry = reuse;
1887 *ep = entry;
1888 evaluate_initializer(sym, parent);
1889 return 0;
1892 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1894 struct expression *entry;
1895 struct symbol *sym;
1897 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1898 FOR_EACH_PTR(expr->expr_list, entry) {
1899 if (entry->type == EXPR_IDENTIFIER) {
1900 struct ident *ident = entry->expr_ident;
1901 /* We special-case the "already right place" case */
1902 if (!sym || sym->ident != ident) {
1903 RESET_PTR_LIST(sym);
1904 for (;;) {
1905 if (!sym)
1906 break;
1907 if (sym->ident == ident)
1908 break;
1909 NEXT_PTR_LIST(sym);
1913 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1914 return;
1915 NEXT_PTR_LIST(sym);
1916 } END_FOR_EACH_PTR(entry);
1917 FINISH_PTR_LIST(sym);
1921 * Initializers are kind of like assignments. Except
1922 * they can be a hell of a lot more complex.
1924 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1926 struct expression *expr = *ep;
1929 * Simple non-structure/array initializers are the simple
1930 * case, and look (and parse) largely like assignments.
1932 switch (expr->type) {
1933 default: {
1934 int is_string = expr->type == EXPR_STRING;
1935 struct symbol *rtype = evaluate_expression(expr);
1936 if (rtype) {
1938 * Special case:
1939 * char array[] = "string"
1940 * should _not_ degenerate.
1942 if (!is_string || !is_string_type(ctype))
1943 rtype = degenerate(expr);
1944 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1946 return;
1949 case EXPR_INITIALIZER:
1950 expr->ctype = ctype;
1951 if (ctype->type == SYM_NODE)
1952 ctype = ctype->ctype.base_type;
1954 switch (ctype->type) {
1955 case SYM_ARRAY:
1956 case SYM_PTR:
1957 evaluate_array_initializer(ctype->ctype.base_type, expr);
1958 return;
1959 case SYM_UNION:
1960 evaluate_struct_or_union_initializer(ctype, expr, 0);
1961 return;
1962 case SYM_STRUCT:
1963 evaluate_struct_or_union_initializer(ctype, expr, 1);
1964 return;
1965 default:
1966 evaluate_scalar_initializer(ctype, expr);
1967 return;
1970 case EXPR_IDENTIFIER:
1971 if (ctype->type == SYM_NODE)
1972 ctype = ctype->ctype.base_type;
1973 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1974 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1975 show_symbol(ctype);
1976 return;
1978 evaluate_one_struct_initializer(ctype, ep,
1979 find_struct_ident(ctype, expr->expr_ident));
1980 return;
1982 case EXPR_INDEX:
1983 if (ctype->type == SYM_NODE)
1984 ctype = ctype->ctype.base_type;
1985 if (ctype->type != SYM_ARRAY) {
1986 error(expr->pos, "expected array");
1987 return;
1989 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1990 return;
1992 case EXPR_POS:
1994 * An EXPR_POS expression has already been evaluated, and we don't
1995 * need to do anything more
1997 return;
2001 static int get_as(struct symbol *sym)
2003 int as;
2004 unsigned long mod;
2006 if (!sym)
2007 return 0;
2008 as = sym->ctype.as;
2009 mod = sym->ctype.modifiers;
2010 if (sym->type == SYM_NODE) {
2011 sym = sym->ctype.base_type;
2012 as |= sym->ctype.as;
2013 mod |= sym->ctype.modifiers;
2017 * At least for now, allow casting to a "unsigned long".
2018 * That's how we do things like pointer arithmetic and
2019 * store pointers to registers.
2021 if (sym == &ulong_ctype)
2022 return -1;
2024 if (sym && sym->type == SYM_PTR) {
2025 sym = sym->ctype.base_type;
2026 as |= sym->ctype.as;
2027 mod |= sym->ctype.modifiers;
2029 if (mod & MOD_FORCE)
2030 return -1;
2031 return as;
2034 static struct symbol *evaluate_cast(struct expression *expr)
2036 struct expression *target = expr->cast_expression;
2037 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2038 enum type type;
2040 if (!target)
2041 return NULL;
2043 expr->ctype = ctype;
2044 expr->cast_type = ctype;
2047 * Special case: a cast can be followed by an
2048 * initializer, in which case we need to pass
2049 * the type value down to that initializer rather
2050 * than trying to evaluate it as an expression
2052 * A more complex case is when the initializer is
2053 * dereferenced as part of a post-fix expression.
2054 * We need to produce an expression that can be dereferenced.
2056 if (target->type == EXPR_INITIALIZER) {
2057 struct symbol *sym = expr->cast_type;
2058 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2060 sym->initializer = expr->cast_expression;
2061 evaluate_symbol(sym);
2063 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2064 addr->symbol = sym;
2066 expr->type = EXPR_PREOP;
2067 expr->op = '*';
2068 expr->unop = addr;
2069 expr->ctype = sym;
2071 return sym;
2074 evaluate_expression(target);
2075 degenerate(target);
2078 * You can always throw a value away by casting to
2079 * "void" - that's an implicit "force". Note that
2080 * the same is _not_ true of "void *".
2082 if (ctype == &void_ctype)
2083 goto out;
2085 type = ctype->type;
2086 if (type == SYM_NODE) {
2087 type = ctype->ctype.base_type->type;
2088 if (ctype->ctype.base_type == &void_ctype)
2089 goto out;
2091 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2092 warning(expr->pos, "cast to non-scalar");
2094 if (!target->ctype) {
2095 warning(expr->pos, "cast from unknown type");
2096 goto out;
2099 type = target->ctype->type;
2100 if (type == SYM_NODE)
2101 type = target->ctype->ctype.base_type->type;
2102 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2103 warning(expr->pos, "cast from non-scalar");
2105 if (!get_as(ctype) && get_as(target->ctype) > 0)
2106 warning(expr->pos, "cast removes address space of expression");
2108 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2109 struct symbol *t1 = ctype, *t2 = target->ctype;
2110 if (t1->type == SYM_NODE)
2111 t1 = t1->ctype.base_type;
2112 if (t2->type == SYM_NODE)
2113 t2 = t2->ctype.base_type;
2114 if (t1 != t2) {
2115 if (t1->type == SYM_RESTRICT)
2116 warning(expr->pos, "cast to restricted type");
2117 if (t2->type == SYM_RESTRICT)
2118 warning(expr->pos, "cast from restricted type");
2123 * Casts of constant values are special: they
2124 * can be NULL, and thus need to be simplified
2125 * early.
2127 if (target->type == EXPR_VALUE)
2128 cast_value(expr, ctype, target, target->ctype);
2130 out:
2131 return ctype;
2135 * Evaluate a call expression with a symbol. This
2136 * should expand inline functions, and evaluate
2137 * builtins.
2139 static int evaluate_symbol_call(struct expression *expr)
2141 struct expression *fn = expr->fn;
2142 struct symbol *ctype = fn->ctype;
2144 if (fn->type != EXPR_PREOP)
2145 return 0;
2147 if (ctype->op && ctype->op->evaluate)
2148 return ctype->op->evaluate(expr);
2150 if (ctype->ctype.modifiers & MOD_INLINE) {
2151 int ret;
2152 struct symbol *curr = current_fn;
2153 current_fn = ctype->ctype.base_type;
2154 examine_fn_arguments(current_fn);
2156 ret = inline_function(expr, ctype);
2158 /* restore the old function */
2159 current_fn = curr;
2160 return ret;
2163 return 0;
2166 static struct symbol *evaluate_call(struct expression *expr)
2168 int args, fnargs;
2169 struct symbol *ctype, *sym;
2170 struct expression *fn = expr->fn;
2171 struct expression_list *arglist = expr->args;
2173 if (!evaluate_expression(fn))
2174 return NULL;
2175 sym = ctype = fn->ctype;
2176 if (ctype->type == SYM_NODE)
2177 ctype = ctype->ctype.base_type;
2178 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2179 ctype = ctype->ctype.base_type;
2180 if (!evaluate_arguments(sym, ctype, arglist))
2181 return NULL;
2182 if (ctype->type != SYM_FN) {
2183 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2184 return NULL;
2186 args = expression_list_size(expr->args);
2187 fnargs = symbol_list_size(ctype->arguments);
2188 if (args < fnargs)
2189 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2190 if (args > fnargs && !ctype->variadic)
2191 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2192 if (sym->type == SYM_NODE) {
2193 if (evaluate_symbol_call(expr))
2194 return expr->ctype;
2196 expr->ctype = ctype->ctype.base_type;
2197 return expr->ctype;
2200 struct symbol *evaluate_expression(struct expression *expr)
2202 if (!expr)
2203 return NULL;
2204 if (expr->ctype)
2205 return expr->ctype;
2207 switch (expr->type) {
2208 case EXPR_VALUE:
2209 case EXPR_FVALUE:
2210 warning(expr->pos, "value expression without a type");
2211 return NULL;
2212 case EXPR_STRING:
2213 return evaluate_string(expr);
2214 case EXPR_SYMBOL:
2215 return evaluate_symbol_expression(expr);
2216 case EXPR_BINOP:
2217 if (!evaluate_expression(expr->left))
2218 return NULL;
2219 if (!evaluate_expression(expr->right))
2220 return NULL;
2221 return evaluate_binop(expr);
2222 case EXPR_LOGICAL:
2223 return evaluate_logical(expr);
2224 case EXPR_COMMA:
2225 evaluate_expression(expr->left);
2226 if (!evaluate_expression(expr->right))
2227 return NULL;
2228 return evaluate_comma(expr);
2229 case EXPR_COMPARE:
2230 if (!evaluate_expression(expr->left))
2231 return NULL;
2232 if (!evaluate_expression(expr->right))
2233 return NULL;
2234 return evaluate_compare(expr);
2235 case EXPR_ASSIGNMENT:
2236 if (!evaluate_expression(expr->left))
2237 return NULL;
2238 if (!evaluate_expression(expr->right))
2239 return NULL;
2240 return evaluate_assignment(expr);
2241 case EXPR_PREOP:
2242 if (!evaluate_expression(expr->unop))
2243 return NULL;
2244 return evaluate_preop(expr);
2245 case EXPR_POSTOP:
2246 if (!evaluate_expression(expr->unop))
2247 return NULL;
2248 return evaluate_postop(expr);
2249 case EXPR_CAST:
2250 case EXPR_IMPLIED_CAST:
2251 return evaluate_cast(expr);
2252 case EXPR_SIZEOF:
2253 return evaluate_sizeof(expr);
2254 case EXPR_PTRSIZEOF:
2255 return evaluate_ptrsizeof(expr);
2256 case EXPR_ALIGNOF:
2257 return evaluate_alignof(expr);
2258 case EXPR_DEREF:
2259 return evaluate_member_dereference(expr);
2260 case EXPR_CALL:
2261 return evaluate_call(expr);
2262 case EXPR_SELECT:
2263 case EXPR_CONDITIONAL:
2264 return evaluate_conditional_expression(expr);
2265 case EXPR_STATEMENT:
2266 expr->ctype = evaluate_statement(expr->statement);
2267 return expr->ctype;
2269 case EXPR_LABEL:
2270 expr->ctype = &ptr_ctype;
2271 return &ptr_ctype;
2273 case EXPR_TYPE:
2274 /* Evaluate the type of the symbol .. */
2275 evaluate_symbol(expr->symbol);
2276 /* .. but the type of the _expression_ is a "type" */
2277 expr->ctype = &type_ctype;
2278 return &type_ctype;
2280 /* These can not exist as stand-alone expressions */
2281 case EXPR_INITIALIZER:
2282 case EXPR_IDENTIFIER:
2283 case EXPR_INDEX:
2284 case EXPR_POS:
2285 warning(expr->pos, "internal front-end error: initializer in expression");
2286 return NULL;
2287 case EXPR_SLICE:
2288 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2289 return NULL;
2291 return NULL;
2294 static void check_duplicates(struct symbol *sym)
2296 int declared = 0;
2297 struct symbol *next = sym;
2299 while ((next = next->same_symbol) != NULL) {
2300 const char *typediff;
2301 evaluate_symbol(next);
2302 declared++;
2303 typediff = type_difference(sym, next, 0, 0);
2304 if (typediff) {
2305 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2306 show_ident(sym->ident),
2307 stream_name(next->pos.stream), next->pos.line, typediff);
2308 return;
2311 if (!declared) {
2312 unsigned long mod = sym->ctype.modifiers;
2313 if (mod & (MOD_STATIC | MOD_REGISTER))
2314 return;
2315 if (!(mod & MOD_TOPLEVEL))
2316 return;
2317 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2321 static struct symbol *evaluate_symbol(struct symbol *sym)
2323 struct symbol *base_type;
2325 if (!sym)
2326 return sym;
2328 sym = examine_symbol_type(sym);
2329 base_type = sym->ctype.base_type;
2330 if (!base_type)
2331 return NULL;
2333 /* Evaluate the initializers */
2334 if (sym->initializer)
2335 evaluate_initializer(sym, &sym->initializer);
2337 /* And finally, evaluate the body of the symbol too */
2338 if (base_type->type == SYM_FN) {
2339 struct symbol *curr = current_fn;
2341 current_fn = base_type;
2343 examine_fn_arguments(base_type);
2344 if (!base_type->stmt && base_type->inline_stmt)
2345 uninline(sym);
2346 if (base_type->stmt)
2347 evaluate_statement(base_type->stmt);
2349 current_fn = curr;
2352 return base_type;
2355 void evaluate_symbol_list(struct symbol_list *list)
2357 struct symbol *sym;
2359 FOR_EACH_PTR(list, sym) {
2360 check_duplicates(sym);
2361 evaluate_symbol(sym);
2362 } END_FOR_EACH_PTR(sym);
2365 static struct symbol *evaluate_return_expression(struct statement *stmt)
2367 struct expression *expr = stmt->expression;
2368 struct symbol *ctype, *fntype;
2370 evaluate_expression(expr);
2371 ctype = degenerate(expr);
2372 fntype = current_fn->ctype.base_type;
2373 if (!fntype || fntype == &void_ctype) {
2374 if (expr && ctype != &void_ctype)
2375 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2376 return NULL;
2379 if (!expr) {
2380 warning(stmt->pos, "return with no return value");
2381 return NULL;
2383 if (!ctype)
2384 return NULL;
2385 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2386 return NULL;
2389 static void evaluate_if_statement(struct statement *stmt)
2391 if (!stmt->if_conditional)
2392 return;
2394 evaluate_conditional(stmt->if_conditional, 0);
2395 evaluate_statement(stmt->if_true);
2396 evaluate_statement(stmt->if_false);
2399 static void evaluate_iterator(struct statement *stmt)
2401 evaluate_conditional(stmt->iterator_pre_condition, 1);
2402 evaluate_conditional(stmt->iterator_post_condition,1);
2403 evaluate_statement(stmt->iterator_pre_statement);
2404 evaluate_statement(stmt->iterator_statement);
2405 evaluate_statement(stmt->iterator_post_statement);
2408 static void verify_output_constraint(struct expression *expr, const char *constraint)
2410 switch (*constraint) {
2411 case '=': /* Assignment */
2412 case '+': /* Update */
2413 break;
2414 default:
2415 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2419 static void verify_input_constraint(struct expression *expr, const char *constraint)
2421 switch (*constraint) {
2422 case '=': /* Assignment */
2423 case '+': /* Update */
2424 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2428 static void evaluate_asm_statement(struct statement *stmt)
2430 struct expression *expr;
2431 int state;
2433 expr = stmt->asm_string;
2434 if (!expr || expr->type != EXPR_STRING) {
2435 warning(stmt->pos, "need constant string for inline asm");
2436 return;
2439 state = 0;
2440 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2441 struct ident *ident;
2443 switch (state) {
2444 case 0: /* Identifier */
2445 state = 1;
2446 ident = (struct ident *)expr;
2447 continue;
2449 case 1: /* Constraint */
2450 state = 2;
2451 if (!expr || expr->type != EXPR_STRING) {
2452 warning(expr->pos, "asm output constraint is not a string");
2453 *THIS_ADDRESS(expr) = NULL;
2454 continue;
2456 verify_output_constraint(expr, expr->string->data);
2457 continue;
2459 case 2: /* Expression */
2460 state = 0;
2461 if (!evaluate_expression(expr))
2462 return;
2463 if (!lvalue_expression(expr))
2464 warning(expr->pos, "asm output is not an lvalue");
2465 evaluate_assign_to(expr, expr->ctype);
2466 continue;
2468 } END_FOR_EACH_PTR(expr);
2470 state = 0;
2471 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2472 struct ident *ident;
2474 switch (state) {
2475 case 0: /* Identifier */
2476 state = 1;
2477 ident = (struct ident *)expr;
2478 continue;
2480 case 1: /* Constraint */
2481 state = 2;
2482 if (!expr || expr->type != EXPR_STRING) {
2483 warning(expr->pos, "asm input constraint is not a string");
2484 *THIS_ADDRESS(expr) = NULL;
2485 continue;
2487 verify_input_constraint(expr, expr->string->data);
2488 continue;
2490 case 2: /* Expression */
2491 state = 0;
2492 if (!evaluate_expression(expr))
2493 return;
2494 continue;
2496 } END_FOR_EACH_PTR(expr);
2498 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2499 if (!expr) {
2500 warning(stmt->pos, "bad asm output");
2501 return;
2503 if (expr->type == EXPR_STRING)
2504 continue;
2505 warning(expr->pos, "asm clobber is not a string");
2506 } END_FOR_EACH_PTR(expr);
2509 struct symbol *evaluate_statement(struct statement *stmt)
2511 if (!stmt)
2512 return NULL;
2514 switch (stmt->type) {
2515 case STMT_RETURN:
2516 return evaluate_return_expression(stmt);
2518 case STMT_EXPRESSION:
2519 if (!evaluate_expression(stmt->expression))
2520 return NULL;
2521 return degenerate(stmt->expression);
2523 case STMT_COMPOUND: {
2524 struct statement *s;
2525 struct symbol *type = NULL;
2526 struct symbol *sym;
2528 /* Evaluate each symbol in the compound statement */
2529 FOR_EACH_PTR(stmt->syms, sym) {
2530 evaluate_symbol(sym);
2531 } END_FOR_EACH_PTR(sym);
2532 evaluate_symbol(stmt->ret);
2535 * Then, evaluate each statement, making the type of the
2536 * compound statement be the type of the last statement
2538 type = NULL;
2539 FOR_EACH_PTR(stmt->stmts, s) {
2540 type = evaluate_statement(s);
2541 } END_FOR_EACH_PTR(s);
2542 if (!type)
2543 type = &void_ctype;
2544 return type;
2546 case STMT_IF:
2547 evaluate_if_statement(stmt);
2548 return NULL;
2549 case STMT_ITERATOR:
2550 evaluate_iterator(stmt);
2551 return NULL;
2552 case STMT_SWITCH:
2553 evaluate_expression(stmt->switch_expression);
2554 evaluate_statement(stmt->switch_statement);
2555 return NULL;
2556 case STMT_CASE:
2557 evaluate_expression(stmt->case_expression);
2558 evaluate_expression(stmt->case_to);
2559 evaluate_statement(stmt->case_statement);
2560 return NULL;
2561 case STMT_LABEL:
2562 return evaluate_statement(stmt->label_statement);
2563 case STMT_GOTO:
2564 evaluate_expression(stmt->goto_expression);
2565 return NULL;
2566 case STMT_NONE:
2567 break;
2568 case STMT_ASM:
2569 evaluate_asm_statement(stmt);
2570 return NULL;
2571 case STMT_CONTEXT:
2572 evaluate_expression(stmt->expression);
2573 return NULL;
2574 case STMT_RANGE:
2575 evaluate_expression(stmt->range_expression);
2576 evaluate_expression(stmt->range_low);
2577 evaluate_expression(stmt->range_high);
2578 return NULL;
2580 return NULL;