]> git.lizzy.rs Git - rust.git/blob - src/rt/arch/x86_64/morestack.S
rt: Remove some duplicated code from 64-bit __morestack
[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         .cfi_startproc
54         
55         // Set up a normal backtrace
56         pushq %rbp
57         .cfi_def_cfa_offset 16
58         .cfi_offset %rbp, -16
59         movq %rsp, %rbp
60         .cfi_def_cfa_register %rbp
61
62         // During unwinding we want to skip our caller since it's not
63         // a complete frame and will make the unwinder sad
64         // Don't understand this line
65         .cfi_offset 16, 0
66         // Tell the unwinding where to get the stack pointer for
67         // our grandparent frame
68         .cfi_offset %rsp, -24
69
70         // Save the grandparent stack pointer for the unwinder
71         leaq 16(%rbp), %rax
72         pushq %rax
73
74         // FIXME: libgcc also saves rax. not sure if we need to
75
76         // Save argument registers
77         pushq   %rdi
78         pushq   %rsi
79         pushq   %rdx
80         pushq   %rcx
81         pushq   %r8
82         pushq   %r9
83
84         // Calculate the address of the stack arguments.
85         // We have the base pointer, __morestack's return address,
86         // and __morestack's caller's return address to skip
87         movq %rbp, %rcx
88         addq $24, %rcx  // Base pointer, return address x2
89
90         pushq %r11 // Size of stack arguments
91         pushq %rcx // Address of stack arguments
92         pushq %r10 // The amount of stack needed
93         pushq $0   // Out pointer
94
95         movq UPCALL_NEW_STACK@GOTPCREL(%rip), %rsi
96         movq %rsp, %rdi
97 #ifdef __APPLE__
98         call UPCALL_CALL_C
99 #endif
100 #ifdef __linux__
101         call UPCALL_CALL_C@PLT
102 #endif
103
104         // Pop the new_stack_args struct
105         popq %rax
106         addq $24, %rsp
107
108         // Pop the saved arguments
109         popq %r9
110         popq %r8
111         popq %rcx
112         popq %rdx
113         popq %rsi
114         popq %rdi
115
116         // Pop the unwinding %rsp
117         addq $8, %rsp
118
119         movq 8(%rbp),%r10       // Grab the return pointer.
120         incq %r10               // Skip past the `ret` in our parent frame
121         movq %rax,%rsp          // Switch to the new stack.
122
123         call *%r10              // Reenter the caller function
124
125         // Switch back to the rust stack
126         movq %rbp, %rsp
127
128         // Align the stack again
129         pushq $0
130         
131         movq UPCALL_DEL_STACK@GOTPCREL(%rip), %rsi
132         movq $0, %rdi
133 #ifdef __APPLE__
134         call UPCALL_CALL_C
135 #endif
136 #ifdef __linux__
137         call UPCALL_CALL_C@PLT
138 #endif
139
140         addq $8, %rsp
141         popq %rbp
142         .cfi_restore %rbp
143         .cfi_def_cfa %rsp, 8
144         ret
145         
146         .cfi_endproc
147
148 #else
149 MORESTACK:
150         ret
151 #endif