Skip to content

Commit 89d4136

Browse files
committed
Fix AllocRefValue
1 parent c8d9260 commit 89d4136

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

kmir/src/kmir/decoding.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
UintT,
2626
)
2727
from .value import (
28-
NO_METADATA,
28+
NO_SIZE,
2929
AggregateValue,
3030
AllocRefValue,
3131
BoolValue,
3232
DynamicSize,
3333
IntValue,
34+
Metadata,
3435
RangeValue,
3536
StaticSize,
3637
StrValue,
@@ -120,18 +121,30 @@ def _decode_memory_alloc_or_unable(
120121
except KeyError:
121122
return UnableToDecodeValue(f'Unknown pointee type: {pointee_ty}')
122123

123-
metadata = _metadata(pointee_type_info)
124+
metadata_size = _metadata_size(pointee_type_info)
124125

125126
if len(data) == 8:
126127
# single slim pointer (assumes usize == u64)
127-
return AllocRefValue(alloc_id=alloc_id, metadata=metadata)
128+
return AllocRefValue(
129+
alloc_id=alloc_id,
130+
metadata=Metadata(
131+
size=metadata_size,
132+
pointer_offset=0,
133+
origin_size=metadata_size,
134+
),
135+
)
128136

129-
if len(data) == 16 and metadata == DynamicSize(1):
137+
if len(data) == 16 and metadata_size == DynamicSize(1):
130138
# sufficient data to decode dynamic size (assumes usize == u64)
131139
# expect fat pointer
140+
actual_size = DynamicSize(int.from_bytes(data[8:16], byteorder='little', signed=False))
132141
return AllocRefValue(
133142
alloc_id=alloc_id,
134-
metadata=DynamicSize(int.from_bytes(data[8:16], byteorder='little', signed=False)),
143+
metadata=Metadata(
144+
size=actual_size,
145+
pointer_offset=0,
146+
origin_size=actual_size,
147+
),
135148
)
136149

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

147160

148-
def _metadata(type_info: TypeMetadata) -> MetadataSize:
161+
def _metadata_size(type_info: TypeMetadata) -> MetadataSize:
149162
match type_info:
150163
case ArrayT(length=None):
151164
return DynamicSize(1) # 1 is a placeholder, the actual size is inferred from the slice data
152165
case ArrayT(length=int() as length):
153166
return StaticSize(length)
154167
case _:
155-
return NO_METADATA
168+
return NO_SIZE
156169

157170

158171
def decode_value_or_unable(data: bytes, type_info: TypeMetadata, types: Mapping[Ty, TypeMetadata]) -> Value:

kmir/src/kmir/value.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,29 @@ def to_kast(self) -> KInner:
9090
class AllocRefValue(Value):
9191
alloc_id: AllocId
9292
# projection_elems: tuple[ProjectionElem, ...]
93-
metadata: MetadataSize
93+
metadata: Metadata
9494

9595
def to_kast(self) -> KInner:
9696
return KApply(
9797
'Value::AllocRef',
9898
KApply('allocId', intToken(self.alloc_id)),
9999
KApply('ProjectionElems::empty'), # TODO
100-
KApply(
101-
'Metadata',
102-
(
103-
self.metadata.to_kast(),
104-
intToken(0),
105-
self.metadata.to_kast(),
106-
),
107-
),
100+
self.metadata.to_kast(),
101+
)
102+
103+
104+
@dataclass
105+
class Metadata:
106+
size: MetadataSize
107+
pointer_offset: int
108+
origin_size: MetadataSize
109+
110+
def to_kast(self) -> KInner:
111+
return KApply(
112+
'Metadata',
113+
self.size.to_kast(),
114+
intToken(self.pointer_offset),
115+
self.origin_size.to_kast(),
108116
)
109117

110118

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

115123

116124
@dataclass
117-
class NoMetadata(MetadataSize):
125+
class NoSize(MetadataSize):
118126
def to_kast(self) -> KInner:
119-
return KApply('noMetadata')
127+
return KApply('noMetadataSize')
120128

121129

122-
NO_METADATA: Final = NoMetadata()
130+
NO_SIZE: Final = NoSize()
123131

124132

125133
@dataclass

0 commit comments

Comments
 (0)