1 /* -----------------------------------------------------------------------
2 sysv.S - Copyright (c) 1998, 2008 Red Hat, Inc.
4 ARM Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include <fficonfig.h>
30 #ifdef HAVE_MACHINE_ASM_H
31 #include <machine/asm.h>
33 #ifdef __USER_LABEL_PREFIX__
34 #define CONCAT1(a, b) CONCAT2(a, b)
35 #define CONCAT2(a, b) a ## b
37 /* Use the right prefix for global labels. */
38 #define CNAME(x) CONCAT1 (__USER_LABEL_PREFIX__, x)
42 #define ENTRY(x) .globl CNAME(x); .type CNAME(x),%function; CNAME(x):
51 /* We need a better way of testing for this, but for now, this is all
53 @ This selects the minimum architecture level required.
54 #define __ARM_ARCH__ 3
56 #if defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__)
58 # define __ARM_ARCH__ 4
61 #if defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) \
62 || defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) \
63 || defined(__ARM_ARCH_5TEJ__)
65 # define __ARM_ARCH__ 5
68 #if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
69 || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) \
70 || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) \
71 || defined(__ARM_ARCH_6M__)
73 # define __ARM_ARCH__ 6
76 #if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
77 || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
78 || defined(__ARM_ARCH_7EM__)
80 # define __ARM_ARCH__ 7
84 # define call_reg(x) blx x
85 #elif defined (__ARM_ARCH_4T__)
86 # define call_reg(x) mov lr, pc ; bx x
87 # if defined(__thumb__) || defined(__THUMB_INTERWORK__)
88 # define __INTERWORKING__
91 # define call_reg(x) mov lr, pc ; mov pc, x
94 /* Conditionally compile unwinder directives. */
102 #if defined(__thumb__) && !defined(__THUMB_INTERWORK__)
103 .macro ARM_FUNC_START name
113 /* A hook to tell gdb that we've switched to ARM mode. Also used to call
114 directly from other local arm routines. */
118 .macro ARM_FUNC_START name
127 .macro RETLDM regs=, cond=, dirn=ia
128 #if defined (__INTERWORKING__)
130 ldr\cond lr, [sp], #4
132 ldm\cond\dirn sp!, {\regs, lr}
137 ldr\cond pc, [sp], #4
139 ldm\cond\dirn sp!, {\regs, pc}
151 @ This assumes we are using gas.
152 ARM_FUNC_START ffi_call_SYSV
154 stmfd sp!, {r0-r3, fp, lr}
155 UNWIND .save {r0-r3, fp, lr}
160 @ Make room for all of the new args.
163 @ Place all of the ffi_prep_args in position
167 @ Call ffi_prep_args(stack, &ecif)
170 @ move first 4 parameters in registers
174 sub lr, fp, sp @ cif->bytes == fp - sp
175 ldr ip, [fp] @ load fn() in advance
183 @ Remove the space we pushed for the args
186 @ Load r2 with the pointer to storage for the return value
189 @ Load r3 with the return type code
192 @ If the return value pointer is NULL, assume no return value.
197 cmp r3, #FFI_TYPE_INT
198 #if defined(__SOFTFP__) || defined(__ARM_EABI__)
199 cmpne r3, #FFI_TYPE_FLOAT
205 cmp r3, #FFI_TYPE_SINT64
206 #if defined(__SOFTFP__) || defined(__ARM_EABI__)
207 cmpne r3, #FFI_TYPE_DOUBLE
211 #if !defined(__SOFTFP__) && !defined(__ARM_EABI__)
215 cmp r3, #FFI_TYPE_FLOAT
219 @ return DOUBLE or LONGDOUBLE
220 cmp r3, #FFI_TYPE_DOUBLE
229 .size CNAME(ffi_call_SYSV),.ffi_call_SYSV_end-CNAME(ffi_call_SYSV)
238 ARM_FUNC_START ffi_call_VFP
240 stmfd sp!, {r0-r3, fp, lr}
241 UNWIND .save {r0-r3, fp, lr}
245 @ Make room for all of the new args.
248 @ Make room for loading VFP args
251 @ Place all of the ffi_prep_args in position
254 sub r2, fp, #64 @ VFP scratch space
256 @ Call ffi_prep_args(stack, &ecif, vfp_space)
259 @ Load VFP register args if needed
263 @ Load only d0 if possible
267 fldmiadgt ip, {d0-d7}
270 @ move first 4 parameters in registers
274 sub lr, ip, sp @ cif->bytes == (fp - 64) - sp
275 ldr ip, [fp] @ load fn() in advance
283 @ Remove the space we pushed for the args
286 @ Load r2 with the pointer to storage for
290 @ Load r3 with the return type code
293 @ If the return value pointer is NULL,
294 @ assume no return value.
296 beq LSYM(Lepilogue_vfp)
298 cmp r3, #FFI_TYPE_INT
300 beq LSYM(Lepilogue_vfp)
302 cmp r3, #FFI_TYPE_SINT64
304 beq LSYM(Lepilogue_vfp)
306 cmp r3, #FFI_TYPE_FLOAT
308 beq LSYM(Lepilogue_vfp)
310 cmp r3, #FFI_TYPE_DOUBLE
312 beq LSYM(Lepilogue_vfp)
314 cmp r3, #FFI_TYPE_STRUCT_VFP_FLOAT
315 cmpne r3, #FFI_TYPE_STRUCT_VFP_DOUBLE
316 fstmiadeq r2, {d0-d3}
323 .size CNAME(ffi_call_VFP),.ffi_call_VFP_end-CNAME(ffi_call_VFP)
327 unsigned int FFI_HIDDEN
328 ffi_closure_SYSV_inner (closure, respp, args)
329 ffi_closure *closure;
334 ARM_FUNC_START ffi_closure_SYSV
338 UNWIND .save {r0, lr}
344 bl ffi_closure_SYSV_inner
345 cmp r0, #FFI_TYPE_INT
348 cmp r0, #FFI_TYPE_FLOAT
349 #if defined(__SOFTFP__) || defined(__ARM_EABI__)
355 cmp r0, #FFI_TYPE_DOUBLE
356 #if defined(__SOFTFP__) || defined(__ARM_EABI__)
362 cmp r0, #FFI_TYPE_LONGDOUBLE
363 #if defined(__SOFTFP__) || defined(__ARM_EABI__)
369 cmp r0, #FFI_TYPE_SINT64
382 #if !defined(__SOFTFP__) && !defined(__ARM_EABI__)
394 .ffi_closure_SYSV_end:
396 .size CNAME(ffi_closure_SYSV),.ffi_closure_SYSV_end-CNAME(ffi_closure_SYSV)
399 ARM_FUNC_START ffi_closure_VFP
405 UNWIND .save {r0, lr}
412 bl ffi_closure_SYSV_inner
414 cmp r0, #FFI_TYPE_INT
417 cmp r0, #FFI_TYPE_FLOAT
420 cmp r0, #FFI_TYPE_DOUBLE
421 cmpne r0, #FFI_TYPE_LONGDOUBLE
424 cmp r0, #FFI_TYPE_SINT64
425 beq .Lretlonglong_vfp
427 cmp r0, #FFI_TYPE_STRUCT_VFP_FLOAT
428 beq .Lretfloat_struct_vfp
430 cmp r0, #FFI_TYPE_STRUCT_VFP_DOUBLE
431 beq .Lretdouble_struct_vfp
433 .Lclosure_epilogue_vfp:
439 b .Lclosure_epilogue_vfp
442 b .Lclosure_epilogue_vfp
445 b .Lclosure_epilogue_vfp
448 b .Lclosure_epilogue_vfp
449 .Lretfloat_struct_vfp:
451 b .Lclosure_epilogue_vfp
452 .Lretdouble_struct_vfp:
454 b .Lclosure_epilogue_vfp
456 .ffi_closure_VFP_end:
458 .size CNAME(ffi_closure_VFP),.ffi_closure_VFP_end-CNAME(ffi_closure_VFP)
460 #if defined __ELF__ && defined __linux__
461 .section .note.GNU-stack,"",%progbits