Skip to content

Commit bdbaf0c

Browse files
committed
rustc: Factor out the align-elements logic in dynamic_size_of()
1 parent 348c77c commit bdbaf0c

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

src/comp/middle/trans.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -851,50 +851,45 @@ fn align_of(@block_ctxt cx, @ty.t t) -> result {
851851
}
852852

853853
fn dynamic_size_of(@block_ctxt cx, @ty.t t) -> result {
854+
fn align_elements(@block_ctxt cx, vec[@ty.t] elts) -> result {
855+
//
856+
// C padding rules:
857+
//
858+
//
859+
// - Pad after each element so that next element is aligned.
860+
// - Pad after final structure member so that whole structure
861+
// is aligned to max alignment of interior.
862+
//
863+
auto off = C_int(0);
864+
auto max_align = C_int(1);
865+
auto bcx = cx;
866+
for (@ty.t e in elts) {
867+
auto elt_align = align_of(bcx, e);
868+
bcx = elt_align.bcx;
869+
auto elt_size = size_of(bcx, e);
870+
bcx = elt_size.bcx;
871+
auto aligned_off = align_to(bcx, off, elt_align.val);
872+
off = cx.build.Add(aligned_off, elt_size.val);
873+
max_align = umax(bcx, max_align, elt_align.val);
874+
}
875+
off = align_to(bcx, off, max_align);
876+
ret res(bcx, off);
877+
}
878+
854879
alt (t.struct) {
855880
case (ty.ty_param(?p)) {
856881
auto szptr = field_of_tydesc(cx, t, abi.tydesc_field_size);
857882
ret res(szptr.bcx, szptr.bcx.build.Load(szptr.val));
858883
}
859884
case (ty.ty_tup(?elts)) {
860-
//
861-
// C padding rules:
862-
//
863-
//
864-
// - Pad after each element so that next element is aligned.
865-
// - Pad after final structure member so that whole structure
866-
// is aligned to max alignment of interior.
867-
//
868-
auto off = C_int(0);
869-
auto max_align = C_int(1);
870-
auto bcx = cx;
871-
for (@ty.t e in elts) {
872-
auto elt_align = align_of(bcx, e);
873-
bcx = elt_align.bcx;
874-
auto elt_size = size_of(bcx, e);
875-
bcx = elt_size.bcx;
876-
auto aligned_off = align_to(bcx, off, elt_align.val);
877-
off = cx.build.Add(aligned_off, elt_size.val);
878-
max_align = umax(bcx, max_align, elt_align.val);
879-
}
880-
off = align_to(bcx, off, max_align);
881-
ret res(bcx, off);
885+
ret align_elements(cx, elts);
882886
}
883887
case (ty.ty_rec(?flds)) {
884-
auto off = C_int(0);
885-
auto max_align = C_int(1);
886-
auto bcx = cx;
888+
let vec[@ty.t] tys = vec();
887889
for (ty.field f in flds) {
888-
auto elt_align = align_of(bcx, f.ty);
889-
bcx = elt_align.bcx;
890-
auto elt_size = size_of(bcx, f.ty);
891-
bcx = elt_size.bcx;
892-
auto aligned_off = align_to(bcx, off, elt_align.val);
893-
off = cx.build.Add(aligned_off, elt_size.val);
894-
max_align = umax(bcx, max_align, elt_align.val);
890+
tys += vec(f.ty);
895891
}
896-
off = align_to(bcx, off, max_align);
897-
ret res(bcx, off);
892+
ret align_elements(cx, tys);
898893
}
899894
}
900895
}

0 commit comments

Comments
 (0)