Cleanup various endianess issues in assembler backend.
[luajit-2.0.git] / src / lj_ccall.h
blobd9b1e42ca0a60e3ca8cccf3978827065abb1dd46
1 /*
2 ** FFI C call handling.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_CCALL_H
7 #define _LJ_CCALL_H
9 #include "lj_obj.h"
11 #if LJ_HASFFI
13 /* -- C calling conventions ----------------------------------------------- */
15 #if LJ_TARGET_X86ORX64
17 #if LJ_TARGET_X86
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. */
23 #elif LJ_ABI_WIN
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
29 #else
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. */
35 #endif
37 #define CCALL_SPS_FREE 1
39 typedef LJ_ALIGN(16) union FPRArg {
40 double d[2];
41 float f[4];
42 uint8_t b[16];
43 uint16_t s[8];
44 int i[4];
45 int64_t l[2];
46 } FPRArg;
48 typedef intptr_t GPRArg;
50 #elif LJ_TARGET_ARM
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;
60 #elif LJ_TARGET_PPC
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;
82 #else
83 #error "missing calling convention definitions for this architecture"
84 #endif
86 #ifndef CCALL_SPS_EXTRA
87 #define CCALL_SPS_EXTRA 0
88 #endif
89 #ifndef CCALL_VECTOR_REG
90 #define CCALL_VECTOR_REG 0
91 #endif
92 #ifndef CCALL_ALIGN_STACKARG
93 #define CCALL_ALIGN_STACKARG 1
94 #endif
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. */
110 #if LJ_TARGET_X64
111 uint8_t ngpr; /* Number of arguments in GPRs. */
112 uint8_t nfpr; /* Number of arguments in FPRs. */
113 #elif LJ_TARGET_X86
114 uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */
115 #elif LJ_TARGET_PPC
116 uint8_t nfpr; /* Number of arguments in FPRs. */
117 #endif
118 #if CCALL_NUM_FPR
119 #if LJ_32
120 int32_t align1;
121 #endif
122 FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */
123 #endif
124 GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */
125 GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */
126 } CCallState;
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);
134 #endif
136 #endif