aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-03-01 16:01:33 -0800
committerPatrick Walton <[email protected]>2011-03-01 16:02:19 -0800
commitbdbaf0c7897366f17e36797e5c9fc7ef12fa9fce (patch)
treeb3d97ce6a3b14a58c0b0c7938e5fff8ebe534622 /src
parentPopulate default compilation environment as in rustboot. (diff)
downloadrust-bdbaf0c7897366f17e36797e5c9fc7ef12fa9fce.tar.xz
rust-bdbaf0c7897366f17e36797e5c9fc7ef12fa9fce.zip
rustc: Factor out the align-elements logic in dynamic_size_of()
Diffstat (limited to 'src')
-rw-r--r--src/comp/middle/trans.rs63
1 files changed, 29 insertions, 34 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index 9a650da5..7bdca92a 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -851,50 +851,45 @@ fn align_of(@block_ctxt cx, @ty.t t) -> result {
}
fn dynamic_size_of(@block_ctxt cx, @ty.t t) -> result {
+ fn align_elements(@block_ctxt cx, vec[@ty.t] elts) -> result {
+ //
+ // C padding rules:
+ //
+ //
+ // - Pad after each element so that next element is aligned.
+ // - Pad after final structure member so that whole structure
+ // is aligned to max alignment of interior.
+ //
+ auto off = C_int(0);
+ auto max_align = C_int(1);
+ auto bcx = cx;
+ for (@ty.t e in elts) {
+ auto elt_align = align_of(bcx, e);
+ bcx = elt_align.bcx;
+ auto elt_size = size_of(bcx, e);
+ bcx = elt_size.bcx;
+ auto aligned_off = align_to(bcx, off, elt_align.val);
+ off = cx.build.Add(aligned_off, elt_size.val);
+ max_align = umax(bcx, max_align, elt_align.val);
+ }
+ off = align_to(bcx, off, max_align);
+ ret res(bcx, off);
+ }
+
alt (t.struct) {
case (ty.ty_param(?p)) {
auto szptr = field_of_tydesc(cx, t, abi.tydesc_field_size);
ret res(szptr.bcx, szptr.bcx.build.Load(szptr.val));
}
case (ty.ty_tup(?elts)) {
- //
- // C padding rules:
- //
- //
- // - Pad after each element so that next element is aligned.
- // - Pad after final structure member so that whole structure
- // is aligned to max alignment of interior.
- //
- auto off = C_int(0);
- auto max_align = C_int(1);
- auto bcx = cx;
- for (@ty.t e in elts) {
- auto elt_align = align_of(bcx, e);
- bcx = elt_align.bcx;
- auto elt_size = size_of(bcx, e);
- bcx = elt_size.bcx;
- auto aligned_off = align_to(bcx, off, elt_align.val);
- off = cx.build.Add(aligned_off, elt_size.val);
- max_align = umax(bcx, max_align, elt_align.val);
- }
- off = align_to(bcx, off, max_align);
- ret res(bcx, off);
+ ret align_elements(cx, elts);
}
case (ty.ty_rec(?flds)) {
- auto off = C_int(0);
- auto max_align = C_int(1);
- auto bcx = cx;
+ let vec[@ty.t] tys = vec();
for (ty.field f in flds) {
- auto elt_align = align_of(bcx, f.ty);
- bcx = elt_align.bcx;
- auto elt_size = size_of(bcx, f.ty);
- bcx = elt_size.bcx;
- auto aligned_off = align_to(bcx, off, elt_align.val);
- off = cx.build.Add(aligned_off, elt_size.val);
- max_align = umax(bcx, max_align, elt_align.val);
+ tys += vec(f.ty);
}
- off = align_to(bcx, off, max_align);
- ret res(bcx, off);
+ ret align_elements(cx, tys);
}
}
}