Move constant switch simplification to the instruction
[smatch.git] / evaluate.c
blob2c60db4191253e2e19bba59784bb4fad0d1b2b37
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 "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 struct symbol *current_fn;
30 static struct symbol *degenerate(struct expression *expr);
31 static struct symbol *evaluate_symbol(struct symbol *sym);
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 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
40 return NULL;
43 examine_symbol_type(sym);
45 base_type = sym->ctype.base_type;
46 if (!base_type) {
47 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
48 return NULL;
51 /* The type of a symbol is the symbol itself! */
52 expr->ctype = sym;
54 /* enums can be turned into plain values */
55 if (sym->type != SYM_ENUM) {
56 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
57 addr->symbol = sym;
58 addr->symbol_name = expr->symbol_name;
59 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
60 expr->type = EXPR_PREOP;
61 expr->op = '*';
62 expr->unop = addr;
63 return sym;
64 } else if (base_type->bit_size < bits_in_int) {
65 /* ugly - we need to force sizeof for these guys */
66 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
67 e->value = sym->value;
68 e->ctype = base_type;
69 expr->type = EXPR_PREOP;
70 expr->op = '+';
71 expr->unop = e;
72 } else {
73 expr->type = EXPR_VALUE;
74 expr->value = sym->value;
76 expr->ctype = base_type;
77 return base_type;
80 static struct symbol *evaluate_string(struct expression *expr)
82 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
83 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
84 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
85 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
86 unsigned int length = expr->string->length;
88 sym->array_size = alloc_const_expression(expr->pos, length);
89 sym->bit_size = bits_in_char * length;
90 sym->ctype.alignment = 1;
91 sym->ctype.modifiers = MOD_STATIC;
92 sym->ctype.base_type = array;
93 sym->initializer = initstr;
95 initstr->ctype = sym;
96 initstr->string = expr->string;
98 array->array_size = sym->array_size;
99 array->bit_size = bits_in_char * length;
100 array->ctype.alignment = 1;
101 array->ctype.modifiers = MOD_STATIC;
102 array->ctype.base_type = &char_ctype;
104 addr->symbol = sym;
105 addr->ctype = &lazy_ptr_ctype;
107 expr->type = EXPR_PREOP;
108 expr->op = '*';
109 expr->unop = addr;
110 expr->ctype = sym;
111 return sym;
114 static inline struct symbol *integer_promotion(struct symbol *type)
116 unsigned long mod = type->ctype.modifiers;
117 int width;
119 if (type->type == SYM_NODE)
120 type = type->ctype.base_type;
121 if (type->type == SYM_ENUM)
122 type = type->ctype.base_type;
123 width = type->bit_size;
124 if (type->type == SYM_BITFIELD)
125 type = type->ctype.base_type;
126 mod = type->ctype.modifiers;
127 if (width < bits_in_int)
128 return &int_ctype;
130 /* If char/short has as many bits as int, it still gets "promoted" */
131 if (mod & (MOD_CHAR | MOD_SHORT)) {
132 type = &int_ctype;
133 if (mod & MOD_UNSIGNED)
134 type = &uint_ctype;
136 return type;
140 * integer part of usual arithmetic conversions:
141 * integer promotions are applied
142 * if left and right are identical, we are done
143 * if signedness is the same, convert one with lower rank
144 * unless unsigned argument has rank lower than signed one, convert the
145 * signed one.
146 * if signed argument is bigger than unsigned one, convert the unsigned.
147 * otherwise, convert signed.
149 * Leaving aside the integer promotions, that is equivalent to
150 * if identical, don't convert
151 * if left is bigger than right, convert right
152 * if right is bigger than left, convert right
153 * otherwise, if signedness is the same, convert one with lower rank
154 * otherwise convert the signed one.
156 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
158 unsigned long lmod, rmod;
160 left = integer_promotion(left);
161 right = integer_promotion(right);
163 if (left == right)
164 goto left;
166 if (left->bit_size > right->bit_size)
167 goto left;
169 if (right->bit_size > left->bit_size)
170 goto right;
172 lmod = left->ctype.modifiers;
173 rmod = right->ctype.modifiers;
174 if ((lmod ^ rmod) & MOD_UNSIGNED) {
175 if (lmod & MOD_UNSIGNED)
176 goto left;
177 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
178 goto left;
179 right:
180 left = right;
181 left:
182 return left;
185 static int same_cast_type(struct symbol *orig, struct symbol *new)
187 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
191 * This gets called for implicit casts in assignments and
192 * integer promotion. We often want to try to move the
193 * cast down, because the ops involved may have been
194 * implicitly cast up, and we can get rid of the casts
195 * early.
197 static struct expression * cast_to(struct expression *old, struct symbol *type)
199 struct expression *expr;
202 * See if we can simplify the op. Move the cast down.
204 switch (old->type) {
205 case EXPR_PREOP:
206 if (old->op == '~') {
207 old->ctype = type;
208 old->unop = cast_to(old->unop, type);
209 return old;
211 break;
213 case EXPR_IMPLIED_CAST:
214 if (old->ctype->bit_size >= type->bit_size) {
215 struct expression *orig = old->cast_expression;
216 if (same_cast_type(orig->ctype, type))
217 return orig;
218 if (old->ctype->bit_offset == type->bit_offset) {
219 old->ctype = type;
220 old->cast_type = type;
221 return old;
224 break;
226 default:
227 /* nothing */;
230 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
231 expr->ctype = type;
232 expr->cast_type = type;
233 expr->cast_expression = old;
234 return expr;
237 static int is_type_type(struct symbol *type)
239 return (type->ctype.modifiers & MOD_TYPE) != 0;
242 static int is_ptr_type(struct symbol *type)
244 if (type->type == SYM_NODE)
245 type = type->ctype.base_type;
246 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
249 static inline int is_float_type(struct symbol *type)
251 if (type->type == SYM_NODE)
252 type = type->ctype.base_type;
253 return type->ctype.base_type == &fp_type;
256 static inline int is_byte_type(struct symbol *type)
258 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
261 static inline int is_string_type(struct symbol *type)
263 if (type->type == SYM_NODE)
264 type = type->ctype.base_type;
265 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
268 static struct symbol *bad_expr_type(struct expression *expr)
270 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
271 switch (expr->type) {
272 case EXPR_BINOP:
273 case EXPR_COMPARE:
274 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
275 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
276 break;
277 case EXPR_PREOP:
278 case EXPR_POSTOP:
279 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
280 break;
281 default:
282 break;
285 return NULL;
288 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
290 struct expression *left = *lp, *right = *rp;
291 struct symbol *ltype = left->ctype, *rtype = right->ctype;
293 if (ltype->type == SYM_NODE)
294 ltype = ltype->ctype.base_type;
295 if (rtype->type == SYM_NODE)
296 rtype = rtype->ctype.base_type;
297 if (is_float_type(ltype)) {
298 if (is_int_type(rtype))
299 goto Left;
300 if (is_float_type(rtype)) {
301 unsigned long lmod = ltype->ctype.modifiers;
302 unsigned long rmod = rtype->ctype.modifiers;
303 lmod &= MOD_LONG | MOD_LONGLONG;
304 rmod &= MOD_LONG | MOD_LONGLONG;
305 if (lmod == rmod)
306 return ltype;
307 if (lmod & ~rmod)
308 goto Left;
309 else
310 goto Right;
312 return NULL;
314 if (!is_float_type(rtype) || !is_int_type(ltype))
315 return NULL;
316 Right:
317 *lp = cast_to(left, rtype);
318 return rtype;
319 Left:
320 *rp = cast_to(right, ltype);
321 return ltype;
324 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
326 struct expression *left = *lp, *right = *rp;
327 struct symbol *ltype = left->ctype, *rtype = right->ctype;
329 if (ltype->type == SYM_NODE)
330 ltype = ltype->ctype.base_type;
331 if (rtype->type == SYM_NODE)
332 rtype = rtype->ctype.base_type;
333 if (is_int_type(ltype) && is_int_type(rtype)) {
334 struct symbol *ctype = bigger_int_type(ltype, rtype);
336 /* Don't bother promoting same-size entities, it only adds clutter */
337 if (ltype->bit_size != ctype->bit_size)
338 *lp = cast_to(left, ctype);
339 if (rtype->bit_size != ctype->bit_size)
340 *rp = cast_to(right, ctype);
341 return ctype;
343 return NULL;
346 static int restricted_value(struct expression *v, struct symbol *type)
348 if (v->type != EXPR_VALUE)
349 return 1;
350 if (v->value != 0)
351 return 1;
352 return 0;
355 static int restricted_binop(int op, struct symbol *type)
357 switch (op) {
358 case '&':
359 case '|':
360 case '^':
361 case '?':
362 case '=':
363 case SPECIAL_EQUAL:
364 case SPECIAL_NOTEQUAL:
365 case SPECIAL_AND_ASSIGN:
366 case SPECIAL_OR_ASSIGN:
367 case SPECIAL_XOR_ASSIGN:
368 return 0;
369 default:
370 return 1;
374 static int restricted_unop(int op, struct symbol *type)
376 if (op == '~' && type->bit_size >= bits_in_int)
377 return 0;
378 if (op == '+')
379 return 0;
380 return 1;
383 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
385 struct expression *left = *lp, *right = *rp;
386 struct symbol *ltype = left->ctype, *rtype = right->ctype;
387 struct symbol *type = NULL;
389 if (ltype->type == SYM_NODE)
390 ltype = ltype->ctype.base_type;
391 if (ltype->type == SYM_ENUM)
392 ltype = ltype->ctype.base_type;
393 if (rtype->type == SYM_NODE)
394 rtype = rtype->ctype.base_type;
395 if (rtype->type == SYM_ENUM)
396 rtype = rtype->ctype.base_type;
397 if (is_restricted_type(ltype)) {
398 if (is_restricted_type(rtype)) {
399 if (ltype == rtype)
400 type = ltype;
401 } else {
402 if (!restricted_value(right, ltype))
403 type = ltype;
405 } else if (is_restricted_type(rtype)) {
406 if (!restricted_value(left, rtype))
407 type = rtype;
409 if (!type)
410 return NULL;
411 if (restricted_binop(op, type))
412 return NULL;
413 return type;
416 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
418 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
419 if (!ctype && float_ok)
420 ctype = compatible_float_binop(&expr->left, &expr->right);
421 if (!ctype)
422 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
423 if (ctype) {
424 expr->ctype = ctype;
425 return ctype;
427 return bad_expr_type(expr);
430 static inline int lvalue_expression(struct expression *expr)
432 return expr->type == EXPR_PREOP && expr->op == '*';
435 static int ptr_object_size(struct symbol *ptr_type)
437 if (ptr_type->type == SYM_NODE)
438 ptr_type = ptr_type->ctype.base_type;
439 if (ptr_type->type == SYM_PTR)
440 ptr_type = ptr_type->ctype.base_type;
441 return ptr_type->bit_size;
444 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
446 struct expression *i = *ip;
447 struct symbol *ptr_type = ctype;
448 int bit_size;
450 if (ptr_type->type == SYM_NODE)
451 ptr_type = ptr_type->ctype.base_type;
453 if (!is_int_type(i->ctype))
454 return bad_expr_type(expr);
456 examine_symbol_type(ctype);
458 if (!ctype->ctype.base_type) {
459 warning(expr->pos, "missing type information");
460 return NULL;
463 /* Get the size of whatever the pointer points to */
464 bit_size = ptr_object_size(ctype);
466 if (i->type == EXPR_VALUE) {
467 i->value *= bit_size >> 3;
468 } else if (bit_size > bits_in_char) {
469 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
470 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
472 val->ctype = size_t_ctype;
473 val->value = bit_size >> 3;
475 mul->op = '*';
476 mul->ctype = size_t_ctype;
477 mul->left = i;
478 mul->right = val;
480 *ip = mul;
483 expr->ctype = ctype;
484 return ctype;
487 static struct symbol *evaluate_add(struct expression *expr)
489 struct expression *left = expr->left, *right = expr->right;
490 struct symbol *ltype = left->ctype, *rtype = right->ctype;
492 if (is_ptr_type(ltype))
493 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
495 if (is_ptr_type(rtype))
496 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
498 return evaluate_arith(expr, 1);
501 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
502 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
503 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
505 const char * type_difference(struct symbol *target, struct symbol *source,
506 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
508 for (;;) {
509 unsigned long mod1, mod2, diff;
510 unsigned long as1, as2;
511 int type1, type2;
512 struct symbol *base1, *base2;
514 if (target == source)
515 break;
516 if (!target || !source)
517 return "different types";
519 * Peel of per-node information.
520 * FIXME! Check alignment and context too here!
522 mod1 = target->ctype.modifiers;
523 as1 = target->ctype.as;
524 mod2 = source->ctype.modifiers;
525 as2 = source->ctype.as;
526 if (target->type == SYM_NODE) {
527 target = target->ctype.base_type;
528 if (!target)
529 return "bad types";
530 if (target->type == SYM_PTR) {
531 mod1 = 0;
532 as1 = 0;
534 mod1 |= target->ctype.modifiers;
535 as1 |= target->ctype.as;
537 if (source->type == SYM_NODE) {
538 source = source->ctype.base_type;
539 if (!source)
540 return "bad types";
541 if (source->type == SYM_PTR) {
542 mod2 = 0;
543 as2 = 0;
545 mod2 |= source->ctype.modifiers;
546 as2 |= source->ctype.as;
548 if (target->type == SYM_ENUM) {
549 target = target->ctype.base_type;
550 if (!target)
551 return "bad types";
553 if (source->type == SYM_ENUM) {
554 source = source->ctype.base_type;
555 if (!source)
556 return "bad types";
559 if (target == source)
560 break;
561 if (!target || !source)
562 return "different types";
564 type1 = target->type;
565 base1 = target->ctype.base_type;
567 type2 = source->type;
568 base2 = source->ctype.base_type;
571 * Pointers to functions compare as the function itself
573 if (type1 == SYM_PTR && base1) {
574 switch (base1->type) {
575 case SYM_FN:
576 type1 = SYM_FN;
577 target = base1;
578 base1 = base1->ctype.base_type;
579 default:
580 /* nothing */;
583 if (type2 == SYM_PTR && base2) {
584 switch (base2->type) {
585 case SYM_FN:
586 type2 = SYM_FN;
587 source = base2;
588 base2 = base2->ctype.base_type;
589 default:
590 /* nothing */;
594 /* Arrays degenerate to pointers for type comparisons */
595 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
596 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
598 if (type1 != type2 || type1 == SYM_RESTRICT)
599 return "different base types";
601 /* Must be same address space to be comparable */
602 if (as1 != as2)
603 return "different address spaces";
605 /* Ignore differences in storage types or addressability */
606 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
607 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
608 if (diff) {
609 if (diff & MOD_SIZE)
610 return "different type sizes";
611 if (diff & ~MOD_SIGNEDNESS)
612 return "different modifiers";
614 /* Differs in signedness only.. */
615 if (Wtypesign) {
617 * Warn if both are explicitly signed ("unsigned" is obvously
618 * always explicit, and since we know one of them has to be
619 * unsigned, we check if the signed one was explicit).
621 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
622 return "different explicit signedness";
625 * "char" matches both "unsigned char" and "signed char",
626 * so if the explicit test didn't trigger, then we should
627 * not warn about a char.
629 if (!(mod1 & MOD_CHAR))
630 return "different signedness";
634 if (type1 == SYM_FN) {
635 int i;
636 struct symbol *arg1, *arg2;
637 if (base1->variadic != base2->variadic)
638 return "incompatible variadic arguments";
639 PREPARE_PTR_LIST(target->arguments, arg1);
640 PREPARE_PTR_LIST(source->arguments, arg2);
641 i = 1;
642 for (;;) {
643 const char *diff;
644 diff = type_difference(arg1, arg2, 0, 0);
645 if (diff) {
646 static char argdiff[80];
647 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
648 return argdiff;
650 if (!arg1)
651 break;
652 NEXT_PTR_LIST(arg1);
653 NEXT_PTR_LIST(arg2);
654 i++;
656 FINISH_PTR_LIST(arg2);
657 FINISH_PTR_LIST(arg1);
660 target = base1;
661 source = base2;
663 return NULL;
666 static int is_null_ptr(struct expression *expr)
668 if (expr->type != EXPR_VALUE || expr->value)
669 return 0;
670 if (!is_ptr_type(expr->ctype))
671 warning(expr->pos, "Using plain integer as NULL pointer");
672 return 1;
675 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
677 /* NULL expression? Just return the type of the "other side" */
678 if (is_null_ptr(r))
679 return l->ctype;
680 if (is_null_ptr(l))
681 return r->ctype;
682 return NULL;
686 * Ignore differences in "volatile" and "const"ness when
687 * subtracting pointers
689 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
691 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
693 const char *typediff;
694 struct symbol *ctype;
695 struct symbol *ltype, *rtype;
696 struct expression *r = *rp;
698 ltype = degenerate(l);
699 rtype = degenerate(r);
702 * If it is an integer subtract: the ptr add case will do the
703 * right thing.
705 if (!is_ptr_type(rtype))
706 return evaluate_ptr_add(expr, degenerate(l), rp);
708 ctype = ltype;
709 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
710 if (typediff) {
711 ctype = common_ptr_type(l, r);
712 if (!ctype) {
713 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
714 return NULL;
717 examine_symbol_type(ctype);
719 /* Figure out the base type we point to */
720 if (ctype->type == SYM_NODE)
721 ctype = ctype->ctype.base_type;
722 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
723 warning(expr->pos, "subtraction of functions? Share your drugs");
724 return NULL;
726 ctype = ctype->ctype.base_type;
728 expr->ctype = ssize_t_ctype;
729 if (ctype->bit_size > bits_in_char) {
730 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
731 struct expression *div = expr;
732 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
734 val->ctype = size_t_ctype;
735 val->value = ctype->bit_size >> 3;
737 sub->op = '-';
738 sub->ctype = ssize_t_ctype;
739 sub->left = l;
740 sub->right = r;
742 div->op = '/';
743 div->left = sub;
744 div->right = val;
747 return ssize_t_ctype;
750 static struct symbol *evaluate_sub(struct expression *expr)
752 struct expression *left = expr->left;
753 struct symbol *ltype = left->ctype;
755 if (is_ptr_type(ltype))
756 return evaluate_ptr_sub(expr, left, &expr->right);
758 return evaluate_arith(expr, 1);
761 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
763 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
765 struct symbol *ctype;
767 if (!expr)
768 return NULL;
770 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
771 warning(expr->pos, "assignment expression in conditional");
773 ctype = evaluate_expression(expr);
774 if (ctype) {
775 if (is_safe_type(ctype))
776 warning(expr->pos, "testing a 'safe expression'");
779 return ctype;
782 static struct symbol *evaluate_logical(struct expression *expr)
784 if (!evaluate_conditional(expr->left, 0))
785 return NULL;
786 if (!evaluate_conditional(expr->right, 0))
787 return NULL;
789 expr->ctype = &bool_ctype;
790 return &bool_ctype;
793 static struct symbol *evaluate_shift(struct expression *expr)
795 struct expression *left = expr->left, *right = expr->right;
796 struct symbol *ltype = left->ctype, *rtype = right->ctype;
798 if (ltype->type == SYM_NODE)
799 ltype = ltype->ctype.base_type;
800 if (rtype->type == SYM_NODE)
801 rtype = rtype->ctype.base_type;
802 if (is_int_type(ltype) && is_int_type(rtype)) {
803 struct symbol *ctype = integer_promotion(ltype);
804 if (ltype->bit_size != ctype->bit_size)
805 expr->left = cast_to(expr->left, ctype);
806 expr->ctype = ctype;
807 ctype = integer_promotion(rtype);
808 if (rtype->bit_size != ctype->bit_size)
809 expr->right = cast_to(expr->right, ctype);
810 return expr->ctype;
812 return bad_expr_type(expr);
815 static struct symbol *evaluate_binop(struct expression *expr)
817 switch (expr->op) {
818 // addition can take ptr+int, fp and int
819 case '+':
820 return evaluate_add(expr);
822 // subtraction can take ptr-ptr, fp and int
823 case '-':
824 return evaluate_sub(expr);
826 // Arithmetic operations can take fp and int
827 case '*': case '/':
828 return evaluate_arith(expr, 1);
830 // shifts do integer promotions, but that's it.
831 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
832 return evaluate_shift(expr);
834 // The rest are integer operations
835 // '%', '&', '^', '|'
836 default:
837 return evaluate_arith(expr, 0);
841 static struct symbol *evaluate_comma(struct expression *expr)
843 expr->ctype = expr->right->ctype;
844 return expr->ctype;
847 static int modify_for_unsigned(int op)
849 if (op == '<')
850 op = SPECIAL_UNSIGNED_LT;
851 else if (op == '>')
852 op = SPECIAL_UNSIGNED_GT;
853 else if (op == SPECIAL_LTE)
854 op = SPECIAL_UNSIGNED_LTE;
855 else if (op == SPECIAL_GTE)
856 op = SPECIAL_UNSIGNED_GTE;
857 return op;
860 static struct symbol *evaluate_compare(struct expression *expr)
862 struct expression *left = expr->left, *right = expr->right;
863 struct symbol *ltype = left->ctype, *rtype = right->ctype;
864 struct symbol *ctype;
866 /* Type types? */
867 if (is_type_type(ltype) && is_type_type(rtype))
868 goto OK;
870 if (is_safe_type(ltype) || is_safe_type(rtype))
871 warning(expr->pos, "testing a 'safe expression'");
873 /* Pointer types? */
874 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
875 // FIXME! Check the types for compatibility
876 goto OK;
879 ctype = compatible_integer_binop(&expr->left, &expr->right);
880 if (ctype) {
881 if (ctype->ctype.modifiers & MOD_UNSIGNED)
882 expr->op = modify_for_unsigned(expr->op);
883 goto OK;
886 ctype = compatible_float_binop(&expr->left, &expr->right);
887 if (ctype)
888 goto OK;
890 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
891 if (ctype)
892 goto OK;
894 bad_expr_type(expr);
897 expr->ctype = &bool_ctype;
898 return &bool_ctype;
902 * FIXME!! This should do casts, array degeneration etc..
904 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
906 struct symbol *ltype = left->ctype, *rtype = right->ctype;
908 if (ltype->type == SYM_NODE)
909 ltype = ltype->ctype.base_type;
911 if (rtype->type == SYM_NODE)
912 rtype = rtype->ctype.base_type;
914 if (ltype->type == SYM_PTR) {
915 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
916 return ltype;
919 if (rtype->type == SYM_PTR) {
920 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
921 return rtype;
923 return NULL;
927 * NOTE! The degenerate case of "x ? : y", where we don't
928 * have a true case, this will possibly promote "x" to the
929 * same type as "y", and thus _change_ the conditional
930 * test in the expression. But since promotion is "safe"
931 * for testing, that's ok.
933 static struct symbol *evaluate_conditional_expression(struct expression *expr)
935 struct expression **true;
936 struct symbol *ctype, *ltype, *rtype;
937 const char * typediff;
939 if (!evaluate_conditional(expr->conditional, 0))
940 return NULL;
941 if (!evaluate_expression(expr->cond_false))
942 return NULL;
944 ctype = degenerate(expr->conditional);
945 rtype = degenerate(expr->cond_false);
947 true = &expr->conditional;
948 ltype = ctype;
949 if (expr->cond_true) {
950 if (!evaluate_expression(expr->cond_true))
951 return NULL;
952 ltype = degenerate(expr->cond_true);
953 true = &expr->cond_true;
956 ctype = compatible_integer_binop(true, &expr->cond_false);
957 if (ctype)
958 goto out;
959 ctype = compatible_ptr_type(*true, expr->cond_false);
960 if (ctype)
961 goto out;
962 ctype = compatible_float_binop(true, &expr->cond_false);
963 if (ctype)
964 goto out;
965 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
966 if (ctype)
967 goto out;
968 ctype = ltype;
969 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
970 if (!typediff)
971 goto out;
972 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
973 return NULL;
975 out:
976 expr->ctype = ctype;
977 return ctype;
980 /* FP assignments can not do modulo or bit operations */
981 static int compatible_float_op(int op)
983 return op == '=' ||
984 op == SPECIAL_ADD_ASSIGN ||
985 op == SPECIAL_SUB_ASSIGN ||
986 op == SPECIAL_MUL_ASSIGN ||
987 op == SPECIAL_DIV_ASSIGN;
990 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
991 struct expression **rp, struct symbol *source, const char *where, int op)
993 const char *typediff;
994 struct symbol *t;
995 int target_as;
997 if (is_int_type(target)) {
998 if (is_int_type(source)) {
999 if (target->bit_size != source->bit_size)
1000 goto Cast;
1001 if (target->bit_offset != source->bit_offset)
1002 goto Cast;
1003 return 1;
1005 if (is_float_type(source))
1006 goto Cast;
1007 } else if (is_float_type(target)) {
1008 if (!compatible_float_op(op)) {
1009 warning(expr->pos, "invalid assignment");
1010 return 0;
1012 if (is_int_type(source))
1013 goto Cast;
1014 if (is_float_type(source)) {
1015 if (target->bit_size != source->bit_size)
1016 goto Cast;
1017 return 1;
1019 } else if (is_restricted_type(target)) {
1020 if (restricted_binop(op, target)) {
1021 warning(expr->pos, "bad restricted assignment");
1022 return 0;
1024 if (!restricted_value(*rp, target))
1025 return 1;
1026 } else if (is_ptr_type(target)) {
1027 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1028 evaluate_ptr_add(expr, target, rp);
1029 return 1;
1031 if (op != '=') {
1032 warning(expr->pos, "invalid pointer assignment");
1033 return 0;
1035 } else if (op != '=') {
1036 warning(expr->pos, "invalid assignment");
1037 return 0;
1040 /* It's ok if the target is more volatile or const than the source */
1041 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1042 if (!typediff)
1043 return 1;
1045 /* Pointer destination? */
1046 t = target;
1047 target_as = t->ctype.as;
1048 if (t->type == SYM_NODE) {
1049 t = t->ctype.base_type;
1050 target_as |= t->ctype.as;
1052 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1053 struct expression *right = *rp;
1054 struct symbol *s = source;
1055 int source_as;
1057 // NULL pointer is always ok
1058 if (is_null_ptr(right))
1059 return 1;
1061 /* "void *" matches anything as long as the address space is ok */
1062 source_as = s->ctype.as;
1063 if (s->type == SYM_NODE) {
1064 s = s->ctype.base_type;
1065 source_as |= s->ctype.as;
1067 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1068 s = s->ctype.base_type;
1069 t = t->ctype.base_type;
1070 if (s == &void_ctype || t == &void_ctype)
1071 return 1;
1075 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1076 info(expr->pos, " expected %s", show_typename(target));
1077 info(expr->pos, " got %s", show_typename(source));
1078 *rp = cast_to(*rp, target);
1079 return 0;
1080 Cast:
1081 *rp = cast_to(*rp, target);
1082 return 1;
1085 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1087 if (type->ctype.modifiers & MOD_CONST)
1088 warning(left->pos, "assignment to const expression");
1089 if (type->type == SYM_NODE)
1090 type->ctype.modifiers |= MOD_ASSIGNED;
1093 static struct symbol *evaluate_assignment(struct expression *expr)
1095 struct expression *left = expr->left, *right = expr->right;
1096 struct expression *where = expr;
1097 struct symbol *ltype, *rtype;
1099 if (!lvalue_expression(left)) {
1100 warning(expr->pos, "not an lvalue");
1101 return NULL;
1104 ltype = left->ctype;
1106 rtype = degenerate(right);
1108 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1109 return NULL;
1111 evaluate_assign_to(left, ltype);
1113 expr->ctype = ltype;
1114 return ltype;
1117 static void examine_fn_arguments(struct symbol *fn)
1119 struct symbol *s;
1121 FOR_EACH_PTR(fn->arguments, s) {
1122 struct symbol *arg = evaluate_symbol(s);
1123 /* Array/function arguments silently degenerate into pointers */
1124 if (arg) {
1125 struct symbol *ptr;
1126 switch(arg->type) {
1127 case SYM_ARRAY:
1128 case SYM_FN:
1129 ptr = alloc_symbol(s->pos, SYM_PTR);
1130 if (arg->type == SYM_ARRAY)
1131 ptr->ctype = arg->ctype;
1132 else
1133 ptr->ctype.base_type = arg;
1134 ptr->ctype.as |= s->ctype.as;
1135 ptr->ctype.modifiers |= s->ctype.modifiers;
1137 s->ctype.base_type = ptr;
1138 s->ctype.as = 0;
1139 s->ctype.modifiers = 0;
1140 s->bit_size = 0;
1141 s->examined = 0;
1142 examine_symbol_type(s);
1143 break;
1144 default:
1145 /* nothing */
1146 break;
1149 } END_FOR_EACH_PTR(s);
1152 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1154 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1155 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1156 *newsym = *sym;
1157 newsym->ctype.as = as;
1158 newsym->ctype.modifiers = mod;
1159 sym = newsym;
1161 return sym;
1164 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1166 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1167 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1169 node->ctype.base_type = ptr;
1170 ptr->bit_size = bits_in_pointer;
1171 ptr->ctype.alignment = pointer_alignment;
1173 node->bit_size = bits_in_pointer;
1174 node->ctype.alignment = pointer_alignment;
1176 access_symbol(sym);
1177 if (sym->ctype.modifiers & MOD_REGISTER) {
1178 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1179 sym->ctype.modifiers &= ~MOD_REGISTER;
1181 if (sym->type == SYM_NODE) {
1182 ptr->ctype.as |= sym->ctype.as;
1183 ptr->ctype.modifiers |= sym->ctype.modifiers;
1184 sym = sym->ctype.base_type;
1186 if (degenerate && sym->type == SYM_ARRAY) {
1187 ptr->ctype.as |= sym->ctype.as;
1188 ptr->ctype.modifiers |= sym->ctype.modifiers;
1189 sym = sym->ctype.base_type;
1191 ptr->ctype.base_type = sym;
1193 return node;
1196 /* Arrays degenerate into pointers on pointer arithmetic */
1197 static struct symbol *degenerate(struct expression *expr)
1199 struct symbol *ctype, *base;
1201 if (!expr)
1202 return NULL;
1203 ctype = expr->ctype;
1204 if (!ctype)
1205 return NULL;
1206 base = ctype;
1207 if (ctype->type == SYM_NODE)
1208 base = ctype->ctype.base_type;
1210 * Arrays degenerate into pointers to the entries, while
1211 * functions degenerate into pointers to themselves.
1212 * If array was part of non-lvalue compound, we create a copy
1213 * of that compound first and then act as if we were dealing with
1214 * the corresponding field in there.
1216 switch (base->type) {
1217 case SYM_ARRAY:
1218 if (expr->type == EXPR_SLICE) {
1219 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1220 struct expression *e0, *e1, *e2, *e3, *e4;
1222 a->ctype.base_type = expr->base->ctype;
1223 a->bit_size = expr->base->ctype->bit_size;
1224 a->array_size = expr->base->ctype->array_size;
1226 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1227 e0->symbol = a;
1228 e0->ctype = &lazy_ptr_ctype;
1230 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1231 e1->unop = e0;
1232 e1->op = '*';
1233 e1->ctype = expr->base->ctype; /* XXX */
1235 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1236 e2->left = e1;
1237 e2->right = expr->base;
1238 e2->op = '=';
1239 e2->ctype = expr->base->ctype;
1241 if (expr->r_bitpos) {
1242 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1243 e3->op = '+';
1244 e3->left = e0;
1245 e3->right = alloc_const_expression(expr->pos,
1246 expr->r_bitpos >> 3);
1247 e3->ctype = &lazy_ptr_ctype;
1248 } else {
1249 e3 = e0;
1252 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1253 e4->left = e2;
1254 e4->right = e3;
1255 e4->ctype = &lazy_ptr_ctype;
1257 expr->unop = e4;
1258 expr->type = EXPR_PREOP;
1259 expr->op = '*';
1261 case SYM_FN:
1262 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1263 warning(expr->pos, "strange non-value function or array");
1264 return &bad_ctype;
1266 *expr = *expr->unop;
1267 ctype = create_pointer(expr, ctype, 1);
1268 expr->ctype = ctype;
1269 default:
1270 /* nothing */;
1272 return ctype;
1275 static struct symbol *evaluate_addressof(struct expression *expr)
1277 struct expression *op = expr->unop;
1278 struct symbol *ctype;
1280 if (op->op != '*' || op->type != EXPR_PREOP) {
1281 warning(expr->pos, "not addressable");
1282 return NULL;
1284 ctype = op->ctype;
1285 *expr = *op->unop;
1288 * symbol expression evaluation is lazy about the type
1289 * of the sub-expression, so we may have to generate
1290 * the type here if so..
1292 if (expr->ctype == &lazy_ptr_ctype) {
1293 ctype = create_pointer(expr, ctype, 0);
1294 expr->ctype = ctype;
1296 return expr->ctype;
1300 static struct symbol *evaluate_dereference(struct expression *expr)
1302 struct expression *op = expr->unop;
1303 struct symbol *ctype = op->ctype, *node, *target;
1305 /* Simplify: *&(expr) => (expr) */
1306 if (op->type == EXPR_PREOP && op->op == '&') {
1307 *expr = *op->unop;
1308 return expr->ctype;
1311 /* Dereferencing a node drops all the node information. */
1312 if (ctype->type == SYM_NODE)
1313 ctype = ctype->ctype.base_type;
1315 node = alloc_symbol(expr->pos, SYM_NODE);
1316 target = ctype->ctype.base_type;
1318 switch (ctype->type) {
1319 default:
1320 warning(expr->pos, "cannot derefence this type");
1321 return NULL;
1322 case SYM_PTR:
1323 merge_type(node, ctype);
1324 if (ctype->type != SYM_ARRAY)
1325 break;
1327 * Dereferencing a pointer to an array results in a
1328 * degenerate dereference: the expression becomes
1329 * just a pointer to the entry, and the derefence
1330 * goes away.
1332 *expr = *op;
1334 target = alloc_symbol(expr->pos, SYM_PTR);
1335 target->bit_size = bits_in_pointer;
1336 target->ctype.alignment = pointer_alignment;
1337 merge_type(target, ctype->ctype.base_type);
1338 break;
1340 case SYM_ARRAY:
1341 if (!lvalue_expression(op)) {
1342 warning(op->pos, "non-lvalue array??");
1343 return NULL;
1346 /* Do the implied "addressof" on the array */
1347 *op = *op->unop;
1350 * When an array is dereferenced, we need to pick
1351 * up the attributes of the original node too..
1353 merge_type(node, op->ctype);
1354 merge_type(node, ctype);
1355 break;
1358 node->bit_size = target->bit_size;
1359 node->array_size = target->array_size;
1361 expr->ctype = node;
1362 return node;
1366 * Unary post-ops: x++ and x--
1368 static struct symbol *evaluate_postop(struct expression *expr)
1370 struct expression *op = expr->unop;
1371 struct symbol *ctype = op->ctype;
1373 if (!lvalue_expression(expr->unop)) {
1374 warning(expr->pos, "need lvalue expression for ++/--");
1375 return NULL;
1377 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1378 warning(expr->pos, "bad operation on restricted");
1379 return NULL;
1382 evaluate_assign_to(op, ctype);
1384 expr->ctype = ctype;
1385 expr->op_value = 1;
1386 if (is_ptr_type(ctype))
1387 expr->op_value = ptr_object_size(ctype) >> 3;
1389 return ctype;
1392 static struct symbol *evaluate_sign(struct expression *expr)
1394 struct symbol *ctype = expr->unop->ctype;
1395 if (is_int_type(ctype)) {
1396 struct symbol *rtype = rtype = integer_promotion(ctype);
1397 if (rtype->bit_size != ctype->bit_size)
1398 expr->unop = cast_to(expr->unop, rtype);
1399 ctype = rtype;
1400 } else if (is_float_type(ctype) && expr->op != '~') {
1401 /* no conversions needed */
1402 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1403 /* no conversions needed */
1404 } else {
1405 return bad_expr_type(expr);
1407 if (expr->op == '+')
1408 *expr = *expr->unop;
1409 expr->ctype = ctype;
1410 return ctype;
1413 static struct symbol *evaluate_preop(struct expression *expr)
1415 struct symbol *ctype = expr->unop->ctype;
1417 switch (expr->op) {
1418 case '(':
1419 *expr = *expr->unop;
1420 return ctype;
1422 case '+':
1423 case '-':
1424 case '~':
1425 return evaluate_sign(expr);
1427 case '*':
1428 return evaluate_dereference(expr);
1430 case '&':
1431 return evaluate_addressof(expr);
1433 case SPECIAL_INCREMENT:
1434 case SPECIAL_DECREMENT:
1436 * From a type evaluation standpoint the pre-ops are
1437 * the same as the postops
1439 return evaluate_postop(expr);
1441 case '!':
1442 if (is_safe_type(ctype))
1443 warning(expr->pos, "testing a 'safe expression'");
1444 if (is_float_type(ctype)) {
1445 struct expression *arg = expr->unop;
1446 expr->type = EXPR_BINOP;
1447 expr->op = SPECIAL_EQUAL;
1448 expr->left = arg;
1449 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1450 expr->right->ctype = ctype;
1451 expr->right->fvalue = 0;
1453 ctype = &bool_ctype;
1454 break;
1456 default:
1457 break;
1459 expr->ctype = ctype;
1460 return &bool_ctype;
1463 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1465 struct ptr_list *head = (struct ptr_list *)_list;
1466 struct ptr_list *list = head;
1468 if (!head)
1469 return NULL;
1470 do {
1471 int i;
1472 for (i = 0; i < list->nr; i++) {
1473 struct symbol *sym = (struct symbol *) list->list[i];
1474 if (sym->ident) {
1475 if (sym->ident != ident)
1476 continue;
1477 *offset = sym->offset;
1478 return sym;
1479 } else {
1480 struct symbol *ctype = sym->ctype.base_type;
1481 struct symbol *sub;
1482 if (!ctype)
1483 continue;
1484 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1485 continue;
1486 sub = find_identifier(ident, ctype->symbol_list, offset);
1487 if (!sub)
1488 continue;
1489 *offset += sym->offset;
1490 return sub;
1493 } while ((list = list->next) != head);
1494 return NULL;
1497 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1499 struct expression *add;
1502 * Create a new add-expression
1504 * NOTE! Even if we just add zero, we need a new node
1505 * for the member pointer, since it has a different
1506 * type than the original pointer. We could make that
1507 * be just a cast, but the fact is, a node is a node,
1508 * so we might as well just do the "add zero" here.
1510 add = alloc_expression(expr->pos, EXPR_BINOP);
1511 add->op = '+';
1512 add->left = expr;
1513 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1514 add->right->ctype = &int_ctype;
1515 add->right->value = offset;
1518 * The ctype of the pointer will be lazily evaluated if
1519 * we ever take the address of this member dereference..
1521 add->ctype = &lazy_ptr_ctype;
1522 return add;
1525 /* structure/union dereference */
1526 static struct symbol *evaluate_member_dereference(struct expression *expr)
1528 int offset;
1529 struct symbol *ctype, *member;
1530 struct expression *deref = expr->deref, *add;
1531 struct ident *ident = expr->member;
1532 unsigned int mod;
1533 int address_space;
1535 if (!evaluate_expression(deref))
1536 return NULL;
1537 if (!ident) {
1538 warning(expr->pos, "bad member name");
1539 return NULL;
1542 ctype = deref->ctype;
1543 address_space = ctype->ctype.as;
1544 mod = ctype->ctype.modifiers;
1545 if (ctype->type == SYM_NODE) {
1546 ctype = ctype->ctype.base_type;
1547 address_space |= ctype->ctype.as;
1548 mod |= ctype->ctype.modifiers;
1550 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1551 warning(expr->pos, "expected structure or union");
1552 return NULL;
1554 offset = 0;
1555 member = find_identifier(ident, ctype->symbol_list, &offset);
1556 if (!member) {
1557 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1558 const char *name = "<unnamed>";
1559 int namelen = 9;
1560 if (ctype->ident) {
1561 name = ctype->ident->name;
1562 namelen = ctype->ident->len;
1564 warning(expr->pos, "no member '%s' in %s %.*s",
1565 show_ident(ident), type, namelen, name);
1566 return NULL;
1570 * The member needs to take on the address space and modifiers of
1571 * the "parent" type.
1573 member = convert_to_as_mod(member, address_space, mod);
1574 ctype = member->ctype.base_type;
1576 if (!lvalue_expression(deref)) {
1577 if (deref->type != EXPR_SLICE) {
1578 expr->base = deref;
1579 expr->r_bitpos = 0;
1580 } else {
1581 expr->base = deref->base;
1582 expr->r_bitpos = deref->r_bitpos;
1584 expr->r_bitpos += offset << 3;
1585 expr->type = EXPR_SLICE;
1586 expr->r_nrbits = member->bit_size;
1587 expr->r_bitpos += member->bit_offset;
1588 expr->ctype = member;
1589 return member;
1592 deref = deref->unop;
1593 expr->deref = deref;
1595 add = evaluate_offset(deref, offset);
1596 expr->type = EXPR_PREOP;
1597 expr->op = '*';
1598 expr->unop = add;
1600 expr->ctype = member;
1601 return member;
1604 static int is_promoted(struct expression *expr)
1606 while (1) {
1607 switch (expr->type) {
1608 case EXPR_BINOP:
1609 case EXPR_SELECT:
1610 case EXPR_CONDITIONAL:
1611 return 1;
1612 case EXPR_COMMA:
1613 expr = expr->right;
1614 continue;
1615 case EXPR_PREOP:
1616 switch (expr->op) {
1617 case '(':
1618 expr = expr->unop;
1619 continue;
1620 case '+':
1621 case '-':
1622 case '~':
1623 return 1;
1624 default:
1625 return 0;
1627 default:
1628 return 0;
1634 static struct symbol *evaluate_cast(struct expression *);
1636 static struct symbol *evaluate_type_information(struct expression *expr)
1638 struct symbol *sym = expr->cast_type;
1639 if (!sym) {
1640 sym = evaluate_expression(expr->cast_expression);
1641 if (!sym)
1642 return NULL;
1644 * Expressions of restricted types will possibly get
1645 * promoted - check that here
1647 if (is_restricted_type(sym)) {
1648 if (sym->bit_size < bits_in_int && is_promoted(expr))
1649 sym = &int_ctype;
1652 examine_symbol_type(sym);
1653 if (is_bitfield_type(sym)) {
1654 warning(expr->pos, "trying to examine bitfield type");
1655 return NULL;
1657 return sym;
1660 static struct symbol *evaluate_sizeof(struct expression *expr)
1662 struct symbol *type;
1663 int size;
1665 type = evaluate_type_information(expr);
1666 if (!type)
1667 return NULL;
1669 size = type->bit_size;
1670 if (size & 7)
1671 warning(expr->pos, "cannot size expression");
1672 expr->type = EXPR_VALUE;
1673 expr->value = size >> 3;
1674 expr->ctype = size_t_ctype;
1675 return size_t_ctype;
1678 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1680 struct symbol *type;
1681 int size;
1683 type = evaluate_type_information(expr);
1684 if (!type)
1685 return NULL;
1687 if (type->type == SYM_NODE)
1688 type = type->ctype.base_type;
1689 if (!type)
1690 return NULL;
1691 switch (type->type) {
1692 case SYM_ARRAY:
1693 break;
1694 case SYM_PTR:
1695 type = type->ctype.base_type;
1696 if (type)
1697 break;
1698 default:
1699 warning(expr->pos, "expected pointer expression");
1700 return NULL;
1702 size = type->bit_size;
1703 if (size & 7)
1704 size = 0;
1705 expr->type = EXPR_VALUE;
1706 expr->value = size >> 3;
1707 expr->ctype = size_t_ctype;
1708 return size_t_ctype;
1711 static struct symbol *evaluate_alignof(struct expression *expr)
1713 struct symbol *type;
1715 type = evaluate_type_information(expr);
1716 if (!type)
1717 return NULL;
1719 expr->type = EXPR_VALUE;
1720 expr->value = type->ctype.alignment;
1721 expr->ctype = size_t_ctype;
1722 return size_t_ctype;
1725 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1727 struct expression *expr;
1728 struct symbol_list *argument_types = fn->arguments;
1729 struct symbol *argtype;
1730 int i = 1;
1732 PREPARE_PTR_LIST(argument_types, argtype);
1733 FOR_EACH_PTR (head, expr) {
1734 struct expression **p = THIS_ADDRESS(expr);
1735 struct symbol *ctype, *target;
1736 ctype = evaluate_expression(expr);
1738 if (!ctype)
1739 return 0;
1741 ctype = degenerate(expr);
1743 target = argtype;
1744 if (!target && ctype->bit_size < bits_in_int)
1745 target = &int_ctype;
1746 if (target) {
1747 static char where[30];
1748 examine_symbol_type(target);
1749 sprintf(where, "argument %d", i);
1750 compatible_assignment_types(expr, target, p, ctype, where, '=');
1753 i++;
1754 NEXT_PTR_LIST(argtype);
1755 } END_FOR_EACH_PTR(expr);
1756 FINISH_PTR_LIST(argtype);
1757 return 1;
1760 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1762 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1764 struct expression *entry = *ep;
1765 struct expression **parent, *reuse = NULL;
1766 unsigned long offset;
1767 struct symbol *sym;
1768 unsigned long from, to;
1769 int accept_string = is_byte_type(ctype);
1771 from = current;
1772 to = from+1;
1773 parent = ep;
1774 if (entry->type == EXPR_INDEX) {
1775 from = entry->idx_from;
1776 to = entry->idx_to+1;
1777 parent = &entry->idx_expression;
1778 reuse = entry;
1779 entry = entry->idx_expression;
1782 offset = from * (ctype->bit_size>>3);
1783 if (offset) {
1784 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1785 reuse->type = EXPR_POS;
1786 reuse->ctype = ctype;
1787 reuse->init_offset = offset;
1788 reuse->init_nr = to - from;
1789 reuse->init_expr = entry;
1790 parent = &reuse->init_expr;
1791 entry = reuse;
1793 *ep = entry;
1795 if (accept_string && entry->type == EXPR_STRING) {
1796 sym = evaluate_expression(entry);
1797 to = from + get_expression_value(sym->array_size);
1798 } else {
1799 evaluate_initializer(ctype, parent);
1801 return to;
1804 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1806 struct expression *entry;
1807 int current = 0;
1809 FOR_EACH_PTR(expr->expr_list, entry) {
1810 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1811 } END_FOR_EACH_PTR(entry);
1814 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1815 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1817 if (expression_list_size(expr->expr_list) != 1) {
1818 warning(expr->pos, "unexpected compound initializer");
1819 return;
1821 evaluate_array_initializer(ctype, expr);
1822 return;
1825 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1827 struct symbol *sym;
1829 FOR_EACH_PTR(ctype->symbol_list, sym) {
1830 if (sym->ident == ident)
1831 return sym;
1832 } END_FOR_EACH_PTR(sym);
1833 return NULL;
1836 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1838 struct expression *entry = *ep;
1839 struct expression **parent;
1840 struct expression *reuse = NULL;
1841 unsigned long offset;
1843 if (!sym) {
1844 error(entry->pos, "unknown named initializer");
1845 return -1;
1848 if (entry->type == EXPR_IDENTIFIER) {
1849 reuse = entry;
1850 entry = entry->ident_expression;
1853 parent = ep;
1854 offset = sym->offset;
1855 if (offset) {
1856 if (!reuse)
1857 reuse = alloc_expression(entry->pos, EXPR_POS);
1858 reuse->type = EXPR_POS;
1859 reuse->ctype = sym;
1860 reuse->init_offset = offset;
1861 reuse->init_nr = 1;
1862 reuse->init_expr = entry;
1863 parent = &reuse->init_expr;
1864 entry = reuse;
1866 *ep = entry;
1867 evaluate_initializer(sym, parent);
1868 return 0;
1871 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1873 struct expression *entry;
1874 struct symbol *sym;
1876 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1877 FOR_EACH_PTR(expr->expr_list, entry) {
1878 if (entry->type == EXPR_IDENTIFIER) {
1879 struct ident *ident = entry->expr_ident;
1880 /* We special-case the "already right place" case */
1881 if (!sym || sym->ident != ident) {
1882 RESET_PTR_LIST(sym);
1883 for (;;) {
1884 if (!sym)
1885 break;
1886 if (sym->ident == ident)
1887 break;
1888 NEXT_PTR_LIST(sym);
1892 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1893 return;
1894 NEXT_PTR_LIST(sym);
1895 } END_FOR_EACH_PTR(entry);
1896 FINISH_PTR_LIST(sym);
1900 * Initializers are kind of like assignments. Except
1901 * they can be a hell of a lot more complex.
1903 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1905 struct expression *expr = *ep;
1908 * Simple non-structure/array initializers are the simple
1909 * case, and look (and parse) largely like assignments.
1911 switch (expr->type) {
1912 default: {
1913 int is_string = expr->type == EXPR_STRING;
1914 struct symbol *rtype = evaluate_expression(expr);
1915 if (rtype) {
1917 * Special case:
1918 * char array[] = "string"
1919 * should _not_ degenerate.
1921 if (!is_string || !is_string_type(ctype))
1922 rtype = degenerate(expr);
1923 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1925 return;
1928 case EXPR_INITIALIZER:
1929 expr->ctype = ctype;
1930 if (ctype->type == SYM_NODE)
1931 ctype = ctype->ctype.base_type;
1933 switch (ctype->type) {
1934 case SYM_ARRAY:
1935 case SYM_PTR:
1936 evaluate_array_initializer(ctype->ctype.base_type, expr);
1937 return;
1938 case SYM_UNION:
1939 evaluate_struct_or_union_initializer(ctype, expr, 0);
1940 return;
1941 case SYM_STRUCT:
1942 evaluate_struct_or_union_initializer(ctype, expr, 1);
1943 return;
1944 default:
1945 evaluate_scalar_initializer(ctype, expr);
1946 return;
1949 case EXPR_IDENTIFIER:
1950 if (ctype->type == SYM_NODE)
1951 ctype = ctype->ctype.base_type;
1952 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1953 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1954 show_symbol(ctype);
1955 return;
1957 evaluate_one_struct_initializer(ctype, ep,
1958 find_struct_ident(ctype, expr->expr_ident));
1959 return;
1961 case EXPR_INDEX:
1962 if (ctype->type == SYM_NODE)
1963 ctype = ctype->ctype.base_type;
1964 if (ctype->type != SYM_ARRAY) {
1965 error(expr->pos, "expected array");
1966 return;
1968 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1969 return;
1971 case EXPR_POS:
1973 * An EXPR_POS expression has already been evaluated, and we don't
1974 * need to do anything more
1976 return;
1980 static int get_as(struct symbol *sym)
1982 int as;
1983 unsigned long mod;
1985 if (!sym)
1986 return 0;
1987 as = sym->ctype.as;
1988 mod = sym->ctype.modifiers;
1989 if (sym->type == SYM_NODE) {
1990 sym = sym->ctype.base_type;
1991 as |= sym->ctype.as;
1992 mod |= sym->ctype.modifiers;
1996 * At least for now, allow casting to a "unsigned long".
1997 * That's how we do things like pointer arithmetic and
1998 * store pointers to registers.
2000 if (sym == &ulong_ctype)
2001 return -1;
2003 if (sym && sym->type == SYM_PTR) {
2004 sym = sym->ctype.base_type;
2005 as |= sym->ctype.as;
2006 mod |= sym->ctype.modifiers;
2008 if (mod & MOD_FORCE)
2009 return -1;
2010 return as;
2013 static struct symbol *evaluate_cast(struct expression *expr)
2015 struct expression *target = expr->cast_expression;
2016 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2017 enum type type;
2019 if (!target)
2020 return NULL;
2022 expr->ctype = ctype;
2023 expr->cast_type = ctype;
2026 * Special case: a cast can be followed by an
2027 * initializer, in which case we need to pass
2028 * the type value down to that initializer rather
2029 * than trying to evaluate it as an expression
2031 * A more complex case is when the initializer is
2032 * dereferenced as part of a post-fix expression.
2033 * We need to produce an expression that can be dereferenced.
2035 if (target->type == EXPR_INITIALIZER) {
2036 struct symbol *sym = expr->cast_type;
2037 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2039 sym->initializer = expr->cast_expression;
2040 evaluate_symbol(sym);
2042 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2043 addr->symbol = sym;
2045 expr->type = EXPR_PREOP;
2046 expr->op = '*';
2047 expr->unop = addr;
2048 expr->ctype = sym;
2050 return sym;
2053 evaluate_expression(target);
2054 degenerate(target);
2057 * You can always throw a value away by casting to
2058 * "void" - that's an implicit "force". Note that
2059 * the same is _not_ true of "void *".
2061 if (ctype == &void_ctype)
2062 goto out;
2064 type = ctype->type;
2065 if (type == SYM_NODE) {
2066 type = ctype->ctype.base_type->type;
2067 if (ctype->ctype.base_type == &void_ctype)
2068 goto out;
2070 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2071 warning(expr->pos, "cast to non-scalar");
2073 if (!target->ctype) {
2074 warning(expr->pos, "cast from unknown type");
2075 goto out;
2078 type = target->ctype->type;
2079 if (type == SYM_NODE)
2080 type = target->ctype->ctype.base_type->type;
2081 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2082 warning(expr->pos, "cast from non-scalar");
2084 if (!get_as(ctype) && get_as(target->ctype) > 0)
2085 warning(expr->pos, "cast removes address space of expression");
2087 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2088 struct symbol *t1 = ctype, *t2 = target->ctype;
2089 if (t1->type == SYM_NODE)
2090 t1 = t1->ctype.base_type;
2091 if (t2->type == SYM_NODE)
2092 t2 = t2->ctype.base_type;
2093 if (t1 != t2) {
2094 if (t1->type == SYM_RESTRICT)
2095 warning(expr->pos, "cast to restricted type");
2096 if (t2->type == SYM_RESTRICT)
2097 warning(expr->pos, "cast from restricted type");
2102 * Casts of constant values are special: they
2103 * can be NULL, and thus need to be simplified
2104 * early.
2106 if (target->type == EXPR_VALUE)
2107 cast_value(expr, ctype, target, target->ctype);
2109 out:
2110 return ctype;
2114 * Evaluate a call expression with a symbol. This
2115 * should expand inline functions, and evaluate
2116 * builtins.
2118 static int evaluate_symbol_call(struct expression *expr)
2120 struct expression *fn = expr->fn;
2121 struct symbol *ctype = fn->ctype;
2123 if (fn->type != EXPR_PREOP)
2124 return 0;
2126 if (ctype->op && ctype->op->evaluate)
2127 return ctype->op->evaluate(expr);
2129 if (ctype->ctype.modifiers & MOD_INLINE) {
2130 int ret;
2131 struct symbol *curr = current_fn;
2132 current_fn = ctype->ctype.base_type;
2133 examine_fn_arguments(current_fn);
2135 ret = inline_function(expr, ctype);
2137 /* restore the old function */
2138 current_fn = curr;
2139 return ret;
2142 return 0;
2145 static struct symbol *evaluate_call(struct expression *expr)
2147 int args, fnargs;
2148 struct symbol *ctype, *sym;
2149 struct expression *fn = expr->fn;
2150 struct expression_list *arglist = expr->args;
2152 if (!evaluate_expression(fn))
2153 return NULL;
2154 sym = ctype = fn->ctype;
2155 if (ctype->type == SYM_NODE)
2156 ctype = ctype->ctype.base_type;
2157 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2158 ctype = ctype->ctype.base_type;
2159 if (!evaluate_arguments(sym, ctype, arglist))
2160 return NULL;
2161 if (ctype->type != SYM_FN) {
2162 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2163 return NULL;
2165 args = expression_list_size(expr->args);
2166 fnargs = symbol_list_size(ctype->arguments);
2167 if (args < fnargs)
2168 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2169 if (args > fnargs && !ctype->variadic)
2170 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2171 if (sym->type == SYM_NODE) {
2172 if (evaluate_symbol_call(expr))
2173 return expr->ctype;
2175 expr->ctype = ctype->ctype.base_type;
2176 return expr->ctype;
2179 struct symbol *evaluate_expression(struct expression *expr)
2181 if (!expr)
2182 return NULL;
2183 if (expr->ctype)
2184 return expr->ctype;
2186 switch (expr->type) {
2187 case EXPR_VALUE:
2188 case EXPR_FVALUE:
2189 warning(expr->pos, "value expression without a type");
2190 return NULL;
2191 case EXPR_STRING:
2192 return evaluate_string(expr);
2193 case EXPR_SYMBOL:
2194 return evaluate_symbol_expression(expr);
2195 case EXPR_BINOP:
2196 if (!evaluate_expression(expr->left))
2197 return NULL;
2198 if (!evaluate_expression(expr->right))
2199 return NULL;
2200 return evaluate_binop(expr);
2201 case EXPR_LOGICAL:
2202 return evaluate_logical(expr);
2203 case EXPR_COMMA:
2204 evaluate_expression(expr->left);
2205 if (!evaluate_expression(expr->right))
2206 return NULL;
2207 return evaluate_comma(expr);
2208 case EXPR_COMPARE:
2209 if (!evaluate_expression(expr->left))
2210 return NULL;
2211 if (!evaluate_expression(expr->right))
2212 return NULL;
2213 return evaluate_compare(expr);
2214 case EXPR_ASSIGNMENT:
2215 if (!evaluate_expression(expr->left))
2216 return NULL;
2217 if (!evaluate_expression(expr->right))
2218 return NULL;
2219 return evaluate_assignment(expr);
2220 case EXPR_PREOP:
2221 if (!evaluate_expression(expr->unop))
2222 return NULL;
2223 return evaluate_preop(expr);
2224 case EXPR_POSTOP:
2225 if (!evaluate_expression(expr->unop))
2226 return NULL;
2227 return evaluate_postop(expr);
2228 case EXPR_CAST:
2229 case EXPR_IMPLIED_CAST:
2230 return evaluate_cast(expr);
2231 case EXPR_SIZEOF:
2232 return evaluate_sizeof(expr);
2233 case EXPR_PTRSIZEOF:
2234 return evaluate_ptrsizeof(expr);
2235 case EXPR_ALIGNOF:
2236 return evaluate_alignof(expr);
2237 case EXPR_DEREF:
2238 return evaluate_member_dereference(expr);
2239 case EXPR_CALL:
2240 return evaluate_call(expr);
2241 case EXPR_SELECT:
2242 case EXPR_CONDITIONAL:
2243 return evaluate_conditional_expression(expr);
2244 case EXPR_STATEMENT:
2245 expr->ctype = evaluate_statement(expr->statement);
2246 return expr->ctype;
2248 case EXPR_LABEL:
2249 expr->ctype = &ptr_ctype;
2250 return &ptr_ctype;
2252 case EXPR_TYPE:
2253 /* Evaluate the type of the symbol .. */
2254 evaluate_symbol(expr->symbol);
2255 /* .. but the type of the _expression_ is a "type" */
2256 expr->ctype = &type_ctype;
2257 return &type_ctype;
2259 /* These can not exist as stand-alone expressions */
2260 case EXPR_INITIALIZER:
2261 case EXPR_IDENTIFIER:
2262 case EXPR_INDEX:
2263 case EXPR_POS:
2264 warning(expr->pos, "internal front-end error: initializer in expression");
2265 return NULL;
2266 case EXPR_SLICE:
2267 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2268 return NULL;
2270 return NULL;
2273 static void check_duplicates(struct symbol *sym)
2275 struct symbol *next = sym;
2277 while ((next = next->same_symbol) != NULL) {
2278 const char *typediff;
2279 evaluate_symbol(next);
2280 typediff = type_difference(sym, next, 0, 0);
2281 if (typediff) {
2282 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2283 show_ident(sym->ident),
2284 input_streams[next->pos.stream].name, next->pos.line, typediff);
2285 return;
2290 static struct symbol *evaluate_symbol(struct symbol *sym)
2292 struct symbol *base_type;
2294 if (!sym)
2295 return sym;
2297 sym = examine_symbol_type(sym);
2298 base_type = sym->ctype.base_type;
2299 if (!base_type)
2300 return NULL;
2302 /* Evaluate the initializers */
2303 if (sym->initializer)
2304 evaluate_initializer(sym, &sym->initializer);
2306 /* And finally, evaluate the body of the symbol too */
2307 if (base_type->type == SYM_FN) {
2308 struct symbol *curr = current_fn;
2310 current_fn = base_type;
2312 examine_fn_arguments(base_type);
2313 if (!base_type->stmt && base_type->inline_stmt)
2314 uninline(sym);
2315 if (base_type->stmt)
2316 evaluate_statement(base_type->stmt);
2318 current_fn = curr;
2321 return base_type;
2324 void evaluate_symbol_list(struct symbol_list *list)
2326 struct symbol *sym;
2328 FOR_EACH_PTR(list, sym) {
2329 check_duplicates(sym);
2330 evaluate_symbol(sym);
2331 } END_FOR_EACH_PTR(sym);
2334 static struct symbol *evaluate_return_expression(struct statement *stmt)
2336 struct expression *expr = stmt->expression;
2337 struct symbol *ctype, *fntype;
2339 evaluate_expression(expr);
2340 ctype = degenerate(expr);
2341 fntype = current_fn->ctype.base_type;
2342 if (!fntype || fntype == &void_ctype) {
2343 if (expr && ctype != &void_ctype)
2344 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2345 return NULL;
2348 if (!expr) {
2349 warning(stmt->pos, "return with no return value");
2350 return NULL;
2352 if (!ctype)
2353 return NULL;
2354 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2355 return NULL;
2358 static void evaluate_if_statement(struct statement *stmt)
2360 if (!stmt->if_conditional)
2361 return;
2363 evaluate_conditional(stmt->if_conditional, 0);
2364 evaluate_statement(stmt->if_true);
2365 evaluate_statement(stmt->if_false);
2368 static void evaluate_iterator(struct statement *stmt)
2370 evaluate_conditional(stmt->iterator_pre_condition, 1);
2371 evaluate_conditional(stmt->iterator_post_condition,1);
2372 evaluate_statement(stmt->iterator_pre_statement);
2373 evaluate_statement(stmt->iterator_statement);
2374 evaluate_statement(stmt->iterator_post_statement);
2377 struct symbol *evaluate_statement(struct statement *stmt)
2379 if (!stmt)
2380 return NULL;
2382 switch (stmt->type) {
2383 case STMT_RETURN:
2384 return evaluate_return_expression(stmt);
2386 case STMT_EXPRESSION:
2387 if (!evaluate_expression(stmt->expression))
2388 return NULL;
2389 return degenerate(stmt->expression);
2391 case STMT_COMPOUND: {
2392 struct statement *s;
2393 struct symbol *type = NULL;
2394 struct symbol *sym;
2396 /* Evaluate each symbol in the compound statement */
2397 FOR_EACH_PTR(stmt->syms, sym) {
2398 evaluate_symbol(sym);
2399 } END_FOR_EACH_PTR(sym);
2400 evaluate_symbol(stmt->ret);
2403 * Then, evaluate each statement, making the type of the
2404 * compound statement be the type of the last statement
2406 type = NULL;
2407 FOR_EACH_PTR(stmt->stmts, s) {
2408 type = evaluate_statement(s);
2409 } END_FOR_EACH_PTR(s);
2410 if (!type)
2411 type = &void_ctype;
2412 return type;
2414 case STMT_IF:
2415 evaluate_if_statement(stmt);
2416 return NULL;
2417 case STMT_ITERATOR:
2418 evaluate_iterator(stmt);
2419 return NULL;
2420 case STMT_SWITCH:
2421 evaluate_expression(stmt->switch_expression);
2422 evaluate_statement(stmt->switch_statement);
2423 return NULL;
2424 case STMT_CASE:
2425 evaluate_expression(stmt->case_expression);
2426 evaluate_expression(stmt->case_to);
2427 evaluate_statement(stmt->case_statement);
2428 return NULL;
2429 case STMT_LABEL:
2430 return evaluate_statement(stmt->label_statement);
2431 case STMT_GOTO:
2432 evaluate_expression(stmt->goto_expression);
2433 return NULL;
2434 case STMT_NONE:
2435 break;
2436 case STMT_ASM:
2437 /* FIXME! Do the asm parameter evaluation! */
2438 break;
2439 case STMT_INTERNAL:
2440 evaluate_expression(stmt->expression);
2441 return NULL;
2443 return NULL;