summaryrefslogtreecommitdiffstats
path: root/tests/run-make/extern-fn-struct-passing-abi
diff options
context:
space:
mode:
Diffstat (limited to 'tests/run-make/extern-fn-struct-passing-abi')
-rw-r--r--tests/run-make/extern-fn-struct-passing-abi/Makefile6
-rw-r--r--tests/run-make/extern-fn-struct-passing-abi/test.c314
-rw-r--r--tests/run-make/extern-fn-struct-passing-abi/test.rs138
3 files changed, 458 insertions, 0 deletions
diff --git a/tests/run-make/extern-fn-struct-passing-abi/Makefile b/tests/run-make/extern-fn-struct-passing-abi/Makefile
new file mode 100644
index 000000000..3cbbf3839
--- /dev/null
+++ b/tests/run-make/extern-fn-struct-passing-abi/Makefile
@@ -0,0 +1,6 @@
+# ignore-cross-compile
+include ../tools.mk
+
+all: $(call NATIVE_STATICLIB,test)
+ $(RUSTC) test.rs
+ $(call RUN,test) || exit 1
diff --git a/tests/run-make/extern-fn-struct-passing-abi/test.c b/tests/run-make/extern-fn-struct-passing-abi/test.c
new file mode 100644
index 000000000..136b07129
--- /dev/null
+++ b/tests/run-make/extern-fn-struct-passing-abi/test.c
@@ -0,0 +1,314 @@
+#include <assert.h>
+#include <stdint.h>
+
+struct Rect {
+ int32_t a;
+ int32_t b;
+ int32_t c;
+ int32_t d;
+};
+
+struct BiggerRect {
+ struct Rect s;
+ int32_t a;
+ int32_t b;
+};
+
+struct FloatRect {
+ int32_t a;
+ int32_t b;
+ double c;
+};
+
+struct Huge {
+ int32_t a;
+ int32_t b;
+ int32_t c;
+ int32_t d;
+ int32_t e;
+};
+
+struct FloatPoint {
+ double x;
+ double y;
+};
+
+struct FloatOne {
+ double x;
+};
+
+struct IntOdd {
+ int8_t a;
+ int8_t b;
+ int8_t c;
+};
+
+// System V x86_64 ABI:
+// a, b, c, d, e should be in registers
+// s should be byval pointer
+//
+// Win64 ABI:
+// a, b, c, d should be in registers
+// e should be on the stack
+// s should be byval pointer
+void byval_rect(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3);
+ assert(d == 4);
+ assert(e == 5);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+}
+
+// System V x86_64 ABI:
+// a, b, c, d, e, f should be in registers
+// s should be byval pointer on the stack
+//
+// Win64 ABI:
+// a, b, c, d should be in registers
+// e, f should be on the stack
+// s should be byval pointer on the stack
+void byval_many_rect(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e,
+ int32_t f, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3);
+ assert(d == 4);
+ assert(e == 5);
+ assert(f == 6);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+}
+
+// System V x86_64 ABI:
+// a, b, c, d, e, f, g should be in sse registers
+// s should be split across 2 registers
+// t should be byval pointer
+//
+// Win64 ABI:
+// a, b, c, d should be in sse registers
+// e, f, g should be on the stack
+// s should be on the stack (treated as 2 i64's)
+// t should be on the stack (treated as an i64 and a double)
+void byval_rect_floats(float a, float b, double c, float d, float e,
+ float f, double g, struct Rect s, struct FloatRect t) {
+ assert(a == 1.);
+ assert(b == 2.);
+ assert(c == 3.);
+ assert(d == 4.);
+ assert(e == 5.);
+ assert(f == 6.);
+ assert(g == 7.);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+ assert(t.a == 3489);
+ assert(t.b == 3490);
+ assert(t.c == 8.);
+}
+
+// System V x86_64 ABI:
+// a, b, d, e, f should be in registers
+// c passed via sse registers
+// s should be byval pointer
+//
+// Win64 ABI:
+// a, b, d should be in registers
+// c passed via sse registers
+// e, f should be on the stack
+// s should be byval pointer
+void byval_rect_with_float(int32_t a, int32_t b, float c, int32_t d,
+ int32_t e, int32_t f, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3.);
+ assert(d == 4);
+ assert(e == 5);
+ assert(f == 6);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+}
+
+// System V x86_64 ABI:
+// a, b, d, e, f should be byval pointer (on the stack)
+// g passed via register (fixes #41375)
+//
+// Win64 ABI:
+// a, b, d, e, f, g should be byval pointer
+void byval_rect_with_many_huge(struct Huge a, struct Huge b, struct Huge c,
+ struct Huge d, struct Huge e, struct Huge f,
+ struct Rect g) {
+ assert(g.a == 123);
+ assert(g.b == 456);
+ assert(g.c == 789);
+ assert(g.d == 420);
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b should be in registers
+// s should be split across 2 integer registers
+void split_rect(int32_t a, int32_t b, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b should be in sse registers
+// s should be split across integer & sse registers
+void split_rect_floats(float a, float b, struct FloatRect s) {
+ assert(a == 1.);
+ assert(b == 2.);
+ assert(s.a == 3489);
+ assert(s.b == 3490);
+ assert(s.c == 8.);
+}
+
+// System V x86_64 ABI:
+// a, b, d, f should be in registers
+// c, e passed via sse registers
+// s should be split across 2 registers
+//
+// Win64 ABI:
+// a, b, d should be in registers
+// c passed via sse registers
+// e, f should be on the stack
+// s should be on the stack (treated as 2 i64's)
+void split_rect_with_floats(int32_t a, int32_t b, float c,
+ int32_t d, float e, int32_t f, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3.);
+ assert(d == 4);
+ assert(e == 5.);
+ assert(f == 6);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b, c should be in registers
+// s should be split across 2 registers
+// t should be a byval pointer
+void split_and_byval_rect(int32_t a, int32_t b, int32_t c, struct Rect s, struct Rect t) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+ assert(t.a == 553);
+ assert(t.b == 554);
+ assert(t.c == 555);
+ assert(t.d == 556);
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b should in registers
+// s and return should be split across 2 registers
+struct Rect split_ret_byval_struct(int32_t a, int32_t b, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+ return s;
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b, c, d should be in registers
+// return should be in a hidden sret pointer
+// s should be a byval pointer
+struct BiggerRect sret_byval_struct(int32_t a, int32_t b, int32_t c, int32_t d, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(c == 3);
+ assert(d == 4);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+
+ struct BiggerRect t;
+ t.s = s; t.a = 27834; t.b = 7657;
+ return t;
+}
+
+// System V x86_64 & Win64 ABI:
+// a, b should be in registers
+// return should be in a hidden sret pointer
+// s should be split across 2 registers
+struct BiggerRect sret_split_struct(int32_t a, int32_t b, struct Rect s) {
+ assert(a == 1);
+ assert(b == 2);
+ assert(s.a == 553);
+ assert(s.b == 554);
+ assert(s.c == 555);
+ assert(s.d == 556);
+
+ struct BiggerRect t;
+ t.s = s; t.a = 27834; t.b = 7657;
+ return t;
+}
+
+// System V x86_64 & Win64 ABI:
+// s should be byval pointer (since sizeof(s) > 16)
+// return should in a hidden sret pointer
+struct Huge huge_struct(struct Huge s) {
+ assert(s.a == 5647);
+ assert(s.b == 5648);
+ assert(s.c == 5649);
+ assert(s.d == 5650);
+ assert(s.e == 5651);
+
+ return s;
+}
+
+// System V x86_64 ABI:
+// p should be in registers
+// return should be in registers
+//
+// Win64 ABI and 64-bit PowerPC ELFv1 ABI:
+// p should be a byval pointer
+// return should be in a hidden sret pointer
+struct FloatPoint float_point(struct FloatPoint p) {
+ assert(p.x == 5.);
+ assert(p.y == -3.);
+
+ return p;
+}
+
+// 64-bit PowerPC ELFv1 ABI:
+// f1 should be in a register
+// return should be in a hidden sret pointer
+struct FloatOne float_one(struct FloatOne f1) {
+ assert(f1.x == 7.);
+
+ return f1;
+}
+
+// 64-bit PowerPC ELFv1 ABI:
+// i should be in the least-significant bits of a register
+// return should be in a hidden sret pointer
+struct IntOdd int_odd(struct IntOdd i) {
+ assert(i.a == 1);
+ assert(i.b == 2);
+ assert(i.c == 3);
+
+ return i;
+}
diff --git a/tests/run-make/extern-fn-struct-passing-abi/test.rs b/tests/run-make/extern-fn-struct-passing-abi/test.rs
new file mode 100644
index 000000000..afe0f52ef
--- /dev/null
+++ b/tests/run-make/extern-fn-struct-passing-abi/test.rs
@@ -0,0 +1,138 @@
+// Passing structs via FFI should work regardless of whether
+// they get passed in multiple registers, byval pointers or the stack
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+struct Rect {
+ a: i32,
+ b: i32,
+ c: i32,
+ d: i32,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+struct BiggerRect {
+ s: Rect,
+ a: i32,
+ b: i32,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+struct FloatRect {
+ a: i32,
+ b: i32,
+ c: f64,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+struct Huge {
+ a: i32,
+ b: i32,
+ c: i32,
+ d: i32,
+ e: i32,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+struct FloatPoint {
+ x: f64,
+ y: f64,
+}
+
+#[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,
+}
+
+#[link(name = "test", kind = "static")]
+extern "C" {
+ fn byval_rect(a: i32, b: i32, c: i32, d: i32, e: i32, s: Rect);
+
+ fn byval_many_rect(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, s: Rect);
+
+ fn byval_rect_floats(
+ a: f32,
+ b: f32,
+ c: f64,
+ d: f32,
+ e: f32,
+ f: f32,
+ g: f64,
+ s: Rect,
+ t: FloatRect,
+ );
+
+ fn byval_rect_with_float(a: i32, b: i32, c: f32, d: i32, e: i32, f: i32, s: Rect);
+
+ fn byval_rect_with_many_huge(a: Huge, b: Huge, c: Huge, d: Huge, e: Huge, f: Huge, g: Rect);
+
+ 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;
+
+ fn float_point(p: FloatPoint) -> FloatPoint;
+
+ fn float_one(f: FloatOne) -> FloatOne;
+
+ fn int_odd(i: IntOdd) -> IntOdd;
+}
+
+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 };
+ let p = FloatPoint { x: 5., y: -3. };
+ let f1 = FloatOne { x: 7. };
+ let i = IntOdd { a: 1, b: 2, c: 3 };
+
+ unsafe {
+ byval_rect(1, 2, 3, 4, 5, s);
+ byval_many_rect(1, 2, 3, 4, 5, 6, s);
+ byval_rect_floats(1., 2., 3., 4., 5., 6., 7., s, u);
+ byval_rect_with_float(1, 2, 3.0, 4, 5, 6, s);
+ byval_rect_with_many_huge(v, v, v, v, v, v, Rect { a: 123, b: 456, c: 789, d: 420 });
+ 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);
+ 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);
+ assert_eq!(float_point(p), p);
+ assert_eq!(int_odd(i), i);
+
+ // 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")))]
+ assert_eq!(float_one(f1), f1);
+ }
+}