Fix OSX build issues. Bump minimum required OSX version to 10.4
[luajit-2.0.git] / src / lj_ccall.h
blob5985c4a9893aca4add956cb952948d68b2383448
1 /*
2 ** FFI C call handling.
3 ** Copyright (C) 2005-2012 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
40 typedef LJ_ALIGN(16) union FPRArg {
41 double d[2];
42 float f[4];
43 uint8_t b[16];
44 uint16_t s[8];
45 int i[4];
46 int64_t l[2];
47 } FPRArg;
49 typedef intptr_t GPRArg;
51 #elif LJ_TARGET_ARM
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;
61 #elif LJ_TARGET_PPC
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;
83 #elif LJ_TARGET_MIPS
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 {
94 double d;
95 struct { LJ_ENDIAN_LOHI(float f; , float g;) };
96 } FPRArg;
98 #else
99 #error "Missing calling convention definitions for this architecture"
100 #endif
102 #ifndef CCALL_SPS_EXTRA
103 #define CCALL_SPS_EXTRA 0
104 #endif
105 #ifndef CCALL_VECTOR_REG
106 #define CCALL_VECTOR_REG 0
107 #endif
108 #ifndef CCALL_ALIGN_STACKARG
109 #define CCALL_ALIGN_STACKARG 1
110 #endif
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. */
130 #if LJ_TARGET_X64
131 uint8_t ngpr; /* Number of arguments in GPRs. */
132 uint8_t nfpr; /* Number of arguments in FPRs. */
133 #elif LJ_TARGET_X86
134 uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */
135 #elif LJ_TARGET_PPC
136 uint8_t nfpr; /* Number of arguments in FPRs. */
137 #endif
138 #if CCALL_NUM_FPR
139 #if LJ_32
140 int32_t align1;
141 #endif
142 FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */
143 #endif
144 GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */
145 GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */
146 } CCallState;
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);
156 #endif
158 #endif