Make string constants evaluate as a proper array dereference.
[smatch.git] / evaluate.c
blob1d81b31c118443813b2170a8ea11c838c5c0017d
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_NODE);
77 struct symbol *array = alloc_symbol(expr->pos, SYM_ARRAY);
78 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
79 int length = expr->string->length;
81 sym->array_size = length;
82 sym->bit_size = BITS_IN_CHAR * length;
83 sym->ctype.alignment = 1;
84 sym->ctype.modifiers = MOD_CONST;
85 sym->ctype.base_type = array;
87 array->array_size = length;
88 array->bit_size = BITS_IN_CHAR * length;
89 array->ctype.alignment = 1;
90 array->ctype.modifiers = 0;
91 array->ctype.base_type = &char_ctype;
93 addr->symbol = sym;
94 addr->ctype = &ptr_ctype;
96 expr->type = EXPR_PREOP;
97 expr->op = '*';
98 expr->unop = addr;
99 expr->ctype = sym;
100 return sym;
103 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
105 unsigned long lmod, rmod, mod;
107 if (left == right)
108 return left;
110 if (left->bit_size > right->bit_size)
111 return left;
113 if (right->bit_size > left->bit_size)
114 return right;
116 /* Same size integers - promote to unsigned, promote to long */
117 lmod = left->ctype.modifiers;
118 rmod = right->ctype.modifiers;
119 mod = lmod | rmod;
120 if (mod == lmod)
121 return left;
122 if (mod == rmod)
123 return right;
124 return ctype_integer(mod);
127 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
128 struct expression *old, struct symbol *oldtype)
130 int old_size = oldtype->bit_size;
131 int new_size = newtype->bit_size;
132 long long value, mask, ormask, andmask;
133 int is_signed;
135 // FIXME! We don't handle FP casts of constant values yet
136 if (newtype->ctype.base_type == &fp_type)
137 return NULL;
138 if (oldtype->ctype.base_type == &fp_type)
139 return NULL;
141 // For pointers and integers, we can just move the value around
142 expr->type = EXPR_VALUE;
143 if (old_size == new_size) {
144 expr->value = old->value;
145 return newtype;
148 // expand it to the full "long long" value
149 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
150 mask = 1ULL << (old_size-1);
151 value = old->value;
152 if (!(value & mask))
153 is_signed = 0;
154 andmask = mask | (mask-1);
155 ormask = ~andmask;
156 if (!is_signed)
157 ormask = 0;
158 value = (value & andmask) | ormask;
160 // Truncate it to the new size
161 mask = 1ULL << (new_size-1);
162 mask = mask | (mask-1);
163 expr->value = value & mask;
164 return newtype;
167 static struct expression * cast_to(struct expression *old, struct symbol *type)
169 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
170 expr->ctype = type;
171 expr->cast_type = type;
172 expr->cast_expression = old;
173 if (old->type == EXPR_VALUE)
174 cast_value(expr, type, old, old->ctype);
175 return expr;
178 static int is_ptr_type(struct symbol *type)
180 if (type->type == SYM_NODE)
181 type = type->ctype.base_type;
182 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
185 static int is_int_type(struct symbol *type)
187 if (type->type == SYM_NODE)
188 type = type->ctype.base_type;
189 return type->ctype.base_type == &int_type;
192 static struct symbol *bad_expr_type(struct expression *expr)
194 warn(expr->pos, "incompatible types for operation");
195 return NULL;
198 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
200 struct expression *left = *lp, *right = *rp;
201 struct symbol *ltype = left->ctype, *rtype = right->ctype;
203 if (ltype->type == SYM_NODE)
204 ltype = ltype->ctype.base_type;
205 if (rtype->type == SYM_NODE)
206 rtype = rtype->ctype.base_type;
207 /* Integer promotion? */
208 if (ltype->type == SYM_ENUM)
209 ltype = &int_ctype;
210 if (rtype->type == SYM_ENUM)
211 rtype = &int_ctype;
212 if (is_int_type(ltype) && is_int_type(rtype)) {
213 struct symbol *ctype = bigger_int_type(ltype, rtype);
215 /* Don't bother promoting same-size entities, it only adds clutter */
216 if (ltype->bit_size != ctype->bit_size)
217 *lp = cast_to(left, ctype);
218 if (rtype->bit_size != ctype->bit_size)
219 *rp = cast_to(right, ctype);
220 return ctype;
222 return NULL;
225 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
227 if (count >= ctype->bit_size) {
228 warn(expr->pos, "shift too big for type");
229 count &= ctype->bit_size-1;
231 return count;
235 * CAREFUL! We need to get the size and sign of the
236 * result right!
238 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
240 struct expression *left = expr->left, *right = expr->right;
241 unsigned long long v, l, r, mask;
242 signed long long s, sl, sr;
243 int is_signed, shift;
245 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
246 return;
247 l = left->value; r = right->value;
248 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
249 mask = 1ULL << (ctype->bit_size-1);
250 sl = l; sr = r;
251 if (is_signed && (sl & mask))
252 sl |= ~(mask-1);
253 if (is_signed && (sr & mask))
254 sr |= ~(mask-1);
256 switch (expr->op) {
257 case '+': v = l + r; s = v; break;
258 case '-': v = l - r; s = v; break;
259 case '&': v = l & r; s = v; break;
260 case '|': v = l | r; s = v; break;
261 case '^': v = l ^ r; s = v; break;
262 case '*': v = l * r; s = sl * sr; break;
263 case '/': if (!r) return; v = l / r; s = sl / sr; break;
264 case '%': if (!r) return; v = l % r; s = sl % sr; break;
265 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
266 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
267 case '<': v = l < r; s = sl < sr; break;
268 case '>': v = l > r; s = sl > sr; break;
269 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
270 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
271 case SPECIAL_EQUAL: v = l == r; s = v; break;
272 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
273 default: return;
275 if (is_signed)
276 v = s;
277 mask = mask | (mask-1);
278 expr->value = v & mask;
279 expr->type = EXPR_VALUE;
282 static struct symbol *evaluate_int_binop(struct expression *expr)
284 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
285 if (ctype) {
286 expr->ctype = ctype;
287 simplify_int_binop(expr, ctype);
288 return ctype;
290 return bad_expr_type(expr);
293 static inline int lvalue_expression(struct expression *expr)
295 return (expr->type == EXPR_PREOP && expr->op == '*') || expr->type == EXPR_BITFIELD;
298 /* Arrays degenerate into pointers on pointer arithmetic */
299 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
301 if (ctype->type == SYM_ARRAY) {
302 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
303 struct expression *ptr;
305 sym->ctype = ctype->ctype;
306 sym->bit_size = BITS_IN_POINTER;
307 sym->ctype.alignment = POINTER_ALIGNMENT;
308 ctype = sym;
310 if (expr->type != EXPR_STRING) {
311 ptr = *ptr_p;
312 *ptr_p = ptr->unop;
315 * This all really assumes that we got the degenerate
316 * array as an lvalue (ie a dereference). If that
317 * is not the case, then holler - because we've screwed
318 * up.
320 if (!lvalue_expression(ptr))
321 warn(ptr->pos, "internal error: strange degenerate array case");
324 return ctype;
327 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
329 struct symbol *ctype;
330 struct symbol *ptr_type = ptr->ctype;
331 struct symbol *i_type = i->ctype;
332 int bit_size;
334 if (i_type->type == SYM_NODE)
335 i_type = i_type->ctype.base_type;
336 if (ptr_type->type == SYM_NODE)
337 ptr_type = ptr_type->ctype.base_type;
339 if (i_type->type == SYM_ENUM)
340 i_type = &int_ctype;
341 if (!is_int_type(i_type))
342 return bad_expr_type(expr);
344 ctype = ptr->ctype;
345 examine_symbol_type(ctype);
347 ctype = degenerate(expr, ctype, &ptr);
348 bit_size = ctype->bit_size;
350 /* Special case: adding zero commonly happens as a result of 'array[0]' */
351 if (i->type == EXPR_VALUE && !i->value) {
352 *expr = *ptr;
353 } else if (bit_size > BITS_IN_CHAR) {
354 struct expression *add = expr;
355 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
356 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
358 val->ctype = size_t_ctype;
359 val->value = bit_size >> 3;
361 mul->op = '*';
362 mul->ctype = size_t_ctype;
363 mul->left = i;
364 mul->right = val;
365 simplify_int_binop(mul, size_t_ctype);
367 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
368 add->left = ptr;
369 add->right = mul;
370 simplify_int_binop(add, ctype);
373 expr->ctype = ctype;
374 return ctype;
377 static struct symbol *evaluate_add(struct expression *expr)
379 struct expression *left = expr->left, *right = expr->right;
380 struct symbol *ltype = left->ctype, *rtype = right->ctype;
382 if (is_ptr_type(ltype))
383 return evaluate_ptr_add(expr, left, right);
385 if (is_ptr_type(rtype))
386 return evaluate_ptr_add(expr, right, left);
388 // FIXME! FP promotion
389 return evaluate_int_binop(expr);
392 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
394 static const char * type_difference(struct symbol *target, struct symbol *source,
395 unsigned long target_mod_ignore, unsigned long source_mod_ignore)
397 for (;;) {
398 unsigned long mod1, mod2, diff;
399 unsigned long as1, as2;
401 if (target == source)
402 break;
403 if (!target || !source)
404 return "different types";
406 * Peel of per-node information.
407 * FIXME! Check alignment, address space, and context too here!
409 if (target->type == SYM_NODE)
410 target = target->ctype.base_type;
411 if (source->type == SYM_NODE)
412 source = source->ctype.base_type;
413 mod1 = target->ctype.modifiers;
414 as1 = target->ctype.as;
415 mod2 = source->ctype.modifiers;
416 as2 = source->ctype.as;
418 if (target->type != source->type) {
419 int type1 = target->type;
420 int type2 = source->type;
422 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
423 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
424 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
425 if (type1 != type2)
426 return "different base types";
429 /* Must be same address space to be comparable */
430 if (as1 != as2)
431 return "different address spaces";
433 /* Ignore differences in storage types, sign, or addressability */
434 diff = (mod1 ^ mod2) & ~(MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED);
435 if (diff) {
436 mod1 &= diff & ~target_mod_ignore;
437 mod2 &= diff & ~source_mod_ignore;
438 if (mod1 | mod2) {
439 if ((mod1 | mod2) & MOD_SIZE)
440 return "different type sizes";
441 return "different modifiers";
445 target = target->ctype.base_type;
446 source = source->ctype.base_type;
448 return NULL;
451 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
453 /* NULL expression? Just return the type of the "other side" */
454 if (r->type == EXPR_VALUE && !r->value)
455 return l->ctype;
456 if (l->type == EXPR_VALUE && !l->value)
457 return r->ctype;
458 return NULL;
462 * Ignore differences in "volatile" and "const"ness when
463 * subtracting pointers
465 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
467 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
469 const char *typediff;
470 struct symbol *ctype;
471 struct symbol *ltype = l->ctype, *rtype = r->ctype;
474 * If it is an integer subtract: the ptr add case will do the
475 * right thing.
477 if (!is_ptr_type(rtype))
478 return evaluate_ptr_add(expr, l, r);
480 ctype = ltype;
481 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
482 if (typediff) {
483 ctype = common_ptr_type(l, r);
484 if (!ctype) {
485 warn(expr->pos, "subtraction of different types can't work (%s)", typediff);
486 return NULL;
489 examine_symbol_type(ctype);
491 /* Figure out the base type we point to */
492 if (ctype->type == SYM_NODE)
493 ctype = ctype->ctype.base_type;
494 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
495 warn(expr->pos, "subtraction of functions? Share your drugs");
496 return NULL;
498 ctype = ctype->ctype.base_type;
500 expr->ctype = ssize_t_ctype;
501 if (ctype->bit_size > BITS_IN_CHAR) {
502 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
503 struct expression *div = expr;
504 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
506 val->ctype = size_t_ctype;
507 val->value = ctype->bit_size >> 3;
509 sub->op = '-';
510 sub->ctype = ssize_t_ctype;
511 sub->left = l;
512 sub->right = r;
514 div->op = '/';
515 div->left = sub;
516 div->right = val;
519 return ssize_t_ctype;
522 static struct symbol *evaluate_sub(struct expression *expr)
524 struct expression *left = expr->left, *right = expr->right;
525 struct symbol *ltype = left->ctype;
527 if (is_ptr_type(ltype))
528 return evaluate_ptr_sub(expr, left, right);
530 // FIXME! FP promotion
531 return evaluate_int_binop(expr);
534 static struct symbol *evaluate_logical(struct expression *expr)
536 struct expression *left = expr->left;
537 struct expression *right;
539 if (!evaluate_expression(left))
540 return NULL;
542 /* Do immediate short-circuiting ... */
543 if (left->type == EXPR_VALUE) {
544 if (expr->op == SPECIAL_LOGICAL_AND) {
545 if (!left->value) {
546 expr->type = EXPR_VALUE;
547 expr->value = 0;
548 expr->ctype = &bool_ctype;
549 return &bool_ctype;
551 } else {
552 if (left->value) {
553 expr->type = EXPR_VALUE;
554 expr->value = 1;
555 expr->ctype = &bool_ctype;
556 return &bool_ctype;
561 right = expr->right;
562 if (!evaluate_expression(right))
563 return NULL;
564 expr->ctype = &bool_ctype;
565 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
567 * We know the left value doesn't matter, since
568 * otherwise we would have short-circuited it..
570 expr->type = EXPR_VALUE;
571 expr->value = right->value;
574 return &bool_ctype;
577 static struct symbol *evaluate_arithmetic(struct expression *expr)
579 // FIXME! Floating-point promotion!
580 return evaluate_int_binop(expr);
583 static struct symbol *evaluate_binop(struct expression *expr)
585 switch (expr->op) {
586 // addition can take ptr+int, fp and int
587 case '+':
588 return evaluate_add(expr);
590 // subtraction can take ptr-ptr, fp and int
591 case '-':
592 return evaluate_sub(expr);
594 // Arithmetic operations can take fp and int
595 case '*': case '/': case '%':
596 return evaluate_arithmetic(expr);
598 // The rest are integer operations (bitops)
599 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
600 // '&', '^', '|'
601 default:
602 return evaluate_int_binop(expr);
606 static struct symbol *evaluate_comma(struct expression *expr)
608 expr->ctype = expr->right->ctype;
609 return expr->ctype;
612 static struct symbol *evaluate_compare(struct expression *expr)
614 struct expression *left = expr->left, *right = expr->right;
615 struct symbol *ltype = left->ctype, *rtype = right->ctype;
616 struct symbol *ctype;
618 /* Pointer types? */
619 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
620 expr->ctype = &bool_ctype;
621 // FIXME! Check the types for compatibility
622 return &bool_ctype;
625 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
626 if (ctype) {
627 simplify_int_binop(expr, ctype);
628 expr->ctype = &bool_ctype;
629 return &bool_ctype;
632 return bad_expr_type(expr);
635 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
637 /* Integer promotion? */
638 if (ltype->type == SYM_NODE)
639 ltype = ltype->ctype.base_type;
640 if (rtype->type == SYM_NODE)
641 rtype = rtype->ctype.base_type;
642 if (ltype->type == SYM_ENUM)
643 ltype = &int_ctype;
644 if (rtype->type == SYM_ENUM)
645 rtype = &int_ctype;
646 return (is_int_type(ltype) && is_int_type(rtype));
649 static int is_void_ptr(struct expression *expr)
651 return (expr->type == EXPR_VALUE &&
652 expr->value == 0);
656 * FIXME!! This shoul ddo casts, array degeneration etc..
658 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
660 struct symbol *ltype = left->ctype, *rtype = right->ctype;
662 if (ltype->type == SYM_PTR) {
663 if (is_void_ptr(right))
664 return ltype;
667 if (rtype->type == SYM_PTR) {
668 if (is_void_ptr(left))
669 return rtype;
671 return NULL;
674 static struct symbol * evaluate_conditional(struct expression *expr)
676 struct expression *cond, *true, *false;
677 struct symbol *ctype, *ltype, *rtype;
678 const char * typediff;
680 cond = expr->conditional;
681 true = expr->cond_true ? : cond;
682 false = expr->cond_false;
684 ltype = true->ctype;
685 rtype = false->ctype;
687 ctype = ltype;
688 typediff = type_difference(ltype, rtype, MOD_IGN, MOD_IGN);
689 if (typediff) {
690 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
691 if (!ctype) {
692 ctype = compatible_ptr_type(true, expr->cond_false);
693 if (!ctype) {
694 warn(expr->pos, "incompatible types in conditional expression (%s)", typediff);
695 return NULL;
700 /* Simplify conditional expression.. */
701 if (cond->type == EXPR_VALUE) {
702 if (!cond->value)
703 true = false;
704 *expr = *true;
706 expr->ctype = ctype;
707 return ctype;
710 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
711 struct expression **rp, struct symbol *source, const char *where)
713 const char *typediff;
714 struct symbol *t;
715 int target_as;
717 /* It's ok if the target is more volatile or const than the source */
718 typediff = type_difference(target, source, MOD_VOLATILE | MOD_CONST, 0);
719 if (!typediff)
720 return 1;
722 if (compatible_integer_types(target, source)) {
723 if (target->bit_size != source->bit_size)
724 *rp = cast_to(*rp, target);
725 return 1;
728 /* Pointer destination? */
729 t = target;
730 target_as = t->ctype.as;
731 if (t->type == SYM_NODE) {
732 t = t->ctype.base_type;
733 target_as |= t->ctype.as;
735 if (t->type == SYM_PTR) {
736 struct expression *right = *rp;
737 struct symbol *s = source;
738 int source_as;
740 // NULL pointer is always ok
741 if (right->type == EXPR_VALUE && !right->value)
742 return 1;
744 /* "void *" matches anything as long as the address space is ok */
745 source_as = s->ctype.as;
746 if (s->type == SYM_NODE) {
747 s = s->ctype.base_type;
748 source_as |= s->ctype.as;
750 if (source_as == target_as && (s->type == SYM_PTR || s->type == SYM_ARRAY)) {
751 s = s->ctype.base_type;
752 t = t->ctype.base_type;
753 if (s == &void_ctype || t == &void_ctype)
754 return 1;
757 if (s->type == SYM_FN) {
758 typediff = type_difference(t->ctype.base_type, s, 0, 0);
759 if (!typediff)
760 return 1;
763 // FIXME!! Cast it!
764 warn(expr->pos, "different types in %s (%s)", where, typediff);
765 return 0;
768 // FIXME!! Cast it?
769 warn(expr->pos, "incorrect type in %s (%s)", where, typediff);
770 warn(expr->pos, " expected %s", show_typename(target));
771 warn(expr->pos, " got %s", show_typename(source));
772 return 0;
776 * FIXME!! This is wrong from a double evaluation standpoint. We can't
777 * just expand the expression twice, that would make any side effects
778 * happen twice too.
780 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
782 int op = expr->op;
783 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
784 static const int op_trans[] = {
785 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
786 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
787 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
788 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
789 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
790 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
791 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
792 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
793 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
794 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
797 subexpr->left = left;
798 subexpr->right = right;
799 subexpr->op = op_trans[op - SPECIAL_BASE];
800 expr->op = '=';
801 expr->right = subexpr;
802 return evaluate_binop(subexpr);
805 static struct symbol *evaluate_assignment(struct expression *expr)
807 struct expression *left = expr->left, *right = expr->right;
808 struct symbol *ltype, *rtype;
810 ltype = left->ctype;
811 rtype = right->ctype;
812 if (expr->op != '=') {
813 rtype = evaluate_binop_assignment(expr, left, right);
814 if (!rtype)
815 return 0;
816 right = expr->right;
819 if (!lvalue_expression(left)) {
820 warn(expr->pos, "not an lvalue");
821 return NULL;
824 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype, "assignment"))
825 return 0;
827 expr->ctype = expr->left->ctype;
828 return expr->ctype;
831 static struct symbol *evaluate_addressof(struct expression *expr)
833 struct symbol *ctype, *symbol;
834 struct expression *op = expr->unop;
836 if (op->op != '*' || op->type != EXPR_PREOP) {
837 warn(expr->pos, "not addressable");
838 return NULL;
841 symbol = alloc_symbol(expr->pos, SYM_PTR);
842 symbol->ctype.alignment = POINTER_ALIGNMENT;
843 symbol->bit_size = BITS_IN_POINTER;
845 ctype = op->ctype;
846 if (ctype->type == SYM_NODE) {
847 ctype->ctype.modifiers |= MOD_ADDRESSABLE;
848 if (ctype->ctype.modifiers & MOD_REGISTER) {
849 warn(expr->pos, "taking address of 'register' variable '%s'", show_ident(ctype->ident));
850 ctype->ctype.modifiers &= ~MOD_REGISTER;
852 symbol->ctype.modifiers = ctype->ctype.modifiers;
853 symbol->ctype.as = ctype->ctype.as;
854 symbol->ctype.context = ctype->ctype.context;
855 symbol->ctype.contextmask = ctype->ctype.contextmask;
856 ctype = ctype->ctype.base_type;
859 symbol->ctype.base_type = ctype;
860 *expr = *op->unop;
861 expr->ctype = symbol;
862 return symbol;
866 static struct symbol *evaluate_dereference(struct expression *expr)
868 struct expression *op = expr->unop;
869 struct symbol *ctype = op->ctype, *sym;
871 sym = alloc_symbol(expr->pos, SYM_NODE);
872 sym->ctype = ctype->ctype;
873 if (ctype->type == SYM_NODE) {
874 ctype = ctype->ctype.base_type;
875 sym->ctype.modifiers |= ctype->ctype.modifiers;
876 sym->ctype.as |= ctype->ctype.as;
878 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
879 warn(expr->pos, "cannot derefence this type");
880 return 0;
883 ctype = ctype->ctype.base_type;
884 if (!ctype) {
885 warn(expr->pos, "undefined type");
886 return NULL;
888 examine_symbol_type(ctype);
889 sym->ctype.base_type = ctype;
891 sym->bit_size = ctype->bit_size;
892 sym->array_size = ctype->array_size;
894 /* Simplify: *&(expr) => (expr) */
895 if (op->type == EXPR_PREOP && op->op == '&') {
896 *expr = *op->unop;
899 expr->ctype = sym;
900 return sym;
903 static void simplify_preop(struct expression *expr)
905 struct expression *op = expr->unop;
906 unsigned long long v, mask;
908 if (op->type != EXPR_VALUE)
909 return;
910 v = op->value;
911 switch (expr->op) {
912 case '+': break;
913 case '-': v = -v; break;
914 case '!': v = !v; break;
915 case '~': v = ~v; break;
916 default: return;
918 mask = 1ULL << (expr->ctype->bit_size-1);
919 mask = mask | (mask-1);
920 expr->value = v & mask;
921 expr->type = EXPR_VALUE;
925 * Unary post-ops: x++ and x--
927 static struct symbol *evaluate_postop(struct expression *expr)
929 struct expression *op = expr->unop;
930 struct symbol *ctype = op->ctype;
932 if (!lvalue_expression(expr->unop)) {
933 warn(expr->pos, "need lvalue expression for ++/--");
934 return NULL;
936 expr->ctype = ctype;
937 return ctype;
940 static struct symbol *evaluate_preop(struct expression *expr)
942 struct symbol *ctype = expr->unop->ctype;
944 switch (expr->op) {
945 case '(':
946 case '+':
947 *expr = *expr->unop;
948 return ctype;
950 case '*':
951 return evaluate_dereference(expr);
953 case '&':
954 return evaluate_addressof(expr);
956 case '!':
957 expr->ctype = &bool_ctype;
958 simplify_preop(expr);
959 return &bool_ctype;
961 case SPECIAL_INCREMENT:
962 case SPECIAL_DECREMENT:
964 * From a type evaluation standpoint the pre-ops are
965 * the same as the postops
967 return evaluate_postop(expr);
969 default:
970 expr->ctype = ctype;
971 simplify_preop(expr);
972 return ctype;
976 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
978 struct ptr_list *head = (struct ptr_list *)_list;
979 struct ptr_list *list = head;
981 if (!head)
982 return NULL;
983 do {
984 int i;
985 for (i = 0; i < list->nr; i++) {
986 struct symbol *sym = (struct symbol *) list->list[i];
987 if (sym->ident) {
988 if (sym->ident != ident)
989 continue;
990 *offset = sym->offset;
991 return sym;
992 } else {
993 struct symbol *ctype = sym->ctype.base_type;
994 struct symbol *sub;
995 if (!ctype)
996 continue;
997 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
998 continue;
999 sub = find_identifier(ident, ctype->symbol_list, offset);
1000 if (!sub)
1001 continue;
1002 *offset += sym->offset;
1003 return sub;
1006 } while ((list = list->next) != head);
1007 return NULL;
1010 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
1012 struct expression *add;
1014 if (!offset)
1015 return expr;
1017 /* Create a new add-expression */
1018 add = alloc_expression(expr->pos, EXPR_BINOP);
1019 add->op = '+';
1020 add->ctype = &ptr_ctype;
1021 add->left = expr;
1022 add->right = alloc_expression(expr->pos, EXPR_VALUE);
1023 add->right->ctype = &int_ctype;
1024 add->right->value = offset;
1026 simplify_int_binop(add, &ptr_ctype);
1027 return add;
1030 /* structure/union dereference */
1031 static struct symbol *evaluate_member_dereference(struct expression *expr)
1033 int offset;
1034 struct symbol *ctype, *member, *sym;
1035 struct expression *deref = expr->deref, *add;
1036 struct ident *ident = expr->member;
1037 unsigned int mod;
1038 int address_space;
1040 if (!evaluate_expression(deref))
1041 return NULL;
1042 if (!ident) {
1043 warn(expr->pos, "bad member name");
1044 return NULL;
1047 ctype = deref->ctype;
1048 if (ctype->type == SYM_NODE)
1049 ctype = ctype->ctype.base_type;
1050 mod = ctype->ctype.modifiers;
1051 address_space = ctype->ctype.as;
1052 if (expr->op == SPECIAL_DEREFERENCE) {
1053 /* Arrays will degenerate into pointers for '->' */
1054 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
1055 warn(expr->pos, "expected a pointer to a struct/union");
1056 return NULL;
1058 address_space = ctype->ctype.as;
1059 ctype = ctype->ctype.base_type;
1060 mod |= ctype->ctype.modifiers;
1061 if (ctype->type == SYM_NODE) {
1062 ctype = ctype->ctype.base_type;
1063 mod |= ctype->ctype.modifiers;
1065 } else {
1066 if (!lvalue_expression(deref)) {
1067 warn(deref->pos, "expected lvalue for member dereference");
1068 return NULL;
1070 deref = deref->unop;
1071 expr->deref = deref;
1073 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1074 warn(expr->pos, "expected structure or union");
1075 return NULL;
1077 offset = 0;
1078 member = find_identifier(ident, ctype->symbol_list, &offset);
1079 if (!member) {
1080 warn(expr->pos, "no such struct/union member");
1081 return NULL;
1084 add = evaluate_offset(deref, offset);
1086 sym = alloc_symbol(expr->pos, SYM_NODE);
1087 sym->bit_size = member->bit_size;
1088 sym->array_size = member->array_size;
1089 sym->ctype = member->ctype;
1090 sym->ctype.modifiers = mod;
1091 sym->ctype.as = address_space;
1092 ctype = member->ctype.base_type;
1093 if (ctype->type == SYM_BITFIELD) {
1094 ctype = ctype->ctype.base_type;
1095 expr->type = EXPR_BITFIELD;
1096 expr->bitpos = member->bit_offset;
1097 expr->nrbits = member->fieldwidth;
1098 expr->address = add;
1099 } else {
1100 expr->type = EXPR_PREOP;
1101 expr->op = '*';
1102 expr->unop = add;
1105 expr->ctype = sym;
1106 return sym;
1109 static struct symbol *evaluate_sizeof(struct expression *expr)
1111 int size;
1113 if (expr->cast_type) {
1114 examine_symbol_type(expr->cast_type);
1115 size = expr->cast_type->bit_size;
1116 } else {
1117 if (!evaluate_expression(expr->cast_expression))
1118 return 0;
1119 size = expr->cast_expression->ctype->bit_size;
1121 if (size & 7) {
1122 warn(expr->pos, "cannot size expression");
1123 return 0;
1125 expr->type = EXPR_VALUE;
1126 expr->value = size >> 3;
1127 expr->ctype = size_t_ctype;
1128 return size_t_ctype;
1131 static int evaluate_arguments(struct symbol *fn, struct expression_list *head)
1133 struct expression *expr;
1134 struct symbol_list *argument_types = fn->arguments;
1135 struct symbol *argtype;
1136 int i = 1;
1138 PREPARE_PTR_LIST(argument_types, argtype);
1139 FOR_EACH_PTR (head, expr) {
1140 struct expression **p = THIS_ADDRESS(expr);
1141 struct symbol *ctype, *target;
1142 ctype = evaluate_expression(expr);
1144 if (!ctype)
1145 return 0;
1147 if (ctype->type == SYM_ARRAY) {
1148 ctype = degenerate(expr, ctype, p);
1149 expr->ctype = ctype;
1152 target = argtype;
1153 if (!target && ctype->bit_size < BITS_IN_INT)
1154 target = &int_ctype;
1155 if (target) {
1156 static char where[30];
1157 examine_symbol_type(target);
1158 sprintf(where, "argument %d", i);
1159 compatible_assignment_types(expr, target, p, ctype, where);
1162 i++;
1163 NEXT_PTR_LIST(argument_types, argtype);
1164 } END_FOR_EACH_PTR;
1165 FINISH_PTR_LIST;
1166 return 1;
1170 * FIXME! This is bogus: we need to take array index
1171 * entries into account when calculating the size of
1172 * the array.
1174 static int count_array_initializer(struct expression *expr)
1176 struct expression_list *list;
1178 if (expr->type != EXPR_INITIALIZER)
1179 return 1;
1180 list = expr->expr_list;
1181 return expression_list_size(list);
1184 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1186 return count_array_initializer(expr);
1189 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1191 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1192 return 0;
1196 * Initializers are kind of like assignments. Except
1197 * they can be a hell of a lot more complex.
1199 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1201 struct expression *expr = *ep;
1204 * Simple non-structure/array initializers are the simple
1205 * case, and look (and parse) largely like assignments.
1207 if (expr->type != EXPR_INITIALIZER) {
1208 int size = 0;
1209 struct symbol *rtype = evaluate_expression(expr);
1210 if (rtype) {
1211 compatible_assignment_types(expr, ctype, ep, rtype, "initializer");
1212 /* strings are special: char arrays */
1213 if (rtype->type == SYM_ARRAY)
1214 size = rtype->array_size;
1216 return size;
1219 expr->ctype = ctype;
1220 switch (ctype->type) {
1221 case SYM_ARRAY:
1222 case SYM_PTR:
1223 return evaluate_array_initializer(ctype, expr);
1224 case SYM_UNION:
1225 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1226 case SYM_STRUCT:
1227 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1228 default:
1229 break;
1231 warn(expr->pos, "unexpected compound initializer");
1232 return 0;
1235 static struct symbol *evaluate_cast(struct expression *expr)
1237 struct expression *target = expr->cast_expression;
1238 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1240 expr->ctype = ctype;
1241 expr->cast_type = ctype;
1244 * Special case: a cast can be followed by an
1245 * initializer, in which case we need to pass
1246 * the type value down to that initializer rather
1247 * than trying to evaluate it as an expression
1249 if (target->type == EXPR_INITIALIZER) {
1250 evaluate_initializer(ctype, &expr->cast_expression);
1251 return ctype;
1254 evaluate_expression(target);
1256 /* Simplify normal integer casts.. */
1257 if (target->type == EXPR_VALUE)
1258 cast_value(expr, ctype, target, target->ctype);
1259 return ctype;
1262 static struct symbol *evaluate_call(struct expression *expr)
1264 int args, fnargs;
1265 struct symbol *ctype;
1266 struct expression *fn = expr->fn;
1267 struct expression_list *arglist = expr->args;
1269 if (!evaluate_expression(fn))
1270 return NULL;
1271 ctype = fn->ctype;
1272 if (ctype->type == SYM_NODE) {
1274 * FIXME!! We should really expand the inline functions.
1275 * For now we just mark them accessed so that they show
1276 * up on the list of used symbols.
1278 if (ctype->ctype.modifiers & MOD_INLINE)
1279 access_symbol(ctype);
1280 ctype = ctype->ctype.base_type;
1282 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1283 ctype = ctype->ctype.base_type;
1284 if (ctype->type != SYM_FN) {
1285 warn(expr->pos, "not a function");
1286 return NULL;
1288 if (!evaluate_arguments(ctype, arglist))
1289 return NULL;
1290 args = expression_list_size(expr->args);
1291 fnargs = symbol_list_size(ctype->arguments);
1292 if (args < fnargs)
1293 warn(expr->pos, "not enough arguments for function");
1294 if (args > fnargs && !ctype->variadic)
1295 warn(expr->pos, "too many arguments for function");
1296 expr->ctype = ctype->ctype.base_type;
1297 return expr->ctype;
1300 struct symbol *evaluate_expression(struct expression *expr)
1302 if (!expr)
1303 return NULL;
1304 if (expr->ctype)
1305 return expr->ctype;
1307 switch (expr->type) {
1308 case EXPR_VALUE:
1309 warn(expr->pos, "value expression without a type");
1310 return NULL;
1311 case EXPR_STRING:
1312 return evaluate_string(expr);
1313 case EXPR_SYMBOL:
1314 return evaluate_symbol_expression(expr);
1315 case EXPR_BINOP:
1316 if (!evaluate_expression(expr->left))
1317 return NULL;
1318 if (!evaluate_expression(expr->right))
1319 return NULL;
1320 return evaluate_binop(expr);
1321 case EXPR_LOGICAL:
1322 return evaluate_logical(expr);
1323 case EXPR_COMMA:
1324 if (!evaluate_expression(expr->left))
1325 return NULL;
1326 if (!evaluate_expression(expr->right))
1327 return NULL;
1328 return evaluate_comma(expr);
1329 case EXPR_COMPARE:
1330 if (!evaluate_expression(expr->left))
1331 return NULL;
1332 if (!evaluate_expression(expr->right))
1333 return NULL;
1334 return evaluate_compare(expr);
1335 case EXPR_ASSIGNMENT:
1336 if (!evaluate_expression(expr->left))
1337 return NULL;
1338 if (!evaluate_expression(expr->right))
1339 return NULL;
1340 return evaluate_assignment(expr);
1341 case EXPR_PREOP:
1342 if (!evaluate_expression(expr->unop))
1343 return NULL;
1344 return evaluate_preop(expr);
1345 case EXPR_POSTOP:
1346 if (!evaluate_expression(expr->unop))
1347 return NULL;
1348 return evaluate_postop(expr);
1349 case EXPR_CAST:
1350 return evaluate_cast(expr);
1351 case EXPR_SIZEOF:
1352 return evaluate_sizeof(expr);
1353 case EXPR_DEREF:
1354 return evaluate_member_dereference(expr);
1355 case EXPR_CALL:
1356 return evaluate_call(expr);
1357 case EXPR_BITFIELD:
1358 warn(expr->pos, "bitfield generated by parser");
1359 return NULL;
1360 case EXPR_CONDITIONAL:
1361 if (!evaluate_expression(expr->conditional))
1362 return NULL;
1363 if (!evaluate_expression(expr->cond_false))
1364 return NULL;
1365 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1366 return NULL;
1367 return evaluate_conditional(expr);
1368 case EXPR_STATEMENT:
1369 expr->ctype = evaluate_statement(expr->statement);
1370 return expr->ctype;
1372 /* These can not exist as stand-alone expressions */
1373 case EXPR_INITIALIZER:
1374 case EXPR_IDENTIFIER:
1375 case EXPR_INDEX:
1376 warn(expr->pos, "internal front-end error: initializer in expression");
1377 return NULL;
1379 return NULL;
1382 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1384 struct symbol **last = _last;
1385 struct symbol *type = evaluate_statement(stmt);
1387 if (flags & ITERATE_LAST)
1388 *last = type;
1391 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1393 evaluate_symbol(sym);
1396 struct symbol *evaluate_symbol(struct symbol *sym)
1398 struct symbol *base_type;
1400 examine_symbol_type(sym);
1401 base_type = sym->ctype.base_type;
1402 if (!base_type)
1403 return NULL;
1404 sym->ctype.base_type = base_type;
1406 /* Evaluate the initializers */
1407 if (sym->initializer) {
1408 int count = evaluate_initializer(base_type, &sym->initializer);
1409 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1410 int bit_size = count * base_type->ctype.base_type->bit_size;
1411 base_type->array_size = count;
1412 base_type->bit_size = bit_size;
1413 sym->array_size = count;
1414 sym->bit_size = bit_size;
1418 /* And finally, evaluate the body of the symbol too */
1419 if (base_type->type == SYM_FN) {
1420 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1421 if (base_type->stmt) {
1422 current_fn = base_type;
1423 current_contextmask = sym->ctype.contextmask;
1424 current_context = sym->ctype.context;
1425 evaluate_statement(base_type->stmt);
1429 return base_type;
1432 struct symbol *evaluate_return_expression(struct statement *stmt)
1434 struct expression *expr = stmt->expression;
1435 struct symbol *ctype, *fntype;
1437 fntype = current_fn->ctype.base_type;
1438 if (fntype == &void_ctype) {
1439 if (expr)
1440 warn(expr->pos, "return expression in void function");
1441 return NULL;
1444 if (!expr) {
1445 warn(stmt->pos, "return with no return value");
1446 return NULL;
1448 ctype = evaluate_expression(expr);
1449 if (!ctype)
1450 return NULL;
1451 ctype = degenerate(expr, ctype, &expr);
1452 expr->ctype = ctype;
1453 compatible_assignment_types(expr, fntype, &expr, ctype, "return expression");
1454 stmt->expression = expr;
1455 return NULL;
1458 struct symbol *evaluate_statement(struct statement *stmt)
1460 if (!stmt)
1461 return NULL;
1463 switch (stmt->type) {
1464 case STMT_RETURN:
1465 return evaluate_return_expression(stmt);
1467 case STMT_EXPRESSION:
1468 return evaluate_expression(stmt->expression);
1470 case STMT_COMPOUND: {
1471 struct symbol *type = NULL;
1472 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1473 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1474 return type;
1476 case STMT_IF:
1477 evaluate_expression(stmt->if_conditional);
1478 evaluate_statement(stmt->if_true);
1479 evaluate_statement(stmt->if_false);
1480 return NULL;
1481 case STMT_ITERATOR:
1482 evaluate_expression(stmt->iterator_pre_condition);
1483 evaluate_expression(stmt->iterator_post_condition);
1484 evaluate_statement(stmt->iterator_pre_statement);
1485 evaluate_statement(stmt->iterator_statement);
1486 evaluate_statement(stmt->iterator_post_statement);
1487 return NULL;
1488 case STMT_SWITCH:
1489 evaluate_expression(stmt->switch_expression);
1490 evaluate_statement(stmt->switch_statement);
1491 return NULL;
1492 case STMT_CASE:
1493 evaluate_expression(stmt->case_expression);
1494 evaluate_expression(stmt->case_to);
1495 evaluate_statement(stmt->case_statement);
1496 return NULL;
1497 case STMT_LABEL:
1498 evaluate_statement(stmt->label_statement);
1499 return NULL;
1500 case STMT_GOTO:
1501 evaluate_expression(stmt->goto_expression);
1502 return NULL;
1503 case STMT_NONE:
1504 break;
1505 case STMT_ASM:
1506 /* FIXME! Do the asm parameter evaluation! */
1507 break;
1509 return NULL;
1512 long long get_expression_value(struct expression *expr)
1514 long long value, mask;
1515 struct symbol *ctype;
1517 ctype = evaluate_expression(expr);
1518 if (!ctype || expr->type != EXPR_VALUE) {
1519 warn(expr->pos, "bad constant expression");
1520 return 0;
1523 value = expr->value;
1524 mask = 1ULL << (ctype->bit_size-1);
1526 if (value & mask) {
1527 while (ctype->type != SYM_BASETYPE)
1528 ctype = ctype->ctype.base_type;
1529 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1530 value = value | mask | ~(mask-1);
1532 return value;