You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Commit 49d2b57 adds support for packed structures that are also aligned, and also packed structures that directly contain aligned structure fields. However, packed structures that indirectly contain aligned structures aren't supported by the current implementation, e.g.
I have some ideas on how to support this, but the implementation would be somewhat complex.
Basically, we need to produce X_Inner and X_PADDING for all types that are directly or indirectly included in a packed structure, and manually lay out the fields of the X_Inner structure to perfectly match what C does.
This is tricky because, in addition to the post-field padding that the current implementation adds, we'd need to insert pre-field padding inside non-packed structures, e.g., between the B::x and B::c fields above.
We cannot rely on rustc inserting that padding by itself, since we're not using the #[repr(align(N))] attribute for alignment.
We need to control the exact layout of every structure ourselves using padding fields.
To start, we will distinguish between manually-aligned structures, i.e., structures whose layouts are directly or indirectly controlled by an aligned(N) attribute, and rustc-aligned ones.
I propose the following algorithm to emit a manually-aligned structure in Rust:
Find the last manually-aligned field in the structure
Split the structure, e.g., Foo, into the following components:
a. The sub-structure containing all fields before the manually-aligned one (called Foo_Pre below)
b. The pre-field padding
c. The field itself (of type T)
d. The post-field padding
e. The substructure with all the following fields, if any (all rustc-managed), e.g.
Compute field_POSTPADDING as align_to(size_of::<Foo_PreField>, T_ALIGNMENT).
If Foo_Pre contains other manually-aligned fields, go back to step 1 with Foo_Pre as the new structure. Otherwise, stop.
The align_to function needs to be implemented as a const fn, and the actual names of Foo_Pre, Foo_PreField, Foo_Post and the padding fields and constants should be produced by the renamer.
For example, the generated structures corresponding to struct B above would look something like
Commit 49d2b57 adds support for packed structures that are also aligned, and also packed structures that directly contain aligned structure fields. However, packed structures that indirectly contain aligned structures aren't supported by the current implementation, e.g.
I have some ideas on how to support this, but the implementation would be somewhat complex.
Basically, we need to produce
X_Inner
andX_PADDING
for all types that are directly or indirectly included in a packed structure, and manually lay out the fields of theX_Inner
structure to perfectly match what C does.This is tricky because, in addition to the post-field padding that the current implementation adds, we'd need to insert pre-field padding inside non-packed structures, e.g., between the
B::x
andB::c
fields above.We cannot rely on rustc inserting that padding by itself, since we're not using the
#[repr(align(N))]
attribute for alignment.We need to control the exact layout of every structure ourselves using padding fields.
To start, we will distinguish between manually-aligned structures, i.e., structures whose layouts are directly or indirectly controlled by an
aligned(N)
attribute, and rustc-aligned ones.I propose the following algorithm to emit a manually-aligned structure in Rust:
Foo
, into the following components:a. The sub-structure containing all fields before the manually-aligned one (called
Foo_Pre
below)b. The pre-field padding
c. The field itself (of type
T
)d. The post-field padding
e. The substructure with all the following fields, if any (all rustc-managed), e.g.
T_ALIGNMENT
ofT
as the maximum between the manually-specified alignment andalign_of::<T>()
from Rust.field_PREPADDING
asalign_to(size_of::<Pre>(), T_ALIGNMENT)
after computingT_ALIGNMENT
.field_POSTPADDING
asalign_to(size_of::<Foo_PreField>, T_ALIGNMENT)
.Foo_Pre
contains other manually-aligned fields, go back to step 1 withFoo_Pre
as the new structure. Otherwise, stop.The
align_to
function needs to be implemented as aconst fn
, and the actual names ofFoo_Pre
,Foo_PreField
,Foo_Post
and the padding fields and constants should be produced by the renamer.For example, the generated structures corresponding to
struct B
above would look something likeSo far, we haven't run into any code that needs this, so the implementation work will be done later.
The text was updated successfully, but these errors were encountered: