Add "argument pseudo" for incoming arguments to a function.
[smatch.git] / evaluate.c
blob24aa4992b4ba2d3f3b29493ff0575121ea5adc4b
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 struct symbol *current_fn;
30 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol_expression(struct expression *expr)
34 struct symbol *sym = expr->symbol;
35 struct symbol *base_type;
37 if (!sym) {
38 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
39 return NULL;
42 examine_symbol_type(sym);
44 base_type = sym->ctype.base_type;
45 if (!base_type) {
46 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
47 return NULL;
50 /* The type of a symbol is the symbol itself! */
51 expr->ctype = sym;
53 /* enums can be turned into plain values */
54 if (sym->type != SYM_ENUM) {
55 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
56 addr->symbol = sym;
57 addr->symbol_name = expr->symbol_name;
58 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
59 expr->type = EXPR_PREOP;
60 expr->op = '*';
61 expr->unop = addr;
62 return sym;
63 } else if (base_type->bit_size < bits_in_int) {
64 /* ugly - we need to force sizeof for these guys */
65 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
66 e->value = sym->value;
67 e->ctype = base_type;
68 expr->type = EXPR_PREOP;
69 expr->op = '+';
70 expr->unop = e;
71 } else {
72 expr->type = EXPR_VALUE;
73 expr->value = sym->value;
75 expr->ctype = base_type;
76 return base_type;
79 static struct symbol *evaluate_string(struct expression *expr)
81 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
82 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
83 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
84 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
85 unsigned int length = expr->string->length;
87 sym->array_size = alloc_const_expression(expr->pos, length);
88 sym->bit_size = bits_in_char * length;
89 sym->ctype.alignment = 1;
90 sym->ctype.modifiers = MOD_STATIC;
91 sym->ctype.base_type = array;
92 sym->initializer = initstr;
94 initstr->ctype = sym;
95 initstr->string = expr->string;
97 array->array_size = sym->array_size;
98 array->bit_size = bits_in_char * length;
99 array->ctype.alignment = 1;
100 array->ctype.modifiers = MOD_STATIC;
101 array->ctype.base_type = &char_ctype;
103 addr->symbol = sym;
104 addr->ctype = &lazy_ptr_ctype;
106 expr->type = EXPR_PREOP;
107 expr->op = '*';
108 expr->unop = addr;
109 expr->ctype = sym;
110 return sym;
113 static inline struct symbol *integer_promotion(struct symbol *type)
115 unsigned long mod = type->ctype.modifiers;
116 int width;
118 if (type->type == SYM_NODE)
119 type = type->ctype.base_type;
120 if (type->type == SYM_ENUM)
121 type = type->ctype.base_type;
122 width = type->bit_size;
123 if (type->type == SYM_BITFIELD)
124 type = type->ctype.base_type;
125 mod = type->ctype.modifiers;
126 if (width < bits_in_int)
127 return &int_ctype;
129 /* If char/short has as many bits as int, it still gets "promoted" */
130 if (mod & (MOD_CHAR | MOD_SHORT)) {
131 type = &int_ctype;
132 if (mod & MOD_UNSIGNED)
133 type = &uint_ctype;
135 return type;
139 * integer part of usual arithmetic conversions:
140 * integer promotions are applied
141 * if left and right are identical, we are done
142 * if signedness is the same, convert one with lower rank
143 * unless unsigned argument has rank lower than signed one, convert the
144 * signed one.
145 * if signed argument is bigger than unsigned one, convert the unsigned.
146 * otherwise, convert signed.
148 * Leaving aside the integer promotions, that is equivalent to
149 * if identical, don't convert
150 * if left is bigger than right, convert right
151 * if right is bigger than left, convert right
152 * otherwise, if signedness is the same, convert one with lower rank
153 * otherwise convert the signed one.
155 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
157 unsigned long lmod, rmod;
159 left = integer_promotion(left);
160 right = integer_promotion(right);
162 if (left == right)
163 goto left;
165 if (left->bit_size > right->bit_size)
166 goto left;
168 if (right->bit_size > left->bit_size)
169 goto right;
171 lmod = left->ctype.modifiers;
172 rmod = right->ctype.modifiers;
173 if ((lmod ^ rmod) & MOD_UNSIGNED) {
174 if (lmod & MOD_UNSIGNED)
175 goto left;
176 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
177 goto left;
178 right:
179 left = right;
180 left:
181 return left;
184 static int same_cast_type(struct symbol *orig, struct symbol *new)
186 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
190 * This gets called for implicit casts in assignments and
191 * integer promotion. We often want to try to move the
192 * cast down, because the ops involved may have been
193 * implicitly cast up, and we can get rid of the casts
194 * early.
196 static struct expression * cast_to(struct expression *old, struct symbol *type)
198 struct expression *expr;
201 * See if we can simplify the op. Move the cast down.
203 switch (old->type) {
204 case EXPR_PREOP:
205 if (old->op == '~') {
206 old->ctype = type;
207 old->unop = cast_to(old->unop, type);
208 return old;
210 break;
212 case EXPR_IMPLIED_CAST:
213 if (old->ctype->bit_size >= type->bit_size) {
214 struct expression *orig = old->cast_expression;
215 if (same_cast_type(orig->ctype, type))
216 return orig;
217 if (old->ctype->bit_offset == type->bit_offset) {
218 old->ctype = type;
219 old->cast_type = type;
220 return old;
223 break;
225 default:
226 /* nothing */;
229 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
230 expr->ctype = type;
231 expr->cast_type = type;
232 expr->cast_expression = old;
233 return expr;
236 static int is_type_type(struct symbol *type)
238 return (type->ctype.modifiers & MOD_TYPE) != 0;
241 static int is_ptr_type(struct symbol *type)
243 if (type->type == SYM_NODE)
244 type = type->ctype.base_type;
245 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
248 static inline int is_float_type(struct symbol *type)
250 if (type->type == SYM_NODE)
251 type = type->ctype.base_type;
252 return type->ctype.base_type == &fp_type;
255 static inline int is_byte_type(struct symbol *type)
257 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
260 static inline int is_string_type(struct symbol *type)
262 if (type->type == SYM_NODE)
263 type = type->ctype.base_type;
264 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
267 static struct symbol *bad_expr_type(struct expression *expr)
269 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
270 switch (expr->type) {
271 case EXPR_BINOP:
272 case EXPR_COMPARE:
273 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
274 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
275 break;
276 case EXPR_PREOP:
277 case EXPR_POSTOP:
278 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
279 break;
280 default:
281 break;
284 return NULL;
287 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
289 struct expression *left = *lp, *right = *rp;
290 struct symbol *ltype = left->ctype, *rtype = right->ctype;
292 if (ltype->type == SYM_NODE)
293 ltype = ltype->ctype.base_type;
294 if (rtype->type == SYM_NODE)
295 rtype = rtype->ctype.base_type;
296 if (is_float_type(ltype)) {
297 if (is_int_type(rtype))
298 goto Left;
299 if (is_float_type(rtype)) {
300 unsigned long lmod = ltype->ctype.modifiers;
301 unsigned long rmod = rtype->ctype.modifiers;
302 lmod &= MOD_LONG | MOD_LONGLONG;
303 rmod &= MOD_LONG | MOD_LONGLONG;
304 if (lmod == rmod)
305 return ltype;
306 if (lmod & ~rmod)
307 goto Left;
308 else
309 goto Right;
311 return NULL;
313 if (!is_float_type(rtype) || !is_int_type(ltype))
314 return NULL;
315 Right:
316 *lp = cast_to(left, rtype);
317 return rtype;
318 Left:
319 *rp = cast_to(right, ltype);
320 return ltype;
323 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
325 struct expression *left = *lp, *right = *rp;
326 struct symbol *ltype = left->ctype, *rtype = right->ctype;
328 if (ltype->type == SYM_NODE)
329 ltype = ltype->ctype.base_type;
330 if (rtype->type == SYM_NODE)
331 rtype = rtype->ctype.base_type;
332 if (is_int_type(ltype) && is_int_type(rtype)) {
333 struct symbol *ctype = bigger_int_type(ltype, rtype);
335 /* Don't bother promoting same-size entities, it only adds clutter */
336 if (ltype->bit_size != ctype->bit_size)
337 *lp = cast_to(left, ctype);
338 if (rtype->bit_size != ctype->bit_size)
339 *rp = cast_to(right, ctype);
340 return ctype;
342 return NULL;
345 static int restricted_value(struct expression *v, struct symbol *type)
347 if (v->type != EXPR_VALUE)
348 return 1;
349 if (v->value != 0)
350 return 1;
351 return 0;
354 static int restricted_binop(int op, struct symbol *type)
356 switch (op) {
357 case '&':
358 case '|':
359 case '^':
360 case '?':
361 case SPECIAL_EQUAL:
362 case SPECIAL_NOTEQUAL:
363 return 0;
364 default:
365 return 1;
369 static int restricted_unop(int op, struct symbol *type)
371 if (op == '~' && type->bit_size >= bits_in_int)
372 return 0;
373 if (op == '+')
374 return 0;
375 return 1;
378 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
380 struct expression *left = *lp, *right = *rp;
381 struct symbol *ltype = left->ctype, *rtype = right->ctype;
382 struct symbol *type = NULL;
384 if (ltype->type == SYM_NODE)
385 ltype = ltype->ctype.base_type;
386 if (ltype->type == SYM_ENUM)
387 ltype = ltype->ctype.base_type;
388 if (rtype->type == SYM_NODE)
389 rtype = rtype->ctype.base_type;
390 if (rtype->type == SYM_ENUM)
391 rtype = rtype->ctype.base_type;
392 if (is_restricted_type(ltype)) {
393 if (is_restricted_type(rtype)) {
394 if (ltype == rtype)
395 type = ltype;
396 } else {
397 if (!restricted_value(right, ltype))
398 type = ltype;
400 } else if (is_restricted_type(rtype)) {
401 if (!restricted_value(left, rtype))
402 type = rtype;
404 if (!type)
405 return NULL;
406 if (restricted_binop(op, type))
407 return NULL;
408 return type;
411 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
413 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
414 if (!ctype && float_ok)
415 ctype = compatible_float_binop(&expr->left, &expr->right);
416 if (!ctype)
417 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
418 if (ctype) {
419 expr->ctype = ctype;
420 return ctype;
422 return bad_expr_type(expr);
425 static inline int lvalue_expression(struct expression *expr)
427 return expr->type == EXPR_PREOP && expr->op == '*';
430 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
432 struct expression *i = *ip;
433 struct symbol *ptr_type = ctype;
434 int bit_size;
436 if (ptr_type->type == SYM_NODE)
437 ptr_type = ptr_type->ctype.base_type;
439 if (!is_int_type(i->ctype))
440 return bad_expr_type(expr);
442 examine_symbol_type(ctype);
444 if (!ctype->ctype.base_type) {
445 warning(expr->pos, "missing type information");
446 return NULL;
449 /* Get the size of whatever the pointer points to */
450 ptr_type = ctype;
451 if (ptr_type->type == SYM_NODE)
452 ptr_type = ptr_type->ctype.base_type;
453 if (ptr_type->type == SYM_PTR)
454 ptr_type = ptr_type->ctype.base_type;
455 bit_size = ptr_type->bit_size;
457 if (i->type == EXPR_VALUE) {
458 i->value *= bit_size >> 3;
459 } else if (bit_size > bits_in_char) {
460 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
461 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
463 val->ctype = size_t_ctype;
464 val->value = bit_size >> 3;
466 mul->op = '*';
467 mul->ctype = size_t_ctype;
468 mul->left = i;
469 mul->right = val;
471 *ip = mul;
474 expr->ctype = ctype;
475 return ctype;
478 static struct symbol *evaluate_add(struct expression *expr)
480 struct expression *left = expr->left, *right = expr->right;
481 struct symbol *ltype = left->ctype, *rtype = right->ctype;
483 if (is_ptr_type(ltype))
484 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
486 if (is_ptr_type(rtype))
487 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
489 return evaluate_arith(expr, 1);
492 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
493 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
494 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
496 const char * type_difference(struct symbol *target, struct symbol *source,
497 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
499 for (;;) {
500 unsigned long mod1, mod2, diff;
501 unsigned long as1, as2;
502 int type1, type2;
503 struct symbol *base1, *base2;
505 if (target == source)
506 break;
507 if (!target || !source)
508 return "different types";
510 * Peel of per-node information.
511 * FIXME! Check alignment and context too here!
513 mod1 = target->ctype.modifiers;
514 as1 = target->ctype.as;
515 mod2 = source->ctype.modifiers;
516 as2 = source->ctype.as;
517 if (target->type == SYM_NODE) {
518 target = target->ctype.base_type;
519 if (!target)
520 return "bad types";
521 if (target->type == SYM_PTR) {
522 mod1 = 0;
523 as1 = 0;
525 mod1 |= target->ctype.modifiers;
526 as1 |= target->ctype.as;
528 if (source->type == SYM_NODE) {
529 source = source->ctype.base_type;
530 if (!source)
531 return "bad types";
532 if (source->type == SYM_PTR) {
533 mod2 = 0;
534 as2 = 0;
536 mod2 |= source->ctype.modifiers;
537 as2 |= source->ctype.as;
539 if (target->type == SYM_ENUM) {
540 target = target->ctype.base_type;
541 if (!target)
542 return "bad types";
544 if (source->type == SYM_ENUM) {
545 source = source->ctype.base_type;
546 if (!source)
547 return "bad types";
550 if (target == source)
551 break;
552 if (!target || !source)
553 return "different types";
555 type1 = target->type;
556 base1 = target->ctype.base_type;
558 type2 = source->type;
559 base2 = source->ctype.base_type;
562 * Pointers to functions compare as the function itself
564 if (type1 == SYM_PTR && base1) {
565 switch (base1->type) {
566 case SYM_FN:
567 type1 = SYM_FN;
568 target = base1;
569 base1 = base1->ctype.base_type;
570 default:
571 /* nothing */;
574 if (type2 == SYM_PTR && base2) {
575 switch (base2->type) {
576 case SYM_FN:
577 type2 = SYM_FN;
578 source = base2;
579 base2 = base2->ctype.base_type;
580 default:
581 /* nothing */;
585 /* Arrays degenerate to pointers for type comparisons */
586 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
587 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
589 if (type1 != type2 || type1 == SYM_RESTRICT)
590 return "different base types";
592 /* Must be same address space to be comparable */
593 if (as1 != as2)
594 return "different address spaces";
596 /* Ignore differences in storage types or addressability */
597 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
598 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
599 if (diff) {
600 if (diff & MOD_SIZE)
601 return "different type sizes";
602 if (diff & ~MOD_SIGNEDNESS)
603 return "different modifiers";
605 /* Differs in signedness only.. */
606 if (Wtypesign) {
608 * Warn if both are explicitly signed ("unsigned" is obvously
609 * always explicit, and since we know one of them has to be
610 * unsigned, we check if the signed one was explicit).
612 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
613 return "different explicit signedness";
616 * "char" matches both "unsigned char" and "signed char",
617 * so if the explicit test didn't trigger, then we should
618 * not warn about a char.
620 if (!(mod1 & MOD_CHAR))
621 return "different signedness";
625 if (type1 == SYM_FN) {
626 int i;
627 struct symbol *arg1, *arg2;
628 if (base1->variadic != base2->variadic)
629 return "incompatible variadic arguments";
630 PREPARE_PTR_LIST(target->arguments, arg1);
631 PREPARE_PTR_LIST(source->arguments, arg2);
632 i = 1;
633 for (;;) {
634 const char *diff;
635 diff = type_difference(arg1, arg2, 0, 0);
636 if (diff) {
637 static char argdiff[80];
638 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
639 return argdiff;
641 if (!arg1)
642 break;
643 NEXT_PTR_LIST(arg1);
644 NEXT_PTR_LIST(arg2);
645 i++;
647 FINISH_PTR_LIST(arg2);
648 FINISH_PTR_LIST(arg1);
651 target = base1;
652 source = base2;
654 return NULL;
657 static int is_null_ptr(struct expression *expr)
659 if (expr->type != EXPR_VALUE || expr->value)
660 return 0;
661 if (!is_ptr_type(expr->ctype))
662 warning(expr->pos, "Using plain integer as NULL pointer");
663 return 1;
666 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
668 /* NULL expression? Just return the type of the "other side" */
669 if (is_null_ptr(r))
670 return l->ctype;
671 if (is_null_ptr(l))
672 return r->ctype;
673 return NULL;
677 * Ignore differences in "volatile" and "const"ness when
678 * subtracting pointers
680 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
682 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
684 const char *typediff;
685 struct symbol *ctype;
686 struct symbol *ltype, *rtype;
687 struct expression *r = *rp;
689 ltype = degenerate(l);
690 rtype = degenerate(r);
693 * If it is an integer subtract: the ptr add case will do the
694 * right thing.
696 if (!is_ptr_type(rtype))
697 return evaluate_ptr_add(expr, degenerate(l), rp);
699 ctype = ltype;
700 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
701 if (typediff) {
702 ctype = common_ptr_type(l, r);
703 if (!ctype) {
704 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
705 return NULL;
708 examine_symbol_type(ctype);
710 /* Figure out the base type we point to */
711 if (ctype->type == SYM_NODE)
712 ctype = ctype->ctype.base_type;
713 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
714 warning(expr->pos, "subtraction of functions? Share your drugs");
715 return NULL;
717 ctype = ctype->ctype.base_type;
719 expr->ctype = ssize_t_ctype;
720 if (ctype->bit_size > bits_in_char) {
721 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
722 struct expression *div = expr;
723 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
725 val->ctype = size_t_ctype;
726 val->value = ctype->bit_size >> 3;
728 sub->op = '-';
729 sub->ctype = ssize_t_ctype;
730 sub->left = l;
731 sub->right = r;
733 div->op = '/';
734 div->left = sub;
735 div->right = val;
738 return ssize_t_ctype;
741 static struct symbol *evaluate_sub(struct expression *expr)
743 struct expression *left = expr->left;
744 struct symbol *ltype = left->ctype;
746 if (is_ptr_type(ltype))
747 return evaluate_ptr_sub(expr, left, &expr->right);
749 return evaluate_arith(expr, 1);
752 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
754 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
756 struct symbol *ctype;
758 if (!expr)
759 return NULL;
761 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
762 warning(expr->pos, "assignment expression in conditional");
764 ctype = evaluate_expression(expr);
765 if (ctype) {
766 if (is_safe_type(ctype))
767 warning(expr->pos, "testing a 'safe expression'");
770 return ctype;
773 static struct symbol *evaluate_logical(struct expression *expr)
775 if (!evaluate_conditional(expr->left, 0))
776 return NULL;
777 if (!evaluate_conditional(expr->right, 0))
778 return NULL;
780 expr->ctype = &bool_ctype;
781 return &bool_ctype;
784 static struct symbol *evaluate_shift(struct expression *expr)
786 struct expression *left = expr->left, *right = expr->right;
787 struct symbol *ltype = left->ctype, *rtype = right->ctype;
789 if (ltype->type == SYM_NODE)
790 ltype = ltype->ctype.base_type;
791 if (rtype->type == SYM_NODE)
792 rtype = rtype->ctype.base_type;
793 if (is_int_type(ltype) && is_int_type(rtype)) {
794 struct symbol *ctype = integer_promotion(ltype);
795 if (ltype->bit_size != ctype->bit_size)
796 expr->left = cast_to(expr->left, ctype);
797 expr->ctype = ctype;
798 ctype = integer_promotion(rtype);
799 if (rtype->bit_size != ctype->bit_size)
800 expr->right = cast_to(expr->right, ctype);
801 return expr->ctype;
803 return bad_expr_type(expr);
806 static struct symbol *evaluate_binop(struct expression *expr)
808 switch (expr->op) {
809 // addition can take ptr+int, fp and int
810 case '+':
811 return evaluate_add(expr);
813 // subtraction can take ptr-ptr, fp and int
814 case '-':
815 return evaluate_sub(expr);
817 // Arithmetic operations can take fp and int
818 case '*': case '/':
819 return evaluate_arith(expr, 1);
821 // shifts do integer promotions, but that's it.
822 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
823 return evaluate_shift(expr);
825 // The rest are integer operations
826 // '%', '&', '^', '|'
827 default:
828 return evaluate_arith(expr, 0);
832 static struct symbol *evaluate_comma(struct expression *expr)
834 expr->ctype = expr->right->ctype;
835 return expr->ctype;
838 static int modify_for_unsigned(int op)
840 if (op == '<')
841 op = SPECIAL_UNSIGNED_LT;
842 else if (op == '>')
843 op = SPECIAL_UNSIGNED_GT;
844 else if (op == SPECIAL_LTE)
845 op = SPECIAL_UNSIGNED_LTE;
846 else if (op == SPECIAL_GTE)
847 op = SPECIAL_UNSIGNED_GTE;
848 return op;
851 static struct symbol *evaluate_compare(struct expression *expr)
853 struct expression *left = expr->left, *right = expr->right;
854 struct symbol *ltype = left->ctype, *rtype = right->ctype;
855 struct symbol *ctype;
857 /* Type types? */
858 if (is_type_type(ltype) && is_type_type(rtype))
859 goto OK;
861 if (is_safe_type(ltype) || is_safe_type(rtype))
862 warning(expr->pos, "testing a 'safe expression'");
864 /* Pointer types? */
865 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
866 // FIXME! Check the types for compatibility
867 goto OK;
870 ctype = compatible_integer_binop(&expr->left, &expr->right);
871 if (ctype) {
872 if (ctype->ctype.modifiers & MOD_UNSIGNED)
873 expr->op = modify_for_unsigned(expr->op);
874 goto OK;
877 ctype = compatible_float_binop(&expr->left, &expr->right);
878 if (ctype)
879 goto OK;
881 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
882 if (ctype)
883 goto OK;
885 bad_expr_type(expr);
888 expr->ctype = &bool_ctype;
889 return &bool_ctype;
893 * FIXME!! This should do casts, array degeneration etc..
895 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
897 struct symbol *ltype = left->ctype, *rtype = right->ctype;
899 if (ltype->type == SYM_NODE)
900 ltype = ltype->ctype.base_type;
902 if (rtype->type == SYM_NODE)
903 rtype = rtype->ctype.base_type;
905 if (ltype->type == SYM_PTR) {
906 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
907 return ltype;
910 if (rtype->type == SYM_PTR) {
911 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
912 return rtype;
914 return NULL;
918 * NOTE! The degenerate case of "x ? : y", where we don't
919 * have a true case, this will possibly promote "x" to the
920 * same type as "y", and thus _change_ the conditional
921 * test in the expression. But since promotion is "safe"
922 * for testing, that's ok.
924 static struct symbol *evaluate_conditional_expression(struct expression *expr)
926 struct expression **true;
927 struct symbol *ctype, *ltype, *rtype;
928 const char * typediff;
930 if (!evaluate_conditional(expr->conditional, 0))
931 return NULL;
932 if (!evaluate_expression(expr->cond_false))
933 return NULL;
935 ctype = degenerate(expr->conditional);
936 rtype = degenerate(expr->cond_false);
938 true = &expr->conditional;
939 ltype = ctype;
940 if (expr->cond_true) {
941 if (!evaluate_expression(expr->cond_true))
942 return NULL;
943 ltype = degenerate(expr->cond_true);
944 true = &expr->cond_true;
947 ctype = compatible_integer_binop(true, &expr->cond_false);
948 if (ctype)
949 goto out;
950 ctype = compatible_ptr_type(*true, expr->cond_false);
951 if (ctype)
952 goto out;
953 ctype = compatible_float_binop(true, &expr->cond_false);
954 if (ctype)
955 goto out;
956 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
957 if (ctype)
958 goto out;
959 ctype = ltype;
960 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
961 if (!typediff)
962 goto out;
963 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
964 return NULL;
966 out:
967 expr->ctype = ctype;
968 return ctype;
971 /* FP assignments can not do modulo or bit operations */
972 static int compatible_float_op(int op)
974 return op == '=' ||
975 op == SPECIAL_ADD_ASSIGN ||
976 op == SPECIAL_SUB_ASSIGN ||
977 op == SPECIAL_MUL_ASSIGN ||
978 op == SPECIAL_DIV_ASSIGN;
981 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
982 struct expression **rp, struct symbol *source, const char *where, int op)
984 const char *typediff;
985 struct symbol *t;
986 int target_as;
988 if (is_int_type(target)) {
989 if (is_int_type(source)) {
990 if (target->bit_size != source->bit_size)
991 goto Cast;
992 if (target->bit_offset != source->bit_offset)
993 goto Cast;
994 return 1;
996 if (is_float_type(source))
997 goto Cast;
998 } else if (is_float_type(target)) {
999 if (!compatible_float_op(op)) {
1000 warning(expr->pos, "invalid assignment");
1001 return 0;
1003 if (is_int_type(source))
1004 goto Cast;
1005 if (is_float_type(source)) {
1006 if (target->bit_size != source->bit_size)
1007 goto Cast;
1008 return 1;
1010 } else if (is_ptr_type(target)) {
1011 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1012 evaluate_ptr_add(expr, target, rp);
1013 return 1;
1017 if (op != '=') {
1018 warning(expr->pos, "invalid assignment");
1019 return 0;
1022 /* It's ok if the target is more volatile or const than the source */
1023 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1024 if (!typediff)
1025 return 1;
1027 if (is_restricted_type(target) && !restricted_value(*rp, target))
1028 return 1;
1030 /* Pointer destination? */
1031 t = target;
1032 target_as = t->ctype.as;
1033 if (t->type == SYM_NODE) {
1034 t = t->ctype.base_type;
1035 target_as |= t->ctype.as;
1037 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1038 struct expression *right = *rp;
1039 struct symbol *s = source;
1040 int source_as;
1042 // NULL pointer is always ok
1043 if (is_null_ptr(right))
1044 return 1;
1046 /* "void *" matches anything as long as the address space is ok */
1047 source_as = s->ctype.as;
1048 if (s->type == SYM_NODE) {
1049 s = s->ctype.base_type;
1050 source_as |= s->ctype.as;
1052 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1053 s = s->ctype.base_type;
1054 t = t->ctype.base_type;
1055 if (s == &void_ctype || t == &void_ctype)
1056 return 1;
1060 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1061 info(expr->pos, " expected %s", show_typename(target));
1062 info(expr->pos, " got %s", show_typename(source));
1063 *rp = cast_to(*rp, target);
1064 return 0;
1065 Cast:
1066 *rp = cast_to(*rp, target);
1067 return 1;
1070 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1072 if (type->ctype.modifiers & MOD_CONST)
1073 warning(left->pos, "assignment to const expression");
1074 if (type->type == SYM_NODE)
1075 type->ctype.modifiers |= MOD_ASSIGNED;
1078 static struct symbol *evaluate_assignment(struct expression *expr)
1080 struct expression *left = expr->left, *right = expr->right;
1081 struct expression *where = expr;
1082 struct symbol *ltype, *rtype;
1084 if (!lvalue_expression(left)) {
1085 warning(expr->pos, "not an lvalue");
1086 return NULL;
1089 ltype = left->ctype;
1091 rtype = degenerate(right);
1093 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1094 return NULL;
1096 evaluate_assign_to(left, ltype);
1098 expr->ctype = ltype;
1099 return ltype;
1102 static void examine_fn_arguments(struct symbol *fn)
1104 struct symbol *s;
1106 FOR_EACH_PTR(fn->arguments, s) {
1107 struct symbol *arg = evaluate_symbol(s);
1108 /* Array/function arguments silently degenerate into pointers */
1109 if (arg) {
1110 struct symbol *ptr;
1111 switch(arg->type) {
1112 case SYM_ARRAY:
1113 case SYM_FN:
1114 ptr = alloc_symbol(s->pos, SYM_PTR);
1115 if (arg->type == SYM_ARRAY)
1116 ptr->ctype = arg->ctype;
1117 else
1118 ptr->ctype.base_type = arg;
1119 ptr->ctype.as |= s->ctype.as;
1120 ptr->ctype.modifiers |= s->ctype.modifiers;
1122 s->ctype.base_type = ptr;
1123 s->ctype.as = 0;
1124 s->ctype.modifiers = 0;
1125 s->bit_size = 0;
1126 examine_symbol_type(s);
1127 break;
1128 default:
1129 /* nothing */
1130 break;
1133 } END_FOR_EACH_PTR(s);
1136 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1138 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1139 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1140 *newsym = *sym;
1141 newsym->ctype.as = as;
1142 newsym->ctype.modifiers = mod;
1143 sym = newsym;
1145 return sym;
1148 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1150 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1151 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1153 node->ctype.base_type = ptr;
1154 ptr->bit_size = bits_in_pointer;
1155 ptr->ctype.alignment = pointer_alignment;
1157 node->bit_size = bits_in_pointer;
1158 node->ctype.alignment = pointer_alignment;
1160 access_symbol(sym);
1161 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1162 if (sym->ctype.modifiers & MOD_REGISTER) {
1163 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1164 sym->ctype.modifiers &= ~MOD_REGISTER;
1166 if (sym->type == SYM_NODE) {
1167 ptr->ctype.as |= sym->ctype.as;
1168 ptr->ctype.modifiers |= sym->ctype.modifiers;
1169 sym = sym->ctype.base_type;
1171 if (degenerate && sym->type == SYM_ARRAY) {
1172 ptr->ctype.as |= sym->ctype.as;
1173 ptr->ctype.modifiers |= sym->ctype.modifiers;
1174 sym = sym->ctype.base_type;
1176 ptr->ctype.base_type = sym;
1178 return node;
1181 /* Arrays degenerate into pointers on pointer arithmetic */
1182 static struct symbol *degenerate(struct expression *expr)
1184 struct symbol *ctype, *base;
1186 if (!expr)
1187 return NULL;
1188 ctype = expr->ctype;
1189 if (!ctype)
1190 return NULL;
1191 base = ctype;
1192 if (ctype->type == SYM_NODE)
1193 base = ctype->ctype.base_type;
1195 * Arrays degenerate into pointers to the entries, while
1196 * functions degenerate into pointers to themselves.
1197 * If array was part of non-lvalue compound, we create a copy
1198 * of that compound first and then act as if we were dealing with
1199 * the corresponding field in there.
1201 switch (base->type) {
1202 case SYM_ARRAY:
1203 if (expr->type == EXPR_SLICE) {
1204 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1205 struct expression *e0, *e1, *e2, *e3, *e4;
1207 a->ctype.base_type = expr->base->ctype;
1208 a->bit_size = expr->base->ctype->bit_size;
1209 a->array_size = expr->base->ctype->array_size;
1211 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1212 e0->symbol = a;
1213 e0->ctype = &lazy_ptr_ctype;
1215 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1216 e1->unop = e0;
1217 e1->op = '*';
1218 e1->ctype = expr->base->ctype; /* XXX */
1220 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1221 e2->left = e1;
1222 e2->right = expr->base;
1223 e2->op = '=';
1224 e2->ctype = expr->base->ctype;
1226 if (expr->r_bitpos) {
1227 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1228 e3->op = '+';
1229 e3->left = e0;
1230 e3->right = alloc_const_expression(expr->pos,
1231 expr->r_bitpos >> 3);
1232 e3->ctype = &lazy_ptr_ctype;
1233 } else {
1234 e3 = e0;
1237 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1238 e4->left = e2;
1239 e4->right = e3;
1240 e4->ctype = &lazy_ptr_ctype;
1242 expr->unop = e4;
1243 expr->type = EXPR_PREOP;
1244 expr->op = '*';
1246 case SYM_FN:
1247 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1248 warning(expr->pos, "strange non-value function or array");
1249 return &bad_ctype;
1251 *expr = *expr->unop;
1252 ctype = create_pointer(expr, ctype, 1);
1253 expr->ctype = ctype;
1254 default:
1255 /* nothing */;
1257 return ctype;
1260 static struct symbol *evaluate_addressof(struct expression *expr)
1262 struct expression *op = expr->unop;
1263 struct symbol *ctype;
1265 if (op->op != '*' || op->type != EXPR_PREOP) {
1266 warning(expr->pos, "not addressable");
1267 return NULL;
1269 ctype = op->ctype;
1270 *expr = *op->unop;
1273 * symbol expression evaluation is lazy about the type
1274 * of the sub-expression, so we may have to generate
1275 * the type here if so..
1277 if (expr->ctype == &lazy_ptr_ctype) {
1278 ctype = create_pointer(expr, ctype, 0);
1279 expr->ctype = ctype;
1281 return expr->ctype;
1285 static struct symbol *evaluate_dereference(struct expression *expr)
1287 struct expression *op = expr->unop;
1288 struct symbol *ctype = op->ctype, *node, *target;
1290 /* Simplify: *&(expr) => (expr) */
1291 if (op->type == EXPR_PREOP && op->op == '&') {
1292 *expr = *op->unop;
1293 return expr->ctype;
1296 /* Dereferencing a node drops all the node information. */
1297 if (ctype->type == SYM_NODE)
1298 ctype = ctype->ctype.base_type;
1300 node = alloc_symbol(expr->pos, SYM_NODE);
1301 target = ctype->ctype.base_type;
1303 switch (ctype->type) {
1304 default:
1305 warning(expr->pos, "cannot derefence this type");
1306 return NULL;
1307 case SYM_PTR:
1308 merge_type(node, ctype);
1309 if (ctype->type != SYM_ARRAY)
1310 break;
1312 * Dereferencing a pointer to an array results in a
1313 * degenerate dereference: the expression becomes
1314 * just a pointer to the entry, and the derefence
1315 * goes away.
1317 *expr = *op;
1319 target = alloc_symbol(expr->pos, SYM_PTR);
1320 target->bit_size = bits_in_pointer;
1321 target->ctype.alignment = pointer_alignment;
1322 merge_type(target, ctype->ctype.base_type);
1323 break;
1325 case SYM_ARRAY:
1326 if (!lvalue_expression(op)) {
1327 warning(op->pos, "non-lvalue array??");
1328 return NULL;
1331 /* Do the implied "addressof" on the array */
1332 *op = *op->unop;
1335 * When an array is dereferenced, we need to pick
1336 * up the attributes of the original node too..
1338 merge_type(node, op->ctype);
1339 merge_type(node, ctype);
1340 break;
1343 node->bit_size = target->bit_size;
1344 node->array_size = target->array_size;
1346 expr->ctype = node;
1347 return node;
1351 * Unary post-ops: x++ and x--
1353 static struct symbol *evaluate_postop(struct expression *expr)
1355 struct expression *op = expr->unop;
1356 struct symbol *ctype = op->ctype;
1358 if (!lvalue_expression(expr->unop)) {
1359 warning(expr->pos, "need lvalue expression for ++/--");
1360 return NULL;
1362 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1363 warning(expr->pos, "bad operation on restricted");
1364 return NULL;
1367 evaluate_assign_to(op, ctype);
1369 expr->ctype = ctype;
1370 return ctype;
1373 static struct symbol *evaluate_sign(struct expression *expr)
1375 struct symbol *ctype = expr->unop->ctype;
1376 if (is_int_type(ctype)) {
1377 struct symbol *rtype = rtype = integer_promotion(ctype);
1378 if (rtype->bit_size != ctype->bit_size)
1379 expr->unop = cast_to(expr->unop, rtype);
1380 ctype = rtype;
1381 } else if (is_float_type(ctype) && expr->op != '~') {
1382 /* no conversions needed */
1383 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1384 /* no conversions needed */
1385 } else {
1386 return bad_expr_type(expr);
1388 if (expr->op == '+')
1389 *expr = *expr->unop;
1390 expr->ctype = ctype;
1391 return ctype;
1394 static struct symbol *evaluate_preop(struct expression *expr)
1396 struct symbol *ctype = expr->unop->ctype;
1398 switch (expr->op) {
1399 case '(':
1400 *expr = *expr->unop;
1401 return ctype;
1403 case '+':
1404 case '-':
1405 case '~':
1406 return evaluate_sign(expr);
1408 case '*':
1409 return evaluate_dereference(expr);
1411 case '&':
1412 return evaluate_addressof(expr);
1414 case SPECIAL_INCREMENT:
1415 case SPECIAL_DECREMENT:
1417 * From a type evaluation standpoint the pre-ops are
1418 * the same as the postops
1420 return evaluate_postop(expr);
1422 case '!':
1423 if (is_safe_type(ctype))
1424 warning(expr->pos, "testing a 'safe expression'");
1425 if (is_float_type(ctype)) {
1426 struct expression *arg = expr->unop;
1427 expr->type = EXPR_BINOP;
1428 expr->op = SPECIAL_EQUAL;
1429 expr->left = arg;
1430 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1431 expr->right->ctype = ctype;
1432 expr->right->fvalue = 0;
1434 ctype = &bool_ctype;
1435 break;
1437 default:
1438 break;
1440 expr->ctype = ctype;
1441 return &bool_ctype;
1444 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1446 struct ptr_list *head = (struct ptr_list *)_list;
1447 struct ptr_list *list = head;
1449 if (!head)
1450 return NULL;
1451 do {
1452 int i;
1453 for (i = 0; i < list->nr; i++) {
1454 struct symbol *sym = (struct symbol *) list->list[i];
1455 if (sym->ident) {
1456 if (sym->ident != ident)
1457 continue;
1458 *offset = sym->offset;
1459 return sym;
1460 } else {
1461 struct symbol *ctype = sym->ctype.base_type;
1462 struct symbol *sub;
1463 if (!ctype)
1464 continue;
1465 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1466 continue;
1467 sub = find_identifier(ident, ctype->symbol_list, offset);
1468 if (!sub)
1469 continue;
1470 *offset += sym->offset;
1471 return sub;
1474 } while ((list = list->next) != head);
1475 return NULL;
1478 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1480 struct expression *add;
1483 * Create a new add-expression
1485 * NOTE! Even if we just add zero, we need a new node
1486 * for the member pointer, since it has a different
1487 * type than the original pointer. We could make that
1488 * be just a cast, but the fact is, a node is a node,
1489 * so we might as well just do the "add zero" here.
1491 add = alloc_expression(expr->pos, EXPR_BINOP);
1492 add->op = '+';
1493 add->left = expr;
1494 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1495 add->right->ctype = &int_ctype;
1496 add->right->value = offset;
1499 * The ctype of the pointer will be lazily evaluated if
1500 * we ever take the address of this member dereference..
1502 add->ctype = &lazy_ptr_ctype;
1503 return add;
1506 /* structure/union dereference */
1507 static struct symbol *evaluate_member_dereference(struct expression *expr)
1509 int offset;
1510 struct symbol *ctype, *member;
1511 struct expression *deref = expr->deref, *add;
1512 struct ident *ident = expr->member;
1513 unsigned int mod;
1514 int address_space;
1516 if (!evaluate_expression(deref))
1517 return NULL;
1518 if (!ident) {
1519 warning(expr->pos, "bad member name");
1520 return NULL;
1523 ctype = deref->ctype;
1524 address_space = ctype->ctype.as;
1525 mod = ctype->ctype.modifiers;
1526 if (ctype->type == SYM_NODE) {
1527 ctype = ctype->ctype.base_type;
1528 address_space |= ctype->ctype.as;
1529 mod |= ctype->ctype.modifiers;
1531 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1532 warning(expr->pos, "expected structure or union");
1533 return NULL;
1535 offset = 0;
1536 member = find_identifier(ident, ctype->symbol_list, &offset);
1537 if (!member) {
1538 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1539 const char *name = "<unnamed>";
1540 int namelen = 9;
1541 if (ctype->ident) {
1542 name = ctype->ident->name;
1543 namelen = ctype->ident->len;
1545 warning(expr->pos, "no member '%s' in %s %.*s",
1546 show_ident(ident), type, namelen, name);
1547 return NULL;
1551 * The member needs to take on the address space and modifiers of
1552 * the "parent" type.
1554 member = convert_to_as_mod(member, address_space, mod);
1555 ctype = member->ctype.base_type;
1557 if (!lvalue_expression(deref)) {
1558 if (deref->type != EXPR_SLICE) {
1559 expr->base = deref;
1560 expr->r_bitpos = 0;
1561 } else {
1562 expr->base = deref->base;
1563 expr->r_bitpos = deref->r_bitpos;
1565 expr->r_bitpos += offset << 3;
1566 expr->type = EXPR_SLICE;
1567 expr->r_nrbits = member->bit_size;
1568 expr->r_bitpos += member->bit_offset;
1569 expr->ctype = member;
1570 return member;
1573 deref = deref->unop;
1574 expr->deref = deref;
1576 add = evaluate_offset(deref, offset);
1577 expr->type = EXPR_PREOP;
1578 expr->op = '*';
1579 expr->unop = add;
1581 expr->ctype = member;
1582 return member;
1585 static int is_promoted(struct expression *expr)
1587 while (1) {
1588 switch (expr->type) {
1589 case EXPR_BINOP:
1590 case EXPR_SELECT:
1591 case EXPR_CONDITIONAL:
1592 return 1;
1593 case EXPR_COMMA:
1594 expr = expr->right;
1595 continue;
1596 case EXPR_PREOP:
1597 switch (expr->op) {
1598 case '(':
1599 expr = expr->unop;
1600 continue;
1601 case '+':
1602 case '-':
1603 case '~':
1604 return 1;
1605 default:
1606 return 0;
1608 default:
1609 return 0;
1615 static struct symbol *evaluate_cast(struct expression *);
1617 static struct symbol *evaluate_type_information(struct expression *expr)
1619 struct symbol *sym = expr->cast_type;
1620 if (!sym) {
1621 sym = evaluate_expression(expr->cast_expression);
1622 if (!sym)
1623 return NULL;
1625 * Expressions of restricted types will possibly get
1626 * promoted - check that here
1628 if (is_restricted_type(sym)) {
1629 if (sym->bit_size < bits_in_int && is_promoted(expr))
1630 sym = &int_ctype;
1633 examine_symbol_type(sym);
1634 if (is_bitfield_type(sym)) {
1635 warning(expr->pos, "trying to examine bitfield type");
1636 return NULL;
1638 return sym;
1641 static struct symbol *evaluate_sizeof(struct expression *expr)
1643 struct symbol *type;
1644 int size;
1646 type = evaluate_type_information(expr);
1647 if (!type)
1648 return NULL;
1650 size = type->bit_size;
1651 if (size & 7)
1652 warning(expr->pos, "cannot size expression");
1653 expr->type = EXPR_VALUE;
1654 expr->value = size >> 3;
1655 expr->ctype = size_t_ctype;
1656 return size_t_ctype;
1659 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1661 struct symbol *type;
1662 int size;
1664 type = evaluate_type_information(expr);
1665 if (!type)
1666 return NULL;
1668 if (type->type == SYM_NODE)
1669 type = type->ctype.base_type;
1670 if (!type)
1671 return NULL;
1672 switch (type->type) {
1673 case SYM_ARRAY:
1674 break;
1675 case SYM_PTR:
1676 type = type->ctype.base_type;
1677 if (type)
1678 break;
1679 default:
1680 warning(expr->pos, "expected pointer expression");
1681 return NULL;
1683 size = type->bit_size;
1684 if (size & 7)
1685 size = 0;
1686 expr->type = EXPR_VALUE;
1687 expr->value = size >> 3;
1688 expr->ctype = size_t_ctype;
1689 return size_t_ctype;
1692 static struct symbol *evaluate_alignof(struct expression *expr)
1694 struct symbol *type;
1696 type = evaluate_type_information(expr);
1697 if (!type)
1698 return NULL;
1700 expr->type = EXPR_VALUE;
1701 expr->value = type->ctype.alignment;
1702 expr->ctype = size_t_ctype;
1703 return size_t_ctype;
1706 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1708 struct expression *expr;
1709 struct symbol_list *argument_types = fn->arguments;
1710 struct symbol *argtype;
1711 int i = 1;
1713 PREPARE_PTR_LIST(argument_types, argtype);
1714 FOR_EACH_PTR (head, expr) {
1715 struct expression **p = THIS_ADDRESS(expr);
1716 struct symbol *ctype, *target;
1717 ctype = evaluate_expression(expr);
1719 if (!ctype)
1720 return 0;
1722 ctype = degenerate(expr);
1724 target = argtype;
1725 if (!target && ctype->bit_size < bits_in_int)
1726 target = &int_ctype;
1727 if (target) {
1728 static char where[30];
1729 examine_symbol_type(target);
1730 sprintf(where, "argument %d", i);
1731 compatible_assignment_types(expr, target, p, ctype, where, '=');
1734 i++;
1735 NEXT_PTR_LIST(argtype);
1736 } END_FOR_EACH_PTR(expr);
1737 FINISH_PTR_LIST(argtype);
1738 return 1;
1741 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1743 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1745 struct expression *entry = *ep;
1746 struct expression **parent, *reuse = NULL;
1747 unsigned long offset;
1748 struct symbol *sym;
1749 unsigned long from, to;
1750 int accept_string = is_byte_type(ctype);
1752 from = current;
1753 to = from+1;
1754 parent = ep;
1755 if (entry->type == EXPR_INDEX) {
1756 from = entry->idx_from;
1757 to = entry->idx_to+1;
1758 parent = &entry->idx_expression;
1759 reuse = entry;
1760 entry = entry->idx_expression;
1763 offset = from * (ctype->bit_size>>3);
1764 if (offset) {
1765 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1766 reuse->type = EXPR_POS;
1767 reuse->ctype = ctype;
1768 reuse->init_offset = offset;
1769 reuse->init_nr = to - from;
1770 reuse->init_expr = entry;
1771 parent = &reuse->init_expr;
1772 entry = reuse;
1774 *ep = entry;
1776 if (accept_string && entry->type == EXPR_STRING) {
1777 sym = evaluate_expression(entry);
1778 to = from + get_expression_value(sym->array_size);
1779 } else {
1780 evaluate_initializer(ctype, parent);
1782 return to;
1785 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1787 struct expression *entry;
1788 int current = 0;
1790 FOR_EACH_PTR(expr->expr_list, entry) {
1791 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1792 } END_FOR_EACH_PTR(entry);
1795 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1796 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1798 if (expression_list_size(expr->expr_list) != 1) {
1799 warning(expr->pos, "unexpected compound initializer");
1800 return;
1802 evaluate_array_initializer(ctype, expr);
1803 return;
1806 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1808 struct symbol *sym;
1810 FOR_EACH_PTR(ctype->symbol_list, sym) {
1811 if (sym->ident == ident)
1812 return sym;
1813 } END_FOR_EACH_PTR(sym);
1814 return NULL;
1817 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1819 struct expression *entry = *ep;
1820 struct expression **parent;
1821 struct expression *reuse = NULL;
1822 unsigned long offset;
1824 if (!sym) {
1825 error(entry->pos, "unknown named initializer");
1826 return -1;
1829 if (entry->type == EXPR_IDENTIFIER) {
1830 reuse = entry;
1831 entry = entry->ident_expression;
1834 parent = ep;
1835 offset = sym->offset;
1836 if (offset) {
1837 if (!reuse)
1838 reuse = alloc_expression(entry->pos, EXPR_POS);
1839 reuse->type = EXPR_POS;
1840 reuse->ctype = sym;
1841 reuse->init_offset = offset;
1842 reuse->init_nr = 1;
1843 reuse->init_expr = entry;
1844 parent = &reuse->init_expr;
1845 entry = reuse;
1847 *ep = entry;
1848 evaluate_initializer(sym, parent);
1849 return 0;
1852 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1854 struct expression *entry;
1855 struct symbol *sym;
1857 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1858 FOR_EACH_PTR(expr->expr_list, entry) {
1859 if (entry->type == EXPR_IDENTIFIER) {
1860 struct ident *ident = entry->expr_ident;
1861 /* We special-case the "already right place" case */
1862 if (!sym || sym->ident != ident) {
1863 RESET_PTR_LIST(sym);
1864 for (;;) {
1865 if (!sym)
1866 break;
1867 if (sym->ident == ident)
1868 break;
1869 NEXT_PTR_LIST(sym);
1873 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1874 return;
1875 NEXT_PTR_LIST(sym);
1876 } END_FOR_EACH_PTR(entry);
1877 FINISH_PTR_LIST(sym);
1881 * Initializers are kind of like assignments. Except
1882 * they can be a hell of a lot more complex.
1884 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1886 struct expression *expr = *ep;
1889 * Simple non-structure/array initializers are the simple
1890 * case, and look (and parse) largely like assignments.
1892 switch (expr->type) {
1893 default: {
1894 int is_string = expr->type == EXPR_STRING;
1895 struct symbol *rtype = evaluate_expression(expr);
1896 if (rtype) {
1898 * Special case:
1899 * char array[] = "string"
1900 * should _not_ degenerate.
1902 if (!is_string || !is_string_type(ctype))
1903 rtype = degenerate(expr);
1904 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1906 return;
1909 case EXPR_INITIALIZER:
1910 expr->ctype = ctype;
1911 if (ctype->type == SYM_NODE)
1912 ctype = ctype->ctype.base_type;
1914 switch (ctype->type) {
1915 case SYM_ARRAY:
1916 case SYM_PTR:
1917 evaluate_array_initializer(ctype->ctype.base_type, expr);
1918 return;
1919 case SYM_UNION:
1920 evaluate_struct_or_union_initializer(ctype, expr, 0);
1921 return;
1922 case SYM_STRUCT:
1923 evaluate_struct_or_union_initializer(ctype, expr, 1);
1924 return;
1925 default:
1926 evaluate_scalar_initializer(ctype, expr);
1927 return;
1930 case EXPR_IDENTIFIER:
1931 if (ctype->type == SYM_NODE)
1932 ctype = ctype->ctype.base_type;
1933 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1934 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1935 show_symbol(ctype);
1936 return;
1938 evaluate_one_struct_initializer(ctype, ep,
1939 find_struct_ident(ctype, expr->expr_ident));
1940 return;
1942 case EXPR_INDEX:
1943 if (ctype->type == SYM_NODE)
1944 ctype = ctype->ctype.base_type;
1945 if (ctype->type != SYM_ARRAY) {
1946 error(expr->pos, "expected array");
1947 return;
1949 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1950 return;
1952 case EXPR_POS:
1954 * An EXPR_POS expression has already been evaluated, and we don't
1955 * need to do anything more
1957 return;
1961 static int get_as(struct symbol *sym)
1963 int as;
1964 unsigned long mod;
1966 if (!sym)
1967 return 0;
1968 as = sym->ctype.as;
1969 mod = sym->ctype.modifiers;
1970 if (sym->type == SYM_NODE) {
1971 sym = sym->ctype.base_type;
1972 as |= sym->ctype.as;
1973 mod |= sym->ctype.modifiers;
1977 * At least for now, allow casting to a "unsigned long".
1978 * That's how we do things like pointer arithmetic and
1979 * store pointers to registers.
1981 if (sym == &ulong_ctype)
1982 return -1;
1984 if (sym && sym->type == SYM_PTR) {
1985 sym = sym->ctype.base_type;
1986 as |= sym->ctype.as;
1987 mod |= sym->ctype.modifiers;
1989 if (mod & MOD_FORCE)
1990 return -1;
1991 return as;
1994 static struct symbol *evaluate_cast(struct expression *expr)
1996 struct expression *target = expr->cast_expression;
1997 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1998 enum type type;
2000 if (!target)
2001 return NULL;
2003 expr->ctype = ctype;
2004 expr->cast_type = ctype;
2007 * Special case: a cast can be followed by an
2008 * initializer, in which case we need to pass
2009 * the type value down to that initializer rather
2010 * than trying to evaluate it as an expression
2012 * A more complex case is when the initializer is
2013 * dereferenced as part of a post-fix expression.
2014 * We need to produce an expression that can be dereferenced.
2016 if (target->type == EXPR_INITIALIZER) {
2017 struct symbol *sym = expr->cast_type;
2018 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2020 sym->initializer = expr->cast_expression;
2021 evaluate_symbol(sym);
2023 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2024 addr->symbol = sym;
2026 expr->type = EXPR_PREOP;
2027 expr->op = '*';
2028 expr->unop = addr;
2029 expr->ctype = sym;
2031 return sym;
2034 evaluate_expression(target);
2035 degenerate(target);
2038 * You can always throw a value away by casting to
2039 * "void" - that's an implicit "force". Note that
2040 * the same is _not_ true of "void *".
2042 if (ctype == &void_ctype)
2043 goto out;
2045 type = ctype->type;
2046 if (type == SYM_NODE) {
2047 type = ctype->ctype.base_type->type;
2048 if (ctype->ctype.base_type == &void_ctype)
2049 goto out;
2051 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2052 warning(expr->pos, "cast to non-scalar");
2054 if (!target->ctype) {
2055 warning(expr->pos, "cast from unknown type");
2056 goto out;
2059 type = target->ctype->type;
2060 if (type == SYM_NODE)
2061 type = target->ctype->ctype.base_type->type;
2062 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2063 warning(expr->pos, "cast from non-scalar");
2065 if (!get_as(ctype) && get_as(target->ctype) > 0)
2066 warning(expr->pos, "cast removes address space of expression");
2068 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2069 struct symbol *t1 = ctype, *t2 = target->ctype;
2070 if (t1->type == SYM_NODE)
2071 t1 = t1->ctype.base_type;
2072 if (t2->type == SYM_NODE)
2073 t2 = t2->ctype.base_type;
2074 if (t1 != t2) {
2075 if (t1->type == SYM_RESTRICT)
2076 warning(expr->pos, "cast to restricted type");
2077 if (t2->type == SYM_RESTRICT)
2078 warning(expr->pos, "cast from restricted type");
2083 * Casts of constant values are special: they
2084 * can be NULL, and thus need to be simplified
2085 * early.
2087 if (target->type == EXPR_VALUE)
2088 cast_value(expr, ctype, target, target->ctype);
2090 out:
2091 return ctype;
2095 * Evaluate a call expression with a symbol. This
2096 * should expand inline functions, and evaluate
2097 * builtins.
2099 static int evaluate_symbol_call(struct expression *expr)
2101 struct expression *fn = expr->fn;
2102 struct symbol *ctype = fn->ctype;
2104 if (fn->type != EXPR_PREOP)
2105 return 0;
2107 if (ctype->op && ctype->op->evaluate)
2108 return ctype->op->evaluate(expr);
2110 if (ctype->ctype.modifiers & MOD_INLINE) {
2111 int ret;
2112 struct symbol *curr = current_fn;
2113 current_fn = ctype->ctype.base_type;
2114 examine_fn_arguments(current_fn);
2116 ret = inline_function(expr, ctype);
2118 /* restore the old function */
2119 current_fn = curr;
2120 return ret;
2123 return 0;
2126 static struct symbol *evaluate_call(struct expression *expr)
2128 int args, fnargs;
2129 struct symbol *ctype, *sym;
2130 struct expression *fn = expr->fn;
2131 struct expression_list *arglist = expr->args;
2133 if (!evaluate_expression(fn))
2134 return NULL;
2135 sym = ctype = fn->ctype;
2136 if (ctype->type == SYM_NODE)
2137 ctype = ctype->ctype.base_type;
2138 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2139 ctype = ctype->ctype.base_type;
2140 if (!evaluate_arguments(sym, ctype, arglist))
2141 return NULL;
2142 if (ctype->type != SYM_FN) {
2143 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2144 return NULL;
2146 args = expression_list_size(expr->args);
2147 fnargs = symbol_list_size(ctype->arguments);
2148 if (args < fnargs)
2149 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2150 if (args > fnargs && !ctype->variadic)
2151 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2152 if (sym->type == SYM_NODE) {
2153 if (evaluate_symbol_call(expr))
2154 return expr->ctype;
2156 expr->ctype = ctype->ctype.base_type;
2157 return expr->ctype;
2160 struct symbol *evaluate_expression(struct expression *expr)
2162 if (!expr)
2163 return NULL;
2164 if (expr->ctype)
2165 return expr->ctype;
2167 switch (expr->type) {
2168 case EXPR_VALUE:
2169 case EXPR_FVALUE:
2170 warning(expr->pos, "value expression without a type");
2171 return NULL;
2172 case EXPR_STRING:
2173 return evaluate_string(expr);
2174 case EXPR_SYMBOL:
2175 return evaluate_symbol_expression(expr);
2176 case EXPR_BINOP:
2177 if (!evaluate_expression(expr->left))
2178 return NULL;
2179 if (!evaluate_expression(expr->right))
2180 return NULL;
2181 return evaluate_binop(expr);
2182 case EXPR_LOGICAL:
2183 return evaluate_logical(expr);
2184 case EXPR_COMMA:
2185 evaluate_expression(expr->left);
2186 if (!evaluate_expression(expr->right))
2187 return NULL;
2188 return evaluate_comma(expr);
2189 case EXPR_COMPARE:
2190 if (!evaluate_expression(expr->left))
2191 return NULL;
2192 if (!evaluate_expression(expr->right))
2193 return NULL;
2194 return evaluate_compare(expr);
2195 case EXPR_ASSIGNMENT:
2196 if (!evaluate_expression(expr->left))
2197 return NULL;
2198 if (!evaluate_expression(expr->right))
2199 return NULL;
2200 return evaluate_assignment(expr);
2201 case EXPR_PREOP:
2202 if (!evaluate_expression(expr->unop))
2203 return NULL;
2204 return evaluate_preop(expr);
2205 case EXPR_POSTOP:
2206 if (!evaluate_expression(expr->unop))
2207 return NULL;
2208 return evaluate_postop(expr);
2209 case EXPR_CAST:
2210 case EXPR_IMPLIED_CAST:
2211 return evaluate_cast(expr);
2212 case EXPR_SIZEOF:
2213 return evaluate_sizeof(expr);
2214 case EXPR_PTRSIZEOF:
2215 return evaluate_ptrsizeof(expr);
2216 case EXPR_ALIGNOF:
2217 return evaluate_alignof(expr);
2218 case EXPR_DEREF:
2219 return evaluate_member_dereference(expr);
2220 case EXPR_CALL:
2221 return evaluate_call(expr);
2222 case EXPR_SELECT:
2223 case EXPR_CONDITIONAL:
2224 return evaluate_conditional_expression(expr);
2225 case EXPR_STATEMENT:
2226 expr->ctype = evaluate_statement(expr->statement);
2227 return expr->ctype;
2229 case EXPR_LABEL:
2230 expr->ctype = &ptr_ctype;
2231 return &ptr_ctype;
2233 case EXPR_TYPE:
2234 /* Evaluate the type of the symbol .. */
2235 evaluate_symbol(expr->symbol);
2236 /* .. but the type of the _expression_ is a "type" */
2237 expr->ctype = &type_ctype;
2238 return &type_ctype;
2240 /* These can not exist as stand-alone expressions */
2241 case EXPR_INITIALIZER:
2242 case EXPR_IDENTIFIER:
2243 case EXPR_INDEX:
2244 case EXPR_POS:
2245 warning(expr->pos, "internal front-end error: initializer in expression");
2246 return NULL;
2247 case EXPR_SLICE:
2248 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2249 return NULL;
2251 return NULL;
2254 void check_duplicates(struct symbol *sym)
2256 struct symbol *next = sym;
2258 while ((next = next->same_symbol) != NULL) {
2259 const char *typediff;
2260 evaluate_symbol(next);
2261 typediff = type_difference(sym, next, 0, 0);
2262 if (typediff) {
2263 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2264 show_ident(sym->ident),
2265 input_streams[next->pos.stream].name, next->pos.line, typediff);
2266 return;
2271 struct symbol *evaluate_symbol(struct symbol *sym)
2273 struct symbol *base_type;
2275 if (!sym)
2276 return sym;
2278 sym = examine_symbol_type(sym);
2279 base_type = sym->ctype.base_type;
2280 if (!base_type)
2281 return NULL;
2283 /* Evaluate the initializers */
2284 if (sym->initializer)
2285 evaluate_initializer(sym, &sym->initializer);
2287 /* And finally, evaluate the body of the symbol too */
2288 if (base_type->type == SYM_FN) {
2289 struct symbol *curr = current_fn;
2291 current_fn = base_type;
2293 examine_fn_arguments(base_type);
2294 if (!base_type->stmt && base_type->inline_stmt)
2295 uninline(sym);
2296 if (base_type->stmt)
2297 evaluate_statement(base_type->stmt);
2299 current_fn = curr;
2302 return base_type;
2305 struct symbol *evaluate_return_expression(struct statement *stmt)
2307 struct expression *expr = stmt->expression;
2308 struct symbol *ctype, *fntype;
2310 evaluate_expression(expr);
2311 ctype = degenerate(expr);
2312 fntype = current_fn->ctype.base_type;
2313 if (!fntype || fntype == &void_ctype) {
2314 if (expr && ctype != &void_ctype)
2315 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2316 return NULL;
2319 if (!expr) {
2320 warning(stmt->pos, "return with no return value");
2321 return NULL;
2323 if (!ctype)
2324 return NULL;
2325 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2326 return NULL;
2329 static void evaluate_if_statement(struct statement *stmt)
2331 if (!stmt->if_conditional)
2332 return;
2334 evaluate_conditional(stmt->if_conditional, 0);
2335 evaluate_statement(stmt->if_true);
2336 evaluate_statement(stmt->if_false);
2339 static void evaluate_iterator(struct statement *stmt)
2341 evaluate_conditional(stmt->iterator_pre_condition, 1);
2342 evaluate_conditional(stmt->iterator_post_condition,1);
2343 evaluate_statement(stmt->iterator_pre_statement);
2344 evaluate_statement(stmt->iterator_statement);
2345 evaluate_statement(stmt->iterator_post_statement);
2348 struct symbol *evaluate_statement(struct statement *stmt)
2350 if (!stmt)
2351 return NULL;
2353 switch (stmt->type) {
2354 case STMT_RETURN:
2355 return evaluate_return_expression(stmt);
2357 case STMT_EXPRESSION:
2358 if (!evaluate_expression(stmt->expression))
2359 return NULL;
2360 return degenerate(stmt->expression);
2362 case STMT_COMPOUND: {
2363 struct statement *s;
2364 struct symbol *type = NULL;
2365 struct symbol *sym;
2367 /* Evaluate each symbol in the compound statement */
2368 FOR_EACH_PTR(stmt->syms, sym) {
2369 evaluate_symbol(sym);
2370 } END_FOR_EACH_PTR(sym);
2371 evaluate_symbol(stmt->ret);
2374 * Then, evaluate each statement, making the type of the
2375 * compound statement be the type of the last statement
2377 type = NULL;
2378 FOR_EACH_PTR(stmt->stmts, s) {
2379 type = evaluate_statement(s);
2380 } END_FOR_EACH_PTR(s);
2381 if (!type)
2382 type = &void_ctype;
2383 return type;
2385 case STMT_IF:
2386 evaluate_if_statement(stmt);
2387 return NULL;
2388 case STMT_ITERATOR:
2389 evaluate_iterator(stmt);
2390 return NULL;
2391 case STMT_SWITCH:
2392 evaluate_expression(stmt->switch_expression);
2393 evaluate_statement(stmt->switch_statement);
2394 return NULL;
2395 case STMT_CASE:
2396 evaluate_expression(stmt->case_expression);
2397 evaluate_expression(stmt->case_to);
2398 evaluate_statement(stmt->case_statement);
2399 return NULL;
2400 case STMT_LABEL:
2401 return evaluate_statement(stmt->label_statement);
2402 case STMT_GOTO:
2403 evaluate_expression(stmt->goto_expression);
2404 return NULL;
2405 case STMT_NONE:
2406 break;
2407 case STMT_ASM:
2408 /* FIXME! Do the asm parameter evaluation! */
2409 break;
2410 case STMT_INTERNAL:
2411 evaluate_expression(stmt->expression);
2412 return NULL;
2414 return NULL;