Add support for evaluating builtin functions at compile time.
[smatch.git] / evaluate.c
blob78912c13e4b4d155bd98468bbd099887fee44572
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 const char *diff;
463 diff = type_difference(arg1, arg2, 0, 0);
464 if (diff) {
465 static char argdiff[80];
466 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
467 return argdiff;
469 if (!arg1)
470 break;
471 NEXT_PTR_LIST(arg1);
472 NEXT_PTR_LIST(arg2);
473 i++;
475 FINISH_PTR_LIST(arg2);
476 FINISH_PTR_LIST(arg1);
479 target = target->ctype.base_type;
480 source = source->ctype.base_type;
482 return NULL;
485 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
487 /* NULL expression? Just return the type of the "other side" */
488 if (r->type == EXPR_VALUE && !r->value)
489 return l->ctype;
490 if (l->type == EXPR_VALUE && !l->value)
491 return r->ctype;
492 return NULL;
496 * Ignore differences in "volatile" and "const"ness when
497 * subtracting pointers
499 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
501 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
503 const char *typediff;
504 struct symbol *ctype;
505 struct symbol *ltype = l->ctype, *rtype = r->ctype;
508 * If it is an integer subtract: the ptr add case will do the
509 * right thing.
511 if (!is_ptr_type(rtype))
512 return evaluate_ptr_add(expr, l, r);
514 ctype = ltype;
515 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
516 if (typediff) {
517 ctype = common_ptr_type(l, r);
518 if (!ctype) {
519 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
520 return NULL;
523 examine_symbol_type(ctype);
525 /* Figure out the base type we point to */
526 if (ctype->type == SYM_NODE)
527 ctype = ctype->ctype.base_type;
528 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
529 warn(expr->pos, "subtraction of functions? Share your drugs");
530 return NULL;
532 ctype = ctype->ctype.base_type;
534 expr->ctype = ssize_t_ctype;
535 if (ctype->bit_size > BITS_IN_CHAR) {
536 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
537 struct expression *div = expr;
538 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
540 val->ctype = size_t_ctype;
541 val->value = ctype->bit_size >> 3;
543 sub->op = '-';
544 sub->ctype = ssize_t_ctype;
545 sub->left = l;
546 sub->right = r;
548 div->op = '/';
549 div->left = sub;
550 div->right = val;
553 return ssize_t_ctype;
556 static struct symbol *evaluate_sub(struct expression *expr)
558 struct expression *left = expr->left, *right = expr->right;
559 struct symbol *ltype = left->ctype;
561 if (is_ptr_type(ltype))
562 return evaluate_ptr_sub(expr, left, right);
564 // FIXME! FP promotion
565 return evaluate_int_binop(expr);
568 static struct symbol *evaluate_logical(struct expression *expr)
570 struct expression *left = expr->left;
571 struct expression *right;
573 if (!evaluate_expression(left))
574 return NULL;
576 /* Do immediate short-circuiting ... */
577 if (left->type == EXPR_VALUE) {
578 if (expr->op == SPECIAL_LOGICAL_AND) {
579 if (!left->value) {
580 expr->type = EXPR_VALUE;
581 expr->value = 0;
582 expr->ctype = &bool_ctype;
583 return &bool_ctype;
585 } else {
586 if (left->value) {
587 expr->type = EXPR_VALUE;
588 expr->value = 1;
589 expr->ctype = &bool_ctype;
590 return &bool_ctype;
595 right = expr->right;
596 if (!evaluate_expression(right))
597 return NULL;
598 expr->ctype = &bool_ctype;
599 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
601 * We know the left value doesn't matter, since
602 * otherwise we would have short-circuited it..
604 expr->type = EXPR_VALUE;
605 expr->value = right->value;
608 return &bool_ctype;
611 static struct symbol *evaluate_arithmetic(struct expression *expr)
613 // FIXME! Floating-point promotion!
614 return evaluate_int_binop(expr);
617 static struct symbol *evaluate_binop(struct expression *expr)
619 switch (expr->op) {
620 // addition can take ptr+int, fp and int
621 case '+':
622 return evaluate_add(expr);
624 // subtraction can take ptr-ptr, fp and int
625 case '-':
626 return evaluate_sub(expr);
628 // Arithmetic operations can take fp and int
629 case '*': case '/': case '%':
630 return evaluate_arithmetic(expr);
632 // The rest are integer operations (bitops)
633 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
634 // '&', '^', '|'
635 default:
636 return evaluate_int_binop(expr);
640 static struct symbol *evaluate_comma(struct expression *expr)
642 expr->ctype = expr->right->ctype;
643 return expr->ctype;
646 static struct symbol *evaluate_compare(struct expression *expr)
648 struct expression *left = expr->left, *right = expr->right;
649 struct symbol *ltype = left->ctype, *rtype = right->ctype;
650 struct symbol *ctype;
652 /* Pointer types? */
653 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
654 expr->ctype = &bool_ctype;
655 // FIXME! Check the types for compatibility
656 return &bool_ctype;
659 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
660 if (ctype) {
661 simplify_int_binop(expr, ctype);
662 expr->ctype = &bool_ctype;
663 return &bool_ctype;
666 return bad_expr_type(expr);
669 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
671 /* Integer promotion? */
672 if (ltype->type == SYM_NODE)
673 ltype = ltype->ctype.base_type;
674 if (rtype->type == SYM_NODE)
675 rtype = rtype->ctype.base_type;
676 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
677 ltype = &int_ctype;
678 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
679 rtype = &int_ctype;
680 return (is_int_type(ltype) && is_int_type(rtype));
683 static int is_null_ptr(struct expression *expr)
685 return (expr->type == EXPR_VALUE &&
686 expr->value == 0);
690 * FIXME!! This shoul ddo casts, array degeneration etc..
692 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
694 struct symbol *ltype = left->ctype, *rtype = right->ctype;
696 if (ltype->type == SYM_PTR) {
697 if (is_null_ptr(right))
698 return ltype;
701 if (rtype->type == SYM_PTR) {
702 if (is_null_ptr(left))
703 return rtype;
705 return NULL;
708 static struct symbol *do_degenerate(struct expression **ep)
710 struct expression *expr = *ep;
711 return degenerate(expr, expr->ctype, ep);
714 static struct symbol * evaluate_conditional(struct expression *expr)
716 struct expression *cond, *true, *false;
717 struct symbol *ctype, *ltype, *rtype;
718 const char * typediff;
720 ctype = do_degenerate(&expr->conditional);
721 cond = expr->conditional;
723 ltype = ctype;
724 true = cond;
725 if (expr->cond_true) {
726 ltype = do_degenerate(&expr->cond_true);
727 true = expr->cond_true;
730 rtype = do_degenerate(&expr->cond_false);
731 false = expr->cond_false;
733 ctype = ltype;
734 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
735 if (typediff) {
736 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
737 if (!ctype) {
738 ctype = compatible_ptr_type(true, expr->cond_false);
739 if (!ctype) {
740 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
741 return NULL;
746 /* Simplify conditional expression.. */
747 if (cond->type == EXPR_VALUE) {
748 if (!cond->value)
749 true = false;
750 *expr = *true;
752 expr->ctype = ctype;
753 return ctype;
756 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
757 struct expression **rp, struct symbol *source, const char *where)
759 const char *typediff;
760 struct symbol *t;
761 int target_as;
763 /* It's ok if the target is more volatile or const than the source */
764 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
765 if (!typediff)
766 return 1;
768 if (compatible_integer_types(target, source)) {
769 if (target->bit_size != source->bit_size)
770 *rp = cast_to(*rp, target);
771 return 1;
774 /* Pointer destination? */
775 t = target;
776 target_as = t->ctype.as;
777 if (t->type == SYM_NODE) {
778 t = t->ctype.base_type;
779 target_as |= t->ctype.as;
781 if (t->type == SYM_PTR) {
782 struct expression *right = *rp;
783 struct symbol *s = source;
784 int source_as;
786 // NULL pointer is always ok
787 if (right->type == EXPR_VALUE && !right->value)
788 return 1;
790 /* "void *" matches anything as long as the address space is ok */
791 source_as = s->ctype.as;
792 if (s->type == SYM_NODE) {
793 s = s->ctype.base_type;
794 source_as |= s->ctype.as;
796 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
797 s = s->ctype.base_type;
798 t = t->ctype.base_type;
799 if (s == &void_ctype || t == &void_ctype)
800 return 1;
804 // FIXME!! Cast it?
805 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
806 warn(expr->pos, " expected %s", show_typename(target));
807 warn(expr->pos, " got %s", show_typename(source));
808 return 0;
812 * FIXME!! This is wrong from a double evaluation standpoint. We can't
813 * just expand the expression twice, that would make any side effects
814 * happen twice too.
816 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
818 int op = expr->op;
819 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
820 static const int op_trans[] = {
821 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
822 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
823 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
824 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
825 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
826 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
827 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
828 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
829 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
830 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
833 subexpr->left = left;
834 subexpr->right = right;
835 subexpr->op = op_trans[op - SPECIAL_BASE];
836 expr->op = '=';
837 expr->right = subexpr;
838 return evaluate_binop(subexpr);
841 static struct symbol *evaluate_assignment(struct expression *expr)
843 struct expression *left = expr->left, *right = expr->right;
844 struct symbol *ltype, *rtype;
846 ltype = left->ctype;
847 rtype = right->ctype;
848 if (expr->op != '=') {
849 rtype = evaluate_binop_assignment(expr, left, right);
850 if (!rtype)
851 return 0;
852 right = expr->right;
855 if (!lvalue_expression(left)) {
856 warn(expr->pos, "not an lvalue");
857 return NULL;
860 rtype = degenerate(right, rtype, &expr->right);
862 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
863 return 0;
865 expr->ctype = expr->left->ctype;
866 return expr->ctype;
869 static struct symbol *evaluate_addressof(struct expression *expr)
871 struct symbol *ctype, *symbol;
872 struct expression *op = expr->unop;
874 if (op->op != '*' || op->type != EXPR_PREOP) {
875 warn(expr->pos, "not addressable");
876 return NULL;
879 symbol = alloc_symbol(expr->pos, SYM_PTR);
880 symbol->ctype.alignment = POINTER_ALIGNMENT;
881 symbol->bit_size = BITS_IN_POINTER;
883 ctype = op->ctype;
884 if (ctype->type == SYM_NODE) {
885 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
886 if (ctype->ctype.modifiers & MOD_REGISTER) {
887 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
888 ctype->ctype.modifiers &= ~MOD_REGISTER;
890 symbol->ctype.modifiers = ctype->ctype.modifiers;
891 symbol->ctype.as = ctype->ctype.as;
892 symbol->ctype.context = ctype->ctype.context;
893 symbol->ctype.contextmask = ctype->ctype.contextmask;
894 ctype = ctype->ctype.base_type;
897 symbol->ctype.base_type = ctype;
898 *expr = *op->unop;
899 expr->ctype = symbol;
900 return symbol;
904 static struct symbol *evaluate_dereference(struct expression *expr)
906 struct expression *op = expr->unop;
907 struct symbol *ctype = op->ctype, *sym;
909 sym = alloc_symbol(expr->pos, SYM_NODE);
910 if (ctype->type == SYM_NODE) {
911 ctype = ctype->ctype.base_type;
912 merge_type(sym, ctype);
914 sym->ctype = ctype->ctype;
915 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
916 warn(expr->pos, "cannot derefence this type");
917 return 0;
920 ctype = ctype->ctype.base_type;
921 examine_symbol_type(ctype);
922 if (!ctype) {
923 warn(expr->pos, "undefined type");
924 return NULL;
927 sym->bit_size = ctype->bit_size;
928 sym->array_size = ctype->array_size;
930 /* Simplify: *&(expr) => (expr) */
931 if (op->type == EXPR_PREOP && op->op == '&') {
932 *expr = *op->unop;
935 expr->ctype = sym;
936 return sym;
939 static void simplify_preop(struct expression *expr)
941 struct expression *op = expr->unop;
942 unsigned long long v, mask;
944 if (op->type != EXPR_VALUE)
945 return;
946 v = op->value;
947 switch (expr->op) {
948 case '+': break;
949 case '-': v = -v; break;
950 case '!': v = !v; break;
951 case '~': v = ~v; break;
952 default: return;
954 mask = 1ULL << (expr->ctype->bit_size-1);
955 mask = mask | (mask-1);
956 expr->value = v & mask;
957 expr->type = EXPR_VALUE;
961 * Unary post-ops: x++ and x--
963 static struct symbol *evaluate_postop(struct expression *expr)
965 struct expression *op = expr->unop;
966 struct symbol *ctype = op->ctype;
968 if (!lvalue_expression(expr->unop)) {
969 warn(expr->pos, "need lvalue expression for ++/--");
970 return NULL;
972 expr->ctype = ctype;
973 return ctype;
976 static struct symbol *evaluate_preop(struct expression *expr)
978 struct symbol *ctype = expr->unop->ctype;
980 switch (expr->op) {
981 case '(':
982 case '+':
983 *expr = *expr->unop;
984 return ctype;
986 case '*':
987 return evaluate_dereference(expr);
989 case '&':
990 return evaluate_addressof(expr);
992 case SPECIAL_INCREMENT:
993 case SPECIAL_DECREMENT:
995 * From a type evaluation standpoint the pre-ops are
996 * the same as the postops
998 return evaluate_postop(expr);
1000 case '!':
1001 ctype = &bool_ctype;
1002 break;
1004 default:
1005 break;
1007 expr->ctype = ctype;
1008 simplify_preop(expr);
1009 return &bool_ctype;
1012 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1014 struct ptr_list *head = (struct ptr_list *)_list;
1015 struct ptr_list *list = head;
1017 if (!head)
1018 return NULL;
1019 do {
1020 int i;
1021 for (i = 0; i < list->nr; i++) {
1022 struct symbol *sym = (struct symbol *) list->list[i];
1023 if (sym->ident) {
1024 if (sym->ident != ident)
1025 continue;
1026 *offset = sym->offset;
1027 return sym;
1028 } else {
1029 struct symbol *ctype = sym->ctype.base_type;
1030 struct symbol *sub;
1031 if (!ctype)
1032 continue;
1033 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1034 continue;
1035 sub = find_identifier(ident, ctype->symbol_list, offset);
1036 if (!sub)
1037 continue;
1038 *offset += sym->offset;
1039 return sub;
1042 } while ((list = list->next) != head);
1043 return NULL;
1046 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1048 struct expression *add;
1050 if (!offset)
1051 return expr;
1053 /* Create a new add-expression */
1054 add = alloc_expression(expr->pos, EXPR_BINOP);
1055 add->op = '+';
1056 add->ctype = &ptr_ctype;
1057 add->left = expr;
1058 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1059 add->right->ctype = &int_ctype;
1060 add->right->value = offset;
1062 simplify_int_binop(add, &ptr_ctype);
1063 return add;
1066 /* structure/union dereference */
1067 static struct symbol *evaluate_member_dereference(struct expression *expr)
1069 int offset;
1070 struct symbol *ctype, *member, *sym;
1071 struct expression *deref = expr->deref, *add;
1072 struct ident *ident = expr->member;
1073 unsigned int mod;
1074 int address_space;
1076 if (!evaluate_expression(deref))
1077 return NULL;
1078 if (!ident) {
1079 warn(expr->pos, "bad member name");
1080 return NULL;
1083 ctype = deref->ctype;
1084 address_space = ctype->ctype.as;
1085 mod = ctype->ctype.modifiers;
1086 if (ctype->type == SYM_NODE) {
1087 ctype = ctype->ctype.base_type;
1088 address_space |= ctype->ctype.as;
1089 mod |= ctype->ctype.modifiers;
1091 if (expr->op == SPECIAL_DEREFERENCE) {
1092 /* Arrays will degenerate into pointers for '->' */
1093 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1094 warn(expr->pos, "expected a pointer to a struct/union");
1095 return NULL;
1097 mod = ctype->ctype.modifiers;
1098 address_space = ctype->ctype.as;
1099 ctype = ctype->ctype.base_type;
1100 if (ctype->type == SYM_NODE) {
1101 mod |= ctype->ctype.modifiers;
1102 address_space |= ctype->ctype.as;
1103 ctype = ctype->ctype.base_type;
1105 } else {
1106 if (!lvalue_expression(deref)) {
1107 warn(deref->pos, "expected lvalue for member dereference");
1108 return NULL;
1110 deref = deref->unop;
1111 expr->deref = deref;
1113 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1114 warn(expr->pos, "expected structure or union");
1115 return NULL;
1117 offset = 0;
1118 member = find_identifier(ident, ctype->symbol_list, &offset);
1119 if (!member) {
1120 warn(expr->pos, "no such struct/union member");
1121 return NULL;
1124 add = evaluate_offset(deref, offset);
1126 sym = alloc_symbol(expr->pos, SYM_NODE);
1127 sym->bit_size = member->bit_size;
1128 sym->array_size = member->array_size;
1129 sym->ctype = member->ctype;
1130 sym->ctype.modifiers = mod;
1131 sym->ctype.as = address_space;
1132 ctype = member->ctype.base_type;
1133 if (ctype->type == SYM_BITFIELD) {
1134 ctype = ctype->ctype.base_type;
1135 expr->type = EXPR_BITFIELD;
1136 expr->bitpos = member->bit_offset;
1137 expr->nrbits = member->fieldwidth;
1138 expr->address = add;
1139 } else {
1140 expr->type = EXPR_PREOP;
1141 expr->op = '*';
1142 expr->unop = add;
1145 expr->ctype = sym;
1146 return sym;
1149 static struct symbol *evaluate_sizeof(struct expression *expr)
1151 int size;
1153 if (expr->cast_type) {
1154 examine_symbol_type(expr->cast_type);
1155 size = expr->cast_type->bit_size;
1156 } else {
1157 if (!evaluate_expression(expr->cast_expression))
1158 return 0;
1159 size = expr->cast_expression->ctype->bit_size;
1161 if (size & 7) {
1162 warn(expr->pos, "cannot size expression");
1163 return 0;
1165 expr->type = EXPR_VALUE;
1166 expr->value = size >> 3;
1167 expr->ctype = size_t_ctype;
1168 return size_t_ctype;
1171 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1173 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1174 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1175 return clash != 0;
1178 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1180 struct expression *expr;
1181 struct symbol_list *argument_types = fn->arguments;
1182 struct symbol *argtype;
1183 int i = 1;
1185 PREPARE_PTR_LIST(argument_types, argtype);
1186 FOR_EACH_PTR (head, expr) {
1187 struct expression **p = THIS_ADDRESS(expr);
1188 struct symbol *ctype, *target;
1189 ctype = evaluate_expression(expr);
1191 if (!ctype)
1192 return 0;
1194 if (context_clash(f, ctype))
1195 warn(expr->pos, "argument %d used in wrong context", i);
1197 ctype = degenerate(expr, ctype, p);
1199 target = argtype;
1200 if (!target && ctype->bit_size < BITS_IN_INT)
1201 target = &int_ctype;
1202 if (target) {
1203 static char where[30];
1204 examine_symbol_type(target);
1205 sprintf(where, "argument %d", i);
1206 compatible_assignment_types(expr, target, p, ctype, where);
1209 i++;
1210 NEXT_PTR_LIST(argtype);
1211 } END_FOR_EACH_PTR;
1212 FINISH_PTR_LIST(argtype);
1213 return 1;
1216 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1217 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1219 struct expression *entry;
1220 int current = 0;
1221 int max = 0;
1223 FOR_EACH_PTR(expr->expr_list, entry) {
1224 struct expression **p = THIS_ADDRESS(entry);
1226 if (entry->type == EXPR_INDEX) {
1227 current = entry->idx_to;
1228 continue;
1230 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1231 current++;
1232 if (current > max)
1233 max = current;
1234 } END_FOR_EACH_PTR;
1235 return max;
1238 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1240 struct expression *entry;
1241 struct symbol *sym;
1243 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1244 FOR_EACH_PTR(expr->expr_list, entry) {
1245 struct expression **p = THIS_ADDRESS(entry);
1247 if (entry->type == EXPR_IDENTIFIER) {
1248 struct ident *ident = entry->expr_ident;
1249 /* We special-case the "already right place" case */
1250 if (sym && sym->ident == ident)
1251 continue;
1252 RESET_PTR_LIST(sym);
1253 for (;;) {
1254 if (!sym) {
1255 warn(entry->pos, "unknown named initializer");
1256 return 0;
1258 if (sym->ident == ident)
1259 break;
1260 NEXT_PTR_LIST(sym);
1262 continue;
1265 if (!sym) {
1266 warn(expr->pos, "too many initializers for struct/union");
1267 return 0;
1270 evaluate_initializer(sym, p, offset + sym->offset);
1272 NEXT_PTR_LIST(sym);
1273 } END_FOR_EACH_PTR;
1274 FINISH_PTR_LIST(sym);
1276 return 0;
1280 * Initializers are kind of like assignments. Except
1281 * they can be a hell of a lot more complex.
1283 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1285 struct expression *expr = *ep;
1288 * Simple non-structure/array initializers are the simple
1289 * case, and look (and parse) largely like assignments.
1291 if (expr->type != EXPR_INITIALIZER) {
1292 int size = 0;
1293 struct symbol *rtype = evaluate_expression(expr);
1294 if (rtype) {
1295 struct expression *pos;
1297 // FIXME! char array[] = "string" special case
1298 // should _not_ degenerate.
1299 rtype = degenerate(expr, rtype, ep);
1300 expr = *ep;
1301 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1302 /* strings are special: char arrays */
1303 if (rtype->type == SYM_ARRAY)
1304 size = rtype->array_size;
1306 * Don't bother creating a position expression for
1307 * the simple initializer cases that don't need it.
1309 * We need a position if the initializer has a byte
1310 * offset, _or_ if we're initializing a bitfield.
1312 if (offset || ctype->fieldwidth) {
1313 pos = alloc_expression(expr->pos, EXPR_POS);
1314 pos->init_offset = offset;
1315 pos->init_sym = ctype;
1316 pos->init_expr = *ep;
1317 pos->ctype = expr->ctype;
1318 *ep = pos;
1321 return size;
1324 expr->ctype = ctype;
1325 if (ctype->type == SYM_NODE)
1326 ctype = ctype->ctype.base_type;
1328 switch (ctype->type) {
1329 case SYM_ARRAY:
1330 case SYM_PTR:
1331 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1332 case SYM_UNION:
1333 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1334 case SYM_STRUCT:
1335 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1336 default:
1337 break;
1339 warn(expr->pos, "unexpected compound initializer");
1340 return 0;
1343 static struct symbol *evaluate_cast(struct expression *expr)
1345 struct expression *target = expr->cast_expression;
1346 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1348 expr->ctype = ctype;
1349 expr->cast_type = ctype;
1352 * Special case: a cast can be followed by an
1353 * initializer, in which case we need to pass
1354 * the type value down to that initializer rather
1355 * than trying to evaluate it as an expression
1357 if (target->type == EXPR_INITIALIZER) {
1358 evaluate_initializer(ctype, &expr->cast_expression, 0);
1359 return ctype;
1362 evaluate_expression(target);
1364 /* Simplify normal integer casts.. */
1365 if (target->type == EXPR_VALUE)
1366 cast_value(expr, ctype, target, target->ctype);
1367 return ctype;
1371 * Evaluate a call expression with a symbol. This
1372 * should expand inline functions, and evaluate
1373 * builtins.
1375 static struct symbol *evaluate_symbol_call(struct expression *expr)
1377 struct expression *fn = expr->fn;
1378 struct symbol *ctype = fn->ctype;
1380 if (fn->type != EXPR_PREOP)
1381 goto out;
1383 if (ctype->evaluate)
1384 return ctype->evaluate(expr);
1387 * FIXME!! We should really expand the inline functions.
1388 * For now we just mark them accessed so that they show
1389 * up on the list of used symbols.
1391 if (ctype->ctype.modifiers & MOD_INLINE)
1392 access_symbol(ctype);
1393 out:
1394 return ctype->ctype.base_type;
1397 static struct symbol *evaluate_call(struct expression *expr)
1399 int args, fnargs;
1400 struct symbol *ctype, *sym;
1401 struct expression *fn = expr->fn;
1402 struct expression_list *arglist = expr->args;
1404 if (!evaluate_expression(fn))
1405 return NULL;
1406 sym = ctype = fn->ctype;
1407 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1408 ctype = ctype->ctype.base_type;
1409 if (!evaluate_arguments(sym, ctype, arglist))
1410 return NULL;
1411 if (sym->type == SYM_NODE) {
1412 ctype = evaluate_symbol_call(expr);
1413 if (!ctype)
1414 return expr->ctype;
1416 if (ctype->type != SYM_FN) {
1417 warn(expr->pos, "not a function");
1418 return NULL;
1420 args = expression_list_size(expr->args);
1421 fnargs = symbol_list_size(ctype->arguments);
1422 if (args < fnargs)
1423 warn(expr->pos, "not enough arguments for function");
1424 if (args > fnargs && !ctype->variadic)
1425 warn(expr->pos, "too many arguments for function");
1426 expr->ctype = ctype->ctype.base_type;
1427 return expr->ctype;
1430 struct symbol *evaluate_expression(struct expression *expr)
1432 if (!expr)
1433 return NULL;
1434 if (expr->ctype)
1435 return expr->ctype;
1437 switch (expr->type) {
1438 case EXPR_VALUE:
1439 warn(expr->pos, "value expression without a type");
1440 return NULL;
1441 case EXPR_STRING:
1442 return evaluate_string(expr);
1443 case EXPR_SYMBOL:
1444 return evaluate_symbol_expression(expr);
1445 case EXPR_BINOP:
1446 if (!evaluate_expression(expr->left))
1447 return NULL;
1448 if (!evaluate_expression(expr->right))
1449 return NULL;
1450 return evaluate_binop(expr);
1451 case EXPR_LOGICAL:
1452 return evaluate_logical(expr);
1453 case EXPR_COMMA:
1454 if (!evaluate_expression(expr->left))
1455 return NULL;
1456 if (!evaluate_expression(expr->right))
1457 return NULL;
1458 return evaluate_comma(expr);
1459 case EXPR_COMPARE:
1460 if (!evaluate_expression(expr->left))
1461 return NULL;
1462 if (!evaluate_expression(expr->right))
1463 return NULL;
1464 return evaluate_compare(expr);
1465 case EXPR_ASSIGNMENT:
1466 if (!evaluate_expression(expr->left))
1467 return NULL;
1468 if (!evaluate_expression(expr->right))
1469 return NULL;
1470 return evaluate_assignment(expr);
1471 case EXPR_PREOP:
1472 if (!evaluate_expression(expr->unop))
1473 return NULL;
1474 return evaluate_preop(expr);
1475 case EXPR_POSTOP:
1476 if (!evaluate_expression(expr->unop))
1477 return NULL;
1478 return evaluate_postop(expr);
1479 case EXPR_CAST:
1480 return evaluate_cast(expr);
1481 case EXPR_SIZEOF:
1482 return evaluate_sizeof(expr);
1483 case EXPR_DEREF:
1484 return evaluate_member_dereference(expr);
1485 case EXPR_CALL:
1486 return evaluate_call(expr);
1487 case EXPR_BITFIELD:
1488 warn(expr->pos, "bitfield generated by parser");
1489 return NULL;
1490 case EXPR_CONDITIONAL:
1491 if (!evaluate_expression(expr->conditional))
1492 return NULL;
1493 if (!evaluate_expression(expr->cond_false))
1494 return NULL;
1495 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1496 return NULL;
1497 return evaluate_conditional(expr);
1498 case EXPR_STATEMENT:
1499 expr->ctype = evaluate_statement(expr->statement);
1500 return expr->ctype;
1502 /* These can not exist as stand-alone expressions */
1503 case EXPR_INITIALIZER:
1504 case EXPR_IDENTIFIER:
1505 case EXPR_INDEX:
1506 case EXPR_POS:
1507 warn(expr->pos, "internal front-end error: initializer in expression");
1508 return NULL;
1510 return NULL;
1513 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1515 struct symbol **last = _last;
1516 struct symbol *type = evaluate_statement(stmt);
1518 if (flags & ITERATE_LAST)
1519 *last = type;
1522 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1524 evaluate_symbol(sym);
1527 void check_duplicates(struct symbol *sym)
1529 struct symbol *next = sym;
1531 while ((next = next->same_symbol) != NULL) {
1532 const char *typediff;
1533 evaluate_symbol(next);
1534 typediff = type_difference(sym, next, 0, 0);
1535 if (typediff) {
1536 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1537 show_ident(sym->ident),
1538 input_streams[next->pos.stream].name, next->pos.line, typediff);
1539 return;
1544 struct symbol *evaluate_symbol(struct symbol *sym)
1546 struct symbol *base_type;
1548 sym = examine_symbol_type(sym);
1549 base_type = sym->ctype.base_type;
1550 if (!base_type)
1551 return NULL;
1553 /* Evaluate the initializers */
1554 if (sym->initializer) {
1555 int count = evaluate_initializer(sym, &sym->initializer, 0);
1556 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1557 int bit_size = count * base_type->ctype.base_type->bit_size;
1558 base_type->array_size = count;
1559 base_type->bit_size = bit_size;
1560 sym->array_size = count;
1561 sym->bit_size = bit_size;
1565 /* And finally, evaluate the body of the symbol too */
1566 if (base_type->type == SYM_FN) {
1567 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1568 if (base_type->stmt) {
1569 current_fn = base_type;
1570 current_contextmask = sym->ctype.contextmask;
1571 current_context = sym->ctype.context;
1572 evaluate_statement(base_type->stmt);
1576 return base_type;
1579 struct symbol *evaluate_return_expression(struct statement *stmt)
1581 struct expression *expr = stmt->expression;
1582 struct symbol *ctype, *fntype;
1584 fntype = current_fn->ctype.base_type;
1585 if (fntype == &void_ctype) {
1586 if (expr)
1587 warn(expr->pos, "return expression in void function");
1588 return NULL;
1591 if (!expr) {
1592 warn(stmt->pos, "return with no return value");
1593 return NULL;
1595 ctype = evaluate_expression(expr);
1596 if (!ctype)
1597 return NULL;
1598 ctype = degenerate(expr, ctype, &expr);
1599 expr->ctype = ctype;
1600 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1601 stmt->expression = expr;
1602 return NULL;
1605 struct symbol *evaluate_statement(struct statement *stmt)
1607 if (!stmt)
1608 return NULL;
1610 switch (stmt->type) {
1611 case STMT_RETURN:
1612 return evaluate_return_expression(stmt);
1614 case STMT_EXPRESSION:
1615 return evaluate_expression(stmt->expression);
1617 case STMT_COMPOUND: {
1618 struct symbol *type = NULL;
1619 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1620 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1621 return type;
1623 case STMT_IF:
1624 evaluate_expression(stmt->if_conditional);
1625 evaluate_statement(stmt->if_true);
1626 evaluate_statement(stmt->if_false);
1627 return NULL;
1628 case STMT_ITERATOR:
1629 evaluate_expression(stmt->iterator_pre_condition);
1630 evaluate_expression(stmt->iterator_post_condition);
1631 evaluate_statement(stmt->iterator_pre_statement);
1632 evaluate_statement(stmt->iterator_statement);
1633 evaluate_statement(stmt->iterator_post_statement);
1634 return NULL;
1635 case STMT_SWITCH:
1636 evaluate_expression(stmt->switch_expression);
1637 evaluate_statement(stmt->switch_statement);
1638 return NULL;
1639 case STMT_CASE:
1640 evaluate_expression(stmt->case_expression);
1641 evaluate_expression(stmt->case_to);
1642 evaluate_statement(stmt->case_statement);
1643 return NULL;
1644 case STMT_LABEL:
1645 evaluate_statement(stmt->label_statement);
1646 return NULL;
1647 case STMT_GOTO:
1648 evaluate_expression(stmt->goto_expression);
1649 return NULL;
1650 case STMT_NONE:
1651 break;
1652 case STMT_ASM:
1653 /* FIXME! Do the asm parameter evaluation! */
1654 break;
1656 return NULL;
1659 long long get_expression_value(struct expression *expr)
1661 long long value, mask;
1662 struct symbol *ctype;
1664 ctype = evaluate_expression(expr);
1665 if (!ctype || expr->type != EXPR_VALUE) {
1666 warn(expr->pos, "bad constant expression");
1667 return 0;
1670 value = expr->value;
1671 mask = 1ULL << (ctype->bit_size-1);
1673 if (value & mask) {
1674 while (ctype->type != SYM_BASETYPE)
1675 ctype = ctype->ctype.base_type;
1676 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1677 value = value | mask | ~(mask-1);
1679 return value;