Don't die on unknown expressions at linearization time.
[smatch.git] / evaluate.c
blobeb0d964b6f2188eb0290498f210829ecdd49097c
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 static struct symbol *current_fn;
29 static int current_context, current_contextmask;
31 static struct symbol *degenerate(struct expression *expr);
33 static struct symbol *evaluate_symbol_expression(struct expression *expr)
35 struct symbol *sym = expr->symbol;
36 struct symbol *base_type;
38 if (!sym) {
39 if (preprocessing) {
40 expr->ctype = &int_ctype;
41 return &int_ctype;
43 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
44 return NULL;
47 examine_symbol_type(sym);
48 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
49 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
51 base_type = sym->ctype.base_type;
52 if (!base_type) {
53 warn(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
54 return NULL;
57 /* The type of a symbol is the symbol itself! */
58 expr->ctype = sym;
60 /* enum's can be turned into plain values */
61 if (sym->type != SYM_ENUM) {
62 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
63 addr->symbol = sym;
64 addr->symbol_name = expr->symbol_name;
65 addr->ctype = NULL; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr->type = EXPR_PREOP;
67 expr->op = '*';
68 expr->unop = addr;
69 return sym;
71 expr->type = EXPR_VALUE;
72 expr->value = sym->value;
73 expr->ctype = base_type;
74 return sym;
77 static struct symbol *evaluate_string(struct expression *expr)
79 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
80 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
81 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
82 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
83 unsigned int length = expr->string->length;
85 sym->array_size = alloc_const_expression(expr->pos, length);
86 sym->bit_size = bits_in_char * length;
87 sym->ctype.alignment = 1;
88 sym->ctype.modifiers = MOD_STATIC;
89 sym->ctype.base_type = array;
90 sym->initializer = initstr;
92 initstr->ctype = sym;
93 initstr->string = expr->string;
95 array->array_size = sym->array_size;
96 array->bit_size = bits_in_char * length;
97 array->ctype.alignment = 1;
98 array->ctype.modifiers = MOD_STATIC;
99 array->ctype.base_type = &char_ctype;
101 addr->symbol = sym;
102 addr->ctype = NULL;
104 expr->type = EXPR_PREOP;
105 expr->op = '*';
106 expr->unop = addr;
107 expr->ctype = sym;
108 return sym;
111 static inline struct symbol *integer_promotion(struct symbol *type)
113 unsigned long mod = type->ctype.modifiers;
114 int width;
116 if (type->type == SYM_ENUM)
117 return &int_ctype;
118 else if (type->type == SYM_BITFIELD) {
119 mod = type->ctype.base_type->ctype.modifiers;
120 width = type->fieldwidth;
121 } else if (mod & (MOD_CHAR | MOD_SHORT))
122 width = type->bit_size;
123 else
124 return type;
125 if (mod & MOD_UNSIGNED && width == bits_in_int)
126 return &uint_ctype;
127 return &int_ctype;
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
136 * signed one.
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
149 unsigned long lmod, rmod;
151 left = integer_promotion(left);
152 right = integer_promotion(right);
154 if (left == right)
155 goto left;
157 if (left->bit_size > right->bit_size)
158 goto left;
160 if (right->bit_size > left->bit_size)
161 goto right;
163 lmod = left->ctype.modifiers;
164 rmod = right->ctype.modifiers;
165 if ((lmod ^ rmod) & MOD_UNSIGNED) {
166 if (lmod & MOD_UNSIGNED)
167 goto left;
168 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
169 goto left;
170 right:
171 left = right;
172 left:
173 return left;
176 static struct expression * cast_to(struct expression *old, struct symbol *type)
178 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
179 expr->ctype = type;
180 expr->cast_type = type;
181 expr->cast_expression = old;
182 return expr;
185 static int is_type_type(struct symbol *type)
187 return (type->ctype.modifiers & MOD_TYPE) != 0;
190 static int is_ptr_type(struct symbol *type)
192 if (type->type == SYM_NODE)
193 type = type->ctype.base_type;
194 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
197 static inline int is_int_type(struct symbol *type)
199 if (type->type == SYM_NODE)
200 type = type->ctype.base_type;
201 return (type->type == SYM_ENUM) ||
202 (type->type == SYM_BITFIELD) ||
203 type->ctype.base_type == &int_type;
206 static inline int is_float_type(struct symbol *type)
208 if (type->type == SYM_NODE)
209 type = type->ctype.base_type;
210 return type->ctype.base_type == &fp_type;
213 static struct symbol *bad_expr_type(struct expression *expr)
215 warn(expr->pos, "incompatible types for operation");
216 return NULL;
219 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
221 struct expression *left = *lp, *right = *rp;
222 struct symbol *ltype = left->ctype, *rtype = right->ctype;
224 if (ltype->type == SYM_NODE)
225 ltype = ltype->ctype.base_type;
226 if (rtype->type == SYM_NODE)
227 rtype = rtype->ctype.base_type;
228 if (is_float_type(ltype)) {
229 if (is_int_type(rtype))
230 goto Left;
231 if (is_float_type(rtype)) {
232 unsigned long lmod = ltype->ctype.modifiers;
233 unsigned long rmod = rtype->ctype.modifiers;
234 lmod &= MOD_LONG | MOD_LONGLONG;
235 rmod &= MOD_LONG | MOD_LONGLONG;
236 if (lmod == rmod)
237 return ltype;
238 if (lmod & ~rmod)
239 goto Left;
240 else
241 goto Right;
243 return NULL;
245 if (!is_float_type(rtype) || !is_int_type(ltype))
246 return NULL;
247 Right:
248 *lp = cast_to(left, rtype);
249 return rtype;
250 Left:
251 *rp = cast_to(right, ltype);
252 return ltype;
255 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
257 struct expression *left = *lp, *right = *rp;
258 struct symbol *ltype = left->ctype, *rtype = right->ctype;
260 if (ltype->type == SYM_NODE)
261 ltype = ltype->ctype.base_type;
262 if (rtype->type == SYM_NODE)
263 rtype = rtype->ctype.base_type;
264 if (is_int_type(ltype) && is_int_type(rtype)) {
265 struct symbol *ctype = bigger_int_type(ltype, rtype);
267 /* Don't bother promoting same-size entities, it only adds clutter */
268 if (ltype->bit_size != ctype->bit_size)
269 *lp = cast_to(left, ctype);
270 if (rtype->bit_size != ctype->bit_size)
271 *rp = cast_to(right, ctype);
272 return ctype;
274 return NULL;
277 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
279 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
280 if (!ctype && float_ok)
281 ctype = compatible_float_binop(&expr->left, &expr->right);
282 if (ctype) {
283 expr->ctype = ctype;
284 return ctype;
286 return bad_expr_type(expr);
289 static inline int lvalue_expression(struct expression *expr)
291 while (expr->type == EXPR_CAST)
292 expr = expr->cast_expression;
293 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
296 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
298 struct symbol *ctype;
299 struct symbol *ptr_type = ptr->ctype;
300 int bit_size;
302 if (ptr_type->type == SYM_NODE)
303 ptr_type = ptr_type->ctype.base_type;
305 if (!is_int_type(i->ctype))
306 return bad_expr_type(expr);
308 ctype = ptr->ctype;
309 examine_symbol_type(ctype);
311 ctype = degenerate(ptr);
312 if (!ctype->ctype.base_type) {
313 warn(expr->pos, "missing type information");
314 return NULL;
317 /* Get the size of whatever the pointer points to */
318 ptr_type = ctype;
319 if (ptr_type->type == SYM_NODE)
320 ptr_type = ptr_type->ctype.base_type;
321 if (ptr_type->type == SYM_PTR)
322 ptr_type = ptr_type->ctype.base_type;
323 bit_size = ptr_type->bit_size;
325 /* Special case: adding zero commonly happens as a result of 'array[0]' */
326 if (i->type == EXPR_VALUE && !i->value) {
327 *expr = *ptr;
328 } else if (bit_size > bits_in_char) {
329 struct expression *add = expr;
330 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
331 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
333 val->ctype = size_t_ctype;
334 val->value = bit_size >> 3;
336 mul->op = '*';
337 mul->ctype = size_t_ctype;
338 mul->left = i;
339 mul->right = val;
341 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
342 add->left = ptr;
343 add->right = mul;
346 expr->ctype = ctype;
347 return ctype;
350 static struct symbol *evaluate_add(struct expression *expr)
352 struct expression *left = expr->left, *right = expr->right;
353 struct symbol *ltype = left->ctype, *rtype = right->ctype;
355 if (is_ptr_type(ltype))
356 return evaluate_ptr_add(expr, left, right);
358 if (is_ptr_type(rtype))
359 return evaluate_ptr_add(expr, right, left);
361 return evaluate_arith(expr, 1);
364 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
365 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
367 const char * type_difference(struct symbol *target, struct symbol *source,
368 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
370 for (;;) {
371 unsigned long mod1, mod2, diff;
372 unsigned long as1, as2;
373 int type1, type2;
374 struct symbol *base1, *base2;
376 if (target == source)
377 break;
378 if (!target || !source)
379 return "different types";
381 * Peel of per-node information.
382 * FIXME! Check alignment and context too here!
384 mod1 = target->ctype.modifiers;
385 as1 = target->ctype.as;
386 mod2 = source->ctype.modifiers;
387 as2 = source->ctype.as;
388 if (target->type == SYM_NODE) {
389 target = target->ctype.base_type;
390 if (!target)
391 return "bad types";
392 if (target->type == SYM_PTR) {
393 mod1 = 0;
394 as1 = 0;
396 mod1 |= target->ctype.modifiers;
397 as1 |= target->ctype.as;
399 if (source->type == SYM_NODE) {
400 source = source->ctype.base_type;
401 if (!source)
402 return "bad types";
403 if (source->type == SYM_PTR) {
404 mod2 = 0;
405 as2 = 0;
407 mod2 |= source->ctype.modifiers;
408 as2 |= source->ctype.as;
411 if (target == source)
412 break;
413 if (!target || !source)
414 return "different types";
416 type1 = target->type;
417 base1 = target->ctype.base_type;
419 type2 = source->type;
420 base2 = source->ctype.base_type;
423 * Pointers to functions compare as the function itself
425 if (type1 == SYM_PTR && base1) {
426 switch (base1->type) {
427 case SYM_FN:
428 type1 = SYM_FN;
429 target = base1;
430 base1 = base1->ctype.base_type;
431 default:
432 /* nothing */;
435 if (type2 == SYM_PTR && base2) {
436 switch (base2->type) {
437 case SYM_FN:
438 type2 = SYM_FN;
439 source = base2;
440 base2 = base2->ctype.base_type;
441 default:
442 /* nothing */;
446 /* Arrays degenerate to pointers for type comparisons */
447 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
448 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
450 if (type1 != type2)
451 return "different base types";
453 /* Must be same address space to be comparable */
454 if (as1 != as2)
455 return "different address spaces";
457 /* Ignore differences in storage types, sign, or addressability */
458 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
459 if (diff) {
460 mod1 &= diff & ~target_mod_ignore;
461 mod2 &= diff & ~source_mod_ignore;
462 if (mod1 | mod2) {
463 if ((mod1 | mod2) & MOD_SIZE)
464 return "different type sizes";
465 return "different modifiers";
469 if (type1 == SYM_FN) {
470 int i;
471 struct symbol *arg1, *arg2;
472 if (base1->variadic != base2->variadic)
473 return "incompatible variadic arguments";
474 PREPARE_PTR_LIST(target->arguments, arg1);
475 PREPARE_PTR_LIST(source->arguments, arg2);
476 i = 1;
477 for (;;) {
478 const char *diff;
479 diff = type_difference(arg1, arg2, 0, 0);
480 if (diff) {
481 static char argdiff[80];
482 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
483 return argdiff;
485 if (!arg1)
486 break;
487 NEXT_PTR_LIST(arg1);
488 NEXT_PTR_LIST(arg2);
489 i++;
491 FINISH_PTR_LIST(arg2);
492 FINISH_PTR_LIST(arg1);
495 target = base1;
496 source = base2;
498 return NULL;
501 static int is_null_ptr(struct expression *expr)
503 if (expr->type != EXPR_VALUE || expr->value)
504 return 0;
505 if (!is_ptr_type(expr->ctype))
506 warn(expr->pos, "Using plain integer as NULL pointer");
507 return 1;
510 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
512 /* NULL expression? Just return the type of the "other side" */
513 if (is_null_ptr(r))
514 return l->ctype;
515 if (is_null_ptr(l))
516 return r->ctype;
517 return NULL;
521 * Ignore differences in "volatile" and "const"ness when
522 * subtracting pointers
524 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
526 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
528 const char *typediff;
529 struct symbol *ctype;
530 struct symbol *ltype, *rtype;
532 ltype = degenerate(l);
533 rtype = degenerate(r);
536 * If it is an integer subtract: the ptr add case will do the
537 * right thing.
539 if (!is_ptr_type(rtype))
540 return evaluate_ptr_add(expr, l, r);
542 ctype = ltype;
543 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
544 if (typediff) {
545 ctype = common_ptr_type(l, r);
546 if (!ctype) {
547 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
548 return NULL;
551 examine_symbol_type(ctype);
553 /* Figure out the base type we point to */
554 if (ctype->type == SYM_NODE)
555 ctype = ctype->ctype.base_type;
556 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
557 warn(expr->pos, "subtraction of functions? Share your drugs");
558 return NULL;
560 ctype = ctype->ctype.base_type;
562 expr->ctype = ssize_t_ctype;
563 if (ctype->bit_size > bits_in_char) {
564 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
565 struct expression *div = expr;
566 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
568 val->ctype = size_t_ctype;
569 val->value = ctype->bit_size >> 3;
571 sub->op = '-';
572 sub->ctype = ssize_t_ctype;
573 sub->left = l;
574 sub->right = r;
576 div->op = '/';
577 div->left = sub;
578 div->right = val;
581 return ssize_t_ctype;
584 static struct symbol *evaluate_sub(struct expression *expr)
586 struct expression *left = expr->left, *right = expr->right;
587 struct symbol *ltype = left->ctype;
589 if (is_ptr_type(ltype))
590 return evaluate_ptr_sub(expr, left, right);
592 return evaluate_arith(expr, 1);
595 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
597 static struct symbol *evaluate_conditional(struct expression **p)
599 struct symbol *ctype;
600 struct expression *expr = *p;
602 if (!expr)
603 return NULL;
605 if (expr->type == EXPR_ASSIGNMENT)
606 warn(expr->pos, "assignment expression in conditional");
608 ctype = evaluate_expression(expr);
609 if (ctype) {
610 if (is_safe_type(ctype))
611 warn(expr->pos, "testing a 'safe expression'");
612 if (is_float_type(ctype)) {
613 struct expression *comp;
615 * It's easier to handle here, rather than deal with
616 * FP all over the place. Floating point in boolean
617 * context is rare enough (and very often wrong),
618 * so price of explicit comparison with appropriate
619 * FP zero is not too high. And it simplifies things
620 * elsewhere.
622 comp = alloc_expression(expr->pos, EXPR_BINOP);
623 comp->op = SPECIAL_NOTEQUAL;
624 comp->left = expr;
625 comp->right = alloc_expression(expr->pos, EXPR_FVALUE);
626 comp->right->ctype = comp->left->ctype;
627 comp->right->fvalue = 0;
628 ctype = comp->ctype = &bool_ctype;
629 *p = comp;
633 return ctype;
636 static struct symbol *evaluate_logical(struct expression *expr)
638 if (!evaluate_conditional(&expr->left))
639 return NULL;
640 if (!evaluate_conditional(&expr->right))
641 return NULL;
643 expr->ctype = &bool_ctype;
644 return &bool_ctype;
647 static struct symbol *evaluate_shift(struct expression *expr)
649 struct expression *left = expr->left, *right = expr->right;
650 struct symbol *ltype = left->ctype, *rtype = right->ctype;
652 if (ltype->type == SYM_NODE)
653 ltype = ltype->ctype.base_type;
654 if (rtype->type == SYM_NODE)
655 rtype = rtype->ctype.base_type;
656 if (is_int_type(ltype) && is_int_type(rtype)) {
657 struct symbol *ctype = integer_promotion(ltype);
658 if (ltype->bit_size != ctype->bit_size)
659 expr->left = cast_to(expr->left, ctype);
660 expr->ctype = ctype;
661 ctype = integer_promotion(rtype);
662 if (rtype->bit_size != ctype->bit_size)
663 expr->right = cast_to(expr->right, ctype);
664 return expr->ctype;
666 return bad_expr_type(expr);
669 static struct symbol *evaluate_binop(struct expression *expr)
671 switch (expr->op) {
672 // addition can take ptr+int, fp and int
673 case '+':
674 return evaluate_add(expr);
676 // subtraction can take ptr-ptr, fp and int
677 case '-':
678 return evaluate_sub(expr);
680 // Arithmetic operations can take fp and int
681 case '*': case '/':
682 return evaluate_arith(expr, 1);
684 // shifts do integer promotions, but that's it.
685 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
686 return evaluate_shift(expr);
688 // The rest are integer operations
689 // '%', '&', '^', '|'
690 default:
691 return evaluate_arith(expr, 0);
695 static struct symbol *evaluate_comma(struct expression *expr)
697 expr->ctype = expr->right->ctype;
698 return expr->ctype;
701 static int modify_for_unsigned(int op)
703 if (op == '<')
704 op = SPECIAL_UNSIGNED_LT;
705 else if (op == '>')
706 op = SPECIAL_UNSIGNED_GT;
707 else if (op == SPECIAL_LTE)
708 op = SPECIAL_UNSIGNED_LTE;
709 else if (op == SPECIAL_GTE)
710 op = SPECIAL_UNSIGNED_GTE;
711 return op;
714 static struct symbol *evaluate_compare(struct expression *expr)
716 struct expression *left = expr->left, *right = expr->right;
717 struct symbol *ltype = left->ctype, *rtype = right->ctype;
718 struct symbol *ctype;
720 /* Type types? */
721 if (is_type_type(ltype) && is_type_type(rtype)) {
722 expr->ctype = &bool_ctype;
723 return &bool_ctype;
726 if (is_safe_type(ltype) || is_safe_type(rtype))
727 warn(expr->pos, "testing a 'safe expression'");
729 /* Pointer types? */
730 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
731 expr->ctype = &bool_ctype;
732 // FIXME! Check the types for compatibility
733 return &bool_ctype;
736 ctype = compatible_integer_binop(&expr->left, &expr->right);
737 if (ctype) {
738 if (ctype->ctype.modifiers & MOD_UNSIGNED)
739 expr->op = modify_for_unsigned(expr->op);
740 expr->ctype = &bool_ctype;
741 return &bool_ctype;
743 ctype = compatible_float_binop(&expr->left, &expr->right);
745 return bad_expr_type(expr);
749 * FIXME!! This should do casts, array degeneration etc..
751 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
753 struct symbol *ltype = left->ctype, *rtype = right->ctype;
755 if (ltype->type == SYM_NODE)
756 ltype = ltype->ctype.base_type;
758 if (rtype->type == SYM_NODE)
759 rtype = rtype->ctype.base_type;
761 if (ltype->type == SYM_PTR) {
762 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
763 return ltype;
766 if (rtype->type == SYM_PTR) {
767 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
768 return rtype;
770 return NULL;
773 static struct symbol * evaluate_conditional_expression(struct expression *expr)
775 struct expression *cond, *true, *false;
776 struct symbol *ctype, *ltype, *rtype;
777 const char * typediff;
779 ctype = degenerate(expr->conditional);
780 cond = expr->conditional;
782 ltype = ctype;
783 true = cond;
784 if (expr->cond_true) {
785 ltype = degenerate(expr->cond_true);
786 true = expr->cond_true;
789 rtype = degenerate(expr->cond_false);
790 false = expr->cond_false;
792 ctype = ltype;
793 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
794 if (!typediff)
795 goto out;
797 ctype = compatible_integer_binop(&true, &expr->cond_false);
798 if (ctype)
799 goto out;
800 ctype = compatible_ptr_type(true, expr->cond_false);
801 if (ctype)
802 goto out;
803 ctype = compatible_float_binop(&true, &expr->cond_false);
804 if (ctype)
805 goto out;
806 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
807 return NULL;
809 out:
810 expr->ctype = ctype;
811 return ctype;
814 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
815 struct expression **rp, struct symbol *source, const char *where)
817 const char *typediff;
818 struct symbol *t;
819 int target_as;
821 /* It's ok if the target is more volatile or const than the source */
822 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
823 if (!typediff)
824 return 1;
826 if (is_int_type(target)) {
827 if (is_int_type(source)) {
828 if (target->bit_size != source->bit_size)
829 goto Cast;
830 return 1;
832 if (is_float_type(source))
833 goto Cast;
834 } else if (is_float_type(target)) {
835 if (is_int_type(source))
836 goto Cast;
837 if (is_float_type(source)) {
838 if (target->bit_size != source->bit_size)
839 goto Cast;
840 return 1;
844 /* Pointer destination? */
845 t = target;
846 target_as = t->ctype.as;
847 if (t->type == SYM_NODE) {
848 t = t->ctype.base_type;
849 target_as |= t->ctype.as;
851 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
852 struct expression *right = *rp;
853 struct symbol *s = source;
854 int source_as;
856 // NULL pointer is always ok
857 if (is_null_ptr(right))
858 return 1;
860 /* "void *" matches anything as long as the address space is ok */
861 source_as = s->ctype.as;
862 if (s->type == SYM_NODE) {
863 s = s->ctype.base_type;
864 source_as |= s->ctype.as;
866 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
867 s = s->ctype.base_type;
868 t = t->ctype.base_type;
869 if (s == &void_ctype || t == &void_ctype)
870 return 1;
874 // FIXME!! Cast it?
875 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
876 info(expr->pos, " expected %s", show_typename(target));
877 info(expr->pos, " got %s", show_typename(source));
878 return 0;
879 Cast:
880 *rp = cast_to(*rp, target);
881 return 1;
885 * FIXME!! This is wrong from a double evaluation standpoint. We can't
886 * just expand the expression twice, that would make any side effects
887 * happen twice too.
889 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
891 int op = expr->op;
892 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
893 static const int op_trans[] = {
894 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
895 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
896 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
897 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
898 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
899 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
900 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
901 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
902 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '|',
903 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
906 subexpr->left = left;
907 subexpr->right = right;
908 subexpr->op = op_trans[op - SPECIAL_BASE];
909 expr->op = '=';
910 expr->right = subexpr;
911 return evaluate_binop(subexpr);
914 static void evaluate_assign_to(struct expression *left, struct symbol *type)
916 if (type->ctype.modifiers & MOD_CONST)
917 warn(left->pos, "assignment to const expression");
918 if (type->type == SYM_NODE)
919 type->ctype.modifiers |= MOD_ASSIGNED;
922 static struct symbol *evaluate_assignment(struct expression *expr)
924 struct expression *left = expr->left, *right = expr->right;
925 struct symbol *ltype, *rtype;
927 ltype = left->ctype;
928 rtype = right->ctype;
929 if (expr->op != '=') {
930 rtype = evaluate_binop_assignment(expr, left, right);
931 if (!rtype)
932 return NULL;
933 right = expr->right;
936 if (!lvalue_expression(left)) {
937 warn(expr->pos, "not an lvalue");
938 return NULL;
941 rtype = degenerate(right);
943 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
944 return NULL;
946 evaluate_assign_to(left, ltype);
948 expr->ctype = ltype;
949 return ltype;
952 static void examine_fn_arguments(struct symbol *fn)
954 struct symbol *s;
956 FOR_EACH_PTR(fn->arguments, s) {
957 struct symbol *arg = evaluate_symbol(s);
958 /* Array/function arguments silently degenerate into pointers */
959 if (arg) {
960 struct symbol *ptr;
961 switch(arg->type) {
962 case SYM_ARRAY:
963 case SYM_FN:
964 ptr = alloc_symbol(s->pos, SYM_PTR);
965 if (arg->type == SYM_ARRAY)
966 ptr->ctype = arg->ctype;
967 else
968 ptr->ctype.base_type = arg;
969 ptr->ctype.as |= s->ctype.as;
970 ptr->ctype.modifiers |= s->ctype.modifiers;
972 s->ctype.base_type = ptr;
973 s->ctype.as = 0;
974 s->ctype.modifiers = 0;
975 examine_symbol_type(s);
976 break;
977 default:
978 /* nothing */
979 break;
982 } END_FOR_EACH_PTR;
985 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
987 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
988 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
989 *newsym = *sym;
990 newsym->ctype.as = as;
991 newsym->ctype.modifiers = mod;
992 sym = newsym;
994 return sym;
997 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
999 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1000 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1002 node->ctype.base_type = ptr;
1003 ptr->bit_size = bits_in_pointer;
1004 ptr->ctype.alignment = pointer_alignment;
1006 node->bit_size = bits_in_pointer;
1007 node->ctype.alignment = pointer_alignment;
1009 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1010 if (sym->ctype.modifiers & MOD_REGISTER) {
1011 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1012 sym->ctype.modifiers &= ~MOD_REGISTER;
1014 if (sym->type == SYM_NODE) {
1015 ptr->ctype.as |= sym->ctype.as;
1016 ptr->ctype.modifiers |= sym->ctype.modifiers;
1017 sym = sym->ctype.base_type;
1019 if (degenerate && sym->type == SYM_ARRAY) {
1020 ptr->ctype.as |= sym->ctype.as;
1021 ptr->ctype.modifiers |= sym->ctype.modifiers;
1022 sym = sym->ctype.base_type;
1024 ptr->ctype.base_type = sym;
1026 return node;
1029 /* Arrays degenerate into pointers on pointer arithmetic */
1030 static struct symbol *degenerate(struct expression *expr)
1032 struct symbol *ctype, *base;
1034 if (!expr)
1035 return NULL;
1036 ctype = expr->ctype;
1037 if (!ctype)
1038 return NULL;
1039 base = ctype;
1040 if (ctype->type == SYM_NODE)
1041 base = ctype->ctype.base_type;
1043 * Arrays degenerate into pointers to the entries, while
1044 * functions degenerate into pointers to themselves
1046 switch (base->type) {
1047 case SYM_FN:
1048 case SYM_ARRAY:
1049 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1050 warn(expr->pos, "strange non-value function or array");
1051 return NULL;
1053 *expr = *expr->unop;
1054 ctype = create_pointer(expr, ctype, 1);
1055 expr->ctype = ctype;
1056 default:
1057 /* nothing */;
1059 return ctype;
1062 static struct symbol *evaluate_addressof(struct expression *expr)
1064 struct expression *op = expr->unop;
1065 struct symbol *ctype;
1067 if (op->op != '*' || op->type != EXPR_PREOP) {
1068 warn(expr->pos, "not addressable");
1069 return NULL;
1071 ctype = op->ctype;
1072 *expr = *op->unop;
1075 * symbol expression evaluation is lazy about the type
1076 * of the sub-expression, so we may have to generate
1077 * the type here if so..
1079 if (!expr->ctype) {
1080 ctype = create_pointer(expr, ctype, 0);
1081 expr->ctype = ctype;
1083 return expr->ctype;
1087 static struct symbol *evaluate_dereference(struct expression *expr)
1089 struct expression *op = expr->unop;
1090 struct symbol *ctype = op->ctype, *node, *target;
1092 /* Simplify: *&(expr) => (expr) */
1093 if (op->type == EXPR_PREOP && op->op == '&') {
1094 *expr = *op->unop;
1095 return expr->ctype;
1098 /* Dereferencing a node drops all the node information. */
1099 if (ctype->type == SYM_NODE)
1100 ctype = ctype->ctype.base_type;
1102 node = alloc_symbol(expr->pos, SYM_NODE);
1103 target = ctype->ctype.base_type;
1105 switch (ctype->type) {
1106 default:
1107 warn(expr->pos, "cannot derefence this type");
1108 return NULL;
1109 case SYM_PTR:
1110 merge_type(node, ctype);
1111 if (ctype->type != SYM_ARRAY)
1112 break;
1114 * Dereferencing a pointer to an array results in a
1115 * degenerate dereference: the expression becomes
1116 * just a pointer to the entry, and the derefence
1117 * goes away.
1119 *expr = *op;
1121 target = alloc_symbol(expr->pos, SYM_PTR);
1122 target->bit_size = bits_in_pointer;
1123 target->ctype.alignment = pointer_alignment;
1124 merge_type(target, ctype->ctype.base_type);
1125 break;
1127 case SYM_ARRAY:
1129 * When an array is dereferenced, we need to pick
1130 * up the attributes of the original node too..
1132 merge_type(node, op->ctype);
1133 merge_type(node, ctype);
1134 break;
1137 node->bit_size = target->bit_size;
1138 node->array_size = target->array_size;
1140 expr->ctype = node;
1141 return node;
1145 * Unary post-ops: x++ and x--
1147 static struct symbol *evaluate_postop(struct expression *expr)
1149 struct expression *op = expr->unop;
1150 struct symbol *ctype = op->ctype;
1152 if (!lvalue_expression(expr->unop)) {
1153 warn(expr->pos, "need lvalue expression for ++/--");
1154 return NULL;
1157 evaluate_assign_to(op, ctype);
1159 expr->ctype = ctype;
1160 return ctype;
1163 static struct symbol *evaluate_sign(struct expression *expr)
1165 struct symbol *ctype = expr->unop->ctype;
1166 if (is_int_type(ctype)) {
1167 struct symbol *rtype = rtype = integer_promotion(ctype);
1168 if (rtype->bit_size != ctype->bit_size)
1169 expr->unop = cast_to(expr->unop, rtype);
1170 ctype = rtype;
1171 } else if (is_float_type(ctype) && expr->op != '%') {
1172 /* no conversions needed */
1173 } else {
1174 return bad_expr_type(expr);
1176 if (expr->op == '+')
1177 *expr = *expr->unop;
1178 expr->ctype = ctype;
1179 return ctype;
1182 static struct symbol *evaluate_preop(struct expression *expr)
1184 struct symbol *ctype = expr->unop->ctype;
1186 switch (expr->op) {
1187 case '(':
1188 *expr = *expr->unop;
1189 return ctype;
1191 case '+':
1192 case '-':
1193 case '~':
1194 return evaluate_sign(expr);
1196 case '*':
1197 return evaluate_dereference(expr);
1199 case '&':
1200 return evaluate_addressof(expr);
1202 case SPECIAL_INCREMENT:
1203 case SPECIAL_DECREMENT:
1205 * From a type evaluation standpoint the pre-ops are
1206 * the same as the postops
1208 return evaluate_postop(expr);
1210 case '!':
1211 if (is_safe_type(ctype))
1212 warn(expr->pos, "testing a 'safe expression'");
1213 if (is_float_type(ctype)) {
1214 struct expression *arg = expr->unop;
1215 expr->type = EXPR_BINOP;
1216 expr->op = SPECIAL_EQUAL;
1217 expr->left = arg;
1218 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1219 expr->right->ctype = ctype;
1220 expr->right->fvalue = 0;
1222 ctype = &bool_ctype;
1223 break;
1225 default:
1226 break;
1228 expr->ctype = ctype;
1229 return &bool_ctype;
1232 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1234 struct ptr_list *head = (struct ptr_list *)_list;
1235 struct ptr_list *list = head;
1237 if (!head)
1238 return NULL;
1239 do {
1240 int i;
1241 for (i = 0; i < list->nr; i++) {
1242 struct symbol *sym = (struct symbol *) list->list[i];
1243 if (sym->ident) {
1244 if (sym->ident != ident)
1245 continue;
1246 *offset = sym->offset;
1247 return sym;
1248 } else {
1249 struct symbol *ctype = sym->ctype.base_type;
1250 struct symbol *sub;
1251 if (!ctype)
1252 continue;
1253 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1254 continue;
1255 sub = find_identifier(ident, ctype->symbol_list, offset);
1256 if (!sub)
1257 continue;
1258 *offset += sym->offset;
1259 return sub;
1262 } while ((list = list->next) != head);
1263 return NULL;
1266 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1268 struct expression *add;
1270 add = expr;
1271 if (offset) {
1272 /* Create a new add-expression */
1273 add = alloc_expression(expr->pos, EXPR_BINOP);
1274 add->op = '+';
1275 add->left = expr;
1276 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1277 add->right->ctype = &int_ctype;
1278 add->right->value = offset;
1282 * The ctype of the pointer will be lazily evaluated if
1283 * we ever take the address of this member dereference..
1285 add->ctype = NULL;
1286 return add;
1289 /* structure/union dereference */
1290 static struct symbol *evaluate_member_dereference(struct expression *expr)
1292 int offset;
1293 struct symbol *ctype, *member;
1294 struct expression *deref = expr->deref, *add;
1295 struct ident *ident = expr->member;
1296 unsigned int mod;
1297 int address_space;
1299 if (!evaluate_expression(deref))
1300 return NULL;
1301 if (!ident) {
1302 warn(expr->pos, "bad member name");
1303 return NULL;
1306 ctype = deref->ctype;
1307 address_space = ctype->ctype.as;
1308 mod = ctype->ctype.modifiers;
1309 if (ctype->type == SYM_NODE) {
1310 ctype = ctype->ctype.base_type;
1311 address_space |= ctype->ctype.as;
1312 mod |= ctype->ctype.modifiers;
1314 if (!lvalue_expression(deref)) {
1315 warn(deref->pos, "expected lvalue for member dereference");
1316 return NULL;
1318 deref = deref->unop;
1319 expr->deref = deref;
1320 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1321 warn(expr->pos, "expected structure or union");
1322 return NULL;
1324 offset = 0;
1325 member = find_identifier(ident, ctype->symbol_list, &offset);
1326 if (!member) {
1327 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1328 const char *name = "<unnamed>";
1329 int namelen = 9;
1330 if (ctype->ident) {
1331 name = ctype->ident->name;
1332 namelen = ctype->ident->len;
1334 warn(expr->pos, "no member '%s' in %s %.*s",
1335 show_ident(ident), type, namelen, name);
1336 return NULL;
1340 * The member needs to take on the address space and modifiers of
1341 * the "parent" type.
1343 member = convert_to_as_mod(member, address_space, mod);
1344 add = evaluate_offset(deref, offset);
1346 ctype = member->ctype.base_type;
1347 if (ctype->type == SYM_BITFIELD) {
1348 expr->type = EXPR_BITFIELD;
1349 expr->bitpos = member->bit_offset;
1350 expr->nrbits = member->fieldwidth;
1351 expr->address = add;
1352 } else {
1353 expr->type = EXPR_PREOP;
1354 expr->op = '*';
1355 expr->unop = add;
1358 expr->ctype = member;
1359 return member;
1362 static struct symbol *evaluate_sizeof(struct expression *expr)
1364 int size;
1366 if (expr->cast_type) {
1367 examine_symbol_type(expr->cast_type);
1368 size = expr->cast_type->bit_size;
1369 } else {
1370 if (!evaluate_expression(expr->cast_expression))
1371 return NULL;
1372 size = expr->cast_expression->ctype->bit_size;
1374 if (size & 7) {
1375 warn(expr->pos, "cannot size expression");
1376 return NULL;
1378 expr->type = EXPR_VALUE;
1379 expr->value = size >> 3;
1380 expr->ctype = size_t_ctype;
1381 return size_t_ctype;
1384 static struct symbol *evaluate_alignof(struct expression *expr)
1386 struct symbol *type = expr->cast_type;
1388 if (!type) {
1389 type = evaluate_expression(expr->cast_expression);
1390 if (!type)
1391 return NULL;
1393 examine_symbol_type(type);
1394 expr->type = EXPR_VALUE;
1395 expr->value = type->ctype.alignment;
1396 expr->ctype = size_t_ctype;
1397 return size_t_ctype;
1400 static int context_clash(struct symbol *sym1, struct symbol *sym2)
1402 unsigned long clash = (sym1->ctype.context ^ sym2->ctype.context);
1403 clash &= (sym1->ctype.contextmask & sym2->ctype.contextmask);
1404 return clash != 0;
1407 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1409 struct expression *expr;
1410 struct symbol_list *argument_types = fn->arguments;
1411 struct symbol *argtype;
1412 int i = 1;
1414 PREPARE_PTR_LIST(argument_types, argtype);
1415 FOR_EACH_PTR (head, expr) {
1416 struct expression **p = THIS_ADDRESS(expr);
1417 struct symbol *ctype, *target;
1418 ctype = evaluate_expression(expr);
1420 if (!ctype)
1421 return 0;
1423 if (context_clash(f, ctype))
1424 warn(expr->pos, "argument %d used in wrong context", i);
1426 ctype = degenerate(expr);
1428 target = argtype;
1429 if (!target && ctype->bit_size < bits_in_int)
1430 target = &int_ctype;
1431 if (target) {
1432 static char where[30];
1433 examine_symbol_type(target);
1434 sprintf(where, "argument %d", i);
1435 compatible_assignment_types(expr, target, p, ctype, where);
1438 i++;
1439 NEXT_PTR_LIST(argtype);
1440 } END_FOR_EACH_PTR;
1441 FINISH_PTR_LIST(argtype);
1442 return 1;
1445 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset);
1446 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1448 struct expression *entry;
1449 int current = 0;
1450 int max = 0;
1452 FOR_EACH_PTR(expr->expr_list, entry) {
1453 struct expression **p = THIS_ADDRESS(entry);
1455 if (entry->type == EXPR_INDEX) {
1456 current = entry->idx_to;
1457 continue;
1459 evaluate_initializer(ctype, p, offset + current*(ctype->bit_size>>3));
1460 current++;
1461 if (current > max)
1462 max = current;
1463 } END_FOR_EACH_PTR;
1464 return max;
1467 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1468 static int evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr, unsigned long offset)
1470 if (offset || expression_list_size(expr->expr_list) != 1) {
1471 warn(expr->pos, "unexpected compound initializer");
1472 return 0;
1474 return evaluate_array_initializer(ctype, expr, 0);
1477 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple, unsigned long offset)
1479 struct expression *entry;
1480 struct symbol *sym;
1482 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1483 FOR_EACH_PTR(expr->expr_list, entry) {
1484 struct expression **p = THIS_ADDRESS(entry);
1486 if (entry->type == EXPR_IDENTIFIER) {
1487 struct ident *ident = entry->expr_ident;
1488 /* We special-case the "already right place" case */
1489 if (sym && sym->ident == ident)
1490 continue;
1491 RESET_PTR_LIST(sym);
1492 for (;;) {
1493 if (!sym) {
1494 warn(entry->pos, "unknown named initializer '%s'", show_ident(ident));
1495 return 0;
1497 if (sym->ident == ident)
1498 break;
1499 NEXT_PTR_LIST(sym);
1501 continue;
1504 if (!sym) {
1505 warn(expr->pos, "too many initializers for struct/union");
1506 return 0;
1509 evaluate_initializer(sym, p, offset + sym->offset);
1511 NEXT_PTR_LIST(sym);
1512 } END_FOR_EACH_PTR;
1513 FINISH_PTR_LIST(sym);
1515 return 0;
1519 * Initializers are kind of like assignments. Except
1520 * they can be a hell of a lot more complex.
1522 static int evaluate_initializer(struct symbol *ctype, struct expression **ep, unsigned long offset)
1524 struct expression *expr = *ep;
1527 * Simple non-structure/array initializers are the simple
1528 * case, and look (and parse) largely like assignments.
1530 if (expr->type != EXPR_INITIALIZER) {
1531 int size = 0;
1532 struct symbol *rtype = evaluate_expression(expr);
1533 if (rtype) {
1534 struct expression *pos;
1536 // FIXME! char array[] = "string" special case
1537 // should _not_ degenerate.
1538 rtype = degenerate(expr);
1539 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1540 /* strings are special: char arrays */
1541 if (rtype->type == SYM_ARRAY)
1542 size = get_expression_value(rtype->array_size);
1544 * Don't bother creating a position expression for
1545 * the simple initializer cases that don't need it.
1547 * We need a position if the initializer has a byte
1548 * offset, _or_ if we're initializing a bitfield.
1550 if (offset || ctype->fieldwidth) {
1551 pos = alloc_expression(expr->pos, EXPR_POS);
1552 pos->init_offset = offset;
1553 pos->init_sym = ctype;
1554 pos->init_expr = *ep;
1555 pos->ctype = expr->ctype;
1556 *ep = pos;
1559 return size;
1562 expr->ctype = ctype;
1563 if (ctype->type == SYM_NODE)
1564 ctype = ctype->ctype.base_type;
1566 switch (ctype->type) {
1567 case SYM_ARRAY:
1568 case SYM_PTR:
1569 return evaluate_array_initializer(ctype->ctype.base_type, expr, offset);
1570 case SYM_UNION:
1571 return evaluate_struct_or_union_initializer(ctype, expr, 0, offset);
1572 case SYM_STRUCT:
1573 return evaluate_struct_or_union_initializer(ctype, expr, 1, offset);
1574 default:
1575 return evaluate_scalar_initializer(ctype, expr, offset);
1579 static int get_as(struct symbol *sym)
1581 int as;
1582 unsigned long mod;
1584 if (!sym)
1585 return 0;
1586 as = sym->ctype.as;
1587 mod = sym->ctype.modifiers;
1588 if (sym->type == SYM_NODE) {
1589 sym = sym->ctype.base_type;
1590 as |= sym->ctype.as;
1591 mod |= sym->ctype.modifiers;
1594 * You can always throw a value away by casting to
1595 * "void" - that's an implicit "force". Note that
1596 * the same is _not_ true of "void *".
1598 if (sym == &void_ctype)
1599 return -1;
1602 * At least for now, allow casting to a "unsigned long".
1603 * That's how we do things like pointer arithmetic and
1604 * store pointers to registers.
1606 if (sym == &ulong_ctype)
1607 return -1;
1609 if (sym && sym->type == SYM_PTR) {
1610 sym = sym->ctype.base_type;
1611 as |= sym->ctype.as;
1612 mod |= sym->ctype.modifiers;
1614 if (mod & MOD_FORCE)
1615 return -1;
1616 return as;
1619 static struct symbol *evaluate_cast(struct expression *expr)
1621 struct expression *target = expr->cast_expression;
1622 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1624 expr->ctype = ctype;
1625 expr->cast_type = ctype;
1628 * Special case: a cast can be followed by an
1629 * initializer, in which case we need to pass
1630 * the type value down to that initializer rather
1631 * than trying to evaluate it as an expression
1633 * A more complex case is when the initializer is
1634 * dereferenced as part of a post-fix expression.
1635 * We need to produce an expression that can be dereferenced.
1637 if (target->type == EXPR_INITIALIZER) {
1638 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
1639 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
1641 sym->ctype.base_type = ctype;
1642 sym->initializer = expr->cast_expression;
1643 evaluate_symbol(sym);
1645 addr->ctype = NULL; /* Lazy eval */
1646 addr->symbol = sym;
1648 expr->type = EXPR_PREOP;
1649 expr->op = '*';
1650 expr->unop = addr;
1651 expr->ctype = ctype;
1652 return ctype;
1655 evaluate_expression(target);
1656 degenerate(target);
1658 if (!get_as(ctype) && get_as(target->ctype) > 0)
1659 warn(expr->pos, "cast removes address space of expression");
1662 * Casts of constant values are special: they
1663 * can be NULL, and thus need to be simplified
1664 * early.
1666 if (target->type == EXPR_VALUE)
1667 cast_value(expr, ctype, target, target->ctype);
1669 return ctype;
1673 * Evaluate a call expression with a symbol. This
1674 * should expand inline functions, and evaluate
1675 * builtins.
1677 static int evaluate_symbol_call(struct expression *expr)
1679 struct expression *fn = expr->fn;
1680 struct symbol *ctype = fn->ctype;
1682 if (fn->type != EXPR_PREOP)
1683 return 0;
1685 if (ctype->op && ctype->op->evaluate)
1686 return ctype->op->evaluate(expr);
1688 if (ctype->ctype.modifiers & MOD_INLINE) {
1689 int ret;
1690 struct symbol *curr = current_fn;
1691 unsigned long context = current_context;
1692 unsigned long mask = current_contextmask;
1694 current_context |= ctype->ctype.context;
1695 current_contextmask |= ctype->ctype.contextmask;
1696 current_fn = ctype->ctype.base_type;
1697 examine_fn_arguments(current_fn);
1699 ret = inline_function(expr, ctype);
1701 /* restore the old function context */
1702 current_fn = curr;
1703 current_context = context;
1704 current_contextmask = mask;
1705 return ret;
1708 return 0;
1711 static struct symbol *evaluate_call(struct expression *expr)
1713 int args, fnargs;
1714 struct symbol *ctype, *sym;
1715 struct expression *fn = expr->fn;
1716 struct expression_list *arglist = expr->args;
1718 if (!evaluate_expression(fn))
1719 return NULL;
1720 sym = ctype = fn->ctype;
1721 if (ctype->type == SYM_NODE)
1722 ctype = ctype->ctype.base_type;
1723 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1724 ctype = ctype->ctype.base_type;
1725 if (!evaluate_arguments(sym, ctype, arglist))
1726 return NULL;
1727 if (ctype->type != SYM_FN) {
1728 warn(expr->pos, "not a function %s", show_ident(sym->ident));
1729 return NULL;
1731 args = expression_list_size(expr->args);
1732 fnargs = symbol_list_size(ctype->arguments);
1733 if (args < fnargs)
1734 warn(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
1735 if (args > fnargs && !ctype->variadic)
1736 warn(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
1737 if (sym->type == SYM_NODE) {
1738 if (evaluate_symbol_call(expr))
1739 return expr->ctype;
1741 expr->ctype = ctype->ctype.base_type;
1742 return expr->ctype;
1745 struct symbol *evaluate_expression(struct expression *expr)
1747 if (!expr)
1748 return NULL;
1749 if (expr->ctype)
1750 return expr->ctype;
1752 switch (expr->type) {
1753 case EXPR_VALUE:
1754 case EXPR_FVALUE:
1755 warn(expr->pos, "value expression without a type");
1756 return NULL;
1757 case EXPR_STRING:
1758 return evaluate_string(expr);
1759 case EXPR_SYMBOL:
1760 return evaluate_symbol_expression(expr);
1761 case EXPR_BINOP:
1762 if (!evaluate_expression(expr->left))
1763 return NULL;
1764 if (!evaluate_expression(expr->right))
1765 return NULL;
1766 return evaluate_binop(expr);
1767 case EXPR_LOGICAL:
1768 return evaluate_logical(expr);
1769 case EXPR_COMMA:
1770 if (!evaluate_expression(expr->left))
1771 return NULL;
1772 if (!evaluate_expression(expr->right))
1773 return NULL;
1774 return evaluate_comma(expr);
1775 case EXPR_COMPARE:
1776 if (!evaluate_expression(expr->left))
1777 return NULL;
1778 if (!evaluate_expression(expr->right))
1779 return NULL;
1780 return evaluate_compare(expr);
1781 case EXPR_ASSIGNMENT:
1782 if (!evaluate_expression(expr->left))
1783 return NULL;
1784 if (!evaluate_expression(expr->right))
1785 return NULL;
1786 return evaluate_assignment(expr);
1787 case EXPR_PREOP:
1788 if (!evaluate_expression(expr->unop))
1789 return NULL;
1790 return evaluate_preop(expr);
1791 case EXPR_POSTOP:
1792 if (!evaluate_expression(expr->unop))
1793 return NULL;
1794 return evaluate_postop(expr);
1795 case EXPR_CAST:
1796 return evaluate_cast(expr);
1797 case EXPR_SIZEOF:
1798 return evaluate_sizeof(expr);
1799 case EXPR_ALIGNOF:
1800 return evaluate_alignof(expr);
1801 case EXPR_DEREF:
1802 return evaluate_member_dereference(expr);
1803 case EXPR_CALL:
1804 return evaluate_call(expr);
1805 case EXPR_BITFIELD:
1806 warn(expr->pos, "bitfield generated by parser");
1807 return NULL;
1808 case EXPR_CONDITIONAL:
1809 if (!evaluate_conditional(&expr->conditional))
1810 return NULL;
1811 if (!evaluate_expression(expr->cond_false))
1812 return NULL;
1813 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1814 return NULL;
1815 return evaluate_conditional_expression(expr);
1816 case EXPR_STATEMENT:
1817 expr->ctype = evaluate_statement(expr->statement);
1818 return expr->ctype;
1820 case EXPR_LABEL:
1821 expr->ctype = &ptr_ctype;
1822 return &ptr_ctype;
1824 case EXPR_TYPE:
1825 /* Evaluate the type of the symbol .. */
1826 evaluate_symbol(expr->symbol);
1827 /* .. but the type of the _expression_ is a "type" */
1828 expr->ctype = &type_ctype;
1829 return &type_ctype;
1831 /* These can not exist as stand-alone expressions */
1832 case EXPR_INITIALIZER:
1833 case EXPR_IDENTIFIER:
1834 case EXPR_INDEX:
1835 case EXPR_POS:
1836 warn(expr->pos, "internal front-end error: initializer in expression");
1837 return NULL;
1839 return NULL;
1842 void check_duplicates(struct symbol *sym)
1844 struct symbol *next = sym;
1846 while ((next = next->same_symbol) != NULL) {
1847 const char *typediff;
1848 evaluate_symbol(next);
1849 typediff = type_difference(sym, next, 0, 0);
1850 if (typediff) {
1851 warn(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1852 show_ident(sym->ident),
1853 input_streams[next->pos.stream].name, next->pos.line, typediff);
1854 return;
1859 struct symbol *evaluate_symbol(struct symbol *sym)
1861 struct symbol *base_type;
1863 if (!sym)
1864 return sym;
1866 sym = examine_symbol_type(sym);
1867 base_type = sym->ctype.base_type;
1868 if (!base_type)
1869 return NULL;
1871 /* Evaluate the initializers */
1872 if (sym->initializer) {
1873 int count = evaluate_initializer(sym, &sym->initializer, 0);
1874 if (base_type->type == SYM_ARRAY && !base_type->array_size) {
1875 int bit_size = count * base_type->ctype.base_type->bit_size;
1876 base_type->array_size = alloc_const_expression(sym->pos, count);
1877 base_type->bit_size = bit_size;
1878 sym->array_size = base_type->array_size;
1879 sym->bit_size = bit_size;
1883 /* And finally, evaluate the body of the symbol too */
1884 if (base_type->type == SYM_FN) {
1885 examine_fn_arguments(base_type);
1886 if (base_type->stmt) {
1887 current_fn = base_type;
1888 current_contextmask = sym->ctype.contextmask;
1889 current_context = sym->ctype.context;
1890 evaluate_statement(base_type->stmt);
1894 return base_type;
1897 struct symbol *evaluate_return_expression(struct statement *stmt)
1899 struct expression *expr = stmt->expression;
1900 struct symbol *ctype, *fntype;
1902 evaluate_expression(expr);
1903 ctype = degenerate(expr);
1904 fntype = current_fn->ctype.base_type;
1905 if (!fntype || fntype == &void_ctype) {
1906 if (expr && ctype != &void_ctype)
1907 warn(expr->pos, "return expression in %s function", fntype?"void":"typeless");
1908 return NULL;
1911 if (!expr) {
1912 warn(stmt->pos, "return with no return value");
1913 return NULL;
1915 if (!ctype)
1916 return NULL;
1917 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression");
1918 return NULL;
1921 static void evaluate_if_statement(struct statement *stmt)
1923 struct symbol *ctype;
1925 if (!stmt->if_conditional)
1926 return;
1928 ctype = evaluate_conditional(&stmt->if_conditional);
1929 if (!ctype)
1930 return;
1932 evaluate_statement(stmt->if_true);
1933 evaluate_statement(stmt->if_false);
1936 struct symbol *evaluate_statement(struct statement *stmt)
1938 if (!stmt)
1939 return NULL;
1941 switch (stmt->type) {
1942 case STMT_RETURN:
1943 return evaluate_return_expression(stmt);
1945 case STMT_EXPRESSION:
1946 evaluate_expression(stmt->expression);
1947 return degenerate(stmt->expression);
1949 case STMT_COMPOUND: {
1950 struct statement *s;
1951 struct symbol *type = NULL;
1952 struct symbol *sym;
1954 /* Evaluate each symbol in the compound statement */
1955 FOR_EACH_PTR(stmt->syms, sym) {
1956 evaluate_symbol(sym);
1957 } END_FOR_EACH_PTR;
1958 evaluate_symbol(stmt->ret);
1961 * Then, evaluate each statement, making the type of the
1962 * compound statement be the type of the last statement
1964 type = NULL;
1965 FOR_EACH_PTR(stmt->stmts, s) {
1966 type = evaluate_statement(s);
1967 } END_FOR_EACH_PTR;
1968 if (!type)
1969 type = &void_ctype;
1970 return type;
1972 case STMT_IF:
1973 evaluate_if_statement(stmt);
1974 return NULL;
1975 case STMT_ITERATOR:
1976 evaluate_conditional(&stmt->iterator_pre_condition);
1977 evaluate_conditional(&stmt->iterator_post_condition);
1978 evaluate_statement(stmt->iterator_pre_statement);
1979 evaluate_statement(stmt->iterator_statement);
1980 evaluate_statement(stmt->iterator_post_statement);
1981 return NULL;
1982 case STMT_SWITCH:
1983 evaluate_expression(stmt->switch_expression);
1984 evaluate_statement(stmt->switch_statement);
1985 return NULL;
1986 case STMT_CASE:
1987 evaluate_expression(stmt->case_expression);
1988 evaluate_expression(stmt->case_to);
1989 evaluate_statement(stmt->case_statement);
1990 return NULL;
1991 case STMT_LABEL:
1992 return evaluate_statement(stmt->label_statement);
1993 case STMT_GOTO:
1994 evaluate_expression(stmt->goto_expression);
1995 return NULL;
1996 case STMT_NONE:
1997 break;
1998 case STMT_ASM:
1999 /* FIXME! Do the asm parameter evaluation! */
2000 break;
2002 return NULL;