Put a note about the horrible wrongness in doing the expansion
[smatch.git] / evaluate.c
blob705c31e26a97df5e2553371e5a2a519a23acf834
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 <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <limits.h>
17 #include "lib.h"
18 #include "parse.h"
19 #include "token.h"
20 #include "symbol.h"
21 #include "target.h"
22 #include "expression.h"
24 static int current_context, current_contextmask;
26 static struct symbol *evaluate_symbol_expression(struct expression *expr)
28 struct symbol *sym = expr->symbol;
29 struct symbol *base_type;
31 if (!sym) {
32 warn(expr->pos, "undefined identifier '%s'", show_ident(expr->symbol_name));
33 return NULL;
36 examine_symbol_type(sym);
37 if ((sym->ctype.context ^ current_context) & (sym->ctype.contextmask & current_contextmask))
38 warn(expr->pos, "Using symbol '%s' in wrong context", show_ident(expr->symbol_name));
40 base_type = sym->ctype.base_type;
41 if (!base_type) {
42 warn(sym->pos, "identifier '%s' has no type", show_ident(expr->symbol_name));
43 return NULL;
46 /* The type of a symbol is the symbol itself! */
47 expr->ctype = sym;
49 /* enum's can be turned into plain values */
50 if (base_type->type != SYM_ENUM) {
51 struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL);
52 addr->symbol = sym;
53 addr->symbol_name = expr->symbol_name;
54 addr->ctype = &ptr_ctype;
55 expr->type = EXPR_PREOP;
56 expr->op = '*';
57 expr->unop = addr;
58 return base_type;
60 expr->type = EXPR_VALUE;
61 expr->value = sym->value;
62 return sym;
65 static struct symbol *evaluate_string(struct expression *expr)
67 struct symbol *sym = alloc_symbol(expr->pos, SYM_ARRAY);
68 int length = expr->string->length;
70 sym->array_size = length;
71 sym->bit_size = BITS_IN_CHAR * length;
72 sym->ctype.alignment = 1;
73 sym->ctype.modifiers = MOD_CONST;
74 sym->ctype.base_type = &char_ctype;
75 expr->ctype = sym;
76 return sym;
79 static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
81 unsigned long lmod, rmod, mod;
83 if (left == right)
84 return left;
86 if (left->bit_size > right->bit_size)
87 return left;
89 if (right->bit_size > left->bit_size)
90 return right;
92 /* Same size integers - promote to unsigned, promote to long */
93 lmod = left->ctype.modifiers;
94 rmod = right->ctype.modifiers;
95 mod = lmod | rmod;
96 if (mod == lmod)
97 return left;
98 if (mod == rmod)
99 return right;
100 return ctype_integer(mod);
103 static struct symbol * cast_value(struct expression *expr, struct symbol *newtype,
104 struct expression *old, struct symbol *oldtype)
106 int old_size = oldtype->bit_size;
107 int new_size = newtype->bit_size;
108 long long value, mask, ormask, andmask;
109 int is_signed;
111 // FIXME! We don't handle FP casts of constant values yet
112 if (newtype->ctype.base_type == &fp_type)
113 return NULL;
114 if (oldtype->ctype.base_type == &fp_type)
115 return NULL;
117 // For pointers and integers, we can just move the value around
118 expr->type = EXPR_VALUE;
119 if (old_size == new_size) {
120 expr->value = old->value;
121 return newtype;
124 // expand it to the full "long long" value
125 is_signed = !(oldtype->ctype.modifiers & MOD_UNSIGNED);
126 mask = 1ULL << (old_size-1);
127 value = old->value;
128 if (!(value & mask))
129 is_signed = 0;
130 andmask = mask | (mask-1);
131 ormask = ~andmask;
132 if (!is_signed)
133 ormask = 0;
134 value = (value & andmask) | ormask;
136 // Truncate it to the new size
137 mask = 1ULL << (new_size-1);
138 mask = mask | (mask-1);
139 expr->value = value & mask;
140 return newtype;
143 static struct expression * cast_to(struct expression *old, struct symbol *type)
145 struct expression *expr = alloc_expression(old->pos, EXPR_CAST);
146 expr->ctype = type;
147 expr->cast_type = type;
148 expr->cast_expression = old;
149 if (old->type == EXPR_VALUE)
150 cast_value(expr, type, old, old->ctype);
151 return expr;
154 static int is_ptr_type(struct symbol *type)
156 if (type->type == SYM_NODE)
157 type = type->ctype.base_type;
158 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
161 static int is_int_type(struct symbol *type)
163 if (type->type == SYM_NODE)
164 type = type->ctype.base_type;
165 return type->ctype.base_type == &int_type;
168 static struct symbol *bad_expr_type(struct expression *expr)
170 warn(expr->pos, "incompatible types for operation");
171 return NULL;
174 static struct symbol * compatible_integer_binop(struct expression *expr, struct expression **lp, struct expression **rp)
176 struct expression *left = *lp, *right = *rp;
177 struct symbol *ltype = left->ctype, *rtype = right->ctype;
179 if (ltype->type == SYM_NODE)
180 ltype = ltype->ctype.base_type;
181 if (rtype->type == SYM_NODE)
182 rtype = rtype->ctype.base_type;
183 /* Integer promotion? */
184 if (ltype->type == SYM_ENUM)
185 ltype = &int_ctype;
186 if (rtype->type == SYM_ENUM)
187 rtype = &int_ctype;
188 if (is_int_type(ltype) && is_int_type(rtype)) {
189 struct symbol *ctype = bigger_int_type(ltype, rtype);
191 /* Don't bother promoting same-size entities, it only adds clutter */
192 if (ltype->bit_size != ctype->bit_size)
193 *lp = cast_to(left, ctype);
194 if (rtype->bit_size != ctype->bit_size)
195 *rp = cast_to(right, ctype);
196 return ctype;
198 return NULL;
201 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
203 if (count >= ctype->bit_size) {
204 warn(expr->pos, "shift too big for type");
205 count &= ctype->bit_size-1;
207 return count;
211 * CAREFUL! We need to get the size and sign of the
212 * result right!
214 static void simplify_int_binop(struct expression *expr, struct symbol *ctype)
216 struct expression *left = expr->left, *right = expr->right;
217 unsigned long long v, l, r, mask;
218 signed long long s, sl, sr;
219 int is_signed, shift;
221 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
222 return;
223 l = left->value; r = right->value;
224 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
225 mask = 1ULL << (ctype->bit_size-1);
226 sl = l; sr = r;
227 if (is_signed && (sl & mask))
228 sl |= ~(mask-1);
229 if (is_signed && (sr & mask))
230 sr |= ~(mask-1);
232 switch (expr->op) {
233 case '+': v = l + r; s = v; break;
234 case '-': v = l - r; s = v; break;
235 case '&': v = l & r; s = v; break;
236 case '|': v = l | r; s = v; break;
237 case '^': v = l ^ r; s = v; break;
238 case '*': v = l * r; s = sl * sr; break;
239 case '/': if (!r) return; v = l / r; s = sl / sr; break;
240 case '%': if (!r) return; v = l % r; s = sl % sr; break;
241 case SPECIAL_LEFTSHIFT: shift = check_shift_count(expr, ctype, r); v = l << shift; s = v; break;
242 case SPECIAL_RIGHTSHIFT:shift = check_shift_count(expr, ctype, r); v = l >> shift; s = sl >> shift; break;
243 case '<': v = l < r; s = sl < sr; break;
244 case '>': v = l > r; s = sl > sr; break;
245 case SPECIAL_LTE: v = l <= r; s = sl <= sr; break;
246 case SPECIAL_GTE: v = l >= r; s = sl >= sr; break;
247 case SPECIAL_EQUAL: v = l == r; s = v; break;
248 case SPECIAL_NOTEQUAL: v = l != r; s = v; break;
249 default: return;
251 if (is_signed)
252 v = s;
253 mask = mask | (mask-1);
254 expr->value = v & mask;
255 expr->type = EXPR_VALUE;
258 static struct symbol *evaluate_int_binop(struct expression *expr)
260 struct symbol *ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
261 if (ctype) {
262 expr->ctype = ctype;
263 simplify_int_binop(expr, ctype);
264 return ctype;
266 return bad_expr_type(expr);
269 static inline int lvalue_expression(struct expression *expr)
271 return expr->type == EXPR_PREOP && expr->op == '*';
275 /* Arrays degenerate into pointers on pointer arithmetic */
276 static struct symbol *degenerate(struct expression *expr, struct symbol *ctype, struct expression **ptr_p)
278 if (ctype->type == SYM_ARRAY) {
279 struct symbol *sym = alloc_symbol(expr->pos, SYM_PTR);
280 struct expression *ptr;
282 sym->ctype = ctype->ctype;
283 sym->bit_size = BITS_IN_POINTER;
284 sym->ctype.alignment = POINTER_ALIGNMENT;
285 ctype = sym;
286 ptr = *ptr_p;
287 *ptr_p = ptr->unop;
290 * This all really assumes that we got the degenerate
291 * array as an lvalue (ie a dereference). If that
292 * is not the case, then holler - because we've screwed
293 * up.
295 if (!lvalue_expression(ptr))
296 warn(ptr->pos, "internal error: strange degenerate array case");
298 return ctype;
301 static struct symbol *evaluate_ptr_add(struct expression *expr, struct expression *ptr, struct expression *i)
303 struct symbol *ctype;
304 struct symbol *ptr_type = ptr->ctype;
305 struct symbol *i_type = i->ctype;
306 int bit_size;
308 if (i_type->type == SYM_NODE)
309 i_type = i_type->ctype.base_type;
310 if (ptr_type->type == SYM_NODE)
311 ptr_type = ptr_type->ctype.base_type;
313 if (i_type->type == SYM_ENUM)
314 i_type = &int_ctype;
315 if (!is_int_type(i_type))
316 return bad_expr_type(expr);
318 ctype = ptr_type->ctype.base_type;
319 examine_symbol_type(ctype);
321 bit_size = ctype->bit_size;
322 ctype = degenerate(expr, ptr_type, &ptr);
324 /* Special case: adding zero commonly happens as a result of 'array[0]' */
325 if (i->type == EXPR_VALUE && !i->value) {
326 *expr = *ptr;
327 } else if (bit_size > BITS_IN_CHAR) {
328 struct expression *add = expr;
329 struct expression *mul = alloc_expression(expr->pos, EXPR_BINOP);
330 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
332 val->ctype = size_t_ctype;
333 val->value = bit_size >> 3;
335 mul->op = '*';
336 mul->ctype = size_t_ctype;
337 mul->left = i;
338 mul->right = val;
339 simplify_int_binop(mul, size_t_ctype);
341 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
342 add->left = ptr;
343 add->right = mul;
344 simplify_int_binop(add, add->ctype);
347 expr->ctype = ctype;
348 return ctype;
351 static struct symbol *evaluate_add(struct expression *expr)
353 struct expression *left = expr->left, *right = expr->right;
354 struct symbol *ltype = left->ctype, *rtype = right->ctype;
356 if (is_ptr_type(ltype))
357 return evaluate_ptr_add(expr, left, right);
359 if (is_ptr_type(rtype))
360 return evaluate_ptr_add(expr, right, left);
362 // FIXME! FP promotion
363 return evaluate_int_binop(expr);
366 static int same_type(struct symbol *target, struct symbol *source)
368 int dropped_modifiers = 0;
369 for (;;) {
370 unsigned long mod1, mod2;
371 unsigned long as1, as2;
373 if (target == source)
374 break;
375 if (!target || !source)
376 return 0;
378 * Peel of per-node information.
379 * FIXME! Check alignment, address space, and context too here!
381 mod1 = target->ctype.modifiers;
382 as1 = target->ctype.as;
383 mod2 = source->ctype.modifiers;
384 as2 = source->ctype.as;
385 if (target->type == SYM_NODE) {
386 target = target->ctype.base_type;
387 mod1 |= target->ctype.modifiers;
388 as1 |= target->ctype.as;
390 if (source->type == SYM_NODE) {
391 source = source->ctype.base_type;
392 mod2 |= source->ctype.modifiers;
393 as2 |= source->ctype.as;
396 /* Ignore differences in storage types */
397 if ((mod1 ^ mod2) & ~MOD_STORAGE)
398 return 0;
400 /* Must be same address space to be comparable */
401 if (as1 != as2)
402 return 0;
404 if (target->type != source->type) {
405 int type1 = target->type;
406 int type2 = source->type;
408 /* Ignore ARRAY/PTR differences, as long as they point to the same type */
409 type1 = type1 == SYM_ARRAY ? SYM_PTR : type1;
410 type2 = type2 == SYM_ARRAY ? SYM_PTR : type2;
411 if (type1 != type2)
412 return 0;
415 target = target->ctype.base_type;
416 source = source->ctype.base_type;
418 if (dropped_modifiers)
419 warn(target->pos, "assignment drops modifiers");
420 return 1;
423 static struct symbol *common_ptr_type(struct expression *l, struct expression *r)
425 /* NULL expression? Just return the type of the "other side" */
426 if (r->type == EXPR_VALUE && !r->value)
427 return l->ctype;
428 if (l->type == EXPR_VALUE && !l->value)
429 return r->ctype;
430 return NULL;
433 static struct symbol *evaluate_ptr_sub(struct expression *expr, struct expression *l, struct expression *r)
435 struct symbol *ctype;
436 struct symbol *ltype = l->ctype, *rtype = r->ctype;
439 * If it is an integer subtract: the ptr add case will do the
440 * right thing.
442 if (!is_ptr_type(rtype))
443 return evaluate_ptr_add(expr, l, r);
445 ctype = ltype;
446 if (!same_type(ltype, rtype)) {
447 ctype = common_ptr_type(l, r);
448 if (!ctype) {
449 warn(expr->pos, "subtraction of different types can't work");
450 return NULL;
453 examine_symbol_type(ctype);
455 /* Figure out the base type we point to */
456 if (ctype->type == SYM_NODE)
457 ctype = ctype->ctype.base_type;
458 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
459 warn(expr->pos, "subtraction of functions? Share your drugs");
460 return NULL;
462 ctype = ctype->ctype.base_type;
464 expr->ctype = ssize_t_ctype;
465 if (ctype->bit_size > BITS_IN_CHAR) {
466 struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
467 struct expression *div = expr;
468 struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
470 val->ctype = size_t_ctype;
471 val->value = ctype->bit_size >> 3;
473 sub->op = '-';
474 sub->ctype = ssize_t_ctype;
475 sub->left = l;
476 sub->right = r;
478 div->op = '/';
479 div->left = sub;
480 div->right = val;
483 return ssize_t_ctype;
486 static struct symbol *evaluate_sub(struct expression *expr)
488 struct expression *left = expr->left, *right = expr->right;
489 struct symbol *ltype = left->ctype;
491 if (is_ptr_type(ltype))
492 return evaluate_ptr_sub(expr, left, right);
494 // FIXME! FP promotion
495 return evaluate_int_binop(expr);
498 static struct symbol *evaluate_logical(struct expression *expr)
500 struct expression *left = expr->left;
501 struct expression *right;
503 if (!evaluate_expression(left))
504 return NULL;
506 /* Do immediate short-circuiting ... */
507 if (left->type == EXPR_VALUE) {
508 if (expr->op == SPECIAL_LOGICAL_AND) {
509 if (!left->value) {
510 expr->type = EXPR_VALUE;
511 expr->value = 0;
512 expr->ctype = &bool_ctype;
513 return &bool_ctype;
515 } else {
516 if (left->value) {
517 expr->type = EXPR_VALUE;
518 expr->value = 1;
519 expr->ctype = &bool_ctype;
520 return &bool_ctype;
525 right = expr->right;
526 if (!evaluate_expression(right))
527 return NULL;
528 expr->ctype = &bool_ctype;
529 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
531 * We know the left value doesn't matter, since
532 * otherwise we would have short-circuited it..
534 expr->type = EXPR_VALUE;
535 expr->value = right->value;
538 return &bool_ctype;
541 static struct symbol *evaluate_arithmetic(struct expression *expr)
543 // FIXME! Floating-point promotion!
544 return evaluate_int_binop(expr);
547 static struct symbol *evaluate_binop(struct expression *expr)
549 switch (expr->op) {
550 // addition can take ptr+int, fp and int
551 case '+':
552 return evaluate_add(expr);
554 // subtraction can take ptr-ptr, fp and int
555 case '-':
556 return evaluate_sub(expr);
558 // Arithmetic operations can take fp and int
559 case '*': case '/': case '%':
560 return evaluate_arithmetic(expr);
562 // The rest are integer operations (bitops)
563 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
564 // '&', '^', '|'
565 default:
566 return evaluate_int_binop(expr);
570 static struct symbol *evaluate_comma(struct expression *expr)
572 expr->ctype = expr->right->ctype;
573 return expr->ctype;
576 static struct symbol *evaluate_compare(struct expression *expr)
578 struct expression *left = expr->left, *right = expr->right;
579 struct symbol *ltype = left->ctype, *rtype = right->ctype;
580 struct symbol *ctype;
582 /* Pointer types? */
583 if (is_ptr_type(ltype) || is_ptr_type(rtype)) {
584 expr->ctype = &bool_ctype;
585 // FIXME! Check the types for compatibility
586 return &bool_ctype;
589 ctype = compatible_integer_binop(expr, &expr->left, &expr->right);
590 if (ctype) {
591 simplify_int_binop(expr, ctype);
592 expr->ctype = &bool_ctype;
593 return &bool_ctype;
596 return bad_expr_type(expr);
599 static int compatible_integer_types(struct symbol *ltype, struct symbol *rtype)
601 /* Integer promotion? */
602 if (ltype->type == SYM_ENUM)
603 ltype = &int_ctype;
604 if (rtype->type == SYM_ENUM)
605 rtype = &int_ctype;
606 return (is_int_type(ltype) && is_int_type(rtype));
609 static int is_void_ptr(struct expression *expr)
611 return (expr->type == EXPR_VALUE &&
612 expr->value == 0);
616 * FIXME!! This shoul ddo casts, array degeneration etc..
618 static struct symbol *compatible_ptr_type(struct expression *left, struct expression *right)
620 struct symbol *ltype = left->ctype, *rtype = right->ctype;
622 if (ltype->type == SYM_PTR) {
623 if (is_void_ptr(right))
624 return ltype;
627 if (rtype->type == SYM_PTR) {
628 if (is_void_ptr(left))
629 return rtype;
631 return NULL;
634 static struct symbol * evaluate_conditional(struct expression *expr)
636 struct expression *cond, *true, *false;
637 struct symbol *ctype, *ltype, *rtype;
639 cond = expr->conditional;
640 true = expr->cond_true ? : cond;
641 false = expr->cond_false;
643 ltype = true->ctype;
644 rtype = false->ctype;
646 ctype = ltype;
647 if (!same_type(ltype, rtype)) {
648 ctype = compatible_integer_binop(expr, &true, &expr->cond_false);
649 if (!ctype) {
650 ctype = compatible_ptr_type(true, expr->cond_false);
651 if (!ctype) {
652 warn(expr->pos, "incompatible types in conditional expression");
653 return NULL;
658 /* Simplify conditional expression.. */
659 if (cond->type == EXPR_VALUE) {
660 if (!cond->value)
661 true = false;
662 *expr = *true;
664 expr->ctype = ctype;
665 return ctype;
668 static int compatible_assignment_types(struct expression *expr, struct symbol *target,
669 struct expression **rp, struct symbol *source)
671 if (same_type(target, source))
672 return 1;
674 if (compatible_integer_types(target, source)) {
675 if (target->bit_size != source->bit_size)
676 *rp = cast_to(*rp, target);
677 return 1;
680 /* Pointer destination? */
681 if (target->type == SYM_NODE)
682 target = target->ctype.base_type;
683 if (source->type == SYM_NODE)
684 source = source->ctype.base_type;
685 if (target->type == SYM_PTR) {
686 struct expression *right = *rp;
687 struct symbol *source_base = source->ctype.base_type;
688 struct symbol *target_base = target->ctype.base_type;
690 if (source->type == SYM_NODE) {
691 source = source_base;
692 source_base = source->ctype.base_type;
694 if (target->type == SYM_NODE) {
695 target = target_base;
696 target_base = target->ctype.base_type;
698 if (source->type == SYM_ARRAY && same_type(target_base, source_base))
699 return 1;
700 if (source->type == SYM_FN && same_type(target_base, source))
701 return 1;
703 // NULL pointer?
704 if (right->type == EXPR_VALUE && !right->value)
705 return 1;
707 // void pointer ?
708 if (target->type == SYM_PTR) {
709 struct symbol *source_base = source->ctype.base_type;
710 struct symbol *target_base = target->ctype.base_type;
711 if (source_base == &void_ctype || target_base == &void_ctype)
712 return 1;
713 warn(expr->pos, "assignment from incompatible pointer types");
714 return 1;
717 // FIXME!! Cast it!
718 warn(expr->pos, "assignment from different types");
719 return 0;
722 // FIXME!! Cast it!
723 warn(expr->pos, "assignment from bad type");
724 return 0;
728 * FIXME!! This is wrong from a double evaluation standpoint. We can't
729 * just expand the expression twice, that would make any side effects
730 * happen twice too.
732 static struct symbol *evaluate_binop_assignment(struct expression *expr, struct expression *left, struct expression *right)
734 int op = expr->op;
735 struct expression *subexpr = alloc_expression(expr->pos, EXPR_BINOP);
736 static const int op_trans[] = {
737 [SPECIAL_ADD_ASSIGN - SPECIAL_BASE] = '+',
738 [SPECIAL_SUB_ASSIGN - SPECIAL_BASE] = '-',
739 [SPECIAL_MUL_ASSIGN - SPECIAL_BASE] = '*',
740 [SPECIAL_DIV_ASSIGN - SPECIAL_BASE] = '/',
741 [SPECIAL_MOD_ASSIGN - SPECIAL_BASE] = '%',
742 [SPECIAL_SHL_ASSIGN - SPECIAL_BASE] = SPECIAL_LEFTSHIFT,
743 [SPECIAL_SHR_ASSIGN - SPECIAL_BASE] = SPECIAL_RIGHTSHIFT,
744 [SPECIAL_AND_ASSIGN - SPECIAL_BASE] = '&',
745 [SPECIAL_OR_ASSIGN - SPECIAL_BASE] = '&',
746 [SPECIAL_XOR_ASSIGN - SPECIAL_BASE] = '^'
749 subexpr->left = left;
750 subexpr->right = right;
751 subexpr->op = op_trans[op - SPECIAL_BASE];
752 expr->op = '=';
753 expr->right = subexpr;
754 return evaluate_binop(subexpr);
757 static struct symbol *evaluate_assignment(struct expression *expr)
759 struct expression *left = expr->left, *right = expr->right;
760 struct symbol *ltype, *rtype;
762 ltype = left->ctype;
763 rtype = right->ctype;
764 if (expr->op != '=') {
765 rtype = evaluate_binop_assignment(expr, left, right);
766 if (!rtype)
767 return 0;
768 right = expr->right;
771 if (!lvalue_expression(left)) {
772 warn(expr->pos, "not an lvalue");
773 return NULL;
776 if (!compatible_assignment_types(expr, ltype, &expr->right, rtype))
777 return 0;
779 expr->ctype = expr->left->ctype;
780 return expr->ctype;
783 static struct symbol *evaluate_addressof(struct expression *expr)
785 struct symbol *ctype, *symbol;
786 struct expression *op = expr->unop;
788 if (!lvalue_expression(op)) {
789 warn(expr->pos, "not an lvalue");
790 return NULL;
792 ctype = op->ctype;
793 symbol = alloc_symbol(expr->pos, SYM_PTR);
794 symbol->ctype.base_type = ctype;
795 symbol->ctype.alignment = POINTER_ALIGNMENT;
796 symbol->bit_size = BITS_IN_POINTER;
798 *expr = *op->unop;
799 expr->ctype = symbol;
800 return symbol;
804 static struct symbol *evaluate_dereference(struct expression *expr)
806 struct expression *op = expr->unop;
807 struct symbol *ctype = op->ctype;
808 unsigned long mod;
810 mod = ctype->ctype.modifiers;
811 if (ctype->type == SYM_NODE) {
812 ctype = ctype->ctype.base_type;
813 mod |= ctype->ctype.modifiers;
815 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
816 warn(expr->pos, "cannot derefence this type");
817 return 0;
820 if (mod & MOD_NODEREF)
821 warn(expr->pos, "bad dereference");
822 ctype = ctype->ctype.base_type;
823 if (!ctype) {
824 warn(expr->pos, "undefined type");
825 return 0;
828 examine_symbol_type(ctype);
830 /* Simplify: *&(expr) => (expr) */
831 if (op->type == EXPR_PREOP && op->op == '&') {
832 *expr = *op->unop;
835 expr->ctype = ctype;
836 return ctype;
839 static void simplify_preop(struct expression *expr)
841 struct expression *op = expr->unop;
842 unsigned long long v, mask;
844 if (op->type != EXPR_VALUE)
845 return;
846 v = op->value;
847 switch (expr->op) {
848 case '+': break;
849 case '-': v = -v; break;
850 case '!': v = !v; break;
851 case '~': v = ~v; break;
852 default: return;
854 mask = 1ULL << (expr->ctype->bit_size-1);
855 mask = mask | (mask-1);
856 expr->value = v & mask;
857 expr->type = EXPR_VALUE;
861 * Unary post-ops: x++ and x--
863 static struct symbol *evaluate_postop(struct expression *expr)
865 struct expression *op = expr->unop;
866 struct symbol *ctype = op->ctype;
868 if (!lvalue_expression(expr->unop)) {
869 warn(expr->pos, "need lvalue expression for ++/--");
870 return NULL;
872 expr->ctype = ctype;
873 return ctype;
876 static struct symbol *evaluate_preop(struct expression *expr)
878 struct symbol *ctype = expr->unop->ctype;
880 switch (expr->op) {
881 case '(':
882 *expr = *expr->unop;
883 return ctype;
885 case '*':
886 return evaluate_dereference(expr);
888 case '&':
889 return evaluate_addressof(expr);
891 case '!':
892 expr->ctype = &bool_ctype;
893 simplify_preop(expr);
894 return &bool_ctype;
896 case SPECIAL_INCREMENT:
897 case SPECIAL_DECREMENT:
899 * From a type evaluation standpoint the pre-ops are
900 * the same as the postops
902 return evaluate_postop(expr);
904 default:
905 expr->ctype = ctype;
906 simplify_preop(expr);
907 return ctype;
911 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset)
913 struct ptr_list *head = (struct ptr_list *)_list;
914 struct ptr_list *list = head;
916 if (!head)
917 return NULL;
918 do {
919 int i;
920 for (i = 0; i < list->nr; i++) {
921 struct symbol *sym = (struct symbol *) list->list[i];
922 if (sym->ident) {
923 if (sym->ident != ident)
924 continue;
925 *offset = sym->offset;
926 return sym;
927 } else {
928 struct symbol *ctype = sym->ctype.base_type;
929 struct symbol *sub;
930 if (!ctype)
931 continue;
932 if (ctype->type != SYM_UNION && ctype->type != SYM_STRUCT)
933 continue;
934 sub = find_identifier(ident, ctype->symbol_list, offset);
935 if (!sub)
936 continue;
937 *offset += sym->offset;
938 return sub;
941 } while ((list = list->next) != head);
942 return NULL;
945 static struct expression *evaluate_offset(struct expression *expr, unsigned long offset)
947 struct expression *add;
949 if (!offset)
950 return expr;
952 /* Create a new add-expression */
953 add = alloc_expression(expr->pos, EXPR_BINOP);
954 add->op = '+';
955 add->ctype = &ptr_ctype;
956 add->left = expr;
957 add->right = alloc_expression(expr->pos, EXPR_VALUE);
958 add->right->ctype = &int_ctype;
959 add->right->value = offset;
961 simplify_int_binop(add, &ptr_ctype);
962 return add;
965 /* structure/union dereference */
966 static struct symbol *evaluate_member_dereference(struct expression *expr)
968 int offset;
969 struct symbol *ctype, *member;
970 struct expression *deref = expr->deref, *add;
971 struct ident *ident = expr->member;
972 unsigned int mod;
974 if (!evaluate_expression(deref))
975 return NULL;
976 if (!ident) {
977 warn(expr->pos, "bad member name");
978 return NULL;
981 ctype = deref->ctype;
982 mod = ctype->ctype.modifiers;
983 if (ctype->type == SYM_NODE) {
984 ctype = ctype->ctype.base_type;
985 mod |= ctype->ctype.modifiers;
987 if (expr->op == SPECIAL_DEREFERENCE) {
988 /* Arrays will degenerate into pointers for '->' */
989 if (ctype->type != SYM_PTR && ctype->type != SYM_ARRAY) {
990 warn(expr->pos, "expected a pointer to a struct/union");
991 return NULL;
993 ctype = ctype->ctype.base_type;
994 mod |= ctype->ctype.modifiers;
995 if (ctype->type == SYM_NODE) {
996 ctype = ctype->ctype.base_type;
997 mod |= ctype->ctype.modifiers;
999 } else {
1000 if (!lvalue_expression(deref)) {
1001 warn(deref->pos, "expected lvalue for member dereference");
1002 return NULL;
1004 deref = deref->unop;
1005 expr->deref = deref;
1007 if (mod & MOD_NODEREF)
1008 warn(expr->pos, "bad dereference");
1009 if (!ctype || (ctype->type != SYM_STRUCT && ctype->type != SYM_UNION)) {
1010 warn(expr->pos, "expected structure or union");
1011 return NULL;
1013 offset = 0;
1014 member = find_identifier(ident, ctype->symbol_list, &offset);
1015 if (!member) {
1016 warn(expr->pos, "no such struct/union member");
1017 return NULL;
1020 add = evaluate_offset(deref, offset);
1022 ctype = member->ctype.base_type;
1023 if (ctype->type == SYM_BITFIELD) {
1024 ctype = ctype->ctype.base_type;
1025 expr->type = EXPR_BITFIELD;
1026 expr->bitpos = member->bit_offset;
1027 expr->nrbits = member->fieldwidth;
1028 expr->address = add;
1029 } else {
1030 expr->type = EXPR_PREOP;
1031 expr->op = '*';
1032 expr->unop = add;
1035 expr->ctype = ctype;
1036 return ctype;
1039 static struct symbol *evaluate_sizeof(struct expression *expr)
1041 int size;
1043 if (expr->cast_type) {
1044 examine_symbol_type(expr->cast_type);
1045 size = expr->cast_type->bit_size;
1046 } else {
1047 if (!evaluate_expression(expr->cast_expression))
1048 return 0;
1049 size = expr->cast_expression->ctype->bit_size;
1051 if (size & 7) {
1052 warn(expr->pos, "cannot size expression");
1053 return 0;
1055 expr->type = EXPR_VALUE;
1056 expr->value = size >> 3;
1057 expr->ctype = size_t_ctype;
1058 return size_t_ctype;
1061 static int evaluate_expression_list(struct expression_list *head)
1063 if (head) {
1064 struct ptr_list *list = (struct ptr_list *)head;
1065 do {
1066 int i;
1067 for (i = 0; i < list->nr; i++) {
1068 struct expression *expr = (struct expression *)list->list[i];
1069 evaluate_expression(expr);
1071 } while ((list = list->next) != (struct ptr_list *)head);
1073 // FIXME!
1074 return 1;
1078 * FIXME! This is bogus: we need to take array index
1079 * entries into account when calculating the size of
1080 * the array.
1082 static int count_array_initializer(struct expression *expr)
1084 struct expression_list *list;
1086 if (expr->type != EXPR_INITIALIZER)
1087 return 1;
1088 list = expr->expr_list;
1089 return expression_list_size(list);
1092 static int evaluate_array_initializer(struct symbol *ctype, struct expression *expr)
1094 return count_array_initializer(expr);
1097 static int evaluate_struct_or_union_initializer(struct symbol *ctype, struct expression *expr, int multiple)
1099 /* Fixme: walk through the struct/union definitions and try to assign right types! */
1100 return 0;
1104 * Initializers are kind of like assignments. Except
1105 * they can be a hell of a lot more complex.
1107 static int evaluate_initializer(struct symbol *ctype, struct expression **ep)
1109 struct expression *expr = *ep;
1112 * Simple non-structure/array initializers are the simple
1113 * case, and look (and parse) largely like assignments.
1115 if (expr->type != EXPR_INITIALIZER) {
1116 int size = 0;
1117 struct symbol *rtype = evaluate_expression(expr);
1118 if (rtype) {
1119 compatible_assignment_types(expr, ctype, ep, rtype);
1120 /* strings are special: char arrays */
1121 if (rtype->type == SYM_ARRAY)
1122 size = rtype->array_size;
1124 return size;
1127 expr->ctype = ctype;
1128 switch (ctype->type) {
1129 case SYM_ARRAY:
1130 case SYM_PTR:
1131 return evaluate_array_initializer(ctype, expr);
1132 case SYM_UNION:
1133 return evaluate_struct_or_union_initializer(ctype, expr, 0);
1134 case SYM_STRUCT:
1135 return evaluate_struct_or_union_initializer(ctype, expr, 1);
1136 default:
1137 break;
1139 warn(expr->pos, "unexpected compound initializer");
1140 return 0;
1143 static struct symbol *evaluate_cast(struct expression *expr)
1145 struct expression *target = expr->cast_expression;
1146 struct symbol *ctype = examine_symbol_type(expr->cast_type);
1148 expr->ctype = ctype;
1149 expr->cast_type = ctype;
1152 * Special case: a cast can be followed by an
1153 * initializer, in which case we need to pass
1154 * the type value down to that initializer rather
1155 * than trying to evaluate it as an expression
1157 if (target->type == EXPR_INITIALIZER) {
1158 evaluate_initializer(ctype, &expr->cast_expression);
1159 return ctype;
1162 evaluate_expression(target);
1164 /* Simplify normal integer casts.. */
1165 if (target->type == EXPR_VALUE)
1166 cast_value(expr, ctype, target, target->ctype);
1167 return ctype;
1170 static struct symbol *evaluate_call(struct expression *expr)
1172 int args, fnargs;
1173 struct symbol *ctype;
1174 struct expression *fn = expr->fn;
1175 struct expression_list *arglist = expr->args;
1177 if (!evaluate_expression(fn))
1178 return NULL;
1179 if (!evaluate_expression_list(arglist))
1180 return NULL;
1181 ctype = fn->ctype;
1182 if (ctype->type == SYM_NODE)
1183 ctype = ctype->ctype.base_type;
1184 if (ctype->type == SYM_PTR || ctype->type == SYM_ARRAY)
1185 ctype = ctype->ctype.base_type;
1186 if (ctype->type != SYM_FN) {
1187 warn(expr->pos, "not a function");
1188 return NULL;
1190 args = expression_list_size(expr->args);
1191 fnargs = symbol_list_size(ctype->arguments);
1192 if (args < fnargs)
1193 warn(expr->pos, "not enough arguments for function");
1194 if (args > fnargs && !ctype->variadic)
1195 warn(expr->pos, "too many arguments for function");
1196 expr->ctype = ctype->ctype.base_type;
1197 return expr->ctype;
1200 struct symbol *evaluate_expression(struct expression *expr)
1202 if (!expr)
1203 return NULL;
1204 if (expr->ctype)
1205 return expr->ctype;
1207 switch (expr->type) {
1208 case EXPR_VALUE:
1209 warn(expr->pos, "value expression without a type");
1210 return NULL;
1211 case EXPR_STRING:
1212 return evaluate_string(expr);
1213 case EXPR_SYMBOL:
1214 return evaluate_symbol_expression(expr);
1215 case EXPR_BINOP:
1216 if (!evaluate_expression(expr->left))
1217 return NULL;
1218 if (!evaluate_expression(expr->right))
1219 return NULL;
1220 return evaluate_binop(expr);
1221 case EXPR_LOGICAL:
1222 return evaluate_logical(expr);
1223 case EXPR_COMMA:
1224 if (!evaluate_expression(expr->left))
1225 return NULL;
1226 if (!evaluate_expression(expr->right))
1227 return NULL;
1228 return evaluate_comma(expr);
1229 case EXPR_COMPARE:
1230 if (!evaluate_expression(expr->left))
1231 return NULL;
1232 if (!evaluate_expression(expr->right))
1233 return NULL;
1234 return evaluate_compare(expr);
1235 case EXPR_ASSIGNMENT:
1236 if (!evaluate_expression(expr->left))
1237 return NULL;
1238 if (!evaluate_expression(expr->right))
1239 return NULL;
1240 return evaluate_assignment(expr);
1241 case EXPR_PREOP:
1242 if (!evaluate_expression(expr->unop))
1243 return NULL;
1244 return evaluate_preop(expr);
1245 case EXPR_POSTOP:
1246 if (!evaluate_expression(expr->unop))
1247 return NULL;
1248 return evaluate_postop(expr);
1249 case EXPR_CAST:
1250 return evaluate_cast(expr);
1251 case EXPR_SIZEOF:
1252 return evaluate_sizeof(expr);
1253 case EXPR_DEREF:
1254 return evaluate_member_dereference(expr);
1255 case EXPR_CALL:
1256 return evaluate_call(expr);
1257 case EXPR_BITFIELD:
1258 warn(expr->pos, "bitfield generated by parser");
1259 return NULL;
1260 case EXPR_CONDITIONAL:
1261 if (!evaluate_expression(expr->conditional))
1262 return NULL;
1263 if (!evaluate_expression(expr->cond_false))
1264 return NULL;
1265 if (expr->cond_true && !evaluate_expression(expr->cond_true))
1266 return NULL;
1267 return evaluate_conditional(expr);
1268 case EXPR_STATEMENT:
1269 expr->ctype = evaluate_statement(expr->statement);
1270 return expr->ctype;
1272 /* These can not exist as stand-alone expressions */
1273 case EXPR_INITIALIZER:
1274 case EXPR_IDENTIFIER:
1275 case EXPR_INDEX:
1276 warn(expr->pos, "internal front-end error: initializer in expression");
1277 return NULL;
1279 return NULL;
1282 static void evaluate_one_statement(struct statement *stmt, void *_last, int flags)
1284 struct symbol **last = _last;
1285 struct symbol *type = evaluate_statement(stmt);
1287 if (flags & ITERATE_LAST)
1288 *last = type;
1291 static void evaluate_one_symbol(struct symbol *sym, void *unused, int flags)
1293 evaluate_symbol(sym);
1296 struct symbol *evaluate_symbol(struct symbol *sym)
1298 struct symbol *base_type;
1300 examine_symbol_type(sym);
1301 base_type = sym->ctype.base_type;
1302 if (!base_type)
1303 return NULL;
1304 sym->ctype.base_type = base_type;
1306 /* Evaluate the initializers */
1307 if (sym->initializer) {
1308 int count = evaluate_initializer(base_type, &sym->initializer);
1309 if (base_type->type == SYM_ARRAY && base_type->array_size < 0) {
1310 int bit_size = count * base_type->ctype.base_type->bit_size;
1311 base_type->array_size = count;
1312 base_type->bit_size = bit_size;
1313 sym->array_size = count;
1314 sym->bit_size = bit_size;
1318 /* And finally, evaluate the body of the symbol too */
1319 if (base_type->type == SYM_FN) {
1320 symbol_iterate(base_type->arguments, evaluate_one_symbol, NULL);
1321 if (base_type->stmt) {
1322 current_contextmask = sym->ctype.contextmask;
1323 current_context = sym->ctype.context;
1324 evaluate_statement(base_type->stmt);
1328 return base_type;
1331 struct symbol *evaluate_statement(struct statement *stmt)
1333 if (!stmt)
1334 return NULL;
1336 switch (stmt->type) {
1337 case STMT_RETURN:
1338 case STMT_EXPRESSION:
1339 return evaluate_expression(stmt->expression);
1341 case STMT_COMPOUND: {
1342 struct symbol *type = NULL;
1343 symbol_iterate(stmt->syms, evaluate_one_symbol, NULL);
1344 statement_iterate(stmt->stmts, evaluate_one_statement, &type);
1345 return type;
1347 case STMT_IF:
1348 evaluate_expression(stmt->if_conditional);
1349 evaluate_statement(stmt->if_true);
1350 evaluate_statement(stmt->if_false);
1351 return NULL;
1352 case STMT_ITERATOR:
1353 evaluate_expression(stmt->iterator_pre_condition);
1354 evaluate_expression(stmt->iterator_post_condition);
1355 evaluate_statement(stmt->iterator_pre_statement);
1356 evaluate_statement(stmt->iterator_statement);
1357 evaluate_statement(stmt->iterator_post_statement);
1358 return NULL;
1359 case STMT_SWITCH:
1360 evaluate_expression(stmt->switch_expression);
1361 evaluate_statement(stmt->switch_statement);
1362 return NULL;
1363 case STMT_CASE:
1364 evaluate_expression(stmt->case_expression);
1365 evaluate_expression(stmt->case_to);
1366 evaluate_statement(stmt->case_statement);
1367 return NULL;
1368 case STMT_LABEL:
1369 evaluate_statement(stmt->label_statement);
1370 return NULL;
1371 case STMT_GOTO:
1372 evaluate_expression(stmt->goto_expression);
1373 return NULL;
1374 case STMT_NONE:
1375 case STMT_BREAK:
1376 case STMT_CONTINUE:
1377 break;
1378 case STMT_ASM:
1379 /* FIXME! Do the asm parameter evaluation! */
1380 break;
1382 return NULL;
1385 long long get_expression_value(struct expression *expr)
1387 long long value, mask;
1388 struct symbol *ctype;
1390 ctype = evaluate_expression(expr);
1391 if (!ctype || expr->type != EXPR_VALUE) {
1392 warn(expr->pos, "bad constant expression");
1393 show_expression(expr);
1394 printf(": badness\n");
1395 return 0;
1398 value = expr->value;
1399 mask = 1ULL << (ctype->bit_size-1);
1401 if (value & mask) {
1402 while (ctype->type != SYM_BASETYPE)
1403 ctype = ctype->ctype.base_type;
1404 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1405 value = value | mask | ~(mask-1);
1407 return value;