Skip to content

Commit

Permalink
Refactor: use index_by_increasing_offset() when building struct type …
Browse files Browse the repository at this point in the history
…instead of making a temporary data-structure (rust-lang#467)
  • Loading branch information
danielsn authored and tedinski committed Sep 3, 2021
1 parent 6715873 commit 145aa97
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions compiler/rustc_codegen_llvm/src/gotoc/mir_to_goto/codegen/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ impl Expr {
}
}

struct StructField<'tcx> {
idx: u32,
offset: u64,
name: String,
ty: Ty<'tcx>,
}

/// Function signatures
impl<'tcx> GotocCtx<'tcx> {
/// Closures expect their last arg untupled at call site, see comment at
Expand Down Expand Up @@ -604,35 +597,23 @@ impl<'tcx> GotocCtx<'tcx> {
) -> Vec<DatatypeComponent> {
match &layout.fields {
FieldsShape::Arbitrary { offsets, memory_index } => {
let mut fields: Vec<_> = memory_index
.iter()
.zip(flds)
.zip(offsets)
.map(|((idx, (n, t)), ofs)| StructField {
idx: *idx,
offset: ofs.bits(),
name: n,
ty: t,
})
.collect();
// first we determine the order of the fields
fields.sort_by(|a, b| a.idx.cmp(&b.idx));
// then we organize all the fields
let mut final_fields = Vec::with_capacity(fields.len());
assert_eq!(flds.len(), offsets.len());
assert_eq!(offsets.len(), memory_index.len());
let mut final_fields = Vec::with_capacity(flds.len());
let mut offset: u64 = initial_offset.try_into().unwrap();
while !fields.is_empty() {
let fld = fields.remove(0);
// We insert padding, if necessary
for idx in layout.fields.index_by_increasing_offset() {
let fld_offset = offsets[idx].bits();
let (fld_name, fld_ty) = &flds[idx];
if let Some(padding) =
self.codegen_struct_padding(offset, fld.offset, final_fields.len())
self.codegen_struct_padding(offset, fld_offset, final_fields.len())
{
final_fields.push(padding)
}
// we insert the actual field
final_fields.push(Type::datatype_component(&fld.name, self.codegen_ty(fld.ty)));
let layout = self.layout_of(fld.ty);
final_fields.push(Type::datatype_component(fld_name, self.codegen_ty(fld_ty)));
let layout = self.layout_of(fld_ty);
// we compute the overall offset of the end of the current struct
offset = fld.offset + layout.size.bits();
offset = fld_offset + layout.size.bits();
}

// If we don't meet our expected alignment, pad until we do
Expand Down

0 comments on commit 145aa97

Please sign in to comment.