Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions kmir/src/kmir/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
UintT,
)
from .value import (
NO_METADATA,
NO_SIZE,
AggregateValue,
AllocRefValue,
BoolValue,
DynamicSize,
IntValue,
Metadata,
RangeValue,
StaticSize,
StrValue,
Expand Down Expand Up @@ -120,18 +121,30 @@ def _decode_memory_alloc_or_unable(
except KeyError:
return UnableToDecodeValue(f'Unknown pointee type: {pointee_ty}')

metadata = _metadata(pointee_type_info)
metadata_size = _metadata_size(pointee_type_info)

if len(data) == 8:
# single slim pointer (assumes usize == u64)
return AllocRefValue(alloc_id=alloc_id, metadata=metadata)
return AllocRefValue(
alloc_id=alloc_id,
metadata=Metadata(
size=metadata_size,
pointer_offset=0,
origin_size=metadata_size,
),
Comment on lines +130 to +134
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this assumption based on the previous version of AllocRevValue.to_kast(), I don't know if it makes sense though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense, when we decode we won't ever have an offset, and the origin_size should be the same as the size field IIUC.

)

if len(data) == 16 and metadata == DynamicSize(1):
if len(data) == 16 and metadata_size == DynamicSize(1):
# sufficient data to decode dynamic size (assumes usize == u64)
# expect fat pointer
actual_size = DynamicSize(int.from_bytes(data[8:16], byteorder='little', signed=False))
return AllocRefValue(
alloc_id=alloc_id,
metadata=DynamicSize(int.from_bytes(data[8:16], byteorder='little', signed=False)),
metadata=Metadata(
size=actual_size,
pointer_offset=0,
origin_size=actual_size,
),
Comment on lines +143 to +147
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

)

return UnableToDecodeValue(f'Unable to decode alloc: {data!r}, of type: {type_info}')
Expand All @@ -145,14 +158,14 @@ def _pointee_ty(type_info: TypeMetadata) -> Ty | None:
return None


def _metadata(type_info: TypeMetadata) -> MetadataSize:
def _metadata_size(type_info: TypeMetadata) -> MetadataSize:
match type_info:
case ArrayT(length=None):
return DynamicSize(1) # 1 is a placeholder, the actual size is inferred from the slice data
case ArrayT(length=int() as length):
return StaticSize(length)
case _:
return NO_METADATA
return NO_SIZE


def decode_value_or_unable(data: bytes, type_info: TypeMetadata, types: Mapping[Ty, TypeMetadata]) -> Value:
Expand Down
32 changes: 20 additions & 12 deletions kmir/src/kmir/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,29 @@ def to_kast(self) -> KInner:
class AllocRefValue(Value):
alloc_id: AllocId
# projection_elems: tuple[ProjectionElem, ...]
metadata: MetadataSize
metadata: Metadata

def to_kast(self) -> KInner:
return KApply(
'Value::AllocRef',
KApply('allocId', intToken(self.alloc_id)),
KApply('ProjectionElems::empty'), # TODO
KApply(
'Metadata',
(
self.metadata.to_kast(),
intToken(0),
self.metadata.to_kast(),
),
),
self.metadata.to_kast(),
)


@dataclass
class Metadata:
size: MetadataSize
pointer_offset: int
origin_size: MetadataSize

def to_kast(self) -> KInner:
return KApply(
'Metadata',
self.size.to_kast(),
intToken(self.pointer_offset),
self.origin_size.to_kast(),
)


Expand All @@ -114,12 +122,12 @@ def to_kast(self) -> KInner: ...


@dataclass
class NoMetadata(MetadataSize):
class NoSize(MetadataSize):
def to_kast(self) -> KInner:
return KApply('noMetadata')
return KApply('noMetadataSize')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for changing this! I encountered a problem when running spl-token proof because of this.



NO_METADATA: Final = NoMetadata()
NO_SIZE: Final = NoSize()


@dataclass
Expand Down