FFI: Don't assert on #1LL (5.2 compatibility mode only).
[luajit-2.0.git] / src / lj_ircall.h
blob44afde3ae6107ad3758ca5bea347e69f32248aee
1 /*
2 ** IR CALL* instruction definitions.
3 ** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #ifndef _LJ_IRCALL_H
7 #define _LJ_IRCALL_H
9 #include "lj_obj.h"
10 #include "lj_ir.h"
11 #include "lj_jit.h"
13 /* C call info for CALL* instructions. */
14 typedef struct CCallInfo {
15 ASMFunction func; /* Function pointer. */
16 uint32_t flags; /* Number of arguments and flags. */
17 } CCallInfo;
19 #define CCI_NARGS(ci) ((ci)->flags & 0xff) /* Extract # of args. */
20 #define CCI_NARGS_MAX 32 /* Max. # of args. */
22 #define CCI_OTSHIFT 16
23 #define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */
24 #define CCI_OPSHIFT 24
25 #define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */
27 #define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT)
28 #define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT)
29 #define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT)
30 #define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL)
31 #define CCI_CALL_FL (CCI_CALL_L|CCI_CC_FASTCALL)
32 #define CCI_CALL_FS (CCI_CALL_S|CCI_CC_FASTCALL)
34 /* C call info flags. */
35 #define CCI_L 0x0100 /* Implicit L arg. */
36 #define CCI_CASTU64 0x0200 /* Cast u64 result to number. */
37 #define CCI_NOFPRCLOBBER 0x0400 /* Does not clobber any FPRs. */
38 #define CCI_VARARG 0x0800 /* Vararg function. */
40 #define CCI_CC_MASK 0x3000 /* Calling convention mask. */
41 #define CCI_CC_SHIFT 12
42 /* ORDER CC */
43 #define CCI_CC_CDECL 0x0000 /* Default cdecl calling convention. */
44 #define CCI_CC_THISCALL 0x1000 /* Thiscall calling convention. */
45 #define CCI_CC_FASTCALL 0x2000 /* Fastcall calling convention. */
46 #define CCI_CC_STDCALL 0x3000 /* Stdcall calling convention. */
48 /* Helpers for conditional function definitions. */
49 #define IRCALLCOND_ANY(x) x
51 #if LJ_TARGET_X86ORX64
52 #define IRCALLCOND_FPMATH(x) NULL
53 #else
54 #define IRCALLCOND_FPMATH(x) x
55 #endif
57 #if LJ_SOFTFP
58 #define IRCALLCOND_SOFTFP(x) x
59 #if LJ_HASFFI
60 #define IRCALLCOND_SOFTFP_FFI(x) x
61 #else
62 #define IRCALLCOND_SOFTFP_FFI(x) NULL
63 #endif
64 #else
65 #define IRCALLCOND_SOFTFP(x) NULL
66 #define IRCALLCOND_SOFTFP_FFI(x) NULL
67 #endif
69 #define LJ_NEED_FP64 (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS)
71 #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
72 #define IRCALLCOND_FP64_FFI(x) x
73 #else
74 #define IRCALLCOND_FP64_FFI(x) NULL
75 #endif
77 #if LJ_HASFFI
78 #define IRCALLCOND_FFI(x) x
79 #if LJ_32
80 #define IRCALLCOND_FFI32(x) x
81 #else
82 #define IRCALLCOND_FFI32(x) NULL
83 #endif
84 #else
85 #define IRCALLCOND_FFI(x) NULL
86 #define IRCALLCOND_FFI32(x) NULL
87 #endif
89 #if LJ_SOFTFP
90 #define ARG1_FP 2 /* Treat as 2 32 bit arguments. */
91 #else
92 #define ARG1_FP 1
93 #endif
95 #if LJ_32
96 #define ARG2_64 4 /* Treat as 4 32 bit arguments. */
97 #else
98 #define ARG2_64 2
99 #endif
101 /* Function definitions for CALL* instructions. */
102 #define IRCALLDEF(_) \
103 _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \
104 _(ANY, lj_str_new, 3, S, STR, CCI_L) \
105 _(ANY, lj_strscan_num, 2, FN, INT, 0) \
106 _(ANY, lj_str_fromint, 2, FN, STR, CCI_L) \
107 _(ANY, lj_str_fromnum, 2, FN, STR, CCI_L) \
108 _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \
109 _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \
110 _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \
111 _(ANY, lj_tab_len, 1, FL, INT, 0) \
112 _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \
113 _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \
114 _(ANY, lj_mem_newgco, 2, FS, P32, CCI_L) \
115 _(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64) \
116 _(ANY, lj_vm_modi, 2, FN, INT, 0) \
117 _(ANY, sinh, ARG1_FP, N, NUM, 0) \
118 _(ANY, cosh, ARG1_FP, N, NUM, 0) \
119 _(ANY, tanh, ARG1_FP, N, NUM, 0) \
120 _(ANY, fputc, 2, S, INT, 0) \
121 _(ANY, fwrite, 4, S, INT, 0) \
122 _(ANY, fflush, 1, S, INT, 0) \
123 /* ORDER FPM */ \
124 _(FPMATH, lj_vm_floor, ARG1_FP, N, NUM, 0) \
125 _(FPMATH, lj_vm_ceil, ARG1_FP, N, NUM, 0) \
126 _(FPMATH, lj_vm_trunc, ARG1_FP, N, NUM, 0) \
127 _(FPMATH, sqrt, ARG1_FP, N, NUM, 0) \
128 _(FPMATH, exp, ARG1_FP, N, NUM, 0) \
129 _(FPMATH, lj_vm_exp2, ARG1_FP, N, NUM, 0) \
130 _(FPMATH, log, ARG1_FP, N, NUM, 0) \
131 _(FPMATH, lj_vm_log2, ARG1_FP, N, NUM, 0) \
132 _(FPMATH, log10, ARG1_FP, N, NUM, 0) \
133 _(FPMATH, sin, ARG1_FP, N, NUM, 0) \
134 _(FPMATH, cos, ARG1_FP, N, NUM, 0) \
135 _(FPMATH, tan, ARG1_FP, N, NUM, 0) \
136 _(FPMATH, lj_vm_powi, ARG1_FP+1, N, NUM, 0) \
137 _(FPMATH, pow, ARG1_FP*2, N, NUM, 0) \
138 _(FPMATH, atan2, ARG1_FP*2, N, NUM, 0) \
139 _(FPMATH, ldexp, ARG1_FP+1, N, NUM, 0) \
140 _(SOFTFP, lj_vm_tobit, 2, N, INT, 0) \
141 _(SOFTFP, softfp_add, 4, N, NUM, 0) \
142 _(SOFTFP, softfp_sub, 4, N, NUM, 0) \
143 _(SOFTFP, softfp_mul, 4, N, NUM, 0) \
144 _(SOFTFP, softfp_div, 4, N, NUM, 0) \
145 _(SOFTFP, softfp_cmp, 4, N, NIL, 0) \
146 _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \
147 _(SOFTFP, softfp_d2i, 2, N, INT, 0) \
148 _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \
149 _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \
150 _(SOFTFP_FFI, softfp_d2ui, 2, N, INT, 0) \
151 _(SOFTFP_FFI, softfp_d2f, 2, N, FLOAT, 0) \
152 _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \
153 _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \
154 _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \
155 _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \
156 _(FP64_FFI, fp64_l2d, 2, N, NUM, 0) \
157 _(FP64_FFI, fp64_ul2d, 2, N, NUM, 0) \
158 _(FP64_FFI, fp64_l2f, 2, N, FLOAT, 0) \
159 _(FP64_FFI, fp64_ul2f, 2, N, FLOAT, 0) \
160 _(FP64_FFI, fp64_d2l, ARG1_FP, N, I64, 0) \
161 _(FP64_FFI, fp64_d2ul, ARG1_FP, N, U64, 0) \
162 _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \
163 _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \
164 _(FFI, lj_carith_divi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
165 _(FFI, lj_carith_divu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
166 _(FFI, lj_carith_modi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
167 _(FFI, lj_carith_modu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
168 _(FFI, lj_carith_powi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
169 _(FFI, lj_carith_powu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
170 _(FFI, lj_cdata_setfin, 2, FN, P32, CCI_L) \
171 _(FFI, strlen, 1, L, INTP, 0) \
172 _(FFI, memcpy, 3, S, PTR, 0) \
173 _(FFI, memset, 3, S, PTR, 0) \
174 _(FFI, lj_vm_errno, 0, S, INT, CCI_NOFPRCLOBBER) \
175 _(FFI32, lj_carith_mul64, ARG2_64, N, I64, CCI_NOFPRCLOBBER)
177 /* End of list. */
179 typedef enum {
180 #define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name,
181 IRCALLDEF(IRCALLENUM)
182 #undef IRCALLENUM
183 IRCALL__MAX
184 } IRCallID;
186 LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);
188 LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1];
190 /* Soft-float declarations. */
191 #if LJ_SOFTFP
192 #if LJ_TARGET_ARM
193 #define softfp_add __aeabi_dadd
194 #define softfp_sub __aeabi_dsub
195 #define softfp_mul __aeabi_dmul
196 #define softfp_div __aeabi_ddiv
197 #define softfp_cmp __aeabi_cdcmple
198 #define softfp_i2d __aeabi_i2d
199 #define softfp_d2i __aeabi_d2iz
200 #define softfp_ui2d __aeabi_ui2d
201 #define softfp_f2d __aeabi_f2d
202 #define softfp_d2ui __aeabi_d2uiz
203 #define softfp_d2f __aeabi_d2f
204 #define softfp_i2f __aeabi_i2f
205 #define softfp_ui2f __aeabi_ui2f
206 #define softfp_f2i __aeabi_f2iz
207 #define softfp_f2ui __aeabi_f2uiz
208 #define fp64_l2d __aeabi_l2d
209 #define fp64_ul2d __aeabi_ul2d
210 #define fp64_l2f __aeabi_l2f
211 #define fp64_ul2f __aeabi_ul2f
212 #if LJ_TARGET_IOS
213 #define fp64_d2l __fixdfdi
214 #define fp64_d2ul __fixunsdfdi
215 #define fp64_f2l __fixsfdi
216 #define fp64_f2ul __fixunssfdi
217 #else
218 #define fp64_d2l __aeabi_d2lz
219 #define fp64_d2ul __aeabi_d2ulz
220 #define fp64_f2l __aeabi_f2lz
221 #define fp64_f2ul __aeabi_f2ulz
222 #endif
223 #else
224 #error "Missing soft-float definitions for target architecture"
225 #endif
226 extern double softfp_add(double a, double b);
227 extern double softfp_sub(double a, double b);
228 extern double softfp_mul(double a, double b);
229 extern double softfp_div(double a, double b);
230 extern void softfp_cmp(double a, double b);
231 extern double softfp_i2d(int32_t a);
232 extern int32_t softfp_d2i(double a);
233 #if LJ_HASFFI
234 extern double softfp_ui2d(uint32_t a);
235 extern double softfp_f2d(float a);
236 extern uint32_t softfp_d2ui(double a);
237 extern float softfp_d2f(double a);
238 extern float softfp_i2f(int32_t a);
239 extern float softfp_ui2f(uint32_t a);
240 extern int32_t softfp_f2i(float a);
241 extern uint32_t softfp_f2ui(float a);
242 #endif
243 #endif
245 #if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP)
246 #ifdef __GNUC__
247 #define fp64_l2d __floatdidf
248 #define fp64_ul2d __floatundidf
249 #define fp64_l2f __floatdisf
250 #define fp64_ul2f __floatundisf
251 #define fp64_d2l __fixdfdi
252 #define fp64_d2ul __fixunsdfdi
253 #define fp64_f2l __fixsfdi
254 #define fp64_f2ul __fixunssfdi
255 #else
256 #error "Missing fp64 helper definitions for this compiler"
257 #endif
258 #endif
260 #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
261 extern double fp64_l2d(int64_t a);
262 extern double fp64_ul2d(uint64_t a);
263 extern float fp64_l2f(int64_t a);
264 extern float fp64_ul2f(uint64_t a);
265 extern int64_t fp64_d2l(double a);
266 extern uint64_t fp64_d2ul(double a);
267 extern int64_t fp64_f2l(float a);
268 extern uint64_t fp64_f2ul(float a);
269 #endif
271 #endif