2 ** FFI C call handling.
3 ** Copyright (C) 2005-2011 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
40 typedef LJ_ALIGN(16) union FPRArg
{
49 typedef intptr_t GPRArg
;
53 #define CCALL_NARG_GPR 4
54 #define CCALL_NARG_FPR 0
55 #define CCALL_NRET_GPR 2 /* For softfp double. */
56 #define CCALL_NRET_FPR 0
57 #define CCALL_SPS_FREE 0
59 typedef intptr_t GPRArg
;
63 #define CCALL_NARG_GPR 8
64 #define CCALL_NARG_FPR 8
65 #define CCALL_NRET_GPR 4 /* For complex double. */
66 #define CCALL_NRET_FPR 1
67 #define CCALL_SPS_EXTRA 4
68 #define CCALL_SPS_FREE 0
70 typedef intptr_t GPRArg
;
71 typedef double FPRArg
;
73 #elif LJ_TARGET_PPCSPE
75 #define CCALL_NARG_GPR 8
76 #define CCALL_NARG_FPR 0
77 #define CCALL_NRET_GPR 4 /* For softfp complex double. */
78 #define CCALL_NRET_FPR 0
79 #define CCALL_SPS_FREE 0 /* NYI */
81 typedef intptr_t GPRArg
;
84 #error "Missing calling convention definitions for this architecture"
87 #ifndef CCALL_SPS_EXTRA
88 #define CCALL_SPS_EXTRA 0
90 #ifndef CCALL_VECTOR_REG
91 #define CCALL_VECTOR_REG 0
93 #ifndef CCALL_ALIGN_STACKARG
94 #define CCALL_ALIGN_STACKARG 1
97 #define CCALL_NUM_GPR \
98 (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
99 #define CCALL_NUM_FPR \
100 (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
102 /* Check against constants in lj_ctype.h. */
103 LJ_STATIC_ASSERT(CCALL_NUM_GPR
<= CCALL_MAX_GPR
);
104 LJ_STATIC_ASSERT(CCALL_NUM_FPR
<= CCALL_MAX_FPR
);
106 #define CCALL_MAXSTACK 32
108 /* -- C call state -------------------------------------------------------- */
110 typedef struct CCallState
{
111 void (*func
)(void); /* Pointer to called function. */
112 uint32_t spadj
; /* Stack pointer adjustment. */
113 uint8_t nsp
; /* Number of stack slots. */
114 uint8_t retref
; /* Return value by reference. */
116 uint8_t ngpr
; /* Number of arguments in GPRs. */
117 uint8_t nfpr
; /* Number of arguments in FPRs. */
119 uint8_t resx87
; /* Result on x87 stack: 1:float, 2:double. */
121 uint8_t nfpr
; /* Number of arguments in FPRs. */
127 FPRArg fpr
[CCALL_NUM_FPR
]; /* Arguments/results in FPRs. */
129 GPRArg gpr
[CCALL_NUM_GPR
]; /* Arguments/results in GPRs. */
130 GPRArg stack
[CCALL_MAXSTACK
]; /* Stack slots. */
133 /* -- C call handling ----------------------------------------------------- */
135 /* Really belongs to lj_vm.h. */
136 LJ_ASMF
void LJ_FASTCALL
lj_vm_ffi_call(CCallState
*cc
);
138 LJ_FUNC CTypeID
lj_ccall_ctid_vararg(CTState
*cts
, cTValue
*o
);
139 LJ_FUNC
int lj_ccall_func(lua_State
*L
, GCcdata
*cd
);