Oops. The argument symbol initializers got lost on inlining,
[smatch.git] / evaluate.c
blob40139d72490d8c117fd7b1d265417a0b40f3cf38
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 /* Const symbol with a constant initializer? */
61 if (sym->ctype.modifiers & MOD_CONST) {
62 struct expression *value = sym->initializer;
63 if (value && value->type == EXPR_VALUE) {
64 expr->type = EXPR_VALUE;
65 expr->value = value->value;
66 return sym;
70 /* enum's can be turned into plain values */
71 if (sym->type != SYM_ENUM) {
72 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
73 addr->symbol = sym;
74 addr->symbol_name = expr->symbol_name;
75 addr->ctype = &ptr_ctype;
76 expr->type = EXPR_PREOP;
77 expr->op = '*';
78 expr->unop = addr;
79 return sym;
81 expr->type = EXPR_VALUE;
82 expr->value = sym->value;
83 expr->ctype = base_type;
84 return sym;
87 static struct symbol *evaluate_string(struct expression *expr)
89 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
90 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
91 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
92 int length = expr->string->length;
94 sym->array_size = length;
95 sym->bit_size = BITS_IN_CHAR * length;
96 sym->ctype.alignment = 1;
97 sym->ctype.modifiers = MOD_STATIC;
98 sym->ctype.base_type = array;
100 array->array_size = length;
101 array->bit_size = BITS_IN_CHAR * length;
102 array->ctype.alignment = 1;
103 array->ctype.modifiers = MOD_STATIC;
104 array->ctype.base_type = &char_ctype;
106 addr->symbol = sym;
107 addr->ctype = &ptr_ctype;
109 expr->type = EXPR_PREOP;
110 expr->op = '*';
111 expr->unop = addr;
112 expr->ctype = sym;
113 return sym;
116 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
118 unsigned long lmod, rmod, mod;
120 if (left == right)
121 return left;
123 if (left->bit_size > right->bit_size)
124 return left;
126 if (right->bit_size > left->bit_size)
127 return right;
129 /* Same size integers - promote to unsigned, promote to long */
130 lmod = left->ctype.modifiers;
131 rmod = right->ctype.modifiers;
132 mod = lmod | rmod;
133 if (mod == lmod)
134 return left;
135 if (mod == rmod)
136 return right;
137 return ctype_integer(mod);
140 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
141 struct expression *old, struct symbol *oldtype)
143 int old_size = oldtype->bit_size;
144 int new_size = newtype->bit_size;
145 long long value, mask, ormask, andmask;
146 int is_signed;
148 // FIXME! We don't handle FP casts of constant values yet
149 if (newtype->ctype.base_type == &fp_type)
150 return NULL;
151 if (oldtype->ctype.base_type == &fp_type)
152 return NULL;
154 // For pointers and integers, we can just move the value around
155 expr->type = EXPR_VALUE;
156 if (old_size == new_size) {
157 expr->value = old->value;
158 return newtype;
161 // expand it to the full "long long" value
162 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
163 mask = 1ULL << (old_size-1);
164 value = old->value;
165 if (!(value & mask))
166 is_signed = 0;
167 andmask = mask | (mask-1);
168 ormask = ~andmask;
169 if (!is_signed)
170 ormask = 0;
171 value = (value & andmask) | ormask;
173 // Truncate it to the new size
174 mask = 1ULL << (new_size-1);
175 mask = mask | (mask-1);
176 expr->value = value & mask;
177 return newtype;
180 static struct expression * cast_to(struct expression *old, struct symbol *type)
182 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
183 expr->ctype = type;
184 expr->cast_type = type;
185 expr->cast_expression = old;
186 if (old->type == EXPR_VALUE)
187 cast_value(expr, type, old, old->ctype);
188 return expr;
191 static int is_ptr_type(struct symbol *type)
193 if (type->type == SYM_NODE)
194 type = type->ctype.base_type;
195 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
198 static int is_int_type(struct symbol *type)
200 if (type->type == SYM_NODE)
201 type = type->ctype.base_type;
202 return type->ctype.base_type == &int_type;
205 static struct symbol *bad_expr_type(struct expression *expr)
207 warn(expr->pos, "incompatible types for operation");
208 return NULL;
211 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
213 struct expression *left = *lp, *right = *rp;
214 struct symbol *ltype = left->ctype, *rtype = right->ctype;
216 if (ltype->type == SYM_NODE)
217 ltype = ltype->ctype.base_type;
218 if (rtype->type == SYM_NODE)
219 rtype = rtype->ctype.base_type;
220 /* Integer promotion? */
221 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
222 ltype = &int_ctype;
223 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
224 rtype = &int_ctype;
225 if (is_int_type(ltype) && is_int_type(rtype)) {
226 struct symbol *ctype = bigger_int_type(ltype, rtype);
228 /* Don't bother promoting same-size entities, it only adds clutter */
229 if (ltype->bit_size != ctype->bit_size)
230 *lp = cast_to(left, ctype);
231 if (rtype->bit_size != ctype->bit_size)
232 *rp = cast_to(right, ctype);
233 return ctype;
235 return NULL;
238 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
240 if (count >= ctype->bit_size) {
241 warn(expr->pos, "shift too big for type");
242 count &= ctype->bit_size-1;
244 return count;
248 * CAREFUL! We need to get the size and sign of the
249 * result right!
251 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
253 struct expression *left = expr->left, *right = expr->right;
254 unsigned long long v, l, r, mask;
255 signed long long s, sl, sr;
256 int is_signed, shift;
258 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
259 return;
260 l = left->value; r = right->value;
261 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
262 mask = 1ULL << (ctype->bit_size-1);
263 sl = l; sr = r;
264 if (is_signed && (sl & mask))
265 sl |= ~(mask-1);
266 if (is_signed && (sr & mask))
267 sr |= ~(mask-1);
269 switch (expr->op) {
270 case '+': v = l + r; s = v; break;
271 case '-': v = l - r; s = v; break;
272 case '&': v = l & r; s = v; break;
273 case '|': v = l | r; s = v; break;
274 case '^': v = l ^ r; s = v; break;
275 case '*': v = l * r; s = sl * sr; break;
276 case '/': if (!r) return; v = l / r; s = sl / sr; break;
277 case '%': if (!r) return; v = l % r; s = sl % sr; break;
278 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
279 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
280 case '<': v = l < r; s = sl < sr; break;
281 case '>': v = l > r; s = sl > sr; break;
282 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
283 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
284 case SPECIAL_EQUAL: v = l == r; s = v; break;
285 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
286 default: return;
288 if (is_signed)
289 v = s;
290 mask = mask | (mask-1);
291 expr->value = v & mask;
292 expr->type = EXPR_VALUE;
295 static struct symbol *evaluate_int_binop(struct expression *expr)
297 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
298 if (ctype) {
299 expr->ctype = ctype;
300 simplify_int_binop(expr, ctype);
301 return ctype;
303 return bad_expr_type(expr);
306 static inline int lvalue_expression(struct expression *expr)
308 while (expr->type == EXPR_CAST)
309 expr = expr->cast_expression;
310 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
313 /* Arrays degenerate into pointers on pointer arithmetic */
314 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
316 struct symbol *base = ctype;
318 if (ctype->type == SYM_NODE)
319 base = ctype->ctype.base_type;
320 if (base->type == SYM_ARRAY || base->type == SYM_FN) {
321 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
322 struct expression *ptr;
324 merge_type(sym, ctype);
325 if (base->type == SYM_FN)
326 base = ctype;
327 merge_type(sym, base);
328 sym->bit_size = BITS_IN_POINTER;
329 ctype = sym;
331 ptr = *ptr_p;
332 *ptr_p = ptr->unop;
335 * This all really assumes that we got the degenerate
336 * array as an lvalue (ie a dereference). If that
337 * is not the case, then holler - because we've screwed
338 * up.
340 if (!lvalue_expression(ptr))
341 warn(ptr->pos, "internal error: strange degenerate array case");
342 ptr->ctype = base;
344 return ctype;
347 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
349 struct symbol *ctype;
350 struct symbol *ptr_type = ptr->ctype;
351 struct symbol *i_type = i->ctype;
352 int bit_size;
354 if (i_type->type == SYM_NODE)
355 i_type = i_type->ctype.base_type;
356 if (ptr_type->type == SYM_NODE)
357 ptr_type = ptr_type->ctype.base_type;
359 if (i_type->type == SYM_ENUM)
360 i_type = &int_ctype;
361 if (!is_int_type(i_type))
362 return bad_expr_type(expr);
364 ctype = ptr->ctype;
365 examine_symbol_type(ctype);
367 ctype = degenerate(expr, ctype, &ptr);
368 bit_size = ctype->bit_size;
370 /* Special case: adding zero commonly happens as a result of 'array[0]' */
371 if (i->type == EXPR_VALUE && !i->value) {
372 *expr = *ptr;
373 } else if (bit_size > BITS_IN_CHAR) {
374 struct expression *add = expr;
375 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
376 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
378 val->ctype = size_t_ctype;
379 val->value = bit_size >> 3;
381 mul->op = '*';
382 mul->ctype = size_t_ctype;
383 mul->left = i;
384 mul->right = val;
385 simplify_int_binop(mul, size_t_ctype);
387 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
388 add->left = ptr;
389 add->right = mul;
390 simplify_int_binop(add, ctype);
393 expr->ctype = ctype;
394 return ctype;
397 static struct symbol *evaluate_add(struct expression *expr)
399 struct expression *left = expr->left, *right = expr->right;
400 struct symbol *ltype = left->ctype, *rtype = right->ctype;
402 if (is_ptr_type(ltype))
403 return evaluate_ptr_add(expr, left, right);
405 if (is_ptr_type(rtype))
406 return evaluate_ptr_add(expr, right, left);
408 // FIXME! FP promotion
409 return evaluate_int_binop(expr);
412 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
413 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
415 static const char * type_difference(struct symbol *target, struct symbol *source,
416 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
418 for (;;) {
419 unsigned long mod1, mod2, diff;
420 unsigned long as1, as2;
422 if (target == source)
423 break;
424 if (!target || !source)
425 return "different types";
427 * Peel of per-node information.
428 * FIXME! Check alignment, address space, and context too here!
430 if (target->type == SYM_NODE)
431 target = target->ctype.base_type;
432 if (source->type == SYM_NODE)
433 source = source->ctype.base_type;
434 mod1 = target->ctype.modifiers;
435 as1 = target->ctype.as;
436 mod2 = source->ctype.modifiers;
437 as2 = source->ctype.as;
439 if (target->type != source->type) {
440 int type1 = target->type;
441 int type2 = source->type;
443 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
444 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
445 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
447 if ((type1 == SYM_PTR) && (target->ctype.base_type->type == SYM_FN)) {
448 target = target->ctype.base_type;
449 type1 = SYM_FN;
452 if ((type2 == SYM_PTR) && (source->ctype.base_type->type == SYM_FN)) {
453 source = source->ctype.base_type;
454 type2 = SYM_FN;
457 if (type1 != type2)
458 return "different base types";
461 /* Must be same address space to be comparable */
462 if (as1 != as2)
463 return "different address spaces";
465 /* Ignore differences in storage types, sign, or addressability */
466 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
467 if (diff) {
468 mod1 &= diff & ~target_mod_ignore;
469 mod2 &= diff & ~source_mod_ignore;
470 if (mod1 | mod2) {
471 if ((mod1 | mod2) & MOD_SIZE)
472 return "different type sizes";
473 return "different modifiers";
477 if (target->type == SYM_FN) {
478 int i;
479 struct symbol *arg1, *arg2;
480 if (target->variadic != source->variadic)
481 return "incompatible variadic arguments";
482 PREPARE_PTR_LIST(target->arguments, arg1);
483 PREPARE_PTR_LIST(source->arguments, arg2);
484 i = 1;
485 for (;;) {
486 const char *diff;
487 diff = type_difference(arg1, arg2, 0, 0);
488 if (diff) {
489 static char argdiff[80];
490 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
491 return argdiff;
493 if (!arg1)
494 break;
495 NEXT_PTR_LIST(arg1);
496 NEXT_PTR_LIST(arg2);
497 i++;
499 FINISH_PTR_LIST(arg2);
500 FINISH_PTR_LIST(arg1);
503 target = target->ctype.base_type;
504 source = source->ctype.base_type;
506 return NULL;
509 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
511 /* NULL expression? Just return the type of the "other side" */
512 if (r->type == EXPR_VALUE && !r->value)
513 return l->ctype;
514 if (l->type == EXPR_VALUE && !l->value)
515 return r->ctype;
516 return NULL;
520 * Ignore differences in "volatile" and "const"ness when
521 * subtracting pointers
523 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
525 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
527 const char *typediff;
528 struct symbol *ctype;
529 struct symbol *ltype = l->ctype, *rtype = r->ctype;
532 * If it is an integer subtract: the ptr add case will do the
533 * right thing.
535 if (!is_ptr_type(rtype))
536 return evaluate_ptr_add(expr, l, r);
538 ctype = ltype;
539 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
540 if (typediff) {
541 ctype = common_ptr_type(l, r);
542 if (!ctype) {
543 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
544 return NULL;
547 examine_symbol_type(ctype);
549 /* Figure out the base type we point to */
550 if (ctype->type == SYM_NODE)
551 ctype = ctype->ctype.base_type;
552 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
553 warn(expr->pos, "subtraction of functions? Share your drugs");
554 return NULL;
556 ctype = ctype->ctype.base_type;
558 expr->ctype = ssize_t_ctype;
559 if (ctype->bit_size > BITS_IN_CHAR) {
560 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
561 struct expression *div = expr;
562 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
564 val->ctype = size_t_ctype;
565 val->value = ctype->bit_size >> 3;
567 sub->op = '-';
568 sub->ctype = ssize_t_ctype;
569 sub->left = l;
570 sub->right = r;
572 div->op = '/';
573 div->left = sub;
574 div->right = val;
577 return ssize_t_ctype;
580 static struct symbol *evaluate_sub(struct expression *expr)
582 struct expression *left = expr->left, *right = expr->right;
583 struct symbol *ltype = left->ctype;
585 if (is_ptr_type(ltype))
586 return evaluate_ptr_sub(expr, left, right);
588 // FIXME! FP promotion
589 return evaluate_int_binop(expr);
592 static struct symbol *evaluate_logical(struct expression *expr)
594 struct expression *left = expr->left;
595 struct expression *right;
597 if (!evaluate_expression(left))
598 return NULL;
600 /* Do immediate short-circuiting ... */
601 if (left->type == EXPR_VALUE) {
602 if (expr->op == SPECIAL_LOGICAL_AND) {
603 if (!left->value) {
604 expr->type = EXPR_VALUE;
605 expr->value = 0;
606 expr->ctype = &bool_ctype;
607 return &bool_ctype;
609 } else {
610 if (left->value) {
611 expr->type = EXPR_VALUE;
612 expr->value = 1;
613 expr->ctype = &bool_ctype;
614 return &bool_ctype;
619 right = expr->right;
620 if (!evaluate_expression(right))
621 return NULL;
622 expr->ctype = &bool_ctype;
623 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
625 * We know the left value doesn't matter, since
626 * otherwise we would have short-circuited it..
628 expr->type = EXPR_VALUE;
629 expr->value = right->value;
632 return &bool_ctype;
635 static struct symbol *evaluate_arithmetic(struct expression *expr)
637 // FIXME! Floating-point promotion!
638 return evaluate_int_binop(expr);
641 static struct symbol *evaluate_binop(struct expression *expr)
643 switch (expr->op) {
644 // addition can take ptr+int, fp and int
645 case '+':
646 return evaluate_add(expr);
648 // subtraction can take ptr-ptr, fp and int
649 case '-':
650 return evaluate_sub(expr);
652 // Arithmetic operations can take fp and int
653 case '*': case '/': case '%':
654 return evaluate_arithmetic(expr);
656 // The rest are integer operations (bitops)
657 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
658 // '&', '^', '|'
659 default:
660 return evaluate_int_binop(expr);
664 static struct symbol *evaluate_comma(struct expression *expr)
666 expr->ctype = expr->right->ctype;
667 return expr->ctype;
670 static struct symbol *evaluate_compare(struct expression *expr)
672 struct expression *left = expr->left, *right = expr->right;
673 struct symbol *ltype = left->ctype, *rtype = right->ctype;
674 struct symbol *ctype;
676 /* Pointer types? */
677 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
678 expr->ctype = &bool_ctype;
679 // FIXME! Check the types for compatibility
680 return &bool_ctype;
683 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
684 if (ctype) {
685 simplify_int_binop(expr, ctype);
686 expr->ctype = &bool_ctype;
687 return &bool_ctype;
690 return bad_expr_type(expr);
693 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
695 /* Integer promotion? */
696 if (ltype->type == SYM_NODE)
697 ltype = ltype->ctype.base_type;
698 if (rtype->type == SYM_NODE)
699 rtype = rtype->ctype.base_type;
700 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
701 ltype = &int_ctype;
702 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
703 rtype = &int_ctype;
704 return (is_int_type(ltype) && is_int_type(rtype));
707 static int is_null_ptr(struct expression *expr)
709 return (expr->type == EXPR_VALUE &&
710 expr->value == 0);
714 * FIXME!! This shoul ddo casts, array degeneration etc..
716 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
718 struct symbol *ltype = left->ctype, *rtype = right->ctype;
720 if (ltype->type == SYM_PTR) {
721 if (is_null_ptr(right))
722 return ltype;
725 if (rtype->type == SYM_PTR) {
726 if (is_null_ptr(left))
727 return rtype;
729 return NULL;
732 static struct symbol *do_degenerate(struct expression **ep)
734 struct expression *expr = *ep;
735 return degenerate(expr, expr->ctype, ep);
738 static struct symbol * evaluate_conditional(struct expression *expr)
740 struct expression *cond, *true, *false;
741 struct symbol *ctype, *ltype, *rtype;
742 const char * typediff;
744 ctype = do_degenerate(&expr->conditional);
745 cond = expr->conditional;
747 ltype = ctype;
748 true = cond;
749 if (expr->cond_true) {
750 ltype = do_degenerate(&expr->cond_true);
751 true = expr->cond_true;
754 rtype = do_degenerate(&expr->cond_false);
755 false = expr->cond_false;
757 ctype = ltype;
758 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
759 if (typediff) {
760 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
761 if (!ctype) {
762 ctype = compatible_ptr_type(true, expr->cond_false);
763 if (!ctype) {
764 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
765 return NULL;
770 /* Simplify conditional expression.. */
771 if (cond->type == EXPR_VALUE) {
772 if (!cond->value)
773 true = false;
774 *expr = *true;
776 expr->ctype = ctype;
777 return ctype;
780 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
781 struct expression **rp, struct symbol *source, const char *where)
783 const char *typediff;
784 struct symbol *t;
785 int target_as;
787 /* It's ok if the target is more volatile or const than the source */
788 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
789 if (!typediff)
790 return 1;
792 if (compatible_integer_types(target, source)) {
793 if (target->bit_size != source->bit_size)
794 *rp = cast_to(*rp, target);
795 return 1;
798 /* Pointer destination? */
799 t = target;
800 target_as = t->ctype.as;
801 if (t->type == SYM_NODE) {
802 t = t->ctype.base_type;
803 target_as |= t->ctype.as;
805 if (t->type == SYM_PTR) {
806 struct expression *right = *rp;
807 struct symbol *s = source;
808 int source_as;
810 // NULL pointer is always ok
811 if (right->type == EXPR_VALUE && !right->value)
812 return 1;
814 /* "void *" matches anything as long as the address space is ok */
815 source_as = s->ctype.as;
816 if (s->type == SYM_NODE) {
817 s = s->ctype.base_type;
818 source_as |= s->ctype.as;
820 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
821 s = s->ctype.base_type;
822 t = t->ctype.base_type;
823 if (s == &void_ctype || t == &void_ctype)
824 return 1;
828 // FIXME!! Cast it?
829 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
830 warn(expr->pos, " expected %s", show_typename(target));
831 warn(expr->pos, " got %s", show_typename(source));
832 return 0;
836 * FIXME!! This is wrong from a double evaluation standpoint. We can't
837 * just expand the expression twice, that would make any side effects
838 * happen twice too.
840 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
842 int op = expr->op;
843 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
844 static const int op_trans[] = {
845 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
846 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
847 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
848 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
849 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
850 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
851 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
852 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
853 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
854 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
857 subexpr->left = left;
858 subexpr->right = right;
859 subexpr->op = op_trans[op - SPECIAL_BASE];
860 expr->op = '=';
861 expr->right = subexpr;
862 return evaluate_binop(subexpr);
865 static struct symbol *evaluate_assignment(struct expression *expr)
867 struct expression *left = expr->left, *right = expr->right;
868 struct symbol *ltype, *rtype;
870 ltype = left->ctype;
871 rtype = right->ctype;
872 if (expr->op != '=') {
873 rtype = evaluate_binop_assignment(expr, left, right);
874 if (!rtype)
875 return 0;
876 right = expr->right;
879 if (!lvalue_expression(left)) {
880 warn(expr->pos, "not an lvalue");
881 return NULL;
884 rtype = degenerate(right, rtype, &expr->right);
886 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
887 return 0;
889 expr->ctype = expr->left->ctype;
890 return expr->ctype;
893 static struct symbol *evaluate_addressof(struct expression *expr)
895 struct symbol *ctype, *symbol;
896 struct expression *op = expr->unop;
898 if (op->op != '*' || op->type != EXPR_PREOP) {
899 warn(expr->pos, "not addressable");
900 return NULL;
903 symbol = alloc_symbol(expr->pos, SYM_PTR);
904 symbol->ctype.alignment = POINTER_ALIGNMENT;
905 symbol->bit_size = BITS_IN_POINTER;
907 ctype = op->ctype;
908 if (ctype->type == SYM_NODE) {
909 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
910 if (ctype->ctype.modifiers & MOD_REGISTER) {
911 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
912 ctype->ctype.modifiers &= ~MOD_REGISTER;
914 symbol->ctype.modifiers = ctype->ctype.modifiers;
915 symbol->ctype.as = ctype->ctype.as;
916 symbol->ctype.context = ctype->ctype.context;
917 symbol->ctype.contextmask = ctype->ctype.contextmask;
918 ctype = ctype->ctype.base_type;
921 symbol->ctype.base_type = ctype;
922 *expr = *op->unop;
923 expr->ctype = symbol;
924 return symbol;
928 static struct symbol *evaluate_dereference(struct expression *expr)
930 struct expression *op = expr->unop;
931 struct symbol *ctype = op->ctype, *sym;
933 sym = alloc_symbol(expr->pos, SYM_NODE);
934 if (ctype->type == SYM_NODE) {
935 ctype = ctype->ctype.base_type;
936 merge_type(sym, ctype);
938 sym->ctype = ctype->ctype;
939 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
940 warn(expr->pos, "cannot derefence this type");
941 return 0;
944 ctype = ctype->ctype.base_type;
945 examine_symbol_type(ctype);
946 if (!ctype) {
947 warn(expr->pos, "undefined type");
948 return NULL;
951 sym->bit_size = ctype->bit_size;
952 sym->array_size = ctype->array_size;
954 /* Simplify: *&(expr) => (expr) */
955 if (op->type == EXPR_PREOP && op->op == '&') {
956 *expr = *op->unop;
959 expr->ctype = sym;
960 return sym;
963 static void simplify_preop(struct expression *expr)
965 struct expression *op = expr->unop;
966 unsigned long long v, mask;
968 if (op->type != EXPR_VALUE)
969 return;
970 v = op->value;
971 switch (expr->op) {
972 case '+': break;
973 case '-': v = -v; break;
974 case '!': v = !v; break;
975 case '~': v = ~v; break;
976 default: return;
978 mask = 1ULL << (expr->ctype->bit_size-1);
979 mask = mask | (mask-1);
980 expr->value = v & mask;
981 expr->type = EXPR_VALUE;
985 * Unary post-ops: x++ and x--
987 static struct symbol *evaluate_postop(struct expression *expr)
989 struct expression *op = expr->unop;
990 struct symbol *ctype = op->ctype;
992 if (!lvalue_expression(expr->unop)) {
993 warn(expr->pos, "need lvalue expression for ++/--");
994 return NULL;
996 expr->ctype = ctype;
997 return ctype;
1000 static struct symbol *evaluate_preop(struct expression *expr)
1002 struct symbol *ctype = expr->unop->ctype;
1004 switch (expr->op) {
1005 case '(':
1006 case '+':
1007 *expr = *expr->unop;
1008 return ctype;
1010 case '*':
1011 return evaluate_dereference(expr);
1013 case '&':
1014 return evaluate_addressof(expr);
1016 case SPECIAL_INCREMENT:
1017 case SPECIAL_DECREMENT:
1019 * From a type evaluation standpoint the pre-ops are
1020 * the same as the postops
1022 return evaluate_postop(expr);
1024 case '!':
1025 ctype = &bool_ctype;
1026 break;
1028 default:
1029 break;
1031 expr->ctype = ctype;
1032 simplify_preop(expr);
1033 return &bool_ctype;
1036 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1038 struct ptr_list *head = (struct ptr_list *)_list;
1039 struct ptr_list *list = head;
1041 if (!head)
1042 return NULL;
1043 do {
1044 int i;
1045 for (i = 0; i < list->nr; i++) {
1046 struct symbol *sym = (struct symbol *) list->list[i];
1047 if (sym->ident) {
1048 if (sym->ident != ident)
1049 continue;
1050 *offset = sym->offset;
1051 return sym;
1052 } else {
1053 struct symbol *ctype = sym->ctype.base_type;
1054 struct symbol *sub;
1055 if (!ctype)
1056 continue;
1057 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1058 continue;
1059 sub = find_identifier(ident, ctype->symbol_list, offset);
1060 if (!sub)
1061 continue;
1062 *offset += sym->offset;
1063 return sub;
1066 } while ((list = list->next) != head);
1067 return NULL;
1070 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1072 struct expression *add;
1074 if (!offset)
1075 return expr;
1077 /* Create a new add-expression */
1078 add = alloc_expression(expr->pos, EXPR_BINOP);
1079 add->op = '+';
1080 add->ctype = &ptr_ctype;
1081 add->left = expr;
1082 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1083 add->right->ctype = &int_ctype;
1084 add->right->value = offset;
1086 simplify_int_binop(add, &ptr_ctype);
1087 return add;
1090 /* structure/union dereference */
1091 static struct symbol *evaluate_member_dereference(struct expression *expr)
1093 int offset;
1094 struct symbol *ctype, *member, *sym;
1095 struct expression *deref = expr->deref, *add;
1096 struct ident *ident = expr->member;
1097 unsigned int mod;
1098 int address_space;
1100 if (!evaluate_expression(deref))
1101 return NULL;
1102 if (!ident) {
1103 warn(expr->pos, "bad member name");
1104 return NULL;
1107 ctype = deref->ctype;
1108 address_space = ctype->ctype.as;
1109 mod = ctype->ctype.modifiers;
1110 if (ctype->type == SYM_NODE) {
1111 ctype = ctype->ctype.base_type;
1112 address_space |= ctype->ctype.as;
1113 mod |= ctype->ctype.modifiers;
1115 if (expr->op == SPECIAL_DEREFERENCE) {
1116 /* Arrays will degenerate into pointers for '->' */
1117 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1118 warn(expr->pos, "expected a pointer to a struct/union");
1119 return NULL;
1121 mod = ctype->ctype.modifiers;
1122 address_space = ctype->ctype.as;
1123 ctype = ctype->ctype.base_type;
1124 if (ctype->type == SYM_NODE) {
1125 mod |= ctype->ctype.modifiers;
1126 address_space |= ctype->ctype.as;
1127 ctype = ctype->ctype.base_type;
1129 } else {
1130 if (!lvalue_expression(deref)) {
1131 warn(deref->pos, "expected lvalue for member dereference");
1132 return NULL;
1134 deref = deref->unop;
1135 expr->deref = deref;
1137 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1138 warn(expr->pos, "expected structure or union");
1139 return NULL;
1141 offset = 0;
1142 member = find_identifier(ident, ctype->symbol_list, &offset);
1143 if (!member) {
1144 warn(expr->pos, "no such struct/union member");
1145 return NULL;
1148 add = evaluate_offset(deref, offset);
1150 sym = alloc_symbol(expr->pos, SYM_NODE);
1151 sym->bit_size = member->bit_size;
1152 sym->array_size = member->array_size;
1153 sym->ctype = member->ctype;
1154 sym->ctype.modifiers = mod;
1155 sym->ctype.as = address_space;
1156 ctype = member->ctype.base_type;
1157 if (ctype->type == SYM_BITFIELD) {
1158 ctype = ctype->ctype.base_type;
1159 expr->type = EXPR_BITFIELD;
1160 expr->bitpos = member->bit_offset;
1161 expr->nrbits = member->fieldwidth;
1162 expr->address = add;
1163 } else {
1164 expr->type = EXPR_PREOP;
1165 expr->op = '*';
1166 expr->unop = add;
1169 expr->ctype = sym;
1170 return sym;
1173 static struct symbol *evaluate_sizeof(struct expression *expr)
1175 int size;
1177 if (expr->cast_type) {
1178 examine_symbol_type(expr->cast_type);
1179 size = expr->cast_type->bit_size;
1180 } else {
1181 if (!evaluate_expression(expr->cast_expression))
1182 return 0;
1183 size = expr->cast_expression->ctype->bit_size;
1185 if (size & 7) {
1186 warn(expr->pos, "cannot size expression");
1187 return 0;
1189 expr->type = EXPR_VALUE;
1190 expr->value = size >> 3;
1191 expr->ctype = size_t_ctype;
1192 return size_t_ctype;
1195 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1197 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1198 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1199 return clash != 0;
1202 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1204 struct expression *expr;
1205 struct symbol_list *argument_types = fn->arguments;
1206 struct symbol *argtype;
1207 int i = 1;
1209 PREPARE_PTR_LIST(argument_types, argtype);
1210 FOR_EACH_PTR (head, expr) {
1211 struct expression **p = THIS_ADDRESS(expr);
1212 struct symbol *ctype, *target;
1213 ctype = evaluate_expression(expr);
1215 if (!ctype)
1216 return 0;
1218 if (context_clash(f, ctype))
1219 warn(expr->pos, "argument %d used in wrong context", i);
1221 ctype = degenerate(expr, ctype, p);
1223 target = argtype;
1224 if (!target && ctype->bit_size < BITS_IN_INT)
1225 target = &int_ctype;
1226 if (target) {
1227 static char where[30];
1228 examine_symbol_type(target);
1229 sprintf(where, "argument %d", i);
1230 compatible_assignment_types(expr, target, p, ctype, where);
1233 i++;
1234 NEXT_PTR_LIST(argtype);
1235 } END_FOR_EACH_PTR;
1236 FINISH_PTR_LIST(argtype);
1237 return 1;
1240 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1241 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1243 struct expression *entry;
1244 int current = 0;
1245 int max = 0;
1247 FOR_EACH_PTR(expr->expr_list, entry) {
1248 struct expression **p = THIS_ADDRESS(entry);
1250 if (entry->type == EXPR_INDEX) {
1251 current = entry->idx_to;
1252 continue;
1254 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1255 current++;
1256 if (current > max)
1257 max = current;
1258 } END_FOR_EACH_PTR;
1259 return max;
1262 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1264 struct expression *entry;
1265 struct symbol *sym;
1267 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1268 FOR_EACH_PTR(expr->expr_list, entry) {
1269 struct expression **p = THIS_ADDRESS(entry);
1271 if (entry->type == EXPR_IDENTIFIER) {
1272 struct ident *ident = entry->expr_ident;
1273 /* We special-case the "already right place" case */
1274 if (sym && sym->ident == ident)
1275 continue;
1276 RESET_PTR_LIST(sym);
1277 for (;;) {
1278 if (!sym) {
1279 warn(entry->pos, "unknown named initializer");
1280 return 0;
1282 if (sym->ident == ident)
1283 break;
1284 NEXT_PTR_LIST(sym);
1286 continue;
1289 if (!sym) {
1290 warn(expr->pos, "too many initializers for struct/union");
1291 return 0;
1294 evaluate_initializer(sym, p, offset + sym->offset);
1296 NEXT_PTR_LIST(sym);
1297 } END_FOR_EACH_PTR;
1298 FINISH_PTR_LIST(sym);
1300 return 0;
1304 * Initializers are kind of like assignments. Except
1305 * they can be a hell of a lot more complex.
1307 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1309 struct expression *expr = *ep;
1312 * Simple non-structure/array initializers are the simple
1313 * case, and look (and parse) largely like assignments.
1315 if (expr->type != EXPR_INITIALIZER) {
1316 int size = 0;
1317 struct symbol *rtype = evaluate_expression(expr);
1318 if (rtype) {
1319 struct expression *pos;
1321 // FIXME! char array[] = "string" special case
1322 // should _not_ degenerate.
1323 rtype = degenerate(expr, rtype, ep);
1324 expr = *ep;
1325 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1326 /* strings are special: char arrays */
1327 if (rtype->type == SYM_ARRAY)
1328 size = rtype->array_size;
1330 * Don't bother creating a position expression for
1331 * the simple initializer cases that don't need it.
1333 * We need a position if the initializer has a byte
1334 * offset, _or_ if we're initializing a bitfield.
1336 if (offset || ctype->fieldwidth) {
1337 pos = alloc_expression(expr->pos, EXPR_POS);
1338 pos->init_offset = offset;
1339 pos->init_sym = ctype;
1340 pos->init_expr = *ep;
1341 pos->ctype = expr->ctype;
1342 *ep = pos;
1345 return size;
1348 expr->ctype = ctype;
1349 if (ctype->type == SYM_NODE)
1350 ctype = ctype->ctype.base_type;
1352 switch (ctype->type) {
1353 case SYM_ARRAY:
1354 case SYM_PTR:
1355 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1356 case SYM_UNION:
1357 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1358 case SYM_STRUCT:
1359 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1360 default:
1361 break;
1363 warn(expr->pos, "unexpected compound initializer");
1364 return 0;
1367 static struct symbol *evaluate_cast(struct expression *expr)
1369 struct expression *target = expr->cast_expression;
1370 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1372 expr->ctype = ctype;
1373 expr->cast_type = ctype;
1376 * Special case: a cast can be followed by an
1377 * initializer, in which case we need to pass
1378 * the type value down to that initializer rather
1379 * than trying to evaluate it as an expression
1381 if (target->type == EXPR_INITIALIZER) {
1382 evaluate_initializer(ctype, &expr->cast_expression, 0);
1383 return ctype;
1386 evaluate_expression(target);
1388 /* Simplify normal integer casts.. */
1389 if (target->type == EXPR_VALUE)
1390 cast_value(expr, ctype, target, target->ctype);
1391 return ctype;
1395 * Evaluate a call expression with a symbol. This
1396 * should expand inline functions, and evaluate
1397 * builtins.
1399 static int evaluate_symbol_call(struct expression *expr)
1401 struct expression *fn = expr->fn;
1402 struct symbol *ctype = fn->ctype;
1404 if (fn->type != EXPR_PREOP)
1405 return 0;
1407 if (ctype->evaluate)
1408 return ctype->evaluate(expr);
1411 * FIXME!! We should really expand the inline functions.
1412 * For now we just mark them accessed so that they show
1413 * up on the list of used symbols.
1415 if (ctype->ctype.modifiers & MOD_INLINE) {
1416 int ret;
1417 struct symbol *curr = current_fn;
1418 current_fn = ctype->ctype.base_type;
1419 ret = inline_function(expr, ctype);
1420 current_fn = curr;
1421 return ret;
1424 return 0;
1427 static struct symbol *evaluate_call(struct expression *expr)
1429 int args, fnargs;
1430 struct symbol *ctype, *sym;
1431 struct expression *fn = expr->fn;
1432 struct expression_list *arglist = expr->args;
1434 if (!evaluate_expression(fn))
1435 return NULL;
1436 sym = ctype = fn->ctype;
1437 if (ctype->type == SYM_NODE)
1438 ctype = ctype->ctype.base_type;
1439 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1440 ctype = ctype->ctype.base_type;
1441 if (!evaluate_arguments(sym, ctype, arglist))
1442 return NULL;
1443 if (sym->type == SYM_NODE) {
1444 if (evaluate_symbol_call(expr))
1445 return expr->ctype;
1447 if (ctype->type != SYM_FN) {
1448 warn(expr->pos, "not a function");
1449 return NULL;
1451 args = expression_list_size(expr->args);
1452 fnargs = symbol_list_size(ctype->arguments);
1453 if (args < fnargs)
1454 warn(expr->pos, "not enough arguments for function");
1455 if (args > fnargs && !ctype->variadic)
1456 warn(expr->pos, "too many arguments for function");
1457 expr->ctype = ctype->ctype.base_type;
1458 return expr->ctype;
1461 struct symbol *evaluate_expression(struct expression *expr)
1463 if (!expr)
1464 return NULL;
1465 if (expr->ctype)
1466 return expr->ctype;
1468 switch (expr->type) {
1469 case EXPR_VALUE:
1470 warn(expr->pos, "value expression without a type");
1471 return NULL;
1472 case EXPR_STRING:
1473 return evaluate_string(expr);
1474 case EXPR_SYMBOL:
1475 return evaluate_symbol_expression(expr);
1476 case EXPR_BINOP:
1477 if (!evaluate_expression(expr->left))
1478 return NULL;
1479 if (!evaluate_expression(expr->right))
1480 return NULL;
1481 return evaluate_binop(expr);
1482 case EXPR_LOGICAL:
1483 return evaluate_logical(expr);
1484 case EXPR_COMMA:
1485 if (!evaluate_expression(expr->left))
1486 return NULL;
1487 if (!evaluate_expression(expr->right))
1488 return NULL;
1489 return evaluate_comma(expr);
1490 case EXPR_COMPARE:
1491 if (!evaluate_expression(expr->left))
1492 return NULL;
1493 if (!evaluate_expression(expr->right))
1494 return NULL;
1495 return evaluate_compare(expr);
1496 case EXPR_ASSIGNMENT:
1497 if (!evaluate_expression(expr->left))
1498 return NULL;
1499 if (!evaluate_expression(expr->right))
1500 return NULL;
1501 return evaluate_assignment(expr);
1502 case EXPR_PREOP:
1503 if (!evaluate_expression(expr->unop))
1504 return NULL;
1505 return evaluate_preop(expr);
1506 case EXPR_POSTOP:
1507 if (!evaluate_expression(expr->unop))
1508 return NULL;
1509 return evaluate_postop(expr);
1510 case EXPR_CAST:
1511 return evaluate_cast(expr);
1512 case EXPR_SIZEOF:
1513 return evaluate_sizeof(expr);
1514 case EXPR_DEREF:
1515 return evaluate_member_dereference(expr);
1516 case EXPR_CALL:
1517 return evaluate_call(expr);
1518 case EXPR_BITFIELD:
1519 warn(expr->pos, "bitfield generated by parser");
1520 return NULL;
1521 case EXPR_CONDITIONAL:
1522 if (!evaluate_expression(expr->conditional))
1523 return NULL;
1524 if (!evaluate_expression(expr->cond_false))
1525 return NULL;
1526 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1527 return NULL;
1528 return evaluate_conditional(expr);
1529 case EXPR_STATEMENT:
1530 expr->ctype = evaluate_statement(expr->statement);
1531 return expr->ctype;
1533 case EXPR_LABEL:
1534 expr->ctype = &ptr_ctype;
1535 return &ptr_ctype;
1537 /* These can not exist as stand-alone expressions */
1538 case EXPR_INITIALIZER:
1539 case EXPR_IDENTIFIER:
1540 case EXPR_INDEX:
1541 case EXPR_POS:
1542 warn(expr->pos, "internal front-end error: initializer in expression");
1543 return NULL;
1545 return NULL;
1548 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1550 struct symbol **last = _last;
1551 struct symbol *type = evaluate_statement(stmt);
1553 if (flags & ITERATE_LAST)
1554 *last = type;
1557 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1559 evaluate_symbol(sym);
1562 void check_duplicates(struct symbol *sym)
1564 struct symbol *next = sym;
1566 while ((next = next->same_symbol) != NULL) {
1567 const char *typediff;
1568 evaluate_symbol(next);
1569 typediff = type_difference(sym, next, 0, 0);
1570 if (typediff) {
1571 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1572 show_ident(sym->ident),
1573 input_streams[next->pos.stream].name, next->pos.line, typediff);
1574 return;
1579 struct symbol *evaluate_symbol(struct symbol *sym)
1581 struct symbol *base_type;
1583 sym = examine_symbol_type(sym);
1584 base_type = sym->ctype.base_type;
1585 if (!base_type)
1586 return NULL;
1588 /* Evaluate the initializers */
1589 if (sym->initializer) {
1590 int count = evaluate_initializer(sym, &sym->initializer, 0);
1591 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1592 int bit_size = count * base_type->ctype.base_type->bit_size;
1593 base_type->array_size = count;
1594 base_type->bit_size = bit_size;
1595 sym->array_size = count;
1596 sym->bit_size = bit_size;
1600 /* And finally, evaluate the body of the symbol too */
1601 if (base_type->type == SYM_FN) {
1602 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1603 if (base_type->stmt) {
1604 current_fn = base_type;
1605 current_contextmask = sym->ctype.contextmask;
1606 current_context = sym->ctype.context;
1607 evaluate_statement(base_type->stmt);
1611 return base_type;
1614 struct symbol *evaluate_return_expression(struct statement *stmt)
1616 struct expression *expr = stmt->expression;
1617 struct symbol *ctype, *fntype;
1619 fntype = current_fn->ctype.base_type;
1620 if (fntype == &void_ctype) {
1621 if (expr)
1622 warn(expr->pos, "return expression in void function");
1623 return NULL;
1626 if (!expr) {
1627 warn(stmt->pos, "return with no return value");
1628 return NULL;
1630 ctype = evaluate_expression(expr);
1631 if (!ctype)
1632 return NULL;
1633 ctype = degenerate(expr, ctype, &expr);
1634 expr->ctype = ctype;
1635 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1636 stmt->expression = expr;
1637 return NULL;
1640 static void evaluate_if_statement(struct statement *stmt)
1642 struct expression *expr = stmt->if_conditional;
1643 struct symbol *ctype;
1645 if (!expr)
1646 return;
1647 if (expr->type == EXPR_ASSIGNMENT)
1648 warn(expr->pos, "assignment expression in conditional");
1650 ctype = evaluate_expression(expr);
1651 if (!ctype)
1652 return;
1654 /* Simplify constant conditionals without even evaluating the false side */
1655 if (expr->type == EXPR_VALUE) {
1656 struct statement *simple;
1657 simple = expr->value ? stmt->if_true : stmt->if_false;
1659 /* Nothing? */
1660 if (!simple) {
1661 stmt->type = STMT_NONE;
1662 return;
1664 evaluate_statement(simple);
1665 *stmt = *simple;
1666 return;
1668 evaluate_statement(stmt->if_true);
1669 evaluate_statement(stmt->if_false);
1672 struct symbol *evaluate_statement(struct statement *stmt)
1674 if (!stmt)
1675 return NULL;
1677 switch (stmt->type) {
1678 case STMT_RETURN:
1679 return evaluate_return_expression(stmt);
1681 case STMT_EXPRESSION:
1682 return evaluate_expression(stmt->expression);
1684 case STMT_COMPOUND: {
1685 struct symbol *type = NULL;
1686 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1687 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1688 return type;
1690 case STMT_IF:
1691 evaluate_if_statement(stmt);
1692 return NULL;
1693 case STMT_ITERATOR:
1694 evaluate_expression(stmt->iterator_pre_condition);
1695 evaluate_expression(stmt->iterator_post_condition);
1696 evaluate_statement(stmt->iterator_pre_statement);
1697 evaluate_statement(stmt->iterator_statement);
1698 evaluate_statement(stmt->iterator_post_statement);
1699 return NULL;
1700 case STMT_SWITCH:
1701 evaluate_expression(stmt->switch_expression);
1702 evaluate_statement(stmt->switch_statement);
1703 return NULL;
1704 case STMT_CASE:
1705 evaluate_expression(stmt->case_expression);
1706 evaluate_expression(stmt->case_to);
1707 evaluate_statement(stmt->case_statement);
1708 return NULL;
1709 case STMT_LABEL:
1710 evaluate_statement(stmt->label_statement);
1711 return NULL;
1712 case STMT_GOTO:
1713 evaluate_expression(stmt->goto_expression);
1714 return NULL;
1715 case STMT_NONE:
1716 break;
1717 case STMT_ASM:
1718 /* FIXME! Do the asm parameter evaluation! */
1719 break;
1721 return NULL;
1724 long long get_expression_value(struct expression *expr)
1726 long long value, mask;
1727 struct symbol *ctype;
1729 ctype = evaluate_expression(expr);
1730 if (!ctype || expr->type != EXPR_VALUE) {
1731 warn(expr->pos, "bad constant expression");
1732 return 0;
1735 value = expr->value;
1736 mask = 1ULL << (ctype->bit_size-1);
1738 if (value & mask) {
1739 while (ctype->type != SYM_BASETYPE)
1740 ctype = ctype->ctype.base_type;
1741 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1742 value = value | mask | ~(mask-1);
1744 return value;