2 ** FFI C call handling.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
13 /* -- C calling conventions ----------------------------------------------- */
15 #if LJ_TARGET_X86ORX64
18 #define CCALL_NARG_GPR 2 /* For fastcall arguments. */
19 #define CCALL_NARG_FPR 0
20 #define CCALL_NRET_GPR 2
21 #define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */
22 #define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */
24 #define CCALL_NARG_GPR 4
25 #define CCALL_NARG_FPR 4
26 #define CCALL_NRET_GPR 1
27 #define CCALL_NRET_FPR 1
28 #define CCALL_SPS_EXTRA 4
30 #define CCALL_NARG_GPR 6
31 #define CCALL_NARG_FPR 8
32 #define CCALL_NRET_GPR 2
33 #define CCALL_NRET_FPR 2
34 #define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */
37 #define CCALL_SPS_FREE 1
39 typedef LJ_ALIGN(16) union FPRArg
{
48 typedef intptr_t GPRArg
;
52 #define CCALL_NARG_GPR 4
53 #define CCALL_NARG_FPR 0
54 #define CCALL_NRET_GPR 2 /* For softfp double. */
55 #define CCALL_NRET_FPR 0
56 #define CCALL_SPS_FREE 0
58 typedef intptr_t GPRArg
;
62 #define CCALL_NARG_GPR 8
63 #define CCALL_NARG_FPR 8
64 #define CCALL_NRET_GPR 4 /* For complex double. */
65 #define CCALL_NRET_FPR 1
66 #define CCALL_SPS_EXTRA 3
67 #define CCALL_SPS_FREE 1
69 typedef intptr_t GPRArg
;
70 typedef double FPRArg
;
72 #elif LJ_TARGET_PPCSPE
74 #define CCALL_NARG_GPR 8
75 #define CCALL_NARG_FPR 0
76 #define CCALL_NRET_GPR 4 /* For softfp complex double. */
77 #define CCALL_NRET_FPR 0
78 #define CCALL_SPS_FREE 0 /* NYI */
80 typedef intptr_t GPRArg
;
83 #error "missing calling convention definitions for this architecture"
86 #ifndef CCALL_SPS_EXTRA
87 #define CCALL_SPS_EXTRA 0
89 #ifndef CCALL_VECTOR_REG
90 #define CCALL_VECTOR_REG 0
92 #ifndef CCALL_ALIGN_STACKARG
93 #define CCALL_ALIGN_STACKARG 1
96 #define CCALL_NUM_GPR \
97 (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
98 #define CCALL_NUM_FPR \
99 (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
101 #define CCALL_MAXSTACK 32
103 /* -- C call state -------------------------------------------------------- */
105 typedef struct CCallState
{
106 void (*func
)(void); /* Pointer to called function. */
107 uint32_t spadj
; /* Stack pointer adjustment. */
108 uint8_t nsp
; /* Number of stack slots. */
109 uint8_t retref
; /* Return value by reference. */
111 uint8_t ngpr
; /* Number of arguments in GPRs. */
112 uint8_t nfpr
; /* Number of arguments in FPRs. */
114 uint8_t resx87
; /* Result on x87 stack: 1:float, 2:double. */
116 uint8_t nfpr
; /* Number of arguments in FPRs. */
122 FPRArg fpr
[CCALL_NUM_FPR
]; /* Arguments/results in FPRs. */
124 GPRArg gpr
[CCALL_NUM_GPR
]; /* Arguments/results in GPRs. */
125 GPRArg stack
[CCALL_MAXSTACK
]; /* Stack slots. */
128 /* -- C call handling ----------------------------------------------------- */
130 /* Really belongs to lj_vm.h. */
131 LJ_ASMF
void LJ_FASTCALL
lj_vm_ffi_call(CCallState
*cc
);
132 LJ_FUNC
int lj_ccall_func(lua_State
*L
, GCcdata
*cd
);