Skip to content

Commit 52f39af

Browse files
committed
size_of_val intrinsic implementation (sized)
1 parent d1c52ad commit 52f39af

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,28 @@ the second argument, so the returned difference is always positive.
319319
[priority(100)]
320320
```
321321

322+
#### Size of a value (`std::intrinsics::size_of_val`)
323+
324+
`size_of_val` returns the size in bytes of the value its argument points to. The pointee type is
325+
computed from the argument's type and its size from the existing `#sizeOf`.
326+
327+
Only statically-sized pointees are handled here: the rule requires the pointee not to be a
328+
dynamically-sized type (`#metadataSize =/=K dynamicSize(1)`). Dynamically-sized pointees (slices,
329+
`str`, `dyn`) need the runtime metadata carried by the fat pointer and are left for later (the
330+
intrinsic stays stuck on them rather than returning a wrong size).
331+
332+
```k
333+
rule <k> #execIntrinsic(IntrinsicFunction(symbol("size_of_val")), ARG:Operand .Operands, DEST, _SPAN)
334+
=> #setLocalValue(DEST, Integer(#sizeOf(TYPESMAP, lookupTy(TYPESMAP, {pointeeTy(lookupTy(TYPESMAP, {#extractOperandType(TYPESMAP, ARG, LOCALS)}:>Ty))}:>Ty)), 64, false))
335+
... </k>
336+
<locals> LOCALS </locals>
337+
<types> TYPESMAP </types>
338+
requires isTy(#extractOperandType(TYPESMAP, ARG, LOCALS))
339+
andBool isTy(pointeeTy(lookupTy(TYPESMAP, {#extractOperandType(TYPESMAP, ARG, LOCALS)}:>Ty)))
340+
andBool #metadataSize(TYPESMAP, pointeeTy(lookupTy(TYPESMAP, {#extractOperandType(TYPESMAP, ARG, LOCALS)}:>Ty))) =/=K dynamicSize(1)
341+
[preserves-definedness]
342+
```
343+
322344
```k
323345
endmodule
324346
```

0 commit comments

Comments
 (0)