Make "value_pseudo()" always return the same pseudo for
[smatch.git] / evaluate.c
blobf595f8a9cf80cd27d89837447c87662b06628645
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 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 struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
437 struct expression *i = *ip;
438 struct symbol *ptr_type = ctype;
439 int bit_size;
441 if (ptr_type->type == SYM_NODE)
442 ptr_type = ptr_type->ctype.base_type;
444 if (!is_int_type(i->ctype))
445 return bad_expr_type(expr);
447 examine_symbol_type(ctype);
449 if (!ctype->ctype.base_type) {
450 warning(expr->pos, "missing type information");
451 return NULL;
454 /* Get the size of whatever the pointer points to */
455 ptr_type = ctype;
456 if (ptr_type->type == SYM_NODE)
457 ptr_type = ptr_type->ctype.base_type;
458 if (ptr_type->type == SYM_PTR)
459 ptr_type = ptr_type->ctype.base_type;
460 bit_size = ptr_type->bit_size;
462 if (i->type == EXPR_VALUE) {
463 i->value *= bit_size >> 3;
464 } else if (bit_size > bits_in_char) {
465 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
466 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
468 val->ctype = size_t_ctype;
469 val->value = bit_size >> 3;
471 mul->op = '*';
472 mul->ctype = size_t_ctype;
473 mul->left = i;
474 mul->right = val;
476 *ip = mul;
479 expr->ctype = ctype;
480 return ctype;
483 static struct symbol *evaluate_add(struct expression *expr)
485 struct expression *left = expr->left, *right = expr->right;
486 struct symbol *ltype = left->ctype, *rtype = right->ctype;
488 if (is_ptr_type(ltype))
489 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
491 if (is_ptr_type(rtype))
492 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
494 return evaluate_arith(expr, 1);
497 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
498 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
499 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
501 const char * type_difference(struct symbol *target, struct symbol *source,
502 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
504 for (;;) {
505 unsigned long mod1, mod2, diff;
506 unsigned long as1, as2;
507 int type1, type2;
508 struct symbol *base1, *base2;
510 if (target == source)
511 break;
512 if (!target || !source)
513 return "different types";
515 * Peel of per-node information.
516 * FIXME! Check alignment and context too here!
518 mod1 = target->ctype.modifiers;
519 as1 = target->ctype.as;
520 mod2 = source->ctype.modifiers;
521 as2 = source->ctype.as;
522 if (target->type == SYM_NODE) {
523 target = target->ctype.base_type;
524 if (!target)
525 return "bad types";
526 if (target->type == SYM_PTR) {
527 mod1 = 0;
528 as1 = 0;
530 mod1 |= target->ctype.modifiers;
531 as1 |= target->ctype.as;
533 if (source->type == SYM_NODE) {
534 source = source->ctype.base_type;
535 if (!source)
536 return "bad types";
537 if (source->type == SYM_PTR) {
538 mod2 = 0;
539 as2 = 0;
541 mod2 |= source->ctype.modifiers;
542 as2 |= source->ctype.as;
544 if (target->type == SYM_ENUM) {
545 target = target->ctype.base_type;
546 if (!target)
547 return "bad types";
549 if (source->type == SYM_ENUM) {
550 source = source->ctype.base_type;
551 if (!source)
552 return "bad types";
555 if (target == source)
556 break;
557 if (!target || !source)
558 return "different types";
560 type1 = target->type;
561 base1 = target->ctype.base_type;
563 type2 = source->type;
564 base2 = source->ctype.base_type;
567 * Pointers to functions compare as the function itself
569 if (type1 == SYM_PTR && base1) {
570 switch (base1->type) {
571 case SYM_FN:
572 type1 = SYM_FN;
573 target = base1;
574 base1 = base1->ctype.base_type;
575 default:
576 /* nothing */;
579 if (type2 == SYM_PTR && base2) {
580 switch (base2->type) {
581 case SYM_FN:
582 type2 = SYM_FN;
583 source = base2;
584 base2 = base2->ctype.base_type;
585 default:
586 /* nothing */;
590 /* Arrays degenerate to pointers for type comparisons */
591 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
592 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
594 if (type1 != type2 || type1 == SYM_RESTRICT)
595 return "different base types";
597 /* Must be same address space to be comparable */
598 if (as1 != as2)
599 return "different address spaces";
601 /* Ignore differences in storage types or addressability */
602 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
603 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
604 if (diff) {
605 if (diff & MOD_SIZE)
606 return "different type sizes";
607 if (diff & ~MOD_SIGNEDNESS)
608 return "different modifiers";
610 /* Differs in signedness only.. */
611 if (Wtypesign) {
613 * Warn if both are explicitly signed ("unsigned" is obvously
614 * always explicit, and since we know one of them has to be
615 * unsigned, we check if the signed one was explicit).
617 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
618 return "different explicit signedness";
621 * "char" matches both "unsigned char" and "signed char",
622 * so if the explicit test didn't trigger, then we should
623 * not warn about a char.
625 if (!(mod1 & MOD_CHAR))
626 return "different signedness";
630 if (type1 == SYM_FN) {
631 int i;
632 struct symbol *arg1, *arg2;
633 if (base1->variadic != base2->variadic)
634 return "incompatible variadic arguments";
635 PREPARE_PTR_LIST(target->arguments, arg1);
636 PREPARE_PTR_LIST(source->arguments, arg2);
637 i = 1;
638 for (;;) {
639 const char *diff;
640 diff = type_difference(arg1, arg2, 0, 0);
641 if (diff) {
642 static char argdiff[80];
643 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
644 return argdiff;
646 if (!arg1)
647 break;
648 NEXT_PTR_LIST(arg1);
649 NEXT_PTR_LIST(arg2);
650 i++;
652 FINISH_PTR_LIST(arg2);
653 FINISH_PTR_LIST(arg1);
656 target = base1;
657 source = base2;
659 return NULL;
662 static int is_null_ptr(struct expression *expr)
664 if (expr->type != EXPR_VALUE || expr->value)
665 return 0;
666 if (!is_ptr_type(expr->ctype))
667 warning(expr->pos, "Using plain integer as NULL pointer");
668 return 1;
671 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
673 /* NULL expression? Just return the type of the "other side" */
674 if (is_null_ptr(r))
675 return l->ctype;
676 if (is_null_ptr(l))
677 return r->ctype;
678 return NULL;
682 * Ignore differences in "volatile" and "const"ness when
683 * subtracting pointers
685 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
687 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
689 const char *typediff;
690 struct symbol *ctype;
691 struct symbol *ltype, *rtype;
692 struct expression *r = *rp;
694 ltype = degenerate(l);
695 rtype = degenerate(r);
698 * If it is an integer subtract: the ptr add case will do the
699 * right thing.
701 if (!is_ptr_type(rtype))
702 return evaluate_ptr_add(expr, degenerate(l), rp);
704 ctype = ltype;
705 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
706 if (typediff) {
707 ctype = common_ptr_type(l, r);
708 if (!ctype) {
709 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
710 return NULL;
713 examine_symbol_type(ctype);
715 /* Figure out the base type we point to */
716 if (ctype->type == SYM_NODE)
717 ctype = ctype->ctype.base_type;
718 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
719 warning(expr->pos, "subtraction of functions? Share your drugs");
720 return NULL;
722 ctype = ctype->ctype.base_type;
724 expr->ctype = ssize_t_ctype;
725 if (ctype->bit_size > bits_in_char) {
726 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
727 struct expression *div = expr;
728 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
730 val->ctype = size_t_ctype;
731 val->value = ctype->bit_size >> 3;
733 sub->op = '-';
734 sub->ctype = ssize_t_ctype;
735 sub->left = l;
736 sub->right = r;
738 div->op = '/';
739 div->left = sub;
740 div->right = val;
743 return ssize_t_ctype;
746 static struct symbol *evaluate_sub(struct expression *expr)
748 struct expression *left = expr->left;
749 struct symbol *ltype = left->ctype;
751 if (is_ptr_type(ltype))
752 return evaluate_ptr_sub(expr, left, &expr->right);
754 return evaluate_arith(expr, 1);
757 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
759 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
761 struct symbol *ctype;
763 if (!expr)
764 return NULL;
766 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
767 warning(expr->pos, "assignment expression in conditional");
769 ctype = evaluate_expression(expr);
770 if (ctype) {
771 if (is_safe_type(ctype))
772 warning(expr->pos, "testing a 'safe expression'");
775 return ctype;
778 static struct symbol *evaluate_logical(struct expression *expr)
780 if (!evaluate_conditional(expr->left, 0))
781 return NULL;
782 if (!evaluate_conditional(expr->right, 0))
783 return NULL;
785 expr->ctype = &bool_ctype;
786 return &bool_ctype;
789 static struct symbol *evaluate_shift(struct expression *expr)
791 struct expression *left = expr->left, *right = expr->right;
792 struct symbol *ltype = left->ctype, *rtype = right->ctype;
794 if (ltype->type == SYM_NODE)
795 ltype = ltype->ctype.base_type;
796 if (rtype->type == SYM_NODE)
797 rtype = rtype->ctype.base_type;
798 if (is_int_type(ltype) && is_int_type(rtype)) {
799 struct symbol *ctype = integer_promotion(ltype);
800 if (ltype->bit_size != ctype->bit_size)
801 expr->left = cast_to(expr->left, ctype);
802 expr->ctype = ctype;
803 ctype = integer_promotion(rtype);
804 if (rtype->bit_size != ctype->bit_size)
805 expr->right = cast_to(expr->right, ctype);
806 return expr->ctype;
808 return bad_expr_type(expr);
811 static struct symbol *evaluate_binop(struct expression *expr)
813 switch (expr->op) {
814 // addition can take ptr+int, fp and int
815 case '+':
816 return evaluate_add(expr);
818 // subtraction can take ptr-ptr, fp and int
819 case '-':
820 return evaluate_sub(expr);
822 // Arithmetic operations can take fp and int
823 case '*': case '/':
824 return evaluate_arith(expr, 1);
826 // shifts do integer promotions, but that's it.
827 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
828 return evaluate_shift(expr);
830 // The rest are integer operations
831 // '%', '&', '^', '|'
832 default:
833 return evaluate_arith(expr, 0);
837 static struct symbol *evaluate_comma(struct expression *expr)
839 expr->ctype = expr->right->ctype;
840 return expr->ctype;
843 static int modify_for_unsigned(int op)
845 if (op == '<')
846 op = SPECIAL_UNSIGNED_LT;
847 else if (op == '>')
848 op = SPECIAL_UNSIGNED_GT;
849 else if (op == SPECIAL_LTE)
850 op = SPECIAL_UNSIGNED_LTE;
851 else if (op == SPECIAL_GTE)
852 op = SPECIAL_UNSIGNED_GTE;
853 return op;
856 static struct symbol *evaluate_compare(struct expression *expr)
858 struct expression *left = expr->left, *right = expr->right;
859 struct symbol *ltype = left->ctype, *rtype = right->ctype;
860 struct symbol *ctype;
862 /* Type types? */
863 if (is_type_type(ltype) && is_type_type(rtype))
864 goto OK;
866 if (is_safe_type(ltype) || is_safe_type(rtype))
867 warning(expr->pos, "testing a 'safe expression'");
869 /* Pointer types? */
870 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
871 // FIXME! Check the types for compatibility
872 goto OK;
875 ctype = compatible_integer_binop(&expr->left, &expr->right);
876 if (ctype) {
877 if (ctype->ctype.modifiers & MOD_UNSIGNED)
878 expr->op = modify_for_unsigned(expr->op);
879 goto OK;
882 ctype = compatible_float_binop(&expr->left, &expr->right);
883 if (ctype)
884 goto OK;
886 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
887 if (ctype)
888 goto OK;
890 bad_expr_type(expr);
893 expr->ctype = &bool_ctype;
894 return &bool_ctype;
898 * FIXME!! This should do casts, array degeneration etc..
900 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
902 struct symbol *ltype = left->ctype, *rtype = right->ctype;
904 if (ltype->type == SYM_NODE)
905 ltype = ltype->ctype.base_type;
907 if (rtype->type == SYM_NODE)
908 rtype = rtype->ctype.base_type;
910 if (ltype->type == SYM_PTR) {
911 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
912 return ltype;
915 if (rtype->type == SYM_PTR) {
916 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
917 return rtype;
919 return NULL;
923 * NOTE! The degenerate case of "x ? : y", where we don't
924 * have a true case, this will possibly promote "x" to the
925 * same type as "y", and thus _change_ the conditional
926 * test in the expression. But since promotion is "safe"
927 * for testing, that's ok.
929 static struct symbol *evaluate_conditional_expression(struct expression *expr)
931 struct expression **true;
932 struct symbol *ctype, *ltype, *rtype;
933 const char * typediff;
935 if (!evaluate_conditional(expr->conditional, 0))
936 return NULL;
937 if (!evaluate_expression(expr->cond_false))
938 return NULL;
940 ctype = degenerate(expr->conditional);
941 rtype = degenerate(expr->cond_false);
943 true = &expr->conditional;
944 ltype = ctype;
945 if (expr->cond_true) {
946 if (!evaluate_expression(expr->cond_true))
947 return NULL;
948 ltype = degenerate(expr->cond_true);
949 true = &expr->cond_true;
952 ctype = compatible_integer_binop(true, &expr->cond_false);
953 if (ctype)
954 goto out;
955 ctype = compatible_ptr_type(*true, expr->cond_false);
956 if (ctype)
957 goto out;
958 ctype = compatible_float_binop(true, &expr->cond_false);
959 if (ctype)
960 goto out;
961 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
962 if (ctype)
963 goto out;
964 ctype = ltype;
965 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
966 if (!typediff)
967 goto out;
968 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
969 return NULL;
971 out:
972 expr->ctype = ctype;
973 return ctype;
976 /* FP assignments can not do modulo or bit operations */
977 static int compatible_float_op(int op)
979 return op == '=' ||
980 op == SPECIAL_ADD_ASSIGN ||
981 op == SPECIAL_SUB_ASSIGN ||
982 op == SPECIAL_MUL_ASSIGN ||
983 op == SPECIAL_DIV_ASSIGN;
986 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
987 struct expression **rp, struct symbol *source, const char *where, int op)
989 const char *typediff;
990 struct symbol *t;
991 int target_as;
993 if (is_int_type(target)) {
994 if (is_int_type(source)) {
995 if (target->bit_size != source->bit_size)
996 goto Cast;
997 if (target->bit_offset != source->bit_offset)
998 goto Cast;
999 return 1;
1001 if (is_float_type(source))
1002 goto Cast;
1003 } else if (is_float_type(target)) {
1004 if (!compatible_float_op(op)) {
1005 warning(expr->pos, "invalid assignment");
1006 return 0;
1008 if (is_int_type(source))
1009 goto Cast;
1010 if (is_float_type(source)) {
1011 if (target->bit_size != source->bit_size)
1012 goto Cast;
1013 return 1;
1015 } else if (is_restricted_type(target)) {
1016 if (restricted_binop(op, target)) {
1017 warning(expr->pos, "bad restricted assignment");
1018 return 0;
1020 if (!restricted_value(*rp, target))
1021 return 1;
1022 } else if (is_ptr_type(target)) {
1023 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1024 evaluate_ptr_add(expr, target, rp);
1025 return 1;
1027 if (op != '=') {
1028 warning(expr->pos, "invalid pointer assignment");
1029 return 0;
1031 } else if (op != '=') {
1032 warning(expr->pos, "invalid assignment");
1033 return 0;
1036 /* It's ok if the target is more volatile or const than the source */
1037 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1038 if (!typediff)
1039 return 1;
1041 /* Pointer destination? */
1042 t = target;
1043 target_as = t->ctype.as;
1044 if (t->type == SYM_NODE) {
1045 t = t->ctype.base_type;
1046 target_as |= t->ctype.as;
1048 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1049 struct expression *right = *rp;
1050 struct symbol *s = source;
1051 int source_as;
1053 // NULL pointer is always ok
1054 if (is_null_ptr(right))
1055 return 1;
1057 /* "void *" matches anything as long as the address space is ok */
1058 source_as = s->ctype.as;
1059 if (s->type == SYM_NODE) {
1060 s = s->ctype.base_type;
1061 source_as |= s->ctype.as;
1063 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1064 s = s->ctype.base_type;
1065 t = t->ctype.base_type;
1066 if (s == &void_ctype || t == &void_ctype)
1067 return 1;
1071 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1072 info(expr->pos, " expected %s", show_typename(target));
1073 info(expr->pos, " got %s", show_typename(source));
1074 *rp = cast_to(*rp, target);
1075 return 0;
1076 Cast:
1077 *rp = cast_to(*rp, target);
1078 return 1;
1081 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1083 if (type->ctype.modifiers & MOD_CONST)
1084 warning(left->pos, "assignment to const expression");
1085 if (type->type == SYM_NODE)
1086 type->ctype.modifiers |= MOD_ASSIGNED;
1089 static struct symbol *evaluate_assignment(struct expression *expr)
1091 struct expression *left = expr->left, *right = expr->right;
1092 struct expression *where = expr;
1093 struct symbol *ltype, *rtype;
1095 if (!lvalue_expression(left)) {
1096 warning(expr->pos, "not an lvalue");
1097 return NULL;
1100 ltype = left->ctype;
1102 rtype = degenerate(right);
1104 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1105 return NULL;
1107 evaluate_assign_to(left, ltype);
1109 expr->ctype = ltype;
1110 return ltype;
1113 static void examine_fn_arguments(struct symbol *fn)
1115 struct symbol *s;
1117 FOR_EACH_PTR(fn->arguments, s) {
1118 struct symbol *arg = evaluate_symbol(s);
1119 /* Array/function arguments silently degenerate into pointers */
1120 if (arg) {
1121 struct symbol *ptr;
1122 switch(arg->type) {
1123 case SYM_ARRAY:
1124 case SYM_FN:
1125 ptr = alloc_symbol(s->pos, SYM_PTR);
1126 if (arg->type == SYM_ARRAY)
1127 ptr->ctype = arg->ctype;
1128 else
1129 ptr->ctype.base_type = arg;
1130 ptr->ctype.as |= s->ctype.as;
1131 ptr->ctype.modifiers |= s->ctype.modifiers;
1133 s->ctype.base_type = ptr;
1134 s->ctype.as = 0;
1135 s->ctype.modifiers = 0;
1136 s->bit_size = 0;
1137 examine_symbol_type(s);
1138 break;
1139 default:
1140 /* nothing */
1141 break;
1144 } END_FOR_EACH_PTR(s);
1147 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1149 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1150 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1151 *newsym = *sym;
1152 newsym->ctype.as = as;
1153 newsym->ctype.modifiers = mod;
1154 sym = newsym;
1156 return sym;
1159 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1161 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1162 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1164 node->ctype.base_type = ptr;
1165 ptr->bit_size = bits_in_pointer;
1166 ptr->ctype.alignment = pointer_alignment;
1168 node->bit_size = bits_in_pointer;
1169 node->ctype.alignment = pointer_alignment;
1171 access_symbol(sym);
1172 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1173 if (sym->ctype.modifiers & MOD_REGISTER) {
1174 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1175 sym->ctype.modifiers &= ~MOD_REGISTER;
1177 if (sym->type == SYM_NODE) {
1178 ptr->ctype.as |= sym->ctype.as;
1179 ptr->ctype.modifiers |= sym->ctype.modifiers;
1180 sym = sym->ctype.base_type;
1182 if (degenerate && sym->type == SYM_ARRAY) {
1183 ptr->ctype.as |= sym->ctype.as;
1184 ptr->ctype.modifiers |= sym->ctype.modifiers;
1185 sym = sym->ctype.base_type;
1187 ptr->ctype.base_type = sym;
1189 return node;
1192 /* Arrays degenerate into pointers on pointer arithmetic */
1193 static struct symbol *degenerate(struct expression *expr)
1195 struct symbol *ctype, *base;
1197 if (!expr)
1198 return NULL;
1199 ctype = expr->ctype;
1200 if (!ctype)
1201 return NULL;
1202 base = ctype;
1203 if (ctype->type == SYM_NODE)
1204 base = ctype->ctype.base_type;
1206 * Arrays degenerate into pointers to the entries, while
1207 * functions degenerate into pointers to themselves.
1208 * If array was part of non-lvalue compound, we create a copy
1209 * of that compound first and then act as if we were dealing with
1210 * the corresponding field in there.
1212 switch (base->type) {
1213 case SYM_ARRAY:
1214 if (expr->type == EXPR_SLICE) {
1215 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1216 struct expression *e0, *e1, *e2, *e3, *e4;
1218 a->ctype.base_type = expr->base->ctype;
1219 a->bit_size = expr->base->ctype->bit_size;
1220 a->array_size = expr->base->ctype->array_size;
1222 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1223 e0->symbol = a;
1224 e0->ctype = &lazy_ptr_ctype;
1226 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1227 e1->unop = e0;
1228 e1->op = '*';
1229 e1->ctype = expr->base->ctype; /* XXX */
1231 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1232 e2->left = e1;
1233 e2->right = expr->base;
1234 e2->op = '=';
1235 e2->ctype = expr->base->ctype;
1237 if (expr->r_bitpos) {
1238 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1239 e3->op = '+';
1240 e3->left = e0;
1241 e3->right = alloc_const_expression(expr->pos,
1242 expr->r_bitpos >> 3);
1243 e3->ctype = &lazy_ptr_ctype;
1244 } else {
1245 e3 = e0;
1248 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1249 e4->left = e2;
1250 e4->right = e3;
1251 e4->ctype = &lazy_ptr_ctype;
1253 expr->unop = e4;
1254 expr->type = EXPR_PREOP;
1255 expr->op = '*';
1257 case SYM_FN:
1258 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1259 warning(expr->pos, "strange non-value function or array");
1260 return &bad_ctype;
1262 *expr = *expr->unop;
1263 ctype = create_pointer(expr, ctype, 1);
1264 expr->ctype = ctype;
1265 default:
1266 /* nothing */;
1268 return ctype;
1271 static struct symbol *evaluate_addressof(struct expression *expr)
1273 struct expression *op = expr->unop;
1274 struct symbol *ctype;
1276 if (op->op != '*' || op->type != EXPR_PREOP) {
1277 warning(expr->pos, "not addressable");
1278 return NULL;
1280 ctype = op->ctype;
1281 *expr = *op->unop;
1284 * symbol expression evaluation is lazy about the type
1285 * of the sub-expression, so we may have to generate
1286 * the type here if so..
1288 if (expr->ctype == &lazy_ptr_ctype) {
1289 ctype = create_pointer(expr, ctype, 0);
1290 expr->ctype = ctype;
1292 return expr->ctype;
1296 static struct symbol *evaluate_dereference(struct expression *expr)
1298 struct expression *op = expr->unop;
1299 struct symbol *ctype = op->ctype, *node, *target;
1301 /* Simplify: *&(expr) => (expr) */
1302 if (op->type == EXPR_PREOP && op->op == '&') {
1303 *expr = *op->unop;
1304 return expr->ctype;
1307 /* Dereferencing a node drops all the node information. */
1308 if (ctype->type == SYM_NODE)
1309 ctype = ctype->ctype.base_type;
1311 node = alloc_symbol(expr->pos, SYM_NODE);
1312 target = ctype->ctype.base_type;
1314 switch (ctype->type) {
1315 default:
1316 warning(expr->pos, "cannot derefence this type");
1317 return NULL;
1318 case SYM_PTR:
1319 merge_type(node, ctype);
1320 if (ctype->type != SYM_ARRAY)
1321 break;
1323 * Dereferencing a pointer to an array results in a
1324 * degenerate dereference: the expression becomes
1325 * just a pointer to the entry, and the derefence
1326 * goes away.
1328 *expr = *op;
1330 target = alloc_symbol(expr->pos, SYM_PTR);
1331 target->bit_size = bits_in_pointer;
1332 target->ctype.alignment = pointer_alignment;
1333 merge_type(target, ctype->ctype.base_type);
1334 break;
1336 case SYM_ARRAY:
1337 if (!lvalue_expression(op)) {
1338 warning(op->pos, "non-lvalue array??");
1339 return NULL;
1342 /* Do the implied "addressof" on the array */
1343 *op = *op->unop;
1346 * When an array is dereferenced, we need to pick
1347 * up the attributes of the original node too..
1349 merge_type(node, op->ctype);
1350 merge_type(node, ctype);
1351 break;
1354 node->bit_size = target->bit_size;
1355 node->array_size = target->array_size;
1357 expr->ctype = node;
1358 return node;
1362 * Unary post-ops: x++ and x--
1364 static struct symbol *evaluate_postop(struct expression *expr)
1366 struct expression *op = expr->unop;
1367 struct symbol *ctype = op->ctype;
1369 if (!lvalue_expression(expr->unop)) {
1370 warning(expr->pos, "need lvalue expression for ++/--");
1371 return NULL;
1373 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1374 warning(expr->pos, "bad operation on restricted");
1375 return NULL;
1378 evaluate_assign_to(op, ctype);
1380 expr->ctype = ctype;
1381 return ctype;
1384 static struct symbol *evaluate_sign(struct expression *expr)
1386 struct symbol *ctype = expr->unop->ctype;
1387 if (is_int_type(ctype)) {
1388 struct symbol *rtype = rtype = integer_promotion(ctype);
1389 if (rtype->bit_size != ctype->bit_size)
1390 expr->unop = cast_to(expr->unop, rtype);
1391 ctype = rtype;
1392 } else if (is_float_type(ctype) && expr->op != '~') {
1393 /* no conversions needed */
1394 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1395 /* no conversions needed */
1396 } else {
1397 return bad_expr_type(expr);
1399 if (expr->op == '+')
1400 *expr = *expr->unop;
1401 expr->ctype = ctype;
1402 return ctype;
1405 static struct symbol *evaluate_preop(struct expression *expr)
1407 struct symbol *ctype = expr->unop->ctype;
1409 switch (expr->op) {
1410 case '(':
1411 *expr = *expr->unop;
1412 return ctype;
1414 case '+':
1415 case '-':
1416 case '~':
1417 return evaluate_sign(expr);
1419 case '*':
1420 return evaluate_dereference(expr);
1422 case '&':
1423 return evaluate_addressof(expr);
1425 case SPECIAL_INCREMENT:
1426 case SPECIAL_DECREMENT:
1428 * From a type evaluation standpoint the pre-ops are
1429 * the same as the postops
1431 return evaluate_postop(expr);
1433 case '!':
1434 if (is_safe_type(ctype))
1435 warning(expr->pos, "testing a 'safe expression'");
1436 if (is_float_type(ctype)) {
1437 struct expression *arg = expr->unop;
1438 expr->type = EXPR_BINOP;
1439 expr->op = SPECIAL_EQUAL;
1440 expr->left = arg;
1441 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1442 expr->right->ctype = ctype;
1443 expr->right->fvalue = 0;
1445 ctype = &bool_ctype;
1446 break;
1448 default:
1449 break;
1451 expr->ctype = ctype;
1452 return &bool_ctype;
1455 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1457 struct ptr_list *head = (struct ptr_list *)_list;
1458 struct ptr_list *list = head;
1460 if (!head)
1461 return NULL;
1462 do {
1463 int i;
1464 for (i = 0; i < list->nr; i++) {
1465 struct symbol *sym = (struct symbol *) list->list[i];
1466 if (sym->ident) {
1467 if (sym->ident != ident)
1468 continue;
1469 *offset = sym->offset;
1470 return sym;
1471 } else {
1472 struct symbol *ctype = sym->ctype.base_type;
1473 struct symbol *sub;
1474 if (!ctype)
1475 continue;
1476 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1477 continue;
1478 sub = find_identifier(ident, ctype->symbol_list, offset);
1479 if (!sub)
1480 continue;
1481 *offset += sym->offset;
1482 return sub;
1485 } while ((list = list->next) != head);
1486 return NULL;
1489 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1491 struct expression *add;
1494 * Create a new add-expression
1496 * NOTE! Even if we just add zero, we need a new node
1497 * for the member pointer, since it has a different
1498 * type than the original pointer. We could make that
1499 * be just a cast, but the fact is, a node is a node,
1500 * so we might as well just do the "add zero" here.
1502 add = alloc_expression(expr->pos, EXPR_BINOP);
1503 add->op = '+';
1504 add->left = expr;
1505 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1506 add->right->ctype = &int_ctype;
1507 add->right->value = offset;
1510 * The ctype of the pointer will be lazily evaluated if
1511 * we ever take the address of this member dereference..
1513 add->ctype = &lazy_ptr_ctype;
1514 return add;
1517 /* structure/union dereference */
1518 static struct symbol *evaluate_member_dereference(struct expression *expr)
1520 int offset;
1521 struct symbol *ctype, *member;
1522 struct expression *deref = expr->deref, *add;
1523 struct ident *ident = expr->member;
1524 unsigned int mod;
1525 int address_space;
1527 if (!evaluate_expression(deref))
1528 return NULL;
1529 if (!ident) {
1530 warning(expr->pos, "bad member name");
1531 return NULL;
1534 ctype = deref->ctype;
1535 address_space = ctype->ctype.as;
1536 mod = ctype->ctype.modifiers;
1537 if (ctype->type == SYM_NODE) {
1538 ctype = ctype->ctype.base_type;
1539 address_space |= ctype->ctype.as;
1540 mod |= ctype->ctype.modifiers;
1542 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1543 warning(expr->pos, "expected structure or union");
1544 return NULL;
1546 offset = 0;
1547 member = find_identifier(ident, ctype->symbol_list, &offset);
1548 if (!member) {
1549 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1550 const char *name = "<unnamed>";
1551 int namelen = 9;
1552 if (ctype->ident) {
1553 name = ctype->ident->name;
1554 namelen = ctype->ident->len;
1556 warning(expr->pos, "no member '%s' in %s %.*s",
1557 show_ident(ident), type, namelen, name);
1558 return NULL;
1562 * The member needs to take on the address space and modifiers of
1563 * the "parent" type.
1565 member = convert_to_as_mod(member, address_space, mod);
1566 ctype = member->ctype.base_type;
1568 if (!lvalue_expression(deref)) {
1569 if (deref->type != EXPR_SLICE) {
1570 expr->base = deref;
1571 expr->r_bitpos = 0;
1572 } else {
1573 expr->base = deref->base;
1574 expr->r_bitpos = deref->r_bitpos;
1576 expr->r_bitpos += offset << 3;
1577 expr->type = EXPR_SLICE;
1578 expr->r_nrbits = member->bit_size;
1579 expr->r_bitpos += member->bit_offset;
1580 expr->ctype = member;
1581 return member;
1584 deref = deref->unop;
1585 expr->deref = deref;
1587 add = evaluate_offset(deref, offset);
1588 expr->type = EXPR_PREOP;
1589 expr->op = '*';
1590 expr->unop = add;
1592 expr->ctype = member;
1593 return member;
1596 static int is_promoted(struct expression *expr)
1598 while (1) {
1599 switch (expr->type) {
1600 case EXPR_BINOP:
1601 case EXPR_SELECT:
1602 case EXPR_CONDITIONAL:
1603 return 1;
1604 case EXPR_COMMA:
1605 expr = expr->right;
1606 continue;
1607 case EXPR_PREOP:
1608 switch (expr->op) {
1609 case '(':
1610 expr = expr->unop;
1611 continue;
1612 case '+':
1613 case '-':
1614 case '~':
1615 return 1;
1616 default:
1617 return 0;
1619 default:
1620 return 0;
1626 static struct symbol *evaluate_cast(struct expression *);
1628 static struct symbol *evaluate_type_information(struct expression *expr)
1630 struct symbol *sym = expr->cast_type;
1631 if (!sym) {
1632 sym = evaluate_expression(expr->cast_expression);
1633 if (!sym)
1634 return NULL;
1636 * Expressions of restricted types will possibly get
1637 * promoted - check that here
1639 if (is_restricted_type(sym)) {
1640 if (sym->bit_size < bits_in_int && is_promoted(expr))
1641 sym = &int_ctype;
1644 examine_symbol_type(sym);
1645 if (is_bitfield_type(sym)) {
1646 warning(expr->pos, "trying to examine bitfield type");
1647 return NULL;
1649 return sym;
1652 static struct symbol *evaluate_sizeof(struct expression *expr)
1654 struct symbol *type;
1655 int size;
1657 type = evaluate_type_information(expr);
1658 if (!type)
1659 return NULL;
1661 size = type->bit_size;
1662 if (size & 7)
1663 warning(expr->pos, "cannot size expression");
1664 expr->type = EXPR_VALUE;
1665 expr->value = size >> 3;
1666 expr->ctype = size_t_ctype;
1667 return size_t_ctype;
1670 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1672 struct symbol *type;
1673 int size;
1675 type = evaluate_type_information(expr);
1676 if (!type)
1677 return NULL;
1679 if (type->type == SYM_NODE)
1680 type = type->ctype.base_type;
1681 if (!type)
1682 return NULL;
1683 switch (type->type) {
1684 case SYM_ARRAY:
1685 break;
1686 case SYM_PTR:
1687 type = type->ctype.base_type;
1688 if (type)
1689 break;
1690 default:
1691 warning(expr->pos, "expected pointer expression");
1692 return NULL;
1694 size = type->bit_size;
1695 if (size & 7)
1696 size = 0;
1697 expr->type = EXPR_VALUE;
1698 expr->value = size >> 3;
1699 expr->ctype = size_t_ctype;
1700 return size_t_ctype;
1703 static struct symbol *evaluate_alignof(struct expression *expr)
1705 struct symbol *type;
1707 type = evaluate_type_information(expr);
1708 if (!type)
1709 return NULL;
1711 expr->type = EXPR_VALUE;
1712 expr->value = type->ctype.alignment;
1713 expr->ctype = size_t_ctype;
1714 return size_t_ctype;
1717 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1719 struct expression *expr;
1720 struct symbol_list *argument_types = fn->arguments;
1721 struct symbol *argtype;
1722 int i = 1;
1724 PREPARE_PTR_LIST(argument_types, argtype);
1725 FOR_EACH_PTR (head, expr) {
1726 struct expression **p = THIS_ADDRESS(expr);
1727 struct symbol *ctype, *target;
1728 ctype = evaluate_expression(expr);
1730 if (!ctype)
1731 return 0;
1733 ctype = degenerate(expr);
1735 target = argtype;
1736 if (!target && ctype->bit_size < bits_in_int)
1737 target = &int_ctype;
1738 if (target) {
1739 static char where[30];
1740 examine_symbol_type(target);
1741 sprintf(where, "argument %d", i);
1742 compatible_assignment_types(expr, target, p, ctype, where, '=');
1745 i++;
1746 NEXT_PTR_LIST(argtype);
1747 } END_FOR_EACH_PTR(expr);
1748 FINISH_PTR_LIST(argtype);
1749 return 1;
1752 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1754 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1756 struct expression *entry = *ep;
1757 struct expression **parent, *reuse = NULL;
1758 unsigned long offset;
1759 struct symbol *sym;
1760 unsigned long from, to;
1761 int accept_string = is_byte_type(ctype);
1763 from = current;
1764 to = from+1;
1765 parent = ep;
1766 if (entry->type == EXPR_INDEX) {
1767 from = entry->idx_from;
1768 to = entry->idx_to+1;
1769 parent = &entry->idx_expression;
1770 reuse = entry;
1771 entry = entry->idx_expression;
1774 offset = from * (ctype->bit_size>>3);
1775 if (offset) {
1776 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1777 reuse->type = EXPR_POS;
1778 reuse->ctype = ctype;
1779 reuse->init_offset = offset;
1780 reuse->init_nr = to - from;
1781 reuse->init_expr = entry;
1782 parent = &reuse->init_expr;
1783 entry = reuse;
1785 *ep = entry;
1787 if (accept_string && entry->type == EXPR_STRING) {
1788 sym = evaluate_expression(entry);
1789 to = from + get_expression_value(sym->array_size);
1790 } else {
1791 evaluate_initializer(ctype, parent);
1793 return to;
1796 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1798 struct expression *entry;
1799 int current = 0;
1801 FOR_EACH_PTR(expr->expr_list, entry) {
1802 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1803 } END_FOR_EACH_PTR(entry);
1806 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1807 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1809 if (expression_list_size(expr->expr_list) != 1) {
1810 warning(expr->pos, "unexpected compound initializer");
1811 return;
1813 evaluate_array_initializer(ctype, expr);
1814 return;
1817 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1819 struct symbol *sym;
1821 FOR_EACH_PTR(ctype->symbol_list, sym) {
1822 if (sym->ident == ident)
1823 return sym;
1824 } END_FOR_EACH_PTR(sym);
1825 return NULL;
1828 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1830 struct expression *entry = *ep;
1831 struct expression **parent;
1832 struct expression *reuse = NULL;
1833 unsigned long offset;
1835 if (!sym) {
1836 error(entry->pos, "unknown named initializer");
1837 return -1;
1840 if (entry->type == EXPR_IDENTIFIER) {
1841 reuse = entry;
1842 entry = entry->ident_expression;
1845 parent = ep;
1846 offset = sym->offset;
1847 if (offset) {
1848 if (!reuse)
1849 reuse = alloc_expression(entry->pos, EXPR_POS);
1850 reuse->type = EXPR_POS;
1851 reuse->ctype = sym;
1852 reuse->init_offset = offset;
1853 reuse->init_nr = 1;
1854 reuse->init_expr = entry;
1855 parent = &reuse->init_expr;
1856 entry = reuse;
1858 *ep = entry;
1859 evaluate_initializer(sym, parent);
1860 return 0;
1863 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1865 struct expression *entry;
1866 struct symbol *sym;
1868 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1869 FOR_EACH_PTR(expr->expr_list, entry) {
1870 if (entry->type == EXPR_IDENTIFIER) {
1871 struct ident *ident = entry->expr_ident;
1872 /* We special-case the "already right place" case */
1873 if (!sym || sym->ident != ident) {
1874 RESET_PTR_LIST(sym);
1875 for (;;) {
1876 if (!sym)
1877 break;
1878 if (sym->ident == ident)
1879 break;
1880 NEXT_PTR_LIST(sym);
1884 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1885 return;
1886 NEXT_PTR_LIST(sym);
1887 } END_FOR_EACH_PTR(entry);
1888 FINISH_PTR_LIST(sym);
1892 * Initializers are kind of like assignments. Except
1893 * they can be a hell of a lot more complex.
1895 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1897 struct expression *expr = *ep;
1900 * Simple non-structure/array initializers are the simple
1901 * case, and look (and parse) largely like assignments.
1903 switch (expr->type) {
1904 default: {
1905 int is_string = expr->type == EXPR_STRING;
1906 struct symbol *rtype = evaluate_expression(expr);
1907 if (rtype) {
1909 * Special case:
1910 * char array[] = "string"
1911 * should _not_ degenerate.
1913 if (!is_string || !is_string_type(ctype))
1914 rtype = degenerate(expr);
1915 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1917 return;
1920 case EXPR_INITIALIZER:
1921 expr->ctype = ctype;
1922 if (ctype->type == SYM_NODE)
1923 ctype = ctype->ctype.base_type;
1925 switch (ctype->type) {
1926 case SYM_ARRAY:
1927 case SYM_PTR:
1928 evaluate_array_initializer(ctype->ctype.base_type, expr);
1929 return;
1930 case SYM_UNION:
1931 evaluate_struct_or_union_initializer(ctype, expr, 0);
1932 return;
1933 case SYM_STRUCT:
1934 evaluate_struct_or_union_initializer(ctype, expr, 1);
1935 return;
1936 default:
1937 evaluate_scalar_initializer(ctype, expr);
1938 return;
1941 case EXPR_IDENTIFIER:
1942 if (ctype->type == SYM_NODE)
1943 ctype = ctype->ctype.base_type;
1944 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1945 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1946 show_symbol(ctype);
1947 return;
1949 evaluate_one_struct_initializer(ctype, ep,
1950 find_struct_ident(ctype, expr->expr_ident));
1951 return;
1953 case EXPR_INDEX:
1954 if (ctype->type == SYM_NODE)
1955 ctype = ctype->ctype.base_type;
1956 if (ctype->type != SYM_ARRAY) {
1957 error(expr->pos, "expected array");
1958 return;
1960 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1961 return;
1963 case EXPR_POS:
1965 * An EXPR_POS expression has already been evaluated, and we don't
1966 * need to do anything more
1968 return;
1972 static int get_as(struct symbol *sym)
1974 int as;
1975 unsigned long mod;
1977 if (!sym)
1978 return 0;
1979 as = sym->ctype.as;
1980 mod = sym->ctype.modifiers;
1981 if (sym->type == SYM_NODE) {
1982 sym = sym->ctype.base_type;
1983 as |= sym->ctype.as;
1984 mod |= sym->ctype.modifiers;
1988 * At least for now, allow casting to a "unsigned long".
1989 * That's how we do things like pointer arithmetic and
1990 * store pointers to registers.
1992 if (sym == &ulong_ctype)
1993 return -1;
1995 if (sym && sym->type == SYM_PTR) {
1996 sym = sym->ctype.base_type;
1997 as |= sym->ctype.as;
1998 mod |= sym->ctype.modifiers;
2000 if (mod & MOD_FORCE)
2001 return -1;
2002 return as;
2005 static struct symbol *evaluate_cast(struct expression *expr)
2007 struct expression *target = expr->cast_expression;
2008 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2009 enum type type;
2011 if (!target)
2012 return NULL;
2014 expr->ctype = ctype;
2015 expr->cast_type = ctype;
2018 * Special case: a cast can be followed by an
2019 * initializer, in which case we need to pass
2020 * the type value down to that initializer rather
2021 * than trying to evaluate it as an expression
2023 * A more complex case is when the initializer is
2024 * dereferenced as part of a post-fix expression.
2025 * We need to produce an expression that can be dereferenced.
2027 if (target->type == EXPR_INITIALIZER) {
2028 struct symbol *sym = expr->cast_type;
2029 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2031 sym->initializer = expr->cast_expression;
2032 evaluate_symbol(sym);
2034 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2035 addr->symbol = sym;
2037 expr->type = EXPR_PREOP;
2038 expr->op = '*';
2039 expr->unop = addr;
2040 expr->ctype = sym;
2042 return sym;
2045 evaluate_expression(target);
2046 degenerate(target);
2049 * You can always throw a value away by casting to
2050 * "void" - that's an implicit "force". Note that
2051 * the same is _not_ true of "void *".
2053 if (ctype == &void_ctype)
2054 goto out;
2056 type = ctype->type;
2057 if (type == SYM_NODE) {
2058 type = ctype->ctype.base_type->type;
2059 if (ctype->ctype.base_type == &void_ctype)
2060 goto out;
2062 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2063 warning(expr->pos, "cast to non-scalar");
2065 if (!target->ctype) {
2066 warning(expr->pos, "cast from unknown type");
2067 goto out;
2070 type = target->ctype->type;
2071 if (type == SYM_NODE)
2072 type = target->ctype->ctype.base_type->type;
2073 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2074 warning(expr->pos, "cast from non-scalar");
2076 if (!get_as(ctype) && get_as(target->ctype) > 0)
2077 warning(expr->pos, "cast removes address space of expression");
2079 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2080 struct symbol *t1 = ctype, *t2 = target->ctype;
2081 if (t1->type == SYM_NODE)
2082 t1 = t1->ctype.base_type;
2083 if (t2->type == SYM_NODE)
2084 t2 = t2->ctype.base_type;
2085 if (t1 != t2) {
2086 if (t1->type == SYM_RESTRICT)
2087 warning(expr->pos, "cast to restricted type");
2088 if (t2->type == SYM_RESTRICT)
2089 warning(expr->pos, "cast from restricted type");
2094 * Casts of constant values are special: they
2095 * can be NULL, and thus need to be simplified
2096 * early.
2098 if (target->type == EXPR_VALUE)
2099 cast_value(expr, ctype, target, target->ctype);
2101 out:
2102 return ctype;
2106 * Evaluate a call expression with a symbol. This
2107 * should expand inline functions, and evaluate
2108 * builtins.
2110 static int evaluate_symbol_call(struct expression *expr)
2112 struct expression *fn = expr->fn;
2113 struct symbol *ctype = fn->ctype;
2115 if (fn->type != EXPR_PREOP)
2116 return 0;
2118 if (ctype->op && ctype->op->evaluate)
2119 return ctype->op->evaluate(expr);
2121 if (ctype->ctype.modifiers & MOD_INLINE) {
2122 int ret;
2123 struct symbol *curr = current_fn;
2124 current_fn = ctype->ctype.base_type;
2125 examine_fn_arguments(current_fn);
2127 ret = inline_function(expr, ctype);
2129 /* restore the old function */
2130 current_fn = curr;
2131 return ret;
2134 return 0;
2137 static struct symbol *evaluate_call(struct expression *expr)
2139 int args, fnargs;
2140 struct symbol *ctype, *sym;
2141 struct expression *fn = expr->fn;
2142 struct expression_list *arglist = expr->args;
2144 if (!evaluate_expression(fn))
2145 return NULL;
2146 sym = ctype = fn->ctype;
2147 if (ctype->type == SYM_NODE)
2148 ctype = ctype->ctype.base_type;
2149 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2150 ctype = ctype->ctype.base_type;
2151 if (!evaluate_arguments(sym, ctype, arglist))
2152 return NULL;
2153 if (ctype->type != SYM_FN) {
2154 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2155 return NULL;
2157 args = expression_list_size(expr->args);
2158 fnargs = symbol_list_size(ctype->arguments);
2159 if (args < fnargs)
2160 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2161 if (args > fnargs && !ctype->variadic)
2162 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2163 if (sym->type == SYM_NODE) {
2164 if (evaluate_symbol_call(expr))
2165 return expr->ctype;
2167 expr->ctype = ctype->ctype.base_type;
2168 return expr->ctype;
2171 struct symbol *evaluate_expression(struct expression *expr)
2173 if (!expr)
2174 return NULL;
2175 if (expr->ctype)
2176 return expr->ctype;
2178 switch (expr->type) {
2179 case EXPR_VALUE:
2180 case EXPR_FVALUE:
2181 warning(expr->pos, "value expression without a type");
2182 return NULL;
2183 case EXPR_STRING:
2184 return evaluate_string(expr);
2185 case EXPR_SYMBOL:
2186 return evaluate_symbol_expression(expr);
2187 case EXPR_BINOP:
2188 if (!evaluate_expression(expr->left))
2189 return NULL;
2190 if (!evaluate_expression(expr->right))
2191 return NULL;
2192 return evaluate_binop(expr);
2193 case EXPR_LOGICAL:
2194 return evaluate_logical(expr);
2195 case EXPR_COMMA:
2196 evaluate_expression(expr->left);
2197 if (!evaluate_expression(expr->right))
2198 return NULL;
2199 return evaluate_comma(expr);
2200 case EXPR_COMPARE:
2201 if (!evaluate_expression(expr->left))
2202 return NULL;
2203 if (!evaluate_expression(expr->right))
2204 return NULL;
2205 return evaluate_compare(expr);
2206 case EXPR_ASSIGNMENT:
2207 if (!evaluate_expression(expr->left))
2208 return NULL;
2209 if (!evaluate_expression(expr->right))
2210 return NULL;
2211 return evaluate_assignment(expr);
2212 case EXPR_PREOP:
2213 if (!evaluate_expression(expr->unop))
2214 return NULL;
2215 return evaluate_preop(expr);
2216 case EXPR_POSTOP:
2217 if (!evaluate_expression(expr->unop))
2218 return NULL;
2219 return evaluate_postop(expr);
2220 case EXPR_CAST:
2221 case EXPR_IMPLIED_CAST:
2222 return evaluate_cast(expr);
2223 case EXPR_SIZEOF:
2224 return evaluate_sizeof(expr);
2225 case EXPR_PTRSIZEOF:
2226 return evaluate_ptrsizeof(expr);
2227 case EXPR_ALIGNOF:
2228 return evaluate_alignof(expr);
2229 case EXPR_DEREF:
2230 return evaluate_member_dereference(expr);
2231 case EXPR_CALL:
2232 return evaluate_call(expr);
2233 case EXPR_SELECT:
2234 case EXPR_CONDITIONAL:
2235 return evaluate_conditional_expression(expr);
2236 case EXPR_STATEMENT:
2237 expr->ctype = evaluate_statement(expr->statement);
2238 return expr->ctype;
2240 case EXPR_LABEL:
2241 expr->ctype = &ptr_ctype;
2242 return &ptr_ctype;
2244 case EXPR_TYPE:
2245 /* Evaluate the type of the symbol .. */
2246 evaluate_symbol(expr->symbol);
2247 /* .. but the type of the _expression_ is a "type" */
2248 expr->ctype = &type_ctype;
2249 return &type_ctype;
2251 /* These can not exist as stand-alone expressions */
2252 case EXPR_INITIALIZER:
2253 case EXPR_IDENTIFIER:
2254 case EXPR_INDEX:
2255 case EXPR_POS:
2256 warning(expr->pos, "internal front-end error: initializer in expression");
2257 return NULL;
2258 case EXPR_SLICE:
2259 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2260 return NULL;
2262 return NULL;
2265 static void check_duplicates(struct symbol *sym)
2267 struct symbol *next = sym;
2269 while ((next = next->same_symbol) != NULL) {
2270 const char *typediff;
2271 evaluate_symbol(next);
2272 typediff = type_difference(sym, next, 0, 0);
2273 if (typediff) {
2274 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2275 show_ident(sym->ident),
2276 input_streams[next->pos.stream].name, next->pos.line, typediff);
2277 return;
2282 static struct symbol *evaluate_symbol(struct symbol *sym)
2284 struct symbol *base_type;
2286 if (!sym)
2287 return sym;
2289 sym = examine_symbol_type(sym);
2290 base_type = sym->ctype.base_type;
2291 if (!base_type)
2292 return NULL;
2294 /* Evaluate the initializers */
2295 if (sym->initializer)
2296 evaluate_initializer(sym, &sym->initializer);
2298 /* And finally, evaluate the body of the symbol too */
2299 if (base_type->type == SYM_FN) {
2300 struct symbol *curr = current_fn;
2302 current_fn = base_type;
2304 examine_fn_arguments(base_type);
2305 if (!base_type->stmt && base_type->inline_stmt)
2306 uninline(sym);
2307 if (base_type->stmt)
2308 evaluate_statement(base_type->stmt);
2310 current_fn = curr;
2313 return base_type;
2316 void evaluate_symbol_list(struct symbol_list *list)
2318 struct symbol *sym;
2320 FOR_EACH_PTR(list, sym) {
2321 check_duplicates(sym);
2322 evaluate_symbol(sym);
2323 } END_FOR_EACH_PTR(sym);
2326 static struct symbol *evaluate_return_expression(struct statement *stmt)
2328 struct expression *expr = stmt->expression;
2329 struct symbol *ctype, *fntype;
2331 evaluate_expression(expr);
2332 ctype = degenerate(expr);
2333 fntype = current_fn->ctype.base_type;
2334 if (!fntype || fntype == &void_ctype) {
2335 if (expr && ctype != &void_ctype)
2336 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2337 return NULL;
2340 if (!expr) {
2341 warning(stmt->pos, "return with no return value");
2342 return NULL;
2344 if (!ctype)
2345 return NULL;
2346 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2347 return NULL;
2350 static void evaluate_if_statement(struct statement *stmt)
2352 if (!stmt->if_conditional)
2353 return;
2355 evaluate_conditional(stmt->if_conditional, 0);
2356 evaluate_statement(stmt->if_true);
2357 evaluate_statement(stmt->if_false);
2360 static void evaluate_iterator(struct statement *stmt)
2362 evaluate_conditional(stmt->iterator_pre_condition, 1);
2363 evaluate_conditional(stmt->iterator_post_condition,1);
2364 evaluate_statement(stmt->iterator_pre_statement);
2365 evaluate_statement(stmt->iterator_statement);
2366 evaluate_statement(stmt->iterator_post_statement);
2369 struct symbol *evaluate_statement(struct statement *stmt)
2371 if (!stmt)
2372 return NULL;
2374 switch (stmt->type) {
2375 case STMT_RETURN:
2376 return evaluate_return_expression(stmt);
2378 case STMT_EXPRESSION:
2379 if (!evaluate_expression(stmt->expression))
2380 return NULL;
2381 return degenerate(stmt->expression);
2383 case STMT_COMPOUND: {
2384 struct statement *s;
2385 struct symbol *type = NULL;
2386 struct symbol *sym;
2388 /* Evaluate each symbol in the compound statement */
2389 FOR_EACH_PTR(stmt->syms, sym) {
2390 evaluate_symbol(sym);
2391 } END_FOR_EACH_PTR(sym);
2392 evaluate_symbol(stmt->ret);
2395 * Then, evaluate each statement, making the type of the
2396 * compound statement be the type of the last statement
2398 type = NULL;
2399 FOR_EACH_PTR(stmt->stmts, s) {
2400 type = evaluate_statement(s);
2401 } END_FOR_EACH_PTR(s);
2402 if (!type)
2403 type = &void_ctype;
2404 return type;
2406 case STMT_IF:
2407 evaluate_if_statement(stmt);
2408 return NULL;
2409 case STMT_ITERATOR:
2410 evaluate_iterator(stmt);
2411 return NULL;
2412 case STMT_SWITCH:
2413 evaluate_expression(stmt->switch_expression);
2414 evaluate_statement(stmt->switch_statement);
2415 return NULL;
2416 case STMT_CASE:
2417 evaluate_expression(stmt->case_expression);
2418 evaluate_expression(stmt->case_to);
2419 evaluate_statement(stmt->case_statement);
2420 return NULL;
2421 case STMT_LABEL:
2422 return evaluate_statement(stmt->label_statement);
2423 case STMT_GOTO:
2424 evaluate_expression(stmt->goto_expression);
2425 return NULL;
2426 case STMT_NONE:
2427 break;
2428 case STMT_ASM:
2429 /* FIXME! Do the asm parameter evaluation! */
2430 break;
2431 case STMT_INTERNAL:
2432 evaluate_expression(stmt->expression);
2433 return NULL;
2435 return NULL;