@@ -88,6 +88,15 @@ Pointers to structs with a single zero-offset field are compatible with pointers
8888 rule #layoutOffsets(_) => .MachineSizes [owise]
8989```
9090
91+ Helper function to identify an ` union ` type, this is needed so ` #setLocalValue `
92+ will not create an ` Aggregate ` instead of a ` Union ` ` Value ` .
93+ ``` k
94+ syntax Bool ::= #isUnionType ( TypeInfo ) [function, total]
95+ // --------------------------------------------------------
96+ rule #isUnionType(typeInfoUnionType(_NAME, _ADTDEF, _FIELDS, _LAYOUT) ) => true
97+ rule #isUnionType(_) => false [owise]
98+ ```
99+
91100## Determining types of places with projection
92101
93102A helper function ` getTyOf ` traverses type metadata (using the type metadata map ` Ty -> TypeInfo ` ) along the applied projections to determine the ` Ty ` of the projected place.
@@ -203,6 +212,72 @@ Slices, `str`s and dynamic types require it, and any `Ty` that `is_sized` does
203212 rule #zeroSizedType(_) => false [owise]
204213```
205214
215+ ## Alignment and Size of Types as per ` TypeInfo `
216+
217+ The ` alignOf ` and ` sizeOf ` nullary operations return the alignment / size in bytes as a ` usize ` .
218+ This information is either hard-wired for primitive types (numbers, first and foremost), or read from the layout in ` TypeInfo ` .
219+
220+ ``` k
221+ syntax Int ::= #sizeOf ( TypeInfo ) [function, total]
222+ | #alignOf ( TypeInfo ) [function, total]
223+
224+ // primitive int types: use bit width (both for size and alignment)
225+ rule #sizeOf(typeInfoPrimitiveType(primTypeInt(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
226+ rule #alignOf(typeInfoPrimitiveType(primTypeInt(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
227+ rule #sizeOf(typeInfoPrimitiveType(primTypeUint(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
228+ rule #alignOf(typeInfoPrimitiveType(primTypeUint(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
229+ rule #sizeOf(typeInfoPrimitiveType(primTypeFloat(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
230+ rule #alignOf(typeInfoPrimitiveType(primTypeFloat(NUMTY))) => #bitWidth(NUMTY) /Int 8 [preserves-definedness]
231+ // bool and char
232+ rule #sizeOf(typeInfoPrimitiveType(primTypeBool)) => 1
233+ rule #alignOf(typeInfoPrimitiveType(primTypeBool)) => 1
234+ rule #sizeOf(typeInfoPrimitiveType(primTypeChar)) => 4
235+ rule #alignOf(typeInfoPrimitiveType(primTypeChar)) => 4
236+ // The str primitive has alignment of a Char but size 0 (indicating dynamic size)
237+ rule #sizeOf(typeInfoPrimitiveType(primTypeStr)) => 0
238+ rule #alignOf(typeInfoPrimitiveType(primTypeStr)) => 4
239+ // enums, structs , and tuples provide the values from their layout information
240+ rule #sizeOf(typeInfoEnumType(_, _, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize( BITS ))))) => BITS /Int 8 [preserves-definedness]
241+ rule #sizeOf(typeInfoEnumType(_, _, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize(mirInt(BITS)))))) => BITS /Int 8 [preserves-definedness]
242+ rule #sizeOf(typeInfoEnumType(_, _, _, _, noLayoutShape)) => 0
243+ rule #alignOf(typeInfoEnumType(_, _, _, _, someLayoutShape(layoutShape(_, _, _, align(BYTES),_)))) => BYTES
244+ rule #alignOf(typeInfoEnumType(_, _, _, _, noLayoutShape)) => 1
245+ // struct
246+ rule #sizeOf(typeInfoStructType(_, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize( BITS ))))) => BITS /Int 8 [preserves-definedness]
247+ rule #sizeOf(typeInfoStructType(_, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize(mirInt(BITS)))))) => BITS /Int 8 [preserves-definedness]
248+ rule #sizeOf(typeInfoStructType(_, _, _, noLayoutShape)) => 0
249+ rule #alignOf(typeInfoStructType(_, _, _, someLayoutShape(layoutShape(_, _, _, align(BYTES),_)))) => BYTES
250+ rule #alignOf(typeInfoStructType(_, _, _, noLayoutShape)) => 1
251+ // tuple
252+ rule #sizeOf(typeInfoTupleType(_, someLayoutShape(layoutShape(_, _, _, _, machineSize( BITS ))))) => BITS /Int 8 [preserves-definedness]
253+ rule #sizeOf(typeInfoTupleType(_, someLayoutShape(layoutShape(_, _, _, _, machineSize(mirInt(BITS)))))) => BITS /Int 8 [preserves-definedness]
254+ rule #sizeOf(typeInfoTupleType(_, noLayoutShape)) => 0
255+ rule #alignOf(typeInfoTupleType(_, someLayoutShape(layoutShape(_, _, _, align(BYTES),_)))) => BYTES
256+ rule #alignOf(typeInfoTupleType(_, noLayoutShape)) => 1
257+ // union
258+ rule #sizeOf(typeInfoUnionType(_, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize( BITS ))))) => BITS /Int 8 [preserves-definedness]
259+ rule #sizeOf(typeInfoUnionType(_, _, _, someLayoutShape(layoutShape(_, _, _, _, machineSize(mirInt(BITS)))))) => BITS /Int 8 [preserves-definedness]
260+ rule #sizeOf(typeInfoUnionType(_, _, _, noLayoutShape)) => 0
261+ rule #alignOf(typeInfoUnionType(_, _, _, someLayoutShape(layoutShape(_, _, _, align(BYTES),_)))) => BYTES
262+ rule #alignOf(typeInfoUnionType(_, _, _, noLayoutShape)) => 1
263+ // arrays with known length have the alignment of the element type, and a size multiplying element count and element size
264+ rule #sizeOf(typeInfoArrayType(ELEM_TY, someTyConst(tyConst(KIND, _)))) => #sizeOf(lookupTy(ELEM_TY)) *Int readTyConstInt(KIND)
265+ rule #sizeOf(typeInfoArrayType( _ , noTyConst )) => 0
266+ rule #alignOf(typeInfoArrayType(ELEM_TY, _)) => #alignOf(lookupTy(ELEM_TY))
267+ // thin ptr and ref types have the size of `usize` and twice that for fat pointers/refs. Alignment is that of `usize`
268+ rule #sizeOf(typeInfoPtrType(POINTEE_TY))
269+ => #sizeOf(typeInfoPrimitiveType(primTypeUint(uintTyUsize)))
270+ *Int (#if #metadataSize(POINTEE_TY) ==K dynamicSize(1) #then 2 #else 1 #fi)
271+ rule #sizeOf(typeInfoRefType(POINTEE_TY))
272+ => #sizeOf(typeInfoPrimitiveType(primTypeUint(uintTyUsize)))
273+ *Int (#if #metadataSize(POINTEE_TY) ==K dynamicSize(1) #then 2 #else 1 #fi)
274+ rule #alignOf(typeInfoPtrType(_)) => #alignOf(typeInfoPrimitiveType(primTypeUint(uintTyUsize)))
275+ rule #alignOf(typeInfoRefType(_)) => #alignOf(typeInfoPrimitiveType(primTypeUint(uintTyUsize)))
276+ // other types (fun and void types) have size and alignment 0
277+ rule #sizeOf(_) => 0 [owise]
278+ rule #alignOf(_) => 0 [owise]
279+ ```
280+
206281``` k
207282endmodule
208283```
0 commit comments