[PATCH] misc small updates
[smatch.git] / evaluate.c
blob3e826b091d82dff411fb1aade4bb80c6a6cdef2b
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 struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
113 unsigned long lmod, rmod, mod;
115 if (left == right)
116 goto left;
118 if (left->bit_size > right->bit_size)
119 goto left;
121 if (right->bit_size > left->bit_size)
122 goto right;
124 /* Same size integers - promote to unsigned, promote to long */
125 lmod = left->ctype.modifiers;
126 rmod = right->ctype.modifiers;
127 mod = lmod | rmod;
128 if (mod == lmod)
129 goto left;
130 if (mod == rmod)
131 goto right;
132 return ctype_integer(mod);
134 right:
135 left = right;
136 left:
137 if (left->bit_size < bits_in_int)
138 left = &int_ctype;
139 return left;
142 static struct expression * cast_to(struct expression *old, struct symbol *type)
144 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
145 expr->ctype = type;
146 expr->cast_type = type;
147 expr->cast_expression = old;
148 return expr;
151 static int is_type_type(struct symbol *type)
153 return (type->ctype.modifiers & MOD_TYPE) != 0;
156 static int is_ptr_type(struct symbol *type)
158 if (type->type == SYM_NODE)
159 type = type->ctype.base_type;
160 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
163 static int is_int_type(struct symbol *type)
165 if (type->type == SYM_NODE)
166 type = type->ctype.base_type;
167 return (type->type == SYM_BITFIELD) || type->ctype.base_type == &int_type;
170 static struct symbol *bad_expr_type(struct expression *expr)
172 warn(expr->pos, "incompatible types for operation");
173 return NULL;
176 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
178 struct expression *left = *lp, *right = *rp;
179 struct symbol *ltype = left->ctype, *rtype = right->ctype;
181 if (ltype->type == SYM_NODE)
182 ltype = ltype->ctype.base_type;
183 if (rtype->type == SYM_NODE)
184 rtype = rtype->ctype.base_type;
185 /* Integer promotion? */
186 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
187 ltype = &int_ctype;
188 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
189 rtype = &int_ctype;
190 if (is_int_type(ltype) && is_int_type(rtype)) {
191 struct symbol *ctype = bigger_int_type(ltype, rtype);
193 /* Don't bother promoting same-size entities, it only adds clutter */
194 if (ltype->bit_size != ctype->bit_size)
195 *lp = cast_to(left, ctype);
196 if (rtype->bit_size != ctype->bit_size)
197 *rp = cast_to(right, ctype);
198 return ctype;
200 return NULL;
203 static struct symbol *evaluate_int_binop(struct expression *expr)
205 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
206 if (ctype) {
207 expr->ctype = ctype;
208 return ctype;
210 return bad_expr_type(expr);
213 static inline int lvalue_expression(struct expression *expr)
215 while (expr->type == EXPR_CAST)
216 expr = expr->cast_expression;
217 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
220 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
222 struct symbol *ctype;
223 struct symbol *ptr_type = ptr->ctype;
224 struct symbol *i_type = i->ctype;
225 int bit_size;
227 if (i_type->type == SYM_NODE)
228 i_type = i_type->ctype.base_type;
229 if (ptr_type->type == SYM_NODE)
230 ptr_type = ptr_type->ctype.base_type;
232 if (i_type->type == SYM_ENUM)
233 i_type = &int_ctype;
234 if (!is_int_type(i_type))
235 return bad_expr_type(expr);
237 ctype = ptr->ctype;
238 examine_symbol_type(ctype);
240 ctype = degenerate(ptr);
241 if (!ctype->ctype.base_type) {
242 warn(expr->pos, "missing type information");
243 return NULL;
246 /* Get the size of whatever the pointer points to */
247 ptr_type = ctype;
248 if (ptr_type->type == SYM_NODE)
249 ptr_type = ptr_type->ctype.base_type;
250 if (ptr_type->type == SYM_PTR)
251 ptr_type = ptr_type->ctype.base_type;
252 bit_size = ptr_type->bit_size;
254 /* Special case: adding zero commonly happens as a result of 'array[0]' */
255 if (i->type == EXPR_VALUE && !i->value) {
256 *expr = *ptr;
257 } else if (bit_size > bits_in_char) {
258 struct expression *add = expr;
259 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
260 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
262 val->ctype = size_t_ctype;
263 val->value = bit_size >> 3;
265 mul->op = '*';
266 mul->ctype = size_t_ctype;
267 mul->left = i;
268 mul->right = val;
270 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
271 add->left = ptr;
272 add->right = mul;
275 expr->ctype = ctype;
276 return ctype;
279 static struct symbol *evaluate_add(struct expression *expr)
281 struct expression *left = expr->left, *right = expr->right;
282 struct symbol *ltype = left->ctype, *rtype = right->ctype;
284 if (is_ptr_type(ltype))
285 return evaluate_ptr_add(expr, left, right);
287 if (is_ptr_type(rtype))
288 return evaluate_ptr_add(expr, right, left);
290 // FIXME! FP promotion
291 return evaluate_int_binop(expr);
294 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
295 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
297 const char * type_difference(struct symbol *target, struct symbol *source,
298 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
300 for (;;) {
301 unsigned long mod1, mod2, diff;
302 unsigned long as1, as2;
303 int type1, type2;
304 struct symbol *base1, *base2;
306 if (target == source)
307 break;
308 if (!target || !source)
309 return "different types";
311 * Peel of per-node information.
312 * FIXME! Check alignment and context too here!
314 mod1 = target->ctype.modifiers;
315 as1 = target->ctype.as;
316 mod2 = source->ctype.modifiers;
317 as2 = source->ctype.as;
318 if (target->type == SYM_NODE) {
319 target = target->ctype.base_type;
320 if (!target)
321 return "bad types";
322 if (target->type == SYM_PTR) {
323 mod1 = 0;
324 as1 = 0;
326 mod1 |= target->ctype.modifiers;
327 as1 |= target->ctype.as;
329 if (source->type == SYM_NODE) {
330 source = source->ctype.base_type;
331 if (!source)
332 return "bad types";
333 if (source->type == SYM_PTR) {
334 mod2 = 0;
335 as2 = 0;
337 mod2 |= source->ctype.modifiers;
338 as2 |= source->ctype.as;
341 if (target == source)
342 break;
343 if (!target || !source)
344 return "different types";
346 type1 = target->type;
347 base1 = target->ctype.base_type;
349 type2 = source->type;
350 base2 = source->ctype.base_type;
353 * Pointers to functions compare as the function itself
355 if (type1 == SYM_PTR && base1) {
356 switch (base1->type) {
357 case SYM_FN:
358 type1 = SYM_FN;
359 target = base1;
360 base1 = base1->ctype.base_type;
361 default:
362 /* nothing */;
365 if (type2 == SYM_PTR && base2) {
366 switch (base2->type) {
367 case SYM_FN:
368 type2 = SYM_FN;
369 source = base2;
370 base2 = base2->ctype.base_type;
371 default:
372 /* nothing */;
376 /* Arrays degenerate to pointers for type comparisons */
377 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
378 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
380 if (type1 != type2)
381 return "different base types";
383 /* Must be same address space to be comparable */
384 if (as1 != as2)
385 return "different address spaces";
387 /* Ignore differences in storage types, sign, or addressability */
388 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
389 if (diff) {
390 mod1 &= diff & ~target_mod_ignore;
391 mod2 &= diff & ~source_mod_ignore;
392 if (mod1 | mod2) {
393 if ((mod1 | mod2) & MOD_SIZE)
394 return "different type sizes";
395 return "different modifiers";
399 if (type1 == SYM_FN) {
400 int i;
401 struct symbol *arg1, *arg2;
402 if (base1->variadic != base2->variadic)
403 return "incompatible variadic arguments";
404 PREPARE_PTR_LIST(target->arguments, arg1);
405 PREPARE_PTR_LIST(source->arguments, arg2);
406 i = 1;
407 for (;;) {
408 const char *diff;
409 diff = type_difference(arg1, arg2, 0, 0);
410 if (diff) {
411 static char argdiff[80];
412 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
413 return argdiff;
415 if (!arg1)
416 break;
417 NEXT_PTR_LIST(arg1);
418 NEXT_PTR_LIST(arg2);
419 i++;
421 FINISH_PTR_LIST(arg2);
422 FINISH_PTR_LIST(arg1);
425 target = base1;
426 source = base2;
428 return NULL;
431 static int is_null_ptr(struct expression *expr)
433 if (expr->type != EXPR_VALUE || expr->value)
434 return 0;
435 if (!is_ptr_type(expr->ctype))
436 warn(expr->pos, "Using plain integer as NULL pointer");
437 return 1;
440 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
442 /* NULL expression? Just return the type of the "other side" */
443 if (is_null_ptr(r))
444 return l->ctype;
445 if (is_null_ptr(l))
446 return r->ctype;
447 return NULL;
451 * Ignore differences in "volatile" and "const"ness when
452 * subtracting pointers
454 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
456 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
458 const char *typediff;
459 struct symbol *ctype;
460 struct symbol *ltype, *rtype;
462 ltype = degenerate(l);
463 rtype = degenerate(r);
466 * If it is an integer subtract: the ptr add case will do the
467 * right thing.
469 if (!is_ptr_type(rtype))
470 return evaluate_ptr_add(expr, l, r);
472 ctype = ltype;
473 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
474 if (typediff) {
475 ctype = common_ptr_type(l, r);
476 if (!ctype) {
477 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
478 return NULL;
481 examine_symbol_type(ctype);
483 /* Figure out the base type we point to */
484 if (ctype->type == SYM_NODE)
485 ctype = ctype->ctype.base_type;
486 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
487 warn(expr->pos, "subtraction of functions? Share your drugs");
488 return NULL;
490 ctype = ctype->ctype.base_type;
492 expr->ctype = ssize_t_ctype;
493 if (ctype->bit_size > bits_in_char) {
494 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
495 struct expression *div = expr;
496 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
498 val->ctype = size_t_ctype;
499 val->value = ctype->bit_size >> 3;
501 sub->op = '-';
502 sub->ctype = ssize_t_ctype;
503 sub->left = l;
504 sub->right = r;
506 div->op = '/';
507 div->left = sub;
508 div->right = val;
511 return ssize_t_ctype;
514 static struct symbol *evaluate_sub(struct expression *expr)
516 struct expression *left = expr->left, *right = expr->right;
517 struct symbol *ltype = left->ctype;
519 if (is_ptr_type(ltype))
520 return evaluate_ptr_sub(expr, left, right);
522 // FIXME! FP promotion
523 return evaluate_int_binop(expr);
526 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
528 static struct symbol *evaluate_conditional(struct expression *expr)
530 struct symbol *ctype;
532 if (!expr)
533 return NULL;
535 if (expr->type == EXPR_ASSIGNMENT)
536 warn(expr->pos, "assignment expression in conditional");
538 ctype = evaluate_expression(expr);
539 if (ctype && is_safe_type(ctype))
540 warn(expr->pos, "testing a 'safe expression'");
542 return ctype;
545 static struct symbol *evaluate_logical(struct expression *expr)
547 if (!evaluate_conditional(expr->left))
548 return NULL;
549 if (!evaluate_conditional(expr->right))
550 return NULL;
552 expr->ctype = &bool_ctype;
553 return &bool_ctype;
556 static struct symbol *evaluate_arithmetic(struct expression *expr)
558 // FIXME! Floating-point promotion!
559 return evaluate_int_binop(expr);
562 static struct symbol *evaluate_binop(struct expression *expr)
564 switch (expr->op) {
565 // addition can take ptr+int, fp and int
566 case '+':
567 return evaluate_add(expr);
569 // subtraction can take ptr-ptr, fp and int
570 case '-':
571 return evaluate_sub(expr);
573 // Arithmetic operations can take fp and int
574 case '*': case '/': case '%':
575 return evaluate_arithmetic(expr);
577 // The rest are integer operations (bitops)
578 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
579 // '&', '^', '|'
580 default:
581 return evaluate_int_binop(expr);
585 static struct symbol *evaluate_comma(struct expression *expr)
587 expr->ctype = expr->right->ctype;
588 return expr->ctype;
591 static struct symbol *evaluate_compare(struct expression *expr)
593 struct expression *left = expr->left, *right = expr->right;
594 struct symbol *ltype = left->ctype, *rtype = right->ctype;
595 struct symbol *ctype;
597 /* Type types? */
598 if (is_type_type(ltype) && is_type_type(rtype)) {
599 expr->ctype = &bool_ctype;
600 return &bool_ctype;
603 if (is_safe_type(ltype) || is_safe_type(rtype))
604 warn(expr->pos, "testing a 'safe expression'");
606 /* Pointer types? */
607 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
608 expr->ctype = &bool_ctype;
609 // FIXME! Check the types for compatibility
610 return &bool_ctype;
613 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
614 if (ctype) {
615 expr->ctype = &bool_ctype;
616 return &bool_ctype;
619 return bad_expr_type(expr);
622 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
624 /* Integer promotion? */
625 if (ltype->type == SYM_NODE)
626 ltype = ltype->ctype.base_type;
627 if (rtype->type == SYM_NODE)
628 rtype = rtype->ctype.base_type;
629 if (ltype->type == SYM_ENUM || ltype->type == SYM_BITFIELD)
630 ltype = &int_ctype;
631 if (rtype->type == SYM_ENUM || rtype->type == SYM_BITFIELD)
632 rtype = &int_ctype;
633 return (is_int_type(ltype) && is_int_type(rtype));
637 * FIXME!! This should do casts, array degeneration etc..
639 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
641 struct symbol *ltype = left->ctype, *rtype = right->ctype;
643 if (ltype->type == SYM_NODE)
644 ltype = ltype->ctype.base_type;
646 if (rtype->type == SYM_NODE)
647 rtype = rtype->ctype.base_type;
649 if (ltype->type == SYM_PTR) {
650 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
651 return ltype;
654 if (rtype->type == SYM_PTR) {
655 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
656 return rtype;
658 return NULL;
661 static struct symbol * evaluate_conditional_expression(struct expression *expr)
663 struct expression *cond, *true, *false;
664 struct symbol *ctype, *ltype, *rtype;
665 const char * typediff;
667 ctype = degenerate(expr->conditional);
668 cond = expr->conditional;
670 ltype = ctype;
671 true = cond;
672 if (expr->cond_true) {
673 ltype = degenerate(expr->cond_true);
674 true = expr->cond_true;
677 rtype = degenerate(expr->cond_false);
678 false = expr->cond_false;
680 ctype = ltype;
681 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
682 if (typediff) {
683 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
684 if (!ctype) {
685 ctype = compatible_ptr_type(true, expr->cond_false);
686 if (!ctype) {
687 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
688 return NULL;
693 expr->ctype = ctype;
694 return ctype;
697 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
698 struct expression **rp, struct symbol *source, const char *where)
700 const char *typediff;
701 struct symbol *t;
702 int target_as;
704 /* It's ok if the target is more volatile or const than the source */
705 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
706 if (!typediff)
707 return 1;
709 if (compatible_integer_types(target, source)) {
710 if (target->bit_size != source->bit_size)
711 *rp = cast_to(*rp, target);
712 return 1;
715 /* Pointer destination? */
716 t = target;
717 target_as = t->ctype.as;
718 if (t->type == SYM_NODE) {
719 t = t->ctype.base_type;
720 target_as |= t->ctype.as;
722 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
723 struct expression *right = *rp;
724 struct symbol *s = source;
725 int source_as;
727 // NULL pointer is always ok
728 if (is_null_ptr(right))
729 return 1;
731 /* "void *" matches anything as long as the address space is ok */
732 source_as = s->ctype.as;
733 if (s->type == SYM_NODE) {
734 s = s->ctype.base_type;
735 source_as |= s->ctype.as;
737 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
738 s = s->ctype.base_type;
739 t = t->ctype.base_type;
740 if (s == &void_ctype || t == &void_ctype)
741 return 1;
745 // FIXME!! Cast it?
746 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
747 info(expr->pos, " expected %s", show_typename(target));
748 info(expr->pos, " got %s", show_typename(source));
749 return 0;
753 * FIXME!! This is wrong from a double evaluation standpoint. We can't
754 * just expand the expression twice, that would make any side effects
755 * happen twice too.
757 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
759 int op = expr->op;
760 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
761 static const int op_trans[] = {
762 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
763 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
764 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
765 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
766 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
767 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
768 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
769 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
770 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
771 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
774 subexpr->left = left;
775 subexpr->right = right;
776 subexpr->op = op_trans[op - SPECIAL_BASE];
777 expr->op = '=';
778 expr->right = subexpr;
779 return evaluate_binop(subexpr);
782 static struct symbol *evaluate_assignment(struct expression *expr)
784 struct expression *left = expr->left, *right = expr->right;
785 struct symbol *ltype, *rtype;
787 ltype = left->ctype;
788 rtype = right->ctype;
789 if (expr->op != '=') {
790 rtype = evaluate_binop_assignment(expr, left, right);
791 if (!rtype)
792 return NULL;
793 right = expr->right;
796 if (!lvalue_expression(left)) {
797 warn(expr->pos, "not an lvalue");
798 return NULL;
801 rtype = degenerate(right);
803 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
804 return NULL;
806 if (ltype->type == SYM_NODE)
807 ltype->ctype.modifiers |= MOD_ASSIGNED;
809 expr->ctype = ltype;
810 return ltype;
813 static void examine_fn_arguments(struct symbol *fn)
815 struct symbol *s;
817 FOR_EACH_PTR(fn->arguments, s) {
818 struct symbol *arg = evaluate_symbol(s);
819 /* Array/function arguments silently degenerate into pointers */
820 if (arg) {
821 struct symbol *ptr;
822 switch(arg->type) {
823 case SYM_ARRAY:
824 case SYM_FN:
825 ptr = alloc_symbol(s->pos, SYM_PTR);
826 if (arg->type == SYM_ARRAY)
827 ptr->ctype = arg->ctype;
828 else
829 ptr->ctype.base_type = arg;
830 ptr->ctype.as |= s->ctype.as;
831 ptr->ctype.modifiers |= s->ctype.modifiers;
833 s->ctype.base_type = ptr;
834 s->ctype.as = 0;
835 s->ctype.modifiers = 0;
836 examine_symbol_type(s);
837 break;
838 default:
839 /* nothing */
840 break;
843 } END_FOR_EACH_PTR;
846 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
848 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
849 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
850 *newsym = *sym;
851 newsym->ctype.as = as;
852 newsym->ctype.modifiers = mod;
853 sym = newsym;
855 return sym;
858 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
860 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
861 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
863 node->ctype.base_type = ptr;
864 ptr->bit_size = bits_in_pointer;
865 ptr->ctype.alignment = pointer_alignment;
867 node->bit_size = bits_in_pointer;
868 node->ctype.alignment = pointer_alignment;
870 sym->ctype.modifiers |= MOD_ADDRESSABLE;
871 if (sym->ctype.modifiers & MOD_REGISTER) {
872 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
873 sym->ctype.modifiers &= ~MOD_REGISTER;
875 if (sym->type == SYM_NODE) {
876 ptr->ctype.as |= sym->ctype.as;
877 ptr->ctype.modifiers |= sym->ctype.modifiers;
878 sym = sym->ctype.base_type;
880 if (degenerate && sym->type == SYM_ARRAY) {
881 ptr->ctype.as |= sym->ctype.as;
882 ptr->ctype.modifiers |= sym->ctype.modifiers;
883 sym = sym->ctype.base_type;
885 ptr->ctype.base_type = sym;
887 return node;
890 /* Arrays degenerate into pointers on pointer arithmetic */
891 static struct symbol *degenerate(struct expression *expr)
893 struct symbol *ctype, *base;
895 if (!expr)
896 return NULL;
897 ctype = expr->ctype;
898 if (!ctype)
899 return NULL;
900 base = ctype;
901 if (ctype->type == SYM_NODE)
902 base = ctype->ctype.base_type;
904 * Arrays degenerate into pointers to the entries, while
905 * functions degenerate into pointers to themselves
907 switch (base->type) {
908 case SYM_FN:
909 case SYM_ARRAY:
910 if (expr->op != '*' || expr->type != EXPR_PREOP) {
911 warn(expr->pos, "strange non-value function or array");
912 return NULL;
914 *expr = *expr->unop;
915 ctype = create_pointer(expr, ctype, 1);
916 expr->ctype = ctype;
917 default:
918 /* nothing */;
920 return ctype;
923 static struct symbol *evaluate_addressof(struct expression *expr)
925 struct expression *op = expr->unop;
926 struct symbol *ctype;
928 if (op->op != '*' || op->type != EXPR_PREOP) {
929 warn(expr->pos, "not addressable");
930 return NULL;
932 ctype = op->ctype;
933 *expr = *op->unop;
936 * symbol expression evaluation is lazy about the type
937 * of the sub-expression, so we may have to generate
938 * the type here if so..
940 if (!expr->ctype) {
941 ctype = create_pointer(expr, ctype, 0);
942 expr->ctype = ctype;
944 return expr->ctype;
948 static struct symbol *evaluate_dereference(struct expression *expr)
950 struct expression *op = expr->unop;
951 struct symbol *ctype = op->ctype, *node, *target;
953 /* Simplify: *&(expr) => (expr) */
954 if (op->type == EXPR_PREOP && op->op == '&') {
955 *expr = *op->unop;
956 return expr->ctype;
959 /* Dereferencing a node drops all the node information. */
960 if (ctype->type == SYM_NODE)
961 ctype = ctype->ctype.base_type;
963 node = alloc_symbol(expr->pos, SYM_NODE);
964 target = ctype->ctype.base_type;
966 switch (ctype->type) {
967 default:
968 warn(expr->pos, "cannot derefence this type");
969 return NULL;
970 case SYM_PTR:
971 merge_type(node, ctype);
972 if (ctype->type != SYM_ARRAY)
973 break;
975 * Dereferencing a pointer to an array results in a
976 * degenerate dereference: the expression becomes
977 * just a pointer to the entry, and the derefence
978 * goes away.
980 *expr = *op;
982 target = alloc_symbol(expr->pos, SYM_PTR);
983 target->bit_size = bits_in_pointer;
984 target->ctype.alignment = pointer_alignment;
985 merge_type(target, ctype->ctype.base_type);
986 break;
988 case SYM_ARRAY:
990 * When an array is dereferenced, we need to pick
991 * up the attributes of the original node too..
993 merge_type(node, op->ctype);
994 merge_type(node, ctype);
995 break;
998 node->bit_size = target->bit_size;
999 node->array_size = target->array_size;
1001 expr->ctype = node;
1002 return node;
1006 * Unary post-ops: x++ and x--
1008 static struct symbol *evaluate_postop(struct expression *expr)
1010 struct expression *op = expr->unop;
1011 struct symbol *ctype = op->ctype;
1013 if (!lvalue_expression(expr->unop)) {
1014 warn(expr->pos, "need lvalue expression for ++/--");
1015 return NULL;
1018 if (ctype->type == SYM_NODE)
1019 ctype->ctype.modifiers |= MOD_ASSIGNED;
1021 expr->ctype = ctype;
1022 return ctype;
1025 static struct symbol *evaluate_preop(struct expression *expr)
1027 struct symbol *ctype = expr->unop->ctype;
1029 switch (expr->op) {
1030 case '(':
1031 case '+':
1032 *expr = *expr->unop;
1033 return ctype;
1035 case '*':
1036 return evaluate_dereference(expr);
1038 case '&':
1039 return evaluate_addressof(expr);
1041 case SPECIAL_INCREMENT:
1042 case SPECIAL_DECREMENT:
1044 * From a type evaluation standpoint the pre-ops are
1045 * the same as the postops
1047 return evaluate_postop(expr);
1049 case '!':
1050 if (is_safe_type(ctype))
1051 warn(expr->pos, "testing a 'safe expression'");
1052 ctype = &bool_ctype;
1053 break;
1055 default:
1056 break;
1058 expr->ctype = ctype;
1059 return &bool_ctype;
1062 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1064 struct ptr_list *head = (struct ptr_list *)_list;
1065 struct ptr_list *list = head;
1067 if (!head)
1068 return NULL;
1069 do {
1070 int i;
1071 for (i = 0; i < list->nr; i++) {
1072 struct symbol *sym = (struct symbol *) list->list[i];
1073 if (sym->ident) {
1074 if (sym->ident != ident)
1075 continue;
1076 *offset = sym->offset;
1077 return sym;
1078 } else {
1079 struct symbol *ctype = sym->ctype.base_type;
1080 struct symbol *sub;
1081 if (!ctype)
1082 continue;
1083 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1084 continue;
1085 sub = find_identifier(ident, ctype->symbol_list, offset);
1086 if (!sub)
1087 continue;
1088 *offset += sym->offset;
1089 return sub;
1092 } while ((list = list->next) != head);
1093 return NULL;
1096 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1098 struct expression *add;
1100 add = expr;
1101 if (offset) {
1102 /* Create a new add-expression */
1103 add = alloc_expression(expr->pos, EXPR_BINOP);
1104 add->op = '+';
1105 add->left = expr;
1106 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1107 add->right->ctype = &int_ctype;
1108 add->right->value = offset;
1112 * The ctype of the pointer will be lazily evaluated if
1113 * we ever take the address of this member dereference..
1115 add->ctype = NULL;
1116 return add;
1119 /* structure/union dereference */
1120 static struct symbol *evaluate_member_dereference(struct expression *expr)
1122 int offset;
1123 struct symbol *ctype, *member;
1124 struct expression *deref = expr->deref, *add;
1125 struct ident *ident = expr->member;
1126 unsigned int mod;
1127 int address_space;
1129 if (!evaluate_expression(deref))
1130 return NULL;
1131 if (!ident) {
1132 warn(expr->pos, "bad member name");
1133 return NULL;
1136 ctype = deref->ctype;
1137 address_space = ctype->ctype.as;
1138 mod = ctype->ctype.modifiers;
1139 if (ctype->type == SYM_NODE) {
1140 ctype = ctype->ctype.base_type;
1141 address_space |= ctype->ctype.as;
1142 mod |= ctype->ctype.modifiers;
1144 if (!lvalue_expression(deref)) {
1145 warn(deref->pos, "expected lvalue for member dereference");
1146 return NULL;
1148 deref = deref->unop;
1149 expr->deref = deref;
1150 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1151 warn(expr->pos, "expected structure or union");
1152 return NULL;
1154 offset = 0;
1155 member = find_identifier(ident, ctype->symbol_list, &offset);
1156 if (!member) {
1157 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1158 const char *name = "<unnamed>";
1159 int namelen = 9;
1160 if (ctype->ident) {
1161 name = ctype->ident->name;
1162 namelen = ctype->ident->len;
1164 warn(expr->pos, "no member '%s' in %s %.*s",
1165 show_ident(ident), type, namelen, name);
1166 return NULL;
1170 * The member needs to take on the address space and modifiers of
1171 * the "parent" type.
1173 member = convert_to_as_mod(member, address_space, mod);
1174 add = evaluate_offset(deref, offset);
1176 ctype = member->ctype.base_type;
1177 if (ctype->type == SYM_BITFIELD) {
1178 expr->type = EXPR_BITFIELD;
1179 expr->bitpos = member->bit_offset;
1180 expr->nrbits = member->fieldwidth;
1181 expr->address = add;
1182 } else {
1183 expr->type = EXPR_PREOP;
1184 expr->op = '*';
1185 expr->unop = add;
1188 expr->ctype = member;
1189 return member;
1192 static struct symbol *evaluate_sizeof(struct expression *expr)
1194 int size;
1196 if (expr->cast_type) {
1197 examine_symbol_type(expr->cast_type);
1198 size = expr->cast_type->bit_size;
1199 } else {
1200 if (!evaluate_expression(expr->cast_expression))
1201 return NULL;
1202 size = expr->cast_expression->ctype->bit_size;
1204 if (size & 7) {
1205 warn(expr->pos, "cannot size expression");
1206 return NULL;
1208 expr->type = EXPR_VALUE;
1209 expr->value = size >> 3;
1210 expr->ctype = size_t_ctype;
1211 return size_t_ctype;
1214 static struct symbol *evaluate_alignof(struct expression *expr)
1216 struct symbol *type = expr->cast_type;
1218 if (!type) {
1219 type = evaluate_expression(expr->cast_expression);
1220 if (!type)
1221 return NULL;
1223 examine_symbol_type(type);
1224 expr->type = EXPR_VALUE;
1225 expr->value = type->ctype.alignment;
1226 expr->ctype = size_t_ctype;
1227 return size_t_ctype;
1230 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1232 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1233 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1234 return clash != 0;
1237 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1239 struct expression *expr;
1240 struct symbol_list *argument_types = fn->arguments;
1241 struct symbol *argtype;
1242 int i = 1;
1244 PREPARE_PTR_LIST(argument_types, argtype);
1245 FOR_EACH_PTR (head, expr) {
1246 struct expression **p = THIS_ADDRESS(expr);
1247 struct symbol *ctype, *target;
1248 ctype = evaluate_expression(expr);
1250 if (!ctype)
1251 return 0;
1253 if (context_clash(f, ctype))
1254 warn(expr->pos, "argument %d used in wrong context", i);
1256 ctype = degenerate(expr);
1258 target = argtype;
1259 if (!target && ctype->bit_size < bits_in_int)
1260 target = &int_ctype;
1261 if (target) {
1262 static char where[30];
1263 examine_symbol_type(target);
1264 sprintf(where, "argument %d", i);
1265 compatible_assignment_types(expr, target, p, ctype, where);
1268 i++;
1269 NEXT_PTR_LIST(argtype);
1270 } END_FOR_EACH_PTR;
1271 FINISH_PTR_LIST(argtype);
1272 return 1;
1275 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1276 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1278 struct expression *entry;
1279 int current = 0;
1280 int max = 0;
1282 FOR_EACH_PTR(expr->expr_list, entry) {
1283 struct expression **p = THIS_ADDRESS(entry);
1285 if (entry->type == EXPR_INDEX) {
1286 current = entry->idx_to;
1287 continue;
1289 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1290 current++;
1291 if (current > max)
1292 max = current;
1293 } END_FOR_EACH_PTR;
1294 return max;
1297 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1298 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1300 if (offset || expression_list_size(expr->expr_list) != 1) {
1301 warn(expr->pos, "unexpected compound initializer");
1302 return 0;
1304 return evaluate_array_initializer(ctype, expr, 0);
1307 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1309 struct expression *entry;
1310 struct symbol *sym;
1312 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1313 FOR_EACH_PTR(expr->expr_list, entry) {
1314 struct expression **p = THIS_ADDRESS(entry);
1316 if (entry->type == EXPR_IDENTIFIER) {
1317 struct ident *ident = entry->expr_ident;
1318 /* We special-case the "already right place" case */
1319 if (sym && sym->ident == ident)
1320 continue;
1321 RESET_PTR_LIST(sym);
1322 for (;;) {
1323 if (!sym) {
1324 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1325 return 0;
1327 if (sym->ident == ident)
1328 break;
1329 NEXT_PTR_LIST(sym);
1331 continue;
1334 if (!sym) {
1335 warn(expr->pos, "too many initializers for struct/union");
1336 return 0;
1339 evaluate_initializer(sym, p, offset + sym->offset);
1341 NEXT_PTR_LIST(sym);
1342 } END_FOR_EACH_PTR;
1343 FINISH_PTR_LIST(sym);
1345 return 0;
1349 * Initializers are kind of like assignments. Except
1350 * they can be a hell of a lot more complex.
1352 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1354 struct expression *expr = *ep;
1357 * Simple non-structure/array initializers are the simple
1358 * case, and look (and parse) largely like assignments.
1360 if (expr->type != EXPR_INITIALIZER) {
1361 int size = 0;
1362 struct symbol *rtype = evaluate_expression(expr);
1363 if (rtype) {
1364 struct expression *pos;
1366 // FIXME! char array[] = "string" special case
1367 // should _not_ degenerate.
1368 rtype = degenerate(expr);
1369 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1370 /* strings are special: char arrays */
1371 if (rtype->type == SYM_ARRAY)
1372 size = get_expression_value(rtype->array_size);
1374 * Don't bother creating a position expression for
1375 * the simple initializer cases that don't need it.
1377 * We need a position if the initializer has a byte
1378 * offset, _or_ if we're initializing a bitfield.
1380 if (offset || ctype->fieldwidth) {
1381 pos = alloc_expression(expr->pos, EXPR_POS);
1382 pos->init_offset = offset;
1383 pos->init_sym = ctype;
1384 pos->init_expr = *ep;
1385 pos->ctype = expr->ctype;
1386 *ep = pos;
1389 return size;
1392 expr->ctype = ctype;
1393 if (ctype->type == SYM_NODE)
1394 ctype = ctype->ctype.base_type;
1396 switch (ctype->type) {
1397 case SYM_ARRAY:
1398 case SYM_PTR:
1399 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1400 case SYM_UNION:
1401 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1402 case SYM_STRUCT:
1403 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1404 default:
1405 return evaluate_scalar_initializer(ctype, expr, offset);
1409 static int get_as(struct symbol *sym)
1411 int as;
1412 unsigned long mod;
1414 if (!sym)
1415 return 0;
1416 as = sym->ctype.as;
1417 mod = sym->ctype.modifiers;
1418 if (sym->type == SYM_NODE) {
1419 sym = sym->ctype.base_type;
1420 as |= sym->ctype.as;
1421 mod |= sym->ctype.modifiers;
1424 * You can always throw a value away by casting to
1425 * "void" - that's an implicit "force". Note that
1426 * the same is _not_ true of "void *".
1428 if (sym == &void_ctype)
1429 return -1;
1432 * At least for now, allow casting to a "unsigned long".
1433 * That's how we do things like pointer arithmetic and
1434 * store pointers to registers.
1436 if (sym == &ulong_ctype)
1437 return -1;
1439 if (sym && sym->type == SYM_PTR) {
1440 sym = sym->ctype.base_type;
1441 as |= sym->ctype.as;
1442 mod |= sym->ctype.modifiers;
1444 if (mod & MOD_FORCE)
1445 return -1;
1446 return as;
1449 static struct symbol *evaluate_cast(struct expression *expr)
1451 struct expression *target = expr->cast_expression;
1452 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1454 expr->ctype = ctype;
1455 expr->cast_type = ctype;
1458 * Special case: a cast can be followed by an
1459 * initializer, in which case we need to pass
1460 * the type value down to that initializer rather
1461 * than trying to evaluate it as an expression
1463 * A more complex case is when the initializer is
1464 * dereferenced as part of a post-fix expression.
1465 * We need to produce an expression that can be dereferenced.
1467 if (target->type == EXPR_INITIALIZER) {
1468 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1469 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1471 sym->ctype.base_type = ctype;
1472 sym->initializer = expr->cast_expression;
1473 evaluate_symbol(sym);
1475 addr->ctype = NULL; /* Lazy eval */
1476 addr->symbol = sym;
1478 expr->type = EXPR_PREOP;
1479 expr->op = '*';
1480 expr->unop = addr;
1481 expr->ctype = ctype;
1482 return ctype;
1485 evaluate_expression(target);
1486 degenerate(target);
1488 if (!get_as(ctype) && get_as(target->ctype) > 0)
1489 warn(expr->pos, "cast removes address space of expression");
1492 * Casts of constant values are special: they
1493 * can be NULL, and thus need to be simplified
1494 * early.
1496 if (target->type == EXPR_VALUE)
1497 cast_value(expr, ctype, target, target->ctype);
1499 return ctype;
1503 * Evaluate a call expression with a symbol. This
1504 * should expand inline functions, and evaluate
1505 * builtins.
1507 static int evaluate_symbol_call(struct expression *expr)
1509 struct expression *fn = expr->fn;
1510 struct symbol *ctype = fn->ctype;
1512 if (fn->type != EXPR_PREOP)
1513 return 0;
1515 if (ctype->op && ctype->op->evaluate)
1516 return ctype->op->evaluate(expr);
1518 if (ctype->ctype.modifiers & MOD_INLINE) {
1519 int ret;
1520 struct symbol *curr = current_fn;
1521 unsigned long context = current_context;
1522 unsigned long mask = current_contextmask;
1524 current_context |= ctype->ctype.context;
1525 current_contextmask |= ctype->ctype.contextmask;
1526 current_fn = ctype->ctype.base_type;
1527 examine_fn_arguments(current_fn);
1529 ret = inline_function(expr, ctype);
1531 /* restore the old function context */
1532 current_fn = curr;
1533 current_context = context;
1534 current_contextmask = mask;
1535 return ret;
1538 return 0;
1541 static struct symbol *evaluate_call(struct expression *expr)
1543 int args, fnargs;
1544 struct symbol *ctype, *sym;
1545 struct expression *fn = expr->fn;
1546 struct expression_list *arglist = expr->args;
1548 if (!evaluate_expression(fn))
1549 return NULL;
1550 sym = ctype = fn->ctype;
1551 if (ctype->type == SYM_NODE)
1552 ctype = ctype->ctype.base_type;
1553 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1554 ctype = ctype->ctype.base_type;
1555 if (!evaluate_arguments(sym, ctype, arglist))
1556 return NULL;
1557 if (ctype->type != SYM_FN) {
1558 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1559 return NULL;
1561 args = expression_list_size(expr->args);
1562 fnargs = symbol_list_size(ctype->arguments);
1563 if (args < fnargs)
1564 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1565 if (args > fnargs && !ctype->variadic)
1566 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1567 if (sym->type == SYM_NODE) {
1568 if (evaluate_symbol_call(expr))
1569 return expr->ctype;
1571 expr->ctype = ctype->ctype.base_type;
1572 return expr->ctype;
1575 struct symbol *evaluate_expression(struct expression *expr)
1577 if (!expr)
1578 return NULL;
1579 if (expr->ctype)
1580 return expr->ctype;
1582 switch (expr->type) {
1583 case EXPR_VALUE:
1584 warn(expr->pos, "value expression without a type");
1585 return NULL;
1586 case EXPR_STRING:
1587 return evaluate_string(expr);
1588 case EXPR_SYMBOL:
1589 return evaluate_symbol_expression(expr);
1590 case EXPR_BINOP:
1591 if (!evaluate_expression(expr->left))
1592 return NULL;
1593 if (!evaluate_expression(expr->right))
1594 return NULL;
1595 return evaluate_binop(expr);
1596 case EXPR_LOGICAL:
1597 return evaluate_logical(expr);
1598 case EXPR_COMMA:
1599 if (!evaluate_expression(expr->left))
1600 return NULL;
1601 if (!evaluate_expression(expr->right))
1602 return NULL;
1603 return evaluate_comma(expr);
1604 case EXPR_COMPARE:
1605 if (!evaluate_expression(expr->left))
1606 return NULL;
1607 if (!evaluate_expression(expr->right))
1608 return NULL;
1609 return evaluate_compare(expr);
1610 case EXPR_ASSIGNMENT:
1611 if (!evaluate_expression(expr->left))
1612 return NULL;
1613 if (!evaluate_expression(expr->right))
1614 return NULL;
1615 return evaluate_assignment(expr);
1616 case EXPR_PREOP:
1617 if (!evaluate_expression(expr->unop))
1618 return NULL;
1619 return evaluate_preop(expr);
1620 case EXPR_POSTOP:
1621 if (!evaluate_expression(expr->unop))
1622 return NULL;
1623 return evaluate_postop(expr);
1624 case EXPR_CAST:
1625 return evaluate_cast(expr);
1626 case EXPR_SIZEOF:
1627 return evaluate_sizeof(expr);
1628 case EXPR_ALIGNOF:
1629 return evaluate_alignof(expr);
1630 case EXPR_DEREF:
1631 return evaluate_member_dereference(expr);
1632 case EXPR_CALL:
1633 return evaluate_call(expr);
1634 case EXPR_BITFIELD:
1635 warn(expr->pos, "bitfield generated by parser");
1636 return NULL;
1637 case EXPR_CONDITIONAL:
1638 if (!evaluate_conditional(expr->conditional))
1639 return NULL;
1640 if (!evaluate_expression(expr->cond_false))
1641 return NULL;
1642 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1643 return NULL;
1644 return evaluate_conditional_expression(expr);
1645 case EXPR_STATEMENT:
1646 expr->ctype = evaluate_statement(expr->statement);
1647 return expr->ctype;
1649 case EXPR_LABEL:
1650 expr->ctype = &ptr_ctype;
1651 return &ptr_ctype;
1653 case EXPR_TYPE:
1654 /* Evaluate the type of the symbol .. */
1655 evaluate_symbol(expr->symbol);
1656 /* .. but the type of the _expression_ is a "type" */
1657 expr->ctype = &type_ctype;
1658 return &type_ctype;
1660 /* These can not exist as stand-alone expressions */
1661 case EXPR_INITIALIZER:
1662 case EXPR_IDENTIFIER:
1663 case EXPR_INDEX:
1664 case EXPR_POS:
1665 warn(expr->pos, "internal front-end error: initializer in expression");
1666 return NULL;
1668 return NULL;
1671 void check_duplicates(struct symbol *sym)
1673 struct symbol *next = sym;
1675 while ((next = next->same_symbol) != NULL) {
1676 const char *typediff;
1677 evaluate_symbol(next);
1678 typediff = type_difference(sym, next, 0, 0);
1679 if (typediff) {
1680 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1681 show_ident(sym->ident),
1682 input_streams[next->pos.stream].name, next->pos.line, typediff);
1683 return;
1688 struct symbol *evaluate_symbol(struct symbol *sym)
1690 struct symbol *base_type;
1692 if (!sym)
1693 return sym;
1695 sym = examine_symbol_type(sym);
1696 base_type = sym->ctype.base_type;
1697 if (!base_type)
1698 return NULL;
1700 /* Evaluate the initializers */
1701 if (sym->initializer) {
1702 int count = evaluate_initializer(sym, &sym->initializer, 0);
1703 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1704 int bit_size = count * base_type->ctype.base_type->bit_size;
1705 base_type->array_size = alloc_const_expression(sym->pos, count);
1706 base_type->bit_size = bit_size;
1707 sym->array_size = base_type->array_size;
1708 sym->bit_size = bit_size;
1712 /* And finally, evaluate the body of the symbol too */
1713 if (base_type->type == SYM_FN) {
1714 examine_fn_arguments(base_type);
1715 if (base_type->stmt) {
1716 current_fn = base_type;
1717 current_contextmask = sym->ctype.contextmask;
1718 current_context = sym->ctype.context;
1719 evaluate_statement(base_type->stmt);
1723 return base_type;
1726 struct symbol *evaluate_return_expression(struct statement *stmt)
1728 struct expression *expr = stmt->expression;
1729 struct symbol *ctype, *fntype;
1731 evaluate_expression(expr);
1732 ctype = degenerate(expr);
1733 fntype = current_fn->ctype.base_type;
1734 if (!fntype || fntype == &void_ctype) {
1735 if (expr && ctype != &void_ctype)
1736 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1737 return NULL;
1740 if (!expr) {
1741 warn(stmt->pos, "return with no return value");
1742 return NULL;
1744 if (!ctype)
1745 return NULL;
1746 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1747 return NULL;
1750 static void evaluate_if_statement(struct statement *stmt)
1752 struct expression *expr = stmt->if_conditional;
1753 struct symbol *ctype;
1755 if (!expr)
1756 return;
1758 ctype = evaluate_conditional(expr);
1759 if (!ctype)
1760 return;
1762 evaluate_statement(stmt->if_true);
1763 evaluate_statement(stmt->if_false);
1766 struct symbol *evaluate_statement(struct statement *stmt)
1768 if (!stmt)
1769 return NULL;
1771 switch (stmt->type) {
1772 case STMT_RETURN:
1773 return evaluate_return_expression(stmt);
1775 case STMT_EXPRESSION:
1776 evaluate_expression(stmt->expression);
1777 return degenerate(stmt->expression);
1779 case STMT_COMPOUND: {
1780 struct statement *s;
1781 struct symbol *type = NULL;
1782 struct symbol *sym;
1784 /* Evaluate each symbol in the compound statement */
1785 FOR_EACH_PTR(stmt->syms, sym) {
1786 evaluate_symbol(sym);
1787 } END_FOR_EACH_PTR;
1788 evaluate_symbol(stmt->ret);
1791 * Then, evaluate each statement, making the type of the
1792 * compound statement be the type of the last statement
1794 type = NULL;
1795 FOR_EACH_PTR(stmt->stmts, s) {
1796 type = evaluate_statement(s);
1797 } END_FOR_EACH_PTR;
1798 return type;
1800 case STMT_IF:
1801 evaluate_if_statement(stmt);
1802 return NULL;
1803 case STMT_ITERATOR:
1804 evaluate_conditional(stmt->iterator_pre_condition);
1805 evaluate_conditional(stmt->iterator_post_condition);
1806 evaluate_statement(stmt->iterator_pre_statement);
1807 evaluate_statement(stmt->iterator_statement);
1808 evaluate_statement(stmt->iterator_post_statement);
1809 return NULL;
1810 case STMT_SWITCH:
1811 evaluate_expression(stmt->switch_expression);
1812 evaluate_statement(stmt->switch_statement);
1813 return NULL;
1814 case STMT_CASE:
1815 evaluate_expression(stmt->case_expression);
1816 evaluate_expression(stmt->case_to);
1817 evaluate_statement(stmt->case_statement);
1818 return NULL;
1819 case STMT_LABEL:
1820 evaluate_statement(stmt->label_statement);
1821 return NULL;
1822 case STMT_GOTO:
1823 evaluate_expression(stmt->goto_expression);
1824 return NULL;
1825 case STMT_NONE:
1826 break;
1827 case STMT_ASM:
1828 /* FIXME! Do the asm parameter evaluation! */
1829 break;
1831 return NULL;