Totally re-do how we build up the initializer tree: make the
[smatch.git] / evaluate.c
blobd27ada92b03affda6fe5a828c559967dcf239670
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 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 "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warning(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (sym->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = &lazy_ptr_ctype;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static inline struct symbol *integer_promotion(struct symbol *type)
113 unsigned long mod = type->ctype.modifiers;
114 int width;
116 if (type->type == SYM_ENUM)
117 return &int_ctype;
118 else if (type->type == SYM_BITFIELD) {
119 mod = type->ctype.base_type->ctype.modifiers;
120 width = type->fieldwidth;
121 } else if (mod & (MOD_CHAR | MOD_SHORT))
122 width = type->bit_size;
123 else
124 return type;
125 if (mod & MOD_UNSIGNED && width == bits_in_int)
126 return &uint_ctype;
127 return &int_ctype;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static struct expression * cast_to(struct expression *old, struct symbol *type)
178 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
179 expr->ctype = type;
180 expr->cast_type = type;
181 expr->cast_expression = old;
182 return expr;
185 static int is_type_type(struct symbol *type)
187 return (type->ctype.modifiers & MOD_TYPE) != 0;
190 static int is_ptr_type(struct symbol *type)
192 if (type->type == SYM_NODE)
193 type = type->ctype.base_type;
194 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
197 static inline int is_float_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return type->ctype.base_type == &fp_type;
204 static inline int is_byte_type(struct symbol *type)
206 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
209 static inline int is_string_type(struct symbol *type)
211 if (type->type == SYM_NODE)
212 type = type->ctype.base_type;
213 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
216 static struct symbol *bad_expr_type(struct expression *expr)
218 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
219 switch (expr->type) {
220 case EXPR_BINOP:
221 case EXPR_COMPARE:
222 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
223 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
224 break;
225 case EXPR_PREOP:
226 case EXPR_POSTOP:
227 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
228 break;
229 default:
230 break;
233 return NULL;
236 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
238 struct expression *left = *lp, *right = *rp;
239 struct symbol *ltype = left->ctype, *rtype = right->ctype;
241 if (ltype->type == SYM_NODE)
242 ltype = ltype->ctype.base_type;
243 if (rtype->type == SYM_NODE)
244 rtype = rtype->ctype.base_type;
245 if (is_float_type(ltype)) {
246 if (is_int_type(rtype))
247 goto Left;
248 if (is_float_type(rtype)) {
249 unsigned long lmod = ltype->ctype.modifiers;
250 unsigned long rmod = rtype->ctype.modifiers;
251 lmod &= MOD_LONG | MOD_LONGLONG;
252 rmod &= MOD_LONG | MOD_LONGLONG;
253 if (lmod == rmod)
254 return ltype;
255 if (lmod & ~rmod)
256 goto Left;
257 else
258 goto Right;
260 return NULL;
262 if (!is_float_type(rtype) || !is_int_type(ltype))
263 return NULL;
264 Right:
265 *lp = cast_to(left, rtype);
266 return rtype;
267 Left:
268 *rp = cast_to(right, ltype);
269 return ltype;
272 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
274 struct expression *left = *lp, *right = *rp;
275 struct symbol *ltype = left->ctype, *rtype = right->ctype;
277 if (ltype->type == SYM_NODE)
278 ltype = ltype->ctype.base_type;
279 if (rtype->type == SYM_NODE)
280 rtype = rtype->ctype.base_type;
281 if (is_int_type(ltype) && is_int_type(rtype)) {
282 struct symbol *ctype = bigger_int_type(ltype, rtype);
284 /* Don't bother promoting same-size entities, it only adds clutter */
285 if (ltype->bit_size != ctype->bit_size)
286 *lp = cast_to(left, ctype);
287 if (rtype->bit_size != ctype->bit_size)
288 *rp = cast_to(right, ctype);
289 return ctype;
291 return NULL;
294 static int restricted_value(struct expression *v, struct symbol *type)
296 if (v->type != EXPR_VALUE)
297 return 1;
298 if (v->value != 0)
299 return 1;
300 return 0;
303 static int restricted_binop(int op, struct symbol *type)
305 switch (op) {
306 case '&':
307 case '|':
308 case '^':
309 case '?':
310 case SPECIAL_EQUAL:
311 case SPECIAL_NOTEQUAL:
312 return 0;
313 default:
314 return 1;
318 static int restricted_unop(int op, struct symbol *type)
320 if (op == '~' && type->bit_size >= bits_in_int)
321 return 0;
322 return 1;
325 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
327 struct expression *left = *lp, *right = *rp;
328 struct symbol *ltype = left->ctype, *rtype = right->ctype;
329 struct symbol *type = NULL;
331 if (ltype->type == SYM_NODE)
332 ltype = ltype->ctype.base_type;
333 if (rtype->type == SYM_NODE)
334 rtype = rtype->ctype.base_type;
335 if (is_restricted_type(ltype)) {
336 if (is_restricted_type(rtype)) {
337 if (ltype == rtype)
338 type = ltype;
339 } else {
340 if (!restricted_value(right, ltype))
341 type = ltype;
343 } else if (is_restricted_type(rtype)) {
344 if (!restricted_value(left, rtype))
345 type = rtype;
347 if (!type)
348 return NULL;
349 if (restricted_binop(op, type))
350 return NULL;
351 return type;
354 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
356 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
357 if (!ctype && float_ok)
358 ctype = compatible_float_binop(&expr->left, &expr->right);
359 if (!ctype)
360 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
361 if (ctype) {
362 expr->ctype = ctype;
363 return ctype;
365 return bad_expr_type(expr);
368 static inline int lvalue_expression(struct expression *expr)
370 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
373 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
375 struct symbol *ctype;
376 struct symbol *ptr_type = ptr->ctype;
377 int bit_size;
379 if (ptr_type->type == SYM_NODE)
380 ptr_type = ptr_type->ctype.base_type;
382 if (!is_int_type(i->ctype))
383 return bad_expr_type(expr);
385 ctype = ptr->ctype;
386 examine_symbol_type(ctype);
388 ctype = degenerate(ptr);
389 if (!ctype->ctype.base_type) {
390 warning(expr->pos, "missing type information");
391 return NULL;
394 /* Get the size of whatever the pointer points to */
395 ptr_type = ctype;
396 if (ptr_type->type == SYM_NODE)
397 ptr_type = ptr_type->ctype.base_type;
398 if (ptr_type->type == SYM_PTR)
399 ptr_type = ptr_type->ctype.base_type;
400 bit_size = ptr_type->bit_size;
402 /* Special case: adding zero commonly happens as a result of 'array[0]' */
403 if (i->type == EXPR_VALUE && !i->value) {
404 *expr = *ptr;
405 } else if (bit_size > bits_in_char) {
406 struct expression *add = expr;
407 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
408 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
410 val->ctype = size_t_ctype;
411 val->value = bit_size >> 3;
413 mul->op = '*';
414 mul->ctype = size_t_ctype;
415 mul->left = i;
416 mul->right = val;
418 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
419 add->left = ptr;
420 add->right = mul;
423 expr->ctype = ctype;
424 return ctype;
427 static struct symbol *evaluate_add(struct expression *expr)
429 struct expression *left = expr->left, *right = expr->right;
430 struct symbol *ltype = left->ctype, *rtype = right->ctype;
432 if (is_ptr_type(ltype))
433 return evaluate_ptr_add(expr, left, right);
435 if (is_ptr_type(rtype))
436 return evaluate_ptr_add(expr, right, left);
438 return evaluate_arith(expr, 1);
441 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
442 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
443 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
444 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED)
446 const char * type_difference(struct symbol *target, struct symbol *source,
447 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
449 for (;;) {
450 unsigned long mod1, mod2, diff;
451 unsigned long as1, as2;
452 int type1, type2;
453 struct symbol *base1, *base2;
455 if (target == source)
456 break;
457 if (!target || !source)
458 return "different types";
460 * Peel of per-node information.
461 * FIXME! Check alignment and context too here!
463 mod1 = target->ctype.modifiers;
464 as1 = target->ctype.as;
465 mod2 = source->ctype.modifiers;
466 as2 = source->ctype.as;
467 if (target->type == SYM_NODE) {
468 target = target->ctype.base_type;
469 if (!target)
470 return "bad types";
471 if (target->type == SYM_PTR) {
472 mod1 = 0;
473 as1 = 0;
475 mod1 |= target->ctype.modifiers;
476 as1 |= target->ctype.as;
478 if (source->type == SYM_NODE) {
479 source = source->ctype.base_type;
480 if (!source)
481 return "bad types";
482 if (source->type == SYM_PTR) {
483 mod2 = 0;
484 as2 = 0;
486 mod2 |= source->ctype.modifiers;
487 as2 |= source->ctype.as;
490 if (target == source)
491 break;
492 if (!target || !source)
493 return "different types";
495 type1 = target->type;
496 base1 = target->ctype.base_type;
498 type2 = source->type;
499 base2 = source->ctype.base_type;
502 * Pointers to functions compare as the function itself
504 if (type1 == SYM_PTR && base1) {
505 switch (base1->type) {
506 case SYM_FN:
507 type1 = SYM_FN;
508 target = base1;
509 base1 = base1->ctype.base_type;
510 default:
511 /* nothing */;
514 if (type2 == SYM_PTR && base2) {
515 switch (base2->type) {
516 case SYM_FN:
517 type2 = SYM_FN;
518 source = base2;
519 base2 = base2->ctype.base_type;
520 default:
521 /* nothing */;
525 /* Arrays degenerate to pointers for type comparisons */
526 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
527 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
529 if (type1 != type2 || type1 == SYM_RESTRICT)
530 return "different base types";
532 /* Must be same address space to be comparable */
533 if (as1 != as2)
534 return "different address spaces";
536 /* Ignore differences in storage types or addressability */
537 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
538 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
539 if (diff) {
540 if (diff & MOD_SIZE)
541 return "different type sizes";
542 if (diff & ~MOD_SIGNEDNESS)
543 return "different modifiers";
545 /* Differs in signedness only.. */
546 if (Wtypesign) {
548 * Warn if both are explicitly signed ("unsigned" is obvously
549 * always explicit, and since we know one of them has to be
550 * unsigned, we check if the signed one was explicit).
552 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
553 return "different explicit signedness";
556 * "char" matches both "unsigned char" and "signed char",
557 * so if the explicit test didn't trigger, then we should
558 * not warn about a char.
560 if (!(mod1 & MOD_CHAR))
561 return "different signedness";
565 if (type1 == SYM_FN) {
566 int i;
567 struct symbol *arg1, *arg2;
568 if (base1->variadic != base2->variadic)
569 return "incompatible variadic arguments";
570 PREPARE_PTR_LIST(target->arguments, arg1);
571 PREPARE_PTR_LIST(source->arguments, arg2);
572 i = 1;
573 for (;;) {
574 const char *diff;
575 diff = type_difference(arg1, arg2, 0, 0);
576 if (diff) {
577 static char argdiff[80];
578 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
579 return argdiff;
581 if (!arg1)
582 break;
583 NEXT_PTR_LIST(arg1);
584 NEXT_PTR_LIST(arg2);
585 i++;
587 FINISH_PTR_LIST(arg2);
588 FINISH_PTR_LIST(arg1);
591 target = base1;
592 source = base2;
594 return NULL;
597 static int is_null_ptr(struct expression *expr)
599 if (expr->type != EXPR_VALUE || expr->value)
600 return 0;
601 if (!is_ptr_type(expr->ctype))
602 warning(expr->pos, "Using plain integer as NULL pointer");
603 return 1;
606 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
608 /* NULL expression? Just return the type of the "other side" */
609 if (is_null_ptr(r))
610 return l->ctype;
611 if (is_null_ptr(l))
612 return r->ctype;
613 return NULL;
617 * Ignore differences in "volatile" and "const"ness when
618 * subtracting pointers
620 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
622 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
624 const char *typediff;
625 struct symbol *ctype;
626 struct symbol *ltype, *rtype;
628 ltype = degenerate(l);
629 rtype = degenerate(r);
632 * If it is an integer subtract: the ptr add case will do the
633 * right thing.
635 if (!is_ptr_type(rtype))
636 return evaluate_ptr_add(expr, l, r);
638 ctype = ltype;
639 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
640 if (typediff) {
641 ctype = common_ptr_type(l, r);
642 if (!ctype) {
643 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
644 return NULL;
647 examine_symbol_type(ctype);
649 /* Figure out the base type we point to */
650 if (ctype->type == SYM_NODE)
651 ctype = ctype->ctype.base_type;
652 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
653 warning(expr->pos, "subtraction of functions? Share your drugs");
654 return NULL;
656 ctype = ctype->ctype.base_type;
658 expr->ctype = ssize_t_ctype;
659 if (ctype->bit_size > bits_in_char) {
660 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
661 struct expression *div = expr;
662 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
664 val->ctype = size_t_ctype;
665 val->value = ctype->bit_size >> 3;
667 sub->op = '-';
668 sub->ctype = ssize_t_ctype;
669 sub->left = l;
670 sub->right = r;
672 div->op = '/';
673 div->left = sub;
674 div->right = val;
677 return ssize_t_ctype;
680 static struct symbol *evaluate_sub(struct expression *expr)
682 struct expression *left = expr->left, *right = expr->right;
683 struct symbol *ltype = left->ctype;
685 if (is_ptr_type(ltype))
686 return evaluate_ptr_sub(expr, left, right);
688 return evaluate_arith(expr, 1);
691 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
693 static struct symbol *evaluate_conditional(struct expression **p)
695 struct symbol *ctype;
696 struct expression *expr = *p;
698 if (!expr)
699 return NULL;
701 if (expr->type == EXPR_ASSIGNMENT)
702 warning(expr->pos, "assignment expression in conditional");
704 ctype = evaluate_expression(expr);
705 if (ctype) {
706 if (is_safe_type(ctype))
707 warning(expr->pos, "testing a 'safe expression'");
708 if (is_float_type(ctype)) {
709 struct expression *comp;
711 * It's easier to handle here, rather than deal with
712 * FP all over the place. Floating point in boolean
713 * context is rare enough (and very often wrong),
714 * so price of explicit comparison with appropriate
715 * FP zero is not too high. And it simplifies things
716 * elsewhere.
718 comp = alloc_expression(expr->pos, EXPR_BINOP);
719 comp->op = SPECIAL_NOTEQUAL;
720 comp->left = expr;
721 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
722 comp->right->ctype = comp->left->ctype;
723 comp->right->fvalue = 0;
724 ctype = comp->ctype = &bool_ctype;
725 *p = comp;
729 return ctype;
732 static struct symbol *evaluate_logical(struct expression *expr)
734 if (!evaluate_conditional(&expr->left))
735 return NULL;
736 if (!evaluate_conditional(&expr->right))
737 return NULL;
739 expr->ctype = &bool_ctype;
740 return &bool_ctype;
743 static struct symbol *evaluate_shift(struct expression *expr)
745 struct expression *left = expr->left, *right = expr->right;
746 struct symbol *ltype = left->ctype, *rtype = right->ctype;
748 if (ltype->type == SYM_NODE)
749 ltype = ltype->ctype.base_type;
750 if (rtype->type == SYM_NODE)
751 rtype = rtype->ctype.base_type;
752 if (is_int_type(ltype) && is_int_type(rtype)) {
753 struct symbol *ctype = integer_promotion(ltype);
754 if (ltype->bit_size != ctype->bit_size)
755 expr->left = cast_to(expr->left, ctype);
756 expr->ctype = ctype;
757 ctype = integer_promotion(rtype);
758 if (rtype->bit_size != ctype->bit_size)
759 expr->right = cast_to(expr->right, ctype);
760 return expr->ctype;
762 return bad_expr_type(expr);
765 static struct symbol *evaluate_binop(struct expression *expr)
767 switch (expr->op) {
768 // addition can take ptr+int, fp and int
769 case '+':
770 return evaluate_add(expr);
772 // subtraction can take ptr-ptr, fp and int
773 case '-':
774 return evaluate_sub(expr);
776 // Arithmetic operations can take fp and int
777 case '*': case '/':
778 return evaluate_arith(expr, 1);
780 // shifts do integer promotions, but that's it.
781 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
782 return evaluate_shift(expr);
784 // The rest are integer operations
785 // '%', '&', '^', '|'
786 default:
787 return evaluate_arith(expr, 0);
791 static struct symbol *evaluate_comma(struct expression *expr)
793 expr->ctype = expr->right->ctype;
794 return expr->ctype;
797 static int modify_for_unsigned(int op)
799 if (op == '<')
800 op = SPECIAL_UNSIGNED_LT;
801 else if (op == '>')
802 op = SPECIAL_UNSIGNED_GT;
803 else if (op == SPECIAL_LTE)
804 op = SPECIAL_UNSIGNED_LTE;
805 else if (op == SPECIAL_GTE)
806 op = SPECIAL_UNSIGNED_GTE;
807 return op;
810 static struct symbol *evaluate_compare(struct expression *expr)
812 struct expression *left = expr->left, *right = expr->right;
813 struct symbol *ltype = left->ctype, *rtype = right->ctype;
814 struct symbol *ctype;
816 /* Type types? */
817 if (is_type_type(ltype) && is_type_type(rtype))
818 goto OK;
820 if (is_safe_type(ltype) || is_safe_type(rtype))
821 warning(expr->pos, "testing a 'safe expression'");
823 /* Pointer types? */
824 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
825 // FIXME! Check the types for compatibility
826 goto OK;
829 ctype = compatible_integer_binop(&expr->left, &expr->right);
830 if (ctype) {
831 if (ctype->ctype.modifiers & MOD_UNSIGNED)
832 expr->op = modify_for_unsigned(expr->op);
833 goto OK;
836 ctype = compatible_float_binop(&expr->left, &expr->right);
837 if (ctype)
838 goto OK;
840 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
841 if (ctype)
842 goto OK;
844 bad_expr_type(expr);
847 expr->ctype = &bool_ctype;
848 return &bool_ctype;
852 * FIXME!! This should do casts, array degeneration etc..
854 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
856 struct symbol *ltype = left->ctype, *rtype = right->ctype;
858 if (ltype->type == SYM_NODE)
859 ltype = ltype->ctype.base_type;
861 if (rtype->type == SYM_NODE)
862 rtype = rtype->ctype.base_type;
864 if (ltype->type == SYM_PTR) {
865 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
866 return ltype;
869 if (rtype->type == SYM_PTR) {
870 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
871 return rtype;
873 return NULL;
876 static struct symbol * evaluate_conditional_expression(struct expression *expr)
878 struct expression *cond, *false;
879 struct expression **true_p;
880 struct symbol *ctype, *ltype, *rtype;
881 const char * typediff;
883 ctype = degenerate(expr->conditional);
884 cond = expr->conditional;
886 ltype = ctype;
887 true_p = &expr->conditional;
888 if (expr->cond_true) {
889 ltype = degenerate(expr->cond_true);
890 true_p = &expr->cond_true;
893 rtype = degenerate(expr->cond_false);
894 false = expr->cond_false;
896 ctype = ltype;
897 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
898 if (!typediff)
899 goto out;
901 ctype = compatible_integer_binop(true_p, &expr->cond_false);
902 if (ctype)
903 goto out;
904 ctype = compatible_ptr_type(*true_p, expr->cond_false);
905 if (ctype)
906 goto out;
907 ctype = compatible_float_binop(true_p, &expr->cond_false);
908 if (ctype)
909 goto out;
910 ctype = compatible_restricted_binop('?', true_p, &expr->cond_false);
911 if (ctype)
912 goto out;
913 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
914 return NULL;
916 out:
917 expr->ctype = ctype;
918 return ctype;
921 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
922 struct expression **rp, struct symbol *source, const char *where)
924 const char *typediff;
925 struct symbol *t;
926 int target_as;
928 /* It's ok if the target is more volatile or const than the source */
929 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
930 if (!typediff)
931 return 1;
933 if (is_int_type(target)) {
934 if (is_int_type(source)) {
935 if (target->bit_size != source->bit_size)
936 goto Cast;
937 return 1;
939 if (is_float_type(source))
940 goto Cast;
941 } else if (is_float_type(target)) {
942 if (is_int_type(source))
943 goto Cast;
944 if (is_float_type(source)) {
945 if (target->bit_size != source->bit_size)
946 goto Cast;
947 return 1;
951 if (is_restricted_type(target) && !restricted_value(*rp, target))
952 return 1;
954 /* Pointer destination? */
955 t = target;
956 target_as = t->ctype.as;
957 if (t->type == SYM_NODE) {
958 t = t->ctype.base_type;
959 target_as |= t->ctype.as;
961 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
962 struct expression *right = *rp;
963 struct symbol *s = source;
964 int source_as;
966 // NULL pointer is always ok
967 if (is_null_ptr(right))
968 return 1;
970 /* "void *" matches anything as long as the address space is ok */
971 source_as = s->ctype.as;
972 if (s->type == SYM_NODE) {
973 s = s->ctype.base_type;
974 source_as |= s->ctype.as;
976 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
977 s = s->ctype.base_type;
978 t = t->ctype.base_type;
979 if (s == &void_ctype || t == &void_ctype)
980 return 1;
984 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
985 info(expr->pos, " expected %s", show_typename(target));
986 info(expr->pos, " got %s", show_typename(source));
987 *rp = cast_to(*rp, target);
988 return 0;
989 Cast:
990 *rp = cast_to(*rp, target);
991 return 1;
995 * FIXME!! This is wrong from a double evaluation standpoint. We can't
996 * just expand the expression twice, that would make any side effects
997 * happen twice too.
999 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
1001 int op = expr->op;
1002 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
1003 static const int op_trans[] = {
1004 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
1005 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
1006 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
1007 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
1008 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
1009 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
1010 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
1011 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
1012 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
1013 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
1015 struct expression *e0, *e1, *e2, *e3, *e4, *e5;
1016 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1017 struct symbol *ltype = left->ctype;
1018 struct expression *addr;
1019 struct symbol *lptype;
1021 if (left->type == EXPR_BITFIELD)
1022 addr = left->address;
1023 else
1024 addr = left->unop;
1026 lptype = addr->ctype;
1028 a->ctype.base_type = lptype;
1029 a->bit_size = lptype->bit_size;
1030 a->array_size = lptype->array_size;
1032 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1033 e0->symbol = a;
1034 e0->ctype = &lazy_ptr_ctype;
1036 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1037 e1->unop = e0;
1038 e1->op = '*';
1039 e1->ctype = lptype;
1041 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1042 e2->left = e1;
1043 e2->right = addr;
1044 e2->op = '=';
1045 e2->ctype = lptype;
1047 /* we can't cannibalize left, unfortunately */
1048 e3 = alloc_expression(expr->pos, left->type);
1049 *e3 = *left;
1050 if (e3->type == EXPR_BITFIELD)
1051 e3->address = e1;
1052 else
1053 e3->unop = e1;
1055 e4 = alloc_expression(expr->pos, EXPR_BINOP);
1056 e4->op = subexpr->op = op_trans[op - SPECIAL_BASE];
1057 e4->left = e3;
1058 e4->right = right;
1059 /* will calculate type later */
1061 e5 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1062 e5->left = e3; /* we can share that one */
1063 e5->right = e4;
1064 e5->op = '=';
1065 e5->ctype = ltype;
1067 expr->type = EXPR_COMMA;
1068 expr->left = e2;
1069 expr->right = e5;
1070 expr->ctype = ltype;
1072 return evaluate_binop(e4);
1075 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1077 if (type->ctype.modifiers & MOD_CONST)
1078 warning(left->pos, "assignment to const expression");
1079 if (type->type == SYM_NODE)
1080 type->ctype.modifiers |= MOD_ASSIGNED;
1083 static struct symbol *evaluate_assignment(struct expression *expr)
1085 struct expression *left = expr->left, *right = expr->right;
1086 struct expression *where = expr;
1087 struct symbol *ltype, *rtype;
1089 if (!lvalue_expression(left)) {
1090 warning(expr->pos, "not an lvalue");
1091 return NULL;
1094 ltype = left->ctype;
1096 if (expr->op != '=') {
1097 if (!evaluate_binop_assignment(expr, left, right))
1098 return NULL;
1099 where = expr->right; /* expr is EXPR_COMMA now */
1100 left = where->left;
1101 right = where->right;
1104 rtype = degenerate(right);
1106 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1107 return NULL;
1109 evaluate_assign_to(left, ltype);
1111 expr->ctype = ltype;
1112 return ltype;
1115 static void examine_fn_arguments(struct symbol *fn)
1117 struct symbol *s;
1119 FOR_EACH_PTR(fn->arguments, s) {
1120 struct symbol *arg = evaluate_symbol(s);
1121 /* Array/function arguments silently degenerate into pointers */
1122 if (arg) {
1123 struct symbol *ptr;
1124 switch(arg->type) {
1125 case SYM_ARRAY:
1126 case SYM_FN:
1127 ptr = alloc_symbol(s->pos, SYM_PTR);
1128 if (arg->type == SYM_ARRAY)
1129 ptr->ctype = arg->ctype;
1130 else
1131 ptr->ctype.base_type = arg;
1132 ptr->ctype.as |= s->ctype.as;
1133 ptr->ctype.modifiers |= s->ctype.modifiers;
1135 s->ctype.base_type = ptr;
1136 s->ctype.as = 0;
1137 s->ctype.modifiers = 0;
1138 examine_symbol_type(s);
1139 break;
1140 default:
1141 /* nothing */
1142 break;
1145 } END_FOR_EACH_PTR(s);
1148 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1150 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1151 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1152 *newsym = *sym;
1153 newsym->ctype.as = as;
1154 newsym->ctype.modifiers = mod;
1155 sym = newsym;
1157 return sym;
1160 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1162 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1163 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1165 node->ctype.base_type = ptr;
1166 ptr->bit_size = bits_in_pointer;
1167 ptr->ctype.alignment = pointer_alignment;
1169 node->bit_size = bits_in_pointer;
1170 node->ctype.alignment = pointer_alignment;
1172 access_symbol(sym);
1173 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1174 if (sym->ctype.modifiers & MOD_REGISTER) {
1175 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1176 sym->ctype.modifiers &= ~MOD_REGISTER;
1178 if (sym->type == SYM_NODE) {
1179 ptr->ctype.as |= sym->ctype.as;
1180 ptr->ctype.modifiers |= sym->ctype.modifiers;
1181 sym = sym->ctype.base_type;
1183 if (degenerate && sym->type == SYM_ARRAY) {
1184 ptr->ctype.as |= sym->ctype.as;
1185 ptr->ctype.modifiers |= sym->ctype.modifiers;
1186 sym = sym->ctype.base_type;
1188 ptr->ctype.base_type = sym;
1190 return node;
1193 /* Arrays degenerate into pointers on pointer arithmetic */
1194 static struct symbol *degenerate(struct expression *expr)
1196 struct symbol *ctype, *base;
1198 if (!expr)
1199 return NULL;
1200 ctype = expr->ctype;
1201 if (!ctype)
1202 return NULL;
1203 base = ctype;
1204 if (ctype->type == SYM_NODE)
1205 base = ctype->ctype.base_type;
1207 * Arrays degenerate into pointers to the entries, while
1208 * functions degenerate into pointers to themselves.
1209 * If array was part of non-lvalue compound, we create a copy
1210 * of that compound first and then act as if we were dealing with
1211 * the corresponding field in there.
1213 switch (base->type) {
1214 case SYM_ARRAY:
1215 if (expr->type == EXPR_SLICE) {
1216 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1217 struct expression *e0, *e1, *e2, *e3, *e4;
1219 a->ctype.base_type = expr->base->ctype;
1220 a->bit_size = expr->base->ctype->bit_size;
1221 a->array_size = expr->base->ctype->array_size;
1223 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1224 e0->symbol = a;
1225 e0->ctype = &lazy_ptr_ctype;
1227 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1228 e1->unop = e0;
1229 e1->op = '*';
1230 e1->ctype = expr->base->ctype; /* XXX */
1232 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1233 e2->left = e1;
1234 e2->right = expr->base;
1235 e2->op = '=';
1236 e2->ctype = expr->base->ctype;
1238 if (expr->r_bitpos) {
1239 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1240 e3->op = '+';
1241 e3->left = e0;
1242 e3->right = alloc_const_expression(expr->pos,
1243 expr->r_bitpos >> 3);
1244 e3->ctype = &lazy_ptr_ctype;
1245 } else {
1246 e3 = e0;
1249 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1250 e4->left = e2;
1251 e4->right = e3;
1252 e4->ctype = &lazy_ptr_ctype;
1254 expr->unop = e4;
1255 expr->type = EXPR_PREOP;
1256 expr->op = '*';
1258 case SYM_FN:
1259 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1260 warning(expr->pos, "strange non-value function or array");
1261 return NULL;
1263 *expr = *expr->unop;
1264 ctype = create_pointer(expr, ctype, 1);
1265 expr->ctype = ctype;
1266 default:
1267 /* nothing */;
1269 return ctype;
1272 static struct symbol *evaluate_addressof(struct expression *expr)
1274 struct expression *op = expr->unop;
1275 struct symbol *ctype;
1277 if (op->op != '*' || op->type != EXPR_PREOP) {
1278 warning(expr->pos, "not addressable");
1279 return NULL;
1281 ctype = op->ctype;
1282 *expr = *op->unop;
1285 * symbol expression evaluation is lazy about the type
1286 * of the sub-expression, so we may have to generate
1287 * the type here if so..
1289 if (expr->ctype == &lazy_ptr_ctype) {
1290 ctype = create_pointer(expr, ctype, 0);
1291 expr->ctype = ctype;
1293 return expr->ctype;
1297 static struct symbol *evaluate_dereference(struct expression *expr)
1299 struct expression *op = expr->unop;
1300 struct symbol *ctype = op->ctype, *node, *target;
1302 /* Simplify: *&(expr) => (expr) */
1303 if (op->type == EXPR_PREOP && op->op == '&') {
1304 *expr = *op->unop;
1305 return expr->ctype;
1308 /* Dereferencing a node drops all the node information. */
1309 if (ctype->type == SYM_NODE)
1310 ctype = ctype->ctype.base_type;
1312 node = alloc_symbol(expr->pos, SYM_NODE);
1313 target = ctype->ctype.base_type;
1315 switch (ctype->type) {
1316 default:
1317 warning(expr->pos, "cannot derefence this type");
1318 return NULL;
1319 case SYM_PTR:
1320 merge_type(node, ctype);
1321 if (ctype->type != SYM_ARRAY)
1322 break;
1324 * Dereferencing a pointer to an array results in a
1325 * degenerate dereference: the expression becomes
1326 * just a pointer to the entry, and the derefence
1327 * goes away.
1329 *expr = *op;
1331 target = alloc_symbol(expr->pos, SYM_PTR);
1332 target->bit_size = bits_in_pointer;
1333 target->ctype.alignment = pointer_alignment;
1334 merge_type(target, ctype->ctype.base_type);
1335 break;
1337 case SYM_ARRAY:
1338 if (!lvalue_expression(op)) {
1339 warning(op->pos, "non-lvalue array??");
1340 return NULL;
1343 /* Do the implied "addressof" on the array */
1344 *op = *op->unop;
1347 * When an array is dereferenced, we need to pick
1348 * up the attributes of the original node too..
1350 merge_type(node, op->ctype);
1351 merge_type(node, ctype);
1352 break;
1355 node->bit_size = target->bit_size;
1356 node->array_size = target->array_size;
1358 expr->ctype = node;
1359 return node;
1363 * Unary post-ops: x++ and x--
1365 static struct symbol *evaluate_postop(struct expression *expr)
1367 struct expression *op = expr->unop;
1368 struct symbol *ctype = op->ctype;
1370 if (!lvalue_expression(expr->unop)) {
1371 warning(expr->pos, "need lvalue expression for ++/--");
1372 return NULL;
1374 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1375 warning(expr->pos, "bad operation on restricted");
1376 return NULL;
1379 evaluate_assign_to(op, ctype);
1381 expr->ctype = ctype;
1382 return ctype;
1385 static struct symbol *evaluate_sign(struct expression *expr)
1387 struct symbol *ctype = expr->unop->ctype;
1388 if (is_int_type(ctype)) {
1389 struct symbol *rtype = rtype = integer_promotion(ctype);
1390 if (rtype->bit_size != ctype->bit_size)
1391 expr->unop = cast_to(expr->unop, rtype);
1392 ctype = rtype;
1393 } else if (is_float_type(ctype) && expr->op != '~') {
1394 /* no conversions needed */
1395 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1396 /* no conversions needed */
1397 } else {
1398 return bad_expr_type(expr);
1400 if (expr->op == '+')
1401 *expr = *expr->unop;
1402 expr->ctype = ctype;
1403 return ctype;
1406 static struct symbol *evaluate_preop(struct expression *expr)
1408 struct symbol *ctype = expr->unop->ctype;
1410 switch (expr->op) {
1411 case '(':
1412 *expr = *expr->unop;
1413 return ctype;
1415 case '+':
1416 case '-':
1417 case '~':
1418 return evaluate_sign(expr);
1420 case '*':
1421 return evaluate_dereference(expr);
1423 case '&':
1424 return evaluate_addressof(expr);
1426 case SPECIAL_INCREMENT:
1427 case SPECIAL_DECREMENT:
1429 * From a type evaluation standpoint the pre-ops are
1430 * the same as the postops
1432 return evaluate_postop(expr);
1434 case '!':
1435 if (is_safe_type(ctype))
1436 warning(expr->pos, "testing a 'safe expression'");
1437 if (is_float_type(ctype)) {
1438 struct expression *arg = expr->unop;
1439 expr->type = EXPR_BINOP;
1440 expr->op = SPECIAL_EQUAL;
1441 expr->left = arg;
1442 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1443 expr->right->ctype = ctype;
1444 expr->right->fvalue = 0;
1446 ctype = &bool_ctype;
1447 break;
1449 default:
1450 break;
1452 expr->ctype = ctype;
1453 return &bool_ctype;
1456 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1458 struct ptr_list *head = (struct ptr_list *)_list;
1459 struct ptr_list *list = head;
1461 if (!head)
1462 return NULL;
1463 do {
1464 int i;
1465 for (i = 0; i < list->nr; i++) {
1466 struct symbol *sym = (struct symbol *) list->list[i];
1467 if (sym->ident) {
1468 if (sym->ident != ident)
1469 continue;
1470 *offset = sym->offset;
1471 return sym;
1472 } else {
1473 struct symbol *ctype = sym->ctype.base_type;
1474 struct symbol *sub;
1475 if (!ctype)
1476 continue;
1477 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1478 continue;
1479 sub = find_identifier(ident, ctype->symbol_list, offset);
1480 if (!sub)
1481 continue;
1482 *offset += sym->offset;
1483 return sub;
1486 } while ((list = list->next) != head);
1487 return NULL;
1490 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1492 struct expression *add;
1495 * Create a new add-expression
1497 * NOTE! Even if we just add zero, we need a new node
1498 * for the member pointer, since it has a different
1499 * type than the original pointer. We could make that
1500 * be just a cast, but the fact is, a node is a node,
1501 * so we might as well just do the "add zero" here.
1503 add = alloc_expression(expr->pos, EXPR_BINOP);
1504 add->op = '+';
1505 add->left = expr;
1506 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1507 add->right->ctype = &int_ctype;
1508 add->right->value = offset;
1511 * The ctype of the pointer will be lazily evaluated if
1512 * we ever take the address of this member dereference..
1514 add->ctype = &lazy_ptr_ctype;
1515 return add;
1518 /* structure/union dereference */
1519 static struct symbol *evaluate_member_dereference(struct expression *expr)
1521 int offset;
1522 struct symbol *ctype, *member;
1523 struct expression *deref = expr->deref, *add;
1524 struct ident *ident = expr->member;
1525 unsigned int mod;
1526 int address_space;
1528 if (!evaluate_expression(deref))
1529 return NULL;
1530 if (!ident) {
1531 warning(expr->pos, "bad member name");
1532 return NULL;
1535 ctype = deref->ctype;
1536 address_space = ctype->ctype.as;
1537 mod = ctype->ctype.modifiers;
1538 if (ctype->type == SYM_NODE) {
1539 ctype = ctype->ctype.base_type;
1540 address_space |= ctype->ctype.as;
1541 mod |= ctype->ctype.modifiers;
1543 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1544 warning(expr->pos, "expected structure or union");
1545 return NULL;
1547 offset = 0;
1548 member = find_identifier(ident, ctype->symbol_list, &offset);
1549 if (!member) {
1550 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1551 const char *name = "<unnamed>";
1552 int namelen = 9;
1553 if (ctype->ident) {
1554 name = ctype->ident->name;
1555 namelen = ctype->ident->len;
1557 warning(expr->pos, "no member '%s' in %s %.*s",
1558 show_ident(ident), type, namelen, name);
1559 return NULL;
1563 * The member needs to take on the address space and modifiers of
1564 * the "parent" type.
1566 member = convert_to_as_mod(member, address_space, mod);
1567 ctype = member->ctype.base_type;
1569 if (!lvalue_expression(deref)) {
1570 if (deref->type != EXPR_SLICE) {
1571 expr->base = deref;
1572 expr->r_bitpos = 0;
1573 } else {
1574 expr->base = deref->base;
1575 expr->r_bitpos = deref->r_bitpos;
1577 expr->r_bitpos += offset << 3;
1578 expr->type = EXPR_SLICE;
1579 if (ctype->type == SYM_BITFIELD) {
1580 expr->r_bitpos += member->bit_offset;
1581 expr->r_nrbits = member->fieldwidth;
1582 } else {
1583 expr->r_nrbits = member->bit_size;
1585 expr->ctype = member;
1586 return member;
1589 deref = deref->unop;
1590 expr->deref = deref;
1592 add = evaluate_offset(deref, offset);
1593 if (ctype->type == SYM_BITFIELD) {
1594 expr->type = EXPR_BITFIELD;
1595 expr->bitpos = member->bit_offset;
1596 expr->nrbits = member->fieldwidth;
1597 expr->address = add;
1598 } else {
1599 expr->type = EXPR_PREOP;
1600 expr->op = '*';
1601 expr->unop = add;
1604 expr->ctype = member;
1605 return member;
1608 static int is_promoted(struct expression *expr)
1610 while (1) {
1611 switch (expr->type) {
1612 case EXPR_BINOP:
1613 case EXPR_SELECT:
1614 case EXPR_CONDITIONAL:
1615 return 1;
1616 case EXPR_COMMA:
1617 expr = expr->right;
1618 continue;
1619 case EXPR_PREOP:
1620 switch (expr->op) {
1621 case '(':
1622 expr = expr->unop;
1623 continue;
1624 case '+':
1625 case '-':
1626 case '~':
1627 return 1;
1628 default:
1629 return 0;
1631 default:
1632 return 0;
1638 static struct symbol *evaluate_cast(struct expression *);
1640 static struct symbol *evaluate_sizeof(struct expression *expr)
1642 struct expression *what = expr->cast_expression;
1643 int size;
1645 if (expr->cast_type) {
1646 if (what) {
1647 struct symbol *sym = evaluate_cast(expr);
1648 size = sym->bit_size;
1649 } else {
1650 examine_symbol_type(expr->cast_type);
1651 size = expr->cast_type->bit_size;
1653 } else {
1654 if (!evaluate_expression(what))
1655 return NULL;
1656 size = what->ctype->bit_size;
1657 if (is_restricted_type(what->ctype)) {
1658 if (size < bits_in_int && is_promoted(what))
1659 size = bits_in_int;
1661 if (is_bitfield_type(what->ctype))
1662 warning(expr->pos, "sizeof applied to bitfield type");
1664 if (size & 7)
1665 warning(expr->pos, "cannot size expression");
1666 expr->type = EXPR_VALUE;
1667 expr->value = size >> 3;
1668 expr->ctype = size_t_ctype;
1669 return size_t_ctype;
1672 static struct symbol *evaluate_alignof(struct expression *expr)
1674 struct symbol *type = expr->cast_type;
1676 if (!type) {
1677 type = evaluate_expression(expr->cast_expression);
1678 if (!type)
1679 return NULL;
1681 if (is_bitfield_type(type))
1682 warning(expr->pos, "alignof applied to bitfield type");
1683 examine_symbol_type(type);
1684 expr->type = EXPR_VALUE;
1685 expr->value = type->ctype.alignment;
1686 expr->ctype = size_t_ctype;
1687 return size_t_ctype;
1690 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1692 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1693 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1694 return clash != 0;
1697 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1699 struct expression *expr;
1700 struct symbol_list *argument_types = fn->arguments;
1701 struct symbol *argtype;
1702 int i = 1;
1704 PREPARE_PTR_LIST(argument_types, argtype);
1705 FOR_EACH_PTR (head, expr) {
1706 struct expression **p = THIS_ADDRESS(expr);
1707 struct symbol *ctype, *target;
1708 ctype = evaluate_expression(expr);
1710 if (!ctype)
1711 return 0;
1713 if (context_clash(f, ctype))
1714 warning(expr->pos, "argument %d used in wrong context", i);
1716 ctype = degenerate(expr);
1718 target = argtype;
1719 if (!target && ctype->bit_size < bits_in_int)
1720 target = &int_ctype;
1721 if (target) {
1722 static char where[30];
1723 examine_symbol_type(target);
1724 sprintf(where, "argument %d", i);
1725 compatible_assignment_types(expr, target, p, ctype, where);
1728 i++;
1729 NEXT_PTR_LIST(argtype);
1730 } END_FOR_EACH_PTR(expr);
1731 FINISH_PTR_LIST(argtype);
1732 return 1;
1735 static int evaluate_initializer(struct symbol *ctype, struct expression **ep);
1737 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1739 struct expression *entry = *ep;
1740 struct expression **parent, *reuse = NULL;
1741 unsigned long offset;
1742 struct symbol *sym;
1743 unsigned long from, to;
1744 int accept_string = is_byte_type(ctype);
1746 from = current;
1747 to = from+1;
1748 parent = ep;
1749 if (entry->type == EXPR_INDEX) {
1750 from = entry->idx_from;
1751 to = entry->idx_to;
1752 parent = &entry->idx_expression;
1753 reuse = entry;
1754 entry = entry->idx_expression;
1757 offset = from * (ctype->bit_size>>3);
1758 if (offset) {
1759 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1760 reuse->type = EXPR_POS;
1761 reuse->init_offset = offset;
1762 reuse->init_nr = to - from;
1763 reuse->init_expr = entry;
1764 parent = &reuse->init_expr;
1765 entry = reuse;
1767 *ep = entry;
1769 if (accept_string && entry->type == EXPR_STRING) {
1770 sym = evaluate_expression(entry);
1771 to = from + get_expression_value(sym->array_size);
1772 } else {
1773 evaluate_initializer(ctype, parent);
1775 return to;
1778 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1780 struct expression *entry;
1781 int current = 0;
1782 int max = 0;
1784 FOR_EACH_PTR(expr->expr_list, entry) {
1785 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1786 if (current > max)
1787 max = current;
1788 } END_FOR_EACH_PTR(entry);
1789 return max;
1792 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1793 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1795 if (expression_list_size(expr->expr_list) != 1) {
1796 warning(expr->pos, "unexpected compound initializer");
1797 return 0;
1799 return evaluate_array_initializer(ctype, expr);
1802 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1804 struct symbol *sym;
1806 FOR_EACH_PTR(ctype->symbol_list, sym) {
1807 if (sym->ident == ident)
1808 return sym;
1809 } END_FOR_EACH_PTR(sym);
1810 return NULL;
1813 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1815 struct expression *entry = *ep;
1816 struct expression **parent;
1817 struct expression *reuse = NULL;
1818 unsigned long offset;
1820 if (!sym) {
1821 error(entry->pos, "unknown named initializer");
1822 return -1;
1825 if (entry->type == EXPR_IDENTIFIER) {
1826 reuse = entry;
1827 entry = entry->ident_expression;
1830 parent = ep;
1831 offset = sym->offset;
1832 if (offset) {
1833 if (!reuse)
1834 reuse = alloc_expression(entry->pos, EXPR_POS);
1835 reuse->type = EXPR_POS;
1836 reuse->init_offset = offset;
1837 reuse->init_nr = 1;
1838 reuse->init_expr = entry;
1839 parent = &reuse->init_expr;
1840 entry = reuse;
1842 *ep = entry;
1843 evaluate_initializer(sym, parent);
1844 return 0;
1847 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1849 struct expression *entry;
1850 struct symbol *sym;
1852 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1853 FOR_EACH_PTR(expr->expr_list, entry) {
1854 if (entry->type == EXPR_IDENTIFIER) {
1855 struct ident *ident = entry->expr_ident;
1856 /* We special-case the "already right place" case */
1857 if (!sym || sym->ident != ident) {
1858 RESET_PTR_LIST(sym);
1859 for (;;) {
1860 if (!sym)
1861 break;
1862 if (sym->ident == ident)
1863 break;
1864 NEXT_PTR_LIST(sym);
1868 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1869 return 0;
1870 NEXT_PTR_LIST(sym);
1871 } END_FOR_EACH_PTR(entry);
1872 FINISH_PTR_LIST(sym);
1874 return 0;
1878 * Initializers are kind of like assignments. Except
1879 * they can be a hell of a lot more complex.
1881 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1883 struct expression *expr = *ep;
1886 * Simple non-structure/array initializers are the simple
1887 * case, and look (and parse) largely like assignments.
1889 switch (expr->type) {
1890 default: {
1891 int size = 0, is_string = expr->type == EXPR_STRING;
1892 struct symbol *rtype = evaluate_expression(expr);
1893 if (rtype) {
1895 * Special case:
1896 * char array[] = "string"
1897 * should _not_ degenerate.
1899 if (is_string && is_string_type(ctype)) {
1900 struct expression *array_size = ctype->array_size;
1901 if (!array_size)
1902 array_size = ctype->array_size = rtype->array_size;
1903 size = get_expression_value(array_size);
1904 } else {
1905 rtype = degenerate(expr);
1906 size = 1;
1908 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1910 return size;
1913 case EXPR_INITIALIZER:
1914 expr->ctype = ctype;
1915 if (ctype->type == SYM_NODE)
1916 ctype = ctype->ctype.base_type;
1918 switch (ctype->type) {
1919 case SYM_ARRAY:
1920 case SYM_PTR:
1921 return evaluate_array_initializer(ctype->ctype.base_type, expr);
1922 case SYM_UNION:
1923 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1924 case SYM_STRUCT:
1925 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1926 default:
1927 return evaluate_scalar_initializer(ctype, expr);
1930 case EXPR_IDENTIFIER:
1931 if (ctype->type == SYM_NODE)
1932 ctype = ctype->ctype.base_type;
1933 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1934 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1935 show_symbol(ctype);
1936 return 0;
1938 return evaluate_one_struct_initializer(ctype, ep,
1939 find_struct_ident(ctype, expr->expr_ident));
1941 case EXPR_INDEX:
1942 if (ctype->type == SYM_NODE)
1943 ctype = ctype->ctype.base_type;
1944 if (ctype->type != SYM_ARRAY) {
1945 error(expr->pos, "expected array");
1946 return 0;
1948 return evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1952 static int get_as(struct symbol *sym)
1954 int as;
1955 unsigned long mod;
1957 if (!sym)
1958 return 0;
1959 as = sym->ctype.as;
1960 mod = sym->ctype.modifiers;
1961 if (sym->type == SYM_NODE) {
1962 sym = sym->ctype.base_type;
1963 as |= sym->ctype.as;
1964 mod |= sym->ctype.modifiers;
1968 * At least for now, allow casting to a "unsigned long".
1969 * That's how we do things like pointer arithmetic and
1970 * store pointers to registers.
1972 if (sym == &ulong_ctype)
1973 return -1;
1975 if (sym && sym->type == SYM_PTR) {
1976 sym = sym->ctype.base_type;
1977 as |= sym->ctype.as;
1978 mod |= sym->ctype.modifiers;
1980 if (mod & MOD_FORCE)
1981 return -1;
1982 return as;
1985 static struct symbol *evaluate_cast(struct expression *expr)
1987 struct expression *target = expr->cast_expression;
1988 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1989 enum type type;
1991 expr->ctype = ctype;
1992 expr->cast_type = ctype;
1995 * Special case: a cast can be followed by an
1996 * initializer, in which case we need to pass
1997 * the type value down to that initializer rather
1998 * than trying to evaluate it as an expression
2000 * A more complex case is when the initializer is
2001 * dereferenced as part of a post-fix expression.
2002 * We need to produce an expression that can be dereferenced.
2004 if (target->type == EXPR_INITIALIZER) {
2005 struct symbol *sym = expr->cast_type;
2006 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2008 sym->initializer = expr->cast_expression;
2009 evaluate_symbol(sym);
2011 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2012 addr->symbol = sym;
2014 expr->type = EXPR_PREOP;
2015 expr->op = '*';
2016 expr->unop = addr;
2017 expr->ctype = sym;
2019 return sym;
2022 evaluate_expression(target);
2023 degenerate(target);
2026 * You can always throw a value away by casting to
2027 * "void" - that's an implicit "force". Note that
2028 * the same is _not_ true of "void *".
2030 if (ctype == &void_ctype)
2031 goto out;
2033 type = ctype->type;
2034 if (type == SYM_NODE) {
2035 type = ctype->ctype.base_type->type;
2036 if (ctype->ctype.base_type == &void_ctype)
2037 goto out;
2039 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2040 warning(expr->pos, "cast to non-scalar");
2042 if (!target->ctype) {
2043 warning(expr->pos, "cast from unknown type");
2044 goto out;
2047 type = target->ctype->type;
2048 if (type == SYM_NODE)
2049 type = target->ctype->ctype.base_type->type;
2050 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2051 warning(expr->pos, "cast from non-scalar");
2053 if (!get_as(ctype) && get_as(target->ctype) > 0)
2054 warning(expr->pos, "cast removes address space of expression");
2056 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2057 struct symbol *t1 = ctype, *t2 = target->ctype;
2058 if (t1->type == SYM_NODE)
2059 t1 = t1->ctype.base_type;
2060 if (t2->type == SYM_NODE)
2061 t2 = t2->ctype.base_type;
2062 if (t1 != t2) {
2063 if (t1->type == SYM_RESTRICT)
2064 warning(expr->pos, "cast to restricted type");
2065 if (t2->type == SYM_RESTRICT)
2066 warning(expr->pos, "cast from restricted type");
2071 * Casts of constant values are special: they
2072 * can be NULL, and thus need to be simplified
2073 * early.
2075 if (target->type == EXPR_VALUE)
2076 cast_value(expr, ctype, target, target->ctype);
2078 out:
2079 return ctype;
2083 * Evaluate a call expression with a symbol. This
2084 * should expand inline functions, and evaluate
2085 * builtins.
2087 static int evaluate_symbol_call(struct expression *expr)
2089 struct expression *fn = expr->fn;
2090 struct symbol *ctype = fn->ctype;
2092 if (fn->type != EXPR_PREOP)
2093 return 0;
2095 if (ctype->op && ctype->op->evaluate)
2096 return ctype->op->evaluate(expr);
2098 if (ctype->ctype.modifiers & MOD_INLINE) {
2099 int ret;
2100 struct symbol *curr = current_fn;
2101 unsigned long context = current_context;
2102 unsigned long mask = current_contextmask;
2104 current_context |= ctype->ctype.context;
2105 current_contextmask |= ctype->ctype.contextmask;
2106 current_fn = ctype->ctype.base_type;
2107 examine_fn_arguments(current_fn);
2109 ret = inline_function(expr, ctype);
2111 /* restore the old function context */
2112 current_fn = curr;
2113 current_context = context;
2114 current_contextmask = mask;
2115 return ret;
2118 return 0;
2121 static struct symbol *evaluate_call(struct expression *expr)
2123 int args, fnargs;
2124 struct symbol *ctype, *sym;
2125 struct expression *fn = expr->fn;
2126 struct expression_list *arglist = expr->args;
2128 if (!evaluate_expression(fn))
2129 return NULL;
2130 sym = ctype = fn->ctype;
2131 if (ctype->type == SYM_NODE)
2132 ctype = ctype->ctype.base_type;
2133 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2134 ctype = ctype->ctype.base_type;
2135 if (!evaluate_arguments(sym, ctype, arglist))
2136 return NULL;
2137 if (ctype->type != SYM_FN) {
2138 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2139 return NULL;
2141 args = expression_list_size(expr->args);
2142 fnargs = symbol_list_size(ctype->arguments);
2143 if (args < fnargs)
2144 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2145 if (args > fnargs && !ctype->variadic)
2146 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2147 if (sym->type == SYM_NODE) {
2148 if (evaluate_symbol_call(expr))
2149 return expr->ctype;
2151 expr->ctype = ctype->ctype.base_type;
2152 return expr->ctype;
2155 struct symbol *evaluate_expression(struct expression *expr)
2157 if (!expr)
2158 return NULL;
2159 if (expr->ctype)
2160 return expr->ctype;
2162 switch (expr->type) {
2163 case EXPR_VALUE:
2164 case EXPR_FVALUE:
2165 warning(expr->pos, "value expression without a type");
2166 return NULL;
2167 case EXPR_STRING:
2168 return evaluate_string(expr);
2169 case EXPR_SYMBOL:
2170 return evaluate_symbol_expression(expr);
2171 case EXPR_BINOP:
2172 if (!evaluate_expression(expr->left))
2173 return NULL;
2174 if (!evaluate_expression(expr->right))
2175 return NULL;
2176 return evaluate_binop(expr);
2177 case EXPR_LOGICAL:
2178 return evaluate_logical(expr);
2179 case EXPR_COMMA:
2180 evaluate_expression(expr->left);
2181 if (!evaluate_expression(expr->right))
2182 return NULL;
2183 return evaluate_comma(expr);
2184 case EXPR_COMPARE:
2185 if (!evaluate_expression(expr->left))
2186 return NULL;
2187 if (!evaluate_expression(expr->right))
2188 return NULL;
2189 return evaluate_compare(expr);
2190 case EXPR_ASSIGNMENT:
2191 if (!evaluate_expression(expr->left))
2192 return NULL;
2193 if (!evaluate_expression(expr->right))
2194 return NULL;
2195 return evaluate_assignment(expr);
2196 case EXPR_PREOP:
2197 if (!evaluate_expression(expr->unop))
2198 return NULL;
2199 return evaluate_preop(expr);
2200 case EXPR_POSTOP:
2201 if (!evaluate_expression(expr->unop))
2202 return NULL;
2203 return evaluate_postop(expr);
2204 case EXPR_CAST:
2205 return evaluate_cast(expr);
2206 case EXPR_SIZEOF:
2207 return evaluate_sizeof(expr);
2208 case EXPR_ALIGNOF:
2209 return evaluate_alignof(expr);
2210 case EXPR_DEREF:
2211 return evaluate_member_dereference(expr);
2212 case EXPR_CALL:
2213 return evaluate_call(expr);
2214 case EXPR_BITFIELD:
2215 warning(expr->pos, "bitfield generated by parser");
2216 return NULL;
2217 case EXPR_SELECT:
2218 case EXPR_CONDITIONAL:
2219 if (!evaluate_conditional(&expr->conditional))
2220 return NULL;
2221 if (!evaluate_expression(expr->cond_false))
2222 return NULL;
2223 if (expr->cond_true && !evaluate_expression(expr->cond_true))
2224 return NULL;
2225 return evaluate_conditional_expression(expr);
2226 case EXPR_STATEMENT:
2227 expr->ctype = evaluate_statement(expr->statement);
2228 return expr->ctype;
2230 case EXPR_LABEL:
2231 expr->ctype = &ptr_ctype;
2232 return &ptr_ctype;
2234 case EXPR_TYPE:
2235 /* Evaluate the type of the symbol .. */
2236 evaluate_symbol(expr->symbol);
2237 /* .. but the type of the _expression_ is a "type" */
2238 expr->ctype = &type_ctype;
2239 return &type_ctype;
2241 /* These can not exist as stand-alone expressions */
2242 case EXPR_INITIALIZER:
2243 case EXPR_IDENTIFIER:
2244 case EXPR_INDEX:
2245 case EXPR_POS:
2246 warning(expr->pos, "internal front-end error: initializer in expression");
2247 return NULL;
2248 case EXPR_SLICE:
2249 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2250 return NULL;
2252 return NULL;
2255 void check_duplicates(struct symbol *sym)
2257 struct symbol *next = sym;
2259 while ((next = next->same_symbol) != NULL) {
2260 const char *typediff;
2261 evaluate_symbol(next);
2262 typediff = type_difference(sym, next, 0, 0);
2263 if (typediff) {
2264 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2265 show_ident(sym->ident),
2266 input_streams[next->pos.stream].name, next->pos.line, typediff);
2267 return;
2272 struct symbol *evaluate_symbol(struct symbol *sym)
2274 struct symbol *base_type;
2276 if (!sym)
2277 return sym;
2279 sym = examine_symbol_type(sym);
2280 base_type = sym->ctype.base_type;
2281 if (!base_type)
2282 return NULL;
2284 /* Evaluate the initializers */
2285 if (sym->initializer) {
2286 int count = evaluate_initializer(sym, &sym->initializer);
2287 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
2288 int bit_size = count * base_type->ctype.base_type->bit_size;
2289 base_type->array_size = alloc_const_expression(sym->pos, count);
2290 base_type->bit_size = bit_size;
2291 sym->array_size = base_type->array_size;
2292 sym->bit_size = bit_size;
2296 /* And finally, evaluate the body of the symbol too */
2297 if (base_type->type == SYM_FN) {
2298 struct symbol *curr = current_fn;
2299 unsigned long context = current_context;
2300 unsigned long mask = current_contextmask;
2302 current_fn = base_type;
2303 current_contextmask = sym->ctype.contextmask;
2304 current_context = sym->ctype.context;
2306 examine_fn_arguments(base_type);
2307 if (!base_type->stmt && base_type->inline_stmt)
2308 uninline(sym);
2309 if (base_type->stmt)
2310 evaluate_statement(base_type->stmt);
2312 current_fn = curr;
2313 current_contextmask = mask;
2314 current_context = context;
2317 return base_type;
2320 struct symbol *evaluate_return_expression(struct statement *stmt)
2322 struct expression *expr = stmt->expression;
2323 struct symbol *ctype, *fntype;
2325 evaluate_expression(expr);
2326 ctype = degenerate(expr);
2327 fntype = current_fn->ctype.base_type;
2328 if (!fntype || fntype == &void_ctype) {
2329 if (expr && ctype != &void_ctype)
2330 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2331 return NULL;
2334 if (!expr) {
2335 warning(stmt->pos, "return with no return value");
2336 return NULL;
2338 if (!ctype)
2339 return NULL;
2340 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2341 return NULL;
2344 static void evaluate_if_statement(struct statement *stmt)
2346 if (!stmt->if_conditional)
2347 return;
2349 evaluate_conditional(&stmt->if_conditional);
2350 evaluate_statement(stmt->if_true);
2351 evaluate_statement(stmt->if_false);
2354 static void evaluate_iterator(struct statement *stmt)
2356 struct expression **pre = &stmt->iterator_pre_condition;
2357 struct expression **post = &stmt->iterator_post_condition;
2358 if (*pre == *post) {
2359 evaluate_conditional(pre);
2360 *post = *pre;
2361 } else {
2362 evaluate_conditional(pre);
2363 evaluate_conditional(post);
2365 evaluate_statement(stmt->iterator_pre_statement);
2366 evaluate_statement(stmt->iterator_statement);
2367 evaluate_statement(stmt->iterator_post_statement);
2370 struct symbol *evaluate_statement(struct statement *stmt)
2372 if (!stmt)
2373 return NULL;
2375 switch (stmt->type) {
2376 case STMT_RETURN:
2377 return evaluate_return_expression(stmt);
2379 case STMT_EXPRESSION:
2380 if (!evaluate_expression(stmt->expression))
2381 return NULL;
2382 return degenerate(stmt->expression);
2384 case STMT_COMPOUND: {
2385 struct statement *s;
2386 struct symbol *type = NULL;
2387 struct symbol *sym;
2389 /* Evaluate each symbol in the compound statement */
2390 FOR_EACH_PTR(stmt->syms, sym) {
2391 evaluate_symbol(sym);
2392 } END_FOR_EACH_PTR(sym);
2393 evaluate_symbol(stmt->ret);
2396 * Then, evaluate each statement, making the type of the
2397 * compound statement be the type of the last statement
2399 type = NULL;
2400 FOR_EACH_PTR(stmt->stmts, s) {
2401 type = evaluate_statement(s);
2402 } END_FOR_EACH_PTR(s);
2403 if (!type)
2404 type = &void_ctype;
2405 return type;
2407 case STMT_IF:
2408 evaluate_if_statement(stmt);
2409 return NULL;
2410 case STMT_ITERATOR:
2411 evaluate_iterator(stmt);
2412 return NULL;
2413 case STMT_SWITCH:
2414 evaluate_expression(stmt->switch_expression);
2415 evaluate_statement(stmt->switch_statement);
2416 return NULL;
2417 case STMT_CASE:
2418 evaluate_expression(stmt->case_expression);
2419 evaluate_expression(stmt->case_to);
2420 evaluate_statement(stmt->case_statement);
2421 return NULL;
2422 case STMT_LABEL:
2423 return evaluate_statement(stmt->label_statement);
2424 case STMT_GOTO:
2425 evaluate_expression(stmt->goto_expression);
2426 return NULL;
2427 case STMT_NONE:
2428 break;
2429 case STMT_ASM:
2430 /* FIXME! Do the asm parameter evaluation! */
2431 break;
2433 return NULL;