4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * expand constant expressions.
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
;
53 andmask
= mask
| (mask
-1);
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
)
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
;
79 // expand it to the full "long long" value
80 value
= get_longlong(old
);
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)",
103 if (newtype
->ctype
.base_type
!= &fp_type
) {
104 value
= (long long)old
->fvalue
;
105 expr
->type
= EXPR_VALUE
;
109 if (oldtype
->ctype
.base_type
!= &fp_type
)
110 expr
->fvalue
= (long double)get_longlong(old
);
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
;
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;
133 * CAREFUL! We need to get the size and sign of the
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
;
146 if (right
->type
!= EXPR_VALUE
)
149 if (expr
->op
== SPECIAL_LEFTSHIFT
|| expr
->op
== SPECIAL_RIGHTSHIFT
) {
150 r
= check_shift_count(expr
, ctype
, r
);
153 if (left
->type
!= EXPR_VALUE
)
155 l
= left
->value
; r
= right
->value
;
156 is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
157 mask
= 1ULL << (ctype
->bit_size
-1);
159 if (is_signed
&& (sl
& mask
))
161 if (is_signed
&& (sr
& mask
))
164 switch (CONVERT(expr
->op
,is_signed
)) {
201 if (l
== mask
&& sr
== -1)
222 case SIGNED(SPECIAL_LEFTSHIFT
):
223 case UNSIGNED(SPECIAL_LEFTSHIFT
):
227 case SIGNED(SPECIAL_RIGHTSHIFT
):
231 case UNSIGNED(SPECIAL_RIGHTSHIFT
):
238 mask
= mask
| (mask
-1);
239 expr
->value
= v
& mask
;
240 expr
->type
= EXPR_VALUE
;
243 warning(expr
->pos
, "division by zero");
246 warning(expr
->pos
, "constant integer operation overflow");
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
)
258 l
= left
->value
; r
= right
->value
;
259 mask
= 1ULL << (ctype
->bit_size
-1);
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
;
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
)
293 if (mod
& MOD_LONGLONG
) {
295 case '+': res
= l
+ r
; break;
296 case '-': res
= l
- r
; break;
297 case '*': res
= l
* r
; break;
298 case '/': if (!r
) goto Div
;
302 } else if (mod
& MOD_LONG
) {
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;
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;
321 expr
->type
= EXPR_FVALUE
;
325 warning(expr
->pos
, "division by zero");
329 static int simplify_float_cmp(struct expression
*expr
, struct symbol
*ctype
)
331 struct expression
*left
= expr
->left
, *right
= expr
->right
;
334 if (left
->type
!= EXPR_FVALUE
|| right
->type
!= EXPR_FVALUE
)
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
;
351 static int expand_binop(struct expression
*expr
)
355 cost
= expand_expression(expr
->left
);
356 cost
+= expand_expression(expr
->right
);
357 if (simplify_int_binop(expr
, expr
->ctype
))
359 if (simplify_float_binop(expr
))
364 static int expand_logical(struct expression
*expr
)
366 struct expression
*left
= expr
->left
;
367 struct expression
*right
;
370 /* Do immediate short-circuiting ... */
371 cost
= expand_expression(left
);
372 if (left
->type
== EXPR_VALUE
) {
373 if (expr
->op
== SPECIAL_LOGICAL_AND
) {
375 expr
->type
= EXPR_VALUE
;
381 expr
->type
= EXPR_VALUE
;
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;
401 * If the right side is safe and cheaper than a branch,
402 * just avoid the branch and turn it into a regular binop
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
)
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
;
424 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
426 static int compare_types(int op
, struct symbol
*left
, struct symbol
*right
)
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
;
434 return left
->bit_size
< right
->bit_size
;
436 return left
->bit_size
> right
->bit_size
;
438 return left
->bit_size
<= right
->bit_size
;
440 return left
->bit_size
>= right
->bit_size
;
445 static int expand_compare(struct expression
*expr
)
447 struct expression
*left
= expr
->left
, *right
= expr
->right
;
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
) {
456 expr
->type
= EXPR_VALUE
;
457 expr
->value
= compare_types(op
, left
->symbol
, right
->symbol
);
460 if (simplify_cmp_binop(expr
, left
->ctype
))
462 if (simplify_float_cmp(expr
, left
->ctype
))
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
;
474 cond_cost
= expand_expression(cond
);
475 if (cond
->type
== EXPR_VALUE
) {
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
);
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
;
529 if (value
->type
== EXPR_VALUE
) {
530 expr
->type
= EXPR_VALUE
;
531 expr
->value
= value
->value
;
533 } else if (value
->type
== EXPR_FVALUE
) {
534 expr
->type
= EXPR_FVALUE
;
535 expr
->fvalue
= value
->fvalue
;
541 /* Direct symbol dereference? Cheap and safe */
542 return (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
)) ? 2 : 1;
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
)
555 mask
= 1ULL << (expr
->ctype
->bit_size
-1);
560 if (v
== mask
&& !(expr
->ctype
->ctype
.modifiers
& MOD_UNSIGNED
))
564 case '!': v
= !v
; break;
565 case '~': v
= ~v
; break;
568 mask
= mask
| (mask
-1);
569 expr
->value
= v
& mask
;
570 expr
->type
= EXPR_VALUE
;
574 warning(expr
->pos
, "constant integer operation overflow");
578 static int simplify_float_preop(struct expression
*expr
)
580 struct expression
*op
= expr
->unop
;
583 if (op
->type
!= EXPR_FVALUE
)
588 case '-': v
= -v
; break;
592 expr
->type
= EXPR_FVALUE
;
597 * Unary post-ops: x++ and x--
599 static int expand_postop(struct expression
*expr
)
601 expand_expression(expr
->unop
);
605 static int expand_preop(struct expression
*expr
)
611 return expand_dereference(expr
);
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
);
627 cost
= expand_expression(expr
->unop
);
629 if (simplify_preop(expr
))
631 if (simplify_float_preop(expr
))
636 static int expand_arguments(struct expression_list
*head
)
639 struct expression
*expr
;
641 FOR_EACH_PTR (head
, expr
) {
642 cost
+= expand_expression(expr
);
643 } END_FOR_EACH_PTR(expr
);
647 static int expand_cast(struct expression
*expr
)
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
);
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
;
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
);
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
)
690 if (ctype
->op
&& ctype
->op
->expand
)
691 return ctype
->op
->expand(expr
, cost
);
696 static int expand_call(struct expression
*expr
)
700 struct expression
*fn
= expr
->fn
;
702 cost
= expand_arguments(expr
->args
);
705 error(expr
->pos
, "function has no type");
708 if (sym
->type
== SYM_NODE
)
709 return expand_symbol_call(expr
, cost
);
714 static int expand_expression_list(struct expression_list
*list
)
717 struct expression
*expr
;
719 FOR_EACH_PTR(list
, expr
) {
720 cost
+= expand_expression(expr
);
721 } END_FOR_EACH_PTR(expr
);
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
;
736 switch (nested
->type
) {
738 offset
+= nested
->init_offset
;
740 expr
->init_offset
= offset
;
744 case EXPR_INITIALIZER
: {
745 struct expression
*reuse
= nested
, *entry
;
747 FOR_EACH_PTR(expr
->expr_list
, entry
) {
748 if (entry
->type
== EXPR_POS
) {
749 entry
->init_offset
+= offset
;
753 * This happens rarely, but it can happen
754 * with bitfields that are all at offset
757 reuse
= alloc_expression(entry
->pos
, EXPR_POS
);
759 reuse
->type
= EXPR_POS
;
760 reuse
->ctype
= entry
->ctype
;
761 reuse
->init_offset
= offset
;
763 reuse
->init_expr
= entry
;
764 REPLACE_CURRENT_PTR(entry
, reuse
);
767 } END_FOR_EACH_PTR(entry
);
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
;
785 r
= (b
->type
!= EXPR_POS
) - (a
->type
!= EXPR_POS
);
788 if (a
->init_offset
< b
->init_offset
)
790 if (a
->init_offset
> b
->init_offset
)
792 /* Check bitfield offset.. */
796 if (a
->ctype
&& b
->ctype
) {
797 if (a
->ctype
->bit_offset
< b
->ctype
->bit_offset
)
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
)
817 switch (expr
->type
) {
824 return expand_symbol_expression(expr
);
826 return expand_binop(expr
);
829 return expand_logical(expr
);
832 return expand_comma(expr
);
835 return expand_compare(expr
);
837 case EXPR_ASSIGNMENT
:
838 return expand_assignment(expr
);
841 return expand_preop(expr
);
844 return expand_postop(expr
);
847 case EXPR_IMPLIED_CAST
:
848 return expand_cast(expr
);
851 return expand_call(expr
);
854 warning(expr
->pos
, "we should not have an EXPR_DEREF left at expansion time");
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
;
873 case EXPR_INITIALIZER
:
874 sort_expression_list(&expr
->expr_list
);
875 return expand_expression_list(expr
->expr_list
);
877 case EXPR_IDENTIFIER
:
884 return expand_expression(expr
->base
) + 1;
887 return expand_pos_expression(expr
);
892 warning(expr
->pos
, "internal front-end error: sizeof in expansion?");
898 static void expand_const_expression(struct expression
*expr
, const char *where
)
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
;
913 base_type
= sym
->ctype
.base_type
;
917 expand_expression(sym
->initializer
);
918 /* expand the body of the symbol */
919 if (base_type
->type
== SYM_FN
) {
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
)
937 expand_expression(expr
);
939 /* This is only valid if nobody jumps into the "dead" side */
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
;
948 stmt
->type
= STMT_NONE
;
951 expand_statement(simple
);
956 expand_statement(stmt
->if_true
);
957 expand_statement(stmt
->if_false
);
962 * Expanding a compound statement is really just
963 * about adding up the costs of each individual
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
)
974 struct statement
*s
, *last
;
975 int cost
, symbols
, statements
;
978 FOR_EACH_PTR(stmt
->syms
, sym
) {
981 } END_FOR_EACH_PTR(sym
);
985 expand_symbol(stmt
->ret
);
991 FOR_EACH_PTR(stmt
->stmts
, s
) {
994 cost
+= expand_statement(s
);
995 } END_FOR_EACH_PTR(s
);
997 if (!symbols
&& statements
== 1)
1003 static int expand_statement(struct statement
*stmt
)
1008 switch (stmt
->type
) {
1010 expand_return_expression(stmt
);
1011 return SIDE_EFFECTS
;
1013 case STMT_EXPRESSION
:
1014 return expand_expression(stmt
->expression
);
1017 return expand_compound(stmt
);
1020 return expand_if_statement(stmt
);
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
;
1031 expand_expression(stmt
->switch_expression
);
1032 expand_statement(stmt
->switch_statement
);
1033 return SIDE_EFFECTS
;
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
;
1042 expand_statement(stmt
->label_statement
);
1043 return SIDE_EFFECTS
;
1046 expand_expression(stmt
->goto_expression
);
1047 return SIDE_EFFECTS
;
1052 /* FIXME! Do the asm parameter evaluation! */
1055 expand_expression(stmt
->expression
);
1058 return SIDE_EFFECTS
;
1061 long long get_expression_value(struct expression
*expr
)
1063 long long value
, mask
;
1064 struct symbol
*ctype
;
1068 ctype
= evaluate_expression(expr
);
1070 warning(expr
->pos
, "bad constant expression type");
1073 expand_expression(expr
);
1074 if (expr
->type
!= EXPR_VALUE
) {
1075 warning(expr
->pos
, "bad constant expression");
1079 value
= expr
->value
;
1080 mask
= 1ULL << (ctype
->bit_size
-1);
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);