Debian Codegraph @

src/x86/win64.S

raw
1 #ifdef __x86_64__
2 #define LIBFFI_ASM
3 #include <fficonfig.h>
4 #include <ffi.h>
5 #include <ffi_cfi.h>
6 #include "asmnames.h"
7
8 #if defined(HAVE_AS_CFI_PSEUDO_OP)
9 .cfi_sections .debug_frame
10 #endif
11
12 #ifdef X86_WIN64
13 #define SEH(...) __VA_ARGS__
14 #define arg0 %rcx
15 #define arg1 %rdx
16 #define arg2 %r8
17 #define arg3 %r9
18 #else
19 #define SEH(...)
20 #define arg0 %rdi
21 #define arg1 %rsi
22 #define arg2 %rdx
23 #define arg3 %rcx
24 #endif
25
26 /* This macro allows the safe creation of jump tables without an
27 actual table. The entry points into the table are all 8 bytes.
28 The use of ORG asserts that we're at the correct location. */
29 /* ??? The clang assembler doesn't handle .org with symbolic expressions. */
30 #if defined(__clang__) || defined(__APPLE__) || (defined (__sun__) && defined(__svr4__))
31 # define E(BASE, X) .balign 8
32 #else
33 # define E(BASE, X) .balign 8; .org BASE + (X) * 8
34 #endif
35
36 .text
37
38 /* ffi_call_win64 (void *stack, struct win64_call_frame *frame, void *r10)
39
40 Bit o trickiness here -- FRAME is the base of the stack frame
41 for this function. This has been allocated by ffi_call. We also
42 deallocate some of the stack that has been alloca'd. */
43
44 .align 8
45 .globl C(ffi_call_win64)
46 FFI_HIDDEN(C(ffi_call_win64))
47
48 SEH(.seh_proc ffi_call_win64)
49 C(ffi_call_win64):
50 cfi_startproc
51 _CET_ENDBR
52 /* Set up the local stack frame and install it in rbp/rsp. */
53 movq (%rsp), %rax
54 movq %rbp, (arg1)
55 movq %rax, 8(arg1)
56 movq arg1, %rbp
57 cfi_def_cfa(%rbp, 16)
58 cfi_rel_offset(%rbp, 0)
59 SEH(.seh_pushreg %rbp)
60 SEH(.seh_setframe %rbp, 0)
61 SEH(.seh_endprologue)
62 movq arg0, %rsp
63
64 movq arg2, %r10
65
66 /* Load all slots into both general and xmm registers. */
67 movq (%rsp), %rcx
68 movsd (%rsp), %xmm0
69 movq 8(%rsp), %rdx
70 movsd 8(%rsp), %xmm1
71 movq 16(%rsp), %r8
72 movsd 16(%rsp), %xmm2
73 movq 24(%rsp), %r9
74 movsd 24(%rsp), %xmm3
75
76 call *16(%rbp)
77
78 movl 24(%rbp), %ecx
79 movq 32(%rbp), %r8
80 leaq 0f(%rip), %r10
81 cmpl $FFI_TYPE_SMALL_STRUCT_4B, %ecx
82 leaq (%r10, %rcx, 8), %r10
83 ja 99f
84 _CET_NOTRACK jmp *%r10
85
86 /* Below, we're space constrained most of the time. Thus we eschew the
87 modern "mov, pop, ret" sequence (5 bytes) for "leave, ret" (2 bytes). */
88 #define epilogue \
89 leaveq; \
90 cfi_remember_state; \
91 cfi_def_cfa(%rsp, 8); \
92 cfi_restore(%rbp); \
93 ret; \
94 cfi_restore_state
95
96 .align 8
97 0:
98 E(0b, FFI_TYPE_VOID)
99 epilogue
100 E(0b, FFI_TYPE_INT)
101 movslq %eax, %rax
102 movq %rax, (%r8)
103 epilogue
104 E(0b, FFI_TYPE_FLOAT)
105 movss %xmm0, (%r8)
106 epilogue
107 E(0b, FFI_TYPE_DOUBLE)
108 movsd %xmm0, (%r8)
109 epilogue
110 // FFI_TYPE_LONGDOUBLE may be FFI_TYPE_DOUBLE but we need a different value here.
111 E(0b, FFI_TYPE_DOUBLE + 1)
112 call PLT(C(abort))
113 E(0b, FFI_TYPE_UINT8)
114 movzbl %al, %eax
115 movq %rax, (%r8)
116 epilogue
117 E(0b, FFI_TYPE_SINT8)
118 movsbq %al, %rax
119 jmp 98f
120 E(0b, FFI_TYPE_UINT16)
121 movzwl %ax, %eax
122 movq %rax, (%r8)
123 epilogue
124 E(0b, FFI_TYPE_SINT16)
125 movswq %ax, %rax
126 jmp 98f
127 E(0b, FFI_TYPE_UINT32)
128 movl %eax, %eax
129 movq %rax, (%r8)
130 epilogue
131 E(0b, FFI_TYPE_SINT32)
132 movslq %eax, %rax
133 movq %rax, (%r8)
134 epilogue
135 E(0b, FFI_TYPE_UINT64)
136 98: movq %rax, (%r8)
137 epilogue
138 E(0b, FFI_TYPE_SINT64)
139 movq %rax, (%r8)
140 epilogue
141 E(0b, FFI_TYPE_STRUCT)
142 epilogue
143 E(0b, FFI_TYPE_POINTER)
144 movq %rax, (%r8)
145 epilogue
146 E(0b, FFI_TYPE_COMPLEX)
147 call PLT(C(abort))
148 E(0b, FFI_TYPE_SMALL_STRUCT_1B)
149 movb %al, (%r8)
150 epilogue
151 E(0b, FFI_TYPE_SMALL_STRUCT_2B)
152 movw %ax, (%r8)
153 epilogue
154 E(0b, FFI_TYPE_SMALL_STRUCT_4B)
155 movl %eax, (%r8)
156 epilogue
157
158 .align 8
159 99: call PLT(C(abort))
160
161 epilogue
162
163 cfi_endproc
164 SEH(.seh_endproc)
165
166
167 /* 32 bytes of outgoing register stack space, 8 bytes of alignment,
168 16 bytes of result, 32 bytes of xmm registers. */
169 #define ffi_clo_FS (32+8+16+32)
170 #define ffi_clo_OFF_R (32+8)
171 #define ffi_clo_OFF_X (32+8+16)
172
173 .align 8
174 .globl C(ffi_go_closure_win64)
175 FFI_HIDDEN(C(ffi_go_closure_win64))
176
177 SEH(.seh_proc ffi_go_closure_win64)
178 C(ffi_go_closure_win64):
179 cfi_startproc
180 _CET_ENDBR
181 /* Save all integer arguments into the incoming reg stack space. */
182 movq %rcx, 8(%rsp)
183 movq %rdx, 16(%rsp)
184 movq %r8, 24(%rsp)
185 movq %r9, 32(%rsp)
186
187 movq 8(%r10), %rcx /* load cif */
188 movq 16(%r10), %rdx /* load fun */
189 movq %r10, %r8 /* closure is user_data */
190 jmp 0f
191 cfi_endproc
192 SEH(.seh_endproc)
193
194 .align 8
195 .globl C(ffi_closure_win64)
196 FFI_HIDDEN(C(ffi_closure_win64))
197
198 SEH(.seh_proc ffi_closure_win64)
199 C(ffi_closure_win64):
200 cfi_startproc
201 _CET_ENDBR
202 /* Save all integer arguments into the incoming reg stack space. */
203 movq %rcx, 8(%rsp)
204 movq %rdx, 16(%rsp)
205 movq %r8, 24(%rsp)
206 movq %r9, 32(%rsp)
207
208 movq FFI_TRAMPOLINE_SIZE(%r10), %rcx /* load cif */
209 movq FFI_TRAMPOLINE_SIZE+8(%r10), %rdx /* load fun */
210 movq FFI_TRAMPOLINE_SIZE+16(%r10), %r8 /* load user_data */
211 0:
212 subq $ffi_clo_FS, %rsp
213 cfi_adjust_cfa_offset(ffi_clo_FS)
214 SEH(.seh_stackalloc ffi_clo_FS)
215 SEH(.seh_endprologue)
216
217 /* Save all sse arguments into the stack frame. */
218 movsd %xmm0, ffi_clo_OFF_X(%rsp)
219 movsd %xmm1, ffi_clo_OFF_X+8(%rsp)
220 movsd %xmm2, ffi_clo_OFF_X+16(%rsp)
221 movsd %xmm3, ffi_clo_OFF_X+24(%rsp)
222
223 leaq ffi_clo_OFF_R(%rsp), %r9
224 call PLT(C(ffi_closure_win64_inner))
225
226 /* Load the result into both possible result registers. */
227 movq ffi_clo_OFF_R(%rsp), %rax
228 movsd ffi_clo_OFF_R(%rsp), %xmm0
229
230 addq $ffi_clo_FS, %rsp
231 cfi_adjust_cfa_offset(-ffi_clo_FS)
232 ret
233
234 cfi_endproc
235 SEH(.seh_endproc)
236
237 #if defined(FFI_EXEC_STATIC_TRAMP)
238 .align 8
239 .globl C(ffi_closure_win64_alt)
240 FFI_HIDDEN(C(ffi_closure_win64_alt))
241
242 SEH(.seh_proc ffi_closure_win64_alt)
243 C(ffi_closure_win64_alt):
244 _CET_ENDBR
245 movq 8(%rsp), %r10
246 addq $16, %rsp
247 jmp C(ffi_closure_win64)
248 SEH(.seh_endproc)
249 #endif
250 #endif /* __x86_64__ */
251
252 #if defined __ELF__ && defined __linux__
253 .section .note.GNU-stack,"",@progbits
254 #endif