Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csharp multi return #1

Merged
merged 2 commits into from
Jan 7, 2024
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
86 changes: 76 additions & 10 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ impl WorldGenerator for CSharp {
BitConverter.TryWriteBytes(span.Slice(offset), value);
}}

public void SetF32(int offset, float value)
{{
Span<byte> span = this;

BitConverter.TryWriteBytes(span.Slice(offset), value);
}}

internal unsafe int AddrOfBuffer()
{{
fixed(byte* ptr = &buffer)
Expand Down Expand Up @@ -623,7 +630,14 @@ impl InterfaceGenerator<'_> {
r#"
private unsafe struct ReturnArea
{{
public int GetS32(IntPtr ptr, int offset)
internal int GetS32(IntPtr ptr, int offset)
{{
var span = new Span<byte>((void*)ptr, {});

return BitConverter.ToInt32(span.Slice(offset, 4));
}}

internal int GetF32(IntPtr ptr, int offset)
{{
var span = new Span<byte>((void*)ptr, {});

Expand All @@ -641,6 +655,7 @@ impl InterfaceGenerator<'_> {
[FixedAddressValueType]
private static ReturnArea returnArea;
"#,
self.gen.return_area_size,
self.gen.return_area_size
);
}
Expand Down Expand Up @@ -683,6 +698,13 @@ impl InterfaceGenerator<'_> {
BitConverter.TryWriteBytes(span.Slice(offset), value);
}}

public void SetF32(int offset, float value)
{{
Span<byte> span = this;

BitConverter.TryWriteBytes(span.Slice(offset), value);
}}

internal unsafe int AddrOfBuffer()
{{
fixed(byte* ptr = &buffer)
Expand Down Expand Up @@ -737,7 +759,15 @@ impl InterfaceGenerator<'_> {
let ty = func.results.iter_types().next().unwrap();
self.type_name_with_qualifier(ty, true)
}
_ => unreachable!(), //TODO
_ => {
let types = func
.results
.iter_types()
.map(|ty| self.type_name(ty))
.collect::<Vec<_>>()
.join(", ");
format!("Tuple<{}>", types)
},
};

let camel_name = func.name.to_upper_camel_case();
Expand Down Expand Up @@ -838,12 +868,13 @@ impl InterfaceGenerator<'_> {
let result_type = match func.results.len() {
0 => "void".to_owned(),
1 => self.type_name(func.results.iter_types().next().unwrap()),
_ => func
_ => format!("Tuple<{}>",
func
.results
.iter_types()
.map(|ty| self.type_name(ty))
.collect::<Vec<String>>()
.join(", "),
.join(", ")),
};

let camel_name = func.name.to_upper_camel_case();
Expand Down Expand Up @@ -1069,7 +1100,7 @@ impl InterfaceGenerator<'_> {
count => {
self.gen.tuple_counts.insert(count);
format!(
"({})",
"Tuple<{}>",
func.results
.iter_types()
.map(|ty| self.type_name_boxed(ty, qualifier))
Expand Down Expand Up @@ -1424,7 +1455,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
}
.to_owned()
})),

Instruction::I32Load { offset } => {
if self.gen.in_import {
results.push(format!("returnArea.GetS32(ptr, {offset})"))
Expand All @@ -1445,7 +1475,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
results.push(format!("returnArea.GetS16({offset})"))
}
Instruction::I64Load { offset } => results.push(format!("returnArea.GetS64({offset})")),
Instruction::F32Load { offset } => results.push(format!("returnArea.GetF32({offset})")),
Instruction::F32Load { offset } => results.push(format!("returnArea.GetF32(ptr, {offset})")),
Instruction::F64Load { offset } => results.push(format!("returnArea.GetF64({offset})")),

Instruction::I32Store { offset } => {
Expand All @@ -1454,7 +1484,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::I32Store8 { .. } => todo!("I32Store8"),
Instruction::I32Store16 { .. } => todo!("I32Store16"),
Instruction::I64Store { .. } => todo!("I64Store"),
Instruction::F32Store { .. } => todo!("F32Store"),
Instruction::F32Store { offset } => {
uwriteln!(self.src, "returnArea.SetF32({}, {});", offset, operands[0])
},
Instruction::F64Store { .. } => todo!("F64Store"),

Instruction::I64FromU64 => results.push(format!("unchecked((long)({}))", operands[0])),
Expand All @@ -1467,6 +1499,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::U32FromI32 => results.push(format!("unchecked((uint)({}))", operands[0])),
Instruction::U64FromI64 => results.push(format!("unchecked((ulong)({}))", operands[0])),
Instruction::CharFromI32 => results.push(format!("unchecked((uint)({}))", operands[0])),
Instruction::Float32FromF32 => results.push(format!("unchecked((float){})", operands[0])),
Instruction::I64FromS64
| Instruction::I32FromU16
| Instruction::I32FromS16
Expand All @@ -1477,7 +1510,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
| Instruction::F64FromFloat64
| Instruction::S32FromI32
| Instruction::S64FromI64
| Instruction::Float32FromF32
| Instruction::Float64FromF64 => results.push(operands[0].clone()),

Instruction::Bitcasts { .. } => todo!("Bitcasts"),
Expand Down Expand Up @@ -1656,7 +1688,41 @@ impl Bindgen for FunctionBindgen<'_, '_> {
.src
.push_str(&format!("{class_name}Impl.{func_name}({oper});")),
1 => results.push(format!("{class_name}Impl.{func_name}({oper})")),
_ => results.push(format!("{class_name}Impl.{func_name}({oper})")),
_ => {
let ty = format!(
"Tuple<{}>",
func.results
.iter_types()
.map(|ty| self.gen.type_name_boxed(ty, false))
.collect::<Vec<_>>()
.join(", ")
);

let result = self.locals.tmp("result");
let assignment = format!("{ty} {result} = ");

let destructure = func
.results
.iter_types()
.enumerate()
.map(|(index, ty)| {
let ty = self.gen.type_name(ty);
let my_result = self.locals.tmp("result");
let assignment = format!("{ty} {my_result} = {result}.Item{};", index+1);
results.push(my_result);
assignment
})
.collect::<Vec<_>>()
.join("\n");

uwrite!(
self.src,
"
{assignment} {class_name}Impl.{func_name}({oper});
{destructure}
"
);
},
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/csharp/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ macro_rules! codegen_test {
"lift-lower-foreign",
"lists",
"many-arguments",
"multi-return",
"option-result",
"records",
"rename-interface",
Expand Down