2 ** FFI C call handling.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
14 /* -- C calling conventions ----------------------------------------------- */
16 #if LJ_TARGET_X86ORX64
19 #define CCALL_NARG_GPR 2 /* For fastcall arguments. */
20 #define CCALL_NARG_FPR 0
21 #define CCALL_NRET_GPR 2
22 #define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */
23 #define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */
25 #define CCALL_NARG_GPR 4
26 #define CCALL_NARG_FPR 4
27 #define CCALL_NRET_GPR 1
28 #define CCALL_NRET_FPR 1
29 #define CCALL_SPS_EXTRA 4
31 #define CCALL_NARG_GPR 6
32 #define CCALL_NARG_FPR 8
33 #define CCALL_NRET_GPR 2
34 #define CCALL_NRET_FPR 2
35 #define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */
38 #define CCALL_SPS_FREE 1
39 #define CCALL_ALIGN_CALLSTATE 16
41 typedef LJ_ALIGN(16) union FPRArg
{
50 typedef intptr_t GPRArg
;
54 #define CCALL_NARG_GPR 4
55 #define CCALL_NRET_GPR 2 /* For softfp double. */
57 #define CCALL_NARG_FPR 0
58 #define CCALL_NRET_FPR 0
60 #define CCALL_NARG_FPR 8
61 #define CCALL_NRET_FPR 4
63 #define CCALL_SPS_FREE 0
65 typedef intptr_t GPRArg
;
66 typedef union FPRArg
{
73 #define CCALL_NARG_GPR 8
74 #define CCALL_NRET_GPR 2
75 #define CCALL_NARG_FPR 8
76 #define CCALL_NRET_FPR 4
77 #define CCALL_SPS_FREE 0
79 #define CCALL_PACK_STACKARG 1
82 typedef intptr_t GPRArg
;
83 typedef union FPRArg
{
85 struct { LJ_ENDIAN_LOHI(float f
; , float g
;) };
86 struct { LJ_ENDIAN_LOHI(uint32_t lo
; , uint32_t hi
;) };
91 #define CCALL_NARG_GPR 8
92 #define CCALL_NARG_FPR (LJ_ABI_SOFTFP ? 0 : 8)
93 #define CCALL_NRET_GPR 4 /* For complex double. */
94 #define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 1)
95 #define CCALL_SPS_EXTRA 4
96 #define CCALL_SPS_FREE 0
98 typedef intptr_t GPRArg
;
99 typedef double FPRArg
;
101 #elif LJ_TARGET_MIPS32
103 #define CCALL_NARG_GPR 4
104 #define CCALL_NARG_FPR (LJ_ABI_SOFTFP ? 0 : 2)
105 #define CCALL_NRET_GPR (LJ_ABI_SOFTFP ? 4 : 2)
106 #define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 2)
107 #define CCALL_SPS_EXTRA 7
108 #define CCALL_SPS_FREE 1
110 typedef intptr_t GPRArg
;
111 typedef union FPRArg
{
113 struct { LJ_ENDIAN_LOHI(float f
; , float g
;) };
116 #elif LJ_TARGET_MIPS64
118 /* FP args are positional and overlay the GPR array. */
119 #define CCALL_NARG_GPR 8
120 #define CCALL_NARG_FPR 0
121 #define CCALL_NRET_GPR 2
122 #define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 2)
123 #define CCALL_SPS_EXTRA 3
124 #define CCALL_SPS_FREE 1
126 typedef intptr_t GPRArg
;
127 typedef union FPRArg
{
129 struct { LJ_ENDIAN_LOHI(float f
; , float g
;) };
133 #error "Missing calling convention definitions for this architecture"
136 #ifndef CCALL_SPS_EXTRA
137 #define CCALL_SPS_EXTRA 0
139 #ifndef CCALL_VECTOR_REG
140 #define CCALL_VECTOR_REG 0
142 #ifndef CCALL_ALIGN_STACKARG
143 #define CCALL_ALIGN_STACKARG 1
145 #ifndef CCALL_PACK_STACKARG
146 #define CCALL_PACK_STACKARG 0
148 #ifndef CCALL_ALIGN_CALLSTATE
149 #define CCALL_ALIGN_CALLSTATE 8
152 #define CCALL_NUM_GPR \
153 (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
154 #define CCALL_NUM_FPR \
155 (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
157 /* Check against constants in lj_ctype.h. */
158 LJ_STATIC_ASSERT(CCALL_NUM_GPR
<= CCALL_MAX_GPR
);
159 LJ_STATIC_ASSERT(CCALL_NUM_FPR
<= CCALL_MAX_FPR
);
161 #define CCALL_NUM_STACK 31
162 #define CCALL_SIZE_STACK (CCALL_NUM_STACK * CTSIZE_PTR)
164 /* -- C call state -------------------------------------------------------- */
166 typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE
) struct CCallState
{
167 void (*func
)(void); /* Pointer to called function. */
168 uint32_t spadj
; /* Stack pointer adjustment. */
169 uint8_t nsp
; /* Number of bytes on stack. */
170 uint8_t retref
; /* Return value by reference. */
172 uint8_t ngpr
; /* Number of arguments in GPRs. */
173 uint8_t nfpr
; /* Number of arguments in FPRs. */
175 uint8_t resx87
; /* Result on x87 stack: 1:float, 2:double. */
176 #elif LJ_TARGET_ARM64
177 void *retp
; /* Aggregate return pointer in x8. */
179 uint8_t nfpr
; /* Number of arguments in FPRs. */
185 FPRArg fpr
[CCALL_NUM_FPR
]; /* Arguments/results in FPRs. */
187 GPRArg gpr
[CCALL_NUM_GPR
]; /* Arguments/results in GPRs. */
188 GPRArg stack
[CCALL_NUM_STACK
]; /* Stack slots. */
191 /* -- C call handling ----------------------------------------------------- */
193 /* Really belongs to lj_vm.h. */
194 LJ_ASMF
void LJ_FASTCALL
lj_vm_ffi_call(CCallState
*cc
);
196 LJ_FUNC CTypeID
lj_ccall_ctid_vararg(CTState
*cts
, cTValue
*o
);
197 LJ_FUNC
int lj_ccall_func(lua_State
*L
, GCcdata
*cd
);