Create a valid linearization of EXPR_SELECT.
[smatch.git] / expand.c
blob9fbcad81443f22ba6147da907e2f1f969c5306d7
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 = left->fvalue;
278 long double r = right->fvalue;
279 long double res;
281 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
282 return 0;
283 if (mod & MOD_LONGLONG) {
284 switch (expr->op) {
285 case '+': res = l + r; break;
286 case '-': res = l - r; break;
287 case '*': res = l * r; break;
288 case '/': if (!r) goto Div;
289 res = l / r; break;
290 default: return 0;
292 } else if (mod & MOD_LONG) {
293 switch (expr->op) {
294 case '+': res = (double) l + (double) r; break;
295 case '-': res = (double) l - (double) r; break;
296 case '*': res = (double) l * (double) r; break;
297 case '/': if (!r) goto Div;
298 res = (double) l / (double) r; break;
299 default: return 0;
301 } else {
302 switch (expr->op) {
303 case '+': res = (float)l + (float)r; break;
304 case '-': res = (float)l - (float)r; break;
305 case '*': res = (float)l * (float)r; break;
306 case '/': if (!r) goto Div;
307 res = (float)l / (float)r; break;
308 default: return 0;
311 expr->type = EXPR_FVALUE;
312 expr->fvalue = res;
313 return 1;
314 Div:
315 warn(expr->pos, "division by zero");
316 return 0;
319 static int simplify_float_cmp(struct expression *expr, struct symbol *ctype)
321 struct expression *left = expr->left, *right = expr->right;
322 long double l, r;
324 if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
325 return 0;
327 l = left->fvalue;
328 r = right->fvalue;
329 switch (expr->op) {
330 case '<': expr->value = l < r; break;
331 case '>': expr->value = l > r; break;
332 case SPECIAL_LTE: expr->value = l <= r; break;
333 case SPECIAL_GTE: expr->value = l >= r; break;
334 case SPECIAL_EQUAL: expr->value = l == r; break;
335 case SPECIAL_NOTEQUAL: expr->value = l != r; break;
337 expr->type = EXPR_VALUE;
338 return 1;
341 static int expand_binop(struct expression *expr)
343 int cost;
345 cost = expand_expression(expr->left);
346 cost += expand_expression(expr->right);
347 if (simplify_int_binop(expr, expr->ctype))
348 return 1;
349 if (simplify_float_binop(expr))
350 return 1;
351 return cost + 1;
354 static int expand_logical(struct expression *expr)
356 struct expression *left = expr->left;
357 struct expression *right;
358 int cost, rcost;
360 /* Do immediate short-circuiting ... */
361 cost = expand_expression(left);
362 if (left->type == EXPR_VALUE) {
363 if (expr->op == SPECIAL_LOGICAL_AND) {
364 if (!left->value) {
365 expr->type = EXPR_VALUE;
366 expr->value = 0;
367 return 0;
369 } else {
370 if (left->value) {
371 expr->type = EXPR_VALUE;
372 expr->value = 1;
373 return 0;
378 right = expr->right;
379 rcost = expand_expression(right);
380 if (left->type == EXPR_VALUE && right->type == EXPR_VALUE) {
382 * We know the left value doesn't matter, since
383 * otherwise we would have short-circuited it..
385 expr->type = EXPR_VALUE;
386 expr->value = right->value != 0;
387 return 0;
391 * If the right side is safe and cheaper than a branch,
392 * just avoid the branch and turn it into a regular binop
393 * style SAFELOGICAL.
395 if (rcost < BRANCH_COST) {
396 expr->type = EXPR_BINOP;
397 rcost -= BRANCH_COST - 1;
400 return cost + BRANCH_COST + rcost;
403 static int expand_comma(struct expression *expr)
405 int cost;
407 cost = expand_expression(expr->left);
408 cost += expand_expression(expr->right);
409 if (expr->left->type == EXPR_VALUE || expr->left->type == EXPR_FVALUE)
410 *expr = *expr->right;
411 return cost;
414 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
416 static int compare_types(int op, struct symbol *left, struct symbol *right)
418 switch (op) {
419 case SPECIAL_EQUAL:
420 return !type_difference(left, right, MOD_IGN, MOD_IGN);
421 case SPECIAL_NOTEQUAL:
422 return type_difference(left, right, MOD_IGN, MOD_IGN) != NULL;
423 case '<':
424 return left->bit_size < right->bit_size;
425 case '>':
426 return left->bit_size > right->bit_size;
427 case SPECIAL_LTE:
428 return left->bit_size <= right->bit_size;
429 case SPECIAL_GTE:
430 return left->bit_size >= right->bit_size;
432 return 0;
435 static int expand_compare(struct expression *expr)
437 struct expression *left = expr->left, *right = expr->right;
438 int cost;
440 cost = expand_expression(left);
441 cost += expand_expression(right);
443 /* Type comparison? */
444 if (left && right && left->type == EXPR_TYPE && right->type == EXPR_TYPE) {
445 int op = expr->op;
446 expr->type = EXPR_VALUE;
447 expr->value = compare_types(op, left->symbol, right->symbol);
448 return 0;
450 if (simplify_cmp_binop(expr, left->ctype))
451 return 0;
452 if (simplify_float_cmp(expr, left->ctype))
453 return 0;
454 return cost + 1;
457 static int expand_conditional(struct expression *expr)
459 struct expression *cond = expr->conditional;
460 struct expression *true = expr->cond_true;
461 struct expression *false = expr->cond_false;
462 int cost, cond_cost;
464 cond_cost = expand_expression(cond);
465 if (cond->type == EXPR_VALUE) {
466 true = true ? : cond;
467 if (!cond->value)
468 true = false;
469 *expr = *true;
470 return expand_expression(expr);
473 cost = expand_expression(true);
474 cost += expand_expression(false);
476 if (cost < SELECT_COST) {
477 expr->type = EXPR_SELECT;
478 cost -= BRANCH_COST - 1;
481 return cost + cond_cost + BRANCH_COST;
484 static int expand_assignment(struct expression *expr)
486 expand_expression(expr->left);
487 expand_expression(expr->right);
488 return UNSAFE;
491 static int expand_addressof(struct expression *expr)
493 return expand_expression(expr->unop);
496 static int expand_dereference(struct expression *expr)
498 struct expression *unop = expr->unop;
500 expand_expression(unop);
503 * NOTE! We get a bogus warning right now for some special
504 * cases: apparently I've screwed up the optimization of
505 * a zero-offset derefence, and the ctype is wrong.
507 * Leave the warning in anyway, since this is also a good
508 * test for me to get the type evaluation right..
510 if (expr->ctype->ctype.modifiers & MOD_NODEREF)
511 warn(unop->pos, "dereference of noderef expression");
513 if (unop->type == EXPR_SYMBOL) {
514 struct symbol *sym = unop->symbol;
516 /* Const symbol with a constant initializer? */
517 if (!(sym->ctype.modifiers & (MOD_ASSIGNED | MOD_ADDRESSABLE))) {
518 struct expression *value = sym->initializer;
519 if (value) {
520 if (value->type == EXPR_VALUE) {
521 expr->type = EXPR_VALUE;
522 expr->value = value->value;
523 return 0;
524 } else if (value->type == EXPR_FVALUE) {
525 expr->type = EXPR_FVALUE;
526 expr->fvalue = value->fvalue;
527 return 0;
532 /* Direct symbol dereference? Cheap and safe */
533 return (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN)) ? 2 : 1;
535 return UNSAFE;
538 static int simplify_preop(struct expression *expr)
540 struct expression *op = expr->unop;
541 unsigned long long v, mask;
543 if (op->type != EXPR_VALUE)
544 return 0;
545 v = op->value;
546 switch (expr->op) {
547 case '+': break;
548 case '-': v = -v; break;
549 case '!': v = !v; break;
550 case '~': v = ~v; break;
551 default: return 0;
553 mask = 1ULL << (expr->ctype->bit_size-1);
554 mask = mask | (mask-1);
555 expr->value = v & mask;
556 expr->type = EXPR_VALUE;
557 return 1;
560 static int simplify_float_preop(struct expression *expr)
562 struct expression *op = expr->unop;
563 long double v;
565 if (op->type != EXPR_FVALUE)
566 return 0;
567 v = op->fvalue;
568 switch (expr->op) {
569 case '+': break;
570 case '-': v = -v; break;
571 default: return 0;
573 expr->fvalue = v;
574 expr->type = EXPR_FVALUE;
575 return 1;
579 * Unary post-ops: x++ and x--
581 static int expand_postop(struct expression *expr)
583 expand_expression(expr->unop);
584 return UNSAFE;
587 static int expand_preop(struct expression *expr)
589 int cost;
591 switch (expr->op) {
592 case '*':
593 return expand_dereference(expr);
595 case '&':
596 return expand_addressof(expr);
598 case SPECIAL_INCREMENT:
599 case SPECIAL_DECREMENT:
601 * From a type evaluation standpoint the pre-ops are
602 * the same as the postops
604 return expand_postop(expr);
606 default:
607 break;
609 cost = expand_expression(expr->unop);
611 if (simplify_preop(expr))
612 return 0;
613 if (simplify_float_preop(expr))
614 return 0;
615 return cost + 1;
618 static int expand_arguments(struct expression_list *head)
620 int cost = 0;
621 struct expression *expr;
623 FOR_EACH_PTR (head, expr) {
624 cost += expand_expression(expr);
625 } END_FOR_EACH_PTR;
626 return cost;
629 static int expand_cast(struct expression *expr)
631 int cost;
632 struct expression *target = expr->cast_expression;
634 cost = expand_expression(target);
636 /* Simplify normal integer casts.. */
637 if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
638 cast_value(expr, expr->ctype, target, target->ctype);
639 return 0;
641 return cost + 1;
645 * expand a call expression with a symbol. This
646 * should expand builtins.
648 static int expand_symbol_call(struct expression *expr)
650 struct expression *fn = expr->fn;
651 struct symbol *ctype = fn->ctype;
653 if (fn->type != EXPR_PREOP)
654 return UNSAFE;
656 if (ctype->op && ctype->op->expand)
657 return ctype->op->expand(expr);
659 return UNSAFE;
662 static int expand_call(struct expression *expr)
664 int cost;
665 struct symbol *sym;
666 struct expression *fn = expr->fn;
668 cost = expand_arguments(expr->args);
669 sym = fn->ctype;
670 if (sym->type == SYM_NODE)
671 return expand_symbol_call(expr);
673 return UNSAFE;
676 static int expand_expression_list(struct expression_list *list)
678 int cost = 0;
679 struct expression *expr;
681 FOR_EACH_PTR(list, expr) {
682 cost += expand_expression(expr);
683 } END_FOR_EACH_PTR;
684 return cost;
687 static int expand_expression(struct expression *expr)
689 if (!expr)
690 return 0;
691 if (!expr->ctype)
692 return UNSAFE;
694 switch (expr->type) {
695 case EXPR_VALUE:
696 case EXPR_FVALUE:
697 case EXPR_STRING:
698 return 0;
699 case EXPR_TYPE:
700 case EXPR_SYMBOL:
701 return expand_symbol_expression(expr);
702 case EXPR_BINOP:
703 return expand_binop(expr);
705 case EXPR_LOGICAL:
706 return expand_logical(expr);
708 case EXPR_COMMA:
709 return expand_comma(expr);
711 case EXPR_COMPARE:
712 return expand_compare(expr);
714 case EXPR_ASSIGNMENT:
715 return expand_assignment(expr);
717 case EXPR_PREOP:
718 return expand_preop(expr);
720 case EXPR_POSTOP:
721 return expand_postop(expr);
723 case EXPR_CAST:
724 return expand_cast(expr);
726 case EXPR_CALL:
727 return expand_call(expr);
729 case EXPR_DEREF:
730 warn(expr->pos, "we should not have an EXPR_DEREF left at expansion time");
731 return UNSAFE;
733 case EXPR_BITFIELD:
734 return expand_expression(expr->address);
736 case EXPR_SELECT:
737 case EXPR_CONDITIONAL:
738 return expand_conditional(expr);
740 case EXPR_STATEMENT:
741 return expand_statement(expr->statement);
743 case EXPR_LABEL:
744 return 0;
746 case EXPR_INITIALIZER:
747 return expand_expression_list(expr->expr_list);
749 case EXPR_IDENTIFIER:
750 return UNSAFE;
752 case EXPR_INDEX:
753 return UNSAFE;
755 case EXPR_POS:
756 return expand_expression(expr->init_expr);
758 case EXPR_SIZEOF:
759 case EXPR_ALIGNOF:
760 warn(expr->pos, "internal front-end error: sizeof in expansion?");
761 return UNSAFE;
763 return UNSAFE;
766 static void expand_const_expression(struct expression *expr, const char *where)
768 if (expr) {
769 expand_expression(expr);
770 if (expr->type != EXPR_VALUE)
771 warn(expr->pos, "Expected constant expression in %s", where);
775 void expand_symbol(struct symbol *sym)
777 struct symbol *base_type;
779 if (!sym)
780 return;
781 base_type = sym->ctype.base_type;
782 if (!base_type)
783 return;
785 expand_expression(sym->initializer);
786 /* expand the body of the symbol */
787 if (base_type->type == SYM_FN) {
788 if (base_type->stmt)
789 expand_statement(base_type->stmt);
793 static void expand_return_expression(struct statement *stmt)
795 expand_expression(stmt->expression);
798 static int expand_if_statement(struct statement *stmt)
800 struct expression *expr = stmt->if_conditional;
802 if (!expr || !expr->ctype)
803 return UNSAFE;
805 expand_expression(expr);
807 /* This is only valid if nobody jumps into the "dead" side */
808 #if 0
809 /* Simplify constant conditionals without even evaluating the false side */
810 if (expr->type == EXPR_VALUE) {
811 struct statement *simple;
812 simple = expr->value ? stmt->if_true : stmt->if_false;
814 /* Nothing? */
815 if (!simple) {
816 stmt->type = STMT_NONE;
817 return 0;
819 expand_statement(simple);
820 *stmt = *simple;
821 return UNSAFE;
823 #endif
824 expand_statement(stmt->if_true);
825 expand_statement(stmt->if_false);
826 return UNSAFE;
829 static int expand_statement(struct statement *stmt)
831 if (!stmt)
832 return 0;
834 switch (stmt->type) {
835 case STMT_RETURN:
836 expand_return_expression(stmt);
837 return UNSAFE;
839 case STMT_EXPRESSION:
840 return expand_expression(stmt->expression);
842 case STMT_COMPOUND: {
843 struct symbol *sym;
844 struct statement *s;
845 int cost;
847 FOR_EACH_PTR(stmt->syms, sym) {
848 expand_symbol(sym);
849 } END_FOR_EACH_PTR;
850 expand_symbol(stmt->ret);
852 cost = 0;
853 FOR_EACH_PTR(stmt->stmts, s) {
854 cost += expand_statement(s);
855 } END_FOR_EACH_PTR;
856 return cost;
859 case STMT_IF:
860 return expand_if_statement(stmt);
862 case STMT_ITERATOR:
863 expand_expression(stmt->iterator_pre_condition);
864 expand_expression(stmt->iterator_post_condition);
865 expand_statement(stmt->iterator_pre_statement);
866 expand_statement(stmt->iterator_statement);
867 expand_statement(stmt->iterator_post_statement);
868 return UNSAFE;
870 case STMT_SWITCH:
871 expand_expression(stmt->switch_expression);
872 expand_statement(stmt->switch_statement);
873 return UNSAFE;
875 case STMT_CASE:
876 expand_const_expression(stmt->case_expression, "case statement");
877 expand_const_expression(stmt->case_to, "case statement");
878 expand_statement(stmt->case_statement);
879 return UNSAFE;
881 case STMT_LABEL:
882 expand_statement(stmt->label_statement);
883 return UNSAFE;
885 case STMT_GOTO:
886 expand_expression(stmt->goto_expression);
887 return UNSAFE;
889 case STMT_NONE:
890 break;
891 case STMT_ASM:
892 /* FIXME! Do the asm parameter evaluation! */
893 break;
895 return UNSAFE;
898 long long get_expression_value(struct expression *expr)
900 long long value, mask;
901 struct symbol *ctype;
903 if (!expr)
904 return 0;
905 ctype = evaluate_expression(expr);
906 if (!ctype) {
907 warn(expr->pos, "bad constant expression type");
908 return 0;
910 expand_expression(expr);
911 if (expr->type != EXPR_VALUE) {
912 warn(expr->pos, "bad constant expression");
913 return 0;
916 value = expr->value;
917 mask = 1ULL << (ctype->bit_size-1);
919 if (value & mask) {
920 while (ctype->type != SYM_BASETYPE)
921 ctype = ctype->ctype.base_type;
922 if (!(ctype->ctype.modifiers & MOD_UNSIGNED))
923 value = value | mask | ~(mask-1);
925 return value;