Skip to content

Commit 6cbbad6

Browse files
committed
returning a fixed size list works
1 parent 0d57981 commit 6cbbad6

File tree

7 files changed

+79
-24
lines changed

7 files changed

+79
-24
lines changed

crates/core/src/abi.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,25 @@ def_instruction! {
310310
/// Pops all fields for a fixed list off the stack and then composes them
311311
/// into an array.
312312
FixedSizeListLift {
313-
elements: &'a Type,
313+
element: &'a Type,
314314
size: u32,
315315
id: TypeId,
316316
} : [*size as usize] => [1],
317317

318318
/// Pops an array off the stack, decomposes the elements and then pushes them onto the stack.
319319
FixedSizeListLower {
320-
elements: &'a Type,
320+
element: &'a Type,
321321
size: u32,
322322
id: TypeId,
323323
} : [1] => [*size as usize],
324324

325+
/// Pops an array and an address off the stack, passes each element to a block
326+
FixedSizeListLowerBlock {
327+
element: &'a Type,
328+
size: u32,
329+
id: TypeId,
330+
} : [2] => [0],
331+
325332
/// Pushes an operand onto the stack representing the list item from
326333
/// each iteration of the list.
327334
///
@@ -1403,7 +1410,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
14031410
TypeDefKind::Unknown => unreachable!(),
14041411
TypeDefKind::FixedSizeList(ty, size) => {
14051412
self.emit(&FixedSizeListLower {
1406-
elements: ty,
1413+
element: ty,
14071414
size: *size,
14081415
id,
14091416
});
@@ -1616,7 +1623,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
16161623
self.lift(ty);
16171624
}
16181625
self.emit(&FixedSizeListLift {
1619-
elements: ty,
1626+
element: ty,
16201627
size: *size,
16211628
id,
16221629
});
@@ -1778,7 +1785,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17781785
}
17791786

17801787
TypeDefKind::Unknown => unreachable!(),
1781-
TypeDefKind::FixedSizeList(ty, size) => {
1788+
TypeDefKind::FixedSizeList(element, size) => {
17821789
// let increment = self.bindgen.sizes().size(ty);
17831790
// let mut position = offset;
17841791
// let resultvar = self.stack[0];
@@ -1788,11 +1795,29 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17881795
// self.write_to_memory(ty, addr.clone(), position);
17891796
// position = position + increment;
17901797
// }
1791-
self.emit(&FixedSizeListLower {
1792-
elements: ty,
1793-
size: *size,
1794-
id,
1795-
});
1798+
// @@@@
1799+
// self.emit(&FixedSizeListLower {
1800+
// elements: ty,
1801+
// size: *size,
1802+
// id,
1803+
// });
1804+
1805+
// resembles write_list_to_memory
1806+
self.push_block();
1807+
self.emit(&IterElem { element });
1808+
self.emit(&IterBasePointer);
1809+
let elem_addr = self.stack.pop().unwrap();
1810+
self.write_to_memory(element, elem_addr, Default::default());
1811+
self.finish_block(0);
1812+
// let target = self.stack.pop().unwrap();
1813+
self.stack.push(addr);
1814+
self.emit(&FixedSizeListLowerBlock { element, size: *size, id });
1815+
1816+
// for idx in 0..*size {
1817+
// //self.write_fields_to_memory(tuple.types.iter(), addr, offset);
1818+
// self.emit(&FixedSizeListLowerElement { elements: ty, idx, });
1819+
// self.write_to_memory(ty, addr.clone(), offset + (field_offset));
1820+
// }
17961821
}
17971822
},
17981823
}
@@ -1988,7 +2013,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
19882013
position = position + increment;
19892014
}
19902015
self.emit(&FixedSizeListLift {
1991-
elements: ty,
2016+
element: ty,
19922017
size: *size,
19932018
id,
19942019
});

crates/csharp/src/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12671267
=> todo!(),
12681268
Instruction::FixedSizeListLift { .. } => todo!(),
12691269
Instruction::FixedSizeListLower { .. } => todo!(),
1270+
Instruction::FixedSizeListLowerBlock { .. } => todo!(),
12701271
}
12711272
}
12721273

