@@ -74,6 +74,24 @@ Definedness of the list and list elements is also guaranteed.
7474 [simplification]
7575```
7676
77+ ## Simplifications for ` enum ` Discriminants and Variant Indexes
78+
79+ For symbolic enum values, the variant index remains unevaluated but the original (symbolic) discriminant can be restored:
80+
81+ ``` k
82+ rule #lookupDiscriminant(typeInfoEnumType(_, _, _, _, _), #findVariantIdxAux(DISCR, DISCRS, _IDX)) => DISCR
83+ requires isOneOf(DISCR, DISCRS)
84+ [simplification, preserves-definedness, symbolic(DISCR)]
85+
86+ syntax Bool ::= isOneOf ( Int , Discriminants ) [function, total]
87+ // --------------------------------------------------------------
88+ rule isOneOf( _, .Discriminants ) => false
89+ rule isOneOf( I, discriminant(D) .Discriminants ) => I ==Int D
90+ rule isOneOf( I, discriminant(mirInt(D)) .Discriminants ) => I ==Int D
91+ rule isOneOf( I, discriminant(D) ((discriminant(_) _MORE) #as REST)) => I ==Int D orBool isOneOf(I, REST)
92+ rule isOneOf( I, discriminant(mirInt(D)) ((discriminant(_) _MORE) #as REST)) => I ==Int D orBool isOneOf(I, REST)
93+ ```
94+
7795## Simplifications for Int
7896
7997These are trivial simplifications driven by syntactic equality, which should be present upstream.
@@ -130,6 +148,14 @@ power of two but the semantics will always operate with these particular ones.
130148 rule VAL &Int bitmask128 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask128 [simplification, preserves-definedness, smt-lemma]
131149```
132150
151+ Repeated bit-masking can be simplified in an even more general way:
152+
153+ ``` k
154+ rule (X &Int MASK &Int MASK) => X &Int MASK
155+ requires 0 <Int MASK
156+ [simplification, smt-lemma]
157+ ```
158+
133159Support for ` transmute ` between byte arrays and numbers (and vice-versa) uses computations involving bit masks with 255 and 8-bit shifts.
134160To support simplifying round-trip conversion, the following simplifications are essential.
135161
@@ -165,6 +191,24 @@ To support simplifying round-trip conversion, the following simplifications are
165191 [simplification, preserves-definedness, symbolic(VAL)]
166192```
167193
194+ More generally, a value which is composed of sliced bytes can generally be assumed to be in range of a suitable bitmask for the byte length.
195+ This avoids building up large expressions related to overflow checks and vacuous branches leading to overflow errors.
196+
197+ ``` k
198+ rule ((( _X0 ) &Int 255) +Int 256 *Int (
199+ (( _X1 >>Int 8) &Int 255) +Int 256 *Int (
200+ (( _X2 >>Int 8 >>Int 8) &Int 255) +Int 256 *Int (
201+ (( _X3 >>Int 8 >>Int 8 >>Int 8) &Int 255) +Int 256 *Int (
202+ (( _X4 >>Int 8 >>Int 8 >>Int 8 >>Int 8) &Int 255) +Int 256 *Int (
203+ (( _X5 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8) &Int 255) +Int 256 *Int (
204+ (( _X6 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8) &Int 255) +Int 256 *Int (
205+ (( _X7 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8 >>Int 8) &Int 255)))))))))
206+ <=Int bitmask64
207+ => true
208+ [simplification, preserves-definedness, symbolic]
209+ ```
210+
211+
168212For the case where the (symbolic) byte values are first converted to a number, the round-trip simplification requires different matching.
169213First, the bit-masking with ` &Int 255 ` eliminates ` Bytes2Int(Int2Bytes(1, ..) +Bytes ..) ` enclosing a byte-valued variable:
170214
0 commit comments