Skip to content

Commit c221c81

Browse files
authored
Extend type metadata from stable mir json (#520)
Upgrades the `stable-mir-json` dependency and adjusts parser and execution to use the new `Basetype` and `TypeInfo` sorts. Second change to implement `enum` support will follow this one in a separate PR.
1 parent 94c1c78 commit c221c81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+17616
-16995
lines changed

deps/stable-mir-json

Submodule stable-mir-json updated 28 files

kmir/pyproject.toml

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

55
[tool.poetry]
66
name = "kmir"
7-
version = "0.3.111"
7+
version = "0.3.112"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <[email protected]>",

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.111'
3+
VERSION: Final = '0.3.112'

kmir/src/kmir/kdist/mir-semantics/kmir-ast.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module KMIR-AST
1717
imports TYPES
1818
1919
20-
syntax Pgm ::= Symbol GlobalAllocs FunctionNames MonoItems BaseTypes
20+
syntax Pgm ::= Symbol GlobalAllocs FunctionNames MonoItems TypeMappings
2121
[symbol(pgm), group(mir---name--allocs--functions--items--types)]
2222
2323
syntax FunctionKind ::= functionNormalSym(Symbol) [symbol(FunctionKind::NormalSym), group(mir-enum)]
@@ -29,9 +29,9 @@ module KMIR-AST
2929
3030
syntax FunctionNames ::= List [group(mir-klist-FunctionName)]
3131
32-
syntax BaseType ::= baseType( Ty, TyKind ) [group(mir)]
32+
syntax TypeMapping ::= TypeMapping( Ty, TypeInfo ) [group(mir)]
3333
34-
syntax BaseTypes ::= List{BaseType, ""} [group(mir-list), symbol(BaseTypes::append), terminator-symbol(BaseTypes::empty)]
34+
syntax TypeMappings ::= List{TypeMapping, ""} [group(mir-list), symbol(TypeMappings::append), terminator-symbol(TypeMappings::empty)]
3535
3636
syntax KItem ::= #init( Pgm )
3737

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,32 @@ function map and the initial memory have to be set up.
4444

4545
```k
4646
// #init step, assuming a singleton in the K cell
47-
rule <k> #init(_NAME:Symbol _ALLOCS:GlobalAllocs FUNCTIONS:FunctionNames ITEMS:MonoItems TYPES:BaseTypes)
47+
rule <k> #init(_NAME:Symbol _ALLOCS:GlobalAllocs FUNCTIONS:FunctionNames ITEMS:MonoItems TYPES:TypeMappings)
4848
=>
4949
#execFunction(#findItem(ITEMS, FUNCNAME), FUNCTIONS)
5050
</k>
5151
<functions> _ => #mkFunctionMap(FUNCTIONS, ITEMS) </functions>
5252
<start-symbol> FUNCNAME </start-symbol>
53-
<basetypes> _ => #mkTypeMap(.Map, TYPES) </basetypes>
53+
<types> _ => #mkTypeMap(.Map, TYPES) </types>
5454
```
5555

5656
The `Map` of types is static information used for decoding constants and allocated data into `Value`s.
5757
It maps `Ty` IDs to `RigidTy` that can be supplied to decoding functions.
5858

5959
```k
60-
syntax Map ::= #mkTypeMap ( Map, BaseTypes ) [function, total]
60+
syntax Map ::= #mkTypeMap ( Map, TypeMappings ) [function, total]
6161
62-
rule #mkTypeMap(ACC, .BaseTypes) => ACC
62+
rule #mkTypeMap(ACC, .TypeMappings) => ACC
6363
6464
// build map of Ty -> RigidTy from suitable pairs
65-
rule #mkTypeMap(ACC, baseType(TY, tyKindRigidTy(RIGID)) MORE:BaseTypes)
65+
rule #mkTypeMap(ACC, TypeMapping(TY, TYPEINFO) MORE:TypeMappings)
6666
=>
67-
#mkTypeMap(ACC[TY <- RIGID], MORE)
67+
#mkTypeMap(ACC[TY <- TYPEINFO], MORE)
6868
requires notBool TY in_keys(ACC)
6969
[preserves-definedness] // key collision checked
7070
71-
// skip anything that is not a RigidTy or causes a key collision
72-
rule #mkTypeMap(ACC, baseType(_TY, _OTHERTYKIND) MORE:BaseTypes)
71+
// skip anything that causes a key collision
72+
rule #mkTypeMap(ACC, _OTHERTYKIND:TypeMapping MORE:TypeMappings)
7373
=>
7474
#mkTypeMap(ACC, MORE)
7575
[owise]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module KMIR-CONFIGURATION
5353
// FIXME where do we put the "GlobalAllocs"? in the heap, presumably?
5454
<start-symbol> symbol($STARTSYM:String) </start-symbol>
5555
// static information about the base type interning in the MIR
56-
<basetypes> .Map </basetypes>
56+
<types> .Map </types>
5757
</kmir>
5858
5959
endmodule

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ Constant operands are simply decoded according to their type.
6767
```k
6868
rule <k> operandConstant(constOperand(_, _, mirConst(KIND, TY, _)))
6969
=>
70-
typedValue(#decodeConstant(KIND, {TYPEMAP[TY]}:>RigidTy), TY, mutabilityNot)
70+
typedValue(#decodeConstant(KIND, {TYPEMAP[TY]}:>TypeInfo), TY, mutabilityNot)
7171
...
7272
</k>
73-
<basetypes> TYPEMAP </basetypes>
73+
<types> TYPEMAP </types>
7474
requires TY in_keys(TYPEMAP)
75-
andBool isRigidTy(TYPEMAP[TY])
75+
andBool isTypeInfo(TYPEMAP[TY])
7676
[preserves-definedness] // valid Map lookup checked
7777
```
7878

@@ -687,7 +687,7 @@ rewriting `typedLocal(...) ~> #cast(...) ~> REST` to `typedLocal(...) ~> REST`.
687687
syntax MIRError ::= CastError
688688
689689
syntax CastError ::= UnknownCastTarget ( Ty , Map )
690-
| UnexpectedCastTarget ( CastKind, RigidTy )
690+
| UnexpectedCastTarget ( CastKind, TypeInfo )
691691
| UnexpectedCastArgument ( TypedLocal, CastKind )
692692
| CastNotimplemented ( CastKind )
693693
@@ -703,28 +703,28 @@ bit width, signedness, and possibly truncating or 2s-complementing the value.
703703
// int casts
704704
rule <k> #cast(typedValue(Integer(VAL, WIDTH, _SIGNEDNESS), _, MUT), castKindIntToInt, TY)
705705
=>
706-
typedValue(#intAsType(VAL, WIDTH, #numTypeOf({TYPEMAP[TY]}:>RigidTy)), TY, MUT)
706+
typedValue(#intAsType(VAL, WIDTH, #numTypeOf({TYPEMAP[TY]}:>TypeInfo)), TY, MUT)
707707
...
708708
</k>
709-
<basetypes> TYPEMAP </basetypes>
710-
requires #isIntType({TYPEMAP[TY]}:>RigidTy)
709+
<types> TYPEMAP </types>
710+
requires #isIntType({TYPEMAP[TY]}:>TypeInfo)
711711
[preserves-definedness] // ensures #numTypeOf is defined
712712
```
713713

714714
Error cases for `castKindIntToInt`
715-
* unknown target type (not in `basetypes`)
715+
* unknown target type (not in `types`)
716716
* target type is not an `Int` type
717717
* value is not a `Integer`
718718

719719
```k
720720
rule <k> #cast(_, castKindIntToInt, TY) => UnknownCastTarget(TY, TYPEMAP) ... </k>
721-
<basetypes> TYPEMAP </basetypes>
722-
requires notBool isRigidTy(TYPEMAP[TY])
721+
<types> TYPEMAP </types>
722+
requires notBool isTypeInfo(TYPEMAP[TY])
723723
[preserves-definedness]
724724
725-
rule <k> #cast(_, castKindIntToInt, TY) => UnexpectedCastTarget(castKindIntToInt, {TYPEMAP[TY]}:>RigidTy) ... </k>
726-
<basetypes> TYPEMAP </basetypes>
727-
requires notBool (#isIntType({TYPEMAP[TY]}:>RigidTy))
725+
rule <k> #cast(_, castKindIntToInt, TY) => UnexpectedCastTarget(castKindIntToInt, {TYPEMAP[TY]}:>TypeInfo) ... </k>
726+
<types> TYPEMAP </types>
727+
requires notBool (#isIntType({TYPEMAP[TY]}:>TypeInfo))
728728
[preserves-definedness]
729729
730730
rule <k> #cast(NONINT, castKindIntToInt, _TY) => UnexpectedCastArgument(NONINT, castKindIntToInt) ... </k>
@@ -744,34 +744,31 @@ Other type casts are not implemented yet.
744744

745745
## Decoding constants from their bytes representation to values
746746

747-
The `Value` sort above operates at a higher level than the bytes representation found in the MIR syntax for constant values. The bytes have to be interpreted according to the given `RigidTy` to produce the higher-level value.
747+
The `Value` sort above operates at a higher level than the bytes representation found in the MIR syntax for constant values. The bytes have to be interpreted according to the given `TypeInfo` to produce the higher-level value. This is currently only defined for `PrimitiveType`s (primitive types in MIR).
748748

749749
```k
750-
syntax Value ::= #decodeConstant ( ConstantKind, RigidTy ) [function]
750+
syntax Value ::= #decodeConstant ( ConstantKind, TypeInfo ) [function]
751751
752752
//////////////////////////////////////////////////////////////////////////////////////
753753
// decoding the correct amount of bytes depending on base type size
754754
755755
// Boolean: should be one byte with value one or zero
756-
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), rigidTyBool) => BoolVal(false)
756+
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), typeInfoPrimitiveType(primTypeBool)) => BoolVal(false)
757757
requires 0 ==Int Bytes2Int(BYTES, LE, Unsigned) andBool lengthBytes(BYTES) ==Int 1
758-
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), rigidTyBool) => BoolVal(true)
758+
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), typeInfoPrimitiveType(primTypeBool)) => BoolVal(true)
759759
requires 1 ==Int Bytes2Int(BYTES, LE, Unsigned) andBool lengthBytes(BYTES) ==Int 1
760760
761761
// Integer: handled in separate module for numeric operations
762-
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), RIGIDTY)
762+
rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), TYPEINFO)
763763
=>
764-
#decodeInteger(BYTES, #intTypeOf(RIGIDTY))
765-
requires #isIntType(RIGIDTY)
766-
andBool lengthBytes(BYTES) ==K #bitWidth(#intTypeOf(RIGIDTY)) /Int 8
764+
#decodeInteger(BYTES, #intTypeOf(TYPEINFO))
765+
requires #isIntType(TYPEINFO)
766+
andBool lengthBytes(BYTES) ==K #bitWidth(#intTypeOf(TYPEINFO)) /Int 8
767767
[preserves-definedness]
768768
769769
////////////////////////////////////////////////////////////////////////////////////////////////
770-
// FIXME Char and str types
771-
// rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), rigidTyChar)
772-
// =>
773-
// Str(...)
774-
// rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), rigidTyStr)
770+
// FIXME Char type
771+
// rule #decodeConstant(constantKindAllocated(allocation(BYTES, _, _, _)), typeInfoPrimitiveType(primTypeChar))
775772
// =>
776773
// Str(...)
777774
/////////////////////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ module RT-NUMBERS
2222
2323
syntax InTy ::= IntTy | UintTy
2424
25-
syntax NumTy ::= #numTypeOf( RigidTy ) [function]
25+
syntax NumTy ::= #numTypeOf( TypeInfo ) [function]
2626
// ----------------------------------------------
27-
rule #numTypeOf(rigidTyInt(INTTY)) => INTTY
28-
rule #numTypeOf(rigidTyUint(UINTTY)) => UINTTY
29-
rule #numTypeOf(rigidTyFloat(FLOATTY)) => FLOATTY
27+
rule #numTypeOf(typeInfoPrimitiveType(primTypeInt(INTTY))) => INTTY
28+
rule #numTypeOf(typeInfoPrimitiveType(primTypeUint(UINTTY))) => UINTTY
29+
rule #numTypeOf(typeInfoPrimitiveType(primTypeFloat(FLOATTY))) => FLOATTY
3030
31-
syntax InTy ::= #intTypeOf( RigidTy ) [function]
31+
syntax InTy ::= #intTypeOf( TypeInfo ) [function]
3232
// ----------------------------------------------
33-
rule #intTypeOf(rigidTyInt(INTTY)) => INTTY
34-
rule #intTypeOf(rigidTyUint(UINTTY)) => UINTTY
33+
rule #intTypeOf(typeInfoPrimitiveType(primTypeInt(INTTY))) => INTTY
34+
rule #intTypeOf(typeInfoPrimitiveType(primTypeUint(UINTTY))) => UINTTY
3535
36-
syntax Bool ::= #isIntType ( RigidTy ) [function, total]
36+
syntax Bool ::= #isIntType ( TypeInfo ) [function, total]
3737
// -----------------------------------------------------
38-
rule #isIntType(rigidTyInt(_)) => true
39-
rule #isIntType(rigidTyUint(_)) => true
40-
rule #isIntType(_) => false [owise]
38+
rule #isIntType(typeInfoPrimitiveType(primTypeInt(_))) => true
39+
rule #isIntType(typeInfoPrimitiveType(primTypeUint(_))) => true
40+
rule #isIntType(_) => false [owise]
4141
```
4242

4343
Constants used for overflow-checking and truncation are defined here as macros.

kmir/src/kmir/kdist/mir-semantics/ty.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ syntax ExistentialPredicateBinders ::= List {ExistentialPredicateBinder, ""}
219219
syntax Movability ::= "movabilityStatic" [group(mir-enum), symbol(Movability::Static)]
220220
| "movabilityMovable" [group(mir-enum), symbol(Movability::Movable)]
221221
222+
```
223+
224+
`PrimitiveType`s are not present as an explicit type in MIR or Stable MIR AST, but are a conceptual sub-sort of `RigidTy` for types that are "primitive".
225+
226+
```k
227+
syntax PrimitiveType ::= "primTypeBool" [group(mir-enum), symbol(PrimitiveType::Bool)]
228+
| "primTypeChar" [group(mir-enum), symbol(PrimitiveType::Char)]
229+
| primTypeInt(IntTy) [group(mir-enum), symbol(PrimitiveType::Int)]
230+
| primTypeUint(UintTy) [group(mir-enum), symbol(PrimitiveType::Uint)]
231+
| primTypeFloat(FloatTy) [group(mir-enum), symbol(PrimitiveType::Float)]
232+
222233
syntax RigidTy ::= "rigidTyBool" [group(mir-enum), symbol(RigidTy::Bool)]
223234
| "rigidTyChar" [group(mir-enum), symbol(RigidTy::Char)]
224235
| rigidTyInt(IntTy) [group(mir-enum), symbol(RigidTy::Int)]
@@ -242,6 +253,16 @@ syntax ExistentialPredicateBinders ::= List {ExistentialPredicateBinder, ""}
242253
| rigidTyCoroutineWitness(CoroutineWitnessDef, GenericArgs) [group(mir-enum), symbol(RigidTy::CoroutineWitness)]
243254
| "rigidTyUnimplemented" [group(mir-enum), symbol(RigidTy::Unimplemented), deprecated] // TODO: remove
244255
256+
// additional sort to provide type information in stable-mir-json
257+
syntax TypeInfo ::= typeInfoPrimitiveType(PrimitiveType) [symbol(TypeInfo::PrimitiveType), group(mir-enum)]
258+
| typeInfoEnumType(MIRString, Discriminants) [symbol(TypeInfo::EnumType) , group(mir-enum---name--discriminants)]
259+
| typeInfoStructType(MIRString) [symbol(TypeInfo::StructType) , group(mir-enum---name)]
260+
261+
// discriminant information for enum types
262+
syntax Discriminant ::= Discriminant ( Ty , MIRInt ) [group(mir)]
263+
264+
syntax Discriminants ::= List{Discriminant, ""} [group(mir-list), symbol(Discriminants::append), terminator-symbol(Discriminants::empty)]
265+
245266
syntax TyKind ::= tyKindRigidTy(RigidTy) [group(mir-enum), symbol(TyKind::RigidTy)]
246267
| tyKindAlias(AliasKind, AliasTy) [group(mir-enum), symbol(TyKind::Alias)]
247268
| tyKindParam(ParamTy) [group(mir-enum), symbol(TyKind::Param)]

0 commit comments

Comments
 (0)