Skip to content

Commit 01251db

Browse files
committed
returning a fixed size list works
1 parent 913fa35 commit 01251db

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
///
@@ -1373,7 +1380,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
13731380
TypeDefKind::Unknown => unreachable!(),
13741381
TypeDefKind::FixedSizeList(ty, size) => {
13751382
self.emit(&FixedSizeListLower {
1376-
elements: ty,
1383+
element: ty,
13771384
size: *size,
13781385
id,
13791386
});
@@ -1593,7 +1600,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
15931600
self.lift(ty);
15941601
}
15951602
self.emit(&FixedSizeListLift {
1596-
elements: ty,
1603+
element: ty,
15971604
size: *size,
15981605
id,
15991606
});
@@ -1758,7 +1765,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17581765
}
17591766

17601767
TypeDefKind::Unknown => unreachable!(),
1761-
TypeDefKind::FixedSizeList(ty, size) => {
1768+
TypeDefKind::FixedSizeList(element, size) => {
17621769
// let increment = self.bindgen.sizes().size(ty);
17631770
// let mut position = offset;
17641771
// let resultvar = self.stack[0];
@@ -1768,11 +1775,29 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17681775
// self.write_to_memory(ty, addr.clone(), position);
17691776
// position = position + increment;
17701777
// }
1771-
self.emit(&FixedSizeListLower {
1772-
elements: ty,
1773-
size: *size,
1774-
id,
1775-
});
1778+
// @@@@
1779+
// self.emit(&FixedSizeListLower {
1780+
// elements: ty,
1781+
// size: *size,
1782+
// id,
1783+
// });
1784+
1785+
// resembles write_list_to_memory
1786+
self.push_block();
1787+
self.emit(&IterElem { element });
1788+
self.emit(&IterBasePointer);
1789+
let elem_addr = self.stack.pop().unwrap();
1790+
self.write_to_memory(element, elem_addr, Default::default());
1791+
self.finish_block(0);
1792+
// let target = self.stack.pop().unwrap();
1793+
self.stack.push(addr);
1794+
self.emit(&FixedSizeListLowerBlock { element, size: *size, id });
1795+
1796+
// for idx in 0..*size {
1797+
// //self.write_fields_to_memory(tuple.types.iter(), addr, offset);
1798+
// self.emit(&FixedSizeListLowerElement { elements: ty, idx, });
1799+
// self.write_to_memory(ty, addr.clone(), offset + (field_offset));
1800+
// }
17761801
}
17771802
},
17781803
}
@@ -1968,7 +1993,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
19681993
position = position + increment;
19691994
}
19701995
self.emit(&FixedSizeListLift {
1971-
elements: ty,
1996+
element: ty,
19721997
size: *size,
19731998
id,
19741999
});

crates/csharp/src/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12651265
| Instruction::ErrorContextLift { .. } => todo!(),
12661266
Instruction::FixedSizeListLift { .. } => todo!(),
12671267
Instruction::FixedSizeListLower { .. } => todo!(),
1268+
Instruction::FixedSizeListLowerBlock { .. } => todo!(),
12681269
}
12691270
}
12701271

crates/moonbit/src/lib.rs

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

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();
@@ -1203,7 +1224,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12031224
));
12041225
}
12051226
Instruction::FixedSizeListLift {
1206-
elements: _,
1227+
element: _,
12071228
size,
12081229
id: _,
12091230
} => {
@@ -1218,7 +1239,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12181239
results.push(result);
12191240
}
12201241
Instruction::FixedSizeListLower {
1221-
elements: _,
1242+
element: _,
12221243
size,
12231244
id: _,
12241245
} => {

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)