Make functions degenerate to function pointers, and fix the
[smatch.git] / evaluate.c
blobc205107f1e623c8d0a8e84c694937293d89706df
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
8 * Evaluate constant expressions.
9 */
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <limits.h>
20 #include "lib.h"
21 #include "parse.h"
22 #include "token.h"
23 #include "symbol.h"
24 #include "target.h"
25 #include "expression.h"
27 static struct symbol *current_fn;
28 static int current_context, current_contextmask;
30 static struct symbol *evaluate_symbol_expression(struct expression *expr)
32 struct symbol *sym = expr->symbol;
33 struct symbol *base_type;
35 if (!sym) {
36 if (preprocessing) {
37 warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
38 expr->type = EXPR_VALUE;
39 expr->value = 0;
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (base_type->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = &ptr_ctype;
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 return sym;
76 static struct symbol *evaluate_string(struct expression *expr)
78 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
79 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
80 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
81 int length = expr->string->length;
83 sym->array_size = length;
84 sym->bit_size = BITS_IN_CHAR * length;
85 sym->ctype.alignment = 1;
86 sym->ctype.modifiers = MOD_STATIC;
87 sym->ctype.base_type = array;
89 array->array_size = length;
90 array->bit_size = BITS_IN_CHAR * length;
91 array->ctype.alignment = 1;
92 array->ctype.modifiers = MOD_STATIC;
93 array->ctype.base_type = &char_ctype;
95 addr->symbol = sym;
96 addr->ctype = &ptr_ctype;
98 expr->type = EXPR_PREOP;
99 expr->op = '*';
100 expr->unop = addr;
101 expr->ctype = sym;
102 return sym;
105 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
107 unsigned long lmod, rmod, mod;
109 if (left == right)
110 return left;
112 if (left->bit_size > right->bit_size)
113 return left;
115 if (right->bit_size > left->bit_size)
116 return right;
118 /* Same size integers - promote to unsigned, promote to long */
119 lmod = left->ctype.modifiers;
120 rmod = right->ctype.modifiers;
121 mod = lmod | rmod;
122 if (mod == lmod)
123 return left;
124 if (mod == rmod)
125 return right;
126 return ctype_integer(mod);
129 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
130 struct expression *old, struct symbol *oldtype)
132 int old_size = oldtype->bit_size;
133 int new_size = newtype->bit_size;
134 long long value, mask, ormask, andmask;
135 int is_signed;
137 // FIXME! We don't handle FP casts of constant values yet
138 if (newtype->ctype.base_type == &fp_type)
139 return NULL;
140 if (oldtype->ctype.base_type == &fp_type)
141 return NULL;
143 // For pointers and integers, we can just move the value around
144 expr->type = EXPR_VALUE;
145 if (old_size == new_size) {
146 expr->value = old->value;
147 return newtype;
150 // expand it to the full "long long" value
151 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
152 mask = 1ULL << (old_size-1);
153 value = old->value;
154 if (!(value & mask))
155 is_signed = 0;
156 andmask = mask | (mask-1);
157 ormask = ~andmask;
158 if (!is_signed)
159 ormask = 0;
160 value = (value & andmask) | ormask;
162 // Truncate it to the new size
163 mask = 1ULL << (new_size-1);
164 mask = mask | (mask-1);
165 expr->value = value & mask;
166 return newtype;
169 static struct expression * cast_to(struct expression *old, struct symbol *type)
171 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
172 expr->ctype = type;
173 expr->cast_type = type;
174 expr->cast_expression = old;
175 if (old->type == EXPR_VALUE)
176 cast_value(expr, type, old, old->ctype);
177 return expr;
180 static int is_ptr_type(struct symbol *type)
182 if (type->type == SYM_NODE)
183 type = type->ctype.base_type;
184 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
187 static int is_int_type(struct symbol *type)
189 if (type->type == SYM_NODE)
190 type = type->ctype.base_type;
191 return type->ctype.base_type == &int_type;
194 static struct symbol *bad_expr_type(struct expression *expr)
196 warn(expr->pos, "incompatible types for operation");
197 return NULL;
200 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
202 struct expression *left = *lp, *right = *rp;
203 struct symbol *ltype = left->ctype, *rtype = right->ctype;
205 if (ltype->type == SYM_NODE)
206 ltype = ltype->ctype.base_type;
207 if (rtype->type == SYM_NODE)
208 rtype = rtype->ctype.base_type;
209 /* Integer promotion? */
210 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
211 ltype = &int_ctype;
212 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
213 rtype = &int_ctype;
214 if (is_int_type(ltype) && is_int_type(rtype)) {
215 struct symbol *ctype = bigger_int_type(ltype, rtype);
217 /* Don't bother promoting same-size entities, it only adds clutter */
218 if (ltype->bit_size != ctype->bit_size)
219 *lp = cast_to(left, ctype);
220 if (rtype->bit_size != ctype->bit_size)
221 *rp = cast_to(right, ctype);
222 return ctype;
224 return NULL;
227 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
229 if (count >= ctype->bit_size) {
230 warn(expr->pos, "shift too big for type");
231 count &= ctype->bit_size-1;
233 return count;
237 * CAREFUL! We need to get the size and sign of the
238 * result right!
240 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
242 struct expression *left = expr->left, *right = expr->right;
243 unsigned long long v, l, r, mask;
244 signed long long s, sl, sr;
245 int is_signed, shift;
247 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
248 return;
249 l = left->value; r = right->value;
250 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
251 mask = 1ULL << (ctype->bit_size-1);
252 sl = l; sr = r;
253 if (is_signed && (sl & mask))
254 sl |= ~(mask-1);
255 if (is_signed && (sr & mask))
256 sr |= ~(mask-1);
258 switch (expr->op) {
259 case '+': v = l + r; s = v; break;
260 case '-': v = l - r; s = v; break;
261 case '&': v = l & r; s = v; break;
262 case '|': v = l | r; s = v; break;
263 case '^': v = l ^ r; s = v; break;
264 case '*': v = l * r; s = sl * sr; break;
265 case '/': if (!r) return; v = l / r; s = sl / sr; break;
266 case '%': if (!r) return; v = l % r; s = sl % sr; break;
267 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
268 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
269 case '<': v = l < r; s = sl < sr; break;
270 case '>': v = l > r; s = sl > sr; break;
271 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
272 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
273 case SPECIAL_EQUAL: v = l == r; s = v; break;
274 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
275 default: return;
277 if (is_signed)
278 v = s;
279 mask = mask | (mask-1);
280 expr->value = v & mask;
281 expr->type = EXPR_VALUE;
284 static struct symbol *evaluate_int_binop(struct expression *expr)
286 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
287 if (ctype) {
288 expr->ctype = ctype;
289 simplify_int_binop(expr, ctype);
290 return ctype;
292 return bad_expr_type(expr);
295 static inline int lvalue_expression(struct expression *expr)
297 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
300 /* Arrays degenerate into pointers on pointer arithmetic */
301 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
303 struct symbol *base = ctype;
305 if (ctype->type == SYM_NODE)
306 base = ctype->ctype.base_type;
307 if (base->type == SYM_ARRAY || base->type == SYM_FN) {
308 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
309 struct expression *ptr;
311 merge_type(sym, ctype);
312 if (base->type == SYM_FN)
313 base = ctype;
314 merge_type(sym, base);
315 sym->bit_size = BITS_IN_POINTER;
316 ctype = sym;
318 ptr = *ptr_p;
319 *ptr_p = ptr->unop;
322 * This all really assumes that we got the degenerate
323 * array as an lvalue (ie a dereference). If that
324 * is not the case, then holler - because we've screwed
325 * up.
327 if (!lvalue_expression(ptr))
328 warn(ptr->pos, "internal error: strange degenerate array case");
329 ptr->ctype = base;
331 return ctype;
334 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
336 struct symbol *ctype;
337 struct symbol *ptr_type = ptr->ctype;
338 struct symbol *i_type = i->ctype;
339 int bit_size;
341 if (i_type->type == SYM_NODE)
342 i_type = i_type->ctype.base_type;
343 if (ptr_type->type == SYM_NODE)
344 ptr_type = ptr_type->ctype.base_type;
346 if (i_type->type == SYM_ENUM)
347 i_type = &int_ctype;
348 if (!is_int_type(i_type))
349 return bad_expr_type(expr);
351 ctype = ptr->ctype;
352 examine_symbol_type(ctype);
354 ctype = degenerate(expr, ctype, &ptr);
355 bit_size = ctype->bit_size;
357 /* Special case: adding zero commonly happens as a result of 'array[0]' */
358 if (i->type == EXPR_VALUE && !i->value) {
359 *expr = *ptr;
360 } else if (bit_size > BITS_IN_CHAR) {
361 struct expression *add = expr;
362 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
363 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
365 val->ctype = size_t_ctype;
366 val->value = bit_size >> 3;
368 mul->op = '*';
369 mul->ctype = size_t_ctype;
370 mul->left = i;
371 mul->right = val;
372 simplify_int_binop(mul, size_t_ctype);
374 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
375 add->left = ptr;
376 add->right = mul;
377 simplify_int_binop(add, ctype);
380 expr->ctype = ctype;
381 return ctype;
384 static struct symbol *evaluate_add(struct expression *expr)
386 struct expression *left = expr->left, *right = expr->right;
387 struct symbol *ltype = left->ctype, *rtype = right->ctype;
389 if (is_ptr_type(ltype))
390 return evaluate_ptr_add(expr, left, right);
392 if (is_ptr_type(rtype))
393 return evaluate_ptr_add(expr, right, left);
395 // FIXME! FP promotion
396 return evaluate_int_binop(expr);
399 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
400 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
402 static const char * type_difference(struct symbol *target, struct symbol *source,
403 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
405 for (;;) {
406 unsigned long mod1, mod2, diff;
407 unsigned long as1, as2;
409 if (target == source)
410 break;
411 if (!target || !source)
412 return "different types";
414 * Peel of per-node information.
415 * FIXME! Check alignment, address space, and context too here!
417 if (target->type == SYM_NODE)
418 target = target->ctype.base_type;
419 if (source->type == SYM_NODE)
420 source = source->ctype.base_type;
421 mod1 = target->ctype.modifiers;
422 as1 = target->ctype.as;
423 mod2 = source->ctype.modifiers;
424 as2 = source->ctype.as;
426 if (target->type != source->type) {
427 int type1 = target->type;
428 int type2 = source->type;
430 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
431 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
432 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
433 if (type1 != type2)
434 return "different base types";
437 /* Must be same address space to be comparable */
438 if (as1 != as2)
439 return "different address spaces";
441 /* Ignore differences in storage types, sign, or addressability */
442 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
443 if (diff) {
444 mod1 &= diff & ~target_mod_ignore;
445 mod2 &= diff & ~source_mod_ignore;
446 if (mod1 | mod2) {
447 if ((mod1 | mod2) & MOD_SIZE)
448 return "different type sizes";
449 return "different modifiers";
453 if (target->type == SYM_FN) {
454 int i;
455 struct symbol *arg1, *arg2;
456 if (target->variadic != source->variadic)
457 return "incompatible variadic arguments";
458 PREPARE_PTR_LIST(target->arguments, arg1);
459 PREPARE_PTR_LIST(source->arguments, arg2);
460 i = 1;
461 for (;;) {
462 if (type_difference(arg1, arg2, 0, 0)) {
463 static char argdiff[30];
464 sprintf(argdiff, "incompatible argument %d", i);
465 return argdiff;
467 if (!arg1)
468 break;
469 NEXT_PTR_LIST(arg1);
470 NEXT_PTR_LIST(arg2);
471 i++;
473 FINISH_PTR_LIST(arg2);
474 FINISH_PTR_LIST(arg1);
477 target = target->ctype.base_type;
478 source = source->ctype.base_type;
480 return NULL;
483 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
485 /* NULL expression? Just return the type of the "other side" */
486 if (r->type == EXPR_VALUE && !r->value)
487 return l->ctype;
488 if (l->type == EXPR_VALUE && !l->value)
489 return r->ctype;
490 return NULL;
494 * Ignore differences in "volatile" and "const"ness when
495 * subtracting pointers
497 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
499 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
501 const char *typediff;
502 struct symbol *ctype;
503 struct symbol *ltype = l->ctype, *rtype = r->ctype;
506 * If it is an integer subtract: the ptr add case will do the
507 * right thing.
509 if (!is_ptr_type(rtype))
510 return evaluate_ptr_add(expr, l, r);
512 ctype = ltype;
513 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
514 if (typediff) {
515 ctype = common_ptr_type(l, r);
516 if (!ctype) {
517 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
518 return NULL;
521 examine_symbol_type(ctype);
523 /* Figure out the base type we point to */
524 if (ctype->type == SYM_NODE)
525 ctype = ctype->ctype.base_type;
526 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
527 warn(expr->pos, "subtraction of functions? Share your drugs");
528 return NULL;
530 ctype = ctype->ctype.base_type;
532 expr->ctype = ssize_t_ctype;
533 if (ctype->bit_size > BITS_IN_CHAR) {
534 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
535 struct expression *div = expr;
536 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
538 val->ctype = size_t_ctype;
539 val->value = ctype->bit_size >> 3;
541 sub->op = '-';
542 sub->ctype = ssize_t_ctype;
543 sub->left = l;
544 sub->right = r;
546 div->op = '/';
547 div->left = sub;
548 div->right = val;
551 return ssize_t_ctype;
554 static struct symbol *evaluate_sub(struct expression *expr)
556 struct expression *left = expr->left, *right = expr->right;
557 struct symbol *ltype = left->ctype;
559 if (is_ptr_type(ltype))
560 return evaluate_ptr_sub(expr, left, right);
562 // FIXME! FP promotion
563 return evaluate_int_binop(expr);
566 static struct symbol *evaluate_logical(struct expression *expr)
568 struct expression *left = expr->left;
569 struct expression *right;
571 if (!evaluate_expression(left))
572 return NULL;
574 /* Do immediate short-circuiting ... */
575 if (left->type == EXPR_VALUE) {
576 if (expr->op == SPECIAL_LOGICAL_AND) {
577 if (!left->value) {
578 expr->type = EXPR_VALUE;
579 expr->value = 0;
580 expr->ctype = &bool_ctype;
581 return &bool_ctype;
583 } else {
584 if (left->value) {
585 expr->type = EXPR_VALUE;
586 expr->value = 1;
587 expr->ctype = &bool_ctype;
588 return &bool_ctype;
593 right = expr->right;
594 if (!evaluate_expression(right))
595 return NULL;
596 expr->ctype = &bool_ctype;
597 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
599 * We know the left value doesn't matter, since
600 * otherwise we would have short-circuited it..
602 expr->type = EXPR_VALUE;
603 expr->value = right->value;
606 return &bool_ctype;
609 static struct symbol *evaluate_arithmetic(struct expression *expr)
611 // FIXME! Floating-point promotion!
612 return evaluate_int_binop(expr);
615 static struct symbol *evaluate_binop(struct expression *expr)
617 switch (expr->op) {
618 // addition can take ptr+int, fp and int
619 case '+':
620 return evaluate_add(expr);
622 // subtraction can take ptr-ptr, fp and int
623 case '-':
624 return evaluate_sub(expr);
626 // Arithmetic operations can take fp and int
627 case '*': case '/': case '%':
628 return evaluate_arithmetic(expr);
630 // The rest are integer operations (bitops)
631 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
632 // '&', '^', '|'
633 default:
634 return evaluate_int_binop(expr);
638 static struct symbol *evaluate_comma(struct expression *expr)
640 expr->ctype = expr->right->ctype;
641 return expr->ctype;
644 static struct symbol *evaluate_compare(struct expression *expr)
646 struct expression *left = expr->left, *right = expr->right;
647 struct symbol *ltype = left->ctype, *rtype = right->ctype;
648 struct symbol *ctype;
650 /* Pointer types? */
651 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
652 expr->ctype = &bool_ctype;
653 // FIXME! Check the types for compatibility
654 return &bool_ctype;
657 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
658 if (ctype) {
659 simplify_int_binop(expr, ctype);
660 expr->ctype = &bool_ctype;
661 return &bool_ctype;
664 return bad_expr_type(expr);
667 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
669 /* Integer promotion? */
670 if (ltype->type == SYM_NODE)
671 ltype = ltype->ctype.base_type;
672 if (rtype->type == SYM_NODE)
673 rtype = rtype->ctype.base_type;
674 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
675 ltype = &int_ctype;
676 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
677 rtype = &int_ctype;
678 return (is_int_type(ltype) && is_int_type(rtype));
681 static int is_void_ptr(struct expression *expr)
683 return (expr->type == EXPR_VALUE &&
684 expr->value == 0);
688 * FIXME!! This shoul ddo casts, array degeneration etc..
690 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
692 struct symbol *ltype = left->ctype, *rtype = right->ctype;
694 if (ltype->type == SYM_PTR) {
695 if (is_void_ptr(right))
696 return ltype;
699 if (rtype->type == SYM_PTR) {
700 if (is_void_ptr(left))
701 return rtype;
703 return NULL;
706 static struct symbol *do_degenerate(struct expression **ep)
708 struct expression *expr = *ep;
709 return degenerate(expr, expr->ctype, ep);
712 static struct symbol * evaluate_conditional(struct expression *expr)
714 struct expression *cond, *true, *false;
715 struct symbol *ctype, *ltype, *rtype;
716 const char * typediff;
718 ctype = do_degenerate(&expr->conditional);
719 cond = expr->conditional;
721 ltype = ctype;
722 true = cond;
723 if (expr->cond_true) {
724 ltype = do_degenerate(&expr->cond_true);
725 true = expr->cond_true;
728 rtype = do_degenerate(&expr->cond_false);
729 false = expr->cond_false;
731 ctype = ltype;
732 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
733 if (typediff) {
734 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
735 if (!ctype) {
736 ctype = compatible_ptr_type(true, expr->cond_false);
737 if (!ctype) {
738 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
739 return NULL;
744 /* Simplify conditional expression.. */
745 if (cond->type == EXPR_VALUE) {
746 if (!cond->value)
747 true = false;
748 *expr = *true;
750 expr->ctype = ctype;
751 return ctype;
754 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
755 struct expression **rp, struct symbol *source, const char *where)
757 const char *typediff;
758 struct symbol *t;
759 int target_as;
761 /* It's ok if the target is more volatile or const than the source */
762 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
763 if (!typediff)
764 return 1;
766 if (compatible_integer_types(target, source)) {
767 if (target->bit_size != source->bit_size)
768 *rp = cast_to(*rp, target);
769 return 1;
772 /* Pointer destination? */
773 t = target;
774 target_as = t->ctype.as;
775 if (t->type == SYM_NODE) {
776 t = t->ctype.base_type;
777 target_as |= t->ctype.as;
779 if (t->type == SYM_PTR) {
780 struct expression *right = *rp;
781 struct symbol *s = source;
782 int source_as;
784 // NULL pointer is always ok
785 if (right->type == EXPR_VALUE && !right->value)
786 return 1;
788 /* "void *" matches anything as long as the address space is ok */
789 source_as = s->ctype.as;
790 if (s->type == SYM_NODE) {
791 s = s->ctype.base_type;
792 source_as |= s->ctype.as;
794 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
795 s = s->ctype.base_type;
796 t = t->ctype.base_type;
797 if (s == &void_ctype || t == &void_ctype)
798 return 1;
802 // FIXME!! Cast it?
803 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
804 warn(expr->pos, " expected %s", show_typename(target));
805 warn(expr->pos, " got %s", show_typename(source));
806 return 0;
810 * FIXME!! This is wrong from a double evaluation standpoint. We can't
811 * just expand the expression twice, that would make any side effects
812 * happen twice too.
814 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
816 int op = expr->op;
817 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
818 static const int op_trans[] = {
819 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
820 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
821 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
822 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
823 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
824 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
825 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
826 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
827 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
828 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
831 subexpr->left = left;
832 subexpr->right = right;
833 subexpr->op = op_trans[op - SPECIAL_BASE];
834 expr->op = '=';
835 expr->right = subexpr;
836 return evaluate_binop(subexpr);
839 static struct symbol *evaluate_assignment(struct expression *expr)
841 struct expression *left = expr->left, *right = expr->right;
842 struct symbol *ltype, *rtype;
844 ltype = left->ctype;
845 rtype = right->ctype;
846 if (expr->op != '=') {
847 rtype = evaluate_binop_assignment(expr, left, right);
848 if (!rtype)
849 return 0;
850 right = expr->right;
853 if (!lvalue_expression(left)) {
854 warn(expr->pos, "not an lvalue");
855 return NULL;
858 rtype = degenerate(right, rtype, &expr->right);
860 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
861 return 0;
863 expr->ctype = expr->left->ctype;
864 return expr->ctype;
867 static struct symbol *evaluate_addressof(struct expression *expr)
869 struct symbol *ctype, *symbol;
870 struct expression *op = expr->unop;
872 if (op->op != '*' || op->type != EXPR_PREOP) {
873 warn(expr->pos, "not addressable");
874 return NULL;
877 symbol = alloc_symbol(expr->pos, SYM_PTR);
878 symbol->ctype.alignment = POINTER_ALIGNMENT;
879 symbol->bit_size = BITS_IN_POINTER;
881 ctype = op->ctype;
882 if (ctype->type == SYM_NODE) {
883 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
884 if (ctype->ctype.modifiers & MOD_REGISTER) {
885 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
886 ctype->ctype.modifiers &= ~MOD_REGISTER;
888 symbol->ctype.modifiers = ctype->ctype.modifiers;
889 symbol->ctype.as = ctype->ctype.as;
890 symbol->ctype.context = ctype->ctype.context;
891 symbol->ctype.contextmask = ctype->ctype.contextmask;
892 ctype = ctype->ctype.base_type;
895 symbol->ctype.base_type = ctype;
896 *expr = *op->unop;
897 expr->ctype = symbol;
898 return symbol;
902 static struct symbol *evaluate_dereference(struct expression *expr)
904 struct expression *op = expr->unop;
905 struct symbol *ctype = op->ctype, *sym;
907 sym = alloc_symbol(expr->pos, SYM_NODE);
908 if (ctype->type == SYM_NODE) {
909 ctype = ctype->ctype.base_type;
910 merge_type(sym, ctype);
912 sym->ctype = ctype->ctype;
913 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
914 warn(expr->pos, "cannot derefence this type");
915 return 0;
918 ctype = ctype->ctype.base_type;
919 examine_symbol_type(ctype);
920 if (!ctype) {
921 warn(expr->pos, "undefined type");
922 return NULL;
925 sym->bit_size = ctype->bit_size;
926 sym->array_size = ctype->array_size;
928 /* Simplify: *&(expr) => (expr) */
929 if (op->type == EXPR_PREOP && op->op == '&') {
930 *expr = *op->unop;
933 expr->ctype = sym;
934 return sym;
937 static void simplify_preop(struct expression *expr)
939 struct expression *op = expr->unop;
940 unsigned long long v, mask;
942 if (op->type != EXPR_VALUE)
943 return;
944 v = op->value;
945 switch (expr->op) {
946 case '+': break;
947 case '-': v = -v; break;
948 case '!': v = !v; break;
949 case '~': v = ~v; break;
950 default: return;
952 mask = 1ULL << (expr->ctype->bit_size-1);
953 mask = mask | (mask-1);
954 expr->value = v & mask;
955 expr->type = EXPR_VALUE;
959 * Unary post-ops: x++ and x--
961 static struct symbol *evaluate_postop(struct expression *expr)
963 struct expression *op = expr->unop;
964 struct symbol *ctype = op->ctype;
966 if (!lvalue_expression(expr->unop)) {
967 warn(expr->pos, "need lvalue expression for ++/--");
968 return NULL;
970 expr->ctype = ctype;
971 return ctype;
974 static struct symbol *evaluate_preop(struct expression *expr)
976 struct symbol *ctype = expr->unop->ctype;
978 switch (expr->op) {
979 case '(':
980 case '+':
981 *expr = *expr->unop;
982 return ctype;
984 case '*':
985 return evaluate_dereference(expr);
987 case '&':
988 return evaluate_addressof(expr);
990 case SPECIAL_INCREMENT:
991 case SPECIAL_DECREMENT:
993 * From a type evaluation standpoint the pre-ops are
994 * the same as the postops
996 return evaluate_postop(expr);
998 case '!':
999 ctype = &bool_ctype;
1000 break;
1002 default:
1003 break;
1005 expr->ctype = ctype;
1006 simplify_preop(expr);
1007 return &bool_ctype;
1010 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1012 struct ptr_list *head = (struct ptr_list *)_list;
1013 struct ptr_list *list = head;
1015 if (!head)
1016 return NULL;
1017 do {
1018 int i;
1019 for (i = 0; i < list->nr; i++) {
1020 struct symbol *sym = (struct symbol *) list->list[i];
1021 if (sym->ident) {
1022 if (sym->ident != ident)
1023 continue;
1024 *offset = sym->offset;
1025 return sym;
1026 } else {
1027 struct symbol *ctype = sym->ctype.base_type;
1028 struct symbol *sub;
1029 if (!ctype)
1030 continue;
1031 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1032 continue;
1033 sub = find_identifier(ident, ctype->symbol_list, offset);
1034 if (!sub)
1035 continue;
1036 *offset += sym->offset;
1037 return sub;
1040 } while ((list = list->next) != head);
1041 return NULL;
1044 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1046 struct expression *add;
1048 if (!offset)
1049 return expr;
1051 /* Create a new add-expression */
1052 add = alloc_expression(expr->pos, EXPR_BINOP);
1053 add->op = '+';
1054 add->ctype = &ptr_ctype;
1055 add->left = expr;
1056 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1057 add->right->ctype = &int_ctype;
1058 add->right->value = offset;
1060 simplify_int_binop(add, &ptr_ctype);
1061 return add;
1064 /* structure/union dereference */
1065 static struct symbol *evaluate_member_dereference(struct expression *expr)
1067 int offset;
1068 struct symbol *ctype, *member, *sym;
1069 struct expression *deref = expr->deref, *add;
1070 struct ident *ident = expr->member;
1071 unsigned int mod;
1072 int address_space;
1074 if (!evaluate_expression(deref))
1075 return NULL;
1076 if (!ident) {
1077 warn(expr->pos, "bad member name");
1078 return NULL;
1081 ctype = deref->ctype;
1082 address_space = ctype->ctype.as;
1083 mod = ctype->ctype.modifiers;
1084 if (ctype->type == SYM_NODE) {
1085 ctype = ctype->ctype.base_type;
1086 address_space |= ctype->ctype.as;
1087 mod |= ctype->ctype.modifiers;
1089 if (expr->op == SPECIAL_DEREFERENCE) {
1090 /* Arrays will degenerate into pointers for '->' */
1091 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1092 warn(expr->pos, "expected a pointer to a struct/union");
1093 return NULL;
1095 mod = ctype->ctype.modifiers;
1096 address_space = ctype->ctype.as;
1097 ctype = ctype->ctype.base_type;
1098 if (ctype->type == SYM_NODE) {
1099 mod |= ctype->ctype.modifiers;
1100 address_space |= ctype->ctype.as;
1101 ctype = ctype->ctype.base_type;
1103 } else {
1104 if (!lvalue_expression(deref)) {
1105 warn(deref->pos, "expected lvalue for member dereference");
1106 return NULL;
1108 deref = deref->unop;
1109 expr->deref = deref;
1111 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1112 warn(expr->pos, "expected structure or union");
1113 return NULL;
1115 offset = 0;
1116 member = find_identifier(ident, ctype->symbol_list, &offset);
1117 if (!member) {
1118 warn(expr->pos, "no such struct/union member");
1119 return NULL;
1122 add = evaluate_offset(deref, offset);
1124 sym = alloc_symbol(expr->pos, SYM_NODE);
1125 sym->bit_size = member->bit_size;
1126 sym->array_size = member->array_size;
1127 sym->ctype = member->ctype;
1128 sym->ctype.modifiers = mod;
1129 sym->ctype.as = address_space;
1130 ctype = member->ctype.base_type;
1131 if (ctype->type == SYM_BITFIELD) {
1132 ctype = ctype->ctype.base_type;
1133 expr->type = EXPR_BITFIELD;
1134 expr->bitpos = member->bit_offset;
1135 expr->nrbits = member->fieldwidth;
1136 expr->address = add;
1137 } else {
1138 expr->type = EXPR_PREOP;
1139 expr->op = '*';
1140 expr->unop = add;
1143 expr->ctype = sym;
1144 return sym;
1147 static struct symbol *evaluate_sizeof(struct expression *expr)
1149 int size;
1151 if (expr->cast_type) {
1152 examine_symbol_type(expr->cast_type);
1153 size = expr->cast_type->bit_size;
1154 } else {
1155 if (!evaluate_expression(expr->cast_expression))
1156 return 0;
1157 size = expr->cast_expression->ctype->bit_size;
1159 if (size & 7) {
1160 warn(expr->pos, "cannot size expression");
1161 return 0;
1163 expr->type = EXPR_VALUE;
1164 expr->value = size >> 3;
1165 expr->ctype = size_t_ctype;
1166 return size_t_ctype;
1169 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
1171 struct expression *expr;
1172 struct symbol_list *argument_types = fn->arguments;
1173 struct symbol *argtype;
1174 int i = 1;
1176 PREPARE_PTR_LIST(argument_types, argtype);
1177 FOR_EACH_PTR (head, expr) {
1178 struct expression **p = THIS_ADDRESS(expr);
1179 struct symbol *ctype, *target;
1180 ctype = evaluate_expression(expr);
1182 if (!ctype)
1183 return 0;
1185 ctype = degenerate(expr, ctype, p);
1187 target = argtype;
1188 if (!target && ctype->bit_size < BITS_IN_INT)
1189 target = &int_ctype;
1190 if (target) {
1191 static char where[30];
1192 examine_symbol_type(target);
1193 sprintf(where, "argument %d", i);
1194 compatible_assignment_types(expr, target, p, ctype, where);
1197 i++;
1198 NEXT_PTR_LIST(argtype);
1199 } END_FOR_EACH_PTR;
1200 FINISH_PTR_LIST(argtype);
1201 return 1;
1204 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1205 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1207 struct expression *entry;
1208 int current = 0;
1209 int max = 0;
1211 FOR_EACH_PTR(expr->expr_list, entry) {
1212 struct expression **p = THIS_ADDRESS(entry);
1214 if (entry->type == EXPR_INDEX) {
1215 current = entry->idx_to;
1216 continue;
1218 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1219 current++;
1220 if (current > max)
1221 max = current;
1222 } END_FOR_EACH_PTR;
1223 return max;
1226 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1228 struct expression *entry;
1229 struct symbol *sym;
1231 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1232 FOR_EACH_PTR(expr->expr_list, entry) {
1233 struct expression **p = THIS_ADDRESS(entry);
1235 if (entry->type == EXPR_IDENTIFIER) {
1236 struct ident *ident = entry->expr_ident;
1237 /* We special-case the "already right place" case */
1238 if (sym && sym->ident == ident)
1239 continue;
1240 RESET_PTR_LIST(sym);
1241 for (;;) {
1242 if (!sym) {
1243 warn(entry->pos, "unknown named initializer");
1244 return 0;
1246 if (sym->ident == ident)
1247 break;
1248 NEXT_PTR_LIST(sym);
1250 continue;
1253 if (!sym) {
1254 warn(expr->pos, "too many initializers for struct/union");
1255 return 0;
1258 evaluate_initializer(sym, p, offset + sym->offset);
1260 NEXT_PTR_LIST(sym);
1261 } END_FOR_EACH_PTR;
1262 FINISH_PTR_LIST(sym);
1264 return 0;
1268 * Initializers are kind of like assignments. Except
1269 * they can be a hell of a lot more complex.
1271 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1273 struct expression *expr = *ep;
1276 * Simple non-structure/array initializers are the simple
1277 * case, and look (and parse) largely like assignments.
1279 if (expr->type != EXPR_INITIALIZER) {
1280 int size = 0;
1281 struct symbol *rtype = evaluate_expression(expr);
1282 if (rtype) {
1283 struct expression *pos;
1284 struct symbol *fn = rtype;
1285 if (rtype->type == SYM_NODE)
1286 fn = rtype->ctype.base_type;
1287 if (fn->type == SYM_FN) {
1288 rtype = degenerate(expr, rtype, ep);
1289 expr = *ep;
1291 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1292 /* strings are special: char arrays */
1293 if (rtype->type == SYM_ARRAY)
1294 size = rtype->array_size;
1295 pos = alloc_expression(expr->pos, EXPR_POS);
1296 pos->init_offset = offset;
1297 pos->init_sym = ctype;
1298 pos->init_expr = *ep;
1299 *ep = pos;
1301 return size;
1304 expr->ctype = ctype;
1305 if (ctype->type == SYM_NODE)
1306 ctype = ctype->ctype.base_type;
1308 switch (ctype->type) {
1309 case SYM_ARRAY:
1310 case SYM_PTR:
1311 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1312 case SYM_UNION:
1313 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1314 case SYM_STRUCT:
1315 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1316 default:
1317 break;
1319 warn(expr->pos, "unexpected compound initializer");
1320 return 0;
1323 static struct symbol *evaluate_cast(struct expression *expr)
1325 struct expression *target = expr->cast_expression;
1326 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1328 expr->ctype = ctype;
1329 expr->cast_type = ctype;
1332 * Special case: a cast can be followed by an
1333 * initializer, in which case we need to pass
1334 * the type value down to that initializer rather
1335 * than trying to evaluate it as an expression
1337 if (target->type == EXPR_INITIALIZER) {
1338 evaluate_initializer(ctype, &expr->cast_expression, 0);
1339 return ctype;
1342 evaluate_expression(target);
1344 /* Simplify normal integer casts.. */
1345 if (target->type == EXPR_VALUE)
1346 cast_value(expr, ctype, target, target->ctype);
1347 return ctype;
1350 static struct symbol *evaluate_call(struct expression *expr)
1352 int args, fnargs;
1353 struct symbol *ctype;
1354 struct expression *fn = expr->fn;
1355 struct expression_list *arglist = expr->args;
1357 if (!evaluate_expression(fn))
1358 return NULL;
1359 ctype = fn->ctype;
1360 if (ctype->type == SYM_NODE) {
1362 * FIXME!! We should really expand the inline functions.
1363 * For now we just mark them accessed so that they show
1364 * up on the list of used symbols.
1366 if (ctype->ctype.modifiers & MOD_INLINE)
1367 access_symbol(ctype);
1368 ctype = ctype->ctype.base_type;
1370 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1371 ctype = ctype->ctype.base_type;
1372 if (ctype->type != SYM_FN) {
1373 warn(expr->pos, "not a function");
1374 return NULL;
1376 if (!evaluate_arguments(ctype, arglist))
1377 return NULL;
1378 args = expression_list_size(expr->args);
1379 fnargs = symbol_list_size(ctype->arguments);
1380 if (args < fnargs)
1381 warn(expr->pos, "not enough arguments for function");
1382 if (args > fnargs && !ctype->variadic)
1383 warn(expr->pos, "too many arguments for function");
1384 expr->ctype = ctype->ctype.base_type;
1385 return expr->ctype;
1388 struct symbol *evaluate_expression(struct expression *expr)
1390 if (!expr)
1391 return NULL;
1392 if (expr->ctype)
1393 return expr->ctype;
1395 switch (expr->type) {
1396 case EXPR_VALUE:
1397 warn(expr->pos, "value expression without a type");
1398 return NULL;
1399 case EXPR_STRING:
1400 return evaluate_string(expr);
1401 case EXPR_SYMBOL:
1402 return evaluate_symbol_expression(expr);
1403 case EXPR_BINOP:
1404 if (!evaluate_expression(expr->left))
1405 return NULL;
1406 if (!evaluate_expression(expr->right))
1407 return NULL;
1408 return evaluate_binop(expr);
1409 case EXPR_LOGICAL:
1410 return evaluate_logical(expr);
1411 case EXPR_COMMA:
1412 if (!evaluate_expression(expr->left))
1413 return NULL;
1414 if (!evaluate_expression(expr->right))
1415 return NULL;
1416 return evaluate_comma(expr);
1417 case EXPR_COMPARE:
1418 if (!evaluate_expression(expr->left))
1419 return NULL;
1420 if (!evaluate_expression(expr->right))
1421 return NULL;
1422 return evaluate_compare(expr);
1423 case EXPR_ASSIGNMENT:
1424 if (!evaluate_expression(expr->left))
1425 return NULL;
1426 if (!evaluate_expression(expr->right))
1427 return NULL;
1428 return evaluate_assignment(expr);
1429 case EXPR_PREOP:
1430 if (!evaluate_expression(expr->unop))
1431 return NULL;
1432 return evaluate_preop(expr);
1433 case EXPR_POSTOP:
1434 if (!evaluate_expression(expr->unop))
1435 return NULL;
1436 return evaluate_postop(expr);
1437 case EXPR_CAST:
1438 return evaluate_cast(expr);
1439 case EXPR_SIZEOF:
1440 return evaluate_sizeof(expr);
1441 case EXPR_DEREF:
1442 return evaluate_member_dereference(expr);
1443 case EXPR_CALL:
1444 return evaluate_call(expr);
1445 case EXPR_BITFIELD:
1446 warn(expr->pos, "bitfield generated by parser");
1447 return NULL;
1448 case EXPR_CONDITIONAL:
1449 if (!evaluate_expression(expr->conditional))
1450 return NULL;
1451 if (!evaluate_expression(expr->cond_false))
1452 return NULL;
1453 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1454 return NULL;
1455 return evaluate_conditional(expr);
1456 case EXPR_STATEMENT:
1457 expr->ctype = evaluate_statement(expr->statement);
1458 return expr->ctype;
1460 /* These can not exist as stand-alone expressions */
1461 case EXPR_INITIALIZER:
1462 case EXPR_IDENTIFIER:
1463 case EXPR_INDEX:
1464 case EXPR_POS:
1465 warn(expr->pos, "internal front-end error: initializer in expression");
1466 return NULL;
1468 return NULL;
1471 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1473 struct symbol **last = _last;
1474 struct symbol *type = evaluate_statement(stmt);
1476 if (flags & ITERATE_LAST)
1477 *last = type;
1480 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1482 evaluate_symbol(sym);
1485 static void check_duplicates(struct symbol *sym)
1487 struct symbol *next = sym;
1489 while ((next = next->same_symbol) != NULL) {
1490 const char *typediff;
1491 evaluate_symbol(next);
1492 typediff = type_difference(sym, next, 0, 0);
1493 if (typediff) {
1494 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1495 show_ident(sym->ident),
1496 input_streams[next->pos.stream].name, next->pos.line, typediff);
1497 return;
1502 struct symbol *evaluate_symbol(struct symbol *sym)
1504 struct symbol *base_type;
1506 sym = examine_symbol_type(sym);
1507 base_type = sym->ctype.base_type;
1508 if (!base_type)
1509 return NULL;
1511 check_duplicates(sym);
1513 /* Evaluate the initializers */
1514 if (sym->initializer) {
1515 int count = evaluate_initializer(sym, &sym->initializer, 0);
1516 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1517 int bit_size = count * base_type->ctype.base_type->bit_size;
1518 base_type->array_size = count;
1519 base_type->bit_size = bit_size;
1520 sym->array_size = count;
1521 sym->bit_size = bit_size;
1525 /* And finally, evaluate the body of the symbol too */
1526 if (base_type->type == SYM_FN) {
1527 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1528 if (base_type->stmt) {
1529 current_fn = base_type;
1530 current_contextmask = sym->ctype.contextmask;
1531 current_context = sym->ctype.context;
1532 evaluate_statement(base_type->stmt);
1536 return base_type;
1539 struct symbol *evaluate_return_expression(struct statement *stmt)
1541 struct expression *expr = stmt->expression;
1542 struct symbol *ctype, *fntype;
1544 fntype = current_fn->ctype.base_type;
1545 if (fntype == &void_ctype) {
1546 if (expr)
1547 warn(expr->pos, "return expression in void function");
1548 return NULL;
1551 if (!expr) {
1552 warn(stmt->pos, "return with no return value");
1553 return NULL;
1555 ctype = evaluate_expression(expr);
1556 if (!ctype)
1557 return NULL;
1558 ctype = degenerate(expr, ctype, &expr);
1559 expr->ctype = ctype;
1560 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1561 stmt->expression = expr;
1562 return NULL;
1565 struct symbol *evaluate_statement(struct statement *stmt)
1567 if (!stmt)
1568 return NULL;
1570 switch (stmt->type) {
1571 case STMT_RETURN:
1572 return evaluate_return_expression(stmt);
1574 case STMT_EXPRESSION:
1575 return evaluate_expression(stmt->expression);
1577 case STMT_COMPOUND: {
1578 struct symbol *type = NULL;
1579 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1580 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1581 return type;
1583 case STMT_IF:
1584 evaluate_expression(stmt->if_conditional);
1585 evaluate_statement(stmt->if_true);
1586 evaluate_statement(stmt->if_false);
1587 return NULL;
1588 case STMT_ITERATOR:
1589 evaluate_expression(stmt->iterator_pre_condition);
1590 evaluate_expression(stmt->iterator_post_condition);
1591 evaluate_statement(stmt->iterator_pre_statement);
1592 evaluate_statement(stmt->iterator_statement);
1593 evaluate_statement(stmt->iterator_post_statement);
1594 return NULL;
1595 case STMT_SWITCH:
1596 evaluate_expression(stmt->switch_expression);
1597 evaluate_statement(stmt->switch_statement);
1598 return NULL;
1599 case STMT_CASE:
1600 evaluate_expression(stmt->case_expression);
1601 evaluate_expression(stmt->case_to);
1602 evaluate_statement(stmt->case_statement);
1603 return NULL;
1604 case STMT_LABEL:
1605 evaluate_statement(stmt->label_statement);
1606 return NULL;
1607 case STMT_GOTO:
1608 evaluate_expression(stmt->goto_expression);
1609 return NULL;
1610 case STMT_NONE:
1611 break;
1612 case STMT_ASM:
1613 /* FIXME! Do the asm parameter evaluation! */
1614 break;
1616 return NULL;
1619 long long get_expression_value(struct expression *expr)
1621 long long value, mask;
1622 struct symbol *ctype;
1624 ctype = evaluate_expression(expr);
1625 if (!ctype || expr->type != EXPR_VALUE) {
1626 warn(expr->pos, "bad constant expression");
1627 return 0;
1630 value = expr->value;
1631 mask = 1ULL << (ctype->bit_size-1);
1633 if (value & mask) {
1634 while (ctype->type != SYM_BASETYPE)
1635 ctype = ctype->ctype.base_type;
1636 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1637 value = value | mask | ~(mask-1);
1639 return value;