[PATCH] better recovery from type errors in EXPR_COMMA
[smatch.git] / expand.c
blobe8bc1f4aaeabf7f4825055150ff685da5454d272
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 UNSAFE 100 /* The expression may be "infinitely costly" due to exceptions */
30 #define SELECT_COST 20 /* Cut-off for turning a conditional into a select */
31 #define BRANCH_COST 10 /* Cost of a conditional branch */
33 static int expand_expression(struct expression *);
34 static int expand_statement(struct statement *);
36 static int expand_symbol_expression(struct expression *expr)
38 struct symbol *sym = expr->symbol;
41 * The preprocessor can cause unknown symbols to be generated
43 if (!sym) {
44 warn(expr->pos, "undefined preprocessor identifier '%s'", show_ident(expr->symbol_name));
45 expr->type = EXPR_VALUE;
46 expr->value = 0;
47 return UNSAFE;
50 /* The cost of a symbol expression is lower for on-stack symbols */
51 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
54 static long long get_longlong(struct expression *expr)
56 int no_expand = expr->ctype->ctype.modifiers & MOD_UNSIGNED;
57 long long mask = 1ULL << (expr->ctype->bit_size - 1);
58 long long value = expr->value;
59 long long ormask, andmask;
61 if (!(value & mask))
62 no_expand = 1;
63 andmask = mask | (mask-1);
64 ormask = ~andmask;
65 if (no_expand)
66 ormask = 0;
67 return (value & andmask) | ormask;
70 void cast_value(struct expression *expr, struct symbol *newtype,
71 struct expression *old, struct symbol *oldtype)
73 int old_size = oldtype->bit_size;
74 int new_size = newtype->bit_size;
75 long long value, mask;
77 if (newtype->ctype.base_type == &fp_type ||
78 oldtype->ctype.base_type == &fp_type)
79 goto Float;
81 // For pointers and integers, we can just move the value around
82 expr->type = EXPR_VALUE;
83 if (old_size == new_size) {
84 expr->value = old->value;
85 return;
88 // expand it to the full "long long" value
89 value = get_longlong(old);
91 Int:
92 // Truncate it to the new size
93 mask = 1ULL << (new_size-1);
94 mask = mask | (mask-1);
95 expr->value = value & mask;
96 return;
98 Float:
99 if (newtype->ctype.base_type != &fp_type) {
100 value = (long long)old->fvalue;
101 expr->type = EXPR_VALUE;
102 goto Int;
105 if (oldtype->ctype.base_type != &fp_type)
106 expr->fvalue = (long double)get_longlong(old);
107 else
108 expr->fvalue = old->value;
110 if (!(newtype->ctype.modifiers & MOD_LONGLONG)) {
111 if ((newtype->ctype.modifiers & MOD_LONG))
112 expr->fvalue = (double)expr->fvalue;
113 else
114 expr->fvalue = (float)expr->fvalue;
116 expr->type = EXPR_FVALUE;
119 static int check_shift_count(struct expression *expr, struct symbol *ctype, unsigned int count)
121 if (count >= ctype->bit_size) {
122 warn(expr->pos, "shift too big for type (%x)", ctype->ctype.modifiers);
123 count &= ctype->bit_size-1;
125 return count;
129 * CAREFUL! We need to get the size and sign of the
130 * result right!
132 #define CONVERT(op,s) (((op)<<1)+(s))
133 #define SIGNED(op) CONVERT(op, 1)
134 #define UNSIGNED(op) CONVERT(op, 0)
135 static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
137 struct expression *left = expr->left, *right = expr->right;
138 unsigned long long v, l, r, mask;
139 signed long long sl, sr;
140 int is_signed, shift;
142 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
143 return 0;
144 l = left->value; r = right->value;
145 is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
146 mask = 1ULL << (ctype->bit_size-1);
147 sl = l; sr = r;
148 if (is_signed && (sl & mask))
149 sl |= ~(mask-1);
150 if (is_signed && (sr & mask))
151 sr |= ~(mask-1);
153 switch (CONVERT(expr->op,is_signed)) {
154 case SIGNED('+'):
155 case UNSIGNED('+'):
156 v = l + r;
157 break;
159 case SIGNED('-'):
160 case UNSIGNED('-'):
161 v = l - r;
162 break;
164 case SIGNED('&'):
165 case UNSIGNED('&'):
166 v = l & r;
167 break;
169 case SIGNED('|'):
170 case UNSIGNED('|'):
171 v = l | r;
172 break;
174 case SIGNED('^'):
175 case UNSIGNED('^'):
176 v = l ^ r;
177 break;
179 case SIGNED('*'):
180 v = sl * sr;
181 break;
183 case UNSIGNED('*'):
184 v = l * r;
185 break;
187 case SIGNED('/'):
188 if (!r)
189 goto Div;
190 if (l == mask && sr == -1)
191 goto Overflow;
192 v = sl / sr;
193 break;
195 case UNSIGNED('/'):
196 if (!r) goto Div;
197 v = l / r;
198 break;
200 case SIGNED('%'):
201 if (!r)
202 goto Div;
203 v = sl % sr;
204 break;
206 case UNSIGNED('%'):
207 if (!r) goto Div;
208 v = l % r;
209 break;
211 case SIGNED(SPECIAL_LEFTSHIFT):
212 case UNSIGNED(SPECIAL_LEFTSHIFT):
213 shift = check_shift_count(expr, ctype, r);
214 v = l << shift;
215 break;
217 case SIGNED(SPECIAL_RIGHTSHIFT):
218 shift = check_shift_count(expr, ctype, r);
219 v = sl >> shift;
220 break;
222 case UNSIGNED(SPECIAL_RIGHTSHIFT):
223 shift = check_shift_count(expr, ctype, r);
224 v = l >> shift;
225 break;
227 default:
228 return 0;
230 mask = mask | (mask-1);
231 expr->value = v & mask;
232 expr->type = EXPR_VALUE;
233 return 1;
234 Div:
235 warn(expr->pos, "division by zero");
236 return 0;
237 Overflow:
238 warn(expr->pos, "constant integer operation overflow");
239 return 0;
242 static int simplify_cmp_binop(struct expression *expr, struct symbol *ctype)
244 struct expression *left = expr->left, *right = expr->right;
245 unsigned long long l, r, mask;
246 signed long long sl, sr;
248 if (left->type != EXPR_VALUE || right->type != EXPR_VALUE)
249 return 0;
250 l = left->value; r = right->value;
251 mask = 1ULL << (ctype->bit_size-1);
252 sl = l; sr = r;
253 if (sl & mask)
254 sl |= ~(mask-1);
255 if (sr & mask)
256 sr |= ~(mask-1);
257 switch (expr->op) {
258 case '<': expr->value = sl < sr; break;
259 case '>': expr->value = sl > sr; break;
260 case SPECIAL_LTE: expr->value = sl <= sr; break;
261 case SPECIAL_GTE: expr->value = sl >= sr; break;
262 case SPECIAL_EQUAL: expr->value = l == r; break;
263 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
264 case SPECIAL_UNSIGNED_LT:expr->value = l < r; break;
265 case SPECIAL_UNSIGNED_GT:expr->value = l > r; break;
266 case SPECIAL_UNSIGNED_LTE:expr->value = l <= r; break;
267 case SPECIAL_UNSIGNED_GTE:expr->value = l >= r; break;
269 expr->type = EXPR_VALUE;
270 return 1;
273 static int simplify_float_binop(struct expression *expr)
275 struct expression *left = expr->left, *right = expr->right;
276 unsigned long mod = expr->ctype->ctype.modifiers;
277 long double l, r, res;
279 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
280 return 0;
282 l = left->fvalue;
283 r = right->fvalue;
285 if (mod & MOD_LONGLONG) {
286 switch (expr->op) {
287 case '+': res = l + r; break;
288 case '-': res = l - r; break;
289 case '*': res = l * r; break;
290 case '/': if (!r) goto Div;
291 res = l / r; break;
292 default: return 0;
294 } else if (mod & MOD_LONG) {
295 switch (expr->op) {
296 case '+': res = (double) l + (double) r; break;
297 case '-': res = (double) l - (double) r; break;
298 case '*': res = (double) l * (double) r; break;
299 case '/': if (!r) goto Div;
300 res = (double) l / (double) r; break;
301 default: return 0;
303 } else {
304 switch (expr->op) {
305 case '+': res = (float)l + (float)r; break;
306 case '-': res = (float)l - (float)r; break;
307 case '*': res = (float)l * (float)r; break;
308 case '/': if (!r) goto Div;
309 res = (float)l / (float)r; break;
310 default: return 0;
313 expr->type = EXPR_FVALUE;
314 expr->fvalue = res;
315 return 1;
316 Div:
317 warn(expr->pos, "division by zero");
318 return 0;
321 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
323 struct expression *left = expr->left, *right = expr->right;
324 long double l, r;
326 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
327 return 0;
329 l = left->fvalue;
330 r = right->fvalue;
331 switch (expr->op) {
332 case '<': expr->value = l < r; break;
333 case '>': expr->value = l > r; break;
334 case SPECIAL_LTE: expr->value = l <= r; break;
335 case SPECIAL_GTE: expr->value = l >= r; break;
336 case SPECIAL_EQUAL: expr->value = l == r; break;
337 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
339 expr->type = EXPR_VALUE;
340 return 1;
343 static int expand_binop(struct expression *expr)
345 int cost;
347 cost = expand_expression(expr->left);
348 cost += expand_expression(expr->right);
349 if (simplify_int_binop(expr, expr->ctype))
350 return 1;
351 if (simplify_float_binop(expr))
352 return 1;
353 return cost + 1;
356 static int expand_logical(struct expression *expr)
358 struct expression *left = expr->left;
359 struct expression *right;
360 int cost, rcost;
362 /* Do immediate short-circuiting ... */
363 cost = expand_expression(left);
364 if (left->type == EXPR_VALUE) {
365 if (expr->op == SPECIAL_LOGICAL_AND) {
366 if (!left->value) {
367 expr->type = EXPR_VALUE;
368 expr->value = 0;
369 return 0;
371 } else {
372 if (left->value) {
373 expr->type = EXPR_VALUE;
374 expr->value = 1;
375 return 0;
380 right = expr->right;
381 rcost = expand_expression(right);
382 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
384 * We know the left value doesn't matter, since
385 * otherwise we would have short-circuited it..
387 expr->type = EXPR_VALUE;
388 expr->value = right->value != 0;
389 return 0;
393 * If the right side is safe and cheaper than a branch,
394 * just avoid the branch and turn it into a regular binop
395 * style SAFELOGICAL.
397 if (rcost < BRANCH_COST) {
398 expr->type = EXPR_BINOP;
399 rcost -= BRANCH_COST - 1;
402 return cost + BRANCH_COST + rcost;
405 static int expand_comma(struct expression *expr)
407 int cost;
409 cost = expand_expression(expr->left);
410 cost += expand_expression(expr->right);
411 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE)
412 *expr = *expr->right;
413 return cost;
416 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
418 static int compare_types(int op, struct symbol *left, struct symbol *right)
420 switch (op) {
421 case SPECIAL_EQUAL:
422 return !type_difference(left, right, MOD_IGN, MOD_IGN);
423 case SPECIAL_NOTEQUAL:
424 return type_difference(left, right, MOD_IGN, MOD_IGN) != NULL;
425 case '<':
426 return left->bit_size < right->bit_size;
427 case '>':
428 return left->bit_size > right->bit_size;
429 case SPECIAL_LTE:
430 return left->bit_size <= right->bit_size;
431 case SPECIAL_GTE:
432 return left->bit_size >= right->bit_size;
434 return 0;
437 static int expand_compare(struct expression *expr)
439 struct expression *left = expr->left, *right = expr->right;
440 int cost;
442 cost = expand_expression(left);
443 cost += expand_expression(right);
445 /* Type comparison? */
446 if (left && right && left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
447 int op = expr->op;
448 expr->type = EXPR_VALUE;
449 expr->value = compare_types(op, left->symbol, right->symbol);
450 return 0;
452 if (simplify_cmp_binop(expr, left->ctype))
453 return 0;
454 if (simplify_float_cmp(expr, left->ctype))
455 return 0;
456 return cost + 1;
459 static int expand_conditional(struct expression *expr)
461 struct expression *cond = expr->conditional;
462 struct expression *true = expr->cond_true;
463 struct expression *false = expr->cond_false;
464 int cost, cond_cost;
466 cond_cost = expand_expression(cond);
467 if (cond->type == EXPR_VALUE) {
468 true = true ? : cond;
469 if (!cond->value)
470 true = false;
471 *expr = *true;
472 return expand_expression(expr);
475 cost = expand_expression(true);
476 cost += expand_expression(false);
478 if (cost < SELECT_COST) {
479 expr->type = EXPR_SELECT;
480 cost -= BRANCH_COST - 1;
483 return cost + cond_cost + BRANCH_COST;
486 static int expand_assignment(struct expression *expr)
488 expand_expression(expr->left);
489 expand_expression(expr->right);
490 return UNSAFE;
493 static int expand_addressof(struct expression *expr)
495 return expand_expression(expr->unop);
498 static int expand_dereference(struct expression *expr)
500 struct expression *unop = expr->unop;
502 expand_expression(unop);
505 * NOTE! We get a bogus warning right now for some special
506 * cases: apparently I've screwed up the optimization of
507 * a zero-offset derefence, and the ctype is wrong.
509 * Leave the warning in anyway, since this is also a good
510 * test for me to get the type evaluation right..
512 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
513 warn(unop->pos, "dereference of noderef expression");
515 if (unop->type == EXPR_SYMBOL) {
516 struct symbol *sym = unop->symbol;
518 /* Const symbol with a constant initializer? */
519 if (!(sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))) {
520 struct expression *value = sym->initializer;
521 if (value) {
522 if (value->type == EXPR_VALUE) {
523 expr->type = EXPR_VALUE;
524 expr->value = value->value;
525 return 0;
526 } else if (value->type == EXPR_FVALUE) {
527 expr->type = EXPR_FVALUE;
528 expr->fvalue = value->fvalue;
529 return 0;
534 /* Direct symbol dereference? Cheap and safe */
535 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
537 return UNSAFE;
540 static int simplify_preop(struct expression *expr)
542 struct expression *op = expr->unop;
543 unsigned long long v, mask;
545 if (op->type != EXPR_VALUE)
546 return 0;
548 mask = 1ULL << (expr->ctype->bit_size-1);
549 v = op->value;
550 switch (expr->op) {
551 case '+': break;
552 case '-':
553 if (v == mask && !(expr->ctype->ctype.modifiers & MOD_UNSIGNED))
554 goto Overflow;
555 v = -v;
556 break;
557 case '!': v = !v; break;
558 case '~': v = ~v; break;
559 default: return 0;
561 mask = mask | (mask-1);
562 expr->value = v & mask;
563 expr->type = EXPR_VALUE;
564 return 1;
566 Overflow:
567 warn(expr->pos, "constant integer operation overflow");
568 return 0;
571 static int simplify_float_preop(struct expression *expr)
573 struct expression *op = expr->unop;
574 long double v;
576 if (op->type != EXPR_FVALUE)
577 return 0;
578 v = op->fvalue;
579 switch (expr->op) {
580 case '+': break;
581 case '-': v = -v; break;
582 default: return 0;
584 expr->fvalue = v;
585 expr->type = EXPR_FVALUE;
586 return 1;
590 * Unary post-ops: x++ and x--
592 static int expand_postop(struct expression *expr)
594 expand_expression(expr->unop);
595 return UNSAFE;
598 static int expand_preop(struct expression *expr)
600 int cost;
602 switch (expr->op) {
603 case '*':
604 return expand_dereference(expr);
606 case '&':
607 return expand_addressof(expr);
609 case SPECIAL_INCREMENT:
610 case SPECIAL_DECREMENT:
612 * From a type evaluation standpoint the pre-ops are
613 * the same as the postops
615 return expand_postop(expr);
617 default:
618 break;
620 cost = expand_expression(expr->unop);
622 if (simplify_preop(expr))
623 return 0;
624 if (simplify_float_preop(expr))
625 return 0;
626 return cost + 1;
629 static int expand_arguments(struct expression_list *head)
631 int cost = 0;
632 struct expression *expr;
634 FOR_EACH_PTR (head, expr) {
635 cost += expand_expression(expr);
636 } END_FOR_EACH_PTR;
637 return cost;
640 static int expand_cast(struct expression *expr)
642 int cost;
643 struct expression *target = expr->cast_expression;
645 cost = expand_expression(target);
647 /* Simplify normal integer casts.. */
648 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
649 cast_value(expr, expr->ctype, target, target->ctype);
650 return 0;
652 return cost + 1;
656 * expand a call expression with a symbol. This
657 * should expand builtins.
659 static int expand_symbol_call(struct expression *expr)
661 struct expression *fn = expr->fn;
662 struct symbol *ctype = fn->ctype;
664 if (fn->type != EXPR_PREOP)
665 return UNSAFE;
667 if (ctype->op && ctype->op->expand)
668 return ctype->op->expand(expr);
670 return UNSAFE;
673 static int expand_call(struct expression *expr)
675 int cost;
676 struct symbol *sym;
677 struct expression *fn = expr->fn;
679 cost = expand_arguments(expr->args);
680 sym = fn->ctype;
681 if (sym->type == SYM_NODE)
682 return expand_symbol_call(expr);
684 return UNSAFE;
687 static int expand_expression_list(struct expression_list *list)
689 int cost = 0;
690 struct expression *expr;
692 FOR_EACH_PTR(list, expr) {
693 cost += expand_expression(expr);
694 } END_FOR_EACH_PTR;
695 return cost;
698 static int expand_expression(struct expression *expr)
700 if (!expr)
701 return 0;
702 if (!expr->ctype)
703 return UNSAFE;
705 switch (expr->type) {
706 case EXPR_VALUE:
707 case EXPR_FVALUE:
708 case EXPR_STRING:
709 return 0;
710 case EXPR_TYPE:
711 case EXPR_SYMBOL:
712 return expand_symbol_expression(expr);
713 case EXPR_BINOP:
714 return expand_binop(expr);
716 case EXPR_LOGICAL:
717 return expand_logical(expr);
719 case EXPR_COMMA:
720 return expand_comma(expr);
722 case EXPR_COMPARE:
723 return expand_compare(expr);
725 case EXPR_ASSIGNMENT:
726 return expand_assignment(expr);
728 case EXPR_PREOP:
729 return expand_preop(expr);
731 case EXPR_POSTOP:
732 return expand_postop(expr);
734 case EXPR_CAST:
735 return expand_cast(expr);
737 case EXPR_CALL:
738 return expand_call(expr);
740 case EXPR_DEREF:
741 warn(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
742 return UNSAFE;
744 case EXPR_BITFIELD:
745 return expand_expression(expr->address);
747 case EXPR_SELECT:
748 case EXPR_CONDITIONAL:
749 return expand_conditional(expr);
751 case EXPR_STATEMENT:
752 return expand_statement(expr->statement);
754 case EXPR_LABEL:
755 return 0;
757 case EXPR_INITIALIZER:
758 return expand_expression_list(expr->expr_list);
760 case EXPR_IDENTIFIER:
761 return UNSAFE;
763 case EXPR_INDEX:
764 return UNSAFE;
766 case EXPR_SLICE:
767 return expand_expression(expr->base) + 1;
769 case EXPR_POS:
770 return expand_expression(expr->init_expr);
772 case EXPR_SIZEOF:
773 case EXPR_ALIGNOF:
774 warn(expr->pos, "internal front-end error: sizeof in expansion?");
775 return UNSAFE;
777 return UNSAFE;
780 static void expand_const_expression(struct expression *expr, const char *where)
782 if (expr) {
783 expand_expression(expr);
784 if (expr->type != EXPR_VALUE)
785 warn(expr->pos, "Expected constant expression in %s", where);
789 void expand_symbol(struct symbol *sym)
791 struct symbol *base_type;
793 if (!sym)
794 return;
795 base_type = sym->ctype.base_type;
796 if (!base_type)
797 return;
799 expand_expression(sym->initializer);
800 /* expand the body of the symbol */
801 if (base_type->type == SYM_FN) {
802 if (base_type->stmt)
803 expand_statement(base_type->stmt);
807 static void expand_return_expression(struct statement *stmt)
809 expand_expression(stmt->expression);
812 static int expand_if_statement(struct statement *stmt)
814 struct expression *expr = stmt->if_conditional;
816 if (!expr || !expr->ctype)
817 return UNSAFE;
819 expand_expression(expr);
821 /* This is only valid if nobody jumps into the "dead" side */
822 #if 0
823 /* Simplify constant conditionals without even evaluating the false side */
824 if (expr->type == EXPR_VALUE) {
825 struct statement *simple;
826 simple = expr->value ? stmt->if_true : stmt->if_false;
828 /* Nothing? */
829 if (!simple) {
830 stmt->type = STMT_NONE;
831 return 0;
833 expand_statement(simple);
834 *stmt = *simple;
835 return UNSAFE;
837 #endif
838 expand_statement(stmt->if_true);
839 expand_statement(stmt->if_false);
840 return UNSAFE;
843 static int expand_statement(struct statement *stmt)
845 if (!stmt)
846 return 0;
848 switch (stmt->type) {
849 case STMT_RETURN:
850 expand_return_expression(stmt);
851 return UNSAFE;
853 case STMT_EXPRESSION:
854 return expand_expression(stmt->expression);
856 case STMT_COMPOUND: {
857 struct symbol *sym;
858 struct statement *s;
859 int cost;
861 FOR_EACH_PTR(stmt->syms, sym) {
862 expand_symbol(sym);
863 } END_FOR_EACH_PTR;
864 expand_symbol(stmt->ret);
866 cost = 0;
867 FOR_EACH_PTR(stmt->stmts, s) {
868 cost += expand_statement(s);
869 } END_FOR_EACH_PTR;
870 return cost;
873 case STMT_IF:
874 return expand_if_statement(stmt);
876 case STMT_ITERATOR:
877 expand_expression(stmt->iterator_pre_condition);
878 expand_expression(stmt->iterator_post_condition);
879 expand_statement(stmt->iterator_pre_statement);
880 expand_statement(stmt->iterator_statement);
881 expand_statement(stmt->iterator_post_statement);
882 return UNSAFE;
884 case STMT_SWITCH:
885 expand_expression(stmt->switch_expression);
886 expand_statement(stmt->switch_statement);
887 return UNSAFE;
889 case STMT_CASE:
890 expand_const_expression(stmt->case_expression, "case statement");
891 expand_const_expression(stmt->case_to, "case statement");
892 expand_statement(stmt->case_statement);
893 return UNSAFE;
895 case STMT_LABEL:
896 expand_statement(stmt->label_statement);
897 return UNSAFE;
899 case STMT_GOTO:
900 expand_expression(stmt->goto_expression);
901 return UNSAFE;
903 case STMT_NONE:
904 break;
905 case STMT_ASM:
906 /* FIXME! Do the asm parameter evaluation! */
907 break;
909 return UNSAFE;
912 long long get_expression_value(struct expression *expr)
914 long long value, mask;
915 struct symbol *ctype;
917 if (!expr)
918 return 0;
919 ctype = evaluate_expression(expr);
920 if (!ctype) {
921 warn(expr->pos, "bad constant expression type");
922 return 0;
924 expand_expression(expr);
925 if (expr->type != EXPR_VALUE) {
926 warn(expr->pos, "bad constant expression");
927 return 0;
930 value = expr->value;
931 mask = 1ULL << (ctype->bit_size-1);
933 if (value & mask) {
934 while (ctype->type != SYM_BASETYPE)
935 ctype = ctype->ctype.base_type;
936 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
937 value = value | mask | ~(mask-1);
939 return value;