[PATCH] parser.c cleanup
[smatch.git] / evaluate.c
blob42eb50e47b2c1419f6f996a44b195e1b1490d130
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 = &lazy_ptr_ctype; /* 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 = &lazy_ptr_ctype;
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_float_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return type->ctype.base_type == &fp_type;
204 static inline int is_byte_type(struct symbol *type)
206 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
209 static inline int is_string_type(struct symbol *type)
211 if (type->type == SYM_NODE)
212 type = type->ctype.base_type;
213 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
216 static struct symbol *bad_expr_type(struct expression *expr)
218 warn(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
219 switch (expr->type) {
220 case EXPR_BINOP:
221 case EXPR_COMPARE:
222 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
223 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
224 break;
225 case EXPR_PREOP:
226 case EXPR_POSTOP:
227 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
228 break;
229 default:
230 break;
233 return NULL;
236 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
238 struct expression *left = *lp, *right = *rp;
239 struct symbol *ltype = left->ctype, *rtype = right->ctype;
241 if (ltype->type == SYM_NODE)
242 ltype = ltype->ctype.base_type;
243 if (rtype->type == SYM_NODE)
244 rtype = rtype->ctype.base_type;
245 if (is_float_type(ltype)) {
246 if (is_int_type(rtype))
247 goto Left;
248 if (is_float_type(rtype)) {
249 unsigned long lmod = ltype->ctype.modifiers;
250 unsigned long rmod = rtype->ctype.modifiers;
251 lmod &= MOD_LONG | MOD_LONGLONG;
252 rmod &= MOD_LONG | MOD_LONGLONG;
253 if (lmod == rmod)
254 return ltype;
255 if (lmod & ~rmod)
256 goto Left;
257 else
258 goto Right;
260 return NULL;
262 if (!is_float_type(rtype) || !is_int_type(ltype))
263 return NULL;
264 Right:
265 *lp = cast_to(left, rtype);
266 return rtype;
267 Left:
268 *rp = cast_to(right, ltype);
269 return ltype;
272 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
274 struct expression *left = *lp, *right = *rp;
275 struct symbol *ltype = left->ctype, *rtype = right->ctype;
277 if (ltype->type == SYM_NODE)
278 ltype = ltype->ctype.base_type;
279 if (rtype->type == SYM_NODE)
280 rtype = rtype->ctype.base_type;
281 if (is_int_type(ltype) && is_int_type(rtype)) {
282 struct symbol *ctype = bigger_int_type(ltype, rtype);
284 /* Don't bother promoting same-size entities, it only adds clutter */
285 if (ltype->bit_size != ctype->bit_size)
286 *lp = cast_to(left, ctype);
287 if (rtype->bit_size != ctype->bit_size)
288 *rp = cast_to(right, ctype);
289 return ctype;
291 return NULL;
294 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
296 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
297 if (!ctype && float_ok)
298 ctype = compatible_float_binop(&expr->left, &expr->right);
299 if (ctype) {
300 expr->ctype = ctype;
301 return ctype;
303 return bad_expr_type(expr);
306 static inline int lvalue_expression(struct expression *expr)
308 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
311 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
313 struct symbol *ctype;
314 struct symbol *ptr_type = ptr->ctype;
315 int bit_size;
317 if (ptr_type->type == SYM_NODE)
318 ptr_type = ptr_type->ctype.base_type;
320 if (!is_int_type(i->ctype))
321 return bad_expr_type(expr);
323 ctype = ptr->ctype;
324 examine_symbol_type(ctype);
326 ctype = degenerate(ptr);
327 if (!ctype->ctype.base_type) {
328 warn(expr->pos, "missing type information");
329 return NULL;
332 /* Get the size of whatever the pointer points to */
333 ptr_type = ctype;
334 if (ptr_type->type == SYM_NODE)
335 ptr_type = ptr_type->ctype.base_type;
336 if (ptr_type->type == SYM_PTR)
337 ptr_type = ptr_type->ctype.base_type;
338 bit_size = ptr_type->bit_size;
340 /* Special case: adding zero commonly happens as a result of 'array[0]' */
341 if (i->type == EXPR_VALUE && !i->value) {
342 *expr = *ptr;
343 } else if (bit_size > bits_in_char) {
344 struct expression *add = expr;
345 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
346 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
348 val->ctype = size_t_ctype;
349 val->value = bit_size >> 3;
351 mul->op = '*';
352 mul->ctype = size_t_ctype;
353 mul->left = i;
354 mul->right = val;
356 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
357 add->left = ptr;
358 add->right = mul;
361 expr->ctype = ctype;
362 return ctype;
365 static struct symbol *evaluate_add(struct expression *expr)
367 struct expression *left = expr->left, *right = expr->right;
368 struct symbol *ltype = left->ctype, *rtype = right->ctype;
370 if (is_ptr_type(ltype))
371 return evaluate_ptr_add(expr, left, right);
373 if (is_ptr_type(rtype))
374 return evaluate_ptr_add(expr, right, left);
376 return evaluate_arith(expr, 1);
379 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
380 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
381 MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED | \
382 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED)
384 const char * type_difference(struct symbol *target, struct symbol *source,
385 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
387 for (;;) {
388 unsigned long mod1, mod2, diff;
389 unsigned long as1, as2;
390 int type1, type2;
391 struct symbol *base1, *base2;
393 if (target == source)
394 break;
395 if (!target || !source)
396 return "different types";
398 * Peel of per-node information.
399 * FIXME! Check alignment and context too here!
401 mod1 = target->ctype.modifiers;
402 as1 = target->ctype.as;
403 mod2 = source->ctype.modifiers;
404 as2 = source->ctype.as;
405 if (target->type == SYM_NODE) {
406 target = target->ctype.base_type;
407 if (!target)
408 return "bad types";
409 if (target->type == SYM_PTR) {
410 mod1 = 0;
411 as1 = 0;
413 mod1 |= target->ctype.modifiers;
414 as1 |= target->ctype.as;
416 if (source->type == SYM_NODE) {
417 source = source->ctype.base_type;
418 if (!source)
419 return "bad types";
420 if (source->type == SYM_PTR) {
421 mod2 = 0;
422 as2 = 0;
424 mod2 |= source->ctype.modifiers;
425 as2 |= source->ctype.as;
428 if (target == source)
429 break;
430 if (!target || !source)
431 return "different types";
433 type1 = target->type;
434 base1 = target->ctype.base_type;
436 type2 = source->type;
437 base2 = source->ctype.base_type;
440 * Pointers to functions compare as the function itself
442 if (type1 == SYM_PTR && base1) {
443 switch (base1->type) {
444 case SYM_FN:
445 type1 = SYM_FN;
446 target = base1;
447 base1 = base1->ctype.base_type;
448 default:
449 /* nothing */;
452 if (type2 == SYM_PTR && base2) {
453 switch (base2->type) {
454 case SYM_FN:
455 type2 = SYM_FN;
456 source = base2;
457 base2 = base2->ctype.base_type;
458 default:
459 /* nothing */;
463 /* Arrays degenerate to pointers for type comparisons */
464 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
465 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
467 if (type1 != type2)
468 return "different base types";
470 /* Must be same address space to be comparable */
471 if (as1 != as2)
472 return "different address spaces";
474 /* Ignore differences in storage types, sign, or addressability */
475 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
476 if (diff) {
477 mod1 &= diff & ~target_mod_ignore;
478 mod2 &= diff & ~source_mod_ignore;
479 if (mod1 | mod2) {
480 if ((mod1 | mod2) & MOD_SIZE)
481 return "different type sizes";
482 return "different modifiers";
486 if (type1 == SYM_FN) {
487 int i;
488 struct symbol *arg1, *arg2;
489 if (base1->variadic != base2->variadic)
490 return "incompatible variadic arguments";
491 PREPARE_PTR_LIST(target->arguments, arg1);
492 PREPARE_PTR_LIST(source->arguments, arg2);
493 i = 1;
494 for (;;) {
495 const char *diff;
496 diff = type_difference(arg1, arg2, 0, 0);
497 if (diff) {
498 static char argdiff[80];
499 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
500 return argdiff;
502 if (!arg1)
503 break;
504 NEXT_PTR_LIST(arg1);
505 NEXT_PTR_LIST(arg2);
506 i++;
508 FINISH_PTR_LIST(arg2);
509 FINISH_PTR_LIST(arg1);
512 target = base1;
513 source = base2;
515 return NULL;
518 static int is_null_ptr(struct expression *expr)
520 if (expr->type != EXPR_VALUE || expr->value)
521 return 0;
522 if (!is_ptr_type(expr->ctype))
523 warn(expr->pos, "Using plain integer as NULL pointer");
524 return 1;
527 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
529 /* NULL expression? Just return the type of the "other side" */
530 if (is_null_ptr(r))
531 return l->ctype;
532 if (is_null_ptr(l))
533 return r->ctype;
534 return NULL;
538 * Ignore differences in "volatile" and "const"ness when
539 * subtracting pointers
541 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
543 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
545 const char *typediff;
546 struct symbol *ctype;
547 struct symbol *ltype, *rtype;
549 ltype = degenerate(l);
550 rtype = degenerate(r);
553 * If it is an integer subtract: the ptr add case will do the
554 * right thing.
556 if (!is_ptr_type(rtype))
557 return evaluate_ptr_add(expr, l, r);
559 ctype = ltype;
560 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
561 if (typediff) {
562 ctype = common_ptr_type(l, r);
563 if (!ctype) {
564 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
565 return NULL;
568 examine_symbol_type(ctype);
570 /* Figure out the base type we point to */
571 if (ctype->type == SYM_NODE)
572 ctype = ctype->ctype.base_type;
573 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
574 warn(expr->pos, "subtraction of functions? Share your drugs");
575 return NULL;
577 ctype = ctype->ctype.base_type;
579 expr->ctype = ssize_t_ctype;
580 if (ctype->bit_size > bits_in_char) {
581 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
582 struct expression *div = expr;
583 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
585 val->ctype = size_t_ctype;
586 val->value = ctype->bit_size >> 3;
588 sub->op = '-';
589 sub->ctype = ssize_t_ctype;
590 sub->left = l;
591 sub->right = r;
593 div->op = '/';
594 div->left = sub;
595 div->right = val;
598 return ssize_t_ctype;
601 static struct symbol *evaluate_sub(struct expression *expr)
603 struct expression *left = expr->left, *right = expr->right;
604 struct symbol *ltype = left->ctype;
606 if (is_ptr_type(ltype))
607 return evaluate_ptr_sub(expr, left, right);
609 return evaluate_arith(expr, 1);
612 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
614 static struct symbol *evaluate_conditional(struct expression **p)
616 struct symbol *ctype;
617 struct expression *expr = *p;
619 if (!expr)
620 return NULL;
622 if (expr->type == EXPR_ASSIGNMENT)
623 warn(expr->pos, "assignment expression in conditional");
625 ctype = evaluate_expression(expr);
626 if (ctype) {
627 if (is_safe_type(ctype))
628 warn(expr->pos, "testing a 'safe expression'");
629 if (is_float_type(ctype)) {
630 struct expression *comp;
632 * It's easier to handle here, rather than deal with
633 * FP all over the place. Floating point in boolean
634 * context is rare enough (and very often wrong),
635 * so price of explicit comparison with appropriate
636 * FP zero is not too high. And it simplifies things
637 * elsewhere.
639 comp = alloc_expression(expr->pos, EXPR_BINOP);
640 comp->op = SPECIAL_NOTEQUAL;
641 comp->left = expr;
642 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
643 comp->right->ctype = comp->left->ctype;
644 comp->right->fvalue = 0;
645 ctype = comp->ctype = &bool_ctype;
646 *p = comp;
650 return ctype;
653 static struct symbol *evaluate_logical(struct expression *expr)
655 if (!evaluate_conditional(&expr->left))
656 return NULL;
657 if (!evaluate_conditional(&expr->right))
658 return NULL;
660 expr->ctype = &bool_ctype;
661 return &bool_ctype;
664 static struct symbol *evaluate_shift(struct expression *expr)
666 struct expression *left = expr->left, *right = expr->right;
667 struct symbol *ltype = left->ctype, *rtype = right->ctype;
669 if (ltype->type == SYM_NODE)
670 ltype = ltype->ctype.base_type;
671 if (rtype->type == SYM_NODE)
672 rtype = rtype->ctype.base_type;
673 if (is_int_type(ltype) && is_int_type(rtype)) {
674 struct symbol *ctype = integer_promotion(ltype);
675 if (ltype->bit_size != ctype->bit_size)
676 expr->left = cast_to(expr->left, ctype);
677 expr->ctype = ctype;
678 ctype = integer_promotion(rtype);
679 if (rtype->bit_size != ctype->bit_size)
680 expr->right = cast_to(expr->right, ctype);
681 return expr->ctype;
683 return bad_expr_type(expr);
686 static struct symbol *evaluate_binop(struct expression *expr)
688 switch (expr->op) {
689 // addition can take ptr+int, fp and int
690 case '+':
691 return evaluate_add(expr);
693 // subtraction can take ptr-ptr, fp and int
694 case '-':
695 return evaluate_sub(expr);
697 // Arithmetic operations can take fp and int
698 case '*': case '/':
699 return evaluate_arith(expr, 1);
701 // shifts do integer promotions, but that's it.
702 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
703 return evaluate_shift(expr);
705 // The rest are integer operations
706 // '%', '&', '^', '|'
707 default:
708 return evaluate_arith(expr, 0);
712 static struct symbol *evaluate_comma(struct expression *expr)
714 expr->ctype = expr->right->ctype;
715 return expr->ctype;
718 static int modify_for_unsigned(int op)
720 if (op == '<')
721 op = SPECIAL_UNSIGNED_LT;
722 else if (op == '>')
723 op = SPECIAL_UNSIGNED_GT;
724 else if (op == SPECIAL_LTE)
725 op = SPECIAL_UNSIGNED_LTE;
726 else if (op == SPECIAL_GTE)
727 op = SPECIAL_UNSIGNED_GTE;
728 return op;
731 static struct symbol *evaluate_compare(struct expression *expr)
733 struct expression *left = expr->left, *right = expr->right;
734 struct symbol *ltype = left->ctype, *rtype = right->ctype;
735 struct symbol *ctype;
737 /* Type types? */
738 if (is_type_type(ltype) && is_type_type(rtype)) {
739 expr->ctype = &bool_ctype;
740 return &bool_ctype;
743 if (is_safe_type(ltype) || is_safe_type(rtype))
744 warn(expr->pos, "testing a 'safe expression'");
746 /* Pointer types? */
747 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
748 expr->ctype = &bool_ctype;
749 // FIXME! Check the types for compatibility
750 return &bool_ctype;
753 ctype = compatible_integer_binop(&expr->left, &expr->right);
754 if (ctype) {
755 if (ctype->ctype.modifiers & MOD_UNSIGNED)
756 expr->op = modify_for_unsigned(expr->op);
757 expr->ctype = &bool_ctype;
758 return &bool_ctype;
760 ctype = compatible_float_binop(&expr->left, &expr->right);
761 if (ctype) {
762 expr->ctype = &bool_ctype;
763 return &bool_ctype;
766 return bad_expr_type(expr);
770 * FIXME!! This should do casts, array degeneration etc..
772 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
774 struct symbol *ltype = left->ctype, *rtype = right->ctype;
776 if (ltype->type == SYM_NODE)
777 ltype = ltype->ctype.base_type;
779 if (rtype->type == SYM_NODE)
780 rtype = rtype->ctype.base_type;
782 if (ltype->type == SYM_PTR) {
783 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
784 return ltype;
787 if (rtype->type == SYM_PTR) {
788 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
789 return rtype;
791 return NULL;
794 static struct symbol * evaluate_conditional_expression(struct expression *expr)
796 struct expression *cond, *true, *false;
797 struct symbol *ctype, *ltype, *rtype;
798 const char * typediff;
800 ctype = degenerate(expr->conditional);
801 cond = expr->conditional;
803 ltype = ctype;
804 true = cond;
805 if (expr->cond_true) {
806 ltype = degenerate(expr->cond_true);
807 true = expr->cond_true;
810 rtype = degenerate(expr->cond_false);
811 false = expr->cond_false;
813 ctype = ltype;
814 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
815 if (!typediff)
816 goto out;
818 ctype = compatible_integer_binop(&true, &expr->cond_false);
819 if (ctype)
820 goto out;
821 ctype = compatible_ptr_type(true, expr->cond_false);
822 if (ctype)
823 goto out;
824 ctype = compatible_float_binop(&true, &expr->cond_false);
825 if (ctype)
826 goto out;
827 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
828 return NULL;
830 out:
831 expr->ctype = ctype;
832 return ctype;
835 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
836 struct expression **rp, struct symbol *source, const char *where)
838 const char *typediff;
839 struct symbol *t;
840 int target_as;
842 /* It's ok if the target is more volatile or const than the source */
843 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
844 if (!typediff)
845 return 1;
847 if (is_int_type(target)) {
848 if (is_int_type(source)) {
849 if (target->bit_size != source->bit_size)
850 goto Cast;
851 return 1;
853 if (is_float_type(source))
854 goto Cast;
855 } else if (is_float_type(target)) {
856 if (is_int_type(source))
857 goto Cast;
858 if (is_float_type(source)) {
859 if (target->bit_size != source->bit_size)
860 goto Cast;
861 return 1;
865 /* Pointer destination? */
866 t = target;
867 target_as = t->ctype.as;
868 if (t->type == SYM_NODE) {
869 t = t->ctype.base_type;
870 target_as |= t->ctype.as;
872 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
873 struct expression *right = *rp;
874 struct symbol *s = source;
875 int source_as;
877 // NULL pointer is always ok
878 if (is_null_ptr(right))
879 return 1;
881 /* "void *" matches anything as long as the address space is ok */
882 source_as = s->ctype.as;
883 if (s->type == SYM_NODE) {
884 s = s->ctype.base_type;
885 source_as |= s->ctype.as;
887 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
888 s = s->ctype.base_type;
889 t = t->ctype.base_type;
890 if (s == &void_ctype || t == &void_ctype)
891 return 1;
895 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
896 info(expr->pos, " expected %s", show_typename(target));
897 info(expr->pos, " got %s", show_typename(source));
898 *rp = cast_to(*rp, target);
899 return 0;
900 Cast:
901 *rp = cast_to(*rp, target);
902 return 1;
906 * FIXME!! This is wrong from a double evaluation standpoint. We can't
907 * just expand the expression twice, that would make any side effects
908 * happen twice too.
910 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
912 int op = expr->op;
913 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
914 static const int op_trans[] = {
915 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
916 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
917 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
918 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
919 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
920 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
921 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
922 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
923 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
924 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
926 struct expression *e0, *e1, *e2, *e3, *e4, *e5;
927 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
928 struct symbol *ltype = left->ctype;
929 struct expression *addr;
930 struct symbol *lptype;
932 if (left->type == EXPR_BITFIELD)
933 addr = left->address;
934 else
935 addr = left->unop;
937 lptype = addr->ctype;
939 a->ctype.base_type = lptype;
940 a->bit_size = lptype->bit_size;
941 a->array_size = lptype->array_size;
943 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
944 e0->symbol = a;
945 e0->ctype = &lazy_ptr_ctype;
947 e1 = alloc_expression(expr->pos, EXPR_PREOP);
948 e1->unop = e0;
949 e1->op = '*';
950 e1->ctype = lptype;
952 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
953 e2->left = e1;
954 e2->right = addr;
955 e2->op = '=';
956 e2->ctype = lptype;
958 /* we can't cannibalize left, unfortunately */
959 e3 = alloc_expression(expr->pos, left->type);
960 *e3 = *left;
961 if (e3->type == EXPR_BITFIELD)
962 e3->address = e1;
963 else
964 e3->unop = e1;
966 e4 = alloc_expression(expr->pos, EXPR_BINOP);
967 e4->op = subexpr->op = op_trans[op - SPECIAL_BASE];
968 e4->left = e3;
969 e4->right = right;
970 /* will calculate type later */
972 e5 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
973 e5->left = e3; /* we can share that one */
974 e5->right = e4;
975 e5->op = '=';
976 e5->ctype = ltype;
978 expr->type = EXPR_COMMA;
979 expr->left = e2;
980 expr->right = e5;
981 expr->ctype = ltype;
983 return evaluate_binop(e4);
986 static void evaluate_assign_to(struct expression *left, struct symbol *type)
988 if (type->ctype.modifiers & MOD_CONST)
989 warn(left->pos, "assignment to const expression");
990 if (type->type == SYM_NODE)
991 type->ctype.modifiers |= MOD_ASSIGNED;
994 static struct symbol *evaluate_assignment(struct expression *expr)
996 struct expression *left = expr->left, *right = expr->right;
997 struct expression *where = expr;
998 struct symbol *ltype, *rtype;
1000 if (!lvalue_expression(left)) {
1001 warn(expr->pos, "not an lvalue");
1002 return NULL;
1005 ltype = left->ctype;
1007 if (expr->op != '=') {
1008 if (!evaluate_binop_assignment(expr, left, right))
1009 return NULL;
1010 where = expr->right; /* expr is EXPR_COMMA now */
1011 left = where->left;
1012 right = where->right;
1015 rtype = degenerate(right);
1017 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment"))
1018 return NULL;
1020 evaluate_assign_to(left, ltype);
1022 expr->ctype = ltype;
1023 return ltype;
1026 static void examine_fn_arguments(struct symbol *fn)
1028 struct symbol *s;
1030 FOR_EACH_PTR(fn->arguments, s) {
1031 struct symbol *arg = evaluate_symbol(s);
1032 /* Array/function arguments silently degenerate into pointers */
1033 if (arg) {
1034 struct symbol *ptr;
1035 switch(arg->type) {
1036 case SYM_ARRAY:
1037 case SYM_FN:
1038 ptr = alloc_symbol(s->pos, SYM_PTR);
1039 if (arg->type == SYM_ARRAY)
1040 ptr->ctype = arg->ctype;
1041 else
1042 ptr->ctype.base_type = arg;
1043 ptr->ctype.as |= s->ctype.as;
1044 ptr->ctype.modifiers |= s->ctype.modifiers;
1046 s->ctype.base_type = ptr;
1047 s->ctype.as = 0;
1048 s->ctype.modifiers = 0;
1049 examine_symbol_type(s);
1050 break;
1051 default:
1052 /* nothing */
1053 break;
1056 } END_FOR_EACH_PTR;
1059 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1061 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1062 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1063 *newsym = *sym;
1064 newsym->ctype.as = as;
1065 newsym->ctype.modifiers = mod;
1066 sym = newsym;
1068 return sym;
1071 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1073 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1074 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1076 node->ctype.base_type = ptr;
1077 ptr->bit_size = bits_in_pointer;
1078 ptr->ctype.alignment = pointer_alignment;
1080 node->bit_size = bits_in_pointer;
1081 node->ctype.alignment = pointer_alignment;
1083 access_symbol(sym);
1084 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1085 if (sym->ctype.modifiers & MOD_REGISTER) {
1086 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1087 sym->ctype.modifiers &= ~MOD_REGISTER;
1089 if (sym->type == SYM_NODE) {
1090 ptr->ctype.as |= sym->ctype.as;
1091 ptr->ctype.modifiers |= sym->ctype.modifiers;
1092 sym = sym->ctype.base_type;
1094 if (degenerate && sym->type == SYM_ARRAY) {
1095 ptr->ctype.as |= sym->ctype.as;
1096 ptr->ctype.modifiers |= sym->ctype.modifiers;
1097 sym = sym->ctype.base_type;
1099 ptr->ctype.base_type = sym;
1101 return node;
1104 /* Arrays degenerate into pointers on pointer arithmetic */
1105 static struct symbol *degenerate(struct expression *expr)
1107 struct symbol *ctype, *base;
1109 if (!expr)
1110 return NULL;
1111 ctype = expr->ctype;
1112 if (!ctype)
1113 return NULL;
1114 base = ctype;
1115 if (ctype->type == SYM_NODE)
1116 base = ctype->ctype.base_type;
1118 * Arrays degenerate into pointers to the entries, while
1119 * functions degenerate into pointers to themselves.
1120 * If array was part of non-lvalue compound, we create a copy
1121 * of that compound first and then act as if we were dealing with
1122 * the corresponding field in there.
1124 switch (base->type) {
1125 case SYM_ARRAY:
1126 if (expr->type == EXPR_SLICE) {
1127 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1128 struct expression *e0, *e1, *e2, *e3, *e4;
1130 a->ctype.base_type = expr->base->ctype;
1131 a->bit_size = expr->base->ctype->bit_size;
1132 a->array_size = expr->base->ctype->array_size;
1134 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1135 e0->symbol = a;
1136 e0->ctype = &lazy_ptr_ctype;
1138 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1139 e1->unop = e0;
1140 e1->op = '*';
1141 e1->ctype = expr->base->ctype; /* XXX */
1143 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1144 e2->left = e1;
1145 e2->right = expr->base;
1146 e2->op = '=';
1147 e2->ctype = expr->base->ctype;
1149 if (expr->r_bitpos) {
1150 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1151 e3->op = '+';
1152 e3->left = e0;
1153 e3->right = alloc_const_expression(expr->pos,
1154 expr->r_bitpos >> 3);
1155 e3->ctype = &lazy_ptr_ctype;
1156 } else {
1157 e3 = e0;
1160 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1161 e4->left = e2;
1162 e4->right = e3;
1163 e4->ctype = &lazy_ptr_ctype;
1165 expr->unop = e4;
1166 expr->type = EXPR_PREOP;
1167 expr->op = '*';
1169 case SYM_FN:
1170 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1171 warn(expr->pos, "strange non-value function or array");
1172 return NULL;
1174 *expr = *expr->unop;
1175 ctype = create_pointer(expr, ctype, 1);
1176 expr->ctype = ctype;
1177 default:
1178 /* nothing */;
1180 return ctype;
1183 static struct symbol *evaluate_addressof(struct expression *expr)
1185 struct expression *op = expr->unop;
1186 struct symbol *ctype;
1188 if (op->op != '*' || op->type != EXPR_PREOP) {
1189 warn(expr->pos, "not addressable");
1190 return NULL;
1192 ctype = op->ctype;
1193 *expr = *op->unop;
1196 * symbol expression evaluation is lazy about the type
1197 * of the sub-expression, so we may have to generate
1198 * the type here if so..
1200 if (expr->ctype == &lazy_ptr_ctype) {
1201 ctype = create_pointer(expr, ctype, 0);
1202 expr->ctype = ctype;
1204 return expr->ctype;
1208 static struct symbol *evaluate_dereference(struct expression *expr)
1210 struct expression *op = expr->unop;
1211 struct symbol *ctype = op->ctype, *node, *target;
1213 /* Simplify: *&(expr) => (expr) */
1214 if (op->type == EXPR_PREOP && op->op == '&') {
1215 *expr = *op->unop;
1216 return expr->ctype;
1219 /* Dereferencing a node drops all the node information. */
1220 if (ctype->type == SYM_NODE)
1221 ctype = ctype->ctype.base_type;
1223 node = alloc_symbol(expr->pos, SYM_NODE);
1224 target = ctype->ctype.base_type;
1226 switch (ctype->type) {
1227 default:
1228 warn(expr->pos, "cannot derefence this type");
1229 return NULL;
1230 case SYM_PTR:
1231 merge_type(node, ctype);
1232 if (ctype->type != SYM_ARRAY)
1233 break;
1235 * Dereferencing a pointer to an array results in a
1236 * degenerate dereference: the expression becomes
1237 * just a pointer to the entry, and the derefence
1238 * goes away.
1240 *expr = *op;
1242 target = alloc_symbol(expr->pos, SYM_PTR);
1243 target->bit_size = bits_in_pointer;
1244 target->ctype.alignment = pointer_alignment;
1245 merge_type(target, ctype->ctype.base_type);
1246 break;
1248 case SYM_ARRAY:
1249 if (!lvalue_expression(op)) {
1250 warn(op->pos, "non-lvalue array??");
1251 return NULL;
1254 /* Do the implied "addressof" on the array */
1255 *op = *op->unop;
1258 * When an array is dereferenced, we need to pick
1259 * up the attributes of the original node too..
1261 merge_type(node, op->ctype);
1262 merge_type(node, ctype);
1263 break;
1266 node->bit_size = target->bit_size;
1267 node->array_size = target->array_size;
1269 expr->ctype = node;
1270 return node;
1274 * Unary post-ops: x++ and x--
1276 static struct symbol *evaluate_postop(struct expression *expr)
1278 struct expression *op = expr->unop;
1279 struct symbol *ctype = op->ctype;
1281 if (!lvalue_expression(expr->unop)) {
1282 warn(expr->pos, "need lvalue expression for ++/--");
1283 return NULL;
1286 evaluate_assign_to(op, ctype);
1288 expr->ctype = ctype;
1289 return ctype;
1292 static struct symbol *evaluate_sign(struct expression *expr)
1294 struct symbol *ctype = expr->unop->ctype;
1295 if (is_int_type(ctype)) {
1296 struct symbol *rtype = rtype = integer_promotion(ctype);
1297 if (rtype->bit_size != ctype->bit_size)
1298 expr->unop = cast_to(expr->unop, rtype);
1299 ctype = rtype;
1300 } else if (is_float_type(ctype) && expr->op != '~') {
1301 /* no conversions needed */
1302 } else {
1303 return bad_expr_type(expr);
1305 if (expr->op == '+')
1306 *expr = *expr->unop;
1307 expr->ctype = ctype;
1308 return ctype;
1311 static struct symbol *evaluate_preop(struct expression *expr)
1313 struct symbol *ctype = expr->unop->ctype;
1315 switch (expr->op) {
1316 case '(':
1317 *expr = *expr->unop;
1318 return ctype;
1320 case '+':
1321 case '-':
1322 case '~':
1323 return evaluate_sign(expr);
1325 case '*':
1326 return evaluate_dereference(expr);
1328 case '&':
1329 return evaluate_addressof(expr);
1331 case SPECIAL_INCREMENT:
1332 case SPECIAL_DECREMENT:
1334 * From a type evaluation standpoint the pre-ops are
1335 * the same as the postops
1337 return evaluate_postop(expr);
1339 case '!':
1340 if (is_safe_type(ctype))
1341 warn(expr->pos, "testing a 'safe expression'");
1342 if (is_float_type(ctype)) {
1343 struct expression *arg = expr->unop;
1344 expr->type = EXPR_BINOP;
1345 expr->op = SPECIAL_EQUAL;
1346 expr->left = arg;
1347 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1348 expr->right->ctype = ctype;
1349 expr->right->fvalue = 0;
1351 ctype = &bool_ctype;
1352 break;
1354 default:
1355 break;
1357 expr->ctype = ctype;
1358 return &bool_ctype;
1361 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1363 struct ptr_list *head = (struct ptr_list *)_list;
1364 struct ptr_list *list = head;
1366 if (!head)
1367 return NULL;
1368 do {
1369 int i;
1370 for (i = 0; i < list->nr; i++) {
1371 struct symbol *sym = (struct symbol *) list->list[i];
1372 if (sym->ident) {
1373 if (sym->ident != ident)
1374 continue;
1375 *offset = sym->offset;
1376 return sym;
1377 } else {
1378 struct symbol *ctype = sym->ctype.base_type;
1379 struct symbol *sub;
1380 if (!ctype)
1381 continue;
1382 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1383 continue;
1384 sub = find_identifier(ident, ctype->symbol_list, offset);
1385 if (!sub)
1386 continue;
1387 *offset += sym->offset;
1388 return sub;
1391 } while ((list = list->next) != head);
1392 return NULL;
1395 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1397 struct expression *add;
1400 * Create a new add-expression
1402 * NOTE! Even if we just add zero, we need a new node
1403 * for the member pointer, since it has a different
1404 * type than the original pointer. We could make that
1405 * be just a cast, but the fact is, a node is a node,
1406 * so we might as well just do the "add zero" here.
1408 add = alloc_expression(expr->pos, EXPR_BINOP);
1409 add->op = '+';
1410 add->left = expr;
1411 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1412 add->right->ctype = &int_ctype;
1413 add->right->value = offset;
1416 * The ctype of the pointer will be lazily evaluated if
1417 * we ever take the address of this member dereference..
1419 add->ctype = &lazy_ptr_ctype;
1420 return add;
1423 /* structure/union dereference */
1424 static struct symbol *evaluate_member_dereference(struct expression *expr)
1426 int offset;
1427 struct symbol *ctype, *member;
1428 struct expression *deref = expr->deref, *add;
1429 struct ident *ident = expr->member;
1430 unsigned int mod;
1431 int address_space;
1433 if (!evaluate_expression(deref))
1434 return NULL;
1435 if (!ident) {
1436 warn(expr->pos, "bad member name");
1437 return NULL;
1440 ctype = deref->ctype;
1441 address_space = ctype->ctype.as;
1442 mod = ctype->ctype.modifiers;
1443 if (ctype->type == SYM_NODE) {
1444 ctype = ctype->ctype.base_type;
1445 address_space |= ctype->ctype.as;
1446 mod |= ctype->ctype.modifiers;
1448 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1449 warn(expr->pos, "expected structure or union");
1450 return NULL;
1452 offset = 0;
1453 member = find_identifier(ident, ctype->symbol_list, &offset);
1454 if (!member) {
1455 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1456 const char *name = "<unnamed>";
1457 int namelen = 9;
1458 if (ctype->ident) {
1459 name = ctype->ident->name;
1460 namelen = ctype->ident->len;
1462 warn(expr->pos, "no member '%s' in %s %.*s",
1463 show_ident(ident), type, namelen, name);
1464 return NULL;
1468 * The member needs to take on the address space and modifiers of
1469 * the "parent" type.
1471 member = convert_to_as_mod(member, address_space, mod);
1472 ctype = member->ctype.base_type;
1474 if (!lvalue_expression(deref)) {
1475 if (deref->type != EXPR_SLICE) {
1476 expr->base = deref;
1477 expr->r_bitpos = 0;
1478 } else {
1479 expr->base = deref->base;
1480 expr->r_bitpos = deref->r_bitpos;
1482 expr->r_bitpos += offset << 3;
1483 expr->type = EXPR_SLICE;
1484 if (ctype->type == SYM_BITFIELD) {
1485 expr->r_bitpos += member->bit_offset;
1486 expr->r_nrbits = member->fieldwidth;
1487 } else {
1488 expr->r_nrbits = member->bit_size;
1490 expr->ctype = member;
1491 return member;
1494 deref = deref->unop;
1495 expr->deref = deref;
1497 add = evaluate_offset(deref, offset);
1498 if (ctype->type == SYM_BITFIELD) {
1499 expr->type = EXPR_BITFIELD;
1500 expr->bitpos = member->bit_offset;
1501 expr->nrbits = member->fieldwidth;
1502 expr->address = add;
1503 } else {
1504 expr->type = EXPR_PREOP;
1505 expr->op = '*';
1506 expr->unop = add;
1509 expr->ctype = member;
1510 return member;
1513 static struct symbol *evaluate_cast(struct expression *);
1515 static struct symbol *evaluate_sizeof(struct expression *expr)
1517 int size;
1519 if (expr->cast_type) {
1520 if (expr->cast_expression) {
1521 struct symbol *sym = evaluate_cast(expr);
1522 size = sym->bit_size;
1523 } else {
1524 examine_symbol_type(expr->cast_type);
1525 size = expr->cast_type->bit_size;
1527 } else {
1528 if (!evaluate_expression(expr->cast_expression))
1529 return NULL;
1530 size = expr->cast_expression->ctype->bit_size;
1531 if (is_bitfield_type (expr->cast_expression->ctype))
1532 warn(expr->pos, "sizeof applied to bitfield type");
1534 if (size & 7)
1535 warn(expr->pos, "cannot size expression");
1536 expr->type = EXPR_VALUE;
1537 expr->value = size >> 3;
1538 expr->ctype = size_t_ctype;
1539 return size_t_ctype;
1542 static struct symbol *evaluate_alignof(struct expression *expr)
1544 struct symbol *type = expr->cast_type;
1546 if (!type) {
1547 type = evaluate_expression(expr->cast_expression);
1548 if (!type)
1549 return NULL;
1551 if (is_bitfield_type(type))
1552 warn(expr->pos, "alignof applied to bitfield type");
1553 examine_symbol_type(type);
1554 expr->type = EXPR_VALUE;
1555 expr->value = type->ctype.alignment;
1556 expr->ctype = size_t_ctype;
1557 return size_t_ctype;
1560 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1562 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1563 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1564 return clash != 0;
1567 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1569 struct expression *expr;
1570 struct symbol_list *argument_types = fn->arguments;
1571 struct symbol *argtype;
1572 int i = 1;
1574 PREPARE_PTR_LIST(argument_types, argtype);
1575 FOR_EACH_PTR (head, expr) {
1576 struct expression **p = THIS_ADDRESS(expr);
1577 struct symbol *ctype, *target;
1578 ctype = evaluate_expression(expr);
1580 if (!ctype)
1581 return 0;
1583 if (context_clash(f, ctype))
1584 warn(expr->pos, "argument %d used in wrong context", i);
1586 ctype = degenerate(expr);
1588 target = argtype;
1589 if (!target && ctype->bit_size < bits_in_int)
1590 target = &int_ctype;
1591 if (target) {
1592 static char where[30];
1593 examine_symbol_type(target);
1594 sprintf(where, "argument %d", i);
1595 compatible_assignment_types(expr, target, p, ctype, where);
1598 i++;
1599 NEXT_PTR_LIST(argtype);
1600 } END_FOR_EACH_PTR;
1601 FINISH_PTR_LIST(argtype);
1602 return 1;
1605 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1606 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1608 struct expression *entry;
1609 int current = 0;
1610 int max = 0;
1611 int accept_string = is_byte_type(ctype);
1613 FOR_EACH_PTR(expr->expr_list, entry) {
1614 struct expression **p = THIS_ADDRESS(entry);
1615 struct symbol *sym;
1616 int entries;
1618 if (entry->type == EXPR_INDEX) {
1619 current = entry->idx_to;
1620 continue;
1622 if (accept_string && entry->type == EXPR_STRING) {
1623 sym = evaluate_expression(entry);
1624 entries = get_expression_value(sym->array_size);
1625 } else {
1626 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1627 entries = 1;
1629 current += entries;
1630 if (current > max)
1631 max = current;
1632 } END_FOR_EACH_PTR;
1633 return max;
1636 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1637 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1639 if (offset || expression_list_size(expr->expr_list) != 1) {
1640 warn(expr->pos, "unexpected compound initializer");
1641 return 0;
1643 return evaluate_array_initializer(ctype, expr, 0);
1646 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1648 struct expression *entry;
1649 struct symbol *sym;
1651 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1652 FOR_EACH_PTR(expr->expr_list, entry) {
1653 struct expression **p = THIS_ADDRESS(entry);
1655 if (entry->type == EXPR_IDENTIFIER) {
1656 struct ident *ident = entry->expr_ident;
1657 /* We special-case the "already right place" case */
1658 if (sym && sym->ident == ident)
1659 continue;
1660 RESET_PTR_LIST(sym);
1661 for (;;) {
1662 if (!sym) {
1663 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1664 return 0;
1666 if (sym->ident == ident)
1667 break;
1668 NEXT_PTR_LIST(sym);
1670 continue;
1673 if (!sym) {
1674 warn(expr->pos, "too many initializers for struct/union");
1675 return 0;
1678 evaluate_initializer(sym, p, offset + sym->offset);
1680 NEXT_PTR_LIST(sym);
1681 } END_FOR_EACH_PTR;
1682 FINISH_PTR_LIST(sym);
1684 return 0;
1688 * Initializers are kind of like assignments. Except
1689 * they can be a hell of a lot more complex.
1691 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1693 struct expression *expr = *ep;
1696 * Simple non-structure/array initializers are the simple
1697 * case, and look (and parse) largely like assignments.
1699 if (expr->type != EXPR_INITIALIZER) {
1700 int size = 0, is_string = expr->type == EXPR_STRING;
1701 struct symbol *rtype = evaluate_expression(expr);
1702 if (rtype) {
1703 struct expression *pos;
1706 * Special case:
1707 * char array[] = "string"
1708 * should _not_ degenerate.
1710 if (is_string && is_string_type(ctype)) {
1711 struct expression *array_size = ctype->array_size;
1712 if (!array_size)
1713 array_size = ctype->array_size = rtype->array_size;
1714 size = get_expression_value(array_size);
1715 } else {
1716 rtype = degenerate(expr);
1717 size = 1;
1719 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1722 * Don't bother creating a position expression for
1723 * the simple initializer cases that don't need it.
1725 * We need a position if the initializer has a byte
1726 * offset, _or_ if we're initializing a bitfield.
1728 if (offset || ctype->fieldwidth) {
1729 pos = alloc_expression(expr->pos, EXPR_POS);
1730 pos->init_offset = offset;
1731 pos->init_sym = ctype;
1732 pos->init_expr = *ep;
1733 pos->ctype = expr->ctype;
1734 *ep = pos;
1737 return size;
1740 expr->ctype = ctype;
1741 if (ctype->type == SYM_NODE)
1742 ctype = ctype->ctype.base_type;
1744 switch (ctype->type) {
1745 case SYM_ARRAY:
1746 case SYM_PTR:
1747 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1748 case SYM_UNION:
1749 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1750 case SYM_STRUCT:
1751 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1752 default:
1753 return evaluate_scalar_initializer(ctype, expr, offset);
1757 static int get_as(struct symbol *sym)
1759 int as;
1760 unsigned long mod;
1762 if (!sym)
1763 return 0;
1764 as = sym->ctype.as;
1765 mod = sym->ctype.modifiers;
1766 if (sym->type == SYM_NODE) {
1767 sym = sym->ctype.base_type;
1768 as |= sym->ctype.as;
1769 mod |= sym->ctype.modifiers;
1773 * At least for now, allow casting to a "unsigned long".
1774 * That's how we do things like pointer arithmetic and
1775 * store pointers to registers.
1777 if (sym == &ulong_ctype)
1778 return -1;
1780 if (sym && sym->type == SYM_PTR) {
1781 sym = sym->ctype.base_type;
1782 as |= sym->ctype.as;
1783 mod |= sym->ctype.modifiers;
1785 if (mod & MOD_FORCE)
1786 return -1;
1787 return as;
1790 static struct symbol *evaluate_cast(struct expression *expr)
1792 struct expression *target = expr->cast_expression;
1793 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1794 enum type type;
1796 expr->ctype = ctype;
1797 expr->cast_type = ctype;
1800 * Special case: a cast can be followed by an
1801 * initializer, in which case we need to pass
1802 * the type value down to that initializer rather
1803 * than trying to evaluate it as an expression
1805 * A more complex case is when the initializer is
1806 * dereferenced as part of a post-fix expression.
1807 * We need to produce an expression that can be dereferenced.
1809 if (target->type == EXPR_INITIALIZER) {
1810 struct symbol *sym = expr->cast_type;
1811 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1813 sym->initializer = expr->cast_expression;
1814 evaluate_symbol(sym);
1816 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
1817 addr->symbol = sym;
1819 expr->type = EXPR_PREOP;
1820 expr->op = '*';
1821 expr->unop = addr;
1822 expr->ctype = sym;
1824 return sym;
1827 evaluate_expression(target);
1828 degenerate(target);
1831 * You can always throw a value away by casting to
1832 * "void" - that's an implicit "force". Note that
1833 * the same is _not_ true of "void *".
1835 if (ctype == &void_ctype)
1836 goto out;
1838 type = ctype->type;
1839 if (type == SYM_NODE) {
1840 type = ctype->ctype.base_type->type;
1841 if (ctype->ctype.base_type == &void_ctype)
1842 goto out;
1844 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
1845 warn(expr->pos, "cast to non-scalar");
1847 if (!target->ctype) {
1848 warn(expr->pos, "cast from unknown type");
1849 goto out;
1852 type = target->ctype->type;
1853 if (type == SYM_NODE)
1854 type = target->ctype->ctype.base_type->type;
1855 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
1856 warn(expr->pos, "cast from non-scalar");
1858 if (!get_as(ctype) && get_as(target->ctype) > 0)
1859 warn(expr->pos, "cast removes address space of expression");
1862 * Casts of constant values are special: they
1863 * can be NULL, and thus need to be simplified
1864 * early.
1866 if (target->type == EXPR_VALUE)
1867 cast_value(expr, ctype, target, target->ctype);
1869 out:
1870 return ctype;
1874 * Evaluate a call expression with a symbol. This
1875 * should expand inline functions, and evaluate
1876 * builtins.
1878 static int evaluate_symbol_call(struct expression *expr)
1880 struct expression *fn = expr->fn;
1881 struct symbol *ctype = fn->ctype;
1883 if (fn->type != EXPR_PREOP)
1884 return 0;
1886 if (ctype->op && ctype->op->evaluate)
1887 return ctype->op->evaluate(expr);
1889 if (ctype->ctype.modifiers & MOD_INLINE) {
1890 int ret;
1891 struct symbol *curr = current_fn;
1892 unsigned long context = current_context;
1893 unsigned long mask = current_contextmask;
1895 current_context |= ctype->ctype.context;
1896 current_contextmask |= ctype->ctype.contextmask;
1897 current_fn = ctype->ctype.base_type;
1898 examine_fn_arguments(current_fn);
1900 ret = inline_function(expr, ctype);
1902 /* restore the old function context */
1903 current_fn = curr;
1904 current_context = context;
1905 current_contextmask = mask;
1906 return ret;
1909 return 0;
1912 static struct symbol *evaluate_call(struct expression *expr)
1914 int args, fnargs;
1915 struct symbol *ctype, *sym;
1916 struct expression *fn = expr->fn;
1917 struct expression_list *arglist = expr->args;
1919 if (!evaluate_expression(fn))
1920 return NULL;
1921 sym = ctype = fn->ctype;
1922 if (ctype->type == SYM_NODE)
1923 ctype = ctype->ctype.base_type;
1924 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1925 ctype = ctype->ctype.base_type;
1926 if (!evaluate_arguments(sym, ctype, arglist))
1927 return NULL;
1928 if (ctype->type != SYM_FN) {
1929 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1930 return NULL;
1932 args = expression_list_size(expr->args);
1933 fnargs = symbol_list_size(ctype->arguments);
1934 if (args < fnargs)
1935 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1936 if (args > fnargs && !ctype->variadic)
1937 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1938 if (sym->type == SYM_NODE) {
1939 if (evaluate_symbol_call(expr))
1940 return expr->ctype;
1942 expr->ctype = ctype->ctype.base_type;
1943 return expr->ctype;
1946 struct symbol *evaluate_expression(struct expression *expr)
1948 if (!expr)
1949 return NULL;
1950 if (expr->ctype)
1951 return expr->ctype;
1953 switch (expr->type) {
1954 case EXPR_VALUE:
1955 case EXPR_FVALUE:
1956 warn(expr->pos, "value expression without a type");
1957 return NULL;
1958 case EXPR_STRING:
1959 return evaluate_string(expr);
1960 case EXPR_SYMBOL:
1961 return evaluate_symbol_expression(expr);
1962 case EXPR_BINOP:
1963 if (!evaluate_expression(expr->left))
1964 return NULL;
1965 if (!evaluate_expression(expr->right))
1966 return NULL;
1967 return evaluate_binop(expr);
1968 case EXPR_LOGICAL:
1969 return evaluate_logical(expr);
1970 case EXPR_COMMA:
1971 evaluate_expression(expr->left);
1972 if (!evaluate_expression(expr->right))
1973 return NULL;
1974 return evaluate_comma(expr);
1975 case EXPR_COMPARE:
1976 if (!evaluate_expression(expr->left))
1977 return NULL;
1978 if (!evaluate_expression(expr->right))
1979 return NULL;
1980 return evaluate_compare(expr);
1981 case EXPR_ASSIGNMENT:
1982 if (!evaluate_expression(expr->left))
1983 return NULL;
1984 if (!evaluate_expression(expr->right))
1985 return NULL;
1986 return evaluate_assignment(expr);
1987 case EXPR_PREOP:
1988 if (!evaluate_expression(expr->unop))
1989 return NULL;
1990 return evaluate_preop(expr);
1991 case EXPR_POSTOP:
1992 if (!evaluate_expression(expr->unop))
1993 return NULL;
1994 return evaluate_postop(expr);
1995 case EXPR_CAST:
1996 return evaluate_cast(expr);
1997 case EXPR_SIZEOF:
1998 return evaluate_sizeof(expr);
1999 case EXPR_ALIGNOF:
2000 return evaluate_alignof(expr);
2001 case EXPR_DEREF:
2002 return evaluate_member_dereference(expr);
2003 case EXPR_CALL:
2004 return evaluate_call(expr);
2005 case EXPR_BITFIELD:
2006 warn(expr->pos, "bitfield generated by parser");
2007 return NULL;
2008 case EXPR_SELECT:
2009 case EXPR_CONDITIONAL:
2010 if (!evaluate_conditional(&expr->conditional))
2011 return NULL;
2012 if (!evaluate_expression(expr->cond_false))
2013 return NULL;
2014 if (expr->cond_true && !evaluate_expression(expr->cond_true))
2015 return NULL;
2016 return evaluate_conditional_expression(expr);
2017 case EXPR_STATEMENT:
2018 expr->ctype = evaluate_statement(expr->statement);
2019 return expr->ctype;
2021 case EXPR_LABEL:
2022 expr->ctype = &ptr_ctype;
2023 return &ptr_ctype;
2025 case EXPR_TYPE:
2026 /* Evaluate the type of the symbol .. */
2027 evaluate_symbol(expr->symbol);
2028 /* .. but the type of the _expression_ is a "type" */
2029 expr->ctype = &type_ctype;
2030 return &type_ctype;
2032 /* These can not exist as stand-alone expressions */
2033 case EXPR_INITIALIZER:
2034 case EXPR_IDENTIFIER:
2035 case EXPR_INDEX:
2036 case EXPR_POS:
2037 warn(expr->pos, "internal front-end error: initializer in expression");
2038 return NULL;
2039 case EXPR_SLICE:
2040 warn(expr->pos, "internal front-end error: SLICE re-evaluated");
2041 return NULL;
2043 return NULL;
2046 void check_duplicates(struct symbol *sym)
2048 struct symbol *next = sym;
2050 while ((next = next->same_symbol) != NULL) {
2051 const char *typediff;
2052 evaluate_symbol(next);
2053 typediff = type_difference(sym, next, 0, 0);
2054 if (typediff) {
2055 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2056 show_ident(sym->ident),
2057 input_streams[next->pos.stream].name, next->pos.line, typediff);
2058 return;
2063 struct symbol *evaluate_symbol(struct symbol *sym)
2065 struct symbol *base_type;
2067 if (!sym)
2068 return sym;
2070 sym = examine_symbol_type(sym);
2071 base_type = sym->ctype.base_type;
2072 if (!base_type)
2073 return NULL;
2075 /* Evaluate the initializers */
2076 if (sym->initializer) {
2077 int count = evaluate_initializer(sym, &sym->initializer, 0);
2078 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
2079 int bit_size = count * base_type->ctype.base_type->bit_size;
2080 base_type->array_size = alloc_const_expression(sym->pos, count);
2081 base_type->bit_size = bit_size;
2082 sym->array_size = base_type->array_size;
2083 sym->bit_size = bit_size;
2087 /* And finally, evaluate the body of the symbol too */
2088 if (base_type->type == SYM_FN) {
2089 struct symbol *curr = current_fn;
2090 unsigned long context = current_context;
2091 unsigned long mask = current_contextmask;
2093 current_fn = base_type;
2094 current_contextmask = sym->ctype.contextmask;
2095 current_context = sym->ctype.context;
2097 examine_fn_arguments(base_type);
2098 if (!base_type->stmt && base_type->inline_stmt)
2099 uninline(sym);
2100 if (base_type->stmt)
2101 evaluate_statement(base_type->stmt);
2103 current_fn = curr;
2104 current_contextmask = mask;
2105 current_context = context;
2108 return base_type;
2111 struct symbol *evaluate_return_expression(struct statement *stmt)
2113 struct expression *expr = stmt->expression;
2114 struct symbol *ctype, *fntype;
2116 evaluate_expression(expr);
2117 ctype = degenerate(expr);
2118 fntype = current_fn->ctype.base_type;
2119 if (!fntype || fntype == &void_ctype) {
2120 if (expr && ctype != &void_ctype)
2121 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2122 return NULL;
2125 if (!expr) {
2126 warn(stmt->pos, "return with no return value");
2127 return NULL;
2129 if (!ctype)
2130 return NULL;
2131 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
2132 return NULL;
2135 static void evaluate_if_statement(struct statement *stmt)
2137 if (!stmt->if_conditional)
2138 return;
2140 evaluate_conditional(&stmt->if_conditional);
2141 evaluate_statement(stmt->if_true);
2142 evaluate_statement(stmt->if_false);
2145 static void evaluate_iterator(struct statement *stmt)
2147 struct expression **pre = &stmt->iterator_pre_condition;
2148 struct expression **post = &stmt->iterator_post_condition;
2149 if (*pre == *post) {
2150 evaluate_conditional(pre);
2151 *post = *pre;
2152 } else {
2153 evaluate_conditional(pre);
2154 evaluate_conditional(post);
2156 evaluate_statement(stmt->iterator_pre_statement);
2157 evaluate_statement(stmt->iterator_statement);
2158 evaluate_statement(stmt->iterator_post_statement);
2161 struct symbol *evaluate_statement(struct statement *stmt)
2163 if (!stmt)
2164 return NULL;
2166 switch (stmt->type) {
2167 case STMT_RETURN:
2168 return evaluate_return_expression(stmt);
2170 case STMT_EXPRESSION:
2171 if (!evaluate_expression(stmt->expression))
2172 return NULL;
2173 return degenerate(stmt->expression);
2175 case STMT_COMPOUND: {
2176 struct statement *s;
2177 struct symbol *type = NULL;
2178 struct symbol *sym;
2180 /* Evaluate each symbol in the compound statement */
2181 FOR_EACH_PTR(stmt->syms, sym) {
2182 evaluate_symbol(sym);
2183 } END_FOR_EACH_PTR;
2184 evaluate_symbol(stmt->ret);
2187 * Then, evaluate each statement, making the type of the
2188 * compound statement be the type of the last statement
2190 type = NULL;
2191 FOR_EACH_PTR(stmt->stmts, s) {
2192 type = evaluate_statement(s);
2193 } END_FOR_EACH_PTR;
2194 if (!type)
2195 type = &void_ctype;
2196 return type;
2198 case STMT_IF:
2199 evaluate_if_statement(stmt);
2200 return NULL;
2201 case STMT_ITERATOR:
2202 evaluate_iterator(stmt);
2203 return NULL;
2204 case STMT_SWITCH:
2205 evaluate_expression(stmt->switch_expression);
2206 evaluate_statement(stmt->switch_statement);
2207 return NULL;
2208 case STMT_CASE:
2209 evaluate_expression(stmt->case_expression);
2210 evaluate_expression(stmt->case_to);
2211 evaluate_statement(stmt->case_statement);
2212 return NULL;
2213 case STMT_LABEL:
2214 return evaluate_statement(stmt->label_statement);
2215 case STMT_GOTO:
2216 evaluate_expression(stmt->goto_expression);
2217 return NULL;
2218 case STMT_NONE:
2219 break;
2220 case STMT_ASM:
2221 /* FIXME! Do the asm parameter evaluation! */
2222 break;
2224 return NULL;