about summary refs log tree commit diff
path: root/tests/assembly-llvm/reg-struct-return.rs
blob: b251d791d5136e7db4254691207a40d03f0cfe8a (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Tests that -Zreg-struct-return changes ABI for small struct returns
//! from hidden-pointer convention to register-return on x86_32.
//! This test covers:
//! * Callee side, verifying that the structs are correctly loaded into registers when
//!   `-Zreg-struct-return` is activated
//! * Caller side, verifying callers do receive returned structs in registers when
//!   `-Zreg-struct-return` is activated
//@ add-core-stubs
//@ assembly-output: emit-asm
//@ compile-flags: -O --target=i686-unknown-linux-gnu -Crelocation-model=static
//@ revisions: WITH WITHOUT
//@[WITH] compile-flags: -Zreg-struct-return
//@ needs-llvm-components: x86

#![feature(no_core)]
#![no_std]
#![no_core]
#![crate_type = "lib"]

extern crate minicore;
use minicore::*;

// Verifies ABI changes for small structs, where both fields fit into one register.
// WITH is expected to use register return, WITHOUT should use hidden pointer.
mod Small {
    struct SmallStruct {
        a: i8,
        b: i8,
    }

    unsafe extern "C" {
        fn small() -> SmallStruct;
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn small_callee() -> SmallStruct {
        // (42 << 8) | 42 = 10794

        // WITH-LABEL: small_callee
        // WITH: movw $10794, %ax
        // WITH: retl

        // WITHOUT-LABEL: small_callee
        // WITHOUT: movl 4(%esp), %e{{.*}}
        // WITHOUT: movw $10794, (%e{{.*}})
        // WITHOUT: retl $4
        SmallStruct { a: 42, b: 42 }
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn small_caller(dst: &mut SmallStruct) {
        // WITH-LABEL: small_caller
        // WITH: calll small
        // WITH: movw %ax, (%e{{.*}})

        // WITHOUT-LABEL: small_caller
        // WITHOUT: calll small
        // WITHOUT: movzwl {{.*}}(%esp), %e[[TMP:..]]
        // WITHOUT: movw %[[TMP]], (%e{{..}})
        *dst = small();
    }
}

// Verifies ABI changes for a struct of size 8, which is the maximum size
// for reg-struct-return.
// WITH is expected to still use register return, WITHOUT should use hidden
// pointer.
mod Pivot {
    struct PivotStruct {
        a: i32,
        b: i32,
    }

    unsafe extern "C" {
        fn pivot() -> PivotStruct;
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn pivot_callee() -> PivotStruct {
        // WITH-LABEL: pivot_callee
        // WITH: movl $42, %e{{.*}}
        // WITH: movl $42, %e{{.*}}
        // WITH: retl

        // WITHOUT-LABEL: pivot_callee
        // WITHOUT: movl 4(%esp), %e{{.*}}
        // WITHOUT-DAG: movl $42, (%e{{.*}})
        // WITHOUT-DAG: movl $42, 4(%e{{.*}})
        // WITHOUT: retl $4
        PivotStruct { a: 42, b: 42 }
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn pivot_caller(dst: &mut PivotStruct) {
        // WITH-LABEL: pivot_caller
        // WITH: calll pivot
        // WITH-DAG: movl %e{{.*}}, 4(%e{{.*}})
        // WITH-DAG: movl %e{{.*}}, (%e{{.*}})

        // WITHOUT-LABEL: pivot_caller
        // WITHOUT: calll pivot
        // WITHOUT: movsd {{.*}}(%esp), %[[TMP:xmm.]]
        // WITHOUT: movsd %[[TMP]], (%e{{..}})
        *dst = pivot();
    }
}

// Verifies ABI changes for a struct of size 12, which is larger than the
// maximum size for reg-struct-return (8 bytes).
// Here, the hidden pointer convention should be used even when `-Zreg-struct-return` is set.
mod Large {
    struct LargeStruct {
        a: i32,
        b: i32,
        c: i32,
    }

    unsafe extern "C" {
        fn large() -> LargeStruct;
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn large_callee() -> LargeStruct {
        // CHECK-LABEL: large_callee
        // CHECK: movl 4(%esp), %e{{.*}}
        // CHECK-DAG: movl $42, (%e{{.*}})
        // CHECK-DAG: movl $42, 4(%e{{.*}})
        // CHECK-DAG: movl $42, 8(%e{{.*}})
        // CHECK: retl $4
        LargeStruct { a: 42, b: 42, c: 42 }
    }

    #[unsafe(no_mangle)]
    pub unsafe extern "C" fn large_caller(dst: &mut LargeStruct) {
        // CHECK-LABEL: large_caller
        // CHECK: calll large
        // CHECK-DAG: movl   {{.*}}(%esp), %[[TMP1:e..]]
        // CHECK-DAG: movl  %[[TMP1]], {{.*}}(%e{{..}})
        // CHECK-DAG: movsd  {{.*}}(%esp), %[[TMP2:xmm.]]
        // CHECK-DAG: movsd  %[[TMP2]], {{.*}}(%e{{..}})
        *dst = large();
    }
}