Skip to main content

compute_struct_layout_with_ptr

Function compute_struct_layout_with_ptr 

Source
pub fn compute_struct_layout_with_ptr(
    fields: &[StructField],
    types: &BTreeMap<TypeId, AirType>,
    ptr_width: u32,
) -> Option<(Vec<u64>, u64)>
Expand description

Computes struct layout: field byte offsets and total size (including tail padding), using the specified pointer width.

ptr_width is the pointer size in bytes (4 for 32-bit, 8 for 64-bit).

Returns (field_offsets, total_size) or None if any field has an unknown type.

Algorithm adapted from LLVM’s StructLayout constructor in DataLayout.cpp:

  1. For each field, align the current offset to the field’s ABI alignment.
  2. Record the aligned offset, then advance by the field’s allocation size.
  3. After all fields, apply tail padding by aligning to the struct’s overall alignment.