From 1e410f6206cee9f75cc156fba1e467cad8f16fdc Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 30 Apr 2012 16:44:34 -0700 Subject: [PATCH] rt: Fix some record alignment issues on windows --- src/rt/rust_shape.h | 2 + src/test/run-pass/intrinsic-alignment.rs | 32 ++++++--- .../{rec-align-32-bit.rs => rec-align-u32.rs} | 24 ++++--- src/test/run-pass/rec-align-u64.rs | 68 +++++++++++++++++++ 4 files changed, 109 insertions(+), 17 deletions(-) rename src/test/run-pass/{rec-align-32-bit.rs => rec-align-u32.rs} (73%) create mode 100644 src/test/run-pass/rec-align-u64.rs diff --git a/src/rt/rust_shape.h b/src/rt/rust_shape.h index 3aaea6f739c..ee9b6400cd4 100644 --- a/src/rt/rust_shape.h +++ b/src/rt/rust_shape.h @@ -127,6 +127,7 @@ rust_alignof() { // On 32-bit x86 the alignment of 64-bit ints in structures is 4 bytes // Which is different from the preferred 8-byte alignment reported // by __alignof__ (at least on gcc). +#ifndef __WIN32__ #ifdef __i386__ template<> inline size_t @@ -134,6 +135,7 @@ rust_alignof() { return 4; } #endif +#endif // Utility classes diff --git a/src/test/run-pass/intrinsic-alignment.rs b/src/test/run-pass/intrinsic-alignment.rs index 7841cda4522..bc1d75172f0 100644 --- a/src/test/run-pass/intrinsic-alignment.rs +++ b/src/test/run-pass/intrinsic-alignment.rs @@ -1,4 +1,4 @@ -// xfail-win32 need to investigate alignment on windows +// xfail-fast Does not work with main in a submodule #[abi = "rust-intrinsic"] native mod rusti { @@ -6,14 +6,28 @@ native mod rusti { fn min_align_of() -> uint; } -#[cfg(target_arch = "x86")] -fn main() { - assert rusti::pref_align_of::() == 8u; - assert rusti::min_align_of::() == 4u; +#[cfg(target_os = "linux")] +#[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] +mod m { + #[cfg(target_arch = "x86")] + fn main() { + assert rusti::pref_align_of::() == 8u; + assert rusti::min_align_of::() == 4u; + } + + #[cfg(target_arch = "x86_64")] + fn main() { + assert rusti::pref_align_of::() == 8u; + assert rusti::min_align_of::() == 8u; + } } -#[cfg(target_arch = "x86_64")] -fn main() { - assert rusti::pref_align_of::() == 8u; - assert rusti::min_align_of::() == 8u; +#[cfg(target_os = "win32")] +mod m { + #[cfg(target_arch = "x86")] + fn main() { + assert rusti::pref_align_of::() == 8u; + assert rusti::min_align_of::() == 8u; + } } diff --git a/src/test/run-pass/rec-align-32-bit.rs b/src/test/run-pass/rec-align-u32.rs similarity index 73% rename from src/test/run-pass/rec-align-32-bit.rs rename to src/test/run-pass/rec-align-u32.rs index 28e0268b6a5..89f3305bf79 100644 --- a/src/test/run-pass/rec-align-32-bit.rs +++ b/src/test/run-pass/rec-align-u32.rs @@ -1,5 +1,4 @@ // xfail-pretty -// xfail-win32 // Issue #2303 #[abi = "rust-intrinsic"] @@ -10,7 +9,7 @@ native mod rusti { // This is the type with the questionable alignment type inner = { - c64: u64 + c64: u32 }; // This is the type that contains the type with the @@ -20,10 +19,22 @@ type outer = { t: inner }; + #[cfg(target_arch = "x86")] +mod m { + fn align() -> uint { 4u } + fn size() -> uint { 8u } +} + +#[cfg(target_arch = "x86_64")] +mod m { + fn align() -> uint { 4u } + fn size() -> uint { 8u } +} + fn main() { - let x = {c8: 22u8, t: {c64: 44u64}}; + let x = {c8: 22u8, t: {c64: 44u32}}; // Send it through the shape code let y = #fmt["%?", x]; @@ -33,14 +44,11 @@ fn main() { #debug("y = %s", y); // per clang/gcc the alignment of `inner` is 4 on x86. - assert rusti::min_align_of::() == 4u; + assert rusti::min_align_of::() == m::align(); // per clang/gcc the size of `outer` should be 12 // because `inner`s alignment was 4. - assert sys::size_of::() == 12u; + assert sys::size_of::() == m::size(); assert y == "(22, (44))"; } - -#[cfg(target_arch = "x86_64")] -fn main() { } diff --git a/src/test/run-pass/rec-align-u64.rs b/src/test/run-pass/rec-align-u64.rs new file mode 100644 index 00000000000..39646bb04e0 --- /dev/null +++ b/src/test/run-pass/rec-align-u64.rs @@ -0,0 +1,68 @@ +// xfail-pretty +// Issue #2303 + +#[abi = "rust-intrinsic"] +native mod rusti { + fn pref_align_of() -> uint; + fn min_align_of() -> uint; +} + +// This is the type with the questionable alignment +type inner = { + c64: u64 +}; + +// This is the type that contains the type with the +// questionable alignment, for testing +type outer = { + c8: u8, + t: inner +}; + + +#[cfg(target_os = "linux")] +#[cfg(target_os = "macos")] +#[cfg(target_os = "freebsd")] +mod m { + #[cfg(target_arch = "x86")] + mod m { + fn align() -> uint { 4u } + fn size() -> uint { 12u } + } + + #[cfg(target_arch = "x86_64")] + mod m { + fn align() -> uint { 8u } + fn size() -> uint { 16u } + } +} + +#[cfg(target_os = "win32")] +mod m { + #[cfg(target_arch = "x86")] + mod m { + fn align() -> uint { 8u } + fn size() -> uint { 16u } + } +} + +fn main() { + + let x = {c8: 22u8, t: {c64: 44u64}}; + + // Send it through the shape code + let y = #fmt["%?", x]; + + #debug("align inner = %?", rusti::min_align_of::()); + #debug("size outer = %?", sys::size_of::()); + #debug("y = %s", y); + + // per clang/gcc the alignment of `inner` is 4 on x86. + assert rusti::min_align_of::() == m::m::align(); + + // per clang/gcc the size of `outer` should be 12 + // because `inner`s alignment was 4. + assert sys::size_of::() == m::m::size(); + + assert y == "(22, (44))"; +}