2015-07-13 12:16:43 -05:00
|
|
|
// Passing structs via FFI should work regardless of whether
|
2015-10-14 21:16:13 -05:00
|
|
|
// they get passed in multiple registers, byval pointers or the stack
|
2015-07-13 12:16:43 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct Rect {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
c: i32,
|
2020-09-01 16:12:52 -05:00
|
|
|
d: i32,
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct BiggerRect {
|
|
|
|
s: Rect,
|
|
|
|
a: i32,
|
2020-09-01 16:12:52 -05:00
|
|
|
b: i32,
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct FloatRect {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
2020-09-01 16:12:52 -05:00
|
|
|
c: f64,
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct Huge {
|
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
c: i32,
|
|
|
|
d: i32,
|
2020-09-01 16:12:52 -05:00
|
|
|
e: i32,
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
2023-05-20 14:33:20 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct Huge64 {
|
|
|
|
a: i64,
|
|
|
|
b: i64,
|
|
|
|
c: i64,
|
|
|
|
d: i64,
|
|
|
|
e: i64,
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:04:33 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct FloatPoint {
|
|
|
|
x: f64,
|
2020-09-01 16:12:52 -05:00
|
|
|
y: f64,
|
2017-04-10 17:04:33 -05:00
|
|
|
}
|
|
|
|
|
2017-08-23 16:38:45 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct FloatOne {
|
|
|
|
x: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct IntOdd {
|
|
|
|
a: i8,
|
|
|
|
b: i8,
|
|
|
|
c: i8,
|
|
|
|
}
|
|
|
|
|
2015-07-13 12:16:43 -05:00
|
|
|
#[link(name = "test", kind = "static")]
|
2020-09-01 16:12:52 -05:00
|
|
|
extern "C" {
|
2015-07-13 12:16:43 -05:00
|
|
|
fn byval_rect(a: i32, b: i32, c: i32, d: i32, e: i32, s: Rect);
|
|
|
|
|
2015-10-15 16:03:27 -05:00
|
|
|
fn byval_many_rect(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, s: Rect);
|
|
|
|
|
2020-09-01 16:12:52 -05:00
|
|
|
fn byval_rect_floats(
|
|
|
|
a: f32,
|
|
|
|
b: f32,
|
|
|
|
c: f64,
|
|
|
|
d: f32,
|
|
|
|
e: f32,
|
|
|
|
f: f32,
|
|
|
|
g: f64,
|
|
|
|
s: Rect,
|
|
|
|
t: FloatRect,
|
|
|
|
);
|
2015-07-13 12:16:43 -05:00
|
|
|
|
|
|
|
fn byval_rect_with_float(a: i32, b: i32, c: f32, d: i32, e: i32, f: i32, s: Rect);
|
|
|
|
|
2017-04-18 15:59:25 -05:00
|
|
|
fn byval_rect_with_many_huge(a: Huge, b: Huge, c: Huge, d: Huge, e: Huge, f: Huge, g: Rect);
|
|
|
|
|
2023-05-20 15:11:09 -05:00
|
|
|
fn byval_rect_with_many_huge64(
|
|
|
|
a: Huge64, b: Huge64, c: Huge64,
|
|
|
|
d: Huge64, e: Huge64, f: Huge64,
|
|
|
|
g: Rect,
|
|
|
|
);
|
2023-05-20 14:33:20 -05:00
|
|
|
|
2015-07-13 12:16:43 -05:00
|
|
|
fn split_rect(a: i32, b: i32, s: Rect);
|
|
|
|
|
|
|
|
fn split_rect_floats(a: f32, b: f32, s: FloatRect);
|
|
|
|
|
|
|
|
fn split_rect_with_floats(a: i32, b: i32, c: f32, d: i32, e: f32, f: i32, s: Rect);
|
|
|
|
|
|
|
|
fn split_and_byval_rect(a: i32, b: i32, c: i32, s: Rect, t: Rect);
|
|
|
|
|
|
|
|
fn split_ret_byval_struct(a: i32, b: i32, s: Rect) -> Rect;
|
|
|
|
|
|
|
|
fn sret_byval_struct(a: i32, b: i32, c: i32, d: i32, s: Rect) -> BiggerRect;
|
|
|
|
|
|
|
|
fn sret_split_struct(a: i32, b: i32, s: Rect) -> BiggerRect;
|
|
|
|
|
|
|
|
fn huge_struct(s: Huge) -> Huge;
|
2017-04-10 17:04:33 -05:00
|
|
|
|
2023-05-20 14:33:20 -05:00
|
|
|
fn huge64_struct(s: Huge64) -> Huge64;
|
|
|
|
|
2017-04-10 17:04:33 -05:00
|
|
|
fn float_point(p: FloatPoint) -> FloatPoint;
|
2017-08-23 16:38:45 -05:00
|
|
|
|
|
|
|
fn float_one(f: FloatOne) -> FloatOne;
|
|
|
|
|
|
|
|
fn int_odd(i: IntOdd) -> IntOdd;
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s = Rect { a: 553, b: 554, c: 555, d: 556 };
|
|
|
|
let t = BiggerRect { s: s, a: 27834, b: 7657 };
|
|
|
|
let u = FloatRect { a: 3489, b: 3490, c: 8. };
|
|
|
|
let v = Huge { a: 5647, b: 5648, c: 5649, d: 5650, e: 5651 };
|
2023-05-20 14:33:20 -05:00
|
|
|
let w = Huge64 { a: 1234, b: 1335, c: 1436, d: 1537, e: 1638 };
|
2017-04-10 17:04:33 -05:00
|
|
|
let p = FloatPoint { x: 5., y: -3. };
|
2017-08-23 16:38:45 -05:00
|
|
|
let f1 = FloatOne { x: 7. };
|
|
|
|
let i = IntOdd { a: 1, b: 2, c: 3 };
|
2015-07-13 12:16:43 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
byval_rect(1, 2, 3, 4, 5, s);
|
2015-10-15 16:03:27 -05:00
|
|
|
byval_many_rect(1, 2, 3, 4, 5, 6, s);
|
2015-07-13 12:16:43 -05:00
|
|
|
byval_rect_floats(1., 2., 3., 4., 5., 6., 7., s, u);
|
|
|
|
byval_rect_with_float(1, 2, 3.0, 4, 5, 6, s);
|
2020-09-01 16:12:52 -05:00
|
|
|
byval_rect_with_many_huge(v, v, v, v, v, v, Rect { a: 123, b: 456, c: 789, d: 420 });
|
2023-05-20 14:33:20 -05:00
|
|
|
byval_rect_with_many_huge64(w, w, w, w, w, w, Rect { a: 1234, b: 4567, c: 7890, d: 4209 });
|
2015-07-13 12:16:43 -05:00
|
|
|
split_rect(1, 2, s);
|
|
|
|
split_rect_floats(1., 2., u);
|
|
|
|
split_rect_with_floats(1, 2, 3.0, 4, 5.0, 6, s);
|
|
|
|
split_and_byval_rect(1, 2, 3, s, s);
|
|
|
|
split_rect(1, 2, s);
|
|
|
|
assert_eq!(huge_struct(v), v);
|
2023-05-20 14:33:20 -05:00
|
|
|
assert_eq!(huge64_struct(w), w);
|
2015-07-13 12:16:43 -05:00
|
|
|
assert_eq!(split_ret_byval_struct(1, 2, s), s);
|
|
|
|
assert_eq!(sret_byval_struct(1, 2, 3, 4, s), t);
|
|
|
|
assert_eq!(sret_split_struct(1, 2, s), t);
|
2017-04-10 17:04:33 -05:00
|
|
|
assert_eq!(float_point(p), p);
|
2017-08-23 16:38:45 -05:00
|
|
|
assert_eq!(int_odd(i), i);
|
2017-08-29 16:48:59 -05:00
|
|
|
|
2017-08-30 14:26:01 -05:00
|
|
|
// MSVC/GCC/Clang are not consistent in the ABI of single-float aggregates.
|
|
|
|
// x86_64: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82028
|
|
|
|
// i686: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82041
|
|
|
|
#[cfg(not(all(windows, target_env = "gnu")))]
|
2017-08-29 16:48:59 -05:00
|
|
|
assert_eq!(float_one(f1), f1);
|
2015-07-13 12:16:43 -05:00
|
|
|
}
|
|
|
|
}
|