Skip to content

Commit 8c41f44

Browse files
committed
Added bitwise operations ^, &, |
1 parent 0bc45b2 commit 8c41f44

File tree

2 files changed

+85
-26
lines changed

2 files changed

+85
-26
lines changed

kmir/src/kmir/kdist/mir-semantics/rt/data.md

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,62 @@ The `unOpNot` operation works on boolean and integral values, with the usual sem
13401340
`binOpShr`
13411341
`binOpShrUnchecked`
13421342

1343+
```k
1344+
syntax Bool ::= isBitwise ( BinOp ) [function, total]
1345+
// --------------------------------------------------
1346+
rule isBitwise(binOpBitXor) => true
1347+
rule isBitwise(binOpBitAnd) => true
1348+
rule isBitwise(binOpBitOr) => true
1349+
rule isBitwise(_) => false [owise]
1350+
1351+
syntax Bool ::= isShift ( BinOp ) [function, total]
1352+
// ------------------------------------------------
1353+
rule isShift(binOpShl) => true
1354+
rule isShift(binOpShlUnchecked) => true
1355+
rule isShift(binOpShr) => true
1356+
rule isShift(binOpShrUnchecked) => true
1357+
rule isShift(_) => false [owise]
1358+
1359+
rule onInt(binOpBitXor, X, Y) => X xorInt Y
1360+
rule onInt(binOpBitAnd, X, Y) => X &Int Y
1361+
rule onInt(binOpBitOr, X, Y) => X |Int Y
1362+
1363+
// Only permitting non-shift bitwise operations on same width integers, overflow check not required
1364+
rule #compute(
1365+
BOP,
1366+
typedValue(Integer(ARG1, WIDTH, _), _, _),
1367+
typedValue(Integer(ARG2, WIDTH, _), _, _),
1368+
false) // unchecked
1369+
=>
1370+
typedValue(
1371+
Integer(onInt(BOP, ARG1, ARG2), WIDTH, false),
1372+
TyUnknown,
1373+
mutabilityNot
1374+
)
1375+
requires isBitwise(BOP)
1376+
[preserves-definedness]
1377+
1378+
rule #compute(
1379+
BOP,
1380+
typedValue(Ptr(_ARG1, _, _), _, _),
1381+
typedValue(Integer(_ARG2, WIDTH, _), _, _),
1382+
false) // unchecked
1383+
=>
1384+
typedValue(
1385+
Integer(4242, WIDTH, false),
1386+
TyUnknown,
1387+
mutabilityNot
1388+
)
1389+
// typedValue(
1390+
// Integer(onInt(BOP, ARG1, ARG2), WIDTH, false),
1391+
// TyUnknown,
1392+
// mutabilityNot
1393+
// )
1394+
requires isBitwise(BOP)
1395+
[preserves-definedness]
1396+
1397+
```
1398+
13431399

13441400
#### Nullary operations for activating certain checks
13451401

@@ -1358,32 +1414,6 @@ One important use case of `UbChecks` is to determine overflows in unchecked arit
13581414
`nullOpOffsetOf(VariantAndFieldIndices)`
13591415

13601416
```k
1361-
1362-
// FIXME: Alignment is platform specific
1363-
syntax Int ::= #alignOf( TypeInfo ) [function]
1364-
1365-
rule #alignOf( typeInfoPrimitiveType(primTypeBool) ) => 1
1366-
rule #alignOf( typeInfoPrimitiveType(primTypeChar) ) => 4
1367-
1368-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyIsize)) ) => 8 // FIXME: Hard coded since usize not implemented
1369-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI8)) ) => 1
1370-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI16)) ) => 2
1371-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI32)) ) => 4
1372-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI64)) ) => 8
1373-
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI128)) ) => 16
1374-
1375-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyUsize)) ) => 8 // FIXME: Hard coded since usize not implemented
1376-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU8)) ) => 1
1377-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU16)) ) => 2
1378-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU32)) ) => 4
1379-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU64)) ) => 8
1380-
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU128)) ) => 16
1381-
1382-
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF16)) ) => 2
1383-
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF32)) ) => 4
1384-
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF64)) ) => 8
1385-
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF128)) ) => 16
1386-
13871417
rule <k> rvalueNullaryOp(nullOpAlignOf, TY) => typedValue( Integer(#alignOf({TYPEMAP[TY]}:>TypeInfo), 64, false), TyUnknown, mutabilityNot ) ... </k> // FIXME: 64 is hardcoded since usize not supported
13881418
<types> TYPEMAP </types>
13891419
requires TY in_keys(TYPEMAP)

kmir/src/kmir/kdist/mir-semantics/rt/numbers.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,35 @@ This truncation function is instrumental in the implementation of Integer arithm
153153
[preserves-definedness] // positive shift, divisor non-zero
154154
```
155155

156+
## Alignment of Primitives
157+
158+
```k
159+
// FIXME: Alignment is platform specific
160+
syntax Int ::= #alignOf( TypeInfo ) [function]
161+
162+
rule #alignOf( typeInfoPrimitiveType(primTypeBool) ) => 1
163+
rule #alignOf( typeInfoPrimitiveType(primTypeChar) ) => 4
164+
165+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyIsize)) ) => 8 // FIXME: Hard coded since usize not implemented
166+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI8)) ) => 1
167+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI16)) ) => 2
168+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI32)) ) => 4
169+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI64)) ) => 8
170+
rule #alignOf( typeInfoPrimitiveType(primTypeInt(intTyI128)) ) => 16
171+
172+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyUsize)) ) => 8 // FIXME: Hard coded since usize not implemented
173+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU8)) ) => 1
174+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU16)) ) => 2
175+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU32)) ) => 4
176+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU64)) ) => 8
177+
rule #alignOf( typeInfoPrimitiveType(primTypeUint(uintTyU128)) ) => 16
178+
179+
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF16)) ) => 2
180+
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF32)) ) => 4
181+
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF64)) ) => 8
182+
rule #alignOf( typeInfoPrimitiveType(primTypeFloat(floatTyF128)) ) => 16
183+
```
184+
156185
```k
157186
endmodule
158187
```

0 commit comments

Comments
 (0)