Warn about sizeof of zero size.
[smatch.git] / evaluate.c
blob9e060545a7c25951c883b8b6286f30b2c850ab8c
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 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 "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 struct symbol *current_fn;
31 static struct symbol *degenerate(struct expression *expr);
32 static struct symbol *evaluate_symbol(struct symbol *sym);
34 static struct symbol *evaluate_symbol_expression(struct expression *expr)
36 struct symbol *sym = expr->symbol;
37 struct symbol *base_type;
39 if (!sym) {
40 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
41 return NULL;
44 examine_symbol_type(sym);
46 base_type = sym->ctype.base_type;
47 if (!base_type) {
48 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
49 return NULL;
52 /* The type of a symbol is the symbol itself! */
53 expr->ctype = sym;
55 /* enums can be turned into plain values */
56 if (sym->type != SYM_ENUM) {
57 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
58 addr->symbol = sym;
59 addr->symbol_name = expr->symbol_name;
60 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
61 expr->type = EXPR_PREOP;
62 expr->op = '*';
63 expr->unop = addr;
64 return sym;
65 } else if (base_type->bit_size < bits_in_int) {
66 /* ugly - we need to force sizeof for these guys */
67 struct expression *e = alloc_expression(expr->pos, EXPR_VALUE);
68 e->value = sym->value;
69 e->ctype = base_type;
70 expr->type = EXPR_PREOP;
71 expr->op = '+';
72 expr->unop = e;
73 } else {
74 expr->type = EXPR_VALUE;
75 expr->value = sym->value;
77 expr->ctype = base_type;
78 return base_type;
81 static struct symbol *evaluate_string(struct expression *expr)
83 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
84 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
85 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
86 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
87 unsigned int length = expr->string->length;
89 sym->array_size = alloc_const_expression(expr->pos, length);
90 sym->bit_size = bits_in_char * length;
91 sym->ctype.alignment = 1;
92 sym->ctype.modifiers = MOD_STATIC;
93 sym->ctype.base_type = array;
94 sym->initializer = initstr;
96 initstr->ctype = sym;
97 initstr->string = expr->string;
99 array->array_size = sym->array_size;
100 array->bit_size = bits_in_char * length;
101 array->ctype.alignment = 1;
102 array->ctype.modifiers = MOD_STATIC;
103 array->ctype.base_type = &char_ctype;
105 addr->symbol = sym;
106 addr->ctype = &lazy_ptr_ctype;
108 expr->type = EXPR_PREOP;
109 expr->op = '*';
110 expr->unop = addr;
111 expr->ctype = sym;
112 return sym;
115 static inline struct symbol *integer_promotion(struct symbol *type)
117 unsigned long mod = type->ctype.modifiers;
118 int width;
120 if (type->type == SYM_NODE)
121 type = type->ctype.base_type;
122 if (type->type == SYM_ENUM)
123 type = type->ctype.base_type;
124 width = type->bit_size;
125 if (type->type == SYM_BITFIELD)
126 type = type->ctype.base_type;
127 mod = type->ctype.modifiers;
128 if (width < bits_in_int)
129 return &int_ctype;
131 /* If char/short has as many bits as int, it still gets "promoted" */
132 if (mod & (MOD_CHAR | MOD_SHORT)) {
133 type = &int_ctype;
134 if (mod & MOD_UNSIGNED)
135 type = &uint_ctype;
137 return type;
141 * integer part of usual arithmetic conversions:
142 * integer promotions are applied
143 * if left and right are identical, we are done
144 * if signedness is the same, convert one with lower rank
145 * unless unsigned argument has rank lower than signed one, convert the
146 * signed one.
147 * if signed argument is bigger than unsigned one, convert the unsigned.
148 * otherwise, convert signed.
150 * Leaving aside the integer promotions, that is equivalent to
151 * if identical, don't convert
152 * if left is bigger than right, convert right
153 * if right is bigger than left, convert right
154 * otherwise, if signedness is the same, convert one with lower rank
155 * otherwise convert the signed one.
157 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
159 unsigned long lmod, rmod;
161 left = integer_promotion(left);
162 right = integer_promotion(right);
164 if (left == right)
165 goto left;
167 if (left->bit_size > right->bit_size)
168 goto left;
170 if (right->bit_size > left->bit_size)
171 goto right;
173 lmod = left->ctype.modifiers;
174 rmod = right->ctype.modifiers;
175 if ((lmod ^ rmod) & MOD_UNSIGNED) {
176 if (lmod & MOD_UNSIGNED)
177 goto left;
178 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
179 goto left;
180 right:
181 left = right;
182 left:
183 return left;
186 static int same_cast_type(struct symbol *orig, struct symbol *new)
188 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
191 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
193 unsigned long mod, as;
195 mod = 0; as = 0;
196 while (node) {
197 mod |= node->ctype.modifiers;
198 as |= node->ctype.as;
199 switch (node->type) {
200 case SYM_NODE:
201 case SYM_ENUM:
202 node = node->ctype.base_type;
203 continue;
204 default:
205 break;
207 break;
209 *modp = mod & ~MOD_IGNORE;
210 *asp = as;
211 return node;
214 static int is_same_type(struct symbol *old, struct symbol *new)
216 unsigned long oldmod, newmod, oldas, newas;
218 old = base_type(old, &oldmod, &oldas);
219 new = base_type(new, &newmod, &newas);
220 return old == new && oldmod == newmod && oldas == newas;
224 * This gets called for implicit casts in assignments and
225 * integer promotion. We often want to try to move the
226 * cast down, because the ops involved may have been
227 * implicitly cast up, and we can get rid of the casts
228 * early.
230 static struct expression * cast_to(struct expression *old, struct symbol *type)
232 struct expression *expr;
234 if (is_same_type(old->ctype, type))
235 return old;
238 * See if we can simplify the op. Move the cast down.
240 switch (old->type) {
241 case EXPR_PREOP:
242 if (old->op == '~') {
243 old->ctype = type;
244 old->unop = cast_to(old->unop, type);
245 return old;
247 break;
249 case EXPR_IMPLIED_CAST:
250 if (old->ctype->bit_size >= type->bit_size) {
251 struct expression *orig = old->cast_expression;
252 if (same_cast_type(orig->ctype, type))
253 return orig;
254 if (old->ctype->bit_offset == type->bit_offset) {
255 old->ctype = type;
256 old->cast_type = type;
257 return old;
260 break;
262 default:
263 /* nothing */;
266 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
267 expr->ctype = type;
268 expr->cast_type = type;
269 expr->cast_expression = old;
270 return expr;
273 static int is_type_type(struct symbol *type)
275 return (type->ctype.modifiers & MOD_TYPE) != 0;
278 int is_ptr_type(struct symbol *type)
280 if (type->type == SYM_NODE)
281 type = type->ctype.base_type;
282 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
285 static inline int is_float_type(struct symbol *type)
287 if (type->type == SYM_NODE)
288 type = type->ctype.base_type;
289 return type->ctype.base_type == &fp_type;
292 static inline int is_byte_type(struct symbol *type)
294 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
297 static inline int is_string_type(struct symbol *type)
299 if (type->type == SYM_NODE)
300 type = type->ctype.base_type;
301 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
304 static struct symbol *bad_expr_type(struct expression *expr)
306 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
307 switch (expr->type) {
308 case EXPR_BINOP:
309 case EXPR_COMPARE:
310 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
311 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
312 break;
313 case EXPR_PREOP:
314 case EXPR_POSTOP:
315 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
316 break;
317 default:
318 break;
321 return NULL;
324 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
326 struct expression *left = *lp, *right = *rp;
327 struct symbol *ltype = left->ctype, *rtype = right->ctype;
329 if (ltype->type == SYM_NODE)
330 ltype = ltype->ctype.base_type;
331 if (rtype->type == SYM_NODE)
332 rtype = rtype->ctype.base_type;
333 if (is_float_type(ltype)) {
334 if (is_int_type(rtype))
335 goto Left;
336 if (is_float_type(rtype)) {
337 unsigned long lmod = ltype->ctype.modifiers;
338 unsigned long rmod = rtype->ctype.modifiers;
339 lmod &= MOD_LONG | MOD_LONGLONG;
340 rmod &= MOD_LONG | MOD_LONGLONG;
341 if (lmod == rmod)
342 return ltype;
343 if (lmod & ~rmod)
344 goto Left;
345 else
346 goto Right;
348 return NULL;
350 if (!is_float_type(rtype) || !is_int_type(ltype))
351 return NULL;
352 Right:
353 *lp = cast_to(left, rtype);
354 return rtype;
355 Left:
356 *rp = cast_to(right, ltype);
357 return ltype;
360 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
362 struct expression *left = *lp, *right = *rp;
363 struct symbol *ltype = left->ctype, *rtype = right->ctype;
365 if (ltype->type == SYM_NODE)
366 ltype = ltype->ctype.base_type;
367 if (rtype->type == SYM_NODE)
368 rtype = rtype->ctype.base_type;
369 if (is_int_type(ltype) && is_int_type(rtype)) {
370 struct symbol *ctype = bigger_int_type(ltype, rtype);
372 *lp = cast_to(left, ctype);
373 *rp = cast_to(right, ctype);
374 return ctype;
376 return NULL;
379 static int restricted_value(struct expression *v, struct symbol *type)
381 if (v->type != EXPR_VALUE)
382 return 1;
383 if (v->value != 0)
384 return 1;
385 return 0;
388 static int restricted_binop(int op, struct symbol *type)
390 switch (op) {
391 case '&':
392 case '|':
393 case '^':
394 case '?':
395 case '=':
396 case SPECIAL_EQUAL:
397 case SPECIAL_NOTEQUAL:
398 case SPECIAL_AND_ASSIGN:
399 case SPECIAL_OR_ASSIGN:
400 case SPECIAL_XOR_ASSIGN:
401 return 0;
402 default:
403 return 1;
407 static int restricted_unop(int op, struct symbol *type)
409 if (op == '~' && type->bit_size >= bits_in_int)
410 return 0;
411 if (op == '+')
412 return 0;
413 return 1;
416 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
418 struct expression *left = *lp, *right = *rp;
419 struct symbol *ltype = left->ctype, *rtype = right->ctype;
420 struct symbol *type = NULL;
422 if (ltype->type == SYM_NODE)
423 ltype = ltype->ctype.base_type;
424 if (ltype->type == SYM_ENUM)
425 ltype = ltype->ctype.base_type;
426 if (rtype->type == SYM_NODE)
427 rtype = rtype->ctype.base_type;
428 if (rtype->type == SYM_ENUM)
429 rtype = rtype->ctype.base_type;
430 if (is_restricted_type(ltype)) {
431 if (is_restricted_type(rtype)) {
432 if (ltype == rtype)
433 type = ltype;
434 } else {
435 if (!restricted_value(right, ltype))
436 type = ltype;
438 } else if (is_restricted_type(rtype)) {
439 if (!restricted_value(left, rtype))
440 type = rtype;
442 if (!type)
443 return NULL;
444 if (restricted_binop(op, type))
445 return NULL;
446 return type;
449 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
451 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
452 if (!ctype && float_ok)
453 ctype = compatible_float_binop(&expr->left, &expr->right);
454 if (!ctype)
455 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
456 if (ctype) {
457 expr->ctype = ctype;
458 return ctype;
460 return bad_expr_type(expr);
463 static inline int lvalue_expression(struct expression *expr)
465 return expr->type == EXPR_PREOP && expr->op == '*';
468 static int ptr_object_size(struct symbol *ptr_type)
470 if (ptr_type->type == SYM_NODE)
471 ptr_type = ptr_type->ctype.base_type;
472 if (ptr_type->type == SYM_PTR)
473 ptr_type = ptr_type->ctype.base_type;
474 return ptr_type->bit_size;
477 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
479 struct expression *i = *ip;
480 struct symbol *ptr_type = ctype;
481 int bit_size;
483 if (ptr_type->type == SYM_NODE)
484 ptr_type = ptr_type->ctype.base_type;
486 if (!is_int_type(i->ctype))
487 return bad_expr_type(expr);
489 examine_symbol_type(ctype);
491 if (!ctype->ctype.base_type) {
492 warning(expr->pos, "missing type information");
493 return NULL;
496 /* Get the size of whatever the pointer points to */
497 bit_size = ptr_object_size(ctype);
499 if (bit_size > bits_in_char) {
500 int multiply = bit_size >> 3;
501 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
503 if (i->type == EXPR_VALUE) {
504 val->value = i->value * multiply;
505 val->ctype = size_t_ctype;
506 *ip = val;
507 } else {
508 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
510 val->ctype = size_t_ctype;
511 val->value = bit_size >> 3;
513 mul->op = '*';
514 mul->ctype = size_t_ctype;
515 mul->left = i;
516 mul->right = val;
518 *ip = mul;
522 expr->ctype = ctype;
523 return ctype;
526 static struct symbol *evaluate_add(struct expression *expr)
528 struct expression *left = expr->left, *right = expr->right;
529 struct symbol *ltype = left->ctype, *rtype = right->ctype;
531 if (is_ptr_type(ltype))
532 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
534 if (is_ptr_type(rtype))
535 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
537 return evaluate_arith(expr, 1);
540 const char * type_difference(struct symbol *target, struct symbol *source,
541 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
543 for (;;) {
544 unsigned long mod1, mod2, diff;
545 unsigned long as1, as2;
546 int type1, type2;
547 struct symbol *base1, *base2;
549 if (target == source)
550 break;
551 if (!target || !source)
552 return "different types";
554 * Peel of per-node information.
555 * FIXME! Check alignment and context too here!
557 mod1 = target->ctype.modifiers;
558 as1 = target->ctype.as;
559 mod2 = source->ctype.modifiers;
560 as2 = source->ctype.as;
561 if (target->type == SYM_NODE) {
562 target = target->ctype.base_type;
563 if (!target)
564 return "bad types";
565 if (target->type == SYM_PTR) {
566 mod1 = 0;
567 as1 = 0;
569 mod1 |= target->ctype.modifiers;
570 as1 |= target->ctype.as;
572 if (source->type == SYM_NODE) {
573 source = source->ctype.base_type;
574 if (!source)
575 return "bad types";
576 if (source->type == SYM_PTR) {
577 mod2 = 0;
578 as2 = 0;
580 mod2 |= source->ctype.modifiers;
581 as2 |= source->ctype.as;
583 if (target->type == SYM_ENUM) {
584 target = target->ctype.base_type;
585 if (!target)
586 return "bad types";
588 if (source->type == SYM_ENUM) {
589 source = source->ctype.base_type;
590 if (!source)
591 return "bad types";
594 if (target == source)
595 break;
596 if (!target || !source)
597 return "different types";
599 type1 = target->type;
600 base1 = target->ctype.base_type;
602 type2 = source->type;
603 base2 = source->ctype.base_type;
606 * Pointers to functions compare as the function itself
608 if (type1 == SYM_PTR && base1) {
609 switch (base1->type) {
610 case SYM_FN:
611 type1 = SYM_FN;
612 target = base1;
613 base1 = base1->ctype.base_type;
614 default:
615 /* nothing */;
618 if (type2 == SYM_PTR && base2) {
619 switch (base2->type) {
620 case SYM_FN:
621 type2 = SYM_FN;
622 source = base2;
623 base2 = base2->ctype.base_type;
624 default:
625 /* nothing */;
629 /* Arrays degenerate to pointers for type comparisons */
630 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
631 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
633 if (type1 != type2 || type1 == SYM_RESTRICT)
634 return "different base types";
636 /* Must be same address space to be comparable */
637 if (as1 != as2)
638 return "different address spaces";
640 /* Ignore differences in storage types or addressability */
641 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
642 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
643 if (diff) {
644 if (diff & MOD_SIZE)
645 return "different type sizes";
646 if (diff & ~MOD_SIGNEDNESS)
647 return "different modifiers";
649 /* Differs in signedness only.. */
650 if (Wtypesign) {
652 * Warn if both are explicitly signed ("unsigned" is obvously
653 * always explicit, and since we know one of them has to be
654 * unsigned, we check if the signed one was explicit).
656 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
657 return "different explicit signedness";
660 * "char" matches both "unsigned char" and "signed char",
661 * so if the explicit test didn't trigger, then we should
662 * not warn about a char.
664 if (!(mod1 & MOD_CHAR))
665 return "different signedness";
669 if (type1 == SYM_FN) {
670 int i;
671 struct symbol *arg1, *arg2;
672 if (base1->variadic != base2->variadic)
673 return "incompatible variadic arguments";
674 PREPARE_PTR_LIST(target->arguments, arg1);
675 PREPARE_PTR_LIST(source->arguments, arg2);
676 i = 1;
677 for (;;) {
678 const char *diff;
679 diff = type_difference(arg1, arg2, 0, 0);
680 if (diff) {
681 static char argdiff[80];
682 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
683 return argdiff;
685 if (!arg1)
686 break;
687 NEXT_PTR_LIST(arg1);
688 NEXT_PTR_LIST(arg2);
689 i++;
691 FINISH_PTR_LIST(arg2);
692 FINISH_PTR_LIST(arg1);
695 target = base1;
696 source = base2;
698 return NULL;
701 static int is_null_ptr(struct expression *expr)
703 if (expr->type != EXPR_VALUE || expr->value)
704 return 0;
705 if (!is_ptr_type(expr->ctype))
706 warning(expr->pos, "Using plain integer as NULL pointer");
707 return 1;
710 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
712 /* NULL expression? Just return the type of the "other side" */
713 if (is_null_ptr(r))
714 return l->ctype;
715 if (is_null_ptr(l))
716 return r->ctype;
717 return NULL;
721 * Ignore differences in "volatile" and "const"ness when
722 * subtracting pointers
724 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
726 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
728 const char *typediff;
729 struct symbol *ctype;
730 struct symbol *ltype, *rtype;
731 struct expression *r = *rp;
733 ltype = degenerate(l);
734 rtype = degenerate(r);
737 * If it is an integer subtract: the ptr add case will do the
738 * right thing.
740 if (!is_ptr_type(rtype))
741 return evaluate_ptr_add(expr, degenerate(l), rp);
743 ctype = ltype;
744 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
745 if (typediff) {
746 ctype = common_ptr_type(l, r);
747 if (!ctype) {
748 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
749 return NULL;
752 examine_symbol_type(ctype);
754 /* Figure out the base type we point to */
755 if (ctype->type == SYM_NODE)
756 ctype = ctype->ctype.base_type;
757 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
758 warning(expr->pos, "subtraction of functions? Share your drugs");
759 return NULL;
761 ctype = ctype->ctype.base_type;
763 expr->ctype = ssize_t_ctype;
764 if (ctype->bit_size > bits_in_char) {
765 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
766 struct expression *div = expr;
767 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
768 unsigned long value = ctype->bit_size >> 3;
770 val->ctype = size_t_ctype;
771 val->value = value;
773 if (value & (value-1)) {
774 if (Wptr_subtraction_blows)
775 warning(expr->pos, "potentially expensive pointer subtraction");
778 sub->op = '-';
779 sub->ctype = ssize_t_ctype;
780 sub->left = l;
781 sub->right = r;
783 div->op = '/';
784 div->left = sub;
785 div->right = val;
788 return ssize_t_ctype;
791 static struct symbol *evaluate_sub(struct expression *expr)
793 struct expression *left = expr->left;
794 struct symbol *ltype = left->ctype;
796 if (is_ptr_type(ltype))
797 return evaluate_ptr_sub(expr, left, &expr->right);
799 return evaluate_arith(expr, 1);
802 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
804 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
806 struct symbol *ctype;
808 if (!expr)
809 return NULL;
811 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
812 warning(expr->pos, "assignment expression in conditional");
814 ctype = evaluate_expression(expr);
815 if (ctype) {
816 if (is_safe_type(ctype))
817 warning(expr->pos, "testing a 'safe expression'");
820 return ctype;
823 static struct symbol *evaluate_logical(struct expression *expr)
825 if (!evaluate_conditional(expr->left, 0))
826 return NULL;
827 if (!evaluate_conditional(expr->right, 0))
828 return NULL;
830 expr->ctype = &bool_ctype;
831 return &bool_ctype;
834 static struct symbol *evaluate_shift(struct expression *expr)
836 struct expression *left = expr->left, *right = expr->right;
837 struct symbol *ltype = left->ctype, *rtype = right->ctype;
839 if (ltype->type == SYM_NODE)
840 ltype = ltype->ctype.base_type;
841 if (rtype->type == SYM_NODE)
842 rtype = rtype->ctype.base_type;
843 if (is_int_type(ltype) && is_int_type(rtype)) {
844 struct symbol *ctype = integer_promotion(ltype);
845 expr->left = cast_to(expr->left, ctype);
846 expr->ctype = ctype;
847 ctype = integer_promotion(rtype);
848 expr->right = cast_to(expr->right, ctype);
849 return expr->ctype;
851 return bad_expr_type(expr);
854 static struct symbol *evaluate_binop(struct expression *expr)
856 switch (expr->op) {
857 // addition can take ptr+int, fp and int
858 case '+':
859 return evaluate_add(expr);
861 // subtraction can take ptr-ptr, fp and int
862 case '-':
863 return evaluate_sub(expr);
865 // Arithmetic operations can take fp and int
866 case '*': case '/':
867 return evaluate_arith(expr, 1);
869 // shifts do integer promotions, but that's it.
870 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
871 return evaluate_shift(expr);
873 // The rest are integer operations
874 // '%', '&', '^', '|'
875 default:
876 return evaluate_arith(expr, 0);
880 static struct symbol *evaluate_comma(struct expression *expr)
882 expr->ctype = expr->right->ctype;
883 return expr->ctype;
886 static int modify_for_unsigned(int op)
888 if (op == '<')
889 op = SPECIAL_UNSIGNED_LT;
890 else if (op == '>')
891 op = SPECIAL_UNSIGNED_GT;
892 else if (op == SPECIAL_LTE)
893 op = SPECIAL_UNSIGNED_LTE;
894 else if (op == SPECIAL_GTE)
895 op = SPECIAL_UNSIGNED_GTE;
896 return op;
899 static struct symbol *evaluate_compare(struct expression *expr)
901 struct expression *left = expr->left, *right = expr->right;
902 struct symbol *ltype = left->ctype, *rtype = right->ctype;
903 struct symbol *ctype;
905 /* Type types? */
906 if (is_type_type(ltype) && is_type_type(rtype))
907 goto OK;
909 if (is_safe_type(ltype) || is_safe_type(rtype))
910 warning(expr->pos, "testing a 'safe expression'");
912 /* Pointer types? */
913 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
914 // FIXME! Check the types for compatibility
915 expr->op = modify_for_unsigned(expr->op);
916 goto OK;
919 ctype = compatible_integer_binop(&expr->left, &expr->right);
920 if (ctype) {
921 if (ctype->ctype.modifiers & MOD_UNSIGNED)
922 expr->op = modify_for_unsigned(expr->op);
923 goto OK;
926 ctype = compatible_float_binop(&expr->left, &expr->right);
927 if (ctype)
928 goto OK;
930 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
931 if (ctype) {
932 if (ctype->ctype.modifiers & MOD_UNSIGNED)
933 expr->op = modify_for_unsigned(expr->op);
934 goto OK;
937 bad_expr_type(expr);
940 expr->ctype = &bool_ctype;
941 return &bool_ctype;
945 * FIXME!! This should do casts, array degeneration etc..
947 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
949 struct symbol *ltype = left->ctype, *rtype = right->ctype;
951 if (ltype->type == SYM_NODE)
952 ltype = ltype->ctype.base_type;
954 if (rtype->type == SYM_NODE)
955 rtype = rtype->ctype.base_type;
957 if (ltype->type == SYM_PTR) {
958 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
959 return ltype;
962 if (rtype->type == SYM_PTR) {
963 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
964 return rtype;
966 return NULL;
970 * NOTE! The degenerate case of "x ? : y", where we don't
971 * have a true case, this will possibly promote "x" to the
972 * same type as "y", and thus _change_ the conditional
973 * test in the expression. But since promotion is "safe"
974 * for testing, that's ok.
976 static struct symbol *evaluate_conditional_expression(struct expression *expr)
978 struct expression **true;
979 struct symbol *ctype, *ltype, *rtype;
980 const char * typediff;
982 if (!evaluate_conditional(expr->conditional, 0))
983 return NULL;
984 if (!evaluate_expression(expr->cond_false))
985 return NULL;
987 ctype = degenerate(expr->conditional);
988 rtype = degenerate(expr->cond_false);
990 true = &expr->conditional;
991 ltype = ctype;
992 if (expr->cond_true) {
993 if (!evaluate_expression(expr->cond_true))
994 return NULL;
995 ltype = degenerate(expr->cond_true);
996 true = &expr->cond_true;
999 ctype = compatible_integer_binop(true, &expr->cond_false);
1000 if (ctype)
1001 goto out;
1002 ctype = compatible_ptr_type(*true, expr->cond_false);
1003 if (ctype)
1004 goto out;
1005 ctype = compatible_float_binop(true, &expr->cond_false);
1006 if (ctype)
1007 goto out;
1008 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1009 if (ctype)
1010 goto out;
1011 ctype = ltype;
1012 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1013 if (!typediff)
1014 goto out;
1015 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1016 return NULL;
1018 out:
1019 expr->ctype = ctype;
1020 return ctype;
1023 /* FP assignments can not do modulo or bit operations */
1024 static int compatible_float_op(int op)
1026 return op == '=' ||
1027 op == SPECIAL_ADD_ASSIGN ||
1028 op == SPECIAL_SUB_ASSIGN ||
1029 op == SPECIAL_MUL_ASSIGN ||
1030 op == SPECIAL_DIV_ASSIGN;
1033 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1034 struct expression **rp, struct symbol *source, const char *where, int op)
1036 const char *typediff;
1037 struct symbol *t;
1038 int target_as;
1040 if (is_int_type(target)) {
1041 if (is_int_type(source))
1042 goto Cast;
1043 if (is_float_type(source))
1044 goto Cast;
1045 } else if (is_float_type(target)) {
1046 if (!compatible_float_op(op)) {
1047 warning(expr->pos, "invalid assignment");
1048 return 0;
1050 if (is_int_type(source))
1051 goto Cast;
1052 if (is_float_type(source))
1053 goto Cast;
1054 } else if (is_restricted_type(target)) {
1055 if (restricted_binop(op, target)) {
1056 warning(expr->pos, "bad restricted assignment");
1057 return 0;
1059 if (!restricted_value(*rp, target))
1060 return 1;
1061 } else if (is_ptr_type(target)) {
1062 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1063 evaluate_ptr_add(expr, target, rp);
1064 return 1;
1066 if (op != '=') {
1067 warning(expr->pos, "invalid pointer assignment");
1068 return 0;
1070 } else if (op != '=') {
1071 warning(expr->pos, "invalid assignment");
1072 return 0;
1075 /* It's ok if the target is more volatile or const than the source */
1076 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1077 if (!typediff)
1078 return 1;
1080 /* Pointer destination? */
1081 t = target;
1082 target_as = t->ctype.as;
1083 if (t->type == SYM_NODE) {
1084 t = t->ctype.base_type;
1085 target_as |= t->ctype.as;
1087 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1088 struct expression *right = *rp;
1089 struct symbol *s = source;
1090 int source_as;
1092 // NULL pointer is always ok
1093 if (is_null_ptr(right))
1094 return 1;
1096 /* "void *" matches anything as long as the address space is ok */
1097 source_as = s->ctype.as;
1098 if (s->type == SYM_NODE) {
1099 s = s->ctype.base_type;
1100 source_as |= s->ctype.as;
1102 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1103 s = s->ctype.base_type;
1104 t = t->ctype.base_type;
1105 if (s == &void_ctype || t == &void_ctype)
1106 return 1;
1110 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1111 info(expr->pos, " expected %s", show_typename(target));
1112 info(expr->pos, " got %s", show_typename(source));
1113 *rp = cast_to(*rp, target);
1114 return 0;
1115 Cast:
1116 *rp = cast_to(*rp, target);
1117 return 1;
1120 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1122 if (type->ctype.modifiers & MOD_CONST)
1123 warning(left->pos, "assignment to const expression");
1124 if (type->type == SYM_NODE)
1125 type->ctype.modifiers |= MOD_ASSIGNED;
1128 static struct symbol *evaluate_assignment(struct expression *expr)
1130 struct expression *left = expr->left, *right = expr->right;
1131 struct expression *where = expr;
1132 struct symbol *ltype, *rtype;
1134 if (!lvalue_expression(left)) {
1135 warning(expr->pos, "not an lvalue");
1136 return NULL;
1139 ltype = left->ctype;
1141 rtype = degenerate(right);
1143 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1144 return NULL;
1146 evaluate_assign_to(left, ltype);
1148 expr->ctype = ltype;
1149 return ltype;
1152 static void examine_fn_arguments(struct symbol *fn)
1154 struct symbol *s;
1156 FOR_EACH_PTR(fn->arguments, s) {
1157 struct symbol *arg = evaluate_symbol(s);
1158 /* Array/function arguments silently degenerate into pointers */
1159 if (arg) {
1160 struct symbol *ptr;
1161 switch(arg->type) {
1162 case SYM_ARRAY:
1163 case SYM_FN:
1164 ptr = alloc_symbol(s->pos, SYM_PTR);
1165 if (arg->type == SYM_ARRAY)
1166 ptr->ctype = arg->ctype;
1167 else
1168 ptr->ctype.base_type = arg;
1169 ptr->ctype.as |= s->ctype.as;
1170 ptr->ctype.modifiers |= s->ctype.modifiers;
1172 s->ctype.base_type = ptr;
1173 s->ctype.as = 0;
1174 s->ctype.modifiers = 0;
1175 s->bit_size = 0;
1176 s->examined = 0;
1177 examine_symbol_type(s);
1178 break;
1179 default:
1180 /* nothing */
1181 break;
1184 } END_FOR_EACH_PTR(s);
1187 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1189 /* Take the modifiers of the pointer, and apply them to the member */
1190 mod |= sym->ctype.modifiers;
1191 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1192 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1193 *newsym = *sym;
1194 newsym->ctype.as = as;
1195 newsym->ctype.modifiers = mod;
1196 sym = newsym;
1198 return sym;
1201 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1203 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1204 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1206 node->ctype.base_type = ptr;
1207 ptr->bit_size = bits_in_pointer;
1208 ptr->ctype.alignment = pointer_alignment;
1210 node->bit_size = bits_in_pointer;
1211 node->ctype.alignment = pointer_alignment;
1213 access_symbol(sym);
1214 if (sym->ctype.modifiers & MOD_REGISTER) {
1215 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1216 sym->ctype.modifiers &= ~MOD_REGISTER;
1218 if (sym->type == SYM_NODE) {
1219 ptr->ctype.as |= sym->ctype.as;
1220 ptr->ctype.modifiers |= sym->ctype.modifiers;
1221 sym = sym->ctype.base_type;
1223 if (degenerate && sym->type == SYM_ARRAY) {
1224 ptr->ctype.as |= sym->ctype.as;
1225 ptr->ctype.modifiers |= sym->ctype.modifiers;
1226 sym = sym->ctype.base_type;
1228 ptr->ctype.base_type = sym;
1230 return node;
1233 /* Arrays degenerate into pointers on pointer arithmetic */
1234 static struct symbol *degenerate(struct expression *expr)
1236 struct symbol *ctype, *base;
1238 if (!expr)
1239 return NULL;
1240 ctype = expr->ctype;
1241 if (!ctype)
1242 return NULL;
1243 base = ctype;
1244 if (ctype->type == SYM_NODE)
1245 base = ctype->ctype.base_type;
1247 * Arrays degenerate into pointers to the entries, while
1248 * functions degenerate into pointers to themselves.
1249 * If array was part of non-lvalue compound, we create a copy
1250 * of that compound first and then act as if we were dealing with
1251 * the corresponding field in there.
1253 switch (base->type) {
1254 case SYM_ARRAY:
1255 if (expr->type == EXPR_SLICE) {
1256 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1257 struct expression *e0, *e1, *e2, *e3, *e4;
1259 a->ctype.base_type = expr->base->ctype;
1260 a->bit_size = expr->base->ctype->bit_size;
1261 a->array_size = expr->base->ctype->array_size;
1263 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1264 e0->symbol = a;
1265 e0->ctype = &lazy_ptr_ctype;
1267 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1268 e1->unop = e0;
1269 e1->op = '*';
1270 e1->ctype = expr->base->ctype; /* XXX */
1272 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1273 e2->left = e1;
1274 e2->right = expr->base;
1275 e2->op = '=';
1276 e2->ctype = expr->base->ctype;
1278 if (expr->r_bitpos) {
1279 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1280 e3->op = '+';
1281 e3->left = e0;
1282 e3->right = alloc_const_expression(expr->pos,
1283 expr->r_bitpos >> 3);
1284 e3->ctype = &lazy_ptr_ctype;
1285 } else {
1286 e3 = e0;
1289 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1290 e4->left = e2;
1291 e4->right = e3;
1292 e4->ctype = &lazy_ptr_ctype;
1294 expr->unop = e4;
1295 expr->type = EXPR_PREOP;
1296 expr->op = '*';
1298 case SYM_FN:
1299 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1300 warning(expr->pos, "strange non-value function or array");
1301 return &bad_ctype;
1303 *expr = *expr->unop;
1304 ctype = create_pointer(expr, ctype, 1);
1305 expr->ctype = ctype;
1306 default:
1307 /* nothing */;
1309 return ctype;
1312 static struct symbol *evaluate_addressof(struct expression *expr)
1314 struct expression *op = expr->unop;
1315 struct symbol *ctype;
1317 if (op->op != '*' || op->type != EXPR_PREOP) {
1318 warning(expr->pos, "not addressable");
1319 return NULL;
1321 ctype = op->ctype;
1322 *expr = *op->unop;
1324 if (expr->type == EXPR_SYMBOL) {
1325 struct symbol *sym = expr->symbol;
1326 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1330 * symbol expression evaluation is lazy about the type
1331 * of the sub-expression, so we may have to generate
1332 * the type here if so..
1334 if (expr->ctype == &lazy_ptr_ctype) {
1335 ctype = create_pointer(expr, ctype, 0);
1336 expr->ctype = ctype;
1338 return expr->ctype;
1342 static struct symbol *evaluate_dereference(struct expression *expr)
1344 struct expression *op = expr->unop;
1345 struct symbol *ctype = op->ctype, *node, *target;
1347 /* Simplify: *&(expr) => (expr) */
1348 if (op->type == EXPR_PREOP && op->op == '&') {
1349 *expr = *op->unop;
1350 return expr->ctype;
1353 /* Dereferencing a node drops all the node information. */
1354 if (ctype->type == SYM_NODE)
1355 ctype = ctype->ctype.base_type;
1357 node = alloc_symbol(expr->pos, SYM_NODE);
1358 target = ctype->ctype.base_type;
1360 switch (ctype->type) {
1361 default:
1362 warning(expr->pos, "cannot derefence this type");
1363 return NULL;
1364 case SYM_PTR:
1365 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1366 merge_type(node, ctype);
1367 break;
1369 case SYM_ARRAY:
1370 if (!lvalue_expression(op)) {
1371 warning(op->pos, "non-lvalue array??");
1372 return NULL;
1375 /* Do the implied "addressof" on the array */
1376 *op = *op->unop;
1379 * When an array is dereferenced, we need to pick
1380 * up the attributes of the original node too..
1382 merge_type(node, op->ctype);
1383 merge_type(node, ctype);
1384 break;
1387 node->bit_size = target->bit_size;
1388 node->array_size = target->array_size;
1390 expr->ctype = node;
1391 return node;
1395 * Unary post-ops: x++ and x--
1397 static struct symbol *evaluate_postop(struct expression *expr)
1399 struct expression *op = expr->unop;
1400 struct symbol *ctype = op->ctype;
1402 if (!lvalue_expression(expr->unop)) {
1403 warning(expr->pos, "need lvalue expression for ++/--");
1404 return NULL;
1406 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1407 warning(expr->pos, "bad operation on restricted");
1408 return NULL;
1411 evaluate_assign_to(op, ctype);
1413 expr->ctype = ctype;
1414 expr->op_value = 1;
1415 if (is_ptr_type(ctype))
1416 expr->op_value = ptr_object_size(ctype) >> 3;
1418 return ctype;
1421 static struct symbol *evaluate_sign(struct expression *expr)
1423 struct symbol *ctype = expr->unop->ctype;
1424 if (is_int_type(ctype)) {
1425 struct symbol *rtype = rtype = integer_promotion(ctype);
1426 expr->unop = cast_to(expr->unop, rtype);
1427 ctype = rtype;
1428 } else if (is_float_type(ctype) && expr->op != '~') {
1429 /* no conversions needed */
1430 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1431 /* no conversions needed */
1432 } else {
1433 return bad_expr_type(expr);
1435 if (expr->op == '+')
1436 *expr = *expr->unop;
1437 expr->ctype = ctype;
1438 return ctype;
1441 static struct symbol *evaluate_preop(struct expression *expr)
1443 struct symbol *ctype = expr->unop->ctype;
1445 switch (expr->op) {
1446 case '(':
1447 *expr = *expr->unop;
1448 return ctype;
1450 case '+':
1451 case '-':
1452 case '~':
1453 return evaluate_sign(expr);
1455 case '*':
1456 return evaluate_dereference(expr);
1458 case '&':
1459 return evaluate_addressof(expr);
1461 case SPECIAL_INCREMENT:
1462 case SPECIAL_DECREMENT:
1464 * From a type evaluation standpoint the pre-ops are
1465 * the same as the postops
1467 return evaluate_postop(expr);
1469 case '!':
1470 if (is_safe_type(ctype))
1471 warning(expr->pos, "testing a 'safe expression'");
1472 if (is_float_type(ctype)) {
1473 struct expression *arg = expr->unop;
1474 expr->type = EXPR_BINOP;
1475 expr->op = SPECIAL_EQUAL;
1476 expr->left = arg;
1477 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1478 expr->right->ctype = ctype;
1479 expr->right->fvalue = 0;
1481 ctype = &bool_ctype;
1482 break;
1484 default:
1485 break;
1487 expr->ctype = ctype;
1488 return &bool_ctype;
1491 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1493 struct ptr_list *head = (struct ptr_list *)_list;
1494 struct ptr_list *list = head;
1496 if (!head)
1497 return NULL;
1498 do {
1499 int i;
1500 for (i = 0; i < list->nr; i++) {
1501 struct symbol *sym = (struct symbol *) list->list[i];
1502 if (sym->ident) {
1503 if (sym->ident != ident)
1504 continue;
1505 *offset = sym->offset;
1506 return sym;
1507 } else {
1508 struct symbol *ctype = sym->ctype.base_type;
1509 struct symbol *sub;
1510 if (!ctype)
1511 continue;
1512 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1513 continue;
1514 sub = find_identifier(ident, ctype->symbol_list, offset);
1515 if (!sub)
1516 continue;
1517 *offset += sym->offset;
1518 return sub;
1521 } while ((list = list->next) != head);
1522 return NULL;
1525 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1527 struct expression *add;
1530 * Create a new add-expression
1532 * NOTE! Even if we just add zero, we need a new node
1533 * for the member pointer, since it has a different
1534 * type than the original pointer. We could make that
1535 * be just a cast, but the fact is, a node is a node,
1536 * so we might as well just do the "add zero" here.
1538 add = alloc_expression(expr->pos, EXPR_BINOP);
1539 add->op = '+';
1540 add->left = expr;
1541 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1542 add->right->ctype = &int_ctype;
1543 add->right->value = offset;
1546 * The ctype of the pointer will be lazily evaluated if
1547 * we ever take the address of this member dereference..
1549 add->ctype = &lazy_ptr_ctype;
1550 return add;
1553 /* structure/union dereference */
1554 static struct symbol *evaluate_member_dereference(struct expression *expr)
1556 int offset;
1557 struct symbol *ctype, *member;
1558 struct expression *deref = expr->deref, *add;
1559 struct ident *ident = expr->member;
1560 unsigned int mod;
1561 int address_space;
1563 if (!evaluate_expression(deref))
1564 return NULL;
1565 if (!ident) {
1566 warning(expr->pos, "bad member name");
1567 return NULL;
1570 ctype = deref->ctype;
1571 address_space = ctype->ctype.as;
1572 mod = ctype->ctype.modifiers;
1573 if (ctype->type == SYM_NODE) {
1574 ctype = ctype->ctype.base_type;
1575 address_space |= ctype->ctype.as;
1576 mod |= ctype->ctype.modifiers;
1578 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1579 warning(expr->pos, "expected structure or union");
1580 return NULL;
1582 offset = 0;
1583 member = find_identifier(ident, ctype->symbol_list, &offset);
1584 if (!member) {
1585 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1586 const char *name = "<unnamed>";
1587 int namelen = 9;
1588 if (ctype->ident) {
1589 name = ctype->ident->name;
1590 namelen = ctype->ident->len;
1592 warning(expr->pos, "no member '%s' in %s %.*s",
1593 show_ident(ident), type, namelen, name);
1594 return NULL;
1598 * The member needs to take on the address space and modifiers of
1599 * the "parent" type.
1601 member = convert_to_as_mod(member, address_space, mod);
1602 ctype = member->ctype.base_type;
1604 if (!lvalue_expression(deref)) {
1605 if (deref->type != EXPR_SLICE) {
1606 expr->base = deref;
1607 expr->r_bitpos = 0;
1608 } else {
1609 expr->base = deref->base;
1610 expr->r_bitpos = deref->r_bitpos;
1612 expr->r_bitpos += offset << 3;
1613 expr->type = EXPR_SLICE;
1614 expr->r_nrbits = member->bit_size;
1615 expr->r_bitpos += member->bit_offset;
1616 expr->ctype = member;
1617 return member;
1620 deref = deref->unop;
1621 expr->deref = deref;
1623 add = evaluate_offset(deref, offset);
1624 expr->type = EXPR_PREOP;
1625 expr->op = '*';
1626 expr->unop = add;
1628 expr->ctype = member;
1629 return member;
1632 static int is_promoted(struct expression *expr)
1634 while (1) {
1635 switch (expr->type) {
1636 case EXPR_BINOP:
1637 case EXPR_SELECT:
1638 case EXPR_CONDITIONAL:
1639 return 1;
1640 case EXPR_COMMA:
1641 expr = expr->right;
1642 continue;
1643 case EXPR_PREOP:
1644 switch (expr->op) {
1645 case '(':
1646 expr = expr->unop;
1647 continue;
1648 case '+':
1649 case '-':
1650 case '~':
1651 return 1;
1652 default:
1653 return 0;
1655 default:
1656 return 0;
1662 static struct symbol *evaluate_cast(struct expression *);
1664 static struct symbol *evaluate_type_information(struct expression *expr)
1666 struct symbol *sym = expr->cast_type;
1667 if (!sym) {
1668 sym = evaluate_expression(expr->cast_expression);
1669 if (!sym)
1670 return NULL;
1672 * Expressions of restricted types will possibly get
1673 * promoted - check that here
1675 if (is_restricted_type(sym)) {
1676 if (sym->bit_size < bits_in_int && is_promoted(expr))
1677 sym = &int_ctype;
1680 examine_symbol_type(sym);
1681 if (is_bitfield_type(sym)) {
1682 warning(expr->pos, "trying to examine bitfield type");
1683 return NULL;
1685 return sym;
1688 static struct symbol *evaluate_sizeof(struct expression *expr)
1690 struct symbol *type;
1691 int size;
1693 type = evaluate_type_information(expr);
1694 if (!type)
1695 return NULL;
1697 size = type->bit_size;
1698 if ((size <= 0) || (size & 7))
1699 warning(expr->pos, "cannot size expression");
1700 expr->type = EXPR_VALUE;
1701 expr->value = size >> 3;
1702 expr->ctype = size_t_ctype;
1703 return size_t_ctype;
1706 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1708 struct symbol *type;
1709 int size;
1711 type = evaluate_type_information(expr);
1712 if (!type)
1713 return NULL;
1715 if (type->type == SYM_NODE)
1716 type = type->ctype.base_type;
1717 if (!type)
1718 return NULL;
1719 switch (type->type) {
1720 case SYM_ARRAY:
1721 break;
1722 case SYM_PTR:
1723 type = type->ctype.base_type;
1724 if (type)
1725 break;
1726 default:
1727 warning(expr->pos, "expected pointer expression");
1728 return NULL;
1730 size = type->bit_size;
1731 if (size & 7)
1732 size = 0;
1733 expr->type = EXPR_VALUE;
1734 expr->value = size >> 3;
1735 expr->ctype = size_t_ctype;
1736 return size_t_ctype;
1739 static struct symbol *evaluate_alignof(struct expression *expr)
1741 struct symbol *type;
1743 type = evaluate_type_information(expr);
1744 if (!type)
1745 return NULL;
1747 expr->type = EXPR_VALUE;
1748 expr->value = type->ctype.alignment;
1749 expr->ctype = size_t_ctype;
1750 return size_t_ctype;
1753 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1755 struct expression *expr;
1756 struct symbol_list *argument_types = fn->arguments;
1757 struct symbol *argtype;
1758 int i = 1;
1760 PREPARE_PTR_LIST(argument_types, argtype);
1761 FOR_EACH_PTR (head, expr) {
1762 struct expression **p = THIS_ADDRESS(expr);
1763 struct symbol *ctype, *target;
1764 ctype = evaluate_expression(expr);
1766 if (!ctype)
1767 return 0;
1769 ctype = degenerate(expr);
1771 target = argtype;
1772 if (!target && ctype->bit_size < bits_in_int)
1773 target = &int_ctype;
1774 if (target) {
1775 static char where[30];
1776 examine_symbol_type(target);
1777 sprintf(where, "argument %d", i);
1778 compatible_assignment_types(expr, target, p, ctype, where, '=');
1781 i++;
1782 NEXT_PTR_LIST(argtype);
1783 } END_FOR_EACH_PTR(expr);
1784 FINISH_PTR_LIST(argtype);
1785 return 1;
1788 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1790 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1792 struct expression *entry = *ep;
1793 struct expression **parent, *reuse = NULL;
1794 unsigned long offset;
1795 struct symbol *sym;
1796 unsigned long from, to;
1797 int accept_string = is_byte_type(ctype);
1799 from = current;
1800 to = from+1;
1801 parent = ep;
1802 if (entry->type == EXPR_INDEX) {
1803 from = entry->idx_from;
1804 to = entry->idx_to+1;
1805 parent = &entry->idx_expression;
1806 reuse = entry;
1807 entry = entry->idx_expression;
1810 offset = from * (ctype->bit_size>>3);
1811 if (offset) {
1812 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1813 reuse->type = EXPR_POS;
1814 reuse->ctype = ctype;
1815 reuse->init_offset = offset;
1816 reuse->init_nr = to - from;
1817 reuse->init_expr = entry;
1818 parent = &reuse->init_expr;
1819 entry = reuse;
1821 *ep = entry;
1823 if (accept_string && entry->type == EXPR_STRING) {
1824 sym = evaluate_expression(entry);
1825 to = from + get_expression_value(sym->array_size);
1826 } else {
1827 evaluate_initializer(ctype, parent);
1829 return to;
1832 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1834 struct expression *entry;
1835 int current = 0;
1837 FOR_EACH_PTR(expr->expr_list, entry) {
1838 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1839 } END_FOR_EACH_PTR(entry);
1842 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1843 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1845 if (expression_list_size(expr->expr_list) != 1) {
1846 warning(expr->pos, "unexpected compound initializer");
1847 return;
1849 evaluate_array_initializer(ctype, expr);
1850 return;
1853 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1855 struct symbol *sym;
1857 FOR_EACH_PTR(ctype->symbol_list, sym) {
1858 if (sym->ident == ident)
1859 return sym;
1860 } END_FOR_EACH_PTR(sym);
1861 return NULL;
1864 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1866 struct expression *entry = *ep;
1867 struct expression **parent;
1868 struct expression *reuse = NULL;
1869 unsigned long offset;
1871 if (!sym) {
1872 error(entry->pos, "unknown named initializer");
1873 return -1;
1876 if (entry->type == EXPR_IDENTIFIER) {
1877 reuse = entry;
1878 entry = entry->ident_expression;
1881 parent = ep;
1882 offset = sym->offset;
1883 if (offset) {
1884 if (!reuse)
1885 reuse = alloc_expression(entry->pos, EXPR_POS);
1886 reuse->type = EXPR_POS;
1887 reuse->ctype = sym;
1888 reuse->init_offset = offset;
1889 reuse->init_nr = 1;
1890 reuse->init_expr = entry;
1891 parent = &reuse->init_expr;
1892 entry = reuse;
1894 *ep = entry;
1895 evaluate_initializer(sym, parent);
1896 return 0;
1899 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1901 struct expression *entry;
1902 struct symbol *sym;
1904 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1905 FOR_EACH_PTR(expr->expr_list, entry) {
1906 if (entry->type == EXPR_IDENTIFIER) {
1907 struct ident *ident = entry->expr_ident;
1908 /* We special-case the "already right place" case */
1909 if (!sym || sym->ident != ident) {
1910 RESET_PTR_LIST(sym);
1911 for (;;) {
1912 if (!sym)
1913 break;
1914 if (sym->ident == ident)
1915 break;
1916 NEXT_PTR_LIST(sym);
1920 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1921 return;
1922 NEXT_PTR_LIST(sym);
1923 } END_FOR_EACH_PTR(entry);
1924 FINISH_PTR_LIST(sym);
1928 * Initializers are kind of like assignments. Except
1929 * they can be a hell of a lot more complex.
1931 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1933 struct expression *expr = *ep;
1936 * Simple non-structure/array initializers are the simple
1937 * case, and look (and parse) largely like assignments.
1939 switch (expr->type) {
1940 default: {
1941 int is_string = expr->type == EXPR_STRING;
1942 struct symbol *rtype = evaluate_expression(expr);
1943 if (rtype) {
1945 * Special case:
1946 * char array[] = "string"
1947 * should _not_ degenerate.
1949 if (!is_string || !is_string_type(ctype))
1950 rtype = degenerate(expr);
1951 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1953 return;
1956 case EXPR_INITIALIZER:
1957 expr->ctype = ctype;
1958 if (ctype->type == SYM_NODE)
1959 ctype = ctype->ctype.base_type;
1961 switch (ctype->type) {
1962 case SYM_ARRAY:
1963 case SYM_PTR:
1964 evaluate_array_initializer(ctype->ctype.base_type, expr);
1965 return;
1966 case SYM_UNION:
1967 evaluate_struct_or_union_initializer(ctype, expr, 0);
1968 return;
1969 case SYM_STRUCT:
1970 evaluate_struct_or_union_initializer(ctype, expr, 1);
1971 return;
1972 default:
1973 evaluate_scalar_initializer(ctype, expr);
1974 return;
1977 case EXPR_IDENTIFIER:
1978 if (ctype->type == SYM_NODE)
1979 ctype = ctype->ctype.base_type;
1980 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1981 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1982 show_symbol(ctype);
1983 return;
1985 evaluate_one_struct_initializer(ctype, ep,
1986 find_struct_ident(ctype, expr->expr_ident));
1987 return;
1989 case EXPR_INDEX:
1990 if (ctype->type == SYM_NODE)
1991 ctype = ctype->ctype.base_type;
1992 if (ctype->type != SYM_ARRAY) {
1993 error(expr->pos, "expected array");
1994 return;
1996 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1997 return;
1999 case EXPR_POS:
2001 * An EXPR_POS expression has already been evaluated, and we don't
2002 * need to do anything more
2004 return;
2008 static int get_as(struct symbol *sym)
2010 int as;
2011 unsigned long mod;
2013 if (!sym)
2014 return 0;
2015 as = sym->ctype.as;
2016 mod = sym->ctype.modifiers;
2017 if (sym->type == SYM_NODE) {
2018 sym = sym->ctype.base_type;
2019 as |= sym->ctype.as;
2020 mod |= sym->ctype.modifiers;
2024 * At least for now, allow casting to a "unsigned long".
2025 * That's how we do things like pointer arithmetic and
2026 * store pointers to registers.
2028 if (sym == &ulong_ctype)
2029 return -1;
2031 if (sym && sym->type == SYM_PTR) {
2032 sym = sym->ctype.base_type;
2033 as |= sym->ctype.as;
2034 mod |= sym->ctype.modifiers;
2036 if (mod & MOD_FORCE)
2037 return -1;
2038 return as;
2041 static struct symbol *evaluate_cast(struct expression *expr)
2043 struct expression *target = expr->cast_expression;
2044 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2045 enum type type;
2047 if (!target)
2048 return NULL;
2050 expr->ctype = ctype;
2051 expr->cast_type = ctype;
2054 * Special case: a cast can be followed by an
2055 * initializer, in which case we need to pass
2056 * the type value down to that initializer rather
2057 * than trying to evaluate it as an expression
2059 * A more complex case is when the initializer is
2060 * dereferenced as part of a post-fix expression.
2061 * We need to produce an expression that can be dereferenced.
2063 if (target->type == EXPR_INITIALIZER) {
2064 struct symbol *sym = expr->cast_type;
2065 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2067 sym->initializer = expr->cast_expression;
2068 evaluate_symbol(sym);
2070 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2071 addr->symbol = sym;
2073 expr->type = EXPR_PREOP;
2074 expr->op = '*';
2075 expr->unop = addr;
2076 expr->ctype = sym;
2078 return sym;
2081 evaluate_expression(target);
2082 degenerate(target);
2085 * You can always throw a value away by casting to
2086 * "void" - that's an implicit "force". Note that
2087 * the same is _not_ true of "void *".
2089 if (ctype == &void_ctype)
2090 goto out;
2092 type = ctype->type;
2093 if (type == SYM_NODE) {
2094 type = ctype->ctype.base_type->type;
2095 if (ctype->ctype.base_type == &void_ctype)
2096 goto out;
2098 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2099 warning(expr->pos, "cast to non-scalar");
2101 if (!target->ctype) {
2102 warning(expr->pos, "cast from unknown type");
2103 goto out;
2106 type = target->ctype->type;
2107 if (type == SYM_NODE)
2108 type = target->ctype->ctype.base_type->type;
2109 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2110 warning(expr->pos, "cast from non-scalar");
2112 if (!get_as(ctype) && get_as(target->ctype) > 0)
2113 warning(expr->pos, "cast removes address space of expression");
2115 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2116 struct symbol *t1 = ctype, *t2 = target->ctype;
2117 if (t1->type == SYM_NODE)
2118 t1 = t1->ctype.base_type;
2119 if (t2->type == SYM_NODE)
2120 t2 = t2->ctype.base_type;
2121 if (t1 != t2) {
2122 if (t1->type == SYM_RESTRICT)
2123 warning(expr->pos, "cast to restricted type");
2124 if (t2->type == SYM_RESTRICT)
2125 warning(expr->pos, "cast from restricted type");
2130 * Casts of constant values are special: they
2131 * can be NULL, and thus need to be simplified
2132 * early.
2134 if (target->type == EXPR_VALUE)
2135 cast_value(expr, ctype, target, target->ctype);
2137 out:
2138 return ctype;
2142 * Evaluate a call expression with a symbol. This
2143 * should expand inline functions, and evaluate
2144 * builtins.
2146 static int evaluate_symbol_call(struct expression *expr)
2148 struct expression *fn = expr->fn;
2149 struct symbol *ctype = fn->ctype;
2151 if (fn->type != EXPR_PREOP)
2152 return 0;
2154 if (ctype->op && ctype->op->evaluate)
2155 return ctype->op->evaluate(expr);
2157 if (ctype->ctype.modifiers & MOD_INLINE) {
2158 int ret;
2159 struct symbol *curr = current_fn;
2160 current_fn = ctype->ctype.base_type;
2161 examine_fn_arguments(current_fn);
2163 ret = inline_function(expr, ctype);
2165 /* restore the old function */
2166 current_fn = curr;
2167 return ret;
2170 return 0;
2173 static struct symbol *evaluate_call(struct expression *expr)
2175 int args, fnargs;
2176 struct symbol *ctype, *sym;
2177 struct expression *fn = expr->fn;
2178 struct expression_list *arglist = expr->args;
2180 if (!evaluate_expression(fn))
2181 return NULL;
2182 sym = ctype = fn->ctype;
2183 if (ctype->type == SYM_NODE)
2184 ctype = ctype->ctype.base_type;
2185 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2186 ctype = ctype->ctype.base_type;
2187 if (!evaluate_arguments(sym, ctype, arglist))
2188 return NULL;
2189 if (ctype->type != SYM_FN) {
2190 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2191 return NULL;
2193 args = expression_list_size(expr->args);
2194 fnargs = symbol_list_size(ctype->arguments);
2195 if (args < fnargs)
2196 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2197 if (args > fnargs && !ctype->variadic)
2198 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2199 if (sym->type == SYM_NODE) {
2200 if (evaluate_symbol_call(expr))
2201 return expr->ctype;
2203 expr->ctype = ctype->ctype.base_type;
2204 return expr->ctype;
2207 struct symbol *evaluate_expression(struct expression *expr)
2209 if (!expr)
2210 return NULL;
2211 if (expr->ctype)
2212 return expr->ctype;
2214 switch (expr->type) {
2215 case EXPR_VALUE:
2216 case EXPR_FVALUE:
2217 warning(expr->pos, "value expression without a type");
2218 return NULL;
2219 case EXPR_STRING:
2220 return evaluate_string(expr);
2221 case EXPR_SYMBOL:
2222 return evaluate_symbol_expression(expr);
2223 case EXPR_BINOP:
2224 if (!evaluate_expression(expr->left))
2225 return NULL;
2226 if (!evaluate_expression(expr->right))
2227 return NULL;
2228 return evaluate_binop(expr);
2229 case EXPR_LOGICAL:
2230 return evaluate_logical(expr);
2231 case EXPR_COMMA:
2232 evaluate_expression(expr->left);
2233 if (!evaluate_expression(expr->right))
2234 return NULL;
2235 return evaluate_comma(expr);
2236 case EXPR_COMPARE:
2237 if (!evaluate_expression(expr->left))
2238 return NULL;
2239 if (!evaluate_expression(expr->right))
2240 return NULL;
2241 return evaluate_compare(expr);
2242 case EXPR_ASSIGNMENT:
2243 if (!evaluate_expression(expr->left))
2244 return NULL;
2245 if (!evaluate_expression(expr->right))
2246 return NULL;
2247 return evaluate_assignment(expr);
2248 case EXPR_PREOP:
2249 if (!evaluate_expression(expr->unop))
2250 return NULL;
2251 return evaluate_preop(expr);
2252 case EXPR_POSTOP:
2253 if (!evaluate_expression(expr->unop))
2254 return NULL;
2255 return evaluate_postop(expr);
2256 case EXPR_CAST:
2257 case EXPR_IMPLIED_CAST:
2258 return evaluate_cast(expr);
2259 case EXPR_SIZEOF:
2260 return evaluate_sizeof(expr);
2261 case EXPR_PTRSIZEOF:
2262 return evaluate_ptrsizeof(expr);
2263 case EXPR_ALIGNOF:
2264 return evaluate_alignof(expr);
2265 case EXPR_DEREF:
2266 return evaluate_member_dereference(expr);
2267 case EXPR_CALL:
2268 return evaluate_call(expr);
2269 case EXPR_SELECT:
2270 case EXPR_CONDITIONAL:
2271 return evaluate_conditional_expression(expr);
2272 case EXPR_STATEMENT:
2273 expr->ctype = evaluate_statement(expr->statement);
2274 return expr->ctype;
2276 case EXPR_LABEL:
2277 expr->ctype = &ptr_ctype;
2278 return &ptr_ctype;
2280 case EXPR_TYPE:
2281 /* Evaluate the type of the symbol .. */
2282 evaluate_symbol(expr->symbol);
2283 /* .. but the type of the _expression_ is a "type" */
2284 expr->ctype = &type_ctype;
2285 return &type_ctype;
2287 /* These can not exist as stand-alone expressions */
2288 case EXPR_INITIALIZER:
2289 case EXPR_IDENTIFIER:
2290 case EXPR_INDEX:
2291 case EXPR_POS:
2292 warning(expr->pos, "internal front-end error: initializer in expression");
2293 return NULL;
2294 case EXPR_SLICE:
2295 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2296 return NULL;
2298 return NULL;
2301 static void check_duplicates(struct symbol *sym)
2303 struct symbol *next = sym;
2305 while ((next = next->same_symbol) != NULL) {
2306 const char *typediff;
2307 evaluate_symbol(next);
2308 typediff = type_difference(sym, next, 0, 0);
2309 if (typediff) {
2310 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2311 show_ident(sym->ident),
2312 stream_name(next->pos.stream), next->pos.line, typediff);
2313 return;
2318 static struct symbol *evaluate_symbol(struct symbol *sym)
2320 struct symbol *base_type;
2322 if (!sym)
2323 return sym;
2325 sym = examine_symbol_type(sym);
2326 base_type = sym->ctype.base_type;
2327 if (!base_type)
2328 return NULL;
2330 /* Evaluate the initializers */
2331 if (sym->initializer)
2332 evaluate_initializer(sym, &sym->initializer);
2334 /* And finally, evaluate the body of the symbol too */
2335 if (base_type->type == SYM_FN) {
2336 struct symbol *curr = current_fn;
2338 current_fn = base_type;
2340 examine_fn_arguments(base_type);
2341 if (!base_type->stmt && base_type->inline_stmt)
2342 uninline(sym);
2343 if (base_type->stmt)
2344 evaluate_statement(base_type->stmt);
2346 current_fn = curr;
2349 return base_type;
2352 void evaluate_symbol_list(struct symbol_list *list)
2354 struct symbol *sym;
2356 FOR_EACH_PTR(list, sym) {
2357 check_duplicates(sym);
2358 evaluate_symbol(sym);
2359 } END_FOR_EACH_PTR(sym);
2362 static struct symbol *evaluate_return_expression(struct statement *stmt)
2364 struct expression *expr = stmt->expression;
2365 struct symbol *ctype, *fntype;
2367 evaluate_expression(expr);
2368 ctype = degenerate(expr);
2369 fntype = current_fn->ctype.base_type;
2370 if (!fntype || fntype == &void_ctype) {
2371 if (expr && ctype != &void_ctype)
2372 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2373 return NULL;
2376 if (!expr) {
2377 warning(stmt->pos, "return with no return value");
2378 return NULL;
2380 if (!ctype)
2381 return NULL;
2382 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2383 return NULL;
2386 static void evaluate_if_statement(struct statement *stmt)
2388 if (!stmt->if_conditional)
2389 return;
2391 evaluate_conditional(stmt->if_conditional, 0);
2392 evaluate_statement(stmt->if_true);
2393 evaluate_statement(stmt->if_false);
2396 static void evaluate_iterator(struct statement *stmt)
2398 evaluate_conditional(stmt->iterator_pre_condition, 1);
2399 evaluate_conditional(stmt->iterator_post_condition,1);
2400 evaluate_statement(stmt->iterator_pre_statement);
2401 evaluate_statement(stmt->iterator_statement);
2402 evaluate_statement(stmt->iterator_post_statement);
2405 static void verify_output_constraint(struct expression *expr, const char *constraint)
2407 switch (*constraint) {
2408 case '=': /* Assignment */
2409 case '+': /* Update */
2410 break;
2411 default:
2412 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2416 static void verify_input_constraint(struct expression *expr, const char *constraint)
2418 switch (*constraint) {
2419 case '=': /* Assignment */
2420 case '+': /* Update */
2421 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2425 static void evaluate_asm_statement(struct statement *stmt)
2427 struct expression *expr;
2428 int state;
2430 expr = stmt->asm_string;
2431 if (!expr || expr->type != EXPR_STRING) {
2432 warning(stmt->pos, "need constant string for inline asm");
2433 return;
2436 state = 0;
2437 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2438 struct ident *ident;
2440 switch (state) {
2441 case 0: /* Identifier */
2442 state = 1;
2443 ident = (struct ident *)expr;
2444 continue;
2446 case 1: /* Constraint */
2447 state = 2;
2448 if (!expr || expr->type != EXPR_STRING) {
2449 warning(expr->pos, "asm output constraint is not a string");
2450 *THIS_ADDRESS(expr) = NULL;
2451 continue;
2453 verify_output_constraint(expr, expr->string->data);
2454 continue;
2456 case 2: /* Expression */
2457 state = 0;
2458 if (!evaluate_expression(expr))
2459 return;
2460 if (!lvalue_expression(expr))
2461 warning(expr->pos, "asm output is not an lvalue");
2462 evaluate_assign_to(expr, expr->ctype);
2463 continue;
2465 } END_FOR_EACH_PTR(expr);
2467 state = 0;
2468 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2469 struct ident *ident;
2471 switch (state) {
2472 case 0: /* Identifier */
2473 state = 1;
2474 ident = (struct ident *)expr;
2475 continue;
2477 case 1: /* Constraint */
2478 state = 2;
2479 if (!expr || expr->type != EXPR_STRING) {
2480 warning(expr->pos, "asm input constraint is not a string");
2481 *THIS_ADDRESS(expr) = NULL;
2482 continue;
2484 verify_input_constraint(expr, expr->string->data);
2485 continue;
2487 case 2: /* Expression */
2488 state = 0;
2489 if (!evaluate_expression(expr))
2490 return;
2491 continue;
2493 } END_FOR_EACH_PTR(expr);
2495 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2496 if (!expr) {
2497 warning(stmt->pos, "bad asm output");
2498 return;
2500 if (expr->type == EXPR_STRING)
2501 continue;
2502 warning(expr->pos, "asm clobber is not a string");
2503 } END_FOR_EACH_PTR(expr);
2506 struct symbol *evaluate_statement(struct statement *stmt)
2508 if (!stmt)
2509 return NULL;
2511 switch (stmt->type) {
2512 case STMT_RETURN:
2513 return evaluate_return_expression(stmt);
2515 case STMT_EXPRESSION:
2516 if (!evaluate_expression(stmt->expression))
2517 return NULL;
2518 return degenerate(stmt->expression);
2520 case STMT_COMPOUND: {
2521 struct statement *s;
2522 struct symbol *type = NULL;
2523 struct symbol *sym;
2525 /* Evaluate each symbol in the compound statement */
2526 FOR_EACH_PTR(stmt->syms, sym) {
2527 evaluate_symbol(sym);
2528 } END_FOR_EACH_PTR(sym);
2529 evaluate_symbol(stmt->ret);
2532 * Then, evaluate each statement, making the type of the
2533 * compound statement be the type of the last statement
2535 type = NULL;
2536 FOR_EACH_PTR(stmt->stmts, s) {
2537 type = evaluate_statement(s);
2538 } END_FOR_EACH_PTR(s);
2539 if (!type)
2540 type = &void_ctype;
2541 return type;
2543 case STMT_IF:
2544 evaluate_if_statement(stmt);
2545 return NULL;
2546 case STMT_ITERATOR:
2547 evaluate_iterator(stmt);
2548 return NULL;
2549 case STMT_SWITCH:
2550 evaluate_expression(stmt->switch_expression);
2551 evaluate_statement(stmt->switch_statement);
2552 return NULL;
2553 case STMT_CASE:
2554 evaluate_expression(stmt->case_expression);
2555 evaluate_expression(stmt->case_to);
2556 evaluate_statement(stmt->case_statement);
2557 return NULL;
2558 case STMT_LABEL:
2559 return evaluate_statement(stmt->label_statement);
2560 case STMT_GOTO:
2561 evaluate_expression(stmt->goto_expression);
2562 return NULL;
2563 case STMT_NONE:
2564 break;
2565 case STMT_ASM:
2566 evaluate_asm_statement(stmt);
2567 return NULL;
2568 case STMT_CONTEXT:
2569 evaluate_expression(stmt->expression);
2570 return NULL;
2571 case STMT_RANGE:
2572 evaluate_expression(stmt->range_expression);
2573 evaluate_expression(stmt->range_low);
2574 evaluate_expression(stmt->range_high);
2575 return NULL;
2577 return NULL;