[PATCH] evaluate_sign() typo
[smatch.git] / expand.c
blobf07ad5ff920def211959ece1c4e92cd064cbfe04
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;
42 * The preprocessor can cause unknown symbols to be generated
44 if (!sym) {
45 warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
46 expr->type = EXPR_VALUE;
47 expr->value = 0;
48 return UNSAFE;
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;
78 if (newtype->ctype.base_type == &fp_type ||
79 oldtype->ctype.base_type == &fp_type)
80 goto Float;
82 // For pointers and integers, we can just move the value around
83 expr->type = EXPR_VALUE;
84 if (old_size == new_size) {
85 expr->value = old->value;
86 return;
89 // expand it to the full "long long" value
90 value = get_longlong(old);
92 Int:
93 // Truncate it to the new size
94 mask = 1ULL << (new_size-1);
95 mask = mask | (mask-1);
96 expr->value = value & mask;
97 return;
99 Float:
100 if (newtype->ctype.base_type != &fp_type) {
101 value = (long long)old->fvalue;
102 expr->type = EXPR_VALUE;
103 goto Int;
106 if (oldtype->ctype.base_type != &fp_type)
107 expr->fvalue = (long double)get_longlong(old);
108 else
109 expr->fvalue = old->value;
111 if (!(newtype->ctype.modifiers & MOD_LONGLONG)) {
112 if ((newtype->ctype.modifiers & MOD_LONG))
113 expr->fvalue = (double)expr->fvalue;
114 else
115 expr->fvalue = (float)expr->fvalue;
117 expr->type = EXPR_FVALUE;
120 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
122 if (count >= ctype->bit_size) {
123 warn(expr->pos, "shift too big for type (%x)", ctype->ctype.modifiers);
124 count &= ctype->bit_size-1;
126 return count;
130 * CAREFUL! We need to get the size and sign of the
131 * result right!
133 #define CONVERT(op,s) (((op)<<1)+(s))
134 #define SIGNED(op) CONVERT(op, 1)
135 #define UNSIGNED(op) CONVERT(op, 0)
136 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
138 struct expression *left = expr->left, *right = expr->right;
139 unsigned long long v, l, r, mask;
140 signed long long sl, sr;
141 int is_signed, shift;
143 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
144 return 0;
145 l = left->value; r = right->value;
146 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
147 mask = 1ULL << (ctype->bit_size-1);
148 sl = l; sr = r;
149 if (is_signed && (sl & mask))
150 sl |= ~(mask-1);
151 if (is_signed && (sr & mask))
152 sr |= ~(mask-1);
154 switch (CONVERT(expr->op,is_signed)) {
155 case SIGNED('+'):
156 case UNSIGNED('+'):
157 v = l + r;
158 break;
160 case SIGNED('-'):
161 case UNSIGNED('-'):
162 v = l - r;
163 break;
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 v = sl * sr;
182 break;
184 case UNSIGNED('*'):
185 v = l * r;
186 break;
188 case SIGNED('/'):
189 if (!r)
190 goto Div;
191 if (l == mask && sr == -1)
192 goto Overflow;
193 v = sl / sr;
194 break;
196 case UNSIGNED('/'):
197 if (!r) goto Div;
198 v = l / r;
199 break;
201 case SIGNED('%'):
202 if (!r)
203 goto Div;
204 v = sl % sr;
205 break;
207 case UNSIGNED('%'):
208 if (!r) goto Div;
209 v = l % r;
210 break;
212 case SIGNED(SPECIAL_LEFTSHIFT):
213 case UNSIGNED(SPECIAL_LEFTSHIFT):
214 shift = check_shift_count(expr, ctype, r);
215 v = l << shift;
216 break;
218 case SIGNED(SPECIAL_RIGHTSHIFT):
219 shift = check_shift_count(expr, ctype, r);
220 v = sl >> shift;
221 break;
223 case UNSIGNED(SPECIAL_RIGHTSHIFT):
224 shift = check_shift_count(expr, ctype, r);
225 v = l >> shift;
226 break;
228 default:
229 return 0;
231 mask = mask | (mask-1);
232 expr->value = v & mask;
233 expr->type = EXPR_VALUE;
234 return 1;
235 Div:
236 warn(expr->pos, "division by zero");
237 return 0;
238 Overflow:
239 warn(expr->pos, "constant integer operation overflow");
240 return 0;
243 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
245 struct expression *left = expr->left, *right = expr->right;
246 unsigned long long l, r, mask;
247 signed long long sl, sr;
249 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
250 return 0;
251 l = left->value; r = right->value;
252 mask = 1ULL << (ctype->bit_size-1);
253 sl = l; sr = r;
254 if (sl & mask)
255 sl |= ~(mask-1);
256 if (sr & mask)
257 sr |= ~(mask-1);
258 switch (expr->op) {
259 case '<': expr->value = sl < sr; break;
260 case '>': expr->value = sl > sr; break;
261 case SPECIAL_LTE: expr->value = sl <= sr; break;
262 case SPECIAL_GTE: expr->value = sl >= sr; break;
263 case SPECIAL_EQUAL: expr->value = l == r; break;
264 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
265 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
266 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
267 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
268 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
270 expr->type = EXPR_VALUE;
271 return 1;
274 static int simplify_float_binop(struct expression *expr)
276 struct expression *left = expr->left, *right = expr->right;
277 unsigned long mod = expr->ctype->ctype.modifiers;
278 long double l, r, res;
280 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
281 return 0;
283 l = left->fvalue;
284 r = right->fvalue;
286 if (mod & MOD_LONGLONG) {
287 switch (expr->op) {
288 case '+': res = l + r; break;
289 case '-': res = l - r; break;
290 case '*': res = l * r; break;
291 case '/': if (!r) goto Div;
292 res = l / r; break;
293 default: return 0;
295 } else if (mod & MOD_LONG) {
296 switch (expr->op) {
297 case '+': res = (double) l + (double) r; break;
298 case '-': res = (double) l - (double) r; break;
299 case '*': res = (double) l * (double) r; break;
300 case '/': if (!r) goto Div;
301 res = (double) l / (double) r; break;
302 default: return 0;
304 } else {
305 switch (expr->op) {
306 case '+': res = (float)l + (float)r; break;
307 case '-': res = (float)l - (float)r; break;
308 case '*': res = (float)l * (float)r; break;
309 case '/': if (!r) goto Div;
310 res = (float)l / (float)r; break;
311 default: return 0;
314 expr->type = EXPR_FVALUE;
315 expr->fvalue = res;
316 return 1;
317 Div:
318 warn(expr->pos, "division by zero");
319 return 0;
322 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
324 struct expression *left = expr->left, *right = expr->right;
325 long double l, r;
327 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
328 return 0;
330 l = left->fvalue;
331 r = right->fvalue;
332 switch (expr->op) {
333 case '<': expr->value = l < r; break;
334 case '>': expr->value = l > r; break;
335 case SPECIAL_LTE: expr->value = l <= r; break;
336 case SPECIAL_GTE: expr->value = l >= r; break;
337 case SPECIAL_EQUAL: expr->value = l == r; break;
338 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
340 expr->type = EXPR_VALUE;
341 return 1;
344 static int expand_binop(struct expression *expr)
346 int cost;
348 cost = expand_expression(expr->left);
349 cost += expand_expression(expr->right);
350 if (simplify_int_binop(expr, expr->ctype))
351 return 1;
352 if (simplify_float_binop(expr))
353 return 1;
354 return cost + 1;
357 static int expand_logical(struct expression *expr)
359 struct expression *left = expr->left;
360 struct expression *right;
361 int cost, rcost;
363 /* Do immediate short-circuiting ... */
364 cost = expand_expression(left);
365 if (left->type == EXPR_VALUE) {
366 if (expr->op == SPECIAL_LOGICAL_AND) {
367 if (!left->value) {
368 expr->type = EXPR_VALUE;
369 expr->value = 0;
370 return 0;
372 } else {
373 if (left->value) {
374 expr->type = EXPR_VALUE;
375 expr->value = 1;
376 return 0;
381 right = expr->right;
382 rcost = expand_expression(right);
383 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
385 * We know the left value doesn't matter, since
386 * otherwise we would have short-circuited it..
388 expr->type = EXPR_VALUE;
389 expr->value = right->value != 0;
390 return 0;
394 * If the right side is safe and cheaper than a branch,
395 * just avoid the branch and turn it into a regular binop
396 * style SAFELOGICAL.
398 if (rcost < BRANCH_COST) {
399 expr->type = EXPR_BINOP;
400 rcost -= BRANCH_COST - 1;
403 return cost + BRANCH_COST + rcost;
406 static int expand_comma(struct expression *expr)
408 int cost;
410 cost = expand_expression(expr->left);
411 cost += expand_expression(expr->right);
412 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE)
413 *expr = *expr->right;
414 return cost;
417 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
419 static int compare_types(int op, struct symbol *left, struct symbol *right)
421 switch (op) {
422 case SPECIAL_EQUAL:
423 return !type_difference(left, right, MOD_IGN, MOD_IGN);
424 case SPECIAL_NOTEQUAL:
425 return type_difference(left, right, MOD_IGN, MOD_IGN) != NULL;
426 case '<':
427 return left->bit_size < right->bit_size;
428 case '>':
429 return left->bit_size > right->bit_size;
430 case SPECIAL_LTE:
431 return left->bit_size <= right->bit_size;
432 case SPECIAL_GTE:
433 return left->bit_size >= right->bit_size;
435 return 0;
438 static int expand_compare(struct expression *expr)
440 struct expression *left = expr->left, *right = expr->right;
441 int cost;
443 cost = expand_expression(left);
444 cost += expand_expression(right);
446 /* Type comparison? */
447 if (left && right && left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
448 int op = expr->op;
449 expr->type = EXPR_VALUE;
450 expr->value = compare_types(op, left->symbol, right->symbol);
451 return 0;
453 if (simplify_cmp_binop(expr, left->ctype))
454 return 0;
455 if (simplify_float_cmp(expr, left->ctype))
456 return 0;
457 return cost + 1;
460 static int expand_conditional(struct expression *expr)
462 struct expression *cond = expr->conditional;
463 struct expression *true = expr->cond_true;
464 struct expression *false = expr->cond_false;
465 int cost, cond_cost;
467 cond_cost = expand_expression(cond);
468 if (cond->type == EXPR_VALUE) {
469 true = true ? : cond;
470 if (!cond->value)
471 true = false;
472 *expr = *true;
473 return expand_expression(expr);
476 cost = expand_expression(true);
477 cost += expand_expression(false);
479 if (cost < SELECT_COST) {
480 expr->type = EXPR_SELECT;
481 cost -= BRANCH_COST - 1;
484 return cost + cond_cost + BRANCH_COST;
487 static int expand_assignment(struct expression *expr)
489 expand_expression(expr->left);
490 expand_expression(expr->right);
491 return SIDE_EFFECTS;
494 static int expand_addressof(struct expression *expr)
496 return expand_expression(expr->unop);
499 static int expand_dereference(struct expression *expr)
501 struct expression *unop = expr->unop;
503 expand_expression(unop);
506 * NOTE! We get a bogus warning right now for some special
507 * cases: apparently I've screwed up the optimization of
508 * a zero-offset derefence, and the ctype is wrong.
510 * Leave the warning in anyway, since this is also a good
511 * test for me to get the type evaluation right..
513 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
514 warn(unop->pos, "dereference of noderef expression");
516 if (unop->type == EXPR_SYMBOL) {
517 struct symbol *sym = unop->symbol;
519 /* Const symbol with a constant initializer? */
520 if (!(sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))) {
521 struct expression *value = sym->initializer;
522 if (value) {
523 if (value->type == EXPR_VALUE) {
524 expr->type = EXPR_VALUE;
525 expr->value = value->value;
526 return 0;
527 } else if (value->type == EXPR_FVALUE) {
528 expr->type = EXPR_FVALUE;
529 expr->fvalue = value->fvalue;
530 return 0;
535 /* Direct symbol dereference? Cheap and safe */
536 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
538 return UNSAFE;
541 static int simplify_preop(struct expression *expr)
543 struct expression *op = expr->unop;
544 unsigned long long v, mask;
546 if (op->type != EXPR_VALUE)
547 return 0;
549 mask = 1ULL << (expr->ctype->bit_size-1);
550 v = op->value;
551 switch (expr->op) {
552 case '+': break;
553 case '-':
554 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
555 goto Overflow;
556 v = -v;
557 break;
558 case '!': v = !v; break;
559 case '~': v = ~v; break;
560 default: return 0;
562 mask = mask | (mask-1);
563 expr->value = v & mask;
564 expr->type = EXPR_VALUE;
565 return 1;
567 Overflow:
568 warn(expr->pos, "constant integer operation overflow");
569 return 0;
572 static int simplify_float_preop(struct expression *expr)
574 struct expression *op = expr->unop;
575 long double v;
577 if (op->type != EXPR_FVALUE)
578 return 0;
579 v = op->fvalue;
580 switch (expr->op) {
581 case '+': break;
582 case '-': v = -v; break;
583 default: return 0;
585 expr->fvalue = v;
586 expr->type = EXPR_FVALUE;
587 return 1;
591 * Unary post-ops: x++ and x--
593 static int expand_postop(struct expression *expr)
595 expand_expression(expr->unop);
596 return SIDE_EFFECTS;
599 static int expand_preop(struct expression *expr)
601 int cost;
603 switch (expr->op) {
604 case '*':
605 return expand_dereference(expr);
607 case '&':
608 return expand_addressof(expr);
610 case SPECIAL_INCREMENT:
611 case SPECIAL_DECREMENT:
613 * From a type evaluation standpoint the pre-ops are
614 * the same as the postops
616 return expand_postop(expr);
618 default:
619 break;
621 cost = expand_expression(expr->unop);
623 if (simplify_preop(expr))
624 return 0;
625 if (simplify_float_preop(expr))
626 return 0;
627 return cost + 1;
630 static int expand_arguments(struct expression_list *head)
632 int cost = 0;
633 struct expression *expr;
635 FOR_EACH_PTR (head, expr) {
636 cost += expand_expression(expr);
637 } END_FOR_EACH_PTR;
638 return cost;
641 static int expand_cast(struct expression *expr)
643 int cost;
644 struct expression *target = expr->cast_expression;
646 cost = expand_expression(target);
648 /* Simplify normal integer casts.. */
649 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
650 cast_value(expr, expr->ctype, target, target->ctype);
651 return 0;
653 return cost + 1;
657 * expand a call expression with a symbol. This
658 * should expand builtins.
660 static int expand_symbol_call(struct expression *expr)
662 struct expression *fn = expr->fn;
663 struct symbol *ctype = fn->ctype;
665 if (fn->type != EXPR_PREOP)
666 return SIDE_EFFECTS;
668 if (ctype->op && ctype->op->expand)
669 return ctype->op->expand(expr);
671 return SIDE_EFFECTS;
674 static int expand_call(struct expression *expr)
676 int cost;
677 struct symbol *sym;
678 struct expression *fn = expr->fn;
680 cost = expand_arguments(expr->args);
681 sym = fn->ctype;
682 if (sym->type == SYM_NODE)
683 return expand_symbol_call(expr);
685 return SIDE_EFFECTS;
688 static int expand_expression_list(struct expression_list *list)
690 int cost = 0;
691 struct expression *expr;
693 FOR_EACH_PTR(list, expr) {
694 cost += expand_expression(expr);
695 } END_FOR_EACH_PTR;
696 return cost;
699 static int expand_expression(struct expression *expr)
701 if (!expr)
702 return 0;
703 if (!expr->ctype)
704 return UNSAFE;
706 switch (expr->type) {
707 case EXPR_VALUE:
708 case EXPR_FVALUE:
709 case EXPR_STRING:
710 return 0;
711 case EXPR_TYPE:
712 case EXPR_SYMBOL:
713 return expand_symbol_expression(expr);
714 case EXPR_BINOP:
715 return expand_binop(expr);
717 case EXPR_LOGICAL:
718 return expand_logical(expr);
720 case EXPR_COMMA:
721 return expand_comma(expr);
723 case EXPR_COMPARE:
724 return expand_compare(expr);
726 case EXPR_ASSIGNMENT:
727 return expand_assignment(expr);
729 case EXPR_PREOP:
730 return expand_preop(expr);
732 case EXPR_POSTOP:
733 return expand_postop(expr);
735 case EXPR_CAST:
736 return expand_cast(expr);
738 case EXPR_CALL:
739 return expand_call(expr);
741 case EXPR_DEREF:
742 warn(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
743 return UNSAFE;
745 case EXPR_BITFIELD:
746 return expand_expression(expr->address);
748 case EXPR_SELECT:
749 case EXPR_CONDITIONAL:
750 return expand_conditional(expr);
752 case EXPR_STATEMENT:
753 return expand_statement(expr->statement);
755 case EXPR_LABEL:
756 return 0;
758 case EXPR_INITIALIZER:
759 return expand_expression_list(expr->expr_list);
761 case EXPR_IDENTIFIER:
762 return UNSAFE;
764 case EXPR_INDEX:
765 return UNSAFE;
767 case EXPR_SLICE:
768 return expand_expression(expr->base) + 1;
770 case EXPR_POS:
771 return expand_expression(expr->init_expr);
773 case EXPR_SIZEOF:
774 case EXPR_ALIGNOF:
775 warn(expr->pos, "internal front-end error: sizeof in expansion?");
776 return UNSAFE;
778 return SIDE_EFFECTS;
781 static void expand_const_expression(struct expression *expr, const char *where)
783 if (expr) {
784 expand_expression(expr);
785 if (expr->type != EXPR_VALUE)
786 warn(expr->pos, "Expected constant expression in %s", where);
790 void expand_symbol(struct symbol *sym)
792 struct symbol *base_type;
794 if (!sym)
795 return;
796 base_type = sym->ctype.base_type;
797 if (!base_type)
798 return;
800 expand_expression(sym->initializer);
801 /* expand the body of the symbol */
802 if (base_type->type == SYM_FN) {
803 if (base_type->stmt)
804 expand_statement(base_type->stmt);
808 static void expand_return_expression(struct statement *stmt)
810 expand_expression(stmt->expression);
813 static int expand_if_statement(struct statement *stmt)
815 struct expression *expr = stmt->if_conditional;
817 if (!expr || !expr->ctype)
818 return UNSAFE;
820 expand_expression(expr);
822 /* This is only valid if nobody jumps into the "dead" side */
823 #if 0
824 /* Simplify constant conditionals without even evaluating the false side */
825 if (expr->type == EXPR_VALUE) {
826 struct statement *simple;
827 simple = expr->value ? stmt->if_true : stmt->if_false;
829 /* Nothing? */
830 if (!simple) {
831 stmt->type = STMT_NONE;
832 return 0;
834 expand_statement(simple);
835 *stmt = *simple;
836 return SIDE_EFFECTS;
838 #endif
839 expand_statement(stmt->if_true);
840 expand_statement(stmt->if_false);
841 return SIDE_EFFECTS;
844 static int expand_statement(struct statement *stmt)
846 if (!stmt)
847 return 0;
849 switch (stmt->type) {
850 case STMT_RETURN:
851 expand_return_expression(stmt);
852 return SIDE_EFFECTS;
854 case STMT_EXPRESSION:
855 return expand_expression(stmt->expression);
857 case STMT_COMPOUND: {
858 struct symbol *sym;
859 struct statement *s;
860 int cost;
862 FOR_EACH_PTR(stmt->syms, sym) {
863 expand_symbol(sym);
864 } END_FOR_EACH_PTR;
865 expand_symbol(stmt->ret);
867 cost = 0;
868 FOR_EACH_PTR(stmt->stmts, s) {
869 cost += expand_statement(s);
870 } END_FOR_EACH_PTR;
871 return cost;
874 case STMT_IF:
875 return expand_if_statement(stmt);
877 case STMT_ITERATOR:
878 expand_expression(stmt->iterator_pre_condition);
879 expand_expression(stmt->iterator_post_condition);
880 expand_statement(stmt->iterator_pre_statement);
881 expand_statement(stmt->iterator_statement);
882 expand_statement(stmt->iterator_post_statement);
883 return SIDE_EFFECTS;
885 case STMT_SWITCH:
886 expand_expression(stmt->switch_expression);
887 expand_statement(stmt->switch_statement);
888 return SIDE_EFFECTS;
890 case STMT_CASE:
891 expand_const_expression(stmt->case_expression, "case statement");
892 expand_const_expression(stmt->case_to, "case statement");
893 expand_statement(stmt->case_statement);
894 return SIDE_EFFECTS;
896 case STMT_LABEL:
897 expand_statement(stmt->label_statement);
898 return SIDE_EFFECTS;
900 case STMT_GOTO:
901 expand_expression(stmt->goto_expression);
902 return SIDE_EFFECTS;
904 case STMT_NONE:
905 break;
906 case STMT_ASM:
907 /* FIXME! Do the asm parameter evaluation! */
908 break;
910 return SIDE_EFFECTS;
913 long long get_expression_value(struct expression *expr)
915 long long value, mask;
916 struct symbol *ctype;
918 if (!expr)
919 return 0;
920 ctype = evaluate_expression(expr);
921 if (!ctype) {
922 warn(expr->pos, "bad constant expression type");
923 return 0;
925 expand_expression(expr);
926 if (expr->type != EXPR_VALUE) {
927 warn(expr->pos, "bad constant expression");
928 return 0;
931 value = expr->value;
932 mask = 1ULL << (ctype->bit_size-1);
934 if (value & mask) {
935 while (ctype->type != SYM_BASETYPE)
936 ctype = ctype->ctype.base_type;
937 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
938 value = value | mask | ~(mask-1);
940 return value;