add killing of pure calls
[smatch.git] / expand.c
blob7af12707edbca64bbe9d0d593359f3a20ec33d11
1 /*
2 * sparse/expand.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 * expand constant expressions.
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include "lib.h"
38 #include "allocate.h"
39 #include "parse.h"
40 #include "token.h"
41 #include "symbol.h"
42 #include "target.h"
43 #include "expression.h"
44 #include "expand.h"
47 static int expand_expression(struct expression *);
48 static int expand_statement(struct statement *);
49 static int conservative;
51 static int expand_symbol_expression(struct expression *expr)
53 struct symbol *sym = expr->symbol;
55 if (sym == &zero_int) {
56 if (Wundef)
57 warning(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
58 expr->type = EXPR_VALUE;
59 expr->value = 0;
60 expr->taint = 0;
61 return 0;
63 /* The cost of a symbol expression is lower for on-stack symbols */
64 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
67 static long long get_longlong(struct expression *expr)
69 int no_expand = expr->ctype->ctype.modifiers & MOD_UNSIGNED;
70 long long mask = 1ULL << (expr->ctype->bit_size - 1);
71 long long value = expr->value;
72 long long ormask, andmask;
74 if (!(value & mask))
75 no_expand = 1;
76 andmask = mask | (mask-1);
77 ormask = ~andmask;
78 if (no_expand)
79 ormask = 0;
80 return (value & andmask) | ormask;
83 void cast_value(struct expression *expr, struct symbol *newtype,
84 struct expression *old, struct symbol *oldtype)
86 int old_size = oldtype->bit_size;
87 int new_size = newtype->bit_size;
88 long long value, mask, signmask;
89 long long oldmask, oldsignmask, dropped;
91 if (newtype->ctype.base_type == &fp_type ||
92 oldtype->ctype.base_type == &fp_type)
93 goto Float;
95 // For pointers and integers, we can just move the value around
96 expr->type = EXPR_VALUE;
97 expr->taint = old->taint;
98 if (old_size == new_size) {
99 expr->value = old->value;
100 return;
103 // expand it to the full "long long" value
104 value = get_longlong(old);
106 Int:
107 // _Bool requires a zero test rather than truncation.
108 if (is_bool_type(newtype)) {
109 expr->value = !!value;
110 if (!conservative && value != 0 && value != 1)
111 warning(old->pos, "odd constant _Bool cast (%llx becomes 1)", value);
112 return;
115 // Truncate it to the new size
116 signmask = 1ULL << (new_size-1);
117 mask = signmask | (signmask-1);
118 expr->value = value & mask;
120 // Stop here unless checking for truncation
121 if (!Wcast_truncate || conservative)
122 return;
124 // Check if we dropped any bits..
125 oldsignmask = 1ULL << (old_size-1);
126 oldmask = oldsignmask | (oldsignmask-1);
127 dropped = oldmask & ~mask;
129 // OK if the bits were (and still are) purely sign bits
130 if (value & dropped) {
131 if (!(value & oldsignmask) || !(value & signmask) || (value & dropped) != dropped)
132 warning(old->pos, "cast truncates bits from constant value (%llx becomes %llx)",
133 value & oldmask,
134 value & mask);
136 return;
138 Float:
139 if (!is_float_type(newtype)) {
140 value = (long long)old->fvalue;
141 expr->type = EXPR_VALUE;
142 expr->taint = 0;
143 goto Int;
146 if (!is_float_type(oldtype))
147 expr->fvalue = (long double)get_longlong(old);
148 else
149 expr->fvalue = old->fvalue;
151 if (!(newtype->ctype.modifiers & MOD_LONGLONG) && \
152 !(newtype->ctype.modifiers & MOD_LONGLONGLONG)) {
153 if ((newtype->ctype.modifiers & MOD_LONG))
154 expr->fvalue = (double)expr->fvalue;
155 else
156 expr->fvalue = (float)expr->fvalue;
158 expr->type = EXPR_FVALUE;
161 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
163 warning(expr->pos, "shift too big (%u) for type %s", count, show_typename(ctype));
164 count &= ctype->bit_size-1;
165 return count;
169 * CAREFUL! We need to get the size and sign of the
170 * result right!
172 #define CONVERT(op,s) (((op)<<1)+(s))
173 #define SIGNED(op) CONVERT(op, 1)
174 #define UNSIGNED(op) CONVERT(op, 0)
175 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
177 struct expression *left = expr->left, *right = expr->right;
178 unsigned long long v, l, r, mask;
179 signed long long sl, sr;
180 int is_signed;
182 if (right->type != EXPR_VALUE)
183 return 0;
184 r = right->value;
185 if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) {
186 if (r >= ctype->bit_size) {
187 if (conservative)
188 return 0;
189 r = check_shift_count(expr, ctype, r);
190 right->value = r;
193 if (left->type != EXPR_VALUE)
194 return 0;
195 l = left->value; r = right->value;
196 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
197 mask = 1ULL << (ctype->bit_size-1);
198 sl = l; sr = r;
199 if (is_signed && (sl & mask))
200 sl |= ~(mask-1);
201 if (is_signed && (sr & mask))
202 sr |= ~(mask-1);
204 switch (CONVERT(expr->op,is_signed)) {
205 case SIGNED('+'):
206 case UNSIGNED('+'):
207 v = l + r;
208 break;
210 case SIGNED('-'):
211 case UNSIGNED('-'):
212 v = l - r;
213 break;
215 case SIGNED('&'):
216 case UNSIGNED('&'):
217 v = l & r;
218 break;
220 case SIGNED('|'):
221 case UNSIGNED('|'):
222 v = l | r;
223 break;
225 case SIGNED('^'):
226 case UNSIGNED('^'):
227 v = l ^ r;
228 break;
230 case SIGNED('*'):
231 v = sl * sr;
232 break;
234 case UNSIGNED('*'):
235 v = l * r;
236 break;
238 case SIGNED('/'):
239 if (!r)
240 goto Div;
241 if (l == mask && sr == -1)
242 goto Overflow;
243 v = sl / sr;
244 break;
246 case UNSIGNED('/'):
247 if (!r) goto Div;
248 v = l / r;
249 break;
251 case SIGNED('%'):
252 if (!r)
253 goto Div;
254 if (l == mask && sr == -1)
255 goto Overflow;
256 v = sl % sr;
257 break;
259 case UNSIGNED('%'):
260 if (!r) goto Div;
261 v = l % r;
262 break;
264 case SIGNED(SPECIAL_LEFTSHIFT):
265 case UNSIGNED(SPECIAL_LEFTSHIFT):
266 v = l << r;
267 break;
269 case SIGNED(SPECIAL_RIGHTSHIFT):
270 v = sl >> r;
271 break;
273 case UNSIGNED(SPECIAL_RIGHTSHIFT):
274 v = l >> r;
275 break;
277 default:
278 return 0;
280 mask = mask | (mask-1);
281 expr->value = v & mask;
282 expr->type = EXPR_VALUE;
283 expr->taint = left->taint | right->taint;
284 return 1;
285 Div:
286 if (!conservative)
287 warning(expr->pos, "division by zero");
288 return 0;
289 Overflow:
290 if (!conservative)
291 warning(expr->pos, "constant integer operation overflow");
292 return 0;
295 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
297 struct expression *left = expr->left, *right = expr->right;
298 unsigned long long l, r, mask;
299 signed long long sl, sr;
301 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
302 return 0;
303 l = left->value; r = right->value;
304 mask = 1ULL << (ctype->bit_size-1);
305 sl = l; sr = r;
306 if (sl & mask)
307 sl |= ~(mask-1);
308 if (sr & mask)
309 sr |= ~(mask-1);
310 switch (expr->op) {
311 case '<': expr->value = sl < sr; break;
312 case '>': expr->value = sl > sr; break;
313 case SPECIAL_LTE: expr->value = sl <= sr; break;
314 case SPECIAL_GTE: expr->value = sl >= sr; break;
315 case SPECIAL_EQUAL: expr->value = l == r; break;
316 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
317 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
318 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
319 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
320 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
322 expr->type = EXPR_VALUE;
323 expr->taint = left->taint | right->taint;
324 return 1;
327 static int simplify_float_binop(struct expression *expr)
329 struct expression *left = expr->left, *right = expr->right;
330 unsigned long mod = expr->ctype->ctype.modifiers;
331 long double l, r, res;
333 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
334 return 0;
336 l = left->fvalue;
337 r = right->fvalue;
339 if (mod & MOD_LONGLONG) {
340 switch (expr->op) {
341 case '+': res = l + r; break;
342 case '-': res = l - r; break;
343 case '*': res = l * r; break;
344 case '/': if (!r) goto Div;
345 res = l / r; break;
346 default: return 0;
348 } else if (mod & MOD_LONG) {
349 switch (expr->op) {
350 case '+': res = (double) l + (double) r; break;
351 case '-': res = (double) l - (double) r; break;
352 case '*': res = (double) l * (double) r; break;
353 case '/': if (!r) goto Div;
354 res = (double) l / (double) r; break;
355 default: return 0;
357 } else {
358 switch (expr->op) {
359 case '+': res = (float)l + (float)r; break;
360 case '-': res = (float)l - (float)r; break;
361 case '*': res = (float)l * (float)r; break;
362 case '/': if (!r) goto Div;
363 res = (float)l / (float)r; break;
364 default: return 0;
367 expr->type = EXPR_FVALUE;
368 expr->fvalue = res;
369 return 1;
370 Div:
371 if (!conservative)
372 warning(expr->pos, "division by zero");
373 return 0;
376 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
378 struct expression *left = expr->left, *right = expr->right;
379 long double l, r;
381 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
382 return 0;
384 l = left->fvalue;
385 r = right->fvalue;
386 switch (expr->op) {
387 case '<': expr->value = l < r; break;
388 case '>': expr->value = l > r; break;
389 case SPECIAL_LTE: expr->value = l <= r; break;
390 case SPECIAL_GTE: expr->value = l >= r; break;
391 case SPECIAL_EQUAL: expr->value = l == r; break;
392 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
394 expr->type = EXPR_VALUE;
395 expr->taint = 0;
396 return 1;
399 static int expand_binop(struct expression *expr)
401 int cost;
403 cost = expand_expression(expr->left);
404 cost += expand_expression(expr->right);
405 if (simplify_int_binop(expr, expr->ctype))
406 return 0;
407 if (simplify_float_binop(expr))
408 return 0;
409 return cost + 1;
412 static int expand_logical(struct expression *expr)
414 struct expression *left = expr->left;
415 struct expression *right;
416 int cost, rcost;
418 /* Do immediate short-circuiting ... */
419 cost = expand_expression(left);
420 if (left->type == EXPR_VALUE) {
421 if (expr->op == SPECIAL_LOGICAL_AND) {
422 if (!left->value) {
423 expr->type = EXPR_VALUE;
424 expr->value = 0;
425 expr->taint = left->taint;
426 return 0;
428 } else {
429 if (left->value) {
430 expr->type = EXPR_VALUE;
431 expr->value = 1;
432 expr->taint = left->taint;
433 return 0;
438 right = expr->right;
439 rcost = expand_expression(right);
440 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
442 * We know the left value doesn't matter, since
443 * otherwise we would have short-circuited it..
445 expr->type = EXPR_VALUE;
446 expr->value = right->value != 0;
447 expr->taint = left->taint | right->taint;
448 return 0;
452 * If the right side is safe and cheaper than a branch,
453 * just avoid the branch and turn it into a regular binop
454 * style SAFELOGICAL.
456 if (rcost < BRANCH_COST) {
457 expr->type = EXPR_BINOP;
458 rcost -= BRANCH_COST - 1;
461 return cost + BRANCH_COST + rcost;
464 static int expand_comma(struct expression *expr)
466 int cost;
468 cost = expand_expression(expr->left);
469 cost += expand_expression(expr->right);
470 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE) {
471 unsigned flags = expr->flags;
472 unsigned taint;
473 taint = expr->left->type == EXPR_VALUE ? expr->left->taint : 0;
474 *expr = *expr->right;
475 expr->flags = flags;
476 if (expr->type == EXPR_VALUE)
477 expr->taint |= Taint_comma | taint;
479 return cost;
482 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
484 static int compare_types(int op, struct symbol *left, struct symbol *right)
486 struct ctype c1 = {.base_type = left};
487 struct ctype c2 = {.base_type = right};
488 switch (op) {
489 case SPECIAL_EQUAL:
490 return !type_difference(&c1, &c2, MOD_IGN, MOD_IGN);
491 case SPECIAL_NOTEQUAL:
492 return type_difference(&c1, &c2, MOD_IGN, MOD_IGN) != NULL;
493 case '<':
494 return left->bit_size < right->bit_size;
495 case '>':
496 return left->bit_size > right->bit_size;
497 case SPECIAL_LTE:
498 return left->bit_size <= right->bit_size;
499 case SPECIAL_GTE:
500 return left->bit_size >= right->bit_size;
502 return 0;
505 static int expand_compare(struct expression *expr)
507 struct expression *left = expr->left, *right = expr->right;
508 int cost;
510 cost = expand_expression(left);
511 cost += expand_expression(right);
513 if (left && right) {
514 /* Type comparison? */
515 if (left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
516 int op = expr->op;
517 expr->type = EXPR_VALUE;
518 expr->value = compare_types(op, left->symbol, right->symbol);
519 expr->taint = 0;
520 return 0;
522 if (simplify_cmp_binop(expr, left->ctype))
523 return 0;
524 if (simplify_float_cmp(expr, left->ctype))
525 return 0;
527 return cost + 1;
530 static int expand_conditional(struct expression *expr)
532 struct expression *cond = expr->conditional;
533 struct expression *true = expr->cond_true;
534 struct expression *false = expr->cond_false;
535 int cost, cond_cost;
537 cond_cost = expand_expression(cond);
538 if (cond->type == EXPR_VALUE) {
539 unsigned flags = expr->flags;
540 if (!cond->value)
541 true = false;
542 if (!true)
543 true = cond;
544 cost = expand_expression(true);
545 *expr = *true;
546 expr->flags = flags;
547 if (expr->type == EXPR_VALUE)
548 expr->taint |= cond->taint;
549 return cost;
552 cost = expand_expression(true);
553 cost += expand_expression(false);
555 if (cost < SELECT_COST) {
556 expr->type = EXPR_SELECT;
557 cost -= BRANCH_COST - 1;
560 return cost + cond_cost + BRANCH_COST;
563 static int expand_assignment(struct expression *expr)
565 expand_expression(expr->left);
566 expand_expression(expr->right);
567 return SIDE_EFFECTS;
570 static int expand_addressof(struct expression *expr)
572 return expand_expression(expr->unop);
576 * Look up a trustable initializer value at the requested offset.
578 * Return NULL if no such value can be found or statically trusted.
580 * FIXME!! We should check that the size is right!
582 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
584 struct expression *value;
586 if (sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))
587 return NULL;
588 value = sym->initializer;
589 if (!value)
590 return NULL;
591 if (value->type == EXPR_INITIALIZER) {
592 struct expression *entry;
593 FOR_EACH_PTR(value->expr_list, entry) {
594 if (entry->type != EXPR_POS) {
595 if (offset)
596 continue;
597 return entry;
599 if (entry->init_offset < offset)
600 continue;
601 if (entry->init_offset > offset)
602 return NULL;
603 return entry->init_expr;
604 } END_FOR_EACH_PTR(entry);
605 return NULL;
607 return value;
610 static int expand_dereference(struct expression *expr)
612 struct expression *unop = expr->unop;
613 unsigned int offset;
615 expand_expression(unop);
618 * NOTE! We get a bogus warning right now for some special
619 * cases: apparently I've screwed up the optimization of
620 * a zero-offset dereference, and the ctype is wrong.
622 * Leave the warning in anyway, since this is also a good
623 * test for me to get the type evaluation right..
625 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
626 warning(unop->pos, "dereference of noderef expression");
629 * Is it "symbol" or "symbol + offset"?
631 offset = 0;
632 if (unop->type == EXPR_BINOP && unop->op == '+') {
633 struct expression *right = unop->right;
634 if (right->type == EXPR_VALUE) {
635 offset = right->value;
636 unop = unop->left;
640 if (unop->type == EXPR_SYMBOL) {
641 struct symbol *sym = unop->symbol;
642 struct expression *value = constant_symbol_value(sym, offset);
644 /* Const symbol with a constant initializer? */
645 if (value) {
646 /* FIXME! We should check that the size is right! */
647 if (value->type == EXPR_VALUE) {
648 expr->type = EXPR_VALUE;
649 expr->value = value->value;
650 expr->taint = 0;
651 return 0;
652 } else if (value->type == EXPR_FVALUE) {
653 expr->type = EXPR_FVALUE;
654 expr->fvalue = value->fvalue;
655 return 0;
659 /* Direct symbol dereference? Cheap and safe */
660 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
663 return UNSAFE;
666 static int simplify_preop(struct expression *expr)
668 struct expression *op = expr->unop;
669 unsigned long long v, mask;
671 if (op->type != EXPR_VALUE)
672 return 0;
674 mask = 1ULL << (expr->ctype->bit_size-1);
675 v = op->value;
676 switch (expr->op) {
677 case '+': break;
678 case '-':
679 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
680 goto Overflow;
681 v = -v;
682 break;
683 case '!': v = !v; break;
684 case '~': v = ~v; break;
685 default: return 0;
687 mask = mask | (mask-1);
688 expr->value = v & mask;
689 expr->type = EXPR_VALUE;
690 expr->taint = op->taint;
691 return 1;
693 Overflow:
694 if (!conservative)
695 warning(expr->pos, "constant integer operation overflow");
696 return 0;
699 static int simplify_float_preop(struct expression *expr)
701 struct expression *op = expr->unop;
702 long double v;
704 if (op->type != EXPR_FVALUE)
705 return 0;
706 v = op->fvalue;
707 switch (expr->op) {
708 case '+': break;
709 case '-': v = -v; break;
710 default: return 0;
712 expr->fvalue = v;
713 expr->type = EXPR_FVALUE;
714 return 1;
718 * Unary post-ops: x++ and x--
720 static int expand_postop(struct expression *expr)
722 expand_expression(expr->unop);
723 return SIDE_EFFECTS;
726 static int expand_preop(struct expression *expr)
728 int cost;
730 switch (expr->op) {
731 case '*':
732 return expand_dereference(expr);
734 case '&':
735 return expand_addressof(expr);
737 case SPECIAL_INCREMENT:
738 case SPECIAL_DECREMENT:
740 * From a type evaluation standpoint the preops are
741 * the same as the postops
743 return expand_postop(expr);
745 default:
746 break;
748 cost = expand_expression(expr->unop);
750 if (simplify_preop(expr))
751 return 0;
752 if (simplify_float_preop(expr))
753 return 0;
754 return cost + 1;
757 static int expand_arguments(struct expression_list *head)
759 int cost = 0;
760 struct expression *expr;
762 FOR_EACH_PTR (head, expr) {
763 cost += expand_expression(expr);
764 } END_FOR_EACH_PTR(expr);
765 return cost;
768 static int expand_cast(struct expression *expr)
770 int cost;
771 struct expression *target = expr->cast_expression;
773 cost = expand_expression(target);
775 /* Simplify normal integer casts.. */
776 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
777 cast_value(expr, expr->ctype, target, target->ctype);
778 return 0;
780 return cost + 1;
784 * expand a call expression with a symbol. This
785 * should expand builtins.
787 static int expand_symbol_call(struct expression *expr, int cost)
789 struct expression *fn = expr->fn;
790 struct symbol *ctype = fn->ctype;
792 if (fn->type != EXPR_PREOP)
793 return SIDE_EFFECTS;
795 if (ctype->op && ctype->op->expand)
796 return ctype->op->expand(expr, cost);
798 if (ctype->ctype.modifiers & MOD_PURE)
799 return 0;
801 return SIDE_EFFECTS;
804 static int expand_call(struct expression *expr)
806 int cost;
807 struct symbol *sym;
808 struct expression *fn = expr->fn;
810 cost = expand_arguments(expr->args);
811 sym = fn->ctype;
812 if (!sym) {
813 expression_error(expr, "function has no type");
814 return SIDE_EFFECTS;
816 if (sym->type == SYM_NODE)
817 return expand_symbol_call(expr, cost);
819 return SIDE_EFFECTS;
822 static int expand_expression_list(struct expression_list *list)
824 int cost = 0;
825 struct expression *expr;
827 FOR_EACH_PTR(list, expr) {
828 cost += expand_expression(expr);
829 } END_FOR_EACH_PTR(expr);
830 return cost;
834 * We can simplify nested position expressions if
835 * this is a simple (single) positional expression.
837 static int expand_pos_expression(struct expression *expr)
839 struct expression *nested = expr->init_expr;
840 unsigned long offset = expr->init_offset;
841 int nr = expr->init_nr;
843 if (nr == 1) {
844 switch (nested->type) {
845 case EXPR_POS:
846 offset += nested->init_offset;
847 *expr = *nested;
848 expr->init_offset = offset;
849 nested = expr;
850 break;
852 case EXPR_INITIALIZER: {
853 struct expression *reuse = nested, *entry;
854 *expr = *nested;
855 FOR_EACH_PTR(expr->expr_list, entry) {
856 if (entry->type == EXPR_POS) {
857 entry->init_offset += offset;
858 } else {
859 if (!reuse) {
861 * This happens rarely, but it can happen
862 * with bitfields that are all at offset
863 * zero..
865 reuse = alloc_expression(entry->pos, EXPR_POS);
867 reuse->type = EXPR_POS;
868 reuse->ctype = entry->ctype;
869 reuse->init_offset = offset;
870 reuse->init_nr = 1;
871 reuse->init_expr = entry;
872 REPLACE_CURRENT_PTR(entry, reuse);
873 reuse = NULL;
875 } END_FOR_EACH_PTR(entry);
876 nested = expr;
877 break;
880 default:
881 break;
884 return expand_expression(nested);
887 static unsigned long bit_offset(const struct expression *expr)
889 unsigned long offset = 0;
890 while (expr->type == EXPR_POS) {
891 offset += bytes_to_bits(expr->init_offset);
892 expr = expr->init_expr;
894 if (expr && expr->ctype)
895 offset += expr->ctype->bit_offset;
896 return offset;
899 static int compare_expressions(const void *_a, const void *_b)
901 const struct expression *a = _a;
902 const struct expression *b = _b;
903 unsigned long a_pos = bit_offset(a);
904 unsigned long b_pos = bit_offset(b);
906 return (a_pos < b_pos) ? -1 : (a_pos == b_pos) ? 0 : 1;
909 static void sort_expression_list(struct expression_list **list)
911 sort_list((struct ptr_list **)list, compare_expressions);
914 static void verify_nonoverlapping(struct expression_list **list)
916 struct expression *a = NULL;
917 struct expression *b;
919 FOR_EACH_PTR(*list, b) {
920 if (!b->ctype || !b->ctype->bit_size)
921 continue;
922 if (a && bit_offset(a) == bit_offset(b)) {
923 warning(a->pos, "Initializer entry defined twice");
924 info(b->pos, " also defined here");
925 return;
927 a = b;
928 } END_FOR_EACH_PTR(b);
931 static int expand_expression(struct expression *expr)
933 if (!expr)
934 return 0;
935 if (!expr->ctype || expr->ctype == &bad_ctype)
936 return UNSAFE;
938 switch (expr->type) {
939 case EXPR_VALUE:
940 case EXPR_FVALUE:
941 case EXPR_STRING:
942 return 0;
943 case EXPR_TYPE:
944 case EXPR_SYMBOL:
945 return expand_symbol_expression(expr);
946 case EXPR_BINOP:
947 return expand_binop(expr);
949 case EXPR_LOGICAL:
950 return expand_logical(expr);
952 case EXPR_COMMA:
953 return expand_comma(expr);
955 case EXPR_COMPARE:
956 return expand_compare(expr);
958 case EXPR_ASSIGNMENT:
959 return expand_assignment(expr);
961 case EXPR_PREOP:
962 return expand_preop(expr);
964 case EXPR_POSTOP:
965 return expand_postop(expr);
967 case EXPR_CAST:
968 case EXPR_FORCE_CAST:
969 case EXPR_IMPLIED_CAST:
970 return expand_cast(expr);
972 case EXPR_CALL:
973 return expand_call(expr);
975 case EXPR_DEREF:
976 warning(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
977 return UNSAFE;
979 case EXPR_SELECT:
980 case EXPR_CONDITIONAL:
981 return expand_conditional(expr);
983 case EXPR_STATEMENT: {
984 struct statement *stmt = expr->statement;
985 int cost = expand_statement(stmt);
987 if (stmt->type == STMT_EXPRESSION && stmt->expression)
988 *expr = *stmt->expression;
989 return cost;
992 case EXPR_LABEL:
993 return 0;
995 case EXPR_INITIALIZER:
996 sort_expression_list(&expr->expr_list);
997 verify_nonoverlapping(&expr->expr_list);
998 return expand_expression_list(expr->expr_list);
1000 case EXPR_IDENTIFIER:
1001 return UNSAFE;
1003 case EXPR_INDEX:
1004 return UNSAFE;
1006 case EXPR_SLICE:
1007 return expand_expression(expr->base) + 1;
1009 case EXPR_POS:
1010 return expand_pos_expression(expr);
1012 case EXPR_SIZEOF:
1013 case EXPR_PTRSIZEOF:
1014 case EXPR_ALIGNOF:
1015 case EXPR_OFFSETOF:
1016 expression_error(expr, "internal front-end error: sizeof in expansion?");
1017 return UNSAFE;
1019 return SIDE_EFFECTS;
1022 static void expand_const_expression(struct expression *expr, const char *where)
1024 if (expr) {
1025 expand_expression(expr);
1026 if (expr->type != EXPR_VALUE)
1027 expression_error(expr, "Expected constant expression in %s", where);
1031 int expand_symbol(struct symbol *sym)
1033 int retval;
1034 struct symbol *base_type;
1036 if (!sym)
1037 return 0;
1038 base_type = sym->ctype.base_type;
1039 if (!base_type)
1040 return 0;
1042 retval = expand_expression(sym->initializer);
1043 /* expand the body of the symbol */
1044 if (base_type->type == SYM_FN) {
1045 if (base_type->stmt)
1046 expand_statement(base_type->stmt);
1048 return retval;
1051 static void expand_return_expression(struct statement *stmt)
1053 expand_expression(stmt->expression);
1056 static int expand_if_statement(struct statement *stmt)
1058 struct expression *expr = stmt->if_conditional;
1060 if (!expr || !expr->ctype || expr->ctype == &bad_ctype)
1061 return UNSAFE;
1063 expand_expression(expr);
1065 /* This is only valid if nobody jumps into the "dead" side */
1066 #if 0
1067 /* Simplify constant conditionals without even evaluating the false side */
1068 if (expr->type == EXPR_VALUE) {
1069 struct statement *simple;
1070 simple = expr->value ? stmt->if_true : stmt->if_false;
1072 /* Nothing? */
1073 if (!simple) {
1074 stmt->type = STMT_NONE;
1075 return 0;
1077 expand_statement(simple);
1078 *stmt = *simple;
1079 return SIDE_EFFECTS;
1081 #endif
1082 expand_statement(stmt->if_true);
1083 expand_statement(stmt->if_false);
1084 return SIDE_EFFECTS;
1088 * Expanding a compound statement is really just
1089 * about adding up the costs of each individual
1090 * statement.
1092 * We also collapse a simple compound statement:
1093 * this would trigger for simple inline functions,
1094 * except we would have to check the "return"
1095 * symbol usage. Next time.
1097 static int expand_compound(struct statement *stmt)
1099 struct statement *s, *last;
1100 int cost, statements;
1102 if (stmt->ret)
1103 expand_symbol(stmt->ret);
1105 last = stmt->args;
1106 cost = expand_statement(last);
1107 statements = last != NULL;
1108 FOR_EACH_PTR(stmt->stmts, s) {
1109 statements++;
1110 last = s;
1111 cost += expand_statement(s);
1112 } END_FOR_EACH_PTR(s);
1114 if (statements == 1 && !stmt->ret)
1115 *stmt = *last;
1117 return cost;
1120 static int expand_statement(struct statement *stmt)
1122 if (!stmt)
1123 return 0;
1125 switch (stmt->type) {
1126 case STMT_DECLARATION: {
1127 struct symbol *sym;
1128 FOR_EACH_PTR(stmt->declaration, sym) {
1129 expand_symbol(sym);
1130 } END_FOR_EACH_PTR(sym);
1131 return SIDE_EFFECTS;
1134 case STMT_RETURN:
1135 expand_return_expression(stmt);
1136 return SIDE_EFFECTS;
1138 case STMT_EXPRESSION:
1139 return expand_expression(stmt->expression);
1141 case STMT_COMPOUND:
1142 return expand_compound(stmt);
1144 case STMT_IF:
1145 return expand_if_statement(stmt);
1147 case STMT_ITERATOR:
1148 expand_expression(stmt->iterator_pre_condition);
1149 expand_expression(stmt->iterator_post_condition);
1150 expand_statement(stmt->iterator_pre_statement);
1151 expand_statement(stmt->iterator_statement);
1152 expand_statement(stmt->iterator_post_statement);
1153 return SIDE_EFFECTS;
1155 case STMT_SWITCH:
1156 expand_expression(stmt->switch_expression);
1157 expand_statement(stmt->switch_statement);
1158 return SIDE_EFFECTS;
1160 case STMT_CASE:
1161 expand_const_expression(stmt->case_expression, "case statement");
1162 expand_const_expression(stmt->case_to, "case statement");
1163 expand_statement(stmt->case_statement);
1164 return SIDE_EFFECTS;
1166 case STMT_LABEL:
1167 expand_statement(stmt->label_statement);
1168 return SIDE_EFFECTS;
1170 case STMT_GOTO:
1171 expand_expression(stmt->goto_expression);
1172 return SIDE_EFFECTS;
1174 case STMT_NONE:
1175 break;
1176 case STMT_ASM:
1177 /* FIXME! Do the asm parameter evaluation! */
1178 break;
1179 case STMT_CONTEXT:
1180 expand_expression(stmt->expression);
1181 break;
1182 case STMT_RANGE:
1183 expand_expression(stmt->range_expression);
1184 expand_expression(stmt->range_low);
1185 expand_expression(stmt->range_high);
1186 break;
1188 return SIDE_EFFECTS;
1191 static inline int bad_integer_constant_expression(struct expression *expr)
1193 if (!(expr->flags & Int_const_expr))
1194 return 1;
1195 if (expr->taint & Taint_comma)
1196 return 1;
1197 return 0;
1200 static long long __get_expression_value(struct expression *expr, int strict)
1202 long long value, mask;
1203 struct symbol *ctype;
1205 if (!expr)
1206 return 0;
1207 ctype = evaluate_expression(expr);
1208 if (!ctype) {
1209 expression_error(expr, "bad constant expression type");
1210 return 0;
1212 expand_expression(expr);
1213 if (expr->type != EXPR_VALUE) {
1214 if (strict != 2)
1215 expression_error(expr, "bad constant expression");
1216 return 0;
1218 if ((strict == 1) && bad_integer_constant_expression(expr)) {
1219 expression_error(expr, "bad integer constant expression");
1220 return 0;
1223 value = expr->value;
1224 mask = 1ULL << (ctype->bit_size-1);
1226 if (value & mask) {
1227 while (ctype->type != SYM_BASETYPE)
1228 ctype = ctype->ctype.base_type;
1229 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1230 value = value | mask | ~(mask-1);
1232 return value;
1235 long long get_expression_value(struct expression *expr)
1237 return __get_expression_value(expr, 0);
1240 long long const_expression_value(struct expression *expr)
1242 return __get_expression_value(expr, 1);
1245 long long get_expression_value_silent(struct expression *expr)
1248 return __get_expression_value(expr, 2);
1251 int is_zero_constant(struct expression *expr)
1253 const int saved = conservative;
1254 conservative = 1;
1255 expand_expression(expr);
1256 conservative = saved;
1257 return expr->type == EXPR_VALUE && !expr->value;