Skip to content

Commit 14a4554

Browse files
Fixed length lists
1 parent fa89c21 commit 14a4554

1 file changed

Lines changed: 75 additions & 17 deletions

File tree

crates/d/src/lib.rs

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,12 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
19161916
fn emit_ret_area_if_needed(&self) -> String {
19171917
if !self.return_pointer_area_size.is_empty() {
19181918
format!(
1919-
"align({}) void[{}] _retArea = void;\n",
1919+
"{}align({}) void[{}] _retArea = void;\n",
1920+
if self.r#gen.direction == Some(Direction::Export) {
1921+
"static "
1922+
} else {
1923+
""
1924+
},
19201925
self.return_pointer_area_align.format("size_t.sizeof"),
19211926
self.return_pointer_area_size.format("size_t.sizeof")
19221927
)
@@ -2003,23 +2008,25 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
20032008
| abi::Instruction::I32FromS8
20042009
| abi::Instruction::I32FromU16
20052010
| abi::Instruction::I32FromS16
2006-
| abi::Instruction::I32FromU32
20072011
| abi::Instruction::I32FromS32 => top_as("uint"),
2008-
abi::Instruction::I64FromU64 | abi::Instruction::I64FromS64 => top_as("ulong"),
2009-
abi::Instruction::CoreF32FromF32 => top_as("float"),
2010-
abi::Instruction::CoreF64FromF64 => top_as("double"),
2012+
abi::Instruction::I32FromU32 => results.push(operands.pop().unwrap()),
2013+
2014+
abi::Instruction::I64FromU64 => results.push(operands.pop().unwrap()),
2015+
abi::Instruction::I64FromS64 => top_as("ulong"),
2016+
abi::Instruction::CoreF32FromF32 => results.push(operands.pop().unwrap()),
2017+
abi::Instruction::CoreF64FromF64 => results.push(operands.pop().unwrap()),
20112018

20122019
abi::Instruction::S8FromI32 => top_as("byte"),
20132020
abi::Instruction::U8FromI32 => top_as("ubyte"),
20142021
abi::Instruction::S16FromI32 => top_as("short"),
20152022
abi::Instruction::U16FromI32 => top_as("ushort"),
20162023
abi::Instruction::S32FromI32 => top_as("int"),
2017-
abi::Instruction::U32FromI32 => top_as("uint"),
2024+
abi::Instruction::U32FromI32 => results.push(operands.pop().unwrap()),
20182025
abi::Instruction::S64FromI64 => top_as("long"),
2019-
abi::Instruction::U64FromI64 => top_as("ulong"),
2026+
abi::Instruction::U64FromI64 => results.push(operands.pop().unwrap()),
20202027
abi::Instruction::CharFromI32 => top_as("dchar"),
2021-
abi::Instruction::F32FromCoreF32 => top_as("float"),
2022-
abi::Instruction::F64FromCoreF64 => top_as("double"),
2028+
abi::Instruction::F32FromCoreF32 => results.push(operands.pop().unwrap()),
2029+
abi::Instruction::F64FromCoreF64 => results.push(operands.pop().unwrap()),
20232030
abi::Instruction::BoolFromI32 => results.push(format!("({} != 0)", operands[0])),
20242031

20252032
abi::Instruction::ListCanonLower { .. } | abi::Instruction::StringLower { .. } => {
@@ -2129,17 +2136,68 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
21292136
results.push(format!("{list_name}({list})"));
21302137
}
21312138

2132-
abi::Instruction::FixedLengthListLift { .. } => {
2133-
todo!("instr: FixedLengthListLower");
2139+
abi::Instruction::FixedLengthListLift { size, id, .. } => {
2140+
let result = tempname("_arr", self.tmp());
2141+
let type_name = self.r#gen.type_name(&Type::Id(*id), self.r#gen.fqn);
2142+
self.push_str(&format!("{type_name} {result} = [\n",));
2143+
self.src.indent(1);
2144+
for op in operands.drain(0..(*size as usize)) {
2145+
self.push_str(&op);
2146+
self.push_str(", \n");
2147+
}
2148+
self.src.deindent(1);
2149+
self.push_str("];\n");
2150+
results.push(result);
21342151
}
2135-
abi::Instruction::FixedLengthListLower { .. } => {
2136-
todo!("instr: FixedLengthListLower");
2152+
abi::Instruction::FixedLengthListLower { size, .. } => {
2153+
for i in 0..(*size as usize) {
2154+
results.push(format!("{}[{i}]", operands[0]));
2155+
}
21372156
}
2138-
abi::Instruction::FixedLengthListLowerToMemory { .. } => {
2139-
todo!("instr: FixedLengthListLowerToMemory");
2157+
abi::Instruction::FixedLengthListLowerToMemory { element, .. } => {
2158+
let Block {
2159+
body,
2160+
results: _,
2161+
element: block_element,
2162+
base,
2163+
} = self.blocks.pop().unwrap();
2164+
let arr_src = &operands[0];
2165+
let size_str = self.r#gen.sizes.size(element).format("size_t.sizeof");
2166+
2167+
self.push_str(&format!(
2168+
"foreach ({block_element}_idx, const ref {block_element}; {arr_src}) {{\n"
2169+
));
2170+
self.push_str(&format!(
2171+
"const auto {base} = {arr_src} + {block_element}_idx * {size_str};\n"
2172+
));
2173+
self.push_str(&body);
2174+
self.push_str("\n}\n");
21402175
}
2141-
abi::Instruction::FixedLengthListLiftFromMemory { .. } => {
2142-
todo!("instr: FixedLengthListLiftFromMemory");
2176+
abi::Instruction::FixedLengthListLiftFromMemory { id, element, .. } => {
2177+
let Block {
2178+
body,
2179+
results: block_results,
2180+
element: block_element,
2181+
base,
2182+
} = self.blocks.pop().unwrap();
2183+
let arr_src = &operands[0];
2184+
let type_name = self.r#gen.type_name(&Type::Id(*id), self.r#gen.fqn);
2185+
let size_str = self.r#gen.sizes.size(element).format("size_t.sizeof");
2186+
2187+
let result = tempname("_arr", self.tmp());
2188+
self.push_str(&format!("{type_name} {result} = void;\n"));
2189+
2190+
self.push_str(&format!(
2191+
"foreach ({block_element}_idx, ref {block_element}; {result}) {{\n"
2192+
));
2193+
self.push_str(&format!(
2194+
"const auto {base} = {arr_src} + {block_element}_idx * {size_str};\n"
2195+
));
2196+
self.push_str(&body);
2197+
self.push_str(&format!("{block_element} = {};", block_results[0]));
2198+
self.push_str("\n}\n");
2199+
2200+
results.push(result);
21432201
}
21442202

21452203
abi::Instruction::IterElem { .. } => {

0 commit comments

Comments
 (0)