[PATCH] Fix FP comparison type
[smatch.git] / evaluate.c
blob4ce0ff8aef283eeeacd79fff5ca5979050d5fcf8
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 inline int is_float_type(struct symbol *type)
208 if (type->type == SYM_NODE)
209 type = type->ctype.base_type;
210 return type->ctype.base_type == &fp_type;
213 static struct symbol *bad_expr_type(struct expression *expr)
215 warn(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
216 switch (expr->type) {
217 case EXPR_BINOP:
218 case EXPR_COMPARE:
219 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
220 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
221 break;
222 case EXPR_PREOP:
223 case EXPR_POSTOP:
224 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
225 break;
226 default:
227 break;
230 return NULL;
233 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
235 struct expression *left = *lp, *right = *rp;
236 struct symbol *ltype = left->ctype, *rtype = right->ctype;
238 if (ltype->type == SYM_NODE)
239 ltype = ltype->ctype.base_type;
240 if (rtype->type == SYM_NODE)
241 rtype = rtype->ctype.base_type;
242 if (is_float_type(ltype)) {
243 if (is_int_type(rtype))
244 goto Left;
245 if (is_float_type(rtype)) {
246 unsigned long lmod = ltype->ctype.modifiers;
247 unsigned long rmod = rtype->ctype.modifiers;
248 lmod &= MOD_LONG | MOD_LONGLONG;
249 rmod &= MOD_LONG | MOD_LONGLONG;
250 if (lmod == rmod)
251 return ltype;
252 if (lmod & ~rmod)
253 goto Left;
254 else
255 goto Right;
257 return NULL;
259 if (!is_float_type(rtype) || !is_int_type(ltype))
260 return NULL;
261 Right:
262 *lp = cast_to(left, rtype);
263 return rtype;
264 Left:
265 *rp = cast_to(right, ltype);
266 return ltype;
269 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
271 struct expression *left = *lp, *right = *rp;
272 struct symbol *ltype = left->ctype, *rtype = right->ctype;
274 if (ltype->type == SYM_NODE)
275 ltype = ltype->ctype.base_type;
276 if (rtype->type == SYM_NODE)
277 rtype = rtype->ctype.base_type;
278 if (is_int_type(ltype) && is_int_type(rtype)) {
279 struct symbol *ctype = bigger_int_type(ltype, rtype);
281 /* Don't bother promoting same-size entities, it only adds clutter */
282 if (ltype->bit_size != ctype->bit_size)
283 *lp = cast_to(left, ctype);
284 if (rtype->bit_size != ctype->bit_size)
285 *rp = cast_to(right, ctype);
286 return ctype;
288 return NULL;
291 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
293 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
294 if (!ctype && float_ok)
295 ctype = compatible_float_binop(&expr->left, &expr->right);
296 if (ctype) {
297 expr->ctype = ctype;
298 return ctype;
300 return bad_expr_type(expr);
303 static inline int lvalue_expression(struct expression *expr)
305 while (expr->type == EXPR_CAST)
306 expr = expr->cast_expression;
307 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
310 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
312 struct symbol *ctype;
313 struct symbol *ptr_type = ptr->ctype;
314 int bit_size;
316 if (ptr_type->type == SYM_NODE)
317 ptr_type = ptr_type->ctype.base_type;
319 if (!is_int_type(i->ctype))
320 return bad_expr_type(expr);
322 ctype = ptr->ctype;
323 examine_symbol_type(ctype);
325 ctype = degenerate(ptr);
326 if (!ctype->ctype.base_type) {
327 warn(expr->pos, "missing type information");
328 return NULL;
331 /* Get the size of whatever the pointer points to */
332 ptr_type = ctype;
333 if (ptr_type->type == SYM_NODE)
334 ptr_type = ptr_type->ctype.base_type;
335 if (ptr_type->type == SYM_PTR)
336 ptr_type = ptr_type->ctype.base_type;
337 bit_size = ptr_type->bit_size;
339 /* Special case: adding zero commonly happens as a result of 'array[0]' */
340 if (i->type == EXPR_VALUE && !i->value) {
341 *expr = *ptr;
342 } else if (bit_size > bits_in_char) {
343 struct expression *add = expr;
344 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
345 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
347 val->ctype = size_t_ctype;
348 val->value = bit_size >> 3;
350 mul->op = '*';
351 mul->ctype = size_t_ctype;
352 mul->left = i;
353 mul->right = val;
355 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
356 add->left = ptr;
357 add->right = mul;
360 expr->ctype = ctype;
361 return ctype;
364 static struct symbol *evaluate_add(struct expression *expr)
366 struct expression *left = expr->left, *right = expr->right;
367 struct symbol *ltype = left->ctype, *rtype = right->ctype;
369 if (is_ptr_type(ltype))
370 return evaluate_ptr_add(expr, left, right);
372 if (is_ptr_type(rtype))
373 return evaluate_ptr_add(expr, right, left);
375 return evaluate_arith(expr, 1);
378 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
379 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
381 const char * type_difference(struct symbol *target, struct symbol *source,
382 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
384 for (;;) {
385 unsigned long mod1, mod2, diff;
386 unsigned long as1, as2;
387 int type1, type2;
388 struct symbol *base1, *base2;
390 if (target == source)
391 break;
392 if (!target || !source)
393 return "different types";
395 * Peel of per-node information.
396 * FIXME! Check alignment and context too here!
398 mod1 = target->ctype.modifiers;
399 as1 = target->ctype.as;
400 mod2 = source->ctype.modifiers;
401 as2 = source->ctype.as;
402 if (target->type == SYM_NODE) {
403 target = target->ctype.base_type;
404 if (!target)
405 return "bad types";
406 if (target->type == SYM_PTR) {
407 mod1 = 0;
408 as1 = 0;
410 mod1 |= target->ctype.modifiers;
411 as1 |= target->ctype.as;
413 if (source->type == SYM_NODE) {
414 source = source->ctype.base_type;
415 if (!source)
416 return "bad types";
417 if (source->type == SYM_PTR) {
418 mod2 = 0;
419 as2 = 0;
421 mod2 |= source->ctype.modifiers;
422 as2 |= source->ctype.as;
425 if (target == source)
426 break;
427 if (!target || !source)
428 return "different types";
430 type1 = target->type;
431 base1 = target->ctype.base_type;
433 type2 = source->type;
434 base2 = source->ctype.base_type;
437 * Pointers to functions compare as the function itself
439 if (type1 == SYM_PTR && base1) {
440 switch (base1->type) {
441 case SYM_FN:
442 type1 = SYM_FN;
443 target = base1;
444 base1 = base1->ctype.base_type;
445 default:
446 /* nothing */;
449 if (type2 == SYM_PTR && base2) {
450 switch (base2->type) {
451 case SYM_FN:
452 type2 = SYM_FN;
453 source = base2;
454 base2 = base2->ctype.base_type;
455 default:
456 /* nothing */;
460 /* Arrays degenerate to pointers for type comparisons */
461 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
462 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
464 if (type1 != type2)
465 return "different base types";
467 /* Must be same address space to be comparable */
468 if (as1 != as2)
469 return "different address spaces";
471 /* Ignore differences in storage types, sign, or addressability */
472 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
473 if (diff) {
474 mod1 &= diff & ~target_mod_ignore;
475 mod2 &= diff & ~source_mod_ignore;
476 if (mod1 | mod2) {
477 if ((mod1 | mod2) & MOD_SIZE)
478 return "different type sizes";
479 return "different modifiers";
483 if (type1 == SYM_FN) {
484 int i;
485 struct symbol *arg1, *arg2;
486 if (base1->variadic != base2->variadic)
487 return "incompatible variadic arguments";
488 PREPARE_PTR_LIST(target->arguments, arg1);
489 PREPARE_PTR_LIST(source->arguments, arg2);
490 i = 1;
491 for (;;) {
492 const char *diff;
493 diff = type_difference(arg1, arg2, 0, 0);
494 if (diff) {
495 static char argdiff[80];
496 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
497 return argdiff;
499 if (!arg1)
500 break;
501 NEXT_PTR_LIST(arg1);
502 NEXT_PTR_LIST(arg2);
503 i++;
505 FINISH_PTR_LIST(arg2);
506 FINISH_PTR_LIST(arg1);
509 target = base1;
510 source = base2;
512 return NULL;
515 static int is_null_ptr(struct expression *expr)
517 if (expr->type != EXPR_VALUE || expr->value)
518 return 0;
519 if (!is_ptr_type(expr->ctype))
520 warn(expr->pos, "Using plain integer as NULL pointer");
521 return 1;
524 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
526 /* NULL expression? Just return the type of the "other side" */
527 if (is_null_ptr(r))
528 return l->ctype;
529 if (is_null_ptr(l))
530 return r->ctype;
531 return NULL;
535 * Ignore differences in "volatile" and "const"ness when
536 * subtracting pointers
538 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
540 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
542 const char *typediff;
543 struct symbol *ctype;
544 struct symbol *ltype, *rtype;
546 ltype = degenerate(l);
547 rtype = degenerate(r);
550 * If it is an integer subtract: the ptr add case will do the
551 * right thing.
553 if (!is_ptr_type(rtype))
554 return evaluate_ptr_add(expr, l, r);
556 ctype = ltype;
557 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
558 if (typediff) {
559 ctype = common_ptr_type(l, r);
560 if (!ctype) {
561 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
562 return NULL;
565 examine_symbol_type(ctype);
567 /* Figure out the base type we point to */
568 if (ctype->type == SYM_NODE)
569 ctype = ctype->ctype.base_type;
570 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
571 warn(expr->pos, "subtraction of functions? Share your drugs");
572 return NULL;
574 ctype = ctype->ctype.base_type;
576 expr->ctype = ssize_t_ctype;
577 if (ctype->bit_size > bits_in_char) {
578 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
579 struct expression *div = expr;
580 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
582 val->ctype = size_t_ctype;
583 val->value = ctype->bit_size >> 3;
585 sub->op = '-';
586 sub->ctype = ssize_t_ctype;
587 sub->left = l;
588 sub->right = r;
590 div->op = '/';
591 div->left = sub;
592 div->right = val;
595 return ssize_t_ctype;
598 static struct symbol *evaluate_sub(struct expression *expr)
600 struct expression *left = expr->left, *right = expr->right;
601 struct symbol *ltype = left->ctype;
603 if (is_ptr_type(ltype))
604 return evaluate_ptr_sub(expr, left, right);
606 return evaluate_arith(expr, 1);
609 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
611 static struct symbol *evaluate_conditional(struct expression **p)
613 struct symbol *ctype;
614 struct expression *expr = *p;
616 if (!expr)
617 return NULL;
619 if (expr->type == EXPR_ASSIGNMENT)
620 warn(expr->pos, "assignment expression in conditional");
622 ctype = evaluate_expression(expr);
623 if (ctype) {
624 if (is_safe_type(ctype))
625 warn(expr->pos, "testing a 'safe expression'");
626 if (is_float_type(ctype)) {
627 struct expression *comp;
629 * It's easier to handle here, rather than deal with
630 * FP all over the place. Floating point in boolean
631 * context is rare enough (and very often wrong),
632 * so price of explicit comparison with appropriate
633 * FP zero is not too high. And it simplifies things
634 * elsewhere.
636 comp = alloc_expression(expr->pos, EXPR_BINOP);
637 comp->op = SPECIAL_NOTEQUAL;
638 comp->left = expr;
639 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
640 comp->right->ctype = comp->left->ctype;
641 comp->right->fvalue = 0;
642 ctype = comp->ctype = &bool_ctype;
643 *p = comp;
647 return ctype;
650 static struct symbol *evaluate_logical(struct expression *expr)
652 if (!evaluate_conditional(&expr->left))
653 return NULL;
654 if (!evaluate_conditional(&expr->right))
655 return NULL;
657 expr->ctype = &bool_ctype;
658 return &bool_ctype;
661 static struct symbol *evaluate_shift(struct expression *expr)
663 struct expression *left = expr->left, *right = expr->right;
664 struct symbol *ltype = left->ctype, *rtype = right->ctype;
666 if (ltype->type == SYM_NODE)
667 ltype = ltype->ctype.base_type;
668 if (rtype->type == SYM_NODE)
669 rtype = rtype->ctype.base_type;
670 if (is_int_type(ltype) && is_int_type(rtype)) {
671 struct symbol *ctype = integer_promotion(ltype);
672 if (ltype->bit_size != ctype->bit_size)
673 expr->left = cast_to(expr->left, ctype);
674 expr->ctype = ctype;
675 ctype = integer_promotion(rtype);
676 if (rtype->bit_size != ctype->bit_size)
677 expr->right = cast_to(expr->right, ctype);
678 return expr->ctype;
680 return bad_expr_type(expr);
683 static struct symbol *evaluate_binop(struct expression *expr)
685 switch (expr->op) {
686 // addition can take ptr+int, fp and int
687 case '+':
688 return evaluate_add(expr);
690 // subtraction can take ptr-ptr, fp and int
691 case '-':
692 return evaluate_sub(expr);
694 // Arithmetic operations can take fp and int
695 case '*': case '/':
696 return evaluate_arith(expr, 1);
698 // shifts do integer promotions, but that's it.
699 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
700 return evaluate_shift(expr);
702 // The rest are integer operations
703 // '%', '&', '^', '|'
704 default:
705 return evaluate_arith(expr, 0);
709 static struct symbol *evaluate_comma(struct expression *expr)
711 expr->ctype = expr->right->ctype;
712 return expr->ctype;
715 static int modify_for_unsigned(int op)
717 if (op == '<')
718 op = SPECIAL_UNSIGNED_LT;
719 else if (op == '>')
720 op = SPECIAL_UNSIGNED_GT;
721 else if (op == SPECIAL_LTE)
722 op = SPECIAL_UNSIGNED_LTE;
723 else if (op == SPECIAL_GTE)
724 op = SPECIAL_UNSIGNED_GTE;
725 return op;
728 static struct symbol *evaluate_compare(struct expression *expr)
730 struct expression *left = expr->left, *right = expr->right;
731 struct symbol *ltype = left->ctype, *rtype = right->ctype;
732 struct symbol *ctype;
734 /* Type types? */
735 if (is_type_type(ltype) && is_type_type(rtype)) {
736 expr->ctype = &bool_ctype;
737 return &bool_ctype;
740 if (is_safe_type(ltype) || is_safe_type(rtype))
741 warn(expr->pos, "testing a 'safe expression'");
743 /* Pointer types? */
744 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
745 expr->ctype = &bool_ctype;
746 // FIXME! Check the types for compatibility
747 return &bool_ctype;
750 ctype = compatible_integer_binop(&expr->left, &expr->right);
751 if (ctype) {
752 if (ctype->ctype.modifiers & MOD_UNSIGNED)
753 expr->op = modify_for_unsigned(expr->op);
754 expr->ctype = &bool_ctype;
755 return &bool_ctype;
757 ctype = compatible_float_binop(&expr->left, &expr->right);
758 if (ctype) {
759 expr->ctype = &bool_ctype;
760 return &bool_ctype;
763 return bad_expr_type(expr);
767 * FIXME!! This should do casts, array degeneration etc..
769 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
771 struct symbol *ltype = left->ctype, *rtype = right->ctype;
773 if (ltype->type == SYM_NODE)
774 ltype = ltype->ctype.base_type;
776 if (rtype->type == SYM_NODE)
777 rtype = rtype->ctype.base_type;
779 if (ltype->type == SYM_PTR) {
780 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
781 return ltype;
784 if (rtype->type == SYM_PTR) {
785 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
786 return rtype;
788 return NULL;
791 static struct symbol * evaluate_conditional_expression(struct expression *expr)
793 struct expression *cond, *true, *false;
794 struct symbol *ctype, *ltype, *rtype;
795 const char * typediff;
797 ctype = degenerate(expr->conditional);
798 cond = expr->conditional;
800 ltype = ctype;
801 true = cond;
802 if (expr->cond_true) {
803 ltype = degenerate(expr->cond_true);
804 true = expr->cond_true;
807 rtype = degenerate(expr->cond_false);
808 false = expr->cond_false;
810 ctype = ltype;
811 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
812 if (!typediff)
813 goto out;
815 ctype = compatible_integer_binop(&true, &expr->cond_false);
816 if (ctype)
817 goto out;
818 ctype = compatible_ptr_type(true, expr->cond_false);
819 if (ctype)
820 goto out;
821 ctype = compatible_float_binop(&true, &expr->cond_false);
822 if (ctype)
823 goto out;
824 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
825 return NULL;
827 out:
828 expr->ctype = ctype;
829 return ctype;
832 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
833 struct expression **rp, struct symbol *source, const char *where)
835 const char *typediff;
836 struct symbol *t;
837 int target_as;
839 /* It's ok if the target is more volatile or const than the source */
840 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
841 if (!typediff)
842 return 1;
844 if (is_int_type(target)) {
845 if (is_int_type(source)) {
846 if (target->bit_size != source->bit_size)
847 goto Cast;
848 return 1;
850 if (is_float_type(source))
851 goto Cast;
852 } else if (is_float_type(target)) {
853 if (is_int_type(source))
854 goto Cast;
855 if (is_float_type(source)) {
856 if (target->bit_size != source->bit_size)
857 goto Cast;
858 return 1;
862 /* Pointer destination? */
863 t = target;
864 target_as = t->ctype.as;
865 if (t->type == SYM_NODE) {
866 t = t->ctype.base_type;
867 target_as |= t->ctype.as;
869 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
870 struct expression *right = *rp;
871 struct symbol *s = source;
872 int source_as;
874 // NULL pointer is always ok
875 if (is_null_ptr(right))
876 return 1;
878 /* "void *" matches anything as long as the address space is ok */
879 source_as = s->ctype.as;
880 if (s->type == SYM_NODE) {
881 s = s->ctype.base_type;
882 source_as |= s->ctype.as;
884 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
885 s = s->ctype.base_type;
886 t = t->ctype.base_type;
887 if (s == &void_ctype || t == &void_ctype)
888 return 1;
892 // FIXME!! Cast it?
893 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
894 info(expr->pos, " expected %s", show_typename(target));
895 info(expr->pos, " got %s", show_typename(source));
896 return 0;
897 Cast:
898 *rp = cast_to(*rp, target);
899 return 1;
903 * FIXME!! This is wrong from a double evaluation standpoint. We can't
904 * just expand the expression twice, that would make any side effects
905 * happen twice too.
907 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
909 int op = expr->op;
910 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
911 static const int op_trans[] = {
912 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
913 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
914 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
915 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
916 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
917 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
918 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
919 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
920 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
921 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
924 subexpr->left = left;
925 subexpr->right = right;
926 subexpr->op = op_trans[op - SPECIAL_BASE];
927 expr->op = '=';
928 expr->right = subexpr;
929 return evaluate_binop(subexpr);
932 static void evaluate_assign_to(struct expression *left, struct symbol *type)
934 if (type->ctype.modifiers & MOD_CONST)
935 warn(left->pos, "assignment to const expression");
936 if (type->type == SYM_NODE)
937 type->ctype.modifiers |= MOD_ASSIGNED;
940 static struct symbol *evaluate_assignment(struct expression *expr)
942 struct expression *left = expr->left, *right = expr->right;
943 struct symbol *ltype, *rtype;
945 ltype = left->ctype;
946 rtype = right->ctype;
947 if (expr->op != '=') {
948 rtype = evaluate_binop_assignment(expr, left, right);
949 if (!rtype)
950 return NULL;
951 right = expr->right;
954 if (!lvalue_expression(left)) {
955 warn(expr->pos, "not an lvalue");
956 return NULL;
959 rtype = degenerate(right);
961 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
962 return NULL;
964 evaluate_assign_to(left, ltype);
966 expr->ctype = ltype;
967 return ltype;
970 static void examine_fn_arguments(struct symbol *fn)
972 struct symbol *s;
974 FOR_EACH_PTR(fn->arguments, s) {
975 struct symbol *arg = evaluate_symbol(s);
976 /* Array/function arguments silently degenerate into pointers */
977 if (arg) {
978 struct symbol *ptr;
979 switch(arg->type) {
980 case SYM_ARRAY:
981 case SYM_FN:
982 ptr = alloc_symbol(s->pos, SYM_PTR);
983 if (arg->type == SYM_ARRAY)
984 ptr->ctype = arg->ctype;
985 else
986 ptr->ctype.base_type = arg;
987 ptr->ctype.as |= s->ctype.as;
988 ptr->ctype.modifiers |= s->ctype.modifiers;
990 s->ctype.base_type = ptr;
991 s->ctype.as = 0;
992 s->ctype.modifiers = 0;
993 examine_symbol_type(s);
994 break;
995 default:
996 /* nothing */
997 break;
1000 } END_FOR_EACH_PTR;
1003 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1005 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1006 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1007 *newsym = *sym;
1008 newsym->ctype.as = as;
1009 newsym->ctype.modifiers = mod;
1010 sym = newsym;
1012 return sym;
1015 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1017 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1018 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1020 node->ctype.base_type = ptr;
1021 ptr->bit_size = bits_in_pointer;
1022 ptr->ctype.alignment = pointer_alignment;
1024 node->bit_size = bits_in_pointer;
1025 node->ctype.alignment = pointer_alignment;
1027 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1028 if (sym->ctype.modifiers & MOD_REGISTER) {
1029 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1030 sym->ctype.modifiers &= ~MOD_REGISTER;
1032 if (sym->type == SYM_NODE) {
1033 ptr->ctype.as |= sym->ctype.as;
1034 ptr->ctype.modifiers |= sym->ctype.modifiers;
1035 sym = sym->ctype.base_type;
1037 if (degenerate && sym->type == SYM_ARRAY) {
1038 ptr->ctype.as |= sym->ctype.as;
1039 ptr->ctype.modifiers |= sym->ctype.modifiers;
1040 sym = sym->ctype.base_type;
1042 ptr->ctype.base_type = sym;
1044 return node;
1047 /* Arrays degenerate into pointers on pointer arithmetic */
1048 static struct symbol *degenerate(struct expression *expr)
1050 struct symbol *ctype, *base;
1052 if (!expr)
1053 return NULL;
1054 ctype = expr->ctype;
1055 if (!ctype)
1056 return NULL;
1057 base = ctype;
1058 if (ctype->type == SYM_NODE)
1059 base = ctype->ctype.base_type;
1061 * Arrays degenerate into pointers to the entries, while
1062 * functions degenerate into pointers to themselves
1064 switch (base->type) {
1065 case SYM_FN:
1066 case SYM_ARRAY:
1067 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1068 warn(expr->pos, "strange non-value function or array");
1069 return NULL;
1071 *expr = *expr->unop;
1072 ctype = create_pointer(expr, ctype, 1);
1073 expr->ctype = ctype;
1074 default:
1075 /* nothing */;
1077 return ctype;
1080 static struct symbol *evaluate_addressof(struct expression *expr)
1082 struct expression *op = expr->unop;
1083 struct symbol *ctype;
1085 if (op->op != '*' || op->type != EXPR_PREOP) {
1086 warn(expr->pos, "not addressable");
1087 return NULL;
1089 ctype = op->ctype;
1090 *expr = *op->unop;
1093 * symbol expression evaluation is lazy about the type
1094 * of the sub-expression, so we may have to generate
1095 * the type here if so..
1097 if (!expr->ctype) {
1098 ctype = create_pointer(expr, ctype, 0);
1099 expr->ctype = ctype;
1101 return expr->ctype;
1105 static struct symbol *evaluate_dereference(struct expression *expr)
1107 struct expression *op = expr->unop;
1108 struct symbol *ctype = op->ctype, *node, *target;
1110 /* Simplify: *&(expr) => (expr) */
1111 if (op->type == EXPR_PREOP && op->op == '&') {
1112 *expr = *op->unop;
1113 return expr->ctype;
1116 /* Dereferencing a node drops all the node information. */
1117 if (ctype->type == SYM_NODE)
1118 ctype = ctype->ctype.base_type;
1120 node = alloc_symbol(expr->pos, SYM_NODE);
1121 target = ctype->ctype.base_type;
1123 switch (ctype->type) {
1124 default:
1125 warn(expr->pos, "cannot derefence this type");
1126 return NULL;
1127 case SYM_PTR:
1128 merge_type(node, ctype);
1129 if (ctype->type != SYM_ARRAY)
1130 break;
1132 * Dereferencing a pointer to an array results in a
1133 * degenerate dereference: the expression becomes
1134 * just a pointer to the entry, and the derefence
1135 * goes away.
1137 *expr = *op;
1139 target = alloc_symbol(expr->pos, SYM_PTR);
1140 target->bit_size = bits_in_pointer;
1141 target->ctype.alignment = pointer_alignment;
1142 merge_type(target, ctype->ctype.base_type);
1143 break;
1145 case SYM_ARRAY:
1147 * When an array is dereferenced, we need to pick
1148 * up the attributes of the original node too..
1150 merge_type(node, op->ctype);
1151 merge_type(node, ctype);
1152 break;
1155 node->bit_size = target->bit_size;
1156 node->array_size = target->array_size;
1158 expr->ctype = node;
1159 return node;
1163 * Unary post-ops: x++ and x--
1165 static struct symbol *evaluate_postop(struct expression *expr)
1167 struct expression *op = expr->unop;
1168 struct symbol *ctype = op->ctype;
1170 if (!lvalue_expression(expr->unop)) {
1171 warn(expr->pos, "need lvalue expression for ++/--");
1172 return NULL;
1175 evaluate_assign_to(op, ctype);
1177 expr->ctype = ctype;
1178 return ctype;
1181 static struct symbol *evaluate_sign(struct expression *expr)
1183 struct symbol *ctype = expr->unop->ctype;
1184 if (is_int_type(ctype)) {
1185 struct symbol *rtype = rtype = integer_promotion(ctype);
1186 if (rtype->bit_size != ctype->bit_size)
1187 expr->unop = cast_to(expr->unop, rtype);
1188 ctype = rtype;
1189 } else if (is_float_type(ctype) && expr->op != '%') {
1190 /* no conversions needed */
1191 } else {
1192 return bad_expr_type(expr);
1194 if (expr->op == '+')
1195 *expr = *expr->unop;
1196 expr->ctype = ctype;
1197 return ctype;
1200 static struct symbol *evaluate_preop(struct expression *expr)
1202 struct symbol *ctype = expr->unop->ctype;
1204 switch (expr->op) {
1205 case '(':
1206 *expr = *expr->unop;
1207 return ctype;
1209 case '+':
1210 case '-':
1211 case '~':
1212 return evaluate_sign(expr);
1214 case '*':
1215 return evaluate_dereference(expr);
1217 case '&':
1218 return evaluate_addressof(expr);
1220 case SPECIAL_INCREMENT:
1221 case SPECIAL_DECREMENT:
1223 * From a type evaluation standpoint the pre-ops are
1224 * the same as the postops
1226 return evaluate_postop(expr);
1228 case '!':
1229 if (is_safe_type(ctype))
1230 warn(expr->pos, "testing a 'safe expression'");
1231 if (is_float_type(ctype)) {
1232 struct expression *arg = expr->unop;
1233 expr->type = EXPR_BINOP;
1234 expr->op = SPECIAL_EQUAL;
1235 expr->left = arg;
1236 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1237 expr->right->ctype = ctype;
1238 expr->right->fvalue = 0;
1240 ctype = &bool_ctype;
1241 break;
1243 default:
1244 break;
1246 expr->ctype = ctype;
1247 return &bool_ctype;
1250 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1252 struct ptr_list *head = (struct ptr_list *)_list;
1253 struct ptr_list *list = head;
1255 if (!head)
1256 return NULL;
1257 do {
1258 int i;
1259 for (i = 0; i < list->nr; i++) {
1260 struct symbol *sym = (struct symbol *) list->list[i];
1261 if (sym->ident) {
1262 if (sym->ident != ident)
1263 continue;
1264 *offset = sym->offset;
1265 return sym;
1266 } else {
1267 struct symbol *ctype = sym->ctype.base_type;
1268 struct symbol *sub;
1269 if (!ctype)
1270 continue;
1271 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1272 continue;
1273 sub = find_identifier(ident, ctype->symbol_list, offset);
1274 if (!sub)
1275 continue;
1276 *offset += sym->offset;
1277 return sub;
1280 } while ((list = list->next) != head);
1281 return NULL;
1284 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1286 struct expression *add;
1289 * Create a new add-expression
1291 * NOTE! Even if we just add zero, we need a new node
1292 * for the member pointer, since it has a different
1293 * type than the original pointer. We could make that
1294 * be just a cast, but the fact is, a node is a node,
1295 * so we might as well just do the "add zero" here.
1297 add = alloc_expression(expr->pos, EXPR_BINOP);
1298 add->op = '+';
1299 add->left = expr;
1300 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1301 add->right->ctype = &int_ctype;
1302 add->right->value = offset;
1305 * The ctype of the pointer will be lazily evaluated if
1306 * we ever take the address of this member dereference..
1308 add->ctype = NULL;
1309 return add;
1312 /* structure/union dereference */
1313 static struct symbol *evaluate_member_dereference(struct expression *expr)
1315 int offset;
1316 struct symbol *ctype, *member;
1317 struct expression *deref = expr->deref, *add;
1318 struct ident *ident = expr->member;
1319 unsigned int mod;
1320 int address_space;
1322 if (!evaluate_expression(deref))
1323 return NULL;
1324 if (!ident) {
1325 warn(expr->pos, "bad member name");
1326 return NULL;
1329 ctype = deref->ctype;
1330 address_space = ctype->ctype.as;
1331 mod = ctype->ctype.modifiers;
1332 if (ctype->type == SYM_NODE) {
1333 ctype = ctype->ctype.base_type;
1334 address_space |= ctype->ctype.as;
1335 mod |= ctype->ctype.modifiers;
1337 if (!lvalue_expression(deref)) {
1338 warn(deref->pos, "expected lvalue for member dereference");
1339 return NULL;
1341 deref = deref->unop;
1342 expr->deref = deref;
1343 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1344 warn(expr->pos, "expected structure or union");
1345 return NULL;
1347 offset = 0;
1348 member = find_identifier(ident, ctype->symbol_list, &offset);
1349 if (!member) {
1350 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1351 const char *name = "<unnamed>";
1352 int namelen = 9;
1353 if (ctype->ident) {
1354 name = ctype->ident->name;
1355 namelen = ctype->ident->len;
1357 warn(expr->pos, "no member '%s' in %s %.*s",
1358 show_ident(ident), type, namelen, name);
1359 return NULL;
1363 * The member needs to take on the address space and modifiers of
1364 * the "parent" type.
1366 member = convert_to_as_mod(member, address_space, mod);
1367 add = evaluate_offset(deref, offset);
1369 ctype = member->ctype.base_type;
1370 if (ctype->type == SYM_BITFIELD) {
1371 expr->type = EXPR_BITFIELD;
1372 expr->bitpos = member->bit_offset;
1373 expr->nrbits = member->fieldwidth;
1374 expr->address = add;
1375 } else {
1376 expr->type = EXPR_PREOP;
1377 expr->op = '*';
1378 expr->unop = add;
1381 expr->ctype = member;
1382 return member;
1385 static struct symbol *evaluate_sizeof(struct expression *expr)
1387 int size;
1389 if (expr->cast_type) {
1390 examine_symbol_type(expr->cast_type);
1391 size = expr->cast_type->bit_size;
1392 } else {
1393 if (!evaluate_expression(expr->cast_expression))
1394 return NULL;
1395 size = expr->cast_expression->ctype->bit_size;
1397 if (size & 7) {
1398 warn(expr->pos, "cannot size expression");
1399 return NULL;
1401 expr->type = EXPR_VALUE;
1402 expr->value = size >> 3;
1403 expr->ctype = size_t_ctype;
1404 return size_t_ctype;
1407 static struct symbol *evaluate_alignof(struct expression *expr)
1409 struct symbol *type = expr->cast_type;
1411 if (!type) {
1412 type = evaluate_expression(expr->cast_expression);
1413 if (!type)
1414 return NULL;
1416 examine_symbol_type(type);
1417 expr->type = EXPR_VALUE;
1418 expr->value = type->ctype.alignment;
1419 expr->ctype = size_t_ctype;
1420 return size_t_ctype;
1423 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1425 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1426 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1427 return clash != 0;
1430 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1432 struct expression *expr;
1433 struct symbol_list *argument_types = fn->arguments;
1434 struct symbol *argtype;
1435 int i = 1;
1437 PREPARE_PTR_LIST(argument_types, argtype);
1438 FOR_EACH_PTR (head, expr) {
1439 struct expression **p = THIS_ADDRESS(expr);
1440 struct symbol *ctype, *target;
1441 ctype = evaluate_expression(expr);
1443 if (!ctype)
1444 return 0;
1446 if (context_clash(f, ctype))
1447 warn(expr->pos, "argument %d used in wrong context", i);
1449 ctype = degenerate(expr);
1451 target = argtype;
1452 if (!target && ctype->bit_size < bits_in_int)
1453 target = &int_ctype;
1454 if (target) {
1455 static char where[30];
1456 examine_symbol_type(target);
1457 sprintf(where, "argument %d", i);
1458 compatible_assignment_types(expr, target, p, ctype, where);
1461 i++;
1462 NEXT_PTR_LIST(argtype);
1463 } END_FOR_EACH_PTR;
1464 FINISH_PTR_LIST(argtype);
1465 return 1;
1468 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1469 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1471 struct expression *entry;
1472 int current = 0;
1473 int max = 0;
1475 FOR_EACH_PTR(expr->expr_list, entry) {
1476 struct expression **p = THIS_ADDRESS(entry);
1478 if (entry->type == EXPR_INDEX) {
1479 current = entry->idx_to;
1480 continue;
1482 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1483 current++;
1484 if (current > max)
1485 max = current;
1486 } END_FOR_EACH_PTR;
1487 return max;
1490 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1491 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1493 if (offset || expression_list_size(expr->expr_list) != 1) {
1494 warn(expr->pos, "unexpected compound initializer");
1495 return 0;
1497 return evaluate_array_initializer(ctype, expr, 0);
1500 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1502 struct expression *entry;
1503 struct symbol *sym;
1505 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1506 FOR_EACH_PTR(expr->expr_list, entry) {
1507 struct expression **p = THIS_ADDRESS(entry);
1509 if (entry->type == EXPR_IDENTIFIER) {
1510 struct ident *ident = entry->expr_ident;
1511 /* We special-case the "already right place" case */
1512 if (sym && sym->ident == ident)
1513 continue;
1514 RESET_PTR_LIST(sym);
1515 for (;;) {
1516 if (!sym) {
1517 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1518 return 0;
1520 if (sym->ident == ident)
1521 break;
1522 NEXT_PTR_LIST(sym);
1524 continue;
1527 if (!sym) {
1528 warn(expr->pos, "too many initializers for struct/union");
1529 return 0;
1532 evaluate_initializer(sym, p, offset + sym->offset);
1534 NEXT_PTR_LIST(sym);
1535 } END_FOR_EACH_PTR;
1536 FINISH_PTR_LIST(sym);
1538 return 0;
1542 * Initializers are kind of like assignments. Except
1543 * they can be a hell of a lot more complex.
1545 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1547 struct expression *expr = *ep;
1550 * Simple non-structure/array initializers are the simple
1551 * case, and look (and parse) largely like assignments.
1553 if (expr->type != EXPR_INITIALIZER) {
1554 int size = 0;
1555 struct symbol *rtype = evaluate_expression(expr);
1556 if (rtype) {
1557 struct expression *pos;
1559 // FIXME! char array[] = "string" special case
1560 // should _not_ degenerate.
1561 rtype = degenerate(expr);
1562 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1563 /* strings are special: char arrays */
1564 if (rtype->type == SYM_ARRAY)
1565 size = get_expression_value(rtype->array_size);
1567 * Don't bother creating a position expression for
1568 * the simple initializer cases that don't need it.
1570 * We need a position if the initializer has a byte
1571 * offset, _or_ if we're initializing a bitfield.
1573 if (offset || ctype->fieldwidth) {
1574 pos = alloc_expression(expr->pos, EXPR_POS);
1575 pos->init_offset = offset;
1576 pos->init_sym = ctype;
1577 pos->init_expr = *ep;
1578 pos->ctype = expr->ctype;
1579 *ep = pos;
1582 return size;
1585 expr->ctype = ctype;
1586 if (ctype->type == SYM_NODE)
1587 ctype = ctype->ctype.base_type;
1589 switch (ctype->type) {
1590 case SYM_ARRAY:
1591 case SYM_PTR:
1592 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1593 case SYM_UNION:
1594 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1595 case SYM_STRUCT:
1596 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1597 default:
1598 return evaluate_scalar_initializer(ctype, expr, offset);
1602 static int get_as(struct symbol *sym)
1604 int as;
1605 unsigned long mod;
1607 if (!sym)
1608 return 0;
1609 as = sym->ctype.as;
1610 mod = sym->ctype.modifiers;
1611 if (sym->type == SYM_NODE) {
1612 sym = sym->ctype.base_type;
1613 as |= sym->ctype.as;
1614 mod |= sym->ctype.modifiers;
1617 * You can always throw a value away by casting to
1618 * "void" - that's an implicit "force". Note that
1619 * the same is _not_ true of "void *".
1621 if (sym == &void_ctype)
1622 return -1;
1625 * At least for now, allow casting to a "unsigned long".
1626 * That's how we do things like pointer arithmetic and
1627 * store pointers to registers.
1629 if (sym == &ulong_ctype)
1630 return -1;
1632 if (sym && sym->type == SYM_PTR) {
1633 sym = sym->ctype.base_type;
1634 as |= sym->ctype.as;
1635 mod |= sym->ctype.modifiers;
1637 if (mod & MOD_FORCE)
1638 return -1;
1639 return as;
1642 static struct symbol *evaluate_cast(struct expression *expr)
1644 struct expression *target = expr->cast_expression;
1645 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1647 expr->ctype = ctype;
1648 expr->cast_type = ctype;
1651 * Special case: a cast can be followed by an
1652 * initializer, in which case we need to pass
1653 * the type value down to that initializer rather
1654 * than trying to evaluate it as an expression
1656 * A more complex case is when the initializer is
1657 * dereferenced as part of a post-fix expression.
1658 * We need to produce an expression that can be dereferenced.
1660 if (target->type == EXPR_INITIALIZER) {
1661 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1662 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1664 sym->ctype.base_type = ctype;
1665 sym->initializer = expr->cast_expression;
1666 evaluate_symbol(sym);
1668 addr->ctype = NULL; /* Lazy eval */
1669 addr->symbol = sym;
1671 expr->type = EXPR_PREOP;
1672 expr->op = '*';
1673 expr->unop = addr;
1674 expr->ctype = ctype;
1675 return ctype;
1678 evaluate_expression(target);
1679 degenerate(target);
1681 if (!get_as(ctype) && get_as(target->ctype) > 0)
1682 warn(expr->pos, "cast removes address space of expression");
1685 * Casts of constant values are special: they
1686 * can be NULL, and thus need to be simplified
1687 * early.
1689 if (target->type == EXPR_VALUE)
1690 cast_value(expr, ctype, target, target->ctype);
1692 return ctype;
1696 * Evaluate a call expression with a symbol. This
1697 * should expand inline functions, and evaluate
1698 * builtins.
1700 static int evaluate_symbol_call(struct expression *expr)
1702 struct expression *fn = expr->fn;
1703 struct symbol *ctype = fn->ctype;
1705 if (fn->type != EXPR_PREOP)
1706 return 0;
1708 if (ctype->op && ctype->op->evaluate)
1709 return ctype->op->evaluate(expr);
1711 if (ctype->ctype.modifiers & MOD_INLINE) {
1712 int ret;
1713 struct symbol *curr = current_fn;
1714 unsigned long context = current_context;
1715 unsigned long mask = current_contextmask;
1717 current_context |= ctype->ctype.context;
1718 current_contextmask |= ctype->ctype.contextmask;
1719 current_fn = ctype->ctype.base_type;
1720 examine_fn_arguments(current_fn);
1722 ret = inline_function(expr, ctype);
1724 /* restore the old function context */
1725 current_fn = curr;
1726 current_context = context;
1727 current_contextmask = mask;
1728 return ret;
1731 return 0;
1734 static struct symbol *evaluate_call(struct expression *expr)
1736 int args, fnargs;
1737 struct symbol *ctype, *sym;
1738 struct expression *fn = expr->fn;
1739 struct expression_list *arglist = expr->args;
1741 if (!evaluate_expression(fn))
1742 return NULL;
1743 sym = ctype = fn->ctype;
1744 if (ctype->type == SYM_NODE)
1745 ctype = ctype->ctype.base_type;
1746 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1747 ctype = ctype->ctype.base_type;
1748 if (!evaluate_arguments(sym, ctype, arglist))
1749 return NULL;
1750 if (ctype->type != SYM_FN) {
1751 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1752 return NULL;
1754 args = expression_list_size(expr->args);
1755 fnargs = symbol_list_size(ctype->arguments);
1756 if (args < fnargs)
1757 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1758 if (args > fnargs && !ctype->variadic)
1759 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1760 if (sym->type == SYM_NODE) {
1761 if (evaluate_symbol_call(expr))
1762 return expr->ctype;
1764 expr->ctype = ctype->ctype.base_type;
1765 return expr->ctype;
1768 struct symbol *evaluate_expression(struct expression *expr)
1770 if (!expr)
1771 return NULL;
1772 if (expr->ctype)
1773 return expr->ctype;
1775 switch (expr->type) {
1776 case EXPR_VALUE:
1777 case EXPR_FVALUE:
1778 warn(expr->pos, "value expression without a type");
1779 return NULL;
1780 case EXPR_STRING:
1781 return evaluate_string(expr);
1782 case EXPR_SYMBOL:
1783 return evaluate_symbol_expression(expr);
1784 case EXPR_BINOP:
1785 if (!evaluate_expression(expr->left))
1786 return NULL;
1787 if (!evaluate_expression(expr->right))
1788 return NULL;
1789 return evaluate_binop(expr);
1790 case EXPR_LOGICAL:
1791 return evaluate_logical(expr);
1792 case EXPR_COMMA:
1793 if (!evaluate_expression(expr->left))
1794 return NULL;
1795 if (!evaluate_expression(expr->right))
1796 return NULL;
1797 return evaluate_comma(expr);
1798 case EXPR_COMPARE:
1799 if (!evaluate_expression(expr->left))
1800 return NULL;
1801 if (!evaluate_expression(expr->right))
1802 return NULL;
1803 return evaluate_compare(expr);
1804 case EXPR_ASSIGNMENT:
1805 if (!evaluate_expression(expr->left))
1806 return NULL;
1807 if (!evaluate_expression(expr->right))
1808 return NULL;
1809 return evaluate_assignment(expr);
1810 case EXPR_PREOP:
1811 if (!evaluate_expression(expr->unop))
1812 return NULL;
1813 return evaluate_preop(expr);
1814 case EXPR_POSTOP:
1815 if (!evaluate_expression(expr->unop))
1816 return NULL;
1817 return evaluate_postop(expr);
1818 case EXPR_CAST:
1819 return evaluate_cast(expr);
1820 case EXPR_SIZEOF:
1821 return evaluate_sizeof(expr);
1822 case EXPR_ALIGNOF:
1823 return evaluate_alignof(expr);
1824 case EXPR_DEREF:
1825 return evaluate_member_dereference(expr);
1826 case EXPR_CALL:
1827 return evaluate_call(expr);
1828 case EXPR_BITFIELD:
1829 warn(expr->pos, "bitfield generated by parser");
1830 return NULL;
1831 case EXPR_CONDITIONAL:
1832 if (!evaluate_conditional(&expr->conditional))
1833 return NULL;
1834 if (!evaluate_expression(expr->cond_false))
1835 return NULL;
1836 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1837 return NULL;
1838 return evaluate_conditional_expression(expr);
1839 case EXPR_STATEMENT:
1840 expr->ctype = evaluate_statement(expr->statement);
1841 return expr->ctype;
1843 case EXPR_LABEL:
1844 expr->ctype = &ptr_ctype;
1845 return &ptr_ctype;
1847 case EXPR_TYPE:
1848 /* Evaluate the type of the symbol .. */
1849 evaluate_symbol(expr->symbol);
1850 /* .. but the type of the _expression_ is a "type" */
1851 expr->ctype = &type_ctype;
1852 return &type_ctype;
1854 /* These can not exist as stand-alone expressions */
1855 case EXPR_INITIALIZER:
1856 case EXPR_IDENTIFIER:
1857 case EXPR_INDEX:
1858 case EXPR_POS:
1859 warn(expr->pos, "internal front-end error: initializer in expression");
1860 return NULL;
1862 return NULL;
1865 void check_duplicates(struct symbol *sym)
1867 struct symbol *next = sym;
1869 while ((next = next->same_symbol) != NULL) {
1870 const char *typediff;
1871 evaluate_symbol(next);
1872 typediff = type_difference(sym, next, 0, 0);
1873 if (typediff) {
1874 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1875 show_ident(sym->ident),
1876 input_streams[next->pos.stream].name, next->pos.line, typediff);
1877 return;
1882 struct symbol *evaluate_symbol(struct symbol *sym)
1884 struct symbol *base_type;
1886 if (!sym)
1887 return sym;
1889 sym = examine_symbol_type(sym);
1890 base_type = sym->ctype.base_type;
1891 if (!base_type)
1892 return NULL;
1894 /* Evaluate the initializers */
1895 if (sym->initializer) {
1896 int count = evaluate_initializer(sym, &sym->initializer, 0);
1897 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1898 int bit_size = count * base_type->ctype.base_type->bit_size;
1899 base_type->array_size = alloc_const_expression(sym->pos, count);
1900 base_type->bit_size = bit_size;
1901 sym->array_size = base_type->array_size;
1902 sym->bit_size = bit_size;
1906 /* And finally, evaluate the body of the symbol too */
1907 if (base_type->type == SYM_FN) {
1908 examine_fn_arguments(base_type);
1909 if (base_type->stmt) {
1910 current_fn = base_type;
1911 current_contextmask = sym->ctype.contextmask;
1912 current_context = sym->ctype.context;
1913 evaluate_statement(base_type->stmt);
1917 return base_type;
1920 struct symbol *evaluate_return_expression(struct statement *stmt)
1922 struct expression *expr = stmt->expression;
1923 struct symbol *ctype, *fntype;
1925 evaluate_expression(expr);
1926 ctype = degenerate(expr);
1927 fntype = current_fn->ctype.base_type;
1928 if (!fntype || fntype == &void_ctype) {
1929 if (expr && ctype != &void_ctype)
1930 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1931 return NULL;
1934 if (!expr) {
1935 warn(stmt->pos, "return with no return value");
1936 return NULL;
1938 if (!ctype)
1939 return NULL;
1940 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1941 return NULL;
1944 static void evaluate_if_statement(struct statement *stmt)
1946 struct symbol *ctype;
1948 if (!stmt->if_conditional)
1949 return;
1951 ctype = evaluate_conditional(&stmt->if_conditional);
1952 if (!ctype)
1953 return;
1955 evaluate_statement(stmt->if_true);
1956 evaluate_statement(stmt->if_false);
1959 struct symbol *evaluate_statement(struct statement *stmt)
1961 if (!stmt)
1962 return NULL;
1964 switch (stmt->type) {
1965 case STMT_RETURN:
1966 return evaluate_return_expression(stmt);
1968 case STMT_EXPRESSION:
1969 evaluate_expression(stmt->expression);
1970 return degenerate(stmt->expression);
1972 case STMT_COMPOUND: {
1973 struct statement *s;
1974 struct symbol *type = NULL;
1975 struct symbol *sym;
1977 /* Evaluate each symbol in the compound statement */
1978 FOR_EACH_PTR(stmt->syms, sym) {
1979 evaluate_symbol(sym);
1980 } END_FOR_EACH_PTR;
1981 evaluate_symbol(stmt->ret);
1984 * Then, evaluate each statement, making the type of the
1985 * compound statement be the type of the last statement
1987 type = NULL;
1988 FOR_EACH_PTR(stmt->stmts, s) {
1989 type = evaluate_statement(s);
1990 } END_FOR_EACH_PTR;
1991 if (!type)
1992 type = &void_ctype;
1993 return type;
1995 case STMT_IF:
1996 evaluate_if_statement(stmt);
1997 return NULL;
1998 case STMT_ITERATOR:
1999 evaluate_conditional(&stmt->iterator_pre_condition);
2000 evaluate_conditional(&stmt->iterator_post_condition);
2001 evaluate_statement(stmt->iterator_pre_statement);
2002 evaluate_statement(stmt->iterator_statement);
2003 evaluate_statement(stmt->iterator_post_statement);
2004 return NULL;
2005 case STMT_SWITCH:
2006 evaluate_expression(stmt->switch_expression);
2007 evaluate_statement(stmt->switch_statement);
2008 return NULL;
2009 case STMT_CASE:
2010 evaluate_expression(stmt->case_expression);
2011 evaluate_expression(stmt->case_to);
2012 evaluate_statement(stmt->case_statement);
2013 return NULL;
2014 case STMT_LABEL:
2015 return evaluate_statement(stmt->label_statement);
2016 case STMT_GOTO:
2017 evaluate_expression(stmt->goto_expression);
2018 return NULL;
2019 case STMT_NONE:
2020 break;
2021 case STMT_ASM:
2022 /* FIXME! Do the asm parameter evaluation! */
2023 break;
2025 return NULL;