Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_ccall.h
blob24646d90386c3ca7e0218f96cdbeaddb17276798
1 /*
2 ** FFI C call handling.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_CCALL_H
7 #define _LJ_CCALL_H
9 #include "lj_obj.h"
10 #include "lj_ctype.h"
12 #if LJ_HASFFI
14 /* -- C calling conventions ----------------------------------------------- */
16 #if LJ_TARGET_X86ORX64
18 #if LJ_TARGET_X86
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. */
24 #elif LJ_ABI_WIN
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
30 #else
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. */
36 #endif
38 #define CCALL_SPS_FREE 1
39 #define CCALL_ALIGN_CALLSTATE 16
41 typedef LJ_ALIGN(16) union FPRArg {
42 double d[2];
43 float f[4];
44 uint8_t b[16];
45 uint16_t s[8];
46 int i[4];
47 int64_t l[2];
48 } FPRArg;
50 typedef intptr_t GPRArg;
52 #elif LJ_TARGET_ARM
54 #define CCALL_NARG_GPR 4
55 #define CCALL_NRET_GPR 2 /* For softfp double. */
56 #if LJ_ABI_SOFTFP
57 #define CCALL_NARG_FPR 0
58 #define CCALL_NRET_FPR 0
59 #else
60 #define CCALL_NARG_FPR 8
61 #define CCALL_NRET_FPR 4
62 #endif
63 #define CCALL_SPS_FREE 0
65 typedef intptr_t GPRArg;
66 typedef union FPRArg {
67 double d;
68 float f[2];
69 } FPRArg;
71 #elif LJ_TARGET_ARM64
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
78 #if LJ_TARGET_OSX
79 #define CCALL_PACK_STACKARG 1
80 #endif
82 typedef intptr_t GPRArg;
83 typedef union FPRArg {
84 double d;
85 struct { LJ_ENDIAN_LOHI(float f; , float g;) };
86 struct { LJ_ENDIAN_LOHI(uint32_t lo; , uint32_t hi;) };
87 } FPRArg;
89 #elif LJ_TARGET_PPC
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 {
112 double d;
113 struct { LJ_ENDIAN_LOHI(float f; , float g;) };
114 } FPRArg;
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 {
128 double d;
129 struct { LJ_ENDIAN_LOHI(float f; , float g;) };
130 } FPRArg;
132 #else
133 #error "Missing calling convention definitions for this architecture"
134 #endif
136 #ifndef CCALL_SPS_EXTRA
137 #define CCALL_SPS_EXTRA 0
138 #endif
139 #ifndef CCALL_VECTOR_REG
140 #define CCALL_VECTOR_REG 0
141 #endif
142 #ifndef CCALL_ALIGN_STACKARG
143 #define CCALL_ALIGN_STACKARG 1
144 #endif
145 #ifndef CCALL_PACK_STACKARG
146 #define CCALL_PACK_STACKARG 0
147 #endif
148 #ifndef CCALL_ALIGN_CALLSTATE
149 #define CCALL_ALIGN_CALLSTATE 8
150 #endif
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. */
171 #if LJ_TARGET_X64
172 uint8_t ngpr; /* Number of arguments in GPRs. */
173 uint8_t nfpr; /* Number of arguments in FPRs. */
174 #elif LJ_TARGET_X86
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. */
178 #elif LJ_TARGET_PPC
179 uint8_t nfpr; /* Number of arguments in FPRs. */
180 #endif
181 #if LJ_32
182 int32_t align1;
183 #endif
184 #if CCALL_NUM_FPR
185 FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */
186 #endif
187 GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */
188 GPRArg stack[CCALL_NUM_STACK]; /* Stack slots. */
189 } CCallState;
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);
199 #endif
201 #endif