Duh. When a function returns VOID, we should _not_ add that
[smatch.git] / evaluate.c
bloba5f8db80ad7eee3affa112142234e2b82757d132
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 symbol *sym = expr->symbol;
37 struct symbol *base_type;
39 if (!sym) {
40 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
41 return NULL;
44 examine_symbol_type(sym);
46 base_type = sym->ctype.base_type;
47 if (!base_type) {
48 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
49 return NULL;
52 /* The type of a symbol is the symbol itself! */
53 expr->ctype = sym;
55 /* enums can be turned into plain values */
56 if (sym->type != SYM_ENUM) {
57 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
58 addr->symbol = sym;
59 addr->symbol_name = expr->symbol_name;
60 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
61 expr->type = EXPR_PREOP;
62 expr->op = '*';
63 expr->unop = addr;
64 return sym;
65 } else if (base_type->bit_size < bits_in_int) {
66 /* ugly - we need to force sizeof for these guys */
67 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
68 e->value = sym->value;
69 e->ctype = base_type;
70 expr->type = EXPR_PREOP;
71 expr->op = '+';
72 expr->unop = e;
73 } else {
74 expr->type = EXPR_VALUE;
75 expr->value = sym->value;
77 expr->ctype = base_type;
78 return base_type;
81 static struct symbol *evaluate_string(struct expression *expr)
83 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
84 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
85 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
86 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
87 unsigned int length = expr->string->length;
89 sym->array_size = alloc_const_expression(expr->pos, length);
90 sym->bit_size = bits_in_char * length;
91 sym->ctype.alignment = 1;
92 sym->ctype.modifiers = MOD_STATIC;
93 sym->ctype.base_type = array;
94 sym->initializer = initstr;
96 initstr->ctype = sym;
97 initstr->string = expr->string;
99 array->array_size = sym->array_size;
100 array->bit_size = bits_in_char * length;
101 array->ctype.alignment = 1;
102 array->ctype.modifiers = MOD_STATIC;
103 array->ctype.base_type = &char_ctype;
105 addr->symbol = sym;
106 addr->ctype = &lazy_ptr_ctype;
108 expr->type = EXPR_PREOP;
109 expr->op = '*';
110 expr->unop = addr;
111 expr->ctype = sym;
112 return sym;
115 static inline struct symbol *integer_promotion(struct symbol *type)
117 unsigned long mod = type->ctype.modifiers;
118 int width;
120 if (type->type == SYM_NODE)
121 type = type->ctype.base_type;
122 if (type->type == SYM_ENUM)
123 type = type->ctype.base_type;
124 width = type->bit_size;
125 if (type->type == SYM_BITFIELD)
126 type = type->ctype.base_type;
127 mod = type->ctype.modifiers;
128 if (width < bits_in_int)
129 return &int_ctype;
131 /* If char/short has as many bits as int, it still gets "promoted" */
132 if (mod & (MOD_CHAR | MOD_SHORT)) {
133 type = &int_ctype;
134 if (mod & MOD_UNSIGNED)
135 type = &uint_ctype;
137 return type;
141 * integer part of usual arithmetic conversions:
142 * integer promotions are applied
143 * if left and right are identical, we are done
144 * if signedness is the same, convert one with lower rank
145 * unless unsigned argument has rank lower than signed one, convert the
146 * signed one.
147 * if signed argument is bigger than unsigned one, convert the unsigned.
148 * otherwise, convert signed.
150 * Leaving aside the integer promotions, that is equivalent to
151 * if identical, don't convert
152 * if left is bigger than right, convert right
153 * if right is bigger than left, convert right
154 * otherwise, if signedness is the same, convert one with lower rank
155 * otherwise convert the signed one.
157 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
159 unsigned long lmod, rmod;
161 left = integer_promotion(left);
162 right = integer_promotion(right);
164 if (left == right)
165 goto left;
167 if (left->bit_size > right->bit_size)
168 goto left;
170 if (right->bit_size > left->bit_size)
171 goto right;
173 lmod = left->ctype.modifiers;
174 rmod = right->ctype.modifiers;
175 if ((lmod ^ rmod) & MOD_UNSIGNED) {
176 if (lmod & MOD_UNSIGNED)
177 goto left;
178 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
179 goto left;
180 right:
181 left = right;
182 left:
183 return left;
186 static int same_cast_type(struct symbol *orig, struct symbol *new)
188 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
192 * This gets called for implicit casts in assignments and
193 * integer promotion. We often want to try to move the
194 * cast down, because the ops involved may have been
195 * implicitly cast up, and we can get rid of the casts
196 * early.
198 static struct expression * cast_to(struct expression *old, struct symbol *type)
200 struct expression *expr;
203 * See if we can simplify the op. Move the cast down.
205 switch (old->type) {
206 case EXPR_PREOP:
207 if (old->op == '~') {
208 old->ctype = type;
209 old->unop = cast_to(old->unop, type);
210 return old;
212 break;
214 case EXPR_IMPLIED_CAST:
215 if (old->ctype->bit_size >= type->bit_size) {
216 struct expression *orig = old->cast_expression;
217 if (same_cast_type(orig->ctype, type))
218 return orig;
219 if (old->ctype->bit_offset == type->bit_offset) {
220 old->ctype = type;
221 old->cast_type = type;
222 return old;
225 break;
227 default:
228 /* nothing */;
231 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
232 expr->ctype = type;
233 expr->cast_type = type;
234 expr->cast_expression = old;
235 return expr;
238 static int is_type_type(struct symbol *type)
240 return (type->ctype.modifiers & MOD_TYPE) != 0;
243 static int is_ptr_type(struct symbol *type)
245 if (type->type == SYM_NODE)
246 type = type->ctype.base_type;
247 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
250 static inline int is_float_type(struct symbol *type)
252 if (type->type == SYM_NODE)
253 type = type->ctype.base_type;
254 return type->ctype.base_type == &fp_type;
257 static inline int is_byte_type(struct symbol *type)
259 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
262 static inline int is_string_type(struct symbol *type)
264 if (type->type == SYM_NODE)
265 type = type->ctype.base_type;
266 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
269 static struct symbol *bad_expr_type(struct expression *expr)
271 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
272 switch (expr->type) {
273 case EXPR_BINOP:
274 case EXPR_COMPARE:
275 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
276 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
277 break;
278 case EXPR_PREOP:
279 case EXPR_POSTOP:
280 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
281 break;
282 default:
283 break;
286 return NULL;
289 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
291 struct expression *left = *lp, *right = *rp;
292 struct symbol *ltype = left->ctype, *rtype = right->ctype;
294 if (ltype->type == SYM_NODE)
295 ltype = ltype->ctype.base_type;
296 if (rtype->type == SYM_NODE)
297 rtype = rtype->ctype.base_type;
298 if (is_float_type(ltype)) {
299 if (is_int_type(rtype))
300 goto Left;
301 if (is_float_type(rtype)) {
302 unsigned long lmod = ltype->ctype.modifiers;
303 unsigned long rmod = rtype->ctype.modifiers;
304 lmod &= MOD_LONG | MOD_LONGLONG;
305 rmod &= MOD_LONG | MOD_LONGLONG;
306 if (lmod == rmod)
307 return ltype;
308 if (lmod & ~rmod)
309 goto Left;
310 else
311 goto Right;
313 return NULL;
315 if (!is_float_type(rtype) || !is_int_type(ltype))
316 return NULL;
317 Right:
318 *lp = cast_to(left, rtype);
319 return rtype;
320 Left:
321 *rp = cast_to(right, ltype);
322 return ltype;
325 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
327 struct expression *left = *lp, *right = *rp;
328 struct symbol *ltype = left->ctype, *rtype = right->ctype;
330 if (ltype->type == SYM_NODE)
331 ltype = ltype->ctype.base_type;
332 if (rtype->type == SYM_NODE)
333 rtype = rtype->ctype.base_type;
334 if (is_int_type(ltype) && is_int_type(rtype)) {
335 struct symbol *ctype = bigger_int_type(ltype, rtype);
337 /* Don't bother promoting same-size entities, it only adds clutter */
338 if (ltype->bit_size != ctype->bit_size)
339 *lp = cast_to(left, ctype);
340 if (rtype->bit_size != ctype->bit_size)
341 *rp = cast_to(right, ctype);
342 return ctype;
344 return NULL;
347 static int restricted_value(struct expression *v, struct symbol *type)
349 if (v->type != EXPR_VALUE)
350 return 1;
351 if (v->value != 0)
352 return 1;
353 return 0;
356 static int restricted_binop(int op, struct symbol *type)
358 switch (op) {
359 case '&':
360 case '|':
361 case '^':
362 case '?':
363 case '=':
364 case SPECIAL_EQUAL:
365 case SPECIAL_NOTEQUAL:
366 case SPECIAL_AND_ASSIGN:
367 case SPECIAL_OR_ASSIGN:
368 case SPECIAL_XOR_ASSIGN:
369 return 0;
370 default:
371 return 1;
375 static int restricted_unop(int op, struct symbol *type)
377 if (op == '~' && type->bit_size >= bits_in_int)
378 return 0;
379 if (op == '+')
380 return 0;
381 return 1;
384 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
386 struct expression *left = *lp, *right = *rp;
387 struct symbol *ltype = left->ctype, *rtype = right->ctype;
388 struct symbol *type = NULL;
390 if (ltype->type == SYM_NODE)
391 ltype = ltype->ctype.base_type;
392 if (ltype->type == SYM_ENUM)
393 ltype = ltype->ctype.base_type;
394 if (rtype->type == SYM_NODE)
395 rtype = rtype->ctype.base_type;
396 if (rtype->type == SYM_ENUM)
397 rtype = rtype->ctype.base_type;
398 if (is_restricted_type(ltype)) {
399 if (is_restricted_type(rtype)) {
400 if (ltype == rtype)
401 type = ltype;
402 } else {
403 if (!restricted_value(right, ltype))
404 type = ltype;
406 } else if (is_restricted_type(rtype)) {
407 if (!restricted_value(left, rtype))
408 type = rtype;
410 if (!type)
411 return NULL;
412 if (restricted_binop(op, type))
413 return NULL;
414 return type;
417 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
419 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
420 if (!ctype && float_ok)
421 ctype = compatible_float_binop(&expr->left, &expr->right);
422 if (!ctype)
423 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
424 if (ctype) {
425 expr->ctype = ctype;
426 return ctype;
428 return bad_expr_type(expr);
431 static inline int lvalue_expression(struct expression *expr)
433 return expr->type == EXPR_PREOP && expr->op == '*';
436 static int ptr_object_size(struct symbol *ptr_type)
438 if (ptr_type->type == SYM_NODE)
439 ptr_type = ptr_type->ctype.base_type;
440 if (ptr_type->type == SYM_PTR)
441 ptr_type = ptr_type->ctype.base_type;
442 return ptr_type->bit_size;
445 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
447 struct expression *i = *ip;
448 struct symbol *ptr_type = ctype;
449 int bit_size;
451 if (ptr_type->type == SYM_NODE)
452 ptr_type = ptr_type->ctype.base_type;
454 if (!is_int_type(i->ctype))
455 return bad_expr_type(expr);
457 examine_symbol_type(ctype);
459 if (!ctype->ctype.base_type) {
460 warning(expr->pos, "missing type information");
461 return NULL;
464 /* Get the size of whatever the pointer points to */
465 bit_size = ptr_object_size(ctype);
467 if (bit_size > bits_in_char) {
468 int multiply = bit_size >> 3;
469 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
471 if (i->type == EXPR_VALUE) {
472 val->value = i->value * multiply;
473 val->ctype = size_t_ctype;
474 *ip = val;
475 } else {
476 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
478 val->ctype = size_t_ctype;
479 val->value = bit_size >> 3;
481 mul->op = '*';
482 mul->ctype = size_t_ctype;
483 mul->left = i;
484 mul->right = val;
486 *ip = mul;
490 expr->ctype = ctype;
491 return ctype;
494 static struct symbol *evaluate_add(struct expression *expr)
496 struct expression *left = expr->left, *right = expr->right;
497 struct symbol *ltype = left->ctype, *rtype = right->ctype;
499 if (is_ptr_type(ltype))
500 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
502 if (is_ptr_type(rtype))
503 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
505 return evaluate_arith(expr, 1);
508 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
509 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
510 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
512 const char * type_difference(struct symbol *target, struct symbol *source,
513 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
515 for (;;) {
516 unsigned long mod1, mod2, diff;
517 unsigned long as1, as2;
518 int type1, type2;
519 struct symbol *base1, *base2;
521 if (target == source)
522 break;
523 if (!target || !source)
524 return "different types";
526 * Peel of per-node information.
527 * FIXME! Check alignment and context too here!
529 mod1 = target->ctype.modifiers;
530 as1 = target->ctype.as;
531 mod2 = source->ctype.modifiers;
532 as2 = source->ctype.as;
533 if (target->type == SYM_NODE) {
534 target = target->ctype.base_type;
535 if (!target)
536 return "bad types";
537 if (target->type == SYM_PTR) {
538 mod1 = 0;
539 as1 = 0;
541 mod1 |= target->ctype.modifiers;
542 as1 |= target->ctype.as;
544 if (source->type == SYM_NODE) {
545 source = source->ctype.base_type;
546 if (!source)
547 return "bad types";
548 if (source->type == SYM_PTR) {
549 mod2 = 0;
550 as2 = 0;
552 mod2 |= source->ctype.modifiers;
553 as2 |= source->ctype.as;
555 if (target->type == SYM_ENUM) {
556 target = target->ctype.base_type;
557 if (!target)
558 return "bad types";
560 if (source->type == SYM_ENUM) {
561 source = source->ctype.base_type;
562 if (!source)
563 return "bad types";
566 if (target == source)
567 break;
568 if (!target || !source)
569 return "different types";
571 type1 = target->type;
572 base1 = target->ctype.base_type;
574 type2 = source->type;
575 base2 = source->ctype.base_type;
578 * Pointers to functions compare as the function itself
580 if (type1 == SYM_PTR && base1) {
581 switch (base1->type) {
582 case SYM_FN:
583 type1 = SYM_FN;
584 target = base1;
585 base1 = base1->ctype.base_type;
586 default:
587 /* nothing */;
590 if (type2 == SYM_PTR && base2) {
591 switch (base2->type) {
592 case SYM_FN:
593 type2 = SYM_FN;
594 source = base2;
595 base2 = base2->ctype.base_type;
596 default:
597 /* nothing */;
601 /* Arrays degenerate to pointers for type comparisons */
602 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
603 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
605 if (type1 != type2 || type1 == SYM_RESTRICT)
606 return "different base types";
608 /* Must be same address space to be comparable */
609 if (as1 != as2)
610 return "different address spaces";
612 /* Ignore differences in storage types or addressability */
613 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
614 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
615 if (diff) {
616 if (diff & MOD_SIZE)
617 return "different type sizes";
618 if (diff & ~MOD_SIGNEDNESS)
619 return "different modifiers";
621 /* Differs in signedness only.. */
622 if (Wtypesign) {
624 * Warn if both are explicitly signed ("unsigned" is obvously
625 * always explicit, and since we know one of them has to be
626 * unsigned, we check if the signed one was explicit).
628 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
629 return "different explicit signedness";
632 * "char" matches both "unsigned char" and "signed char",
633 * so if the explicit test didn't trigger, then we should
634 * not warn about a char.
636 if (!(mod1 & MOD_CHAR))
637 return "different signedness";
641 if (type1 == SYM_FN) {
642 int i;
643 struct symbol *arg1, *arg2;
644 if (base1->variadic != base2->variadic)
645 return "incompatible variadic arguments";
646 PREPARE_PTR_LIST(target->arguments, arg1);
647 PREPARE_PTR_LIST(source->arguments, arg2);
648 i = 1;
649 for (;;) {
650 const char *diff;
651 diff = type_difference(arg1, arg2, 0, 0);
652 if (diff) {
653 static char argdiff[80];
654 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
655 return argdiff;
657 if (!arg1)
658 break;
659 NEXT_PTR_LIST(arg1);
660 NEXT_PTR_LIST(arg2);
661 i++;
663 FINISH_PTR_LIST(arg2);
664 FINISH_PTR_LIST(arg1);
667 target = base1;
668 source = base2;
670 return NULL;
673 static int is_null_ptr(struct expression *expr)
675 if (expr->type != EXPR_VALUE || expr->value)
676 return 0;
677 if (!is_ptr_type(expr->ctype))
678 warning(expr->pos, "Using plain integer as NULL pointer");
679 return 1;
682 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
684 /* NULL expression? Just return the type of the "other side" */
685 if (is_null_ptr(r))
686 return l->ctype;
687 if (is_null_ptr(l))
688 return r->ctype;
689 return NULL;
693 * Ignore differences in "volatile" and "const"ness when
694 * subtracting pointers
696 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
698 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
700 const char *typediff;
701 struct symbol *ctype;
702 struct symbol *ltype, *rtype;
703 struct expression *r = *rp;
705 ltype = degenerate(l);
706 rtype = degenerate(r);
709 * If it is an integer subtract: the ptr add case will do the
710 * right thing.
712 if (!is_ptr_type(rtype))
713 return evaluate_ptr_add(expr, degenerate(l), rp);
715 ctype = ltype;
716 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
717 if (typediff) {
718 ctype = common_ptr_type(l, r);
719 if (!ctype) {
720 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
721 return NULL;
724 examine_symbol_type(ctype);
726 /* Figure out the base type we point to */
727 if (ctype->type == SYM_NODE)
728 ctype = ctype->ctype.base_type;
729 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
730 warning(expr->pos, "subtraction of functions? Share your drugs");
731 return NULL;
733 ctype = ctype->ctype.base_type;
735 expr->ctype = ssize_t_ctype;
736 if (ctype->bit_size > bits_in_char) {
737 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
738 struct expression *div = expr;
739 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
740 unsigned long value = ctype->bit_size >> 3;
742 val->ctype = size_t_ctype;
743 val->value = value;
745 if (value & (value-1)) {
746 if (Wptr_subtraction_blows)
747 warning(expr->pos, "potentially expensive pointer subtraction");
750 sub->op = '-';
751 sub->ctype = ssize_t_ctype;
752 sub->left = l;
753 sub->right = r;
755 div->op = '/';
756 div->left = sub;
757 div->right = val;
760 return ssize_t_ctype;
763 static struct symbol *evaluate_sub(struct expression *expr)
765 struct expression *left = expr->left;
766 struct symbol *ltype = left->ctype;
768 if (is_ptr_type(ltype))
769 return evaluate_ptr_sub(expr, left, &expr->right);
771 return evaluate_arith(expr, 1);
774 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
776 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
778 struct symbol *ctype;
780 if (!expr)
781 return NULL;
783 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
784 warning(expr->pos, "assignment expression in conditional");
786 ctype = evaluate_expression(expr);
787 if (ctype) {
788 if (is_safe_type(ctype))
789 warning(expr->pos, "testing a 'safe expression'");
792 return ctype;
795 static struct symbol *evaluate_logical(struct expression *expr)
797 if (!evaluate_conditional(expr->left, 0))
798 return NULL;
799 if (!evaluate_conditional(expr->right, 0))
800 return NULL;
802 expr->ctype = &bool_ctype;
803 return &bool_ctype;
806 static struct symbol *evaluate_shift(struct expression *expr)
808 struct expression *left = expr->left, *right = expr->right;
809 struct symbol *ltype = left->ctype, *rtype = right->ctype;
811 if (ltype->type == SYM_NODE)
812 ltype = ltype->ctype.base_type;
813 if (rtype->type == SYM_NODE)
814 rtype = rtype->ctype.base_type;
815 if (is_int_type(ltype) && is_int_type(rtype)) {
816 struct symbol *ctype = integer_promotion(ltype);
817 if (ltype->bit_size != ctype->bit_size)
818 expr->left = cast_to(expr->left, ctype);
819 expr->ctype = ctype;
820 ctype = integer_promotion(rtype);
821 if (rtype->bit_size != ctype->bit_size)
822 expr->right = cast_to(expr->right, ctype);
823 return expr->ctype;
825 return bad_expr_type(expr);
828 static struct symbol *evaluate_binop(struct expression *expr)
830 switch (expr->op) {
831 // addition can take ptr+int, fp and int
832 case '+':
833 return evaluate_add(expr);
835 // subtraction can take ptr-ptr, fp and int
836 case '-':
837 return evaluate_sub(expr);
839 // Arithmetic operations can take fp and int
840 case '*': case '/':
841 return evaluate_arith(expr, 1);
843 // shifts do integer promotions, but that's it.
844 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
845 return evaluate_shift(expr);
847 // The rest are integer operations
848 // '%', '&', '^', '|'
849 default:
850 return evaluate_arith(expr, 0);
854 static struct symbol *evaluate_comma(struct expression *expr)
856 expr->ctype = expr->right->ctype;
857 return expr->ctype;
860 static int modify_for_unsigned(int op)
862 if (op == '<')
863 op = SPECIAL_UNSIGNED_LT;
864 else if (op == '>')
865 op = SPECIAL_UNSIGNED_GT;
866 else if (op == SPECIAL_LTE)
867 op = SPECIAL_UNSIGNED_LTE;
868 else if (op == SPECIAL_GTE)
869 op = SPECIAL_UNSIGNED_GTE;
870 return op;
873 static struct symbol *evaluate_compare(struct expression *expr)
875 struct expression *left = expr->left, *right = expr->right;
876 struct symbol *ltype = left->ctype, *rtype = right->ctype;
877 struct symbol *ctype;
879 /* Type types? */
880 if (is_type_type(ltype) && is_type_type(rtype))
881 goto OK;
883 if (is_safe_type(ltype) || is_safe_type(rtype))
884 warning(expr->pos, "testing a 'safe expression'");
886 /* Pointer types? */
887 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
888 // FIXME! Check the types for compatibility
889 goto OK;
892 ctype = compatible_integer_binop(&expr->left, &expr->right);
893 if (ctype) {
894 if (ctype->ctype.modifiers & MOD_UNSIGNED)
895 expr->op = modify_for_unsigned(expr->op);
896 goto OK;
899 ctype = compatible_float_binop(&expr->left, &expr->right);
900 if (ctype)
901 goto OK;
903 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
904 if (ctype)
905 goto OK;
907 bad_expr_type(expr);
910 expr->ctype = &bool_ctype;
911 return &bool_ctype;
915 * FIXME!! This should do casts, array degeneration etc..
917 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
919 struct symbol *ltype = left->ctype, *rtype = right->ctype;
921 if (ltype->type == SYM_NODE)
922 ltype = ltype->ctype.base_type;
924 if (rtype->type == SYM_NODE)
925 rtype = rtype->ctype.base_type;
927 if (ltype->type == SYM_PTR) {
928 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
929 return ltype;
932 if (rtype->type == SYM_PTR) {
933 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
934 return rtype;
936 return NULL;
940 * NOTE! The degenerate case of "x ? : y", where we don't
941 * have a true case, this will possibly promote "x" to the
942 * same type as "y", and thus _change_ the conditional
943 * test in the expression. But since promotion is "safe"
944 * for testing, that's ok.
946 static struct symbol *evaluate_conditional_expression(struct expression *expr)
948 struct expression **true;
949 struct symbol *ctype, *ltype, *rtype;
950 const char * typediff;
952 if (!evaluate_conditional(expr->conditional, 0))
953 return NULL;
954 if (!evaluate_expression(expr->cond_false))
955 return NULL;
957 ctype = degenerate(expr->conditional);
958 rtype = degenerate(expr->cond_false);
960 true = &expr->conditional;
961 ltype = ctype;
962 if (expr->cond_true) {
963 if (!evaluate_expression(expr->cond_true))
964 return NULL;
965 ltype = degenerate(expr->cond_true);
966 true = &expr->cond_true;
969 ctype = compatible_integer_binop(true, &expr->cond_false);
970 if (ctype)
971 goto out;
972 ctype = compatible_ptr_type(*true, expr->cond_false);
973 if (ctype)
974 goto out;
975 ctype = compatible_float_binop(true, &expr->cond_false);
976 if (ctype)
977 goto out;
978 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
979 if (ctype)
980 goto out;
981 ctype = ltype;
982 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
983 if (!typediff)
984 goto out;
985 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
986 return NULL;
988 out:
989 expr->ctype = ctype;
990 return ctype;
993 /* FP assignments can not do modulo or bit operations */
994 static int compatible_float_op(int op)
996 return op == '=' ||
997 op == SPECIAL_ADD_ASSIGN ||
998 op == SPECIAL_SUB_ASSIGN ||
999 op == SPECIAL_MUL_ASSIGN ||
1000 op == SPECIAL_DIV_ASSIGN;
1003 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1004 struct expression **rp, struct symbol *source, const char *where, int op)
1006 const char *typediff;
1007 struct symbol *t;
1008 int target_as;
1010 if (is_int_type(target)) {
1011 if (is_int_type(source)) {
1012 if (target->bit_size != source->bit_size)
1013 goto Cast;
1014 if (target->bit_offset != source->bit_offset)
1015 goto Cast;
1016 return 1;
1018 if (is_float_type(source))
1019 goto Cast;
1020 } else if (is_float_type(target)) {
1021 if (!compatible_float_op(op)) {
1022 warning(expr->pos, "invalid assignment");
1023 return 0;
1025 if (is_int_type(source))
1026 goto Cast;
1027 if (is_float_type(source)) {
1028 if (target->bit_size != source->bit_size)
1029 goto Cast;
1030 return 1;
1032 } else if (is_restricted_type(target)) {
1033 if (restricted_binop(op, target)) {
1034 warning(expr->pos, "bad restricted assignment");
1035 return 0;
1037 if (!restricted_value(*rp, target))
1038 return 1;
1039 } else if (is_ptr_type(target)) {
1040 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1041 evaluate_ptr_add(expr, target, rp);
1042 return 1;
1044 if (op != '=') {
1045 warning(expr->pos, "invalid pointer assignment");
1046 return 0;
1048 } else if (op != '=') {
1049 warning(expr->pos, "invalid assignment");
1050 return 0;
1053 /* It's ok if the target is more volatile or const than the source */
1054 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1055 if (!typediff)
1056 return 1;
1058 /* Pointer destination? */
1059 t = target;
1060 target_as = t->ctype.as;
1061 if (t->type == SYM_NODE) {
1062 t = t->ctype.base_type;
1063 target_as |= t->ctype.as;
1065 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1066 struct expression *right = *rp;
1067 struct symbol *s = source;
1068 int source_as;
1070 // NULL pointer is always ok
1071 if (is_null_ptr(right))
1072 return 1;
1074 /* "void *" matches anything as long as the address space is ok */
1075 source_as = s->ctype.as;
1076 if (s->type == SYM_NODE) {
1077 s = s->ctype.base_type;
1078 source_as |= s->ctype.as;
1080 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1081 s = s->ctype.base_type;
1082 t = t->ctype.base_type;
1083 if (s == &void_ctype || t == &void_ctype)
1084 return 1;
1088 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1089 info(expr->pos, " expected %s", show_typename(target));
1090 info(expr->pos, " got %s", show_typename(source));
1091 *rp = cast_to(*rp, target);
1092 return 0;
1093 Cast:
1094 *rp = cast_to(*rp, target);
1095 return 1;
1098 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1100 if (type->ctype.modifiers & MOD_CONST)
1101 warning(left->pos, "assignment to const expression");
1102 if (type->type == SYM_NODE)
1103 type->ctype.modifiers |= MOD_ASSIGNED;
1106 static struct symbol *evaluate_assignment(struct expression *expr)
1108 struct expression *left = expr->left, *right = expr->right;
1109 struct expression *where = expr;
1110 struct symbol *ltype, *rtype;
1112 if (!lvalue_expression(left)) {
1113 warning(expr->pos, "not an lvalue");
1114 return NULL;
1117 ltype = left->ctype;
1119 rtype = degenerate(right);
1121 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1122 return NULL;
1124 evaluate_assign_to(left, ltype);
1126 expr->ctype = ltype;
1127 return ltype;
1130 static void examine_fn_arguments(struct symbol *fn)
1132 struct symbol *s;
1134 FOR_EACH_PTR(fn->arguments, s) {
1135 struct symbol *arg = evaluate_symbol(s);
1136 /* Array/function arguments silently degenerate into pointers */
1137 if (arg) {
1138 struct symbol *ptr;
1139 switch(arg->type) {
1140 case SYM_ARRAY:
1141 case SYM_FN:
1142 ptr = alloc_symbol(s->pos, SYM_PTR);
1143 if (arg->type == SYM_ARRAY)
1144 ptr->ctype = arg->ctype;
1145 else
1146 ptr->ctype.base_type = arg;
1147 ptr->ctype.as |= s->ctype.as;
1148 ptr->ctype.modifiers |= s->ctype.modifiers;
1150 s->ctype.base_type = ptr;
1151 s->ctype.as = 0;
1152 s->ctype.modifiers = 0;
1153 s->bit_size = 0;
1154 s->examined = 0;
1155 examine_symbol_type(s);
1156 break;
1157 default:
1158 /* nothing */
1159 break;
1162 } END_FOR_EACH_PTR(s);
1165 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1167 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1168 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1169 *newsym = *sym;
1170 newsym->ctype.as = as;
1171 newsym->ctype.modifiers = mod;
1172 sym = newsym;
1174 return sym;
1177 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1179 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1180 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1182 node->ctype.base_type = ptr;
1183 ptr->bit_size = bits_in_pointer;
1184 ptr->ctype.alignment = pointer_alignment;
1186 node->bit_size = bits_in_pointer;
1187 node->ctype.alignment = pointer_alignment;
1189 access_symbol(sym);
1190 if (sym->ctype.modifiers & MOD_REGISTER) {
1191 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1192 sym->ctype.modifiers &= ~MOD_REGISTER;
1194 if (sym->type == SYM_NODE) {
1195 ptr->ctype.as |= sym->ctype.as;
1196 ptr->ctype.modifiers |= sym->ctype.modifiers;
1197 sym = sym->ctype.base_type;
1199 if (degenerate && sym->type == SYM_ARRAY) {
1200 ptr->ctype.as |= sym->ctype.as;
1201 ptr->ctype.modifiers |= sym->ctype.modifiers;
1202 sym = sym->ctype.base_type;
1204 ptr->ctype.base_type = sym;
1206 return node;
1209 /* Arrays degenerate into pointers on pointer arithmetic */
1210 static struct symbol *degenerate(struct expression *expr)
1212 struct symbol *ctype, *base;
1214 if (!expr)
1215 return NULL;
1216 ctype = expr->ctype;
1217 if (!ctype)
1218 return NULL;
1219 base = ctype;
1220 if (ctype->type == SYM_NODE)
1221 base = ctype->ctype.base_type;
1223 * Arrays degenerate into pointers to the entries, while
1224 * functions degenerate into pointers to themselves.
1225 * If array was part of non-lvalue compound, we create a copy
1226 * of that compound first and then act as if we were dealing with
1227 * the corresponding field in there.
1229 switch (base->type) {
1230 case SYM_ARRAY:
1231 if (expr->type == EXPR_SLICE) {
1232 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1233 struct expression *e0, *e1, *e2, *e3, *e4;
1235 a->ctype.base_type = expr->base->ctype;
1236 a->bit_size = expr->base->ctype->bit_size;
1237 a->array_size = expr->base->ctype->array_size;
1239 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1240 e0->symbol = a;
1241 e0->ctype = &lazy_ptr_ctype;
1243 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1244 e1->unop = e0;
1245 e1->op = '*';
1246 e1->ctype = expr->base->ctype; /* XXX */
1248 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1249 e2->left = e1;
1250 e2->right = expr->base;
1251 e2->op = '=';
1252 e2->ctype = expr->base->ctype;
1254 if (expr->r_bitpos) {
1255 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1256 e3->op = '+';
1257 e3->left = e0;
1258 e3->right = alloc_const_expression(expr->pos,
1259 expr->r_bitpos >> 3);
1260 e3->ctype = &lazy_ptr_ctype;
1261 } else {
1262 e3 = e0;
1265 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1266 e4->left = e2;
1267 e4->right = e3;
1268 e4->ctype = &lazy_ptr_ctype;
1270 expr->unop = e4;
1271 expr->type = EXPR_PREOP;
1272 expr->op = '*';
1274 case SYM_FN:
1275 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1276 warning(expr->pos, "strange non-value function or array");
1277 return &bad_ctype;
1279 *expr = *expr->unop;
1280 ctype = create_pointer(expr, ctype, 1);
1281 expr->ctype = ctype;
1282 default:
1283 /* nothing */;
1285 return ctype;
1288 static struct symbol *evaluate_addressof(struct expression *expr)
1290 struct expression *op = expr->unop;
1291 struct symbol *ctype;
1293 if (op->op != '*' || op->type != EXPR_PREOP) {
1294 warning(expr->pos, "not addressable");
1295 return NULL;
1297 ctype = op->ctype;
1298 *expr = *op->unop;
1300 if (expr->type == EXPR_SYMBOL) {
1301 struct symbol *sym = expr->symbol;
1302 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1306 * symbol expression evaluation is lazy about the type
1307 * of the sub-expression, so we may have to generate
1308 * the type here if so..
1310 if (expr->ctype == &lazy_ptr_ctype) {
1311 ctype = create_pointer(expr, ctype, 0);
1312 expr->ctype = ctype;
1314 return expr->ctype;
1318 static struct symbol *evaluate_dereference(struct expression *expr)
1320 struct expression *op = expr->unop;
1321 struct symbol *ctype = op->ctype, *node, *target;
1323 /* Simplify: *&(expr) => (expr) */
1324 if (op->type == EXPR_PREOP && op->op == '&') {
1325 *expr = *op->unop;
1326 return expr->ctype;
1329 /* Dereferencing a node drops all the node information. */
1330 if (ctype->type == SYM_NODE)
1331 ctype = ctype->ctype.base_type;
1333 node = alloc_symbol(expr->pos, SYM_NODE);
1334 target = ctype->ctype.base_type;
1336 switch (ctype->type) {
1337 default:
1338 warning(expr->pos, "cannot derefence this type");
1339 return NULL;
1340 case SYM_PTR:
1341 merge_type(node, ctype);
1342 if (ctype->type != SYM_ARRAY)
1343 break;
1345 * Dereferencing a pointer to an array results in a
1346 * degenerate dereference: the expression becomes
1347 * just a pointer to the entry, and the derefence
1348 * goes away.
1350 *expr = *op;
1352 target = alloc_symbol(expr->pos, SYM_PTR);
1353 target->bit_size = bits_in_pointer;
1354 target->ctype.alignment = pointer_alignment;
1355 merge_type(target, ctype->ctype.base_type);
1356 break;
1358 case SYM_ARRAY:
1359 if (!lvalue_expression(op)) {
1360 warning(op->pos, "non-lvalue array??");
1361 return NULL;
1364 /* Do the implied "addressof" on the array */
1365 *op = *op->unop;
1368 * When an array is dereferenced, we need to pick
1369 * up the attributes of the original node too..
1371 merge_type(node, op->ctype);
1372 merge_type(node, ctype);
1373 break;
1376 node->bit_size = target->bit_size;
1377 node->array_size = target->array_size;
1379 expr->ctype = node;
1380 return node;
1384 * Unary post-ops: x++ and x--
1386 static struct symbol *evaluate_postop(struct expression *expr)
1388 struct expression *op = expr->unop;
1389 struct symbol *ctype = op->ctype;
1391 if (!lvalue_expression(expr->unop)) {
1392 warning(expr->pos, "need lvalue expression for ++/--");
1393 return NULL;
1395 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1396 warning(expr->pos, "bad operation on restricted");
1397 return NULL;
1400 evaluate_assign_to(op, ctype);
1402 expr->ctype = ctype;
1403 expr->op_value = 1;
1404 if (is_ptr_type(ctype))
1405 expr->op_value = ptr_object_size(ctype) >> 3;
1407 return ctype;
1410 static struct symbol *evaluate_sign(struct expression *expr)
1412 struct symbol *ctype = expr->unop->ctype;
1413 if (is_int_type(ctype)) {
1414 struct symbol *rtype = rtype = integer_promotion(ctype);
1415 if (rtype->bit_size != ctype->bit_size)
1416 expr->unop = cast_to(expr->unop, rtype);
1417 ctype = rtype;
1418 } else if (is_float_type(ctype) && expr->op != '~') {
1419 /* no conversions needed */
1420 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1421 /* no conversions needed */
1422 } else {
1423 return bad_expr_type(expr);
1425 if (expr->op == '+')
1426 *expr = *expr->unop;
1427 expr->ctype = ctype;
1428 return ctype;
1431 static struct symbol *evaluate_preop(struct expression *expr)
1433 struct symbol *ctype = expr->unop->ctype;
1435 switch (expr->op) {
1436 case '(':
1437 *expr = *expr->unop;
1438 return ctype;
1440 case '+':
1441 case '-':
1442 case '~':
1443 return evaluate_sign(expr);
1445 case '*':
1446 return evaluate_dereference(expr);
1448 case '&':
1449 return evaluate_addressof(expr);
1451 case SPECIAL_INCREMENT:
1452 case SPECIAL_DECREMENT:
1454 * From a type evaluation standpoint the pre-ops are
1455 * the same as the postops
1457 return evaluate_postop(expr);
1459 case '!':
1460 if (is_safe_type(ctype))
1461 warning(expr->pos, "testing a 'safe expression'");
1462 if (is_float_type(ctype)) {
1463 struct expression *arg = expr->unop;
1464 expr->type = EXPR_BINOP;
1465 expr->op = SPECIAL_EQUAL;
1466 expr->left = arg;
1467 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1468 expr->right->ctype = ctype;
1469 expr->right->fvalue = 0;
1471 ctype = &bool_ctype;
1472 break;
1474 default:
1475 break;
1477 expr->ctype = ctype;
1478 return &bool_ctype;
1481 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1483 struct ptr_list *head = (struct ptr_list *)_list;
1484 struct ptr_list *list = head;
1486 if (!head)
1487 return NULL;
1488 do {
1489 int i;
1490 for (i = 0; i < list->nr; i++) {
1491 struct symbol *sym = (struct symbol *) list->list[i];
1492 if (sym->ident) {
1493 if (sym->ident != ident)
1494 continue;
1495 *offset = sym->offset;
1496 return sym;
1497 } else {
1498 struct symbol *ctype = sym->ctype.base_type;
1499 struct symbol *sub;
1500 if (!ctype)
1501 continue;
1502 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1503 continue;
1504 sub = find_identifier(ident, ctype->symbol_list, offset);
1505 if (!sub)
1506 continue;
1507 *offset += sym->offset;
1508 return sub;
1511 } while ((list = list->next) != head);
1512 return NULL;
1515 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1517 struct expression *add;
1520 * Create a new add-expression
1522 * NOTE! Even if we just add zero, we need a new node
1523 * for the member pointer, since it has a different
1524 * type than the original pointer. We could make that
1525 * be just a cast, but the fact is, a node is a node,
1526 * so we might as well just do the "add zero" here.
1528 add = alloc_expression(expr->pos, EXPR_BINOP);
1529 add->op = '+';
1530 add->left = expr;
1531 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1532 add->right->ctype = &int_ctype;
1533 add->right->value = offset;
1536 * The ctype of the pointer will be lazily evaluated if
1537 * we ever take the address of this member dereference..
1539 add->ctype = &lazy_ptr_ctype;
1540 return add;
1543 /* structure/union dereference */
1544 static struct symbol *evaluate_member_dereference(struct expression *expr)
1546 int offset;
1547 struct symbol *ctype, *member;
1548 struct expression *deref = expr->deref, *add;
1549 struct ident *ident = expr->member;
1550 unsigned int mod;
1551 int address_space;
1553 if (!evaluate_expression(deref))
1554 return NULL;
1555 if (!ident) {
1556 warning(expr->pos, "bad member name");
1557 return NULL;
1560 ctype = deref->ctype;
1561 address_space = ctype->ctype.as;
1562 mod = ctype->ctype.modifiers;
1563 if (ctype->type == SYM_NODE) {
1564 ctype = ctype->ctype.base_type;
1565 address_space |= ctype->ctype.as;
1566 mod |= ctype->ctype.modifiers;
1568 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1569 warning(expr->pos, "expected structure or union");
1570 return NULL;
1572 offset = 0;
1573 member = find_identifier(ident, ctype->symbol_list, &offset);
1574 if (!member) {
1575 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1576 const char *name = "<unnamed>";
1577 int namelen = 9;
1578 if (ctype->ident) {
1579 name = ctype->ident->name;
1580 namelen = ctype->ident->len;
1582 warning(expr->pos, "no member '%s' in %s %.*s",
1583 show_ident(ident), type, namelen, name);
1584 return NULL;
1588 * The member needs to take on the address space and modifiers of
1589 * the "parent" type.
1591 member = convert_to_as_mod(member, address_space, mod);
1592 ctype = member->ctype.base_type;
1594 if (!lvalue_expression(deref)) {
1595 if (deref->type != EXPR_SLICE) {
1596 expr->base = deref;
1597 expr->r_bitpos = 0;
1598 } else {
1599 expr->base = deref->base;
1600 expr->r_bitpos = deref->r_bitpos;
1602 expr->r_bitpos += offset << 3;
1603 expr->type = EXPR_SLICE;
1604 expr->r_nrbits = member->bit_size;
1605 expr->r_bitpos += member->bit_offset;
1606 expr->ctype = member;
1607 return member;
1610 deref = deref->unop;
1611 expr->deref = deref;
1613 add = evaluate_offset(deref, offset);
1614 expr->type = EXPR_PREOP;
1615 expr->op = '*';
1616 expr->unop = add;
1618 expr->ctype = member;
1619 return member;
1622 static int is_promoted(struct expression *expr)
1624 while (1) {
1625 switch (expr->type) {
1626 case EXPR_BINOP:
1627 case EXPR_SELECT:
1628 case EXPR_CONDITIONAL:
1629 return 1;
1630 case EXPR_COMMA:
1631 expr = expr->right;
1632 continue;
1633 case EXPR_PREOP:
1634 switch (expr->op) {
1635 case '(':
1636 expr = expr->unop;
1637 continue;
1638 case '+':
1639 case '-':
1640 case '~':
1641 return 1;
1642 default:
1643 return 0;
1645 default:
1646 return 0;
1652 static struct symbol *evaluate_cast(struct expression *);
1654 static struct symbol *evaluate_type_information(struct expression *expr)
1656 struct symbol *sym = expr->cast_type;
1657 if (!sym) {
1658 sym = evaluate_expression(expr->cast_expression);
1659 if (!sym)
1660 return NULL;
1662 * Expressions of restricted types will possibly get
1663 * promoted - check that here
1665 if (is_restricted_type(sym)) {
1666 if (sym->bit_size < bits_in_int && is_promoted(expr))
1667 sym = &int_ctype;
1670 examine_symbol_type(sym);
1671 if (is_bitfield_type(sym)) {
1672 warning(expr->pos, "trying to examine bitfield type");
1673 return NULL;
1675 return sym;
1678 static struct symbol *evaluate_sizeof(struct expression *expr)
1680 struct symbol *type;
1681 int size;
1683 type = evaluate_type_information(expr);
1684 if (!type)
1685 return NULL;
1687 size = type->bit_size;
1688 if (size & 7)
1689 warning(expr->pos, "cannot size expression");
1690 expr->type = EXPR_VALUE;
1691 expr->value = size >> 3;
1692 expr->ctype = size_t_ctype;
1693 return size_t_ctype;
1696 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1698 struct symbol *type;
1699 int size;
1701 type = evaluate_type_information(expr);
1702 if (!type)
1703 return NULL;
1705 if (type->type == SYM_NODE)
1706 type = type->ctype.base_type;
1707 if (!type)
1708 return NULL;
1709 switch (type->type) {
1710 case SYM_ARRAY:
1711 break;
1712 case SYM_PTR:
1713 type = type->ctype.base_type;
1714 if (type)
1715 break;
1716 default:
1717 warning(expr->pos, "expected pointer expression");
1718 return NULL;
1720 size = type->bit_size;
1721 if (size & 7)
1722 size = 0;
1723 expr->type = EXPR_VALUE;
1724 expr->value = size >> 3;
1725 expr->ctype = size_t_ctype;
1726 return size_t_ctype;
1729 static struct symbol *evaluate_alignof(struct expression *expr)
1731 struct symbol *type;
1733 type = evaluate_type_information(expr);
1734 if (!type)
1735 return NULL;
1737 expr->type = EXPR_VALUE;
1738 expr->value = type->ctype.alignment;
1739 expr->ctype = size_t_ctype;
1740 return size_t_ctype;
1743 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1745 struct expression *expr;
1746 struct symbol_list *argument_types = fn->arguments;
1747 struct symbol *argtype;
1748 int i = 1;
1750 PREPARE_PTR_LIST(argument_types, argtype);
1751 FOR_EACH_PTR (head, expr) {
1752 struct expression **p = THIS_ADDRESS(expr);
1753 struct symbol *ctype, *target;
1754 ctype = evaluate_expression(expr);
1756 if (!ctype)
1757 return 0;
1759 ctype = degenerate(expr);
1761 target = argtype;
1762 if (!target && ctype->bit_size < bits_in_int)
1763 target = &int_ctype;
1764 if (target) {
1765 static char where[30];
1766 examine_symbol_type(target);
1767 sprintf(where, "argument %d", i);
1768 compatible_assignment_types(expr, target, p, ctype, where, '=');
1771 i++;
1772 NEXT_PTR_LIST(argtype);
1773 } END_FOR_EACH_PTR(expr);
1774 FINISH_PTR_LIST(argtype);
1775 return 1;
1778 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1780 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1782 struct expression *entry = *ep;
1783 struct expression **parent, *reuse = NULL;
1784 unsigned long offset;
1785 struct symbol *sym;
1786 unsigned long from, to;
1787 int accept_string = is_byte_type(ctype);
1789 from = current;
1790 to = from+1;
1791 parent = ep;
1792 if (entry->type == EXPR_INDEX) {
1793 from = entry->idx_from;
1794 to = entry->idx_to+1;
1795 parent = &entry->idx_expression;
1796 reuse = entry;
1797 entry = entry->idx_expression;
1800 offset = from * (ctype->bit_size>>3);
1801 if (offset) {
1802 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1803 reuse->type = EXPR_POS;
1804 reuse->ctype = ctype;
1805 reuse->init_offset = offset;
1806 reuse->init_nr = to - from;
1807 reuse->init_expr = entry;
1808 parent = &reuse->init_expr;
1809 entry = reuse;
1811 *ep = entry;
1813 if (accept_string && entry->type == EXPR_STRING) {
1814 sym = evaluate_expression(entry);
1815 to = from + get_expression_value(sym->array_size);
1816 } else {
1817 evaluate_initializer(ctype, parent);
1819 return to;
1822 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1824 struct expression *entry;
1825 int current = 0;
1827 FOR_EACH_PTR(expr->expr_list, entry) {
1828 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1829 } END_FOR_EACH_PTR(entry);
1832 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1833 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1835 if (expression_list_size(expr->expr_list) != 1) {
1836 warning(expr->pos, "unexpected compound initializer");
1837 return;
1839 evaluate_array_initializer(ctype, expr);
1840 return;
1843 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1845 struct symbol *sym;
1847 FOR_EACH_PTR(ctype->symbol_list, sym) {
1848 if (sym->ident == ident)
1849 return sym;
1850 } END_FOR_EACH_PTR(sym);
1851 return NULL;
1854 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1856 struct expression *entry = *ep;
1857 struct expression **parent;
1858 struct expression *reuse = NULL;
1859 unsigned long offset;
1861 if (!sym) {
1862 error(entry->pos, "unknown named initializer");
1863 return -1;
1866 if (entry->type == EXPR_IDENTIFIER) {
1867 reuse = entry;
1868 entry = entry->ident_expression;
1871 parent = ep;
1872 offset = sym->offset;
1873 if (offset) {
1874 if (!reuse)
1875 reuse = alloc_expression(entry->pos, EXPR_POS);
1876 reuse->type = EXPR_POS;
1877 reuse->ctype = sym;
1878 reuse->init_offset = offset;
1879 reuse->init_nr = 1;
1880 reuse->init_expr = entry;
1881 parent = &reuse->init_expr;
1882 entry = reuse;
1884 *ep = entry;
1885 evaluate_initializer(sym, parent);
1886 return 0;
1889 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1891 struct expression *entry;
1892 struct symbol *sym;
1894 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1895 FOR_EACH_PTR(expr->expr_list, entry) {
1896 if (entry->type == EXPR_IDENTIFIER) {
1897 struct ident *ident = entry->expr_ident;
1898 /* We special-case the "already right place" case */
1899 if (!sym || sym->ident != ident) {
1900 RESET_PTR_LIST(sym);
1901 for (;;) {
1902 if (!sym)
1903 break;
1904 if (sym->ident == ident)
1905 break;
1906 NEXT_PTR_LIST(sym);
1910 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1911 return;
1912 NEXT_PTR_LIST(sym);
1913 } END_FOR_EACH_PTR(entry);
1914 FINISH_PTR_LIST(sym);
1918 * Initializers are kind of like assignments. Except
1919 * they can be a hell of a lot more complex.
1921 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1923 struct expression *expr = *ep;
1926 * Simple non-structure/array initializers are the simple
1927 * case, and look (and parse) largely like assignments.
1929 switch (expr->type) {
1930 default: {
1931 int is_string = expr->type == EXPR_STRING;
1932 struct symbol *rtype = evaluate_expression(expr);
1933 if (rtype) {
1935 * Special case:
1936 * char array[] = "string"
1937 * should _not_ degenerate.
1939 if (!is_string || !is_string_type(ctype))
1940 rtype = degenerate(expr);
1941 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1943 return;
1946 case EXPR_INITIALIZER:
1947 expr->ctype = ctype;
1948 if (ctype->type == SYM_NODE)
1949 ctype = ctype->ctype.base_type;
1951 switch (ctype->type) {
1952 case SYM_ARRAY:
1953 case SYM_PTR:
1954 evaluate_array_initializer(ctype->ctype.base_type, expr);
1955 return;
1956 case SYM_UNION:
1957 evaluate_struct_or_union_initializer(ctype, expr, 0);
1958 return;
1959 case SYM_STRUCT:
1960 evaluate_struct_or_union_initializer(ctype, expr, 1);
1961 return;
1962 default:
1963 evaluate_scalar_initializer(ctype, expr);
1964 return;
1967 case EXPR_IDENTIFIER:
1968 if (ctype->type == SYM_NODE)
1969 ctype = ctype->ctype.base_type;
1970 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1971 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1972 show_symbol(ctype);
1973 return;
1975 evaluate_one_struct_initializer(ctype, ep,
1976 find_struct_ident(ctype, expr->expr_ident));
1977 return;
1979 case EXPR_INDEX:
1980 if (ctype->type == SYM_NODE)
1981 ctype = ctype->ctype.base_type;
1982 if (ctype->type != SYM_ARRAY) {
1983 error(expr->pos, "expected array");
1984 return;
1986 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1987 return;
1989 case EXPR_POS:
1991 * An EXPR_POS expression has already been evaluated, and we don't
1992 * need to do anything more
1994 return;
1998 static int get_as(struct symbol *sym)
2000 int as;
2001 unsigned long mod;
2003 if (!sym)
2004 return 0;
2005 as = sym->ctype.as;
2006 mod = sym->ctype.modifiers;
2007 if (sym->type == SYM_NODE) {
2008 sym = sym->ctype.base_type;
2009 as |= sym->ctype.as;
2010 mod |= sym->ctype.modifiers;
2014 * At least for now, allow casting to a "unsigned long".
2015 * That's how we do things like pointer arithmetic and
2016 * store pointers to registers.
2018 if (sym == &ulong_ctype)
2019 return -1;
2021 if (sym && sym->type == SYM_PTR) {
2022 sym = sym->ctype.base_type;
2023 as |= sym->ctype.as;
2024 mod |= sym->ctype.modifiers;
2026 if (mod & MOD_FORCE)
2027 return -1;
2028 return as;
2031 static struct symbol *evaluate_cast(struct expression *expr)
2033 struct expression *target = expr->cast_expression;
2034 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2035 enum type type;
2037 if (!target)
2038 return NULL;
2040 expr->ctype = ctype;
2041 expr->cast_type = ctype;
2044 * Special case: a cast can be followed by an
2045 * initializer, in which case we need to pass
2046 * the type value down to that initializer rather
2047 * than trying to evaluate it as an expression
2049 * A more complex case is when the initializer is
2050 * dereferenced as part of a post-fix expression.
2051 * We need to produce an expression that can be dereferenced.
2053 if (target->type == EXPR_INITIALIZER) {
2054 struct symbol *sym = expr->cast_type;
2055 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2057 sym->initializer = expr->cast_expression;
2058 evaluate_symbol(sym);
2060 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2061 addr->symbol = sym;
2063 expr->type = EXPR_PREOP;
2064 expr->op = '*';
2065 expr->unop = addr;
2066 expr->ctype = sym;
2068 return sym;
2071 evaluate_expression(target);
2072 degenerate(target);
2075 * You can always throw a value away by casting to
2076 * "void" - that's an implicit "force". Note that
2077 * the same is _not_ true of "void *".
2079 if (ctype == &void_ctype)
2080 goto out;
2082 type = ctype->type;
2083 if (type == SYM_NODE) {
2084 type = ctype->ctype.base_type->type;
2085 if (ctype->ctype.base_type == &void_ctype)
2086 goto out;
2088 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2089 warning(expr->pos, "cast to non-scalar");
2091 if (!target->ctype) {
2092 warning(expr->pos, "cast from unknown type");
2093 goto out;
2096 type = target->ctype->type;
2097 if (type == SYM_NODE)
2098 type = target->ctype->ctype.base_type->type;
2099 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2100 warning(expr->pos, "cast from non-scalar");
2102 if (!get_as(ctype) && get_as(target->ctype) > 0)
2103 warning(expr->pos, "cast removes address space of expression");
2105 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2106 struct symbol *t1 = ctype, *t2 = target->ctype;
2107 if (t1->type == SYM_NODE)
2108 t1 = t1->ctype.base_type;
2109 if (t2->type == SYM_NODE)
2110 t2 = t2->ctype.base_type;
2111 if (t1 != t2) {
2112 if (t1->type == SYM_RESTRICT)
2113 warning(expr->pos, "cast to restricted type");
2114 if (t2->type == SYM_RESTRICT)
2115 warning(expr->pos, "cast from restricted type");
2120 * Casts of constant values are special: they
2121 * can be NULL, and thus need to be simplified
2122 * early.
2124 if (target->type == EXPR_VALUE)
2125 cast_value(expr, ctype, target, target->ctype);
2127 out:
2128 return ctype;
2132 * Evaluate a call expression with a symbol. This
2133 * should expand inline functions, and evaluate
2134 * builtins.
2136 static int evaluate_symbol_call(struct expression *expr)
2138 struct expression *fn = expr->fn;
2139 struct symbol *ctype = fn->ctype;
2141 if (fn->type != EXPR_PREOP)
2142 return 0;
2144 if (ctype->op && ctype->op->evaluate)
2145 return ctype->op->evaluate(expr);
2147 if (ctype->ctype.modifiers & MOD_INLINE) {
2148 int ret;
2149 struct symbol *curr = current_fn;
2150 current_fn = ctype->ctype.base_type;
2151 examine_fn_arguments(current_fn);
2153 ret = inline_function(expr, ctype);
2155 /* restore the old function */
2156 current_fn = curr;
2157 return ret;
2160 return 0;
2163 static struct symbol *evaluate_call(struct expression *expr)
2165 int args, fnargs;
2166 struct symbol *ctype, *sym;
2167 struct expression *fn = expr->fn;
2168 struct expression_list *arglist = expr->args;
2170 if (!evaluate_expression(fn))
2171 return NULL;
2172 sym = ctype = fn->ctype;
2173 if (ctype->type == SYM_NODE)
2174 ctype = ctype->ctype.base_type;
2175 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2176 ctype = ctype->ctype.base_type;
2177 if (!evaluate_arguments(sym, ctype, arglist))
2178 return NULL;
2179 if (ctype->type != SYM_FN) {
2180 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2181 return NULL;
2183 args = expression_list_size(expr->args);
2184 fnargs = symbol_list_size(ctype->arguments);
2185 if (args < fnargs)
2186 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2187 if (args > fnargs && !ctype->variadic)
2188 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2189 if (sym->type == SYM_NODE) {
2190 if (evaluate_symbol_call(expr))
2191 return expr->ctype;
2193 expr->ctype = ctype->ctype.base_type;
2194 return expr->ctype;
2197 struct symbol *evaluate_expression(struct expression *expr)
2199 if (!expr)
2200 return NULL;
2201 if (expr->ctype)
2202 return expr->ctype;
2204 switch (expr->type) {
2205 case EXPR_VALUE:
2206 case EXPR_FVALUE:
2207 warning(expr->pos, "value expression without a type");
2208 return NULL;
2209 case EXPR_STRING:
2210 return evaluate_string(expr);
2211 case EXPR_SYMBOL:
2212 return evaluate_symbol_expression(expr);
2213 case EXPR_BINOP:
2214 if (!evaluate_expression(expr->left))
2215 return NULL;
2216 if (!evaluate_expression(expr->right))
2217 return NULL;
2218 return evaluate_binop(expr);
2219 case EXPR_LOGICAL:
2220 return evaluate_logical(expr);
2221 case EXPR_COMMA:
2222 evaluate_expression(expr->left);
2223 if (!evaluate_expression(expr->right))
2224 return NULL;
2225 return evaluate_comma(expr);
2226 case EXPR_COMPARE:
2227 if (!evaluate_expression(expr->left))
2228 return NULL;
2229 if (!evaluate_expression(expr->right))
2230 return NULL;
2231 return evaluate_compare(expr);
2232 case EXPR_ASSIGNMENT:
2233 if (!evaluate_expression(expr->left))
2234 return NULL;
2235 if (!evaluate_expression(expr->right))
2236 return NULL;
2237 return evaluate_assignment(expr);
2238 case EXPR_PREOP:
2239 if (!evaluate_expression(expr->unop))
2240 return NULL;
2241 return evaluate_preop(expr);
2242 case EXPR_POSTOP:
2243 if (!evaluate_expression(expr->unop))
2244 return NULL;
2245 return evaluate_postop(expr);
2246 case EXPR_CAST:
2247 case EXPR_IMPLIED_CAST:
2248 return evaluate_cast(expr);
2249 case EXPR_SIZEOF:
2250 return evaluate_sizeof(expr);
2251 case EXPR_PTRSIZEOF:
2252 return evaluate_ptrsizeof(expr);
2253 case EXPR_ALIGNOF:
2254 return evaluate_alignof(expr);
2255 case EXPR_DEREF:
2256 return evaluate_member_dereference(expr);
2257 case EXPR_CALL:
2258 return evaluate_call(expr);
2259 case EXPR_SELECT:
2260 case EXPR_CONDITIONAL:
2261 return evaluate_conditional_expression(expr);
2262 case EXPR_STATEMENT:
2263 expr->ctype = evaluate_statement(expr->statement);
2264 return expr->ctype;
2266 case EXPR_LABEL:
2267 expr->ctype = &ptr_ctype;
2268 return &ptr_ctype;
2270 case EXPR_TYPE:
2271 /* Evaluate the type of the symbol .. */
2272 evaluate_symbol(expr->symbol);
2273 /* .. but the type of the _expression_ is a "type" */
2274 expr->ctype = &type_ctype;
2275 return &type_ctype;
2277 /* These can not exist as stand-alone expressions */
2278 case EXPR_INITIALIZER:
2279 case EXPR_IDENTIFIER:
2280 case EXPR_INDEX:
2281 case EXPR_POS:
2282 warning(expr->pos, "internal front-end error: initializer in expression");
2283 return NULL;
2284 case EXPR_SLICE:
2285 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2286 return NULL;
2288 return NULL;
2291 static void check_duplicates(struct symbol *sym)
2293 struct symbol *next = sym;
2295 while ((next = next->same_symbol) != NULL) {
2296 const char *typediff;
2297 evaluate_symbol(next);
2298 typediff = type_difference(sym, next, 0, 0);
2299 if (typediff) {
2300 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2301 show_ident(sym->ident),
2302 stream_name(next->pos.stream), next->pos.line, typediff);
2303 return;
2308 static struct symbol *evaluate_symbol(struct symbol *sym)
2310 struct symbol *base_type;
2312 if (!sym)
2313 return sym;
2315 sym = examine_symbol_type(sym);
2316 base_type = sym->ctype.base_type;
2317 if (!base_type)
2318 return NULL;
2320 /* Evaluate the initializers */
2321 if (sym->initializer)
2322 evaluate_initializer(sym, &sym->initializer);
2324 /* And finally, evaluate the body of the symbol too */
2325 if (base_type->type == SYM_FN) {
2326 struct symbol *curr = current_fn;
2328 current_fn = base_type;
2330 examine_fn_arguments(base_type);
2331 if (!base_type->stmt && base_type->inline_stmt)
2332 uninline(sym);
2333 if (base_type->stmt)
2334 evaluate_statement(base_type->stmt);
2336 current_fn = curr;
2339 return base_type;
2342 void evaluate_symbol_list(struct symbol_list *list)
2344 struct symbol *sym;
2346 FOR_EACH_PTR(list, sym) {
2347 check_duplicates(sym);
2348 evaluate_symbol(sym);
2349 } END_FOR_EACH_PTR(sym);
2352 static struct symbol *evaluate_return_expression(struct statement *stmt)
2354 struct expression *expr = stmt->expression;
2355 struct symbol *ctype, *fntype;
2357 evaluate_expression(expr);
2358 ctype = degenerate(expr);
2359 fntype = current_fn->ctype.base_type;
2360 if (!fntype || fntype == &void_ctype) {
2361 if (expr && ctype != &void_ctype)
2362 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2363 return NULL;
2366 if (!expr) {
2367 warning(stmt->pos, "return with no return value");
2368 return NULL;
2370 if (!ctype)
2371 return NULL;
2372 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2373 return NULL;
2376 static void evaluate_if_statement(struct statement *stmt)
2378 if (!stmt->if_conditional)
2379 return;
2381 evaluate_conditional(stmt->if_conditional, 0);
2382 evaluate_statement(stmt->if_true);
2383 evaluate_statement(stmt->if_false);
2386 static void evaluate_iterator(struct statement *stmt)
2388 evaluate_conditional(stmt->iterator_pre_condition, 1);
2389 evaluate_conditional(stmt->iterator_post_condition,1);
2390 evaluate_statement(stmt->iterator_pre_statement);
2391 evaluate_statement(stmt->iterator_statement);
2392 evaluate_statement(stmt->iterator_post_statement);
2395 static void verify_output_constraint(struct expression *expr, const char *constraint)
2397 switch (*constraint) {
2398 case '=': /* Assignment */
2399 case '+': /* Update */
2400 break;
2401 default:
2402 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2406 static void verify_input_constraint(struct expression *expr, const char *constraint)
2408 switch (*constraint) {
2409 case '=': /* Assignment */
2410 case '+': /* Update */
2411 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2415 static void evaluate_asm_statement(struct statement *stmt)
2417 struct expression *expr;
2418 int state;
2420 expr = stmt->asm_string;
2421 if (!expr || expr->type != EXPR_STRING) {
2422 warning(stmt->pos, "need constant string for inline asm");
2423 return;
2426 state = 0;
2427 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2428 struct ident *ident;
2430 switch (state) {
2431 case 0: /* Identifier */
2432 state = 1;
2433 ident = (struct ident *)expr;
2434 continue;
2436 case 1: /* Constraint */
2437 state = 2;
2438 if (!expr || expr->type != EXPR_STRING) {
2439 warning(expr->pos, "asm output constraint is not a string");
2440 *THIS_ADDRESS(expr) = NULL;
2441 continue;
2443 verify_output_constraint(expr, expr->string->data);
2444 continue;
2446 case 2: /* Expression */
2447 state = 0;
2448 if (!evaluate_expression(expr))
2449 return;
2450 if (!lvalue_expression(expr))
2451 warning(expr->pos, "asm output is not an lvalue");
2452 evaluate_assign_to(expr, expr->ctype);
2453 continue;
2455 } END_FOR_EACH_PTR(expr);
2457 state = 0;
2458 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2459 struct ident *ident;
2461 switch (state) {
2462 case 0: /* Identifier */
2463 state = 1;
2464 ident = (struct ident *)expr;
2465 continue;
2467 case 1: /* Constraint */
2468 state = 2;
2469 if (!expr || expr->type != EXPR_STRING) {
2470 warning(expr->pos, "asm input constraint is not a string");
2471 *THIS_ADDRESS(expr) = NULL;
2472 continue;
2474 verify_input_constraint(expr, expr->string->data);
2475 continue;
2477 case 2: /* Expression */
2478 state = 0;
2479 if (!evaluate_expression(expr))
2480 return;
2481 continue;
2483 } END_FOR_EACH_PTR(expr);
2485 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2486 if (!expr) {
2487 warning(stmt->pos, "bad asm output");
2488 return;
2490 if (expr->type == EXPR_STRING)
2491 continue;
2492 warning(expr->pos, "asm clobber is not a string");
2493 } END_FOR_EACH_PTR(expr);
2496 struct symbol *evaluate_statement(struct statement *stmt)
2498 if (!stmt)
2499 return NULL;
2501 switch (stmt->type) {
2502 case STMT_RETURN:
2503 return evaluate_return_expression(stmt);
2505 case STMT_EXPRESSION:
2506 if (!evaluate_expression(stmt->expression))
2507 return NULL;
2508 return degenerate(stmt->expression);
2510 case STMT_COMPOUND: {
2511 struct statement *s;
2512 struct symbol *type = NULL;
2513 struct symbol *sym;
2515 /* Evaluate each symbol in the compound statement */
2516 FOR_EACH_PTR(stmt->syms, sym) {
2517 evaluate_symbol(sym);
2518 } END_FOR_EACH_PTR(sym);
2519 evaluate_symbol(stmt->ret);
2522 * Then, evaluate each statement, making the type of the
2523 * compound statement be the type of the last statement
2525 type = NULL;
2526 FOR_EACH_PTR(stmt->stmts, s) {
2527 type = evaluate_statement(s);
2528 } END_FOR_EACH_PTR(s);
2529 if (!type)
2530 type = &void_ctype;
2531 return type;
2533 case STMT_IF:
2534 evaluate_if_statement(stmt);
2535 return NULL;
2536 case STMT_ITERATOR:
2537 evaluate_iterator(stmt);
2538 return NULL;
2539 case STMT_SWITCH:
2540 evaluate_expression(stmt->switch_expression);
2541 evaluate_statement(stmt->switch_statement);
2542 return NULL;
2543 case STMT_CASE:
2544 evaluate_expression(stmt->case_expression);
2545 evaluate_expression(stmt->case_to);
2546 evaluate_statement(stmt->case_statement);
2547 return NULL;
2548 case STMT_LABEL:
2549 return evaluate_statement(stmt->label_statement);
2550 case STMT_GOTO:
2551 evaluate_expression(stmt->goto_expression);
2552 return NULL;
2553 case STMT_NONE:
2554 break;
2555 case STMT_ASM:
2556 evaluate_asm_statement(stmt);
2557 return NULL;
2558 case STMT_INTERNAL:
2559 evaluate_expression(stmt->expression);
2560 return NULL;
2562 return NULL;