Turn 'break'/'continue' statements into goto's with
[smatch.git] / evaluate.c
blobf018d2148bd9529196ee3abdbe4e660d44719874
1 /*
2 * sparse/evaluate.c
4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * Evaluate constant expressions.
7 */
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <limits.h>
18 #include "lib.h"
19 #include "parse.h"
20 #include "token.h"
21 #include "symbol.h"
22 #include "target.h"
23 #include "expression.h"
25 static struct symbol *current_fn;
26 static int current_context, current_contextmask;
28 static struct symbol *evaluate_symbol_expression(struct expression *expr)
30 struct symbol *sym = expr->symbol;
31 struct symbol *base_type;
33 if (!sym) {
34 if (preprocessing) {
35 warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
36 expr->type = EXPR_VALUE;
37 expr->value = 0;
38 expr->ctype = &int_ctype;
39 return &int_ctype;
41 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
42 return NULL;
45 examine_symbol_type(sym);
46 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
47 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
49 base_type = sym->ctype.base_type;
50 if (!base_type) {
51 warn(sym->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
52 return NULL;
55 /* The type of a symbol is the symbol itself! */
56 expr->ctype = sym;
58 /* enum's can be turned into plain values */
59 if (base_type->type != SYM_ENUM) {
60 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
61 addr->symbol = sym;
62 addr->symbol_name = expr->symbol_name;
63 addr->ctype = &ptr_ctype;
64 expr->type = EXPR_PREOP;
65 expr->op = '*';
66 expr->unop = addr;
67 return base_type;
69 expr->type = EXPR_VALUE;
70 expr->value = sym->value;
71 return sym;
74 static struct symbol *evaluate_string(struct expression *expr)
76 struct symbol *sym = alloc_symbol(expr->pos, SYM_ARRAY);
77 int length = expr->string->length;
79 sym->array_size = length;
80 sym->bit_size = BITS_IN_CHAR * length;
81 sym->ctype.alignment = 1;
82 sym->ctype.modifiers = 0;
83 sym->ctype.base_type = &char_ctype;
84 expr->ctype = sym;
85 return sym;
88 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
90 unsigned long lmod, rmod, mod;
92 if (left == right)
93 return left;
95 if (left->bit_size > right->bit_size)
96 return left;
98 if (right->bit_size > left->bit_size)
99 return right;
101 /* Same size integers - promote to unsigned, promote to long */
102 lmod = left->ctype.modifiers;
103 rmod = right->ctype.modifiers;
104 mod = lmod | rmod;
105 if (mod == lmod)
106 return left;
107 if (mod == rmod)
108 return right;
109 return ctype_integer(mod);
112 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
113 struct expression *old, struct symbol *oldtype)
115 int old_size = oldtype->bit_size;
116 int new_size = newtype->bit_size;
117 long long value, mask, ormask, andmask;
118 int is_signed;
120 // FIXME! We don't handle FP casts of constant values yet
121 if (newtype->ctype.base_type == &fp_type)
122 return NULL;
123 if (oldtype->ctype.base_type == &fp_type)
124 return NULL;
126 // For pointers and integers, we can just move the value around
127 expr->type = EXPR_VALUE;
128 if (old_size == new_size) {
129 expr->value = old->value;
130 return newtype;
133 // expand it to the full "long long" value
134 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
135 mask = 1ULL << (old_size-1);
136 value = old->value;
137 if (!(value & mask))
138 is_signed = 0;
139 andmask = mask | (mask-1);
140 ormask = ~andmask;
141 if (!is_signed)
142 ormask = 0;
143 value = (value & andmask) | ormask;
145 // Truncate it to the new size
146 mask = 1ULL << (new_size-1);
147 mask = mask | (mask-1);
148 expr->value = value & mask;
149 return newtype;
152 static struct expression * cast_to(struct expression *old, struct symbol *type)
154 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
155 expr->ctype = type;
156 expr->cast_type = type;
157 expr->cast_expression = old;
158 if (old->type == EXPR_VALUE)
159 cast_value(expr, type, old, old->ctype);
160 return expr;
163 static int is_ptr_type(struct symbol *type)
165 if (type->type == SYM_NODE)
166 type = type->ctype.base_type;
167 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
170 static int is_int_type(struct symbol *type)
172 if (type->type == SYM_NODE)
173 type = type->ctype.base_type;
174 return type->ctype.base_type == &int_type;
177 static struct symbol *bad_expr_type(struct expression *expr)
179 warn(expr->pos, "incompatible types for operation");
180 return NULL;
183 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
185 struct expression *left = *lp, *right = *rp;
186 struct symbol *ltype = left->ctype, *rtype = right->ctype;
188 if (ltype->type == SYM_NODE)
189 ltype = ltype->ctype.base_type;
190 if (rtype->type == SYM_NODE)
191 rtype = rtype->ctype.base_type;
192 /* Integer promotion? */
193 if (ltype->type == SYM_ENUM)
194 ltype = &int_ctype;
195 if (rtype->type == SYM_ENUM)
196 rtype = &int_ctype;
197 if (is_int_type(ltype) && is_int_type(rtype)) {
198 struct symbol *ctype = bigger_int_type(ltype, rtype);
200 /* Don't bother promoting same-size entities, it only adds clutter */
201 if (ltype->bit_size != ctype->bit_size)
202 *lp = cast_to(left, ctype);
203 if (rtype->bit_size != ctype->bit_size)
204 *rp = cast_to(right, ctype);
205 return ctype;
207 return NULL;
210 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
212 if (count >= ctype->bit_size) {
213 warn(expr->pos, "shift too big for type");
214 count &= ctype->bit_size-1;
216 return count;
220 * CAREFUL! We need to get the size and sign of the
221 * result right!
223 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
225 struct expression *left = expr->left, *right = expr->right;
226 unsigned long long v, l, r, mask;
227 signed long long s, sl, sr;
228 int is_signed, shift;
230 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
231 return;
232 l = left->value; r = right->value;
233 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
234 mask = 1ULL << (ctype->bit_size-1);
235 sl = l; sr = r;
236 if (is_signed && (sl & mask))
237 sl |= ~(mask-1);
238 if (is_signed && (sr & mask))
239 sr |= ~(mask-1);
241 switch (expr->op) {
242 case '+': v = l + r; s = v; break;
243 case '-': v = l - r; s = v; break;
244 case '&': v = l & r; s = v; break;
245 case '|': v = l | r; s = v; break;
246 case '^': v = l ^ r; s = v; break;
247 case '*': v = l * r; s = sl * sr; break;
248 case '/': if (!r) return; v = l / r; s = sl / sr; break;
249 case '%': if (!r) return; v = l % r; s = sl % sr; break;
250 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
251 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
252 case '<': v = l < r; s = sl < sr; break;
253 case '>': v = l > r; s = sl > sr; break;
254 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
255 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
256 case SPECIAL_EQUAL: v = l == r; s = v; break;
257 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
258 default: return;
260 if (is_signed)
261 v = s;
262 mask = mask | (mask-1);
263 expr->value = v & mask;
264 expr->type = EXPR_VALUE;
267 static struct symbol *evaluate_int_binop(struct expression *expr)
269 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
270 if (ctype) {
271 expr->ctype = ctype;
272 simplify_int_binop(expr, ctype);
273 return ctype;
275 return bad_expr_type(expr);
278 static inline int lvalue_expression(struct expression *expr)
280 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
283 /* Arrays degenerate into pointers on pointer arithmetic */
284 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
286 if (ctype->type == SYM_ARRAY) {
287 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
288 struct expression *ptr;
290 sym->ctype = ctype->ctype;
291 sym->bit_size = BITS_IN_POINTER;
292 sym->ctype.alignment = POINTER_ALIGNMENT;
293 ctype = sym;
295 if (expr->type != EXPR_STRING) {
296 ptr = *ptr_p;
297 *ptr_p = ptr->unop;
300 * This all really assumes that we got the degenerate
301 * array as an lvalue (ie a dereference). If that
302 * is not the case, then holler - because we've screwed
303 * up.
305 if (!lvalue_expression(ptr))
306 warn(ptr->pos, "internal error: strange degenerate array case");
309 return ctype;
312 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
314 struct symbol *ctype;
315 struct symbol *ptr_type = ptr->ctype;
316 struct symbol *i_type = i->ctype;
317 int bit_size;
319 if (i_type->type == SYM_NODE)
320 i_type = i_type->ctype.base_type;
321 if (ptr_type->type == SYM_NODE)
322 ptr_type = ptr_type->ctype.base_type;
324 if (i_type->type == SYM_ENUM)
325 i_type = &int_ctype;
326 if (!is_int_type(i_type))
327 return bad_expr_type(expr);
329 ctype = ptr->ctype;
330 examine_symbol_type(ctype);
332 ctype = degenerate(expr, ctype, &ptr);
333 bit_size = ctype->bit_size;
335 /* Special case: adding zero commonly happens as a result of 'array[0]' */
336 if (i->type == EXPR_VALUE && !i->value) {
337 *expr = *ptr;
338 } else if (bit_size > BITS_IN_CHAR) {
339 struct expression *add = expr;
340 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
341 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
343 val->ctype = size_t_ctype;
344 val->value = bit_size >> 3;
346 mul->op = '*';
347 mul->ctype = size_t_ctype;
348 mul->left = i;
349 mul->right = val;
350 simplify_int_binop(mul, size_t_ctype);
352 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
353 add->left = ptr;
354 add->right = mul;
355 simplify_int_binop(add, ctype);
358 expr->ctype = ctype;
359 return ctype;
362 static struct symbol *evaluate_add(struct expression *expr)
364 struct expression *left = expr->left, *right = expr->right;
365 struct symbol *ltype = left->ctype, *rtype = right->ctype;
367 if (is_ptr_type(ltype))
368 return evaluate_ptr_add(expr, left, right);
370 if (is_ptr_type(rtype))
371 return evaluate_ptr_add(expr, right, left);
373 // FIXME! FP promotion
374 return evaluate_int_binop(expr);
377 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
379 static const char * type_difference(struct symbol *target, struct symbol *source,
380 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
382 for (;;) {
383 unsigned long mod1, mod2, diff;
384 unsigned long as1, as2;
386 if (target == source)
387 break;
388 if (!target || !source)
389 return "different types";
391 * Peel of per-node information.
392 * FIXME! Check alignment, address space, and context too here!
394 mod1 = target->ctype.modifiers;
395 as1 = target->ctype.as;
396 mod2 = source->ctype.modifiers;
397 as2 = source->ctype.as;
398 if (target->type == SYM_NODE) {
399 target = target->ctype.base_type;
400 mod1 |= target->ctype.modifiers;
401 as1 |= target->ctype.as;
403 if (source->type == SYM_NODE) {
404 source = source->ctype.base_type;
405 mod2 |= source->ctype.modifiers;
406 as2 |= source->ctype.as;
409 if (target->type != source->type) {
410 int type1 = target->type;
411 int type2 = source->type;
413 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
414 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
415 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
416 if (type1 != type2)
417 return "different base types";
420 /* Must be same address space to be comparable */
421 if (as1 != as2)
422 return "different address spaces";
424 /* Ignore differences in storage types, sign, or addressability */
425 diff = (mod1 ^ mod2) & ~(MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED);
426 if (diff) {
427 mod1 &= diff & ~target_mod_ignore;
428 mod2 &= diff & ~source_mod_ignore;
429 if (mod1 | mod2) {
430 if ((mod1 | mod2) & MOD_SIZE)
431 return "different type sizes";
432 return "different modifiers";
436 target = target->ctype.base_type;
437 source = source->ctype.base_type;
439 return NULL;
442 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
444 /* NULL expression? Just return the type of the "other side" */
445 if (r->type == EXPR_VALUE && !r->value)
446 return l->ctype;
447 if (l->type == EXPR_VALUE && !l->value)
448 return r->ctype;
449 return NULL;
453 * Ignore differences in "volatile" and "const"ness when
454 * subtracting pointers
456 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
458 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
460 const char *typediff;
461 struct symbol *ctype;
462 struct symbol *ltype = l->ctype, *rtype = r->ctype;
465 * If it is an integer subtract: the ptr add case will do the
466 * right thing.
468 if (!is_ptr_type(rtype))
469 return evaluate_ptr_add(expr, l, r);
471 ctype = ltype;
472 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
473 if (typediff) {
474 ctype = common_ptr_type(l, r);
475 if (!ctype) {
476 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
477 return NULL;
480 examine_symbol_type(ctype);
482 /* Figure out the base type we point to */
483 if (ctype->type == SYM_NODE)
484 ctype = ctype->ctype.base_type;
485 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
486 warn(expr->pos, "subtraction of functions? Share your drugs");
487 return NULL;
489 ctype = ctype->ctype.base_type;
491 expr->ctype = ssize_t_ctype;
492 if (ctype->bit_size > BITS_IN_CHAR) {
493 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
494 struct expression *div = expr;
495 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
497 val->ctype = size_t_ctype;
498 val->value = ctype->bit_size >> 3;
500 sub->op = '-';
501 sub->ctype = ssize_t_ctype;
502 sub->left = l;
503 sub->right = r;
505 div->op = '/';
506 div->left = sub;
507 div->right = val;
510 return ssize_t_ctype;
513 static struct symbol *evaluate_sub(struct expression *expr)
515 struct expression *left = expr->left, *right = expr->right;
516 struct symbol *ltype = left->ctype;
518 if (is_ptr_type(ltype))
519 return evaluate_ptr_sub(expr, left, right);
521 // FIXME! FP promotion
522 return evaluate_int_binop(expr);
525 static struct symbol *evaluate_logical(struct expression *expr)
527 struct expression *left = expr->left;
528 struct expression *right;
530 if (!evaluate_expression(left))
531 return NULL;
533 /* Do immediate short-circuiting ... */
534 if (left->type == EXPR_VALUE) {
535 if (expr->op == SPECIAL_LOGICAL_AND) {
536 if (!left->value) {
537 expr->type = EXPR_VALUE;
538 expr->value = 0;
539 expr->ctype = &bool_ctype;
540 return &bool_ctype;
542 } else {
543 if (left->value) {
544 expr->type = EXPR_VALUE;
545 expr->value = 1;
546 expr->ctype = &bool_ctype;
547 return &bool_ctype;
552 right = expr->right;
553 if (!evaluate_expression(right))
554 return NULL;
555 expr->ctype = &bool_ctype;
556 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
558 * We know the left value doesn't matter, since
559 * otherwise we would have short-circuited it..
561 expr->type = EXPR_VALUE;
562 expr->value = right->value;
565 return &bool_ctype;
568 static struct symbol *evaluate_arithmetic(struct expression *expr)
570 // FIXME! Floating-point promotion!
571 return evaluate_int_binop(expr);
574 static struct symbol *evaluate_binop(struct expression *expr)
576 switch (expr->op) {
577 // addition can take ptr+int, fp and int
578 case '+':
579 return evaluate_add(expr);
581 // subtraction can take ptr-ptr, fp and int
582 case '-':
583 return evaluate_sub(expr);
585 // Arithmetic operations can take fp and int
586 case '*': case '/': case '%':
587 return evaluate_arithmetic(expr);
589 // The rest are integer operations (bitops)
590 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
591 // '&', '^', '|'
592 default:
593 return evaluate_int_binop(expr);
597 static struct symbol *evaluate_comma(struct expression *expr)
599 expr->ctype = expr->right->ctype;
600 return expr->ctype;
603 static struct symbol *evaluate_compare(struct expression *expr)
605 struct expression *left = expr->left, *right = expr->right;
606 struct symbol *ltype = left->ctype, *rtype = right->ctype;
607 struct symbol *ctype;
609 /* Pointer types? */
610 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
611 expr->ctype = &bool_ctype;
612 // FIXME! Check the types for compatibility
613 return &bool_ctype;
616 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
617 if (ctype) {
618 simplify_int_binop(expr, ctype);
619 expr->ctype = &bool_ctype;
620 return &bool_ctype;
623 return bad_expr_type(expr);
626 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
628 /* Integer promotion? */
629 if (ltype->type == SYM_NODE)
630 ltype = ltype->ctype.base_type;
631 if (rtype->type == SYM_NODE)
632 rtype = rtype->ctype.base_type;
633 if (ltype->type == SYM_ENUM)
634 ltype = &int_ctype;
635 if (rtype->type == SYM_ENUM)
636 rtype = &int_ctype;
637 return (is_int_type(ltype) && is_int_type(rtype));
640 static int is_void_ptr(struct expression *expr)
642 return (expr->type == EXPR_VALUE &&
643 expr->value == 0);
647 * FIXME!! This shoul ddo casts, array degeneration etc..
649 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
651 struct symbol *ltype = left->ctype, *rtype = right->ctype;
653 if (ltype->type == SYM_PTR) {
654 if (is_void_ptr(right))
655 return ltype;
658 if (rtype->type == SYM_PTR) {
659 if (is_void_ptr(left))
660 return rtype;
662 return NULL;
665 static struct symbol * evaluate_conditional(struct expression *expr)
667 struct expression *cond, *true, *false;
668 struct symbol *ctype, *ltype, *rtype;
669 const char * typediff;
671 cond = expr->conditional;
672 true = expr->cond_true ? : cond;
673 false = expr->cond_false;
675 ltype = true->ctype;
676 rtype = false->ctype;
678 ctype = ltype;
679 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
680 if (typediff) {
681 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
682 if (!ctype) {
683 ctype = compatible_ptr_type(true, expr->cond_false);
684 if (!ctype) {
685 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
686 return NULL;
691 /* Simplify conditional expression.. */
692 if (cond->type == EXPR_VALUE) {
693 if (!cond->value)
694 true = false;
695 *expr = *true;
697 expr->ctype = ctype;
698 return ctype;
701 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
702 struct expression **rp, struct symbol *source, const char *where)
704 const char *typediff;
705 struct symbol *t;
706 int target_as;
708 /* It's ok if the target is more volatile or const than the source */
709 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
710 if (!typediff)
711 return 1;
713 if (compatible_integer_types(target, source)) {
714 if (target->bit_size != source->bit_size)
715 *rp = cast_to(*rp, target);
716 return 1;
719 /* Pointer destination? */
720 t = target;
721 target_as = t->ctype.as;
722 if (t->type == SYM_NODE) {
723 t = t->ctype.base_type;
724 target_as |= t->ctype.as;
726 if (t->type == SYM_PTR) {
727 struct expression *right = *rp;
728 struct symbol *s = source;
729 int source_as;
731 // NULL pointer is always ok
732 if (right->type == EXPR_VALUE && !right->value)
733 return 1;
735 /* "void *" matches anything as long as the address space is ok */
736 source_as = s->ctype.as;
737 if (s->type == SYM_NODE) {
738 s = s->ctype.base_type;
739 source_as |= s->ctype.as;
741 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
742 s = s->ctype.base_type;
743 t = t->ctype.base_type;
744 if (s == &void_ctype || t == &void_ctype)
745 return 1;
748 if (s->type == SYM_FN) {
749 typediff = type_difference(t->ctype.base_type, s, 0, 0);
750 if (!typediff)
751 return 1;
754 // FIXME!! Cast it!
755 warn(expr->pos, "different types in %s (%s)", where, typediff);
756 return 0;
759 // FIXME!! Cast it?
760 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
761 warn(expr->pos, " expected %s", show_typename(target));
762 warn(expr->pos, " got %s", show_typename(source));
763 return 0;
767 * FIXME!! This is wrong from a double evaluation standpoint. We can't
768 * just expand the expression twice, that would make any side effects
769 * happen twice too.
771 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
773 int op = expr->op;
774 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
775 static const int op_trans[] = {
776 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
777 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
778 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
779 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
780 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
781 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
782 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
783 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
784 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
785 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
788 subexpr->left = left;
789 subexpr->right = right;
790 subexpr->op = op_trans[op - SPECIAL_BASE];
791 expr->op = '=';
792 expr->right = subexpr;
793 return evaluate_binop(subexpr);
796 static struct symbol *evaluate_assignment(struct expression *expr)
798 struct expression *left = expr->left, *right = expr->right;
799 struct symbol *ltype, *rtype;
801 ltype = left->ctype;
802 rtype = right->ctype;
803 if (expr->op != '=') {
804 rtype = evaluate_binop_assignment(expr, left, right);
805 if (!rtype)
806 return 0;
807 right = expr->right;
810 if (!lvalue_expression(left)) {
811 warn(expr->pos, "not an lvalue");
812 return NULL;
815 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
816 return 0;
818 expr->ctype = expr->left->ctype;
819 return expr->ctype;
822 static struct symbol *evaluate_addressof(struct expression *expr)
824 struct symbol *ctype, *symbol;
825 struct expression *op = expr->unop;
827 if (op->op != '*' || op->type != EXPR_PREOP) {
828 warn(expr->pos, "not addressable");
829 return NULL;
832 symbol = alloc_symbol(expr->pos, SYM_PTR);
833 symbol->ctype.alignment = POINTER_ALIGNMENT;
834 symbol->bit_size = BITS_IN_POINTER;
836 ctype = op->ctype;
837 if (ctype->type == SYM_NODE) {
838 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
839 if (ctype->ctype.modifiers & MOD_REGISTER) {
840 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
841 ctype->ctype.modifiers &= ~MOD_REGISTER;
843 symbol->ctype.modifiers = ctype->ctype.modifiers;
844 symbol->ctype.as = ctype->ctype.as;
845 symbol->ctype.context = ctype->ctype.context;
846 symbol->ctype.contextmask = ctype->ctype.contextmask;
847 ctype = ctype->ctype.base_type;
850 symbol->ctype.base_type = ctype;
851 *expr = *op->unop;
852 expr->ctype = symbol;
853 return symbol;
857 static struct symbol *evaluate_dereference(struct expression *expr)
859 struct expression *op = expr->unop;
860 struct symbol *ctype = op->ctype, *sym;
862 sym = alloc_symbol(expr->pos, SYM_NODE);
863 sym->ctype = ctype->ctype;
864 if (ctype->type == SYM_NODE) {
865 ctype = ctype->ctype.base_type;
866 sym->ctype.modifiers |= ctype->ctype.modifiers;
867 sym->ctype.as |= ctype->ctype.as;
869 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
870 warn(expr->pos, "cannot derefence this type");
871 return 0;
874 ctype = ctype->ctype.base_type;
875 if (!ctype) {
876 warn(expr->pos, "undefined type");
877 return NULL;
879 examine_symbol_type(ctype);
880 sym->ctype.base_type = ctype;
882 sym->bit_size = ctype->bit_size;
883 sym->array_size = ctype->array_size;
885 /* Simplify: *&(expr) => (expr) */
886 if (op->type == EXPR_PREOP && op->op == '&') {
887 *expr = *op->unop;
890 expr->ctype = sym;
891 return sym;
894 static void simplify_preop(struct expression *expr)
896 struct expression *op = expr->unop;
897 unsigned long long v, mask;
899 if (op->type != EXPR_VALUE)
900 return;
901 v = op->value;
902 switch (expr->op) {
903 case '+': break;
904 case '-': v = -v; break;
905 case '!': v = !v; break;
906 case '~': v = ~v; break;
907 default: return;
909 mask = 1ULL << (expr->ctype->bit_size-1);
910 mask = mask | (mask-1);
911 expr->value = v & mask;
912 expr->type = EXPR_VALUE;
916 * Unary post-ops: x++ and x--
918 static struct symbol *evaluate_postop(struct expression *expr)
920 struct expression *op = expr->unop;
921 struct symbol *ctype = op->ctype;
923 if (!lvalue_expression(expr->unop)) {
924 warn(expr->pos, "need lvalue expression for ++/--");
925 return NULL;
927 expr->ctype = ctype;
928 return ctype;
931 static struct symbol *evaluate_preop(struct expression *expr)
933 struct symbol *ctype = expr->unop->ctype;
935 switch (expr->op) {
936 case '(':
937 case '+':
938 *expr = *expr->unop;
939 return ctype;
941 case '*':
942 return evaluate_dereference(expr);
944 case '&':
945 return evaluate_addressof(expr);
947 case '!':
948 expr->ctype = &bool_ctype;
949 simplify_preop(expr);
950 return &bool_ctype;
952 case SPECIAL_INCREMENT:
953 case SPECIAL_DECREMENT:
955 * From a type evaluation standpoint the pre-ops are
956 * the same as the postops
958 return evaluate_postop(expr);
960 default:
961 expr->ctype = ctype;
962 simplify_preop(expr);
963 return ctype;
967 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
969 struct ptr_list *head = (struct ptr_list *)_list;
970 struct ptr_list *list = head;
972 if (!head)
973 return NULL;
974 do {
975 int i;
976 for (i = 0; i < list->nr; i++) {
977 struct symbol *sym = (struct symbol *) list->list[i];
978 if (sym->ident) {
979 if (sym->ident != ident)
980 continue;
981 *offset = sym->offset;
982 return sym;
983 } else {
984 struct symbol *ctype = sym->ctype.base_type;
985 struct symbol *sub;
986 if (!ctype)
987 continue;
988 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
989 continue;
990 sub = find_identifier(ident, ctype->symbol_list, offset);
991 if (!sub)
992 continue;
993 *offset += sym->offset;
994 return sub;
997 } while ((list = list->next) != head);
998 return NULL;
1001 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1003 struct expression *add;
1005 if (!offset)
1006 return expr;
1008 /* Create a new add-expression */
1009 add = alloc_expression(expr->pos, EXPR_BINOP);
1010 add->op = '+';
1011 add->ctype = &ptr_ctype;
1012 add->left = expr;
1013 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1014 add->right->ctype = &int_ctype;
1015 add->right->value = offset;
1017 simplify_int_binop(add, &ptr_ctype);
1018 return add;
1021 /* structure/union dereference */
1022 static struct symbol *evaluate_member_dereference(struct expression *expr)
1024 int offset;
1025 struct symbol *ctype, *member, *sym;
1026 struct expression *deref = expr->deref, *add;
1027 struct ident *ident = expr->member;
1028 unsigned int mod;
1029 int address_space;
1031 if (!evaluate_expression(deref))
1032 return NULL;
1033 if (!ident) {
1034 warn(expr->pos, "bad member name");
1035 return NULL;
1038 ctype = deref->ctype;
1039 mod = ctype->ctype.modifiers;
1040 address_space = ctype->ctype.as;
1041 if (ctype->type == SYM_NODE) {
1042 ctype = ctype->ctype.base_type;
1043 mod |= ctype->ctype.modifiers;
1045 if (expr->op == SPECIAL_DEREFERENCE) {
1046 /* Arrays will degenerate into pointers for '->' */
1047 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1048 warn(expr->pos, "expected a pointer to a struct/union");
1049 return NULL;
1051 address_space = ctype->ctype.as;
1052 ctype = ctype->ctype.base_type;
1053 mod |= ctype->ctype.modifiers;
1054 if (ctype->type == SYM_NODE) {
1055 ctype = ctype->ctype.base_type;
1056 mod |= ctype->ctype.modifiers;
1058 } else {
1059 if (!lvalue_expression(deref)) {
1060 warn(deref->pos, "expected lvalue for member dereference");
1061 return NULL;
1063 deref = deref->unop;
1064 expr->deref = deref;
1066 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1067 warn(expr->pos, "expected structure or union");
1068 return NULL;
1070 offset = 0;
1071 member = find_identifier(ident, ctype->symbol_list, &offset);
1072 if (!member) {
1073 warn(expr->pos, "no such struct/union member");
1074 return NULL;
1077 add = evaluate_offset(deref, offset);
1079 sym = alloc_symbol(expr->pos, SYM_NODE);
1080 sym->bit_size = member->bit_size;
1081 sym->array_size = member->array_size;
1082 sym->ctype = member->ctype;
1083 sym->ctype.modifiers = mod;
1084 sym->ctype.as = address_space;
1085 ctype = member->ctype.base_type;
1086 if (ctype->type == SYM_BITFIELD) {
1087 ctype = ctype->ctype.base_type;
1088 expr->type = EXPR_BITFIELD;
1089 expr->bitpos = member->bit_offset;
1090 expr->nrbits = member->fieldwidth;
1091 expr->address = add;
1092 } else {
1093 expr->type = EXPR_PREOP;
1094 expr->op = '*';
1095 expr->unop = add;
1098 expr->ctype = sym;
1099 return sym;
1102 static struct symbol *evaluate_sizeof(struct expression *expr)
1104 int size;
1106 if (expr->cast_type) {
1107 examine_symbol_type(expr->cast_type);
1108 size = expr->cast_type->bit_size;
1109 } else {
1110 if (!evaluate_expression(expr->cast_expression))
1111 return 0;
1112 size = expr->cast_expression->ctype->bit_size;
1114 if (size & 7) {
1115 warn(expr->pos, "cannot size expression");
1116 return 0;
1118 expr->type = EXPR_VALUE;
1119 expr->value = size >> 3;
1120 expr->ctype = size_t_ctype;
1121 return size_t_ctype;
1124 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
1126 struct expression *expr;
1127 struct symbol_list *argument_types = fn->arguments;
1128 struct symbol *argtype;
1130 PREPARE_PTR_LIST(argument_types, argtype);
1131 FOR_EACH_PTR (head, expr) {
1132 struct expression **p = THIS_ADDRESS(expr);
1133 struct symbol *ctype, *target;
1134 ctype = evaluate_expression(expr);
1136 if (!ctype)
1137 return 0;
1139 if (ctype->type == SYM_ARRAY) {
1140 ctype = degenerate(expr, ctype, p);
1141 expr->ctype = ctype;
1144 target = argtype;
1145 if (!target && ctype->bit_size < BITS_IN_INT)
1146 target = &int_ctype;
1147 if (target) {
1148 examine_symbol_type(target);
1149 compatible_assignment_types(expr, target, p, ctype, "argument passing");
1152 NEXT_PTR_LIST(argument_types, argtype);
1153 } END_FOR_EACH_PTR;
1154 FINISH_PTR_LIST;
1155 return 1;
1159 * FIXME! This is bogus: we need to take array index
1160 * entries into account when calculating the size of
1161 * the array.
1163 static int count_array_initializer(struct expression *expr)
1165 struct expression_list *list;
1167 if (expr->type != EXPR_INITIALIZER)
1168 return 1;
1169 list = expr->expr_list;
1170 return expression_list_size(list);
1173 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1175 return count_array_initializer(expr);
1178 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1180 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1181 return 0;
1185 * Initializers are kind of like assignments. Except
1186 * they can be a hell of a lot more complex.
1188 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1190 struct expression *expr = *ep;
1193 * Simple non-structure/array initializers are the simple
1194 * case, and look (and parse) largely like assignments.
1196 if (expr->type != EXPR_INITIALIZER) {
1197 int size = 0;
1198 struct symbol *rtype = evaluate_expression(expr);
1199 if (rtype) {
1200 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1201 /* strings are special: char arrays */
1202 if (rtype->type == SYM_ARRAY)
1203 size = rtype->array_size;
1205 return size;
1208 expr->ctype = ctype;
1209 switch (ctype->type) {
1210 case SYM_ARRAY:
1211 case SYM_PTR:
1212 return evaluate_array_initializer(ctype, expr);
1213 case SYM_UNION:
1214 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1215 case SYM_STRUCT:
1216 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1217 default:
1218 break;
1220 warn(expr->pos, "unexpected compound initializer");
1221 return 0;
1224 static struct symbol *evaluate_cast(struct expression *expr)
1226 struct expression *target = expr->cast_expression;
1227 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1229 expr->ctype = ctype;
1230 expr->cast_type = ctype;
1233 * Special case: a cast can be followed by an
1234 * initializer, in which case we need to pass
1235 * the type value down to that initializer rather
1236 * than trying to evaluate it as an expression
1238 if (target->type == EXPR_INITIALIZER) {
1239 evaluate_initializer(ctype, &expr->cast_expression);
1240 return ctype;
1243 evaluate_expression(target);
1245 /* Simplify normal integer casts.. */
1246 if (target->type == EXPR_VALUE)
1247 cast_value(expr, ctype, target, target->ctype);
1248 return ctype;
1251 static struct symbol *evaluate_call(struct expression *expr)
1253 int args, fnargs;
1254 struct symbol *ctype;
1255 struct expression *fn = expr->fn;
1256 struct expression_list *arglist = expr->args;
1258 if (!evaluate_expression(fn))
1259 return NULL;
1260 ctype = fn->ctype;
1261 if (ctype->type == SYM_NODE) {
1263 * FIXME!! We should really expand the inline functions.
1264 * For now we just mark them accessed so that they show
1265 * up on the list of used symbols.
1267 if (ctype->ctype.modifiers & MOD_INLINE)
1268 access_symbol(ctype);
1269 ctype = ctype->ctype.base_type;
1271 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1272 ctype = ctype->ctype.base_type;
1273 if (ctype->type != SYM_FN) {
1274 warn(expr->pos, "not a function");
1275 return NULL;
1277 if (!evaluate_arguments(ctype, arglist))
1278 return NULL;
1279 args = expression_list_size(expr->args);
1280 fnargs = symbol_list_size(ctype->arguments);
1281 if (args < fnargs)
1282 warn(expr->pos, "not enough arguments for function");
1283 if (args > fnargs && !ctype->variadic)
1284 warn(expr->pos, "too many arguments for function");
1285 expr->ctype = ctype->ctype.base_type;
1286 return expr->ctype;
1289 struct symbol *evaluate_expression(struct expression *expr)
1291 if (!expr)
1292 return NULL;
1293 if (expr->ctype)
1294 return expr->ctype;
1296 switch (expr->type) {
1297 case EXPR_VALUE:
1298 warn(expr->pos, "value expression without a type");
1299 return NULL;
1300 case EXPR_STRING:
1301 return evaluate_string(expr);
1302 case EXPR_SYMBOL:
1303 return evaluate_symbol_expression(expr);
1304 case EXPR_BINOP:
1305 if (!evaluate_expression(expr->left))
1306 return NULL;
1307 if (!evaluate_expression(expr->right))
1308 return NULL;
1309 return evaluate_binop(expr);
1310 case EXPR_LOGICAL:
1311 return evaluate_logical(expr);
1312 case EXPR_COMMA:
1313 if (!evaluate_expression(expr->left))
1314 return NULL;
1315 if (!evaluate_expression(expr->right))
1316 return NULL;
1317 return evaluate_comma(expr);
1318 case EXPR_COMPARE:
1319 if (!evaluate_expression(expr->left))
1320 return NULL;
1321 if (!evaluate_expression(expr->right))
1322 return NULL;
1323 return evaluate_compare(expr);
1324 case EXPR_ASSIGNMENT:
1325 if (!evaluate_expression(expr->left))
1326 return NULL;
1327 if (!evaluate_expression(expr->right))
1328 return NULL;
1329 return evaluate_assignment(expr);
1330 case EXPR_PREOP:
1331 if (!evaluate_expression(expr->unop))
1332 return NULL;
1333 return evaluate_preop(expr);
1334 case EXPR_POSTOP:
1335 if (!evaluate_expression(expr->unop))
1336 return NULL;
1337 return evaluate_postop(expr);
1338 case EXPR_CAST:
1339 return evaluate_cast(expr);
1340 case EXPR_SIZEOF:
1341 return evaluate_sizeof(expr);
1342 case EXPR_DEREF:
1343 return evaluate_member_dereference(expr);
1344 case EXPR_CALL:
1345 return evaluate_call(expr);
1346 case EXPR_BITFIELD:
1347 warn(expr->pos, "bitfield generated by parser");
1348 return NULL;
1349 case EXPR_CONDITIONAL:
1350 if (!evaluate_expression(expr->conditional))
1351 return NULL;
1352 if (!evaluate_expression(expr->cond_false))
1353 return NULL;
1354 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1355 return NULL;
1356 return evaluate_conditional(expr);
1357 case EXPR_STATEMENT:
1358 expr->ctype = evaluate_statement(expr->statement);
1359 return expr->ctype;
1361 /* These can not exist as stand-alone expressions */
1362 case EXPR_INITIALIZER:
1363 case EXPR_IDENTIFIER:
1364 case EXPR_INDEX:
1365 warn(expr->pos, "internal front-end error: initializer in expression");
1366 return NULL;
1368 return NULL;
1371 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1373 struct symbol **last = _last;
1374 struct symbol *type = evaluate_statement(stmt);
1376 if (flags & ITERATE_LAST)
1377 *last = type;
1380 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1382 evaluate_symbol(sym);
1385 struct symbol *evaluate_symbol(struct symbol *sym)
1387 struct symbol *base_type;
1389 examine_symbol_type(sym);
1390 base_type = sym->ctype.base_type;
1391 if (!base_type)
1392 return NULL;
1393 sym->ctype.base_type = base_type;
1395 /* Evaluate the initializers */
1396 if (sym->initializer) {
1397 int count = evaluate_initializer(base_type, &sym->initializer);
1398 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1399 int bit_size = count * base_type->ctype.base_type->bit_size;
1400 base_type->array_size = count;
1401 base_type->bit_size = bit_size;
1402 sym->array_size = count;
1403 sym->bit_size = bit_size;
1407 /* And finally, evaluate the body of the symbol too */
1408 if (base_type->type == SYM_FN) {
1409 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1410 if (base_type->stmt) {
1411 current_fn = base_type;
1412 current_contextmask = sym->ctype.contextmask;
1413 current_context = sym->ctype.context;
1414 evaluate_statement(base_type->stmt);
1418 return base_type;
1421 struct symbol *evaluate_return_expression(struct statement *stmt)
1423 struct expression *expr = stmt->expression;
1424 struct symbol *ctype, *fntype;
1426 fntype = current_fn->ctype.base_type;
1427 if (fntype == &void_ctype) {
1428 if (expr)
1429 warn(expr->pos, "return expression in void function");
1430 return NULL;
1433 if (!expr) {
1434 warn(stmt->pos, "return with no return value");
1435 return NULL;
1437 ctype = evaluate_expression(expr);
1438 if (!ctype)
1439 return NULL;
1440 ctype = degenerate(expr, ctype, &expr);
1441 expr->ctype = ctype;
1442 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1443 stmt->expression = expr;
1444 return NULL;
1447 struct symbol *evaluate_statement(struct statement *stmt)
1449 if (!stmt)
1450 return NULL;
1452 switch (stmt->type) {
1453 case STMT_RETURN:
1454 return evaluate_return_expression(stmt);
1456 case STMT_EXPRESSION:
1457 return evaluate_expression(stmt->expression);
1459 case STMT_COMPOUND: {
1460 struct symbol *type = NULL;
1461 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1462 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1463 return type;
1465 case STMT_IF:
1466 evaluate_expression(stmt->if_conditional);
1467 evaluate_statement(stmt->if_true);
1468 evaluate_statement(stmt->if_false);
1469 return NULL;
1470 case STMT_ITERATOR:
1471 evaluate_expression(stmt->iterator_pre_condition);
1472 evaluate_expression(stmt->iterator_post_condition);
1473 evaluate_statement(stmt->iterator_pre_statement);
1474 evaluate_statement(stmt->iterator_statement);
1475 evaluate_statement(stmt->iterator_post_statement);
1476 return NULL;
1477 case STMT_SWITCH:
1478 evaluate_expression(stmt->switch_expression);
1479 evaluate_statement(stmt->switch_statement);
1480 return NULL;
1481 case STMT_CASE:
1482 evaluate_expression(stmt->case_expression);
1483 evaluate_expression(stmt->case_to);
1484 evaluate_statement(stmt->case_statement);
1485 return NULL;
1486 case STMT_LABEL:
1487 evaluate_statement(stmt->label_statement);
1488 return NULL;
1489 case STMT_GOTO:
1490 evaluate_expression(stmt->goto_expression);
1491 return NULL;
1492 case STMT_NONE:
1493 break;
1494 case STMT_ASM:
1495 /* FIXME! Do the asm parameter evaluation! */
1496 break;
1498 return NULL;
1501 long long get_expression_value(struct expression *expr)
1503 long long value, mask;
1504 struct symbol *ctype;
1506 ctype = evaluate_expression(expr);
1507 if (!ctype || expr->type != EXPR_VALUE) {
1508 warn(expr->pos, "bad constant expression");
1509 return 0;
1512 value = expr->value;
1513 mask = 1ULL << (ctype->bit_size-1);
1515 if (value & mask) {
1516 while (ctype->type != SYM_BASETYPE)
1517 ctype = ctype->ctype.base_type;
1518 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1519 value = value | mask | ~(mask-1);
1521 return value;