Skip to content

Commit a8e2515

Browse files
committed
more fixed list implementation
1 parent fb3080b commit a8e2515

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

crates/core/src/abi.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,17 @@ impl<'a, B: Bindgen> Generator<'a, B> {
13711371
});
13721372
}
13731373
TypeDefKind::Unknown => unreachable!(),
1374-
TypeDefKind::FixedSizeList(_, _) => todo!(),
1374+
TypeDefKind::FixedSizeList(ty, size) => {
1375+
self.emit(&FixedSizeListLower { elements: ty, size: *size, id });
1376+
let mut values = self
1377+
.stack
1378+
.drain(self.stack.len() - (*size as usize)..)
1379+
.collect::<Vec<_>>();
1380+
for value in values.drain(..) {
1381+
self.stack.push(value);
1382+
self.lower(ty);
1383+
}
1384+
}
13751385
},
13761386
}
13771387
}
@@ -1740,7 +1750,15 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17401750
}
17411751

17421752
TypeDefKind::Unknown => unreachable!(),
1743-
TypeDefKind::FixedSizeList(_, _) => todo!(),
1753+
TypeDefKind::FixedSizeList(ty, size) => {
1754+
let increment = self.bindgen.sizes().size(ty);
1755+
let mut position = offset;
1756+
for _ in 0..*size {
1757+
self.write_to_memory(ty, addr.clone(), position);
1758+
position = position + increment;
1759+
}
1760+
self.emit(&FixedSizeListLower { elements: ty, size: *size, id });
1761+
}
17441762
},
17451763
}
17461764
}
@@ -1927,7 +1945,15 @@ impl<'a, B: Bindgen> Generator<'a, B> {
19271945
}
19281946

19291947
TypeDefKind::Unknown => unreachable!(),
1930-
TypeDefKind::FixedSizeList(_, _) => todo!(),
1948+
TypeDefKind::FixedSizeList(ty, size) => {
1949+
let increment = self.bindgen.sizes().size(ty);
1950+
let mut position = offset;
1951+
for _ in 0..*size {
1952+
self.read_from_memory(ty, addr.clone(), position);
1953+
position = position + increment;
1954+
}
1955+
self.emit(&FixedSizeListLift { elements: ty, size: *size, id });
1956+
}
19311957
},
19321958
}
19331959
}
@@ -2110,7 +2136,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
21102136
TypeDefKind::Future(_) => todo!("read future from memory"),
21112137
TypeDefKind::Stream(_) => todo!("read stream from memory"),
21122138
TypeDefKind::Unknown => unreachable!(),
2113-
TypeDefKind::FixedSizeList(_, _) => todo!(),
2139+
TypeDefKind::FixedSizeList(_, _) => {}
21142140
},
21152141
}
21162142
}

crates/rust/src/bindgen.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,8 +1202,24 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12021202
align = align.format(POINTER_SIZE_EXPRESSION)
12031203
));
12041204
}
1205-
Instruction::FixedSizeListLift { .. } => todo!(),
1206-
Instruction::FixedSizeListLower { .. } => todo!(),
1205+
Instruction::FixedSizeListLift { elements: _, size, id: _ } => {
1206+
let tmp = self.tmp();
1207+
let result = format!("result{tmp}");
1208+
self.push_str(&format!(
1209+
"let mut {result} = [",
1210+
));
1211+
for a in operands.drain(0..(*size as usize)) {
1212+
self.push_str(&a);
1213+
self.push_str(", ");
1214+
}
1215+
self.push_str("];\n");
1216+
results.push(result);
1217+
}
1218+
Instruction::FixedSizeListLower { elements: _, size, id: _ } => {
1219+
for i in 0..(*size as usize) {
1220+
results.push(format!("{}[{i}]", operands[0]));
1221+
}
1222+
}
12071223
}
12081224
}
12091225
}

crates/rust/src/interface.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,9 @@ impl<'a, 'b> wit_bindgen_core::AnonymousTypeGenerator<'a> for AnonTypeGenerator<
29332933
self.interface.push_str(">");
29342934
}
29352935

2936-
fn anonymous_type_fixed_size_list(&mut self, _id: TypeId, _ty: &Type, _size: u32, _docs: &Docs) {
2937-
todo!()
2936+
fn anonymous_type_fixed_size_list(&mut self, _id: TypeId, ty: &Type, size: u32, _docs: &Docs) {
2937+
self.interface.push_str("[");
2938+
self.interface.print_ty(ty, TypeMode{ lifetime: None, lists_borrowed: false, style: TypeOwnershipStyle::Owned });
2939+
self.interface.push_str(&format!(", {size}]"));
29382940
}
29392941
}

tests/runtime/fixed-size-list/test.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test:lists;
33
interface to-test {
44
list-param: func(a: list<u32, 4>);
55
list-param2: func(a: list<list<u32, 2>, 2>);
6+
list-param3: func(a: list<s32, 20>);
67
list-result: func() -> list<u8, 8>;
78

89
list-minmax16: func(a: list<u16, 4>, b: list<s16, 4>) -> tuple<list<u16, 4>, list<s16, 4>>;

0 commit comments

Comments
 (0)