Make our "__builtin_va_arg()" thing a bit closer to real.
[smatch.git] / expand.c
blob36cae55607219a2e70db2b551bbe969fc25cb66f
1 /*
2 * sparse/expand.c
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 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 "parse.h"
23 #include "token.h"
24 #include "symbol.h"
25 #include "target.h"
26 #include "expression.h"
28 /* Random cost numbers */
29 #define SIDE_EFFECTS 10000 /* The expression has side effects */
30 #define UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
31 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
32 #define BRANCH_COST 10 /* Cost of a conditional branch */
34 static int expand_expression(struct expression *);
35 static int expand_statement(struct statement *);
37 static int expand_symbol_expression(struct expression *expr)
39 struct symbol *sym = expr->symbol;
40 /* The cost of a symbol expression is lower for on-stack symbols */
41 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
44 static long long get_longlong(struct expression *expr)
46 int no_expand = expr->ctype->ctype.modifiers & MOD_UNSIGNED;
47 long long mask = 1ULL << (expr->ctype->bit_size - 1);
48 long long value = expr->value;
49 long long ormask, andmask;
51 if (!(value & mask))
52 no_expand = 1;
53 andmask = mask | (mask-1);
54 ormask = ~andmask;
55 if (no_expand)
56 ormask = 0;
57 return (value & andmask) | ormask;
60 void cast_value(struct expression *expr, struct symbol *newtype,
61 struct expression *old, struct symbol *oldtype)
63 int old_size = oldtype->bit_size;
64 int new_size = newtype->bit_size;
65 long long value, mask, signmask;
66 long long oldmask, oldsignmask, dropped;
68 if (newtype->ctype.base_type == &fp_type ||
69 oldtype->ctype.base_type == &fp_type)
70 goto Float;
72 // For pointers and integers, we can just move the value around
73 expr->type = EXPR_VALUE;
74 if (old_size == new_size) {
75 expr->value = old->value;
76 return;
79 // expand it to the full "long long" value
80 value = get_longlong(old);
82 Int:
83 // Truncate it to the new size
84 signmask = 1ULL << (new_size-1);
85 mask = signmask | (signmask-1);
86 expr->value = value & mask;
88 // Check if we dropped any bits..
89 oldsignmask = 1ULL << (old_size-1);
90 oldmask = oldsignmask | (oldsignmask-1);
91 dropped = oldmask & ~mask;
93 // Ok if the bits were (and still are) purely sign bits
94 if (value & dropped) {
95 if (!(value & oldsignmask) || !(value & signmask) || (value & dropped) != dropped)
96 warning(old->pos, "cast truncates bits from constant value (%llx becomes %llx)",
97 value & oldmask,
98 value & mask);
100 return;
102 Float:
103 if (newtype->ctype.base_type != &fp_type) {
104 value = (long long)old->fvalue;
105 expr->type = EXPR_VALUE;
106 goto Int;
109 if (oldtype->ctype.base_type != &fp_type)
110 expr->fvalue = (long double)get_longlong(old);
111 else
112 expr->fvalue = old->value;
114 if (!(newtype->ctype.modifiers & MOD_LONGLONG)) {
115 if ((newtype->ctype.modifiers & MOD_LONG))
116 expr->fvalue = (double)expr->fvalue;
117 else
118 expr->fvalue = (float)expr->fvalue;
120 expr->type = EXPR_FVALUE;
123 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
125 if (count >= ctype->bit_size) {
126 warning(expr->pos, "shift too big (%u) for type %s", count, show_typename(ctype));
127 count &= ctype->bit_size-1;
129 return count;
133 * CAREFUL! We need to get the size and sign of the
134 * result right!
136 #define CONVERT(op,s) (((op)<<1)+(s))
137 #define SIGNED(op) CONVERT(op, 1)
138 #define UNSIGNED(op) CONVERT(op, 0)
139 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
141 struct expression *left = expr->left, *right = expr->right;
142 unsigned long long v, l, r, mask;
143 signed long long sl, sr;
144 int is_signed;
146 if (right->type != EXPR_VALUE)
147 return 0;
148 r = right->value;
149 if (expr->op == SPECIAL_LEFTSHIFT || expr->op == SPECIAL_RIGHTSHIFT) {
150 r = check_shift_count(expr, ctype, r);
151 right->value = r;
153 if (left->type != EXPR_VALUE)
154 return 0;
155 l = left->value; r = right->value;
156 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
157 mask = 1ULL << (ctype->bit_size-1);
158 sl = l; sr = r;
159 if (is_signed && (sl & mask))
160 sl |= ~(mask-1);
161 if (is_signed && (sr & mask))
162 sr |= ~(mask-1);
164 switch (CONVERT(expr->op,is_signed)) {
165 case SIGNED('+'):
166 case UNSIGNED('+'):
167 v = l + r;
168 break;
170 case SIGNED('-'):
171 case UNSIGNED('-'):
172 v = l - r;
173 break;
175 case SIGNED('&'):
176 case UNSIGNED('&'):
177 v = l & r;
178 break;
180 case SIGNED('|'):
181 case UNSIGNED('|'):
182 v = l | r;
183 break;
185 case SIGNED('^'):
186 case UNSIGNED('^'):
187 v = l ^ r;
188 break;
190 case SIGNED('*'):
191 v = sl * sr;
192 break;
194 case UNSIGNED('*'):
195 v = l * r;
196 break;
198 case SIGNED('/'):
199 if (!r)
200 goto Div;
201 if (l == mask && sr == -1)
202 goto Overflow;
203 v = sl / sr;
204 break;
206 case UNSIGNED('/'):
207 if (!r) goto Div;
208 v = l / r;
209 break;
211 case SIGNED('%'):
212 if (!r)
213 goto Div;
214 v = sl % sr;
215 break;
217 case UNSIGNED('%'):
218 if (!r) goto Div;
219 v = l % r;
220 break;
222 case SIGNED(SPECIAL_LEFTSHIFT):
223 case UNSIGNED(SPECIAL_LEFTSHIFT):
224 v = l << r;
225 break;
227 case SIGNED(SPECIAL_RIGHTSHIFT):
228 v = sl >> r;
229 break;
231 case UNSIGNED(SPECIAL_RIGHTSHIFT):
232 v = l >> r;
233 break;
235 default:
236 return 0;
238 mask = mask | (mask-1);
239 expr->value = v & mask;
240 expr->type = EXPR_VALUE;
241 return 1;
242 Div:
243 warning(expr->pos, "division by zero");
244 return 0;
245 Overflow:
246 warning(expr->pos, "constant integer operation overflow");
247 return 0;
250 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
252 struct expression *left = expr->left, *right = expr->right;
253 unsigned long long l, r, mask;
254 signed long long sl, sr;
256 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
257 return 0;
258 l = left->value; r = right->value;
259 mask = 1ULL << (ctype->bit_size-1);
260 sl = l; sr = r;
261 if (sl & mask)
262 sl |= ~(mask-1);
263 if (sr & mask)
264 sr |= ~(mask-1);
265 switch (expr->op) {
266 case '<': expr->value = sl < sr; break;
267 case '>': expr->value = sl > sr; break;
268 case SPECIAL_LTE: expr->value = sl <= sr; break;
269 case SPECIAL_GTE: expr->value = sl >= sr; break;
270 case SPECIAL_EQUAL: expr->value = l == r; break;
271 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
272 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
273 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
274 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
275 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
277 expr->type = EXPR_VALUE;
278 return 1;
281 static int simplify_float_binop(struct expression *expr)
283 struct expression *left = expr->left, *right = expr->right;
284 unsigned long mod = expr->ctype->ctype.modifiers;
285 long double l, r, res;
287 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
288 return 0;
290 l = left->fvalue;
291 r = right->fvalue;
293 if (mod & MOD_LONGLONG) {
294 switch (expr->op) {
295 case '+': res = l + r; break;
296 case '-': res = l - r; break;
297 case '*': res = l * r; break;
298 case '/': if (!r) goto Div;
299 res = l / r; break;
300 default: return 0;
302 } else if (mod & MOD_LONG) {
303 switch (expr->op) {
304 case '+': res = (double) l + (double) r; break;
305 case '-': res = (double) l - (double) r; break;
306 case '*': res = (double) l * (double) r; break;
307 case '/': if (!r) goto Div;
308 res = (double) l / (double) r; break;
309 default: return 0;
311 } else {
312 switch (expr->op) {
313 case '+': res = (float)l + (float)r; break;
314 case '-': res = (float)l - (float)r; break;
315 case '*': res = (float)l * (float)r; break;
316 case '/': if (!r) goto Div;
317 res = (float)l / (float)r; break;
318 default: return 0;
321 expr->type = EXPR_FVALUE;
322 expr->fvalue = res;
323 return 1;
324 Div:
325 warning(expr->pos, "division by zero");
326 return 0;
329 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
331 struct expression *left = expr->left, *right = expr->right;
332 long double l, r;
334 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
335 return 0;
337 l = left->fvalue;
338 r = right->fvalue;
339 switch (expr->op) {
340 case '<': expr->value = l < r; break;
341 case '>': expr->value = l > r; break;
342 case SPECIAL_LTE: expr->value = l <= r; break;
343 case SPECIAL_GTE: expr->value = l >= r; break;
344 case SPECIAL_EQUAL: expr->value = l == r; break;
345 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
347 expr->type = EXPR_VALUE;
348 return 1;
351 static int expand_binop(struct expression *expr)
353 int cost;
355 cost = expand_expression(expr->left);
356 cost += expand_expression(expr->right);
357 if (simplify_int_binop(expr, expr->ctype))
358 return 0;
359 if (simplify_float_binop(expr))
360 return 0;
361 return cost + 1;
364 static int expand_logical(struct expression *expr)
366 struct expression *left = expr->left;
367 struct expression *right;
368 int cost, rcost;
370 /* Do immediate short-circuiting ... */
371 cost = expand_expression(left);
372 if (left->type == EXPR_VALUE) {
373 if (expr->op == SPECIAL_LOGICAL_AND) {
374 if (!left->value) {
375 expr->type = EXPR_VALUE;
376 expr->value = 0;
377 return 0;
379 } else {
380 if (left->value) {
381 expr->type = EXPR_VALUE;
382 expr->value = 1;
383 return 0;
388 right = expr->right;
389 rcost = expand_expression(right);
390 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
392 * We know the left value doesn't matter, since
393 * otherwise we would have short-circuited it..
395 expr->type = EXPR_VALUE;
396 expr->value = right->value != 0;
397 return 0;
401 * If the right side is safe and cheaper than a branch,
402 * just avoid the branch and turn it into a regular binop
403 * style SAFELOGICAL.
405 if (rcost < BRANCH_COST) {
406 expr->type = EXPR_BINOP;
407 rcost -= BRANCH_COST - 1;
410 return cost + BRANCH_COST + rcost;
413 static int expand_comma(struct expression *expr)
415 int cost;
417 cost = expand_expression(expr->left);
418 cost += expand_expression(expr->right);
419 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE)
420 *expr = *expr->right;
421 return cost;
424 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
426 static int compare_types(int op, struct symbol *left, struct symbol *right)
428 switch (op) {
429 case SPECIAL_EQUAL:
430 return !type_difference(left, right, MOD_IGN, MOD_IGN);
431 case SPECIAL_NOTEQUAL:
432 return type_difference(left, right, MOD_IGN, MOD_IGN) != NULL;
433 case '<':
434 return left->bit_size < right->bit_size;
435 case '>':
436 return left->bit_size > right->bit_size;
437 case SPECIAL_LTE:
438 return left->bit_size <= right->bit_size;
439 case SPECIAL_GTE:
440 return left->bit_size >= right->bit_size;
442 return 0;
445 static int expand_compare(struct expression *expr)
447 struct expression *left = expr->left, *right = expr->right;
448 int cost;
450 cost = expand_expression(left);
451 cost += expand_expression(right);
453 /* Type comparison? */
454 if (left && right && left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
455 int op = expr->op;
456 expr->type = EXPR_VALUE;
457 expr->value = compare_types(op, left->symbol, right->symbol);
458 return 0;
460 if (simplify_cmp_binop(expr, left->ctype))
461 return 0;
462 if (simplify_float_cmp(expr, left->ctype))
463 return 0;
464 return cost + 1;
467 static int expand_conditional(struct expression *expr)
469 struct expression *cond = expr->conditional;
470 struct expression *true = expr->cond_true;
471 struct expression *false = expr->cond_false;
472 int cost, cond_cost;
474 cond_cost = expand_expression(cond);
475 if (cond->type == EXPR_VALUE) {
476 if (!cond->value)
477 true = false;
478 *expr = *true;
479 return expand_expression(expr);
482 cost = expand_expression(true);
483 cost += expand_expression(false);
485 if (cost < SELECT_COST) {
486 expr->type = EXPR_SELECT;
487 cost -= BRANCH_COST - 1;
490 return cost + cond_cost + BRANCH_COST;
493 static int expand_assignment(struct expression *expr)
495 expand_expression(expr->left);
496 expand_expression(expr->right);
497 return SIDE_EFFECTS;
500 static int expand_addressof(struct expression *expr)
502 return expand_expression(expr->unop);
505 static int expand_dereference(struct expression *expr)
507 struct expression *unop = expr->unop;
509 expand_expression(unop);
512 * NOTE! We get a bogus warning right now for some special
513 * cases: apparently I've screwed up the optimization of
514 * a zero-offset derefence, and the ctype is wrong.
516 * Leave the warning in anyway, since this is also a good
517 * test for me to get the type evaluation right..
519 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
520 warning(unop->pos, "dereference of noderef expression");
522 if (unop->type == EXPR_SYMBOL) {
523 struct symbol *sym = unop->symbol;
525 /* Const symbol with a constant initializer? */
526 if (!(sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))) {
527 struct expression *value = sym->initializer;
528 if (value) {
529 if (value->type == EXPR_VALUE) {
530 expr->type = EXPR_VALUE;
531 expr->value = value->value;
532 return 0;
533 } else if (value->type == EXPR_FVALUE) {
534 expr->type = EXPR_FVALUE;
535 expr->fvalue = value->fvalue;
536 return 0;
541 /* Direct symbol dereference? Cheap and safe */
542 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
544 return UNSAFE;
547 static int simplify_preop(struct expression *expr)
549 struct expression *op = expr->unop;
550 unsigned long long v, mask;
552 if (op->type != EXPR_VALUE)
553 return 0;
555 mask = 1ULL << (expr->ctype->bit_size-1);
556 v = op->value;
557 switch (expr->op) {
558 case '+': break;
559 case '-':
560 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
561 goto Overflow;
562 v = -v;
563 break;
564 case '!': v = !v; break;
565 case '~': v = ~v; break;
566 default: return 0;
568 mask = mask | (mask-1);
569 expr->value = v & mask;
570 expr->type = EXPR_VALUE;
571 return 1;
573 Overflow:
574 warning(expr->pos, "constant integer operation overflow");
575 return 0;
578 static int simplify_float_preop(struct expression *expr)
580 struct expression *op = expr->unop;
581 long double v;
583 if (op->type != EXPR_FVALUE)
584 return 0;
585 v = op->fvalue;
586 switch (expr->op) {
587 case '+': break;
588 case '-': v = -v; break;
589 default: return 0;
591 expr->fvalue = v;
592 expr->type = EXPR_FVALUE;
593 return 1;
597 * Unary post-ops: x++ and x--
599 static int expand_postop(struct expression *expr)
601 expand_expression(expr->unop);
602 return SIDE_EFFECTS;
605 static int expand_preop(struct expression *expr)
607 int cost;
609 switch (expr->op) {
610 case '*':
611 return expand_dereference(expr);
613 case '&':
614 return expand_addressof(expr);
616 case SPECIAL_INCREMENT:
617 case SPECIAL_DECREMENT:
619 * From a type evaluation standpoint the pre-ops are
620 * the same as the postops
622 return expand_postop(expr);
624 default:
625 break;
627 cost = expand_expression(expr->unop);
629 if (simplify_preop(expr))
630 return 0;
631 if (simplify_float_preop(expr))
632 return 0;
633 return cost + 1;
636 static int expand_arguments(struct expression_list *head)
638 int cost = 0;
639 struct expression *expr;
641 FOR_EACH_PTR (head, expr) {
642 cost += expand_expression(expr);
643 } END_FOR_EACH_PTR(expr);
644 return cost;
647 static int expand_cast(struct expression *expr)
649 int cost;
650 struct expression *target = expr->cast_expression;
652 cost = expand_expression(target);
654 /* Simplify normal integer casts.. */
655 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
656 cast_value(expr, expr->ctype, target, target->ctype);
657 return 0;
659 return cost + 1;
662 /* The arguments are constant if the cost of all of them is zero */
663 int expand_constant_p(struct expression *expr, int cost)
665 expr->type = EXPR_VALUE;
666 expr->value = !cost;
667 return 0;
670 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
671 int expand_safe_p(struct expression *expr, int cost)
673 expr->type = EXPR_VALUE;
674 expr->value = (cost < SIDE_EFFECTS);
675 return 0;
679 * expand a call expression with a symbol. This
680 * should expand builtins.
682 static int expand_symbol_call(struct expression *expr, int cost)
684 struct expression *fn = expr->fn;
685 struct symbol *ctype = fn->ctype;
687 if (fn->type != EXPR_PREOP)
688 return SIDE_EFFECTS;
690 if (ctype->op && ctype->op->expand)
691 return ctype->op->expand(expr, cost);
693 return SIDE_EFFECTS;
696 static int expand_call(struct expression *expr)
698 int cost;
699 struct symbol *sym;
700 struct expression *fn = expr->fn;
702 cost = expand_arguments(expr->args);
703 sym = fn->ctype;
704 if (!sym) {
705 error(expr->pos, "function has no type");
706 return SIDE_EFFECTS;
708 if (sym->type == SYM_NODE)
709 return expand_symbol_call(expr, cost);
711 return SIDE_EFFECTS;
714 static int expand_expression_list(struct expression_list *list)
716 int cost = 0;
717 struct expression *expr;
719 FOR_EACH_PTR(list, expr) {
720 cost += expand_expression(expr);
721 } END_FOR_EACH_PTR(expr);
722 return cost;
726 * We can simplify nested position expressions if
727 * this is a simple (single) positional expression.
729 static int expand_pos_expression(struct expression *expr)
731 struct expression *nested = expr->init_expr;
732 unsigned long offset = expr->init_offset;
733 int nr = expr->init_nr;
735 if (nr == 1) {
736 switch (nested->type) {
737 case EXPR_POS:
738 offset += nested->init_offset;
739 *expr = *nested;
740 expr->init_offset = offset;
741 nested = expr;
742 break;
744 case EXPR_INITIALIZER: {
745 struct expression *reuse = nested, *entry;
746 *expr = *nested;
747 FOR_EACH_PTR(expr->expr_list, entry) {
748 if (entry->type == EXPR_POS) {
749 entry->init_offset += offset;
750 } else {
751 if (!reuse) {
753 * This happens rarely, but it can happen
754 * with bitfields that are all at offset
755 * zero..
757 reuse = alloc_expression(entry->pos, EXPR_POS);
759 reuse->type = EXPR_POS;
760 reuse->ctype = entry->ctype;
761 reuse->init_offset = offset;
762 reuse->init_nr = 1;
763 reuse->init_expr = entry;
764 REPLACE_CURRENT_PTR(entry, reuse);
765 reuse = NULL;
767 } END_FOR_EACH_PTR(entry);
768 nested = expr;
769 break;
772 default:
773 break;
776 return expand_expression(nested);
779 static int compare_expressions(const void *_a, const void *_b)
781 const struct expression *a = _a;
782 const struct expression *b = _b;
783 int r;
785 r = (b->type != EXPR_POS) - (a->type != EXPR_POS);
786 if (r) return r;
788 if (a->init_offset < b->init_offset)
789 return -1;
790 if (a->init_offset > b->init_offset)
791 return +1;
792 /* Check bitfield offset.. */
793 a = a->init_expr;
794 b = b->init_expr;
795 if (a && b) {
796 if (a->ctype && b->ctype) {
797 if (a->ctype->bit_offset < b->ctype->bit_offset)
798 return -1;
799 return +1;
802 return 0;
805 static void sort_expression_list(struct expression_list **list)
807 sort_list((struct ptr_list **)list, compare_expressions);
810 static int expand_expression(struct expression *expr)
812 if (!expr)
813 return 0;
814 if (!expr->ctype)
815 return UNSAFE;
817 switch (expr->type) {
818 case EXPR_VALUE:
819 case EXPR_FVALUE:
820 case EXPR_STRING:
821 return 0;
822 case EXPR_TYPE:
823 case EXPR_SYMBOL:
824 return expand_symbol_expression(expr);
825 case EXPR_BINOP:
826 return expand_binop(expr);
828 case EXPR_LOGICAL:
829 return expand_logical(expr);
831 case EXPR_COMMA:
832 return expand_comma(expr);
834 case EXPR_COMPARE:
835 return expand_compare(expr);
837 case EXPR_ASSIGNMENT:
838 return expand_assignment(expr);
840 case EXPR_PREOP:
841 return expand_preop(expr);
843 case EXPR_POSTOP:
844 return expand_postop(expr);
846 case EXPR_CAST:
847 case EXPR_IMPLIED_CAST:
848 return expand_cast(expr);
850 case EXPR_CALL:
851 return expand_call(expr);
853 case EXPR_DEREF:
854 warning(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
855 return UNSAFE;
857 case EXPR_SELECT:
858 case EXPR_CONDITIONAL:
859 return expand_conditional(expr);
861 case EXPR_STATEMENT: {
862 struct statement *stmt = expr->statement;
863 int cost = expand_statement(stmt);
865 if (stmt->type == STMT_EXPRESSION)
866 *expr = *stmt->expression;
867 return cost;
870 case EXPR_LABEL:
871 return 0;
873 case EXPR_INITIALIZER:
874 sort_expression_list(&expr->expr_list);
875 return expand_expression_list(expr->expr_list);
877 case EXPR_IDENTIFIER:
878 return UNSAFE;
880 case EXPR_INDEX:
881 return UNSAFE;
883 case EXPR_SLICE:
884 return expand_expression(expr->base) + 1;
886 case EXPR_POS:
887 return expand_pos_expression(expr);
889 case EXPR_SIZEOF:
890 case EXPR_PTRSIZEOF:
891 case EXPR_ALIGNOF:
892 warning(expr->pos, "internal front-end error: sizeof in expansion?");
893 return UNSAFE;
895 return SIDE_EFFECTS;
898 static void expand_const_expression(struct expression *expr, const char *where)
900 if (expr) {
901 expand_expression(expr);
902 if (expr->type != EXPR_VALUE)
903 warning(expr->pos, "Expected constant expression in %s", where);
907 void expand_symbol(struct symbol *sym)
909 struct symbol *base_type;
911 if (!sym)
912 return;
913 base_type = sym->ctype.base_type;
914 if (!base_type)
915 return;
917 expand_expression(sym->initializer);
918 /* expand the body of the symbol */
919 if (base_type->type == SYM_FN) {
920 if (base_type->stmt)
921 expand_statement(base_type->stmt);
925 static void expand_return_expression(struct statement *stmt)
927 expand_expression(stmt->expression);
930 static int expand_if_statement(struct statement *stmt)
932 struct expression *expr = stmt->if_conditional;
934 if (!expr || !expr->ctype)
935 return UNSAFE;
937 expand_expression(expr);
939 /* This is only valid if nobody jumps into the "dead" side */
940 #if 0
941 /* Simplify constant conditionals without even evaluating the false side */
942 if (expr->type == EXPR_VALUE) {
943 struct statement *simple;
944 simple = expr->value ? stmt->if_true : stmt->if_false;
946 /* Nothing? */
947 if (!simple) {
948 stmt->type = STMT_NONE;
949 return 0;
951 expand_statement(simple);
952 *stmt = *simple;
953 return SIDE_EFFECTS;
955 #endif
956 expand_statement(stmt->if_true);
957 expand_statement(stmt->if_false);
958 return SIDE_EFFECTS;
962 * Expanding a compound statement is really just
963 * about adding up the costs of each individual
964 * statement.
966 * We also collapse a simple compound statement:
967 * this would trigger for simple inline functions,
968 * except we would have to check the "return"
969 * symbol usage. Next time.
971 static int expand_compound(struct statement *stmt)
973 struct symbol *sym;
974 struct statement *s, *last;
975 int cost, symbols, statements;
977 symbols = 0;
978 FOR_EACH_PTR(stmt->syms, sym) {
979 symbols++;
980 expand_symbol(sym);
981 } END_FOR_EACH_PTR(sym);
983 if (stmt->ret) {
984 symbols++;
985 expand_symbol(stmt->ret);
988 cost = 0;
989 last = NULL;
990 statements = 0;
991 FOR_EACH_PTR(stmt->stmts, s) {
992 statements++;
993 last = s;
994 cost += expand_statement(s);
995 } END_FOR_EACH_PTR(s);
997 if (!symbols && statements == 1)
998 *stmt = *last;
1000 return cost;
1003 static int expand_statement(struct statement *stmt)
1005 if (!stmt)
1006 return 0;
1008 switch (stmt->type) {
1009 case STMT_RETURN:
1010 expand_return_expression(stmt);
1011 return SIDE_EFFECTS;
1013 case STMT_EXPRESSION:
1014 return expand_expression(stmt->expression);
1016 case STMT_COMPOUND:
1017 return expand_compound(stmt);
1019 case STMT_IF:
1020 return expand_if_statement(stmt);
1022 case STMT_ITERATOR:
1023 expand_expression(stmt->iterator_pre_condition);
1024 expand_expression(stmt->iterator_post_condition);
1025 expand_statement(stmt->iterator_pre_statement);
1026 expand_statement(stmt->iterator_statement);
1027 expand_statement(stmt->iterator_post_statement);
1028 return SIDE_EFFECTS;
1030 case STMT_SWITCH:
1031 expand_expression(stmt->switch_expression);
1032 expand_statement(stmt->switch_statement);
1033 return SIDE_EFFECTS;
1035 case STMT_CASE:
1036 expand_const_expression(stmt->case_expression, "case statement");
1037 expand_const_expression(stmt->case_to, "case statement");
1038 expand_statement(stmt->case_statement);
1039 return SIDE_EFFECTS;
1041 case STMT_LABEL:
1042 expand_statement(stmt->label_statement);
1043 return SIDE_EFFECTS;
1045 case STMT_GOTO:
1046 expand_expression(stmt->goto_expression);
1047 return SIDE_EFFECTS;
1049 case STMT_NONE:
1050 break;
1051 case STMT_ASM:
1052 /* FIXME! Do the asm parameter evaluation! */
1053 break;
1054 case STMT_INTERNAL:
1055 expand_expression(stmt->expression);
1056 break;
1058 return SIDE_EFFECTS;
1061 long long get_expression_value(struct expression *expr)
1063 long long value, mask;
1064 struct symbol *ctype;
1066 if (!expr)
1067 return 0;
1068 ctype = evaluate_expression(expr);
1069 if (!ctype) {
1070 warning(expr->pos, "bad constant expression type");
1071 return 0;
1073 expand_expression(expr);
1074 if (expr->type != EXPR_VALUE) {
1075 warning(expr->pos, "bad constant expression");
1076 return 0;
1079 value = expr->value;
1080 mask = 1ULL << (ctype->bit_size-1);
1082 if (value & mask) {
1083 while (ctype->type != SYM_BASETYPE)
1084 ctype = ctype->ctype.base_type;
1085 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
1086 value = value | mask | ~(mask-1);
1088 return value;