]> git.lizzy.rs Git - rust.git/blob - src/rt/arch/x86_64/morestack.S
rt: Fix __morestack prologue on x86_64 mac
[rust.git] / src / rt / arch / x86_64 / morestack.S
1     .text
2
3 // __morestack
4 //
5 // LLVM generates a call to this to allocate more stack space in a functiono
6 // prolog when we run out.
7
8 #if defined(__APPLE__) || defined(_WIN32)
9 #define UPCALL_NEW_STACK        _upcall_new_stack
10 #define UPCALL_DEL_STACK        _upcall_del_stack
11 #define UPCALL_CALL_C           _upcall_call_shim_on_c_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 UPCALL_CALL_C           upcall_call_shim_on_c_stack
17 #define MORESTACK               __morestack
18 #endif
19
20         // Naturally, nobody can agree as to
21         // which arguments should go in which
22         // registers:
23 #if defined(_WIN32)
24 #  define ARG0 %rcx
25 #  define ARG1 %rdx
26 #  define ARG2 %r8
27 #else
28 #  define ARG0 %rdi
29 #  define ARG1 %rsi
30 #  define ARG2 %rdx
31 #endif
32
33 .globl UPCALL_NEW_STACK
34 .globl UPCALL_DEL_STACK
35 .globl UPCALL_CALL_C
36 .globl MORESTACK
37
38 // FIXME: What about _WIN32?    
39 #if defined(__linux__)
40         .hidden MORESTACK
41 #else
42 #if defined(__APPLE__)
43         .private_extern MORESTACK
44 #endif
45 #endif
46
47 #ifdef __ELF__
48         .type MORESTACK,@function
49 #endif
50
51 #if defined(__linux__) || defined(__APPLE__)
52 MORESTACK:
53 #if defined(__ELF__)
54         .cfi_startproc
55 #endif
56         
57         // Set up a normal backtrace
58         pushq %rbp
59 #if defined(__ELF__)
60         .cfi_def_cfa_offset 16
61         .cfi_offset %rbp, -16
62 #endif
63         movq %rsp, %rbp
64 #if defined(__ELF__)
65         .cfi_def_cfa_register %rbp
66 #endif
67
68         // FIXME: libgcc also saves rax. not sure if we need to
69
70         // Save argument registers
71         pushq   %rdi
72         pushq   %rsi
73         pushq   %rdx
74         pushq   %rcx
75         pushq   %r8
76         pushq   %r9
77
78         // Calculate the address of the stack arguments.
79         // We have the base pointer, __morestack's return address,
80         // and __morestack's caller's return address to skip
81         movq %rbp, %rcx
82         addq $24, %rcx  // Base pointer, return address x2
83
84         pushq %r11 // Size of stack arguments
85         pushq %rcx // Address of stack arguments
86         pushq %r10 // The amount of stack needed
87
88         movq UPCALL_NEW_STACK@GOTPCREL(%rip), %rsi
89         movq %rsp, %rdi
90 #ifdef __APPLE__
91         call UPCALL_CALL_C@GOTPCREL
92 #endif
93 #ifdef __linux__
94         call UPCALL_CALL_C@PLT
95 #endif
96
97         // Pop the new_stack_args struct
98         addq $24, %rsp
99
100         // Pop the saved arguments
101         popq %r9
102         popq %r8
103         popq %rcx
104         popq %rdx
105         popq %rsi
106         popq %rdi
107         
108         movq 8(%rbp),%r10       // Grab the return pointer.
109         incq %r10               // Skip past the `ret` in our parent frame
110         movq %rax,%rsp          // Switch to the new stack.
111
112         call *%r10              // Reenter the caller function
113
114         // Switch back to the rust stack
115         movq %rbp, %rsp
116
117         // Align the stack again
118         pushq $0
119         
120         movq UPCALL_DEL_STACK@GOTPCREL(%rip), %rsi
121         movq $0, %rdi
122 #ifdef __APPLE__
123         call UPCALL_CALL_C@GOTPCREL
124 #endif
125 #ifdef __linux__
126         call UPCALL_CALL_C@PLT
127 #endif
128
129         addq $8, %rsp
130         popq %rbp
131         ret
132         
133 #if defined(__ELF__)
134         .cfi_endproc
135 #endif
136
137 #else
138 MORESTACK:
139         ret
140 #endif