Skip to content

Commit 226e29d

Browse files
Code to write to array indexes (#552)
This completes the array support for kmir. Closes #487 --------- Co-authored-by: devops <devops@runtimeverification.com>
1 parent 5c284d9 commit 226e29d

10 files changed

Lines changed: 3024 additions & 5 deletions

File tree

kmir/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "kmir"
7-
version = "0.3.137"
7+
version = "0.3.138"
88
description = ""
99
requires-python = "~=3.10"
1010
dependencies = [

kmir/src/kmir/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from typing import Final
22

3-
VERSION: Final = '0.3.137'
3+
VERSION: Final = '0.3.138'

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ The solution is to use rewrite operations in a downward pass through the project
427427
428428
// retains information about the value that was deconstructed by a projection
429429
syntax Context ::= CtxField( Ty, VariantIdx, List, Int )
430-
// | array context will be added here
430+
| CtxIndex( Ty, List , Int ) // array index constant or has been read before
431431
432432
syntax Contexts ::= List{Context, ""}
433433
@@ -440,6 +440,10 @@ The solution is to use rewrite operations in a downward pass through the project
440440
=> #buildUpdate(typedValue(Aggregate(IDX, ARGS[I <- VAL]), TY, mutabilityMut), CTXS)
441441
[preserves-definedness] // valid list indexing checked upon context construction
442442
443+
rule #buildUpdate(VAL, CtxIndex(TY, ELEMS, I) CTXS)
444+
=> #buildUpdate(typedValue(Range(ELEMS[I <- VAL]), TY, mutabilityMut), CTXS)
445+
[preserves-definedness] // valid list indexing checked upon context construction
446+
443447
rule <k> #projectedUpdate(
444448
DEST,
445449
typedValue(Aggregate(IDX, ARGS), TY, MUT),
@@ -457,6 +461,84 @@ The solution is to use rewrite operations in a downward pass through the project
457461
andBool (FORCE orBool MUT ==K mutabilityMut)
458462
[preserves-definedness] // valid list indexing checked
459463
464+
rule <k> #projectedUpdate(
465+
DEST,
466+
typedValue(Range(ELEMENTS), TY, MUT),
467+
projectionElemIndex(local(LOCAL)) PROJS,
468+
UPDATE,
469+
CTXTS,
470+
FORCE
471+
)
472+
=>
473+
#projectedUpdate(
474+
DEST,
475+
{ELEMENTS[#expectUsize({LOCALS[LOCAL]}:>TypedValue)]}:>TypedValue,
476+
PROJS,
477+
UPDATE,
478+
CtxIndex(TY, ELEMENTS, #expectUsize({LOCALS[LOCAL]}:>TypedValue)) CTXTS,
479+
FORCE)
480+
...
481+
</k>
482+
<locals> LOCALS </locals>
483+
requires 0 <=Int LOCAL
484+
andBool LOCAL <Int size(LOCALS)
485+
andBool isTypedValue(LOCALS[LOCAL])
486+
andBool isInt(#expectUsize({LOCALS[LOCAL]}:>TypedValue))
487+
andBool 0 <=Int #expectUsize({LOCALS[LOCAL]}:>TypedValue)
488+
andBool #expectUsize({LOCALS[LOCAL]}:>TypedValue) <Int size(ELEMENTS)
489+
andBool isTypedValue(ELEMENTS[#expectUsize({LOCALS[LOCAL]}:>TypedValue)])
490+
andBool (FORCE orBool MUT ==K mutabilityMut)
491+
[preserves-definedness] // index checked, valid Int can be read, ELEMENT indexable and writeable or forced
492+
493+
rule <k> #projectedUpdate(
494+
DEST,
495+
typedValue(Range(ELEMENTS), TY, MUT),
496+
projectionElemConstantIndex(OFFSET:Int, _MINLEN, false) PROJS,
497+
UPDATE,
498+
CTXTS,
499+
FORCE
500+
)
501+
=>
502+
#projectedUpdate(
503+
DEST,
504+
{ELEMENTS[OFFSET]}:>TypedValue,
505+
PROJS,
506+
UPDATE,
507+
CtxIndex(TY, ELEMENTS, OFFSET) CTXTS,
508+
FORCE)
509+
...
510+
</k>
511+
requires 0 <=Int OFFSET
512+
andBool OFFSET <Int size(ELEMENTS)
513+
andBool isTypedValue(ELEMENTS[OFFSET])
514+
andBool (FORCE orBool MUT ==K mutabilityMut)
515+
[preserves-definedness] // ELEMENT indexable and writeable or forced
516+
517+
rule <k> #projectedUpdate(
518+
DEST,
519+
typedValue(Range(ELEMENTS), TY, MUT),
520+
projectionElemConstantIndex(OFFSET:Int, MINLEN, true) PROJS, // from end
521+
UPDATE,
522+
CTXTS,
523+
FORCE
524+
)
525+
=>
526+
#projectedUpdate(
527+
DEST,
528+
{ELEMENTS[OFFSET]}:>TypedValue,
529+
PROJS,
530+
UPDATE,
531+
CtxIndex(TY, ELEMENTS, MINLEN -Int OFFSET) CTXTS,
532+
FORCE)
533+
...
534+
</k>
535+
requires 0 <Int OFFSET
536+
andBool OFFSET <=Int MINLEN
537+
andBool MINLEN ==Int size(ELEMENTS) // assumed for valid MIR code
538+
andBool isTypedValue(ELEMENTS[MINLEN -Int OFFSET])
539+
andBool (FORCE orBool MUT ==K mutabilityMut)
540+
[preserves-definedness] // ELEMENT indexable and writeable or forced
541+
460542
rule <k> #projectedUpdate(
461543
_DEST,
462544
typedValue(Reference(OFFSET, place(LOCAL, PLACEPROJ), MUT), _, _),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
3+
let mut a: [i16; 4] = [1;4];
4+
5+
a[0] = 2;
6+
7+
let b = &mut (a[1]);
8+
9+
*b = 2;
10+
11+
assert!(a[0] == a[1]);
12+
}

0 commit comments

Comments
 (0)