Add a one-deep CC-cache for condition code setting and usage.
[smatch.git] / evaluate.c
blobca89e0cde0d078d7b2f4074a13775d9d5f6acb35
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 (i->type == EXPR_VALUE) {
468 i->value *= bit_size >> 3;
469 } else if (bit_size > bits_in_char) {
470 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
471 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
473 val->ctype = size_t_ctype;
474 val->value = bit_size >> 3;
476 mul->op = '*';
477 mul->ctype = size_t_ctype;
478 mul->left = i;
479 mul->right = val;
481 *ip = mul;
484 expr->ctype = ctype;
485 return ctype;
488 static struct symbol *evaluate_add(struct expression *expr)
490 struct expression *left = expr->left, *right = expr->right;
491 struct symbol *ltype = left->ctype, *rtype = right->ctype;
493 if (is_ptr_type(ltype))
494 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
496 if (is_ptr_type(rtype))
497 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
499 return evaluate_arith(expr, 1);
502 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
503 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
504 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
506 const char * type_difference(struct symbol *target, struct symbol *source,
507 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
509 for (;;) {
510 unsigned long mod1, mod2, diff;
511 unsigned long as1, as2;
512 int type1, type2;
513 struct symbol *base1, *base2;
515 if (target == source)
516 break;
517 if (!target || !source)
518 return "different types";
520 * Peel of per-node information.
521 * FIXME! Check alignment and context too here!
523 mod1 = target->ctype.modifiers;
524 as1 = target->ctype.as;
525 mod2 = source->ctype.modifiers;
526 as2 = source->ctype.as;
527 if (target->type == SYM_NODE) {
528 target = target->ctype.base_type;
529 if (!target)
530 return "bad types";
531 if (target->type == SYM_PTR) {
532 mod1 = 0;
533 as1 = 0;
535 mod1 |= target->ctype.modifiers;
536 as1 |= target->ctype.as;
538 if (source->type == SYM_NODE) {
539 source = source->ctype.base_type;
540 if (!source)
541 return "bad types";
542 if (source->type == SYM_PTR) {
543 mod2 = 0;
544 as2 = 0;
546 mod2 |= source->ctype.modifiers;
547 as2 |= source->ctype.as;
549 if (target->type == SYM_ENUM) {
550 target = target->ctype.base_type;
551 if (!target)
552 return "bad types";
554 if (source->type == SYM_ENUM) {
555 source = source->ctype.base_type;
556 if (!source)
557 return "bad types";
560 if (target == source)
561 break;
562 if (!target || !source)
563 return "different types";
565 type1 = target->type;
566 base1 = target->ctype.base_type;
568 type2 = source->type;
569 base2 = source->ctype.base_type;
572 * Pointers to functions compare as the function itself
574 if (type1 == SYM_PTR && base1) {
575 switch (base1->type) {
576 case SYM_FN:
577 type1 = SYM_FN;
578 target = base1;
579 base1 = base1->ctype.base_type;
580 default:
581 /* nothing */;
584 if (type2 == SYM_PTR && base2) {
585 switch (base2->type) {
586 case SYM_FN:
587 type2 = SYM_FN;
588 source = base2;
589 base2 = base2->ctype.base_type;
590 default:
591 /* nothing */;
595 /* Arrays degenerate to pointers for type comparisons */
596 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
597 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
599 if (type1 != type2 || type1 == SYM_RESTRICT)
600 return "different base types";
602 /* Must be same address space to be comparable */
603 if (as1 != as2)
604 return "different address spaces";
606 /* Ignore differences in storage types or addressability */
607 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
608 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
609 if (diff) {
610 if (diff & MOD_SIZE)
611 return "different type sizes";
612 if (diff & ~MOD_SIGNEDNESS)
613 return "different modifiers";
615 /* Differs in signedness only.. */
616 if (Wtypesign) {
618 * Warn if both are explicitly signed ("unsigned" is obvously
619 * always explicit, and since we know one of them has to be
620 * unsigned, we check if the signed one was explicit).
622 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
623 return "different explicit signedness";
626 * "char" matches both "unsigned char" and "signed char",
627 * so if the explicit test didn't trigger, then we should
628 * not warn about a char.
630 if (!(mod1 & MOD_CHAR))
631 return "different signedness";
635 if (type1 == SYM_FN) {
636 int i;
637 struct symbol *arg1, *arg2;
638 if (base1->variadic != base2->variadic)
639 return "incompatible variadic arguments";
640 PREPARE_PTR_LIST(target->arguments, arg1);
641 PREPARE_PTR_LIST(source->arguments, arg2);
642 i = 1;
643 for (;;) {
644 const char *diff;
645 diff = type_difference(arg1, arg2, 0, 0);
646 if (diff) {
647 static char argdiff[80];
648 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
649 return argdiff;
651 if (!arg1)
652 break;
653 NEXT_PTR_LIST(arg1);
654 NEXT_PTR_LIST(arg2);
655 i++;
657 FINISH_PTR_LIST(arg2);
658 FINISH_PTR_LIST(arg1);
661 target = base1;
662 source = base2;
664 return NULL;
667 static int is_null_ptr(struct expression *expr)
669 if (expr->type != EXPR_VALUE || expr->value)
670 return 0;
671 if (!is_ptr_type(expr->ctype))
672 warning(expr->pos, "Using plain integer as NULL pointer");
673 return 1;
676 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
678 /* NULL expression? Just return the type of the "other side" */
679 if (is_null_ptr(r))
680 return l->ctype;
681 if (is_null_ptr(l))
682 return r->ctype;
683 return NULL;
687 * Ignore differences in "volatile" and "const"ness when
688 * subtracting pointers
690 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
692 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
694 const char *typediff;
695 struct symbol *ctype;
696 struct symbol *ltype, *rtype;
697 struct expression *r = *rp;
699 ltype = degenerate(l);
700 rtype = degenerate(r);
703 * If it is an integer subtract: the ptr add case will do the
704 * right thing.
706 if (!is_ptr_type(rtype))
707 return evaluate_ptr_add(expr, degenerate(l), rp);
709 ctype = ltype;
710 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
711 if (typediff) {
712 ctype = common_ptr_type(l, r);
713 if (!ctype) {
714 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
715 return NULL;
718 examine_symbol_type(ctype);
720 /* Figure out the base type we point to */
721 if (ctype->type == SYM_NODE)
722 ctype = ctype->ctype.base_type;
723 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
724 warning(expr->pos, "subtraction of functions? Share your drugs");
725 return NULL;
727 ctype = ctype->ctype.base_type;
729 expr->ctype = ssize_t_ctype;
730 if (ctype->bit_size > bits_in_char) {
731 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
732 struct expression *div = expr;
733 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
735 val->ctype = size_t_ctype;
736 val->value = ctype->bit_size >> 3;
738 sub->op = '-';
739 sub->ctype = ssize_t_ctype;
740 sub->left = l;
741 sub->right = r;
743 div->op = '/';
744 div->left = sub;
745 div->right = val;
748 return ssize_t_ctype;
751 static struct symbol *evaluate_sub(struct expression *expr)
753 struct expression *left = expr->left;
754 struct symbol *ltype = left->ctype;
756 if (is_ptr_type(ltype))
757 return evaluate_ptr_sub(expr, left, &expr->right);
759 return evaluate_arith(expr, 1);
762 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
764 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
766 struct symbol *ctype;
768 if (!expr)
769 return NULL;
771 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
772 warning(expr->pos, "assignment expression in conditional");
774 ctype = evaluate_expression(expr);
775 if (ctype) {
776 if (is_safe_type(ctype))
777 warning(expr->pos, "testing a 'safe expression'");
780 return ctype;
783 static struct symbol *evaluate_logical(struct expression *expr)
785 if (!evaluate_conditional(expr->left, 0))
786 return NULL;
787 if (!evaluate_conditional(expr->right, 0))
788 return NULL;
790 expr->ctype = &bool_ctype;
791 return &bool_ctype;
794 static struct symbol *evaluate_shift(struct expression *expr)
796 struct expression *left = expr->left, *right = expr->right;
797 struct symbol *ltype = left->ctype, *rtype = right->ctype;
799 if (ltype->type == SYM_NODE)
800 ltype = ltype->ctype.base_type;
801 if (rtype->type == SYM_NODE)
802 rtype = rtype->ctype.base_type;
803 if (is_int_type(ltype) && is_int_type(rtype)) {
804 struct symbol *ctype = integer_promotion(ltype);
805 if (ltype->bit_size != ctype->bit_size)
806 expr->left = cast_to(expr->left, ctype);
807 expr->ctype = ctype;
808 ctype = integer_promotion(rtype);
809 if (rtype->bit_size != ctype->bit_size)
810 expr->right = cast_to(expr->right, ctype);
811 return expr->ctype;
813 return bad_expr_type(expr);
816 static struct symbol *evaluate_binop(struct expression *expr)
818 switch (expr->op) {
819 // addition can take ptr+int, fp and int
820 case '+':
821 return evaluate_add(expr);
823 // subtraction can take ptr-ptr, fp and int
824 case '-':
825 return evaluate_sub(expr);
827 // Arithmetic operations can take fp and int
828 case '*': case '/':
829 return evaluate_arith(expr, 1);
831 // shifts do integer promotions, but that's it.
832 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
833 return evaluate_shift(expr);
835 // The rest are integer operations
836 // '%', '&', '^', '|'
837 default:
838 return evaluate_arith(expr, 0);
842 static struct symbol *evaluate_comma(struct expression *expr)
844 expr->ctype = expr->right->ctype;
845 return expr->ctype;
848 static int modify_for_unsigned(int op)
850 if (op == '<')
851 op = SPECIAL_UNSIGNED_LT;
852 else if (op == '>')
853 op = SPECIAL_UNSIGNED_GT;
854 else if (op == SPECIAL_LTE)
855 op = SPECIAL_UNSIGNED_LTE;
856 else if (op == SPECIAL_GTE)
857 op = SPECIAL_UNSIGNED_GTE;
858 return op;
861 static struct symbol *evaluate_compare(struct expression *expr)
863 struct expression *left = expr->left, *right = expr->right;
864 struct symbol *ltype = left->ctype, *rtype = right->ctype;
865 struct symbol *ctype;
867 /* Type types? */
868 if (is_type_type(ltype) && is_type_type(rtype))
869 goto OK;
871 if (is_safe_type(ltype) || is_safe_type(rtype))
872 warning(expr->pos, "testing a 'safe expression'");
874 /* Pointer types? */
875 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
876 // FIXME! Check the types for compatibility
877 goto OK;
880 ctype = compatible_integer_binop(&expr->left, &expr->right);
881 if (ctype) {
882 if (ctype->ctype.modifiers & MOD_UNSIGNED)
883 expr->op = modify_for_unsigned(expr->op);
884 goto OK;
887 ctype = compatible_float_binop(&expr->left, &expr->right);
888 if (ctype)
889 goto OK;
891 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
892 if (ctype)
893 goto OK;
895 bad_expr_type(expr);
898 expr->ctype = &bool_ctype;
899 return &bool_ctype;
903 * FIXME!! This should do casts, array degeneration etc..
905 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
907 struct symbol *ltype = left->ctype, *rtype = right->ctype;
909 if (ltype->type == SYM_NODE)
910 ltype = ltype->ctype.base_type;
912 if (rtype->type == SYM_NODE)
913 rtype = rtype->ctype.base_type;
915 if (ltype->type == SYM_PTR) {
916 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
917 return ltype;
920 if (rtype->type == SYM_PTR) {
921 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
922 return rtype;
924 return NULL;
928 * NOTE! The degenerate case of "x ? : y", where we don't
929 * have a true case, this will possibly promote "x" to the
930 * same type as "y", and thus _change_ the conditional
931 * test in the expression. But since promotion is "safe"
932 * for testing, that's ok.
934 static struct symbol *evaluate_conditional_expression(struct expression *expr)
936 struct expression **true;
937 struct symbol *ctype, *ltype, *rtype;
938 const char * typediff;
940 if (!evaluate_conditional(expr->conditional, 0))
941 return NULL;
942 if (!evaluate_expression(expr->cond_false))
943 return NULL;
945 ctype = degenerate(expr->conditional);
946 rtype = degenerate(expr->cond_false);
948 true = &expr->conditional;
949 ltype = ctype;
950 if (expr->cond_true) {
951 if (!evaluate_expression(expr->cond_true))
952 return NULL;
953 ltype = degenerate(expr->cond_true);
954 true = &expr->cond_true;
957 ctype = compatible_integer_binop(true, &expr->cond_false);
958 if (ctype)
959 goto out;
960 ctype = compatible_ptr_type(*true, expr->cond_false);
961 if (ctype)
962 goto out;
963 ctype = compatible_float_binop(true, &expr->cond_false);
964 if (ctype)
965 goto out;
966 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
967 if (ctype)
968 goto out;
969 ctype = ltype;
970 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
971 if (!typediff)
972 goto out;
973 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
974 return NULL;
976 out:
977 expr->ctype = ctype;
978 return ctype;
981 /* FP assignments can not do modulo or bit operations */
982 static int compatible_float_op(int op)
984 return op == '=' ||
985 op == SPECIAL_ADD_ASSIGN ||
986 op == SPECIAL_SUB_ASSIGN ||
987 op == SPECIAL_MUL_ASSIGN ||
988 op == SPECIAL_DIV_ASSIGN;
991 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
992 struct expression **rp, struct symbol *source, const char *where, int op)
994 const char *typediff;
995 struct symbol *t;
996 int target_as;
998 if (is_int_type(target)) {
999 if (is_int_type(source)) {
1000 if (target->bit_size != source->bit_size)
1001 goto Cast;
1002 if (target->bit_offset != source->bit_offset)
1003 goto Cast;
1004 return 1;
1006 if (is_float_type(source))
1007 goto Cast;
1008 } else if (is_float_type(target)) {
1009 if (!compatible_float_op(op)) {
1010 warning(expr->pos, "invalid assignment");
1011 return 0;
1013 if (is_int_type(source))
1014 goto Cast;
1015 if (is_float_type(source)) {
1016 if (target->bit_size != source->bit_size)
1017 goto Cast;
1018 return 1;
1020 } else if (is_restricted_type(target)) {
1021 if (restricted_binop(op, target)) {
1022 warning(expr->pos, "bad restricted assignment");
1023 return 0;
1025 if (!restricted_value(*rp, target))
1026 return 1;
1027 } else if (is_ptr_type(target)) {
1028 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1029 evaluate_ptr_add(expr, target, rp);
1030 return 1;
1032 if (op != '=') {
1033 warning(expr->pos, "invalid pointer assignment");
1034 return 0;
1036 } else if (op != '=') {
1037 warning(expr->pos, "invalid assignment");
1038 return 0;
1041 /* It's ok if the target is more volatile or const than the source */
1042 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1043 if (!typediff)
1044 return 1;
1046 /* Pointer destination? */
1047 t = target;
1048 target_as = t->ctype.as;
1049 if (t->type == SYM_NODE) {
1050 t = t->ctype.base_type;
1051 target_as |= t->ctype.as;
1053 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1054 struct expression *right = *rp;
1055 struct symbol *s = source;
1056 int source_as;
1058 // NULL pointer is always ok
1059 if (is_null_ptr(right))
1060 return 1;
1062 /* "void *" matches anything as long as the address space is ok */
1063 source_as = s->ctype.as;
1064 if (s->type == SYM_NODE) {
1065 s = s->ctype.base_type;
1066 source_as |= s->ctype.as;
1068 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1069 s = s->ctype.base_type;
1070 t = t->ctype.base_type;
1071 if (s == &void_ctype || t == &void_ctype)
1072 return 1;
1076 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1077 info(expr->pos, " expected %s", show_typename(target));
1078 info(expr->pos, " got %s", show_typename(source));
1079 *rp = cast_to(*rp, target);
1080 return 0;
1081 Cast:
1082 *rp = cast_to(*rp, target);
1083 return 1;
1086 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1088 if (type->ctype.modifiers & MOD_CONST)
1089 warning(left->pos, "assignment to const expression");
1090 if (type->type == SYM_NODE)
1091 type->ctype.modifiers |= MOD_ASSIGNED;
1094 static struct symbol *evaluate_assignment(struct expression *expr)
1096 struct expression *left = expr->left, *right = expr->right;
1097 struct expression *where = expr;
1098 struct symbol *ltype, *rtype;
1100 if (!lvalue_expression(left)) {
1101 warning(expr->pos, "not an lvalue");
1102 return NULL;
1105 ltype = left->ctype;
1107 rtype = degenerate(right);
1109 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1110 return NULL;
1112 evaluate_assign_to(left, ltype);
1114 expr->ctype = ltype;
1115 return ltype;
1118 static void examine_fn_arguments(struct symbol *fn)
1120 struct symbol *s;
1122 FOR_EACH_PTR(fn->arguments, s) {
1123 struct symbol *arg = evaluate_symbol(s);
1124 /* Array/function arguments silently degenerate into pointers */
1125 if (arg) {
1126 struct symbol *ptr;
1127 switch(arg->type) {
1128 case SYM_ARRAY:
1129 case SYM_FN:
1130 ptr = alloc_symbol(s->pos, SYM_PTR);
1131 if (arg->type == SYM_ARRAY)
1132 ptr->ctype = arg->ctype;
1133 else
1134 ptr->ctype.base_type = arg;
1135 ptr->ctype.as |= s->ctype.as;
1136 ptr->ctype.modifiers |= s->ctype.modifiers;
1138 s->ctype.base_type = ptr;
1139 s->ctype.as = 0;
1140 s->ctype.modifiers = 0;
1141 s->bit_size = 0;
1142 s->examined = 0;
1143 examine_symbol_type(s);
1144 break;
1145 default:
1146 /* nothing */
1147 break;
1150 } END_FOR_EACH_PTR(s);
1153 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1155 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1156 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1157 *newsym = *sym;
1158 newsym->ctype.as = as;
1159 newsym->ctype.modifiers = mod;
1160 sym = newsym;
1162 return sym;
1165 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1167 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1168 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1170 node->ctype.base_type = ptr;
1171 ptr->bit_size = bits_in_pointer;
1172 ptr->ctype.alignment = pointer_alignment;
1174 node->bit_size = bits_in_pointer;
1175 node->ctype.alignment = pointer_alignment;
1177 access_symbol(sym);
1178 if (sym->ctype.modifiers & MOD_REGISTER) {
1179 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1180 sym->ctype.modifiers &= ~MOD_REGISTER;
1182 if (sym->type == SYM_NODE) {
1183 ptr->ctype.as |= sym->ctype.as;
1184 ptr->ctype.modifiers |= sym->ctype.modifiers;
1185 sym = sym->ctype.base_type;
1187 if (degenerate && sym->type == SYM_ARRAY) {
1188 ptr->ctype.as |= sym->ctype.as;
1189 ptr->ctype.modifiers |= sym->ctype.modifiers;
1190 sym = sym->ctype.base_type;
1192 ptr->ctype.base_type = sym;
1194 return node;
1197 /* Arrays degenerate into pointers on pointer arithmetic */
1198 static struct symbol *degenerate(struct expression *expr)
1200 struct symbol *ctype, *base;
1202 if (!expr)
1203 return NULL;
1204 ctype = expr->ctype;
1205 if (!ctype)
1206 return NULL;
1207 base = ctype;
1208 if (ctype->type == SYM_NODE)
1209 base = ctype->ctype.base_type;
1211 * Arrays degenerate into pointers to the entries, while
1212 * functions degenerate into pointers to themselves.
1213 * If array was part of non-lvalue compound, we create a copy
1214 * of that compound first and then act as if we were dealing with
1215 * the corresponding field in there.
1217 switch (base->type) {
1218 case SYM_ARRAY:
1219 if (expr->type == EXPR_SLICE) {
1220 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1221 struct expression *e0, *e1, *e2, *e3, *e4;
1223 a->ctype.base_type = expr->base->ctype;
1224 a->bit_size = expr->base->ctype->bit_size;
1225 a->array_size = expr->base->ctype->array_size;
1227 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1228 e0->symbol = a;
1229 e0->ctype = &lazy_ptr_ctype;
1231 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1232 e1->unop = e0;
1233 e1->op = '*';
1234 e1->ctype = expr->base->ctype; /* XXX */
1236 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1237 e2->left = e1;
1238 e2->right = expr->base;
1239 e2->op = '=';
1240 e2->ctype = expr->base->ctype;
1242 if (expr->r_bitpos) {
1243 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1244 e3->op = '+';
1245 e3->left = e0;
1246 e3->right = alloc_const_expression(expr->pos,
1247 expr->r_bitpos >> 3);
1248 e3->ctype = &lazy_ptr_ctype;
1249 } else {
1250 e3 = e0;
1253 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1254 e4->left = e2;
1255 e4->right = e3;
1256 e4->ctype = &lazy_ptr_ctype;
1258 expr->unop = e4;
1259 expr->type = EXPR_PREOP;
1260 expr->op = '*';
1262 case SYM_FN:
1263 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1264 warning(expr->pos, "strange non-value function or array");
1265 return &bad_ctype;
1267 *expr = *expr->unop;
1268 ctype = create_pointer(expr, ctype, 1);
1269 expr->ctype = ctype;
1270 default:
1271 /* nothing */;
1273 return ctype;
1276 static struct symbol *evaluate_addressof(struct expression *expr)
1278 struct expression *op = expr->unop;
1279 struct symbol *ctype;
1281 if (op->op != '*' || op->type != EXPR_PREOP) {
1282 warning(expr->pos, "not addressable");
1283 return NULL;
1285 ctype = op->ctype;
1286 *expr = *op->unop;
1289 * symbol expression evaluation is lazy about the type
1290 * of the sub-expression, so we may have to generate
1291 * the type here if so..
1293 if (expr->ctype == &lazy_ptr_ctype) {
1294 ctype = create_pointer(expr, ctype, 0);
1295 expr->ctype = ctype;
1297 return expr->ctype;
1301 static struct symbol *evaluate_dereference(struct expression *expr)
1303 struct expression *op = expr->unop;
1304 struct symbol *ctype = op->ctype, *node, *target;
1306 /* Simplify: *&(expr) => (expr) */
1307 if (op->type == EXPR_PREOP && op->op == '&') {
1308 *expr = *op->unop;
1309 return expr->ctype;
1312 /* Dereferencing a node drops all the node information. */
1313 if (ctype->type == SYM_NODE)
1314 ctype = ctype->ctype.base_type;
1316 node = alloc_symbol(expr->pos, SYM_NODE);
1317 target = ctype->ctype.base_type;
1319 switch (ctype->type) {
1320 default:
1321 warning(expr->pos, "cannot derefence this type");
1322 return NULL;
1323 case SYM_PTR:
1324 merge_type(node, ctype);
1325 if (ctype->type != SYM_ARRAY)
1326 break;
1328 * Dereferencing a pointer to an array results in a
1329 * degenerate dereference: the expression becomes
1330 * just a pointer to the entry, and the derefence
1331 * goes away.
1333 *expr = *op;
1335 target = alloc_symbol(expr->pos, SYM_PTR);
1336 target->bit_size = bits_in_pointer;
1337 target->ctype.alignment = pointer_alignment;
1338 merge_type(target, ctype->ctype.base_type);
1339 break;
1341 case SYM_ARRAY:
1342 if (!lvalue_expression(op)) {
1343 warning(op->pos, "non-lvalue array??");
1344 return NULL;
1347 /* Do the implied "addressof" on the array */
1348 *op = *op->unop;
1351 * When an array is dereferenced, we need to pick
1352 * up the attributes of the original node too..
1354 merge_type(node, op->ctype);
1355 merge_type(node, ctype);
1356 break;
1359 node->bit_size = target->bit_size;
1360 node->array_size = target->array_size;
1362 expr->ctype = node;
1363 return node;
1367 * Unary post-ops: x++ and x--
1369 static struct symbol *evaluate_postop(struct expression *expr)
1371 struct expression *op = expr->unop;
1372 struct symbol *ctype = op->ctype;
1374 if (!lvalue_expression(expr->unop)) {
1375 warning(expr->pos, "need lvalue expression for ++/--");
1376 return NULL;
1378 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1379 warning(expr->pos, "bad operation on restricted");
1380 return NULL;
1383 evaluate_assign_to(op, ctype);
1385 expr->ctype = ctype;
1386 expr->op_value = 1;
1387 if (is_ptr_type(ctype))
1388 expr->op_value = ptr_object_size(ctype) >> 3;
1390 return ctype;
1393 static struct symbol *evaluate_sign(struct expression *expr)
1395 struct symbol *ctype = expr->unop->ctype;
1396 if (is_int_type(ctype)) {
1397 struct symbol *rtype = rtype = integer_promotion(ctype);
1398 if (rtype->bit_size != ctype->bit_size)
1399 expr->unop = cast_to(expr->unop, rtype);
1400 ctype = rtype;
1401 } else if (is_float_type(ctype) && expr->op != '~') {
1402 /* no conversions needed */
1403 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1404 /* no conversions needed */
1405 } else {
1406 return bad_expr_type(expr);
1408 if (expr->op == '+')
1409 *expr = *expr->unop;
1410 expr->ctype = ctype;
1411 return ctype;
1414 static struct symbol *evaluate_preop(struct expression *expr)
1416 struct symbol *ctype = expr->unop->ctype;
1418 switch (expr->op) {
1419 case '(':
1420 *expr = *expr->unop;
1421 return ctype;
1423 case '+':
1424 case '-':
1425 case '~':
1426 return evaluate_sign(expr);
1428 case '*':
1429 return evaluate_dereference(expr);
1431 case '&':
1432 return evaluate_addressof(expr);
1434 case SPECIAL_INCREMENT:
1435 case SPECIAL_DECREMENT:
1437 * From a type evaluation standpoint the pre-ops are
1438 * the same as the postops
1440 return evaluate_postop(expr);
1442 case '!':
1443 if (is_safe_type(ctype))
1444 warning(expr->pos, "testing a 'safe expression'");
1445 if (is_float_type(ctype)) {
1446 struct expression *arg = expr->unop;
1447 expr->type = EXPR_BINOP;
1448 expr->op = SPECIAL_EQUAL;
1449 expr->left = arg;
1450 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1451 expr->right->ctype = ctype;
1452 expr->right->fvalue = 0;
1454 ctype = &bool_ctype;
1455 break;
1457 default:
1458 break;
1460 expr->ctype = ctype;
1461 return &bool_ctype;
1464 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1466 struct ptr_list *head = (struct ptr_list *)_list;
1467 struct ptr_list *list = head;
1469 if (!head)
1470 return NULL;
1471 do {
1472 int i;
1473 for (i = 0; i < list->nr; i++) {
1474 struct symbol *sym = (struct symbol *) list->list[i];
1475 if (sym->ident) {
1476 if (sym->ident != ident)
1477 continue;
1478 *offset = sym->offset;
1479 return sym;
1480 } else {
1481 struct symbol *ctype = sym->ctype.base_type;
1482 struct symbol *sub;
1483 if (!ctype)
1484 continue;
1485 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1486 continue;
1487 sub = find_identifier(ident, ctype->symbol_list, offset);
1488 if (!sub)
1489 continue;
1490 *offset += sym->offset;
1491 return sub;
1494 } while ((list = list->next) != head);
1495 return NULL;
1498 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1500 struct expression *add;
1503 * Create a new add-expression
1505 * NOTE! Even if we just add zero, we need a new node
1506 * for the member pointer, since it has a different
1507 * type than the original pointer. We could make that
1508 * be just a cast, but the fact is, a node is a node,
1509 * so we might as well just do the "add zero" here.
1511 add = alloc_expression(expr->pos, EXPR_BINOP);
1512 add->op = '+';
1513 add->left = expr;
1514 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1515 add->right->ctype = &int_ctype;
1516 add->right->value = offset;
1519 * The ctype of the pointer will be lazily evaluated if
1520 * we ever take the address of this member dereference..
1522 add->ctype = &lazy_ptr_ctype;
1523 return add;
1526 /* structure/union dereference */
1527 static struct symbol *evaluate_member_dereference(struct expression *expr)
1529 int offset;
1530 struct symbol *ctype, *member;
1531 struct expression *deref = expr->deref, *add;
1532 struct ident *ident = expr->member;
1533 unsigned int mod;
1534 int address_space;
1536 if (!evaluate_expression(deref))
1537 return NULL;
1538 if (!ident) {
1539 warning(expr->pos, "bad member name");
1540 return NULL;
1543 ctype = deref->ctype;
1544 address_space = ctype->ctype.as;
1545 mod = ctype->ctype.modifiers;
1546 if (ctype->type == SYM_NODE) {
1547 ctype = ctype->ctype.base_type;
1548 address_space |= ctype->ctype.as;
1549 mod |= ctype->ctype.modifiers;
1551 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1552 warning(expr->pos, "expected structure or union");
1553 return NULL;
1555 offset = 0;
1556 member = find_identifier(ident, ctype->symbol_list, &offset);
1557 if (!member) {
1558 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1559 const char *name = "<unnamed>";
1560 int namelen = 9;
1561 if (ctype->ident) {
1562 name = ctype->ident->name;
1563 namelen = ctype->ident->len;
1565 warning(expr->pos, "no member '%s' in %s %.*s",
1566 show_ident(ident), type, namelen, name);
1567 return NULL;
1571 * The member needs to take on the address space and modifiers of
1572 * the "parent" type.
1574 member = convert_to_as_mod(member, address_space, mod);
1575 ctype = member->ctype.base_type;
1577 if (!lvalue_expression(deref)) {
1578 if (deref->type != EXPR_SLICE) {
1579 expr->base = deref;
1580 expr->r_bitpos = 0;
1581 } else {
1582 expr->base = deref->base;
1583 expr->r_bitpos = deref->r_bitpos;
1585 expr->r_bitpos += offset << 3;
1586 expr->type = EXPR_SLICE;
1587 expr->r_nrbits = member->bit_size;
1588 expr->r_bitpos += member->bit_offset;
1589 expr->ctype = member;
1590 return member;
1593 deref = deref->unop;
1594 expr->deref = deref;
1596 add = evaluate_offset(deref, offset);
1597 expr->type = EXPR_PREOP;
1598 expr->op = '*';
1599 expr->unop = add;
1601 expr->ctype = member;
1602 return member;
1605 static int is_promoted(struct expression *expr)
1607 while (1) {
1608 switch (expr->type) {
1609 case EXPR_BINOP:
1610 case EXPR_SELECT:
1611 case EXPR_CONDITIONAL:
1612 return 1;
1613 case EXPR_COMMA:
1614 expr = expr->right;
1615 continue;
1616 case EXPR_PREOP:
1617 switch (expr->op) {
1618 case '(':
1619 expr = expr->unop;
1620 continue;
1621 case '+':
1622 case '-':
1623 case '~':
1624 return 1;
1625 default:
1626 return 0;
1628 default:
1629 return 0;
1635 static struct symbol *evaluate_cast(struct expression *);
1637 static struct symbol *evaluate_type_information(struct expression *expr)
1639 struct symbol *sym = expr->cast_type;
1640 if (!sym) {
1641 sym = evaluate_expression(expr->cast_expression);
1642 if (!sym)
1643 return NULL;
1645 * Expressions of restricted types will possibly get
1646 * promoted - check that here
1648 if (is_restricted_type(sym)) {
1649 if (sym->bit_size < bits_in_int && is_promoted(expr))
1650 sym = &int_ctype;
1653 examine_symbol_type(sym);
1654 if (is_bitfield_type(sym)) {
1655 warning(expr->pos, "trying to examine bitfield type");
1656 return NULL;
1658 return sym;
1661 static struct symbol *evaluate_sizeof(struct expression *expr)
1663 struct symbol *type;
1664 int size;
1666 type = evaluate_type_information(expr);
1667 if (!type)
1668 return NULL;
1670 size = type->bit_size;
1671 if (size & 7)
1672 warning(expr->pos, "cannot size expression");
1673 expr->type = EXPR_VALUE;
1674 expr->value = size >> 3;
1675 expr->ctype = size_t_ctype;
1676 return size_t_ctype;
1679 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1681 struct symbol *type;
1682 int size;
1684 type = evaluate_type_information(expr);
1685 if (!type)
1686 return NULL;
1688 if (type->type == SYM_NODE)
1689 type = type->ctype.base_type;
1690 if (!type)
1691 return NULL;
1692 switch (type->type) {
1693 case SYM_ARRAY:
1694 break;
1695 case SYM_PTR:
1696 type = type->ctype.base_type;
1697 if (type)
1698 break;
1699 default:
1700 warning(expr->pos, "expected pointer expression");
1701 return NULL;
1703 size = type->bit_size;
1704 if (size & 7)
1705 size = 0;
1706 expr->type = EXPR_VALUE;
1707 expr->value = size >> 3;
1708 expr->ctype = size_t_ctype;
1709 return size_t_ctype;
1712 static struct symbol *evaluate_alignof(struct expression *expr)
1714 struct symbol *type;
1716 type = evaluate_type_information(expr);
1717 if (!type)
1718 return NULL;
1720 expr->type = EXPR_VALUE;
1721 expr->value = type->ctype.alignment;
1722 expr->ctype = size_t_ctype;
1723 return size_t_ctype;
1726 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1728 struct expression *expr;
1729 struct symbol_list *argument_types = fn->arguments;
1730 struct symbol *argtype;
1731 int i = 1;
1733 PREPARE_PTR_LIST(argument_types, argtype);
1734 FOR_EACH_PTR (head, expr) {
1735 struct expression **p = THIS_ADDRESS(expr);
1736 struct symbol *ctype, *target;
1737 ctype = evaluate_expression(expr);
1739 if (!ctype)
1740 return 0;
1742 ctype = degenerate(expr);
1744 target = argtype;
1745 if (!target && ctype->bit_size < bits_in_int)
1746 target = &int_ctype;
1747 if (target) {
1748 static char where[30];
1749 examine_symbol_type(target);
1750 sprintf(where, "argument %d", i);
1751 compatible_assignment_types(expr, target, p, ctype, where, '=');
1754 i++;
1755 NEXT_PTR_LIST(argtype);
1756 } END_FOR_EACH_PTR(expr);
1757 FINISH_PTR_LIST(argtype);
1758 return 1;
1761 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1763 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1765 struct expression *entry = *ep;
1766 struct expression **parent, *reuse = NULL;
1767 unsigned long offset;
1768 struct symbol *sym;
1769 unsigned long from, to;
1770 int accept_string = is_byte_type(ctype);
1772 from = current;
1773 to = from+1;
1774 parent = ep;
1775 if (entry->type == EXPR_INDEX) {
1776 from = entry->idx_from;
1777 to = entry->idx_to+1;
1778 parent = &entry->idx_expression;
1779 reuse = entry;
1780 entry = entry->idx_expression;
1783 offset = from * (ctype->bit_size>>3);
1784 if (offset) {
1785 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1786 reuse->type = EXPR_POS;
1787 reuse->ctype = ctype;
1788 reuse->init_offset = offset;
1789 reuse->init_nr = to - from;
1790 reuse->init_expr = entry;
1791 parent = &reuse->init_expr;
1792 entry = reuse;
1794 *ep = entry;
1796 if (accept_string && entry->type == EXPR_STRING) {
1797 sym = evaluate_expression(entry);
1798 to = from + get_expression_value(sym->array_size);
1799 } else {
1800 evaluate_initializer(ctype, parent);
1802 return to;
1805 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1807 struct expression *entry;
1808 int current = 0;
1810 FOR_EACH_PTR(expr->expr_list, entry) {
1811 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1812 } END_FOR_EACH_PTR(entry);
1815 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1816 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1818 if (expression_list_size(expr->expr_list) != 1) {
1819 warning(expr->pos, "unexpected compound initializer");
1820 return;
1822 evaluate_array_initializer(ctype, expr);
1823 return;
1826 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1828 struct symbol *sym;
1830 FOR_EACH_PTR(ctype->symbol_list, sym) {
1831 if (sym->ident == ident)
1832 return sym;
1833 } END_FOR_EACH_PTR(sym);
1834 return NULL;
1837 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1839 struct expression *entry = *ep;
1840 struct expression **parent;
1841 struct expression *reuse = NULL;
1842 unsigned long offset;
1844 if (!sym) {
1845 error(entry->pos, "unknown named initializer");
1846 return -1;
1849 if (entry->type == EXPR_IDENTIFIER) {
1850 reuse = entry;
1851 entry = entry->ident_expression;
1854 parent = ep;
1855 offset = sym->offset;
1856 if (offset) {
1857 if (!reuse)
1858 reuse = alloc_expression(entry->pos, EXPR_POS);
1859 reuse->type = EXPR_POS;
1860 reuse->ctype = sym;
1861 reuse->init_offset = offset;
1862 reuse->init_nr = 1;
1863 reuse->init_expr = entry;
1864 parent = &reuse->init_expr;
1865 entry = reuse;
1867 *ep = entry;
1868 evaluate_initializer(sym, parent);
1869 return 0;
1872 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1874 struct expression *entry;
1875 struct symbol *sym;
1877 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1878 FOR_EACH_PTR(expr->expr_list, entry) {
1879 if (entry->type == EXPR_IDENTIFIER) {
1880 struct ident *ident = entry->expr_ident;
1881 /* We special-case the "already right place" case */
1882 if (!sym || sym->ident != ident) {
1883 RESET_PTR_LIST(sym);
1884 for (;;) {
1885 if (!sym)
1886 break;
1887 if (sym->ident == ident)
1888 break;
1889 NEXT_PTR_LIST(sym);
1893 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1894 return;
1895 NEXT_PTR_LIST(sym);
1896 } END_FOR_EACH_PTR(entry);
1897 FINISH_PTR_LIST(sym);
1901 * Initializers are kind of like assignments. Except
1902 * they can be a hell of a lot more complex.
1904 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1906 struct expression *expr = *ep;
1909 * Simple non-structure/array initializers are the simple
1910 * case, and look (and parse) largely like assignments.
1912 switch (expr->type) {
1913 default: {
1914 int is_string = expr->type == EXPR_STRING;
1915 struct symbol *rtype = evaluate_expression(expr);
1916 if (rtype) {
1918 * Special case:
1919 * char array[] = "string"
1920 * should _not_ degenerate.
1922 if (!is_string || !is_string_type(ctype))
1923 rtype = degenerate(expr);
1924 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1926 return;
1929 case EXPR_INITIALIZER:
1930 expr->ctype = ctype;
1931 if (ctype->type == SYM_NODE)
1932 ctype = ctype->ctype.base_type;
1934 switch (ctype->type) {
1935 case SYM_ARRAY:
1936 case SYM_PTR:
1937 evaluate_array_initializer(ctype->ctype.base_type, expr);
1938 return;
1939 case SYM_UNION:
1940 evaluate_struct_or_union_initializer(ctype, expr, 0);
1941 return;
1942 case SYM_STRUCT:
1943 evaluate_struct_or_union_initializer(ctype, expr, 1);
1944 return;
1945 default:
1946 evaluate_scalar_initializer(ctype, expr);
1947 return;
1950 case EXPR_IDENTIFIER:
1951 if (ctype->type == SYM_NODE)
1952 ctype = ctype->ctype.base_type;
1953 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1954 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1955 show_symbol(ctype);
1956 return;
1958 evaluate_one_struct_initializer(ctype, ep,
1959 find_struct_ident(ctype, expr->expr_ident));
1960 return;
1962 case EXPR_INDEX:
1963 if (ctype->type == SYM_NODE)
1964 ctype = ctype->ctype.base_type;
1965 if (ctype->type != SYM_ARRAY) {
1966 error(expr->pos, "expected array");
1967 return;
1969 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1970 return;
1972 case EXPR_POS:
1974 * An EXPR_POS expression has already been evaluated, and we don't
1975 * need to do anything more
1977 return;
1981 static int get_as(struct symbol *sym)
1983 int as;
1984 unsigned long mod;
1986 if (!sym)
1987 return 0;
1988 as = sym->ctype.as;
1989 mod = sym->ctype.modifiers;
1990 if (sym->type == SYM_NODE) {
1991 sym = sym->ctype.base_type;
1992 as |= sym->ctype.as;
1993 mod |= sym->ctype.modifiers;
1997 * At least for now, allow casting to a "unsigned long".
1998 * That's how we do things like pointer arithmetic and
1999 * store pointers to registers.
2001 if (sym == &ulong_ctype)
2002 return -1;
2004 if (sym && sym->type == SYM_PTR) {
2005 sym = sym->ctype.base_type;
2006 as |= sym->ctype.as;
2007 mod |= sym->ctype.modifiers;
2009 if (mod & MOD_FORCE)
2010 return -1;
2011 return as;
2014 static struct symbol *evaluate_cast(struct expression *expr)
2016 struct expression *target = expr->cast_expression;
2017 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2018 enum type type;
2020 if (!target)
2021 return NULL;
2023 expr->ctype = ctype;
2024 expr->cast_type = ctype;
2027 * Special case: a cast can be followed by an
2028 * initializer, in which case we need to pass
2029 * the type value down to that initializer rather
2030 * than trying to evaluate it as an expression
2032 * A more complex case is when the initializer is
2033 * dereferenced as part of a post-fix expression.
2034 * We need to produce an expression that can be dereferenced.
2036 if (target->type == EXPR_INITIALIZER) {
2037 struct symbol *sym = expr->cast_type;
2038 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2040 sym->initializer = expr->cast_expression;
2041 evaluate_symbol(sym);
2043 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2044 addr->symbol = sym;
2046 expr->type = EXPR_PREOP;
2047 expr->op = '*';
2048 expr->unop = addr;
2049 expr->ctype = sym;
2051 return sym;
2054 evaluate_expression(target);
2055 degenerate(target);
2058 * You can always throw a value away by casting to
2059 * "void" - that's an implicit "force". Note that
2060 * the same is _not_ true of "void *".
2062 if (ctype == &void_ctype)
2063 goto out;
2065 type = ctype->type;
2066 if (type == SYM_NODE) {
2067 type = ctype->ctype.base_type->type;
2068 if (ctype->ctype.base_type == &void_ctype)
2069 goto out;
2071 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2072 warning(expr->pos, "cast to non-scalar");
2074 if (!target->ctype) {
2075 warning(expr->pos, "cast from unknown type");
2076 goto out;
2079 type = target->ctype->type;
2080 if (type == SYM_NODE)
2081 type = target->ctype->ctype.base_type->type;
2082 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2083 warning(expr->pos, "cast from non-scalar");
2085 if (!get_as(ctype) && get_as(target->ctype) > 0)
2086 warning(expr->pos, "cast removes address space of expression");
2088 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2089 struct symbol *t1 = ctype, *t2 = target->ctype;
2090 if (t1->type == SYM_NODE)
2091 t1 = t1->ctype.base_type;
2092 if (t2->type == SYM_NODE)
2093 t2 = t2->ctype.base_type;
2094 if (t1 != t2) {
2095 if (t1->type == SYM_RESTRICT)
2096 warning(expr->pos, "cast to restricted type");
2097 if (t2->type == SYM_RESTRICT)
2098 warning(expr->pos, "cast from restricted type");
2103 * Casts of constant values are special: they
2104 * can be NULL, and thus need to be simplified
2105 * early.
2107 if (target->type == EXPR_VALUE)
2108 cast_value(expr, ctype, target, target->ctype);
2110 out:
2111 return ctype;
2115 * Evaluate a call expression with a symbol. This
2116 * should expand inline functions, and evaluate
2117 * builtins.
2119 static int evaluate_symbol_call(struct expression *expr)
2121 struct expression *fn = expr->fn;
2122 struct symbol *ctype = fn->ctype;
2124 if (fn->type != EXPR_PREOP)
2125 return 0;
2127 if (ctype->op && ctype->op->evaluate)
2128 return ctype->op->evaluate(expr);
2130 if (ctype->ctype.modifiers & MOD_INLINE) {
2131 int ret;
2132 struct symbol *curr = current_fn;
2133 current_fn = ctype->ctype.base_type;
2134 examine_fn_arguments(current_fn);
2136 ret = inline_function(expr, ctype);
2138 /* restore the old function */
2139 current_fn = curr;
2140 return ret;
2143 return 0;
2146 static struct symbol *evaluate_call(struct expression *expr)
2148 int args, fnargs;
2149 struct symbol *ctype, *sym;
2150 struct expression *fn = expr->fn;
2151 struct expression_list *arglist = expr->args;
2153 if (!evaluate_expression(fn))
2154 return NULL;
2155 sym = ctype = fn->ctype;
2156 if (ctype->type == SYM_NODE)
2157 ctype = ctype->ctype.base_type;
2158 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2159 ctype = ctype->ctype.base_type;
2160 if (!evaluate_arguments(sym, ctype, arglist))
2161 return NULL;
2162 if (ctype->type != SYM_FN) {
2163 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2164 return NULL;
2166 args = expression_list_size(expr->args);
2167 fnargs = symbol_list_size(ctype->arguments);
2168 if (args < fnargs)
2169 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2170 if (args > fnargs && !ctype->variadic)
2171 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2172 if (sym->type == SYM_NODE) {
2173 if (evaluate_symbol_call(expr))
2174 return expr->ctype;
2176 expr->ctype = ctype->ctype.base_type;
2177 return expr->ctype;
2180 struct symbol *evaluate_expression(struct expression *expr)
2182 if (!expr)
2183 return NULL;
2184 if (expr->ctype)
2185 return expr->ctype;
2187 switch (expr->type) {
2188 case EXPR_VALUE:
2189 case EXPR_FVALUE:
2190 warning(expr->pos, "value expression without a type");
2191 return NULL;
2192 case EXPR_STRING:
2193 return evaluate_string(expr);
2194 case EXPR_SYMBOL:
2195 return evaluate_symbol_expression(expr);
2196 case EXPR_BINOP:
2197 if (!evaluate_expression(expr->left))
2198 return NULL;
2199 if (!evaluate_expression(expr->right))
2200 return NULL;
2201 return evaluate_binop(expr);
2202 case EXPR_LOGICAL:
2203 return evaluate_logical(expr);
2204 case EXPR_COMMA:
2205 evaluate_expression(expr->left);
2206 if (!evaluate_expression(expr->right))
2207 return NULL;
2208 return evaluate_comma(expr);
2209 case EXPR_COMPARE:
2210 if (!evaluate_expression(expr->left))
2211 return NULL;
2212 if (!evaluate_expression(expr->right))
2213 return NULL;
2214 return evaluate_compare(expr);
2215 case EXPR_ASSIGNMENT:
2216 if (!evaluate_expression(expr->left))
2217 return NULL;
2218 if (!evaluate_expression(expr->right))
2219 return NULL;
2220 return evaluate_assignment(expr);
2221 case EXPR_PREOP:
2222 if (!evaluate_expression(expr->unop))
2223 return NULL;
2224 return evaluate_preop(expr);
2225 case EXPR_POSTOP:
2226 if (!evaluate_expression(expr->unop))
2227 return NULL;
2228 return evaluate_postop(expr);
2229 case EXPR_CAST:
2230 case EXPR_IMPLIED_CAST:
2231 return evaluate_cast(expr);
2232 case EXPR_SIZEOF:
2233 return evaluate_sizeof(expr);
2234 case EXPR_PTRSIZEOF:
2235 return evaluate_ptrsizeof(expr);
2236 case EXPR_ALIGNOF:
2237 return evaluate_alignof(expr);
2238 case EXPR_DEREF:
2239 return evaluate_member_dereference(expr);
2240 case EXPR_CALL:
2241 return evaluate_call(expr);
2242 case EXPR_SELECT:
2243 case EXPR_CONDITIONAL:
2244 return evaluate_conditional_expression(expr);
2245 case EXPR_STATEMENT:
2246 expr->ctype = evaluate_statement(expr->statement);
2247 return expr->ctype;
2249 case EXPR_LABEL:
2250 expr->ctype = &ptr_ctype;
2251 return &ptr_ctype;
2253 case EXPR_TYPE:
2254 /* Evaluate the type of the symbol .. */
2255 evaluate_symbol(expr->symbol);
2256 /* .. but the type of the _expression_ is a "type" */
2257 expr->ctype = &type_ctype;
2258 return &type_ctype;
2260 /* These can not exist as stand-alone expressions */
2261 case EXPR_INITIALIZER:
2262 case EXPR_IDENTIFIER:
2263 case EXPR_INDEX:
2264 case EXPR_POS:
2265 warning(expr->pos, "internal front-end error: initializer in expression");
2266 return NULL;
2267 case EXPR_SLICE:
2268 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2269 return NULL;
2271 return NULL;
2274 static void check_duplicates(struct symbol *sym)
2276 struct symbol *next = sym;
2278 while ((next = next->same_symbol) != NULL) {
2279 const char *typediff;
2280 evaluate_symbol(next);
2281 typediff = type_difference(sym, next, 0, 0);
2282 if (typediff) {
2283 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2284 show_ident(sym->ident),
2285 stream_name(next->pos.stream), next->pos.line, typediff);
2286 return;
2291 static struct symbol *evaluate_symbol(struct symbol *sym)
2293 struct symbol *base_type;
2295 if (!sym)
2296 return sym;
2298 sym = examine_symbol_type(sym);
2299 base_type = sym->ctype.base_type;
2300 if (!base_type)
2301 return NULL;
2303 /* Evaluate the initializers */
2304 if (sym->initializer)
2305 evaluate_initializer(sym, &sym->initializer);
2307 /* And finally, evaluate the body of the symbol too */
2308 if (base_type->type == SYM_FN) {
2309 struct symbol *curr = current_fn;
2311 current_fn = base_type;
2313 examine_fn_arguments(base_type);
2314 if (!base_type->stmt && base_type->inline_stmt)
2315 uninline(sym);
2316 if (base_type->stmt)
2317 evaluate_statement(base_type->stmt);
2319 current_fn = curr;
2322 return base_type;
2325 void evaluate_symbol_list(struct symbol_list *list)
2327 struct symbol *sym;
2329 FOR_EACH_PTR(list, sym) {
2330 check_duplicates(sym);
2331 evaluate_symbol(sym);
2332 } END_FOR_EACH_PTR(sym);
2335 static struct symbol *evaluate_return_expression(struct statement *stmt)
2337 struct expression *expr = stmt->expression;
2338 struct symbol *ctype, *fntype;
2340 evaluate_expression(expr);
2341 ctype = degenerate(expr);
2342 fntype = current_fn->ctype.base_type;
2343 if (!fntype || fntype == &void_ctype) {
2344 if (expr && ctype != &void_ctype)
2345 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2346 return NULL;
2349 if (!expr) {
2350 warning(stmt->pos, "return with no return value");
2351 return NULL;
2353 if (!ctype)
2354 return NULL;
2355 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2356 return NULL;
2359 static void evaluate_if_statement(struct statement *stmt)
2361 if (!stmt->if_conditional)
2362 return;
2364 evaluate_conditional(stmt->if_conditional, 0);
2365 evaluate_statement(stmt->if_true);
2366 evaluate_statement(stmt->if_false);
2369 static void evaluate_iterator(struct statement *stmt)
2371 evaluate_conditional(stmt->iterator_pre_condition, 1);
2372 evaluate_conditional(stmt->iterator_post_condition,1);
2373 evaluate_statement(stmt->iterator_pre_statement);
2374 evaluate_statement(stmt->iterator_statement);
2375 evaluate_statement(stmt->iterator_post_statement);
2378 static void verify_output_constraint(struct expression *expr, const char *constraint)
2380 switch (*constraint) {
2381 case '=': /* Assignment */
2382 case '+': /* Update */
2383 break;
2384 default:
2385 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2389 static void verify_input_constraint(struct expression *expr, const char *constraint)
2391 switch (*constraint) {
2392 case '=': /* Assignment */
2393 case '+': /* Update */
2394 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2398 static void evaluate_asm_statement(struct statement *stmt)
2400 struct expression *expr;
2401 int state;
2403 expr = stmt->asm_string;
2404 if (!expr || expr->type != EXPR_STRING) {
2405 warning(stmt->pos, "need constant string for inline asm");
2406 return;
2409 state = 0;
2410 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2411 struct ident *ident;
2413 switch (state) {
2414 case 0: /* Identifier */
2415 state = 1;
2416 ident = (struct ident *)expr;
2417 continue;
2419 case 1: /* Constraint */
2420 state = 2;
2421 if (!expr || expr->type != EXPR_STRING) {
2422 warning(expr->pos, "asm output constraint is not a string");
2423 *THIS_ADDRESS(expr) = NULL;
2424 continue;
2426 verify_output_constraint(expr, expr->string->data);
2427 continue;
2429 case 2: /* Expression */
2430 state = 0;
2431 if (!evaluate_expression(expr))
2432 return;
2433 if (!lvalue_expression(expr))
2434 warning(expr->pos, "asm output is not an lvalue");
2435 continue;
2437 } END_FOR_EACH_PTR(expr);
2439 state = 0;
2440 FOR_EACH_PTR(stmt->asm_inputs, 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 input constraint is not a string");
2453 *THIS_ADDRESS(expr) = NULL;
2454 continue;
2456 verify_input_constraint(expr, expr->string->data);
2457 continue;
2459 case 2: /* Expression */
2460 state = 0;
2461 if (!evaluate_expression(expr))
2462 return;
2463 continue;
2465 } END_FOR_EACH_PTR(expr);
2467 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2468 if (!expr) {
2469 warning(stmt->pos, "bad asm output");
2470 return;
2472 if (expr->type == EXPR_STRING)
2473 continue;
2474 warning(expr->pos, "asm clobber is not a string");
2475 } END_FOR_EACH_PTR(expr);
2478 struct symbol *evaluate_statement(struct statement *stmt)
2480 if (!stmt)
2481 return NULL;
2483 switch (stmt->type) {
2484 case STMT_RETURN:
2485 return evaluate_return_expression(stmt);
2487 case STMT_EXPRESSION:
2488 if (!evaluate_expression(stmt->expression))
2489 return NULL;
2490 return degenerate(stmt->expression);
2492 case STMT_COMPOUND: {
2493 struct statement *s;
2494 struct symbol *type = NULL;
2495 struct symbol *sym;
2497 /* Evaluate each symbol in the compound statement */
2498 FOR_EACH_PTR(stmt->syms, sym) {
2499 evaluate_symbol(sym);
2500 } END_FOR_EACH_PTR(sym);
2501 evaluate_symbol(stmt->ret);
2504 * Then, evaluate each statement, making the type of the
2505 * compound statement be the type of the last statement
2507 type = NULL;
2508 FOR_EACH_PTR(stmt->stmts, s) {
2509 type = evaluate_statement(s);
2510 } END_FOR_EACH_PTR(s);
2511 if (!type)
2512 type = &void_ctype;
2513 return type;
2515 case STMT_IF:
2516 evaluate_if_statement(stmt);
2517 return NULL;
2518 case STMT_ITERATOR:
2519 evaluate_iterator(stmt);
2520 return NULL;
2521 case STMT_SWITCH:
2522 evaluate_expression(stmt->switch_expression);
2523 evaluate_statement(stmt->switch_statement);
2524 return NULL;
2525 case STMT_CASE:
2526 evaluate_expression(stmt->case_expression);
2527 evaluate_expression(stmt->case_to);
2528 evaluate_statement(stmt->case_statement);
2529 return NULL;
2530 case STMT_LABEL:
2531 return evaluate_statement(stmt->label_statement);
2532 case STMT_GOTO:
2533 evaluate_expression(stmt->goto_expression);
2534 return NULL;
2535 case STMT_NONE:
2536 break;
2537 case STMT_ASM:
2538 evaluate_asm_statement(stmt);
2539 return NULL;
2540 case STMT_INTERNAL:
2541 evaluate_expression(stmt->expression);
2542 return NULL;
2544 return NULL;