Don't show bad expression types.
[smatch.git] / evaluate.c
blobaa51118c1f13298ae47ddd5d8262632d9715a7d6
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Evaluate constant expressions.
7 */
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <limits.h>
18 #include "lib.h"
19 #include "parse.h"
20 #include "token.h"
21 #include "symbol.h"
22 #include "target.h"
23 #include "expression.h"
25 static struct symbol *current_fn;
26 static int current_context, current_contextmask;
28 static struct symbol *evaluate_symbol_expression(struct expression *expr)
30 struct symbol *sym = expr->symbol;
31 struct symbol *base_type;
33 if (!sym) {
34 if (preprocessing) {
35 warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
36 expr->type = EXPR_VALUE;
37 expr->value = 0;
38 expr->ctype = &int_ctype;
39 return &int_ctype;
41 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
46 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
47 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
49 base_type = sym->ctype.base_type;
50 if (!base_type) {
51 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
52 return NULL;
55 /* The type of a symbol is the symbol itself! */
56 expr->ctype = sym;
58 /* enum's can be turned into plain values */
59 if (base_type->type != SYM_ENUM) {
60 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
61 addr->symbol = sym;
62 addr->symbol_name = expr->symbol_name;
63 addr->ctype = &ptr_ctype;
64 expr->type = EXPR_PREOP;
65 expr->op = '*';
66 expr->unop = addr;
67 return sym;
69 expr->type = EXPR_VALUE;
70 expr->value = sym->value;
71 return sym;
74 static struct symbol *evaluate_string(struct expression *expr)
76 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
77 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
78 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
79 int length = expr->string->length;
81 sym->array_size = length;
82 sym->bit_size = BITS_IN_CHAR * length;
83 sym->ctype.alignment = 1;
84 sym->ctype.modifiers = MOD_STATIC | MOD_CONST;
85 sym->ctype.base_type = array;
87 array->array_size = length;
88 array->bit_size = BITS_IN_CHAR * length;
89 array->ctype.alignment = 1;
90 array->ctype.modifiers = MOD_STATIC;
91 array->ctype.base_type = &char_ctype;
93 addr->symbol = sym;
94 addr->ctype = &ptr_ctype;
96 expr->type = EXPR_PREOP;
97 expr->op = '*';
98 expr->unop = addr;
99 expr->ctype = sym;
100 return sym;
103 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
105 unsigned long lmod, rmod, mod;
107 if (left == right)
108 return left;
110 if (left->bit_size > right->bit_size)
111 return left;
113 if (right->bit_size > left->bit_size)
114 return right;
116 /* Same size integers - promote to unsigned, promote to long */
117 lmod = left->ctype.modifiers;
118 rmod = right->ctype.modifiers;
119 mod = lmod | rmod;
120 if (mod == lmod)
121 return left;
122 if (mod == rmod)
123 return right;
124 return ctype_integer(mod);
127 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
128 struct expression *old, struct symbol *oldtype)
130 int old_size = oldtype->bit_size;
131 int new_size = newtype->bit_size;
132 long long value, mask, ormask, andmask;
133 int is_signed;
135 // FIXME! We don't handle FP casts of constant values yet
136 if (newtype->ctype.base_type == &fp_type)
137 return NULL;
138 if (oldtype->ctype.base_type == &fp_type)
139 return NULL;
141 // For pointers and integers, we can just move the value around
142 expr->type = EXPR_VALUE;
143 if (old_size == new_size) {
144 expr->value = old->value;
145 return newtype;
148 // expand it to the full "long long" value
149 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
150 mask = 1ULL << (old_size-1);
151 value = old->value;
152 if (!(value & mask))
153 is_signed = 0;
154 andmask = mask | (mask-1);
155 ormask = ~andmask;
156 if (!is_signed)
157 ormask = 0;
158 value = (value & andmask) | ormask;
160 // Truncate it to the new size
161 mask = 1ULL << (new_size-1);
162 mask = mask | (mask-1);
163 expr->value = value & mask;
164 return newtype;
167 static struct expression * cast_to(struct expression *old, struct symbol *type)
169 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
170 expr->ctype = type;
171 expr->cast_type = type;
172 expr->cast_expression = old;
173 if (old->type == EXPR_VALUE)
174 cast_value(expr, type, old, old->ctype);
175 return expr;
178 static int is_ptr_type(struct symbol *type)
180 if (type->type == SYM_NODE)
181 type = type->ctype.base_type;
182 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
185 static int is_int_type(struct symbol *type)
187 if (type->type == SYM_NODE)
188 type = type->ctype.base_type;
189 return type->ctype.base_type == &int_type;
192 static struct symbol *bad_expr_type(struct expression *expr)
194 warn(expr->pos, "incompatible types for operation");
195 return NULL;
198 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
200 struct expression *left = *lp, *right = *rp;
201 struct symbol *ltype = left->ctype, *rtype = right->ctype;
203 if (ltype->type == SYM_NODE)
204 ltype = ltype->ctype.base_type;
205 if (rtype->type == SYM_NODE)
206 rtype = rtype->ctype.base_type;
207 /* Integer promotion? */
208 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
209 ltype = &int_ctype;
210 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
211 rtype = &int_ctype;
212 if (is_int_type(ltype) && is_int_type(rtype)) {
213 struct symbol *ctype = bigger_int_type(ltype, rtype);
215 /* Don't bother promoting same-size entities, it only adds clutter */
216 if (ltype->bit_size != ctype->bit_size)
217 *lp = cast_to(left, ctype);
218 if (rtype->bit_size != ctype->bit_size)
219 *rp = cast_to(right, ctype);
220 return ctype;
222 return NULL;
225 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
227 if (count >= ctype->bit_size) {
228 warn(expr->pos, "shift too big for type");
229 count &= ctype->bit_size-1;
231 return count;
235 * CAREFUL! We need to get the size and sign of the
236 * result right!
238 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
240 struct expression *left = expr->left, *right = expr->right;
241 unsigned long long v, l, r, mask;
242 signed long long s, sl, sr;
243 int is_signed, shift;
245 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
246 return;
247 l = left->value; r = right->value;
248 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
249 mask = 1ULL << (ctype->bit_size-1);
250 sl = l; sr = r;
251 if (is_signed && (sl & mask))
252 sl |= ~(mask-1);
253 if (is_signed && (sr & mask))
254 sr |= ~(mask-1);
256 switch (expr->op) {
257 case '+': v = l + r; s = v; break;
258 case '-': v = l - r; s = v; break;
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 = sl * sr; break;
263 case '/': if (!r) return; v = l / r; s = sl / sr; break;
264 case '%': if (!r) return; v = l % r; s = sl % sr; break;
265 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
266 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
267 case '<': v = l < r; s = sl < sr; break;
268 case '>': v = l > r; s = sl > sr; break;
269 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
270 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
271 case SPECIAL_EQUAL: v = l == r; s = v; break;
272 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
273 default: return;
275 if (is_signed)
276 v = s;
277 mask = mask | (mask-1);
278 expr->value = v & mask;
279 expr->type = EXPR_VALUE;
282 static struct symbol *evaluate_int_binop(struct expression *expr)
284 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
285 if (ctype) {
286 expr->ctype = ctype;
287 simplify_int_binop(expr, ctype);
288 return ctype;
290 return bad_expr_type(expr);
293 static inline int lvalue_expression(struct expression *expr)
295 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
298 /* Arrays degenerate into pointers on pointer arithmetic */
299 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
301 struct symbol *base = ctype;
303 if (ctype->type == SYM_NODE)
304 base = ctype->ctype.base_type;
305 if (base->type == SYM_ARRAY) {
306 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
307 struct expression *ptr;
309 merge_type(sym, base);
310 sym->bit_size = BITS_IN_POINTER;
311 ctype = sym;
313 ptr = *ptr_p;
314 *ptr_p = ptr->unop;
317 * This all really assumes that we got the degenerate
318 * array as an lvalue (ie a dereference). If that
319 * is not the case, then holler - because we've screwed
320 * up.
322 if (!lvalue_expression(ptr))
323 warn(ptr->pos, "internal error: strange degenerate array case");
324 ptr->ctype = ctype;
326 return ctype;
329 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
331 struct symbol *ctype;
332 struct symbol *ptr_type = ptr->ctype;
333 struct symbol *i_type = i->ctype;
334 int bit_size;
336 if (i_type->type == SYM_NODE)
337 i_type = i_type->ctype.base_type;
338 if (ptr_type->type == SYM_NODE)
339 ptr_type = ptr_type->ctype.base_type;
341 if (i_type->type == SYM_ENUM)
342 i_type = &int_ctype;
343 if (!is_int_type(i_type))
344 return bad_expr_type(expr);
346 ctype = ptr->ctype;
347 examine_symbol_type(ctype);
349 ctype = degenerate(expr, ctype, &ptr);
350 bit_size = ctype->bit_size;
352 /* Special case: adding zero commonly happens as a result of 'array[0]' */
353 if (i->type == EXPR_VALUE && !i->value) {
354 *expr = *ptr;
355 } else if (bit_size > BITS_IN_CHAR) {
356 struct expression *add = expr;
357 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
358 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
360 val->ctype = size_t_ctype;
361 val->value = bit_size >> 3;
363 mul->op = '*';
364 mul->ctype = size_t_ctype;
365 mul->left = i;
366 mul->right = val;
367 simplify_int_binop(mul, size_t_ctype);
369 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
370 add->left = ptr;
371 add->right = mul;
372 simplify_int_binop(add, ctype);
375 expr->ctype = ctype;
376 return ctype;
379 static struct symbol *evaluate_add(struct expression *expr)
381 struct expression *left = expr->left, *right = expr->right;
382 struct symbol *ltype = left->ctype, *rtype = right->ctype;
384 if (is_ptr_type(ltype))
385 return evaluate_ptr_add(expr, left, right);
387 if (is_ptr_type(rtype))
388 return evaluate_ptr_add(expr, right, left);
390 // FIXME! FP promotion
391 return evaluate_int_binop(expr);
394 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
395 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED)
397 static const char * type_difference(struct symbol *target, struct symbol *source,
398 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
400 for (;;) {
401 unsigned long mod1, mod2, diff;
402 unsigned long as1, as2;
404 if (target == source)
405 break;
406 if (!target || !source)
407 return "different types";
409 * Peel of per-node information.
410 * FIXME! Check alignment, address space, and context too here!
412 if (target->type == SYM_NODE)
413 target = target->ctype.base_type;
414 if (source->type == SYM_NODE)
415 source = source->ctype.base_type;
416 mod1 = target->ctype.modifiers;
417 as1 = target->ctype.as;
418 mod2 = source->ctype.modifiers;
419 as2 = source->ctype.as;
421 if (target->type != source->type) {
422 int type1 = target->type;
423 int type2 = source->type;
425 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
426 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
427 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
428 if (type1 != type2)
429 return "different base types";
432 /* Must be same address space to be comparable */
433 if (as1 != as2)
434 return "different address spaces";
436 /* Ignore differences in storage types, sign, or addressability */
437 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
438 if (diff) {
439 mod1 &= diff & ~target_mod_ignore;
440 mod2 &= diff & ~source_mod_ignore;
441 if (mod1 | mod2) {
442 if ((mod1 | mod2) & MOD_SIZE)
443 return "different type sizes";
444 return "different modifiers";
448 target = target->ctype.base_type;
449 source = source->ctype.base_type;
451 return NULL;
454 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
456 /* NULL expression? Just return the type of the "other side" */
457 if (r->type == EXPR_VALUE && !r->value)
458 return l->ctype;
459 if (l->type == EXPR_VALUE && !l->value)
460 return r->ctype;
461 return NULL;
465 * Ignore differences in "volatile" and "const"ness when
466 * subtracting pointers
468 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
470 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
472 const char *typediff;
473 struct symbol *ctype;
474 struct symbol *ltype = l->ctype, *rtype = r->ctype;
477 * If it is an integer subtract: the ptr add case will do the
478 * right thing.
480 if (!is_ptr_type(rtype))
481 return evaluate_ptr_add(expr, l, r);
483 ctype = ltype;
484 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
485 if (typediff) {
486 ctype = common_ptr_type(l, r);
487 if (!ctype) {
488 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
489 return NULL;
492 examine_symbol_type(ctype);
494 /* Figure out the base type we point to */
495 if (ctype->type == SYM_NODE)
496 ctype = ctype->ctype.base_type;
497 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
498 warn(expr->pos, "subtraction of functions? Share your drugs");
499 return NULL;
501 ctype = ctype->ctype.base_type;
503 expr->ctype = ssize_t_ctype;
504 if (ctype->bit_size > BITS_IN_CHAR) {
505 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
506 struct expression *div = expr;
507 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
509 val->ctype = size_t_ctype;
510 val->value = ctype->bit_size >> 3;
512 sub->op = '-';
513 sub->ctype = ssize_t_ctype;
514 sub->left = l;
515 sub->right = r;
517 div->op = '/';
518 div->left = sub;
519 div->right = val;
522 return ssize_t_ctype;
525 static struct symbol *evaluate_sub(struct expression *expr)
527 struct expression *left = expr->left, *right = expr->right;
528 struct symbol *ltype = left->ctype;
530 if (is_ptr_type(ltype))
531 return evaluate_ptr_sub(expr, left, right);
533 // FIXME! FP promotion
534 return evaluate_int_binop(expr);
537 static struct symbol *evaluate_logical(struct expression *expr)
539 struct expression *left = expr->left;
540 struct expression *right;
542 if (!evaluate_expression(left))
543 return NULL;
545 /* Do immediate short-circuiting ... */
546 if (left->type == EXPR_VALUE) {
547 if (expr->op == SPECIAL_LOGICAL_AND) {
548 if (!left->value) {
549 expr->type = EXPR_VALUE;
550 expr->value = 0;
551 expr->ctype = &bool_ctype;
552 return &bool_ctype;
554 } else {
555 if (left->value) {
556 expr->type = EXPR_VALUE;
557 expr->value = 1;
558 expr->ctype = &bool_ctype;
559 return &bool_ctype;
564 right = expr->right;
565 if (!evaluate_expression(right))
566 return NULL;
567 expr->ctype = &bool_ctype;
568 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
570 * We know the left value doesn't matter, since
571 * otherwise we would have short-circuited it..
573 expr->type = EXPR_VALUE;
574 expr->value = right->value;
577 return &bool_ctype;
580 static struct symbol *evaluate_arithmetic(struct expression *expr)
582 // FIXME! Floating-point promotion!
583 return evaluate_int_binop(expr);
586 static struct symbol *evaluate_binop(struct expression *expr)
588 switch (expr->op) {
589 // addition can take ptr+int, fp and int
590 case '+':
591 return evaluate_add(expr);
593 // subtraction can take ptr-ptr, fp and int
594 case '-':
595 return evaluate_sub(expr);
597 // Arithmetic operations can take fp and int
598 case '*': case '/': case '%':
599 return evaluate_arithmetic(expr);
601 // The rest are integer operations (bitops)
602 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
603 // '&', '^', '|'
604 default:
605 return evaluate_int_binop(expr);
609 static struct symbol *evaluate_comma(struct expression *expr)
611 expr->ctype = expr->right->ctype;
612 return expr->ctype;
615 static struct symbol *evaluate_compare(struct expression *expr)
617 struct expression *left = expr->left, *right = expr->right;
618 struct symbol *ltype = left->ctype, *rtype = right->ctype;
619 struct symbol *ctype;
621 /* Pointer types? */
622 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
623 expr->ctype = &bool_ctype;
624 // FIXME! Check the types for compatibility
625 return &bool_ctype;
628 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
629 if (ctype) {
630 simplify_int_binop(expr, ctype);
631 expr->ctype = &bool_ctype;
632 return &bool_ctype;
635 return bad_expr_type(expr);
638 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
640 /* Integer promotion? */
641 if (ltype->type == SYM_NODE)
642 ltype = ltype->ctype.base_type;
643 if (rtype->type == SYM_NODE)
644 rtype = rtype->ctype.base_type;
645 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
646 ltype = &int_ctype;
647 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
648 rtype = &int_ctype;
649 return (is_int_type(ltype) && is_int_type(rtype));
652 static int is_void_ptr(struct expression *expr)
654 return (expr->type == EXPR_VALUE &&
655 expr->value == 0);
659 * FIXME!! This shoul ddo casts, array degeneration etc..
661 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
663 struct symbol *ltype = left->ctype, *rtype = right->ctype;
665 if (ltype->type == SYM_PTR) {
666 if (is_void_ptr(right))
667 return ltype;
670 if (rtype->type == SYM_PTR) {
671 if (is_void_ptr(left))
672 return rtype;
674 return NULL;
677 static struct symbol *do_degenerate(struct expression **ep)
679 struct expression *expr = *ep;
680 return degenerate(expr, expr->ctype, ep);
683 static struct symbol * evaluate_conditional(struct expression *expr)
685 struct expression *cond, *true, *false;
686 struct symbol *ctype, *ltype, *rtype;
687 const char * typediff;
689 ctype = do_degenerate(&expr->conditional);
690 cond = expr->conditional;
692 ltype = ctype;
693 true = cond;
694 if (expr->cond_true) {
695 ltype = do_degenerate(&expr->cond_true);
696 true = expr->cond_true;
699 rtype = do_degenerate(&expr->cond_false);
700 false = expr->cond_false;
702 ctype = ltype;
703 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
704 if (typediff) {
705 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
706 if (!ctype) {
707 ctype = compatible_ptr_type(true, expr->cond_false);
708 if (!ctype) {
709 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
710 return NULL;
715 /* Simplify conditional expression.. */
716 if (cond->type == EXPR_VALUE) {
717 if (!cond->value)
718 true = false;
719 *expr = *true;
721 expr->ctype = ctype;
722 return ctype;
725 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
726 struct expression **rp, struct symbol *source, const char *where)
728 const char *typediff;
729 struct symbol *t;
730 int target_as;
732 /* It's ok if the target is more volatile or const than the source */
733 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
734 if (!typediff)
735 return 1;
737 if (compatible_integer_types(target, source)) {
738 if (target->bit_size != source->bit_size)
739 *rp = cast_to(*rp, target);
740 return 1;
743 /* Pointer destination? */
744 t = target;
745 target_as = t->ctype.as;
746 if (t->type == SYM_NODE) {
747 t = t->ctype.base_type;
748 target_as |= t->ctype.as;
750 if (t->type == SYM_PTR) {
751 struct expression *right = *rp;
752 struct symbol *s = source;
753 int source_as;
755 // NULL pointer is always ok
756 if (right->type == EXPR_VALUE && !right->value)
757 return 1;
759 /* "void *" matches anything as long as the address space is ok */
760 source_as = s->ctype.as;
761 if (s->type == SYM_NODE) {
762 s = s->ctype.base_type;
763 source_as |= s->ctype.as;
765 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
766 s = s->ctype.base_type;
767 t = t->ctype.base_type;
768 if (s == &void_ctype || t == &void_ctype)
769 return 1;
772 if (s->type == SYM_FN) {
773 typediff = type_difference(t->ctype.base_type, s, 0, 0);
774 if (!typediff)
775 return 1;
779 // FIXME!! Cast it?
780 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
781 warn(expr->pos, " expected %s", show_typename(target));
782 warn(expr->pos, " got %s", show_typename(source));
783 return 0;
787 * FIXME!! This is wrong from a double evaluation standpoint. We can't
788 * just expand the expression twice, that would make any side effects
789 * happen twice too.
791 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
793 int op = expr->op;
794 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
795 static const int op_trans[] = {
796 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
797 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
798 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
799 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
800 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
801 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
802 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
803 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
804 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
805 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
808 subexpr->left = left;
809 subexpr->right = right;
810 subexpr->op = op_trans[op - SPECIAL_BASE];
811 expr->op = '=';
812 expr->right = subexpr;
813 return evaluate_binop(subexpr);
816 static struct symbol *evaluate_assignment(struct expression *expr)
818 struct expression *left = expr->left, *right = expr->right;
819 struct symbol *ltype, *rtype;
821 ltype = left->ctype;
822 rtype = right->ctype;
823 if (expr->op != '=') {
824 rtype = evaluate_binop_assignment(expr, left, right);
825 if (!rtype)
826 return 0;
827 right = expr->right;
830 if (!lvalue_expression(left)) {
831 warn(expr->pos, "not an lvalue");
832 return NULL;
835 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
836 return 0;
838 expr->ctype = expr->left->ctype;
839 return expr->ctype;
842 static struct symbol *evaluate_addressof(struct expression *expr)
844 struct symbol *ctype, *symbol;
845 struct expression *op = expr->unop;
847 if (op->op != '*' || op->type != EXPR_PREOP) {
848 warn(expr->pos, "not addressable");
849 return NULL;
852 symbol = alloc_symbol(expr->pos, SYM_PTR);
853 symbol->ctype.alignment = POINTER_ALIGNMENT;
854 symbol->bit_size = BITS_IN_POINTER;
856 ctype = op->ctype;
857 if (ctype->type == SYM_NODE) {
858 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
859 if (ctype->ctype.modifiers & MOD_REGISTER) {
860 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
861 ctype->ctype.modifiers &= ~MOD_REGISTER;
863 symbol->ctype.modifiers = ctype->ctype.modifiers;
864 symbol->ctype.as = ctype->ctype.as;
865 symbol->ctype.context = ctype->ctype.context;
866 symbol->ctype.contextmask = ctype->ctype.contextmask;
867 ctype = ctype->ctype.base_type;
870 symbol->ctype.base_type = ctype;
871 *expr = *op->unop;
872 expr->ctype = symbol;
873 return symbol;
877 static struct symbol *evaluate_dereference(struct expression *expr)
879 struct expression *op = expr->unop;
880 struct symbol *ctype = op->ctype, *sym;
882 sym = alloc_symbol(expr->pos, SYM_NODE);
883 sym->ctype = ctype->ctype;
884 if (ctype->type == SYM_NODE) {
885 ctype = ctype->ctype.base_type;
886 merge_type(sym, ctype);
888 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
889 warn(expr->pos, "cannot derefence this type");
890 return 0;
893 ctype = ctype->ctype.base_type;
894 examine_symbol_type(ctype);
895 if (!ctype) {
896 warn(expr->pos, "undefined type");
897 return NULL;
900 sym->bit_size = ctype->bit_size;
901 sym->array_size = ctype->array_size;
903 /* Simplify: *&(expr) => (expr) */
904 if (op->type == EXPR_PREOP && op->op == '&') {
905 *expr = *op->unop;
908 expr->ctype = sym;
909 return sym;
912 static void simplify_preop(struct expression *expr)
914 struct expression *op = expr->unop;
915 unsigned long long v, mask;
917 if (op->type != EXPR_VALUE)
918 return;
919 v = op->value;
920 switch (expr->op) {
921 case '+': break;
922 case '-': v = -v; break;
923 case '!': v = !v; break;
924 case '~': v = ~v; break;
925 default: return;
927 mask = 1ULL << (expr->ctype->bit_size-1);
928 mask = mask | (mask-1);
929 expr->value = v & mask;
930 expr->type = EXPR_VALUE;
934 * Unary post-ops: x++ and x--
936 static struct symbol *evaluate_postop(struct expression *expr)
938 struct expression *op = expr->unop;
939 struct symbol *ctype = op->ctype;
941 if (!lvalue_expression(expr->unop)) {
942 warn(expr->pos, "need lvalue expression for ++/--");
943 return NULL;
945 expr->ctype = ctype;
946 return ctype;
949 static struct symbol *evaluate_preop(struct expression *expr)
951 struct symbol *ctype = expr->unop->ctype;
953 switch (expr->op) {
954 case '(':
955 case '+':
956 *expr = *expr->unop;
957 return ctype;
959 case '*':
960 return evaluate_dereference(expr);
962 case '&':
963 return evaluate_addressof(expr);
965 case SPECIAL_INCREMENT:
966 case SPECIAL_DECREMENT:
968 * From a type evaluation standpoint the pre-ops are
969 * the same as the postops
971 return evaluate_postop(expr);
973 case '!':
974 ctype = &bool_ctype;
975 break;
977 default:
978 break;
980 expr->ctype = ctype;
981 simplify_preop(expr);
982 return &bool_ctype;
985 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
987 struct ptr_list *head = (struct ptr_list *)_list;
988 struct ptr_list *list = head;
990 if (!head)
991 return NULL;
992 do {
993 int i;
994 for (i = 0; i < list->nr; i++) {
995 struct symbol *sym = (struct symbol *) list->list[i];
996 if (sym->ident) {
997 if (sym->ident != ident)
998 continue;
999 *offset = sym->offset;
1000 return sym;
1001 } else {
1002 struct symbol *ctype = sym->ctype.base_type;
1003 struct symbol *sub;
1004 if (!ctype)
1005 continue;
1006 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1007 continue;
1008 sub = find_identifier(ident, ctype->symbol_list, offset);
1009 if (!sub)
1010 continue;
1011 *offset += sym->offset;
1012 return sub;
1015 } while ((list = list->next) != head);
1016 return NULL;
1019 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1021 struct expression *add;
1023 if (!offset)
1024 return expr;
1026 /* Create a new add-expression */
1027 add = alloc_expression(expr->pos, EXPR_BINOP);
1028 add->op = '+';
1029 add->ctype = &ptr_ctype;
1030 add->left = expr;
1031 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1032 add->right->ctype = &int_ctype;
1033 add->right->value = offset;
1035 simplify_int_binop(add, &ptr_ctype);
1036 return add;
1039 /* structure/union dereference */
1040 static struct symbol *evaluate_member_dereference(struct expression *expr)
1042 int offset;
1043 struct symbol *ctype, *member, *sym;
1044 struct expression *deref = expr->deref, *add;
1045 struct ident *ident = expr->member;
1046 unsigned int mod;
1047 int address_space;
1049 if (!evaluate_expression(deref))
1050 return NULL;
1051 if (!ident) {
1052 warn(expr->pos, "bad member name");
1053 return NULL;
1056 ctype = deref->ctype;
1057 address_space = ctype->ctype.as;
1058 mod = ctype->ctype.modifiers;
1059 if (ctype->type == SYM_NODE) {
1060 ctype = ctype->ctype.base_type;
1061 address_space |= ctype->ctype.as;
1062 mod |= ctype->ctype.modifiers;
1064 if (expr->op == SPECIAL_DEREFERENCE) {
1065 /* Arrays will degenerate into pointers for '->' */
1066 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1067 warn(expr->pos, "expected a pointer to a struct/union");
1068 return NULL;
1070 mod = ctype->ctype.modifiers;
1071 address_space = ctype->ctype.as;
1072 ctype = ctype->ctype.base_type;
1073 if (ctype->type == SYM_NODE) {
1074 mod |= ctype->ctype.modifiers;
1075 address_space |= ctype->ctype.as;
1076 ctype = ctype->ctype.base_type;
1078 } else {
1079 if (!lvalue_expression(deref)) {
1080 warn(deref->pos, "expected lvalue for member dereference");
1081 return NULL;
1083 deref = deref->unop;
1084 expr->deref = deref;
1086 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1087 warn(expr->pos, "expected structure or union");
1088 return NULL;
1090 offset = 0;
1091 member = find_identifier(ident, ctype->symbol_list, &offset);
1092 if (!member) {
1093 warn(expr->pos, "no such struct/union member");
1094 return NULL;
1097 add = evaluate_offset(deref, offset);
1099 sym = alloc_symbol(expr->pos, SYM_NODE);
1100 sym->bit_size = member->bit_size;
1101 sym->array_size = member->array_size;
1102 sym->ctype = member->ctype;
1103 sym->ctype.modifiers = mod;
1104 sym->ctype.as = address_space;
1105 ctype = member->ctype.base_type;
1106 if (ctype->type == SYM_BITFIELD) {
1107 ctype = ctype->ctype.base_type;
1108 expr->type = EXPR_BITFIELD;
1109 expr->bitpos = member->bit_offset;
1110 expr->nrbits = member->fieldwidth;
1111 expr->address = add;
1112 } else {
1113 expr->type = EXPR_PREOP;
1114 expr->op = '*';
1115 expr->unop = add;
1118 expr->ctype = sym;
1119 return sym;
1122 static struct symbol *evaluate_sizeof(struct expression *expr)
1124 int size;
1126 if (expr->cast_type) {
1127 examine_symbol_type(expr->cast_type);
1128 size = expr->cast_type->bit_size;
1129 } else {
1130 if (!evaluate_expression(expr->cast_expression))
1131 return 0;
1132 size = expr->cast_expression->ctype->bit_size;
1134 if (size & 7) {
1135 warn(expr->pos, "cannot size expression");
1136 return 0;
1138 expr->type = EXPR_VALUE;
1139 expr->value = size >> 3;
1140 expr->ctype = size_t_ctype;
1141 return size_t_ctype;
1144 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
1146 struct expression *expr;
1147 struct symbol_list *argument_types = fn->arguments;
1148 struct symbol *argtype;
1149 int i = 1;
1151 PREPARE_PTR_LIST(argument_types, argtype);
1152 FOR_EACH_PTR (head, expr) {
1153 struct expression **p = THIS_ADDRESS(expr);
1154 struct symbol *ctype, *target;
1155 ctype = evaluate_expression(expr);
1157 if (!ctype)
1158 return 0;
1160 ctype = degenerate(expr, ctype, p);
1162 target = argtype;
1163 if (!target && ctype->bit_size < BITS_IN_INT)
1164 target = &int_ctype;
1165 if (target) {
1166 static char where[30];
1167 examine_symbol_type(target);
1168 sprintf(where, "argument %d", i);
1169 compatible_assignment_types(expr, target, p, ctype, where);
1172 i++;
1173 NEXT_PTR_LIST(argument_types, argtype);
1174 } END_FOR_EACH_PTR;
1175 FINISH_PTR_LIST;
1176 return 1;
1180 * FIXME! This is bogus: we need to take array index
1181 * entries into account when calculating the size of
1182 * the array.
1184 static int count_array_initializer(struct expression *expr)
1186 struct expression_list *list;
1188 if (expr->type != EXPR_INITIALIZER)
1189 return 1;
1190 list = expr->expr_list;
1191 return expression_list_size(list);
1194 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1196 return count_array_initializer(expr);
1199 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1201 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1202 return 0;
1206 * Initializers are kind of like assignments. Except
1207 * they can be a hell of a lot more complex.
1209 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1211 struct expression *expr = *ep;
1214 * Simple non-structure/array initializers are the simple
1215 * case, and look (and parse) largely like assignments.
1217 if (expr->type != EXPR_INITIALIZER) {
1218 int size = 0;
1219 struct symbol *rtype = evaluate_expression(expr);
1220 if (rtype) {
1221 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1222 /* strings are special: char arrays */
1223 if (rtype->type == SYM_ARRAY)
1224 size = rtype->array_size;
1226 return size;
1229 expr->ctype = ctype;
1230 switch (ctype->type) {
1231 case SYM_ARRAY:
1232 case SYM_PTR:
1233 return evaluate_array_initializer(ctype, expr);
1234 case SYM_UNION:
1235 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1236 case SYM_STRUCT:
1237 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1238 default:
1239 break;
1241 warn(expr->pos, "unexpected compound initializer");
1242 return 0;
1245 static struct symbol *evaluate_cast(struct expression *expr)
1247 struct expression *target = expr->cast_expression;
1248 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1250 expr->ctype = ctype;
1251 expr->cast_type = ctype;
1254 * Special case: a cast can be followed by an
1255 * initializer, in which case we need to pass
1256 * the type value down to that initializer rather
1257 * than trying to evaluate it as an expression
1259 if (target->type == EXPR_INITIALIZER) {
1260 evaluate_initializer(ctype, &expr->cast_expression);
1261 return ctype;
1264 evaluate_expression(target);
1266 /* Simplify normal integer casts.. */
1267 if (target->type == EXPR_VALUE)
1268 cast_value(expr, ctype, target, target->ctype);
1269 return ctype;
1272 static struct symbol *evaluate_call(struct expression *expr)
1274 int args, fnargs;
1275 struct symbol *ctype;
1276 struct expression *fn = expr->fn;
1277 struct expression_list *arglist = expr->args;
1279 if (!evaluate_expression(fn))
1280 return NULL;
1281 ctype = fn->ctype;
1282 if (ctype->type == SYM_NODE) {
1284 * FIXME!! We should really expand the inline functions.
1285 * For now we just mark them accessed so that they show
1286 * up on the list of used symbols.
1288 if (ctype->ctype.modifiers & MOD_INLINE)
1289 access_symbol(ctype);
1290 ctype = ctype->ctype.base_type;
1292 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1293 ctype = ctype->ctype.base_type;
1294 if (ctype->type != SYM_FN) {
1295 warn(expr->pos, "not a function");
1296 return NULL;
1298 if (!evaluate_arguments(ctype, arglist))
1299 return NULL;
1300 args = expression_list_size(expr->args);
1301 fnargs = symbol_list_size(ctype->arguments);
1302 if (args < fnargs)
1303 warn(expr->pos, "not enough arguments for function");
1304 if (args > fnargs && !ctype->variadic)
1305 warn(expr->pos, "too many arguments for function");
1306 expr->ctype = ctype->ctype.base_type;
1307 return expr->ctype;
1310 struct symbol *evaluate_expression(struct expression *expr)
1312 if (!expr)
1313 return NULL;
1314 if (expr->ctype)
1315 return expr->ctype;
1317 switch (expr->type) {
1318 case EXPR_VALUE:
1319 warn(expr->pos, "value expression without a type");
1320 return NULL;
1321 case EXPR_STRING:
1322 return evaluate_string(expr);
1323 case EXPR_SYMBOL:
1324 return evaluate_symbol_expression(expr);
1325 case EXPR_BINOP:
1326 if (!evaluate_expression(expr->left))
1327 return NULL;
1328 if (!evaluate_expression(expr->right))
1329 return NULL;
1330 return evaluate_binop(expr);
1331 case EXPR_LOGICAL:
1332 return evaluate_logical(expr);
1333 case EXPR_COMMA:
1334 if (!evaluate_expression(expr->left))
1335 return NULL;
1336 if (!evaluate_expression(expr->right))
1337 return NULL;
1338 return evaluate_comma(expr);
1339 case EXPR_COMPARE:
1340 if (!evaluate_expression(expr->left))
1341 return NULL;
1342 if (!evaluate_expression(expr->right))
1343 return NULL;
1344 return evaluate_compare(expr);
1345 case EXPR_ASSIGNMENT:
1346 if (!evaluate_expression(expr->left))
1347 return NULL;
1348 if (!evaluate_expression(expr->right))
1349 return NULL;
1350 return evaluate_assignment(expr);
1351 case EXPR_PREOP:
1352 if (!evaluate_expression(expr->unop))
1353 return NULL;
1354 return evaluate_preop(expr);
1355 case EXPR_POSTOP:
1356 if (!evaluate_expression(expr->unop))
1357 return NULL;
1358 return evaluate_postop(expr);
1359 case EXPR_CAST:
1360 return evaluate_cast(expr);
1361 case EXPR_SIZEOF:
1362 return evaluate_sizeof(expr);
1363 case EXPR_DEREF:
1364 return evaluate_member_dereference(expr);
1365 case EXPR_CALL:
1366 return evaluate_call(expr);
1367 case EXPR_BITFIELD:
1368 warn(expr->pos, "bitfield generated by parser");
1369 return NULL;
1370 case EXPR_CONDITIONAL:
1371 if (!evaluate_expression(expr->conditional))
1372 return NULL;
1373 if (!evaluate_expression(expr->cond_false))
1374 return NULL;
1375 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1376 return NULL;
1377 return evaluate_conditional(expr);
1378 case EXPR_STATEMENT:
1379 expr->ctype = evaluate_statement(expr->statement);
1380 return expr->ctype;
1382 /* These can not exist as stand-alone expressions */
1383 case EXPR_INITIALIZER:
1384 case EXPR_IDENTIFIER:
1385 case EXPR_INDEX:
1386 warn(expr->pos, "internal front-end error: initializer in expression");
1387 return NULL;
1389 return NULL;
1392 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1394 struct symbol **last = _last;
1395 struct symbol *type = evaluate_statement(stmt);
1397 if (flags & ITERATE_LAST)
1398 *last = type;
1401 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1403 evaluate_symbol(sym);
1406 struct symbol *evaluate_symbol(struct symbol *sym)
1408 struct symbol *base_type;
1410 sym = examine_symbol_type(sym);
1411 base_type = sym->ctype.base_type;
1412 if (!base_type)
1413 return NULL;
1415 /* Evaluate the initializers */
1416 if (sym->initializer) {
1417 int count = evaluate_initializer(base_type, &sym->initializer);
1418 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1419 int bit_size = count * base_type->ctype.base_type->bit_size;
1420 base_type->array_size = count;
1421 base_type->bit_size = bit_size;
1422 sym->array_size = count;
1423 sym->bit_size = bit_size;
1427 /* And finally, evaluate the body of the symbol too */
1428 if (base_type->type == SYM_FN) {
1429 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1430 if (base_type->stmt) {
1431 current_fn = base_type;
1432 current_contextmask = sym->ctype.contextmask;
1433 current_context = sym->ctype.context;
1434 evaluate_statement(base_type->stmt);
1438 return base_type;
1441 struct symbol *evaluate_return_expression(struct statement *stmt)
1443 struct expression *expr = stmt->expression;
1444 struct symbol *ctype, *fntype;
1446 fntype = current_fn->ctype.base_type;
1447 if (fntype == &void_ctype) {
1448 if (expr)
1449 warn(expr->pos, "return expression in void function");
1450 return NULL;
1453 if (!expr) {
1454 warn(stmt->pos, "return with no return value");
1455 return NULL;
1457 ctype = evaluate_expression(expr);
1458 if (!ctype)
1459 return NULL;
1460 ctype = degenerate(expr, ctype, &expr);
1461 expr->ctype = ctype;
1462 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1463 stmt->expression = expr;
1464 return NULL;
1467 struct symbol *evaluate_statement(struct statement *stmt)
1469 if (!stmt)
1470 return NULL;
1472 switch (stmt->type) {
1473 case STMT_RETURN:
1474 return evaluate_return_expression(stmt);
1476 case STMT_EXPRESSION:
1477 return evaluate_expression(stmt->expression);
1479 case STMT_COMPOUND: {
1480 struct symbol *type = NULL;
1481 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1482 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1483 return type;
1485 case STMT_IF:
1486 evaluate_expression(stmt->if_conditional);
1487 evaluate_statement(stmt->if_true);
1488 evaluate_statement(stmt->if_false);
1489 return NULL;
1490 case STMT_ITERATOR:
1491 evaluate_expression(stmt->iterator_pre_condition);
1492 evaluate_expression(stmt->iterator_post_condition);
1493 evaluate_statement(stmt->iterator_pre_statement);
1494 evaluate_statement(stmt->iterator_statement);
1495 evaluate_statement(stmt->iterator_post_statement);
1496 return NULL;
1497 case STMT_SWITCH:
1498 evaluate_expression(stmt->switch_expression);
1499 evaluate_statement(stmt->switch_statement);
1500 return NULL;
1501 case STMT_CASE:
1502 evaluate_expression(stmt->case_expression);
1503 evaluate_expression(stmt->case_to);
1504 evaluate_statement(stmt->case_statement);
1505 return NULL;
1506 case STMT_LABEL:
1507 evaluate_statement(stmt->label_statement);
1508 return NULL;
1509 case STMT_GOTO:
1510 evaluate_expression(stmt->goto_expression);
1511 return NULL;
1512 case STMT_NONE:
1513 break;
1514 case STMT_ASM:
1515 /* FIXME! Do the asm parameter evaluation! */
1516 break;
1518 return NULL;
1521 long long get_expression_value(struct expression *expr)
1523 long long value, mask;
1524 struct symbol *ctype;
1526 ctype = evaluate_expression(expr);
1527 if (!ctype || expr->type != EXPR_VALUE) {
1528 warn(expr->pos, "bad constant expression");
1529 return 0;
1532 value = expr->value;
1533 mask = 1ULL << (ctype->bit_size-1);
1535 if (value & mask) {
1536 while (ctype->type != SYM_BASETYPE)
1537 ctype = ctype->ctype.base_type;
1538 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1539 value = value | mask | ~(mask-1);
1541 return value;