2 ** FFI C call handling.
3 ** Copyright (C) 2005-2012 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
;
85 #define CCALL_NARG_GPR 4
86 #define CCALL_NARG_FPR 2
87 #define CCALL_NRET_GPR 2
88 #define CCALL_NRET_FPR 2
89 #define CCALL_SPS_EXTRA 7
90 #define CCALL_SPS_FREE 1
92 typedef intptr_t GPRArg
;
93 typedef union FPRArg
{
95 struct { LJ_ENDIAN_LOHI(float f
; , float g
;) };
99 #error "Missing calling convention definitions for this architecture"
102 #ifndef CCALL_SPS_EXTRA
103 #define CCALL_SPS_EXTRA 0
105 #ifndef CCALL_VECTOR_REG
106 #define CCALL_VECTOR_REG 0
108 #ifndef CCALL_ALIGN_STACKARG
109 #define CCALL_ALIGN_STACKARG 1
112 #define CCALL_NUM_GPR \
113 (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
114 #define CCALL_NUM_FPR \
115 (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
117 /* Check against constants in lj_ctype.h. */
118 LJ_STATIC_ASSERT(CCALL_NUM_GPR
<= CCALL_MAX_GPR
);
119 LJ_STATIC_ASSERT(CCALL_NUM_FPR
<= CCALL_MAX_FPR
);
121 #define CCALL_MAXSTACK 32
123 /* -- C call state -------------------------------------------------------- */
125 typedef struct CCallState
{
126 void (*func
)(void); /* Pointer to called function. */
127 uint32_t spadj
; /* Stack pointer adjustment. */
128 uint8_t nsp
; /* Number of stack slots. */
129 uint8_t retref
; /* Return value by reference. */
131 uint8_t ngpr
; /* Number of arguments in GPRs. */
132 uint8_t nfpr
; /* Number of arguments in FPRs. */
134 uint8_t resx87
; /* Result on x87 stack: 1:float, 2:double. */
136 uint8_t nfpr
; /* Number of arguments in FPRs. */
142 FPRArg fpr
[CCALL_NUM_FPR
]; /* Arguments/results in FPRs. */
144 GPRArg gpr
[CCALL_NUM_GPR
]; /* Arguments/results in GPRs. */
145 GPRArg stack
[CCALL_MAXSTACK
]; /* Stack slots. */
148 /* -- C call handling ----------------------------------------------------- */
150 /* Really belongs to lj_vm.h. */
151 LJ_ASMF
void LJ_FASTCALL
lj_vm_ffi_call(CCallState
*cc
);
153 LJ_FUNC CTypeID
lj_ccall_ctid_vararg(CTState
*cts
, cTValue
*o
);
154 LJ_FUNC
int lj_ccall_func(lua_State
*L
, GCcdata
*cd
);