new get_type_name function
[smatch.git] / expand.c
blob06b8127781a64d426f1f2f96e876711b122cd1b6
1 /*
2 * sparse/expand.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * expand constant expressions.
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "parse.h"
24 #include "token.h"
25 #include "symbol.h"
26 #include "target.h"
27 #include "expression.h"
29 /* Random cost numbers */
30 #define SIDE_EFFECTS 10000 /* The expression has side effects */
31 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
32 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
33 #define BRANCH_COST 10 /* Cost of a conditional branch */
35 static int expand_expression(struct expression *);
36 static int expand_statement(struct statement *);
37 static int conservative;
39 static int expand_symbol_expression(struct expression *expr)
41 struct symbol *sym = expr->symbol;
43 if (sym == &zero_int) {
44 if (Wundefined_preprocessor)
45 warning(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
46 expr->type = EXPR_VALUE;
47 expr->value = 0;
48 expr->taint = 0;
49 return 0;
51 /* The cost of a symbol expression is lower for on-stack symbols */
52 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
55 static long long get_longlong(struct expression *expr)
57 int no_expand = expr->ctype->ctype.modifiers & MOD_UNSIGNED;
58 long long mask = 1ULL << (expr->ctype->bit_size - 1);
59 long long value = expr->value;
60 long long ormask, andmask;
62 if (!(value & mask))
63 no_expand = 1;
64 andmask = mask | (mask-1);
65 ormask = ~andmask;
66 if (no_expand)
67 ormask = 0;
68 return (value & andmask) | ormask;
71 void cast_value(struct expression *expr, struct symbol *newtype,
72 struct expression *old, struct symbol *oldtype)
74 int old_size = oldtype->bit_size;
75 int new_size = newtype->bit_size;
76 long long value, mask, signmask;
77 long long oldmask, oldsignmask, dropped;
79 if (newtype->ctype.base_type == &fp_type ||
80 oldtype->ctype.base_type == &fp_type)
81 goto Float;
83 // For pointers and integers, we can just move the value around
84 expr->type = EXPR_VALUE;
85 expr->taint = old->taint;
86 if (old_size == new_size) {
87 expr->value = old->value;
88 return;
91 // expand it to the full "long long" value
92 value = get_longlong(old);
94 Int:
95 // Truncate it to the new size
96 signmask = 1ULL << (new_size-1);
97 mask = signmask | (signmask-1);
98 expr->value = value & mask;
100 // Stop here unless checking for truncation
101 if (!Wcast_truncate || conservative)
102 return;
104 // Check if we dropped any bits..
105 oldsignmask = 1ULL << (old_size-1);
106 oldmask = oldsignmask | (oldsignmask-1);
107 dropped = oldmask & ~mask;
109 // OK if the bits were (and still are) purely sign bits
110 if (value & dropped) {
111 if (!(value & oldsignmask) || !(value & signmask) || (value & dropped) != dropped)
112 warning(old->pos, "cast truncates bits from constant value (%llx becomes %llx)",
113 value & oldmask,
114 value & mask);
116 return;
118 Float:
119 if (newtype->ctype.base_type != &fp_type) {
120 value = (long long)old->fvalue;
121 expr->type = EXPR_VALUE;
122 expr->taint = 0;
123 goto Int;
126 if (oldtype->ctype.base_type != &fp_type)
127 expr->fvalue = (long double)get_longlong(old);
128 else
129 expr->fvalue = old->value;
131 if (!(newtype->ctype.modifiers & MOD_LONGLONG)) {
132 if ((newtype->ctype.modifiers & MOD_LONG))
133 expr->fvalue = (double)expr->fvalue;
134 else
135 expr->fvalue = (float)expr->fvalue;
137 expr->type = EXPR_FVALUE;
140 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
142 warning(expr->pos, "shift too big (%u) for type %s", count, show_typename(ctype));
143 count &= ctype->bit_size-1;
144 return count;
148 * CAREFUL! We need to get the size and sign of the
149 * result right!
151 #define CONVERT(op,s) (((op)<<1)+(s))
152 #define SIGNED(op) CONVERT(op, 1)
153 #define UNSIGNED(op) CONVERT(op, 0)
154 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
156 struct expression *left = expr->left, *right = expr->right;
157 unsigned long long v, l, r, mask;
158 signed long long sl, sr;
159 int is_signed;
161 if (right->type != EXPR_VALUE)
162 return 0;
163 r = right->value;
164 if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) {
165 if (r >= ctype->bit_size) {
166 if (conservative)
167 return 0;
168 r = check_shift_count(expr, ctype, r);
169 right->value = r;
172 if (left->type != EXPR_VALUE)
173 return 0;
174 l = left->value; r = right->value;
175 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
176 mask = 1ULL << (ctype->bit_size-1);
177 sl = l; sr = r;
178 if (is_signed && (sl & mask))
179 sl |= ~(mask-1);
180 if (is_signed && (sr & mask))
181 sr |= ~(mask-1);
183 switch (CONVERT(expr->op,is_signed)) {
184 case SIGNED('+'):
185 case UNSIGNED('+'):
186 v = l + r;
187 break;
189 case SIGNED('-'):
190 case UNSIGNED('-'):
191 v = l - r;
192 break;
194 case SIGNED('&'):
195 case UNSIGNED('&'):
196 v = l & r;
197 break;
199 case SIGNED('|'):
200 case UNSIGNED('|'):
201 v = l | r;
202 break;
204 case SIGNED('^'):
205 case UNSIGNED('^'):
206 v = l ^ r;
207 break;
209 case SIGNED('*'):
210 v = sl * sr;
211 break;
213 case UNSIGNED('*'):
214 v = l * r;
215 break;
217 case SIGNED('/'):
218 if (!r)
219 goto Div;
220 if (l == mask && sr == -1)
221 goto Overflow;
222 v = sl / sr;
223 break;
225 case UNSIGNED('/'):
226 if (!r) goto Div;
227 v = l / r;
228 break;
230 case SIGNED('%'):
231 if (!r)
232 goto Div;
233 v = sl % sr;
234 break;
236 case UNSIGNED('%'):
237 if (!r) goto Div;
238 v = l % r;
239 break;
241 case SIGNED(SPECIAL_LEFTSHIFT):
242 case UNSIGNED(SPECIAL_LEFTSHIFT):
243 v = l << r;
244 break;
246 case SIGNED(SPECIAL_RIGHTSHIFT):
247 v = sl >> r;
248 break;
250 case UNSIGNED(SPECIAL_RIGHTSHIFT):
251 v = l >> r;
252 break;
254 default:
255 return 0;
257 mask = mask | (mask-1);
258 expr->value = v & mask;
259 expr->type = EXPR_VALUE;
260 expr->taint = left->taint | right->taint;
261 return 1;
262 Div:
263 if (!conservative)
264 warning(expr->pos, "division by zero");
265 return 0;
266 Overflow:
267 if (!conservative)
268 warning(expr->pos, "constant integer operation overflow");
269 return 0;
272 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
274 struct expression *left = expr->left, *right = expr->right;
275 unsigned long long l, r, mask;
276 signed long long sl, sr;
278 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
279 return 0;
280 l = left->value; r = right->value;
281 mask = 1ULL << (ctype->bit_size-1);
282 sl = l; sr = r;
283 if (sl & mask)
284 sl |= ~(mask-1);
285 if (sr & mask)
286 sr |= ~(mask-1);
287 switch (expr->op) {
288 case '<': expr->value = sl < sr; break;
289 case '>': expr->value = sl > sr; break;
290 case SPECIAL_LTE: expr->value = sl <= sr; break;
291 case SPECIAL_GTE: expr->value = sl >= sr; break;
292 case SPECIAL_EQUAL: expr->value = l == r; break;
293 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
294 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
295 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
296 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
297 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
299 expr->type = EXPR_VALUE;
300 expr->taint = left->taint | right->taint;
301 return 1;
304 static int simplify_float_binop(struct expression *expr)
306 struct expression *left = expr->left, *right = expr->right;
307 unsigned long mod = expr->ctype->ctype.modifiers;
308 long double l, r, res;
310 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
311 return 0;
313 l = left->fvalue;
314 r = right->fvalue;
316 if (mod & MOD_LONGLONG) {
317 switch (expr->op) {
318 case '+': res = l + r; break;
319 case '-': res = l - r; break;
320 case '*': res = l * r; break;
321 case '/': if (!r) goto Div;
322 res = l / r; break;
323 default: return 0;
325 } else if (mod & MOD_LONG) {
326 switch (expr->op) {
327 case '+': res = (double) l + (double) r; break;
328 case '-': res = (double) l - (double) r; break;
329 case '*': res = (double) l * (double) r; break;
330 case '/': if (!r) goto Div;
331 res = (double) l / (double) r; break;
332 default: return 0;
334 } else {
335 switch (expr->op) {
336 case '+': res = (float)l + (float)r; break;
337 case '-': res = (float)l - (float)r; break;
338 case '*': res = (float)l * (float)r; break;
339 case '/': if (!r) goto Div;
340 res = (float)l / (float)r; break;
341 default: return 0;
344 expr->type = EXPR_FVALUE;
345 expr->fvalue = res;
346 return 1;
347 Div:
348 if (!conservative)
349 warning(expr->pos, "division by zero");
350 return 0;
353 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
355 struct expression *left = expr->left, *right = expr->right;
356 long double l, r;
358 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
359 return 0;
361 l = left->fvalue;
362 r = right->fvalue;
363 switch (expr->op) {
364 case '<': expr->value = l < r; break;
365 case '>': expr->value = l > r; break;
366 case SPECIAL_LTE: expr->value = l <= r; break;
367 case SPECIAL_GTE: expr->value = l >= r; break;
368 case SPECIAL_EQUAL: expr->value = l == r; break;
369 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
371 expr->type = EXPR_VALUE;
372 expr->taint = 0;
373 return 1;
376 static int expand_binop(struct expression *expr)
378 int cost;
380 cost = expand_expression(expr->left);
381 cost += expand_expression(expr->right);
382 if (simplify_int_binop(expr, expr->ctype))
383 return 0;
384 if (simplify_float_binop(expr))
385 return 0;
386 return cost + 1;
389 static int expand_logical(struct expression *expr)
391 struct expression *left = expr->left;
392 struct expression *right;
393 int cost, rcost;
395 /* Do immediate short-circuiting ... */
396 cost = expand_expression(left);
397 if (left->type == EXPR_VALUE) {
398 if (expr->op == SPECIAL_LOGICAL_AND) {
399 if (!left->value) {
400 expr->type = EXPR_VALUE;
401 expr->value = 0;
402 expr->taint = left->taint;
403 return 0;
405 } else {
406 if (left->value) {
407 expr->type = EXPR_VALUE;
408 expr->value = 1;
409 expr->taint = left->taint;
410 return 0;
415 right = expr->right;
416 rcost = expand_expression(right);
417 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
419 * We know the left value doesn't matter, since
420 * otherwise we would have short-circuited it..
422 expr->type = EXPR_VALUE;
423 expr->value = right->value != 0;
424 expr->taint = left->taint | right->taint;
425 return 0;
429 * If the right side is safe and cheaper than a branch,
430 * just avoid the branch and turn it into a regular binop
431 * style SAFELOGICAL.
433 if (rcost < BRANCH_COST) {
434 expr->type = EXPR_BINOP;
435 rcost -= BRANCH_COST - 1;
438 return cost + BRANCH_COST + rcost;
441 static int expand_comma(struct expression *expr)
443 int cost;
445 cost = expand_expression(expr->left);
446 cost += expand_expression(expr->right);
447 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE) {
448 unsigned flags = expr->flags;
449 unsigned taint;
450 taint = expr->left->type == EXPR_VALUE ? expr->left->taint : 0;
451 *expr = *expr->right;
452 expr->flags = flags;
453 if (expr->type == EXPR_VALUE)
454 expr->taint |= Taint_comma | taint;
456 return cost;
459 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
461 static int compare_types(int op, struct symbol *left, struct symbol *right)
463 switch (op) {
464 case SPECIAL_EQUAL:
465 return !type_difference(left, right, MOD_IGN, MOD_IGN);
466 case SPECIAL_NOTEQUAL:
467 return type_difference(left, right, MOD_IGN, MOD_IGN) != NULL;
468 case '<':
469 return left->bit_size < right->bit_size;
470 case '>':
471 return left->bit_size > right->bit_size;
472 case SPECIAL_LTE:
473 return left->bit_size <= right->bit_size;
474 case SPECIAL_GTE:
475 return left->bit_size >= right->bit_size;
477 return 0;
480 static int expand_compare(struct expression *expr)
482 struct expression *left = expr->left, *right = expr->right;
483 int cost;
485 cost = expand_expression(left);
486 cost += expand_expression(right);
488 if (left && right) {
489 /* Type comparison? */
490 if (left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
491 int op = expr->op;
492 expr->type = EXPR_VALUE;
493 expr->value = compare_types(op, left->symbol, right->symbol);
494 expr->taint = 0;
495 return 0;
497 if (simplify_cmp_binop(expr, left->ctype))
498 return 0;
499 if (simplify_float_cmp(expr, left->ctype))
500 return 0;
502 return cost + 1;
505 static int expand_conditional(struct expression *expr)
507 struct expression *cond = expr->conditional;
508 struct expression *true = expr->cond_true;
509 struct expression *false = expr->cond_false;
510 int cost, cond_cost;
512 cond_cost = expand_expression(cond);
513 if (cond->type == EXPR_VALUE) {
514 unsigned flags = expr->flags;
515 if (!cond->value)
516 true = false;
517 if (!true)
518 true = cond;
519 cost = expand_expression(true);
520 *expr = *true;
521 expr->flags = flags;
522 if (expr->type == EXPR_VALUE)
523 expr->taint |= cond->taint;
524 return cost;
527 cost = expand_expression(true);
528 cost += expand_expression(false);
530 if (cost < SELECT_COST) {
531 expr->type = EXPR_SELECT;
532 cost -= BRANCH_COST - 1;
535 return cost + cond_cost + BRANCH_COST;
538 static int expand_assignment(struct expression *expr)
540 expand_expression(expr->left);
541 expand_expression(expr->right);
542 return SIDE_EFFECTS;
545 static int expand_addressof(struct expression *expr)
547 return expand_expression(expr->unop);
551 * Look up a trustable initializer value at the requested offset.
553 * Return NULL if no such value can be found or statically trusted.
555 * FIXME!! We should check that the size is right!
557 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
559 struct expression *value;
561 if (sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))
562 return NULL;
563 value = sym->initializer;
564 if (!value)
565 return NULL;
566 if (value->type == EXPR_INITIALIZER) {
567 struct expression *entry;
568 FOR_EACH_PTR(value->expr_list, entry) {
569 if (entry->type != EXPR_POS) {
570 if (offset)
571 continue;
572 return entry;
574 if (entry->init_offset < offset)
575 continue;
576 if (entry->init_offset > offset)
577 return NULL;
578 return entry->init_expr;
579 } END_FOR_EACH_PTR(entry);
580 return NULL;
582 return value;
585 static int expand_dereference(struct expression *expr)
587 struct expression *unop = expr->unop;
588 unsigned int offset;
590 expand_expression(unop);
593 * NOTE! We get a bogus warning right now for some special
594 * cases: apparently I've screwed up the optimization of
595 * a zero-offset dereference, and the ctype is wrong.
597 * Leave the warning in anyway, since this is also a good
598 * test for me to get the type evaluation right..
600 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
601 warning(unop->pos, "dereference of noderef expression");
604 * Is it "symbol" or "symbol + offset"?
606 offset = 0;
607 if (unop->type == EXPR_BINOP && unop->op == '+') {
608 struct expression *right = unop->right;
609 if (right->type == EXPR_VALUE) {
610 offset = right->value;
611 unop = unop->left;
615 if (unop->type == EXPR_SYMBOL) {
616 struct symbol *sym = unop->symbol;
617 struct expression *value = constant_symbol_value(sym, offset);
619 /* Const symbol with a constant initializer? */
620 if (value) {
621 /* FIXME! We should check that the size is right! */
622 if (value->type == EXPR_VALUE) {
623 expr->type = EXPR_VALUE;
624 expr->value = value->value;
625 expr->taint = 0;
626 return 0;
627 } else if (value->type == EXPR_FVALUE) {
628 expr->type = EXPR_FVALUE;
629 expr->fvalue = value->fvalue;
630 return 0;
634 /* Direct symbol dereference? Cheap and safe */
635 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
638 return UNSAFE;
641 static int simplify_preop(struct expression *expr)
643 struct expression *op = expr->unop;
644 unsigned long long v, mask;
646 if (op->type != EXPR_VALUE)
647 return 0;
649 mask = 1ULL << (expr->ctype->bit_size-1);
650 v = op->value;
651 switch (expr->op) {
652 case '+': break;
653 case '-':
654 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
655 goto Overflow;
656 v = -v;
657 break;
658 case '!': v = !v; break;
659 case '~': v = ~v; break;
660 default: return 0;
662 mask = mask | (mask-1);
663 expr->value = v & mask;
664 expr->type = EXPR_VALUE;
665 expr->taint = op->taint;
666 return 1;
668 Overflow:
669 if (!conservative)
670 warning(expr->pos, "constant integer operation overflow");
671 return 0;
674 static int simplify_float_preop(struct expression *expr)
676 struct expression *op = expr->unop;
677 long double v;
679 if (op->type != EXPR_FVALUE)
680 return 0;
681 v = op->fvalue;
682 switch (expr->op) {
683 case '+': break;
684 case '-': v = -v; break;
685 default: return 0;
687 expr->fvalue = v;
688 expr->type = EXPR_FVALUE;
689 return 1;
693 * Unary post-ops: x++ and x--
695 static int expand_postop(struct expression *expr)
697 expand_expression(expr->unop);
698 return SIDE_EFFECTS;
701 static int expand_preop(struct expression *expr)
703 int cost;
705 switch (expr->op) {
706 case '*':
707 return expand_dereference(expr);
709 case '&':
710 return expand_addressof(expr);
712 case SPECIAL_INCREMENT:
713 case SPECIAL_DECREMENT:
715 * From a type evaluation standpoint the preops are
716 * the same as the postops
718 return expand_postop(expr);
720 default:
721 break;
723 cost = expand_expression(expr->unop);
725 if (simplify_preop(expr))
726 return 0;
727 if (simplify_float_preop(expr))
728 return 0;
729 return cost + 1;
732 static int expand_arguments(struct expression_list *head)
734 int cost = 0;
735 struct expression *expr;
737 FOR_EACH_PTR (head, expr) {
738 cost += expand_expression(expr);
739 } END_FOR_EACH_PTR(expr);
740 return cost;
743 static int expand_cast(struct expression *expr)
745 int cost;
746 struct expression *target = expr->cast_expression;
748 cost = expand_expression(target);
750 /* Simplify normal integer casts.. */
751 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
752 cast_value(expr, expr->ctype, target, target->ctype);
753 return 0;
755 return cost + 1;
758 /* The arguments are constant if the cost of all of them is zero */
759 int expand_constant_p(struct expression *expr, int cost)
761 expr->type = EXPR_VALUE;
762 expr->value = !cost;
763 expr->taint = 0;
764 return 0;
767 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
768 int expand_safe_p(struct expression *expr, int cost)
770 expr->type = EXPR_VALUE;
771 expr->value = (cost < SIDE_EFFECTS);
772 expr->taint = 0;
773 return 0;
777 * expand a call expression with a symbol. This
778 * should expand builtins.
780 static int expand_symbol_call(struct expression *expr, int cost)
782 struct expression *fn = expr->fn;
783 struct symbol *ctype = fn->ctype;
785 if (fn->type != EXPR_PREOP)
786 return SIDE_EFFECTS;
788 if (ctype->op && ctype->op->expand)
789 return ctype->op->expand(expr, cost);
791 return SIDE_EFFECTS;
794 static int expand_call(struct expression *expr)
796 int cost;
797 struct symbol *sym;
798 struct expression *fn = expr->fn;
800 cost = expand_arguments(expr->args);
801 sym = fn->ctype;
802 if (!sym) {
803 expression_error(expr, "function has no type");
804 return SIDE_EFFECTS;
806 if (sym->type == SYM_NODE)
807 return expand_symbol_call(expr, cost);
809 return SIDE_EFFECTS;
812 static int expand_expression_list(struct expression_list *list)
814 int cost = 0;
815 struct expression *expr;
817 FOR_EACH_PTR(list, expr) {
818 cost += expand_expression(expr);
819 } END_FOR_EACH_PTR(expr);
820 return cost;
824 * We can simplify nested position expressions if
825 * this is a simple (single) positional expression.
827 static int expand_pos_expression(struct expression *expr)
829 struct expression *nested = expr->init_expr;
830 unsigned long offset = expr->init_offset;
831 int nr = expr->init_nr;
833 if (nr == 1) {
834 switch (nested->type) {
835 case EXPR_POS:
836 offset += nested->init_offset;
837 *expr = *nested;
838 expr->init_offset = offset;
839 nested = expr;
840 break;
842 case EXPR_INITIALIZER: {
843 struct expression *reuse = nested, *entry;
844 *expr = *nested;
845 FOR_EACH_PTR(expr->expr_list, entry) {
846 if (entry->type == EXPR_POS) {
847 entry->init_offset += offset;
848 } else {
849 if (!reuse) {
851 * This happens rarely, but it can happen
852 * with bitfields that are all at offset
853 * zero..
855 reuse = alloc_expression(entry->pos, EXPR_POS);
857 reuse->type = EXPR_POS;
858 reuse->ctype = entry->ctype;
859 reuse->init_offset = offset;
860 reuse->init_nr = 1;
861 reuse->init_expr = entry;
862 REPLACE_CURRENT_PTR(entry, reuse);
863 reuse = NULL;
865 } END_FOR_EACH_PTR(entry);
866 nested = expr;
867 break;
870 default:
871 break;
874 return expand_expression(nested);
877 static unsigned long bit_offset(const struct expression *expr)
879 unsigned long offset = 0;
880 while (expr->type == EXPR_POS) {
881 offset += expr->init_offset << 3;
882 expr = expr->init_expr;
884 if (expr && expr->ctype)
885 offset += expr->ctype->bit_offset;
886 return offset;
889 static int compare_expressions(const void *_a, const void *_b)
891 const struct expression *a = _a;
892 const struct expression *b = _b;
893 unsigned long a_pos = bit_offset(a);
894 unsigned long b_pos = bit_offset(b);
896 return (a_pos < b_pos) ? -1 : (a_pos == b_pos) ? 0 : 1;
899 static void sort_expression_list(struct expression_list **list)
901 sort_list((struct ptr_list **)list, compare_expressions);
904 static void verify_nonoverlapping(struct expression_list **list)
906 struct expression *a = NULL;
907 struct expression *b;
909 FOR_EACH_PTR(*list, b) {
910 if (!b->ctype || !b->ctype->bit_size)
911 continue;
912 if (a && bit_offset(a) == bit_offset(b)) {
913 sparse_error(a->pos, "Initializer entry defined twice");
914 info(b->pos, " also defined here");
915 return;
917 a = b;
918 } END_FOR_EACH_PTR(b);
921 static int expand_expression(struct expression *expr)
923 if (!expr)
924 return 0;
925 if (!expr->ctype || expr->ctype == &bad_ctype)
926 return UNSAFE;
928 switch (expr->type) {
929 case EXPR_VALUE:
930 case EXPR_FVALUE:
931 case EXPR_STRING:
932 return 0;
933 case EXPR_TYPE:
934 case EXPR_SYMBOL:
935 return expand_symbol_expression(expr);
936 case EXPR_BINOP:
937 return expand_binop(expr);
939 case EXPR_LOGICAL:
940 return expand_logical(expr);
942 case EXPR_COMMA:
943 return expand_comma(expr);
945 case EXPR_COMPARE:
946 return expand_compare(expr);
948 case EXPR_ASSIGNMENT:
949 return expand_assignment(expr);
951 case EXPR_PREOP:
952 return expand_preop(expr);
954 case EXPR_POSTOP:
955 return expand_postop(expr);
957 case EXPR_CAST:
958 case EXPR_FORCE_CAST:
959 case EXPR_IMPLIED_CAST:
960 return expand_cast(expr);
962 case EXPR_CALL:
963 return expand_call(expr);
965 case EXPR_DEREF:
966 warning(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
967 return UNSAFE;
969 case EXPR_SELECT:
970 case EXPR_CONDITIONAL:
971 return expand_conditional(expr);
973 case EXPR_STATEMENT: {
974 struct statement *stmt = expr->statement;
975 int cost = expand_statement(stmt);
977 if (stmt->type == STMT_EXPRESSION && stmt->expression)
978 *expr = *stmt->expression;
979 return cost;
982 case EXPR_LABEL:
983 return 0;
985 case EXPR_INITIALIZER:
986 sort_expression_list(&expr->expr_list);
987 verify_nonoverlapping(&expr->expr_list);
988 return expand_expression_list(expr->expr_list);
990 case EXPR_IDENTIFIER:
991 return UNSAFE;
993 case EXPR_INDEX:
994 return UNSAFE;
996 case EXPR_SLICE:
997 return expand_expression(expr->base) + 1;
999 case EXPR_POS:
1000 return expand_pos_expression(expr);
1002 case EXPR_SIZEOF:
1003 case EXPR_PTRSIZEOF:
1004 case EXPR_ALIGNOF:
1005 case EXPR_OFFSETOF:
1006 expression_error(expr, "internal front-end error: sizeof in expansion?");
1007 return UNSAFE;
1009 return SIDE_EFFECTS;
1012 static void expand_const_expression(struct expression *expr, const char *where)
1014 if (expr) {
1015 expand_expression(expr);
1016 if (expr->type != EXPR_VALUE)
1017 expression_error(expr, "Expected constant expression in %s", where);
1021 int expand_symbol(struct symbol *sym)
1023 int retval;
1024 struct symbol *base_type;
1026 if (!sym)
1027 return 0;
1028 base_type = sym->ctype.base_type;
1029 if (!base_type)
1030 return 0;
1032 retval = expand_expression(sym->initializer);
1033 /* expand the body of the symbol */
1034 if (base_type->type == SYM_FN) {
1035 if (base_type->stmt)
1036 expand_statement(base_type->stmt);
1038 return retval;
1041 static void expand_return_expression(struct statement *stmt)
1043 expand_expression(stmt->expression);
1046 static int expand_if_statement(struct statement *stmt)
1048 struct expression *expr = stmt->if_conditional;
1050 if (!expr || !expr->ctype || expr->ctype == &bad_ctype)
1051 return UNSAFE;
1053 expand_expression(expr);
1055 /* This is only valid if nobody jumps into the "dead" side */
1056 #if 0
1057 /* Simplify constant conditionals without even evaluating the false side */
1058 if (expr->type == EXPR_VALUE) {
1059 struct statement *simple;
1060 simple = expr->value ? stmt->if_true : stmt->if_false;
1062 /* Nothing? */
1063 if (!simple) {
1064 stmt->type = STMT_NONE;
1065 return 0;
1067 expand_statement(simple);
1068 *stmt = *simple;
1069 return SIDE_EFFECTS;
1071 #endif
1072 expand_statement(stmt->if_true);
1073 expand_statement(stmt->if_false);
1074 return SIDE_EFFECTS;
1078 * Expanding a compound statement is really just
1079 * about adding up the costs of each individual
1080 * statement.
1082 * We also collapse a simple compound statement:
1083 * this would trigger for simple inline functions,
1084 * except we would have to check the "return"
1085 * symbol usage. Next time.
1087 static int expand_compound(struct statement *stmt)
1089 struct statement *s, *last;
1090 int cost, statements;
1092 if (stmt->ret)
1093 expand_symbol(stmt->ret);
1095 last = stmt->args;
1096 cost = expand_statement(last);
1097 statements = last != NULL;
1098 FOR_EACH_PTR(stmt->stmts, s) {
1099 statements++;
1100 last = s;
1101 cost += expand_statement(s);
1102 } END_FOR_EACH_PTR(s);
1104 if (statements == 1 && !stmt->ret)
1105 *stmt = *last;
1107 return cost;
1110 static int expand_statement(struct statement *stmt)
1112 if (!stmt)
1113 return 0;
1115 switch (stmt->type) {
1116 case STMT_DECLARATION: {
1117 struct symbol *sym;
1118 FOR_EACH_PTR(stmt->declaration, sym) {
1119 expand_symbol(sym);
1120 } END_FOR_EACH_PTR(sym);
1121 return SIDE_EFFECTS;
1124 case STMT_RETURN:
1125 expand_return_expression(stmt);
1126 return SIDE_EFFECTS;
1128 case STMT_EXPRESSION:
1129 return expand_expression(stmt->expression);
1131 case STMT_COMPOUND:
1132 return expand_compound(stmt);
1134 case STMT_IF:
1135 return expand_if_statement(stmt);
1137 case STMT_ITERATOR:
1138 expand_expression(stmt->iterator_pre_condition);
1139 expand_expression(stmt->iterator_post_condition);
1140 expand_statement(stmt->iterator_pre_statement);
1141 expand_statement(stmt->iterator_statement);
1142 expand_statement(stmt->iterator_post_statement);
1143 return SIDE_EFFECTS;
1145 case STMT_SWITCH:
1146 expand_expression(stmt->switch_expression);
1147 expand_statement(stmt->switch_statement);
1148 return SIDE_EFFECTS;
1150 case STMT_CASE:
1151 expand_const_expression(stmt->case_expression, "case statement");
1152 expand_const_expression(stmt->case_to, "case statement");
1153 expand_statement(stmt->case_statement);
1154 return SIDE_EFFECTS;
1156 case STMT_LABEL:
1157 expand_statement(stmt->label_statement);
1158 return SIDE_EFFECTS;
1160 case STMT_GOTO:
1161 expand_expression(stmt->goto_expression);
1162 return SIDE_EFFECTS;
1164 case STMT_NONE:
1165 break;
1166 case STMT_ASM:
1167 /* FIXME! Do the asm parameter evaluation! */
1168 break;
1169 case STMT_CONTEXT:
1170 expand_expression(stmt->expression);
1171 break;
1172 case STMT_RANGE:
1173 expand_expression(stmt->range_expression);
1174 expand_expression(stmt->range_low);
1175 expand_expression(stmt->range_high);
1176 break;
1178 return SIDE_EFFECTS;
1181 static inline int bad_integer_constant_expression(struct expression *expr)
1183 if (!(expr->flags & Int_const_expr))
1184 return 1;
1185 if (expr->taint & Taint_comma)
1186 return 1;
1187 return 0;
1190 static long long __get_expression_value(struct expression *expr, int strict)
1192 long long value, mask;
1193 struct symbol *ctype;
1195 if (!expr)
1196 return 0;
1197 ctype = evaluate_expression(expr);
1198 if (!ctype) {
1199 expression_error(expr, "bad constant expression type");
1200 return 0;
1202 expand_expression(expr);
1203 if (expr->type != EXPR_VALUE) {
1204 expression_error(expr, "bad constant expression");
1205 return 0;
1207 if (strict && bad_integer_constant_expression(expr)) {
1208 expression_error(expr, "bad integer constant expression");
1209 return 0;
1212 value = expr->value;
1213 mask = 1ULL << (ctype->bit_size-1);
1215 if (value & mask) {
1216 while (ctype->type != SYM_BASETYPE)
1217 ctype = ctype->ctype.base_type;
1218 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1219 value = value | mask | ~(mask-1);
1221 return value;
1224 long long get_expression_value(struct expression *expr)
1226 return __get_expression_value(expr, 0);
1229 long long const_expression_value(struct expression *expr)
1231 return __get_expression_value(expr, 1);
1234 int is_zero_constant(struct expression *expr)
1236 int saved = conservative;
1237 conservative = 1;
1238 expand_expression(expr);
1239 conservative = saved;
1240 return expr->type == EXPR_VALUE && !expr->value;