summaryrefslogtreecommitdiffstats
path: root/src/test/ui/abi/struct-enums/struct-return.rs
blob: 1a7984ea5cd17b8b3a52d03c01ec30a856db83bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// run-pass
#![allow(dead_code)]
// ignore-wasm32-bare no libc to test ffi with

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Quad {
    a: u64,
    b: u64,
    c: u64,
    d: u64,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Floats {
    a: f64,
    b: u8,
    c: f64,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct CharCharDouble {
    a: u8,
    b: u8,
    c: f64,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct CharCharFloat {
    a: u8,
    b: u8,
    c: f32,
}

mod rustrt {
    use super::{CharCharDouble, CharCharFloat, Floats, Quad};

    #[link(name = "rust_test_helpers", kind = "static")]
    extern "C" {
        pub fn rust_dbg_abi_1(q: Quad) -> Quad;
        pub fn rust_dbg_abi_2(f: Floats) -> Floats;
        pub fn rust_dbg_abi_3(a: CharCharDouble) -> CharCharDouble;
        pub fn rust_dbg_abi_4(a: CharCharFloat) -> CharCharFloat;
    }
}

fn test1() {
    unsafe {
        let q = Quad {
            a: 0xaaaa_aaaa_aaaa_aaaa,
            b: 0xbbbb_bbbb_bbbb_bbbb,
            c: 0xcccc_cccc_cccc_cccc,
            d: 0xdddd_dddd_dddd_dddd,
        };
        let qq = rustrt::rust_dbg_abi_1(q);
        println!("a: {:x}", qq.a as usize);
        println!("b: {:x}", qq.b as usize);
        println!("c: {:x}", qq.c as usize);
        println!("d: {:x}", qq.d as usize);
        assert_eq!(qq.a, q.c + 1);
        assert_eq!(qq.b, q.d - 1);
        assert_eq!(qq.c, q.a + 1);
        assert_eq!(qq.d, q.b - 1);
    }
}

#[cfg(target_pointer_width = "64")]
fn test2() {
    unsafe {
        let f = Floats { a: 1.234567890e-15_f64, b: 0b_1010_1010, c: 1.0987654321e-15_f64 };
        let ff = rustrt::rust_dbg_abi_2(f);
        println!("a: {}", ff.a as f64);
        println!("b: {}", ff.b as usize);
        println!("c: {}", ff.c as f64);
        assert_eq!(ff.a, f.c + 1.0f64);
        assert_eq!(ff.b, 0xff);
        assert_eq!(ff.c, f.a - 1.0f64);
    }
}

#[cfg(target_pointer_width = "32")]
fn test2() {}

#[cfg(target_pointer_width = "64")]
fn test3() {
    unsafe {
        let a = CharCharDouble { a: 1, b: 2, c: 3. };
        let b = rustrt::rust_dbg_abi_3(a);
        println!("a: {}", b.a);
        println!("b: {}", b.b);
        println!("c: {}", b.c);
        assert_eq!(b.a, a.a + 1);
        assert_eq!(b.b, a.b - 1);
        assert_eq!(b.c, a.c + 1.0);
    }
}

#[cfg(target_pointer_width = "32")]
fn test3() {}

fn test4() {
    unsafe {
        let a = CharCharFloat { a: 1, b: 2, c: 3. };
        let b = rustrt::rust_dbg_abi_4(a);
        println!("a: {}", b.a);
        println!("b: {}", b.b);
        println!("c: {}", b.c);
        assert_eq!(b.a, a.a + 1);
        assert_eq!(b.b, a.b - 1);
        assert_eq!(b.c, a.c + 1.0);
    }
}

pub fn main() {
    test1();
    test2();
    test3();
    test4();
}