Fix assignment and conditional expression parsing with no left side.
[smatch.git] / evaluate.c
blob33171b1f7bb1b30947b0b056688192313200b7d1
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 expression *addr;
37 struct symbol *sym = expr->symbol;
38 struct symbol *base_type;
40 if (!sym) {
41 warning(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
47 base_type = get_base_type(sym);
48 if (!base_type) {
49 warning(expr->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
50 return NULL;
53 addr = alloc_expression(expr->pos, EXPR_SYMBOL);
54 addr->symbol = sym;
55 addr->symbol_name = expr->symbol_name;
56 addr->ctype = &lazy_ptr_ctype; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr->type = EXPR_PREOP;
58 expr->op = '*';
59 expr->unop = addr;
61 /* The type of a symbol is the symbol itself! */
62 expr->ctype = sym;
63 return sym;
66 static struct symbol *evaluate_string(struct expression *expr)
68 struct symbol *sym = alloc_symbol(expr->pos, SYM_NODE);
69 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
70 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
71 struct expression *initstr = alloc_expression(expr->pos, EXPR_STRING);
72 unsigned int length = expr->string->length;
74 sym->array_size = alloc_const_expression(expr->pos, length);
75 sym->bit_size = bits_in_char * length;
76 sym->ctype.alignment = 1;
77 sym->ctype.modifiers = MOD_STATIC;
78 sym->ctype.base_type = array;
79 sym->initializer = initstr;
81 initstr->ctype = sym;
82 initstr->string = expr->string;
84 array->array_size = sym->array_size;
85 array->bit_size = bits_in_char * length;
86 array->ctype.alignment = 1;
87 array->ctype.modifiers = MOD_STATIC;
88 array->ctype.base_type = &char_ctype;
90 addr->symbol = sym;
91 addr->ctype = &lazy_ptr_ctype;
93 expr->type = EXPR_PREOP;
94 expr->op = '*';
95 expr->unop = addr;
96 expr->ctype = sym;
97 return sym;
100 static inline struct symbol *integer_promotion(struct symbol *type)
102 unsigned long mod = type->ctype.modifiers;
103 int width;
105 if (type->type == SYM_NODE)
106 type = type->ctype.base_type;
107 if (type->type == SYM_ENUM)
108 type = type->ctype.base_type;
109 width = type->bit_size;
110 if (type->type == SYM_BITFIELD)
111 type = type->ctype.base_type;
112 mod = type->ctype.modifiers;
113 if (width < bits_in_int)
114 return &int_ctype;
116 /* If char/short has as many bits as int, it still gets "promoted" */
117 if (mod & (MOD_CHAR | MOD_SHORT)) {
118 type = &int_ctype;
119 if (mod & MOD_UNSIGNED)
120 type = &uint_ctype;
122 return type;
126 * integer part of usual arithmetic conversions:
127 * integer promotions are applied
128 * if left and right are identical, we are done
129 * if signedness is the same, convert one with lower rank
130 * unless unsigned argument has rank lower than signed one, convert the
131 * signed one.
132 * if signed argument is bigger than unsigned one, convert the unsigned.
133 * otherwise, convert signed.
135 * Leaving aside the integer promotions, that is equivalent to
136 * if identical, don't convert
137 * if left is bigger than right, convert right
138 * if right is bigger than left, convert right
139 * otherwise, if signedness is the same, convert one with lower rank
140 * otherwise convert the signed one.
142 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
144 unsigned long lmod, rmod;
146 left = integer_promotion(left);
147 right = integer_promotion(right);
149 if (left == right)
150 goto left;
152 if (left->bit_size > right->bit_size)
153 goto left;
155 if (right->bit_size > left->bit_size)
156 goto right;
158 lmod = left->ctype.modifiers;
159 rmod = right->ctype.modifiers;
160 if ((lmod ^ rmod) & MOD_UNSIGNED) {
161 if (lmod & MOD_UNSIGNED)
162 goto left;
163 } else if ((lmod & ~rmod) & (MOD_LONG | MOD_LONGLONG))
164 goto left;
165 right:
166 left = right;
167 left:
168 return left;
171 static int same_cast_type(struct symbol *orig, struct symbol *new)
173 return orig->bit_size == new->bit_size && orig->bit_offset == orig->bit_offset;
176 static struct symbol *base_type(struct symbol *node, unsigned long *modp, unsigned long *asp)
178 unsigned long mod, as;
180 mod = 0; as = 0;
181 while (node) {
182 mod |= node->ctype.modifiers;
183 as |= node->ctype.as;
184 if (node->type == SYM_NODE) {
185 node = node->ctype.base_type;
186 continue;
188 break;
190 *modp = mod & ~MOD_IGNORE;
191 *asp = as;
192 return node;
195 static int is_same_type(struct expression *expr, struct symbol *new)
197 struct symbol *old = expr->ctype;
198 unsigned long oldmod, newmod, difmod, oldas, newas;
200 old = base_type(old, &oldmod, &oldas);
201 new = base_type(new, &newmod, &newas);
202 difmod = (oldmod ^ newmod) & ~MOD_NOCAST;
203 if (old == new && oldas == newas && !difmod)
204 return 1;
205 if ((oldmod | newmod) & MOD_NOCAST) {
206 const char *tofrom = "to/from";
207 if (!(newmod & MOD_NOCAST))
208 tofrom = "from";
209 if (!(oldmod & MOD_NOCAST))
210 tofrom = "to";
211 warning(expr->pos, "implicit cast %s nocast type", tofrom);
213 return 0;
217 * This gets called for implicit casts in assignments and
218 * integer promotion. We often want to try to move the
219 * cast down, because the ops involved may have been
220 * implicitly cast up, and we can get rid of the casts
221 * early.
223 static struct expression * cast_to(struct expression *old, struct symbol *type)
225 struct expression *expr;
227 if (is_same_type(old, type))
228 return old;
231 * See if we can simplify the op. Move the cast down.
233 switch (old->type) {
234 case EXPR_PREOP:
235 if (old->op == '~') {
236 old->ctype = type;
237 old->unop = cast_to(old->unop, type);
238 return old;
240 break;
242 case EXPR_IMPLIED_CAST:
243 if (old->ctype->bit_size >= type->bit_size) {
244 struct expression *orig = old->cast_expression;
245 if (same_cast_type(orig->ctype, type))
246 return orig;
247 if (old->ctype->bit_offset == type->bit_offset) {
248 old->ctype = type;
249 old->cast_type = type;
250 return old;
253 break;
255 default:
256 /* nothing */;
259 expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
260 expr->ctype = type;
261 expr->cast_type = type;
262 expr->cast_expression = old;
263 return expr;
266 static int is_type_type(struct symbol *type)
268 return (type->ctype.modifiers & MOD_TYPE) != 0;
271 int is_ptr_type(struct symbol *type)
273 if (type->type == SYM_NODE)
274 type = type->ctype.base_type;
275 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
278 static inline int is_float_type(struct symbol *type)
280 if (type->type == SYM_NODE)
281 type = type->ctype.base_type;
282 return type->ctype.base_type == &fp_type;
285 static inline int is_byte_type(struct symbol *type)
287 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
290 static inline int is_string_type(struct symbol *type)
292 if (type->type == SYM_NODE)
293 type = type->ctype.base_type;
294 return type->type == SYM_ARRAY && is_byte_type(type->ctype.base_type);
297 static struct symbol *bad_expr_type(struct expression *expr)
299 warning(expr->pos, "incompatible types for operation (%s)", show_special(expr->op));
300 switch (expr->type) {
301 case EXPR_BINOP:
302 case EXPR_COMPARE:
303 info(expr->pos, " left side has type %s", show_typename(expr->left->ctype));
304 info(expr->pos, " right side has type %s", show_typename(expr->right->ctype));
305 break;
306 case EXPR_PREOP:
307 case EXPR_POSTOP:
308 info(expr->pos, " argument has type %s", show_typename(expr->unop->ctype));
309 break;
310 default:
311 break;
314 return NULL;
317 static struct symbol *compatible_float_binop(struct expression **lp, struct expression **rp)
319 struct expression *left = *lp, *right = *rp;
320 struct symbol *ltype = left->ctype, *rtype = right->ctype;
322 if (ltype->type == SYM_NODE)
323 ltype = ltype->ctype.base_type;
324 if (rtype->type == SYM_NODE)
325 rtype = rtype->ctype.base_type;
326 if (is_float_type(ltype)) {
327 if (is_int_type(rtype))
328 goto Left;
329 if (is_float_type(rtype)) {
330 unsigned long lmod = ltype->ctype.modifiers;
331 unsigned long rmod = rtype->ctype.modifiers;
332 lmod &= MOD_LONG | MOD_LONGLONG;
333 rmod &= MOD_LONG | MOD_LONGLONG;
334 if (lmod == rmod)
335 return ltype;
336 if (lmod & ~rmod)
337 goto Left;
338 else
339 goto Right;
341 return NULL;
343 if (!is_float_type(rtype) || !is_int_type(ltype))
344 return NULL;
345 Right:
346 *lp = cast_to(left, rtype);
347 return rtype;
348 Left:
349 *rp = cast_to(right, ltype);
350 return ltype;
353 static struct symbol *compatible_integer_binop(struct expression **lp, struct expression **rp)
355 struct expression *left = *lp, *right = *rp;
356 struct symbol *ltype = left->ctype, *rtype = right->ctype;
358 if (ltype->type == SYM_NODE)
359 ltype = ltype->ctype.base_type;
360 if (rtype->type == SYM_NODE)
361 rtype = rtype->ctype.base_type;
362 if (is_int_type(ltype) && is_int_type(rtype)) {
363 struct symbol *ctype = bigger_int_type(ltype, rtype);
365 *lp = cast_to(left, ctype);
366 *rp = cast_to(right, ctype);
367 return ctype;
369 return NULL;
372 static int restricted_value(struct expression *v, struct symbol *type)
374 if (v->type != EXPR_VALUE)
375 return 1;
376 if (v->value != 0)
377 return 1;
378 return 0;
381 static int restricted_binop(int op, struct symbol *type)
383 switch (op) {
384 case '&':
385 case '|':
386 case '^':
387 case '?':
388 case '=':
389 case SPECIAL_EQUAL:
390 case SPECIAL_NOTEQUAL:
391 case SPECIAL_AND_ASSIGN:
392 case SPECIAL_OR_ASSIGN:
393 case SPECIAL_XOR_ASSIGN:
394 return 0;
395 default:
396 return 1;
400 static int restricted_unop(int op, struct symbol *type)
402 if (op == '~' && type->bit_size >= bits_in_int)
403 return 0;
404 if (op == '+')
405 return 0;
406 return 1;
409 static struct symbol *compatible_restricted_binop(int op, struct expression **lp, struct expression **rp)
411 struct expression *left = *lp, *right = *rp;
412 struct symbol *ltype = left->ctype, *rtype = right->ctype;
413 struct symbol *type = NULL;
415 if (ltype->type == SYM_NODE)
416 ltype = ltype->ctype.base_type;
417 if (ltype->type == SYM_ENUM)
418 ltype = ltype->ctype.base_type;
419 if (rtype->type == SYM_NODE)
420 rtype = rtype->ctype.base_type;
421 if (rtype->type == SYM_ENUM)
422 rtype = rtype->ctype.base_type;
423 if (is_restricted_type(ltype)) {
424 if (is_restricted_type(rtype)) {
425 if (ltype == rtype)
426 type = ltype;
427 } else {
428 if (!restricted_value(right, ltype))
429 type = ltype;
431 } else if (is_restricted_type(rtype)) {
432 if (!restricted_value(left, rtype))
433 type = rtype;
435 if (!type)
436 return NULL;
437 if (restricted_binop(op, type))
438 return NULL;
439 return type;
442 static struct symbol *evaluate_arith(struct expression *expr, int float_ok)
444 struct symbol *ctype = compatible_integer_binop(&expr->left, &expr->right);
445 if (!ctype && float_ok)
446 ctype = compatible_float_binop(&expr->left, &expr->right);
447 if (!ctype)
448 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
449 if (ctype) {
450 expr->ctype = ctype;
451 return ctype;
453 return bad_expr_type(expr);
456 static inline int lvalue_expression(struct expression *expr)
458 return expr->type == EXPR_PREOP && expr->op == '*';
461 static int ptr_object_size(struct symbol *ptr_type)
463 if (ptr_type->type == SYM_NODE)
464 ptr_type = ptr_type->ctype.base_type;
465 if (ptr_type->type == SYM_PTR)
466 ptr_type = get_base_type(ptr_type);
467 return ptr_type->bit_size;
470 static struct symbol *evaluate_ptr_add(struct expression *expr, struct symbol *ctype, struct expression **ip)
472 struct expression *i = *ip;
473 struct symbol *ptr_type = ctype;
474 int bit_size;
476 if (ptr_type->type == SYM_NODE)
477 ptr_type = ptr_type->ctype.base_type;
479 if (!is_int_type(i->ctype))
480 return bad_expr_type(expr);
482 examine_symbol_type(ctype);
484 if (!ctype->ctype.base_type) {
485 warning(expr->pos, "missing type information");
486 return NULL;
489 /* Get the size of whatever the pointer points to */
490 bit_size = ptr_object_size(ctype);
492 if (bit_size > bits_in_char) {
493 int multiply = bit_size >> 3;
494 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
496 if (i->type == EXPR_VALUE) {
497 val->value = i->value * multiply;
498 val->ctype = size_t_ctype;
499 *ip = val;
500 } else {
501 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
503 val->ctype = size_t_ctype;
504 val->value = bit_size >> 3;
506 mul->op = '*';
507 mul->ctype = size_t_ctype;
508 mul->left = i;
509 mul->right = val;
511 *ip = mul;
515 expr->ctype = ctype;
516 return ctype;
519 static struct symbol *evaluate_add(struct expression *expr)
521 struct expression *left = expr->left, *right = expr->right;
522 struct symbol *ltype = left->ctype, *rtype = right->ctype;
524 if (is_ptr_type(ltype))
525 return evaluate_ptr_add(expr, degenerate(left), &expr->right);
527 if (is_ptr_type(rtype))
528 return evaluate_ptr_add(expr, degenerate(right), &expr->left);
530 return evaluate_arith(expr, 1);
533 const char * type_difference(struct symbol *target, struct symbol *source,
534 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
536 for (;;) {
537 unsigned long mod1, mod2, diff;
538 unsigned long as1, as2;
539 int type1, type2;
540 struct symbol *base1, *base2;
542 if (target == source)
543 break;
544 if (!target || !source)
545 return "different types";
547 * Peel of per-node information.
548 * FIXME! Check alignment and context too here!
550 mod1 = target->ctype.modifiers;
551 as1 = target->ctype.as;
552 mod2 = source->ctype.modifiers;
553 as2 = source->ctype.as;
554 if (target->type == SYM_NODE) {
555 target = target->ctype.base_type;
556 if (!target)
557 return "bad types";
558 if (target->type == SYM_PTR) {
559 mod1 = 0;
560 as1 = 0;
562 mod1 |= target->ctype.modifiers;
563 as1 |= target->ctype.as;
565 if (source->type == SYM_NODE) {
566 source = source->ctype.base_type;
567 if (!source)
568 return "bad types";
569 if (source->type == SYM_PTR) {
570 mod2 = 0;
571 as2 = 0;
573 mod2 |= source->ctype.modifiers;
574 as2 |= source->ctype.as;
576 if (target->type == SYM_ENUM) {
577 target = target->ctype.base_type;
578 if (!target)
579 return "bad types";
581 if (source->type == SYM_ENUM) {
582 source = source->ctype.base_type;
583 if (!source)
584 return "bad types";
587 if (target == source)
588 break;
589 if (!target || !source)
590 return "different types";
592 type1 = target->type;
593 base1 = target->ctype.base_type;
595 type2 = source->type;
596 base2 = source->ctype.base_type;
599 * Pointers to functions compare as the function itself
601 if (type1 == SYM_PTR && base1) {
602 base1 = examine_symbol_type(base1);
603 switch (base1->type) {
604 case SYM_FN:
605 type1 = SYM_FN;
606 target = base1;
607 base1 = base1->ctype.base_type;
608 default:
609 /* nothing */;
612 if (type2 == SYM_PTR && base2) {
613 base2 = examine_symbol_type(base2);
614 switch (base2->type) {
615 case SYM_FN:
616 type2 = SYM_FN;
617 source = base2;
618 base2 = base2->ctype.base_type;
619 default:
620 /* nothing */;
624 /* Arrays degenerate to pointers for type comparisons */
625 type1 = (type1 == SYM_ARRAY) ? SYM_PTR : type1;
626 type2 = (type2 == SYM_ARRAY) ? SYM_PTR : type2;
628 if (type1 != type2 || type1 == SYM_RESTRICT)
629 return "different base types";
631 /* Must be same address space to be comparable */
632 if (as1 != as2)
633 return "different address spaces";
635 /* Ignore differences in storage types or addressability */
636 diff = (mod1 ^ mod2) & ~MOD_IGNORE;
637 diff &= (mod1 & ~target_mod_ignore) | (mod2 & ~source_mod_ignore);
638 if (diff) {
639 if (diff & MOD_SIZE)
640 return "different type sizes";
641 if (diff & ~MOD_SIGNEDNESS)
642 return "different modifiers";
644 /* Differs in signedness only.. */
645 if (Wtypesign) {
647 * Warn if both are explicitly signed ("unsigned" is obvously
648 * always explicit, and since we know one of them has to be
649 * unsigned, we check if the signed one was explicit).
651 if ((mod1 | mod2) & MOD_EXPLICITLY_SIGNED)
652 return "different explicit signedness";
655 * "char" matches both "unsigned char" and "signed char",
656 * so if the explicit test didn't trigger, then we should
657 * not warn about a char.
659 if (!(mod1 & MOD_CHAR))
660 return "different signedness";
664 if (type1 == SYM_FN) {
665 int i;
666 struct symbol *arg1, *arg2;
667 if (base1->variadic != base2->variadic)
668 return "incompatible variadic arguments";
669 PREPARE_PTR_LIST(target->arguments, arg1);
670 PREPARE_PTR_LIST(source->arguments, arg2);
671 i = 1;
672 for (;;) {
673 const char *diff;
674 diff = type_difference(arg1, arg2, 0, 0);
675 if (diff) {
676 static char argdiff[80];
677 sprintf(argdiff, "incompatible argument %d (%s)", i, diff);
678 return argdiff;
680 if (!arg1)
681 break;
682 NEXT_PTR_LIST(arg1);
683 NEXT_PTR_LIST(arg2);
684 i++;
686 FINISH_PTR_LIST(arg2);
687 FINISH_PTR_LIST(arg1);
690 target = base1;
691 source = base2;
693 return NULL;
696 static int is_null_ptr(struct expression *expr)
698 if (expr->type != EXPR_VALUE || expr->value)
699 return 0;
700 if (!is_ptr_type(expr->ctype))
701 warning(expr->pos, "Using plain integer as NULL pointer");
702 return 1;
705 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
707 /* NULL expression? Just return the type of the "other side" */
708 if (is_null_ptr(r))
709 return l->ctype;
710 if (is_null_ptr(l))
711 return r->ctype;
712 return NULL;
716 * Ignore differences in "volatile" and "const"ness when
717 * subtracting pointers
719 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
721 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression **rp)
723 const char *typediff;
724 struct symbol *ctype;
725 struct symbol *ltype, *rtype;
726 struct expression *r = *rp;
728 ltype = degenerate(l);
729 rtype = degenerate(r);
732 * If it is an integer subtract: the ptr add case will do the
733 * right thing.
735 if (!is_ptr_type(rtype))
736 return evaluate_ptr_add(expr, degenerate(l), rp);
738 ctype = ltype;
739 typediff = type_difference(ltype, rtype, ~MOD_SIZE, ~MOD_SIZE);
740 if (typediff) {
741 ctype = common_ptr_type(l, r);
742 if (!ctype) {
743 warning(expr->pos, "subtraction of different types can't work (%s)", typediff);
744 return NULL;
747 examine_symbol_type(ctype);
749 /* Figure out the base type we point to */
750 if (ctype->type == SYM_NODE)
751 ctype = ctype->ctype.base_type;
752 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
753 warning(expr->pos, "subtraction of functions? Share your drugs");
754 return NULL;
756 ctype = get_base_type(ctype);
758 expr->ctype = ssize_t_ctype;
759 if (ctype->bit_size > bits_in_char) {
760 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
761 struct expression *div = expr;
762 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
763 unsigned long value = ctype->bit_size >> 3;
765 val->ctype = size_t_ctype;
766 val->value = value;
768 if (value & (value-1)) {
769 if (Wptr_subtraction_blows)
770 warning(expr->pos, "potentially expensive pointer subtraction");
773 sub->op = '-';
774 sub->ctype = ssize_t_ctype;
775 sub->left = l;
776 sub->right = r;
778 div->op = '/';
779 div->left = sub;
780 div->right = val;
783 return ssize_t_ctype;
786 static struct symbol *evaluate_sub(struct expression *expr)
788 struct expression *left = expr->left;
789 struct symbol *ltype = left->ctype;
791 if (is_ptr_type(ltype))
792 return evaluate_ptr_sub(expr, left, &expr->right);
794 return evaluate_arith(expr, 1);
797 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
799 static struct symbol *evaluate_conditional(struct expression *expr, int iterator)
801 struct symbol *ctype;
803 if (!expr)
804 return NULL;
806 if (!iterator && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
807 warning(expr->pos, "assignment expression in conditional");
809 ctype = evaluate_expression(expr);
810 if (ctype) {
811 if (is_safe_type(ctype))
812 warning(expr->pos, "testing a 'safe expression'");
815 return ctype;
818 static struct symbol *evaluate_logical(struct expression *expr)
820 if (!evaluate_conditional(expr->left, 0))
821 return NULL;
822 if (!evaluate_conditional(expr->right, 0))
823 return NULL;
825 expr->ctype = &bool_ctype;
826 return &bool_ctype;
829 static struct symbol *evaluate_shift(struct expression *expr)
831 struct expression *left = expr->left, *right = expr->right;
832 struct symbol *ltype = left->ctype, *rtype = right->ctype;
834 if (ltype->type == SYM_NODE)
835 ltype = ltype->ctype.base_type;
836 if (rtype->type == SYM_NODE)
837 rtype = rtype->ctype.base_type;
838 if (is_int_type(ltype) && is_int_type(rtype)) {
839 struct symbol *ctype = integer_promotion(ltype);
840 expr->left = cast_to(expr->left, ctype);
841 expr->ctype = ctype;
842 ctype = integer_promotion(rtype);
843 expr->right = cast_to(expr->right, ctype);
844 return expr->ctype;
846 return bad_expr_type(expr);
849 static struct symbol *evaluate_binop(struct expression *expr)
851 switch (expr->op) {
852 // addition can take ptr+int, fp and int
853 case '+':
854 return evaluate_add(expr);
856 // subtraction can take ptr-ptr, fp and int
857 case '-':
858 return evaluate_sub(expr);
860 // Arithmetic operations can take fp and int
861 case '*': case '/':
862 return evaluate_arith(expr, 1);
864 // shifts do integer promotions, but that's it.
865 case SPECIAL_LEFTSHIFT: case SPECIAL_RIGHTSHIFT:
866 return evaluate_shift(expr);
868 // The rest are integer operations
869 // '%', '&', '^', '|'
870 default:
871 return evaluate_arith(expr, 0);
875 static struct symbol *evaluate_comma(struct expression *expr)
877 expr->ctype = expr->right->ctype;
878 return expr->ctype;
881 static int modify_for_unsigned(int op)
883 if (op == '<')
884 op = SPECIAL_UNSIGNED_LT;
885 else if (op == '>')
886 op = SPECIAL_UNSIGNED_GT;
887 else if (op == SPECIAL_LTE)
888 op = SPECIAL_UNSIGNED_LTE;
889 else if (op == SPECIAL_GTE)
890 op = SPECIAL_UNSIGNED_GTE;
891 return op;
894 static struct symbol *evaluate_compare(struct expression *expr)
896 struct expression *left = expr->left, *right = expr->right;
897 struct symbol *ltype = left->ctype, *rtype = right->ctype;
898 struct symbol *ctype;
900 /* Type types? */
901 if (is_type_type(ltype) && is_type_type(rtype))
902 goto OK;
904 if (is_safe_type(ltype) || is_safe_type(rtype))
905 warning(expr->pos, "testing a 'safe expression'");
907 /* Pointer types? */
908 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
909 // FIXME! Check the types for compatibility
910 expr->op = modify_for_unsigned(expr->op);
911 goto OK;
914 ctype = compatible_integer_binop(&expr->left, &expr->right);
915 if (ctype) {
916 if (ctype->ctype.modifiers & MOD_UNSIGNED)
917 expr->op = modify_for_unsigned(expr->op);
918 goto OK;
921 ctype = compatible_float_binop(&expr->left, &expr->right);
922 if (ctype)
923 goto OK;
925 ctype = compatible_restricted_binop(expr->op, &expr->left, &expr->right);
926 if (ctype) {
927 if (ctype->ctype.modifiers & MOD_UNSIGNED)
928 expr->op = modify_for_unsigned(expr->op);
929 goto OK;
932 bad_expr_type(expr);
935 expr->ctype = &bool_ctype;
936 return &bool_ctype;
940 * FIXME!! This should do casts, array degeneration etc..
942 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
944 struct symbol *ltype = left->ctype, *rtype = right->ctype;
946 if (ltype->type == SYM_NODE)
947 ltype = ltype->ctype.base_type;
949 if (rtype->type == SYM_NODE)
950 rtype = rtype->ctype.base_type;
952 if (ltype->type == SYM_PTR) {
953 if (is_null_ptr(right) || rtype->ctype.base_type == &void_ctype)
954 return ltype;
957 if (rtype->type == SYM_PTR) {
958 if (is_null_ptr(left) || ltype->ctype.base_type == &void_ctype)
959 return rtype;
961 return NULL;
965 * NOTE! The degenerate case of "x ? : y", where we don't
966 * have a true case, this will possibly promote "x" to the
967 * same type as "y", and thus _change_ the conditional
968 * test in the expression. But since promotion is "safe"
969 * for testing, that's ok.
971 static struct symbol *evaluate_conditional_expression(struct expression *expr)
973 struct expression **true;
974 struct symbol *ctype, *ltype, *rtype;
975 const char * typediff;
977 if (!evaluate_conditional(expr->conditional, 0))
978 return NULL;
979 if (!evaluate_expression(expr->cond_false))
980 return NULL;
982 ctype = degenerate(expr->conditional);
983 rtype = degenerate(expr->cond_false);
985 true = &expr->conditional;
986 ltype = ctype;
987 if (expr->cond_true) {
988 if (!evaluate_expression(expr->cond_true))
989 return NULL;
990 ltype = degenerate(expr->cond_true);
991 true = &expr->cond_true;
994 ctype = compatible_integer_binop(true, &expr->cond_false);
995 if (ctype)
996 goto out;
997 ctype = compatible_ptr_type(*true, expr->cond_false);
998 if (ctype)
999 goto out;
1000 ctype = compatible_float_binop(true, &expr->cond_false);
1001 if (ctype)
1002 goto out;
1003 ctype = compatible_restricted_binop('?', true, &expr->cond_false);
1004 if (ctype)
1005 goto out;
1006 ctype = ltype;
1007 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
1008 if (!typediff)
1009 goto out;
1010 warning(expr->pos, "incompatible types in conditional expression (%s)", typediff);
1011 return NULL;
1013 out:
1014 expr->ctype = ctype;
1015 return ctype;
1018 /* FP assignments can not do modulo or bit operations */
1019 static int compatible_float_op(int op)
1021 return op == '=' ||
1022 op == SPECIAL_ADD_ASSIGN ||
1023 op == SPECIAL_SUB_ASSIGN ||
1024 op == SPECIAL_MUL_ASSIGN ||
1025 op == SPECIAL_DIV_ASSIGN;
1028 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
1029 struct expression **rp, struct symbol *source, const char *where, int op)
1031 const char *typediff;
1032 struct symbol *t;
1033 int target_as;
1035 if (is_int_type(target)) {
1036 if (is_int_type(source))
1037 goto Cast;
1038 if (is_float_type(source))
1039 goto Cast;
1040 } else if (is_float_type(target)) {
1041 if (!compatible_float_op(op)) {
1042 warning(expr->pos, "invalid assignment");
1043 return 0;
1045 if (is_int_type(source))
1046 goto Cast;
1047 if (is_float_type(source))
1048 goto Cast;
1049 } else if (is_restricted_type(target)) {
1050 if (restricted_binop(op, target)) {
1051 warning(expr->pos, "bad restricted assignment");
1052 return 0;
1054 if (!restricted_value(*rp, target))
1055 return 1;
1056 } else if (is_ptr_type(target)) {
1057 if (op == SPECIAL_ADD_ASSIGN || op == SPECIAL_SUB_ASSIGN) {
1058 evaluate_ptr_add(expr, target, rp);
1059 return 1;
1061 if (op != '=') {
1062 warning(expr->pos, "invalid pointer assignment");
1063 return 0;
1065 } else if (op != '=') {
1066 warning(expr->pos, "invalid assignment");
1067 return 0;
1070 /* It's ok if the target is more volatile or const than the source */
1071 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
1072 if (!typediff)
1073 return 1;
1075 /* Pointer destination? */
1076 t = target;
1077 target_as = t->ctype.as;
1078 if (t->type == SYM_NODE) {
1079 t = t->ctype.base_type;
1080 target_as |= t->ctype.as;
1082 if (t->type == SYM_PTR || t->type == SYM_FN || t->type == SYM_ARRAY) {
1083 struct expression *right = *rp;
1084 struct symbol *s = source;
1085 int source_as;
1087 // NULL pointer is always ok
1088 if (is_null_ptr(right))
1089 goto Cast;
1091 /* "void *" matches anything as long as the address space is ok */
1092 source_as = s->ctype.as;
1093 if (s->type == SYM_NODE) {
1094 s = s->ctype.base_type;
1095 source_as |= s->ctype.as;
1097 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
1098 s = get_base_type(s);
1099 t = get_base_type(t);
1100 if (s == &void_ctype || t == &void_ctype)
1101 goto Cast;
1105 warning(expr->pos, "incorrect type in %s (%s)", where, typediff);
1106 info(expr->pos, " expected %s", show_typename(target));
1107 info(expr->pos, " got %s", show_typename(source));
1108 *rp = cast_to(*rp, target);
1109 return 0;
1110 Cast:
1111 *rp = cast_to(*rp, target);
1112 return 1;
1115 static void evaluate_assign_to(struct expression *left, struct symbol *type)
1117 if (type->ctype.modifiers & MOD_CONST)
1118 warning(left->pos, "assignment to const expression");
1119 if (type->type == SYM_NODE)
1120 type->ctype.modifiers |= MOD_ASSIGNED;
1123 static struct symbol *evaluate_assignment(struct expression *expr)
1125 struct expression *left = expr->left, *right = expr->right;
1126 struct expression *where = expr;
1127 struct symbol *ltype, *rtype;
1129 if (!lvalue_expression(left)) {
1130 warning(expr->pos, "not an lvalue");
1131 return NULL;
1134 ltype = left->ctype;
1136 rtype = degenerate(right);
1138 if (!compatible_assignment_types(where, ltype, &where->right, rtype, "assignment", expr->op))
1139 return NULL;
1141 evaluate_assign_to(left, ltype);
1143 expr->ctype = ltype;
1144 return ltype;
1147 static void examine_fn_arguments(struct symbol *fn)
1149 struct symbol *s;
1151 FOR_EACH_PTR(fn->arguments, s) {
1152 struct symbol *arg = evaluate_symbol(s);
1153 /* Array/function arguments silently degenerate into pointers */
1154 if (arg) {
1155 struct symbol *ptr;
1156 switch(arg->type) {
1157 case SYM_ARRAY:
1158 case SYM_FN:
1159 ptr = alloc_symbol(s->pos, SYM_PTR);
1160 if (arg->type == SYM_ARRAY)
1161 ptr->ctype = arg->ctype;
1162 else
1163 ptr->ctype.base_type = arg;
1164 ptr->ctype.as |= s->ctype.as;
1165 ptr->ctype.modifiers |= s->ctype.modifiers;
1167 s->ctype.base_type = ptr;
1168 s->ctype.as = 0;
1169 s->ctype.modifiers = 0;
1170 s->bit_size = 0;
1171 s->examined = 0;
1172 examine_symbol_type(s);
1173 break;
1174 default:
1175 /* nothing */
1176 break;
1179 } END_FOR_EACH_PTR(s);
1182 static struct symbol *convert_to_as_mod(struct symbol *sym, int as, int mod)
1184 /* Take the modifiers of the pointer, and apply them to the member */
1185 mod |= sym->ctype.modifiers;
1186 if (sym->ctype.as != as || sym->ctype.modifiers != mod) {
1187 struct symbol *newsym = alloc_symbol(sym->pos, SYM_NODE);
1188 *newsym = *sym;
1189 newsym->ctype.as = as;
1190 newsym->ctype.modifiers = mod;
1191 sym = newsym;
1193 return sym;
1196 static struct symbol *create_pointer(struct expression *expr, struct symbol *sym, int degenerate)
1198 struct symbol *node = alloc_symbol(expr->pos, SYM_NODE);
1199 struct symbol *ptr = alloc_symbol(expr->pos, SYM_PTR);
1201 node->ctype.base_type = ptr;
1202 ptr->bit_size = bits_in_pointer;
1203 ptr->ctype.alignment = pointer_alignment;
1205 node->bit_size = bits_in_pointer;
1206 node->ctype.alignment = pointer_alignment;
1208 access_symbol(sym);
1209 if (sym->ctype.modifiers & MOD_REGISTER) {
1210 warning(expr->pos, "taking address of 'register' variable '%s'", show_ident(sym->ident));
1211 sym->ctype.modifiers &= ~MOD_REGISTER;
1213 if (sym->type == SYM_NODE) {
1214 ptr->ctype.as |= sym->ctype.as;
1215 ptr->ctype.modifiers |= sym->ctype.modifiers;
1216 sym = sym->ctype.base_type;
1218 if (degenerate && sym->type == SYM_ARRAY) {
1219 ptr->ctype.as |= sym->ctype.as;
1220 ptr->ctype.modifiers |= sym->ctype.modifiers;
1221 sym = sym->ctype.base_type;
1223 ptr->ctype.base_type = sym;
1225 return node;
1228 /* Arrays degenerate into pointers on pointer arithmetic */
1229 static struct symbol *degenerate(struct expression *expr)
1231 struct symbol *ctype, *base;
1233 if (!expr)
1234 return NULL;
1235 ctype = expr->ctype;
1236 if (!ctype)
1237 return NULL;
1238 base = examine_symbol_type(ctype);
1239 if (ctype->type == SYM_NODE)
1240 base = ctype->ctype.base_type;
1242 * Arrays degenerate into pointers to the entries, while
1243 * functions degenerate into pointers to themselves.
1244 * If array was part of non-lvalue compound, we create a copy
1245 * of that compound first and then act as if we were dealing with
1246 * the corresponding field in there.
1248 switch (base->type) {
1249 case SYM_ARRAY:
1250 if (expr->type == EXPR_SLICE) {
1251 struct symbol *a = alloc_symbol(expr->pos, SYM_NODE);
1252 struct expression *e0, *e1, *e2, *e3, *e4;
1254 a->ctype.base_type = expr->base->ctype;
1255 a->bit_size = expr->base->ctype->bit_size;
1256 a->array_size = expr->base->ctype->array_size;
1258 e0 = alloc_expression(expr->pos, EXPR_SYMBOL);
1259 e0->symbol = a;
1260 e0->ctype = &lazy_ptr_ctype;
1262 e1 = alloc_expression(expr->pos, EXPR_PREOP);
1263 e1->unop = e0;
1264 e1->op = '*';
1265 e1->ctype = expr->base->ctype; /* XXX */
1267 e2 = alloc_expression(expr->pos, EXPR_ASSIGNMENT);
1268 e2->left = e1;
1269 e2->right = expr->base;
1270 e2->op = '=';
1271 e2->ctype = expr->base->ctype;
1273 if (expr->r_bitpos) {
1274 e3 = alloc_expression(expr->pos, EXPR_BINOP);
1275 e3->op = '+';
1276 e3->left = e0;
1277 e3->right = alloc_const_expression(expr->pos,
1278 expr->r_bitpos >> 3);
1279 e3->ctype = &lazy_ptr_ctype;
1280 } else {
1281 e3 = e0;
1284 e4 = alloc_expression(expr->pos, EXPR_COMMA);
1285 e4->left = e2;
1286 e4->right = e3;
1287 e4->ctype = &lazy_ptr_ctype;
1289 expr->unop = e4;
1290 expr->type = EXPR_PREOP;
1291 expr->op = '*';
1293 case SYM_FN:
1294 if (expr->op != '*' || expr->type != EXPR_PREOP) {
1295 warning(expr->pos, "strange non-value function or array");
1296 return &bad_ctype;
1298 *expr = *expr->unop;
1299 ctype = create_pointer(expr, ctype, 1);
1300 expr->ctype = ctype;
1301 default:
1302 /* nothing */;
1304 return ctype;
1307 static struct symbol *evaluate_addressof(struct expression *expr)
1309 struct expression *op = expr->unop;
1310 struct symbol *ctype;
1312 if (op->op != '*' || op->type != EXPR_PREOP) {
1313 warning(expr->pos, "not addressable");
1314 return NULL;
1316 ctype = op->ctype;
1317 *expr = *op->unop;
1319 if (expr->type == EXPR_SYMBOL) {
1320 struct symbol *sym = expr->symbol;
1321 sym->ctype.modifiers |= MOD_ADDRESSABLE;
1325 * symbol expression evaluation is lazy about the type
1326 * of the sub-expression, so we may have to generate
1327 * the type here if so..
1329 if (expr->ctype == &lazy_ptr_ctype) {
1330 ctype = create_pointer(expr, ctype, 0);
1331 expr->ctype = ctype;
1333 return expr->ctype;
1337 static struct symbol *evaluate_dereference(struct expression *expr)
1339 struct expression *op = expr->unop;
1340 struct symbol *ctype = op->ctype, *node, *target;
1342 /* Simplify: *&(expr) => (expr) */
1343 if (op->type == EXPR_PREOP && op->op == '&') {
1344 *expr = *op->unop;
1345 return expr->ctype;
1348 /* Dereferencing a node drops all the node information. */
1349 if (ctype->type == SYM_NODE)
1350 ctype = ctype->ctype.base_type;
1352 node = alloc_symbol(expr->pos, SYM_NODE);
1353 target = ctype->ctype.base_type;
1355 switch (ctype->type) {
1356 default:
1357 warning(expr->pos, "cannot derefence this type");
1358 return NULL;
1359 case SYM_PTR:
1360 node->ctype.modifiers = target->ctype.modifiers & MOD_SPECIFIER;
1361 merge_type(node, ctype);
1362 break;
1364 case SYM_ARRAY:
1365 if (!lvalue_expression(op)) {
1366 warning(op->pos, "non-lvalue array??");
1367 return NULL;
1370 /* Do the implied "addressof" on the array */
1371 *op = *op->unop;
1374 * When an array is dereferenced, we need to pick
1375 * up the attributes of the original node too..
1377 merge_type(node, op->ctype);
1378 merge_type(node, ctype);
1379 break;
1382 node->bit_size = target->bit_size;
1383 node->array_size = target->array_size;
1385 expr->ctype = node;
1386 return node;
1390 * Unary post-ops: x++ and x--
1392 static struct symbol *evaluate_postop(struct expression *expr)
1394 struct expression *op = expr->unop;
1395 struct symbol *ctype = op->ctype;
1397 if (!lvalue_expression(expr->unop)) {
1398 warning(expr->pos, "need lvalue expression for ++/--");
1399 return NULL;
1401 if (is_restricted_type(ctype) && restricted_unop(expr->op, ctype)) {
1402 warning(expr->pos, "bad operation on restricted");
1403 return NULL;
1406 evaluate_assign_to(op, ctype);
1408 expr->ctype = ctype;
1409 expr->op_value = 1;
1410 if (is_ptr_type(ctype))
1411 expr->op_value = ptr_object_size(ctype) >> 3;
1413 return ctype;
1416 static struct symbol *evaluate_sign(struct expression *expr)
1418 struct symbol *ctype = expr->unop->ctype;
1419 if (is_int_type(ctype)) {
1420 struct symbol *rtype = rtype = integer_promotion(ctype);
1421 expr->unop = cast_to(expr->unop, rtype);
1422 ctype = rtype;
1423 } else if (is_float_type(ctype) && expr->op != '~') {
1424 /* no conversions needed */
1425 } else if (is_restricted_type(ctype) && !restricted_unop(expr->op, ctype)) {
1426 /* no conversions needed */
1427 } else {
1428 return bad_expr_type(expr);
1430 if (expr->op == '+')
1431 *expr = *expr->unop;
1432 expr->ctype = ctype;
1433 return ctype;
1436 static struct symbol *evaluate_preop(struct expression *expr)
1438 struct symbol *ctype = expr->unop->ctype;
1440 switch (expr->op) {
1441 case '(':
1442 *expr = *expr->unop;
1443 return ctype;
1445 case '+':
1446 case '-':
1447 case '~':
1448 return evaluate_sign(expr);
1450 case '*':
1451 return evaluate_dereference(expr);
1453 case '&':
1454 return evaluate_addressof(expr);
1456 case SPECIAL_INCREMENT:
1457 case SPECIAL_DECREMENT:
1459 * From a type evaluation standpoint the pre-ops are
1460 * the same as the postops
1462 return evaluate_postop(expr);
1464 case '!':
1465 if (is_safe_type(ctype))
1466 warning(expr->pos, "testing a 'safe expression'");
1467 if (is_float_type(ctype)) {
1468 struct expression *arg = expr->unop;
1469 expr->type = EXPR_BINOP;
1470 expr->op = SPECIAL_EQUAL;
1471 expr->left = arg;
1472 expr->right = alloc_expression(expr->pos, EXPR_FVALUE);
1473 expr->right->ctype = ctype;
1474 expr->right->fvalue = 0;
1476 ctype = &bool_ctype;
1477 break;
1479 default:
1480 break;
1482 expr->ctype = ctype;
1483 return &bool_ctype;
1486 static struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
1488 struct ptr_list *head = (struct ptr_list *)_list;
1489 struct ptr_list *list = head;
1491 if (!head)
1492 return NULL;
1493 do {
1494 int i;
1495 for (i = 0; i < list->nr; i++) {
1496 struct symbol *sym = (struct symbol *) list->list[i];
1497 if (sym->ident) {
1498 if (sym->ident != ident)
1499 continue;
1500 *offset = sym->offset;
1501 return sym;
1502 } else {
1503 struct symbol *ctype = sym->ctype.base_type;
1504 struct symbol *sub;
1505 if (!ctype)
1506 continue;
1507 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
1508 continue;
1509 sub = find_identifier(ident, ctype->symbol_list, offset);
1510 if (!sub)
1511 continue;
1512 *offset += sym->offset;
1513 return sub;
1516 } while ((list = list->next) != head);
1517 return NULL;
1520 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1522 struct expression *add;
1525 * Create a new add-expression
1527 * NOTE! Even if we just add zero, we need a new node
1528 * for the member pointer, since it has a different
1529 * type than the original pointer. We could make that
1530 * be just a cast, but the fact is, a node is a node,
1531 * so we might as well just do the "add zero" here.
1533 add = alloc_expression(expr->pos, EXPR_BINOP);
1534 add->op = '+';
1535 add->left = expr;
1536 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1537 add->right->ctype = &int_ctype;
1538 add->right->value = offset;
1541 * The ctype of the pointer will be lazily evaluated if
1542 * we ever take the address of this member dereference..
1544 add->ctype = &lazy_ptr_ctype;
1545 return add;
1548 /* structure/union dereference */
1549 static struct symbol *evaluate_member_dereference(struct expression *expr)
1551 int offset;
1552 struct symbol *ctype, *member;
1553 struct expression *deref = expr->deref, *add;
1554 struct ident *ident = expr->member;
1555 unsigned int mod;
1556 int address_space;
1558 if (!evaluate_expression(deref))
1559 return NULL;
1560 if (!ident) {
1561 warning(expr->pos, "bad member name");
1562 return NULL;
1565 ctype = deref->ctype;
1566 address_space = ctype->ctype.as;
1567 mod = ctype->ctype.modifiers;
1568 if (ctype->type == SYM_NODE) {
1569 ctype = ctype->ctype.base_type;
1570 address_space |= ctype->ctype.as;
1571 mod |= ctype->ctype.modifiers;
1573 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1574 warning(expr->pos, "expected structure or union");
1575 return NULL;
1577 offset = 0;
1578 member = find_identifier(ident, ctype->symbol_list, &offset);
1579 if (!member) {
1580 const char *type = ctype->type == SYM_STRUCT ? "struct" : "union";
1581 const char *name = "<unnamed>";
1582 int namelen = 9;
1583 if (ctype->ident) {
1584 name = ctype->ident->name;
1585 namelen = ctype->ident->len;
1587 warning(expr->pos, "no member '%s' in %s %.*s",
1588 show_ident(ident), type, namelen, name);
1589 return NULL;
1593 * The member needs to take on the address space and modifiers of
1594 * the "parent" type.
1596 member = convert_to_as_mod(member, address_space, mod);
1597 ctype = get_base_type(member);
1599 if (!lvalue_expression(deref)) {
1600 if (deref->type != EXPR_SLICE) {
1601 expr->base = deref;
1602 expr->r_bitpos = 0;
1603 } else {
1604 expr->base = deref->base;
1605 expr->r_bitpos = deref->r_bitpos;
1607 expr->r_bitpos += offset << 3;
1608 expr->type = EXPR_SLICE;
1609 expr->r_nrbits = member->bit_size;
1610 expr->r_bitpos += member->bit_offset;
1611 expr->ctype = member;
1612 return member;
1615 deref = deref->unop;
1616 expr->deref = deref;
1618 add = evaluate_offset(deref, offset);
1619 expr->type = EXPR_PREOP;
1620 expr->op = '*';
1621 expr->unop = add;
1623 expr->ctype = member;
1624 return member;
1627 static int is_promoted(struct expression *expr)
1629 while (1) {
1630 switch (expr->type) {
1631 case EXPR_BINOP:
1632 case EXPR_SELECT:
1633 case EXPR_CONDITIONAL:
1634 return 1;
1635 case EXPR_COMMA:
1636 expr = expr->right;
1637 continue;
1638 case EXPR_PREOP:
1639 switch (expr->op) {
1640 case '(':
1641 expr = expr->unop;
1642 continue;
1643 case '+':
1644 case '-':
1645 case '~':
1646 return 1;
1647 default:
1648 return 0;
1650 default:
1651 return 0;
1657 static struct symbol *evaluate_cast(struct expression *);
1659 static struct symbol *evaluate_type_information(struct expression *expr)
1661 struct symbol *sym = expr->cast_type;
1662 if (!sym) {
1663 sym = evaluate_expression(expr->cast_expression);
1664 if (!sym)
1665 return NULL;
1667 * Expressions of restricted types will possibly get
1668 * promoted - check that here
1670 if (is_restricted_type(sym)) {
1671 if (sym->bit_size < bits_in_int && is_promoted(expr))
1672 sym = &int_ctype;
1675 examine_symbol_type(sym);
1676 if (is_bitfield_type(sym)) {
1677 warning(expr->pos, "trying to examine bitfield type");
1678 return NULL;
1680 return sym;
1683 static struct symbol *evaluate_sizeof(struct expression *expr)
1685 struct symbol *type;
1686 int size;
1688 type = evaluate_type_information(expr);
1689 if (!type)
1690 return NULL;
1692 size = type->bit_size;
1693 if ((size < 0) || (size & 7))
1694 warning(expr->pos, "cannot size expression");
1695 expr->type = EXPR_VALUE;
1696 expr->value = size >> 3;
1697 expr->ctype = size_t_ctype;
1698 return size_t_ctype;
1701 static struct symbol *evaluate_ptrsizeof(struct expression *expr)
1703 struct symbol *type;
1704 int size;
1706 type = evaluate_type_information(expr);
1707 if (!type)
1708 return NULL;
1710 if (type->type == SYM_NODE)
1711 type = type->ctype.base_type;
1712 if (!type)
1713 return NULL;
1714 switch (type->type) {
1715 case SYM_ARRAY:
1716 break;
1717 case SYM_PTR:
1718 type = get_base_type(type);
1719 if (type)
1720 break;
1721 default:
1722 warning(expr->pos, "expected pointer expression");
1723 return NULL;
1725 size = type->bit_size;
1726 if (size & 7)
1727 size = 0;
1728 expr->type = EXPR_VALUE;
1729 expr->value = size >> 3;
1730 expr->ctype = size_t_ctype;
1731 return size_t_ctype;
1734 static struct symbol *evaluate_alignof(struct expression *expr)
1736 struct symbol *type;
1738 type = evaluate_type_information(expr);
1739 if (!type)
1740 return NULL;
1742 expr->type = EXPR_VALUE;
1743 expr->value = type->ctype.alignment;
1744 expr->ctype = size_t_ctype;
1745 return size_t_ctype;
1748 static int evaluate_arguments(struct symbol *f, struct symbol *fn, struct expression_list *head)
1750 struct expression *expr;
1751 struct symbol_list *argument_types = fn->arguments;
1752 struct symbol *argtype;
1753 int i = 1;
1755 PREPARE_PTR_LIST(argument_types, argtype);
1756 FOR_EACH_PTR (head, expr) {
1757 struct expression **p = THIS_ADDRESS(expr);
1758 struct symbol *ctype, *target;
1759 ctype = evaluate_expression(expr);
1761 if (!ctype)
1762 return 0;
1764 ctype = degenerate(expr);
1766 target = argtype;
1767 if (!target && ctype->bit_size < bits_in_int)
1768 target = &int_ctype;
1769 if (target) {
1770 static char where[30];
1771 examine_symbol_type(target);
1772 sprintf(where, "argument %d", i);
1773 compatible_assignment_types(expr, target, p, ctype, where, '=');
1776 i++;
1777 NEXT_PTR_LIST(argtype);
1778 } END_FOR_EACH_PTR(expr);
1779 FINISH_PTR_LIST(argtype);
1780 return 1;
1783 static void evaluate_initializer(struct symbol *ctype, struct expression **ep);
1785 static int evaluate_one_array_initializer(struct symbol *ctype, struct expression **ep, int current)
1787 struct expression *entry = *ep;
1788 struct expression **parent, *reuse = NULL;
1789 unsigned long offset;
1790 struct symbol *sym;
1791 unsigned long from, to;
1792 int accept_string = is_byte_type(ctype);
1794 from = current;
1795 to = from+1;
1796 parent = ep;
1797 if (entry->type == EXPR_INDEX) {
1798 from = entry->idx_from;
1799 to = entry->idx_to+1;
1800 parent = &entry->idx_expression;
1801 reuse = entry;
1802 entry = entry->idx_expression;
1805 offset = from * (ctype->bit_size>>3);
1806 if (offset) {
1807 if (!reuse) reuse = alloc_expression(entry->pos, EXPR_POS);
1808 reuse->type = EXPR_POS;
1809 reuse->ctype = ctype;
1810 reuse->init_offset = offset;
1811 reuse->init_nr = to - from;
1812 reuse->init_expr = entry;
1813 parent = &reuse->init_expr;
1814 entry = reuse;
1816 *ep = entry;
1818 if (accept_string && entry->type == EXPR_STRING) {
1819 sym = evaluate_expression(entry);
1820 to = from + get_expression_value(sym->array_size);
1821 } else {
1822 evaluate_initializer(ctype, parent);
1824 return to;
1827 static void evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1829 struct expression *entry;
1830 int current = 0;
1832 FOR_EACH_PTR(expr->expr_list, entry) {
1833 current = evaluate_one_array_initializer(ctype, THIS_ADDRESS(entry), current);
1834 } END_FOR_EACH_PTR(entry);
1837 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1838 static void evaluate_scalar_initializer(struct symbol *ctype, struct expression *expr)
1840 if (expression_list_size(expr->expr_list) != 1) {
1841 warning(expr->pos, "unexpected compound initializer");
1842 return;
1844 evaluate_array_initializer(ctype, expr);
1845 return;
1848 static struct symbol *find_struct_ident(struct symbol *ctype, struct ident *ident)
1850 struct symbol *sym;
1852 FOR_EACH_PTR(ctype->symbol_list, sym) {
1853 if (sym->ident == ident)
1854 return sym;
1855 } END_FOR_EACH_PTR(sym);
1856 return NULL;
1859 static int evaluate_one_struct_initializer(struct symbol *ctype, struct expression **ep, struct symbol *sym)
1861 struct expression *entry = *ep;
1862 struct expression **parent;
1863 struct expression *reuse = NULL;
1864 unsigned long offset;
1866 if (!sym) {
1867 error(entry->pos, "unknown named initializer");
1868 return -1;
1871 if (entry->type == EXPR_IDENTIFIER) {
1872 reuse = entry;
1873 entry = entry->ident_expression;
1876 parent = ep;
1877 offset = sym->offset;
1878 if (offset) {
1879 if (!reuse)
1880 reuse = alloc_expression(entry->pos, EXPR_POS);
1881 reuse->type = EXPR_POS;
1882 reuse->ctype = sym;
1883 reuse->init_offset = offset;
1884 reuse->init_nr = 1;
1885 reuse->init_expr = entry;
1886 parent = &reuse->init_expr;
1887 entry = reuse;
1889 *ep = entry;
1890 evaluate_initializer(sym, parent);
1891 return 0;
1894 static void evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1896 struct expression *entry;
1897 struct symbol *sym;
1899 PREPARE_PTR_LIST(ctype->symbol_list, sym);
1900 FOR_EACH_PTR(expr->expr_list, entry) {
1901 if (entry->type == EXPR_IDENTIFIER) {
1902 struct ident *ident = entry->expr_ident;
1903 /* We special-case the "already right place" case */
1904 if (!sym || sym->ident != ident) {
1905 RESET_PTR_LIST(sym);
1906 for (;;) {
1907 if (!sym)
1908 break;
1909 if (sym->ident == ident)
1910 break;
1911 NEXT_PTR_LIST(sym);
1915 if (evaluate_one_struct_initializer(ctype, THIS_ADDRESS(entry), sym))
1916 return;
1917 NEXT_PTR_LIST(sym);
1918 } END_FOR_EACH_PTR(entry);
1919 FINISH_PTR_LIST(sym);
1923 * Initializers are kind of like assignments. Except
1924 * they can be a hell of a lot more complex.
1926 static void evaluate_initializer(struct symbol *ctype, struct expression **ep)
1928 struct expression *expr = *ep;
1931 * Simple non-structure/array initializers are the simple
1932 * case, and look (and parse) largely like assignments.
1934 switch (expr->type) {
1935 default: {
1936 int is_string = expr->type == EXPR_STRING;
1937 struct symbol *rtype = evaluate_expression(expr);
1938 if (rtype) {
1940 * Special case:
1941 * char array[] = "string"
1942 * should _not_ degenerate.
1944 if (!is_string || !is_string_type(ctype))
1945 rtype = degenerate(expr);
1946 compatible_assignment_types(expr, ctype, ep, rtype, "initializer", '=');
1948 return;
1951 case EXPR_INITIALIZER:
1952 expr->ctype = ctype;
1953 if (ctype->type == SYM_NODE)
1954 ctype = ctype->ctype.base_type;
1956 switch (ctype->type) {
1957 case SYM_ARRAY:
1958 case SYM_PTR:
1959 evaluate_array_initializer(get_base_type(ctype), expr);
1960 return;
1961 case SYM_UNION:
1962 evaluate_struct_or_union_initializer(ctype, expr, 0);
1963 return;
1964 case SYM_STRUCT:
1965 evaluate_struct_or_union_initializer(ctype, expr, 1);
1966 return;
1967 default:
1968 evaluate_scalar_initializer(ctype, expr);
1969 return;
1972 case EXPR_IDENTIFIER:
1973 if (ctype->type == SYM_NODE)
1974 ctype = ctype->ctype.base_type;
1975 if (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION) {
1976 error(expr->pos, "expected structure or union for '%s' dereference", show_ident(expr->expr_ident));
1977 show_symbol(ctype);
1978 return;
1980 evaluate_one_struct_initializer(ctype, ep,
1981 find_struct_ident(ctype, expr->expr_ident));
1982 return;
1984 case EXPR_INDEX:
1985 if (ctype->type == SYM_NODE)
1986 ctype = ctype->ctype.base_type;
1987 if (ctype->type != SYM_ARRAY) {
1988 error(expr->pos, "expected array");
1989 return;
1991 evaluate_one_array_initializer(ctype->ctype.base_type, ep, 0);
1992 return;
1994 case EXPR_POS:
1996 * An EXPR_POS expression has already been evaluated, and we don't
1997 * need to do anything more
1999 return;
2003 static int get_as(struct symbol *sym)
2005 int as;
2006 unsigned long mod;
2008 if (!sym)
2009 return 0;
2010 as = sym->ctype.as;
2011 mod = sym->ctype.modifiers;
2012 if (sym->type == SYM_NODE) {
2013 sym = sym->ctype.base_type;
2014 as |= sym->ctype.as;
2015 mod |= sym->ctype.modifiers;
2019 * At least for now, allow casting to a "unsigned long".
2020 * That's how we do things like pointer arithmetic and
2021 * store pointers to registers.
2023 if (sym == &ulong_ctype)
2024 return -1;
2026 if (sym && sym->type == SYM_PTR) {
2027 sym = get_base_type(sym);
2028 as |= sym->ctype.as;
2029 mod |= sym->ctype.modifiers;
2031 if (mod & MOD_FORCE)
2032 return -1;
2033 return as;
2036 static struct symbol *evaluate_cast(struct expression *expr)
2038 struct expression *target = expr->cast_expression;
2039 struct symbol *ctype = examine_symbol_type(expr->cast_type);
2040 enum type type;
2042 if (!target)
2043 return NULL;
2045 expr->ctype = ctype;
2046 expr->cast_type = ctype;
2049 * Special case: a cast can be followed by an
2050 * initializer, in which case we need to pass
2051 * the type value down to that initializer rather
2052 * than trying to evaluate it as an expression
2054 * A more complex case is when the initializer is
2055 * dereferenced as part of a post-fix expression.
2056 * We need to produce an expression that can be dereferenced.
2058 if (target->type == EXPR_INITIALIZER) {
2059 struct symbol *sym = expr->cast_type;
2060 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
2062 sym->initializer = expr->cast_expression;
2063 evaluate_symbol(sym);
2065 addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
2066 addr->symbol = sym;
2068 expr->type = EXPR_PREOP;
2069 expr->op = '*';
2070 expr->unop = addr;
2071 expr->ctype = sym;
2073 return sym;
2076 evaluate_expression(target);
2077 degenerate(target);
2080 * You can always throw a value away by casting to
2081 * "void" - that's an implicit "force". Note that
2082 * the same is _not_ true of "void *".
2084 if (ctype == &void_ctype)
2085 goto out;
2087 type = ctype->type;
2088 if (type == SYM_NODE) {
2089 type = ctype->ctype.base_type->type;
2090 if (ctype->ctype.base_type == &void_ctype)
2091 goto out;
2093 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2094 warning(expr->pos, "cast to non-scalar");
2096 if (!target->ctype) {
2097 warning(expr->pos, "cast from unknown type");
2098 goto out;
2101 type = target->ctype->type;
2102 if (type == SYM_NODE)
2103 type = target->ctype->ctype.base_type->type;
2104 if (type == SYM_ARRAY || type == SYM_UNION || type == SYM_STRUCT)
2105 warning(expr->pos, "cast from non-scalar");
2107 if (!get_as(ctype) && get_as(target->ctype) > 0)
2108 warning(expr->pos, "cast removes address space of expression");
2110 if (!(ctype->ctype.modifiers & MOD_FORCE)) {
2111 struct symbol *t1 = ctype, *t2 = target->ctype;
2112 if (t1->type == SYM_NODE)
2113 t1 = t1->ctype.base_type;
2114 if (t2->type == SYM_NODE)
2115 t2 = t2->ctype.base_type;
2116 if (t1 != t2) {
2117 if (t1->type == SYM_RESTRICT)
2118 warning(expr->pos, "cast to restricted type");
2119 if (t2->type == SYM_RESTRICT)
2120 warning(expr->pos, "cast from restricted type");
2125 * Casts of constant values are special: they
2126 * can be NULL, and thus need to be simplified
2127 * early.
2129 if (target->type == EXPR_VALUE)
2130 cast_value(expr, ctype, target, target->ctype);
2132 out:
2133 return ctype;
2137 * Evaluate a call expression with a symbol. This
2138 * should expand inline functions, and evaluate
2139 * builtins.
2141 static int evaluate_symbol_call(struct expression *expr)
2143 struct expression *fn = expr->fn;
2144 struct symbol *ctype = fn->ctype;
2146 if (fn->type != EXPR_PREOP)
2147 return 0;
2149 if (ctype->op && ctype->op->evaluate)
2150 return ctype->op->evaluate(expr);
2152 if (ctype->ctype.modifiers & MOD_INLINE) {
2153 int ret;
2154 struct symbol *curr = current_fn;
2155 current_fn = ctype->ctype.base_type;
2156 examine_fn_arguments(current_fn);
2158 ret = inline_function(expr, ctype);
2160 /* restore the old function */
2161 current_fn = curr;
2162 return ret;
2165 return 0;
2168 static struct symbol *evaluate_call(struct expression *expr)
2170 int args, fnargs;
2171 struct symbol *ctype, *sym;
2172 struct expression *fn = expr->fn;
2173 struct expression_list *arglist = expr->args;
2175 if (!evaluate_expression(fn))
2176 return NULL;
2177 sym = ctype = fn->ctype;
2178 if (ctype->type == SYM_NODE)
2179 ctype = ctype->ctype.base_type;
2180 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
2181 ctype = get_base_type(ctype);
2182 if (!evaluate_arguments(sym, ctype, arglist))
2183 return NULL;
2184 if (ctype->type != SYM_FN) {
2185 warning(expr->pos, "not a function %s", show_ident(sym->ident));
2186 return NULL;
2188 args = expression_list_size(expr->args);
2189 fnargs = symbol_list_size(ctype->arguments);
2190 if (args < fnargs)
2191 warning(expr->pos, "not enough arguments for function %s", show_ident(sym->ident));
2192 if (args > fnargs && !ctype->variadic)
2193 warning(expr->pos, "too many arguments for function %s", show_ident(sym->ident));
2194 if (sym->type == SYM_NODE) {
2195 if (evaluate_symbol_call(expr))
2196 return expr->ctype;
2198 expr->ctype = ctype->ctype.base_type;
2199 return expr->ctype;
2202 struct symbol *evaluate_expression(struct expression *expr)
2204 if (!expr)
2205 return NULL;
2206 if (expr->ctype)
2207 return expr->ctype;
2209 switch (expr->type) {
2210 case EXPR_VALUE:
2211 case EXPR_FVALUE:
2212 warning(expr->pos, "value expression without a type");
2213 return NULL;
2214 case EXPR_STRING:
2215 return evaluate_string(expr);
2216 case EXPR_SYMBOL:
2217 return evaluate_symbol_expression(expr);
2218 case EXPR_BINOP:
2219 if (!evaluate_expression(expr->left))
2220 return NULL;
2221 if (!evaluate_expression(expr->right))
2222 return NULL;
2223 return evaluate_binop(expr);
2224 case EXPR_LOGICAL:
2225 return evaluate_logical(expr);
2226 case EXPR_COMMA:
2227 evaluate_expression(expr->left);
2228 if (!evaluate_expression(expr->right))
2229 return NULL;
2230 return evaluate_comma(expr);
2231 case EXPR_COMPARE:
2232 if (!evaluate_expression(expr->left))
2233 return NULL;
2234 if (!evaluate_expression(expr->right))
2235 return NULL;
2236 return evaluate_compare(expr);
2237 case EXPR_ASSIGNMENT:
2238 if (!evaluate_expression(expr->left))
2239 return NULL;
2240 if (!evaluate_expression(expr->right))
2241 return NULL;
2242 return evaluate_assignment(expr);
2243 case EXPR_PREOP:
2244 if (!evaluate_expression(expr->unop))
2245 return NULL;
2246 return evaluate_preop(expr);
2247 case EXPR_POSTOP:
2248 if (!evaluate_expression(expr->unop))
2249 return NULL;
2250 return evaluate_postop(expr);
2251 case EXPR_CAST:
2252 case EXPR_IMPLIED_CAST:
2253 return evaluate_cast(expr);
2254 case EXPR_SIZEOF:
2255 return evaluate_sizeof(expr);
2256 case EXPR_PTRSIZEOF:
2257 return evaluate_ptrsizeof(expr);
2258 case EXPR_ALIGNOF:
2259 return evaluate_alignof(expr);
2260 case EXPR_DEREF:
2261 return evaluate_member_dereference(expr);
2262 case EXPR_CALL:
2263 return evaluate_call(expr);
2264 case EXPR_SELECT:
2265 case EXPR_CONDITIONAL:
2266 return evaluate_conditional_expression(expr);
2267 case EXPR_STATEMENT:
2268 expr->ctype = evaluate_statement(expr->statement);
2269 return expr->ctype;
2271 case EXPR_LABEL:
2272 expr->ctype = &ptr_ctype;
2273 return &ptr_ctype;
2275 case EXPR_TYPE:
2276 /* Evaluate the type of the symbol .. */
2277 evaluate_symbol(expr->symbol);
2278 /* .. but the type of the _expression_ is a "type" */
2279 expr->ctype = &type_ctype;
2280 return &type_ctype;
2282 /* These can not exist as stand-alone expressions */
2283 case EXPR_INITIALIZER:
2284 case EXPR_IDENTIFIER:
2285 case EXPR_INDEX:
2286 case EXPR_POS:
2287 warning(expr->pos, "internal front-end error: initializer in expression");
2288 return NULL;
2289 case EXPR_SLICE:
2290 warning(expr->pos, "internal front-end error: SLICE re-evaluated");
2291 return NULL;
2293 return NULL;
2296 static void check_duplicates(struct symbol *sym)
2298 int declared = 0;
2299 struct symbol *next = sym;
2301 while ((next = next->same_symbol) != NULL) {
2302 const char *typediff;
2303 evaluate_symbol(next);
2304 declared++;
2305 typediff = type_difference(sym, next, 0, 0);
2306 if (typediff) {
2307 warning(sym->pos, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
2308 show_ident(sym->ident),
2309 stream_name(next->pos.stream), next->pos.line, typediff);
2310 return;
2313 if (!declared) {
2314 unsigned long mod = sym->ctype.modifiers;
2315 if (mod & (MOD_STATIC | MOD_REGISTER))
2316 return;
2317 if (!(mod & MOD_TOPLEVEL))
2318 return;
2319 if (sym->ident == &main_ident)
2320 return;
2321 warning(sym->pos, "symbol '%s' was not declared. Should it be static?", show_ident(sym->ident));
2325 static struct symbol *evaluate_symbol(struct symbol *sym)
2327 struct symbol *base_type;
2329 if (!sym)
2330 return sym;
2332 sym = examine_symbol_type(sym);
2333 base_type = get_base_type(sym);
2334 if (!base_type)
2335 return NULL;
2337 /* Evaluate the initializers */
2338 if (sym->initializer)
2339 evaluate_initializer(sym, &sym->initializer);
2341 /* And finally, evaluate the body of the symbol too */
2342 if (base_type->type == SYM_FN) {
2343 struct symbol *curr = current_fn;
2345 current_fn = base_type;
2347 examine_fn_arguments(base_type);
2348 if (!base_type->stmt && base_type->inline_stmt)
2349 uninline(sym);
2350 if (base_type->stmt)
2351 evaluate_statement(base_type->stmt);
2353 current_fn = curr;
2356 return base_type;
2359 void evaluate_symbol_list(struct symbol_list *list)
2361 struct symbol *sym;
2363 FOR_EACH_PTR(list, sym) {
2364 check_duplicates(sym);
2365 evaluate_symbol(sym);
2366 } END_FOR_EACH_PTR(sym);
2369 static struct symbol *evaluate_return_expression(struct statement *stmt)
2371 struct expression *expr = stmt->expression;
2372 struct symbol *ctype, *fntype;
2374 evaluate_expression(expr);
2375 ctype = degenerate(expr);
2376 fntype = current_fn->ctype.base_type;
2377 if (!fntype || fntype == &void_ctype) {
2378 if (expr && ctype != &void_ctype)
2379 warning(expr->pos, "return expression in %s function", fntype?"void":"typeless");
2380 return NULL;
2383 if (!expr) {
2384 warning(stmt->pos, "return with no return value");
2385 return NULL;
2387 if (!ctype)
2388 return NULL;
2389 compatible_assignment_types(expr, fntype, &stmt->expression, ctype, "return expression", '=');
2390 return NULL;
2393 static void evaluate_if_statement(struct statement *stmt)
2395 if (!stmt->if_conditional)
2396 return;
2398 evaluate_conditional(stmt->if_conditional, 0);
2399 evaluate_statement(stmt->if_true);
2400 evaluate_statement(stmt->if_false);
2403 static void evaluate_iterator(struct statement *stmt)
2405 evaluate_conditional(stmt->iterator_pre_condition, 1);
2406 evaluate_conditional(stmt->iterator_post_condition,1);
2407 evaluate_statement(stmt->iterator_pre_statement);
2408 evaluate_statement(stmt->iterator_statement);
2409 evaluate_statement(stmt->iterator_post_statement);
2412 static void verify_output_constraint(struct expression *expr, const char *constraint)
2414 switch (*constraint) {
2415 case '=': /* Assignment */
2416 case '+': /* Update */
2417 break;
2418 default:
2419 warning(expr->pos, "output constraint is not an assignment constraint (\"%s\")", constraint);
2423 static void verify_input_constraint(struct expression *expr, const char *constraint)
2425 switch (*constraint) {
2426 case '=': /* Assignment */
2427 case '+': /* Update */
2428 warning(expr->pos, "input constraint with assignment (\"%s\")", constraint);
2432 static void evaluate_asm_statement(struct statement *stmt)
2434 struct expression *expr;
2435 int state;
2437 expr = stmt->asm_string;
2438 if (!expr || expr->type != EXPR_STRING) {
2439 warning(stmt->pos, "need constant string for inline asm");
2440 return;
2443 state = 0;
2444 FOR_EACH_PTR(stmt->asm_outputs, expr) {
2445 struct ident *ident;
2447 switch (state) {
2448 case 0: /* Identifier */
2449 state = 1;
2450 ident = (struct ident *)expr;
2451 continue;
2453 case 1: /* Constraint */
2454 state = 2;
2455 if (!expr || expr->type != EXPR_STRING) {
2456 warning(expr ? expr->pos : stmt->pos, "asm output constraint is not a string");
2457 *THIS_ADDRESS(expr) = NULL;
2458 continue;
2460 verify_output_constraint(expr, expr->string->data);
2461 continue;
2463 case 2: /* Expression */
2464 state = 0;
2465 if (!evaluate_expression(expr))
2466 return;
2467 if (!lvalue_expression(expr))
2468 warning(expr->pos, "asm output is not an lvalue");
2469 evaluate_assign_to(expr, expr->ctype);
2470 continue;
2472 } END_FOR_EACH_PTR(expr);
2474 state = 0;
2475 FOR_EACH_PTR(stmt->asm_inputs, expr) {
2476 struct ident *ident;
2478 switch (state) {
2479 case 0: /* Identifier */
2480 state = 1;
2481 ident = (struct ident *)expr;
2482 continue;
2484 case 1: /* Constraint */
2485 state = 2;
2486 if (!expr || expr->type != EXPR_STRING) {
2487 warning(expr ? expr->pos : stmt->pos, "asm input constraint is not a string");
2488 *THIS_ADDRESS(expr) = NULL;
2489 continue;
2491 verify_input_constraint(expr, expr->string->data);
2492 continue;
2494 case 2: /* Expression */
2495 state = 0;
2496 if (!evaluate_expression(expr))
2497 return;
2498 continue;
2500 } END_FOR_EACH_PTR(expr);
2502 FOR_EACH_PTR(stmt->asm_clobbers, expr) {
2503 if (!expr) {
2504 warning(stmt->pos, "bad asm output");
2505 return;
2507 if (expr->type == EXPR_STRING)
2508 continue;
2509 warning(expr->pos, "asm clobber is not a string");
2510 } END_FOR_EACH_PTR(expr);
2513 struct symbol *evaluate_statement(struct statement *stmt)
2515 if (!stmt)
2516 return NULL;
2518 switch (stmt->type) {
2519 case STMT_RETURN:
2520 return evaluate_return_expression(stmt);
2522 case STMT_EXPRESSION:
2523 if (!evaluate_expression(stmt->expression))
2524 return NULL;
2525 return degenerate(stmt->expression);
2527 case STMT_COMPOUND: {
2528 struct statement *s;
2529 struct symbol *type = NULL;
2530 struct symbol *sym;
2532 /* Evaluate each symbol in the compound statement */
2533 FOR_EACH_PTR(stmt->syms, sym) {
2534 evaluate_symbol(sym);
2535 } END_FOR_EACH_PTR(sym);
2536 evaluate_symbol(stmt->ret);
2539 * Then, evaluate each statement, making the type of the
2540 * compound statement be the type of the last statement
2542 type = NULL;
2543 FOR_EACH_PTR(stmt->stmts, s) {
2544 type = evaluate_statement(s);
2545 } END_FOR_EACH_PTR(s);
2546 if (!type)
2547 type = &void_ctype;
2548 return type;
2550 case STMT_IF:
2551 evaluate_if_statement(stmt);
2552 return NULL;
2553 case STMT_ITERATOR:
2554 evaluate_iterator(stmt);
2555 return NULL;
2556 case STMT_SWITCH:
2557 evaluate_expression(stmt->switch_expression);
2558 evaluate_statement(stmt->switch_statement);
2559 return NULL;
2560 case STMT_CASE:
2561 evaluate_expression(stmt->case_expression);
2562 evaluate_expression(stmt->case_to);
2563 evaluate_statement(stmt->case_statement);
2564 return NULL;
2565 case STMT_LABEL:
2566 return evaluate_statement(stmt->label_statement);
2567 case STMT_GOTO:
2568 evaluate_expression(stmt->goto_expression);
2569 return NULL;
2570 case STMT_NONE:
2571 break;
2572 case STMT_ASM:
2573 evaluate_asm_statement(stmt);
2574 return NULL;
2575 case STMT_CONTEXT:
2576 evaluate_expression(stmt->expression);
2577 return NULL;
2578 case STMT_RANGE:
2579 evaluate_expression(stmt->range_expression);
2580 evaluate_expression(stmt->range_low);
2581 evaluate_expression(stmt->range_high);
2582 return NULL;
2584 return NULL;