crates/moonbit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
26702670
| Instruction::DropHandle { .. } => todo!(),
26712671
Instruction::FixedSizeListLift { .. } => todo!(),
26722672
Instruction::FixedSizeListLower { .. } => todo!(),
2673+
Instruction::FixedSizeListLowerBlock { .. } => todo!(),
26732674
}
26742675
}
26752676

crates/rust/src/bindgen.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,27 @@ impl Bindgen for FunctionBindgen<'_, '_> {
797797
results.push(len);
798798
}
799799

800+
Instruction::FixedSizeListLowerBlock { element, size: _, id: _ } => {
801+
let body = self.blocks.pop().unwrap();
802+
// let tmp = self.tmp();
803+
let vec = operands[0].clone();
804+
//format!("vec{tmp}");
805+
// self.push_str(&format!(
806+
// "let {vec} = {operand0};\n",
807+
// operand0 = operands[0]
808+
// ));
809+
let size = self.r#gen.sizes.size(element);
810+
let target = operands[1].clone();
811+
// let align = self.r#gen.sizes.align(element);
812+
self.push_str(&format!("for (i, e) in {vec}.into_iter().enumerate() {{\n",));
813+
self.push_str(&format!(
814+
"let base = {target}.add(i * {});\n",
815+
size.format(POINTER_SIZE_EXPRESSION)
816+
));
817+
self.push_str(&body);
818+
self.push_str("\n}\n");
819+
}
820+
800821
Instruction::ListLift { element, .. } => {
801822
let body = self.blocks.pop().unwrap();
802823
let tmp = self.tmp();
@@ -1208,7 +1229,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12081229
uwriteln!(self.src, "let _ = {};", operands[0]);
12091230
}
12101231
Instruction::FixedSizeListLift {
1211-
elements: _,
1232+
element: _,
12121233
size,
12131234
id: _,
12141235
} => {
@@ -1223,7 +1244,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12231244
results.push(result);
12241245
}
12251246
Instruction::FixedSizeListLower {
1226-
elements: _,
1247+
element: _,
12271248
size,
12281249
id: _,
12291250
} => {

tests/runtime/fixed-size-list/runner.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ include!(env!("BINDINGS"));
33
use test::fixed_size_lists::to_test::*;
44

55
fn main() {
6-
list_param([1, 2, 3, 4]);
7-
list_param2([[1, 2], [3, 4]]);
6+
// list_param([1, 2, 3, 4]);
7+
// list_param2([[1, 2], [3, 4]]);
8+
{
9+
let result = list_result();
10+
assert_eq!(result, [b'0', b'1', b'A', b'B', b'a', b'b', 128, 255]);
11+
}
812
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ struct Component;
55
export!(Component);
66

77
impl exports::test::fixed_size_lists::to_test::Guest for Component {
8-
fn list_param(a: [u32; 4]) {
9-
assert_eq!(a, [1, 2, 3, 4]);
10-
}
11-
fn list_param2(a: [[u32; 2]; 2]) {
12-
assert_eq!(a, [[1, 2], [3, 4]]);
13-
}
8+
// fn list_param(a: [u32; 4]) {
9+
// assert_eq!(a, [1, 2, 3, 4]);
10+
// }
11+
// fn list_param2(a: [[u32; 2]; 2]) {
12+
// assert_eq!(a, [[1, 2], [3, 4]]);
13+
// }
1414
// fn list_param3(_a: [i32; 20]) {}
15+
fn list_result() -> [u8; 8] {
16+
[b'0', b'1', b'A', b'B', b'a', b'b', 128, 255]
17+
}
1518
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
package test:fixed-size-lists;
44

55
interface to-test {
6-
list-param: func(a: list<u32, 4>);
7-
list-param2: func(a: list<list<u32, 2>, 2>);
6+
// list-param: func(a: list<u32, 4>);
7+
// list-param2: func(a: list<list<u32, 2>, 2>);
88
// list-param3: func(a: list<s32, 20>);s
9-
//list-result: func() -> list<u8, 8>;
9+
list-result: func() -> list<u8, 8>;
1010

1111
// list-minmax16: func(a: list<u16, 4>, b: list<s16, 4>) -> tuple<list<u16, 4>, list<s16, 4>>;
1212
// list-minmax-float: func(a: list<f32, 2>, b: list<f64, 2>)

0 commit comments

Comments
 (0)