]> git.lizzy.rs Git - rust.git/blob - src/rt/arch/x86_64/morestack.S
Try removing code marked with "I don't think this is necessary"
[rust.git] / src / rt / arch / x86_64 / morestack.S
1 /*
2         __morestack
3
4         See i386/morestack.S for the lengthy, general explanation.
5 */
6
7 .text
8
9 #if defined(__APPLE__) || defined(_WIN32)
10 #define UPCALL_NEW_STACK        _upcall_new_stack
11 #define UPCALL_DEL_STACK        _upcall_del_stack
12 #define MORESTACK               ___morestack
13 #else
14 #define UPCALL_NEW_STACK        upcall_new_stack
15 #define UPCALL_DEL_STACK        upcall_del_stack
16 #define MORESTACK               __morestack
17 #endif
18
19 .globl UPCALL_NEW_STACK
20 .globl UPCALL_DEL_STACK
21 .globl MORESTACK
22
23 #if defined(__linux__) || defined(__FreeBSD__)
24         .hidden MORESTACK
25 #else
26 #if defined(__APPLE__)
27         .private_extern MORESTACK
28 #endif
29 #endif
30
31 #ifdef __ELF__
32         .type MORESTACK,@function
33 #endif
34
35
36 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
37 MORESTACK:
38         .cfi_startproc
39
40         pushq %rbp
41         // The CFA is 24 bytes above the register that it will
42         // be associated with for this frame (%rbp). That is 8
43         // bytes greater than a normal frame, to allow the unwinder
44         // to skip the partial frame of the original function.
45         .cfi_def_cfa_offset 24
46         // %rbp is -24 bytes from the CFA
47         .cfi_offset %rbp, -24
48         movq %rsp, %rbp
49         // Calculate the CFA as on offset from %ebp
50         .cfi_def_cfa_register %rbp
51
52         subq $184, %rsp
53
54         // FIXME: libgcc also saves rax. not sure if we need to (#2685)
55
56         // Save argument registers of the original function
57         movq %rdi,       (%rsp)
58         movq %rsi,      8(%rsp)
59         movq %rdx,     16(%rsp)
60         movq %rcx,     24(%rsp)
61         movq %r8,      32(%rsp)
62         movq %r9,      40(%rsp)
63         movdqa %xmm0,  48(%rsp)
64         movdqa %xmm1,  64(%rsp)
65         movdqa %xmm2,  80(%rsp)
66         movdqa %xmm3,  96(%rsp)
67         movdqa %xmm4, 112(%rsp)
68         movdqa %xmm5, 128(%rsp)
69         movdqa %xmm6, 144(%rsp)
70         movdqa %xmm7, 160(%rsp)
71
72         // Calculate the address of the stack arguments.
73         // We have the base pointer, __morestack's return address,
74         // and __morestack's caller's return address to skip
75         movq %rbp, %rax
76         addq $24, %rax  // Base pointer, return address x2
77
78         // The arguments to __morestack are passed in %r10 & %r11
79
80         movq %r11, %rdx // Size of stack arguments
81         movq %rax, %rsi // Address of stack arguments
82         movq %r10, %rdi // The amount of stack needed
83         
84 #ifdef __APPLE__
85         call UPCALL_NEW_STACK
86 #endif
87 #ifdef __linux__
88         call UPCALL_NEW_STACK@PLT
89 #endif
90 #ifdef __FreeBSD__
91         call UPCALL_NEW_STACK@PLT
92 #endif
93
94         // Pop the saved arguments
95         movq      (%rsp), %rdi
96         movq     8(%rsp), %rsi
97         movq    16(%rsp), %rdx
98         movq    24(%rsp), %rcx
99         movq    32(%rsp), %r8
100         movq    40(%rsp), %r9
101         movdqa  48(%rsp), %xmm0
102         movdqa  64(%rsp), %xmm1
103         movdqa  80(%rsp), %xmm2
104         movdqa  96(%rsp), %xmm3
105         movdqa 112(%rsp), %xmm4
106         movdqa 128(%rsp), %xmm5
107         movdqa 144(%rsp), %xmm6
108         movdqa 160(%rsp), %xmm7
109
110         addq $184, %rsp
111
112         movq 8(%rbp),%r10       // Grab the return pointer.
113         incq %r10               // Skip past the `ret` in our parent frame
114         movq %rax,%rsp          // Switch to the new stack.
115
116         call *%r10              // Reenter the caller function
117
118         // Switch back to the rust stack
119         movq %rbp, %rsp
120
121         // Save the return value
122         pushq %rax
123
124 #ifdef __APPLE__
125         call UPCALL_DEL_STACK
126 #endif
127 #ifdef __linux__
128         call UPCALL_DEL_STACK@PLT
129 #endif
130 #ifdef __FreeBSD__
131         call UPCALL_DEL_STACK@PLT
132 #endif
133
134         popq %rax // Restore the return value
135         popq %rbp
136         ret
137         
138         .cfi_endproc
139
140 #else
141 MORESTACK:
142         ret
143 #endif