mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
Use sorted fields from struct layout as well as layout.stack_size in crane and llvm access
This commit is contained in:
parent
59bcda21f5
commit
2c630fd014
2 changed files with 6 additions and 40 deletions
|
@ -252,41 +252,19 @@ pub fn build_expr<'a, B: Backend>(
|
||||||
Access {
|
Access {
|
||||||
label,
|
label,
|
||||||
field_layout,
|
field_layout,
|
||||||
struct_layout: Layout::Struct(fields),
|
struct_layout: Layout::Struct(sorted_fields),
|
||||||
record,
|
record,
|
||||||
} => {
|
} => {
|
||||||
let cfg = env.cfg;
|
let cfg = env.cfg;
|
||||||
|
|
||||||
// Reconstruct the struct to determine the combined layout
|
|
||||||
// TODO get rid of clones
|
|
||||||
let mut reconstructed_struct_layout =
|
|
||||||
Vec::with_capacity_in(fields.len() + 1, env.arena);
|
|
||||||
for field in fields.iter() {
|
|
||||||
reconstructed_struct_layout.push(field.clone());
|
|
||||||
}
|
|
||||||
reconstructed_struct_layout.push((label.clone(), field_layout.clone()));
|
|
||||||
reconstructed_struct_layout.sort_by(|a, b| {
|
|
||||||
a.0.partial_cmp(&b.0)
|
|
||||||
.expect("TODO: failed to sort struct fields in crane access")
|
|
||||||
});
|
|
||||||
|
|
||||||
// Find the offset we are trying to access
|
// Find the offset we are trying to access
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
for (local_label, layout) in reconstructed_struct_layout.iter() {
|
for (local_label, local_field_layout) in sorted_fields.iter() {
|
||||||
if local_label == label {
|
if local_label == label {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let field_size = match layout {
|
offset += local_field_layout.stack_size(ptr_bytes);
|
||||||
Layout::Builtin(Builtin::Int64) => std::mem::size_of::<i64>(),
|
|
||||||
Layout::Builtin(Builtin::Float64) => std::mem::size_of::<f64>(),
|
|
||||||
_ => panic!(
|
|
||||||
"Missing struct field size in offset calculation for struct access for {:?}",
|
|
||||||
layout
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
offset += field_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = i32::try_from(offset)
|
let offset = i32::try_from(offset)
|
||||||
|
|
|
@ -466,26 +466,14 @@ pub fn build_expr<'a, 'ctx, 'env>(
|
||||||
}
|
}
|
||||||
Access {
|
Access {
|
||||||
label,
|
label,
|
||||||
field_layout,
|
struct_layout: Layout::Struct(sorted_fields),
|
||||||
struct_layout: Layout::Struct(fields),
|
|
||||||
record,
|
record,
|
||||||
|
..
|
||||||
} => {
|
} => {
|
||||||
let builder = env.builder;
|
let builder = env.builder;
|
||||||
|
|
||||||
// Reconstruct struct layout
|
|
||||||
let mut reconstructed_struct_layout =
|
|
||||||
Vec::with_capacity_in(fields.len() + 1, env.arena);
|
|
||||||
for field in fields.iter() {
|
|
||||||
reconstructed_struct_layout.push(field.clone());
|
|
||||||
}
|
|
||||||
reconstructed_struct_layout.push((label.clone(), field_layout.clone()));
|
|
||||||
reconstructed_struct_layout.sort_by(|a, b| {
|
|
||||||
a.0.partial_cmp(&b.0)
|
|
||||||
.expect("TODO: failed to sort struct fields in crane access")
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get index
|
// Get index
|
||||||
let index = reconstructed_struct_layout
|
let index = sorted_fields
|
||||||
.iter()
|
.iter()
|
||||||
.position(|(local_label, _)| local_label == label)
|
.position(|(local_label, _)| local_label == label)
|
||||||
.unwrap() as u32; // TODO
|
.unwrap() as u32; // TODO
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue