[PATCH] evaluate_conditional() prepared for FP
[smatch.git] / evaluate.c
blob990a5957af105fc48f63e0fb9e01e42e56fd2dda
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
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 (sym->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 = NULL; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
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 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = NULL;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static inline struct symbol *integer_promotion(struct symbol *type)
113 unsigned long mod = type->ctype.modifiers;
114 int width;
116 if (type->type == SYM_ENUM)
117 return &int_ctype;
118 else if (type->type == SYM_BITFIELD) {
119 mod = type->ctype.base_type->ctype.modifiers;
120 width = type->fieldwidth;
121 } else if (mod & (MOD_CHAR | MOD_SHORT))
122 width = type->bit_size;
123 else
124 return type;
125 if (mod & MOD_UNSIGNED && width == bits_in_int)
126 return &uint_ctype;
127 return &int_ctype;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static struct expression * cast_to(struct expression *old, struct symbol *type)
178 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
179 expr->ctype = type;
180 expr->cast_type = type;
181 expr->cast_expression = old;
182 return expr;
185 static int is_type_type(struct symbol *type)
187 return (type->ctype.modifiers & MOD_TYPE) != 0;
190 static int is_ptr_type(struct symbol *type)
192 if (type->type == SYM_NODE)
193 type = type->ctype.base_type;
194 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
197 static inline int is_int_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return (type->type == SYM_ENUM) ||
202 (type->type == SYM_BITFIELD) ||
203 type->ctype.base_type == &int_type;
206 static struct symbol *bad_expr_type(struct expression *expr)
208 warn(expr->pos, "incompatible types for operation");
209 return NULL;
212 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
214 struct expression *left = *lp, *right = *rp;
215 struct symbol *ltype = left->ctype, *rtype = right->ctype;
217 if (ltype->type == SYM_NODE)
218 ltype = ltype->ctype.base_type;
219 if (rtype->type == SYM_NODE)
220 rtype = rtype->ctype.base_type;
221 if (is_int_type(ltype) && is_int_type(rtype)) {
222 struct symbol *ctype = bigger_int_type(ltype, rtype);
224 /* Don't bother promoting same-size entities, it only adds clutter */
225 if (ltype->bit_size != ctype->bit_size)
226 *lp = cast_to(left, ctype);
227 if (rtype->bit_size != ctype->bit_size)
228 *rp = cast_to(right, ctype);
229 return ctype;
231 return NULL;
234 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
236 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
237 if (ctype) {
238 expr->ctype = ctype;
239 return ctype;
241 return bad_expr_type(expr);
244 static inline int lvalue_expression(struct expression *expr)
246 while (expr->type == EXPR_CAST)
247 expr = expr->cast_expression;
248 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
251 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
253 struct symbol *ctype;
254 struct symbol *ptr_type = ptr->ctype;
255 int bit_size;
257 if (ptr_type->type == SYM_NODE)
258 ptr_type = ptr_type->ctype.base_type;
260 if (!is_int_type(i->ctype))
261 return bad_expr_type(expr);
263 ctype = ptr->ctype;
264 examine_symbol_type(ctype);
266 ctype = degenerate(ptr);
267 if (!ctype->ctype.base_type) {
268 warn(expr->pos, "missing type information");
269 return NULL;
272 /* Get the size of whatever the pointer points to */
273 ptr_type = ctype;
274 if (ptr_type->type == SYM_NODE)
275 ptr_type = ptr_type->ctype.base_type;
276 if (ptr_type->type == SYM_PTR)
277 ptr_type = ptr_type->ctype.base_type;
278 bit_size = ptr_type->bit_size;
280 /* Special case: adding zero commonly happens as a result of 'array[0]' */
281 if (i->type == EXPR_VALUE && !i->value) {
282 *expr = *ptr;
283 } else if (bit_size > bits_in_char) {
284 struct expression *add = expr;
285 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
286 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
288 val->ctype = size_t_ctype;
289 val->value = bit_size >> 3;
291 mul->op = '*';
292 mul->ctype = size_t_ctype;
293 mul->left = i;
294 mul->right = val;
296 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
297 add->left = ptr;
298 add->right = mul;
301 expr->ctype = ctype;
302 return ctype;
305 static struct symbol *evaluate_add(struct expression *expr)
307 struct expression *left = expr->left, *right = expr->right;
308 struct symbol *ltype = left->ctype, *rtype = right->ctype;
310 if (is_ptr_type(ltype))
311 return evaluate_ptr_add(expr, left, right);
313 if (is_ptr_type(rtype))
314 return evaluate_ptr_add(expr, right, left);
316 // FIXME! FP promotion
317 return evaluate_arith(expr, 1);
320 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
321 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
323 const char * type_difference(struct symbol *target, struct symbol *source,
324 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
326 for (;;) {
327 unsigned long mod1, mod2, diff;
328 unsigned long as1, as2;
329 int type1, type2;
330 struct symbol *base1, *base2;
332 if (target == source)
333 break;
334 if (!target || !source)
335 return "different types";
337 * Peel of per-node information.
338 * FIXME! Check alignment and context too here!
340 mod1 = target->ctype.modifiers;
341 as1 = target->ctype.as;
342 mod2 = source->ctype.modifiers;
343 as2 = source->ctype.as;
344 if (target->type == SYM_NODE) {
345 target = target->ctype.base_type;
346 if (!target)
347 return "bad types";
348 if (target->type == SYM_PTR) {
349 mod1 = 0;
350 as1 = 0;
352 mod1 |= target->ctype.modifiers;
353 as1 |= target->ctype.as;
355 if (source->type == SYM_NODE) {
356 source = source->ctype.base_type;
357 if (!source)
358 return "bad types";
359 if (source->type == SYM_PTR) {
360 mod2 = 0;
361 as2 = 0;
363 mod2 |= source->ctype.modifiers;
364 as2 |= source->ctype.as;
367 if (target == source)
368 break;
369 if (!target || !source)
370 return "different types";
372 type1 = target->type;
373 base1 = target->ctype.base_type;
375 type2 = source->type;
376 base2 = source->ctype.base_type;
379 * Pointers to functions compare as the function itself
381 if (type1 == SYM_PTR && base1) {
382 switch (base1->type) {
383 case SYM_FN:
384 type1 = SYM_FN;
385 target = base1;
386 base1 = base1->ctype.base_type;
387 default:
388 /* nothing */;
391 if (type2 == SYM_PTR && base2) {
392 switch (base2->type) {
393 case SYM_FN:
394 type2 = SYM_FN;
395 source = base2;
396 base2 = base2->ctype.base_type;
397 default:
398 /* nothing */;
402 /* Arrays degenerate to pointers for type comparisons */
403 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
404 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
406 if (type1 != type2)
407 return "different base types";
409 /* Must be same address space to be comparable */
410 if (as1 != as2)
411 return "different address spaces";
413 /* Ignore differences in storage types, sign, or addressability */
414 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
415 if (diff) {
416 mod1 &= diff & ~target_mod_ignore;
417 mod2 &= diff & ~source_mod_ignore;
418 if (mod1 | mod2) {
419 if ((mod1 | mod2) & MOD_SIZE)
420 return "different type sizes";
421 return "different modifiers";
425 if (type1 == SYM_FN) {
426 int i;
427 struct symbol *arg1, *arg2;
428 if (base1->variadic != base2->variadic)
429 return "incompatible variadic arguments";
430 PREPARE_PTR_LIST(target->arguments, arg1);
431 PREPARE_PTR_LIST(source->arguments, arg2);
432 i = 1;
433 for (;;) {
434 const char *diff;
435 diff = type_difference(arg1, arg2, 0, 0);
436 if (diff) {
437 static char argdiff[80];
438 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
439 return argdiff;
441 if (!arg1)
442 break;
443 NEXT_PTR_LIST(arg1);
444 NEXT_PTR_LIST(arg2);
445 i++;
447 FINISH_PTR_LIST(arg2);
448 FINISH_PTR_LIST(arg1);
451 target = base1;
452 source = base2;
454 return NULL;
457 static int is_null_ptr(struct expression *expr)
459 if (expr->type != EXPR_VALUE || expr->value)
460 return 0;
461 if (!is_ptr_type(expr->ctype))
462 warn(expr->pos, "Using plain integer as NULL pointer");
463 return 1;
466 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
468 /* NULL expression? Just return the type of the "other side" */
469 if (is_null_ptr(r))
470 return l->ctype;
471 if (is_null_ptr(l))
472 return r->ctype;
473 return NULL;
477 * Ignore differences in "volatile" and "const"ness when
478 * subtracting pointers
480 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
482 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
484 const char *typediff;
485 struct symbol *ctype;
486 struct symbol *ltype, *rtype;
488 ltype = degenerate(l);
489 rtype = degenerate(r);
492 * If it is an integer subtract: the ptr add case will do the
493 * right thing.
495 if (!is_ptr_type(rtype))
496 return evaluate_ptr_add(expr, l, r);
498 ctype = ltype;
499 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
500 if (typediff) {
501 ctype = common_ptr_type(l, r);
502 if (!ctype) {
503 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
504 return NULL;
507 examine_symbol_type(ctype);
509 /* Figure out the base type we point to */
510 if (ctype->type == SYM_NODE)
511 ctype = ctype->ctype.base_type;
512 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
513 warn(expr->pos, "subtraction of functions? Share your drugs");
514 return NULL;
516 ctype = ctype->ctype.base_type;
518 expr->ctype = ssize_t_ctype;
519 if (ctype->bit_size > bits_in_char) {
520 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
521 struct expression *div = expr;
522 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
524 val->ctype = size_t_ctype;
525 val->value = ctype->bit_size >> 3;
527 sub->op = '-';
528 sub->ctype = ssize_t_ctype;
529 sub->left = l;
530 sub->right = r;
532 div->op = '/';
533 div->left = sub;
534 div->right = val;
537 return ssize_t_ctype;
540 static struct symbol *evaluate_sub(struct expression *expr)
542 struct expression *left = expr->left, *right = expr->right;
543 struct symbol *ltype = left->ctype;
545 if (is_ptr_type(ltype))
546 return evaluate_ptr_sub(expr, left, right);
548 // FIXME! FP promotion
549 return evaluate_arith(expr, 1);
552 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
554 static struct symbol *evaluate_conditional(struct expression **p)
556 struct symbol *ctype;
557 struct expression *expr = *p;
559 if (!expr)
560 return NULL;
562 if (expr->type == EXPR_ASSIGNMENT)
563 warn(expr->pos, "assignment expression in conditional");
565 ctype = evaluate_expression(expr);
566 if (ctype && is_safe_type(ctype))
567 warn(expr->pos, "testing a 'safe expression'");
569 return ctype;
572 static struct symbol *evaluate_logical(struct expression *expr)
574 if (!evaluate_conditional(&expr->left))
575 return NULL;
576 if (!evaluate_conditional(&expr->right))
577 return NULL;
579 expr->ctype = &bool_ctype;
580 return &bool_ctype;
583 static struct symbol *evaluate_shift(struct expression *expr)
585 struct expression *left = expr->left, *right = expr->right;
586 struct symbol *ltype = left->ctype, *rtype = right->ctype;
588 if (ltype->type == SYM_NODE)
589 ltype = ltype->ctype.base_type;
590 if (rtype->type == SYM_NODE)
591 rtype = rtype->ctype.base_type;
592 if (is_int_type(ltype) && is_int_type(rtype)) {
593 struct symbol *ctype = integer_promotion(ltype);
594 if (ltype->bit_size != ctype->bit_size)
595 expr->left = cast_to(expr->left, ctype);
596 expr->ctype = ctype;
597 ctype = integer_promotion(rtype);
598 if (rtype->bit_size != ctype->bit_size)
599 expr->right = cast_to(expr->right, ctype);
600 return expr->ctype;
602 return bad_expr_type(expr);
605 static struct symbol *evaluate_binop(struct expression *expr)
607 switch (expr->op) {
608 // addition can take ptr+int, fp and int
609 case '+':
610 return evaluate_add(expr);
612 // subtraction can take ptr-ptr, fp and int
613 case '-':
614 return evaluate_sub(expr);
616 // Arithmetic operations can take fp and int
617 case '*': case '/':
618 return evaluate_arith(expr, 1);
620 // shifts do integer promotions, but that's it.
621 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
622 return evaluate_shift(expr);
624 // The rest are integer operations
625 // '%', '&', '^', '|'
626 default:
627 return evaluate_arith(expr, 0);
631 static struct symbol *evaluate_comma(struct expression *expr)
633 expr->ctype = expr->right->ctype;
634 return expr->ctype;
637 static int modify_for_unsigned(int op)
639 if (op == '<')
640 op = SPECIAL_UNSIGNED_LT;
641 else if (op == '>')
642 op = SPECIAL_UNSIGNED_GT;
643 else if (op == SPECIAL_LTE)
644 op = SPECIAL_UNSIGNED_LTE;
645 else if (op == SPECIAL_GTE)
646 op = SPECIAL_UNSIGNED_GTE;
647 return op;
650 static struct symbol *evaluate_compare(struct expression *expr)
652 struct expression *left = expr->left, *right = expr->right;
653 struct symbol *ltype = left->ctype, *rtype = right->ctype;
654 struct symbol *ctype;
656 /* Type types? */
657 if (is_type_type(ltype) && is_type_type(rtype)) {
658 expr->ctype = &bool_ctype;
659 return &bool_ctype;
662 if (is_safe_type(ltype) || is_safe_type(rtype))
663 warn(expr->pos, "testing a 'safe expression'");
665 /* Pointer types? */
666 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
667 expr->ctype = &bool_ctype;
668 // FIXME! Check the types for compatibility
669 return &bool_ctype;
672 ctype = compatible_integer_binop(&expr->left, &expr->right);
673 if (ctype) {
674 if (ctype->ctype.modifiers & MOD_UNSIGNED)
675 expr->op = modify_for_unsigned(expr->op);
676 expr->ctype = &bool_ctype;
677 return &bool_ctype;
680 return bad_expr_type(expr);
683 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
685 return (is_int_type(ltype) && is_int_type(rtype));
689 * FIXME!! This should do casts, array degeneration etc..
691 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
693 struct symbol *ltype = left->ctype, *rtype = right->ctype;
695 if (ltype->type == SYM_NODE)
696 ltype = ltype->ctype.base_type;
698 if (rtype->type == SYM_NODE)
699 rtype = rtype->ctype.base_type;
701 if (ltype->type == SYM_PTR) {
702 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
703 return ltype;
706 if (rtype->type == SYM_PTR) {
707 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
708 return rtype;
710 return NULL;
713 static struct symbol * evaluate_conditional_expression(struct expression *expr)
715 struct expression *cond, *true, *false;
716 struct symbol *ctype, *ltype, *rtype;
717 const char * typediff;
719 ctype = degenerate(expr->conditional);
720 cond = expr->conditional;
722 ltype = ctype;
723 true = cond;
724 if (expr->cond_true) {
725 ltype = degenerate(expr->cond_true);
726 true = expr->cond_true;
729 rtype = degenerate(expr->cond_false);
730 false = expr->cond_false;
732 ctype = ltype;
733 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
734 if (typediff) {
735 ctype = compatible_integer_binop(&true, &expr->cond_false);
736 if (!ctype) {
737 ctype = compatible_ptr_type(true, expr->cond_false);
738 if (!ctype) {
739 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
740 return NULL;
745 expr->ctype = ctype;
746 return ctype;
749 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
750 struct expression **rp, struct symbol *source, const char *where)
752 const char *typediff;
753 struct symbol *t;
754 int target_as;
756 /* It's ok if the target is more volatile or const than the source */
757 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
758 if (!typediff)
759 return 1;
761 if (compatible_integer_types(target, source)) {
762 if (target->bit_size != source->bit_size)
763 *rp = cast_to(*rp, target);
764 return 1;
767 /* Pointer destination? */
768 t = target;
769 target_as = t->ctype.as;
770 if (t->type == SYM_NODE) {
771 t = t->ctype.base_type;
772 target_as |= t->ctype.as;
774 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
775 struct expression *right = *rp;
776 struct symbol *s = source;
777 int source_as;
779 // NULL pointer is always ok
780 if (is_null_ptr(right))
781 return 1;
783 /* "void *" matches anything as long as the address space is ok */
784 source_as = s->ctype.as;
785 if (s->type == SYM_NODE) {
786 s = s->ctype.base_type;
787 source_as |= s->ctype.as;
789 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
790 s = s->ctype.base_type;
791 t = t->ctype.base_type;
792 if (s == &void_ctype || t == &void_ctype)
793 return 1;
797 // FIXME!! Cast it?
798 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
799 info(expr->pos, " expected %s", show_typename(target));
800 info(expr->pos, " got %s", show_typename(source));
801 return 0;
805 * FIXME!! This is wrong from a double evaluation standpoint. We can't
806 * just expand the expression twice, that would make any side effects
807 * happen twice too.
809 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
811 int op = expr->op;
812 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
813 static const int op_trans[] = {
814 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
815 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
816 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
817 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
818 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
819 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
820 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
821 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
822 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
823 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
826 subexpr->left = left;
827 subexpr->right = right;
828 subexpr->op = op_trans[op - SPECIAL_BASE];
829 expr->op = '=';
830 expr->right = subexpr;
831 return evaluate_binop(subexpr);
834 static void evaluate_assign_to(struct expression *left, struct symbol *type)
836 if (type->ctype.modifiers & MOD_CONST)
837 warn(left->pos, "assignment to const expression");
838 if (type->type == SYM_NODE)
839 type->ctype.modifiers |= MOD_ASSIGNED;
842 static struct symbol *evaluate_assignment(struct expression *expr)
844 struct expression *left = expr->left, *right = expr->right;
845 struct symbol *ltype, *rtype;
847 ltype = left->ctype;
848 rtype = right->ctype;
849 if (expr->op != '=') {
850 rtype = evaluate_binop_assignment(expr, left, right);
851 if (!rtype)
852 return NULL;
853 right = expr->right;
856 if (!lvalue_expression(left)) {
857 warn(expr->pos, "not an lvalue");
858 return NULL;
861 rtype = degenerate(right);
863 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
864 return NULL;
866 evaluate_assign_to(left, ltype);
868 expr->ctype = ltype;
869 return ltype;
872 static void examine_fn_arguments(struct symbol *fn)
874 struct symbol *s;
876 FOR_EACH_PTR(fn->arguments, s) {
877 struct symbol *arg = evaluate_symbol(s);
878 /* Array/function arguments silently degenerate into pointers */
879 if (arg) {
880 struct symbol *ptr;
881 switch(arg->type) {
882 case SYM_ARRAY:
883 case SYM_FN:
884 ptr = alloc_symbol(s->pos, SYM_PTR);
885 if (arg->type == SYM_ARRAY)
886 ptr->ctype = arg->ctype;
887 else
888 ptr->ctype.base_type = arg;
889 ptr->ctype.as |= s->ctype.as;
890 ptr->ctype.modifiers |= s->ctype.modifiers;
892 s->ctype.base_type = ptr;
893 s->ctype.as = 0;
894 s->ctype.modifiers = 0;
895 examine_symbol_type(s);
896 break;
897 default:
898 /* nothing */
899 break;
902 } END_FOR_EACH_PTR;
905 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
907 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
908 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
909 *newsym = *sym;
910 newsym->ctype.as = as;
911 newsym->ctype.modifiers = mod;
912 sym = newsym;
914 return sym;
917 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
919 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
920 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
922 node->ctype.base_type = ptr;
923 ptr->bit_size = bits_in_pointer;
924 ptr->ctype.alignment = pointer_alignment;
926 node->bit_size = bits_in_pointer;
927 node->ctype.alignment = pointer_alignment;
929 sym->ctype.modifiers |= MOD_ADDRESSABLE;
930 if (sym->ctype.modifiers & MOD_REGISTER) {
931 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
932 sym->ctype.modifiers &= ~MOD_REGISTER;
934 if (sym->type == SYM_NODE) {
935 ptr->ctype.as |= sym->ctype.as;
936 ptr->ctype.modifiers |= sym->ctype.modifiers;
937 sym = sym->ctype.base_type;
939 if (degenerate && sym->type == SYM_ARRAY) {
940 ptr->ctype.as |= sym->ctype.as;
941 ptr->ctype.modifiers |= sym->ctype.modifiers;
942 sym = sym->ctype.base_type;
944 ptr->ctype.base_type = sym;
946 return node;
949 /* Arrays degenerate into pointers on pointer arithmetic */
950 static struct symbol *degenerate(struct expression *expr)
952 struct symbol *ctype, *base;
954 if (!expr)
955 return NULL;
956 ctype = expr->ctype;
957 if (!ctype)
958 return NULL;
959 base = ctype;
960 if (ctype->type == SYM_NODE)
961 base = ctype->ctype.base_type;
963 * Arrays degenerate into pointers to the entries, while
964 * functions degenerate into pointers to themselves
966 switch (base->type) {
967 case SYM_FN:
968 case SYM_ARRAY:
969 if (expr->op != '*' || expr->type != EXPR_PREOP) {
970 warn(expr->pos, "strange non-value function or array");
971 return NULL;
973 *expr = *expr->unop;
974 ctype = create_pointer(expr, ctype, 1);
975 expr->ctype = ctype;
976 default:
977 /* nothing */;
979 return ctype;
982 static struct symbol *evaluate_addressof(struct expression *expr)
984 struct expression *op = expr->unop;
985 struct symbol *ctype;
987 if (op->op != '*' || op->type != EXPR_PREOP) {
988 warn(expr->pos, "not addressable");
989 return NULL;
991 ctype = op->ctype;
992 *expr = *op->unop;
995 * symbol expression evaluation is lazy about the type
996 * of the sub-expression, so we may have to generate
997 * the type here if so..
999 if (!expr->ctype) {
1000 ctype = create_pointer(expr, ctype, 0);
1001 expr->ctype = ctype;
1003 return expr->ctype;
1007 static struct symbol *evaluate_dereference(struct expression *expr)
1009 struct expression *op = expr->unop;
1010 struct symbol *ctype = op->ctype, *node, *target;
1012 /* Simplify: *&(expr) => (expr) */
1013 if (op->type == EXPR_PREOP && op->op == '&') {
1014 *expr = *op->unop;
1015 return expr->ctype;
1018 /* Dereferencing a node drops all the node information. */
1019 if (ctype->type == SYM_NODE)
1020 ctype = ctype->ctype.base_type;
1022 node = alloc_symbol(expr->pos, SYM_NODE);
1023 target = ctype->ctype.base_type;
1025 switch (ctype->type) {
1026 default:
1027 warn(expr->pos, "cannot derefence this type");
1028 return NULL;
1029 case SYM_PTR:
1030 merge_type(node, ctype);
1031 if (ctype->type != SYM_ARRAY)
1032 break;
1034 * Dereferencing a pointer to an array results in a
1035 * degenerate dereference: the expression becomes
1036 * just a pointer to the entry, and the derefence
1037 * goes away.
1039 *expr = *op;
1041 target = alloc_symbol(expr->pos, SYM_PTR);
1042 target->bit_size = bits_in_pointer;
1043 target->ctype.alignment = pointer_alignment;
1044 merge_type(target, ctype->ctype.base_type);
1045 break;
1047 case SYM_ARRAY:
1049 * When an array is dereferenced, we need to pick
1050 * up the attributes of the original node too..
1052 merge_type(node, op->ctype);
1053 merge_type(node, ctype);
1054 break;
1057 node->bit_size = target->bit_size;
1058 node->array_size = target->array_size;
1060 expr->ctype = node;
1061 return node;
1065 * Unary post-ops: x++ and x--
1067 static struct symbol *evaluate_postop(struct expression *expr)
1069 struct expression *op = expr->unop;
1070 struct symbol *ctype = op->ctype;
1072 if (!lvalue_expression(expr->unop)) {
1073 warn(expr->pos, "need lvalue expression for ++/--");
1074 return NULL;
1077 evaluate_assign_to(op, ctype);
1079 expr->ctype = ctype;
1080 return ctype;
1083 static struct symbol *evaluate_sign(struct expression *expr)
1085 struct symbol *ctype = expr->unop->ctype;
1086 if (is_int_type(ctype)) {
1087 struct symbol *rtype = rtype = integer_promotion(ctype);
1088 if (rtype->bit_size != ctype->bit_size)
1089 expr->unop = cast_to(expr->unop, rtype);
1090 ctype = rtype;
1091 } else {
1092 return bad_expr_type(expr);
1094 if (expr->op == '+')
1095 *expr = *expr->unop;
1096 expr->ctype = ctype;
1097 return ctype;
1100 static struct symbol *evaluate_preop(struct expression *expr)
1102 struct symbol *ctype = expr->unop->ctype;
1104 switch (expr->op) {
1105 case '(':
1106 *expr = *expr->unop;
1107 return ctype;
1109 case '+':
1110 case '-':
1111 case '~':
1112 return evaluate_sign(expr);
1114 case '*':
1115 return evaluate_dereference(expr);
1117 case '&':
1118 return evaluate_addressof(expr);
1120 case SPECIAL_INCREMENT:
1121 case SPECIAL_DECREMENT:
1123 * From a type evaluation standpoint the pre-ops are
1124 * the same as the postops
1126 return evaluate_postop(expr);
1128 case '!':
1129 if (is_safe_type(ctype))
1130 warn(expr->pos, "testing a 'safe expression'");
1131 ctype = &bool_ctype;
1132 break;
1134 default:
1135 break;
1137 expr->ctype = ctype;
1138 return &bool_ctype;
1141 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1143 struct ptr_list *head = (struct ptr_list *)_list;
1144 struct ptr_list *list = head;
1146 if (!head)
1147 return NULL;
1148 do {
1149 int i;
1150 for (i = 0; i < list->nr; i++) {
1151 struct symbol *sym = (struct symbol *) list->list[i];
1152 if (sym->ident) {
1153 if (sym->ident != ident)
1154 continue;
1155 *offset = sym->offset;
1156 return sym;
1157 } else {
1158 struct symbol *ctype = sym->ctype.base_type;
1159 struct symbol *sub;
1160 if (!ctype)
1161 continue;
1162 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1163 continue;
1164 sub = find_identifier(ident, ctype->symbol_list, offset);
1165 if (!sub)
1166 continue;
1167 *offset += sym->offset;
1168 return sub;
1171 } while ((list = list->next) != head);
1172 return NULL;
1175 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1177 struct expression *add;
1179 add = expr;
1180 if (offset) {
1181 /* Create a new add-expression */
1182 add = alloc_expression(expr->pos, EXPR_BINOP);
1183 add->op = '+';
1184 add->left = expr;
1185 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1186 add->right->ctype = &int_ctype;
1187 add->right->value = offset;
1191 * The ctype of the pointer will be lazily evaluated if
1192 * we ever take the address of this member dereference..
1194 add->ctype = NULL;
1195 return add;
1198 /* structure/union dereference */
1199 static struct symbol *evaluate_member_dereference(struct expression *expr)
1201 int offset;
1202 struct symbol *ctype, *member;
1203 struct expression *deref = expr->deref, *add;
1204 struct ident *ident = expr->member;
1205 unsigned int mod;
1206 int address_space;
1208 if (!evaluate_expression(deref))
1209 return NULL;
1210 if (!ident) {
1211 warn(expr->pos, "bad member name");
1212 return NULL;
1215 ctype = deref->ctype;
1216 address_space = ctype->ctype.as;
1217 mod = ctype->ctype.modifiers;
1218 if (ctype->type == SYM_NODE) {
1219 ctype = ctype->ctype.base_type;
1220 address_space |= ctype->ctype.as;
1221 mod |= ctype->ctype.modifiers;
1223 if (!lvalue_expression(deref)) {
1224 warn(deref->pos, "expected lvalue for member dereference");
1225 return NULL;
1227 deref = deref->unop;
1228 expr->deref = deref;
1229 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1230 warn(expr->pos, "expected structure or union");
1231 return NULL;
1233 offset = 0;
1234 member = find_identifier(ident, ctype->symbol_list, &offset);
1235 if (!member) {
1236 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1237 const char *name = "<unnamed>";
1238 int namelen = 9;
1239 if (ctype->ident) {
1240 name = ctype->ident->name;
1241 namelen = ctype->ident->len;
1243 warn(expr->pos, "no member '%s' in %s %.*s",
1244 show_ident(ident), type, namelen, name);
1245 return NULL;
1249 * The member needs to take on the address space and modifiers of
1250 * the "parent" type.
1252 member = convert_to_as_mod(member, address_space, mod);
1253 add = evaluate_offset(deref, offset);
1255 ctype = member->ctype.base_type;
1256 if (ctype->type == SYM_BITFIELD) {
1257 expr->type = EXPR_BITFIELD;
1258 expr->bitpos = member->bit_offset;
1259 expr->nrbits = member->fieldwidth;
1260 expr->address = add;
1261 } else {
1262 expr->type = EXPR_PREOP;
1263 expr->op = '*';
1264 expr->unop = add;
1267 expr->ctype = member;
1268 return member;
1271 static struct symbol *evaluate_sizeof(struct expression *expr)
1273 int size;
1275 if (expr->cast_type) {
1276 examine_symbol_type(expr->cast_type);
1277 size = expr->cast_type->bit_size;
1278 } else {
1279 if (!evaluate_expression(expr->cast_expression))
1280 return NULL;
1281 size = expr->cast_expression->ctype->bit_size;
1283 if (size & 7) {
1284 warn(expr->pos, "cannot size expression");
1285 return NULL;
1287 expr->type = EXPR_VALUE;
1288 expr->value = size >> 3;
1289 expr->ctype = size_t_ctype;
1290 return size_t_ctype;
1293 static struct symbol *evaluate_alignof(struct expression *expr)
1295 struct symbol *type = expr->cast_type;
1297 if (!type) {
1298 type = evaluate_expression(expr->cast_expression);
1299 if (!type)
1300 return NULL;
1302 examine_symbol_type(type);
1303 expr->type = EXPR_VALUE;
1304 expr->value = type->ctype.alignment;
1305 expr->ctype = size_t_ctype;
1306 return size_t_ctype;
1309 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1311 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1312 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1313 return clash != 0;
1316 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1318 struct expression *expr;
1319 struct symbol_list *argument_types = fn->arguments;
1320 struct symbol *argtype;
1321 int i = 1;
1323 PREPARE_PTR_LIST(argument_types, argtype);
1324 FOR_EACH_PTR (head, expr) {
1325 struct expression **p = THIS_ADDRESS(expr);
1326 struct symbol *ctype, *target;
1327 ctype = evaluate_expression(expr);
1329 if (!ctype)
1330 return 0;
1332 if (context_clash(f, ctype))
1333 warn(expr->pos, "argument %d used in wrong context", i);
1335 ctype = degenerate(expr);
1337 target = argtype;
1338 if (!target && ctype->bit_size < bits_in_int)
1339 target = &int_ctype;
1340 if (target) {
1341 static char where[30];
1342 examine_symbol_type(target);
1343 sprintf(where, "argument %d", i);
1344 compatible_assignment_types(expr, target, p, ctype, where);
1347 i++;
1348 NEXT_PTR_LIST(argtype);
1349 } END_FOR_EACH_PTR;
1350 FINISH_PTR_LIST(argtype);
1351 return 1;
1354 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1355 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1357 struct expression *entry;
1358 int current = 0;
1359 int max = 0;
1361 FOR_EACH_PTR(expr->expr_list, entry) {
1362 struct expression **p = THIS_ADDRESS(entry);
1364 if (entry->type == EXPR_INDEX) {
1365 current = entry->idx_to;
1366 continue;
1368 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1369 current++;
1370 if (current > max)
1371 max = current;
1372 } END_FOR_EACH_PTR;
1373 return max;
1376 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1377 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1379 if (offset || expression_list_size(expr->expr_list) != 1) {
1380 warn(expr->pos, "unexpected compound initializer");
1381 return 0;
1383 return evaluate_array_initializer(ctype, expr, 0);
1386 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1388 struct expression *entry;
1389 struct symbol *sym;
1391 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1392 FOR_EACH_PTR(expr->expr_list, entry) {
1393 struct expression **p = THIS_ADDRESS(entry);
1395 if (entry->type == EXPR_IDENTIFIER) {
1396 struct ident *ident = entry->expr_ident;
1397 /* We special-case the "already right place" case */
1398 if (sym && sym->ident == ident)
1399 continue;
1400 RESET_PTR_LIST(sym);
1401 for (;;) {
1402 if (!sym) {
1403 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1404 return 0;
1406 if (sym->ident == ident)
1407 break;
1408 NEXT_PTR_LIST(sym);
1410 continue;
1413 if (!sym) {
1414 warn(expr->pos, "too many initializers for struct/union");
1415 return 0;
1418 evaluate_initializer(sym, p, offset + sym->offset);
1420 NEXT_PTR_LIST(sym);
1421 } END_FOR_EACH_PTR;
1422 FINISH_PTR_LIST(sym);
1424 return 0;
1428 * Initializers are kind of like assignments. Except
1429 * they can be a hell of a lot more complex.
1431 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1433 struct expression *expr = *ep;
1436 * Simple non-structure/array initializers are the simple
1437 * case, and look (and parse) largely like assignments.
1439 if (expr->type != EXPR_INITIALIZER) {
1440 int size = 0;
1441 struct symbol *rtype = evaluate_expression(expr);
1442 if (rtype) {
1443 struct expression *pos;
1445 // FIXME! char array[] = "string" special case
1446 // should _not_ degenerate.
1447 rtype = degenerate(expr);
1448 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1449 /* strings are special: char arrays */
1450 if (rtype->type == SYM_ARRAY)
1451 size = get_expression_value(rtype->array_size);
1453 * Don't bother creating a position expression for
1454 * the simple initializer cases that don't need it.
1456 * We need a position if the initializer has a byte
1457 * offset, _or_ if we're initializing a bitfield.
1459 if (offset || ctype->fieldwidth) {
1460 pos = alloc_expression(expr->pos, EXPR_POS);
1461 pos->init_offset = offset;
1462 pos->init_sym = ctype;
1463 pos->init_expr = *ep;
1464 pos->ctype = expr->ctype;
1465 *ep = pos;
1468 return size;
1471 expr->ctype = ctype;
1472 if (ctype->type == SYM_NODE)
1473 ctype = ctype->ctype.base_type;
1475 switch (ctype->type) {
1476 case SYM_ARRAY:
1477 case SYM_PTR:
1478 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1479 case SYM_UNION:
1480 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1481 case SYM_STRUCT:
1482 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1483 default:
1484 return evaluate_scalar_initializer(ctype, expr, offset);
1488 static int get_as(struct symbol *sym)
1490 int as;
1491 unsigned long mod;
1493 if (!sym)
1494 return 0;
1495 as = sym->ctype.as;
1496 mod = sym->ctype.modifiers;
1497 if (sym->type == SYM_NODE) {
1498 sym = sym->ctype.base_type;
1499 as |= sym->ctype.as;
1500 mod |= sym->ctype.modifiers;
1503 * You can always throw a value away by casting to
1504 * "void" - that's an implicit "force". Note that
1505 * the same is _not_ true of "void *".
1507 if (sym == &void_ctype)
1508 return -1;
1511 * At least for now, allow casting to a "unsigned long".
1512 * That's how we do things like pointer arithmetic and
1513 * store pointers to registers.
1515 if (sym == &ulong_ctype)
1516 return -1;
1518 if (sym && sym->type == SYM_PTR) {
1519 sym = sym->ctype.base_type;
1520 as |= sym->ctype.as;
1521 mod |= sym->ctype.modifiers;
1523 if (mod & MOD_FORCE)
1524 return -1;
1525 return as;
1528 static struct symbol *evaluate_cast(struct expression *expr)
1530 struct expression *target = expr->cast_expression;
1531 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1533 expr->ctype = ctype;
1534 expr->cast_type = ctype;
1537 * Special case: a cast can be followed by an
1538 * initializer, in which case we need to pass
1539 * the type value down to that initializer rather
1540 * than trying to evaluate it as an expression
1542 * A more complex case is when the initializer is
1543 * dereferenced as part of a post-fix expression.
1544 * We need to produce an expression that can be dereferenced.
1546 if (target->type == EXPR_INITIALIZER) {
1547 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1548 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1550 sym->ctype.base_type = ctype;
1551 sym->initializer = expr->cast_expression;
1552 evaluate_symbol(sym);
1554 addr->ctype = NULL; /* Lazy eval */
1555 addr->symbol = sym;
1557 expr->type = EXPR_PREOP;
1558 expr->op = '*';
1559 expr->unop = addr;
1560 expr->ctype = ctype;
1561 return ctype;
1564 evaluate_expression(target);
1565 degenerate(target);
1567 if (!get_as(ctype) && get_as(target->ctype) > 0)
1568 warn(expr->pos, "cast removes address space of expression");
1571 * Casts of constant values are special: they
1572 * can be NULL, and thus need to be simplified
1573 * early.
1575 if (target->type == EXPR_VALUE)
1576 cast_value(expr, ctype, target, target->ctype);
1578 return ctype;
1582 * Evaluate a call expression with a symbol. This
1583 * should expand inline functions, and evaluate
1584 * builtins.
1586 static int evaluate_symbol_call(struct expression *expr)
1588 struct expression *fn = expr->fn;
1589 struct symbol *ctype = fn->ctype;
1591 if (fn->type != EXPR_PREOP)
1592 return 0;
1594 if (ctype->op && ctype->op->evaluate)
1595 return ctype->op->evaluate(expr);
1597 if (ctype->ctype.modifiers & MOD_INLINE) {
1598 int ret;
1599 struct symbol *curr = current_fn;
1600 unsigned long context = current_context;
1601 unsigned long mask = current_contextmask;
1603 current_context |= ctype->ctype.context;
1604 current_contextmask |= ctype->ctype.contextmask;
1605 current_fn = ctype->ctype.base_type;
1606 examine_fn_arguments(current_fn);
1608 ret = inline_function(expr, ctype);
1610 /* restore the old function context */
1611 current_fn = curr;
1612 current_context = context;
1613 current_contextmask = mask;
1614 return ret;
1617 return 0;
1620 static struct symbol *evaluate_call(struct expression *expr)
1622 int args, fnargs;
1623 struct symbol *ctype, *sym;
1624 struct expression *fn = expr->fn;
1625 struct expression_list *arglist = expr->args;
1627 if (!evaluate_expression(fn))
1628 return NULL;
1629 sym = ctype = fn->ctype;
1630 if (ctype->type == SYM_NODE)
1631 ctype = ctype->ctype.base_type;
1632 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1633 ctype = ctype->ctype.base_type;
1634 if (!evaluate_arguments(sym, ctype, arglist))
1635 return NULL;
1636 if (ctype->type != SYM_FN) {
1637 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1638 return NULL;
1640 args = expression_list_size(expr->args);
1641 fnargs = symbol_list_size(ctype->arguments);
1642 if (args < fnargs)
1643 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1644 if (args > fnargs && !ctype->variadic)
1645 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1646 if (sym->type == SYM_NODE) {
1647 if (evaluate_symbol_call(expr))
1648 return expr->ctype;
1650 expr->ctype = ctype->ctype.base_type;
1651 return expr->ctype;
1654 struct symbol *evaluate_expression(struct expression *expr)
1656 if (!expr)
1657 return NULL;
1658 if (expr->ctype)
1659 return expr->ctype;
1661 switch (expr->type) {
1662 case EXPR_VALUE:
1663 warn(expr->pos, "value expression without a type");
1664 return NULL;
1665 case EXPR_STRING:
1666 return evaluate_string(expr);
1667 case EXPR_SYMBOL:
1668 return evaluate_symbol_expression(expr);
1669 case EXPR_BINOP:
1670 if (!evaluate_expression(expr->left))
1671 return NULL;
1672 if (!evaluate_expression(expr->right))
1673 return NULL;
1674 return evaluate_binop(expr);
1675 case EXPR_LOGICAL:
1676 return evaluate_logical(expr);
1677 case EXPR_COMMA:
1678 if (!evaluate_expression(expr->left))
1679 return NULL;
1680 if (!evaluate_expression(expr->right))
1681 return NULL;
1682 return evaluate_comma(expr);
1683 case EXPR_COMPARE:
1684 if (!evaluate_expression(expr->left))
1685 return NULL;
1686 if (!evaluate_expression(expr->right))
1687 return NULL;
1688 return evaluate_compare(expr);
1689 case EXPR_ASSIGNMENT:
1690 if (!evaluate_expression(expr->left))
1691 return NULL;
1692 if (!evaluate_expression(expr->right))
1693 return NULL;
1694 return evaluate_assignment(expr);
1695 case EXPR_PREOP:
1696 if (!evaluate_expression(expr->unop))
1697 return NULL;
1698 return evaluate_preop(expr);
1699 case EXPR_POSTOP:
1700 if (!evaluate_expression(expr->unop))
1701 return NULL;
1702 return evaluate_postop(expr);
1703 case EXPR_CAST:
1704 return evaluate_cast(expr);
1705 case EXPR_SIZEOF:
1706 return evaluate_sizeof(expr);
1707 case EXPR_ALIGNOF:
1708 return evaluate_alignof(expr);
1709 case EXPR_DEREF:
1710 return evaluate_member_dereference(expr);
1711 case EXPR_CALL:
1712 return evaluate_call(expr);
1713 case EXPR_BITFIELD:
1714 warn(expr->pos, "bitfield generated by parser");
1715 return NULL;
1716 case EXPR_CONDITIONAL:
1717 if (!evaluate_conditional(&expr->conditional))
1718 return NULL;
1719 if (!evaluate_expression(expr->cond_false))
1720 return NULL;
1721 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1722 return NULL;
1723 return evaluate_conditional_expression(expr);
1724 case EXPR_STATEMENT:
1725 expr->ctype = evaluate_statement(expr->statement);
1726 return expr->ctype;
1728 case EXPR_LABEL:
1729 expr->ctype = &ptr_ctype;
1730 return &ptr_ctype;
1732 case EXPR_TYPE:
1733 /* Evaluate the type of the symbol .. */
1734 evaluate_symbol(expr->symbol);
1735 /* .. but the type of the _expression_ is a "type" */
1736 expr->ctype = &type_ctype;
1737 return &type_ctype;
1739 /* These can not exist as stand-alone expressions */
1740 case EXPR_INITIALIZER:
1741 case EXPR_IDENTIFIER:
1742 case EXPR_INDEX:
1743 case EXPR_POS:
1744 warn(expr->pos, "internal front-end error: initializer in expression");
1745 return NULL;
1747 return NULL;
1750 void check_duplicates(struct symbol *sym)
1752 struct symbol *next = sym;
1754 while ((next = next->same_symbol) != NULL) {
1755 const char *typediff;
1756 evaluate_symbol(next);
1757 typediff = type_difference(sym, next, 0, 0);
1758 if (typediff) {
1759 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1760 show_ident(sym->ident),
1761 input_streams[next->pos.stream].name, next->pos.line, typediff);
1762 return;
1767 struct symbol *evaluate_symbol(struct symbol *sym)
1769 struct symbol *base_type;
1771 if (!sym)
1772 return sym;
1774 sym = examine_symbol_type(sym);
1775 base_type = sym->ctype.base_type;
1776 if (!base_type)
1777 return NULL;
1779 /* Evaluate the initializers */
1780 if (sym->initializer) {
1781 int count = evaluate_initializer(sym, &sym->initializer, 0);
1782 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1783 int bit_size = count * base_type->ctype.base_type->bit_size;
1784 base_type->array_size = alloc_const_expression(sym->pos, count);
1785 base_type->bit_size = bit_size;
1786 sym->array_size = base_type->array_size;
1787 sym->bit_size = bit_size;
1791 /* And finally, evaluate the body of the symbol too */
1792 if (base_type->type == SYM_FN) {
1793 examine_fn_arguments(base_type);
1794 if (base_type->stmt) {
1795 current_fn = base_type;
1796 current_contextmask = sym->ctype.contextmask;
1797 current_context = sym->ctype.context;
1798 evaluate_statement(base_type->stmt);
1802 return base_type;
1805 struct symbol *evaluate_return_expression(struct statement *stmt)
1807 struct expression *expr = stmt->expression;
1808 struct symbol *ctype, *fntype;
1810 evaluate_expression(expr);
1811 ctype = degenerate(expr);
1812 fntype = current_fn->ctype.base_type;
1813 if (!fntype || fntype == &void_ctype) {
1814 if (expr && ctype != &void_ctype)
1815 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1816 return NULL;
1819 if (!expr) {
1820 warn(stmt->pos, "return with no return value");
1821 return NULL;
1823 if (!ctype)
1824 return NULL;
1825 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1826 return NULL;
1829 static void evaluate_if_statement(struct statement *stmt)
1831 struct symbol *ctype;
1833 if (!stmt->if_conditional)
1834 return;
1836 ctype = evaluate_conditional(&stmt->if_conditional);
1837 if (!ctype)
1838 return;
1840 evaluate_statement(stmt->if_true);
1841 evaluate_statement(stmt->if_false);
1844 struct symbol *evaluate_statement(struct statement *stmt)
1846 if (!stmt)
1847 return NULL;
1849 switch (stmt->type) {
1850 case STMT_RETURN:
1851 return evaluate_return_expression(stmt);
1853 case STMT_EXPRESSION:
1854 evaluate_expression(stmt->expression);
1855 return degenerate(stmt->expression);
1857 case STMT_COMPOUND: {
1858 struct statement *s;
1859 struct symbol *type = NULL;
1860 struct symbol *sym;
1862 /* Evaluate each symbol in the compound statement */
1863 FOR_EACH_PTR(stmt->syms, sym) {
1864 evaluate_symbol(sym);
1865 } END_FOR_EACH_PTR;
1866 evaluate_symbol(stmt->ret);
1869 * Then, evaluate each statement, making the type of the
1870 * compound statement be the type of the last statement
1872 type = NULL;
1873 FOR_EACH_PTR(stmt->stmts, s) {
1874 type = evaluate_statement(s);
1875 } END_FOR_EACH_PTR;
1876 return type;
1878 case STMT_IF:
1879 evaluate_if_statement(stmt);
1880 return NULL;
1881 case STMT_ITERATOR:
1882 evaluate_conditional(&stmt->iterator_pre_condition);
1883 evaluate_conditional(&stmt->iterator_post_condition);
1884 evaluate_statement(stmt->iterator_pre_statement);
1885 evaluate_statement(stmt->iterator_statement);
1886 evaluate_statement(stmt->iterator_post_statement);
1887 return NULL;
1888 case STMT_SWITCH:
1889 evaluate_expression(stmt->switch_expression);
1890 evaluate_statement(stmt->switch_statement);
1891 return NULL;
1892 case STMT_CASE:
1893 evaluate_expression(stmt->case_expression);
1894 evaluate_expression(stmt->case_to);
1895 evaluate_statement(stmt->case_statement);
1896 return NULL;
1897 case STMT_LABEL:
1898 evaluate_statement(stmt->label_statement);
1899 return NULL;
1900 case STMT_GOTO:
1901 evaluate_expression(stmt->goto_expression);
1902 return NULL;
1903 case STMT_NONE:
1904 break;
1905 case STMT_ASM:
1906 /* FIXME! Do the asm parameter evaluation! */
1907 break;
1909 return NULL;